Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / tn3270 / ascii / map3270.c
blob3b5436427b70e8133482e98942d0df429989cbdd
1 /* $NetBSD: map3270.c,v 1.14 2006/04/30 23:42:06 christos Exp $ */
3 /*-
4 * Copyright (c) 1988 The Regents of the University of California.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)map3270.c 4.2 (Berkeley) 4/26/91";
36 #else
37 __RCSID("$NetBSD: map3270.c,v 1.14 2006/04/30 23:42:06 christos Exp $");
38 #endif
39 #endif /* not lint */
41 /* This program reads a description file, somewhat like /etc/termcap,
42 that describes the mapping between the current terminal's keyboard and
43 a 3270 keyboard.
45 #ifdef DOCUMENTATION_ONLY
46 /* here is a sample (very small) entry...
48 # this table is sensitive to position on a line. In particular,
49 # a terminal definition for a terminal is terminated whenever a
50 # (non-comment) line beginning in column one is found.
52 # this is an entry to map tvi924 to 3270 keys...
53 v8|tvi924|924|televideo model 924 {
54 pfk1 = '\E1';
55 pfk2 = '\E2';
56 clear = '^z'; # clear the screen
59 #endif /* DOCUMENTATION_ONLY */
61 #include <ctype.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
66 #define IsPrint(c) ((isprint((unsigned char)c) && !isspace((unsigned char)c)) || ((c) == ' '))
68 #include "state.h"
69 #include "map3270.h"
71 #include "../general/globals.h"
73 /* this is the list of types returned by the lex processor */
74 #define LEX_CHAR 400 /* plain unadorned character */
75 #define LEX_ESCAPED LEX_CHAR+1 /* escaped with \ */
76 #define LEX_CARETED LEX_ESCAPED+1 /* escaped with ^ */
77 #define LEX_END_OF_FILE LEX_CARETED+1 /* end of file encountered */
78 #define LEX_ILLEGAL LEX_END_OF_FILE+1 /* trailing escape character */
80 /* the following is part of our character set dependency... */
81 #define ESCAPE 0x1b
82 #define TAB 0x09
83 #define NEWLINE 0x0a
84 #define CARRIAGE_RETURN 0x0d
86 typedef struct {
87 int type; /* LEX_* - type of character */
88 int value; /* character this was */
89 } lexicon;
91 typedef struct {
92 int length; /* length of character string */
93 char array[500]; /* character string */
94 } stringWithLength;
96 #define panic(s) { fprintf(stderr, "%s", s); exit(1); }
98 static state firstentry = { 0, STATE_NULL, 0, 0 };
99 static state *headOfQueue = &firstentry;
101 /* the following is a primitive adm3a table, to be used when nothing
102 * else seems to be available.
105 #ifdef DEBUG
106 static int debug = 0; /* debug flag (for debuggin tables) */
107 #endif /* DEBUG */
109 static int (*GetTc)(char *);
110 static int doPaste = 1; /* should we have side effects */
111 static int picky = 0; /* do we complain of unknown functions? */
112 static char usePointer = 0; /* use pointer, or file */
113 static FILE *ourFile= 0;
114 static char *environPointer = 0;/* if non-zero, point to input
115 * string in core.
117 static char **whichkey = 0;
118 static char *keysgeneric[] = {
119 #include "default.map" /* Define the default default */
121 0, /* Terminate list of entries */
125 static int Empty = 1, /* is the unget lifo empty? */
126 Full = 0; /* is the unget lifo full? */
127 static lexicon lifo[200]; /* character stack for parser */
128 static int rp = 0, /* read pointer into lifo */
129 wp = 0; /* write pointer into lifo */
131 static int GetC(void);
132 static lexicon Get(void);
133 static void UnGet(lexicon);
134 static stringWithLength *GetQuotedString(void);
135 #ifdef NOTUSED
136 static stringWithLength *GetCharString(void);
137 #endif
138 static int GetCharacter(int);
139 #ifdef NOTUSED
140 static int GetString(char *);
141 #endif
142 static stringWithLength *GetAlphaMericString(void);
143 static lexicon EatToNL(void);
144 static void GetWS(void);
145 static void FreeState(state *);
146 static state *GetState(void);
147 static state *FindMatchAtThisLevel(state *, int);
148 static state *PasteEntry(state *, char *, int, char *);
149 static int GetInput(int, char *);
150 static int GetDefinition(void);
151 static int GetDefinitions(void);
152 static int GetBegin(void);
153 static int GetEnd(void);
154 static int GetName(void);
155 static int GetNames(void);
156 static int GetEntry0(void);
157 static int GetEntry(void);
159 static int
160 GetC()
162 int character;
164 if (usePointer) {
165 if ((*environPointer) == 0) {
167 * If we have reached the end of this string, go on to
168 * the next (if there is a next).
170 if (whichkey == 0) {
171 static char suffix = 'A'; /* From environment */
172 char envname[9];
174 (void) sprintf(envname, "MAP3270%c", suffix++);
175 environPointer = getenv(envname);
176 } else {
177 whichkey++; /* default map */
178 environPointer = *whichkey;
181 if (*environPointer) {
182 character = 0xff&*environPointer++;
183 } else {
184 character = EOF;
186 } else {
187 character = getc(ourFile);
189 return(character);
192 static lexicon
193 Get()
195 lexicon c;
196 lexicon *pC = &c;
197 int character;
199 if (!Empty) {
200 *pC = lifo[rp];
201 rp++;
202 if (rp == sizeof lifo/sizeof (lexicon)) {
203 rp = 0;
205 if (rp == wp) {
206 Empty = 1;
208 Full = 0;
209 } else {
210 character = GetC();
211 switch (character) {
212 case EOF:
213 pC->type = LEX_END_OF_FILE;
214 break;
215 case '^':
216 character = GetC();
217 if (!IsPrint(character)) {
218 pC->type = LEX_ILLEGAL;
219 } else {
220 pC->type = LEX_CARETED;
221 if (character == '?') {
222 character |= 0x40; /* rubout */
223 } else {
224 character &= 0x1f;
227 break;
228 case '\\':
229 character = GetC();
230 if (!IsPrint(character)) {
231 pC->type = LEX_ILLEGAL;
232 } else {
233 pC->type = LEX_ESCAPED;
234 switch (character) {
235 case 'E': case 'e':
236 character = ESCAPE;
237 break;
238 case 't':
239 character = TAB;
240 break;
241 case 'n':
242 character = NEWLINE;
243 break;
244 case 'r':
245 character = CARRIAGE_RETURN;
246 break;
247 default:
248 pC->type = LEX_ILLEGAL;
249 break;
252 break;
253 default:
254 if ((IsPrint(character)) || isspace(character)) {
255 pC->type = LEX_CHAR;
256 } else {
257 pC->type = LEX_ILLEGAL;
259 break;
261 pC->value = character;
263 return(*pC);
266 static void
267 UnGet(c)
268 lexicon c; /* character to unget */
270 if (Full) {
271 fprintf(stderr, "attempt to put too many characters in lifo\n");
272 panic("map3270");
273 /* NOTREACHED */
274 } else {
275 lifo[wp] = c;
276 wp++;
277 if (wp == sizeof lifo/sizeof (lexicon)) {
278 wp = 0;
280 if (wp == rp) {
281 Full = 1;
283 Empty = 0;
288 * Construct a control character sequence
289 * for a special character.
291 char *
292 uncontrol(c)
293 int c;
295 static char buf[3];
297 if (c == 0x7f)
298 return ("^?");
299 if (c == '\377') {
300 return "-1";
302 if (c >= 0x20) {
303 buf[0] = c;
304 buf[1] = 0;
305 } else {
306 buf[0] = '^';
307 buf[1] = '@'+c;
308 buf[2] = 0;
310 return (buf);
313 /* compare two strings, ignoring case */
316 ustrcmp(string1, string2)
317 char *string1;
318 char *string2;
320 int c1, c2;
322 while ((c1 = (unsigned char) *string1++) != 0) {
323 if (isupper(c1)) {
324 c1 = tolower(c1);
326 if (isupper(c2 = (unsigned char) *string2++)) {
327 c2 = tolower(c2);
329 if (c1 < c2) {
330 return(-1);
331 } else if (c1 > c2) {
332 return(1);
335 if (*string2) {
336 return(-1);
337 } else {
338 return(0);
343 static stringWithLength *
344 GetQuotedString()
346 lexicon lex;
347 static stringWithLength output = { 0 }; /* where return value is held */
348 char *pointer = output.array;
350 lex = Get();
351 if ((lex.type != LEX_CHAR) || (lex.value != '\'')) {
352 UnGet(lex);
353 return(0);
355 while (1) {
356 lex = Get();
357 if ((lex.type == LEX_CHAR) && (lex.value == '\'')) {
358 break;
360 if ((lex.type == LEX_CHAR) && !IsPrint(lex.value)) {
361 UnGet(lex);
362 return(0); /* illegal character in quoted string */
364 if (pointer >= output.array+sizeof output.array) {
365 return(0); /* too long */
367 *pointer++ = lex.value;
369 output.length = pointer-output.array;
370 return(&output);
373 #ifdef NOTUSED
374 static stringWithLength *
375 GetCharString()
377 lexicon lex;
378 static stringWithLength output;
379 char *pointer = output.array;
381 lex = Get();
383 while ((lex.type == LEX_CHAR) &&
384 !isspace(lex.value) && (lex.value != '=')) {
385 *pointer++ = lex.value;
386 lex = Get();
387 if (pointer >= output.array + sizeof output.array) {
388 return(0); /* too long */
391 UnGet(lex);
392 output.length = pointer-output.array;
393 return(&output);
395 #endif /* NOTUSED */
397 static int
398 GetCharacter(character)
399 int character; /* desired character */
401 lexicon lex;
403 lex = Get();
405 if ((lex.type != LEX_CHAR) || (lex.value != character)) {
406 UnGet(lex);
407 return(0);
409 return(1);
412 #ifdef NOTUSED
413 static int
414 GetString(string)
415 char *string; /* string to get */
417 lexicon lex;
419 while (*string) {
420 lex = Get();
421 if ((lex.type != LEX_CHAR) || (lex.value != *string&0xff)) {
422 UnGet(lex);
423 return(0); /* XXX restore to state on entry */
425 string++;
427 return(1);
429 #endif /* NOTUSED */
432 static stringWithLength *
433 GetAlphaMericString()
435 lexicon lex;
436 static stringWithLength output = { 0 };
437 char *pointer = output.array;
438 # define IsAlnum(c) (isalnum(c) || (c == '_') \
439 || (c == '-') || (c == '.'))
441 lex = Get();
443 if ((lex.type != LEX_CHAR) || !IsAlnum(lex.value)) {
444 UnGet(lex);
445 return(0);
448 while ((lex.type == LEX_CHAR) && IsAlnum(lex.value)) {
449 *pointer++ = lex.value;
450 lex = Get();
452 UnGet(lex);
453 *pointer = 0;
454 output.length = pointer-output.array;
455 return(&output);
459 /* eat up characters until a new line, or end of file. returns terminating
460 character.
463 static lexicon
464 EatToNL()
466 lexicon lex;
468 lex = Get();
470 while (!((lex.type != LEX_ESCAPED) && (lex.type != LEX_CARETED) &&
471 (lex.value == '\n')) && (!(lex.type == LEX_END_OF_FILE))) {
472 lex = Get();
474 if (lex.type != LEX_END_OF_FILE) {
475 return(Get());
476 } else {
477 return(lex);
482 static void
483 GetWS()
485 lexicon lex;
487 lex = Get();
489 while ((lex.type == LEX_CHAR) &&
490 (isspace(lex.value) || (lex.value == '#'))) {
491 if (lex.value == '#') {
492 lex = EatToNL();
493 } else {
494 lex = Get();
497 UnGet(lex);
500 static void
501 FreeState(pState)
502 state *pState;
504 free((char *)pState);
508 static state *
509 GetState()
511 state *pState;
513 pState = (state *) malloc(sizeof (state));
515 pState->result = STATE_NULL;
516 pState->next = 0;
518 return(pState);
522 static state *
523 FindMatchAtThisLevel(pState, character)
524 state *pState;
525 int character;
527 while (pState) {
528 if (pState->match == character) {
529 return(pState);
531 pState = pState->next;
533 return(0);
537 static state *
538 PasteEntry(head, string, count, identifier)
539 state *head; /* points to who should point here... */
540 char *string; /* which characters to paste */
541 int count; /* number of character to do */
542 char *identifier; /* for error messages */
544 state *pState, *other;
546 if (!doPaste) { /* flag to not have any side effects */
547 return((state *)1);
549 if (!count) {
550 return(head); /* return pointer to the parent */
552 if ((head->result != STATE_NULL) && (head->result != STATE_GOTO)) {
553 /* this means that a previously defined sequence is an initial
554 * part of this one.
556 fprintf(stderr, "Conflicting entries found when scanning %s\n",
557 identifier);
558 return(0);
560 # ifdef DEBUG
561 if (debug) {
562 fprintf(stderr, "%s", uncontrol(*string));
564 # endif /* DEBUG */
565 pState = GetState();
566 pState->match = *string;
567 if (head->result == STATE_NULL) {
568 head->result = STATE_GOTO;
569 head->address = pState;
570 other = pState;
571 } else { /* search for same character */
572 if ((other = FindMatchAtThisLevel(head->address, *string)) != 0) {
573 FreeState(pState);
574 } else {
575 pState->next = head->address;
576 head->address = pState;
577 other = pState;
580 return(PasteEntry(other, string+1, count-1, identifier));
583 static int
584 GetInput(tc, identifier)
585 int tc;
586 char *identifier; /* entry being parsed (for error messages) */
588 stringWithLength *outputString;
589 state *head;
590 state fakeQueue;
592 if (doPaste) {
593 head = headOfQueue; /* always points to level above this one */
594 } else {
595 head = &fakeQueue; /* don't have any side effects... */
598 if ((outputString = GetQuotedString()) == 0) {
599 return(0);
600 } else if (IsPrint(outputString->array[0])) {
601 fprintf(stderr,
602 "first character of sequence for %s is not a control type character\n",
603 identifier);
604 return(0);
605 } else {
606 if ((head = PasteEntry(head, outputString->array,
607 outputString->length, identifier)) == 0) {
608 return(0);
610 GetWS();
611 while ((outputString = GetQuotedString()) != 0) {
612 if ((head = PasteEntry(head, outputString->array,
613 outputString->length, identifier)) == 0) {
614 return(0);
616 GetWS();
619 if (!doPaste) {
620 return(1);
622 if ((head->result != STATE_NULL) && (head->result != tc)) {
623 /* this means that this sequence is an initial part
624 * of a previously defined one.
626 fprintf(stderr, "Conflicting entries found when scanning %s\n",
627 identifier);
628 return(0);
629 } else {
630 head->result = tc;
631 return(1); /* done */
635 static int
636 GetDefinition()
638 stringWithLength *string;
639 int Tc;
641 GetWS();
642 if ((string = GetAlphaMericString()) == 0) {
643 return(0);
645 string->array[string->length] = 0;
646 if (doPaste) {
647 if ((Tc = (*GetTc)(string->array)) == -1) {
648 if (picky) {
649 fprintf(stderr, "%s: unknown 3270 key identifier\n",
650 string->array);
652 Tc = STATE_NULL;
654 } else {
655 Tc = STATE_NULL; /* XXX ? */
657 GetWS();
658 if (!GetCharacter('=')) {
659 fprintf(stderr,
660 "Required equal sign after 3270 key identifier %s missing\n",
661 string->array);
662 return(0);
664 GetWS();
665 if (!GetInput(Tc, string->array)) {
666 fprintf(stderr, "Missing definition part for 3270 key %s\n",
667 string->array);
668 return(0);
669 } else {
670 GetWS();
671 while (GetCharacter('|')) {
672 # ifdef DEBUG
673 if (debug) {
674 fprintf(stderr, " or ");
676 # endif /* DEBUG */
677 GetWS();
678 if (!GetInput(Tc, string->array)) {
679 fprintf(stderr, "Missing definition part for 3270 key %s\n",
680 string->array);
681 return(0);
683 GetWS();
686 GetWS();
687 if (!GetCharacter(';')) {
688 fprintf(stderr, "Missing semi-colon for 3270 key %s\n", string->array);
689 return(0);
691 # ifdef DEBUG
692 if (debug) {
693 fprintf(stderr, ";\n");
695 # endif /* DEBUG */
696 return(1);
700 static int
701 GetDefinitions()
703 if (!GetDefinition()) {
704 return(0);
705 } else {
706 while (GetDefinition()) {
710 return(1);
713 static int
714 GetBegin()
716 GetWS();
717 if (!GetCharacter('{')) {
718 return(0);
720 return(1);
723 static int
724 GetEnd()
726 GetWS();
727 if (!GetCharacter('}')) {
728 return(0);
730 return(1);
733 static int
734 GetName()
736 if (!GetAlphaMericString()) {
737 return(0);
739 GetWS();
740 while (GetAlphaMericString()) {
741 GetWS();
743 return(1);
746 static int
747 GetNames()
749 GetWS();
750 if (!GetName()) {
751 return(0);
752 } else {
753 GetWS();
754 while (GetCharacter('|')) {
755 GetWS();
756 if (!GetName()) {
757 return(0);
761 return(1);
764 static int
765 GetEntry0()
767 if (!GetBegin()) {
768 fprintf(stderr, "no '{'\n");
769 return(0);
770 } else if (!GetDefinitions()) {
771 fprintf(stderr, "unable to parse the definitions\n");
772 return(0);
773 } else if (!GetEnd()) {
774 fprintf(stderr, "No '}' or scanning stopped early due to error.\n");
775 return(0);
776 } else {
777 /* done */
778 return(1);
783 static int
784 GetEntry()
786 if (!GetNames()) {
787 fprintf(stderr, "Invalid name field in entry.\n");
788 return(0);
789 } else {
790 return(GetEntry0());
794 /* position ourselves within a given filename to the entry for the current
795 * KEYBD (or TERM) variable
799 Position(filename, keybdPointer)
800 char *filename;
801 char *keybdPointer;
803 lexicon lex;
804 stringWithLength *name = 0;
805 stringWithLength *oldName;
806 # define Return(x) {doPaste = 1; return(x);}
808 doPaste = 0;
810 if ((ourFile = fopen(filename, "r")) == NULL) {
811 # if !defined(MSDOS)
812 fprintf(stderr, "Unable to open file %s\n", filename);
813 # endif /* !defined(MSDOS) */
814 Return(0);
816 lex = Get();
817 while (lex.type != LEX_END_OF_FILE) {
818 UnGet(lex);
819 /* now, find an entry that is our type. */
820 GetWS();
821 oldName = name;
822 if ((name = GetAlphaMericString()) != 0) {
823 if (!ustrcmp(name->array, keybdPointer)) {
824 /* need to make sure there is a name here... */
825 lex.type = LEX_CHAR;
826 lex.value = 'a';
827 UnGet(lex);
828 Return(1);
830 } else if (GetCharacter('|')) {
831 ; /* more names coming */
832 } else {
833 lex = Get();
834 UnGet(lex);
835 if (lex.type != LEX_END_OF_FILE) {
836 if (!GetEntry0()) { /* start of an entry */
837 fprintf(stderr,
838 "error was in entry for %s in file %s\n",
839 (oldName)? oldName->array:"(unknown)", filename);
840 Return(0);
844 lex = Get();
846 #if !defined(MSDOS)
847 fprintf(stderr, "Unable to find entry for %s in file %s\n", keybdPointer,
848 filename);
849 #endif /* !defined(MSDOS) */
850 Return(0);
853 char *
854 strsave(string)
855 char *string;
857 char *p;
859 p = malloc((unsigned int)strlen(string)+1);
860 if (p != 0) {
861 strcpy(p, string);
863 return(p);
868 * InitControl - our interface to the outside. What we should
869 * do is figure out keyboard (or terminal) type, set up file pointer
870 * (or string pointer), etc.
873 state *
874 InitControl(keybdPointer, pickyarg, translator)
875 char *keybdPointer;
876 int pickyarg; /* Should we be picky? */
877 int (*translator)(char *); /* Translates ascii string to integer */
879 int GotIt;
881 picky = pickyarg;
882 GetTc = translator;
884 if (keybdPointer == 0) {
885 keybdPointer = getenv("KEYBD");
887 if (keybdPointer == 0) {
888 keybdPointer = getenv("TERM");
892 * Some environments have getenv() return
893 * out of a static area. So, save the keyboard name.
895 if (keybdPointer) {
896 keybdPointer = strsave(keybdPointer);
898 environPointer = getenv("MAP3270");
899 if (keybdPointer && environPointer
900 && (environPointer[0] != '/')
901 #if defined(MSDOS)
902 && (environPointer[0] != '\\')
903 #endif /* defined(MSDOS) */
904 && (strncmp(keybdPointer, environPointer,
905 strlen(keybdPointer) != 0)
906 || (environPointer[strlen(keybdPointer)] != '{'))) /* } */
908 environPointer = 0;
911 if ((!environPointer)
912 #if defined(MSDOS)
913 || (*environPointer == '\\')
914 #endif /* defined(MSDOS) */
915 || (*environPointer == '/')) {
916 usePointer = 0;
917 GotIt = 0;
918 if (!keybdPointer) {
919 #if !defined(MSDOS)
920 fprintf(stderr, "%s%s%s%s",
921 "Neither the KEYBD environment variable nor the TERM ",
922 "environment variable\n(one of which is needed to determine ",
923 "the type of keyboard you are using)\n",
924 "is set. To set it, say 'setenv KEYBD <type>'\n");
925 #endif /* !defined(MSDOS) */
926 } else {
927 if (environPointer) {
928 GotIt = Position(environPointer, keybdPointer);
930 if (!GotIt) {
931 GotIt = Position("/usr/share/misc/map3270", keybdPointer);
934 if (!GotIt) {
935 if (environPointer) {
936 GotIt = Position(environPointer, "unknown");
938 if (!GotIt && keybdPointer) {
939 GotIt = Position("/usr/share/misc/map3270", keybdPointer);
942 if (!GotIt) {
943 #if !defined(MSDOS)
944 fprintf(stderr, "Using default key mappings.\n");
945 #endif /* !defined(MSDOS) */
946 usePointer = 1; /* flag use of non-file */
947 whichkey = keysgeneric;
948 environPointer = *whichkey; /* use default table */
950 } else {
951 usePointer = 1;
953 (void) GetEntry();
954 free(keybdPointer);
955 return(firstentry.address);