home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / rcs5ap1s.lzh / RCSLEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-30  |  24.6 KB  |  1,001 lines

  1. /*
  2.  *                     RCS file input
  3.  */
  4. /*********************************************************************************
  5.  *                     Lexical Analysis.
  6.  *                     hashtable, Lexinit, nextlex, getlex, getkey,
  7.  *                     getid, getnum, readstring, printstring, savestring,
  8.  *                     checkid, fatserror, error, faterror, warn, diagnose
  9.  *                     Testprogram: define LEXDB
  10.  *********************************************************************************
  11.  */
  12.  
  13. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  14.    Copyright 1990 by Paul Eggert
  15.    Distributed under license by the Free Software Foundation, Inc.
  16.  
  17. This file is part of RCS.
  18.  
  19. RCS is free software; you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License as published by
  21. the Free Software Foundation; either version 1, or (at your option)
  22. any later version.
  23.  
  24. RCS is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27. GNU General Public License for more details.
  28.  
  29. You should have received a copy of the GNU General Public License
  30. along with RCS; see the file COPYING.  If not, write to
  31. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  32.  
  33. Report problems and direct all questions to:
  34.  
  35.     rcs-bugs@cs.purdue.edu
  36.  
  37. */
  38.  
  39.  
  40.  
  41. /* $Log: rcslex.c,v $
  42.  * Revision 5.6  1991/01/30  14:21:32  apratt
  43.  * CI with RCS version 5
  44.  *
  45.  * Revision 5.5  90/12/04  05:18:47  eggert
  46.  * checked in with -k by apratt at 91.01.10.13.15.22.
  47.  * 
  48.  * Revision 5.5  1990/12/04  05:18:47  eggert
  49.  * Use -I for prompts and -q for diagnostics.
  50.  *
  51.  * Revision 5.4  1990/11/19  20:05:28  hammer
  52.  * no longer gives warning about unknown keywords if -q is specified
  53.  *
  54.  * Revision 5.3  1990/11/01  05:03:48  eggert
  55.  * When ignoring unknown phrases, copy them to the output RCS file.
  56.  *
  57.  * Revision 5.2  1990/09/04  08:02:27  eggert
  58.  * Count RCS lines better.
  59.  *
  60.  * Revision 5.1  1990/08/29  07:14:03  eggert
  61.  * Work around buggy compilers with defective argument promotion.
  62.  *
  63.  * Revision 5.0  1990/08/22  08:12:55  eggert
  64.  * Remove compile-time limits; use malloc instead.
  65.  * Report errno-related errors with perror().
  66.  * Ansify and Posixate.  Add support for ISO 8859.
  67.  * Use better hash function.
  68.  *
  69.  * Revision 4.6  89/05/01  15:13:07  narten
  70.  * changed copyright header to reflect current distribution rules
  71.  * 
  72.  * Revision 4.5  88/08/28  15:01:12  eggert
  73.  * Don't loop when writing error messages to a full filesystem.
  74.  * Flush stderr/stdout when mixing output.
  75.  * Yield exit status compatible with diff(1).
  76.  * Shrink stdio code size; allow cc -R; remove lint.
  77.  * 
  78.  * Revision 4.4  87/12/18  11:44:47  narten
  79.  * fixed to use "varargs" in "fprintf"; this is required if it is to
  80.  * work on a SPARC machine such as a Sun-4
  81.  * 
  82.  * Revision 4.3  87/10/18  10:37:18  narten
  83.  * Updating version numbers. Changes relative to 1.1 actually relative
  84.  * to version 4.1
  85.  * 
  86.  * Revision 1.3  87/09/24  14:00:17  narten
  87.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  88.  * warnings)
  89.  * 
  90.  * Revision 1.2  87/03/27  14:22:33  jenkins
  91.  * Port to suns
  92.  * 
  93.  * Revision 4.1  83/03/25  18:12:51  wft
  94.  * Only changed $Header to $Id.
  95.  * 
  96.  * Revision 3.3  82/12/10  16:22:37  wft
  97.  * Improved error messages, changed exit status on error to 1.
  98.  *
  99.  * Revision 3.2  82/11/28  21:27:10  wft
  100.  * Renamed ctab to map and included EOFILE; ctab is now a macro in rcsbase.h.
  101.  * Added fflsbuf(), fputs(), and fprintf(), which abort the RCS operations
  102.  * properly in case there is an IO-error (e.g., file system full).
  103.  *
  104.  * Revision 3.1  82/10/11  19:43:56  wft
  105.  * removed unused label out:;
  106.  * made sure all calls to getc() return into an integer, not a char.
  107.  */
  108.  
  109.  
  110. /*
  111. #define LEXDB
  112. */
  113. /* version LEXDB is for testing the lexical analyzer. The testprogram
  114.  * reads a stream of lexemes, enters the revision numbers into the
  115.  * hashtable, and prints the recognized tokens. Keywords are recognized
  116.  * as identifiers.
  117.  */
  118.  
  119.  
  120.  
  121. #include "rcsbase.h"
  122.  
  123. libId(lexId, "$Id: rcslex.c,v 5.6 1991/01/30 14:21:32 apratt Exp $")
  124.  
  125. static struct hshentry *nexthsh;  /*pointer to next hash entry, set by lookup*/
  126.  
  127. enum tokens     nexttok;    /*next token, set by nextlex                    */
  128.  
  129. int             hshenter;   /*if true, next suitable lexeme will be entered */
  130.                             /*into the symbol table. Handle with care.      */
  131. int             nextc;      /*next input character, initialized by Lexinit  */
  132.  
  133. unsigned long    rcsline;    /*current line-number of input            */
  134. int             nerror;     /*counter for errors                            */
  135. int             quietflag;  /*indicates quiet mode                          */
  136. FILE *          finptr;     /*input file descriptor                         */
  137.  
  138. FILE *          frewrite;   /*file descriptor for echoing input             */
  139.  
  140. FILE *        foutptr;        /* copy of frewrite, but NULL to suppress echo  */
  141.  
  142. static struct buf tokbuf;   /* token buffer                    */
  143.  
  144. const char *    NextString; /* next token                    */
  145.  
  146. /*
  147.  * Our hash algorithm is h[0] = 0, h[i+1] = 4*h[i] + c,
  148.  * so hshsize should be odd.
  149.  * See B J McKenzie, R Harries & T Bell, Selecting a hashing algorithm,
  150.  * Software--practice & experience 20, 2 (Feb 1990), 209-224.
  151.  */
  152. #ifndef hshsize
  153. #    define hshsize 511
  154. #endif
  155.  
  156. static struct hshentry *hshtab[hshsize]; /*hashtable                */
  157.  
  158. static int ignored_phrases; /* have we ignored phrases in this RCS file? */
  159.  
  160.     void
  161. warnignore()
  162. {
  163.     if (! (ignored_phrases|quietflag)) {
  164.     ignored_phrases = true;
  165.     warn("Unknown phrases like `%s ...;' are in the RCS file.", NextString);
  166.     }
  167. }
  168.  
  169.  
  170.  
  171.     static void
  172. lookup(str)
  173.     const char *str;
  174. /* Function: Looks up the character string pointed to by str in the
  175.  * hashtable. If the string is not present, a new entry for it is created.
  176.  * In any case, the address of the corresponding hashtable entry is placed
  177.  * into nexthsh.
  178.  */
  179. {
  180.     register unsigned ihash;  /* index into hashtable */
  181.     register const char *sp;
  182.     register struct hshentry *n, **p;
  183.  
  184.         /* calculate hash code */
  185.     sp = str;
  186.         ihash = 0;
  187.     while (*sp)
  188.         ihash  =  (ihash<<2) + *sp++;
  189.     ihash %= hshsize;
  190.  
  191.     for (p = &hshtab[ihash];  ;  p = &n->nexthsh)
  192.         if (!(n = *p)) {
  193.             /* empty slot found */
  194.             *p = n = ftalloc(struct hshentry);
  195.             n->num = fstrsave(str);
  196.             n->nexthsh = nil;
  197. #            ifdef LEXDB
  198.                 VOID printf("\nEntered: %s at %u ", str, ihash);
  199. #            endif
  200.             break;
  201.         } else if (strcmp(str, n->num) == 0)
  202.             /* match found */
  203.             break;
  204.     nexthsh = n;
  205.     NextString = n->num;
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.     void
  214. Lexinit()
  215. /* Function: Initialization of lexical analyzer:
  216.  * initializes the hashtable,
  217.  * initializes nextc, nexttok if finptr != NULL
  218.  */
  219. {       register int            c;
  220.  
  221.     for (c = hshsize;  0 <= --c;  ) {
  222.         hshtab[c] = nil;
  223.         }
  224.  
  225.     hshenter=true; rcsline=1; nerror=0;
  226.     ignored_phrases = false;
  227.     bufrealloc(&tokbuf, 2);
  228.         if (finptr) {
  229.         GETC(finptr,foutptr,c);
  230.         nextc = c; /*initial character*/
  231.         nexttok = DELIM;  /* anything but EOFILE */
  232.                 nextlex();            /*initial token*/
  233.         } else {
  234.                 nextc = '\0';
  235.                 nexttok=EOFILE;
  236.         }
  237. }
  238.  
  239.  
  240.     static exiting void
  241. unexpectedEOF()
  242. {
  243.     fatserror("unexpected EOF");
  244. }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.     void
  253. nextlex()
  254.  
  255. /* Function: Reads the next token and sets nexttok to the next token code.
  256.  * Only if hshenter is set, a revision number is entered into the
  257.  * hashtable and a pointer to it is placed into nexthsh.
  258.  * This is useful for avoiding that dates are placed into the hashtable.
  259.  * For ID's and NUM's, NextString is set to the character string.
  260.  * Assumption: nextc contains the next character.
  261.  */
  262. {       register c;
  263.     register FILE * fin, * frew;
  264.         register char * sp;
  265.     const char *lim;
  266.         register enum tokens d;
  267.  
  268.     if (nexttok == EOFILE)
  269.         unexpectedEOF();
  270.     fin=finptr; frew=foutptr;
  271.  
  272.     for (;;) switch ((nexttok=ctab[nextc])) {
  273.  
  274.     default:
  275.         fatserror("unknown character `%c'", nextc);
  276.         /*NOTREACHED*/
  277.  
  278.         case NEWLN:
  279.         ++rcsline;
  280. #               ifdef LEXDB
  281.         afputc('\n',stdout);
  282. #               endif
  283.                 /* Note: falls into next case */
  284.  
  285.         case SPACE:
  286.         GETC(fin,frew,c);
  287.         nextc = c;
  288.         continue;
  289.  
  290.         case EOFILE:
  291.                 return;
  292.  
  293.         case DIGIT:
  294.         sp = tokbuf.string;
  295.         lim = sp + tokbuf.size;
  296.                 *sp++ = nextc;
  297.         for (;;) {
  298.             GETC(fin,frew,c);
  299.             if ((d=ctab[c])!=DIGIT && d!=PERIOD)
  300.                 break;
  301.                         *sp++ = c;         /* 1.2. and 1.2 are different */
  302.             if (lim <= sp)
  303.                 sp = bufenlarge(&tokbuf, &lim);
  304.                 }
  305.         *sp = 0;
  306.                 nextc = c;
  307.         if (hshenter)
  308.             lookup(tokbuf.string);
  309.         else
  310.             NextString = fstrsave(tokbuf.string);
  311.                 nexttok = NUM;
  312.                 return;
  313.  
  314.  
  315.         case LETTER:
  316.     case Letter:
  317.         sp = tokbuf.string;
  318.         lim = sp + tokbuf.size;
  319.                 *sp++ = nextc;
  320.         for (;;) {
  321.             GETC(fin,frew,c);
  322.             if ((d=ctab[c])!=LETTER && d!=Letter && d!=DIGIT && d!=IDCHAR)
  323.                 break;
  324.                         *sp++ = c;
  325.             if (lim <= sp)
  326.                 sp = bufenlarge(&tokbuf, &lim);
  327.                 }
  328.         *sp = 0;
  329.                 nextc = c;
  330.         NextString = fstrsave(tokbuf.string);
  331.                 nexttok = ID;  /* may be ID or keyword */
  332.                 return;
  333.  
  334.         case SBEGIN: /* long string */
  335.                 nexttok = STRING;
  336.                 /* note: only the initial SBEGIN has been read*/
  337.                 /* read the string, and reset nextc afterwards*/
  338.                 return;
  339.  
  340.     case COLON:
  341.     case SEMI:
  342.         GETC(fin,frew,c);
  343.         nextc = c;
  344.                 return;
  345.         }
  346. }
  347.  
  348.  
  349. int getlex(token)
  350. enum tokens token;
  351. /* Function: Checks if nexttok is the same as token. If so,
  352.  * advances the input by calling nextlex and returns true.
  353.  * otherwise returns false.
  354.  * Doesn't work for strings and keywords; loses the character string for ids.
  355.  */
  356. {
  357.         if (nexttok==token) {
  358.                 nextlex();
  359.                 return(true);
  360.         } else  return(false);
  361. }
  362.  
  363.     int
  364. getkeyopt(key)
  365.     const char *key;
  366. /* Function: If the current token is a keyword identical to key,
  367.  * advances the input by calling nextlex and returns true;
  368.  * otherwise returns false.
  369.  */
  370. {
  371.     if (nexttok==ID  &&  strcmp(key,NextString) == 0) {
  372.          /* match found */
  373.          ffree1(NextString);
  374.          nextlex();
  375.          return(true);
  376.         }
  377.         return(false);
  378. }
  379.  
  380.     void
  381. getkey(key)
  382.     const char *key;
  383. /* Check that the current input token is a keyword identical to key,
  384.  * and advance the input by calling nextlex.
  385.  */
  386. {
  387.     if (!getkeyopt(key))
  388.         fatserror("missing '%s' keyword", key);
  389. }
  390.  
  391.     void
  392. getkeystring(key)
  393.     const char *key;
  394. /* Check that the current input token is a keyword identical to key,
  395.  * and advance the input by calling nextlex; then look ahead for a string.
  396.  */
  397. {
  398.     getkey(key);
  399.     if (nexttok != STRING)
  400.         fatserror("missing string after '%s' keyword", key);
  401. }
  402.  
  403.  
  404.     const char *
  405. getid()
  406. /* Function: Checks if nexttok is an identifier. If so,
  407.  * advances the input by calling nextlex and returns a pointer
  408.  * to the identifier; otherwise returns nil.
  409.  * Treats keywords as identifiers.
  410.  */
  411. {
  412.     register const char *name;
  413.         if (nexttok==ID) {
  414.                 name = NextString;
  415.                 nextlex();
  416.                 return name;
  417.         } else  return nil;
  418. }
  419.  
  420.  
  421. struct hshentry * getnum()
  422. /* Function: Checks if nexttok is a number. If so,
  423.  * advances the input by calling nextlex and returns a pointer
  424.  * to the hashtable entry. Otherwise returns nil.
  425.  * Doesn't work if hshenter is false.
  426.  */
  427. {
  428.         register struct hshentry * num;
  429.         if (nexttok==NUM) {
  430.                 num=nexthsh;
  431.                 nextlex();
  432.                 return num;
  433.         } else  return nil;
  434. }
  435.  
  436.     struct cbuf
  437. getphrases(key)
  438.     const char *key;
  439. /* Get a series of phrases that do not start with KEY, yield resulting buffer.
  440.  * Stop when the next phrase starts with a token that is not an identifier,
  441.  * or is KEY.
  442.  * Assume foutptr == NULL.
  443.  */
  444. {
  445.     register FILE *fin;
  446.     register int c;
  447.     register char *p;
  448.     const char *lim;
  449.     register const char *ki, *kn;
  450.     struct cbuf r;
  451.     struct buf b;
  452.  
  453.     if (nexttok!=ID  ||  strcmp(NextString,key) == 0) {
  454.     r.string = 0;
  455.     r.size = 0;
  456.     return r;
  457.     } else {
  458.     warnignore();
  459.     fin = finptr;
  460.     bufautobegin(&b);
  461.     bufscpy(&b, NextString);
  462.     ffree1(NextString);
  463.     p = b.string + strlen(b.string);
  464.     lim = b.string + b.size;
  465.     c = nextc;
  466.     for (;;) {
  467.         for (;;) {
  468.         if (lim <= p)
  469.             p = bufenlarge(&b, &lim);
  470.         *p++ = c;
  471.         switch (ctab[c]) {
  472.             default:
  473.             fatserror("unknown character `%c'", c);
  474.             /*NOTREACHED*/
  475.             case EOFILE:
  476.             unexpectedEOF();
  477.             /*NOTREACHED*/
  478.             case NEWLN:
  479.             ++rcsline;
  480.             /* fall into */
  481.             case COLON: case DIGIT: case LETTER: case Letter:
  482.             case PERIOD: case SPACE:
  483.             c = getc(fin);
  484.             continue;
  485.             case SBEGIN: /* long string */
  486.             for (;;) {
  487.                 for (;;) {
  488.                 c = getc(fin);
  489.                 if (lim <= p)
  490.                     p = bufenlarge(&b, &lim);
  491.                 *p++ = c;
  492.                 switch (c) {
  493.                     case EOF:
  494.                     unexpectedEOF();
  495.                     /*NOTREACHED*/
  496.                     case '\n':
  497.                     ++rcsline;
  498.                     /* fall into */
  499.                     default:
  500.                     continue;
  501.                     case SDELIM:
  502.                     break;
  503.                 }
  504.                 break;
  505.                 }
  506.                 c = getc(fin);
  507.                 if (c != SDELIM)
  508.                 break;
  509.                 if (lim <= p)
  510.                 p = bufenlarge(&b, &lim);
  511.                 *p++ = c;
  512.             }
  513.             continue;
  514.             case SEMI:
  515.             c = getc(fin);
  516.             if (ctab[c] == NEWLN) {
  517.                 ++rcsline;
  518.                 if (lim <= p)
  519.                 p = bufenlarge(&b, &lim);
  520.                 *p++ = c;
  521.                 c = getc(fin);
  522.             }
  523.             for (;; c = getc(fin)) {
  524.                 switch (ctab[c]) {
  525.                 case NEWLN: ++rcsline; continue;
  526.                 case SPACE: continue;
  527.                 default: break;
  528.                 }
  529.                 break;
  530.             }
  531.             break;
  532.         }
  533.         break;
  534.         }
  535.         switch (ctab[c]) {
  536.         case LETTER:
  537.         case Letter:
  538.             for (kn = key;  c && *kn==c;  kn++)
  539.             if ((c = getc(fin)) == EOF)
  540.                 unexpectedEOF();
  541.             if (!*kn)
  542.             switch (ctab[c]) {
  543.                 case DIGIT: case LETTER: case Letter:
  544.                 break;
  545.                 default:
  546.                 nextc = c;
  547.                 NextString = fstrsave(key);
  548.                 nexttok = ID;
  549.                 goto returnit;
  550.             }
  551.             for (ki=key; ki<kn; ) {
  552.             if (lim <= p)
  553.                 p = bufenlarge(&b, &lim);
  554.             *p++ = *ki++;
  555.             }
  556.             break;
  557.         default:
  558.             nextc = c;
  559.             nextlex();
  560.             goto returnit;
  561.         }
  562.     }
  563.  
  564.     returnit:
  565.     /*
  566.      * Do the following instead of bufautoend(&b),
  567.      * because the buffer must survive until we are done with the file.
  568.      */
  569.     r.size = p - b.string;
  570.     r.string = (char*)fremember(testrealloc((malloc_type)b.string, r.size));
  571.     return r;
  572.     }
  573. }
  574.  
  575.  
  576.     void
  577. readstring()
  578. /* skip over characters until terminating single SDELIM        */
  579. /* If foutptr is set, copy every character read to foutptr.    */
  580. /* Does not advance nextlex at the end.                        */
  581. {       register c;
  582.     register FILE * fin,  * frew;
  583.     fin=finptr; frew=foutptr;
  584.     if (frew) {
  585.         /* Copy string verbatim to foutptr.  */
  586.                 while ((c=getc(fin)) != EOF) {
  587.             aputc(c,frew);
  588.             switch (c) {
  589.                 case '\n':
  590.                 ++rcsline;
  591.                 break;
  592.                 case SDELIM:
  593.                 if ((c=getc(fin)) == EOF) {
  594.                     nextc=c;
  595.                     return;
  596.                 }
  597.                 aputc(c,frew);
  598.                 if (c != SDELIM) {
  599.                                         /* end of string */
  600.                                         nextc=c;
  601.                                         return;
  602.                                 }
  603.                 break;
  604.                         }
  605.                 }
  606.         } else {
  607.                 /* skip string */
  608.                 while ((c=getc(fin)) != EOF) {
  609.             switch (c) {
  610.                 case '\n':
  611.                 ++rcsline;
  612.                 break;
  613.                 case SDELIM:
  614.                                 if ((c=getc(fin)) != SDELIM) {
  615.                                         /* end of string */
  616.                                         nextc=c;
  617.                                         return;
  618.                                 }
  619.                 break;
  620.                         }
  621.                 }
  622.         }
  623.     unterminatedString();
  624. }
  625.  
  626.  
  627.     void
  628. printstring()
  629. /* Function: copy a string to stdout, until terminated with a single SDELIM.
  630.  * Does not advance nextlex at the end.
  631.  */
  632. {
  633.         register c;
  634.     register FILE *fin, *fout;
  635.     fin=finptr;
  636.     fout = stdout;
  637.     while ((c=getc(fin)) != EOF) {
  638.         switch (c) {
  639.             case '\n':
  640.             ++rcsline;
  641.             break;
  642.             case SDELIM:
  643.             if ((c=getc(fin)) != SDELIM) {
  644.                                 /* end of string */
  645.                                 nextc=c;
  646.                                 return;
  647.                         }
  648.             break;
  649.                 }
  650.         aputc(c,fout);
  651.         }
  652.     unterminatedString();
  653. }
  654.  
  655.  
  656.  
  657.     struct cbuf
  658. savestring(target)
  659.     struct buf *target;
  660. /* Copies a string terminated with SDELIM from file finptr to buffer target.
  661.  * Double SDELIM is replaced with SDELIM.
  662.  * If foutptr is set, the string is also copied unchanged to foutptr.
  663.  * Does not advance nextlex at the end.
  664.  * Yield a copy of *TARGET, except with exact length.
  665.  */
  666. {
  667.         register c;
  668.     register FILE * fin, * frew;
  669.     register char *tp;
  670.     const char *lim;
  671.     struct cbuf r;
  672.  
  673.     fin=finptr; frew=foutptr;
  674.     tp = target->string;  lim = tp + target->size;
  675.     for (;;) {
  676.         GETC(fin,frew,c);
  677.         switch (c) {
  678.             case '\n':
  679.             ++rcsline;
  680.             break;
  681.             case SDELIM:
  682.             GETC(fin,frew,c);
  683.             if (c != SDELIM) {
  684.                                 /* end of string */
  685.                                 nextc=c;
  686.                 r.string = target->string;
  687.                 r.size = tp - r.string;
  688.                 return r;
  689.                         }
  690.             break;
  691.             case EOF:
  692.             unterminatedString();
  693.                 }
  694.         if (tp == lim)
  695.             tp = bufenlarge(target, &lim);
  696.         *tp++ = c;
  697.         }
  698. }
  699.  
  700.  
  701.     char *
  702. checkid(id, delimiter)
  703.     register char *id;
  704.     int delimiter;
  705. /*   Function:  check whether the string starting at id is an   */
  706. /*        identifier and return a pointer to the delimiter*/
  707. /*        after the identifier.  White space, delim and 0 */
  708. /*              are legal delimiters.  Aborts the program if not*/
  709. /*              a legal identifier. Useful for checking commands*/
  710. /*        If !delim, the only delimiter is 0.        */
  711. {
  712.         register enum  tokens  d;
  713.         register char    *temp;
  714.         register char    c,tc;
  715.     register char delim = delimiter;
  716.  
  717.     temp = id;
  718.     if ((d = ctab[(unsigned char)(c = *id)])==LETTER || d==Letter) {
  719.         while ((d = ctab[(unsigned char)(c = *++id)])==LETTER
  720.         || d==Letter || d==DIGIT || d==IDCHAR
  721.         )
  722.         ;
  723.         if (c  &&  (!delim || c!=delim && c!=' ' && c!='\t' && c!='\n')) {
  724.                 /* append \0 to end of id before error message */
  725.                 tc = c;
  726.                 while( (c=(*++id))!=' ' && c!='\t' && c!='\n' && c!='\0' && c!=delim) ;
  727.                 *id = '\0';
  728.         faterror("invalid character %c in identifier `%s'",tc,temp);
  729.         }
  730.         } else {
  731.             /* append \0 to end of id before error message */
  732.             while( (c=(*++id))!=' ' && c!='\t' && c!='\n' && c!='\0' && c!=delim) ;
  733.             *id = '\0';
  734.         faterror("identifier `%s' doesn't start with letter", temp);
  735.         }
  736.     return id;
  737. }
  738.  
  739.     void
  740. checksid(id)
  741.     register char *id;
  742. /* Check whether the string ID is an identifier.  */
  743. {
  744.     VOID checkid(id, 0);
  745. }
  746.  
  747.  
  748.     exiting void
  749. IOerror()
  750. {
  751.     static looping;
  752.     if (looping)
  753.         exiterr();
  754.     looping = true;
  755.     faterror("input/output error; is the file system full?");
  756. }
  757.  
  758. void eflush() { if (fflush(stderr) == EOF) IOerror(); }
  759. void oflush() { if (fflush(stdout) == EOF) IOerror(); }
  760.  
  761. exiting void unterminatedString() { fatserror("unterminated string"); }
  762.  
  763.     static exiting void
  764. fatcleanup(already_newline)
  765.     int already_newline;
  766. {
  767.     VOID fprintf(stderr, already_newline+"\n%s aborted\n", cmdid);
  768.     exiterr();
  769. }
  770.  
  771. static void errsay() { oflush(); aprintf(stderr,"%s error: ",cmdid); nerror++; }
  772. static void fatsay() { oflush(); VOID fprintf(stderr,"%s error: ",cmdid); }
  773.  
  774. void eerror(n) const char *n; { errsay(); perror(n); eflush(); }
  775. exiting void efaterror(n) const char *n; { fatsay(); perror(n); fatcleanup(true); }
  776.  
  777. #if has_prototypes
  778.     void
  779. error(const char *format,...)
  780. #else
  781.     /*VARARGS1*/ void error(format, va_alist) const char *format; va_dcl
  782. #endif
  783. /* non-fatal error */
  784. {
  785.     va_list args;
  786.     errsay();
  787.     vararg_start(args, format);
  788.     fvfprintf(stderr, format, args);
  789.     va_end(args);
  790.     afputc('\n',stderr);
  791.     eflush();
  792. }
  793.  
  794. #if has_prototypes
  795.     exiting void
  796. fatserror(const char *format,...)
  797. #else
  798.     /*VARARGS1*/ exiting void
  799.     fatserror(format, va_alist) const char *format; va_dcl
  800. #endif
  801. /* fatal syntax error */
  802. {
  803.     va_list args;
  804.     oflush();
  805.     VOID fprintf(stderr, "%s: %s:%lu: ", cmdid, RCSfilename, rcsline);
  806.     vararg_start(args, format);
  807.     fvfprintf(stderr, format, args);
  808.     va_end(args);
  809.     fatcleanup(false);
  810. }
  811.  
  812. #if has_prototypes
  813.     exiting void
  814. faterror(const char *format,...)
  815. #else
  816.     /*VARARGS1*/ exiting void faterror(format, va_alist)
  817.     const char *format; va_dcl
  818. #endif
  819. /* fatal error, terminates program after cleanup */
  820. {
  821.     va_list args;
  822.     fatsay();
  823.     vararg_start(args, format);
  824.     fvfprintf(stderr, format, args);
  825.     va_end(args);
  826.     fatcleanup(false);
  827. }
  828.  
  829. #if has_prototypes
  830.     void
  831. warn(const char *format,...)
  832. #else
  833.     /*VARARGS1*/ void warn(format, va_alist) const char *format; va_dcl
  834. #endif
  835. /* prints a warning message */
  836. {
  837.     va_list args;
  838.     oflush();
  839.     aprintf(stderr,"%s warning: ",cmdid);
  840.     vararg_start(args, format);
  841.     fvfprintf(stderr, format, args);
  842.     va_end(args);
  843.     afputc('\n',stderr);
  844.     eflush();
  845. }
  846.  
  847.     void
  848. redefined(c)
  849.     int c;
  850. {
  851.     warn("redefinition of -%c option", c);
  852. }
  853.  
  854. #if has_prototypes
  855.     void
  856. diagnose(const char *format,...)
  857. #else
  858.     /*VARARGS1*/ void diagnose(format, va_alist) const char *format; va_dcl
  859. #endif
  860. /* prints a diagnostic message */
  861. /* Unlike the other routines, it does not append a newline. */
  862. /* This lets some callers suppress the newline, and is faster */
  863. /* in implementations that flush stderr just at the end of each printf. */
  864. {
  865.     va_list args;
  866.         if (!quietflag) {
  867.         oflush();
  868.         vararg_start(args, format);
  869.         fvfprintf(stderr, format, args);
  870.         va_end(args);
  871.         eflush();
  872.         }
  873. }
  874.  
  875.  
  876.  
  877.     void
  878. afputc(c, f)
  879. /* Function: afputc(c,f) acts like aputc(c,f), but is smaller and slower.
  880.  */
  881.     int c;
  882.     register FILE *f;
  883. {
  884.     aputc(c,f);
  885. }
  886.  
  887.  
  888.     void
  889. aputs(s, iop)
  890.     const char *s;
  891.     FILE *iop;
  892. /* Function: Put string s on file iop, abort on error.
  893.  */
  894. {
  895.     if (fputs(s, iop) == EOF)
  896.         IOerror();
  897. }
  898.  
  899.  
  900.  
  901.     void
  902. #if has_prototypes
  903. fvfprintf(FILE *stream, const char *format, va_list args)
  904. #else
  905.     fvfprintf(stream,format,args) FILE *stream; char *format; va_list args;
  906. #endif
  907. /* like vfprintf, except abort program on error */
  908. {
  909. #if has_vfprintf
  910.     if (vfprintf(stream, format, args) == EOF)
  911. #else
  912.     _doprnt(format, args, stream);
  913.     if (ferror(stream))
  914. #endif
  915.         IOerror();
  916. }
  917.  
  918. #if has_prototypes
  919.     void
  920. aprintf(FILE *iop, const char *fmt, ...)
  921. #else
  922.     /*VARARGS2*/ void
  923. aprintf(iop, fmt, va_alist)
  924. FILE *iop;
  925. const char *fmt;
  926. va_dcl
  927. #endif
  928. /* Function: formatted output. Same as fprintf in stdio,
  929.  * but aborts program on error
  930.  */
  931. {
  932.     va_list ap;
  933.     vararg_start(ap, fmt);
  934.     fvfprintf(iop, fmt, ap);
  935.     va_end(ap);
  936. }
  937.  
  938.  
  939.  
  940. #ifdef LEXDB
  941. /* test program reading a stream of lexemes and printing the tokens.
  942.  */
  943.  
  944.  
  945.  
  946.     int
  947. main(argc,argv)
  948. int argc; char * argv[];
  949. {
  950.         cmdid="lextest";
  951.         if (argc<2) {
  952.         aputs("No input file\n",stderr);
  953.         exitmain(EXIT_FAILURE);
  954.         }
  955.         if ((finptr=fopen(argv[1], "r")) == NULL) {
  956.         faterror("can't open input file %s",argv[1]);
  957.         }
  958.         Lexinit();
  959.         while (nexttok != EOFILE) {
  960.         switch (nexttok) {
  961.  
  962.         case ID:
  963.                 VOID printf("ID: %s",NextString);
  964.                 break;
  965.  
  966.         case NUM:
  967.         if (hshenter)
  968.                    VOID printf("NUM: %s, index: %d",nexthsh->num, nexthsh-hshtab);
  969.                 else
  970.                    VOID printf("NUM, unentered: %s",NextString);
  971.                 hshenter = !hshenter; /*alternate between dates and numbers*/
  972.                 break;
  973.  
  974.         case COLON:
  975.                 VOID printf("COLON"); break;
  976.  
  977.         case SEMI:
  978.                 VOID printf("SEMI"); break;
  979.  
  980.         case STRING:
  981.                 readstring();
  982.                 VOID printf("STRING"); break;
  983.  
  984.         case UNKN:
  985.                 VOID printf("UNKN"); break;
  986.  
  987.         default:
  988.                 VOID printf("DEFAULT"); break;
  989.         }
  990.         VOID printf(" | ");
  991.         nextlex();
  992.         }
  993.         VOID printf("\nEnd of lexical analyzer test\n");
  994.     exitmain(EXIT_SUCCESS);
  995. }
  996.  
  997. exiting void exiterr() { _exit(EXIT_FAILURE); }
  998.  
  999.  
  1000. #endif
  1001.