home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / tn3270 / ascii / map3270.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-26  |  19.7 KB  |  932 lines

  1. /*-
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)map3270.c    4.2 (Berkeley) 4/26/91";
  36. #endif /* not lint */
  37.  
  38. /*    This program reads a description file, somewhat like /etc/termcap,
  39.     that describes the mapping between the current terminal's keyboard and
  40.     a 3270 keyboard.
  41.  */
  42. #ifdef DOCUMENTATION_ONLY
  43. /* here is a sample (very small) entry...
  44.  
  45.     # this table is sensitive to position on a line.  In particular,
  46.     # a terminal definition for a terminal is terminated whenever a
  47.     # (non-comment) line beginning in column one is found.
  48.     #
  49.     # this is an entry to map tvi924 to 3270 keys...
  50.     v8|tvi924|924|televideo model 924 {
  51.         pfk1 =    '\E1';
  52.         pfk2 =    '\E2';
  53.         clear = '^z';        # clear the screen
  54.     }
  55.  */
  56. #endif /* DOCUMENTATION_ONLY */
  57.  
  58. #include <stdio.h>
  59. #include <ctype.h>
  60. #if    defined(unix)
  61. #include <strings.h>
  62. #else    /* defined(unix) */
  63. #include <string.h>
  64. #endif    /* defined(unix) */
  65.  
  66. #define    IsPrint(c)    ((isprint(c) && !isspace(c)) || ((c) == ' '))
  67.  
  68. #include "state.h"
  69. #include "map3270.h"
  70.  
  71. #include "../general/globals.h"
  72.  
  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 */
  79.  
  80. /* the following is part of our character set dependancy... */
  81. #define    ESCAPE        0x1b
  82. #define    TAB        0x09
  83. #define    NEWLINE     0x0a
  84. #define    CARRIAGE_RETURN 0x0d
  85.  
  86. typedef struct {
  87.     int type;        /* LEX_* - type of character */
  88.     int value;        /* character this was */
  89. } lexicon;
  90.  
  91. typedef struct {
  92.     int        length;        /* length of character string */
  93.     char    array[500];    /* character string */
  94. } stringWithLength;
  95.  
  96. #define    panic(s)    { fprintf(stderr, s); exit(1); }
  97.  
  98. static state firstentry = { 0, STATE_NULL, 0, 0 };
  99. static state *headOfQueue = &firstentry;
  100.  
  101. /* the following is a primitive adm3a table, to be used when nothing
  102.  * else seems to be avaliable.
  103.  */
  104.  
  105. #ifdef    DEBUG
  106. static int debug = 0;        /* debug flag (for debuggin tables) */
  107. #endif    /* DEBUG */
  108.  
  109. static int (*GetTc)();
  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.
  116.                  */
  117. static char **whichkey = 0;
  118. static char *keysgeneric[] = {
  119. #include "default.map"        /* Define the default default */
  120.  
  121.     0,            /* Terminate list of entries */
  122. };
  123.             ;
  124.  
  125. static    int    Empty = 1,        /* is the unget lifo empty? */
  126.         Full = 0;        /* is the unget lifo full? */
  127. static    lexicon    lifo[200] = { 0 };    /* character stack for parser */
  128. static    int    rp = 0,            /* read pointer into lifo */
  129.         wp = 0;            /* write pointer into lifo */
  130.  
  131. static int
  132. GetC()
  133. {
  134.     int character;
  135.  
  136.     if (usePointer) {
  137.     if ((*environPointer) == 0) {
  138.         /*
  139.          * If we have reached the end of this string, go on to
  140.          * the next (if there is a next).
  141.          */
  142.         if (whichkey == 0) {
  143.         static char suffix = 'A';    /* From environment */
  144.         char envname[9];
  145.         extern char *getenv();
  146.  
  147.         (void) sprintf(envname, "MAP3270%c", suffix++);
  148.         environPointer = getenv(envname);
  149.         } else {
  150.         whichkey++;            /* default map */
  151.         environPointer = *whichkey;
  152.         }
  153.     }
  154.     if (*environPointer) {
  155.        character = 0xff&*environPointer++;
  156.     } else {
  157.        character = EOF;
  158.     }
  159.     } else {
  160.     character = getc(ourFile);
  161.     }
  162.     return(character);
  163. }
  164.  
  165. static lexicon
  166. Get()
  167. {
  168.     lexicon c;
  169.     register lexicon *pC = &c;
  170.     register int character;
  171.  
  172.     if (!Empty) {
  173.     *pC = lifo[rp];
  174.     rp++;
  175.     if (rp == sizeof lifo/sizeof (lexicon)) {
  176.         rp = 0;
  177.     }
  178.     if (rp == wp) {
  179.         Empty = 1;
  180.     }
  181.     Full = 0;
  182.     } else {
  183.     character = GetC();
  184.     switch (character) {
  185.     case EOF:
  186.         pC->type = LEX_END_OF_FILE;
  187.         break;
  188.     case '^':
  189.         character = GetC();
  190.         if (!IsPrint(character)) {
  191.         pC->type = LEX_ILLEGAL;
  192.         } else {
  193.         pC->type = LEX_CARETED;
  194.         if (character == '?') {
  195.             character |= 0x40;    /* rubout */
  196.         } else {
  197.             character &= 0x1f;
  198.         }
  199.         }
  200.         break;
  201.     case '\\':
  202.         character = GetC();
  203.         if (!IsPrint(character)) {
  204.         pC->type = LEX_ILLEGAL;
  205.         } else {
  206.         pC->type = LEX_ESCAPED;
  207.         switch (character) {
  208.         case 'E': case 'e':
  209.             character = ESCAPE;
  210.             break;
  211.         case 't':
  212.             character = TAB;
  213.             break;
  214.         case 'n':
  215.             character = NEWLINE;
  216.             break;
  217.         case 'r':
  218.             character = CARRIAGE_RETURN;
  219.             break;
  220.         default:
  221.             pC->type = LEX_ILLEGAL;
  222.             break;
  223.         }
  224.         }
  225.         break;
  226.     default:
  227.         if ((IsPrint(character)) || isspace(character)) {
  228.         pC->type = LEX_CHAR;
  229.         } else {
  230.         pC->type = LEX_ILLEGAL;
  231.         }
  232.         break;
  233.     }
  234.     pC->value = character;
  235.     }
  236.     return(*pC);
  237. }
  238.  
  239. static void
  240. UnGet(c)
  241. lexicon c;            /* character to unget */
  242. {
  243.     if (Full) {
  244.     fprintf(stderr, "attempt to put too many characters in lifo\n");
  245.     panic("map3270");
  246.     /* NOTREACHED */
  247.     } else {
  248.     lifo[wp] = c;
  249.     wp++;
  250.     if (wp == sizeof lifo/sizeof (lexicon)) {
  251.         wp = 0;
  252.     }
  253.     if (wp == rp) {
  254.         Full = 1;
  255.     }
  256.     Empty = 0;
  257.     }
  258. }
  259.  
  260. /*
  261.  * Construct a control character sequence
  262.  * for a special character.
  263.  */
  264. char *
  265. uncontrol(c)
  266.     register int c;
  267. {
  268.     static char buf[3];
  269.  
  270.     if (c == 0x7f)
  271.         return ("^?");
  272.     if (c == '\377') {
  273.         return "-1";
  274.     }
  275.     if (c >= 0x20) {
  276.         buf[0] = c;
  277.         buf[1] = 0;
  278.     } else {
  279.         buf[0] = '^';
  280.         buf[1] = '@'+c;
  281.         buf[2] = 0;
  282.     }
  283.     return (buf);
  284. }
  285.  
  286. /* compare two strings, ignoring case */
  287.  
  288. ustrcmp(string1, string2)
  289. register char *string1;
  290. register char *string2;
  291. {
  292.     register int c1, c2;
  293.  
  294.     while ((c1 = (unsigned char) *string1++) != 0) {
  295.     if (isupper(c1)) {
  296.         c1 = tolower(c1);
  297.     }
  298.     if (isupper(c2 = (unsigned char) *string2++)) {
  299.         c2 = tolower(c2);
  300.     }
  301.     if (c1 < c2) {
  302.         return(-1);
  303.     } else if (c1 > c2) {
  304.         return(1);
  305.     }
  306.     }
  307.     if (*string2) {
  308.     return(-1);
  309.     } else {
  310.     return(0);
  311.     }
  312. }
  313.  
  314.  
  315. static stringWithLength *
  316. GetQuotedString()
  317. {
  318.     lexicon lex;
  319.     static stringWithLength output = { 0 };    /* where return value is held */
  320.     char *pointer = output.array;
  321.  
  322.     lex = Get();
  323.     if ((lex.type != LEX_CHAR) || (lex.value != '\'')) {
  324.     UnGet(lex);
  325.     return(0);
  326.     }
  327.     while (1) {
  328.     lex = Get();
  329.     if ((lex.type == LEX_CHAR) && (lex.value == '\'')) {
  330.         break;
  331.     }
  332.     if ((lex.type == LEX_CHAR) && !IsPrint(lex.value)) {
  333.         UnGet(lex);
  334.         return(0);        /* illegal character in quoted string */
  335.     }
  336.     if (pointer >= output.array+sizeof output.array) {
  337.         return(0);        /* too long */
  338.     }
  339.     *pointer++ = lex.value;
  340.     }
  341.     output.length = pointer-output.array;
  342.     return(&output);
  343. }
  344.  
  345. #ifdef    NOTUSED
  346. static stringWithLength *
  347. GetCharString()
  348. {
  349.     lexicon lex;
  350.     static stringWithLength output;
  351.     char *pointer = output.array;
  352.  
  353.     lex = Get();
  354.  
  355.     while ((lex.type == LEX_CHAR) &&
  356.             !isspace(lex.value) && (lex.value != '=')) {
  357.     *pointer++ = lex.value;
  358.     lex = Get();
  359.     if (pointer >= output.array + sizeof output.array) {
  360.         return(0);        /* too long */
  361.     }
  362.     }
  363.     UnGet(lex);
  364.     output.length = pointer-output.array;
  365.     return(&output);
  366. }
  367. #endif    /* NOTUSED */
  368.  
  369. static
  370. GetCharacter(character)
  371. int    character;        /* desired character */
  372. {
  373.     lexicon lex;
  374.  
  375.     lex = Get();
  376.  
  377.     if ((lex.type != LEX_CHAR) || (lex.value != character)) {
  378.     UnGet(lex);
  379.     return(0);
  380.     }
  381.     return(1);
  382. }
  383.  
  384. #ifdef    NOTUSED
  385. static
  386. GetString(string)
  387. char    *string;        /* string to get */
  388. {
  389.     lexicon lex;
  390.  
  391.     while (*string) {
  392.     lex = Get();
  393.     if ((lex.type != LEX_CHAR) || (lex.value != *string&0xff)) {
  394.         UnGet(lex);
  395.         return(0);        /* XXX restore to state on entry */
  396.     }
  397.     string++;
  398.     }
  399.     return(1);
  400. }
  401. #endif    /* NOTUSED */
  402.  
  403.  
  404. static stringWithLength *
  405. GetAlphaMericString()
  406. {
  407.     lexicon lex;
  408.     static stringWithLength output = { 0 };
  409.     char *pointer = output.array;
  410. #   define    IsAlnum(c)    (isalnum(c) || (c == '_') \
  411.                     || (c == '-') || (c == '.'))
  412.  
  413.     lex = Get();
  414.  
  415.     if ((lex.type != LEX_CHAR) || !IsAlnum(lex.value)) {
  416.     UnGet(lex);
  417.     return(0);
  418.     }
  419.  
  420.     while ((lex.type == LEX_CHAR) && IsAlnum(lex.value)) {
  421.     *pointer++ = lex.value;
  422.     lex = Get();
  423.     }
  424.     UnGet(lex);
  425.     *pointer = 0;
  426.     output.length = pointer-output.array;
  427.     return(&output);
  428. }
  429.  
  430.  
  431. /* eat up characters until a new line, or end of file.  returns terminating
  432.     character.
  433.  */
  434.  
  435. static lexicon
  436. EatToNL()
  437. {
  438.     lexicon lex;
  439.  
  440.     lex = Get();
  441.  
  442.     while (!((lex.type != LEX_ESCAPED) && (lex.type != LEX_CARETED) && 
  443.         (lex.value == '\n')) && (!(lex.type == LEX_END_OF_FILE))) {
  444.     lex = Get();
  445.     }
  446.     if (lex.type != LEX_END_OF_FILE) {
  447.     return(Get());
  448.     } else {
  449.     return(lex);
  450.     }
  451. }
  452.  
  453.  
  454. static void
  455. GetWS()
  456. {
  457.     lexicon lex;
  458.  
  459.     lex = Get();
  460.  
  461.     while ((lex.type == LEX_CHAR) &&
  462.             (isspace(lex.value) || (lex.value == '#'))) {
  463.     if (lex.value == '#') {
  464.         lex = EatToNL();
  465.     } else {
  466.         lex = Get();
  467.     }
  468.     }
  469.     UnGet(lex);
  470. }
  471.  
  472. static void
  473. FreeState(pState)
  474. state *pState;
  475. {
  476.     extern int free();
  477.  
  478.     free((char *)pState);
  479. }
  480.  
  481.  
  482. static state *
  483. GetState()
  484. {
  485.     state *pState;
  486.     extern char *malloc();
  487.  
  488.     pState = (state *) malloc(sizeof (state));
  489.  
  490.     pState->result = STATE_NULL;
  491.     pState->next = 0;
  492.  
  493.     return(pState);
  494. }
  495.  
  496.  
  497. static state *
  498. FindMatchAtThisLevel(pState, character)
  499. state    *pState;
  500. int    character;
  501. {
  502.     while (pState) {
  503.     if (pState->match == character) {
  504.         return(pState);
  505.     }
  506.     pState = pState->next;
  507.     }
  508.     return(0);
  509. }
  510.  
  511.  
  512. static state *
  513. PasteEntry(head, string, count, identifier)
  514. state            *head;        /* points to who should point here... */
  515. char            *string;    /* which characters to paste */
  516. int            count;        /* number of character to do */
  517. char            *identifier;    /* for error messages */
  518. {
  519.     state *pState, *other;
  520.  
  521.     if (!doPaste) {        /* flag to not have any side effects */
  522.     return((state *)1);
  523.     }
  524.     if (!count) {
  525.     return(head);    /* return pointer to the parent */
  526.     }
  527.     if ((head->result != STATE_NULL) && (head->result != STATE_GOTO)) {
  528.     /* this means that a previously defined sequence is an initial
  529.      * part of this one.
  530.      */
  531.     fprintf(stderr, "Conflicting entries found when scanning %s\n",
  532.         identifier);
  533.     return(0);
  534.     }
  535. #   ifdef    DEBUG
  536.     if (debug) {
  537.         fprintf(stderr, "%s", uncontrol(*string));
  538.     }
  539. #   endif    /* DEBUG */
  540.     pState = GetState();
  541.     pState->match = *string;
  542.     if (head->result == STATE_NULL) {
  543.     head->result = STATE_GOTO;
  544.     head->address = pState;
  545.     other = pState;
  546.     } else {        /* search for same character */
  547.     if ((other = FindMatchAtThisLevel(head->address, *string)) != 0) {
  548.         FreeState(pState);
  549.     } else {
  550.         pState->next = head->address;
  551.         head->address = pState;
  552.         other = pState;
  553.     }
  554.     }
  555.     return(PasteEntry(other, string+1, count-1, identifier));
  556. }
  557.  
  558. static
  559. GetInput(tc, identifier)
  560. int tc;
  561. char *identifier;        /* entry being parsed (for error messages) */
  562. {
  563.     stringWithLength *outputString;
  564.     state *head;
  565.     state fakeQueue;
  566.  
  567.     if (doPaste) {
  568.     head = headOfQueue;    /* always points to level above this one */
  569.     } else {
  570.     head = &fakeQueue;    /* don't have any side effects... */
  571.     }
  572.  
  573.     if ((outputString = GetQuotedString()) == 0) {
  574.     return(0);
  575.     } else if (IsPrint(outputString->array[0])) {
  576.     fprintf(stderr,
  577.      "first character of sequence for %s is not a control type character\n",
  578.         identifier);
  579.     return(0);
  580.     } else {
  581.     if ((head = PasteEntry(head, outputString->array,
  582.                 outputString->length, identifier)) == 0) {
  583.         return(0);
  584.     }
  585.     GetWS();
  586.     while ((outputString = GetQuotedString()) != 0) {
  587.         if ((head = PasteEntry(head, outputString->array,
  588.                 outputString->length, identifier)) == 0) {
  589.         return(0);
  590.         }
  591.         GetWS();
  592.     }
  593.     }
  594.     if (!doPaste) {
  595.     return(1);
  596.     }
  597.     if ((head->result != STATE_NULL) && (head->result != tc)) {
  598.     /* this means that this sequence is an initial part
  599.      * of a previously defined one.
  600.      */
  601.     fprintf(stderr, "Conflicting entries found when scanning %s\n",
  602.         identifier);
  603.     return(0);
  604.     } else {
  605.     head->result = tc;
  606.     return(1);        /* done */
  607.     }
  608. }
  609.  
  610. static
  611. GetDefinition()
  612. {
  613.     stringWithLength *string;
  614.     int Tc;
  615.  
  616.     GetWS();
  617.     if ((string = GetAlphaMericString()) == 0) {
  618.     return(0);
  619.     }
  620.     string->array[string->length] = 0;
  621.     if (doPaste) {
  622.     if ((Tc = (*GetTc)(string->array)) == -1) {
  623.         if (picky) {
  624.         fprintf(stderr, "%s: unknown 3270 key identifier\n",
  625.                             string->array);
  626.         }
  627.         Tc = STATE_NULL;
  628.     }
  629.     } else {
  630.     Tc = STATE_NULL;        /* XXX ? */
  631.     }
  632.     GetWS();
  633.     if (!GetCharacter('=')) {
  634.     fprintf(stderr,
  635.         "Required equal sign after 3270 key identifier %s missing\n",
  636.             string->array);
  637.     return(0);
  638.     }
  639.     GetWS();
  640.     if (!GetInput(Tc, string->array)) {
  641.     fprintf(stderr, "Missing definition part for 3270 key %s\n",
  642.                 string->array);
  643.     return(0);
  644.     } else {
  645.     GetWS();
  646.     while (GetCharacter('|')) {
  647. #        ifdef    DEBUG
  648.         if (debug) {
  649.             fprintf(stderr, " or ");
  650.         }
  651. #        endif    /* DEBUG */
  652.         GetWS();
  653.         if (!GetInput(Tc, string->array)) {
  654.         fprintf(stderr, "Missing definition part for 3270 key %s\n",
  655.                     string->array);
  656.         return(0);
  657.         }
  658.         GetWS();
  659.     }
  660.     }
  661.     GetWS();
  662.     if (!GetCharacter(';')) {
  663.     fprintf(stderr, "Missing semi-colon for 3270 key %s\n", string->array);
  664.     return(0);
  665.     }
  666. #   ifdef    DEBUG
  667.     if (debug) {
  668.         fprintf(stderr, ";\n");
  669.     }
  670. #   endif    /* DEBUG */
  671.     return(1);
  672. }
  673.  
  674.  
  675. static
  676. GetDefinitions()
  677. {
  678.     if (!GetDefinition()) {
  679.     return(0);
  680.     } else {
  681.     while (GetDefinition()) {
  682.         ;
  683.     }
  684.     }
  685.     return(1);
  686. }
  687.  
  688. static
  689. GetBegin()
  690. {
  691.     GetWS();
  692.     if (!GetCharacter('{')) {
  693.     return(0);
  694.     }
  695.     return(1);
  696. }
  697.  
  698. static
  699. GetEnd()
  700. {
  701.     GetWS();
  702.     if (!GetCharacter('}')) {
  703.     return(0);
  704.     }
  705.     return(1);
  706. }
  707.  
  708. static
  709. GetName()
  710. {
  711.     if (!GetAlphaMericString()) {
  712.     return(0);
  713.     }
  714.     GetWS();
  715.     while (GetAlphaMericString()) {
  716.     GetWS();
  717.     }
  718.     return(1);
  719. }
  720.  
  721. static
  722. GetNames()
  723. {
  724.     GetWS();
  725.     if (!GetName()) {
  726.     return(0);
  727.     } else {
  728.     GetWS();
  729.     while (GetCharacter('|')) {
  730.         GetWS();
  731.         if (!GetName()) {
  732.         return(0);
  733.         }
  734.     }
  735.     }
  736.     return(1);
  737. }
  738.  
  739. static
  740. GetEntry0()
  741. {
  742.     if (!GetBegin()) {
  743.     fprintf(stderr, "no '{'\n");
  744.     return(0);
  745.     } else if (!GetDefinitions()) {
  746.     fprintf(stderr, "unable to parse the definitions\n");
  747.     return(0);
  748.     } else if (!GetEnd()) {
  749.     fprintf(stderr, "No '}' or scanning stopped early due to error.\n");
  750.     return(0);
  751.     } else {
  752.     /* done */
  753.     return(1);
  754.     }
  755. }
  756.  
  757.  
  758. static
  759. GetEntry()
  760. {
  761.     if (!GetNames()) {
  762.     fprintf(stderr, "Invalid name field in entry.\n");
  763.     return(0);
  764.     } else {
  765.     return(GetEntry0());
  766.     }
  767. }
  768.  
  769. /* position ourselves within a given filename to the entry for the current
  770.  *    KEYBD (or TERM) variable
  771.  */
  772.  
  773. Position(filename, keybdPointer)
  774. char *filename;
  775. char *keybdPointer;
  776. {
  777.     lexicon lex;
  778.     stringWithLength *name = 0;
  779.     stringWithLength *oldName;
  780. #   define    Return(x) {doPaste = 1; return(x);}
  781.  
  782.     doPaste = 0;
  783.  
  784.     if ((ourFile = fopen(filename, "r")) == NULL) {
  785. #   if !defined(MSDOS)
  786.     fprintf(stderr, "Unable to open file %s\n", filename);
  787. #   endif /* !defined(MSDOS) */
  788.     Return(0);
  789.     }
  790.     lex = Get();
  791.     while (lex.type != LEX_END_OF_FILE) {
  792.     UnGet(lex);
  793.     /* now, find an entry that is our type. */
  794.     GetWS();
  795.     oldName = name;
  796.     if ((name = GetAlphaMericString()) != 0) {
  797.         if (!ustrcmp(name->array, keybdPointer)) {
  798.         /* need to make sure there is a name here... */
  799.         lex.type = LEX_CHAR;
  800.         lex.value = 'a';
  801.         UnGet(lex);
  802.         Return(1);
  803.         }
  804.     } else if (GetCharacter('|')) {
  805.         ;        /* more names coming */
  806.     } else {
  807.         lex = Get();
  808.         UnGet(lex);
  809.         if (lex.type != LEX_END_OF_FILE) {
  810.             if (!GetEntry0()) {    /* start of an entry */
  811.             fprintf(stderr,
  812.                 "error was in entry for %s in file %s\n",
  813.                 (oldName)? oldName->array:"(unknown)", filename);
  814.             Return(0);
  815.         }
  816.         }
  817.     }
  818.     lex = Get();
  819.     }
  820. #if !defined(MSDOS)
  821.     fprintf(stderr, "Unable to find entry for %s in file %s\n", keybdPointer,
  822.             filename);
  823. #endif    /* !defined(MSDOS) */
  824.     Return(0);
  825. }
  826.  
  827. char *
  828. strsave(string)
  829. char *string;
  830. {
  831.     char *p;
  832.     extern char *malloc();
  833.  
  834.     p = malloc((unsigned int)strlen(string)+1);
  835.     if (p != 0) {
  836.     strcpy(p, string);
  837.     }
  838.     return(p);
  839. }
  840.  
  841.  
  842. /*
  843.  * InitControl - our interface to the outside.  What we should
  844.  *  do is figure out keyboard (or terminal) type, set up file pointer
  845.  *  (or string pointer), etc.
  846.  */
  847.  
  848. state *
  849. InitControl(keybdPointer, pickyarg, translator)
  850. char    *keybdPointer;
  851. int    pickyarg;        /* Should we be picky? */
  852. int    (*translator)();    /* Translates ascii string to integer */
  853. {
  854.     extern char *getenv();
  855.     int GotIt;
  856.  
  857.     picky = pickyarg;
  858.     GetTc = translator;
  859.  
  860.     if (keybdPointer == 0) {
  861.         keybdPointer = getenv("KEYBD");
  862.     }
  863.     if (keybdPointer == 0) {
  864.        keybdPointer = getenv("TERM");
  865.     }
  866.  
  867.             /*
  868.              * Some environments have getenv() return
  869.              * out of a static area.  So, save the keyboard name.
  870.              */
  871.     if (keybdPointer) {
  872.         keybdPointer = strsave(keybdPointer);
  873.     }
  874.     environPointer = getenv("MAP3270");
  875.     if (environPointer
  876.         && (environPointer[0] != '/')
  877. #if    defined(MSDOS)
  878.         && (environPointer[0] != '\\')
  879. #endif    /* defined(MSDOS) */
  880.         && (strncmp(keybdPointer, environPointer,
  881.             strlen(keybdPointer) != 0)
  882.         || (environPointer[strlen(keybdPointer)] != '{'))) /* } */
  883.     {
  884.     environPointer = 0;
  885.     }
  886.  
  887.     if ((!environPointer)
  888. #if    defined(MSDOS)
  889.         || (*environPointer == '\\')
  890. #endif    /* defined(MSDOS) */
  891.         || (*environPointer == '/')) {
  892.     usePointer = 0;
  893.     GotIt = 0;
  894.     if (!keybdPointer) {
  895. #if !defined(MSDOS)
  896.         fprintf(stderr, "%s%s%s%s",
  897.         "Neither the KEYBD environment variable nor the TERM ",
  898.         "environment variable\n(one of which is needed to determine ",
  899.         "the type of keyboard you are using)\n",
  900.         "is set.  To set it, say 'setenv KEYBD <type>'\n");
  901. #endif    /* !defined(MSDOS) */
  902.     } else {
  903.         if (environPointer) {
  904.         GotIt = Position(environPointer, keybdPointer);
  905.         }
  906.         if (!GotIt) {
  907.         GotIt = Position("/etc/map3270", keybdPointer);
  908.         }
  909.     }
  910.     if (!GotIt) {
  911.         if (environPointer) {
  912.         GotIt = Position(environPointer, "unknown");
  913.         }
  914.         if (!GotIt) {
  915.         GotIt = Position("/etc/map3270", keybdPointer);
  916.         }
  917.     }
  918.     if (!GotIt) {
  919. #if !defined(MSDOS)
  920.         fprintf(stderr, "Using default key mappings.\n");
  921. #endif    /* !defined(MSDOS) */
  922.         usePointer = 1;        /* flag use of non-file */
  923.         whichkey = keysgeneric;
  924.         environPointer = *whichkey;    /* use default table */
  925.     }
  926.     } else {
  927.     usePointer = 1;
  928.     }
  929.     (void) GetEntry();
  930.     return(firstentry.address);
  931. }
  932.