*** empty log message ***
[emacs.git] / lib-src / etags.c
blob69d29109b690213f9dd40e4efce417f4ac6fd440
1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 1987, 1988, 1989, 1992 Free Software Foundation, Inc. and Ken Arnold
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * Authors:
22 * Ctags originally by Ken Arnold.
23 * FORTRAN added by Jim Kleckner.
24 * Ed Pelegri-Llopart added C typedefs.
25 * Gnu Emacs TAGS format and modifications by RMS?
26 * Sam Kendall added C++.
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
34 #include "getopt.h"
36 #ifdef __GNUC__
37 #define alloca __builtin_alloca
38 #else
39 #ifdef sparc
40 #include <alloca.h>
41 #else
42 extern char *alloca ();
43 #endif
44 #endif
46 extern char *malloc (), *realloc ();
47 extern char *getenv ();
48 extern char *index (), *rindex ();
49 extern char *strcpy (), *strncpy ();
50 extern int strcmp ();
52 #ifdef hpux
53 #define notdef
54 #endif
56 /* Define the symbol ETAGS to make the program "etags",
57 which makes emacs-style tag tables by default.
58 Define CTAGS to make the program "ctags" compatible with the usual one.
59 Define neither one to get behavior that depends
60 on the name with which the program is invoked
61 (but we don't normally compile it that way). */
63 #if !defined(ETAGS) && !defined(CTAGS)
64 /* If neither is defined, program can be run as either. */
65 #define ETAGS
66 #define CTAGS
67 #endif
69 /* On VMS, CTAGS is not useful, so always do ETAGS. */
70 #ifdef VMS
71 #ifndef ETAGS
72 #define ETAGS
73 #endif
74 #endif
76 /* Exit codes for success and failure. */
77 #ifdef VMS
78 #define GOOD (1)
79 #define BAD (0)
80 #else
81 #define GOOD (0)
82 #define BAD (1)
83 #endif
86 * The FILEPOS abstract type, which represents a position in a file,
87 * plus the following accessor functions:
89 * long GET_CHARNO (pos)
90 * returns absolute char number.
91 * long GET_COOKIE (pos)
92 * returns ftell () cookie.
93 * void SET_FILEPOS (pos, fp, charno)
94 * FILE *fp; long charno;
95 * sets `pos' from the current file
96 * position of `fp' and from `charno',
97 * which must be the absolute character
98 * number corresponding to the current
99 * position of `fp'.
101 * The `pos' parameter is an lvalue expression of type FILEPOS.
102 * Parameters to the accessor functions are evaluated 0 or more times,
103 * and so must have no side effects.
105 * FILEPOS objects can also be assigned and passed to and from
106 * functions in the normal C manner.
108 * Implementation notes: the `+ 0' is to enforce rvalue-ness.
110 #ifdef VMS
111 typedef struct
113 long cookie;
114 long charno;
115 } FILEPOS;
117 #define GET_CHARNO(pos) ((pos).charno + 0)
118 #define GET_COOKIE(pos) ((pos).cookie + 0)
119 #define SET_FILEPOS(pos, fp, cno) \
120 ((void) ((pos).cookie = ftell (fp), (pos).charno = (cno)))
121 #else
122 #ifndef DEBUG
123 /* UNIX real implementation */
124 typedef long FILEPOS;
125 #define GET_CHARNO(pos) ((pos) + 0)
126 #define GET_COOKIE(pos) GET_CHARNO (pos)
127 #define SET_FILEPOS(pos, fp, cno) ((void) ((pos) = (cno)))
128 #else
129 /* UNIX debugging implementation */
130 typedef struct
132 long charno;
133 } FILEPOS;
135 #define GET_CHARNO(pos) ((pos).charno + 0)
136 #define GET_COOKIE(pos) GET_CHARNO (pos)
137 #define SET_FILEPOS(pos, fp, cno) \
138 ((void) ((pos).charno = (cno), \
139 (cno) != ftell (fp) ? (error ("SET_FILEPOS inconsistency"), 0) \
140 : 0))
141 #endif
142 #endif
144 #define streq(s, t) (strcmp (s, t) == 0)
145 #define strneq(s, t, n) (strncmp (s, t, n) == 0)
146 #define reg register
147 #define logical char
149 #define TRUE 1
150 #define FALSE 0
152 #define iswhite(arg) (_wht[arg]) /* T if char is white */
153 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
154 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
155 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
156 #define isgood(arg) (_gd[arg]) /* T if char can be after ')' */
158 #define max(I1,I2) ((I1) > (I2) ? (I1) : (I2))
160 struct nd_st
161 { /* sorting structure */
162 char *name; /* function or type name */
163 char *file; /* file name */
164 logical is_func; /* use pattern or line no */
165 logical rewritten; /* list name separately */
166 logical been_warned; /* set if noticed dup */
167 int lno; /* line number tag is on */
168 long cno; /* character number line starts on */
169 char *pat; /* search pattern */
170 struct nd_st *left, *right; /* left and right sons */
173 long ftell ();
174 typedef struct nd_st NODE;
176 logical gotone, /* found a func already on line */
177 /* boolean "func" (see init) */
178 header_file, /* TRUE if .h file, FALSE o.w. */
179 _wht[0177], _etk[0177], _itk[0177], _btk[0177], _gd[0177];
182 char *concat ();
183 char *savenstr ();
184 char *savestr ();
185 char *xmalloc ();
186 char *xrealloc ();
187 int L_isdef ();
188 int PF_funcs ();
189 int total_size_of_entries ();
190 logical consider_token ();
191 logical tail ();
192 long readline ();
193 void Asm_funcs ();
194 void C_entries ();
195 void L_funcs ();
196 void L_getit ();
197 void PAS_funcs ();
198 void Scheme_funcs ();
199 void TEX_funcs ();
200 void add_node ();
201 void error ();
202 void fatal ();
203 void find_entries ();
204 void free_tree ();
205 void getit ();
206 void getline ();
207 void init ();
208 void initbuffer ();
209 void initbuffer ();
210 void pfnote ();
211 void process_file ();
212 void put_entries ();
213 void takeprec ();
216 * MACRO
217 * xnew -- allocate storage
219 * SYNOPSIS
220 * Type *xnew (int n, Type);
222 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
227 * Symbol table stuff.
229 * Should probably be implemented with hash table; linked list for now.
232 enum sym_type
234 st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
237 struct stab_entry
239 char *sym;
240 int symlen;
241 enum sym_type type;
242 struct stab_entry *next;
245 typedef struct stab_entry Stab_entry;
246 typedef Stab_entry *Stab;
249 * NAME
250 * Stab, Stab_entry, stab_create, stab_search, stab_find -- symbol table
252 * SYNOPSIS
253 * Types: Stab, Stab_entry, enum sym_type
255 * Stab * stab_create ()
257 * Stab_entry * stab_find (stab, sym)
258 * Stab *stab;
259 * char *sym;
261 * Stab_entry * stab_search (stab, sym)
262 * Stab *stab;
263 * char *sym;
265 * DESCRIPTION
266 * stab_create creates a Stab, a symbol table object, and returns a
267 * pointer to it. stab_find finds a symbol in a Stab; it returns a
268 * pointer to the Stab_entry if found, otherwise NULL. stab_search
269 * is like stab_find, except that it creates a new Stab_entry,
270 * initialized with type = st_none, if one did not exist already
271 * (it never returns NULL).
273 * A Stab_entry is a structure that contains at least the following
274 * members:
276 * char *name; // must not be modified
277 * enum sym_type type; // should be set
279 * The type field is initially set to st_none; it should be set to
280 * something else by the caller of stab_search. Other possible values
281 * of an enum sym_type can be added.
284 Stab *
285 stab_create ()
287 Stab *sp;
288 sp = xnew (1, Stab);
289 *sp = NULL; /* a Stab starts out as a null Stab_entry* */
290 return sp;
293 Stab_entry *
294 stab_find (stab, sym, symlen)
295 Stab *stab;
296 register char *sym;
297 register int symlen;
299 register Stab_entry *se;
300 for (se = *stab; se != NULL; se = se->next)
302 if (se->symlen == symlen && strneq (se->sym, sym, symlen))
303 return se;
306 return NULL;
309 Stab_entry *
310 stab_search (stab, sym, symlen)
311 register Stab *stab;
312 char *sym;
313 int symlen;
315 register Stab_entry *se;
316 se = stab_find (stab, sym, symlen);
318 if (se == NULL)
320 /* make a new one */
321 se = xnew (1, Stab_entry);
322 se->sym = savenstr (sym, symlen);
323 se->symlen = symlen;
324 se->type = st_none;
325 se->next = *stab;
326 *stab = se;
329 return se;
333 * NAME
334 * stab_type -- type of a symbol table entry
336 * SYNOPSIS
337 * enum sym_type stab_type (Stab_entry *se);
339 * WARNING
340 * May evaluate its argument more than once.
343 #define stab_type(se) ((se)==NULL ? st_none : (se)->type)
347 typedef int LINENO;
349 typedef struct
351 char *p;
352 int len;
353 FILEPOS linestart;
354 LINENO lineno;
355 logical rewritten;
356 } TOKEN;
359 /* typedefs are recognized using a simple finite automaton.
360 * tydef is its state variable.
362 typedef enum
364 none, begin, middle, end
365 } TYST;
367 TYST tydef = none;
370 /* struct tags for C++ are recognized using another simple
371 * finite automaton. `structdef' is its state variable.
372 * This machinery is only invoked for C++; otherwise structdef
373 * should remain snone. However, this machinery can easily be
374 * adapted to find structure tags in normal C code.
376 typedef enum
378 snone, /* nothing seen yet */
379 skeyseen, /* struct-like keyword seen */
380 stagseen, /* struct-like tag seen */
381 scolonseen, /* colon seen after struct-like tag */
382 sinbody /* in a class body: recognize member func defs */
383 } STRUCTST;
384 STRUCTST structdef = snone;
386 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
387 * struct tag, and structkey is the preceding struct-like keyword.
389 char structtag[512];
390 Stab_entry *structkey;
393 * Yet another little state machine to deal with preprocessor lines.
395 typedef enum
397 dnone, /* nothing seen */
398 dsharpseen, /* '#' seen as first char on line */
399 ddefineseen, /* '#' and 'define' seen */
400 dignorerest /* ignore rest of line */
401 } DEFINEST;
402 DEFINEST definedef;
405 * LEVEL_OK_FOR_FUNCDEF allows C++ function definition within class body.
406 * Currently tydef and structdef stuff (typedefs and struct definitions) are
407 * only noticed when level==0, but that may change.
409 * Note that this macro may only be evaluated inside C_entries(). It is
410 * for self-documentation only.
412 #define LEVEL_OK_FOR_FUNCDEF() \
413 (level==0 || c_ext && level==1 && structdef==sinbody)
416 * next_token_is_func
417 * set this to TRUE, and the next token considered is called a function.
419 logical next_token_is_func;
421 /* C extensions. Currently all listed extensions are C++ dialects, so
422 * `c_ext' is used as an abbreviation for `c_ext&C_PLPL'. If a non-C++
423 * dialect is added, this must change.
425 #define C_PLPL 0x1 /* C++ */
426 #define C_STAR 0x3 /* C* */
428 char searchar = '/'; /* use /.../ searches */
430 LINENO lineno; /* line number of current line */
431 long charno; /* current character number */
432 FILEPOS linepos; /* start of line (C only) */
433 FILEPOS prev_linepos; /* start of previous line (C only) */
435 long linecharno; /* charno of start of line; not used by C, but
436 * by every other language.
439 char *curfile, /* current input file name */
440 *outfile, /* output file */
441 *white = " \f\t\n", /* white chars */
442 *endtk = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
443 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$", /* token starting chars */
444 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789", /* valid in-token chars */
445 *notgd = ",;"; /* non-valid after-function chars */
447 int append_to_tagfile; /* -a: append to tags */
448 int emacs_tags_format; /* emacs style output (no -e option any more) */
449 /* The following three default to 1 for etags, but to 0 for ctags. */
450 int typedefs; /* -t: create tags for typedefs */
451 int typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
452 /* 0 struct/enum/union decls, and C++ */
453 /* member functions */
454 int constantypedefs; /* -d: create tags for C #define and enum */
455 /* constants. Default under etags. Enum */
456 /* constants not implemented. */
457 /* -D: opposite of -d. Default under ctags. */
458 int update; /* -u: update tags */
459 int vgrind_style; /* -v: create vgrind style index output */
460 int no_warnings; /* -w: suppress warnings */
461 int cxref_style; /* -x: create cxref style output */
462 int cplusplus; /* .[hc] means C++, not C */
463 int noindentypedefs; /* -S: ignore indentation in C */
465 /* Name this program was invoked with. */
466 char *progname;
468 struct option longopts[] = {
469 { "append", no_argument, NULL, 'a' },
470 { "backward-search", no_argument, NULL, 'B' },
471 { "c++", no_argument, NULL, 'C' },
472 { "cxref", no_argument, NULL, 'x' },
473 { "defines", no_argument, NULL, 'd' },
474 { "forward-search", no_argument, NULL, 'F' },
475 { "help", no_argument, NULL, 'H' },
476 { "ignore-indentation", no_argument, NULL, 'S' },
477 { "include", required_argument, NULL, 'i' },
478 { "no-defines", no_argument, NULL, 'D' },
479 { "no-warn", no_argument, NULL, 'w' },
480 { "output", required_argument, NULL, 'o' },
481 { "typedefs", no_argument, NULL, 't' },
482 { "typedefs-and-c++", no_argument, NULL, 'T' },
483 { "update", no_argument, NULL, 'u' },
484 { "version", no_argument, NULL, 'V' },
485 { "vgrind", no_argument, NULL, 'v' },
486 { 0 }
489 FILE *inf, /* ioptr for current input file */
490 *outf; /* ioptr for tags file */
492 NODE *head; /* the head of the binary tree of tags */
494 int permit_duplicates = 1; /* Nonzero means allow duplicate tags. */
496 /* A `struct linebuffer' is a structure which holds a line of text.
497 `readline' reads a line from a stream into a linebuffer
498 and works regardless of the length of the line. */
500 struct linebuffer
502 long size;
503 char *buffer;
506 struct linebuffer lb; /* the current line */
507 struct linebuffer lb1; /* sometimes, a previous line in which a token lies */
508 struct linebuffer filename_lb; /* used to read in filenames */
511 void
512 print_version ()
514 #ifdef CTAGS
515 printf ("CTAGS ");
516 #ifdef ETAGS
517 printf ("and ");
518 #endif
519 #endif
520 #ifdef ETAGS
521 printf ("ETAGS ");
522 #endif
523 printf ("for Emacs version 19.0.\n");
525 exit (0);
528 void
529 print_help ()
531 printf ("These are the options accepted by %s. You may use unambiguous\n\
532 abbreviations for the long option names.\n\n", progname);
534 fputs ("\
535 -a, --append\n\
536 Append tag entries to existing tags file.\n\
537 -C, --c++\n\
538 Treat files with `.c' and `.h' extensions as C++ code, not C\n\
539 code. Files with `.C', `.H', `.cxx', `.hxx', or `.cc'\n\
540 extensions are always assumed to be C++ code.\n\
541 -d, --defines\n\
542 Create tag entries for #defines, too.", stdout);
544 #ifdef ETAGS
545 fputs (" This is the default\n\
546 behavior.", stdout);
547 #endif
549 fputs ("\n\
550 -D, --no-defines\n\
551 Don't create tag entries for #defines.", stdout);
553 #ifdef CTAGS
554 fputs (" This is the default\n\
555 behavior.", stdout);
556 #endif
558 puts ("\n\
559 -o FILE, --output=FILE\n\
560 Write the tags to FILE.\n\
561 -S, --ignore-indentation\n\
562 Don't rely on indentation quite as much as normal. Currently,\n\
563 this means not to assume that a closing brace in the first\n\
564 column is the final brace of a function or structure\n\
565 definition.\n\
566 -t, --typedefs\n\
567 Generate tag entries for typedefs. This is the default\n\
568 behavior.\n\
569 -T, --typedefs-and-c++\n\
570 Generate tag entries for typedefs, struct/enum/union tags, and\n\
571 C++ member functions.");
573 #ifdef ETAGS
574 puts ("-i FILE, --include=FILE\n\
575 Include a note in tag file indicating that, when searching for\n\
576 a tag, one should also consult the tags file FILE after\n\
577 checking the current file.");
578 #endif
580 #ifdef CTAGS
581 puts ("-B, --backward-search\n\
582 Write the search commands for the tag entries using '?', the\n\
583 backward-search command.\n\
584 -F, --forward-search\n\
585 Write the search commands for the tag entries using '/', the\n\
586 forward-search command.\n\
587 -u, --update\n\
588 Update the tag entries for the given files, leaving tag\n\
589 entries for other files in place. Currently, this is\n\
590 implemented by deleting the existing entries for the given\n\
591 files and then rewriting the new entries at the end of the\n\
592 tags file. It is often faster to simply rebuild the entire\n\
593 tag file than to use this.\n\
594 -v, --vgrind\n\
595 Generates an index of items intended for human consumption,\n\
596 similar to the output of vgrind. The index is sorted, and\n\
597 gives the page number of each item.\n\
598 -x, --cxref\n\
599 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
600 The output uses line numbers instead of page numbers, but\n\
601 beyond that the differences are cosmetic; try both to see\n\
602 which you like.\n\
603 -w, --no-warn\n\
604 Suppress warning messages about entries defined in multiple\n\
605 files.");
606 #endif
608 puts ("-V, --version\n\
609 Print the version of the program.\n\
610 -H, --help\n\
611 Print this help message.");
613 exit (0);
617 void
618 main (argc, argv)
619 int argc;
620 char *argv[];
622 char cmd[100];
623 int i;
624 unsigned int nincluded_files = 0;
625 char **included_files = (char **) alloca (argc * sizeof (char *));
626 char *this_file;
627 #ifdef VMS
628 char got_err;
630 extern char *gfnames ();
631 extern char *massage_name ();
632 #endif
634 progname = argv[0];
636 #ifndef CTAGS
637 emacs_tags_format = 1;
638 #else
639 emacs_tags_format = 0;
640 #endif
643 * If etags, always find typedefs and structure tags. Why not?
644 * Also default is to find macro constants.
646 if (emacs_tags_format)
647 typedefs = typedefs_and_cplusplus = constantypedefs = 1;
649 for (;;)
651 int opt;
652 opt = getopt_long (argc, argv, "aCdDo:StTi:BFuvxwVH", longopts, 0);
654 if (opt == EOF)
655 break;
657 switch (opt)
659 case '\0':
660 /* If getopt returns '\0', then it has already processed a
661 long-named option. We should do nothing. */
662 break;
664 /* Common options. */
665 case 'a':
666 append_to_tagfile++;
667 break;
668 case 'C':
669 cplusplus = 1;
670 break;
671 case 'd':
672 constantypedefs = 1;
673 break;
674 case 'D':
675 constantypedefs = 0;
676 break;
677 case 'o':
678 if (outfile)
680 fprintf (stderr,
681 "%s: -o flag may only be given once\n", progname);
682 goto usage;
684 outfile = optarg;
685 break;
686 case 'S':
687 noindentypedefs++;
688 break;
689 case 't':
690 typedefs++;
691 break;
692 case 'T':
693 typedefs++;
694 typedefs_and_cplusplus++;
695 break;
696 case 'V':
697 print_version ();
698 break;
699 case 'H':
700 print_help ();
701 break;
703 /* Etags options */
704 case 'i':
705 if (!emacs_tags_format)
706 goto usage;
707 included_files[nincluded_files++] = optarg;
708 break;
710 /* Ctags options. */
711 case 'B':
712 searchar = '?';
713 if (emacs_tags_format)
714 goto usage;
715 break;
716 case 'F':
717 searchar = '/';
718 if (emacs_tags_format)
719 goto usage;
720 break;
721 case 'u':
722 update++;
723 if (emacs_tags_format)
724 goto usage;
725 break;
726 case 'v':
727 vgrind_style++;
728 /*FALLTHRU*/
729 case 'x':
730 cxref_style++;
731 if (emacs_tags_format)
732 goto usage;
733 break;
734 case 'w':
735 no_warnings++;
736 if (emacs_tags_format)
737 goto usage;
738 break;
740 default:
741 goto usage;
745 if (optind == argc)
747 fprintf (stderr, "%s: No input files specified.\n", progname);
749 usage:
750 fprintf (stderr, "%s: Try '%s --help' for a complete list of options.\n",
751 progname, progname);
752 exit (BAD);
755 if (outfile == 0)
757 outfile = emacs_tags_format ? "TAGS" : "tags";
760 init (); /* set up boolean "functions" */
762 initbuffer (&lb);
763 initbuffer (&lb1);
764 initbuffer (&filename_lb);
766 * loop through files finding functions
768 if (emacs_tags_format)
770 if (streq (outfile, "-"))
771 outf = stdout;
772 else
773 outf = fopen (outfile, append_to_tagfile ? "a" : "w");
774 if (!outf)
776 perror (outfile);
777 exit (1);
781 #ifdef VMS
782 argc -= optind;
783 argv += optind;
784 while (gfnames (&argc, &argv, &got_err) != NULL)
786 if (got_err)
788 error ("Can't find file %s\n", this_file);
789 argc--, argv++;
791 else
793 this_file = massage_name (this_file);
794 #if 0
796 } /* solely to balance out the ifdef'd parens above */
797 #endif
798 #else
799 for (; optind < argc; optind++)
801 this_file = argv[optind];
802 if (1)
804 #endif
805 /* Input file named "-" means read file names from stdin
806 and use them. */
807 if (streq (this_file, "-"))
809 while (!feof (stdin))
811 (void) readline (&filename_lb, stdin);
812 if (strlen (filename_lb.buffer) > 0)
813 process_file (filename_lb.buffer);
816 else
817 process_file (this_file);
821 if (emacs_tags_format)
823 while (nincluded_files-- > 0)
824 fprintf (outf, "\f\n%s,include\n", *included_files++);
826 (void) fclose (outf);
827 exit (0);
830 if (cxref_style)
832 put_entries (head);
833 exit (GOOD);
835 if (update)
837 /* update cannot be set under VMS, so we may assume that argc
838 and argv have not been munged. */
839 for (i = optind; i < argc; i++)
841 sprintf (cmd,
842 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
843 outfile, argv[i], outfile);
844 (void) system (cmd);
846 append_to_tagfile++;
848 outf = fopen (outfile, append_to_tagfile ? "a" : "w");
849 if (outf == NULL)
851 perror (outfile);
852 exit (GOOD);
854 put_entries (head);
855 (void) fclose (outf);
856 if (update)
858 sprintf (cmd, "sort %s -o %s", outfile, outfile);
859 (void) system (cmd);
861 exit (GOOD);
866 * This routine is called on each file argument.
868 void
869 process_file (file)
870 char *file;
872 struct stat stat_buf;
874 stat (file, &stat_buf);
875 if (!(stat_buf.st_mode & S_IFREG) || !(stat_buf.st_mode & S_IFLNK))
877 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
878 return;
881 if (streq (file, outfile) && !streq (outfile, "-"))
883 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
884 return;
886 if (emacs_tags_format)
888 char *cp = rindex (file, '/');
889 if (cp)
890 ++cp;
891 else
892 cp = file;
894 find_entries (file);
895 if (emacs_tags_format)
897 fprintf (outf, "\f\n%s,%d\n",
898 file, total_size_of_entries (head));
899 put_entries (head);
900 free_tree (head);
901 head = NULL;
906 * This routine sets up the boolean psuedo-functions which work
907 * by seting boolean flags dependent upon the corresponding character
908 * Every char which is NOT in that string is not a white char. Therefore,
909 * all of the array "_wht" is set to FALSE, and then the elements
910 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
911 * of a char is TRUE if it is the string "white", else FALSE.
913 void
914 init ()
916 reg char *sp;
917 reg int i;
919 for (i = 0; i < 0177; i++)
921 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
922 _gd[i] = TRUE;
924 for (sp = white; *sp; sp++)
925 _wht[*sp] = TRUE;
926 for (sp = endtk; *sp; sp++)
927 _etk[*sp] = TRUE;
928 for (sp = intk; *sp; sp++)
929 _itk[*sp] = TRUE;
930 for (sp = begtk; *sp; sp++)
931 _btk[*sp] = TRUE;
932 for (sp = notgd; *sp; sp++)
933 _gd[*sp] = FALSE;
934 _wht[0] = _wht['\n'];
935 _etk[0] = _etk['\n'];
936 _btk[0] = _btk['\n'];
937 _itk[0] = _itk['\n'];
938 _gd[0] = _gd['\n'];
942 * This routine opens the specified file and calls the function
943 * which finds the function and type definitions.
945 void
946 find_entries (file)
947 char *file;
949 char *cp;
950 void prolog_funcs ();
952 inf = fopen (file, "r");
953 if (inf == NULL)
955 perror (file);
956 return;
958 curfile = savestr (file);
959 cp = rindex (file, '.');
961 header_file = (cp && (streq (cp + 1, "h")));
963 /* .tex, .aux or .bbl implies LaTeX source code */
964 if (cp && (streq (cp + 1, "tex") || streq (cp + 1, "aux")
965 || streq (cp + 1, "bbl")))
967 TEX_funcs (inf);
968 goto close_and_return;
970 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
971 if (cp && (streq (cp + 1, "l")
972 || streq (cp + 1, "el")
973 || streq (cp + 1, "lsp")
974 || streq (cp + 1, "lisp")
975 || streq (cp + 1, "cl")
976 || streq (cp + 1, "clisp")))
978 L_funcs (inf);
979 goto close_and_return;
981 /* .scm or .sm or .scheme or ... implies scheme source code */
982 if (cp && (streq (cp + 1, "sm")
983 || streq (cp + 1, "scm")
984 || streq (cp + 1, "scheme")
985 || streq (cp + 1, "t")
986 || streq (cp + 1, "sch")
987 || streq (cp + 1, "SM")
988 || streq (cp + 1, "SCM")
989 /* The `SCM' or `scm' prefix with a version number */
990 || (cp[-1] == 'm' && cp[-2] == 'c' && cp[-3] == 's'
991 && string_numeric_p (cp + 1))
992 || (cp[-1] == 'M' && cp[-2] == 'C' && cp[-3] == 'S'
993 && string_numeric_p (cp + 1))))
995 Scheme_funcs (inf);
996 fclose (inf);
997 return;
999 /* Assume that ".s" or ".a" is assembly code. -wolfgang. */
1000 if (cp && (cp[1] == 's' || cp[1] == 'a') && cp[2] == '\0')
1002 Asm_funcs (inf);
1003 fclose (inf);
1004 return;
1006 /* .C or .H or .cxx or .hxx or .cc: a C++ file */
1007 if (cp && (streq (cp + 1, "C")
1008 || streq (cp + 1, "H")
1009 || streq (cp + 1, "cxx")
1010 || streq (cp + 1, "hxx")
1011 || streq (cp + 1, "cc")))
1013 C_entries (C_PLPL); /* C++ */
1014 goto close_and_return;
1016 /* .cs or .hs: a C* file */
1017 if (cp && (cp[1] == 'c' || cp[1] == 'h') && cp[2] == 's' && cp[3] == '\0')
1019 C_entries (C_STAR);
1020 goto close_and_return;
1022 /* .pl implies prolog source code */
1023 if (cp && !strcmp (cp + 1, "pl"))
1025 prolog_funcs (inf);
1026 goto close_and_return;
1028 /* .p or .pas: a Pascal file */
1029 if (cp && (streq (cp + 1, "p")
1030 || streq (cp + 1, "pas")))
1032 PAS_funcs (inf);
1033 goto close_and_return;
1035 /* if not a .c or .h or .y file, try fortran */
1036 else if (cp && ((cp[1] != 'c'
1037 && cp[1] != 'h'
1038 && cp[1] != 'y')
1039 || (cp[1] != 0 && cp[2] != 0)))
1041 if (PF_funcs (inf) != 0)
1042 goto close_and_return;
1043 rewind (inf); /* no fortran tags found, try C */
1045 C_entries (cplusplus ? C_PLPL : 0);
1047 close_and_return:
1048 (void) fclose (inf);
1051 /* Nonzero if string STR is composed of digits. */
1054 string_numeric_p (str)
1055 char *str;
1057 while (*str)
1059 if (*str < '0' || *str > '9')
1060 return 0;
1062 return 1;
1065 /* Record a tag. */
1066 /* Should take a TOKEN* instead!! */
1068 void
1069 pfnote (name, is_func, rewritten, linestart, linelen, lno, cno)
1070 char *name; /* tag name */
1071 logical is_func; /* function or type name? */
1072 logical rewritten; /* tag different from text of definition? */
1073 char *linestart;
1074 int linelen;
1075 int lno;
1076 long cno;
1078 register char *fp;
1079 register NODE *np;
1080 char tem[51];
1081 char c;
1083 np = (NODE *) malloc (sizeof (NODE));
1084 if (np == NULL)
1086 if (!emacs_tags_format)
1088 /* It's okay to output early in etags -- it only disrupts the
1089 * character count of the tag entries, which is no longer used
1090 * by tags.el anyway.
1092 error ("too many entries to sort");
1094 put_entries (head);
1095 free_tree (head);
1096 head = NULL;
1097 np = xnew (1, NODE);
1099 /* If ctags mode, change name "main" to M<thisfilename>. */
1100 if (!emacs_tags_format && !cxref_style && streq (name, "main"))
1102 fp = rindex (curfile, '/');
1103 name = concat ("M", fp == 0 ? curfile : fp + 1, "");
1104 fp = rindex (name, '.');
1105 if (fp && fp[1] != '\0' && fp[2] == '\0')
1106 *fp = 0;
1107 rewritten = TRUE;
1109 np->name = savestr (name);
1110 np->file = curfile;
1111 np->is_func = is_func;
1112 np->rewritten = rewritten;
1113 np->lno = lno;
1114 /* UNCOMMENT THE +1 HERE: */
1115 np->cno = cno /* + 1 */ ; /* our char numbers are 0-base; emacs's are 1-base */
1116 np->left = np->right = 0;
1117 if (emacs_tags_format)
1119 c = linestart[linelen];
1120 linestart[linelen] = 0;
1122 else if (cxref_style == 0)
1124 sprintf (tem, strlen (linestart) < 50 ? "%s$" : "%.50s", linestart);
1125 linestart = tem;
1127 np->pat = savestr (linestart);
1128 if (emacs_tags_format)
1130 linestart[linelen] = c;
1133 add_node (np, &head);
1137 * free_tree ()
1138 * recurse on left children, iterate on right children.
1140 void
1141 free_tree (node)
1142 register NODE *node;
1144 while (node)
1146 register NODE *node_right = node->right;
1147 free_tree (node->left);
1148 free (node->name);
1149 free (node->pat);
1150 free ((char *) node);
1151 node = node_right;
1156 * add_node ()
1157 * Adds a node to the tree of nodes. In etags mode, we don't keep
1158 * it sorted; we just keep a linear list. In ctags mode, maintain
1159 * an ordered tree, with no attempt at balancing.
1161 * add_node is the only function allowed to add nodes, so it can
1162 * maintain state.
1164 void
1165 add_node (node, cur_node_p)
1166 NODE *node, **cur_node_p;
1168 register int dif;
1169 register NODE *cur_node = *cur_node_p;
1170 static NODE *last_node = NULL;/* careful */
1172 if (cur_node == NULL)
1174 *cur_node_p = node;
1175 last_node = node;
1176 return;
1179 if (emacs_tags_format)
1181 /* Etags Mode */
1182 if (!last_node)
1183 fatal ("internal error in add_node");
1184 last_node->right = node;
1185 last_node = node;
1187 else
1189 /* Ctags Mode */
1190 dif = strcmp (node->name, cur_node->name);
1193 * If this tag name matches an existing one, then
1194 * do not add the node, but maybe print a warning.
1196 if (!dif)
1198 if (node->file == cur_node->file)
1200 if (!no_warnings)
1202 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1203 node->file, lineno, node->name);
1204 fprintf (stderr, "Second entry ignored\n");
1206 return;
1208 if (!cur_node->been_warned && !no_warnings)
1210 fprintf (stderr,
1211 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1212 node->file, cur_node->file, node->name);
1214 cur_node->been_warned = TRUE;
1215 return;
1218 /* Maybe refuse to add duplicate nodes. */
1219 if (!permit_duplicates)
1221 if (!strcmp (node->name, cur_node->name)
1222 && !strcmp (node->file, cur_node->file))
1223 return;
1226 /* Actually add the node */
1227 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1231 void
1232 put_entries (node)
1233 reg NODE *node;
1235 reg char *sp;
1237 if (node == NULL)
1238 return;
1240 /* Output subentries that precede this one */
1241 put_entries (node->left);
1243 /* Output this entry */
1245 if (emacs_tags_format)
1247 if (node->rewritten)
1249 fprintf (outf, "%s\177%s\001%d,%d\n",
1250 node->name, node->pat, node->lno, node->cno);
1252 else
1254 fprintf (outf, "%s\177%d,%d\n",
1255 node->pat, node->lno, node->cno);
1258 else if (!cxref_style)
1260 fprintf (outf, "%s\t%s\t",
1261 node->name, node->file);
1263 if (node->is_func)
1264 { /* a function */
1265 putc (searchar, outf);
1266 putc ('^', outf);
1268 for (sp = node->pat; *sp; sp++)
1270 if (*sp == '\\' || *sp == searchar)
1271 putc ('\\', outf);
1272 putc (*sp, outf);
1274 putc (searchar, outf);
1276 else
1277 { /* a typedef; text pattern inadequate */
1278 fprintf (outf, "%d", node->lno);
1280 putc ('\n', outf);
1282 else if (vgrind_style)
1283 fprintf (stdout, "%s %s %d\n",
1284 node->name, node->file, (node->lno + 63) / 64);
1285 else
1286 fprintf (stdout, "%-16s%4d %-16s %s\n",
1287 node->name, node->lno, node->file, node->pat);
1289 /* Output subentries that follow this one */
1290 put_entries (node->right);
1293 /* Length of a number's decimal representation. */
1295 number_len (num)
1296 long num;
1298 int len = 0;
1299 if (!num)
1300 return 1;
1301 for (; num; num /= 10)
1302 ++len;
1303 return len;
1307 * Return total number of characters that put_entries will output for
1308 * the nodes in the subtree of the specified node. Works only if emacs_tags_format
1309 * is set, but called only in that case. This count is irrelevant with
1310 * the new tags.el, but is still supplied for backward compatibility.
1313 total_size_of_entries (node)
1314 reg NODE *node;
1316 reg int total;
1318 if (node == NULL)
1319 return 0;
1321 total = 0;
1322 for (; node; node = node->right)
1324 /* Count left subentries. */
1325 total += total_size_of_entries (node->left);
1327 /* Count this entry */
1328 total += strlen (node->pat) + 1;
1329 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1330 if (node->rewritten)
1331 total += 1 + strlen (node->name); /* \001name */
1334 return total;
1338 * The C symbol tables.
1341 Stab *C_stab, *C_PLPL_stab, *C_STAR_stab;
1344 * SYNOPSIS
1345 * Stab *get_C_stab (int c_ext);
1347 #define get_C_stab(c_ext) ((c_ext&C_STAR) ? C_STAR_stab : \
1348 c_ext ? C_PLPL_stab : \
1349 C_stab)
1351 void
1352 add_keyword (stab, sym, type)
1353 Stab *stab;
1354 char *sym;
1355 enum sym_type type;
1357 stab_search (stab, sym, strlen (sym))->type = type;
1360 Stab *
1361 C_create_stab (c_ext)
1362 int c_ext;
1364 Stab *stab;
1366 stab = stab_create ();
1368 /* C, C++ and C* */
1369 if (c_ext & C_PLPL)
1370 add_keyword (stab, "class", st_C_struct);
1371 if (c_ext & C_STAR)
1372 add_keyword (stab, "domain", st_C_struct);
1373 add_keyword (stab, "union", st_C_struct);
1374 add_keyword (stab, "struct", st_C_struct);
1375 add_keyword (stab, "enum", st_C_enum);
1376 add_keyword (stab, "typedef", st_C_typedef);
1377 add_keyword (stab, "define", st_C_define);
1378 add_keyword (stab, "long", st_C_typespec);
1379 add_keyword (stab, "short", st_C_typespec);
1380 add_keyword (stab, "int", st_C_typespec);
1381 add_keyword (stab, "char", st_C_typespec);
1382 add_keyword (stab, "float", st_C_typespec);
1383 add_keyword (stab, "double", st_C_typespec);
1384 add_keyword (stab, "signed", st_C_typespec);
1385 add_keyword (stab, "unsigned", st_C_typespec);
1386 add_keyword (stab, "const", st_C_typespec);
1387 add_keyword (stab, "volatile", st_C_typespec);
1389 return stab;
1392 void
1393 C_create_stabs ()
1395 C_stab = C_create_stab (0);
1396 C_PLPL_stab = C_create_stab (C_PLPL);
1397 C_STAR_stab = C_create_stab (C_STAR | C_PLPL);
1401 * C_entries ()
1402 * This routine finds functions and typedefs in C syntax and adds them
1403 * to the list.
1406 #define CNL_SAVE_DEFINEDEF \
1408 prev_linepos = linepos; \
1409 SET_FILEPOS (linepos, inf, charno); \
1410 lineno++; \
1411 charno += readline (&lb, inf); \
1412 lp = lb.buffer; \
1415 #define CNL \
1417 CNL_SAVE_DEFINEDEF; \
1418 definedef = dnone; \
1421 void
1422 C_entries (c_ext)
1423 int c_ext; /* extension of C? */
1425 register int c; /* latest char read; '\0' for end of line */
1426 register int tokoff; /* offset in line of beginning of latest token */
1427 register int toklen; /* length of latest token */
1428 register char *lp; /* pointer one beyond the character `c' */
1429 logical incomm, inquote, inchar, midtoken;
1430 int level; /* current curly brace level */
1431 char tokb[BUFSIZ];
1433 lineno = 0;
1434 charno = 0;
1435 lp = lb.buffer;
1436 *lp = 0;
1438 definedef = dnone;
1439 gotone = midtoken = inquote = inchar = incomm = FALSE;
1440 level = 0;
1441 tydef = none;
1442 next_token_is_func = 0;
1444 C_create_stabs ();
1446 while (!feof (inf))
1448 c = *lp++;
1449 if (c == '\\')
1451 /* If we're at the end of the line, the next character is a
1452 '\0'; don't skip it, because it's the thing that tells us
1453 to read the next line. */
1454 if (*lp == 0)
1455 continue;
1456 lp++;
1457 c = ' ';
1459 else if (incomm)
1461 if (c == '*' && *lp == '/')
1463 c = *lp++;
1464 incomm = FALSE;
1467 else if (inquote)
1469 if (c == '"')
1470 inquote = FALSE;
1471 continue;
1473 else if (inchar)
1475 if (c == '\'')
1476 inchar = FALSE;
1477 continue;
1479 else
1480 switch (c)
1482 case '"':
1483 inquote = TRUE;
1484 continue;
1485 case '\'':
1486 inchar = TRUE;
1487 continue;
1488 case '/':
1489 if (*lp == '*')
1491 lp++;
1492 incomm = TRUE;
1494 else if (c_ext && *lp == '/')
1496 c = 0; /* C++ comment: skip rest of line */
1498 continue;
1499 case '#':
1500 if (lp == lb.buffer + 1 && definedef == dnone)
1501 definedef = dsharpseen;
1502 continue;
1505 * The next two are to help the strucdef state machine.
1506 * They break when they are finished, so they don't interfere
1507 * with anything else that is going on.
1509 case ':':
1510 if (structdef == stagseen)
1511 structdef = scolonseen;
1512 break;
1513 /* Not a struct definition when semicolon seen in non-sinbody context. */
1514 case ';':
1515 if (structdef != snone && structdef != sinbody)
1517 structdef = snone;
1518 (void) strcpy (structtag, "<error 1>");
1520 break;
1522 case '{':
1523 if (tydef == begin)
1525 tydef = middle;
1527 switch (structdef)
1529 case skeyseen: /* unnamed struct */
1530 structtag[0] = '\0';
1531 /* FALLTHRU */
1532 case stagseen:
1533 case scolonseen: /* named struct */
1534 structdef = sinbody;
1535 break;
1537 level++;
1538 continue;
1539 case '}':
1540 if (!noindentypedefs && lp == lb.buffer + 1)
1541 level = 0; /* reset level if first column */
1542 else if (level > 0)
1543 level--;
1544 if (level == 0 && tydef == middle)
1546 tydef = end;
1548 if (level == 0)
1550 structdef = snone;
1551 (void) strcpy (structtag, "<error 2>");
1553 continue;
1555 if (LEVEL_OK_FOR_FUNCDEF () && !inquote && !incomm && gotone == FALSE)
1557 if (midtoken)
1559 if (endtoken (c))
1561 if (c_ext && c == ':' && *lp == ':' && intoken (*(lp + 1)))
1564 * This handles :: in the middle, but not at beginning
1565 * of an identifier.
1567 lp += 2;
1568 toklen += 3;
1570 else
1572 /* The following is no longer true,
1573 now that we advance to the next line
1574 at the end of processing the character. */
1576 * We've just finished lexing an identifier.
1577 * Note that if `c' is '\0', `lb' is the NEXT
1578 * line, `lp' points to the beginning of it, and
1579 * old pointers into `lb.buffer' may no longer be
1580 * valid, since `lb.buffer' may have been
1581 * reallocated. In this case (which corresponds
1582 * to an identifier followed immediately by a
1583 * newline), we re-read the line into lb1.
1585 * This would be faster if the previous line's
1586 * buffer were always saved.
1588 logical is_func;
1589 char *tok_linebuf;
1590 TOKEN tok;
1591 logical bingo, tok_at_end_of_line;
1592 char *lp_tmp; /* addressable */
1594 #if 0
1595 if (c == '\0')
1597 getline (GET_COOKIE (prev_linepos));
1598 tok_linebuf = lb1.buffer;
1599 tok_at_end_of_line = TRUE;
1600 tok.linestart = prev_linepos;
1601 tok.lineno = lineno - 1;
1603 else
1604 #endif
1606 tok_linebuf = lb.buffer;
1607 tok_at_end_of_line = FALSE;
1608 tok.linestart = linepos;
1609 tok.lineno = lineno;
1611 tok.p = tok_linebuf + tokoff;
1612 tok.len = toklen;
1613 tok.rewritten = FALSE;
1614 lp_tmp = lp;
1615 bingo = consider_token (c, &lp_tmp, &tok,
1616 &is_func, c_ext, level);
1617 lp = lp_tmp;
1618 if (bingo)
1620 if (GET_CHARNO (tok.linestart) != GET_CHARNO (linepos)
1621 && !tok_at_end_of_line)
1624 * Resynchronize tok.p to point into the right
1625 * linebuffer.
1627 getline (GET_COOKIE (tok.linestart));
1628 if (!tok.rewritten)
1629 tok.p = lb1.buffer + (tok.p - tok_linebuf);
1630 tok_linebuf = lb1.buffer;
1632 if (structdef == sinbody && definedef == dnone && is_func)
1633 { /* function defined in C++ class body */
1634 sprintf (tokb, "%s::%.*s",
1635 structtag[0] == '\0' ? "_anonymous_"
1636 : structtag,
1637 tok.len, tok.p);
1638 tok.rewritten = TRUE;
1640 else
1642 sprintf (tokb, "%.*s", tok.len, tok.p);
1644 pfnote (tokb, is_func, tok.rewritten, tok_linebuf,
1645 tokoff + toklen + (tok_at_end_of_line ? 0 : 1),
1646 tok.lineno, GET_CHARNO (tok.linestart));
1647 gotone = is_func; /* function */
1649 midtoken = FALSE;
1652 else if (intoken (c))
1653 toklen++;
1655 else if (begtoken (c))
1657 tokoff = lp - 1 - lb.buffer;
1658 toklen = 1;
1659 midtoken = TRUE;
1662 /* Detect end of line, after having handled the last token on the line. */
1663 if (c == 0)
1665 CNL;
1666 gotone = FALSE;
1668 if (c == ';' && tydef == end) /* clean with typedefs */
1669 tydef = none;
1674 * consider_token ()
1675 * checks to see if the current token is at the start of a
1676 * function, or corresponds to a typedef. It updates the input
1677 * line pointer *LPP so that the '(' will be in it when it returns.
1679 * *IS_FUNC gets TRUE iff the token is a function.
1680 * C_EXT is which language we are looking at.
1682 * In the future we will need some way to adjust where the end of
1683 * the token is; for instance, implementing the C++ keyword
1684 * `operator' properly will adjust the end of the token to be after
1685 * whatever follows `operator'.
1687 * Globals
1688 * structdef IN OUT
1689 * definedef IN OUT
1690 * tydef IN OUT
1693 logical
1694 consider_token (c, lpp, tokp, is_func, c_ext, level)
1695 reg char c; /* IN: first char after the token */
1696 char **lpp; /* IN OUT: *lpp points to 2nd char after the token */
1697 reg TOKEN *tokp; /* IN */
1698 logical *is_func; /* OUT */
1699 int c_ext; /* IN */
1700 int level; /* IN */
1702 reg char *lp = *lpp;
1703 logical firsttok; /* TRUE if have seen first token in ()'s */
1704 Stab_entry *tokse = stab_find (get_C_stab (c_ext), tokp->p, tokp->len);
1705 enum sym_type toktype = stab_type (tokse);
1707 *is_func = TRUE; /* a function */
1710 * Advance the definedef state machine. We set `gotone' for good measure;
1711 * it's redundant.
1713 switch (definedef)
1715 case dnone:
1716 /* We're not on a preprocessor line. */
1717 break;
1718 case dsharpseen:
1719 if (toktype == st_C_define)
1721 definedef = ddefineseen;
1722 gotone = FALSE;
1724 else
1726 definedef = dignorerest;
1727 gotone = TRUE;
1729 goto badone;
1730 case ddefineseen:
1732 * Make a tag for any macro.
1733 * This will flub up if there is a newline immediately following
1734 * the macro name.
1736 *is_func = (c == '(');
1737 definedef = dignorerest;
1738 gotone = TRUE;
1739 if (!*is_func && !constantypedefs)
1740 goto badone;
1741 goto goodone;
1742 case dignorerest:
1743 goto badone;
1744 default:
1745 error ("internal error: definedef value");
1749 * Skip whitespace and comments after the token. This loop should
1750 * also skip C++ comments.
1752 while (1)
1754 /* At whitespace => skip it. */
1755 if (iswhite (c))
1757 c = *lp++;
1759 /* At a comment => skip to end of comment. */
1760 else if (c == '/' && *lp == '*')
1762 /* If we find a comment, skip it. */
1763 while (!(c == '*' && *lp == '/'))
1765 c = *lp++;
1766 if (c == 0)
1768 lp--;
1769 break;
1772 if (c == '*' && *lp == '/')
1774 lp++; /* lp now points past the '/' */
1775 c = *lp++; /* c is now the --whatever-- after the '/' */
1778 else
1779 break;
1781 /* If we arrived at eof or eol, decide which one it is.
1782 If it's eol, advance to the next line. */
1784 if (c == 0)
1786 lp--;
1787 break;
1792 * If you have custom token types, or when configuration files can
1793 * define custom token types, this switch will be larger.
1795 switch (toktype)
1797 case st_C_typedef:
1798 if (typedefs)
1800 tydef = begin;
1801 goto badone;
1803 break;
1804 case st_C_typespec:
1805 if (tydef == begin || tydef == end)
1807 tydef = end;
1808 goto badone;
1810 break;
1814 * This structdef business is currently only invoked when level==0.
1815 * It should be recursively invoked whatever the level, and a stack of
1816 * states kept, to allow for definitions of structs within structs.
1818 * This structdef business is NOT invoked when we are ctags and the
1819 * file is plain C. This is because a struct tag may have the same
1820 * name as another tag, and this loses with ctags.
1822 * This if statement deals with the tydef state machine as follows: if
1823 * tydef==begin and token is struct/union/class/enum, goto badone.
1824 * All the other code here is for the structdef state machine.
1826 switch (toktype)
1828 case st_C_struct:
1829 case st_C_enum:
1830 if (tydef == begin || (typedefs_and_cplusplus && level == 0 && structdef == snone))
1832 structdef = skeyseen;
1833 structkey = tokse;
1835 goto badone;
1838 if (structdef == skeyseen)
1840 /* If next char is '{' or (for C++) ':', found a structure tag. */
1841 if (c == '{' || (c_ext && c == ':'))
1844 * We should do this slightly differently for straight C:
1845 * instead of defining `tag', as we now do, we should define
1846 * `struct tag'. (Do this only if the find-tag defaulting is
1847 * done on a sophisticated per-mode basis, so that if the user
1848 * says meta-. anywhere in `struct foo', the default comes out
1849 * `struct foo', not `struct' or `foo'.) This will require
1850 * remembering which keyword (struct/union/class/enum) we saw, as a
1851 * Stab_entry* -- this will also make it possible to merge the
1852 * skeyseen and senumseen states, if we want.
1854 if (stab_type (structkey) == st_C_struct)
1856 (void) strncpy (structtag, tokp->p, tokp->len);
1857 structtag[tokp->len] = '\0'; /* for struct/union/class */
1858 structdef = stagseen;
1860 else
1862 structtag[0] = '\0'; /* for enum */
1864 *is_func = FALSE; /* not a function */
1865 goto goodone;
1867 else
1869 /* Not a definition: reset structdef */
1870 structdef = snone;
1871 (void) strcpy (structtag, "<error 3>");
1873 /* Now what? And how does/should this stuff interact with tydef?? */
1874 /* Also maybe reset lp to *lpp for benefit of the function finding code. */
1876 if (tydef == begin)
1878 tydef = end;
1879 goto badone;
1881 if (tydef == end)
1883 *is_func = 0;
1884 goto goodone;
1886 /* Detect GNUmacs's function-defining macros. */
1887 if (definedef == dnone)
1889 if (strneq (tokp->p, "DEF", 3))
1891 next_token_is_func = TRUE;
1892 goto badone;
1894 else if (strneq (tokp->p, "EXFUN", 5))
1896 next_token_is_func = FALSE;
1897 goto badone;
1900 if (next_token_is_func)
1902 next_token_is_func = FALSE;
1903 goto goodone;
1905 if (c != '(')
1906 goto badone;
1907 firsttok = FALSE;
1908 while ((c = *lp++) != ')')
1910 if (c == 0)
1912 lp--;
1913 break;
1916 * This line used to confuse ctags:
1917 * int (*oldhup)();
1918 * This fixes it. A nonwhite char before the first
1919 * token, other than a / (in case of a comment in there)
1920 * makes this not a declaration.
1922 if (begtoken (c) || c == '/')
1923 firsttok++;
1924 else if (!iswhite (c) && !firsttok)
1925 goto badone;
1927 while (iswhite (c = *lp++))
1929 if (c == 0)
1931 lp--;
1932 break;
1935 if (!isgood (c))
1936 goto badone;
1938 goodone:
1939 *lpp = lp - 1;
1940 return TRUE;
1942 badone:
1943 *lpp = lp - 1;
1944 return FALSE;
1947 void
1948 getline (atcookie)
1949 long atcookie;
1951 long saveftell = ftell (inf);
1953 (void) fseek (inf, atcookie, 0);
1954 (void) readline (&lb1, inf);
1955 (void) fseek (inf, saveftell, 0);
1958 /* Fortran parsing */
1960 char *dbp;
1961 int pfcnt;
1964 PF_funcs (fi)
1965 FILE *fi;
1967 lineno = 0;
1968 charno = 0;
1969 pfcnt = 0;
1971 while (!feof (fi))
1973 lineno++;
1974 linecharno = charno;
1975 charno += readline (&lb, fi);
1976 dbp = lb.buffer;
1977 if (*dbp == '%')
1978 dbp++; /* Ratfor escape to fortran */
1979 while (isspace (*dbp))
1980 dbp++;
1981 if (*dbp == 0)
1982 continue;
1983 switch (*dbp | ' ')
1985 case 'i':
1986 if (tail ("integer"))
1987 takeprec ();
1988 break;
1989 case 'r':
1990 if (tail ("real"))
1991 takeprec ();
1992 break;
1993 case 'l':
1994 if (tail ("logical"))
1995 takeprec ();
1996 break;
1997 case 'c':
1998 if (tail ("complex") || tail ("character"))
1999 takeprec ();
2000 break;
2001 case 'd':
2002 if (tail ("double"))
2004 while (isspace (*dbp))
2005 dbp++;
2006 if (*dbp == 0)
2007 continue;
2008 if (tail ("precision"))
2009 break;
2010 continue;
2012 break;
2014 while (isspace (*dbp))
2015 dbp++;
2016 if (*dbp == 0)
2017 continue;
2018 switch (*dbp | ' ')
2020 case 'f':
2021 if (tail ("function"))
2022 getit ();
2023 continue;
2024 case 's':
2025 if (tail ("subroutine"))
2026 getit ();
2027 continue;
2028 case 'p':
2029 if (tail ("program"))
2031 getit ();
2032 continue;
2034 if (tail ("procedure"))
2035 getit ();
2036 continue;
2039 return (pfcnt);
2042 logical
2043 tail (cp)
2044 char *cp;
2046 register int len = 0;
2048 while (*cp && (*cp & ~' ') == ((*(dbp + len)) & ~' '))
2049 cp++, len++;
2050 if (*cp == 0)
2052 dbp += len;
2053 return (1);
2055 return (0);
2058 void
2059 takeprec ()
2061 while (isspace (*dbp))
2062 dbp++;
2063 if (*dbp != '*')
2064 return;
2065 dbp++;
2066 while (isspace (*dbp))
2067 dbp++;
2068 if (!isdigit (*dbp))
2070 --dbp; /* force failure */
2071 return;
2074 dbp++;
2075 while (isdigit (*dbp));
2078 void
2079 getit ()
2081 register char *cp;
2082 char c;
2083 char nambuf[BUFSIZ];
2085 while (isspace (*dbp))
2086 dbp++;
2087 if (*dbp == 0 || (!isalpha (*dbp)) && (*dbp != '_') && (*dbp != '$'))
2088 return;
2089 for (cp = dbp + 1; *cp && (isalpha (*cp) || isdigit (*cp)
2090 || (*cp == '_') || (*cp == '$')); cp++)
2091 continue;
2092 c = cp[0];
2093 cp[0] = 0;
2094 (void) strcpy (nambuf, dbp);
2095 cp[0] = c;
2096 pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2097 pfcnt++;
2100 /* Handle a file of assembler code. */
2102 void
2103 Asm_funcs (fi)
2104 FILE *fi;
2106 int i;
2107 register char c;
2109 lineno = 0;
2110 charno = 0;
2111 pfcnt = 0;
2113 while (!feof (fi))
2115 lineno++;
2116 linecharno = charno;
2117 charno += readline (&lb, fi);
2118 dbp = lb.buffer;
2120 for (i = 0; ((c = dbp[i]) && !isspace (c)) && (c != ':'); i++)
2123 if ((i > 0) && (c == ':'))
2124 getit ();
2128 /* Added by Mosur Mohan, 4/22/88 */
2129 /* Pascal parsing */
2131 #define GET_NEW_LINE \
2133 linecharno = charno; lineno++; \
2134 charno += 1 + readline (&lb, inf); \
2135 dbp = lb.buffer; \
2138 /* Locates tags for procedures & functions.
2139 * Doesn't do any type- or var-definitions.
2140 * It does look for the keyword "extern" or "forward"
2141 * immediately following the procedure statement;
2142 * if found, the tag is skipped.
2145 void
2146 PAS_funcs (fi)
2147 FILE *fi;
2149 struct linebuffer tline; /* mostly copied from C_entries */
2150 long save_lcno;
2151 int save_lineno;
2152 char c, *cp;
2153 char nambuf[BUFSIZ];
2155 logical /* each of these flags is TRUE iff: */
2156 incomm1, /* point is inside {..} comment */
2157 incomm2, /* point is inside (*..*) comment */
2158 inquote, /* point is inside '..' string */
2159 get_tagname, /* point is after PROCEDURE/FUNCTION */
2160 /* keyword, so next item = potential tag */
2161 found_tag, /* point is after a potential tag */
2162 inparms, /* point is within parameter-list */
2163 verify_tag; /* point has passed the parm-list, so the */
2164 /* next token will determine whether */
2165 /* this is a FORWARD/EXTERN to be */
2166 /* ignored, or whether it is a real tag */
2168 lineno = 0;
2169 charno = 0;
2170 dbp = lb.buffer;
2171 *dbp = 0;
2172 initbuffer (&tline);
2174 incomm1 = incomm2 = inquote = FALSE;
2175 found_tag = FALSE; /* have a proc name; check if extern */
2176 get_tagname = FALSE; /* have found "procedure" keyword */
2177 inparms = FALSE; /* found '(' after "proc" */
2178 verify_tag = FALSE; /* check if "extern" is ahead */
2180 /* long main loop to get next char */
2181 while (!feof (fi))
2183 c = *dbp++;
2184 if (c == 0) /* if end of line */
2186 GET_NEW_LINE;
2187 if (*dbp == 0)
2188 continue;
2189 if (!((found_tag && verify_tag) ||
2190 get_tagname))
2191 c = *dbp++; /* only if don't need *dbp pointing */
2192 /* to the beginning of the name of */
2193 /* the procedure or function */
2195 if (incomm1) /* within { - } comments */
2197 if (c == '}')
2198 incomm1 = FALSE;
2199 continue;
2201 else if (incomm2) /* within (* - *) comments */
2203 if (c == '*')
2205 while ((c = *dbp++) == '*')
2206 continue;
2207 if (c == 0)
2208 GET_NEW_LINE;
2209 if (c == ')')
2210 incomm2 = FALSE;
2212 continue;
2214 else if (inquote)
2216 if (c == '\'')
2217 inquote = FALSE;
2218 continue;
2220 else
2221 switch (c)
2223 case '\'':
2224 inquote = TRUE; /* found first quote */
2225 continue;
2226 case '{': /* found open-{-comment */
2227 incomm1 = TRUE;
2228 continue;
2229 case '(':
2230 if (*dbp == '*') /* found open-(*-comment */
2232 incomm2 = TRUE;
2233 dbp++;
2235 else if (found_tag) /* found '(' after tag, i.e., parm-list */
2236 inparms = TRUE;
2237 continue;
2238 case ')': /* end of parms list */
2239 if (inparms)
2240 inparms = FALSE;
2241 continue;
2242 case ';':
2243 if ((found_tag) && (!inparms)) /* end of proc or fn stmt */
2245 verify_tag = TRUE;
2246 break;
2248 continue;
2250 if ((found_tag) && (verify_tag) && (*dbp != ' '))
2252 /* check if this is an "extern" declaration */
2253 if (*dbp == 0)
2254 continue;
2255 if ((*dbp == 'e') || (*dbp == 'E'))
2257 if (tail ("extern")) /* superfluous, really! */
2259 found_tag = FALSE;
2260 verify_tag = FALSE;
2263 else if ((*dbp == 'f') || (*dbp == 'F'))
2265 if (tail ("forward")) /* check for forward reference */
2267 found_tag = FALSE;
2268 verify_tag = FALSE;
2271 if ((found_tag) && (verify_tag)) /* not external proc, so make tag */
2273 found_tag = FALSE;
2274 verify_tag = FALSE;
2275 pfnote (nambuf, TRUE, FALSE,
2276 tline.buffer, cp - tline.buffer + 1,
2277 save_lineno, save_lcno);
2278 continue;
2281 if (get_tagname) /* grab name of proc or fn */
2283 if (*dbp == 0)
2284 continue;
2286 /* save all values for later tagging */
2287 tline.size = lb.size;
2288 strcpy (tline.buffer, lb.buffer);
2289 save_lineno = lineno;
2290 save_lcno = linecharno;
2292 /* grab block name */
2293 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
2294 continue;
2295 c = cp[0];
2296 cp[0] = 0;
2297 strcpy (nambuf, dbp);
2298 cp[0] = c;
2299 dbp = cp; /* restore dbp to e-o-token */
2300 get_tagname = FALSE;
2301 found_tag = TRUE;
2302 continue;
2304 /* and proceed to check for "extern" */
2306 if ((!incomm1) && (!incomm2) && (!inquote) &&
2307 (!found_tag) && (!get_tagname))
2309 /* check for proc/fn keywords */
2310 switch (c | ' ')
2312 case 'p':
2313 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2314 get_tagname = TRUE;
2315 continue;
2316 case 'f':
2317 if (tail ("unction"))
2318 get_tagname = TRUE;
2319 continue;
2322 } /* while not e-o-f */
2326 * lisp tag functions
2327 * just look for (def or (DEF
2330 void
2331 L_funcs (fi)
2332 FILE *fi;
2334 lineno = 0;
2335 charno = 0;
2336 pfcnt = 0;
2338 while (!feof (fi))
2340 lineno++;
2341 linecharno = charno;
2342 charno += readline (&lb, fi);
2343 dbp = lb.buffer;
2344 if (dbp[0] == '(')
2346 if (L_isdef (dbp))
2348 while (!isspace (*dbp))
2349 dbp++;
2350 while (isspace (*dbp))
2351 dbp++;
2352 L_getit ();
2354 else
2356 /* Check for (foo::defmumble name-defined ... */
2357 while (*dbp && *dbp != ':' && !isspace (*dbp)
2358 && *dbp != '(' && *dbp != ')')
2359 dbp++;
2360 if (*dbp == ':')
2362 while (*dbp == ':')
2363 dbp++;
2365 if (L_isdef (dbp))
2367 while (!isspace (*dbp))
2368 dbp++;
2369 while (isspace (*dbp))
2370 dbp++;
2371 L_getit ();
2380 L_isdef (dbp)
2381 char *dbp;
2383 return ((dbp[1] == 'D' || dbp[1] == 'd') &&
2384 (dbp[2] == 'E' || dbp[2] == 'e') &&
2385 (dbp[3] == 'F' || dbp[3] == 'f'));
2388 void
2389 L_getit ()
2391 register char *cp;
2392 char c;
2393 char nambuf[BUFSIZ];
2395 if (*dbp == 0)
2396 return;
2397 for (cp = dbp + 1; *cp && *cp != '(' && *cp != ' '; cp++)
2398 continue;
2399 c = cp[0];
2400 cp[0] = 0;
2401 (void) strcpy (nambuf, dbp);
2402 cp[0] = c;
2403 pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2404 pfcnt++;
2408 * Scheme tag functions
2409 * look for (def... xyzzy
2410 * look for (def... (xyzzy
2411 * look for (def ... ((...(xyzzy ....
2412 * look for (set! xyzzy
2415 static void get_scheme ();
2417 void
2418 Scheme_funcs (fi)
2419 FILE *fi;
2421 lineno = 0;
2422 charno = 0;
2423 pfcnt = 0;
2425 while (!feof (fi))
2427 lineno++;
2428 linecharno = charno;
2429 charno += readline (&lb, fi);
2430 dbp = lb.buffer;
2431 if (dbp[0] == '(' &&
2432 (dbp[1] == 'D' || dbp[1] == 'd') &&
2433 (dbp[2] == 'E' || dbp[2] == 'e') &&
2434 (dbp[3] == 'F' || dbp[3] == 'f'))
2436 while (!isspace (*dbp))
2437 dbp++;
2438 /* Skip over open parens and white space */
2439 while (*dbp && (isspace (*dbp) || *dbp == '('))
2440 dbp++;
2441 get_scheme ();
2443 if (dbp[0] == '(' &&
2444 (dbp[1] == 'S' || dbp[1] == 's') &&
2445 (dbp[2] == 'E' || dbp[2] == 'e') &&
2446 (dbp[3] == 'T' || dbp[3] == 't') &&
2447 (dbp[4] == '!' || dbp[4] == '!') &&
2448 (isspace (dbp[5])))
2450 while (!isspace (*dbp))
2451 dbp++;
2452 /* Skip over white space */
2453 while (isspace (*dbp))
2454 dbp++;
2455 get_scheme ();
2460 static void
2461 get_scheme ()
2463 register char *cp;
2464 char c;
2465 char nambuf[BUFSIZ];
2467 if (*dbp == 0)
2468 return;
2469 /* Go till you get to white space or a syntactic break */
2470 for (cp = dbp + 1; *cp && *cp != '(' && *cp != ')' && !isspace (*cp); cp++)
2471 continue;
2472 /* Null terminate the string there. */
2473 c = cp[0];
2474 cp[0] = 0;
2475 /* Copy the string */
2476 strcpy (nambuf, dbp);
2477 /* Unterminate the string */
2478 cp[0] = c;
2479 /* Announce the change */
2480 pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2481 pfcnt++;
2484 /* Find tags in TeX and LaTeX input files. */
2486 /* TEX_toktab is a table of TeX control sequences that define tags.
2487 Each TEX_tabent records one such control sequence.
2488 CONVERT THIS TO USE THE Stab TYPE!! */
2490 struct TEX_tabent
2492 char *name;
2493 int len;
2496 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
2498 /* Default set of control sequences to put into TEX_toktab.
2499 The value of environment var TEXTAGS is prepended to this. */
2501 static char *TEX_defenv =
2502 ":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2504 void TEX_mode ();
2505 struct TEX_tabent *TEX_decode_env ();
2506 void TEX_getit ();
2507 int TEX_Token ();
2509 static char TEX_esc = '\\';
2510 static char TEX_opgrp = '{';
2511 static char TEX_clgrp = '}';
2514 * TeX/LaTeX scanning loop.
2517 void
2518 TEX_funcs (fi)
2519 FILE *fi;
2521 char *lasthit;
2523 lineno = 0;
2524 charno = 0;
2525 pfcnt = 0;
2527 /* Select either \ or ! as escape character. */
2528 TEX_mode (fi);
2530 /* Initialize token table once from environment. */
2531 if (!TEX_toktab)
2532 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
2534 while (!feof (fi))
2536 lineno++;
2537 linecharno = charno;
2538 charno += readline (&lb, fi);
2539 dbp = lb.buffer;
2540 lasthit = dbp;
2542 while (!feof (fi))
2543 { /* Scan each line in file */
2544 lineno++;
2545 linecharno = charno;
2546 charno += readline (&lb, fi);
2547 dbp = lb.buffer;
2548 lasthit = dbp;
2549 while (dbp = index (dbp, TEX_esc)) /* Look at each escape in line */
2551 register int i;
2553 if (!*(++dbp))
2554 break;
2555 linecharno += dbp - lasthit;
2556 lasthit = dbp;
2557 i = TEX_Token (lasthit);
2558 if (0 <= i)
2560 TEX_getit (lasthit, TEX_toktab[i].len);
2561 break; /* We only save a line once */
2568 #define TEX_LESC '\\'
2569 #define TEX_SESC '!'
2570 #define TEX_cmt '%'
2572 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
2573 /* chars accordingly. */
2575 void
2576 TEX_mode (f)
2577 FILE *f;
2579 int c;
2581 while ((c = getc (f)) != EOF)
2583 /* Skip to next line if we hit the TeX comment char. */
2584 if (c == TEX_cmt)
2585 while (c != '\n')
2586 c = getc (f);
2587 else if (c == TEX_LESC || c == TEX_SESC )
2588 break;
2591 if (c == TEX_LESC)
2593 TEX_esc = TEX_LESC;
2594 TEX_opgrp = '{';
2595 TEX_clgrp = '}';
2597 else
2599 TEX_esc = TEX_SESC;
2600 TEX_opgrp = '<';
2601 TEX_clgrp = '>';
2603 rewind (f);
2606 /* Read environment and prepend it to the default string. */
2607 /* Build token table. */
2609 struct TEX_tabent *
2610 TEX_decode_env (evarname, defenv)
2611 char *evarname;
2612 char *defenv;
2614 register char *env, *p;
2615 extern char *savenstr (), *index ();
2617 struct TEX_tabent *tab;
2618 int size, i;
2620 /* Append default string to environment. */
2621 env = getenv (evarname);
2622 if (!env)
2623 env = defenv;
2624 else
2625 env = concat (env, defenv, "");
2627 /* Allocate a token table */
2628 for (size = 1, p = env; p;)
2629 if ((p = index (p, ':')) && *(++p))
2630 size++;
2631 tab = xnew (size, struct TEX_tabent);
2633 /* Unpack environment string into token table. Be careful about */
2634 /* zero-length strings (leading ':', "::" and trailing ':') */
2635 for (i = 0; *env;)
2637 p = index (env, ':');
2638 if (!p) /* End of environment string. */
2639 p = env + strlen (env);
2640 if (p - env > 0)
2641 { /* Only non-zero strings. */
2642 tab[i].name = savenstr (env, p - env);
2643 tab[i].len = strlen (tab[i].name);
2644 i++;
2646 if (*p)
2647 env = p + 1;
2648 else
2650 tab[i].name = NULL; /* Mark end of table. */
2651 tab[i].len = 0;
2652 break;
2655 return tab;
2658 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
2659 The name being defined actually starts at (NAME + LEN + 1).
2660 But we seem to include the TeX command in the tag name. */
2662 void
2663 TEX_getit (name, len)
2664 char *name;
2665 int len;
2667 char *p = name + len;
2668 char nambuf[BUFSIZ];
2670 if (*name == 0)
2671 return;
2673 /* Let tag name extend to next group close (or end of line) */
2674 while (*p && *p != TEX_clgrp)
2675 p++;
2676 (void) strncpy (nambuf, name, p - name);
2677 nambuf[p - name] = 0;
2679 pfnote (nambuf, TRUE, FALSE, lb.buffer, strlen (lb.buffer), lineno, linecharno);
2680 pfcnt++;
2683 /* If the text at CP matches one of the tag-defining TeX command names,
2684 return the index of that command in TEX_toktab.
2685 Otherwise return -1. */
2687 /* Keep the capital `T' in `Token' for dumb truncating compilers
2688 (this distinguishes it from `TEX_toktab' */
2690 TEX_Token (cp)
2691 char *cp;
2693 int i;
2695 for (i = 0; TEX_toktab[i].len > 0; i++)
2696 if (strncmp (TEX_toktab[i].name, cp, TEX_toktab[i].len) == 0)
2697 return i;
2698 return -1;
2701 /* Support for Prolog. */
2703 /* whole head (not only functor, but also arguments)
2704 is gotten in compound term. */
2706 void
2707 prolog_getit (s, lineno, linecharno)
2708 char *s;
2709 int lineno;
2710 long linecharno;
2712 char nambuf[BUFSIZ], *save_s, tmpc;
2713 int insquote, npar;
2715 save_s = s;
2716 insquote = FALSE;
2717 npar = 0;
2718 while (1)
2720 if (*s == '\0') /* syntax error. */
2721 return;
2722 else if (insquote && *s == '\'' && *(s + 1) == '\'')
2723 s += 2;
2724 else if (*s == '\'')
2726 insquote = !insquote;
2727 s++;
2729 else if (!insquote && *s == '(')
2731 npar++;
2732 s++;
2734 else if (!insquote && *s == ')')
2736 npar--;
2737 s++;
2738 if (npar == 0)
2739 break;
2740 else if (npar < 0) /* syntax error. */
2741 return;
2743 else if (!insquote && *s == '.' && (isspace (*(s + 1)) || *(s + 1) == '\0'))
2744 { /* fullstop. */
2745 if (npar != 0) /* syntax error. */
2746 return;
2747 s++;
2748 break;
2750 else
2751 s++;
2753 tmpc = *s;
2754 *s = '\0';
2755 strcpy (nambuf, save_s);
2756 *s = tmpc;
2757 pfnote (nambuf, TRUE, save_s, strlen (nambuf), lineno, linecharno);
2760 /* It is assumed that prolog predicate starts from column 0. */
2762 void
2763 prolog_funcs (fi)
2764 FILE *fi;
2766 void skip_comment (), prolog_getit ();
2768 lineno = linecharno = charno = 0;
2769 while (!feof (fi))
2771 lineno++;
2772 linecharno += charno;
2773 charno = readline (&lb, fi) + 1; /* 1 for newline. */
2774 dbp = lb.buffer;
2775 if (isspace (dbp[0])) /* not predicate header. */
2776 continue;
2777 else if (dbp[0] == '%') /* comment. */
2778 continue;
2779 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
2780 skip_comment (&lb, fi, &lineno, &linecharno);
2781 else /* found. */
2782 prolog_getit (dbp, lineno, linecharno);
2786 void
2787 skip_comment (plb, fi, plineno, plinecharno)
2788 struct linebuffer *plb;
2789 FILE *fi;
2790 int *plineno; /* result */
2791 long *plinecharno; /* result */
2793 while (!substr ("*/", plb->buffer))
2795 (*plineno)++;
2796 *plinecharno += readline (plb, fi) + 1;
2797 } /* 1 for newline. */
2800 /* Return TRUE if 'sub' exists somewhere in 's'. */
2803 substr (sub, s)
2804 char *sub;
2805 char *s;
2807 while (*s && (s = index (s, *sub)))
2808 if (prestr (sub, s))
2809 return (TRUE);
2810 else
2811 s++;
2812 return (FALSE);
2815 /* Return TRUE if 'pre' is prefix of string 's'. */
2818 prestr (pre, s)
2819 char *pre;
2820 char *s;
2822 if (*pre == '\0')
2823 return (TRUE);
2824 else if (*pre == *s)
2825 return (prestr (pre + 1, s + 1));
2826 else
2827 return (FALSE);
2830 /* Initialize a linebuffer for use */
2832 void
2833 initbuffer (linebuffer)
2834 struct linebuffer *linebuffer;
2836 linebuffer->size = 200;
2837 linebuffer->buffer = xnew (200, char);
2841 * Read a line of text from `stream' into `linebuffer'.
2842 * Return the number of characters read from `stream',
2843 * which is the length of the line including the newline, if any.
2845 long
2846 readline (linebuffer, stream)
2847 struct linebuffer *linebuffer;
2848 register FILE *stream;
2850 char *buffer = linebuffer->buffer;
2851 register char *p = linebuffer->buffer;
2852 register char *pend;
2853 int newline; /* 1 if ended with newline, 0 if ended with EOF */
2855 pend = p + linebuffer->size; /* Separate to avoind 386/IX compiler bug. */
2857 while (1)
2859 register int c = getc (stream);
2860 if (p == pend)
2862 linebuffer->size *= 2;
2863 buffer = (char *) xrealloc (buffer, linebuffer->size);
2864 p += buffer - linebuffer->buffer;
2865 pend = buffer + linebuffer->size;
2866 linebuffer->buffer = buffer;
2868 if (c < 0 || c == '\n')
2870 *p = 0;
2871 newline = (c == '\n' ? 1 : 0);
2872 break;
2874 *p++ = c;
2877 return p - buffer + newline;
2880 char *
2881 savestr (cp)
2882 char *cp;
2884 return savenstr (cp, strlen (cp));
2887 char *
2888 savenstr (cp, len)
2889 char *cp;
2890 int len;
2892 register char *dp;
2894 dp = xnew (len + 1, char);
2895 (void) strncpy (dp, cp, len);
2896 dp[len] = '\0';
2897 return dp;
2900 #ifdef notdef
2902 * Return the ptr in sp at which the character c last
2903 * appears; NULL if not found
2905 * Identical to v7 rindex, included for portability.
2908 char *
2909 rindex (sp, c)
2910 register char *sp, c;
2912 register char *r;
2914 r = NULL;
2917 if (*sp == c)
2918 r = sp;
2919 } while (*sp++);
2920 return (r);
2924 * Return the ptr in sp at which the character c first
2925 * appears; NULL if not found
2927 * Identical to v7 index, included for portability.
2930 char *
2931 index (sp, c)
2932 register char *sp, c;
2936 if (*sp == c)
2937 return (sp);
2938 } while (*sp++);
2939 return (NULL);
2942 #endif /* notdef */
2944 /* Print error message and exit. */
2946 /* VARARGS1 */
2947 void
2948 fatal (s1, s2)
2949 char *s1, *s2;
2951 error (s1, s2);
2952 exit (1);
2955 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
2957 /* VARARGS1 */
2958 void
2959 error (s1, s2)
2960 char *s1, *s2;
2962 fprintf (stderr, "%s: ", progname);
2963 fprintf (stderr, s1, s2);
2964 fprintf (stderr, "\n");
2967 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
2969 char *
2970 concat (s1, s2, s3)
2971 char *s1, *s2, *s3;
2973 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
2974 char *result = xnew (len1 + len2 + len3 + 1, char);
2976 (void) strcpy (result, s1);
2977 (void) strcpy (result + len1, s2);
2978 (void) strcpy (result + len1 + len2, s3);
2979 *(result + len1 + len2 + len3) = 0;
2981 return result;
2984 /* Like malloc but get fatal error if memory is exhausted. */
2986 char *
2987 xmalloc (size)
2988 int size;
2990 char *result = malloc (size);
2991 if (!result)
2992 fatal ("virtual memory exhausted", 0);
2993 return result;
2996 char *
2997 xrealloc (ptr, size)
2998 char *ptr;
2999 int size;
3001 char *result = realloc (ptr, size);
3002 if (!result)
3003 fatal ("virtual memory exhausted");
3004 return result;