home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / gnu / rcs-5.6.0.1-src.lha / rcs-5.6.0.1 / src / rcslex.c < prev    next >
C/C++ Source or Header  |  1991-11-22  |  28KB  |  1,242 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 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.11  1991/11/03  03:30:44  eggert
  43.  * Fix porting bug to ancient hosts lacking vfprintf.
  44.  *
  45.  * Revision 5.10  1991/10/07  17:32:46  eggert
  46.  * Support piece tables even if !has_mmap.
  47.  *
  48.  * Revision 5.9  1991/09/24  00:28:42  eggert
  49.  * Don't export errsay().
  50.  *
  51.  * Revision 5.8  1991/08/19  03:13:55  eggert
  52.  * Add eoflex(), mmap support.  Tune.
  53.  *
  54.  * Revision 5.7  1991/04/21  11:58:26  eggert
  55.  * Add MS-DOS support.
  56.  *
  57.  * Revision 5.6  1991/02/25  07:12:42  eggert
  58.  * Work around fputs bug.  strsave -> str_save (DG/UX name clash)
  59.  *
  60.  * Revision 5.5  1990/12/04  05:18:47  eggert
  61.  * Use -I for prompts and -q for diagnostics.
  62.  *
  63.  * Revision 5.4  1990/11/19  20:05:28  hammer
  64.  * no longer gives warning about unknown keywords if -q is specified
  65.  *
  66.  * Revision 5.3  1990/11/01  05:03:48  eggert
  67.  * When ignoring unknown phrases, copy them to the output RCS file.
  68.  *
  69.  * Revision 5.2  1990/09/04  08:02:27  eggert
  70.  * Count RCS lines better.
  71.  *
  72.  * Revision 5.1  1990/08/29  07:14:03  eggert
  73.  * Work around buggy compilers with defective argument promotion.
  74.  *
  75.  * Revision 5.0  1990/08/22  08:12:55  eggert
  76.  * Remove compile-time limits; use malloc instead.
  77.  * Report errno-related errors with perror().
  78.  * Ansify and Posixate.  Add support for ISO 8859.
  79.  * Use better hash function.
  80.  *
  81.  * Revision 4.6  89/05/01  15:13:07  narten
  82.  * changed copyright header to reflect current distribution rules
  83.  * 
  84.  * Revision 4.5  88/08/28  15:01:12  eggert
  85.  * Don't loop when writing error messages to a full filesystem.
  86.  * Flush stderr/stdout when mixing output.
  87.  * Yield exit status compatible with diff(1).
  88.  * Shrink stdio code size; allow cc -R; remove lint.
  89.  * 
  90.  * Revision 4.4  87/12/18  11:44:47  narten
  91.  * fixed to use "varargs" in "fprintf"; this is required if it is to
  92.  * work on a SPARC machine such as a Sun-4
  93.  * 
  94.  * Revision 4.3  87/10/18  10:37:18  narten
  95.  * Updating version numbers. Changes relative to 1.1 actually relative
  96.  * to version 4.1
  97.  * 
  98.  * Revision 1.3  87/09/24  14:00:17  narten
  99.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  100.  * warnings)
  101.  * 
  102.  * Revision 1.2  87/03/27  14:22:33  jenkins
  103.  * Port to suns
  104.  * 
  105.  * Revision 4.1  83/03/25  18:12:51  wft
  106.  * Only changed $Header to $Id.
  107.  * 
  108.  * Revision 3.3  82/12/10  16:22:37  wft
  109.  * Improved error messages, changed exit status on error to 1.
  110.  *
  111.  * Revision 3.2  82/11/28  21:27:10  wft
  112.  * Renamed ctab to map and included EOFILE; ctab is now a macro in rcsbase.h.
  113.  * Added fflsbuf(), fputs(), and fprintf(), which abort the RCS operations
  114.  * properly in case there is an IO-error (e.g., file system full).
  115.  *
  116.  * Revision 3.1  82/10/11  19:43:56  wft
  117.  * removed unused label out:;
  118.  * made sure all calls to getc() return into an integer, not a char.
  119.  */
  120.  
  121.  
  122. /*
  123. #define LEXDB
  124. */
  125. /* version LEXDB is for testing the lexical analyzer. The testprogram
  126.  * reads a stream of lexemes, enters the revision numbers into the
  127.  * hashtable, and prints the recognized tokens. Keywords are recognized
  128.  * as identifiers.
  129.  */
  130.  
  131.  
  132.  
  133. #include "rcsbase.h"
  134.  
  135. libId(lexId, "$Id: rcslex.c,v 5.11 1991/11/03 03:30:44 eggert Exp $")
  136.  
  137. static struct hshentry *nexthsh;  /*pointer to next hash entry, set by lookup*/
  138.  
  139. enum tokens     nexttok;    /*next token, set by nextlex                    */
  140.  
  141. int             hshenter;   /*if true, next suitable lexeme will be entered */
  142.                             /*into the symbol table. Handle with care.      */
  143. int             nextc;      /*next input character, initialized by Lexinit  */
  144.  
  145. unsigned long    rcsline;    /*current line-number of input            */
  146. int             nerror;     /*counter for errors                            */
  147. int             quietflag;  /*indicates quiet mode                          */
  148. RILE *        finptr;        /*input file descriptor                */
  149.  
  150. FILE *          frewrite;   /*file descriptor for echoing input             */
  151.  
  152. FILE *        foutptr;    /* copy of frewrite, but 0 to suppress echo  */
  153.  
  154. static struct buf tokbuf;   /* token buffer                    */
  155.  
  156. char const *    NextString; /* next token                    */
  157.  
  158. /*
  159.  * Our hash algorithm is h[0] = 0, h[i+1] = 4*h[i] + c,
  160.  * so hshsize should be odd.
  161.  * See B J McKenzie, R Harries & T Bell, Selecting a hashing algorithm,
  162.  * Software--practice & experience 20, 2 (Feb 1990), 209-224.
  163.  */
  164. #ifndef hshsize
  165. #    define hshsize 511
  166. #endif
  167.  
  168. static struct hshentry *hshtab[hshsize]; /*hashtable                */
  169.  
  170. static int ignored_phrases; /* have we ignored phrases in this RCS file? */
  171.  
  172.     void
  173. warnignore()
  174. {
  175.     if (! (ignored_phrases|quietflag)) {
  176.     ignored_phrases = true;
  177.     warn("Unknown phrases like `%s ...;' are in the RCS file.", NextString);
  178.     }
  179. }
  180.  
  181.  
  182.  
  183.     static void
  184. lookup(str)
  185.     char const *str;
  186. /* Function: Looks up the character string pointed to by str in the
  187.  * hashtable. If the string is not present, a new entry for it is created.
  188.  * In any case, the address of the corresponding hashtable entry is placed
  189.  * into nexthsh.
  190.  */
  191. {
  192.     register unsigned ihash;  /* index into hashtable */
  193.     register char const *sp;
  194.     register struct hshentry *n, **p;
  195.  
  196.         /* calculate hash code */
  197.     sp = str;
  198.         ihash = 0;
  199.     while (*sp)
  200.         ihash  =  (ihash<<2) + *sp++;
  201.     ihash %= hshsize;
  202.  
  203.     for (p = &hshtab[ihash];  ;  p = &n->nexthsh)
  204.         if (!(n = *p)) {
  205.             /* empty slot found */
  206.             *p = n = ftalloc(struct hshentry);
  207.             n->num = fstr_save(str);
  208.             n->nexthsh = nil;
  209. #            ifdef LEXDB
  210.                 VOID printf("\nEntered: %s at %u ", str, ihash);
  211. #            endif
  212.             break;
  213.         } else if (strcmp(str, n->num) == 0)
  214.             /* match found */
  215.             break;
  216.     nexthsh = n;
  217.     NextString = n->num;
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.     void
  226. Lexinit()
  227. /* Function: Initialization of lexical analyzer:
  228.  * initializes the hashtable,
  229.  * initializes nextc, nexttok if finptr != 0
  230.  */
  231. {       register int            c;
  232.  
  233.     for (c = hshsize;  0 <= --c;  ) {
  234.         hshtab[c] = nil;
  235.         }
  236.  
  237.     nerror = 0;
  238.     if (finptr) {
  239.         foutptr = 0;
  240.         hshenter = true;
  241.         ignored_phrases = false;
  242.         rcsline = 1;
  243.         bufrealloc(&tokbuf, 2);
  244.         Iget(finptr, nextc);
  245.                 nextlex();            /*initial token*/
  246.         }
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.     void
  256. nextlex()
  257.  
  258. /* Function: Reads the next token and sets nexttok to the next token code.
  259.  * Only if hshenter is set, a revision number is entered into the
  260.  * hashtable and a pointer to it is placed into nexthsh.
  261.  * This is useful for avoiding that dates are placed into the hashtable.
  262.  * For ID's and NUM's, NextString is set to the character string.
  263.  * Assumption: nextc contains the next character.
  264.  */
  265. {       register c;
  266.     declarecache;
  267.     register FILE *frew;
  268.         register char * sp;
  269.     char const *limit;
  270.         register enum tokens d;
  271.     register RILE *fin;
  272.  
  273.     fin=finptr; frew=foutptr;
  274.     setupcache(fin); cache(fin);
  275.     c = nextc;
  276.  
  277.     for (;;) { switch ((d = ctab[c])) {
  278.  
  279.     default:
  280.         fatserror("unknown character `%c'", c);
  281.         /*NOTREACHED*/
  282.  
  283.