home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / cawf407.zip / src / macsup.c < prev    next >
C/C++ Source or Header  |  1993-12-28  |  4KB  |  183 lines

  1. /*
  2.  *    macsup.c - macro processing support functions for cawf(1)
  3.  */
  4.  
  5. /*
  6.  *    Copyright (c) 1991 Purdue University Research Foundation,
  7.  *    West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *    Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *    University Computing Center.  Not derived from licensed software;
  11.  *    derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *    Permission is granted to anyone to use this software for any
  14.  *    purpose on any computer system, and to alter it and redistribute
  15.  *    it freely, subject to the following restrictions:
  16.  *
  17.  *    1. The author is not responsible for any consequences of use of
  18.  *       this software, even if they arise from flaws in it.
  19.  *
  20.  *    2. The origin of this software must not be misrepresented, either
  21.  *       by explicit claim or by omission.  Credits must appear in the
  22.  *       documentation.
  23.  *
  24.  *    3. Altered versions must be plainly marked as such, and must not
  25.  *       be misrepresented as being the original software.  Credits must
  26.  *       appear in the documentation.
  27.  *
  28.  *    4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32.  
  33.  
  34. /*
  35.  * Delmacro(mx) - delete macro
  36.  */
  37.  
  38. void
  39. Delmacro(mx)
  40.     int mx;                /* macro index */
  41. {
  42.     unsigned char buf[MAXLINE];    /* error message buffer */
  43.     int i, j;            /* temporary indexes */
  44.  
  45.     if (mx >= Nmac) {
  46.         (void) sprintf((char *)buf, " bad Delmacro(%d) index", mx);
  47.         Error(FATAL, LINE, (char *)buf, NULL);
  48.     }
  49.     for (i = Macrotab[mx].bx, j = i + Macrotab[mx].ct; i < j; i++) {
  50.         Free(&Macrotxt[i]);
  51.     }
  52.     for (i = mx; i < (Nmac - 1); i++) {
  53.         Macrotab[i] = Macrotab[i+1];
  54.     }
  55.     Nmac--;
  56. }
  57.  
  58.  
  59. /*
  60.  * Field(n, p, c) - skip to field n in p and optionally return a copy
  61.  */
  62.  
  63. unsigned char *
  64. Field(n, p, c)
  65.     int n;                /* field number */
  66.     unsigned char *p;        /* pointer to line containing fields */
  67.     int c;                /* 1: make a copy of the field */
  68. {
  69.     unsigned char *fs, *fe;
  70.  
  71.     if (c)
  72.         Free(&F);
  73.     fe = p;
  74.     while (n) {
  75.         while (*fe == ' ' || *fe == '\t')
  76.             fe++;
  77.         fs = fe;
  78.         while (*fe && *fe != ' ' && *fe != '\t')
  79.             fe++;
  80.         if (fs == fe)
  81.             return(NULL);
  82.         if (n == 1) {
  83.             if ( ! c)
  84.                 return(fs);
  85.             if ((F = (unsigned char *)malloc((size_t)(fe - fs + 1)))
  86.             == NULL)
  87.                 Error(FATAL, LINE, " Field out of string space",
  88.                     NULL);
  89.             (void) strncpy((char *)F, (char *)fs, (fe - fs));
  90.             F[fe -fs] = '\0';
  91.             return(F);
  92.         }
  93.         n--;
  94.     }
  95.     return(NULL);
  96. }
  97.  
  98. /*
  99.  * Findmacro(p, e) - find macro and optionally enter it
  100.  *
  101.  * return = Macrotab[] index or -1 if not found
  102.  */
  103.  
  104.  
  105. Findmacro(p, e)
  106.     unsigned char *p;    /* pointer to 2 character macro name  */
  107.     int e;            /* 0 = find, don't enter
  108.                  * 1 = enter, don't find */
  109. {
  110.     unsigned char c[3];
  111.     int cmp, hi, low, mid;
  112.  
  113.     c[0] = p[0];
  114.     c[1] = (p[1] == ' ' || p[1] == '\t') ? '\0' : p[1];
  115.     c[2] = '\0';
  116.     low = mid = 0;
  117.     hi = Nmac - 1;
  118.     while (low <= hi) {
  119.         mid = (low + hi) / 2;
  120.         if ((cmp = strncmp((char *)c, (char *)Macrotab[mid].name, 2))
  121.         < 0)
  122.             hi = mid - 1;
  123.         else if (cmp > 0)
  124.             low = mid + 1;
  125.         else {
  126.             if ( ! e)
  127.                 return(mid);
  128.              Error(WARN, LINE, " duplicate macro ", (char *)c);
  129.              hi = Macrotab[mid].bx + Macrotab[mid].ct;
  130.              for (low = Macrotab[mid].bx; low < hi; low++) {
  131.                 Free(&Macrotxt[low]);
  132.              }
  133.              goto new_macro;
  134.         }
  135.     }
  136.     if ( ! e)
  137.         return(-1);
  138.     if (Nmac >= MAXMACRO)
  139.         Error(FATAL, LINE, " macro table full at ", (char *)c);
  140.     if (Nmac) {
  141.         if (cmp > 0)
  142.             mid++;
  143.         for (hi = Nmac - 1; hi >= mid; hi--)
  144.             Macrotab[hi+1] = Macrotab[hi];
  145.     }
  146.     Nmac++;
  147.     Macrotab[mid].name[0] = c[0];
  148.     Macrotab[mid].name[1] = c[1];
  149.  
  150. new_macro:
  151.  
  152.     Macrotab[mid].bx = -1;
  153.     Macrotab[mid].ct = 0;
  154.     return(mid);
  155. }
  156.  
  157. void
  158. Free(p)
  159.     unsigned char **p;
  160. {
  161.     if (*p != NULL) {
  162.         (void) free(*p);
  163.         *p = NULL;
  164.     }
  165. }
  166.  
  167. /*
  168.  * Newstr(s) - allocate space for string
  169.  */
  170.  
  171. unsigned char *
  172. Newstr(s)
  173.     unsigned char *s;
  174. {
  175.     unsigned char *ns;
  176.  
  177.     if ((ns = (unsigned char *)malloc((size_t)(strlen((char *)s) + 1)))
  178.     == NULL)
  179.         Error(FATAL, LINE, " Newstr out of malloc space at ", (char *)s);
  180.     (void) strcpy((char *)ns, (char *)s);
  181.     return(ns);
  182. }
  183.