(hack-local-variables-prop-line): Search two lines if "#!".
[emacs.git] / lib-src / etags.c
blob0a77dbc1467c15b1fd7120daa2dd24235a8b6abe
1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 1987, 1988, 1989, 1993 Free Software Foundation, Inc. and Ken Arnold
4 This file is not considered part of GNU Emacs.
6 This program 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 2 of the License, or
9 (at your option) any later version.
11 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 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++.
28 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
31 char pot_etags_version[] = "@(#) pot revision number is 10.18";
33 #ifdef MSDOS
34 #include <fcntl.h>
35 #endif /* MSDOS */
37 #ifdef HAVE_CONFIG_H
38 #include <../src/config.h>
39 #endif
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
46 #if !defined (S_ISREG) && defined (S_IFREG)
47 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
48 #endif
50 #include "getopt.h"
52 extern char *getenv ();
53 extern char *getcwd ();
56 /* Define CTAGS to make the program "ctags" compatible with the usual one.
57 Let it undefined to make the program "etags", which makes emacs-style
58 tag tables and tags typedefs, #defines and struct/union/enum by default. */
59 #ifdef CTAGS
60 # undef CTAGS
61 # define CTAGS TRUE
62 #else
63 # define CTAGS FALSE
64 #endif
66 /* Exit codes for success and failure. */
67 #ifdef VMS
68 #define GOOD 1
69 #define BAD 0
70 #else
71 #define GOOD 0
72 #define BAD 1
73 #endif
76 * The FILEPOS abstract type, which represents a position in a file,
77 * plus the following accessor functions:
79 * long GET_CHARNO (pos)
80 * returns absolute char number.
81 * void SET_FILEPOS (pos, fp, charno)
82 * FILE *fp; long charno;
83 * sets `pos' from the current file
84 * position of `fp' and from `charno',
85 * which must be the absolute character
86 * number corresponding to the current
87 * position of `fp'.
89 * The `pos' parameter is an lvalue expression of type FILEPOS.
90 * Parameters to the accessor functions are evaluated 0 or more times,
91 * and so must have no side effects.
93 * FILEPOS objects can also be assigned and passed to and from
94 * functions in the normal C manner.
96 * Implementation notes: the `+ 0' is to enforce rvalue-ness.
99 #ifndef DEBUG
100 /* real implementation */
101 typedef long FILEPOS;
102 #define GET_CHARNO(pos) ((pos) + 0)
103 #define SET_FILEPOS(pos, fp, cno) ((void) ((pos) = (cno)))
104 #else
105 /* debugging implementation */
106 typedef struct
108 long charno;
109 } FILEPOS;
111 #define GET_CHARNO(pos) ((pos).charno + 0)
112 #define SET_FILEPOS(pos, fp, cno) \
113 ((void) ((pos).charno = (cno), \
114 (cno) != ftell (fp) ? (error ("SET_FILEPOS inconsistency"), 0) \
115 : 0))
116 #endif
118 #define streq(s, t) (strcmp (s, t) == 0)
119 #define strneq(s, t, n) (strncmp (s, t, n) == 0)
120 #define logical int
122 #define TRUE 1
123 #define FALSE 0
125 #define iswhite(arg) (_wht[arg]) /* T if char is white */
126 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
127 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
128 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
130 #define max(I1,I2) ((I1) > (I2) ? (I1) : (I2))
132 struct nd_st
133 { /* sorting structure */
134 char *name; /* function or type name */
135 char *file; /* file name */
136 logical is_func; /* use pattern or line no */
137 logical named; /* list name separately */
138 logical been_warned; /* set if noticed dup */
139 int lno; /* line number tag is on */
140 long cno; /* character number line starts on */
141 char *pat; /* search pattern */
142 struct nd_st *left, *right; /* left and right sons */
145 typedef struct nd_st NODE;
147 logical header_file; /* TRUE if .h file, FALSE o.w. */
148 /* boolean "functions" (see init) */
149 logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
151 char cwd [BUFSIZ]; /* current working directory */
152 char *outfiledir; /* directory of tagfile */
154 char *concat ();
155 char *savenstr (), *savestr ();
156 char *etags_index (), *etags_rindex ();
157 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
158 char *xmalloc (), *xrealloc ();
159 int L_isdef (), L_isquote ();
160 int PF_funcs ();
161 int total_size_of_entries ();
162 logical consider_token ();
163 logical tail ();
164 long readline ();
165 void Asm_funcs ();
166 void C_entries ();
167 void L_funcs ();
168 void L_getit ();
169 void PAS_funcs ();
170 void Scheme_funcs ();
171 void TEX_funcs ();
172 void add_node ();
173 void error ();
174 void fatal ();
175 logical find_entries ();
176 void free_tree ();
177 void getit ();
178 void init ();
179 void initbuffer ();
180 void initbuffer ();
181 void pfnote ();
182 void process_file ();
183 void put_entries ();
184 void takeprec ();
187 * MACRO
188 * xnew -- allocate storage
190 * SYNOPSIS
191 * Type *xnew (int n, Type);
193 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
196 * Symbol table types.
198 enum sym_type
200 st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
205 typedef int LINENO;
207 typedef struct
209 char *p;
210 int len;
211 LINENO lineno;
212 logical named;
213 } TOKEN;
215 /* C extensions.
217 #define C_PLPL 0x00001 /* C++ */
218 #define C_STAR 0x00003 /* C* */
219 #define YACC 0x10000 /* yacc file */
221 char searchar = '/'; /* use /.../ searches */
223 LINENO lineno; /* line number of current line */
224 long charno; /* current character number */
226 long linecharno; /* charno of start of line; not used by C, but
227 * by every other language.
230 char *curfile, /* current input file name */
231 *outfile, /* output file */
232 *white = " \f\t\n", /* white chars */
233 *endtk = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
234 /* token starting chars */
235 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
236 /* valid in-token chars */
237 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
239 int append_to_tagfile; /* -a: append to tags */
240 /* The following three default to 1 for etags, but to 0 for ctags. */
241 int typedefs; /* -t: create tags for typedefs */
242 int typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
243 /* 0 struct/enum/union decls, and C++ */
244 /* member functions. */
245 int constantypedefs; /* -d: create tags for C #define and enum */
246 /* constants. Enum consts not implemented. */
247 /* -D: opposite of -d. Default under ctags. */
248 int update; /* -u: update tags */
249 int vgrind_style; /* -v: create vgrind style index output */
250 int no_warnings; /* -w: suppress warnings */
251 int cxref_style; /* -x: create cxref style output */
252 int cplusplus; /* .[hc] means C++, not C */
253 int noindentypedefs; /* -S: ignore indentation in C */
255 /* Name this program was invoked with. */
256 char *progname;
258 struct option longopts[] = {
259 { "append", no_argument, NULL, 'a' },
260 { "backward-search", no_argument, NULL, 'B' },
261 { "c++", no_argument, NULL, 'C' },
262 { "cxref", no_argument, NULL, 'x' },
263 { "defines", no_argument, NULL, 'd' },
264 { "forward-search", no_argument, NULL, 'F' },
265 { "help", no_argument, NULL, 'H' },
266 { "ignore-indentation", no_argument, NULL, 'S' },
267 { "include", required_argument, NULL, 'i' },
268 { "no-defines", no_argument, NULL, 'D' },
269 { "no-warn", no_argument, NULL, 'w' },
270 { "output", required_argument, NULL, 'o' },
271 { "typedefs", no_argument, NULL, 't' },
272 { "typedefs-and-c++", no_argument, NULL, 'T' },
273 { "update", no_argument, NULL, 'u' },
274 { "version", no_argument, NULL, 'V' },
275 { "vgrind", no_argument, NULL, 'v' },
276 { 0 }
279 FILE *inf, /* ioptr for current input file */
280 *outf; /* ioptr for tags file */
282 NODE *head; /* the head of the binary tree of tags */
284 int permit_duplicates = 1; /* Nonzero means allow duplicate tags. */
286 /* A `struct linebuffer' is a structure which holds a line of text.
287 `readline' reads a line from a stream into a linebuffer
288 and works regardless of the length of the line. */
290 struct linebuffer
292 long size;
293 char *buffer;
296 struct linebuffer lb; /* the current line */
297 struct linebuffer filename_lb; /* used to read in filenames */
298 struct
300 FILEPOS linepos;
301 struct linebuffer lb; /* used by C_entries instead of lb */
302 } lbs[2];
304 void
305 print_version ()
307 #ifdef VERSION
308 printf ("%s for Emacs version %g.\n", (CTAGS) ? "CTAGS" : "ETAGS", VERSION);
309 #else
310 printf ("%s for Emacs version 19.\n", (CTAGS) ? "CTAGS" : "ETAGS");
311 #endif
313 exit (GOOD);
316 void
317 print_help ()
319 printf ("These are the options accepted by %s. You may use unambiguous\n\
320 abbreviations for the long option names. A - as file name means read file\n\
321 names from stdin.\n\n", progname);
323 puts ("-a, --append\n\
324 Append tag entries to existing tags file.");
326 if (CTAGS)
327 puts ("-B, --backward-search\n\
328 Write the search commands for the tag entries using '?', the\n\
329 backward-search command.");
331 puts ("-C, --c++\n\
332 Treat files with `.c' and `.h' extensions as C++ code, not C\n\
333 code. Files with `.C', `.H', `.cxx', `.hxx', or `.cc'\n\
334 extensions are always assumed to be C++ code.");
336 if (CTAGS)
337 puts ("-d, --defines\n\
338 Create tag entries for C #defines, too.");
339 else
340 puts ("-D, --no-defines\n\
341 Don't create tag entries for C #defines. This makes the tags\n\
342 file smaller.");
344 if (CTAGS)
345 puts ("-F, --forward-search\n\
346 Write the search commands for the tag entries using '/', the\n\
347 forward-search command.");
349 if (!CTAGS)
350 puts ("-i FILE, --include=FILE\n\
351 Include a note in tag file indicating that, when searching for\n\
352 a tag, one should also consult the tags file FILE after\n\
353 checking the current file.");
355 puts ("-o FILE, --output=FILE\n\
356 Write the tags to FILE.");
357 puts ("-S, --ignore-indentation\n\
358 Don't rely on indentation quite as much as normal. Currently,\n\
359 this means not to assume that a closing brace in the first\n\
360 column is the final brace of a function or structure\n\
361 definition in C and C++.");
363 if (CTAGS)
365 puts ("-t, --typedefs\n\
366 Generate tag entries for C typedefs.");
367 puts ("-T, --typedefs-and-c++\n\
368 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
369 and C++ member functions.");
372 if (CTAGS)
374 puts ("-u, --update\n\
375 Update the tag entries for the given files, leaving tag\n\
376 entries for other files in place. Currently, this is\n\
377 implemented by deleting the existing entries for the given\n\
378 files and then rewriting the new entries at the end of the\n\
379 tags file. It is often faster to simply rebuild the entire\n\
380 tag file than to use this.");
381 puts ("-v, --vgrind\n\
382 Generates an index of items intended for human consumption,\n\
383 similar to the output of vgrind. The index is sorted, and\n\
384 gives the page number of each item.");
385 puts ("-x, --cxref\n\
386 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
387 The output uses line numbers instead of page numbers, but\n\
388 beyond that the differences are cosmetic; try both to see\n\
389 which you like.");
390 puts ("-w, --no-warn\n\
391 Suppress warning messages about entries defined in multiple\n\
392 files.");
395 puts ("-V, --version\n\
396 Print the version of the program.\n\
397 -H, --help\n\
398 Print this help message.");
400 exit (GOOD);
404 void
405 main (argc, argv)
406 int argc;
407 char *argv[];
409 char cmd[100];
410 int i;
411 unsigned int nincluded_files = 0;
412 char **included_files = xnew (argc, char *);
413 char *this_file;
414 #ifdef VMS
415 char got_err;
417 extern char *gfnames ();
418 extern char *massage_name ();
419 #endif
421 #ifdef MSDOS
422 _fmode = O_BINARY; /* all of files are treated as binary files */
423 #endif /* MSDOS */
425 progname = argv[0];
428 * If etags, always find typedefs and structure tags. Why not?
429 * Also default is to find macro constants.
431 if (!CTAGS)
432 typedefs = typedefs_and_cplusplus = constantypedefs = 1;
434 for (;;)
436 int opt;
437 opt = getopt_long (argc, argv, "aCdDf:o:StTi:BFuvxwVH", longopts, 0);
439 if (opt == EOF)
440 break;
442 switch (opt)
444 case '\0':
445 /* If getopt returns '\0', then it has already processed a
446 long-named option. We should do nothing. */
447 break;
449 /* Common options. */
450 case 'a':
451 append_to_tagfile++;
452 break;
453 case 'C':
454 cplusplus = 1;
455 break;
456 case 'd':
457 constantypedefs = 1;
458 break;
459 case 'D':
460 constantypedefs = 0;
461 break;
462 case 'f': /* for compatibility with old makefiles */
463 case 'o':
464 if (outfile)
466 fprintf (stderr,
467 "%s: -%c flag may only be given once\n", progname, opt);
468 goto usage;
470 outfile = optarg;
471 break;
472 case 'S':
473 noindentypedefs++;
474 break;
475 case 'V':
476 print_version ();
477 break;
478 case 'H':
479 print_help ();
480 break;
482 /* Etags options */
483 case 'i':
484 if (CTAGS)
485 goto usage;
486 included_files[nincluded_files++] = optarg;
487 break;
489 /* Ctags options. */
490 case 'B':
491 searchar = '?';
492 if (!CTAGS) goto usage;
493 break;
494 case 'F':
495 searchar = '/';
496 if (!CTAGS) goto usage;
497 break;
498 case 't':
499 typedefs++;
500 if (!CTAGS) goto usage;
501 break;
502 case 'T':
503 typedefs++;
504 typedefs_and_cplusplus++;
505 if (!CTAGS) goto usage;
506 break;
507 case 'u':
508 update++;
509 if (!CTAGS) goto usage;
510 break;
511 case 'v':
512 vgrind_style++;
513 /*FALLTHRU*/
514 case 'x':
515 cxref_style++;
516 if (!CTAGS) goto usage;
517 break;
518 case 'w':
519 no_warnings++;
520 if (!CTAGS) goto usage;
521 break;
523 default:
524 goto usage;
528 if (optind == argc && nincluded_files == 0)
530 fprintf (stderr, "%s: No input files specified.\n", progname);
532 usage:
533 fprintf (stderr, "%s: Try `%s --help' for a complete list of options.\n",
534 progname, progname);
535 exit (BAD);
538 if (outfile == NULL)
540 outfile = CTAGS ? "tags" : "TAGS";
542 getcwd (cwd, BUFSIZ); /* the current working directory */
543 strcat (cwd, "/");
544 if (streq (outfile, "-"))
546 outfiledir = cwd;
548 else
550 outfiledir = absolute_dirname (outfile, cwd);
553 init (); /* set up boolean "functions" */
555 initbuffer (&lb);
556 initbuffer (&lbs[0].lb);
557 initbuffer (&lbs[1].lb);
558 initbuffer (&filename_lb);
560 * loop through files finding functions
562 if (!CTAGS)
564 if (streq (outfile, "-"))
565 outf = stdout;
566 else
567 outf = fopen (outfile, append_to_tagfile ? "a" : "w");
568 if (outf == NULL)
570 perror (outfile);
571 exit (BAD);
575 #ifdef VMS
576 argc -= optind;
577 argv += optind;
578 while (gfnames (&argc, &argv, &got_err) != NULL)
580 if (got_err)
582 error ("Can't find file %s\n", this_file);
583 argc--, argv++;
585 else
587 this_file = massage_name (this_file);
588 #if 0
590 } /* solely to balance out the ifdef'd parens above */
591 #endif
592 #else
593 for (; optind < argc; optind++)
595 this_file = argv[optind];
596 #endif
597 /* Input file named "-" means read file names from stdin and use them. */
598 if (streq (this_file, "-"))
600 while (!feof (stdin))
602 (void) readline (&filename_lb, stdin);
603 if (strlen (filename_lb.buffer) > 0)
604 process_file (filename_lb.buffer);
607 else
608 process_file (this_file);
611 if (!CTAGS)
613 while (nincluded_files-- > 0)
614 fprintf (outf, "\f\n%s,include\n", *included_files++);
616 (void) fclose (outf);
617 exit (GOOD);
620 if (cxref_style)
622 put_entries (head);
623 exit (GOOD);
625 if (update)
627 /* update cannot be set under VMS, so we may assume that argc
628 and argv have not been munged. */
629 for (i = optind; i < argc; i++)
631 sprintf (cmd,
632 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
633 outfile, argv[i], outfile);
634 (void) system (cmd);
636 append_to_tagfile++;
638 outf = fopen (outfile, append_to_tagfile ? "a" : "w");
639 if (outf == NULL)
641 perror (outfile);
642 exit (GOOD);
644 put_entries (head);
645 (void) fclose (outf);
646 if (update)
648 sprintf (cmd, "sort %s -o %s", outfile, outfile);
649 (void) system (cmd);
651 exit (GOOD);
656 * This routine is called on each file argument.
658 void
659 process_file (file)
660 char *file;
662 struct stat stat_buf;
664 if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
666 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
667 return;
669 if (streq (file, outfile) && !streq (outfile, "-"))
671 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
672 return;
674 if (!find_entries (file))
676 return;
678 if (!CTAGS)
680 char *filename;
682 if (file[0] == '/')
684 /* file is an absolute filename. Canonicalise it. */
685 filename = absolute_filename (file, cwd);
687 else
689 /* file is a filename relative to cwd. Make it relative
690 to the directory of the tags file. */
691 filename = relative_filename (file, outfiledir);
693 fprintf (outf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
694 put_entries (head);
695 free_tree (head);
696 head = NULL;
701 * This routine sets up the boolean pseudo-functions which work
702 * by setting boolean flags dependent upon the corresponding character
703 * Every char which is NOT in that string is not a white char. Therefore,
704 * all of the array "_wht" is set to FALSE, and then the elements
705 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
706 * of a char is TRUE if it is the string "white", else FALSE.
708 void
709 init ()
711 register char *sp;
712 register int i;
714 for (i = 0; i < 0177; i++)
715 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
716 for (sp = white; *sp; sp++)
717 _wht[*sp] = TRUE;
718 for (sp = endtk; *sp; sp++)
719 _etk[*sp] = TRUE;
720 for (sp = intk; *sp; sp++)
721 _itk[*sp] = TRUE;
722 for (sp = begtk; *sp; sp++)
723 _btk[*sp] = TRUE;
724 _wht[0] = _wht['\n'];
725 _etk[0] = _etk['\n'];
726 _btk[0] = _btk['\n'];
727 _itk[0] = _itk['\n'];
731 * This routine opens the specified file and calls the function
732 * which finds the function and type definitions.
734 logical
735 find_entries (file)
736 char *file;
738 char *cp;
739 void prolog_funcs ();
741 inf = fopen (file, "r");
742 if (inf == NULL)
744 perror (file);
745 return FALSE;
747 curfile = savestr (file);
748 cp = etags_rindex (file, '.');
750 header_file = (cp && (streq (cp + 1, "h")));
752 /* .tex, .aux or .bbl implies LaTeX source code */
753 if (cp && (streq (cp + 1, "tex") || streq (cp + 1, "aux")
754 || streq (cp + 1, "bbl")))
756 TEX_funcs (inf);
757 goto close_and_return;
759 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
760 if (cp && (streq (cp + 1, "l")
761 || streq (cp + 1, "el")
762 || streq (cp + 1, "lsp")
763 || streq (cp + 1, "lisp")
764 || streq (cp + 1, "cl")
765 || streq (cp + 1, "clisp")))
767 L_funcs (inf);
768 goto close_and_return;
770 /* .scm or .sm or .scheme or ... implies scheme source code */
771 if (cp && (streq (cp + 1, "sm")
772 || streq (cp + 1, "scm")
773 || streq (cp + 1, "scheme")
774 || streq (cp + 1, "t")
775 || streq (cp + 1, "sch")
776 || streq (cp + 1, "ss")
777 || streq (cp + 1, "SM")
778 || streq (cp + 1, "SCM")
779 /* The `SCM' or `scm' prefix with a version number */
780 || (cp[-1] == 'm' && cp[-2] == 'c' && cp[-3] == 's'
781 && string_numeric_p (cp + 1))
782 || (cp[-1] == 'M' && cp[-2] == 'C' && cp[-3] == 'S'
783 && string_numeric_p (cp + 1))))
785 Scheme_funcs (inf);
786 goto close_and_return;
788 /* Assume that ".s" or ".a" is assembly code. -wolfgang.
789 Or even ".sa". */
790 if (cp && (streq (cp + 1, "s")
791 || streq (cp + 1, "a")
792 || streq (cp + 1, "sa")))
794 Asm_funcs (inf);
795 goto close_and_return;
797 /* .C or .H or .cxx or .hxx or .cc: a C++ file */
798 if (cp && (streq (cp + 1, "C")
799 || streq (cp + 1, "H")
800 || streq (cp + 1, "cxx")
801 || streq (cp + 1, "hxx")
802 || streq (cp + 1, "cc")))
804 C_entries (C_PLPL); /* C++ */
805 goto close_and_return;
807 /* .cs or .hs: a C* file */
808 if (cp && (streq (cp + 1, "cs")
809 || streq (cp + 1, "hs")))
811 C_entries (C_STAR);
812 goto close_and_return;
814 /* .y: a yacc file */
815 if (cp && (streq (cp + 1, "y")))
817 C_entries (YACC);
818 goto close_and_return;
820 /* .pl implies prolog source code */
821 if (cp && streq (cp + 1, "pl"))
823 prolog_funcs (inf);
824 goto close_and_return;
826 /* .p or .pas: a Pascal file */
827 if (cp && (streq (cp + 1, "p")
828 || streq (cp + 1, "pas")))
830 PAS_funcs (inf);
831 goto close_and_return;
833 /* If .f or .for, assume it is fortran or nothing. */
834 if (cp && (streq (cp + 1, "f")
835 || streq (cp + 1, "for")))
837 (void) PF_funcs (inf);
838 goto close_and_return;
840 /* if not a .c or .h or .y file, try fortran */
841 if (cp && ((cp[1] != 'c'
842 && cp[1] != 'h'
843 && cp[1] != 'y')
844 || (cp[1] != 0 && cp[2] != 0)))
846 if (PF_funcs (inf) != 0)
847 goto close_and_return;
848 rewind (inf); /* no fortran tags found, try C */
850 C_entries (cplusplus ? C_PLPL : 0);
852 close_and_return:
853 (void) fclose (inf);
854 return TRUE;
857 /* Nonzero if string STR is composed of digits. */
860 string_numeric_p (str)
861 char *str;
863 while (*str)
865 if (*str < '0' || *str > '9')
866 return 0;
868 return 1;
871 /* Record a tag. */
872 /* Should take a TOKEN* instead!! */
873 void
874 pfnote (name, is_func, named, linestart, linelen, lno, cno)
875 char *name; /* tag name */
876 logical is_func; /* function or type name? */
877 logical named; /* tag different from text of definition? */
878 char *linestart;
879 int linelen;
880 int lno;
881 long cno;
883 register char *fp;
884 register NODE *np;
885 char tem[51];
886 char c;
888 np = xnew (1, NODE);
889 if (np == NULL)
891 if (CTAGS)
893 /* It's okay to output early in etags -- it only disrupts the
894 * character count of the tag entries, which is no longer used
895 * by tags.el anyway.
897 error ("too many entries to sort", 0);
899 put_entries (head);
900 free_tree (head);
901 head = NULL;
902 np = xnew (1, NODE);
904 /* If ctags mode, change name "main" to M<thisfilename>. */
905 if (CTAGS && !cxref_style && streq (name, "main"))
907 fp = etags_rindex (curfile, '/');
908 name = concat ("M", fp == 0 ? curfile : fp + 1, "");
909 fp = etags_rindex (name, '.');
910 if (fp && fp[1] != '\0' && fp[2] == '\0')
911 *fp = 0;
912 named = TRUE;
914 np->name = savestr (name);
915 np->file = curfile;
916 np->is_func = is_func;
917 np->named = named;
918 np->lno = lno;
919 /* UNCOMMENT THE +1 HERE: */
920 np->cno = cno /* + 1 */ ; /* our char numbers are 0-base; emacs's are 1-base */
921 np->left = np->right = 0;
922 if (!CTAGS)
924 c = linestart[linelen];
925 linestart[linelen] = 0;
927 else if (cxref_style == 0)
929 sprintf (tem, strlen (linestart) < 50 ? "%s$" : "%.50s", linestart);
930 linestart = tem;
932 np->pat = savestr (linestart);
933 if (!CTAGS)
935 linestart[linelen] = c;
938 add_node (np, &head);
942 * free_tree ()
943 * recurse on left children, iterate on right children.
945 void
946 free_tree (node)
947 register NODE *node;
949 while (node)
951 register NODE *node_right = node->right;
952 free_tree (node->left);
953 free (node->name);
954 free (node->pat);
955 free ((char *) node);
956 node = node_right;
961 * add_node ()
962 * Adds a node to the tree of nodes. In etags mode, we don't keep
963 * it sorted; we just keep a linear list. In ctags mode, maintain
964 * an ordered tree, with no attempt at balancing.
966 * add_node is the only function allowed to add nodes, so it can
967 * maintain state.
969 /* Must avoid static vars within functions since some systems
970 #define static as nothing. */
971 static NODE *last_node = NULL;
973 void
974 add_node (node, cur_node_p)
975 NODE *node, **cur_node_p;
977 register int dif;
978 register NODE *cur_node = *cur_node_p;
980 if (cur_node == NULL)
982 *cur_node_p = node;
983 last_node = node;
984 return;
987 if (!CTAGS)
989 /* Etags Mode */
990 if (last_node == NULL)
991 fatal ("internal error in add_node", 0);
992 last_node->right = node;
993 last_node = node;
995 else
997 /* Ctags Mode */
998 dif = strcmp (node->name, cur_node->name);
1001 * If this tag name matches an existing one, then
1002 * do not add the node, but maybe print a warning.
1004 if (!dif)
1006 if (node->file == cur_node->file)
1008 if (!no_warnings)
1010 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1011 node->file, lineno, node->name);
1012 fprintf (stderr, "Second entry ignored\n");
1014 return;
1016 if (!cur_node->been_warned && !no_warnings)
1018 fprintf (stderr,
1019 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1020 node->file, cur_node->file, node->name);
1022 cur_node->been_warned = TRUE;
1023 return;
1026 /* Maybe refuse to add duplicate nodes. */
1027 if (!permit_duplicates)
1029 if (streq (node->name, cur_node->name)
1030 && streq (node->file, cur_node->file))
1031 return;
1034 /* Actually add the node */
1035 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1039 void
1040 put_entries (node)
1041 register NODE *node;
1043 register char *sp;
1045 if (node == NULL)
1046 return;
1048 /* Output subentries that precede this one */
1049 put_entries (node->left);
1051 /* Output this entry */
1053 if (!CTAGS)
1055 if (node->named)
1057 fprintf (outf, "%s\177%s\001%d,%d\n",
1058 node->pat, node->name,
1059 node->lno, node->cno);
1061 else
1063 fprintf (outf, "%s\177%d,%d\n",
1064 node->pat,
1065 node->lno, node->cno);
1068 else if (!cxref_style)
1070 fprintf (outf, "%s\t%s\t",
1071 node->name, node->file);
1073 if (node->is_func)
1074 { /* a function */
1075 putc (searchar, outf);
1076 putc ('^', outf);
1078 for (sp = node->pat; *sp; sp++)
1080 if (*sp == '\\' || *sp == searchar)
1081 putc ('\\', outf);
1082 putc (*sp, outf);
1084 putc (searchar, outf);
1086 else
1087 { /* a typedef; text pattern inadequate */
1088 fprintf (outf, "%d", node->lno);
1090 putc ('\n', outf);
1092 else if (vgrind_style)
1093 fprintf (stdout, "%s %s %d\n",
1094 node->name, node->file, (node->lno + 63) / 64);
1095 else
1096 fprintf (stdout, "%-16s %3d %-16s %s\n",
1097 node->name, node->lno, node->file, node->pat);
1099 /* Output subentries that follow this one */
1100 put_entries (node->right);
1103 /* Length of a number's decimal representation. */
1105 number_len (num)
1106 long num;
1108 int len = 0;
1109 if (!num)
1110 return 1;
1111 for (; num; num /= 10)
1112 ++len;
1113 return len;
1117 * Return total number of characters that put_entries will output for
1118 * the nodes in the subtree of the specified node. Works only if
1119 * we are not ctags, but called only in that case. This count
1120 * is irrelevant with the new tags.el, but is still supplied for
1121 * backward compatibility.
1124 total_size_of_entries (node)
1125 register NODE *node;
1127 register int total;
1129 if (node == NULL)
1130 return 0;
1132 total = 0;
1133 for (; node; node = node->right)
1135 /* Count left subentries. */
1136 total += total_size_of_entries (node->left);
1138 /* Count this entry */
1139 total += strlen (node->pat) + 1;
1140 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1141 if (node->named)
1142 total += 1 + strlen (node->name); /* \001name */
1145 return total;
1149 * The C symbol tables.
1152 /* Feed stuff between (but not including) %[ and %] lines to:
1153 gperf -c -k1,3 -o -p -r -t
1155 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1157 class, C_PLPL, st_C_struct
1158 domain, C_STAR, st_C_struct
1159 union, 0, st_C_struct
1160 struct, 0, st_C_struct
1161 enum, 0, st_C_enum
1162 typedef, 0, st_C_typedef
1163 define, 0, st_C_define
1164 long, 0, st_C_typespec
1165 short, 0, st_C_typespec
1166 int, 0, st_C_typespec
1167 char, 0, st_C_typespec
1168 float, 0, st_C_typespec
1169 double, 0, st_C_typespec
1170 signed, 0, st_C_typespec
1171 unsigned, 0, st_C_typespec
1172 auto, 0, st_C_typespec
1173 void, 0, st_C_typespec
1174 extern, 0, st_C_typespec
1175 static, 0, st_C_typespec
1176 const, 0, st_C_typespec
1177 volatile, 0, st_C_typespec
1179 and replace lines between %< and %> with its output. */
1180 /*%<*/
1181 /* C code produced by gperf version 1.8.1 (K&R C version) */
1182 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1185 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
1187 #define MIN_WORD_LENGTH 3
1188 #define MAX_WORD_LENGTH 8
1189 #define MIN_HASH_VALUE 10
1190 #define MAX_HASH_VALUE 62
1192 21 keywords
1193 53 is the maximum key range
1196 static int
1197 hash (str, len)
1198 register char *str;
1199 register int len;
1201 static unsigned char hash_table[] =
1203 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1204 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1205 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1206 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1207 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1208 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1209 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1210 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1211 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1212 62, 62, 62, 62, 62, 62, 62, 2, 62, 7,
1213 6, 9, 15, 30, 62, 24, 62, 62, 1, 24,
1214 7, 27, 13, 62, 19, 26, 18, 27, 1, 62,
1215 62, 62, 62, 62, 62, 62, 62, 62,
1217 return len + hash_table[str[2]] + hash_table[str[0]];
1220 struct C_stab_entry *
1221 in_word_set (str, len)
1222 register char *str;
1223 register int len;
1226 static struct C_stab_entry wordlist[] =
1228 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1229 {"",},
1230 {"volatile", 0, st_C_typespec},
1231 {"",},
1232 {"long", 0, st_C_typespec},
1233 {"char", 0, st_C_typespec},
1234 {"class", C_PLPL, st_C_struct},
1235 {"",}, {"",}, {"",}, {"",},
1236 {"const", 0, st_C_typespec},
1237 {"",}, {"",}, {"",}, {"",},
1238 {"auto", 0, st_C_typespec},
1239 {"",}, {"",},
1240 {"define", 0, st_C_define},
1241 {"",},
1242 {"void", 0, st_C_typespec},
1243 {"",}, {"",}, {"",},
1244 {"extern", 0, st_C_typespec},
1245 {"static", 0, st_C_typespec},
1246 {"",},
1247 {"domain", C_STAR, st_C_struct},
1248 {"",},
1249 {"typedef", 0, st_C_typedef},
1250 {"double", 0, st_C_typespec},
1251 {"enum", 0, st_C_enum},
1252 {"",}, {"",}, {"",}, {"",},
1253 {"int", 0, st_C_typespec},
1254 {"",},
1255 {"float", 0, st_C_typespec},
1256 {"",}, {"",}, {"",},
1257 {"struct", 0, st_C_struct},
1258 {"",}, {"",}, {"",}, {"",},
1259 {"union", 0, st_C_struct},
1260 {"",},
1261 {"short", 0, st_C_typespec},
1262 {"",}, {"",},
1263 {"unsigned", 0, st_C_typespec},
1264 {"signed", 0, st_C_typespec},
1267 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
1269 register int key = hash (str, len);
1271 if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
1273 register char *s = wordlist[key].name;
1275 if (*s == *str && strneq (str + 1, s + 1, len - 1))
1276 return &wordlist[key];
1279 return 0;
1281 /*%>*/
1283 enum sym_type
1284 C_symtype(str, len, c_ext)
1285 char *str;
1286 int len;
1287 int c_ext;
1289 register struct C_stab_entry *se = in_word_set(str, len);
1291 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
1292 return st_none;
1293 return se->type;
1297 * C functions are recognized using a simple finite automaton.
1298 * funcdef is its state variable.
1300 typedef enum
1302 fnone, /* nothing seen */
1303 ftagseen, /* function-like tag seen */
1304 fstartlist, /* just after open parenthesis */
1305 finlist, /* in parameter list */
1306 flistseen, /* after parameter list */
1307 fignore /* before open brace */
1308 } FUNCST;
1309 FUNCST funcdef;
1313 * typedefs are recognized using a simple finite automaton.
1314 * typeddef is its state variable.
1316 typedef enum
1318 tnone, /* nothing seen */
1319 ttypedseen, /* typedef keyword seen */
1320 tinbody, /* inside typedef body */
1321 tend, /* just before typedef tag */
1322 tignore /* junk after typedef tag */
1323 } TYPEDST;
1324 TYPEDST typdef;
1328 * struct-like structures (enum, struct and union) are recognized
1329 * using another simple finite automaton. `structdef' is its state
1330 * variable.
1332 typedef enum
1334 snone, /* nothing seen yet */
1335 skeyseen, /* struct-like keyword seen */
1336 stagseen, /* struct-like tag seen */
1337 scolonseen, /* colon seen after struct-like tag */
1338 sinbody /* in struct body: recognize member func defs*/
1339 } STRUCTST;
1340 STRUCTST structdef;
1343 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1344 * struct tag, and structtype is the type of the preceding struct-like
1345 * keyword.
1347 char structtag[BUFSIZ];
1348 enum sym_type structtype;
1351 * Yet another little state machine to deal with preprocessor lines.
1353 typedef enum
1355 dnone, /* nothing seen */
1356 dsharpseen, /* '#' seen as first char on line */
1357 ddefineseen, /* '#' and 'define' seen */
1358 dignorerest /* ignore rest of line */
1359 } DEFINEST;
1360 DEFINEST definedef;
1363 * Set this to TRUE, and the next token considered is called a function.
1364 * Used only for GNUmacs's function-defining macros.
1366 logical next_token_is_func;
1369 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1371 logical yacc_rules;
1374 * C_entries ()
1375 * This routine finds functions, typedefs, #define's and
1376 * struct/union/enum definitions in C syntax and adds them
1377 * to the list.
1380 #define curlb (lbs[curndx].lb)
1381 #define othlb (lbs[1-curndx].lb)
1382 #define newlb (lbs[newndx].lb)
1383 #define curlinepos (lbs[curndx].linepos)
1384 #define othlinepos (lbs[1-curndx].linepos)
1385 #define newlinepos (lbs[newndx].linepos)
1387 /* Save and restore token state. This is used when preprocessor defines
1388 are handled, to avoid disturbing active function/typedef/struct states. */
1389 #define TOKEN_SAVED_P (savetok.lineno > 0)
1390 #define SAVE_TOKEN (savetok = tok, savetok.p = (char *) tokoff, \
1391 savetok.len = toklen, strcpy(savenameb, nameb))
1392 #define RESTORE_TOKEN (tok = savetok, tokoff = (int) tok.p, \
1393 toklen = tok.len, strcpy(nameb, savenameb), \
1394 savetok.lineno = 0)
1396 #define CNL_SAVE_DEFINEDEF \
1397 do { \
1398 SET_FILEPOS (curlinepos, inf, charno); \
1399 lineno++; \
1400 charno += readline (&curlb, inf); \
1401 lp = curlb.buffer; \
1402 quotednl = FALSE; \
1403 newndx = curndx; \
1404 } while (FALSE)
1406 #define CNL \
1407 do { \
1408 CNL_SAVE_DEFINEDEF; \
1409 if (TOKEN_SAVED_P) \
1410 RESTORE_TOKEN; \
1411 definedef = dnone; \
1412 } while (FALSE)
1414 #define MAKE_TAG_FROM_NEW_LB(isfun) pfnote (nameb, isfun, tok.named, \
1415 newlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (newlinepos))
1416 #define MAKE_TAG_FROM_OTH_LB(isfun) pfnote (nameb, isfun, tok.named, \
1417 othlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (othlinepos))
1419 void
1420 C_entries (c_ext)
1421 int c_ext; /* extension of C? */
1423 register char c; /* latest char read; '\0' for end of line */
1424 register char *lp; /* pointer one beyond the character `c' */
1425 int curndx, newndx; /* indices for current and new lb */
1426 TOKEN tok; /* latest token read for funcdef & structdef */
1427 char nameb[BUFSIZ]; /* latest token name for funcdef & structdef */
1428 register int tokoff; /* offset in line of start of latest token */
1429 register int toklen; /* length of latest token */
1430 int cblev; /* current curly brace level */
1431 int parlev; /* current parenthesis level */
1432 logical incomm, inquote, inchar, quotednl, midtoken;
1433 logical cplpl;
1434 TOKEN savetok; /* saved token during preprocessor handling */
1435 char savenameb[BUFSIZ]; /* ouch! */
1437 savetok.lineno = 0;
1438 curndx = newndx = 0;
1439 lineno = 0;
1440 charno = 0;
1441 lp = curlb.buffer;
1442 *lp = 0;
1444 definedef = dnone; funcdef = fnone; typdef = tnone; structdef = snone;
1445 next_token_is_func = yacc_rules = FALSE;
1446 midtoken = inquote = inchar = incomm = quotednl = FALSE;
1447 cblev = 0;
1448 parlev = 0;
1449 cplpl = c_ext & C_PLPL;
1451 while (!feof (inf))
1453 c = *lp++;
1454 if (c == '\\')
1456 /* If we're at the end of the line, the next character is a
1457 '\0'; don't skip it, because it's the thing that tells us
1458 to read the next line. */
1459 if (*lp == '\0')
1461 quotednl = TRUE;
1462 continue;
1464 lp++;
1465 c = ' ';
1467 else if (incomm)
1469 switch (c)
1471 case '*':
1472 if (*lp == '/')
1474 c = *lp++;
1475 incomm = FALSE;
1477 break;
1478 case '\0':
1479 /* Newlines inside comments do not end macro definitions in
1480 traditional cpp. */
1481 CNL_SAVE_DEFINEDEF;
1482 break;
1484 continue;
1486 else if (inquote)
1488 switch (c)
1490 case '"':
1491 inquote = FALSE;
1492 break;
1493 case '\0':
1494 /* Newlines inside strings do not end macro definitions
1495 in traditional cpp, even though compilers don't
1496 usually accept them. */
1497 CNL_SAVE_DEFINEDEF;
1498 break;
1500 continue;
1502 else if (inchar)
1504 switch (c)
1506 case '\0':
1507 /* Hmmm, something went wrong. */
1508 CNL;
1509 /* FALLTHRU */
1510 case '\'':
1511 inchar = FALSE;
1512 break;
1514 continue;
1516 else
1517 switch (c)
1519 case '"':
1520 inquote = TRUE;
1521 if (funcdef != finlist && funcdef != fignore)
1522 funcdef = fnone;
1523 continue;
1524 case '\'':
1525 inchar = TRUE;
1526 if (funcdef != finlist && funcdef != fignore)
1527 funcdef = fnone;
1528 continue;
1529 case '/':
1530 if (*lp == '*')
1532 lp++;
1533 incomm = TRUE;
1534 continue;
1536 else if (cplpl && *lp == '/')
1538 c = 0;
1539 break;
1541 else
1542 break;
1543 case '%':
1544 if ((c_ext & YACC) && *lp == '%')
1546 /* entering or exiting rules section in yacc file */
1547 lp++;
1548 definedef = dnone; funcdef = fnone;
1549 typdef = tnone; structdef = snone;
1550 next_token_is_func = FALSE;
1551 midtoken = inquote = inchar = incomm = quotednl = FALSE;
1552 cblev = 0;
1553 yacc_rules = !yacc_rules;
1554 continue;
1556 else
1557 break;
1558 case '#':
1559 if (lp == newlb.buffer + 1 && definedef == dnone)
1560 definedef = dsharpseen;
1561 continue;
1562 } /* switch (c) */
1565 /* Consider token only if some complicated conditions are satisfied. */
1566 if (((cblev == 0 && structdef != scolonseen)
1567 || (cblev == 1 && cplpl && structdef == sinbody))
1568 && typdef != tignore
1569 && definedef != dignorerest
1570 && (funcdef != finlist
1571 || definedef != dnone))
1573 if (midtoken)
1575 if (endtoken (c))
1577 if (cplpl && c == ':' && *lp == ':' && begtoken(*(lp + 1)))
1580 * This handles :: in the middle, but not at beginning
1581 * of an identifier.
1583 lp += 2;
1584 toklen += 3;
1586 else
1588 logical is_func = FALSE;
1590 tok.lineno = lineno;
1591 tok.p = newlb.buffer + tokoff;
1592 tok.len = toklen;
1593 tok.named = FALSE;
1594 if (yacc_rules
1595 || consider_token (c, &tok, c_ext, cblev, &is_func))
1597 if (structdef == sinbody
1598 && definedef == dnone
1599 && is_func)
1600 /* function defined in C++ class body */
1602 tok.named = TRUE;
1603 sprintf (nameb, "%s::%.*s",
1604 ((structtag[0] == '\0')
1605 ? "_anonymous_" : structtag),
1606 tok.len, tok.p);
1608 else
1610 sprintf (nameb, "%.*s", tok.len, tok.p);
1613 if (structdef == stagseen
1614 || typdef == tend)
1615 tok.named = TRUE;
1617 if (definedef == dnone
1618 && (funcdef == ftagseen
1619 || structdef == stagseen
1620 || typdef == tend))
1622 if (newndx == curndx)
1623 curndx = 1 - curndx; /* switch line buffers */
1625 else
1626 MAKE_TAG_FROM_NEW_LB (is_func);
1628 midtoken = FALSE;
1630 } /* if (endtoken (c)) */
1631 else if (intoken (c))
1633 toklen++;
1634 continue;
1636 } /* if (midtoken) */
1637 else if (begtoken (c))
1639 switch (definedef)
1641 case dnone:
1642 switch (funcdef)
1644 case fstartlist:
1645 funcdef = finlist;
1646 continue;
1647 case flistseen:
1648 MAKE_TAG_FROM_OTH_LB (TRUE);
1649 funcdef = fignore;
1650 break;
1651 case ftagseen:
1652 funcdef = fnone;
1653 break;
1655 if (structdef == stagseen)
1656 structdef = snone;
1657 break;
1658 case dsharpseen:
1659 /* Take a quick peek ahead for define directive,
1660 so we can avoid saving the token when not absolutely
1661 necessary. [This is a speed hack.] */
1662 if (c == 'd' && strneq(lp, "efine", 5)
1663 && iswhite(*(lp + 5)))
1665 SAVE_TOKEN;
1666 definedef = ddefineseen;
1667 lp += 6;
1669 else
1670 definedef = dignorerest;
1671 continue;
1673 if (!yacc_rules || lp == newlb.buffer + 1)
1675 tokoff = lp - 1 - newlb.buffer;
1676 toklen = 1;
1677 midtoken = TRUE;
1679 continue;
1681 } /* if must look at token */
1684 /* Detect end of line, colon, comma, semicolon and various braces
1685 after having handled a token.*/
1686 switch (c)
1688 case ':':
1689 if (definedef != dnone)
1690 break;
1691 if (structdef == stagseen)
1692 structdef = scolonseen;
1693 else
1694 switch (funcdef)
1696 case ftagseen:
1697 if (yacc_rules)
1699 MAKE_TAG_FROM_OTH_LB (FALSE);
1700 funcdef = fignore;
1702 break;
1703 case fstartlist:
1704 funcdef = fnone;
1705 break;
1707 break;
1708 case ';':
1709 if (definedef != dnone)
1710 break;
1711 if (cblev == 0)
1712 switch (typdef)
1714 case tend:
1715 MAKE_TAG_FROM_OTH_LB (FALSE);
1716 /* FALLTHRU */
1717 default:
1718 typdef = tnone;
1720 if (funcdef != fignore)
1721 funcdef = fnone;
1722 if (structdef == stagseen)
1723 structdef = snone;
1724 break;
1725 case ',':
1726 if (definedef != dnone)
1727 break;
1728 if (funcdef != finlist && funcdef != fignore)
1729 funcdef = fnone;
1730 if (structdef == stagseen)
1731 structdef = snone;
1732 break;
1733 case '[':
1734 if (definedef != dnone)
1735 break;
1736 if (cblev == 0 && typdef == tend)
1738 typdef = tignore;
1739 MAKE_TAG_FROM_OTH_LB (FALSE);
1740 break;
1742 if (funcdef != finlist && funcdef != fignore)
1743 funcdef = fnone;
1744 if (structdef == stagseen)
1745 structdef = snone;
1746 break;
1747 case '(':
1748 if (definedef != dnone)
1749 break;
1750 switch (funcdef)
1752 case ftagseen:
1753 funcdef = fstartlist;
1754 break;
1755 case flistseen:
1756 funcdef = finlist;
1757 break;
1759 parlev++;
1760 break;
1761 case ')':
1762 if (definedef != dnone)
1763 break;
1764 if (--parlev == 0)
1766 switch (funcdef)
1768 case fstartlist:
1769 case finlist:
1770 funcdef = flistseen;
1771 break;
1773 if (cblev == 0 && typdef == tend)
1775 typdef = tignore;
1776 MAKE_TAG_FROM_OTH_LB (FALSE);
1779 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
1780 parlev = 0;
1781 break;
1782 case '{':
1783 if (definedef != dnone)
1784 break;
1785 if (typdef == ttypedseen)
1786 typdef = tinbody;
1787 switch (structdef)
1789 case skeyseen: /* unnamed struct */
1790 structtag[0] = '\0';
1791 structdef = sinbody;
1792 break;
1793 case stagseen:
1794 case scolonseen: /* named struct */
1795 structdef = sinbody;
1796 MAKE_TAG_FROM_OTH_LB (FALSE);
1797 break;
1799 switch (funcdef)
1801 case flistseen:
1802 MAKE_TAG_FROM_OTH_LB (TRUE);
1803 /* FALLTHRU */
1804 case fignore:
1805 funcdef = fnone;
1806 break;
1807 case fnone:
1808 /* Neutralize `extern "C" {' grot.
1809 if (cblev == 0 && structdef == snone && typdef == tnone)
1810 cblev--; */;
1812 cblev++;
1813 break;
1814 case '*':
1815 if (definedef != dnone)
1816 break;
1817 if (funcdef == fstartlist)
1818 funcdef = fnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
1819 break;
1820 case '}':
1821 if (definedef != dnone)
1822 break;
1823 if (!noindentypedefs && lp == newlb.buffer + 1)
1825 cblev = 0; /* reset curly brace level if first column */
1826 parlev = 0; /* also reset paren level, just in case... */
1828 else if (cblev > 0)
1829 cblev--;
1830 if (cblev == 0)
1832 if (typdef == tinbody)
1833 typdef = tend;
1834 structdef = snone;
1835 strcpy (structtag, "<error 2>");
1837 break;
1838 case '=':
1839 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
1840 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
1841 if (definedef != dnone)
1842 break;
1843 /* These surely cannot follow a function tag. */
1844 if (funcdef != finlist && funcdef != fignore)
1845 funcdef = fnone;
1846 break;
1847 case '\0':
1848 /* If a macro spans multiple lines don't reset its state. */
1849 if (quotednl)
1850 CNL_SAVE_DEFINEDEF;
1851 else
1852 CNL;
1853 break;
1854 } /* switch (c) */
1856 } /* while not eof */
1860 * consider_token ()
1861 * checks to see if the current token is at the start of a
1862 * function, or corresponds to a typedef, or is a struct/union/enum
1863 * tag.
1865 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1866 * C_EXT is which language we are looking at.
1868 * In the future we will need some way to adjust where the end of
1869 * the token is; for instance, implementing the C++ keyword
1870 * `operator' properly will adjust the end of the token to be after
1871 * whatever follows `operator'.
1873 * Globals
1874 * funcdef IN OUT
1875 * structdef IN OUT
1876 * definedef IN OUT
1877 * typdef IN OUT
1878 * next_token_is_func IN OUT
1881 logical
1882 consider_token (c, tokp, c_ext, cblev, is_func)
1883 register char c; /* IN: first char after the token */
1884 register TOKEN *tokp; /* IN: token pointer */
1885 int c_ext; /* IN: C extensions mask */
1886 int cblev; /* IN: curly brace level */
1887 logical *is_func; /* OUT */
1889 enum sym_type toktype = C_symtype(tokp->p, tokp->len, c_ext);
1892 * Advance the definedef state machine.
1894 switch (definedef)
1896 case dnone:
1897 /* We're not on a preprocessor line. */
1898 break;
1899 case dsharpseen:
1900 if (toktype == st_C_define)
1902 definedef = ddefineseen;
1904 else
1906 definedef = dignorerest;
1908 return (FALSE);
1909 case ddefineseen:
1911 * Make a tag for any macro.
1913 definedef = dignorerest;
1914 *is_func = (c == '(');
1915 if (!*is_func && !constantypedefs)
1916 return (FALSE);
1917 else
1918 return (TRUE);
1919 case dignorerest:
1920 return (FALSE);
1921 default:
1922 error ("internal error: definedef value.", 0);
1926 * Now typedefs
1928 switch (typdef)
1930 case tnone:
1931 if (toktype == st_C_typedef)
1933 if (typedefs)
1934 typdef = ttypedseen;
1935 funcdef = fnone;
1936 return (FALSE);
1938 break;
1939 case ttypedseen:
1940 switch (toktype)
1942 case st_none:
1943 case st_C_typespec:
1944 typdef = tend;
1945 break;
1946 case st_C_struct:
1947 case st_C_enum:
1948 break;
1950 /* Do not return here, so the structdef stuff has a chance. */
1951 break;
1952 case tend:
1953 switch (toktype)
1955 case st_C_typespec:
1956 case st_C_struct:
1957 case st_C_enum:
1958 return (FALSE);
1960 return (TRUE);
1964 * This structdef business is currently only invoked when cblev==0.
1965 * It should be recursively invoked whatever the curly brace level,
1966 * and a stack of states kept, to allow for definitions of structs
1967 * within structs.
1969 * This structdef business is NOT invoked when we are ctags and the
1970 * file is plain C. This is because a struct tag may have the same
1971 * name as another tag, and this loses with ctags.
1973 * This if statement deals with the typdef state machine as
1974 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1975 * return (FALSE). All the other code here is for the structdef
1976 * state machine.
1978 switch (toktype)
1980 case st_C_struct:
1981 case st_C_enum:
1982 if (typdef == ttypedseen
1983 || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
1985 structdef = skeyseen;
1986 structtype = toktype;
1988 return (FALSE);
1990 if (structdef == skeyseen)
1992 if (structtype == st_C_struct)
1994 strncpy (structtag, tokp->p, tokp->len);
1995 structtag[tokp->len] = '\0'; /* for struct/union/class */
1997 else
1999 structtag[0] = '\0'; /* for enum (why is it treated differently?) */
2001 structdef = stagseen;
2002 return (TRUE);
2005 /* Avoid entering funcdef stuff if typdef is going on. */
2006 if (typdef != tnone)
2008 definedef = dnone;
2009 return (FALSE);
2012 /* Detect GNUmacs's function-defining macros. */
2013 if (definedef == dnone)
2015 if (strneq (tokp->p, "DEF", 3)
2016 || strneq (tokp->p, "ENTRY", 5)
2017 || strneq (tokp->p, "SYSCALL", 7)
2018 || strneq (tokp->p, "PSEUDO", 6))
2020 next_token_is_func = TRUE;
2021 return (FALSE);
2023 if (strneq (tokp->p, "EXFUN", 5))
2025 next_token_is_func = FALSE;
2026 return (FALSE);
2029 if (next_token_is_func)
2031 next_token_is_func = FALSE;
2032 funcdef = fnone;
2033 *is_func = TRUE; /* to force search string in ctags */
2034 return (TRUE);
2037 /* A function? */
2038 switch (toktype)
2040 case st_C_typespec:
2041 if (funcdef != finlist && funcdef != fignore)
2042 funcdef = fnone; /* should be useless */
2043 return (FALSE);
2044 default:
2045 if (funcdef == fnone)
2047 funcdef = ftagseen;
2048 *is_func = TRUE;
2049 return (TRUE);
2053 return (FALSE);
2056 /* Fortran parsing */
2058 char *dbp;
2059 int pfcnt;
2062 PF_funcs (fi)
2063 FILE *fi;
2065 lineno = 0;
2066 charno = 0;
2067 pfcnt = 0;
2069 while (!feof (fi))
2071 lineno++;
2072 linecharno = charno;
2073 charno += readline (&lb, fi);
2074 dbp = lb.buffer;
2075 if (*dbp == '%')
2076 dbp++; /* Ratfor escape to fortran */
2077 while (isspace (*dbp))
2078 dbp++;
2079 if (*dbp == 0)
2080 continue;
2081 switch (*dbp | ' ')
2083 case 'i':
2084 if (tail ("integer"))
2085 takeprec ();
2086 break;
2087 case 'r':
2088 if (tail ("real"))
2089 takeprec ();
2090 break;
2091 case 'l':
2092 if (tail ("logical"))
2093 takeprec ();
2094 break;
2095 case 'c':
2096 if (tail ("complex") || tail ("character"))
2097 takeprec ();
2098 break;
2099 case 'd':
2100 if (tail ("double"))
2102 while (isspace (*dbp))
2103 dbp++;
2104 if (*dbp == 0)
2105 continue;
2106 if (tail ("precision"))
2107 break;
2108 continue;
2110 break;
2112 while (isspace (*dbp))
2113 dbp++;
2114 if (*dbp == 0)
2115 continue;
2116 switch (*dbp | ' ')
2118 case 'f':
2119 if (tail ("function"))
2120 getit (fi);
2121 continue;
2122 case 's':
2123 if (tail ("subroutine"))
2124 getit (fi);
2125 continue;
2126 case 'e':
2127 if (tail ("entry"))
2128 getit (fi);
2129 continue;
2130 case 'p':
2131 if (tail ("program"))
2133 getit (fi);
2134 continue;
2136 if (tail ("procedure"))
2137 getit (fi);
2138 continue;
2141 return (pfcnt);
2144 logical
2145 tail (cp)
2146 char *cp;
2148 register int len = 0;
2150 while (*cp && (*cp | ' ') == (dbp[len] | ' '))
2151 cp++, len++;
2152 if (*cp == 0)
2154 dbp += len;
2155 return (TRUE);
2157 return (FALSE);
2160 void
2161 takeprec ()
2163 while (isspace (*dbp))
2164 dbp++;
2165 if (*dbp != '*')
2166 return;
2167 dbp++;
2168 while (isspace (*dbp))
2169 dbp++;
2170 if (!isdigit (*dbp))
2172 --dbp; /* force failure */
2173 return;
2176 dbp++;
2177 while (isdigit (*dbp));
2180 void
2181 getit (fi)
2182 FILE *fi;
2184 register char *cp;
2185 char c;
2186 char nambuf[BUFSIZ];
2188 while (isspace (*dbp))
2189 dbp++;
2190 if (*dbp == '\0')
2192 lineno++;
2193 linecharno = charno;
2194 charno += readline (&lb, fi);
2195 dbp = lb.buffer;
2196 if (dbp[5] != '&')
2197 return;
2198 dbp += 6;
2199 while (isspace (*dbp))
2200 dbp++;
2202 if (!isalpha (*dbp)
2203 && *dbp != '_'
2204 && *dbp != '$')
2205 return;
2206 for (cp = dbp + 1;
2207 (*cp
2208 && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
2209 cp++)
2210 continue;
2211 c = *cp;
2212 *cp = '\0';
2213 strcpy (nambuf, dbp);
2214 *cp = c;
2215 pfnote (nambuf, TRUE, FALSE, lb.buffer,
2216 cp - lb.buffer + 1, lineno, linecharno);
2217 pfcnt++;
2220 /* Handle a file of assembler code. */
2222 void
2223 Asm_funcs (fi)
2224 FILE *fi;
2226 int i;
2227 register char c;
2229 lineno = 0;
2230 charno = 0;
2231 pfcnt = 0;
2233 while (!feof (fi))
2235 lineno++;
2236 linecharno = charno;
2237 charno += readline (&lb, fi);
2238 dbp = lb.buffer;
2240 for (i = 0; ((c = dbp[i]) && !isspace (c)) && (c != ':'); i++)
2243 if ((i > 0) && (c == ':'))
2244 getit (fi);
2248 /* Added by Mosur Mohan, 4/22/88 */
2249 /* Pascal parsing */
2251 #define GET_NEW_LINE \
2253 linecharno = charno; lineno++; \
2254 charno += 1 + readline (&lb, inf); \
2255 dbp = lb.buffer; \
2258 /* Locates tags for procedures & functions.
2259 * Doesn't do any type- or var-definitions.
2260 * It does look for the keyword "extern" or "forward"
2261 * immediately following the procedure statement;
2262 * if found, the tag is skipped.
2265 void
2266 PAS_funcs (fi)
2267 FILE *fi;
2269 struct linebuffer tline; /* mostly copied from C_entries */
2270 long save_lcno;
2271 int save_lineno;
2272 char c, *cp;
2273 char nambuf[BUFSIZ];
2275 logical /* each of these flags is TRUE iff: */
2276 incomm1, /* point is inside {..} comment */
2277 incomm2, /* point is inside (*..*) comment */
2278 inquote, /* point is inside '..' string */
2279 get_tagname, /* point is after PROCEDURE/FUNCTION */
2280 /* keyword, so next item = potential tag */
2281 found_tag, /* point is after a potential tag */
2282 inparms, /* point is within parameter-list */
2283 verify_tag; /* point has passed the parm-list, so the */
2284 /* next token will determine whether */
2285 /* this is a FORWARD/EXTERN to be */
2286 /* ignored, or whether it is a real tag */
2288 lineno = 0;
2289 charno = 0;
2290 dbp = lb.buffer;
2291 *dbp = 0;
2292 initbuffer (&tline);
2294 incomm1 = incomm2 = inquote = FALSE;
2295 found_tag = FALSE; /* have a proc name; check if extern */
2296 get_tagname = FALSE; /* have found "procedure" keyword */
2297 inparms = FALSE; /* found '(' after "proc" */
2298 verify_tag = FALSE; /* check if "extern" is ahead */
2300 /* long main loop to get next char */
2301 while (!feof (fi))
2303 c = *dbp++;
2304 if (c == 0) /* if end of line */
2306 GET_NEW_LINE;
2307 if (*dbp == 0)
2308 continue;
2309 if (!((found_tag && verify_tag) ||
2310 get_tagname))
2311 c = *dbp++; /* only if don't need *dbp pointing */
2312 /* to the beginning of the name of */
2313 /* the procedure or function */
2315 if (incomm1) /* within { - } comments */
2317 if (c == '}')
2318 incomm1 = FALSE;
2319 continue;
2321 else if (incomm2) /* within (* - *) comments */
2323 if (c == '*')
2325 while ((c = *dbp++) == '*')
2326 continue;
2327 if (c == 0)
2328 GET_NEW_LINE;
2329 if (c == ')')
2330 incomm2 = FALSE;
2332 continue;
2334 else if (inquote)
2336 if (c == '\'')
2337 inquote = FALSE;
2338 continue;
2340 else
2341 switch (c)
2343 case '\'':
2344 inquote = TRUE; /* found first quote */
2345 continue;
2346 case '{': /* found open-{-comment */
2347 incomm1 = TRUE;
2348 continue;
2349 case '(':
2350 if (*dbp == '*') /* found open-(*-comment */
2352 incomm2 = TRUE;
2353 dbp++;
2355 else if (found_tag) /* found '(' after tag, i.e., parm-list */
2356 inparms = TRUE;
2357 continue;
2358 case ')': /* end of parms list */
2359 if (inparms)
2360 inparms = FALSE;
2361 continue;
2362 case ';':
2363 if ((found_tag) && (!inparms)) /* end of proc or fn stmt */
2365 verify_tag = TRUE;
2366 break;
2368 continue;
2370 if ((found_tag) && (verify_tag) && (*dbp != ' '))
2372 /* check if this is an "extern" declaration */
2373 if (*dbp == 0)
2374 continue;
2375 if ((*dbp == 'e') || (*dbp == 'E'))
2377 if (tail ("extern")) /* superfluous, really! */
2379 found_tag = FALSE;
2380 verify_tag = FALSE;
2383 else if ((*dbp == 'f') || (*dbp == 'F'))
2385 if (tail ("forward")) /* check for forward reference */
2387 found_tag = FALSE;
2388 verify_tag = FALSE;
2391 if ((found_tag) && (verify_tag)) /* not external proc, so make tag */
2393 found_tag = FALSE;
2394 verify_tag = FALSE;
2395 pfnote (nambuf, TRUE, FALSE,
2396 tline.buffer, cp - tline.buffer + 1,
2397 save_lineno, save_lcno);
2398 continue;
2401 if (get_tagname) /* grab name of proc or fn */
2403 if (*dbp == 0)
2404 continue;
2406 /* save all values for later tagging */
2407 tline.size = lb.size;
2408 strcpy (tline.buffer, lb.buffer);
2409 save_lineno = lineno;
2410 save_lcno = linecharno;
2412 /* grab block name */
2413 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
2414 continue;
2415 c = cp[0];
2416 cp[0] = 0;
2417 strcpy (nambuf, dbp);
2418 cp[0] = c;
2419 dbp = cp; /* restore dbp to e-o-token */
2420 get_tagname = FALSE;
2421 found_tag = TRUE;
2422 continue;
2424 /* and proceed to check for "extern" */
2426 if ((!incomm1) && (!incomm2) && (!inquote) &&
2427 (!found_tag) && (!get_tagname))
2429 /* check for proc/fn keywords */
2430 switch (c | ' ')
2432 case 'p':
2433 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2434 get_tagname = TRUE;
2435 continue;
2436 case 'f':
2437 if (tail ("unction"))
2438 get_tagname = TRUE;
2439 continue;
2442 } /* while not e-o-f */
2446 * lisp tag functions
2447 * just look for (def or (DEF
2450 void
2451 L_funcs (fi)
2452 FILE *fi;
2454 lineno = 0;
2455 charno = 0;
2456 pfcnt = 0;
2458 while (!feof (fi))
2460 lineno++;
2461 linecharno = charno;
2462 charno += readline (&lb, fi);
2463 dbp = lb.buffer;
2464 if (dbp[0] == '(')
2466 if (L_isdef (dbp))
2468 while (!isspace (*dbp))
2469 dbp++;
2470 while (isspace (*dbp))
2471 dbp++;
2472 L_getit ();
2474 else
2476 /* Check for (foo::defmumble name-defined ... */
2478 dbp++;
2479 while (*dbp && !isspace (*dbp)
2480 && *dbp != ':' && *dbp != '(' && *dbp != ')');
2481 if (*dbp == ':')
2484 dbp++;
2485 while (*dbp == ':');
2487 if (L_isdef (dbp - 1))
2489 while (!isspace (*dbp))
2490 dbp++;
2491 while (isspace (*dbp))
2492 dbp++;
2493 L_getit ();
2502 L_isdef (dbp)
2503 register char *dbp;
2505 return ((dbp[1] == 'd' || dbp[1] == 'D')
2506 && (dbp[2] == 'e' || dbp[2] == 'E')
2507 && (dbp[3] == 'f' || dbp[3] == 'F'));
2511 L_isquote (dbp)
2512 register char *dbp;
2514 return ((*(++dbp) == 'q' || *dbp == 'Q')
2515 && (*(++dbp) == 'u' || *dbp == 'U')
2516 && (*(++dbp) == 'o' || *dbp == 'O')
2517 && (*(++dbp) == 't' || *dbp == 'T')
2518 && (*(++dbp) == 'e' || *dbp == 'E')
2519 && isspace(*(++dbp)));
2522 void
2523 L_getit ()
2525 register char *cp;
2526 char c;
2527 char nambuf[BUFSIZ];
2529 if (*dbp == '\'') /* Skip prefix quote */
2530 dbp++;
2531 else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
2533 dbp += 7;
2534 while (isspace(*dbp))
2535 dbp++;
2537 for (cp = dbp /*+1*/; *cp && *cp != '(' && *cp != ' ' && *cp != ')'; cp++)
2538 continue;
2539 if (cp == dbp)
2540 return;
2542 c = cp[0];
2543 cp[0] = 0;
2544 strcpy (nambuf, dbp);
2545 cp[0] = c;
2546 pfnote (nambuf, TRUE, FALSE, lb.buffer,
2547 cp - lb.buffer + 1, lineno, linecharno);
2548 pfcnt++;
2552 * Scheme tag functions
2553 * look for (def... xyzzy
2554 * look for (def... (xyzzy
2555 * look for (def ... ((...(xyzzy ....
2556 * look for (set! xyzzy
2559 static void get_scheme ();
2561 void
2562 Scheme_funcs (fi)
2563 FILE *fi;
2565 lineno = 0;
2566 charno = 0;
2567 pfcnt = 0;
2569 while (!feof (fi))
2571 lineno++;
2572 linecharno = charno;
2573 charno += readline (&lb, fi);
2574 dbp = lb.buffer;
2575 if (dbp[0] == '(' &&
2576 (dbp[1] == 'D' || dbp[1] == 'd') &&
2577 (dbp[2] == 'E' || dbp[2] == 'e') &&
2578 (dbp[3] == 'F' || dbp[3] == 'f'))
2580 while (!isspace (*dbp))
2581 dbp++;
2582 /* Skip over open parens and white space */
2583 while (*dbp && (isspace (*dbp) || *dbp == '('))
2584 dbp++;
2585 get_scheme ();
2587 if (dbp[0] == '(' &&
2588 (dbp[1] == 'S' || dbp[1] == 's') &&
2589 (dbp[2] == 'E' || dbp[2] == 'e') &&
2590 (dbp[3] == 'T' || dbp[3] == 't') &&
2591 (dbp[4] == '!' || dbp[4] == '!') &&
2592 (isspace (dbp[5])))
2594 while (!isspace (*dbp))
2595 dbp++;
2596 /* Skip over white space */
2597 while (isspace (*dbp))
2598 dbp++;
2599 get_scheme ();
2604 static void
2605 get_scheme ()
2607 register char *cp;
2608 char c;
2609 char nambuf[BUFSIZ];
2611 if (*dbp == 0)
2612 return;
2613 /* Go till you get to white space or a syntactic break */
2614 for (cp = dbp + 1; *cp && *cp != '(' && *cp != ')' && !isspace (*cp); cp++)
2615 continue;
2616 /* Null terminate the string there. */
2617 c = cp[0];
2618 cp[0] = 0;
2619 /* Copy the string */
2620 strcpy (nambuf, dbp);
2621 /* Unterminate the string */
2622 cp[0] = c;
2623 /* Announce the change */
2624 pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2625 pfcnt++;
2628 /* Find tags in TeX and LaTeX input files. */
2630 /* TEX_toktab is a table of TeX control sequences that define tags.
2631 Each TEX_tabent records one such control sequence.
2632 CONVERT THIS TO USE THE Stab TYPE!! */
2634 struct TEX_tabent
2636 char *name;
2637 int len;
2640 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
2642 /* Default set of control sequences to put into TEX_toktab.
2643 The value of environment var TEXTAGS is prepended to this. */
2645 static char *TEX_defenv =
2646 ":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2648 void TEX_mode ();
2649 struct TEX_tabent *TEX_decode_env ();
2650 void TEX_getit ();
2651 int TEX_Token ();
2653 static char TEX_esc = '\\';
2654 static char TEX_opgrp = '{';
2655 static char TEX_clgrp = '}';
2658 * TeX/LaTeX scanning loop.
2661 void
2662 TEX_funcs (fi)
2663 FILE *fi;
2665 char *lasthit;
2667 lineno = 0;
2668 charno = 0;
2669 pfcnt = 0;
2671 /* Select either \ or ! as escape character. */
2672 TEX_mode (fi);
2674 /* Initialize token table once from environment. */
2675 if (!TEX_toktab)
2676 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
2678 while (!feof (fi))
2679 { /* Scan each line in file */
2680 lineno++;
2681 linecharno = charno;
2682 charno += readline (&lb, fi);
2683 dbp = lb.buffer;
2684 lasthit = dbp;
2685 while (dbp = etags_index (dbp, TEX_esc)) /* Look at each escape in line */
2687 register int i;
2689 if (!*(++dbp))
2690 break;
2691 linecharno += dbp - lasthit;
2692 lasthit = dbp;
2693 i = TEX_Token (lasthit);
2694 if (0 <= i)
2696 TEX_getit (lasthit, TEX_toktab[i].len);
2697 break; /* We only save a line once */
2703 #define TEX_LESC '\\'
2704 #define TEX_SESC '!'
2705 #define TEX_cmt '%'
2707 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
2708 /* chars accordingly. */
2710 void
2711 TEX_mode (f)
2712 FILE *f;
2714 int c;
2716 while ((c = getc (f)) != EOF)
2718 /* Skip to next line if we hit the TeX comment char. */
2719 if (c == TEX_cmt)
2720 while (c != '\n')
2721 c = getc (f);
2722 else if (c == TEX_LESC || c == TEX_SESC )
2723 break;
2726 if (c == TEX_LESC)
2728 TEX_esc = TEX_LESC;
2729 TEX_opgrp = '{';
2730 TEX_clgrp = '}';
2732 else
2734 TEX_esc = TEX_SESC;
2735 TEX_opgrp = '<';
2736 TEX_clgrp = '>';
2738 rewind (f);
2741 /* Read environment and prepend it to the default string. */
2742 /* Build token table. */
2744 struct TEX_tabent *
2745 TEX_decode_env (evarname, defenv)
2746 char *evarname;
2747 char *defenv;
2749 register char *env, *p;
2751 struct TEX_tabent *tab;
2752 int size, i;
2754 /* Append default string to environment. */
2755 env = getenv (evarname);
2756 if (!env)
2757 env = defenv;
2758 else
2759 env = concat (env, defenv, "");
2761 /* Allocate a token table */
2762 for (size = 1, p = env; p;)
2763 if ((p = etags_index (p, ':')) && *(++p))
2764 size++;
2765 /* Add 1 to leave room for null terminator. */
2766 tab = xnew (size + 1, struct TEX_tabent);
2768 /* Unpack environment string into token table. Be careful about */
2769 /* zero-length strings (leading ':', "::" and trailing ':') */
2770 for (i = 0; *env;)
2772 p = etags_index (env, ':');
2773 if (!p) /* End of environment string. */
2774 p = env + strlen (env);
2775 if (p - env > 0)
2776 { /* Only non-zero strings. */
2777 tab[i].name = savenstr (env, p - env);
2778 tab[i].len = strlen (tab[i].name);
2779 i++;
2781 if (*p)
2782 env = p + 1;
2783 else
2785 tab[i].name = NULL; /* Mark end of table. */
2786 tab[i].len = 0;
2787 break;
2790 return tab;
2793 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
2794 The name being defined actually starts at (NAME + LEN + 1).
2795 But we seem to include the TeX command in the tag name. */
2797 void
2798 TEX_getit (name, len)
2799 char *name;
2800 int len;
2802 char *p = name + len;
2803 char nambuf[BUFSIZ];
2805 if (*name == 0)
2806 return;
2808 /* Let tag name extend to next group close (or end of line) */
2809 while (*p && *p != TEX_clgrp)
2810 p++;
2811 strncpy (nambuf, name, p - name);
2812 nambuf[p - name] = 0;
2814 pfnote (nambuf, TRUE, FALSE, lb.buffer, strlen (lb.buffer), lineno, linecharno);
2815 pfcnt++;
2818 /* If the text at CP matches one of the tag-defining TeX command names,
2819 return the etags_index of that command in TEX_toktab.
2820 Otherwise return -1. */
2822 /* Keep the capital `T' in `Token' for dumb truncating compilers
2823 (this distinguishes it from `TEX_toktab' */
2825 TEX_Token (cp)
2826 char *cp;
2828 int i;
2830 for (i = 0; TEX_toktab[i].len > 0; i++)
2831 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
2832 return i;
2833 return -1;
2836 /* Support for Prolog. */
2838 /* whole head (not only functor, but also arguments)
2839 is gotten in compound term. */
2841 void
2842 prolog_getit (s, lineno, linecharno)
2843 char *s;
2844 int lineno;
2845 long linecharno;
2847 char nambuf[BUFSIZ], *save_s, tmpc;
2848 int insquote, npar;
2850 save_s = s;
2851 insquote = FALSE;
2852 npar = 0;
2853 while (1)
2855 if (*s == '\0') /* syntax error. */
2856 return;
2857 else if (insquote && *s == '\'' && *(s + 1) == '\'')
2858 s += 2;
2859 else if (*s == '\'')
2861 insquote = !insquote;
2862 s++;
2864 else if (!insquote && *s == '(')
2866 npar++;
2867 s++;
2869 else if (!insquote && *s == ')')
2871 npar--;
2872 s++;
2873 if (npar == 0)
2874 break;
2875 else if (npar < 0) /* syntax error. */
2876 return;
2878 else if (!insquote && *s == '.' && (isspace (*(s + 1)) || *(s + 1) == '\0'))
2879 { /* fullstop. */
2880 if (npar != 0) /* syntax error. */
2881 return;
2882 s++;
2883 break;
2885 else
2886 s++;
2888 tmpc = *s;
2889 *s = '\0';
2890 strcpy (nambuf, save_s);
2891 *s = tmpc;
2892 pfnote (nambuf, TRUE, FALSE, save_s, strlen (nambuf), lineno, linecharno);
2895 /* It is assumed that prolog predicate starts from column 0. */
2897 void
2898 prolog_funcs (fi)
2899 FILE *fi;
2901 void skip_comment (), prolog_getit ();
2903 lineno = linecharno = charno = 0;
2904 while (!feof (fi))
2906 lineno++;
2907 linecharno += charno;
2908 charno = readline (&lb, fi) + 1; /* 1 for newline. */
2909 dbp = lb.buffer;
2910 if (isspace (dbp[0])) /* not predicate header. */
2911 continue;
2912 else if (dbp[0] == '%') /* comment. */
2913 continue;
2914 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
2915 skip_comment (&lb, fi, &lineno, &linecharno);
2916 else /* found. */
2917 prolog_getit (dbp, lineno, linecharno);
2921 void
2922 skip_comment (plb, fi, plineno, plinecharno)
2923 struct linebuffer *plb;
2924 FILE *fi;
2925 int *plineno; /* result */
2926 long *plinecharno; /* result */
2928 while (!substr ("*/", plb->buffer))
2930 (*plineno)++;
2931 *plinecharno += readline (plb, fi) + 1;
2932 } /* 1 for newline. */
2935 /* Return TRUE if 'sub' exists somewhere in 's'. */
2938 substr (sub, s)
2939 char *sub;
2940 char *s;
2942 while (*s && (s = etags_index (s, *sub)))
2943 if (prestr (sub, s))
2944 return (TRUE);
2945 else
2946 s++;
2947 return (FALSE);
2950 /* Return TRUE if 'pre' is prefix of string 's'. */
2953 prestr (pre, s)
2954 char *pre;
2955 char *s;
2957 if (*pre == '\0')
2958 return (TRUE);
2959 else if (*pre == *s)
2960 return (prestr (pre + 1, s + 1));
2961 else
2962 return (FALSE);
2965 /* Initialize a linebuffer for use */
2967 void
2968 initbuffer (linebuffer)
2969 struct linebuffer *linebuffer;
2971 linebuffer->size = 200;
2972 linebuffer->buffer = xnew (200, char);
2976 * Read a line of text from `stream' into `linebuffer'.
2977 * Return the number of characters read from `stream',
2978 * which is the length of the line including the newline, if any.
2980 long
2981 readline (linebuffer, stream)
2982 struct linebuffer *linebuffer;
2983 register FILE *stream;
2985 char *buffer = linebuffer->buffer;
2986 register char *p = linebuffer->buffer;
2987 register char *pend;
2988 int newline; /* 1 if ended with newline, 0 if ended with EOF */
2990 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
2992 while (1)
2994 register int c = getc (stream);
2995 if (p == pend)
2997 linebuffer->size *= 2;
2998 buffer = (char *) xrealloc (buffer, linebuffer->size);
2999 p += buffer - linebuffer->buffer;
3000 pend = buffer + linebuffer->size;
3001 linebuffer->buffer = buffer;
3003 if (c == EOF || c == '\n')
3005 *p = 0;
3006 newline = (c == '\n') ? 1 : 0;
3007 break;
3009 *p++ = c;
3012 return p - buffer + newline;
3015 char *
3016 savestr (cp)
3017 char *cp;
3019 return savenstr (cp, strlen (cp));
3022 char *
3023 savenstr (cp, len)
3024 char *cp;
3025 int len;
3027 register char *dp;
3029 dp = xnew (len + 1, char);
3030 strncpy (dp, cp, len);
3031 dp[len] = '\0';
3032 return dp;
3036 * Return the ptr in sp at which the character c last
3037 * appears; NULL if not found
3039 * Identical to v7 rindex, included for portability.
3042 char *
3043 etags_rindex (sp, c)
3044 register char *sp, c;
3046 register char *r;
3048 r = NULL;
3051 if (*sp == c)
3052 r = sp;
3053 } while (*sp++);
3054 return (r);
3059 * Return the ptr in sp at which the character c first
3060 * appears; NULL if not found
3062 * Identical to v7 index, included for portability.
3065 char *
3066 etags_index (sp, c)
3067 register char *sp, c;
3071 if (*sp == c)
3072 return (sp);
3073 } while (*sp++);
3074 return (NULL);
3077 /* Print error message and exit. */
3079 /* VARARGS1 */
3080 void
3081 fatal (s1, s2)
3082 char *s1, *s2;
3084 error (s1, s2);
3085 exit (BAD);
3088 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3090 /* VARARGS1 */
3091 void
3092 error (s1, s2)
3093 char *s1, *s2;
3095 fprintf (stderr, "%s: ", progname);
3096 fprintf (stderr, s1, s2);
3097 fprintf (stderr, "\n");
3100 /* Return a newly-allocated string whose contents
3101 concatenate those of s1, s2, s3. */
3103 char *
3104 concat (s1, s2, s3)
3105 char *s1, *s2, *s3;
3107 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3108 char *result = xnew (len1 + len2 + len3 + 1, char);
3110 strcpy (result, s1);
3111 strcpy (result + len1, s2);
3112 strcpy (result + len1 + len2, s3);
3113 result[len1 + len2 + len3] = '\0';
3115 return result;
3118 /* Return a newly allocated string containing the filename of FILE relative
3119 to the absolute directory DIR (which should end with a slash). */
3121 char *
3122 relative_filename (file, dir)
3123 char *file, *dir;
3125 char *fp, *dp, *res;
3127 /* Find the common root of file and dir. */
3128 fp = absolute_filename (file, cwd);
3129 dp = dir;
3130 while (*fp++ == *dp++)
3131 continue;
3134 fp--;
3135 dp--;
3137 while (*fp != '/');
3139 /* Build a sequence of "../" strings for the resulting relative filename. */
3140 for (dp = etags_index (dp + 1, '/'), res = "";
3141 dp != NULL;
3142 dp = etags_index (dp + 1, '/'))
3144 res = concat (res, "../", "");
3147 /* Add the filename relative to the common root of file and dir. */
3148 res = concat (res, fp + 1, "");
3150 return res; /* temporary stub */
3153 /* Return a newly allocated string containing the
3154 absolute filename of FILE given CWD (which should end with a slash). */
3155 char *
3156 absolute_filename (file, cwd)
3157 char *file, *cwd;
3159 char *slashp, *cp, *res;
3161 if (file[0] == '/')
3162 res = concat (file, "", "");
3163 else
3164 res = concat (cwd, file, "");
3166 /* Delete the "/dirname/.." and "/." substrings. */
3167 slashp = etags_index (res, '/');
3168 while (slashp != NULL && slashp[0] != '\0')
3170 if (slashp[1] == '.')
3172 if (slashp[2] == '.'
3173 && (slashp[3] == '/' || slashp[3] == '\0'))
3175 cp = slashp;
3177 cp--;
3178 while (cp >= res && *cp != '/');
3179 if (*cp == '/')
3181 strcpy (cp, slashp + 3);
3183 else /* else (cp == res) */
3185 if (slashp[3] != NULL)
3186 strcpy (cp, slashp + 4);
3187 else
3188 return ".";
3190 slashp = cp;
3192 else if (slashp[2] == '/' || slashp[2] == '\0')
3194 strcpy (slashp, slashp + 2);
3197 else
3199 slashp = etags_index (slashp + 1, '/');
3203 return res;
3206 /* Return a newly allocated string containing the absolute filename
3207 of dir where FILE resides given CWD (which should end with a slash). */
3208 char *
3209 absolute_dirname (file, cwd)
3210 char *file, *cwd;
3212 char *slashp, *res;
3213 char save;
3215 slashp = etags_rindex (file, '/');
3216 if (slashp == NULL)
3217 return cwd;
3218 save = slashp[1];
3219 slashp[1] = '\0';
3220 res = absolute_filename (file, cwd);
3221 slashp[1] = save;
3223 return res;
3226 /* Like malloc but get fatal error if memory is exhausted. */
3228 char *
3229 xmalloc (size)
3230 unsigned int size;
3232 char *result = (char *) malloc (size);
3233 if (result == NULL)
3234 fatal ("virtual memory exhausted", 0);
3235 return result;
3238 char *
3239 xrealloc (ptr, size)
3240 char *ptr;
3241 unsigned int size;
3243 char *result = (char *) realloc (ptr, size);
3244 if (result == NULL)
3245 fatal ("virtual memory exhausted");
3246 return result;