(verify_overlay_modification):
[bpt/emacs.git] / lib-src / etags.c
CommitLineData
c6d46f5f 1/* Tags file maker to go with GNU Emacs
13fde0cd 2 Copyright (C) 1984, 1987, 1988, 1989, 1993 Free Software Foundation, Inc. and Ken Arnold
c6d46f5f 3
ea6cd314 4This file is not considered part of GNU Emacs.
c6d46f5f 5
ea6cd314 6This program is free software; you can redistribute it and/or modify
c6d46f5f 7it under the terms of the GNU General Public License as published by
ea6cd314
RS
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
c6d46f5f 10
ea6cd314 11This program is distributed in the hope that it will be useful,
c6d46f5f
JB
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
ea6cd314
RS
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
c6d46f5f
JB
19
20/*
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++.
31d4b314 27 *
46c145db 28 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
c6d46f5f
JB
29 */
30
32daa216 31char pot_etags_version[] = "@(#) pot revision number is 10.18";
46c145db 32
c6880c90
RS
33#ifdef MSDOS
34#include <fcntl.h>
35#endif /* MSDOS */
36
72a339d7 37#ifdef HAVE_CONFIG_H
18160b98 38#include <../src/config.h>
1e134a5f
RM
39#endif
40
918f9ad1
JB
41#include <stdio.h>
42#include <ctype.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45
2b878b4c
FP
46#if !defined (S_ISREG) && defined (S_IFREG)
47# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
48#endif
49
918f9ad1
JB
50#include "getopt.h"
51
c6d46f5f 52extern char *getenv ();
46c145db 53extern char *getcwd ();
c6d46f5f 54
c6d46f5f 55
32daa216
FP
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
c6d46f5f
JB
64#endif
65
66/* Exit codes for success and failure. */
67#ifdef VMS
32daa216
FP
68#define GOOD 1
69#define BAD 0
c6d46f5f 70#else
32daa216
FP
71#define GOOD 0
72#define BAD 1
c6d46f5f
JB
73#endif
74
75/*
76 * The FILEPOS abstract type, which represents a position in a file,
77 * plus the following accessor functions:
78 *
79 * long GET_CHARNO (pos)
80 * returns absolute char number.
c6d46f5f
JB
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'.
88 *
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.
92 *
93 * FILEPOS objects can also be assigned and passed to and from
94 * functions in the normal C manner.
95 *
96 * Implementation notes: the `+ 0' is to enforce rvalue-ness.
97 */
c6d46f5f 98
c6d46f5f 99#ifndef DEBUG
13fde0cd 100 /* real implementation */
c6d46f5f
JB
101typedef long FILEPOS;
102#define GET_CHARNO(pos) ((pos) + 0)
c6d46f5f
JB
103#define SET_FILEPOS(pos, fp, cno) ((void) ((pos) = (cno)))
104#else
13fde0cd 105 /* debugging implementation */
c6d46f5f
JB
106typedef struct
107{
108 long charno;
109} FILEPOS;
110
111#define GET_CHARNO(pos) ((pos).charno + 0)
c6d46f5f
JB
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
c6d46f5f
JB
117
118#define streq(s, t) (strcmp (s, t) == 0)
119#define strneq(s, t, n) (strncmp (s, t, n) == 0)
13fde0cd 120#define logical int
c6d46f5f
JB
121
122#define TRUE 1
123#define FALSE 0
124
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 */
c6d46f5f
JB
129
130#define max(I1,I2) ((I1) > (I2) ? (I1) : (I2))
131
132struct 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 */
fe0b3356 137 logical named; /* list name separately */
c6d46f5f
JB
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 */
143};
144
c6d46f5f
JB
145typedef struct nd_st NODE;
146
13fde0cd
RS
147logical header_file; /* TRUE if .h file, FALSE o.w. */
148/* boolean "functions" (see init) */
149logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
c6d46f5f 150
46c145db
FP
151char cwd [BUFSIZ]; /* current working directory */
152char *outfiledir; /* directory of tagfile */
c6d46f5f
JB
153
154char *concat ();
46c145db
FP
155char *savenstr (), *savestr ();
156char *etags_index (), *etags_rindex ();
157char *relative_filename (), *absolute_filename (), *absolute_dirname ();
158char *xmalloc (), *xrealloc ();
31d4b314 159int L_isdef (), L_isquote ();
c6d46f5f
JB
160int PF_funcs ();
161int total_size_of_entries ();
162logical consider_token ();
163logical tail ();
164long readline ();
165void Asm_funcs ();
166void C_entries ();
167void L_funcs ();
168void L_getit ();
169void PAS_funcs ();
170void Scheme_funcs ();
171void TEX_funcs ();
172void add_node ();
173void error ();
174void fatal ();
42680d3c 175logical find_entries ();
c6d46f5f
JB
176void free_tree ();
177void getit ();
c6d46f5f
JB
178void init ();
179void initbuffer ();
180void initbuffer ();
181void pfnote ();
182void process_file ();
183void put_entries ();
184void takeprec ();
185
186/*
187 * MACRO
188 * xnew -- allocate storage
189 *
190 * SYNOPSIS
191 * Type *xnew (int n, Type);
192 */
193#define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
c6d46f5f
JB
194
195/*
42680d3c 196 * Symbol table types.
c6d46f5f 197 */
c6d46f5f
JB
198enum sym_type
199{
200 st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
201};
c6d46f5f
JB
202\f
203
204
205typedef int LINENO;
206
207typedef struct
208{
209 char *p;
210 int len;
c6d46f5f 211 LINENO lineno;
fe0b3356 212 logical named;
c6d46f5f 213} TOKEN;
c6d46f5f 214
13fde0cd 215/* C extensions.
c6d46f5f 216 */
13fde0cd
RS
217#define C_PLPL 0x00001 /* C++ */
218#define C_STAR 0x00003 /* C* */
219#define YACC 0x10000 /* yacc file */
c6d46f5f
JB
220
221char searchar = '/'; /* use /.../ searches */
222
223LINENO lineno; /* line number of current line */
224long charno; /* current character number */
c6d46f5f
JB
225
226long linecharno; /* charno of start of line; not used by C, but
227 * by every other language.
228 */
229
230char *curfile, /* current input file name */
231 *outfile, /* output file */
232 *white = " \f\t\n", /* white chars */
13fde0cd
RS
233 *endtk = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
234 /* token starting chars */
b12756c8 235 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
46c145db 236 /* valid in-token chars */
13fde0cd 237 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
c6d46f5f 238
c6d46f5f 239int append_to_tagfile; /* -a: append to tags */
c6d46f5f
JB
240/* The following three default to 1 for etags, but to 0 for ctags. */
241int typedefs; /* -t: create tags for typedefs */
242int typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
243 /* 0 struct/enum/union decls, and C++ */
32daa216 244 /* member functions. */
c6d46f5f 245int constantypedefs; /* -d: create tags for C #define and enum */
32daa216 246 /* constants. Enum consts not implemented. */
c6d46f5f
JB
247 /* -D: opposite of -d. Default under ctags. */
248int update; /* -u: update tags */
249int vgrind_style; /* -v: create vgrind style index output */
250int no_warnings; /* -w: suppress warnings */
251int cxref_style; /* -x: create cxref style output */
252int cplusplus; /* .[hc] means C++, not C */
253int noindentypedefs; /* -S: ignore indentation in C */
c6d46f5f
JB
254
255/* Name this program was invoked with. */
256char *progname;
257
4746118a
JB
258struct 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 }
277};
278
c6d46f5f
JB
279FILE *inf, /* ioptr for current input file */
280 *outf; /* ioptr for tags file */
281
282NODE *head; /* the head of the binary tree of tags */
283
284int permit_duplicates = 1; /* Nonzero means allow duplicate tags. */
285
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. */
289
290struct linebuffer
291{
292 long size;
293 char *buffer;
294};
295
296struct linebuffer lb; /* the current line */
c6d46f5f 297struct linebuffer filename_lb; /* used to read in filenames */
13fde0cd
RS
298struct
299{
300 FILEPOS linepos;
301 struct linebuffer lb; /* used by C_entries instead of lb */
302} lbs[2];
c6d46f5f 303\f
4746118a
JB
304void
305print_version ()
306{
32daa216
FP
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
4746118a 312
1a0d8c80 313 exit (GOOD);
4746118a
JB
314}
315
316void
317print_help ()
318{
319 printf ("These are the options accepted by %s. You may use unambiguous\n\
1a0d8c80
FP
320abbreviations for the long option names. A - as file name means read file\n\
321names from stdin.\n\n", progname);
4746118a 322
52cc7c59
JB
323 puts ("-a, --append\n\
324 Append tag entries to existing tags file.");
1a0d8c80 325
32daa216
FP
326 if (CTAGS)
327 puts ("-B, --backward-search\n\
1a0d8c80
FP
328 Write the search commands for the tag entries using '?', the\n\
329 backward-search command.");
1a0d8c80 330
52cc7c59 331 puts ("-C, --c++\n\
4746118a
JB
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\
52cc7c59 334 extensions are always assumed to be C++ code.");
4746118a 335
32daa216
FP
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.");
4746118a 343
32daa216
FP
344 if (CTAGS)
345 puts ("-F, --forward-search\n\
1a0d8c80
FP
346 Write the search commands for the tag entries using '/', the\n\
347 forward-search command.");
1a0d8c80 348
32daa216
FP
349 if (!CTAGS)
350 puts ("-i FILE, --include=FILE\n\
1a0d8c80
FP
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.");
4746118a 354
1a0d8c80
FP
355 puts ("-o FILE, --output=FILE\n\
356 Write the tags to FILE.");
357 puts ("-S, --ignore-indentation\n\
4746118a
JB
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\
32daa216 361 definition in C and C++.");
4746118a 362
32daa216
FP
363 if (CTAGS)
364 {
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.");
370 }
371
372 if (CTAGS)
373 {
374 puts ("-u, --update\n\
4746118a
JB
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\
52cc7c59 380 tag file than to use this.");
32daa216 381 puts ("-v, --vgrind\n\
4746118a
JB
382 Generates an index of items intended for human consumption,\n\
383 similar to the output of vgrind. The index is sorted, and\n\
52cc7c59 384 gives the page number of each item.");
32daa216 385 puts ("-x, --cxref\n\
4746118a
JB
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\
52cc7c59 389 which you like.");
32daa216 390 puts ("-w, --no-warn\n\
4746118a
JB
391 Suppress warning messages about entries defined in multiple\n\
392 files.");
32daa216 393 }
4746118a
JB
394
395 puts ("-V, --version\n\
396 Print the version of the program.\n\
397-H, --help\n\
398 Print this help message.");
399
1a0d8c80 400 exit (GOOD);
4746118a
JB
401}
402
403\f
c6d46f5f
JB
404void
405main (argc, argv)
406 int argc;
407 char *argv[];
408{
409 char cmd[100];
410 int i;
1e134a5f 411 unsigned int nincluded_files = 0;
72a339d7 412 char **included_files = xnew (argc, char *);
c6d46f5f
JB
413 char *this_file;
414#ifdef VMS
415 char got_err;
416
417 extern char *gfnames ();
418 extern char *massage_name ();
419#endif
46c145db 420
c6880c90 421#ifdef MSDOS
42680d3c 422 _fmode = O_BINARY; /* all of files are treated as binary files */
c6880c90
RS
423#endif /* MSDOS */
424
c6d46f5f
JB
425 progname = argv[0];
426
c6d46f5f
JB
427 /*
428 * If etags, always find typedefs and structure tags. Why not?
429 * Also default is to find macro constants.
430 */
32daa216 431 if (!CTAGS)
c6d46f5f
JB
432 typedefs = typedefs_and_cplusplus = constantypedefs = 1;
433
4746118a 434 for (;;)
c6d46f5f 435 {
4746118a 436 int opt;
32daa216 437 opt = getopt_long (argc, argv, "aCdDf:o:StTi:BFuvxwVH", longopts, 0);
4746118a
JB
438
439 if (opt == EOF)
440 break;
441
442 switch (opt)
c6d46f5f 443 {
4746118a
JB
444 case '\0':
445 /* If getopt returns '\0', then it has already processed a
446 long-named option. We should do nothing. */
447 break;
448
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;
32daa216 462 case 'f': /* for compatibility with old makefiles */
4746118a
JB
463 case 'o':
464 if (outfile)
c6d46f5f 465 {
4746118a 466 fprintf (stderr,
3ed79319 467 "%s: -%c flag may only be given once\n", progname, opt);
c6d46f5f
JB
468 goto usage;
469 }
4746118a
JB
470 outfile = optarg;
471 break;
472 case 'S':
473 noindentypedefs++;
474 break;
4746118a
JB
475 case 'V':
476 print_version ();
477 break;
478 case 'H':
479 print_help ();
480 break;
481
482 /* Etags options */
483 case 'i':
32daa216 484 if (CTAGS)
4746118a
JB
485 goto usage;
486 included_files[nincluded_files++] = optarg;
487 break;
488
489 /* Ctags options. */
490 case 'B':
491 searchar = '?';
32daa216 492 if (!CTAGS) goto usage;
4746118a
JB
493 break;
494 case 'F':
495 searchar = '/';
32daa216
FP
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;
4746118a
JB
506 break;
507 case 'u':
508 update++;
32daa216 509 if (!CTAGS) goto usage;
4746118a
JB
510 break;
511 case 'v':
512 vgrind_style++;
513 /*FALLTHRU*/
514 case 'x':
515 cxref_style++;
32daa216 516 if (!CTAGS) goto usage;
4746118a
JB
517 break;
518 case 'w':
519 no_warnings++;
32daa216 520 if (!CTAGS) goto usage;
4746118a
JB
521 break;
522
523 default:
524 goto usage;
c6d46f5f 525 }
c6d46f5f
JB
526 }
527
0e5ad25f 528 if (optind == argc && nincluded_files == 0)
c6d46f5f 529 {
4746118a
JB
530 fprintf (stderr, "%s: No input files specified.\n", progname);
531
c6d46f5f 532 usage:
0e5ad25f 533 fprintf (stderr, "%s: Try `%s --help' for a complete list of options.\n",
4746118a 534 progname, progname);
c6d46f5f
JB
535 exit (BAD);
536 }
537
46c145db 538 if (outfile == NULL)
c6d46f5f 539 {
32daa216 540 outfile = CTAGS ? "tags" : "TAGS";
c6d46f5f 541 }
46c145db
FP
542 getcwd (cwd, BUFSIZ); /* the current working directory */
543 strcat (cwd, "/");
544 if (streq (outfile, "-"))
545 {
546 outfiledir = cwd;
547 }
548 else
549 {
550 outfiledir = absolute_dirname (outfile, cwd);
551 }
c6d46f5f
JB
552
553 init (); /* set up boolean "functions" */
554
555 initbuffer (&lb);
13fde0cd
RS
556 initbuffer (&lbs[0].lb);
557 initbuffer (&lbs[1].lb);
c6d46f5f
JB
558 initbuffer (&filename_lb);
559 /*
560 * loop through files finding functions
561 */
32daa216 562 if (!CTAGS)
c6d46f5f
JB
563 {
564 if (streq (outfile, "-"))
565 outf = stdout;
566 else
567 outf = fopen (outfile, append_to_tagfile ? "a" : "w");
1a0d8c80 568 if (outf == NULL)
c6d46f5f
JB
569 {
570 perror (outfile);
1a0d8c80 571 exit (BAD);
c6d46f5f
JB
572 }
573 }
574
c6d46f5f 575#ifdef VMS
4746118a
JB
576 argc -= optind;
577 argv += optind;
578 while (gfnames (&argc, &argv, &got_err) != NULL)
c6d46f5f
JB
579 {
580 if (got_err)
581 {
582 error ("Can't find file %s\n", this_file);
583 argc--, argv++;
584 }
585 else
586 {
587 this_file = massage_name (this_file);
588#if 0
589 }
13fde0cd 590 } /* solely to balance out the ifdef'd parens above */
c6d46f5f
JB
591#endif
592#else
4746118a 593 for (; optind < argc; optind++)
c6d46f5f 594 {
4746118a 595 this_file = argv[optind];
c6d46f5f 596#endif
42680d3c 597 /* Input file named "-" means read file names from stdin and use them. */
46c145db
FP
598 if (streq (this_file, "-"))
599 {
600 while (!feof (stdin))
c6d46f5f 601 {
46c145db
FP
602 (void) readline (&filename_lb, stdin);
603 if (strlen (filename_lb.buffer) > 0)
604 process_file (filename_lb.buffer);
c6d46f5f 605 }
c6d46f5f 606 }
46c145db
FP
607 else
608 process_file (this_file);
609 }
c6d46f5f 610
32daa216 611 if (!CTAGS)
c6d46f5f 612 {
1e134a5f
RM
613 while (nincluded_files-- > 0)
614 fprintf (outf, "\f\n%s,include\n", *included_files++);
615
c6d46f5f 616 (void) fclose (outf);
1a0d8c80 617 exit (GOOD);
c6d46f5f
JB
618 }
619
620 if (cxref_style)
621 {
622 put_entries (head);
623 exit (GOOD);
624 }
4746118a 625 if (update)
c6d46f5f 626 {
4746118a
JB
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++)
c6d46f5f
JB
630 {
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);
635 }
636 append_to_tagfile++;
637 }
638 outf = fopen (outfile, append_to_tagfile ? "a" : "w");
639 if (outf == NULL)
640 {
641 perror (outfile);
642 exit (GOOD);
643 }
644 put_entries (head);
645 (void) fclose (outf);
646 if (update)
647 {
648 sprintf (cmd, "sort %s -o %s", outfile, outfile);
649 (void) system (cmd);
650 }
651 exit (GOOD);
652}
653
654
655/*
656 * This routine is called on each file argument.
657 */
658void
659process_file (file)
660 char *file;
661{
662 struct stat stat_buf;
663
42680d3c 664 if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
c6d46f5f
JB
665 {
666 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
667 return;
668 }
c6d46f5f
JB
669 if (streq (file, outfile) && !streq (outfile, "-"))
670 {
671 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
672 return;
673 }
42680d3c
FP
674 if (!find_entries (file))
675 {
676 return;
677 }
32daa216 678 if (!CTAGS)
c6d46f5f 679 {
46c145db
FP
680 char *filename;
681
682 if (file[0] == '/')
683 {
684 /* file is an absolute filename. Canonicalise it. */
685 filename = absolute_filename (file, cwd);
686 }
687 else
688 {
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);
692 }
693 fprintf (outf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
c6d46f5f
JB
694 put_entries (head);
695 free_tree (head);
696 head = NULL;
697 }
698}
699
700/*
eb8c3be9 701 * This routine sets up the boolean pseudo-functions which work
99e0a2e0 702 * by setting boolean flags dependent upon the corresponding character
c6d46f5f
JB
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.
707 */
708void
709init ()
710{
13fde0cd
RS
711 register char *sp;
712 register int i;
c6d46f5f
JB
713
714 for (i = 0; i < 0177; i++)
13fde0cd 715 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
c6d46f5f
JB
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;
c6d46f5f
JB
724 _wht[0] = _wht['\n'];
725 _etk[0] = _etk['\n'];
726 _btk[0] = _btk['\n'];
727 _itk[0] = _itk['\n'];
c6d46f5f
JB
728}
729
730/*
731 * This routine opens the specified file and calls the function
732 * which finds the function and type definitions.
733 */
42680d3c 734logical
c6d46f5f
JB
735find_entries (file)
736 char *file;
737{
738 char *cp;
739 void prolog_funcs ();
740
741 inf = fopen (file, "r");
742 if (inf == NULL)
743 {
744 perror (file);
42680d3c 745 return FALSE;
c6d46f5f
JB
746 }
747 curfile = savestr (file);
8a6c8bcf 748 cp = etags_rindex (file, '.');
c6d46f5f
JB
749
750 header_file = (cp && (streq (cp + 1, "h")));
751
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")))
755 {
756 TEX_funcs (inf);
757 goto close_and_return;
758 }
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")))
766 {
767 L_funcs (inf);
768 goto close_and_return;
769 }
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")
32daa216 776 || streq (cp + 1, "ss")
c6d46f5f
JB
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))))
784 {
785 Scheme_funcs (inf);
42680d3c 786 goto close_and_return;
c6d46f5f 787 }
13fde0cd
RS
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")))
c6d46f5f
JB
793 {
794 Asm_funcs (inf);
42680d3c 795 goto close_and_return;
c6d46f5f
JB
796 }
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")))
803 {
804 C_entries (C_PLPL); /* C++ */
805 goto close_and_return;
806 }
807 /* .cs or .hs: a C* file */
13fde0cd
RS
808 if (cp && (streq (cp + 1, "cs")
809 || streq (cp + 1, "hs")))
c6d46f5f
JB
810 {
811 C_entries (C_STAR);
812 goto close_and_return;
813 }
13fde0cd
RS
814 /* .y: a yacc file */
815 if (cp && (streq (cp + 1, "y")))
816 {
817 C_entries (YACC);
818 goto close_and_return;
819 }
c6d46f5f 820 /* .pl implies prolog source code */
13fde0cd 821 if (cp && streq (cp + 1, "pl"))
c6d46f5f
JB
822 {
823 prolog_funcs (inf);
824 goto close_and_return;
825 }
826 /* .p or .pas: a Pascal file */
827 if (cp && (streq (cp + 1, "p")
828 || streq (cp + 1, "pas")))
829 {
830 PAS_funcs (inf);
831 goto close_and_return;
832 }
1e8e1162 833 /* If .f or .for, assume it is fortran or nothing. */
13fde0cd
RS
834 if (cp && (streq (cp + 1, "f")
835 || streq (cp + 1, "for")))
1e8e1162 836 {
42680d3c 837 (void) PF_funcs (inf);
1e8e1162
RS
838 goto close_and_return;
839 }
c6d46f5f 840 /* if not a .c or .h or .y file, try fortran */
1e8e1162
RS
841 if (cp && ((cp[1] != 'c'
842 && cp[1] != 'h'
843 && cp[1] != 'y')
844 || (cp[1] != 0 && cp[2] != 0)))
c6d46f5f
JB
845 {
846 if (PF_funcs (inf) != 0)
847 goto close_and_return;
848 rewind (inf); /* no fortran tags found, try C */
849 }
850 C_entries (cplusplus ? C_PLPL : 0);
851
852close_and_return:
853 (void) fclose (inf);
42680d3c 854 return TRUE;
c6d46f5f
JB
855}
856
857/* Nonzero if string STR is composed of digits. */
858
859int
860string_numeric_p (str)
861 char *str;
862{
863 while (*str)
864 {
865 if (*str < '0' || *str > '9')
866 return 0;
867 }
868 return 1;
869}
870\f
871/* Record a tag. */
872/* Should take a TOKEN* instead!! */
c6d46f5f 873void
fe0b3356 874pfnote (name, is_func, named, linestart, linelen, lno, cno)
c6d46f5f
JB
875 char *name; /* tag name */
876 logical is_func; /* function or type name? */
fe0b3356 877 logical named; /* tag different from text of definition? */
c6d46f5f
JB
878 char *linestart;
879 int linelen;
880 int lno;
881 long cno;
882{
883 register char *fp;
884 register NODE *np;
885 char tem[51];
886 char c;
887
1a0d8c80 888 np = xnew (1, NODE);
c6d46f5f
JB
889 if (np == NULL)
890 {
32daa216 891 if (CTAGS)
c6d46f5f
JB
892 {
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.
896 */
1a0d8c80 897 error ("too many entries to sort", 0);
c6d46f5f
JB
898 }
899 put_entries (head);
900 free_tree (head);
901 head = NULL;
902 np = xnew (1, NODE);
903 }
904 /* If ctags mode, change name "main" to M<thisfilename>. */
32daa216 905 if (CTAGS && !cxref_style && streq (name, "main"))
c6d46f5f 906 {
8a6c8bcf 907 fp = etags_rindex (curfile, '/');
c6d46f5f 908 name = concat ("M", fp == 0 ? curfile : fp + 1, "");
8a6c8bcf 909 fp = etags_rindex (name, '.');
c6d46f5f
JB
910 if (fp && fp[1] != '\0' && fp[2] == '\0')
911 *fp = 0;
fe0b3356 912 named = TRUE;
c6d46f5f
JB
913 }
914 np->name = savestr (name);
915 np->file = curfile;
916 np->is_func = is_func;
fe0b3356 917 np->named = named;
c6d46f5f
JB
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;
32daa216 922 if (!CTAGS)
c6d46f5f
JB
923 {
924 c = linestart[linelen];
925 linestart[linelen] = 0;
926 }
927 else if (cxref_style == 0)
928 {
929 sprintf (tem, strlen (linestart) < 50 ? "%s$" : "%.50s", linestart);
930 linestart = tem;
931 }
932 np->pat = savestr (linestart);
32daa216 933 if (!CTAGS)
c6d46f5f
JB
934 {
935 linestart[linelen] = c;
936 }
937
938 add_node (np, &head);
939}
940
941/*
942 * free_tree ()
943 * recurse on left children, iterate on right children.
944 */
945void
946free_tree (node)
947 register NODE *node;
948{
949 while (node)
950 {
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;
957 }
958}
959
960/*
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.
965 *
966 * add_node is the only function allowed to add nodes, so it can
967 * maintain state.
968 */
8f53e1ee
RS
969/* Must avoid static vars within functions since some systems
970 #define static as nothing. */
971static NODE *last_node = NULL;
972
c6d46f5f
JB
973void
974add_node (node, cur_node_p)
975 NODE *node, **cur_node_p;
976{
977 register int dif;
978 register NODE *cur_node = *cur_node_p;
c6d46f5f
JB
979
980 if (cur_node == NULL)
981 {
982 *cur_node_p = node;
983 last_node = node;
984 return;
985 }
986
32daa216 987 if (!CTAGS)
c6d46f5f
JB
988 {
989 /* Etags Mode */
1a0d8c80
FP
990 if (last_node == NULL)
991 fatal ("internal error in add_node", 0);
c6d46f5f
JB
992 last_node->right = node;
993 last_node = node;
994 }
995 else
996 {
997 /* Ctags Mode */
998 dif = strcmp (node->name, cur_node->name);
999
1000 /*
1001 * If this tag name matches an existing one, then
1002 * do not add the node, but maybe print a warning.
1003 */
1004 if (!dif)
1005 {
1006 if (node->file == cur_node->file)
1007 {
1008 if (!no_warnings)
1009 {
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");
1013 }
1014 return;
1015 }
1016 if (!cur_node->been_warned && !no_warnings)
1017 {
1018 fprintf (stderr,
1019 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1020 node->file, cur_node->file, node->name);
1021 }
1022 cur_node->been_warned = TRUE;
1023 return;
1024 }
1025
1026 /* Maybe refuse to add duplicate nodes. */
1027 if (!permit_duplicates)
1028 {
1a0d8c80
FP
1029 if (streq (node->name, cur_node->name)
1030 && streq (node->file, cur_node->file))
c6d46f5f
JB
1031 return;
1032 }
1033
1034 /* Actually add the node */
1035 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1036 }
1037}
1038\f
1039void
1040put_entries (node)
13fde0cd 1041 register NODE *node;
c6d46f5f 1042{
13fde0cd 1043 register char *sp;
c6d46f5f
JB
1044
1045 if (node == NULL)
1046 return;
1047
1048 /* Output subentries that precede this one */
1049 put_entries (node->left);
1050
1051 /* Output this entry */
1052
32daa216 1053 if (!CTAGS)
c6d46f5f 1054 {
fe0b3356 1055 if (node->named)
c6d46f5f
JB
1056 {
1057 fprintf (outf, "%s\177%s\001%d,%d\n",
cc6d6e58
RM
1058 node->pat, node->name,
1059 node->lno, node->cno);
c6d46f5f
JB
1060 }
1061 else
1062 {
1063 fprintf (outf, "%s\177%d,%d\n",
cc6d6e58
RM
1064 node->pat,
1065 node->lno, node->cno);
c6d46f5f
JB
1066 }
1067 }
1068 else if (!cxref_style)
1069 {
1070 fprintf (outf, "%s\t%s\t",
1071 node->name, node->file);
1072
1073 if (node->is_func)
1074 { /* a function */
1075 putc (searchar, outf);
1076 putc ('^', outf);
1077
1078 for (sp = node->pat; *sp; sp++)
1079 {
1080 if (*sp == '\\' || *sp == searchar)
1081 putc ('\\', outf);
1082 putc (*sp, outf);
1083 }
1084 putc (searchar, outf);
1085 }
1086 else
1087 { /* a typedef; text pattern inadequate */
1088 fprintf (outf, "%d", node->lno);
1089 }
1090 putc ('\n', outf);
1091 }
1092 else if (vgrind_style)
1093 fprintf (stdout, "%s %s %d\n",
1094 node->name, node->file, (node->lno + 63) / 64);
1095 else
daa37602 1096 fprintf (stdout, "%-16s %3d %-16s %s\n",
c6d46f5f
JB
1097 node->name, node->lno, node->file, node->pat);
1098
1099 /* Output subentries that follow this one */
1100 put_entries (node->right);
1101}
1102
1103/* Length of a number's decimal representation. */
1104int
1105number_len (num)
1106 long num;
1107{
1108 int len = 0;
1109 if (!num)
1110 return 1;
1111 for (; num; num /= 10)
1112 ++len;
1113 return len;
1114}
1115
1116/*
1117 * Return total number of characters that put_entries will output for
32daa216
FP
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.
c6d46f5f
JB
1122 */
1123int
1124total_size_of_entries (node)
13fde0cd 1125 register NODE *node;
c6d46f5f 1126{
13fde0cd 1127 register int total;
c6d46f5f
JB
1128
1129 if (node == NULL)
1130 return 0;
1131
1132 total = 0;
1133 for (; node; node = node->right)
1134 {
1135 /* Count left subentries. */
1136 total += total_size_of_entries (node->left);
1137
1138 /* Count this entry */
1139 total += strlen (node->pat) + 1;
1140 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
fe0b3356 1141 if (node->named)
c6d46f5f
JB
1142 total += 1 + strlen (node->name); /* \001name */
1143 }
1144
1145 return total;
1146}
1147\f
1148/*
1149 * The C symbol tables.
1150 */
1151
42680d3c
FP
1152/* Feed stuff between (but not including) %[ and %] lines to:
1153 gperf -c -k1,3 -o -p -r -t
1154%[
1155struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1156%%
1157class, C_PLPL, st_C_struct
1158domain, C_STAR, st_C_struct
1159union, 0, st_C_struct
1160struct, 0, st_C_struct
1161enum, 0, st_C_enum
1162typedef, 0, st_C_typedef
1163define, 0, st_C_define
1164long, 0, st_C_typespec
1165short, 0, st_C_typespec
1166int, 0, st_C_typespec
1167char, 0, st_C_typespec
1168float, 0, st_C_typespec
1169double, 0, st_C_typespec
1170signed, 0, st_C_typespec
1171unsigned, 0, st_C_typespec
1172auto, 0, st_C_typespec
1173void, 0, st_C_typespec
1174extern, 0, st_C_typespec
1175static, 0, st_C_typespec
1176const, 0, st_C_typespec
1177volatile, 0, st_C_typespec
1178%]
1179and 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 */
1183
1184
1185struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
1186
1187#define MIN_WORD_LENGTH 3
1188#define MAX_WORD_LENGTH 8
1189#define MIN_HASH_VALUE 10
1190#define MAX_HASH_VALUE 62
c6d46f5f 1191/*
42680d3c
FP
1192 21 keywords
1193 53 is the maximum key range
1194*/
1195
1196static int
1197hash (str, len)
1198 register char *str;
1199 register int len;
1200{
1201 static unsigned char hash_table[] =
1202 {
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,
1216 };
1217 return len + hash_table[str[2]] + hash_table[str[0]];
1218}
c6d46f5f 1219
42680d3c
FP
1220struct C_stab_entry *
1221in_word_set (str, len)
1222 register char *str;
1223 register int len;
1224{
1225
1226 static struct C_stab_entry wordlist[] =
1227 {
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},
1265 };
1266
1267 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
1268 {
1269 register int key = hash (str, len);
1270
1271 if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
1272 {
1273 register char *s = wordlist[key].name;
1274
1a0d8c80 1275 if (*s == *str && strneq (str + 1, s + 1, len - 1))
42680d3c
FP
1276 return &wordlist[key];
1277 }
1278 }
1279 return 0;
c6d46f5f 1280}
42680d3c 1281/*%>*/
c6d46f5f 1282
42680d3c
FP
1283enum sym_type
1284C_symtype(str, len, c_ext)
1285 char *str;
1286 int len;
c6d46f5f
JB
1287 int c_ext;
1288{
42680d3c 1289 register struct C_stab_entry *se = in_word_set(str, len);
c6d46f5f 1290
42680d3c
FP
1291 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
1292 return st_none;
1293 return se->type;
c6d46f5f
JB
1294}
1295\f
13fde0cd 1296 /*
13fde0cd
RS
1297 * C functions are recognized using a simple finite automaton.
1298 * funcdef is its state variable.
1299 */
1300typedef enum
1301{
31d4b314
FP
1302 fnone, /* nothing seen */
1303 ftagseen, /* function-like tag seen */
b12756c8 1304 fstartlist, /* just after open parenthesis */
31d4b314
FP
1305 finlist, /* in parameter list */
1306 flistseen, /* after parameter list */
46e4cb76 1307 fignore /* before open brace */
13fde0cd
RS
1308} FUNCST;
1309FUNCST funcdef;
1310
1311
46c145db
FP
1312 /*
1313 * typedefs are recognized using a simple finite automaton.
13fde0cd
RS
1314 * typeddef is its state variable.
1315 */
1316typedef enum
1317{
31d4b314
FP
1318 tnone, /* nothing seen */
1319 ttypedseen, /* typedef keyword seen */
1320 tinbody, /* inside typedef body */
46c145db
FP
1321 tend, /* just before typedef tag */
1322 tignore /* junk after typedef tag */
13fde0cd
RS
1323} TYPEDST;
1324TYPEDST typdef;
1325
1326
46c145db
FP
1327 /*
1328 * struct-like structures (enum, struct and union) are recognized
1329 * using another simple finite automaton. `structdef' is its state
1330 * variable.
13fde0cd
RS
1331 */
1332typedef enum
1333{
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 */
46e4cb76 1338 sinbody /* in struct body: recognize member func defs*/
13fde0cd
RS
1339} STRUCTST;
1340STRUCTST structdef;
46c145db 1341
13fde0cd
RS
1342/*
1343 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
42680d3c
FP
1344 * struct tag, and structtype is the type of the preceding struct-like
1345 * keyword.
13fde0cd
RS
1346 */
1347char structtag[BUFSIZ];
42680d3c 1348enum sym_type structtype;
13fde0cd
RS
1349
1350/*
1351 * Yet another little state machine to deal with preprocessor lines.
1352 */
1353typedef enum
1354{
1355 dnone, /* nothing seen */
1356 dsharpseen, /* '#' seen as first char on line */
1357 ddefineseen, /* '#' and 'define' seen */
46e4cb76 1358 dignorerest /* ignore rest of line */
13fde0cd
RS
1359} DEFINEST;
1360DEFINEST definedef;
1361
1362/*
1363 * Set this to TRUE, and the next token considered is called a function.
1364 * Used only for GNUmacs's function-defining macros.
1365 */
1366logical next_token_is_func;
1367
1368/*
1369 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1370 */
1371logical yacc_rules;
1372
c6d46f5f
JB
1373/*
1374 * C_entries ()
13fde0cd
RS
1375 * This routine finds functions, typedefs, #define's and
1376 * struct/union/enum definitions in C syntax and adds them
c6d46f5f
JB
1377 * to the list.
1378 */
1379
13fde0cd
RS
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)
1386
b12756c8
FP
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)
1395
c6d46f5f 1396#define CNL_SAVE_DEFINEDEF \
13fde0cd
RS
1397do { \
1398 SET_FILEPOS (curlinepos, inf, charno); \
c6d46f5f 1399 lineno++; \
13fde0cd
RS
1400 charno += readline (&curlb, inf); \
1401 lp = curlb.buffer; \
1402 quotednl = FALSE; \
1403 newndx = curndx; \
1404} while (FALSE)
c6d46f5f
JB
1405
1406#define CNL \
13fde0cd 1407do { \
c6d46f5f 1408 CNL_SAVE_DEFINEDEF; \
b12756c8
FP
1409 if (TOKEN_SAVED_P) \
1410 RESTORE_TOKEN; \
c6d46f5f 1411 definedef = dnone; \
13fde0cd
RS
1412} while (FALSE)
1413
fe0b3356 1414#define MAKE_TAG_FROM_NEW_LB(isfun) pfnote (nameb, isfun, tok.named, \
13fde0cd 1415 newlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (newlinepos))
fe0b3356 1416#define MAKE_TAG_FROM_OTH_LB(isfun) pfnote (nameb, isfun, tok.named, \
13fde0cd 1417 othlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (othlinepos))
c6d46f5f
JB
1418
1419void
1420C_entries (c_ext)
1421 int c_ext; /* extension of C? */
1422{
13fde0cd 1423 register char c; /* latest char read; '\0' for end of line */
c6d46f5f 1424 register char *lp; /* pointer one beyond the character `c' */
13fde0cd
RS
1425 int curndx, newndx; /* indices for current and new lb */
1426 TOKEN tok; /* latest token read for funcdef & structdef */
fe0b3356 1427 char nameb[BUFSIZ]; /* latest token name for funcdef & structdef */
13fde0cd
RS
1428 register int tokoff; /* offset in line of start of latest token */
1429 register int toklen; /* length of latest token */
591fa824 1430 int cblev; /* current curly brace level */
b12756c8 1431 int parlev; /* current parenthesis level */
13fde0cd
RS
1432 logical incomm, inquote, inchar, quotednl, midtoken;
1433 logical cplpl;
b12756c8 1434 TOKEN savetok; /* saved token during preprocessor handling */
b12756c8 1435 char savenameb[BUFSIZ]; /* ouch! */
c6d46f5f 1436
b12756c8 1437 savetok.lineno = 0;
13fde0cd 1438 curndx = newndx = 0;
c6d46f5f
JB
1439 lineno = 0;
1440 charno = 0;
13fde0cd 1441 lp = curlb.buffer;
c6d46f5f
JB
1442 *lp = 0;
1443
46c145db 1444 definedef = dnone; funcdef = fnone; typdef = tnone; structdef = snone;
13fde0cd
RS
1445 next_token_is_func = yacc_rules = FALSE;
1446 midtoken = inquote = inchar = incomm = quotednl = FALSE;
591fa824 1447 cblev = 0;
b12756c8 1448 parlev = 0;
13fde0cd 1449 cplpl = c_ext & C_PLPL;
c6d46f5f 1450
c6d46f5f
JB
1451 while (!feof (inf))
1452 {
1453 c = *lp++;
c6d46f5f
JB
1454 if (c == '\\')
1455 {
4746118a
JB
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. */
13fde0cd 1459 if (*lp == '\0')
99e0a2e0 1460 {
13fde0cd 1461 quotednl = TRUE;
99e0a2e0
RS
1462 continue;
1463 }
1e134a5f 1464 lp++;
c6d46f5f
JB
1465 c = ' ';
1466 }
1467 else if (incomm)
1468 {
13fde0cd 1469 switch (c)
c6d46f5f 1470 {
13fde0cd
RS
1471 case '*':
1472 if (*lp == '/')
1473 {
1474 c = *lp++;
1475 incomm = FALSE;
1476 }
1477 break;
1478 case '\0':
1479 /* Newlines inside comments do not end macro definitions in
1480 traditional cpp. */
1481 CNL_SAVE_DEFINEDEF;
1482 break;
c6d46f5f 1483 }
13fde0cd 1484 continue;
c6d46f5f
JB
1485 }
1486 else if (inquote)
1487 {
13fde0cd
RS
1488 switch (c)
1489 {
1490 case '"':
1491 inquote = FALSE;
1492 break;
1493 case '\0':
42680d3c 1494 /* Newlines inside strings do not end macro definitions
13fde0cd
RS
1495 in traditional cpp, even though compilers don't
1496 usually accept them. */
1497 CNL_SAVE_DEFINEDEF;
1498 break;
1499 }
1500 continue;
c6d46f5f
JB
1501 }
1502 else if (inchar)
1503 {
42680d3c
FP
1504 switch (c)
1505 {
1506 case '\0':
1507 /* Hmmm, something went wrong. */
1508 CNL;
1509 /* FALLTHRU */
1510 case '\'':
46c145db 1511 inchar = FALSE;
42680d3c
FP
1512 break;
1513 }
c6d46f5f
JB
1514 continue;
1515 }
13fde0cd 1516 else
c6d46f5f
JB
1517 switch (c)
1518 {
1519 case '"':
1520 inquote = TRUE;
b12756c8
FP
1521 if (funcdef != finlist && funcdef != fignore)
1522 funcdef = fnone;
c6d46f5f
JB
1523 continue;
1524 case '\'':
1525 inchar = TRUE;
b12756c8
FP
1526 if (funcdef != finlist && funcdef != fignore)
1527 funcdef = fnone;
c6d46f5f
JB
1528 continue;
1529 case '/':
1530 if (*lp == '*')
1531 {
1532 lp++;
1533 incomm = TRUE;
13fde0cd 1534 continue;
c6d46f5f 1535 }
13fde0cd 1536 else if (cplpl && *lp == '/')
c6d46f5f 1537 {
daa37602
JB
1538 c = 0;
1539 break;
c6d46f5f 1540 }
b12756c8
FP
1541 else
1542 break;
13fde0cd
RS
1543 case '%':
1544 if ((c_ext & YACC) && *lp == '%')
1545 {
1546 /* entering or exiting rules section in yacc file */
1547 lp++;
1548 definedef = dnone; funcdef = fnone;
46c145db 1549 typdef = tnone; structdef = snone;
13fde0cd
RS
1550 next_token_is_func = FALSE;
1551 midtoken = inquote = inchar = incomm = quotednl = FALSE;
591fa824 1552 cblev = 0;
13fde0cd
RS
1553 yacc_rules = !yacc_rules;
1554 continue;
591fa824 1555 }
b12756c8
FP
1556 else
1557 break;
c6d46f5f 1558 case '#':
13fde0cd 1559 if (lp == newlb.buffer + 1 && definedef == dnone)
c6d46f5f
JB
1560 definedef = dsharpseen;
1561 continue;
13fde0cd 1562 } /* switch (c) */
c6d46f5f 1563
c6d46f5f 1564
591fa824
RS
1565 /* Consider token only if some complicated conditions are satisfied. */
1566 if (((cblev == 0 && structdef != scolonseen)
1567 || (cblev == 1 && cplpl && structdef == sinbody))
46c145db 1568 && typdef != tignore
13fde0cd 1569 && definedef != dignorerest
b12756c8 1570 && (funcdef != finlist
46c145db 1571 || definedef != dnone))
c6d46f5f
JB
1572 {
1573 if (midtoken)
1574 {
1575 if (endtoken (c))
1576 {
b12756c8 1577 if (cplpl && c == ':' && *lp == ':' && begtoken(*(lp + 1)))
c6d46f5f
JB
1578 {
1579 /*
1580 * This handles :: in the middle, but not at beginning
1581 * of an identifier.
1582 */
1583 lp += 2;
1584 toklen += 3;
1585 }
1586 else
1587 {
fe0b3356 1588 logical is_func = FALSE;
c6d46f5f 1589
13fde0cd
RS
1590 tok.lineno = lineno;
1591 tok.p = newlb.buffer + tokoff;
c6d46f5f 1592 tok.len = toklen;
fe0b3356 1593 tok.named = FALSE;
13fde0cd 1594 if (yacc_rules
42680d3c 1595 || consider_token (c, &tok, c_ext, cblev, &is_func))
c6d46f5f 1596 {
99e0a2e0 1597 if (structdef == sinbody
fe0b3356
FP
1598 && definedef == dnone
1599 && is_func)
1600 /* function defined in C++ class body */
1601 {
1602 tok.named = TRUE;
1603 sprintf (nameb, "%s::%.*s",
13fde0cd
RS
1604 ((structtag[0] == '\0')
1605 ? "_anonymous_" : structtag),
c6d46f5f 1606 tok.len, tok.p);
c6d46f5f
JB
1607 }
1608 else
1609 {
fe0b3356 1610 sprintf (nameb, "%.*s", tok.len, tok.p);
c6d46f5f 1611 }
13fde0cd 1612
fe0b3356
FP
1613 if (structdef == stagseen
1614 || typdef == tend)
1615 tok.named = TRUE;
1616
b12756c8
FP
1617 if (definedef == dnone
1618 && (funcdef == ftagseen
1619 || structdef == stagseen
1620 || typdef == tend))
13fde0cd
RS
1621 {
1622 if (newndx == curndx)
1623 curndx = 1 - curndx; /* switch line buffers */
1624 }
1625 else
1626 MAKE_TAG_FROM_NEW_LB (is_func);
c6d46f5f
JB
1627 }
1628 midtoken = FALSE;
1629 }
13fde0cd 1630 } /* if (endtoken (c)) */
c6d46f5f 1631 else if (intoken (c))
13fde0cd
RS
1632 {
1633 toklen++;
1634 continue;
1635 }
1636 } /* if (midtoken) */
c6d46f5f
JB
1637 else if (begtoken (c))
1638 {
b12756c8 1639 switch (definedef)
13fde0cd 1640 {
b12756c8
FP
1641 case dnone:
1642 switch (funcdef)
1643 {
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;
1654 }
1655 if (structdef == stagseen)
1656 structdef = snone;
13fde0cd 1657 break;
b12756c8
FP
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)))
1664 {
1665 SAVE_TOKEN;
1666 definedef = ddefineseen;
1667 lp += 6;
1668 }
1669 else
1670 definedef = dignorerest;
1671 continue;
13fde0cd 1672 }
13fde0cd
RS
1673 if (!yacc_rules || lp == newlb.buffer + 1)
1674 {
1675 tokoff = lp - 1 - newlb.buffer;
1676 toklen = 1;
1677 midtoken = TRUE;
1678 }
1679 continue;
c6d46f5f 1680 }
13fde0cd
RS
1681 } /* if must look at token */
1682
1683
1684 /* Detect end of line, colon, comma, semicolon and various braces
b12756c8 1685 after having handled a token.*/
13fde0cd 1686 switch (c)
1e134a5f 1687 {
13fde0cd 1688 case ':':
b12756c8
FP
1689 if (definedef != dnone)
1690 break;
13fde0cd
RS
1691 if (structdef == stagseen)
1692 structdef = scolonseen;
b12756c8
FP
1693 else
1694 switch (funcdef)
1695 {
1696 case ftagseen:
1697 if (yacc_rules)
1698 {
1699 MAKE_TAG_FROM_OTH_LB (FALSE);
1700 funcdef = fignore;
1701 }
1702 break;
1703 case fstartlist:
1704 funcdef = fnone;
1705 break;
1706 }
13fde0cd
RS
1707 break;
1708 case ';':
b12756c8
FP
1709 if (definedef != dnone)
1710 break;
46c145db
FP
1711 if (cblev == 0)
1712 switch (typdef)
1713 {
1714 case tend:
1715 MAKE_TAG_FROM_OTH_LB (FALSE);
1716 /* FALLTHRU */
1717 default:
1718 typdef = tnone;
1719 }
31d4b314
FP
1720 if (funcdef != fignore)
1721 funcdef = fnone;
46c145db
FP
1722 if (structdef == stagseen)
1723 structdef = snone;
1724 break;
13fde0cd 1725 case ',':
46c145db
FP
1726 if (definedef != dnone)
1727 break;
1728 if (funcdef != finlist && funcdef != fignore)
1729 funcdef = fnone;
1730 if (structdef == stagseen)
1731 structdef = snone;
1732 break;
13fde0cd 1733 case '[':
b12756c8
FP
1734 if (definedef != dnone)
1735 break;
46c145db
FP
1736 if (cblev == 0 && typdef == tend)
1737 {
1738 typdef = tignore;
1739 MAKE_TAG_FROM_OTH_LB (FALSE);
1740 break;
1741 }
31d4b314 1742 if (funcdef != finlist && funcdef != fignore)
13fde0cd
RS
1743 funcdef = fnone;
1744 if (structdef == stagseen)
1745 structdef = snone;
1746 break;
1747 case '(':
b12756c8
FP
1748 if (definedef != dnone)
1749 break;
13fde0cd 1750 switch (funcdef)
57e83cfe 1751 {
13fde0cd 1752 case ftagseen:
b12756c8 1753 funcdef = fstartlist;
13fde0cd 1754 break;
13fde0cd 1755 case flistseen:
b12756c8 1756 funcdef = finlist;
13fde0cd 1757 break;
57e83cfe 1758 }
b12756c8 1759 parlev++;
13fde0cd
RS
1760 break;
1761 case ')':
b12756c8
FP
1762 if (definedef != dnone)
1763 break;
1764 if (--parlev == 0)
1765 {
1766 switch (funcdef)
1767 {
1768 case fstartlist:
1769 case finlist:
1770 funcdef = flistseen;
1771 break;
1772 }
46c145db
FP
1773 if (cblev == 0 && typdef == tend)
1774 {
1775 typdef = tignore;
1776 MAKE_TAG_FROM_OTH_LB (FALSE);
1777 }
b12756c8
FP
1778 }
1779 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
1780 parlev = 0;
13fde0cd
RS
1781 break;
1782 case '{':
b12756c8
FP
1783 if (definedef != dnone)
1784 break;
13fde0cd
RS
1785 if (typdef == ttypedseen)
1786 typdef = tinbody;
1787 switch (structdef)
1788 {
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;
1798 }
31d4b314
FP
1799 switch (funcdef)
1800 {
1801 case flistseen:
1802 MAKE_TAG_FROM_OTH_LB (TRUE);
1803 /* FALLTHRU */
1804 case fignore:
1805 funcdef = fnone;
46c145db
FP
1806 break;
1807 case fnone:
1808 /* Neutralize `extern "C" {' grot.
1809 if (cblev == 0 && structdef == snone && typdef == tnone)
1810 cblev--; */;
31d4b314 1811 }
591fa824 1812 cblev++;
31d4b314 1813 break;
13fde0cd 1814 case '*':
b12756c8
FP
1815 if (definedef != dnone)
1816 break;
1817 if (funcdef == fstartlist)
1818 funcdef = fnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
13fde0cd
RS
1819 break;
1820 case '}':
b12756c8
FP
1821 if (definedef != dnone)
1822 break;
13fde0cd 1823 if (!noindentypedefs && lp == newlb.buffer + 1)
b12756c8
FP
1824 {
1825 cblev = 0; /* reset curly brace level if first column */
1826 parlev = 0; /* also reset paren level, just in case... */
1827 }
591fa824
RS
1828 else if (cblev > 0)
1829 cblev--;
1830 if (cblev == 0)
13fde0cd
RS
1831 {
1832 if (typdef == tinbody)
1833 typdef = tend;
1834 structdef = snone;
1a0d8c80 1835 strcpy (structtag, "<error 2>");
13fde0cd
RS
1836 }
1837 break;
b12756c8 1838 case '=':
42680d3c
FP
1839 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
1840 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
b12756c8
FP
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;
13fde0cd
RS
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) */
1855
1856 } /* while not eof */
c6d46f5f
JB
1857}
1858
1859/*
1860 * consider_token ()
1861 * checks to see if the current token is at the start of a
13fde0cd
RS
1862 * function, or corresponds to a typedef, or is a struct/union/enum
1863 * tag.
c6d46f5f 1864 *
13fde0cd 1865 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
c6d46f5f
JB
1866 * C_EXT is which language we are looking at.
1867 *
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'.
1872 *
1873 * Globals
13fde0cd
RS
1874 * funcdef IN OUT
1875 * structdef IN OUT
1876 * definedef IN OUT
1877 * typdef IN OUT
1878 * next_token_is_func IN OUT
c6d46f5f
JB
1879 */
1880
1881logical
42680d3c 1882consider_token (c, tokp, c_ext, cblev, is_func)
13fde0cd 1883 register char c; /* IN: first char after the token */
591fa824
RS
1884 register TOKEN *tokp; /* IN: token pointer */
1885 int c_ext; /* IN: C extensions mask */
1886 int cblev; /* IN: curly brace level */
13fde0cd 1887 logical *is_func; /* OUT */
c6d46f5f 1888{
42680d3c 1889 enum sym_type toktype = C_symtype(tokp->p, tokp->len, c_ext);
c6d46f5f 1890
c6d46f5f 1891 /*
13fde0cd 1892 * Advance the definedef state machine.
c6d46f5f
JB
1893 */
1894 switch (definedef)
1895 {
1896 case dnone:
1897 /* We're not on a preprocessor line. */
1898 break;
1899 case dsharpseen:
1900 if (toktype == st_C_define)
1901 {
1902 definedef = ddefineseen;
c6d46f5f
JB
1903 }
1904 else
1905 {
1906 definedef = dignorerest;
c6d46f5f 1907 }
13fde0cd 1908 return (FALSE);
c6d46f5f
JB
1909 case ddefineseen:
1910 /*
1911 * Make a tag for any macro.
c6d46f5f 1912 */
c6d46f5f 1913 definedef = dignorerest;
13fde0cd 1914 *is_func = (c == '(');
c6d46f5f 1915 if (!*is_func && !constantypedefs)
13fde0cd
RS
1916 return (FALSE);
1917 else
1918 return (TRUE);
c6d46f5f 1919 case dignorerest:
13fde0cd 1920 return (FALSE);
c6d46f5f 1921 default:
14c90d01 1922 error ("internal error: definedef value.", 0);
c6d46f5f
JB
1923 }
1924
1925 /*
13fde0cd 1926 * Now typedefs
c6d46f5f 1927 */
13fde0cd 1928 switch (typdef)
c6d46f5f 1929 {
13fde0cd
RS
1930 case tnone:
1931 if (toktype == st_C_typedef)
c6d46f5f 1932 {
13fde0cd
RS
1933 if (typedefs)
1934 typdef = ttypedseen;
46c145db 1935 funcdef = fnone;
13fde0cd 1936 return (FALSE);
c6d46f5f 1937 }
13fde0cd
RS
1938 break;
1939 case ttypedseen:
1940 switch (toktype)
c6d46f5f 1941 {
13fde0cd
RS
1942 case st_none:
1943 case st_C_typespec:
1944 typdef = tend;
1945 break;
1946 case st_C_struct:
1947 case st_C_enum:
1e134a5f 1948 break;
c6d46f5f 1949 }
13fde0cd 1950 /* Do not return here, so the structdef stuff has a chance. */
c6d46f5f 1951 break;
13fde0cd
RS
1952 case tend:
1953 switch (toktype)
c6d46f5f 1954 {
13fde0cd
RS
1955 case st_C_typespec:
1956 case st_C_struct:
1957 case st_C_enum:
1958 return (FALSE);
c6d46f5f 1959 }
13fde0cd 1960 return (TRUE);
c6d46f5f
JB
1961 }
1962
1963 /*
591fa824
RS
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.
c6d46f5f
JB
1968 *
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.
1972 *
13fde0cd
RS
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.
c6d46f5f
JB
1977 */
1978 switch (toktype)
1979 {
1980 case st_C_struct:
1981 case st_C_enum:
13fde0cd 1982 if (typdef == ttypedseen
591fa824 1983 || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
c6d46f5f
JB
1984 {
1985 structdef = skeyseen;
42680d3c 1986 structtype = toktype;
c6d46f5f 1987 }
13fde0cd 1988 return (FALSE);
c6d46f5f 1989 }
c6d46f5f
JB
1990 if (structdef == skeyseen)
1991 {
42680d3c 1992 if (structtype == st_C_struct)
c6d46f5f 1993 {
1a0d8c80 1994 strncpy (structtag, tokp->p, tokp->len);
13fde0cd 1995 structtag[tokp->len] = '\0'; /* for struct/union/class */
c6d46f5f
JB
1996 }
1997 else
1998 {
13fde0cd 1999 structtag[0] = '\0'; /* for enum (why is it treated differently?) */
c6d46f5f 2000 }
13fde0cd
RS
2001 structdef = stagseen;
2002 return (TRUE);
c6d46f5f 2003 }
13fde0cd
RS
2004
2005 /* Avoid entering funcdef stuff if typdef is going on. */
2006 if (typdef != tnone)
c6d46f5f 2007 {
13fde0cd
RS
2008 definedef = dnone;
2009 return (FALSE);
c6d46f5f 2010 }
13fde0cd 2011
c6d46f5f 2012 /* Detect GNUmacs's function-defining macros. */
4746118a 2013 if (definedef == dnone)
c6d46f5f 2014 {
daa37602
JB
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))
4746118a
JB
2019 {
2020 next_token_is_func = TRUE;
13fde0cd 2021 return (FALSE);
4746118a 2022 }
13fde0cd 2023 if (strneq (tokp->p, "EXFUN", 5))
4746118a
JB
2024 {
2025 next_token_is_func = FALSE;
13fde0cd 2026 return (FALSE);
4746118a 2027 }
c6d46f5f
JB
2028 }
2029 if (next_token_is_func)
2030 {
2031 next_token_is_func = FALSE;
31d4b314
FP
2032 funcdef = fnone;
2033 *is_func = TRUE; /* to force search string in ctags */
13fde0cd 2034 return (TRUE);
c6d46f5f 2035 }
13fde0cd
RS
2036
2037 /* A function? */
2038 switch (toktype)
c6d46f5f 2039 {
13fde0cd 2040 case st_C_typespec:
46c145db
FP
2041 if (funcdef != finlist && funcdef != fignore)
2042 funcdef = fnone; /* should be useless */
13fde0cd
RS
2043 return (FALSE);
2044 default:
31d4b314
FP
2045 if (funcdef == fnone)
2046 {
2047 funcdef = ftagseen;
2048 *is_func = TRUE;
2049 return (TRUE);
2050 }
c6d46f5f 2051 }
31d4b314
FP
2052
2053 return (FALSE);
c6d46f5f
JB
2054}
2055\f
2056/* Fortran parsing */
2057
2058char *dbp;
2059int pfcnt;
2060
2061int
2062PF_funcs (fi)
2063 FILE *fi;
2064{
2065 lineno = 0;
2066 charno = 0;
2067 pfcnt = 0;
2068
2069 while (!feof (fi))
2070 {
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 | ' ')
2082 {
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"))
2101 {
2102 while (isspace (*dbp))
2103 dbp++;
2104 if (*dbp == 0)
2105 continue;
2106 if (tail ("precision"))
2107 break;
2108 continue;
2109 }
2110 break;
2111 }
2112 while (isspace (*dbp))
2113 dbp++;
2114 if (*dbp == 0)
2115 continue;
2116 switch (*dbp | ' ')
2117 {
2118 case 'f':
2119 if (tail ("function"))
42680d3c 2120 getit (fi);
c6d46f5f
JB
2121 continue;
2122 case 's':
2123 if (tail ("subroutine"))
42680d3c 2124 getit (fi);
c6d46f5f 2125 continue;
8a6c8bcf
RS
2126 case 'e':
2127 if (tail ("entry"))
42680d3c 2128 getit (fi);
8a6c8bcf 2129 continue;
c6d46f5f
JB
2130 case 'p':
2131 if (tail ("program"))
2132 {
42680d3c 2133 getit (fi);
c6d46f5f
JB
2134 continue;
2135 }
2136 if (tail ("procedure"))
42680d3c 2137 getit (fi);
c6d46f5f
JB
2138 continue;
2139 }
2140 }
2141 return (pfcnt);
2142}
2143
2144logical
2145tail (cp)
2146 char *cp;
2147{
2148 register int len = 0;
2149
42680d3c 2150 while (*cp && (*cp | ' ') == (dbp[len] | ' '))
c6d46f5f
JB
2151 cp++, len++;
2152 if (*cp == 0)
2153 {
2154 dbp += len;
42680d3c 2155 return (TRUE);
c6d46f5f 2156 }
42680d3c 2157 return (FALSE);
c6d46f5f
JB
2158}
2159
2160void
2161takeprec ()
2162{
2163 while (isspace (*dbp))
2164 dbp++;
2165 if (*dbp != '*')
2166 return;
2167 dbp++;
2168 while (isspace (*dbp))
2169 dbp++;
2170 if (!isdigit (*dbp))
2171 {
2172 --dbp; /* force failure */
2173 return;
2174 }
2175 do
2176 dbp++;
2177 while (isdigit (*dbp));
2178}
2179
2180void
42680d3c
FP
2181getit (fi)
2182 FILE *fi;
c6d46f5f
JB
2183{
2184 register char *cp;
2185 char c;
2186 char nambuf[BUFSIZ];
2187
2188 while (isspace (*dbp))
2189 dbp++;
42680d3c
FP
2190 if (*dbp == '\0')
2191 {
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++;
2201 }
2202 if (!isalpha (*dbp)
46c145db 2203 && *dbp != '_'
42680d3c 2204 && *dbp != '$')
c6d46f5f 2205 return;
42680d3c
FP
2206 for (cp = dbp + 1;
2207 (*cp
2208 && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
2209 cp++)
c6d46f5f 2210 continue;
42680d3c
FP
2211 c = *cp;
2212 *cp = '\0';
1a0d8c80 2213 strcpy (nambuf, dbp);
42680d3c
FP
2214 *cp = c;
2215 pfnote (nambuf, TRUE, FALSE, lb.buffer,
2216 cp - lb.buffer + 1, lineno, linecharno);
c6d46f5f
JB
2217 pfcnt++;
2218}
2219
2220/* Handle a file of assembler code. */
2221
2222void
2223Asm_funcs (fi)
2224 FILE *fi;
2225{
2226 int i;
2227 register char c;
2228
2229 lineno = 0;
2230 charno = 0;
2231 pfcnt = 0;
2232
2233 while (!feof (fi))
2234 {
2235 lineno++;
2236 linecharno = charno;
2237 charno += readline (&lb, fi);
2238 dbp = lb.buffer;
2239
2240 for (i = 0; ((c = dbp[i]) && !isspace (c)) && (c != ':'); i++)
2241 ;
2242
2243 if ((i > 0) && (c == ':'))
42680d3c 2244 getit (fi);
c6d46f5f
JB
2245 }
2246}
2247\f
2248/* Added by Mosur Mohan, 4/22/88 */
2249/* Pascal parsing */
2250
2251#define GET_NEW_LINE \
2252{ \
2253 linecharno = charno; lineno++; \
2254 charno += 1 + readline (&lb, inf); \
2255 dbp = lb.buffer; \
2256}
2257
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.
2263 */
2264
2265void
2266PAS_funcs (fi)
2267 FILE *fi;
2268{
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];
2274
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 */
2287
2288 lineno = 0;
2289 charno = 0;
2290 dbp = lb.buffer;
2291 *dbp = 0;
2292 initbuffer (&tline);
2293
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 */
2299
2300 /* long main loop to get next char */
2301 while (!feof (fi))
2302 {
2303 c = *dbp++;
2304 if (c == 0) /* if end of line */
2305 {
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 */
2314 }
2315 if (incomm1) /* within { - } comments */
2316 {
2317 if (c == '}')
2318 incomm1 = FALSE;
2319 continue;
2320 }
2321 else if (incomm2) /* within (* - *) comments */
2322 {
2323 if (c == '*')
2324 {
2325 while ((c = *dbp++) == '*')
2326 continue;
2327 if (c == 0)
2328 GET_NEW_LINE;
2329 if (c == ')')
2330 incomm2 = FALSE;
2331 }
2332 continue;
2333 }
2334 else if (inquote)
2335 {
2336 if (c == '\'')
2337 inquote = FALSE;
2338 continue;
2339 }
2340 else
2341 switch (c)
2342 {
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 */
2351 {
2352 incomm2 = TRUE;
2353 dbp++;
2354 }
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 */
2364 {
2365 verify_tag = TRUE;
2366 break;
2367 }
2368 continue;
2369 }
2370 if ((found_tag) && (verify_tag) && (*dbp != ' '))
2371 {
2372 /* check if this is an "extern" declaration */
2373 if (*dbp == 0)
2374 continue;
2375 if ((*dbp == 'e') || (*dbp == 'E'))
2376 {
2377 if (tail ("extern")) /* superfluous, really! */
2378 {
2379 found_tag = FALSE;
2380 verify_tag = FALSE;
2381 }
2382 }
2383 else if ((*dbp == 'f') || (*dbp == 'F'))
2384 {
2385 if (tail ("forward")) /* check for forward reference */
2386 {
2387 found_tag = FALSE;
2388 verify_tag = FALSE;
2389 }
2390 }
46c145db 2391 if ((found_tag) && (verify_tag)) /* not external proc, so make tag */
c6d46f5f
JB
2392 {
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;
2399 }
2400 }
2401 if (get_tagname) /* grab name of proc or fn */
2402 {
2403 if (*dbp == 0)
2404 continue;
2405
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;
2411
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;
2423
2424 /* and proceed to check for "extern" */
2425 }
2426 if ((!incomm1) && (!incomm2) && (!inquote) &&
2427 (!found_tag) && (!get_tagname))
2428 {
2429 /* check for proc/fn keywords */
2430 switch (c | ' ')
2431 {
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;
2440 }
2441 }
2442 } /* while not e-o-f */
2443}
2444\f
2445/*
2446 * lisp tag functions
2447 * just look for (def or (DEF
2448 */
2449
2450void
2451L_funcs (fi)
2452 FILE *fi;
2453{
2454 lineno = 0;
2455 charno = 0;
2456 pfcnt = 0;
2457
2458 while (!feof (fi))
2459 {
2460 lineno++;
2461 linecharno = charno;
2462 charno += readline (&lb, fi);
2463 dbp = lb.buffer;
2464 if (dbp[0] == '(')
2465 {
2466 if (L_isdef (dbp))
2467 {
2468 while (!isspace (*dbp))
2469 dbp++;
2470 while (isspace (*dbp))
2471 dbp++;
2472 L_getit ();
2473 }
2474 else
2475 {
2476 /* Check for (foo::defmumble name-defined ... */
31d4b314 2477 do
c6d46f5f 2478 dbp++;
31d4b314
FP
2479 while (*dbp && !isspace (*dbp)
2480 && *dbp != ':' && *dbp != '(' && *dbp != ')');
c6d46f5f
JB
2481 if (*dbp == ':')
2482 {
31d4b314 2483 do
c6d46f5f 2484 dbp++;
31d4b314 2485 while (*dbp == ':');
c6d46f5f 2486
31d4b314 2487 if (L_isdef (dbp - 1))
c6d46f5f
JB
2488 {
2489 while (!isspace (*dbp))
2490 dbp++;
2491 while (isspace (*dbp))
2492 dbp++;
2493 L_getit ();
2494 }
2495 }
2496 }
2497 }
2498 }
2499}
2500
2501int
2502L_isdef (dbp)
31d4b314 2503 register char *dbp;
c6d46f5f 2504{
31d4b314
FP
2505 return ((dbp[1] == 'd' || dbp[1] == 'D')
2506 && (dbp[2] == 'e' || dbp[2] == 'E')
2507 && (dbp[3] == 'f' || dbp[3] == 'F'));
2508}
2509
2510int
2511L_isquote (dbp)
2512 register char *dbp;
2513{
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)));
c6d46f5f
JB
2520}
2521
2522void
2523L_getit ()
2524{
2525 register char *cp;
2526 char c;
2527 char nambuf[BUFSIZ];
2528
31d4b314
FP
2529 if (*dbp == '\'') /* Skip prefix quote */
2530 dbp++;
2531 else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
2532 {
2533 dbp += 7;
2534 while (isspace(*dbp))
2535 dbp++;
2536 }
2537 for (cp = dbp /*+1*/; *cp && *cp != '(' && *cp != ' ' && *cp != ')'; cp++)
c6d46f5f 2538 continue;
31d4b314
FP
2539 if (cp == dbp)
2540 return;
2541
c6d46f5f
JB
2542 c = cp[0];
2543 cp[0] = 0;
1a0d8c80 2544 strcpy (nambuf, dbp);
c6d46f5f 2545 cp[0] = c;
591fa824
RS
2546 pfnote (nambuf, TRUE, FALSE, lb.buffer,
2547 cp - lb.buffer + 1, lineno, linecharno);
c6d46f5f
JB
2548 pfcnt++;
2549}
2550\f
2551/*
2552 * Scheme tag functions
2553 * look for (def... xyzzy
2554 * look for (def... (xyzzy
2555 * look for (def ... ((...(xyzzy ....
2556 * look for (set! xyzzy
2557 */
2558
2559static void get_scheme ();
2560
2561void
2562Scheme_funcs (fi)
2563 FILE *fi;
2564{
2565 lineno = 0;
2566 charno = 0;
2567 pfcnt = 0;
2568
2569 while (!feof (fi))
2570 {
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'))
2579 {
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 ();
2586 }
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])))
2593 {
2594 while (!isspace (*dbp))
2595 dbp++;
2596 /* Skip over white space */
2597 while (isspace (*dbp))
2598 dbp++;
2599 get_scheme ();
2600 }
2601 }
2602}
2603
2604static void
2605get_scheme ()
2606{
2607 register char *cp;
2608 char c;
2609 char nambuf[BUFSIZ];
2610
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++;
2626}
2627\f
2628/* Find tags in TeX and LaTeX input files. */
2629
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!! */
2633
2634struct TEX_tabent
2635{
2636 char *name;
2637 int len;
2638};
2639
2640struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
2641
2642/* Default set of control sequences to put into TEX_toktab.
2643 The value of environment var TEXTAGS is prepended to this. */
2644
2645static char *TEX_defenv =
2646":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2647
2648void TEX_mode ();
2649struct TEX_tabent *TEX_decode_env ();
2650void TEX_getit ();
2651int TEX_Token ();
2652
2653static char TEX_esc = '\\';
2654static char TEX_opgrp = '{';
2655static char TEX_clgrp = '}';
2656
2657/*
2658 * TeX/LaTeX scanning loop.
2659 */
2660
2661void
2662TEX_funcs (fi)
2663 FILE *fi;
2664{
2665 char *lasthit;
2666
2667 lineno = 0;
2668 charno = 0;
2669 pfcnt = 0;
2670
2671 /* Select either \ or ! as escape character. */
2672 TEX_mode (fi);
2673
2674 /* Initialize token table once from environment. */
2675 if (!TEX_toktab)
2676 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
2677
2678 while (!feof (fi))
d2729198 2679 { /* Scan each line in file */
c6d46f5f
JB
2680 lineno++;
2681 linecharno = charno;
2682 charno += readline (&lb, fi);
2683 dbp = lb.buffer;
2684 lasthit = dbp;
d2729198 2685 while (dbp = etags_index (dbp, TEX_esc)) /* Look at each escape in line */
8a6c8bcf
RS
2686 {
2687 register int i;
c6d46f5f 2688
8a6c8bcf
RS
2689 if (!*(++dbp))
2690 break;
2691 linecharno += dbp - lasthit;
c6d46f5f 2692 lasthit = dbp;
8a6c8bcf
RS
2693 i = TEX_Token (lasthit);
2694 if (0 <= i)
c6d46f5f 2695 {
8a6c8bcf 2696 TEX_getit (lasthit, TEX_toktab[i].len);
d2729198 2697 break; /* We only save a line once */
c6d46f5f
JB
2698 }
2699 }
2700 }
2701}
2702
2703#define TEX_LESC '\\'
2704#define TEX_SESC '!'
2705#define TEX_cmt '%'
2706
2707/* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
2708/* chars accordingly. */
2709
2710void
2711TEX_mode (f)
2712 FILE *f;
2713{
2714 int c;
2715
2716 while ((c = getc (f)) != EOF)
2717 {
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;
2724 }
2725
2726 if (c == TEX_LESC)
2727 {
2728 TEX_esc = TEX_LESC;
2729 TEX_opgrp = '{';
2730 TEX_clgrp = '}';
2731 }
2732 else
2733 {
2734 TEX_esc = TEX_SESC;
2735 TEX_opgrp = '<';
2736 TEX_clgrp = '>';
2737 }
2738 rewind (f);
2739}
2740
2741/* Read environment and prepend it to the default string. */
2742/* Build token table. */
2743
2744struct TEX_tabent *
2745TEX_decode_env (evarname, defenv)
2746 char *evarname;
2747 char *defenv;
2748{
2749 register char *env, *p;
c6d46f5f
JB
2750
2751 struct TEX_tabent *tab;
2752 int size, i;
2753
2754 /* Append default string to environment. */
2755 env = getenv (evarname);
2756 if (!env)
2757 env = defenv;
2758 else
2759 env = concat (env, defenv, "");
2760
2761 /* Allocate a token table */
2762 for (size = 1, p = env; p;)
8a6c8bcf 2763 if ((p = etags_index (p, ':')) && *(++p))
c6d46f5f 2764 size++;
8a6c8bcf
RS
2765 /* Add 1 to leave room for null terminator. */
2766 tab = xnew (size + 1, struct TEX_tabent);
c6d46f5f
JB
2767
2768 /* Unpack environment string into token table. Be careful about */
2769 /* zero-length strings (leading ':', "::" and trailing ':') */
2770 for (i = 0; *env;)
2771 {
8a6c8bcf 2772 p = etags_index (env, ':');
c6d46f5f
JB
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++;
2780 }
2781 if (*p)
2782 env = p + 1;
2783 else
2784 {
2785 tab[i].name = NULL; /* Mark end of table. */
2786 tab[i].len = 0;
2787 break;
2788 }
2789 }
2790 return tab;
2791}
2792
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. */
2796
2797void
2798TEX_getit (name, len)
2799 char *name;
2800 int len;
2801{
2802 char *p = name + len;
2803 char nambuf[BUFSIZ];
2804
2805 if (*name == 0)
2806 return;
2807
2808 /* Let tag name extend to next group close (or end of line) */
2809 while (*p && *p != TEX_clgrp)
2810 p++;
1a0d8c80 2811 strncpy (nambuf, name, p - name);
c6d46f5f
JB
2812 nambuf[p - name] = 0;
2813
2814 pfnote (nambuf, TRUE, FALSE, lb.buffer, strlen (lb.buffer), lineno, linecharno);
2815 pfcnt++;
2816}
2817
2818/* If the text at CP matches one of the tag-defining TeX command names,
8a6c8bcf 2819 return the etags_index of that command in TEX_toktab.
c6d46f5f
JB
2820 Otherwise return -1. */
2821
2822/* Keep the capital `T' in `Token' for dumb truncating compilers
2823 (this distinguishes it from `TEX_toktab' */
2824int
2825TEX_Token (cp)
2826 char *cp;
2827{
2828 int i;
2829
2830 for (i = 0; TEX_toktab[i].len > 0; i++)
1a0d8c80 2831 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
c6d46f5f
JB
2832 return i;
2833 return -1;
2834}
2835\f
2836/* Support for Prolog. */
2837
2838/* whole head (not only functor, but also arguments)
2839 is gotten in compound term. */
2840
2841void
2842prolog_getit (s, lineno, linecharno)
2843 char *s;
2844 int lineno;
2845 long linecharno;
2846{
2847 char nambuf[BUFSIZ], *save_s, tmpc;
2848 int insquote, npar;
2849
2850 save_s = s;
2851 insquote = FALSE;
2852 npar = 0;
2853 while (1)
2854 {
2855 if (*s == '\0') /* syntax error. */
2856 return;
2857 else if (insquote && *s == '\'' && *(s + 1) == '\'')
2858 s += 2;
2859 else if (*s == '\'')
2860 {
2861 insquote = !insquote;
2862 s++;
2863 }
2864 else if (!insquote && *s == '(')
2865 {
2866 npar++;
2867 s++;
2868 }
2869 else if (!insquote && *s == ')')
2870 {
2871 npar--;
2872 s++;
2873 if (npar == 0)
2874 break;
2875 else if (npar < 0) /* syntax error. */
2876 return;
2877 }
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;
2884 }
2885 else
2886 s++;
2887 }
2888 tmpc = *s;
2889 *s = '\0';
2890 strcpy (nambuf, save_s);
2891 *s = tmpc;
bff8edcc 2892 pfnote (nambuf, TRUE, FALSE, save_s, strlen (nambuf), lineno, linecharno);
c6d46f5f
JB
2893}
2894
2895/* It is assumed that prolog predicate starts from column 0. */
2896
2897void
2898prolog_funcs (fi)
2899 FILE *fi;
2900{
2901 void skip_comment (), prolog_getit ();
2902
2903 lineno = linecharno = charno = 0;
2904 while (!feof (fi))
2905 {
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);
2918 }
2919}
2920
2921void
2922skip_comment (plb, fi, plineno, plinecharno)
2923 struct linebuffer *plb;
2924 FILE *fi;
2925 int *plineno; /* result */
2926 long *plinecharno; /* result */
2927{
2928 while (!substr ("*/", plb->buffer))
2929 {
2930 (*plineno)++;
2931 *plinecharno += readline (plb, fi) + 1;
2932 } /* 1 for newline. */
2933}
2934
2935/* Return TRUE if 'sub' exists somewhere in 's'. */
2936
2937int
2938substr (sub, s)
2939 char *sub;
2940 char *s;
2941{
8a6c8bcf 2942 while (*s && (s = etags_index (s, *sub)))
c6d46f5f
JB
2943 if (prestr (sub, s))
2944 return (TRUE);
2945 else
2946 s++;
2947 return (FALSE);
2948}
2949
2950/* Return TRUE if 'pre' is prefix of string 's'. */
2951
2952int
2953prestr (pre, s)
2954 char *pre;
2955 char *s;
2956{
2957 if (*pre == '\0')
2958 return (TRUE);
2959 else if (*pre == *s)
2960 return (prestr (pre + 1, s + 1));
2961 else
2962 return (FALSE);
2963}
2964\f
2965/* Initialize a linebuffer for use */
2966
2967void
2968initbuffer (linebuffer)
2969 struct linebuffer *linebuffer;
2970{
2971 linebuffer->size = 200;
2972 linebuffer->buffer = xnew (200, char);
2973}
2974
2975/*
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.
2979 */
2980long
2981readline (linebuffer, stream)
2982 struct linebuffer *linebuffer;
2983 register FILE *stream;
2984{
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 */
2989
eb8c3be9 2990 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
c6d46f5f
JB
2991
2992 while (1)
2993 {
2994 register int c = getc (stream);
2995 if (p == pend)
2996 {
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;
3002 }
42680d3c 3003 if (c == EOF || c == '\n')
c6d46f5f
JB
3004 {
3005 *p = 0;
42680d3c 3006 newline = (c == '\n') ? 1 : 0;
c6d46f5f
JB
3007 break;
3008 }
3009 *p++ = c;
3010 }
3011
3012 return p - buffer + newline;
3013}
3014\f
3015char *
3016savestr (cp)
3017 char *cp;
3018{
3019 return savenstr (cp, strlen (cp));
3020}
3021
3022char *
3023savenstr (cp, len)
3024 char *cp;
3025 int len;
3026{
3027 register char *dp;
3028
3029 dp = xnew (len + 1, char);
1a0d8c80 3030 strncpy (dp, cp, len);
c6d46f5f
JB
3031 dp[len] = '\0';
3032 return dp;
3033}
3034
c6d46f5f
JB
3035/*
3036 * Return the ptr in sp at which the character c last
3037 * appears; NULL if not found
3038 *
3039 * Identical to v7 rindex, included for portability.
3040 */
3041
3042char *
8a6c8bcf 3043etags_rindex (sp, c)
c6d46f5f
JB
3044 register char *sp, c;
3045{
3046 register char *r;
3047
3048 r = NULL;
3049 do
3050 {
3051 if (*sp == c)
3052 r = sp;
3053 } while (*sp++);
3054 return (r);
3055}
3056
9d7ad1b3 3057
c6d46f5f
JB
3058/*
3059 * Return the ptr in sp at which the character c first
3060 * appears; NULL if not found
3061 *
3062 * Identical to v7 index, included for portability.
3063 */
3064
3065char *
8a6c8bcf 3066etags_index (sp, c)
c6d46f5f
JB
3067 register char *sp, c;
3068{
3069 do
3070 {
3071 if (*sp == c)
3072 return (sp);
3073 } while (*sp++);
3074 return (NULL);
3075}
3076
c6d46f5f
JB
3077/* Print error message and exit. */
3078
3079/* VARARGS1 */
3080void
3081fatal (s1, s2)
3082 char *s1, *s2;
3083{
3084 error (s1, s2);
1a0d8c80 3085 exit (BAD);
c6d46f5f
JB
3086}
3087
3088/* Print error message. `s1' is printf control string, `s2' is arg for it. */
3089
3090/* VARARGS1 */
3091void
3092error (s1, s2)
3093 char *s1, *s2;
3094{
3095 fprintf (stderr, "%s: ", progname);
3096 fprintf (stderr, s1, s2);
3097 fprintf (stderr, "\n");
3098}
3099
46c145db
FP
3100/* Return a newly-allocated string whose contents
3101 concatenate those of s1, s2, s3. */
c6d46f5f
JB
3102
3103char *
3104concat (s1, s2, s3)
3105 char *s1, *s2, *s3;
3106{
3107 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3108 char *result = xnew (len1 + len2 + len3 + 1, char);
3109
1a0d8c80
FP
3110 strcpy (result, s1);
3111 strcpy (result + len1, s2);
3112 strcpy (result + len1 + len2, s3);
46c145db 3113 result[len1 + len2 + len3] = '\0';
c6d46f5f
JB
3114
3115 return result;
3116}
3117
46c145db
FP
3118/* Return a newly allocated string containing the filename of FILE relative
3119 to the absolute directory DIR (which should end with a slash). */
3120
3121char *
3122relative_filename (file, dir)
3123 char *file, *dir;
3124{
3125 char *fp, *dp, *res;
3126
3127 /* Find the common root of file and dir. */
3128 fp = absolute_filename (file, cwd);
3129 dp = dir;
3130 while (*fp++ == *dp++)
3131 continue;
3132 do
3133 {
3134 fp--;
3135 dp--;
3136 }
3137 while (*fp != '/');
3138
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, '/'))
3143 {
3144 res = concat (res, "../", "");
3145 }
3146
3147 /* Add the filename relative to the common root of file and dir. */
3148 res = concat (res, fp + 1, "");
3149
3150 return res; /* temporary stub */
3151}
3152
3153/* Return a newly allocated string containing the
3154 absolute filename of FILE given CWD (which should end with a slash). */
3155char *
3156absolute_filename (file, cwd)
3157 char *file, *cwd;
3158{
3159 char *slashp, *cp, *res;
3160
3161 if (file[0] == '/')
3162 res = concat (file, "", "");
3163 else
3164 res = concat (cwd, file, "");
3165
3166 /* Delete the "/dirname/.." and "/." substrings. */
3167 slashp = etags_index (res, '/');
3168 while (slashp != NULL && slashp[0] != '\0')
3169 {
3170 if (slashp[1] == '.')
3171 {
3172 if (slashp[2] == '.'
3173 && (slashp[3] == '/' || slashp[3] == '\0'))
3174 {
3175 cp = slashp;
3176 do
3177 cp--;
3178 while (cp >= res && *cp != '/');
3179 if (*cp == '/')
3180 {
3181 strcpy (cp, slashp + 3);
3182 }
3183 else /* else (cp == res) */
3184 {
3185 if (slashp[3] != NULL)
3186 strcpy (cp, slashp + 4);
3187 else
3188 return ".";
3189 }
3190 slashp = cp;
3191 }
3192 else if (slashp[2] == '/' || slashp[2] == '\0')
3193 {
3194 strcpy (slashp, slashp + 2);
3195 }
3196 }
3197 else
3198 {
3199 slashp = etags_index (slashp + 1, '/');
3200 }
3201 }
3202
3203 return res;
3204}
3205
3206/* Return a newly allocated string containing the absolute filename
3207 of dir where FILE resides given CWD (which should end with a slash). */
3208char *
3209absolute_dirname (file, cwd)
3210 char *file, *cwd;
3211{
3212 char *slashp, *res;
3213 char save;
3214
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;
3222
3223 return res;
3224}
3225
c6d46f5f
JB
3226/* Like malloc but get fatal error if memory is exhausted. */
3227
3228char *
3229xmalloc (size)
42680d3c 3230 unsigned int size;
c6d46f5f 3231{
1a0d8c80
FP
3232 char *result = (char *) malloc (size);
3233 if (result == NULL)
c6d46f5f
JB
3234 fatal ("virtual memory exhausted", 0);
3235 return result;
3236}
3237
3238char *
3239xrealloc (ptr, size)
3240 char *ptr;
42680d3c 3241 unsigned int size;
c6d46f5f 3242{
1a0d8c80
FP
3243 char *result = (char *) realloc (ptr, size);
3244 if (result == NULL)
c6d46f5f
JB
3245 fatal ("virtual memory exhausted");
3246 return result;
3247}