home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / KAYPRO / WS-KP25P.LBR / SETKEY.CQ / SETKEY.C
Text File  |  2000-06-30  |  7KB  |  239 lines

  1. /* setkey -- set Kaypro function keys from keyboard                         */
  2. /* copyright 1984  Michael M Rubenstein                                     */
  3.  
  4. /* syntax:                                                                  */
  5.  
  6. /*          setkey <fname>                                                  */
  7.  
  8. /* If no file type is given, .KEY is assumed                                */
  9.  
  10. /* the file contains lines of the following form                            */
  11.  
  12. /* <keyname> <keyvalue>                                                     */
  13.  
  14. /* where <keyname> is "0" - "9", ".", ",", "-", "e" (enter), "u" (up),      */
  15. /* "d" (down), "l" (left), or "r" (right)                                   */
  16.  
  17. /* and <keyvalue> is any combination of quoted strings, circumflex          */
  18. /* character (e.g. ^m for carriage return) or  hexadecimal values           */
  19.  
  20. /* examples:                                                                */
  21.  
  22. /* 1 "tst"^m            (tst <carriage return>)                             */
  23. /* u "t" ^m ^j          (t <carriage return> <line feed>)                   */
  24. /* u "t" 0d 0a          (same as previous)                                  */
  25.  
  26. #include <sysio.h>
  27. #include <buff.h>
  28.  
  29. #define NKEYS           18
  30.  
  31. struct jump {           char            j_jp;
  32.                         char            *j_addr;
  33.             };
  34.  
  35. struct bios {           struct jump     b_jump[16];
  36.                         char            b_ioconfig;
  37.                         char            b_wrtsafe;
  38.                         char            b_vtab[NKEYS];
  39.                         char            b_mbaud, pbaud;
  40.                         char            b_sndoff;
  41.             };
  42.  
  43. struct sndtab {         char            s_entry[4];
  44.               };
  45.  
  46. static unsigned         lineno = 1;
  47.  
  48. extern struct jump      boot_;
  49. extern FCB              dfcb_;
  50. extern char             dbuff_[];
  51.  
  52. extern char             *cindex();
  53.  
  54. main()
  55. {
  56.   struct FILE           infile;
  57.   static int            keyind;
  58.   static int            quote;
  59.   static int            c, d;
  60.   static char           *p;
  61.   static char           work[4];
  62.   static int            i;
  63.   static char           *v_vtab;
  64.   static struct sndtab  *v_sndtab;
  65.  
  66.   char                  w_vtab[NKEYS];
  67.   struct sndtab         w_sndtab[NKEYS];
  68.  
  69.  
  70.   static char           keynames[] = "udlr0123456789-,e.",
  71.                         keyvalues[] = "\013\012\008\0140123456789-,\015.";
  72.  
  73.   if (dfcb_.name1.fname[0] == ' ' || dfcb_.name1.fname[0] == '/')
  74.     help();
  75.  
  76.   /* set up file name */
  77.   if (dfcb_.name1.ftype[0] == ' ')
  78.     strncpy(dfcb_.name1.ftype, "KEY", 3);
  79.  
  80.   strncpy(&infile, &dfcb_, sizeof(FCB));
  81.   if (open(&infile) == 0xff)
  82.     error("setkey: Cannot open input file");
  83.  
  84.   /* set up for getc */
  85.   infile.f_buff = dbuff_;
  86.   infile.f_bufl = infile.f_bufc = 128;
  87.  
  88.   /* copy current key settings to work area */
  89.   v_vtab = boot_.j_addr->b_vtab;
  90.   v_sndtab = (struct sndtab *) (v_vtab + (boot_.j_addr->b_sndoff & 0xff));
  91.   strncpy(w_vtab, v_vtab, NKEYS);
  92.   strncpy(w_sndtab, v_sndtab, NKEYS * sizeof(struct sndtab));
  93.  
  94.   do
  95.   {
  96.     while (isspace(c = getc(&infile)))
  97.       if (c == '\n')
  98.         ++lineno;
  99.  
  100.     if (c == EOF || c == CPMEOF)
  101.       break;
  102.  
  103.     if ((p = cindex(keynames, tolower(c))) == NULL)
  104.     {
  105.       ps("setkey: Invalid key name on line ");
  106.       pn(lineno);
  107.       ps("\r\n");
  108.       exit(1);
  109.     }
  110.     keyind = p - keynames;
  111.  
  112.     /* get the key values from file */
  113.     i = 0;
  114.     quote = FALSE;
  115.     c = getc(&infile);
  116.     while (c != '\r' && c != '\n' && c != EOF && c != CPMEOF)
  117.     {
  118.       if (quote)
  119.       {
  120.         if (c == '"' && (c = getc(&infile)) != '"')
  121.         {
  122.           quote = FALSE;
  123.           continue;
  124.         }
  125.       }
  126.       else
  127.         switch (c)
  128.         {
  129.           case '"':     quote = TRUE;
  130.                         c = getc(&infile);
  131.                         continue;
  132.  
  133.           case '^':     if ((c = getc(&infile)) == EOF || c == CPMEOF
  134.                          || isspace(c))
  135.                         {
  136.                           ps("setkey: Invalid use of ^ in line ");
  137.                           pn(lineno);
  138.                           ps("\r\n");
  139.                           exit(1);
  140.                         }
  141.                         c &= 0x1f;
  142.                         break;
  143.  
  144.           case ' ':
  145.           case '\t':    c = getc(&infile);
  146.                         continue;
  147.  
  148.           default:      c = 16 * hexdig(c) + hexdig(getc(&infile));
  149.         }
  150.  
  151.       if (i >= 4)
  152.       {
  153.         ps("setkey: Value too long in line ");
  154.         pn(lineno);
  155.         ps("\r\n");
  156.         exit(1);
  157.       }
  158.       work[i++] = c;
  159.       c = getc(&infile);
  160.     }
  161.  
  162.     if (i == 0)
  163.       w_vtab[keyind] = keyvalues[keyind];       /* default */
  164.     else
  165.     if (i == 1)
  166.       w_vtab[keyind] = work[0];                 /* one character value */
  167.     else
  168.     {
  169.       w_vtab[keyind] = '\0';                    /* multiple character value */
  170.       while (i < 3)
  171.         work[i++] = '\0';
  172.       strncpy(&w_sndtab[keyind], work, 4);
  173.     }
  174.     if (c == '\n')
  175.       ++lineno;
  176.   } while (c != EOF && c != CPMEOF);
  177.  
  178.   close(&infile);
  179.  
  180.   /* move the new key values into BIOS */
  181.   strncpy(v_vtab, w_vtab, NKEYS);
  182.   strncpy(v_sndtab, w_sndtab, NKEYS * sizeof(struct sndtab));
  183. }
  184.  
  185. /* convert ascii char to hex                                                */
  186. hexdig(c)
  187.   int                   c;
  188. {
  189.   static char           *p;
  190.   static char           hexd[] = "0123456789abcdef";
  191.  
  192.   if ((p = cindex(hexd, c)) == NULL)
  193.   {
  194.     ps("setkey: Invalid hex digit in line ");
  195.     pn(lineno);
  196.     ps("\r\n");
  197.     exit(1);
  198.   }
  199.   return p - hexd;
  200. }
  201.  
  202. /* print a number on console                                                */
  203. pn(u)
  204.   unsigned              u;
  205. {
  206.   if (u > 10)
  207.     pn(u / 10);
  208.   conout(u % 10 + '0');
  209. }
  210.  
  211. /* show how to use                                                          */
  212. help()
  213. {
  214.   static char           *msg[] =
  215.   {
  216.     "syntax:   setkey <fname>\r\n\r\n",
  217.     "If no file type is given, .KEY is assumed.\r\n\r\n",
  218.     "The file contains lines of the form\r\n\r\n",
  219.     "<keyname> <keyvalue>\r\n\r\n",
  220.   "where <keyname> is \"0\" - \"9\", \".\", \",\", \"-\", \"e\" (enter),\r\n",
  221.     "\"u\" (up), \"d\" (down), \"l\" (left), or \"r\" (right)\r\n",
  222.     "and <keyvalue> is any combination of quoted strings, circumflex\r\n",
  223.     "character (e.g. ^m for carriage return) or  hexadecimal values.\r\n\r\n",
  224.     "Examples:\r\n\r\n",
  225.     "1 \"tst\"^m            (tst <carriage return>)\r\n",
  226.     "u \"t\" ^m ^j          (t <carriage return> <line feed>)\r\n",
  227.     "u \"t\" 0d 0a          (same as previous)\r\n\r\n",
  228.     "Maximum 4 characters per key.\r\n",
  229.     NULL
  230.   };
  231.  
  232.   static char           **p;
  233.  
  234.   for (p = msg; *p != NULL; ++p)
  235.     ps(*p);
  236.  
  237.   exit(0);
  238. }
  239.