home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / szadb21b / source / src / fkeydefs.c < prev    next >
C/C++ Source or Header  |  1990-05-16  |  7KB  |  265 lines

  1. /* Copyright (c) 1990 by Sozobon, Limited.  Author: Michal Jaegermann
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  *    fkeydefs.c  30 April 1990
  12.  */
  13.  
  14. #define IN_FKEY
  15.  
  16. #include <fcntl.h>
  17. #include "adb.h"
  18. #include "lang.h"
  19.  
  20. #define MSIZE    0x800
  21. #define SLACK    0x80
  22. #define NUMFK   20        /* a number of used function keys */
  23.  
  24. extern char   **ftable;
  25. extern int      ibase;
  26. extern int      lastc;
  27.  
  28. void
  29. kdefs (name)
  30.     char           *name;
  31. /*
  32.  * Read file with strings to be attached to function keys and
  33.  * create corresponding ftable entries
  34.  */
  35. {
  36.     char           *rbuf;
  37.     char           *target, *source, *stop;
  38.     int             fh, rcount = 0;
  39.     int             c, keynum = 0;
  40.     extern char    *malloc ();
  41.     extern int      atonum ();
  42.     static int      accept (), attach ();
  43.  
  44.     if (-3 > (fh = open (name, O_RDONLY))) {
  45.     /* prtf ("cannot open %s\n", name); */
  46.     prtf (M1, name);
  47.     return;
  48.     }
  49.  
  50.     if ((char *) 0 == (rbuf = malloc (MSIZE + SLACK))) {
  51.     /* prt ("kdefs: out of memory\n"); */
  52.     prt (M6);
  53.     return;
  54.     }
  55.     if ((char **) 0 == (ftable = (char **) malloc (NUMFK * sizeof (char *)))) {
  56.     /* prt ("no memory for a table of function keys\n"); */
  57.     prt (M2);
  58.     goto out;
  59.     }
  60.     /* initialize ftable */
  61.     keynum = NUMFK - 1;
  62.     do {
  63.     ftable[keynum] = (char *) 0;
  64.     keynum -= 1;
  65.     } while (keynum >= 0);
  66.  
  67.     target = rbuf;
  68.     for (;;) {
  69.     rcount = target - rbuf;    /* already this amount in a buffer */
  70.     rcount = (rcount < SLACK ? MSIZE : MSIZE + SLACK - rcount);
  71.     rcount = read (fh, target, rcount);
  72.     if (rcount <= 0)    /* EOF or error */
  73.         break;
  74.     source = target;
  75.     stop = source + rcount;
  76.  
  77.     while (source < stop) {
  78.         if (target > rbuf) {/* previous string not finished */
  79.         if (accept (&target, &source, stop)) {
  80.             if (0 > attach (keynum, rbuf, target))
  81.             goto out;    /* out of memory */
  82.         }
  83.         else {
  84.  
  85.             /*
  86.              * prtf ("definition of F%i too long - aborted\n",
  87.              * keynum);
  88.              */
  89.             prtf (M3, keynum);
  90.         }
  91.         target = rbuf;
  92.         }
  93.         else {
  94.         if ('F' == *source++) {
  95.             keynum = atonum (source, 10, &c);
  96.             if ((' ' == c || '\t' == c) &&
  97.             (0 < keynum && keynum <= NUMFK)) {
  98.             /* skip digits and following white space */
  99.             do {
  100.                 source++;
  101.             } while (c != *source);
  102.             do {
  103.                 source++;
  104.             } while (' ' == *source || '\t' == *source);
  105.             if ('\r' == *source && '\n' == *(source + 1)) {
  106.                 /* reached end of line - skip this definition */
  107.                 source += 2;
  108.                 continue;
  109.             }
  110.             if (accept (&target, &source, stop)) {    /* full def */
  111.                 if (0 > attach (keynum, rbuf, target))
  112.                 goto out;    /* out of memory */
  113.                 target = rbuf;
  114.             }
  115.             continue;
  116.             }        /* if ((' ' == c || '\t' == c) && ... */
  117.         }        /* if ('F' == *source++) */
  118.         /* skip a remainder of a line */
  119.         while (source < stop && '\n' != *source++);
  120.         }
  121.     }            /* while ( source < step) */
  122.     }                /* for (;;) */
  123.  
  124.     if (target > rbuf) {    /* some leftovers in rbuf */
  125.     (void) attach (keynum, rbuf, target);
  126.     if (rcount < 0) {
  127.         /* prtf ("read error: file %s\n", name); */
  128.         prtf (M4, name);
  129.     }
  130.     }
  131.  
  132. out:
  133.     free (rbuf);
  134.     return;
  135. }                /* kdefs */
  136.  
  137.  
  138. static int
  139. accept (p_target, p_source, stop)
  140.     char          **p_target;
  141.     char          **p_source;
  142.     char           *stop;
  143. /*
  144.  *  Copy characters from *source to *target, skipping escaped newlines,
  145.  *  until newline found (returns 1) or, it this fails,  until 'stop'
  146.  *  (returns 0). Updates values at p_target and at p_source.
  147.  */
  148. {
  149.     register char  *target, *source;
  150.     register char   c = 0;
  151.  
  152.     target = *p_target;
  153.     source = *p_source;
  154.  
  155.     while (c != '\n' && source < stop) {
  156.     if ('\\' == (c = *source++) && source < stop) {
  157.         if ('\r' == (c = *source++) && source < stop) {
  158.         if ('\n' == *source) {
  159.             source++;
  160.             continue;
  161.         }
  162.         }
  163.         *target++ = '\\';
  164.     }
  165.     *target++ = c;
  166.     }
  167.  
  168.     *p_target = target;
  169.     *p_source = source;
  170.     return ('\n' == c);
  171. }                /* accept */
  172.  
  173. static int
  174. attach (keynum, from, to)
  175.     int             keynum;
  176.     char           *from;
  177.     char           *to;
  178. /*
  179.  * If keynum in range, keynum slot is not taken yet, and memory
  180.  * is available then put into ftable a string starting in
  181.  * an address 'from' and ending in 'to'.
  182.  * This string is modified by a removal of all trainling white
  183.  * space and appending a sequence "\n\0" to it.
  184.  *
  185.  * Returns 0 on success, -1 if out of memory, and 1 if keynum
  186.  * out of range or slot already taken
  187.  */
  188. {
  189.     register char  *pos, *table, c;
  190.     extern char    *malloc ();
  191.  
  192.     keynum -= 1;
  193.     if ((unsigned) keynum >= NUMFK || ftable[keynum])
  194.     /* to big or too small or already defined */
  195.     return 1;
  196.  
  197.     to -= 1;
  198.     while ('\n' == (c = *to) || ('\r' == c) || (' ' == c) || ('\t' == c)) {
  199.     to -= 1;
  200.     }
  201.     to += 1;
  202.     *to++ = '\n';
  203.     *to++ = '\0';
  204.  
  205.     pos = from;
  206.     if ((char *) 0 == (ftable[keynum] = malloc ((int) (to - pos)))) {
  207.     /* prtf ("out of memory while defining F%i\n", keynum); */
  208.     prtf (M5, keynum);
  209.     return (-1);
  210.     }
  211.  
  212.     table = ftable[keynum];
  213.     while (pos < to)
  214.     *table++ = *pos++;
  215.  
  216.     return 0;
  217. }                /* attach */
  218.  
  219. void
  220. hndlfkey ()
  221. /*
  222.  *  If what follows on a command line converts, in base 10,
  223.  *  to 0 - print all definition of function keys.
  224.  *  If in range 1 -- NUMFK, then execute definition - if exists.
  225.  *  Otherwise ignore.  Only up to 11 non-blank characters get
  226.  *  converted.
  227.  */
  228. {
  229.     char           *cp, *cs, buf[12];
  230.     int             i, base, c;
  231.     long            n;
  232.     long            atonum ();
  233.     void            src_line ();
  234.  
  235.     if ((char **) 0 == ftable)
  236.     return;
  237.  
  238.     cp = buf;
  239.     cs = buf + 11;
  240.  
  241.     PEEKC;
  242.     do {
  243.     c = getchr (0);
  244.     *cp++ = c;
  245.     } while ('\n' != c && cp < cs);
  246.     PUSHC (c);
  247.     n = atonum (buf, 10, &c);
  248.  
  249.     if (0 == n) {        /* list */
  250.     base = ibase;
  251.     ibase = 10;
  252.     for (i = 0; i < NUMFK; i++) {
  253.         if ((char *) 0 != ftable[i])
  254.         prtf ("F%i %s", i + 1, ftable[i]);
  255.     }
  256.     ibase = base;
  257.     }
  258.     else if (n <= NUMFK) {
  259.     src_line (ftable[n - 1]);
  260.     }
  261.     return;
  262. }                /* hndlfkey */
  263.  
  264. #undef IN_FKEY
  265.