1 /* $NetBSD: map3270.c,v 1.14 2006/04/30 23:42:06 christos Exp $ */
4 * Copyright (c) 1988 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)map3270.c 4.2 (Berkeley) 4/26/91";
37 __RCSID("$NetBSD: map3270.c,v 1.14 2006/04/30 23:42:06 christos Exp $");
41 /* This program reads a description file, somewhat like /etc/termcap,
42 that describes the mapping between the current terminal's keyboard and
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 {
56 clear = '^z'; # clear the screen
59 #endif /* DOCUMENTATION_ONLY */
66 #define IsPrint(c) ((isprint((unsigned char)c) && !isspace((unsigned char)c)) || ((c) == ' '))
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... */
84 #define CARRIAGE_RETURN 0x0d
87 int type
; /* LEX_* - type of character */
88 int value
; /* character this was */
92 int length
; /* length of character string */
93 char array
[500]; /* character string */
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.
106 static int debug
= 0; /* debug flag (for debuggin tables) */
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
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);
136 static stringWithLength
*GetCharString(void);
138 static int GetCharacter(int);
140 static int GetString(char *);
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);
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).
171 static char suffix
= 'A'; /* From environment */
174 (void) sprintf(envname
, "MAP3270%c", suffix
++);
175 environPointer
= getenv(envname
);
177 whichkey
++; /* default map */
178 environPointer
= *whichkey
;
181 if (*environPointer
) {
182 character
= 0xff&*environPointer
++;
187 character
= getc(ourFile
);
202 if (rp
== sizeof lifo
/sizeof (lexicon
)) {
213 pC
->type
= LEX_END_OF_FILE
;
217 if (!IsPrint(character
)) {
218 pC
->type
= LEX_ILLEGAL
;
220 pC
->type
= LEX_CARETED
;
221 if (character
== '?') {
222 character
|= 0x40; /* rubout */
230 if (!IsPrint(character
)) {
231 pC
->type
= LEX_ILLEGAL
;
233 pC
->type
= LEX_ESCAPED
;
245 character
= CARRIAGE_RETURN
;
248 pC
->type
= LEX_ILLEGAL
;
254 if ((IsPrint(character
)) || isspace(character
)) {
257 pC
->type
= LEX_ILLEGAL
;
261 pC
->value
= character
;
268 lexicon c
; /* character to unget */
271 fprintf(stderr
, "attempt to put too many characters in lifo\n");
277 if (wp
== sizeof lifo
/sizeof (lexicon
)) {
288 * Construct a control character sequence
289 * for a special character.
313 /* compare two strings, ignoring case */
316 ustrcmp(string1
, string2
)
322 while ((c1
= (unsigned char) *string1
++) != 0) {
326 if (isupper(c2
= (unsigned char) *string2
++)) {
331 } else if (c1
> c2
) {
343 static stringWithLength
*
347 static stringWithLength output
= { 0 }; /* where return value is held */
348 char *pointer
= output
.array
;
351 if ((lex
.type
!= LEX_CHAR
) || (lex
.value
!= '\'')) {
357 if ((lex
.type
== LEX_CHAR
) && (lex
.value
== '\'')) {
360 if ((lex
.type
== LEX_CHAR
) && !IsPrint(lex
.value
)) {
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
;
374 static stringWithLength
*
378 static stringWithLength output
;
379 char *pointer
= output
.array
;
383 while ((lex
.type
== LEX_CHAR
) &&
384 !isspace(lex
.value
) && (lex
.value
!= '=')) {
385 *pointer
++ = lex
.value
;
387 if (pointer
>= output
.array
+ sizeof output
.array
) {
388 return(0); /* too long */
392 output
.length
= pointer
-output
.array
;
398 GetCharacter(character
)
399 int character
; /* desired character */
405 if ((lex
.type
!= LEX_CHAR
) || (lex
.value
!= character
)) {
415 char *string
; /* string to get */
421 if ((lex
.type
!= LEX_CHAR
) || (lex
.value
!= *string
&0xff)) {
423 return(0); /* XXX restore to state on entry */
432 static stringWithLength
*
433 GetAlphaMericString()
436 static stringWithLength output
= { 0 };
437 char *pointer
= output
.array
;
438 # define IsAlnum(c) (isalnum(c) || (c == '_') \
439 || (c == '-') || (c == '.'))
443 if ((lex
.type
!= LEX_CHAR
) || !IsAlnum(lex
.value
)) {
448 while ((lex
.type
== LEX_CHAR
) && IsAlnum(lex
.value
)) {
449 *pointer
++ = lex
.value
;
454 output
.length
= pointer
-output
.array
;
459 /* eat up characters until a new line, or end of file. returns terminating
470 while (!((lex
.type
!= LEX_ESCAPED
) && (lex
.type
!= LEX_CARETED
) &&
471 (lex
.value
== '\n')) && (!(lex
.type
== LEX_END_OF_FILE
))) {
474 if (lex
.type
!= LEX_END_OF_FILE
) {
489 while ((lex
.type
== LEX_CHAR
) &&
490 (isspace(lex
.value
) || (lex
.value
== '#'))) {
491 if (lex
.value
== '#') {
504 free((char *)pState
);
513 pState
= (state
*) malloc(sizeof (state
));
515 pState
->result
= STATE_NULL
;
523 FindMatchAtThisLevel(pState
, character
)
528 if (pState
->match
== character
) {
531 pState
= pState
->next
;
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 */
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
556 fprintf(stderr
, "Conflicting entries found when scanning %s\n",
562 fprintf(stderr
, "%s", uncontrol(*string
));
566 pState
->match
= *string
;
567 if (head
->result
== STATE_NULL
) {
568 head
->result
= STATE_GOTO
;
569 head
->address
= pState
;
571 } else { /* search for same character */
572 if ((other
= FindMatchAtThisLevel(head
->address
, *string
)) != 0) {
575 pState
->next
= head
->address
;
576 head
->address
= pState
;
580 return(PasteEntry(other
, string
+1, count
-1, identifier
));
584 GetInput(tc
, identifier
)
586 char *identifier
; /* entry being parsed (for error messages) */
588 stringWithLength
*outputString
;
593 head
= headOfQueue
; /* always points to level above this one */
595 head
= &fakeQueue
; /* don't have any side effects... */
598 if ((outputString
= GetQuotedString()) == 0) {
600 } else if (IsPrint(outputString
->array
[0])) {
602 "first character of sequence for %s is not a control type character\n",
606 if ((head
= PasteEntry(head
, outputString
->array
,
607 outputString
->length
, identifier
)) == 0) {
611 while ((outputString
= GetQuotedString()) != 0) {
612 if ((head
= PasteEntry(head
, outputString
->array
,
613 outputString
->length
, identifier
)) == 0) {
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",
631 return(1); /* done */
638 stringWithLength
*string
;
642 if ((string
= GetAlphaMericString()) == 0) {
645 string
->array
[string
->length
] = 0;
647 if ((Tc
= (*GetTc
)(string
->array
)) == -1) {
649 fprintf(stderr
, "%s: unknown 3270 key identifier\n",
655 Tc
= STATE_NULL
; /* XXX ? */
658 if (!GetCharacter('=')) {
660 "Required equal sign after 3270 key identifier %s missing\n",
665 if (!GetInput(Tc
, string
->array
)) {
666 fprintf(stderr
, "Missing definition part for 3270 key %s\n",
671 while (GetCharacter('|')) {
674 fprintf(stderr
, " or ");
678 if (!GetInput(Tc
, string
->array
)) {
679 fprintf(stderr
, "Missing definition part for 3270 key %s\n",
687 if (!GetCharacter(';')) {
688 fprintf(stderr
, "Missing semi-colon for 3270 key %s\n", string
->array
);
693 fprintf(stderr
, ";\n");
703 if (!GetDefinition()) {
706 while (GetDefinition()) {
717 if (!GetCharacter('{')) {
727 if (!GetCharacter('}')) {
736 if (!GetAlphaMericString()) {
740 while (GetAlphaMericString()) {
754 while (GetCharacter('|')) {
768 fprintf(stderr
, "no '{'\n");
770 } else if (!GetDefinitions()) {
771 fprintf(stderr
, "unable to parse the definitions\n");
773 } else if (!GetEnd()) {
774 fprintf(stderr
, "No '}' or scanning stopped early due to error.\n");
787 fprintf(stderr
, "Invalid name field in entry.\n");
794 /* position ourselves within a given filename to the entry for the current
795 * KEYBD (or TERM) variable
799 Position(filename
, keybdPointer
)
804 stringWithLength
*name
= 0;
805 stringWithLength
*oldName
;
806 # define Return(x) {doPaste = 1; return(x);}
810 if ((ourFile
= fopen(filename
, "r")) == NULL
) {
812 fprintf(stderr
, "Unable to open file %s\n", filename
);
813 # endif /* !defined(MSDOS) */
817 while (lex
.type
!= LEX_END_OF_FILE
) {
819 /* now, find an entry that is our type. */
822 if ((name
= GetAlphaMericString()) != 0) {
823 if (!ustrcmp(name
->array
, keybdPointer
)) {
824 /* need to make sure there is a name here... */
830 } else if (GetCharacter('|')) {
831 ; /* more names coming */
835 if (lex
.type
!= LEX_END_OF_FILE
) {
836 if (!GetEntry0()) { /* start of an entry */
838 "error was in entry for %s in file %s\n",
839 (oldName
)? oldName
->array
:"(unknown)", filename
);
847 fprintf(stderr
, "Unable to find entry for %s in file %s\n", keybdPointer
,
849 #endif /* !defined(MSDOS) */
859 p
= malloc((unsigned int)strlen(string
)+1);
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.
874 InitControl(keybdPointer
, pickyarg
, translator
)
876 int pickyarg
; /* Should we be picky? */
877 int (*translator
)(char *); /* Translates ascii string to integer */
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.
896 keybdPointer
= strsave(keybdPointer
);
898 environPointer
= getenv("MAP3270");
899 if (keybdPointer
&& environPointer
900 && (environPointer
[0] != '/')
902 && (environPointer
[0] != '\\')
903 #endif /* defined(MSDOS) */
904 && (strncmp(keybdPointer
, environPointer
,
905 strlen(keybdPointer
) != 0)
906 || (environPointer
[strlen(keybdPointer
)] != '{'))) /* } */
911 if ((!environPointer
)
913 || (*environPointer
== '\\')
914 #endif /* defined(MSDOS) */
915 || (*environPointer
== '/')) {
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) */
927 if (environPointer
) {
928 GotIt
= Position(environPointer
, keybdPointer
);
931 GotIt
= Position("/usr/share/misc/map3270", keybdPointer
);
935 if (environPointer
) {
936 GotIt
= Position(environPointer
, "unknown");
938 if (!GotIt
&& keybdPointer
) {
939 GotIt
= Position("/usr/share/misc/map3270", keybdPointer
);
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 */
955 return(firstentry
.address
);