home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / flistfrontend / src / getpad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-21  |  3.7 KB  |  170 lines

  1. #ifndef NO_IDENT
  2. static char *Id = "$Id: getpad.c,v 1.5 1995/10/21 17:02:18 tom Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Title:    getpad.c - Get keypad "character"
  7.  * Author:    Thomas E. Dickey
  8.  * Created:    07 May 1984
  9.  * Last Update:
  10.  *        21 Oct 1995, renamed variables to avoid min/max conflict.
  11.  *        18 Feb 1995, port to DEC C (renamed 'alarm').
  12.  *        18 Sep 1984, use 'alarm' instead of 'putchar'
  13.  *        22 Jun 1984, corrections to erro handling
  14.  *        23 May 1984, to sound alarm on illegal sequences
  15.  *
  16.  * Function:    Read and parse input character stream, throwing away
  17.  *        escape sequences which do not correspond to VT52 or VT100
  18.  *        keypad controls.  For these sequences, however, translate
  19.  *        them into an internal code to pass back to the caller.
  20.  *
  21.  *        Non-escape sequences are passed back normally, without
  22.  *        translation.
  23.  *
  24.  * Reference:    VAX/VMS I/O User's Guide (version: V2.2, Jan 1981)
  25.  *
  26.  * Escapes:
  27.  *        ESC <int>...<int> <fin>
  28.  *
  29.  * where
  30.  *        ESC is 033
  31.  *        <int> is an "intermediate character" in the range 20-2F.
  32.  *        <fin> is a "final character" in the range 30-7E.
  33.  *
  34.  * Also:    ESC <;> <20-2F>...<30-7E>
  35.  *        ESC <?> <20-2F>...<30-7E>
  36.  *        ESC <O> <20-2F>...<40-7E>
  37.  *        ESC <Y> <20-7E>...<20-7E>
  38.  *
  39.  * Control:    ESC [ <par>...<par> <int>...<int> <fin>
  40.  *
  41.  * where
  42.  *        <par> is a parameter specifier in the range 30-3F.
  43.  *        <int> is an intermediate character, as above.
  44.  *        <fin> is a final character in the range 40-7E.
  45.  */
  46.  
  47. #include    <string.h>
  48.  
  49. #include    "crt.h"
  50. #include    "getpad.h"
  51. #include    "getraw.h"
  52.  
  53. typedef    struct    {
  54.     char    *s;
  55.     int    c;
  56.     } PAD_TABLE;
  57.  
  58. static
  59. PAD_TABLE pad_table[] = {
  60.         "[A",    padUP,        "A",    padUP,
  61.         "[B",    padDOWN,    "B",    padDOWN,
  62.         "[C",    padRIGHT,    "C",    padRIGHT,
  63.         "[D",    padLEFT,    "D",    padLEFT,
  64.  
  65.         "OP",    padPF1,        "P",    padPF1,
  66.         "OQ",    padPF2,        "Q",    padPF2,
  67.         "OR",    padPF3,        "R",    padPF3,
  68.         "OS",    padPF4,        /* ESC-S unused on VT52 */
  69.  
  70.         "Op",    pad0,        "?p",    pad0,
  71.         "Oq",    pad1,        "?q",    pad1,
  72.         "Or",    pad2,        "?r",    pad2,
  73.         "Os",    pad3,        "?s",    pad3,
  74.         "Ot",    pad4,        "?t",    pad4,
  75.         "Ou",    pad5,        "?u",    pad5,
  76.         "Ov",    pad6,        "?v",    pad6,
  77.         "Ow",    pad7,        "?w",    pad7,
  78.         "Ox",    pad8,        "?x",    pad8,
  79.         "Oy",    pad9,        "?y",    pad9,
  80.  
  81.         "Om",    padMINUS,    /* VT52 hasn't this key */
  82.         "Ol",    padCOMMA,    /* VT52 hasn't this key */
  83.         "On",    padDOT,        "?n",    padDOT,
  84.         "OM",    padENTER,    "?M",    padENTER
  85.         };
  86.  
  87. static
  88. int    max_table    = sizeof (pad_table) / sizeof(pad_table[0]),
  89.     raw;
  90. static
  91. char    bfr[80], *c_;
  92.  
  93. #define    ESC    '\033'
  94.  
  95. #define    START    {if (getpad_read() == ESC) {sound_alarm(); goto escape;}}
  96.  
  97. #define    ABSORB(minc,maxc)    while (raw >= minc && raw <= maxc) START
  98.  
  99. int
  100. getpad (void)
  101. {
  102.     c_ = bfr;
  103.     if (getpad_read() == ESC)
  104.     {
  105. escape:        c_ = bfr;
  106.         while (getpad_read() == ESC)    /* Ignore illegal escapes */
  107.         {
  108.             sound_alarm();
  109.             c_ = bfr;        /* (undo 'getpad_read')      */
  110.         }
  111.  
  112.         switch (raw)            /* Decode according to 2nd */
  113.         {
  114.         case ';':
  115.             START;            /* Get 3rd (counting ESC) */
  116.             ABSORB (0x20, 0x2f);
  117.             return (getpad_look (0x30, 0x7e));
  118.  
  119.         case '?':
  120.             START;            /* Get 3rd (counting ESC) */
  121.             ABSORB (0x20, 0x2f);
  122.             return (getpad_look (0x30, 0x7e));
  123.  
  124.         case 'O':
  125.             START;            /* Get 3rd (counting ESC) */
  126.             ABSORB (0x20, 0x2f);
  127.             return (getpad_look (0x40, 0x7e));
  128.  
  129.         case 'Y':
  130.             START;            /* Get 3rd (counting ESC) */
  131.             ABSORB (0x20, 0x2f);
  132.             return (getpad());
  133.  
  134.         case '[':
  135.             START;            /* Get 3rd (counting ESC) */
  136.             ABSORB (0x30, 0x3f);
  137.             ABSORB (0x20, 0x2f);
  138.             return (getpad_look (0x40, 0x7e));
  139.  
  140.         default:
  141.             ABSORB (0x20, 0x2f);
  142.             return (getpad_look (0x30, 0x7e));
  143.         }
  144.     }
  145.     else
  146.         return (raw);
  147. }
  148.  
  149. int
  150. getpad_look (int minc, int maxc)
  151. {
  152.     int    j;
  153.  
  154.     if (raw >= minc && raw <= maxc)
  155.     {
  156.         *c_++ = '\0';
  157.         for (j=0; j < max_table; j++)
  158.             if (strcmp(pad_table[j].s, bfr) == 0)
  159.                 return (pad_table[j].c);
  160.     }
  161.     sound_alarm();
  162.     return (getpad());    /* Ignore if not a keypad sequence */
  163. }
  164.  
  165. int
  166. getpad_read(void)
  167. {
  168.     return (*c_++ = raw = getraw());
  169. }
  170.