home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / rcslex.c < prev    next >
C/C++ Source or Header  |  1992-02-17  |  29KB  |  1,255 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, 1991, 1992 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 2, 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.13  1992/02/17  23:02:27  eggert
  43.  * Work around NFS mmap SIGBUS problem.
  44.  *
  45.  * Revision 5.12  1992/01/06  02:42:34  eggert
  46.  * Use OPEN_O_BINARY if mode contains 'b'.
  47.  *
  48.  * Revision 5.11  1991/11/03  03:30:44  eggert
  49.  * Fix porting bug to ancient hosts lacking vfprintf.
  50.  *
  51.  * Revision 5.10  1991/10/07  17:32:46  eggert
  52.  * Support piece tables even if !has_mmap.
  53.  *
  54.  * Revision 5.9  1991/09/24  00:28:42  eggert
  55.  * Don't export errsay().
  56.  *
  57.  * Revision 5.8  1991/08/19  03:13:55  eggert
  58.  * Add eoflex(), mmap support.  Tune.
  59.  *
  60.  * Revision 5.7  1991/04/21  11:58:26  eggert
  61.  * Add MS-DOS support.
  62.  *
  63.  * Revision 5.6  1991/02/25  07:12:42  eggert
  64.  * Work around fputs bug.  strsave -> str_save (DG/UX name clash)
  65.  *
  66.  * Revision 5.5  1990/12/04  05:18:47  eggert
  67.  * Use -I for prompts and -q for diagnostics.
  68.  *
  69.  * Revision 5.4  1990/11/19  20:05:28  hammer
  70.  * no longer gives warning about unknown keywords if -q is specified
  71.  *
  72.  * Revision 5.3  1990/11/01  05:03:48  eggert
  73.  * When ignoring unknown phrases, copy them to the output RCS file.
  74.  *
  75.  * Revision 5.2  1990/09/04  08:02:27  eggert
  76.  * Count RCS lines better.
  77.  *
  78.  * Revision 5.1  1990/08/29  07:14:03  eggert
  79.  * Work around buggy compilers with defective argument promotion.
  80.  *
  81.  * Revision 5.0  1990/08/22  08:12:55  eggert
  82.  * Remove compile-time limits; use malloc instead.
  83.  * Report errno-related errors with perror().
  84.  * Ansify and Posixate.  Add support for ISO 8859.
  85.  * Use better hash function.
  86.  *
  87.  * Revision 4.6  89/05/01  15:13:07  narten
  88.  * changed copyright header to reflect current distribution rules
  89.  *
  90.  * Revision 4.5  88/08/28  15:01:12  eggert
  91.  * Don't loop when writing error messages to a full filesystem.
  92.  * Flush stderr/stdout when mixing output.
  93.  * Yield exit status compatible with diff(1).
  94.  * Shrink stdio code size; allow cc -R; remove lint.
  95.  *
  96.  * Revision 4.4  87/12/18  11:44:47  narten
  97.  * fixed to use "varargs" in "fprintf"; this is required if it is to
  98.  * work on a SPARC machine such as a Sun-4
  99.  *
  100.  * Revision 4.3  87/10/18  10:37:18  narten
  101.  * Updating version numbers. Changes relative to 1.1 actually relative
  102.  * to version 4.1
  103.  *
  104.  * Revision 1.3  87/09/24  14:00:17  narten
  105.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
  106.  * warnings)
  107.  *
  108.  * Revision 1.2  87/03/27  14:22:33  jenkins
  109.  * Port to suns
  110.  *
  111.  * Revision 4.1  83/03/25  18:12:51  wft
  112.  * Only changed $Header to $Id.
  113.  *
  114.  * Revision 3.3  82/12/10  16:22:37  wft
  115.  * Improved error messages, changed exit status on error to 1.
  116.  *
  117.  * Revision 3.2  82/11/28  21:27:10  wft
  118.  * Renamed ctab to map and included EOFILE; ctab is now a macro in rcsbase.h.
  119.  * Added fflsbuf(), fputs(), and fprintf(), which abort the RCS operations
  120.  * properly in case there is an IO-error (e.g., file system full).
  121.  *
  122.  * Revision 3.1  82/10/11  19:43:56  wft
  123.  * removed unused label out:;
  124.  * made sure all calls to getc() return into an integer, not a char.
  125.  */
  126.  
  127.  
  128. /*
  129. #define LEXDB
  130. */
  131. /* version LEXDB is for testing the lexical analyzer. The testprogram
  132.  * reads a stream of lexemes, enters the revision numbers into the
  133.  * hashtable, and prints the recognized tokens. Keywords are recognized
  134.  * as identifiers.
  135.  */
  136.  
  137.  
  138.  
  139. #include "rcsbase.h"
  140.  
  141. libId(lexId, "$Id: rcslex.c,v 5.13 1992/02/17 23:02:27 eggert Exp $")
  142.  
  143. static struct hshentry *nexthsh;  /*pointer to next hash entry, set by lookup*/
  144.  
  145. enum tokens     nexttok;    /*next token, set by nextlex                    */
  146.  
  147. int             hshenter;   /*if true, next suitable lexeme will be entered */
  148.                             /*into the symbol table. Handle with care.      */
  149. int             nextc;      /*next input character, initialized by Lexinit  */
  150.  
  151. unsigned long    rcsline;    /*current line-number of input            */
  152. int             nerror;     /*counter for errors                            */
  153. int             quietflag;  /*indicates quiet mode                          */
  154. RILE *        finptr;        /*input file descriptor                */
  155.  
  156. FILE *          frewrite;   /*file descriptor for echoing input             */
  157.  
  158. FILE *        foutptr;    /* copy of frewrite, but 0 to suppress echo  */
  159.  
  160. static struct buf tokbuf;   /* token buffer                    */
  161.  
  162. char const *    NextString; /* next token                    */
  163.  
  164. /*
  165.  * Our hash algorithm is h[0] = 0, h[i+1] = 4*h[i] + c,
  166.  * so hshsize should be odd.
  167.  * See B J McKenzie, R Harries & T Bell, Selecting a hashing algorithm,
  168.  * Software--practice & experience 20, 2 (Feb 1990), 209-224.
  169.  */
  170. #ifndef hshsize
  171. #    define hshsize 511
  172. #endif
  173.  
  174. static struct hshentry *hshtab[hshsize]; /*hashtable                */
  175.  
  176. static int ignored_phrases; /* have we ignored phrases in this RCS file? */
  177.  
  178.     void
  179. warnignore()
  180. {
  181.     if (! (ignored_phrases|quietflag)) {
  182.     ignored_phrases = true;
  183.     warn("Unknown phrases like `%s ...;' are in the RCS file.", NextString);
  184.     }
  185. }
  186.  
  187.  
  188.  
  189.     static void
  190. lookup(str)
  191.     char const *str;
  192. /* Function: Looks up the character string pointed to by str in the
  193.  * hashtable. If the string is not present, a new entry for it is created.
  194.  * In any case, the address of the corresponding hashtable entry is placed
  195.  * into nexthsh.
  196.  */
  197. {
  198.     register unsigned ihash;  /* index into hashtable */
  199.     register char const *sp;
  200.     register struct hshentry *n, **p;
  201.  
  202.         /* calculate hash code */
  203.     sp = str;
  204.         ihash = 0;
  205.     while (*sp)
  206.         ihash  =  (ihash<<2) + *sp++;
  207.     ihash %= hshsize;
  208.  
  209.     for (p = &hshtab[ihash];  ;  p = &n->nexthsh)
  210.         if (!(n = *p)) {
  211.             /* empty slot found */
  212.             *p = n = ftalloc(struct hshentry);
  213.             n->num = fstr_save(str);
  214.             n->nexthsh = nil;
  215. #            ifdef LEXDB
  216.                 VOID printf("\nEntered: %s at %u ", str, ihash);
  217. #            endif
  218.             break;
  219.         } else if (strcmp(str, n->num) == 0)
  220.             /* match found */
  221.             break;
  222.     nexthsh = n;
  223.     NextString = n->num;
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.     void
  232. Lexinit()
  233. /* Function: Initialization of lexical analyzer:
  234.  * initializes the hashtable,
  235.  * initializes nextc, nexttok if finptr != 0
  236.  */
  237. {       register int            c;
  238.  
  239.     for (c = hshsize;  0 <= --c;  ) {
  240.         hshtab[c] = nil;
  241.         }
  242.  
  243.     nerror = 0;
  244.     if (finptr) {
  245.         foutptr = 0;
  246.         hshenter = true;
  247.         ignored_phrases = false;
  248.         rcsline = 1;
  249.         bufrealloc(&tokbuf, 2);
  250.         Iget(finptr, nextc);
  251.                 nextlex();            /*initial token*/
  252.         }
  253. }
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.     void
  262. nextlex()
  263.  
  264. /* Function: Reads the next token and sets nexttok to the next token code.
  265.  * Only if hshenter is set, a revision number is entered into the
  266.  * hashtable and a pointer to it is placed into nexthsh.
  267.  * This is useful for avoiding that dates are placed into the hashtable.
  268.  * For ID's and NUM's, NextString is set to the character string.
  269.  * Assumption: nextc contains the next character.
  270.  */
  271. {       register c;
  272.     declarecache;
  273.     register FILE *frew;
  274.         register char * sp;
  275.     char const *limit;
  276.         register enum tokens d;
  277.     register RILE *fin;
  278.  
  279.     fin=finptr; frew=foutptr;
  280.     setupcache(fin); cache(fin);
  281.     c = nextc;
  282.  
  283.     for (;;) { switch ((d = ctab[c])) {
  284.  
  285.     default:
  286.         fatserror("unknown character `%c'", c);
  287.         /*NOTREACHED*/
  288.  
  289.         case NEWLN:
  290.         ++rcsline;
  291. #               ifdef LEXDB
  292.         afputc('\n',stdout);
  293. #               endif
  294.                 /* Note: falls into next case */
  295.  
  296.         case SPACE:
  297.         GETC(frew, c);
  298.         continue;
  299.  
  300.         case DIGIT:
  301.         sp = tokbuf.string;
  302.         limit = sp + tokbuf.size;
  303.         *sp++ = c;
  304.         for (;;) {
  305.             GETC(frew, c);
  306.             if ((d=ctab[c])!=DIGIT && d!=PERIOD)
  307.                 break;
  308.                         *sp++ = c;         /* 1.2. and 1.2 are different */
  309.             if (limit <= sp)
  310.                 sp = bufenlarge(&tokbuf, &limit);
  311.                 }
  312.         *sp = 0;
  313.         if (hshenter)
  314.             lookup(tokbuf.string);
  315.         else
  316.             NextString = fstr_save(tokbuf.string);
  317.         d = NUM;
  318.         break;
  319.  
  320.  
  321.         case LETTER:
  322.     case Letter:
  323.         sp = tokbuf.string;
  324.         limit = sp + tokbuf.size;
  325.         *sp++ = c;
  326.         for (;;) {
  327.             GETC(frew, c);
  328.             if ((d=ctab[c])!=LETTER && d!=Letter && d!=DIGIT && d!=IDCHAR)
  329.                 break;
  330.                         *sp++ = c;
  331.             if (limit <= sp)
  332.                 sp = bufenlarge(&tokbuf, &limit);
  333.                 }
  334.         *sp = 0;
  335.         NextString = fstr_save(tokbuf.string);
  336.         d = ID;  /* may be ID or keyword */
  337.         break;
  338.  
  339.         case SBEGIN: /* long string */
  340.         d = STRING;
  341.                 /* note: only the initial SBEGIN has been read*/
  342.                 /* read the string, and reset nextc afterwards*/
  343.         break;
  344.  
  345.     case COLON:
  346.     case SEMI:
  347.         GETC(frew, c);
  348.         break;
  349.     } break; }
  350.     nextc = c;
  351.     nexttok = d;
  352.     uncache(fin);
  353. }
  354.  
  355.     int
  356. eoflex()
  357. /*
  358.  * Yield true if we look ahead to the end of the input, false otherwise.
  359.  * nextc becomes undefined at end of file.
  360.  */
  361. {
  362.     register int c;
  363.     declarecache;
  364.     register FILE *fout;
  365.     register RILE *fin;
  366.  
  367.     c = nextc;
  368.     fin = finptr;
  369.     fout = foutptr;
  370.     setupcache(fin); cache(fin);
  371.  
  372.     for (;;) {
  373.         switch (ctab[c]) {
  374.             default:
  375.                 nextc = c;
  376.                 uncache(fin);
  377.                 return false;
  378.  
  379.             case NEWLN:
  380.                 ++rcsline;
  381.                 /* fall into */
  382.             case SPACE:
  383.                 cachegeteof(c, {uncache(fin);return true;});
  384.                 break;
  385.         }
  386.         if (fout)
  387.             aputc(c, fout);
  388.     }
  389. }
  390.  
  391.  
  392. int getlex(token)
  393. enum tokens token;
  394. /* Function: Checks if nexttok is the same as token. If so,
  395.  * advances the input by calling nextlex and returns true.
  396.  * otherwise returns false.
  397.  * Doesn't work for strings and keywords; loses the character string for ids.
  398.  */
  399. {
  400.         if (nexttok==token) {
  401.                 nextlex();
  402.                 return(true);
  403.         } else  return(false);
  404. }
  405.  
  406.     int
  407. getkeyopt(key)
  408.     char const *key;
  409. /* Function: If the current token is a keyword identical to key,
  410.  * advances the input by calling nextlex and returns true;
  411.  * otherwise returns false.
  412.  */
  413. {
  414.     if (nexttok==ID  &&  strcmp(key,NextString) == 0) {
  415.          /* match found */
  416.          ffree1(NextString);
  417.          nextlex();
  418.          return(true);
  419.         }
  420.         return(false);
  421. }
  422.  
  423.     void
  424. getkey(key)
  425.     char const *key;
  426. /* Check that the current input token is a keyword identical to key,
  427.  * and advance the input by calling nextlex.
  428.  */
  429. {
  430.     if (!getkeyopt(key))
  431.         fatserror("missing '%s' keyword", key);
  432. }
  433.  
  434.     void
  435. getkeystring(key)
  436.     char const *key;
  437. /* Check that the current input token is a keyword identical to key,
  438.  * and advance the input by calling nextlex; then look ahead for a string.
  439.  */
  440. {
  441.     getkey(key);
  442.     if (nexttok != STRING)
  443.         fatserror("missing string after '%s' keyword", key);
  444. }
  445.  
  446.  
  447.     char const *
  448. getid()
  449. /* Function: Checks if nexttok is an identifier. If so,
  450.  * advances the input by calling nextlex and returns a pointer
  451.  * to the identifier; otherwise returns nil.
  452.  * Treats keywords as identifiers.
  453.  */
  454. {
  455.     register char const *name;
  456.         if (nexttok==ID) {
  457.                 name = NextString;
  458.                 nextlex();
  459.                 return name;
  460.         } else  return nil;
  461. }
  462.  
  463.  
  464. struct hshentry * getnum()
  465. /* Function: Checks if nexttok is a number. If so,
  466.  * advances the input by calling nextlex and returns a pointer
  467.  * to the hashtable entry. Otherwise returns nil.
  468.  * Doesn't work if hshenter is false.
  469.  */
  470. {
  471.         register struct hshentry * num;
  472.         if (nexttok==NUM) {
  473.                 num=nexthsh;
  474.                 nextlex();
  475.                 return num;
  476.         } else  return nil;
  477. }
  478.  
  479.     struct cbuf
  480. getphrases(key)
  481.     char const *key;
  482. /* Get a series of phrases that do not start with KEY, yield resulting buffer.
  483.  * Stop when the next phrase starts with a token that is not an identifier,
  484.  * or is KEY.
  485.  * Assume !foutptr.
  486.  */
  487. {
  488.     declarecache;
  489.     register int c;
  490.     register char *p;
  491.     char const *limit;
  492.     register char const *ki, *kn;
  493.     struct cbuf r;
  494.     struct buf b;
  495.     register RILE *fin;
  496.  
  497.     if (nexttok!=ID  ||  strcmp(NextString,key) == 0) {
  498.     r.string = 0;
  499.     r.size = 0;
  500.     return r;
  501.     } else {
  502.     warnignore();
  503.     fin = finptr;
  504.     setupcache(fin); cache(fin);
  505.     bufautobegin(&b);
  506.     bufscpy(&b, NextString);
  507.     ffree1(NextString);
  508.     p = b.string + strlen(b.string);
  509.     limit = b.string + b.size;
  510.     c = nextc;
  511.     for (;;) {
  512.         for (;;) {
  513.         if (limit <= p)
  514.             p = bufenlarge(&b, &limit);
  515.         *p++ = c;
  516.         switch (ctab[c]) {
  517.             default:
  518.             fatserror("unknown character `%c'", c);
  519.             /*NOTREACHED*/
  520.             case NEWLN:
  521.             ++rcsline;
  522.             /* fall into */
  523.             case COLON: case DIGIT: case LETTER: case Letter:
  524.             case PERIOD: case SPACE:
  525.             cacheget(c);
  526.             continue;
  527.             case SBEGIN: /* long string */
  528.             for (;;) {
  529.                 for (;;) {
  530.                 if (limit <= p)
  531.                     p = bufenlarge(&b, &limit);
  532.                 cacheget(c);
  533.                 *p++ = c;
  534.                 switch (c) {
  535.                     case '\n':
  536.                     ++rcsline;
  537.                     /* fall into */
  538.                     default:
  539.                     continue;
  540.  
  541.                     case SDELIM:
  542.                     break;
  543.                 }
  544.                 break;
  545.                 }
  546.                 cacheget(c);
  547.                 if (c != SDELIM)
  548.                 break;
  549.                 if (limit <= p)
  550.                 p = bufenlarge(&b, &limit);
  551.                 *p++ = c;
  552.             }
  553.             continue;
  554.             case SEMI:
  555.             cacheget(c);
  556.             if (ctab[c] == NEWLN) {
  557.                 ++rcsline;
  558.                 if (limit <= p)
  559.                 p = bufenlarge(&b, &limit);
  560.                 *p++ = c;
  561.                 cacheget(c);
  562.             }
  563.             for (;;) {
  564.                 switch (ctab[c]) {
  565.                 case NEWLN:
  566.                     ++rcsline;
  567.                     /* fall into */
  568.                 case SPACE:
  569.                     cacheget(c);
  570.                     continue;
  571.  
  572.                 default: break;
  573.                 }
  574.                 break;
  575.             }
  576.             break;
  577.         }
  578.         break;
  579.         }
  580.         switch (ctab[c]) {
  581.         case LETTER:
  582.         case Letter:
  583.             for (kn = key;  c && *kn==c;  kn++)
  584.             cacheget(c);
  585.             if (!*kn)
  586.             switch (ctab[c]) {
  587.                 case DIGIT: case LETTER: case Letter:
  588.                 break;
  589.                 default:
  590.                 nextc = c;
  591.                 NextString = fstr_save(key);
  592.                 nexttok = ID;
  593.                 uncache(fin);
  594.                 goto returnit;
  595.             }
  596.             for (ki=key; ki<kn; ) {
  597.             if (limit <= p)
  598.                 p = bufenlarge(&b, &limit);
  599.             *p++ = *ki++;
  600.             }
  601.             break;
  602.  
  603.         default:
  604.             nextc = c;
  605.             uncache(fin);
  606.             nextlex();
  607.             goto returnit;
  608.         }
  609.     }
  610.     returnit:
  611.     return bufremember(&b, (size_t)(p - b.string));
  612.     }
  613. }
  614.  
  615.  
  616.     void
  617. readstring()
  618. /* skip over characters until terminating single SDELIM        */
  619. /* If foutptr is set, copy every character read to foutptr.    */
  620. /* Does not advance nextlex at the end.                        */
  621. {       register c;
  622.     declarecache;
  623.     register FILE *frew;
  624.     register RILE *fin;
  625.     fin=finptr; frew=foutptr;
  626.     setupcache(fin); cache(fin);
  627.     for (;;) {
  628.         GETC(frew, c);
  629.         switch (c) {
  630.             case '\n':
  631.             ++rcsline;
  632.             break;
  633.  
  634.             case SDELIM:
  635.             GETC(frew, c);
  636.             if (c != SDELIM) {
  637.                 /* end of string */
  638.                 nextc = c;
  639.                 uncache(fin);
  640.                 return;
  641.             }
  642.             break;
  643.         }
  644.     }
  645. }
  646.  
  647.  
  648.     void
  649. printstring()
  650. /* Function: copy a string to stdout, until terminated with a single SDELIM.
  651.  * Does not advance nextlex at the end.
  652.  */
  653. {
  654.         register c;
  655.     declarecache;
  656.     register FILE *fout;
  657.     register RILE *fin;
  658.     fin=finptr;
  659.     fout = stdout;
  660.     setupcache(fin); cache(fin);
  661.     for (;;) {
  662.         cacheget(c);
  663.         switch (c) {
  664.             case '\n':
  665.             ++rcsline;
  666.             break;
  667.             case SDELIM:
  668.             cacheget(c);
  669.             if (c != SDELIM) {
  670.                                 nextc=c;
  671.                 uncache(fin);
  672.                                 return;
  673.                         }
  674.             break;
  675.                 }
  676.         aputc(c,fout);
  677.         }
  678. }
  679.  
  680.  
  681.  
  682.     struct cbuf
  683. savestring(target)
  684.     struct buf *target;
  685. /* Copies a string terminated with SDELIM from file finptr to buffer target.
  686.  * Double SDELIM is replaced with SDELIM.
  687.  * If foutptr is set, the string is also copied unchanged to foutptr.
  688.  * Does not advance nextlex at the end.
  689.  * Yield a copy of *TARGET, except with exact length.
  690.  */
  691. {
  692.         register c;
  693.     declarecache;
  694.     register FILE *frew;
  695.     register char *tp;
  696.     register RILE *fin;
  697.     char const *limit;
  698.     struct cbuf r;
  699.  
  700.     fin=finptr; frew=foutptr;
  701.     setupcache(fin); cache(fin);
  702.     tp = target->string;  limit = tp + target->size;
  703.     for (;;) {
  704.         GETC(frew, c);
  705.         switch (c) {
  706.             case '\n':
  707.             ++rcsline;
  708.             break;
  709.             case SDELIM:
  710.             GETC(frew, c);
  711.             if (c != SDELIM) {
  712.                                 /* end of string */
  713.                                 nextc=c;
  714.                 r.string = target->string;
  715.                 r.size = tp - r.string;
  716.                 uncache(fin);
  717.                 return r;
  718.                         }
  719.             break;
  720.                 }
  721.         if (tp == limit)
  722.             tp = bufenlarge(target, &limit);
  723.         *tp++ = c;
  724.         }
  725. }
  726.  
  727.  
  728.     char *
  729. checkid(id, delimiter)
  730.     register char *id;
  731.     int delimiter;
  732. /*   Function:  check whether the string starting at id is an   */
  733. /*        identifier and return a pointer to the delimiter*/
  734. /*        after the identifier.  White space, delim and 0 */
  735. /*              are legal delimiters.  Aborts the program if not*/
  736. /*              a legal identifier. Useful for checking commands*/
  737. /*        If !delim, the only delimiter is 0.        */
  738. {
  739.         register enum  tokens  d;
  740.         register char    *temp;
  741.         register char    c,tc;
  742.     register char delim = delimiter;
  743.  
  744.     temp = id;
  745.     if ((d = ctab[(unsigned char)(c = *id)])==LETTER || d==Letter) {
  746.         while ((d = ctab[(unsigned char)(c = *++id)])==LETTER
  747.         || d==Letter || d==DIGIT || d==IDCHAR
  748.         )
  749.         continue;
  750.         if (c  &&  (!delim || c!=delim && c!=' ' && c!='\t' && c!='\n')) {
  751.                 /* append \0 to end of id before error message */
  752.                 tc = c;
  753.         while ((c=(*++id)) && c!=' ' && c!='\t' && c!='\n' && c!=delim)
  754.             continue;
  755.                 *id = '\0';
  756.         faterror("invalid character %c in identifier `%s'",tc,temp);
  757.         }
  758.         } else {
  759.             /* append \0 to end of id before error message */
  760.         while ((c=(*++id)) && c!=' ' && c!='\t' && c!='\n' && c!=delim)
  761.         continue;
  762.             *id = '\0';
  763.         faterror("identifier `%s' doesn't start with letter", temp);
  764.         }
  765.     return id;
  766. }
  767.  
  768.     void
  769. checksid(id)
  770.     char *id;
  771. /* Check whether the string ID is an identifier.  */
  772. {
  773.     VOID checkid(id, 0);
  774. }
  775.  
  776.  
  777.     static RILE *
  778. #if has_mmap && large_memory
  779. fd2_RILE(fd, filename, status)
  780. #else
  781. fd2RILE(fd, filename, mode, status)
  782.     char const *mode;
  783. #endif
  784.     int fd;
  785.     char const *filename;
  786.     register struct stat *status;
  787. {
  788.     struct stat st;
  789.  
  790.     if (!status)
  791.         status = &st;
  792.     if (fstat(fd, status) != 0)
  793.         efaterror(filename);
  794.     if (!S_ISREG(status->st_mode)) {
  795.         error("`%s' is not a regular file", filename);
  796.         VOID close(fd);
  797.         errno = EINVAL;
  798.         return 0;
  799.     } else {
  800.  
  801. #        if ! (has_mmap && large_memory)
  802.         FILE *stream;
  803.         if (!(stream = fdopen(fd, mode)))
  804.             efaterror(filename);
  805. #        endif
  806.  
  807. #        if !large_memory
  808.         return stream;
  809. #        else
  810. #        define RILES 3
  811.         {
  812.             static RILE rilebuf[RILES];
  813.  
  814.             register RILE *f;
  815.             size_t s = status->st_size;
  816.  
  817.             if (s != status->st_size)
  818.                 faterror("`%s' is enormous", filename);
  819.             for (f = rilebuf;  f->base;  f++)
  820.                 if (f == rilebuf+RILES)
  821.                     faterror("too many RILEs");
  822.             if (!s) {
  823.                 static unsigned char dummy;
  824.                 f->base = &dummy;
  825.             } else {
  826. #                if has_mmap
  827.                 catchmmapints();
  828.                 if (
  829.                     (f->base = (unsigned char *)mmap(
  830.                     (caddr_t)0, s, PROT_READ, MAP_SHARED,
  831.                     fd, (off_t)0
  832.                     )) == (unsigned char *)-1
  833.                 )
  834.                     efaterror("mmap");
  835. #                else
  836.                     f->base = tnalloc(unsigned char, s);
  837. #                endif
  838.             }
  839.             f->ptr = f->base;
  840.             f->lim = f->base + s;
  841. #            if has_mmap
  842.                 f->fd = fd;
  843. #            else
  844.                 f->readlim = f->base;
  845.                 f->stream = stream;
  846. #            endif
  847.             if_advise_access(s, f, MADV_SEQUENTIAL);
  848.             return f;
  849.         }
  850. #        endif
  851.     }
  852. }
  853.  
  854. #if !has_mmap && large_memory
  855.     int
  856. Igetmore(f)
  857.     register RILE *f;
  858. {
  859.     register fread_type r;
  860.     register size_t s = f->lim - f->readlim;
  861.  
  862.     if (BUFSIZ < s)
  863.         s = BUFSIZ;
  864.     if (!(r = Fread(f->readlim, sizeof(*f->readlim), s, f->stream))) {
  865.         testIerror(f->stream);
  866.         f->lim = f->readlim;  /* The file might have shrunk!  */
  867.         return 0;
  868.     }
  869.     f->readlim += r;
  870.     return 1;
  871. }
  872. #endif
  873.  
  874. #if has_madvise && has_mmap && large_memory
  875.     void
  876. advise_access(f, advice)
  877.     register RILE *f;
  878.     int advice;
  879. {
  880.     if (madvise((caddr_t)f->base, (size_t)(f->lim - f->base), advice) != 0)
  881.         efaterror("madvise");
  882. }
  883. #endif
  884.  
  885.     RILE *
  886. #if has_mmap && large_memory
  887. I_open(filename, status)
  888. #else
  889. Iopen(filename, mode, status)
  890.     char const *mode;
  891. #endif
  892.     char const *filename;
  893.     struct stat *status;
  894. /* Open FILENAME for reading, yield its descriptor, and set *STATUS.  */
  895. {
  896.     int fd = open(filename, O_RDONLY
  897. #        if OPEN_O_BINARY  &&  !(has_mmap && large_memory)
  898.             |  (strchr(mode,'b') ? OPEN_O_BINARY : 0)
  899. #        endif
  900.     );
  901.  
  902.     if (fd < 0)
  903.         return 0;
  904. #    if has_mmap && large_memory
  905.         return fd2_RILE(fd, filename, status);
  906. #    else
  907.         return fd2RILE(fd, filename, mode, status);
  908. #    endif
  909. }
  910.  
  911.  
  912. #if !large_memory
  913. #    define Iclose(f) fclose(f)
  914. #else
  915.         static int
  916.     Iclose(f)
  917.         register RILE *f;
  918.     {
  919. #        if has_mmap
  920.         size_t s = f->lim - f->base;
  921.         if (s  &&  munmap((caddr_t)f->base, s) != 0)
  922.             return -1;
  923.         f->base = 0;
  924.         return close(f->fd);
  925. #        else
  926.         tfree(f->base);
  927.         f->base = 0;
  928.         return fclose(f->stream);
  929. #        endif
  930.     }
  931. #endif
  932.  
  933.  
  934. static int Oerrloop;
  935.  
  936.     exiting void
  937. Oerror()
  938. {
  939.     if (Oerrloop)
  940.         exiterr();
  941.     Oerrloop = true;
  942.     efaterror("output error");
  943. }
  944.  
  945. exiting void Ieof() { fatserror("unexpected end of file"); }
  946. exiting void Ierror() { efaterror("input error"); }
  947. void testIerror(f) FILE *f; { if (ferror(f)) Ierror(); }
  948. void testOerror(o) FILE *o; { if (ferror(o)) Oerror(); }
  949.  
  950. void Ifclose(f) RILE *f; { if (f && Iclose(f)!=0) Ierror(); }
  951. void Ofclose(f) FILE *f; { if (f && fclose(f)!=0) Oerror(); }
  952. void Izclose(p) RILE **p; { Ifclose(*p); *p = 0; }
  953. void Ozclose(p) FILE **p; { Ofclose(*p); *p = 0; }
  954.  
  955. #if !large_memory
  956.     void
  957. testIeof(f)
  958.     FILE *f;
  959. {
  960.     testIerror(f);
  961.     if (feof(f))
  962.         Ieof();
  963. }
  964. void Irewind(f) FILE *f; { if (fseek(f,0L,SEEK_SET) != 0) Ierror(); }
  965. #endif
  966.  
  967. void eflush()
  968. {
  969.     if (fflush(stderr) != 0  &&  !Oerrloop)
  970.         Oerror();
  971. }
  972.  
  973. void oflush()
  974. {
  975.     if (fflush(workstdout ? workstdout : stdout) != 0  &&  !Oerrloop)
  976.         Oerror();
  977. }
  978.  
  979.     static exiting void
  980. fatcleanup(already_newline)
  981.     int already_newline;
  982. {
  983.     VOID fprintf(stderr, already_newline+"\n%s aborted\n", cmdid);
  984.     exiterr();
  985. }
  986.  
  987. static void errsay() { oflush(); aprintf(stderr,"%s error: ",cmdid); nerror++; }
  988. static void fatsay() { oflush(); VOID fprintf(stderr,"%s error: ",cmdid); }
  989.  
  990. void eerror(s) char const *s; { enerror(errno,s); }
  991.  
  992.     void
  993. enerror(e,s)
  994.     int e;
  995.     char const *s;
  996. {
  997.     errsay();
  998.     errno = e;
  999.     perror(s);
  1000.     eflush();
  1001. }
  1002.  
  1003. exiting void efaterror(s) char const *s; { enfaterror(errno,s); }
  1004.  
  1005.     exiting void
  1006. enfaterror(e,s)
  1007.     int e;
  1008.     char const *s;
  1009. {
  1010.     fatsay();
  1011.     errno = e;
  1012.     perror(s);
  1013.     fatcleanup(true);
  1014. }
  1015.  
  1016. #if has_prototypes
  1017.     void
  1018. error(char const *format,...)
  1019. #else
  1020.     /*VARARGS1*/ void error(format, va_alist) char const *format; va_dcl
  1021. #endif
  1022. /* non-fatal error */
  1023. {
  1024.     va_list args;
  1025.     errsay();
  1026.     vararg_start(args, format);
  1027.     fvfprintf(stderr, format, args);
  1028.     va_end(args);
  1029.     afputc('\n',stderr);
  1030.     eflush();
  1031. }
  1032.  
  1033. #if has_prototypes
  1034.     exiting void
  1035. fatserror(char const *format,...)
  1036. #else
  1037.     /*VARARGS1*/ exiting void
  1038.     fatserror(format, va_alist) char const *format; va_dcl
  1039. #endif
  1040. /* fatal syntax error */
  1041. {
  1042.     va_list args;
  1043.     oflush();
  1044.     VOID fprintf(stderr, "%s: %s:%lu: ", cmdid, RCSfilename, rcsline);
  1045.     vararg_start(args, format);
  1046.     fvfprintf(stderr, format, args);
  1047.     va_end(args);
  1048.     fatcleanup(false);
  1049. }
  1050.  
  1051. #if has_prototypes
  1052.     exiting void
  1053. faterror(char const *format,...)
  1054. #else
  1055.     /*VARARGS1*/ exiting void faterror(format, va_alist)
  1056.     char const *format; va_dcl
  1057. #endif
  1058. /* fatal error, terminates program after cleanup */
  1059. {
  1060.     va_list args;
  1061.     fatsay();
  1062.     vararg_start(args, format);
  1063.     fvfprintf(stderr, format, args);
  1064.     va_end(args);
  1065.     fatcleanup(false);
  1066. }
  1067.  
  1068. #if has_prototypes
  1069.     void
  1070. warn(char const *format,...)
  1071. #else
  1072.     /*VARARGS1*/ void warn(format, va_alist) char const *format; va_dcl
  1073. #endif
  1074. /* prints a warning message */
  1075. {
  1076.     va_list args;
  1077.     oflush();
  1078.     aprintf(stderr,"%s warning: ",cmdid);
  1079.     vararg_start(args, format);
  1080.     fvfprintf(stderr, format, args);
  1081.     va_end(args);
  1082.     afputc('\n',stderr);
  1083.     eflush();
  1084. }
  1085.  
  1086.     void
  1087. redefined(c)
  1088.     int c;
  1089. {
  1090.     warn("redefinition of -%c option", c);
  1091. }
  1092.  
  1093. #if has_prototypes
  1094.     void
  1095. diagnose(char const *format,...)
  1096. #else
  1097.     /*VARARGS1*/ void diagnose(format, va_alist) char const *format; va_dcl
  1098. #endif
  1099. /* prints a diagnostic message */
  1100. /* Unlike the other routines, it does not append a newline. */
  1101. /* This lets some callers suppress the newline, and is faster */
  1102. /* in implementations that flush stderr just at the end of each printf. */
  1103. {
  1104.     va_list args;
  1105.         if (!quietflag) {
  1106.         oflush();
  1107.         vararg_start(args, format);
  1108.         fvfprintf(stderr, format, args);
  1109.         va_end(args);
  1110.         eflush();
  1111.         }
  1112. }
  1113.  
  1114.  
  1115.  
  1116.     void
  1117. afputc(c, f)
  1118. /* Function: afputc(c,f) acts like aputc(c,f), but is smaller and slower.
  1119.  */
  1120.     int c;
  1121.     register FILE *f;
  1122. {
  1123.     aputc(c,f);
  1124. }
  1125.  
  1126.  
  1127.     void
  1128. aputs(s, iop)
  1129.     char const *s;
  1130.     FILE *iop;
  1131. /* Function: Put string s on file iop, abort on error.
  1132.  */
  1133. {
  1134. #if has_fputs
  1135.     if (fputs(s, iop) < 0)
  1136.         Oerror();
  1137. #else
  1138.     awrite(s, strlen(s), iop);
  1139. #endif
  1140. }
  1141.  
  1142.  
  1143.  
  1144.     void
  1145. #if has_prototypes
  1146. fvfprintf(FILE *stream, char const *format, va_list args)
  1147. #else
  1148.     fvfprintf(stream,format,args) FILE *stream; char *format; va_list args;
  1149. #endif
  1150. /* like vfprintf, except abort program on error */
  1151. {
  1152. #if has_vfprintf
  1153.     if (vfprintf(stream, format, args) < 0)
  1154. #else
  1155. #    if has__doprintf
  1156.         _doprintf(stream, format, args);
  1157. #    else
  1158. #    if has__doprnt
  1159.         _doprnt(format, args, stream);
  1160. #    else
  1161.         int *a = (int *)args;
  1162.         VOID fprintf(stream, format,
  1163.             a[0], a[1], a[2], a[3], a[4],
  1164.             a[5], a[6], a[7], a[8], a[9]
  1165.         );
  1166. #    endif
  1167. #    endif
  1168.     if (ferror(stream))
  1169. #endif
  1170.         Oerror();
  1171. }
  1172.  
  1173. #if has_prototypes
  1174.     void
  1175. aprintf(FILE *iop, char const *fmt, ...)
  1176. #else
  1177.     /*VARARGS2*/ void
  1178. aprintf(iop, fmt, va_alist)
  1179. FILE *iop;
  1180. char const *fmt;
  1181. va_dcl
  1182. #endif
  1183. /* Function: formatted output. Same as fprintf in stdio,
  1184.  * but aborts program on error
  1185.  */
  1186. {
  1187.     va_list ap;
  1188.     vararg_start(ap, fmt);
  1189.     fvfprintf(iop, fmt, ap);
  1190.     va_end(ap);
  1191. }
  1192.  
  1193.  
  1194.  
  1195. #ifdef LEXDB
  1196. /* test program reading a stream of lexemes and printing the tokens.
  1197.  */
  1198.  
  1199.  
  1200.  
  1201.     int
  1202. main(argc,argv)
  1203. int argc; char * argv[];
  1204. {
  1205.         cmdid="lextest";
  1206.         if (argc<2) {
  1207.         aputs("No input file\n",stderr);
  1208.         exitmain(EXIT_FAILURE);
  1209.         }
  1210.     if (!(finptr=Iopen(argv[1], FOPEN_R, (struct stat*)0))) {
  1211.         faterror("can't open input file %s",argv[1]);
  1212.         }
  1213.         Lexinit();
  1214.     while (!eoflex()) {
  1215.         switch (nexttok) {
  1216.  
  1217.         case ID:
  1218.                 VOID printf("ID: %s",NextString);
  1219.                 break;
  1220.  
  1221.         case NUM:
  1222.         if (hshenter)
  1223.                    VOID printf("NUM: %s, index: %d",nexthsh->num, nexthsh-hshtab);
  1224.                 else
  1225.                    VOID printf("NUM, unentered: %s",NextString);
  1226.                 hshenter = !hshenter; /*alternate between dates and numbers*/
  1227.                 break;
  1228.  
  1229.         case COLON:
  1230.                 VOID printf("COLON"); break;
  1231.  
  1232.         case SEMI:
  1233.                 VOID printf("SEMI"); break;
  1234.  
  1235.         case STRING:
  1236.                 readstring();
  1237.                 VOID printf("STRING"); break;
  1238.  
  1239.         case UNKN:
  1240.                 VOID printf("UNKN"); break;
  1241.  
  1242.         default:
  1243.                 VOID printf("DEFAULT"); break;
  1244.         }
  1245.         VOID printf(" | ");
  1246.         nextlex();
  1247.         }
  1248.     exitmain(EXIT_SUCCESS);
  1249. }
  1250.  
  1251. exiting void exiterr() { _exit(EXIT_FAILURE); }
  1252.  
  1253.  
  1254. #endif
  1255.