home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-18.59-bin.lha / lib / emacs / 18.59 / etc / etags.c < prev    next >
C/C++ Source or Header  |  1993-05-09  |  34KB  |  1,643 lines

  1. /* Tags file maker to go with GNUmacs
  2.    Copyright (C) 1984, 1987, 1988 Free Software Foundation, Inc. and Ken Arnold
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. In other words, you are welcome to use, share and improve this program.
  19. You are forbidden to forbid anyone else to use, share and improve
  20. what you give them.   Help stamp out software-hoarding!  */
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24.  
  25. /* Define the symbol ETAGS to make the program "etags",
  26.  which makes emacs-style tag tables by default.
  27.  Define CTAGS to make the program "ctags" compatible with the usual one.
  28.  Define neither one to get behavior that depends
  29.  on the name with which the program is invoked
  30.  (but we don't normally compile it that way).  */
  31.  
  32. /* On VMS, CTAGS is not useful, so always do ETAGS.  */
  33. #ifdef VMS
  34. #ifndef ETAGS
  35. #define ETAGS
  36. #endif
  37. #endif
  38.  
  39. /* Exit codes for success and failure.  */
  40.  
  41. #ifdef VMS
  42. #define    GOOD    (1)
  43. #define BAD    (0)
  44. #else
  45. #define    GOOD    (0)
  46. #define    BAD    (1)
  47. #endif
  48.  
  49. #define    reg    register
  50. #define    logical    char
  51.  
  52. #define    TRUE    (1)
  53. #define    FALSE    (0)
  54.  
  55. #define    iswhite(arg)    (_wht[arg])    /* T if char is white        */
  56. #define    begtoken(arg)    (_btk[arg])    /* T if char can start token    */
  57. #define    intoken(arg)    (_itk[arg])    /* T if char can be in token    */
  58. #define    endtoken(arg)    (_etk[arg])    /* T if char ends tokens    */
  59. #define    isgood(arg)    (_gd[arg])    /* T if char can be after ')'    */
  60.  
  61. #define    max(I1,I2)    (I1 > I2 ? I1 : I2)
  62.  
  63. /* cause token checking for typedef, struct, union, enum to distinguish
  64.    keywords from identifier-prefixes (e.g. struct vs struct_tag).  */
  65. #define istoken(s, tok, len) (!strncmp(s,tok,len) && endtoken(*((s)+(len))))
  66.  
  67. struct    nd_st {            /* sorting structure            */
  68.     char    *name;            /* function or type name    */
  69.     char    *file;            /* file name            */
  70.     logical f;            /* use pattern or line no    */
  71.     int    lno;            /* line number tag is on    */
  72.     long    cno;            /* character number line starts on */
  73.     char    *pat;            /* search pattern        */
  74.     logical    been_warned;        /* set if noticed dup        */
  75.     struct    nd_st    *left,*right;    /* left and right sons        */
  76. };
  77.  
  78. long    ftell();
  79. typedef    struct    nd_st    NODE;
  80.  
  81. int number; /* tokens found so far on line starting with # (including #) */
  82. logical gotone,                /* found a func already on line    */
  83.                     /* boolean "func" (see init)    */
  84.     _wht[0177],_etk[0177],_itk[0177],_btk[0177],_gd[0177];
  85.  
  86.     /* typedefs are recognized using a simple finite automata,
  87.      * tydef is its state variable.
  88.      */
  89. typedef enum {none, begin, tag_ok, middle, end } TYST;
  90.  
  91. TYST tydef = none;
  92.  
  93. logical next_token_is_func;
  94.  
  95. char    searchar = '/';            /* use /.../ searches         */
  96.  
  97. int    lineno;            /* line number of current line */
  98. long    charno;            /* current character number */
  99. long    linecharno;        /* character number of start of line */
  100.  
  101. char    *curfile,        /* current input file name        */
  102.     *outfile= 0,        /* output file                */
  103.     *white    = " \f\t\n",    /* white chars                */
  104.     *endtk    = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?",
  105.                 /* token ending chars            */
  106.     *begtk    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$",
  107.                 /* token starting chars            */
  108.     *intk    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789",
  109.                 /* valid in-token chars            */
  110.     *notgd    = ",;";        /* non-valid after-function chars    */
  111.  
  112. int    file_num = 0;        /* current file number            */
  113. int    aflag = 0;        /* -a: append to tags */
  114. int    tflag = 0;        /* -t: create tags for typedefs */
  115. int    uflag = 0;        /* -u: update tags */
  116. int    wflag = 0;        /* -w: suppress warnings */
  117. int    vflag = 0;        /* -v: create vgrind style index output */
  118. int    xflag = 0;        /* -x: create cxref style output */
  119. int    eflag = 0;        /* -e: emacs style output */
  120.  
  121. /* Name this program was invoked with.  */
  122. char *progname;
  123.  
  124. FILE    *inf,            /* ioptr for current input file        */
  125.     *outf;            /* ioptr for tags file            */
  126.  
  127. NODE    *head;            /* the head of the sorted binary tree    */
  128.  
  129. char *savestr();
  130. char *savenstr ();
  131. char *rindex();
  132. char *index();
  133. char *concat ();
  134. void initbuffer ();
  135. long readline ();
  136.  
  137. /* A `struct linebuffer' is a structure which holds a line of text.
  138.  `readline' reads a line from a stream into a linebuffer
  139.  and works regardless of the length of the line.  */
  140.  
  141. struct linebuffer
  142.   {
  143.     long size;
  144.     char *buffer;
  145.   };
  146.  
  147. struct linebuffer lb, lb1;
  148.  
  149. #if 0  /* VMS now provides the `system' function.  */
  150. #ifdef VMS
  151.  
  152. #include <descrip.h>
  153.  
  154. void
  155. system (buf)
  156.      char *buf;
  157. {
  158.   struct dsc$descriptor_s command =
  159.     {
  160.       strlen(buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf
  161.     };
  162.  
  163.   LIB$SPAWN(&command);
  164. }
  165. #endif /* VMS */
  166. #endif /* 0 */
  167.  
  168. main(ac,av)
  169.      int    ac;
  170.      char    *av[];
  171. {
  172.   char cmd[100];
  173.   int i;
  174.   int fflag = 0;
  175.   char *this_file;
  176. #ifdef VMS
  177.   char got_err;
  178.  
  179.   extern char *gfnames();
  180.   extern char *massage_name();
  181. #endif
  182.  
  183.   progname = av[0];
  184.  
  185. #ifdef ETAGS
  186.   eflag = 1;
  187. #else
  188.   eflag = 0;
  189. #endif
  190.  
  191.   while (ac > 1 && av[1][0] == '-')
  192.     {
  193.       for (i=1; av[1][i]; i++)
  194.     {
  195.       switch(av[1][i])
  196.         {
  197. #ifndef VMS  /* These options are useful only with ctags,
  198.         and VMS can't input them, so just omit them.  */
  199.         case 'B':
  200.           searchar='?';
  201.           eflag = 0;
  202.           break;
  203.         case 'F':
  204.           searchar='/';
  205.           eflag = 0;
  206.           break;
  207. #endif
  208.         case 'a':
  209.           aflag++;
  210.           break;
  211.         case 'e':
  212.           eflag++;
  213.           break;
  214.         case 'f':
  215.           if (fflag > 0)
  216.         {
  217.           fprintf(stderr,
  218.               "%s: -f flag may only be given once\n", progname);
  219.           goto usage;
  220.         }
  221.           fflag++, ac--; av++;
  222.           if (ac <= 1 || av[1][0] == '\0')
  223.         {
  224.           fprintf(stderr,
  225.               "%s: -f flag must be followed by a filename\n",
  226.               progname);
  227.           goto usage;
  228.         }
  229.           outfile = av[1];
  230.           goto end_loop;
  231.         case 't':
  232.           tflag++;
  233.           break;
  234. #ifndef VMS
  235.         case 'u':
  236.           uflag++;
  237.           eflag = 0;
  238.           break;
  239. #endif
  240.         case 'w':
  241.           wflag++;
  242.           break;
  243.         case 'v':
  244.           vflag++;
  245.           xflag++;
  246.           eflag = 0;
  247.           break;
  248.         case 'x':
  249.           xflag++;
  250.           eflag = 0;
  251.           break;
  252.         default:
  253.           goto usage;
  254.         }
  255.     }
  256.     end_loop: ;
  257.       ac--; av++;
  258.     }
  259.  
  260.   if (ac <= 1)
  261.     {
  262.     usage:
  263. #ifdef VMS
  264.       fprintf (stderr, "Usage: %s [-aetwvx] [-f outfile] file ...\n", progname);
  265. #else
  266.       fprintf (stderr, "Usage: %s [-BFaetuwvx] [-f outfile] file ...\n", progname);
  267. #endif
  268.       exit(BAD);
  269.     }
  270.  
  271.   if (outfile == 0)
  272.     {
  273.       outfile = eflag ? "TAGS" : "tags";
  274.     }
  275.  
  276.   init();            /* set up boolean "functions"        */
  277.  
  278.   initbuffer (&lb);
  279.   initbuffer (&lb1);
  280.   /*
  281.    * loop through files finding functions
  282.    */
  283.   if (eflag)
  284.     {
  285.       outf = fopen (outfile, aflag ? "a" : "w");
  286.       if (!outf)
  287.     {
  288.       fprintf (stderr, "%s: ", progname);
  289.       perror (outfile);
  290.       exit (BAD);
  291.     }
  292.     }
  293.  
  294.   file_num = 1;
  295. #ifdef VMS
  296.   for (ac--, av++;
  297.        (this_file = gfnames (&ac, &av, &got_err)) != NULL; file_num++)
  298.     {
  299.       if (got_err)
  300.     {
  301.       error("Can't find file %s\n", this_file);
  302.       ac--, av++;
  303.     }
  304.       else
  305.     {
  306.       this_file = massage_name (this_file);
  307. #else     
  308.   for (; file_num < ac; file_num++)
  309.     {
  310.       this_file = av[file_num];
  311.       if (1)
  312.     {
  313. #endif
  314.       find_entries (this_file);
  315.       if (eflag)
  316.         {
  317.           fprintf (outf, "\f\n%s,%d\n",
  318.                this_file, total_size_of_entries (head));
  319.           put_entries (head);
  320.           free_tree (head);
  321.           head = NULL;
  322.         }
  323.     }
  324.     }
  325.  
  326.   if (eflag)
  327.     {
  328.       fclose (outf);
  329.       exit (GOOD);
  330.     }
  331.  
  332.   if (xflag)
  333.     {
  334.       put_entries(head);
  335.       exit(GOOD);
  336.     }
  337.   if (uflag)
  338.     {
  339.       for (i=1; i<ac; i++)
  340.     {
  341. #ifdef AMIGA
  342.       rename(outfile, "OTAGS");
  343.       sprintf(cmd, "egrep >%s -v '\t%s\t' OTAGS", outfile, av[i]);
  344.       system(cmd);
  345.       unlink("OTAGS");
  346. #else
  347.       sprintf(cmd,
  348.           "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
  349.           outfile, av[i], outfile);
  350.       system(cmd);
  351. #endif
  352.     }
  353.       aflag++;
  354.     }
  355.   outf = fopen(outfile, aflag ? "a" : "w");
  356.   if (outf == NULL)
  357.     {
  358.       fprintf (stderr, "%s: ", outfile);
  359.       perror(outfile);
  360.       exit(BAD);
  361.     }
  362.   put_entries(head);
  363.   fclose(outf);
  364. #ifndef VMS
  365.   if (uflag)
  366.     {
  367. #ifdef AMIGA
  368.       sprintf(cmd, "c:sort from %s to %s", outfile, outfile);
  369. #else
  370.       sprintf(cmd, "sort %s -o %s", outfile, outfile);
  371. #endif
  372.       system(cmd);
  373.     }
  374. #endif
  375.   exit(GOOD);
  376. }
  377.  
  378. /*
  379.  * This routine sets up the boolean psuedo-functions which work
  380.  * by seting boolean flags dependent upon the corresponding character
  381.  * Every char which is NOT in that string is not a white char.  Therefore,
  382.  * all of the array "_wht" is set to FALSE, and then the elements
  383.  * subscripted by the chars in "white" are set to TRUE.  Thus "_wht"
  384.  * of a char is TRUE if it is the string "white", else FALSE.
  385.  */
  386. init()
  387. {
  388.  
  389.   reg char *sp;
  390.   reg int i;
  391.  
  392.   for (i = 0; i < 0177; i++)
  393.     {
  394.       _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
  395.       _gd[i] = TRUE;
  396.     }
  397.   for (sp = white; *sp; sp++)
  398.     _wht[*sp] = TRUE;
  399.   for (sp = endtk; *sp; sp++)
  400.     _etk[*sp] = TRUE;
  401.   for (sp = intk; *sp; sp++)
  402.     _itk[*sp] = TRUE;
  403.   for (sp = begtk; *sp; sp++)
  404.     _btk[*sp] = TRUE;
  405.   for (sp = notgd; *sp; sp++)
  406.     _gd[*sp] = FALSE;
  407.   _wht[0] = _wht['\n'];
  408.   _etk[0] = _etk['\n'];
  409.   _btk[0] = _btk['\n'];
  410.   _itk[0] = _itk['\n'];
  411.   _gd[0] = _gd['\n'];
  412. }
  413.  
  414. /*
  415.  * This routine opens the specified file and calls the function
  416.  * which finds the function and type definitions.
  417.  */
  418. find_entries (file)
  419.      char *file;
  420. {
  421.   char *cp;
  422.  
  423.   if ((inf=fopen(file,"r")) == NULL)
  424.     {
  425.       fprintf (stderr, "%s: ", progname);
  426.       perror(file);
  427.       return;
  428.     }
  429.   curfile = savestr(file);
  430.   cp = rindex(file, '.');
  431.   /* .tex, .aux or .bbl implies LaTeX source code */
  432.   if (cp && (!strcmp (cp + 1, "tex") || !strcmp (cp + 1, "aux")
  433.          || !strcmp (cp + 1, "bbl")))
  434.     {
  435.       TEX_funcs(inf);
  436.       fclose(inf);
  437.       return;
  438.     }
  439.   /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
  440.   if (cp && (!strcmp (cp + 1, "l") ||
  441.          !strcmp (cp + 1, "el") ||
  442.          !strcmp (cp + 1, "lsp") ||
  443.          !strcmp (cp + 1, "lisp") ||
  444.          !strcmp (cp + 1, "cl") ||
  445.          !strcmp (cp + 1, "clisp")))
  446.     {
  447.       L_funcs(inf);
  448.       fclose(inf);
  449.       return;
  450.     }
  451.   /* .scm or .sm or .scheme implies scheme source code */
  452.   if (cp && (!strcmp (cp + 1, "sm")
  453.          || !strcmp (cp + 1, "scm")
  454.          || !strcmp (cp + 1, "scheme")
  455.          || !strcmp (cp + 1, "t")
  456.          || !strcmp (cp + 1, "sch")
  457.          || !strcmp (cp + 1, "SM")
  458.          || !strcmp (cp + 1, "SCM")
  459.              /* The `SCM' or `scm' prefix with a version number */
  460.              || (cp[-1] == 'm' && cp[-2] == 'c' && cp[-3] == 's'
  461.          && string_numeric_p (cp + 1))
  462.              || (cp[-1] == 'M' && cp[-2] == 'C' && cp[-3] == 'S'
  463.          && string_numeric_p (cp + 1))))
  464.     {
  465.       Scheme_funcs(inf);
  466.       fclose(inf);
  467.       return;
  468.     }
  469.   /* if not a .c or .h or .y file, try fortran */
  470.   if (cp && (cp[1] != 'c' && cp[1] != 'h' && cp[1] != 'y')
  471.       && cp[2] == '\0')
  472.     {
  473.       if (PF_funcs(inf) != 0)
  474.     {
  475.       fclose(inf);
  476.       return;
  477.     }
  478.       rewind(inf);    /* no fortran tags found, try C */
  479.     }
  480.   C_entries();
  481.   fclose(inf);
  482. }
  483.  
  484. /* Nonzero if string STR is composed of digits.  */
  485.  
  486. int
  487. string_numeric_p (str)
  488.      char *str;
  489. {
  490.   while (*str)
  491.     {
  492.       if (*str < '0' || *str > '9')
  493.     return 0;
  494.     }
  495.   return 1;
  496. }
  497.  
  498. /* Record a tag on the current line.
  499.   name is the tag name,
  500.   f is nonzero to use a pattern, zero to use line number instead. */
  501.  
  502. pfnote (name, f, linestart, linelen, lno, cno)
  503.      char *name;
  504.      logical f;            /* f == TRUE when function */
  505.      char *linestart;
  506.      int linelen;
  507.      int lno;
  508.      long cno;
  509. {
  510.   register char *fp;
  511.   register NODE *np;
  512.   char *altname;
  513.   char tem[51];
  514.  
  515.   if ((np = (NODE *) malloc (sizeof (NODE))) == NULL)
  516.     {
  517.       fprintf(stderr, "%s: too many entries to sort\n", progname);
  518.       put_entries(head);
  519.       free_tree(head);
  520.       head = NULL;
  521.       np = (NODE *) xmalloc(sizeof (NODE));
  522.     }
  523.   /* Change name "main" to M<thisfilename>. */
  524.   if (!eflag && !xflag && !strcmp(name, "main"))
  525.     {
  526.       fp = rindex(curfile, '/');
  527.       if (fp == 0)
  528.     fp = curfile;
  529.       else
  530.     fp++;
  531.       altname = concat ("M", fp, "");
  532.       fp = rindex(altname, '.');
  533.       if (fp && fp[2] == 0)
  534.     *fp = 0;
  535.       name = altname;
  536.     }
  537.   np->name = savestr(name);
  538.   np->file = curfile;
  539.   np->f = f;
  540.   np->lno = lno;
  541.   np->cno = cno;
  542.   np->left = np->right = 0;
  543.   if (eflag)
  544.     {
  545.       linestart[linelen] = 0;
  546.     }
  547.   else if (xflag == 0)
  548.     {
  549.       sprintf (tem, strlen (linestart) < 50 ? "%s$" : "%.50s", linestart);
  550.       linestart = tem;
  551.     }
  552.   np->pat = savestr (linestart);
  553.   if (head == NULL)
  554.     head = np;
  555.   else
  556.     add_node(np, head);
  557. }
  558.  
  559. free_tree(node)
  560.      NODE *node;
  561. {
  562.   while (node)
  563.     {
  564.       free_tree(node->right);
  565.       free(node);
  566.       node = node->left;
  567.     }
  568. }
  569.  
  570. add_node(node, cur_node)
  571.      NODE *node,*cur_node;
  572. {
  573.   register int dif;
  574.  
  575.   dif = strcmp(node->name, cur_node->name);
  576.  
  577.   /* If this tag name matches an existing one, then
  578.      unless -e was given, do not add the node, but maybe print a warning */
  579.   if (!eflag && !dif)
  580.     {
  581.       if (node->file == cur_node->file)
  582.     {
  583.       if (!wflag)
  584.         {
  585.           fprintf(stderr,"%s: Duplicate entry in file %s, line %d: %s\n",
  586.               progname, node->file,lineno,node->name);
  587.           fprintf(stderr,"Second entry ignored\n");
  588.         }
  589.       return;
  590.     }
  591.       if (!cur_node->been_warned)
  592.     if (!wflag)
  593.       fprintf(stderr,"%s: Duplicate entry in files %s and %s: %s (Warning only)\n",
  594.           progname, node->file, cur_node->file, node->name);
  595.       cur_node->been_warned = TRUE;
  596.       return;
  597.     } 
  598.  
  599.   /* Actually add the node */
  600.   if (dif < 0) 
  601.     {
  602.       if (cur_node->left != NULL)
  603.     add_node(node,cur_node->left);
  604.       else
  605.     cur_node->left = node;
  606.       return;
  607.     }
  608.   if (cur_node->right != NULL)
  609.     add_node(node,cur_node->right);
  610.   else
  611.     cur_node->right = node;
  612. }
  613.  
  614. put_entries(node)
  615.      reg NODE *node;
  616. {
  617.   reg char *sp;
  618.  
  619.   if (node == NULL)
  620.     return;
  621.  
  622.   /* Output subentries that precede this one */
  623.   put_entries (node->left);
  624.  
  625.   /* Output this entry */
  626.  
  627.   if (eflag)
  628.     {
  629.       fprintf (outf, "%s%c%d,%d\n",
  630.            node->pat, 0177, node->lno, node->cno);
  631.     }
  632.   else if (!xflag)
  633.     {
  634.       fprintf (outf, "%s\t%s\t",
  635.            node->name, node->file);
  636.  
  637.       if (node->f)
  638.     {        /* a function */
  639.       putc (searchar, outf);
  640.       putc ('^', outf);
  641.  
  642.       for (sp = node->pat; *sp; sp++)
  643.         {
  644.           if (*sp == '\\' || *sp == searchar)
  645.         putc ('\\', outf);
  646.           putc (*sp, outf);
  647.         }
  648.       putc (searchar, outf);
  649.     }
  650.       else
  651.     {        /* a typedef; text pattern inadequate */
  652.       fprintf (outf, "%d", node->lno);
  653.     }
  654.       putc ('\n', outf);
  655.     }
  656.   else if (vflag)
  657.     fprintf (stdout, "%s %s %d\n",
  658.          node->name, node->file, (node->lno+63)/64);
  659.   else
  660.     fprintf (stdout, "%-16s%4d %-16s %s\n",
  661.          node->name, node->lno, node->file, node->pat);
  662.  
  663.   /* Output subentries that follow this one */
  664.   put_entries (node->right);
  665. }
  666.  
  667. /* Return total number of characters that put_entries will output for
  668.  the nodes in the subtree of the specified node.
  669.  Works only if eflag is set, but called only in that case.  */
  670.  
  671. total_size_of_entries(node)
  672.      reg NODE *node;
  673. {
  674.   reg int total = 0;
  675.   reg long num;
  676.  
  677.   if (node == NULL)
  678.     return 0;
  679.  
  680.   /* Count subentries that precede this one */
  681.   total = total_size_of_entries (node->left);
  682.  
  683.   /* Count subentries that follow this one */
  684.   total += total_size_of_entries (node->right);
  685.  
  686.   /* Count this entry */
  687.  
  688.   total += strlen (node->pat) + 3;
  689.  
  690.   num = node->lno;
  691.   while (num)
  692.     {
  693.       total++;
  694.       num /= 10;
  695.     }
  696.  
  697.   num = node->cno;
  698.   if (!num) total++;
  699.   while (num)
  700.     {
  701.       total++;
  702.       num /= 10;
  703.     }
  704.   return total;
  705. }
  706.  
  707. /*
  708.  * This routine finds functions and typedefs in C syntax and adds them
  709.  * to the list.
  710.  */
  711. #ifdef VMS
  712. long vmslinecharno;
  713. #define VMS_SET_LINECHARNO    (vmslinecharno = ftell(inf))
  714. #else
  715. #define VMS_SET_LINECHARNO
  716. #endif
  717.  
  718. #define CNL_SAVE_NUMBER \
  719. { \
  720.   VMS_SET_LINECHARNO; \
  721.   linecharno = charno; lineno++; \
  722.   charno += 1 + readline (&lb, inf); \
  723.   lp = lb.buffer; \
  724. }
  725.  
  726. #define CNL \
  727. { \
  728.   CNL_SAVE_NUMBER; \
  729.   number = 0; \
  730. }
  731.  
  732. C_entries ()
  733. {
  734.   register int c;
  735.   register char *token, *tp, *lp;
  736.   logical incomm, inquote, inchar, midtoken;
  737.   int level;
  738.   char tok[BUFSIZ];
  739.  
  740.   lineno = 0;
  741.   charno = 0;
  742.   lp = lb.buffer;
  743.   *lp = 0;
  744.  
  745.   number = 0;
  746.   gotone = midtoken = inquote = inchar = incomm = FALSE;
  747.   level = 0;
  748.   tydef = none;
  749.   next_token_is_func = 0;
  750.  
  751.   while (!feof (inf))
  752.     {
  753.       c = *lp++;
  754.       if (c == 0)
  755.     {
  756.       CNL;
  757.       gotone = FALSE;
  758.     }
  759.       if (c == '\\')
  760.     {
  761.       c = *lp++;
  762.       if (c == 0)
  763.         CNL_SAVE_NUMBER;
  764.       c = ' ';
  765.     } 
  766.       else if (incomm)
  767.     {
  768.       if (c == '*')
  769.         {
  770.           while ((c = *lp++) == '*')
  771.         continue;
  772.           if (c == 0)
  773.         CNL;
  774.           if (c == '/')
  775.         incomm = FALSE;
  776.         }
  777.     }
  778.       else if (inquote)
  779.     {
  780.       /*
  781.       * Too dumb to know about \" not being magic, but
  782.       * they usually occur in pairs anyway.
  783.       */
  784.       if (c == '"')
  785.         inquote = FALSE;
  786.       continue;
  787.     }
  788.       else if (inchar)
  789.     {
  790.       if (c == '\'')
  791.         inchar = FALSE;
  792.       continue;
  793.     }
  794.       else switch (c)
  795.     {
  796.     case '"':
  797.       inquote = TRUE;
  798.       continue;
  799.     case '\'':
  800.       inchar = TRUE;
  801.       continue;
  802.     case '/':
  803.       if (*lp == '*')
  804.         {
  805.           lp++;
  806.           incomm = TRUE;
  807.         }
  808.       continue;
  809.     case '#':
  810.       if (lp == lb.buffer + 1)
  811.         number = 1;
  812.       continue;
  813.     case '{':
  814.       if (tydef == tag_ok)
  815.         {
  816.           tydef=middle;
  817.         }
  818.       level++;
  819.       continue;
  820.     case '}':
  821.       if (lp == lb.buffer + 1)
  822.         level = 0;    /* reset */
  823.       else
  824.         level--;
  825.       if (!level && tydef==middle)
  826.         {
  827.           tydef=end;
  828.         }
  829.       continue;
  830.     }
  831.       if (!level && !inquote && !incomm && gotone == FALSE)
  832.     {
  833.       if (midtoken)
  834.         {
  835.           if (endtoken(c))
  836.         {
  837.           int f;
  838.           char *buf = lb.buffer;
  839.           int endpos = lp - lb.buffer;
  840.           char *lp1 = lp;
  841.           int line = lineno;
  842.           long linestart = linecharno;
  843. #ifdef VMS
  844.           long vmslinestart = vmslinecharno;
  845. #endif
  846.           int tem = consider_token (&lp1, token, &f, level);
  847.           lp = lp1;
  848.           if (tem)
  849.             {
  850.               if (linestart != linecharno)
  851.             {
  852. #ifdef VMS
  853.               getline (vmslinestart);
  854. #else
  855.               getline (linestart);
  856. #endif
  857.               strncpy (tok, token + (lb1.buffer - buf),
  858.                    tp-token+1);
  859.               tok[tp-token+1] = 0;
  860.               pfnote(tok, f, lb1.buffer, endpos, line, linestart);
  861.             }
  862.               else
  863.             {
  864.               strncpy (tok, token, tp-token+1);
  865.               tok[tp-token+1] = 0;
  866.               pfnote(tok, f, lb.buffer, endpos, line, linestart);
  867.             }
  868.               gotone = f;    /* function */
  869.             }
  870.           midtoken = FALSE;
  871.           token = lp - 1;
  872.         }
  873.           else if (intoken(c))
  874.         tp++;
  875.         }
  876.       else if (begtoken(c))
  877.         {
  878.           token = tp = lp - 1;
  879.           midtoken = TRUE;
  880.         }
  881.     }
  882.       if (c == ';'  &&  tydef==end)    /* clean with typedefs */
  883.     tydef=none;
  884.     }
  885. }
  886.  
  887. /*
  888.  * This routine  checks to see if the current token is
  889.  * at the start of a function, or corresponds to a typedef
  890.  * It updates the input line * so that the '(' will be
  891.  * in it when it returns.
  892.  */
  893. consider_token (lpp, token, f, level)
  894.      char **lpp, *token;
  895.      int *f, level;
  896. {
  897.   reg char *lp = *lpp;
  898.   reg char c;
  899.   logical firsttok;    /* T if have seen first token in ()'s */
  900.   int bad, win;
  901.  
  902.   *f = 1;            /* a function */
  903.   c = lp[-1];
  904.   bad = FALSE;
  905.   if (!number)
  906.     {        /* space is not allowed in macro defs    */
  907.       while (iswhite(c))
  908.     {
  909.       c = *lp++;
  910.       if (c == 0)
  911.         {
  912.           if (feof (inf))
  913.         break;
  914.           CNL;
  915.         }
  916.     }
  917.       /* the following tries to make it so that a #define a b(c)    */
  918.       /* doesn't count as a define of b.                */
  919.     }
  920.   else
  921.     {
  922.       number++;
  923.       if (number >= 4  || (number==2 && strncmp (token, "define", 6)))
  924.     {
  925.       gotone = TRUE;
  926.     badone:
  927.       bad = TRUE;
  928.       goto ret;
  929.     }
  930.     }
  931.   /* check for the typedef cases        */
  932.   if (tflag && istoken(token, "typedef", 7))
  933.     {
  934.       tydef=begin;
  935.       goto badone;
  936.     }
  937.   if (tydef==begin && (istoken(token, "struct", 6) ||
  938.                istoken(token, "union", 5) || istoken(token, "enum", 4)))
  939.   {
  940.     tydef=tag_ok;      
  941.     goto badone;
  942.   }
  943.   if (tydef==tag_ok)
  944.     {
  945.       tydef=middle;
  946.       goto badone;
  947.     }
  948.   if (tydef==begin)        /* e.g. typedef ->int<- */
  949.     {
  950.       tydef=end;
  951.       goto badone;
  952.     }
  953.   if (tydef==middle && level == 0) /* e.g. typedef struct tag ->struct_t<- */
  954.     {
  955.       tydef=end;
  956.     }
  957.   if (tydef==end)
  958.     {
  959.       *f = 0;
  960.       win = 1;
  961.       goto ret;
  962.     }
  963.   /* Detect GNUmacs's function-defining macros. */
  964.   if (!number && !strncmp (token, "DEF", 3))
  965.      
  966.     {
  967.       next_token_is_func = 1;
  968.       goto badone;
  969.     }
  970.   if (next_token_is_func)
  971.     {
  972.       next_token_is_func = 0;
  973.       win = 1;
  974.       goto ret;
  975.     }
  976.   if (c != '(')
  977.     goto badone;
  978.   firsttok = FALSE;
  979.   while ((c = *lp++) != ')')
  980.     {
  981.       if (c == 0)
  982.     {
  983.       if (feof (inf))
  984.         break;
  985.       CNL;
  986.     }
  987.       /*
  988.     * This line used to confuse ctags:
  989.     *    int    (*oldhup)();
  990.     * This fixes it. A nonwhite char before the first
  991.     * token, other than a / (in case of a comment in there)
  992.     * makes this not a declaration.
  993.     */
  994.       if (begtoken(c) || c=='/') firsttok++;
  995.       else if (!iswhite(c) && !firsttok) goto badone;
  996.     }
  997.   while (iswhite (c = *lp++))
  998.     {
  999.       if (c == 0)
  1000.     {
  1001.       if (feof (inf))
  1002.         break;
  1003.       CNL;
  1004.     }
  1005.     }
  1006.   win = isgood (c);
  1007. ret:
  1008.   *lpp = lp - 1;
  1009.   return !bad && win;
  1010. }
  1011.  
  1012. getline (atchar)
  1013.      long atchar;
  1014. {
  1015.   long saveftell = ftell (inf);
  1016.  
  1017.   fseek (inf, atchar, 0);
  1018.   readline (&lb1, inf);
  1019.   fseek (inf, saveftell, 0);
  1020. }
  1021.  
  1022. /* Fortran parsing */
  1023.  
  1024. char    *dbp;
  1025. int    pfcnt;
  1026.  
  1027. PF_funcs(fi)
  1028.      FILE *fi;
  1029. {
  1030.   lineno = 0;
  1031.   charno = 0;
  1032.   pfcnt = 0;
  1033.  
  1034.   while (!feof (fi))
  1035.     {
  1036.       lineno++;
  1037.       linecharno = charno;
  1038.       charno += readline (&lb, fi) + 1;
  1039.       dbp = lb.buffer;
  1040.       if (*dbp == '%') dbp++ ;    /* Ratfor escape to fortran */
  1041.       while (isspace(*dbp))
  1042.     dbp++;
  1043.       if (*dbp == 0)
  1044.     continue;
  1045.       switch (*dbp |' ')
  1046.     {
  1047.     case 'i':
  1048.       if (tail("integer"))
  1049.         takeprec();
  1050.       break;
  1051.     case 'r':
  1052.       if (tail("real"))
  1053.         takeprec();
  1054.       break;
  1055.     case 'l':
  1056.       if (tail("logical"))
  1057.         takeprec();
  1058.       break;
  1059.     case 'c':
  1060.       if (tail("complex") || tail("character"))
  1061.         takeprec();
  1062.       break;
  1063.     case 'd':
  1064.       if (tail("double"))
  1065.         {
  1066.           while (isspace(*dbp))
  1067.         dbp++;
  1068.           if (*dbp == 0)
  1069.         continue;
  1070.           if (tail("precision"))
  1071.         break;
  1072.           continue;
  1073.         }
  1074.       break;
  1075.     }
  1076.       while (isspace(*dbp))
  1077.     dbp++;
  1078.       if (*dbp == 0)
  1079.     continue;
  1080.       switch (*dbp|' ')
  1081.     {
  1082.     case 'f':
  1083.       if (tail("function"))
  1084.         getit();
  1085.       continue;
  1086.     case 's':
  1087.       if (tail("subroutine"))
  1088.         getit();
  1089.       continue;
  1090.     case 'p':
  1091.       if (tail("program"))
  1092.         {
  1093.           getit();
  1094.           continue;
  1095.         }
  1096.       if (tail("procedure"))
  1097.         getit();
  1098.       continue;
  1099.     }
  1100.     }
  1101.   return (pfcnt);
  1102. }
  1103.  
  1104. tail(cp)
  1105.      char *cp;
  1106. {
  1107.   register int len = 0;
  1108.  
  1109.   while (*cp && (*cp&~' ') == ((*(dbp+len))&~' '))
  1110.     cp++, len++;
  1111.   if (*cp == 0)
  1112.     {
  1113.       dbp += len;
  1114.       return (1);
  1115.     }
  1116.   return (0);
  1117. }
  1118.  
  1119. takeprec()
  1120. {
  1121.   while (isspace(*dbp))
  1122.     dbp++;
  1123.   if (*dbp != '*')
  1124.     return;
  1125.   dbp++;
  1126.   while (isspace(*dbp))
  1127.     dbp++;
  1128.   if (!isdigit(*dbp))
  1129.     {
  1130.       --dbp;        /* force failure */
  1131.       return;
  1132.     }
  1133.   do
  1134.     dbp++;
  1135.   while (isdigit(*dbp));
  1136. }
  1137.  
  1138. getit()
  1139. {
  1140.   register char *cp;
  1141.   char c;
  1142.   char nambuf[BUFSIZ];
  1143.  
  1144.   while (isspace(*dbp))
  1145.     dbp++;
  1146.   if (*dbp == 0 || (!isalpha (*dbp)) && (*dbp != '_') && (*dbp != '$'))
  1147.     return;
  1148.   for (cp = dbp + 1; *cp && (isalpha (*cp) || isdigit (*cp)
  1149.                  || (*cp == '_') || (*cp == '$')); cp++)
  1150.     continue;
  1151.   c = cp[0];
  1152.   cp[0] = 0;
  1153.   strcpy(nambuf, dbp);
  1154.   cp[0] = c;
  1155.   pfnote(nambuf, TRUE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  1156.   pfcnt++;
  1157. }
  1158.  
  1159. /*
  1160.  * lisp tag functions
  1161.  * just look for (def or (DEF
  1162.  */
  1163.  
  1164. L_funcs (fi)
  1165.      FILE *fi;
  1166. {
  1167.   lineno = 0;
  1168.   charno = 0;
  1169.   pfcnt = 0;
  1170.  
  1171.   while (!feof (fi))
  1172.     {
  1173.       lineno++;
  1174.       linecharno = charno;
  1175.       charno += readline (&lb, fi) + 1;
  1176.       dbp = lb.buffer;
  1177.       if (dbp[0] == '(' && 
  1178.       (dbp[1] == 'D' || dbp[1] == 'd') &&
  1179.         (dbp[2] == 'E' || dbp[2] == 'e') &&
  1180.           (dbp[3] == 'F' || dbp[3] == 'f'))
  1181.     {
  1182.       while (!isspace(*dbp)) dbp++;
  1183.       while (isspace(*dbp)) dbp++;
  1184.       L_getit();
  1185.     }
  1186.     }
  1187. }
  1188.  
  1189. L_getit()
  1190. {
  1191.   register char *cp;
  1192.   char c;
  1193.   char nambuf[BUFSIZ];
  1194.  
  1195.   if (*dbp == 0) return;
  1196.   for (cp = dbp+1; *cp && *cp != '(' && *cp != ' '; cp++)
  1197.     continue;
  1198.   c = cp[0];
  1199.   cp[0] = 0;
  1200.   strcpy(nambuf, dbp);
  1201.   cp[0] = c;
  1202.   pfnote(nambuf, TRUE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  1203.   pfcnt++;
  1204. }
  1205.  
  1206. /*
  1207.  * Scheme tag functions
  1208.  * look for (def... xyzzy
  1209.  * look for (def... (xyzzy
  1210.  * look for (def ... ((...(xyzzy ....
  1211.  * look for (set! xyzzy
  1212.  */
  1213.  
  1214. static get_scheme ();
  1215. Scheme_funcs (fi)
  1216.      FILE *fi;
  1217. {
  1218.   lineno = 0;
  1219.   charno = 0;
  1220.   pfcnt = 0;
  1221.  
  1222.   while (!feof (fi))
  1223.     {
  1224.       lineno++;
  1225.       linecharno = charno;
  1226.       charno += readline (&lb, fi) + 1;
  1227.       dbp = lb.buffer;
  1228.       if (dbp[0] == '(' && 
  1229.       (dbp[1] == 'D' || dbp[1] == 'd') &&
  1230.         (dbp[2] == 'E' || dbp[2] == 'e') &&
  1231.           (dbp[3] == 'F' || dbp[3] == 'f'))
  1232.     {
  1233.       while (!isspace(*dbp)) dbp++;
  1234.           /* Skip over open parens and white space */
  1235.           while (*dbp && (isspace(*dbp) || *dbp == '(')) dbp++;
  1236.       get_scheme ();
  1237.     }
  1238.       if (dbp[0] == '(' && 
  1239.       (dbp[1] == 'S' || dbp[1] == 's') &&
  1240.         (dbp[2] == 'E' || dbp[2] == 'e') &&
  1241.           (dbp[3] == 'T' || dbp[3] == 't') &&
  1242.                 (dbp[4] == '!' || dbp[4] == '!') &&
  1243.                   (isspace(dbp[5])))
  1244.     {
  1245.       while (!isspace(*dbp)) dbp++;
  1246.           /* Skip over white space */
  1247.           while (isspace(*dbp)) dbp++;
  1248.       get_scheme ();
  1249.     }
  1250.     }
  1251. }
  1252.  
  1253. static
  1254. get_scheme()
  1255. {
  1256.   register char *cp;
  1257.   char c;
  1258.   char nambuf[BUFSIZ];
  1259.  
  1260.   if (*dbp == 0) return;
  1261.   /* Go till you get to white space or a syntactic break */
  1262.   for (cp = dbp+1; *cp && *cp != '(' && *cp != ')' && !isspace(*cp); cp++)
  1263.     continue;
  1264.   /* Null terminate the string there. */
  1265.   c = cp[0];
  1266.   cp[0] = 0;
  1267.   /* Copy the string */
  1268.   strcpy(nambuf, dbp);
  1269.   /* Unterminate the string */
  1270.   cp[0] = c;
  1271.   /* Announce the change */
  1272.   pfnote(nambuf, TRUE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  1273.   pfcnt++;
  1274. }
  1275.  
  1276. /* Find tags in TeX and LaTeX input files.  */
  1277.  
  1278. /* TEX_toktab is a table of TeX control sequences that define tags.
  1279.    Each TEX_tabent records one such control sequence.  */
  1280.  
  1281. struct TEX_tabent
  1282. {
  1283.   char *name;
  1284.   int len;
  1285. };
  1286.  
  1287. struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
  1288.  
  1289. /* Default set of control sequences to put into TEX_toktab.
  1290.    The value of environment var TEXTAGS is prepended to this.  */
  1291.  
  1292. static char *TEX_defenv =
  1293.   ":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
  1294.  
  1295. struct TEX_tabent *TEX_decode_env (); 
  1296.  
  1297. static char TEX_esc = '\\';
  1298. static char TEX_opgrp = '{';
  1299. static char TEX_clgrp = '}';
  1300.  
  1301. /*
  1302.  * TeX/LaTeX scanning loop.
  1303.  */
  1304.  
  1305. TEX_funcs (fi)
  1306.     FILE *fi;
  1307. {
  1308.   char *lasthit;
  1309.  
  1310.   lineno = 0;
  1311.   charno = 0;
  1312.   pfcnt = 0;
  1313.  
  1314.   /* Select either \ or ! as escape character.  */
  1315.   TEX_mode (fi);
  1316.  
  1317.   /* Initialize token table once from environment. */
  1318.   if (!TEX_toktab)
  1319.     TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
  1320.  
  1321.   while (!feof (fi))
  1322.     {    /* Scan each line in file */
  1323.       lineno++;
  1324.       linecharno = charno;
  1325.       charno += readline (&lb, fi) + 1;
  1326.       dbp = lb.buffer;
  1327.       lasthit = dbp;
  1328.       while (dbp = index (dbp, TEX_esc)) /* Look at each escape in line */
  1329.     {
  1330.       register int i;
  1331.  
  1332.       if (! *(++dbp))
  1333.         break;
  1334.       linecharno += dbp - lasthit;
  1335.       lasthit = dbp;
  1336.       i = TEX_Token (lasthit);
  1337.       if (0 <= i)
  1338.         {
  1339.           TEX_getit (lasthit, TEX_toktab[i].len);
  1340.           break;        /* We only save a line once */
  1341.         }
  1342.     }
  1343.     }
  1344. }
  1345.  
  1346. #define TEX_LESC '\\'
  1347. #define TEX_SESC '!'
  1348. #define TEX_CMT  '%'
  1349.  
  1350. /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
  1351. /* chars accordingly. */
  1352.  
  1353. TEX_mode (f)
  1354.      FILE *f;
  1355. {
  1356.   int c, skip_line = 0;
  1357.  
  1358.   while ((c = getc (f)) != EOF)
  1359.     {
  1360.       /* Skip to next line if we hit the TeX comment char. */
  1361.       if (c == TEX_CMT)
  1362.     skip_line = 1;
  1363.       else if (c == '\n')
  1364.     skip_line = 0;
  1365.       else if (! skip_line)
  1366.     if (c == TEX_LESC || c == TEX_SESC)
  1367.       break;
  1368.     }
  1369.  
  1370.   if (c == TEX_LESC)
  1371.     {
  1372.       TEX_esc = TEX_LESC;
  1373.       TEX_opgrp = '{';
  1374.       TEX_clgrp = '}';
  1375.     } 
  1376.   else
  1377.     {
  1378.       TEX_esc = TEX_SESC;
  1379.       TEX_opgrp = '<';
  1380.       TEX_clgrp = '>';
  1381.     }
  1382.   rewind (f);
  1383. }
  1384.  
  1385. /* Read environment and prepend it to the default string. */
  1386. /* Build token table. */
  1387.  
  1388. struct TEX_tabent *
  1389. TEX_decode_env (evarname, defenv)
  1390.      char *evarname;
  1391.      char *defenv;
  1392. {
  1393.   register char *env, *p;
  1394.   extern char *savenstr (), *index ();
  1395.  
  1396.   struct TEX_tabent *tab;
  1397.   int size, i;
  1398.  
  1399.   /* Append deafult string to environment. */
  1400.   env = (char *) getenv (evarname);
  1401.   if (!env)
  1402.     env = defenv;
  1403.   else
  1404.     env = concat (env, defenv, "");
  1405.  
  1406.   /* Allocate a token table */
  1407.   for (size = 1, p=env; p;)
  1408.     if ((p = index (p, ':')) && *(++p))
  1409.       size++;
  1410.   tab = (struct TEX_tabent *) xmalloc ((size + 1)
  1411.                        * sizeof (struct TEX_tabent));
  1412.  
  1413.   /* Unpack environment string into token table. Be careful about */
  1414.   /* zero-length strings (leading ':', "::" and trailing ':') */
  1415.   for (i = 0; *env;)
  1416.     {
  1417.       p = index (env, ':');
  1418.       if (!p)            /* End of environment string. */
  1419.     p = env + strlen (env);
  1420.       if (p - env > 0)
  1421.     {    /* Only non-zero strings. */
  1422.       tab[i].name = savenstr (env, p - env);
  1423.       tab[i].len = strlen (tab[i].name);
  1424.       i++;
  1425.     }
  1426.       if (*p)
  1427.     env = p + 1;
  1428.       else
  1429.     {
  1430.       tab[i].name = NULL;    /* Mark end of table. */
  1431.       tab[i].len = 0;
  1432.       break;
  1433.     }
  1434.     }
  1435.   return tab;
  1436. }
  1437.  
  1438. /* Record a tag defined by a TeX command of length LEN and starting at NAME.
  1439.    The name being defined actually starts at (NAME + LEN + 1).
  1440.    But we seem to include the TeX command in the tag name.  */
  1441.  
  1442. TEX_getit (name, len)
  1443.     char *name;
  1444.     int len;
  1445. {
  1446.   char *p = name + len;
  1447.   char nambuf[BUFSIZ];
  1448.  
  1449.   if (*name == 0) return;
  1450.  
  1451.   /* Let tag name extend to next group close (or end of line) */
  1452.   while (*p && *p != TEX_clgrp)
  1453.     p++;
  1454.   strncpy (nambuf, name, p - name);
  1455.   nambuf[p - name] = 0;
  1456.  
  1457.   pfnote (nambuf, TRUE, lb.buffer, strlen (lb.buffer), lineno, linecharno);
  1458.   pfcnt++;
  1459. }
  1460.  
  1461. /* If the text at CP matches one of the tag-defining TeX command names,
  1462.    return the index of that command in TEX_toktab.
  1463.    Otherwise return -1.  */
  1464.  
  1465. /* Keep the capital `T' in `Token' for dumb truncating compilers
  1466.    (this distinguishes it from `TEX_toktab' */
  1467. TEX_Token (cp)
  1468.     char *cp;
  1469. {
  1470.   int i;
  1471.  
  1472.   for (i = 0; TEX_toktab[i].len > 0; i++)
  1473.     if (strncmp (TEX_toktab[i].name, cp, TEX_toktab[i].len) == 0)
  1474.       return i;
  1475.   return -1;
  1476. }
  1477.  
  1478. /* Initialize a linebuffer for use */
  1479.  
  1480. void
  1481. initbuffer (linebuffer)
  1482.      struct linebuffer *linebuffer;
  1483. {
  1484.   linebuffer->size = 200;
  1485.   linebuffer->buffer = (char *) xmalloc (200);
  1486. }
  1487.  
  1488. /* Read a line of text from `stream' into `linebuffer'.
  1489.  Return the length of the line.  */
  1490.  
  1491. long
  1492. readline (linebuffer, stream)
  1493.      struct linebuffer *linebuffer;
  1494.      register FILE *stream;
  1495. {
  1496.   char *buffer = linebuffer->buffer;
  1497.   register char *p = linebuffer->buffer;
  1498.   register char *pend;
  1499.  
  1500.   pend = p + linebuffer->size;
  1501.  
  1502.   while (1)
  1503.     {
  1504.       int c = getc (stream);
  1505.       if (p == pend)
  1506.     {
  1507.       linebuffer->size *= 2;
  1508.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1509.       p += buffer - linebuffer->buffer;
  1510.       pend = buffer + linebuffer->size;
  1511.       linebuffer->buffer = buffer;
  1512.     }
  1513.       if (c < 0 || c == '\n')
  1514.     {
  1515.       *p = 0;
  1516.       break;
  1517.     }
  1518.       *p++ = c;
  1519.     }
  1520.  
  1521.   return p - buffer;
  1522. }
  1523.  
  1524. char *
  1525. savestr(cp)
  1526.      char *cp;
  1527. {
  1528.   return savenstr (cp, strlen (cp));
  1529. }
  1530.  
  1531. char *
  1532. savenstr(cp, len)
  1533.     char *cp;
  1534.     int len;
  1535. {
  1536.   register char *dp;
  1537.  
  1538.   dp = (char *) xmalloc (len + 1);
  1539.   strncpy (dp, cp, len);
  1540.   dp[len] = '\0';
  1541.   return dp;
  1542. }
  1543.  
  1544. /*
  1545.  * Return the ptr in sp at which the character c last
  1546.  * appears; NULL if not found
  1547.  *
  1548.  * Identical to v7 rindex, included for portability.
  1549.  */
  1550.  
  1551. char *
  1552. rindex(sp, c)
  1553.      register char *sp, c;
  1554. {
  1555.   register char *r;
  1556.  
  1557.   r = NULL;
  1558.   do
  1559.     {
  1560.       if (*sp == c)
  1561.     r = sp;
  1562.     } while (*sp++);
  1563.   return(r);
  1564. }
  1565.  
  1566. /*
  1567.  * Return the ptr in sp at which the character c first
  1568.  * appears; NULL if not found
  1569.  *
  1570.  * Identical to v7 index, included for portability.
  1571.  */
  1572.  
  1573. char *
  1574. index(sp, c)
  1575.      register char *sp, c;
  1576. {
  1577.   do
  1578.     {
  1579.       if (*sp == c)
  1580.     return (sp);
  1581.     } while (*sp++);
  1582.   return (NULL);
  1583. }
  1584.  
  1585. /* Print error message and exit.  */
  1586.  
  1587. fatal (s1, s2)
  1588.      char *s1, *s2;
  1589. {
  1590.   error (s1, s2);
  1591.   exit (BAD);
  1592. }
  1593.  
  1594. /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
  1595.  
  1596. error (s1, s2)
  1597.      char *s1, *s2;
  1598. {
  1599.   fprintf (stderr, "%s: ", progname);
  1600.   fprintf (stderr, s1, s2);
  1601.   fprintf (stderr, "\n");
  1602. }
  1603.  
  1604. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1605.  
  1606. char *
  1607. concat (s1, s2, s3)
  1608.      char *s1, *s2, *s3;
  1609. {
  1610.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1611.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1612.  
  1613.   strcpy (result, s1);
  1614.   strcpy (result + len1, s2);
  1615.   strcpy (result + len1 + len2, s3);
  1616.   *(result + len1 + len2 + len3) = 0;
  1617.  
  1618.   return result;
  1619. }
  1620.  
  1621. /* Like malloc but get fatal error if memory is exhausted.  */
  1622.  
  1623. int
  1624. xmalloc (size)
  1625.      int size;
  1626. {
  1627.   int result = malloc (size);
  1628.   if (!result)
  1629.     fatal ("virtual memory exhausted", 0);
  1630.   return result;
  1631. }
  1632.  
  1633. int
  1634. xrealloc (ptr, size)
  1635.      char *ptr;
  1636.      int size;
  1637. {
  1638.   int result = realloc (ptr, size);
  1639.   if (!result)
  1640.     fatal ("virtual memory exhausted");
  1641.   return result;
  1642. }
  1643.