(finder-compile-keywords-make-dist): New function.
[emacs.git] / lib-src / etags.c
blob213b959f6751396d3fe09c4be1214d752c7633b5
1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 87, 88, 89, 93, 94, 95
3 Free Software Foundation, Inc. and Ken Arnold
5 This file is not considered part of GNU Emacs.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 * Authors:
23 * Ctags originally by Ken Arnold.
24 * Fortran added by Jim Kleckner.
25 * Ed Pelegri-Llopart added C typedefs.
26 * Gnu Emacs TAGS format and modifications by RMS?
27 * Sam Kendall added C++.
28 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
29 * Regexp tags by Tom Tromey.
31 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
34 char pot_etags_version[] = "@(#) pot revision number is 11.53";
36 #define TRUE 1
37 #define FALSE 0
39 #ifndef DEBUG
40 # define DEBUG FALSE
41 #endif
43 #ifdef MSDOS
44 #include <fcntl.h>
45 #include <sys/param.h>
46 #endif /* MSDOS */
48 #ifdef WINDOWSNT
49 #include <stdlib.h>
50 #include <fcntl.h>
51 #include <string.h>
52 #define MAXPATHLEN _MAX_PATH
53 #endif
55 #ifdef HAVE_CONFIG_H
56 #include <config.h>
57 /* On some systems, Emacs defines static as nothing for the sake
58 of unexec. We don't want that here since we don't use unexec. */
59 #undef static
60 #endif
62 #include <stdio.h>
63 #include <ctype.h>
64 #include <errno.h>
65 #ifndef errno
66 extern int errno;
67 #endif
68 #include <sys/types.h>
69 #include <sys/stat.h>
71 #if !defined (S_ISREG) && defined (S_IFREG)
72 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
73 #endif
75 #include <getopt.h>
77 #ifdef ETAGS_REGEXPS
78 #include <regex.h>
79 #endif /* ETAGS_REGEXPS */
81 /* Define CTAGS to make the program "ctags" compatible with the usual one.
82 Let it undefined to make the program "etags", which makes emacs-style
83 tag tables and tags typedefs, #defines and struct/union/enum by default. */
84 #ifdef CTAGS
85 # undef CTAGS
86 # define CTAGS TRUE
87 #else
88 # define CTAGS FALSE
89 #endif
91 /* Exit codes for success and failure. */
92 #ifdef VMS
93 #define GOOD 1
94 #define BAD 0
95 #else
96 #define GOOD 0
97 #define BAD 1
98 #endif
100 /* C extensions. */
101 #define C_PLPL 0x00001 /* C++ */
102 #define C_STAR 0x00003 /* C* */
103 #define YACC 0x10000 /* yacc file */
105 #define streq(s,t) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strcmp(s,t))
106 #define strneq(s,t,n) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strncmp(s,t,n))
108 #define lowcase(c) tolower ((unsigned char)c)
110 #define iswhite(arg) (_wht[arg]) /* T if char is white */
111 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
112 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
113 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
115 #ifdef DOS_NT
116 # define absolutefn(fn) (fn[0] == '/' || (isalpha (fn[0]) && fn[1] == ':'))
117 #else
118 # define absolutefn(fn) (fn[0] == '/')
119 #endif
123 * xnew -- allocate storage
125 * SYNOPSIS: Type *xnew (int n, Type);
127 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
129 typedef int logical;
131 typedef struct nd_st
132 { /* sorting structure */
133 char *name; /* function or type name */
134 char *file; /* file name */
135 logical is_func; /* use pattern or line no */
136 logical been_warned; /* set if noticed dup */
137 int lno; /* line number tag is on */
138 long cno; /* character number line starts on */
139 char *pat; /* search pattern */
140 struct nd_st *left, *right; /* left and right sons */
141 } NODE;
143 extern char *getenv ();
145 char *concat ();
146 char *savenstr (), *savestr ();
147 char *etags_strchr (), *etags_strrchr ();
148 char *etags_getcwd ();
149 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
150 long *xmalloc (), *xrealloc ();
152 typedef void Lang_function ();
153 #if FALSE /* many compilers barf on this */
154 Lang_function Asm_labels;
155 Lang_function default_C_entries;
156 Lang_function C_entries;
157 Lang_function Cplusplus_entries;
158 Lang_function Cstar_entries;
159 Lang_function Fortran_functions;
160 Lang_function Yacc_entries;
161 Lang_function Lisp_functions;
162 Lang_function Pascal_functions;
163 Lang_function Perl_functions;
164 Lang_function Prolog_functions;
165 Lang_function Scheme_functions;
166 Lang_function TeX_functions;
167 Lang_function just_read_file;
168 #else /* so let's write it this way */
169 void Asm_labels ();
170 void C_entries ();
171 void default_C_entries ();
172 void plain_C_entries ();
173 void Cplusplus_entries ();
174 void Cstar_entries ();
175 void Fortran_functions ();
176 void Yacc_entries ();
177 void Lisp_functions ();
178 void Pascal_functions ();
179 void Perl_functions ();
180 void Prolog_functions ();
181 void Scheme_functions ();
182 void TeX_functions ();
183 void just_read_file ();
184 #endif
186 Lang_function *get_language_from_name ();
187 Lang_function *get_language_from_interpreter ();
188 Lang_function *get_language_from_suffix ();
189 int total_size_of_entries ();
190 long readline ();
191 long readline_internal ();
192 #ifdef ETAGS_REGEXPS
193 void add_regex ();
194 #endif
195 void add_node ();
196 void error ();
197 void suggest_asking_for_help ();
198 void fatal (), pfatal ();
199 void find_entries ();
200 void free_tree ();
201 void getit ();
202 void init ();
203 void initbuffer ();
204 void pfnote ();
205 void process_file ();
206 void put_entries ();
207 void takeprec ();
210 char searchar = '/'; /* use /.../ searches */
212 int lineno; /* line number of current line */
213 long charno; /* current character number */
215 long linecharno; /* charno of start of line; not used by C,
216 but by every other language. */
218 char *curfile; /* current input file name */
219 char *tagfile; /* output file */
220 char *progname; /* name this program was invoked with */
221 char *cwd; /* current working directory */
222 char *tagfiledir; /* directory of tagfile */
224 FILE *tagf; /* ioptr for tags file */
225 NODE *head; /* the head of the binary tree of tags */
228 * A `struct linebuffer' is a structure which holds a line of text.
229 * `readline' reads a line from a stream into a linebuffer and works
230 * regardless of the length of the line.
232 #define GROW_LINEBUFFER(buf,toksize) \
233 while (buf.size < toksize) \
234 buf.buffer = (char *) xrealloc (buf.buffer, buf.size *= 2)
235 struct linebuffer
237 long size;
238 char *buffer;
241 struct linebuffer lb; /* the current line */
242 struct linebuffer token_name; /* used by C_entries as a temporary area */
243 struct
245 long linepos;
246 struct linebuffer lb; /* used by C_entries instead of lb */
247 } lbs[2];
249 /* boolean "functions" (see init) */
250 logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
251 char
252 /* white chars */
253 *white = " \f\t\n\013",
254 /* token ending chars */
255 *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
256 /* token starting chars */
257 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
258 /* valid in-token chars */
259 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
261 logical append_to_tagfile; /* -a: append to tags */
262 /* The following three default to TRUE for etags, but to FALSE for ctags. */
263 logical typedefs; /* -t: create tags for typedefs */
264 logical typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
265 /* 0 struct/enum/union decls, and C++ */
266 /* member functions. */
267 logical constantypedefs; /* -d: create tags for C #define and enum */
268 /* constants. Enum consts not implemented. */
269 /* -D: opposite of -d. Default under ctags. */
270 logical update; /* -u: update tags */
271 logical vgrind_style; /* -v: create vgrind style index output */
272 logical no_warnings; /* -w: suppress warnings */
273 logical cxref_style; /* -x: create cxref style output */
274 logical cplusplus; /* .[hc] means C++, not C */
275 logical noindentypedefs; /* -I: ignore indentation in C */
277 struct option longopts[] =
279 { "append", no_argument, NULL, 'a' },
280 { "backward-search", no_argument, NULL, 'B' },
281 { "c++", no_argument, NULL, 'C' },
282 { "cxref", no_argument, NULL, 'x' },
283 { "defines", no_argument, NULL, 'd' },
284 { "help", no_argument, NULL, 'h' },
285 { "help", no_argument, NULL, 'H' },
286 { "ignore-indentation", no_argument, NULL, 'I' },
287 { "include", required_argument, NULL, 'i' },
288 { "language", required_argument, NULL, 'l' },
289 { "no-defines", no_argument, NULL, 'D' },
290 { "no-regex", no_argument, NULL, 'R' },
291 { "no-warn", no_argument, NULL, 'w' },
292 { "output", required_argument, NULL, 'o' },
293 { "regex", required_argument, NULL, 'r' },
294 { "typedefs", no_argument, NULL, 't' },
295 { "typedefs-and-c++", no_argument, NULL, 'T' },
296 { "update", no_argument, NULL, 'u' },
297 { "version", no_argument, NULL, 'V' },
298 { "vgrind", no_argument, NULL, 'v' },
299 { 0 }
302 #ifdef ETAGS_REGEXPS
303 /* Structure defining a regular expression. Elements are
304 the compiled pattern, and the name string. */
305 struct pattern
307 struct re_pattern_buffer *pattern;
308 struct re_registers regs;
309 char *name_pattern;
310 logical error_signaled;
313 /* Number of regexps found. */
314 int num_patterns = 0;
316 /* Array of all regexps. */
317 struct pattern *patterns = NULL;
318 #endif /* ETAGS_REGEXPS */
321 * Language stuff.
324 /* Non-NULL if language fixed. */
325 Lang_function *lang_func = NULL;
327 /* Assembly code */
328 char *Asm_suffixes [] = { "a", /* Unix assembler */
329 "asm", /* Microcontroller assembly */
330 "def", /* BSO/Tasking definition includes */
331 "inc", /* Microcontroller include files */
332 "ins", /* Microcontroller include files */
333 "s", "sa", /* Unix assembler */
334 "src", /* BSO/Tasking C compiler output */
335 NULL
338 /* Note that .c and .h can be considered C++, if the --c++ flag was
339 given. That is why default_C_entries is called here. */
340 char *default_C_suffixes [] =
341 { "c", "h", NULL };
343 /* .M is for Objective C++ files. */
344 char *Cplusplus_suffixes [] =
345 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL};
347 char *Cstar_suffixes [] =
348 { "cs", "hs", NULL };
350 char *Fortran_suffixes [] =
351 { "F", "f", "f90", "for", NULL };
353 char *Lisp_suffixes [] =
354 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL };
356 char *Pascal_suffixes [] =
357 { "p", "pas", NULL };
359 char *Perl_suffixes [] =
360 { "pl", "pm", NULL };
361 char *Perl_interpreters [] =
362 { "perl", "@PERL@", NULL };
364 char *plain_C_suffixes [] =
365 { "pc", /* Pro*C file */
366 "m", /* Objective C file */
367 "lm", /* Objective lex file */
368 NULL };
370 char *Prolog_suffixes [] =
371 { "prolog", NULL };
373 /* Can't do the `SCM' or `scm' prefix with a version number. */
374 char *Scheme_suffixes [] =
375 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL };
377 char *TeX_suffixes [] =
378 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL };
380 char *Yacc_suffixes [] =
381 { "y", "ym", NULL }; /* .ym is Objective yacc file */
383 /* Table of language names and corresponding functions, file suffixes
384 and interpreter names.
385 It is ok for a given function to be listed under more than one
386 name. I just didn't. */
387 struct lang_entry
389 char *name;
390 Lang_function *function;
391 char **suffixes;
392 char **interpreters;
395 struct lang_entry lang_names [] =
397 { "asm", Asm_labels, Asm_suffixes, NULL },
398 { "c", default_C_entries, default_C_suffixes, NULL },
399 { "c++", Cplusplus_entries, Cplusplus_suffixes, NULL },
400 { "c*", Cstar_entries, Cstar_suffixes, NULL },
401 { "fortran", Fortran_functions, Fortran_suffixes, NULL },
402 { "lisp", Lisp_functions, Lisp_suffixes, NULL },
403 { "pascal", Pascal_functions, Pascal_suffixes, NULL },
404 { "perl", Perl_functions, Perl_suffixes, Perl_interpreters },
405 { "proc", plain_C_entries, plain_C_suffixes, NULL },
406 { "prolog", Prolog_functions, Prolog_suffixes, NULL },
407 { "scheme", Scheme_functions, Scheme_suffixes, NULL },
408 { "tex", TeX_functions, TeX_suffixes, NULL },
409 { "yacc", Yacc_entries, Yacc_suffixes, NULL },
410 { "auto", NULL }, /* default guessing scheme */
411 { "none", just_read_file }, /* regexp matching only */
412 { NULL, NULL } /* end of list */
416 void
417 print_language_names ()
419 struct lang_entry *lang;
420 char **ext;
422 puts ("\nThese are the currently supported languages, along with the\n\
423 default file name suffixes:");
424 for (lang = lang_names; lang->name != NULL; lang++)
426 printf ("\t%s\t", lang->name);
427 if (lang->suffixes != NULL)
428 for (ext = lang->suffixes; *ext != NULL; ext++)
429 printf (" .%s", *ext);
430 puts ("");
432 puts ("Where `auto' means use default language for files based on file\n\
433 name suffix, and `none' means only do regexp processing on files.\n\
434 If no language is specified and no matching suffix is found,\n\
435 the first line of the file is read for a sharp-bang (#!) sequence\n\
436 followed by the name of an interpreter. If no such sequence is found,\n\
437 Fortran is tried first; if no tags are found, C is tried next.");
440 #ifndef VERSION
441 # define VERSION "19"
442 #endif
443 void
444 print_version ()
446 printf ("%s for Emacs version %s\n", (CTAGS) ? "ctags" : "etags", VERSION);
448 exit (GOOD);
451 void
452 print_help ()
454 printf ("These are the options accepted by %s. You may use unambiguous\n\
455 abbreviations for the long option names. A - as file name means read\n\
456 names from stdin.\n\n", progname);
458 puts ("-a, --append\n\
459 Append tag entries to existing tags file.");
461 if (CTAGS)
462 puts ("-B, --backward-search\n\
463 Write the search commands for the tag entries using '?', the\n\
464 backward-search command instead of '/', the forward-search command.");
466 puts ("-C, --c++\n\
467 Treat files whose name suffix defaults to C language as C++ files.");
469 if (CTAGS)
470 puts ("-d, --defines\n\
471 Create tag entries for constant C #defines, too.");
472 else
473 puts ("-D, --no-defines\n\
474 Don't create tag entries for constant C #defines. This makes\n\
475 the tags file smaller.");
477 if (!CTAGS)
479 puts ("-i FILE, --include=FILE\n\
480 Include a note in tag file indicating that, when searching for\n\
481 a tag, one should also consult the tags file FILE after\n\
482 checking the current file.");
483 puts ("-l LANG, --language=LANG\n\
484 Force the following files to be considered as written in the\n\
485 named language up to the next --language=LANG option.");
488 #ifdef ETAGS_REGEXPS
489 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
490 Make a tag for each line matching pattern REGEXP in the\n\
491 following files. REGEXP is anchored (as if preceded by ^).\n\
492 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
493 named tags can be created with:\n\
494 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
495 puts ("-R, --no-regex\n\
496 Don't create tags from regexps for the following files.");
497 #endif /* ETAGS_REGEXPS */
498 puts ("-o FILE, --output=FILE\n\
499 Write the tags to FILE.");
500 puts ("-I, --ignore-indentation\n\
501 Don't rely on indentation quite as much as normal. Currently,\n\
502 this means not to assume that a closing brace in the first\n\
503 column is the final brace of a function or structure\n\
504 definition in C and C++.");
506 if (CTAGS)
508 puts ("-t, --typedefs\n\
509 Generate tag entries for C typedefs.");
510 puts ("-T, --typedefs-and-c++\n\
511 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
512 and C++ member functions.");
513 puts ("-u, --update\n\
514 Update the tag entries for the given files, leaving tag\n\
515 entries for other files in place. Currently, this is\n\
516 implemented by deleting the existing entries for the given\n\
517 files and then rewriting the new entries at the end of the\n\
518 tags file. It is often faster to simply rebuild the entire\n\
519 tag file than to use this.");
520 puts ("-v, --vgrind\n\
521 Generates an index of items intended for human consumption,\n\
522 similar to the output of vgrind. The index is sorted, and\n\
523 gives the page number of each item.");
524 puts ("-w, --no-warn\n\
525 Suppress warning messages about entries defined in multiple\n\
526 files.");
527 puts ("-x, --cxref\n\
528 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
529 The output uses line numbers instead of page numbers, but\n\
530 beyond that the differences are cosmetic; try both to see\n\
531 which you like.");
534 puts ("-V, --version\n\
535 Print the version of the program.\n\
536 -h, --help\n\
537 Print this help message.");
539 print_language_names ();
541 exit (GOOD);
545 enum argument_type
547 at_language,
548 at_regexp,
549 at_filename
552 /* This structure helps us allow mixing of --lang and filenames. */
553 typedef struct
555 enum argument_type arg_type;
556 char *what;
557 Lang_function *function;
558 } argument;
560 #ifdef VMS /* VMS specific functions */
562 #define EOS '\0'
564 /* This is a BUG! ANY arbitrary limit is a BUG!
565 Won't someone please fix this? */
566 #define MAX_FILE_SPEC_LEN 255
567 typedef struct {
568 short curlen;
569 char body[MAX_FILE_SPEC_LEN + 1];
570 } vspec;
573 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
574 returning in each successive call the next filename matching the input
575 spec. The function expects that each in_spec passed
576 to it will be processed to completion; in particular, up to and
577 including the call following that in which the last matching name
578 is returned, the function ignores the value of in_spec, and will
579 only start processing a new spec with the following call.
580 If an error occurs, on return out_spec contains the value
581 of in_spec when the error occurred.
583 With each successive filename returned in out_spec, the
584 function's return value is one. When there are no more matching
585 names the function returns zero. If on the first call no file
586 matches in_spec, or there is any other error, -1 is returned.
589 #include <rmsdef.h>
590 #include <descrip.h>
591 #define OUTSIZE MAX_FILE_SPEC_LEN
592 short
593 fn_exp (out, in)
594 vspec *out;
595 char *in;
597 static long context = 0;
598 static struct dsc$descriptor_s o;
599 static struct dsc$descriptor_s i;
600 static logical pass1 = TRUE;
601 long status;
602 short retval;
604 if (pass1)
606 pass1 = FALSE;
607 o.dsc$a_pointer = (char *) out;
608 o.dsc$w_length = (short)OUTSIZE;
609 i.dsc$a_pointer = in;
610 i.dsc$w_length = (short)strlen(in);
611 i.dsc$b_dtype = DSC$K_DTYPE_T;
612 i.dsc$b_class = DSC$K_CLASS_S;
613 o.dsc$b_dtype = DSC$K_DTYPE_VT;
614 o.dsc$b_class = DSC$K_CLASS_VS;
616 if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL)
618 out->body[out->curlen] = EOS;
619 return 1;
621 else if (status == RMS$_NMF)
622 retval = 0;
623 else
625 strcpy(out->body, in);
626 retval = -1;
628 lib$find_file_end(&context);
629 pass1 = TRUE;
630 return retval;
634 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
635 name of each file specified by the provided arg expanding wildcards.
637 char *
638 gfnames (arg, p_error)
639 char *arg;
640 logical *p_error;
642 static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
644 switch (fn_exp (&filename, arg))
646 case 1:
647 *p_error = FALSE;
648 return filename.body;
649 case 0:
650 *p_error = FALSE;
651 return NULL;
652 default:
653 *p_error = TRUE;
654 return filename.body;
658 #ifndef OLD /* Newer versions of VMS do provide `system'. */
659 system (cmd)
660 char *cmd;
662 fprintf (stderr, "system() function not implemented under VMS\n");
664 #endif
666 #define VERSION_DELIM ';'
667 char *massage_name (s)
668 char *s;
670 char *start = s;
672 for ( ; *s; s++)
673 if (*s == VERSION_DELIM)
675 *s = EOS;
676 break;
678 else
679 *s = lowcase (*s);
680 return start;
682 #endif /* VMS */
685 void
686 main (argc, argv)
687 int argc;
688 char *argv[];
690 int i;
691 unsigned int nincluded_files = 0;
692 char **included_files = xnew (argc, char *);
693 char *this_file;
694 argument *argbuffer;
695 int current_arg = 0, file_count = 0;
696 struct linebuffer filename_lb;
697 #ifdef VMS
698 logical got_err;
699 #endif
701 #ifdef DOS_NT
702 _fmode = O_BINARY; /* all of files are treated as binary files */
703 #endif /* DOS_NT */
705 progname = argv[0];
707 /* Allocate enough no matter what happens. Overkill, but each one
708 is small. */
709 argbuffer = xnew (argc, argument);
711 #ifdef ETAGS_REGEXPS
712 /* Set syntax for regular expression routines. */
713 re_set_syntax (RE_SYNTAX_EMACS);
714 #endif /* ETAGS_REGEXPS */
717 * If etags, always find typedefs and structure tags. Why not?
718 * Also default is to find macro constants.
720 if (!CTAGS)
721 typedefs = typedefs_and_cplusplus = constantypedefs = TRUE;
723 while (1)
725 int opt = getopt_long (argc, argv,
726 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts, 0);
728 if (opt == EOF)
729 break;
731 switch (opt)
733 case 0:
734 /* If getopt returns 0, then it has already processed a
735 long-named option. We should do nothing. */
736 break;
738 case 1:
739 /* This means that a filename has been seen. Record it. */
740 argbuffer[current_arg].arg_type = at_filename;
741 argbuffer[current_arg].what = optarg;
742 ++current_arg;
743 ++file_count;
744 break;
746 /* Common options. */
747 case 'a':
748 append_to_tagfile = TRUE;
749 break;
750 case 'C':
751 cplusplus = TRUE;
752 break;
753 case 'd':
754 constantypedefs = TRUE;
755 break;
756 case 'D':
757 constantypedefs = FALSE;
758 break;
759 case 'f': /* for compatibility with old makefiles */
760 case 'o':
761 if (tagfile)
763 fprintf (stderr, "%s: -%c option may only be given once.\n",
764 progname, opt);
765 suggest_asking_for_help ();
767 tagfile = optarg;
768 break;
769 case 'I':
770 case 'S': /* for backward compatibility */
771 noindentypedefs = TRUE;
772 break;
773 case 'l':
774 argbuffer[current_arg].function = get_language_from_name (optarg);
775 argbuffer[current_arg].arg_type = at_language;
776 ++current_arg;
777 break;
778 #ifdef ETAGS_REGEXPS
779 case 'r':
780 argbuffer[current_arg].arg_type = at_regexp;
781 argbuffer[current_arg].what = optarg;
782 ++current_arg;
783 break;
784 case 'R':
785 argbuffer[current_arg].arg_type = at_regexp;
786 argbuffer[current_arg].what = NULL;
787 ++current_arg;
788 break;
789 #endif /* ETAGS_REGEXPS */
790 case 'V':
791 print_version ();
792 break;
793 case 'h':
794 case 'H':
795 print_help ();
796 break;
797 case 't':
798 typedefs = TRUE;
799 break;
800 case 'T':
801 typedefs = typedefs_and_cplusplus = TRUE;
802 break;
803 #if (!CTAGS)
804 /* Etags options */
805 case 'i':
806 included_files[nincluded_files++] = optarg;
807 break;
808 #else /* CTAGS */
809 /* Ctags options. */
810 case 'B':
811 searchar = '?';
812 break;
813 case 'u':
814 update = TRUE;
815 break;
816 case 'v':
817 vgrind_style = TRUE;
818 /*FALLTHRU*/
819 case 'x':
820 cxref_style = TRUE;
821 break;
822 case 'w':
823 no_warnings = TRUE;
824 break;
825 #endif /* CTAGS */
826 default:
827 suggest_asking_for_help ();
831 for (; optind < argc; ++optind)
833 argbuffer[current_arg].arg_type = at_filename;
834 argbuffer[current_arg].what = argv[optind];
835 ++current_arg;
836 ++file_count;
839 if (nincluded_files == 0 && file_count == 0)
841 fprintf (stderr, "%s: No input files specified.\n", progname);
842 suggest_asking_for_help ();
845 if (tagfile == NULL)
847 tagfile = CTAGS ? "tags" : "TAGS";
849 cwd = etags_getcwd (); /* the current working directory */
850 strcat (cwd, "/");
851 if (streq (tagfile, "-"))
853 tagfiledir = cwd;
855 else
857 tagfiledir = absolute_dirname (tagfile, cwd);
860 init (); /* set up boolean "functions" */
862 initbuffer (&lb);
863 initbuffer (&token_name);
864 initbuffer (&lbs[0].lb);
865 initbuffer (&lbs[1].lb);
866 initbuffer (&filename_lb);
868 if (!CTAGS)
870 if (streq (tagfile, "-"))
871 tagf = stdout;
872 else
873 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
874 if (tagf == NULL)
875 pfatal (tagfile);
879 * Loop through files finding functions.
881 for (i = 0; i < current_arg; ++i)
883 switch (argbuffer[i].arg_type)
885 case at_language:
886 lang_func = argbuffer[i].function;
887 break;
888 #ifdef ETAGS_REGEXPS
889 case at_regexp:
890 add_regex (argbuffer[i].what);
891 break;
892 #endif
893 case at_filename:
894 #ifdef VMS
895 while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL)
897 if (got_err)
899 error ("Can't find file %s\n", this_file);
900 argc--, argv++;
902 else
904 this_file = massage_name (this_file);
906 #else
907 this_file = argbuffer[i].what;
908 #endif
909 /* Input file named "-" means read file names from stdin
910 and use them. */
911 if (streq (this_file, "-"))
912 while (readline_internal (&filename_lb, stdin) > 0)
913 process_file (filename_lb.buffer);
914 else
915 process_file (this_file);
916 #ifdef VMS
918 #endif
919 break;
923 if (!CTAGS)
925 while (nincluded_files-- > 0)
926 fprintf (tagf, "\f\n%s,include\n", *included_files++);
928 fclose (tagf);
929 exit (GOOD);
932 /* If CTAGS, we are here. process_file did not write the tags yet,
933 because we want them ordered. Let's do it now. */
934 if (cxref_style)
936 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
937 if (tagf == NULL)
938 pfatal (tagfile);
939 put_entries (head);
940 exit (GOOD);
943 if (update)
945 char cmd[BUFSIZ];
946 for (i = 0; i < current_arg; ++i)
948 if (argbuffer[i].arg_type != at_filename)
949 continue;
950 sprintf (cmd,
951 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
952 tagfile, argbuffer[i].what, tagfile);
953 if (system (cmd) != GOOD)
954 fatal ("failed to execute shell command");
956 append_to_tagfile = TRUE;
959 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
960 if (tagf == NULL)
961 pfatal (tagfile);
962 put_entries (head);
963 fclose (tagf);
965 if (update)
967 char cmd[BUFSIZ];
968 sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
969 exit (system (cmd));
971 exit (GOOD);
976 * Return a Lang_function given the name.
978 Lang_function *
979 get_language_from_name (name)
980 char *name;
982 struct lang_entry *lang;
984 if (name != NULL)
985 for (lang = lang_names; lang->name != NULL; lang++)
987 if (streq (name, lang->name))
988 return lang->function;
991 fprintf (stderr, "%s: language \"%s\" not recognized.\n",
992 progname, optarg);
993 suggest_asking_for_help ();
995 /* This point should never be reached. The function should either
996 return a function pointer or never return. Note that a NULL
997 pointer cannot be considered as an error, as it means that the
998 language has not been explicitely imposed by the user ("auto"). */
999 return NULL; /* avoid warnings from compiler */
1004 * Return a Lang_function given the interpreter name.
1006 Lang_function *
1007 get_language_from_interpreter (interpreter)
1008 char *interpreter;
1010 struct lang_entry *lang;
1011 char **iname;
1013 if (interpreter == NULL)
1014 return NULL;
1015 for (lang = lang_names; lang->name != NULL; lang++)
1016 if (lang->interpreters != NULL)
1017 for (iname = lang->interpreters; *iname != NULL; iname++)
1018 if (streq (*iname, interpreter))
1019 return lang->function;
1021 return NULL;
1027 * Return a Lang_function given the file suffix.
1029 Lang_function *
1030 get_language_from_suffix (suffix)
1031 char *suffix;
1033 struct lang_entry *lang;
1034 char **ext;
1036 if (suffix == NULL)
1037 return NULL;
1038 for (lang = lang_names; lang->name != NULL; lang++)
1039 if (lang->suffixes != NULL)
1040 for (ext = lang->suffixes; *ext != NULL; ext++)
1041 if (streq (*ext, suffix))
1042 return lang->function;
1044 return NULL;
1049 * This routine is called on each file argument.
1051 void
1052 process_file (file)
1053 char *file;
1055 struct stat stat_buf;
1056 FILE *inf;
1058 if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
1060 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
1061 return;
1063 if (streq (file, tagfile) && !streq (tagfile, "-"))
1065 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
1066 return;
1068 inf = fopen (file, "r");
1069 if (inf == NULL)
1071 perror (file);
1072 return;
1075 find_entries (file, inf);
1077 if (!CTAGS)
1079 char *filename;
1081 if (absolutefn (file))
1083 /* file is an absolute filename. Canonicalise it. */
1084 filename = absolute_filename (file, cwd);
1086 else
1088 /* file is a filename relative to cwd. Make it relative
1089 to the directory of the tags file. */
1090 filename = relative_filename (file, tagfiledir);
1092 fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
1093 free (filename);
1094 put_entries (head);
1095 free_tree (head);
1096 head = NULL;
1101 * This routine sets up the boolean pseudo-functions which work
1102 * by setting boolean flags dependent upon the corresponding character
1103 * Every char which is NOT in that string is not a white char. Therefore,
1104 * all of the array "_wht" is set to FALSE, and then the elements
1105 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1106 * of a char is TRUE if it is the string "white", else FALSE.
1108 void
1109 init ()
1111 register char *sp;
1112 register int i;
1114 for (i = 0; i < 0177; i++)
1115 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
1116 for (sp = white; *sp; sp++)
1117 _wht[*sp] = TRUE;
1118 for (sp = endtk; *sp; sp++)
1119 _etk[*sp] = TRUE;
1120 for (sp = intk; *sp; sp++)
1121 _itk[*sp] = TRUE;
1122 for (sp = begtk; *sp; sp++)
1123 _btk[*sp] = TRUE;
1124 _wht[0] = _wht['\n'];
1125 _etk[0] = _etk['\n'];
1126 _btk[0] = _btk['\n'];
1127 _itk[0] = _itk['\n'];
1131 * This routine opens the specified file and calls the function
1132 * which finds the function and type definitions.
1134 void
1135 find_entries (file, inf)
1136 char *file;
1137 FILE *inf;
1139 char *cp;
1140 Lang_function *function;
1141 NODE *old_last_node;
1142 extern NODE *last_node;
1145 /* Memory leakage here: the memory block pointed by curfile is never
1146 released. The amount of memory leaked here is the sum of the
1147 lengths of the input file names. */
1148 curfile = savestr (file);
1150 /* If user specified a language, use it. */
1151 function = lang_func;
1152 if (function != NULL)
1154 function (inf);
1155 fclose (inf);
1156 return;
1159 cp = etags_strrchr (file, '.');
1160 if (cp != NULL)
1162 cp += 1;
1163 function = get_language_from_suffix (cp);
1164 if (function != NULL)
1166 function (inf);
1167 fclose (inf);
1168 return;
1172 /* Look for sharp-bang as the first two characters. */
1173 if (readline_internal (&lb, inf) > 2
1174 && lb.buffer[0] == '#'
1175 && lb.buffer[1] == '!')
1177 char *lp;
1179 /* Set lp to point at the first char after the last slash in the
1180 line or, if no slashes, at the first nonblank. Then set cp to
1181 the first successive blank and terminate the string. */
1182 lp = etags_strrchr (lb.buffer+2, '/');
1183 if (lp != NULL)
1184 lp += 1;
1185 else
1186 for (lp = lb.buffer+2; *lp != '\0' && isspace (*lp); lp++)
1187 continue;
1188 for (cp = lp; *cp != '\0' && !isspace (*cp); cp++)
1189 continue;
1190 *cp = '\0';
1192 if (strlen (lp) > 0)
1194 function = get_language_from_interpreter (lp);
1195 if (function != NULL)
1197 function (inf);
1198 fclose (inf);
1199 return;
1203 rewind (inf);
1205 /* Try Fortran. */
1206 old_last_node = last_node;
1207 Fortran_functions (inf);
1209 /* No Fortran entries found. Try C. */
1210 if (old_last_node == last_node)
1212 rewind (inf);
1213 default_C_entries (inf);
1215 fclose (inf);
1216 return;
1219 /* Record a tag. */
1220 void
1221 pfnote (name, is_func, linestart, linelen, lno, cno)
1222 char *name; /* tag name, or NULL if unnamed */
1223 logical is_func; /* tag is a function */
1224 char *linestart; /* start of the line where tag is */
1225 int linelen; /* length of the line where tag is */
1226 int lno; /* line number */
1227 long cno; /* character number */
1229 register NODE *np;
1231 if (CTAGS && name == NULL)
1232 return;
1234 np = xnew (1, NODE);
1236 /* If ctags mode, change name "main" to M<thisfilename>. */
1237 if (CTAGS && !cxref_style && streq (name, "main"))
1239 register char *fp = etags_strrchr (curfile, '/');
1240 np->name = concat ("M", fp == 0 ? curfile : fp + 1, "");
1241 fp = etags_strrchr (np->name, '.');
1242 if (fp && fp[1] != '\0' && fp[2] == '\0')
1243 fp[0] = 0;
1245 else
1246 np->name = name;
1247 np->been_warned = FALSE;
1248 np->file = curfile;
1249 np->is_func = is_func;
1250 np->lno = lno;
1251 /* Our char numbers are 0-base, because of C language tradition?
1252 ctags compatibility? old versions compatibility? I don't know.
1253 Anyway, since emacs's are 1-base we expect etags.el to take care
1254 of the difference. If we wanted to have 1-based numbers, we would
1255 uncomment the +1 below. */
1256 np->cno = cno /* + 1 */ ;
1257 np->left = np->right = NULL;
1258 if (CTAGS && !cxref_style)
1260 if (strlen (linestart) < 50)
1261 np->pat = concat (linestart, "$", "");
1262 else
1263 np->pat = savenstr (linestart, 50);
1265 else
1266 np->pat = savenstr (linestart, linelen);
1268 add_node (np, &head);
1272 * free_tree ()
1273 * recurse on left children, iterate on right children.
1275 void
1276 free_tree (node)
1277 register NODE *node;
1279 while (node)
1281 register NODE *node_right = node->right;
1282 free_tree (node->left);
1283 if (node->name != NULL)
1284 free (node->name);
1285 free (node->pat);
1286 free ((char *) node);
1287 node = node_right;
1292 * add_node ()
1293 * Adds a node to the tree of nodes. In etags mode, we don't keep
1294 * it sorted; we just keep a linear list. In ctags mode, maintain
1295 * an ordered tree, with no attempt at balancing.
1297 * add_node is the only function allowed to add nodes, so it can
1298 * maintain state.
1300 NODE *last_node = NULL;
1301 void
1302 add_node (node, cur_node_p)
1303 NODE *node, **cur_node_p;
1305 register int dif;
1306 register NODE *cur_node = *cur_node_p;
1308 if (cur_node == NULL)
1310 *cur_node_p = node;
1311 last_node = node;
1312 return;
1315 if (!CTAGS)
1317 /* Etags Mode */
1318 if (last_node == NULL)
1319 fatal ("internal error in add_node", 0);
1320 last_node->right = node;
1321 last_node = node;
1323 else
1325 /* Ctags Mode */
1326 dif = strcmp (node->name, cur_node->name);
1329 * If this tag name matches an existing one, then
1330 * do not add the node, but maybe print a warning.
1332 if (!dif)
1334 if (streq (node->file, cur_node->file))
1336 if (!no_warnings)
1338 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1339 node->file, lineno, node->name);
1340 fprintf (stderr, "Second entry ignored\n");
1343 else if (!cur_node->been_warned && !no_warnings)
1345 fprintf
1346 (stderr,
1347 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1348 node->file, cur_node->file, node->name);
1349 cur_node->been_warned = TRUE;
1351 return;
1354 /* Actually add the node */
1355 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1359 void
1360 put_entries (node)
1361 register NODE *node;
1363 register char *sp;
1365 if (node == NULL)
1366 return;
1368 /* Output subentries that precede this one */
1369 put_entries (node->left);
1371 /* Output this entry */
1373 if (!CTAGS)
1375 if (node->name != NULL)
1376 fprintf (tagf, "%s\177%s\001%d,%d\n",
1377 node->pat, node->name, node->lno, node->cno);
1378 else
1379 fprintf (tagf, "%s\177%d,%d\n",
1380 node->pat, node->lno, node->cno);
1382 else
1384 if (node->name == NULL)
1385 error ("internal error: NULL name in ctags mode.", 0);
1387 if (cxref_style)
1389 if (vgrind_style)
1390 fprintf (stdout, "%s %s %d\n",
1391 node->name, node->file, (node->lno + 63) / 64);
1392 else
1393 fprintf (stdout, "%-16s %3d %-16s %s\n",
1394 node->name, node->lno, node->file, node->pat);
1396 else
1398 fprintf (tagf, "%s\t%s\t", node->name, node->file);
1400 if (node->is_func)
1401 { /* a function */
1402 putc (searchar, tagf);
1403 putc ('^', tagf);
1405 for (sp = node->pat; *sp; sp++)
1407 if (*sp == '\\' || *sp == searchar)
1408 putc ('\\', tagf);
1409 putc (*sp, tagf);
1411 putc (searchar, tagf);
1413 else
1414 { /* a typedef; text pattern inadequate */
1415 fprintf (tagf, "%d", node->lno);
1417 putc ('\n', tagf);
1421 /* Output subentries that follow this one */
1422 put_entries (node->right);
1425 /* Length of a number's decimal representation. */
1427 number_len (num)
1428 long num;
1430 int len = 0;
1431 if (!num)
1432 return 1;
1433 for (; num; num /= 10)
1434 ++len;
1435 return len;
1439 * Return total number of characters that put_entries will output for
1440 * the nodes in the subtree of the specified node. Works only if
1441 * we are not ctags, but called only in that case. This count
1442 * is irrelevant with the new tags.el, but is still supplied for
1443 * backward compatibility.
1446 total_size_of_entries (node)
1447 register NODE *node;
1449 register int total;
1451 if (node == NULL)
1452 return 0;
1454 total = 0;
1455 for (; node; node = node->right)
1457 /* Count left subentries. */
1458 total += total_size_of_entries (node->left);
1460 /* Count this entry */
1461 total += strlen (node->pat) + 1;
1462 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1463 if (node->name != NULL)
1464 total += 1 + strlen (node->name); /* \001name */
1467 return total;
1471 * The C symbol tables.
1473 enum sym_type
1475 st_none, st_C_objprot, st_C_objimpl, st_C_objend, st_C_gnumacro,
1476 st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
1479 /* Feed stuff between (but not including) %[ and %] lines to:
1480 gperf -c -k1,3 -o -p -r -t
1482 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1484 @interface, 0, st_C_objprot
1485 @protocol, 0, st_C_objprot
1486 @implementation,0, st_C_objimpl
1487 @end, 0, st_C_objend
1488 class, C_PLPL, st_C_struct
1489 domain, C_STAR, st_C_struct
1490 union, 0, st_C_struct
1491 struct, 0, st_C_struct
1492 enum, 0, st_C_enum
1493 typedef, 0, st_C_typedef
1494 define, 0, st_C_define
1495 long, 0, st_C_typespec
1496 short, 0, st_C_typespec
1497 int, 0, st_C_typespec
1498 char, 0, st_C_typespec
1499 float, 0, st_C_typespec
1500 double, 0, st_C_typespec
1501 signed, 0, st_C_typespec
1502 unsigned, 0, st_C_typespec
1503 auto, 0, st_C_typespec
1504 void, 0, st_C_typespec
1505 extern, 0, st_C_typespec
1506 static, 0, st_C_typespec
1507 const, 0, st_C_typespec
1508 volatile, 0, st_C_typespec
1509 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1510 DEFUN, 0, st_C_gnumacro
1511 SYSCALL, 0, st_C_gnumacro
1512 ENTRY, 0, st_C_gnumacro
1513 PSEUDO, 0, st_C_gnumacro
1514 # These are defined inside C functions, so currently they are not met.
1515 # EXFUN used in glibc, DEFVAR_* in emacs.
1516 #EXFUN, 0, st_C_gnumacro
1517 #DEFVAR_, 0, st_C_gnumacro
1519 and replace lines between %< and %> with its output. */
1520 /*%<*/
1521 /* C code produced by gperf version 1.8.1 (K&R C version) */
1522 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1525 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
1527 #define MIN_WORD_LENGTH 3
1528 #define MAX_WORD_LENGTH 15
1529 #define MIN_HASH_VALUE 7
1530 #define MAX_HASH_VALUE 63
1532 29 keywords
1533 57 is the maximum key range
1536 static int
1537 hash (str, len)
1538 register char *str;
1539 register int len;
1541 static unsigned char hash_table[] =
1543 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1544 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1545 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1546 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1547 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1548 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1549 63, 63, 63, 63, 17, 63, 63, 63, 4, 14,
1550 4, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1551 8, 63, 63, 0, 23, 63, 63, 63, 63, 63,
1552 63, 63, 63, 63, 63, 63, 63, 28, 63, 28,
1553 10, 31, 27, 18, 63, 6, 63, 63, 26, 1,
1554 11, 2, 29, 63, 29, 16, 26, 13, 15, 63,
1555 63, 63, 63, 63, 63, 63, 63, 63,
1557 return len + hash_table[str[2]] + hash_table[str[0]];
1560 struct C_stab_entry *
1561 in_word_set (str, len)
1562 register char *str;
1563 register int len;
1566 static struct C_stab_entry wordlist[] =
1568 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1569 {"SYSCALL", 0, st_C_gnumacro},
1570 {"",}, {"",}, {"",}, {"",}, {"",},
1571 {"DEFUN", 0, st_C_gnumacro},
1572 {"",}, {"",}, {"",},
1573 {"domain", C_STAR, st_C_struct},
1574 {"",}, {"",}, {"",}, {"",}, {"",},
1575 {"short", 0, st_C_typespec},
1576 {"union", 0, st_C_struct},
1577 {"void", 0, st_C_typespec},
1578 {"",}, {"",},
1579 {"PSEUDO", 0, st_C_gnumacro},
1580 {"double", 0, st_C_typespec},
1581 {"",}, {"",},
1582 {"@end", 0, st_C_objend},
1583 {"@implementation", 0, st_C_objimpl},
1584 {"float", 0, st_C_typespec},
1585 {"int", 0, st_C_typespec},
1586 {"",},
1587 {"unsigned", 0, st_C_typespec},
1588 {"@interface", 0, st_C_objprot},
1589 {"",},
1590 {"signed", 0, st_C_typespec},
1591 {"long", 0, st_C_typespec},
1592 {"ENTRY", 0, st_C_gnumacro},
1593 {"define", 0, st_C_define},
1594 {"const", 0, st_C_typespec},
1595 {"",}, {"",}, {"",},
1596 {"enum", 0, st_C_enum},
1597 {"volatile", 0, st_C_typespec},
1598 {"static", 0, st_C_typespec},
1599 {"struct", 0, st_C_struct},
1600 {"",}, {"",}, {"",},
1601 {"@protocol", 0, st_C_objprot},
1602 {"",}, {"",},
1603 {"auto", 0, st_C_typespec},
1604 {"",},
1605 {"char", 0, st_C_typespec},
1606 {"class", C_PLPL, st_C_struct},
1607 {"typedef", 0, st_C_typedef},
1608 {"extern", 0, st_C_typespec},
1611 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
1613 register int key = hash (str, len);
1615 if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
1617 register char *s = wordlist[key].name;
1619 if (*s == *str && !strncmp (str + 1, s + 1, len - 1))
1620 return &wordlist[key];
1623 return 0;
1625 /*%>*/
1627 enum sym_type
1628 C_symtype(str, len, c_ext)
1629 char *str;
1630 int len;
1631 int c_ext;
1633 register struct C_stab_entry *se = in_word_set(str, len);
1635 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
1636 return st_none;
1637 return se->type;
1641 * C functions are recognized using a simple finite automaton.
1642 * funcdef is its state variable.
1644 enum
1646 fnone, /* nothing seen */
1647 ftagseen, /* function-like tag seen */
1648 fstartlist, /* just after open parenthesis */
1649 finlist, /* in parameter list */
1650 flistseen, /* after parameter list */
1651 fignore /* before open brace */
1652 } funcdef;
1656 * typedefs are recognized using a simple finite automaton.
1657 * typdef is its state variable.
1659 enum
1661 tnone, /* nothing seen */
1662 ttypedseen, /* typedef keyword seen */
1663 tinbody, /* inside typedef body */
1664 tend, /* just before typedef tag */
1665 tignore /* junk after typedef tag */
1666 } typdef;
1670 * struct-like structures (enum, struct and union) are recognized
1671 * using another simple finite automaton. `structdef' is its state
1672 * variable.
1674 enum
1676 snone, /* nothing seen yet */
1677 skeyseen, /* struct-like keyword seen */
1678 stagseen, /* struct-like tag seen */
1679 scolonseen, /* colon seen after struct-like tag */
1680 sinbody /* in struct body: recognize member func defs*/
1681 } structdef;
1684 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1685 * struct tag, and structtype is the type of the preceding struct-like
1686 * keyword.
1688 char *structtag = "<uninited>";
1689 enum sym_type structtype;
1692 * When objdef is different from onone, objtag is the name of the class.
1694 char *objtag = "<uninited>";
1697 * Yet another little state machine to deal with preprocessor lines.
1699 enum
1701 dnone, /* nothing seen */
1702 dsharpseen, /* '#' seen as first char on line */
1703 ddefineseen, /* '#' and 'define' seen */
1704 dignorerest /* ignore rest of line */
1705 } definedef;
1708 * State machine for Objective C protocols and implementations.
1710 enum
1712 onone, /* nothing seen */
1713 oprotocol, /* @interface or @protocol seen */
1714 oimplementation, /* @implementations seen */
1715 otagseen, /* class name seen */
1716 oparenseen, /* parenthesis before category seen */
1717 ocatseen, /* category name seen */
1718 oinbody, /* in @implementation body */
1719 omethodsign, /* in @implementation body, after +/- */
1720 omethodtag, /* after method name */
1721 omethodcolon, /* after method colon */
1722 omethodparm, /* after method parameter */
1723 oignore /* wait for @end */
1724 } objdef;
1727 * Set this to TRUE, and the next token considered is called a function.
1728 * Used only for GNU emacs's function-defining macros.
1730 logical next_token_is_func;
1733 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1735 logical yacc_rules;
1738 * methodlen is the length of the method name stored in token_name.
1740 int methodlen;
1743 * consider_token ()
1744 * checks to see if the current token is at the start of a
1745 * function, or corresponds to a typedef, or is a struct/union/enum
1746 * tag.
1748 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1749 * C_EXT is which language we are looking at.
1751 * In the future we will need some way to adjust where the end of
1752 * the token is; for instance, implementing the C++ keyword
1753 * `operator' properly will adjust the end of the token to be after
1754 * whatever follows `operator'.
1756 * Globals
1757 * funcdef IN OUT
1758 * structdef IN OUT
1759 * definedef IN OUT
1760 * typdef IN OUT
1761 * objdef IN OUT
1762 * next_token_is_func IN OUT
1765 logical
1766 consider_token (str, len, c, c_ext, cblev, parlev, is_func)
1767 register char *str; /* IN: token pointer */
1768 register int len; /* IN: token length */
1769 register char c; /* IN: first char after the token */
1770 int c_ext; /* IN: C extensions mask */
1771 int cblev; /* IN: curly brace level */
1772 int parlev; /* IN: parenthesis level */
1773 logical *is_func; /* OUT: function found */
1775 enum sym_type toktype = C_symtype (str, len, c_ext);
1778 * Advance the definedef state machine.
1780 switch (definedef)
1782 case dnone:
1783 /* We're not on a preprocessor line. */
1784 break;
1785 case dsharpseen:
1786 if (toktype == st_C_define)
1788 definedef = ddefineseen;
1790 else
1792 definedef = dignorerest;
1794 return FALSE;
1795 case ddefineseen:
1797 * Make a tag for any macro, unless it is a constant
1798 * and constantypedefs is FALSE.
1800 definedef = dignorerest;
1801 *is_func = (c == '(');
1802 if (!*is_func && !constantypedefs)
1803 return FALSE;
1804 else
1805 return TRUE;
1806 case dignorerest:
1807 return FALSE;
1808 default:
1809 error ("internal error: definedef value.", 0);
1813 * Now typedefs
1815 switch (typdef)
1817 case tnone:
1818 if (toktype == st_C_typedef)
1820 if (typedefs)
1821 typdef = ttypedseen;
1822 funcdef = fnone;
1823 return FALSE;
1825 break;
1826 case ttypedseen:
1827 switch (toktype)
1829 case st_none:
1830 case st_C_typespec:
1831 typdef = tend;
1832 break;
1833 case st_C_struct:
1834 case st_C_enum:
1835 break;
1837 /* Do not return here, so the structdef stuff has a chance. */
1838 break;
1839 case tend:
1840 switch (toktype)
1842 case st_C_typespec:
1843 case st_C_struct:
1844 case st_C_enum:
1845 return FALSE;
1847 return TRUE;
1851 * This structdef business is currently only invoked when cblev==0.
1852 * It should be recursively invoked whatever the curly brace level,
1853 * and a stack of states kept, to allow for definitions of structs
1854 * within structs.
1856 * This structdef business is NOT invoked when we are ctags and the
1857 * file is plain C. This is because a struct tag may have the same
1858 * name as another tag, and this loses with ctags.
1860 * This if statement deals with the typdef state machine as
1861 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1862 * return FALSE. All the other code here is for the structdef
1863 * state machine.
1865 switch (toktype)
1867 case st_C_struct:
1868 case st_C_enum:
1869 if (typdef == ttypedseen
1870 || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
1872 structdef = skeyseen;
1873 structtype = toktype;
1875 return FALSE;
1877 if (structdef == skeyseen)
1879 /* Save the tag for struct/union/class, for functions that may be
1880 defined inside. */
1881 if (structtype == st_C_struct)
1882 structtag = savenstr (str, len);
1883 else
1884 structtag = "<enum>";
1885 structdef = stagseen;
1886 return TRUE;
1889 /* Avoid entering funcdef stuff if typdef is going on. */
1890 if (typdef != tnone)
1892 definedef = dnone;
1893 return FALSE;
1896 /* Detect GNU macros. */
1897 if (definedef == dnone && toktype == st_C_gnumacro)
1899 next_token_is_func = TRUE;
1900 return FALSE;
1902 if (next_token_is_func)
1904 next_token_is_func = FALSE;
1905 funcdef = fignore;
1906 *is_func = TRUE;
1907 return TRUE;
1911 * Detecting Objective C constructs.
1913 switch (objdef)
1915 case onone:
1916 switch (toktype)
1918 case st_C_objprot:
1919 objdef = oprotocol;
1920 return FALSE;
1921 case st_C_objimpl:
1922 objdef = oimplementation;
1923 return FALSE;
1925 break;
1926 case oimplementation:
1927 /* Save the class tag for functions that may be defined inside. */
1928 objtag = savenstr (str, len);
1929 objdef = oinbody;
1930 return FALSE;
1931 case oprotocol:
1932 /* Save the class tag for categories. */
1933 objtag = savenstr (str, len);
1934 objdef = otagseen;
1935 *is_func = TRUE;
1936 return TRUE;
1937 case oparenseen:
1938 objdef = ocatseen;
1939 *is_func = TRUE;
1940 return TRUE;
1941 case oinbody:
1942 break;
1943 case omethodsign:
1944 if (parlev == 0)
1946 objdef = omethodtag;
1947 methodlen = len;
1948 GROW_LINEBUFFER (token_name, methodlen+1);
1949 strncpy (token_name.buffer, str, len);
1950 token_name.buffer[methodlen] = '\0';
1951 return TRUE;
1953 return FALSE;
1954 case omethodcolon:
1955 if (parlev == 0)
1956 objdef = omethodparm;
1957 return FALSE;
1958 case omethodparm:
1959 if (parlev == 0)
1961 objdef = omethodtag;
1962 methodlen += len;
1963 GROW_LINEBUFFER (token_name, methodlen+1);
1964 strncat (token_name.buffer, str, len);
1965 return TRUE;
1967 return FALSE;
1968 case oignore:
1969 if (toktype == st_C_objend)
1971 /* Memory leakage here: the string pointed by objtag is
1972 never released, because many tests would be needed to
1973 avoid breaking on incorrect input code. The amount of
1974 memory leaked here is the sum of the lengths of the
1975 class tags.
1976 free (objtag); */
1977 objdef = onone;
1979 return FALSE;
1982 /* A function? */
1983 switch (toktype)
1985 case st_C_typespec:
1986 if (funcdef != finlist && funcdef != fignore)
1987 funcdef = fnone; /* should be useless */
1988 return FALSE;
1989 default:
1990 if (funcdef == fnone)
1992 funcdef = ftagseen;
1993 *is_func = TRUE;
1994 return TRUE;
1998 return FALSE;
2002 * C_entries ()
2003 * This routine finds functions, typedefs, #define's and
2004 * struct/union/enum definitions in C syntax and adds them
2005 * to the list.
2007 typedef struct
2009 logical valid;
2010 char *str;
2011 logical named;
2012 int linelen;
2013 int lineno;
2014 long linepos;
2015 char *buffer;
2016 } TOKEN;
2018 #define current_lb_is_new (newndx == curndx)
2019 #define switch_line_buffers() (curndx = 1 - curndx)
2021 #define curlb (lbs[curndx].lb)
2022 #define othlb (lbs[1-curndx].lb)
2023 #define newlb (lbs[newndx].lb)
2024 #define curlinepos (lbs[curndx].linepos)
2025 #define othlinepos (lbs[1-curndx].linepos)
2026 #define newlinepos (lbs[newndx].linepos)
2028 #define CNL_SAVE_DEFINEDEF \
2029 do { \
2030 curlinepos = charno; \
2031 lineno++; \
2032 charno += readline (&curlb, inf); \
2033 lp = curlb.buffer; \
2034 quotednl = FALSE; \
2035 newndx = curndx; \
2036 } while (0)
2038 #define CNL \
2039 do { \
2040 CNL_SAVE_DEFINEDEF; \
2041 if (savetok.valid) \
2043 tok = savetok; \
2044 savetok.valid = FALSE; \
2046 definedef = dnone; \
2047 } while (0)
2049 /* Ideally this macro should never be called wihen tok.valid is FALSE,
2050 but this would mean that the state machines always guess right. */
2051 #define make_tag(isfun) do \
2052 if (tok.valid) { \
2053 char *name = NULL; \
2054 if (CTAGS || tok.named) \
2055 name = savestr (token_name.buffer); \
2056 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
2057 tok.valid = FALSE; \
2058 } while (0)
2060 void
2061 C_entries (c_ext, inf)
2062 int c_ext; /* extension of C */
2063 FILE *inf; /* input file */
2065 register char c; /* latest char read; '\0' for end of line */
2066 register char *lp; /* pointer one beyond the character `c' */
2067 int curndx, newndx; /* indices for current and new lb */
2068 TOKEN tok; /* latest token read */
2069 register int tokoff; /* offset in line of start of current token */
2070 register int toklen; /* length of current token */
2071 int cblev; /* current curly brace level */
2072 int parlev; /* current parenthesis level */
2073 logical incomm, inquote, inchar, quotednl, midtoken;
2074 logical cplpl;
2075 TOKEN savetok; /* token saved during preprocessor handling */
2078 curndx = newndx = 0;
2079 lineno = 0;
2080 charno = 0;
2081 lp = curlb.buffer;
2082 *lp = 0;
2084 funcdef = fnone; typdef = tnone; structdef = snone;
2085 definedef = dnone; objdef = onone;
2086 next_token_is_func = yacc_rules = FALSE;
2087 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2088 tok.valid = savetok.valid = FALSE;
2089 cblev = 0;
2090 parlev = 0;
2091 cplpl = c_ext & C_PLPL;
2093 while (!feof (inf))
2095 c = *lp++;
2096 if (c == '\\')
2098 /* If we're at the end of the line, the next character is a
2099 '\0'; don't skip it, because it's the thing that tells us
2100 to read the next line. */
2101 if (*lp == '\0')
2103 quotednl = TRUE;
2104 continue;
2106 lp++;
2107 c = ' ';
2109 else if (incomm)
2111 switch (c)
2113 case '*':
2114 if (*lp == '/')
2116 c = *lp++;
2117 incomm = FALSE;
2119 break;
2120 case '\0':
2121 /* Newlines inside comments do not end macro definitions in
2122 traditional cpp. */
2123 CNL_SAVE_DEFINEDEF;
2124 break;
2126 continue;
2128 else if (inquote)
2130 switch (c)
2132 case '"':
2133 inquote = FALSE;
2134 break;
2135 case '\0':
2136 /* Newlines inside strings do not end macro definitions
2137 in traditional cpp, even though compilers don't
2138 usually accept them. */
2139 CNL_SAVE_DEFINEDEF;
2140 break;
2142 continue;
2144 else if (inchar)
2146 switch (c)
2148 case '\0':
2149 /* Hmmm, something went wrong. */
2150 CNL;
2151 /* FALLTHRU */
2152 case '\'':
2153 inchar = FALSE;
2154 break;
2156 continue;
2158 else
2159 switch (c)
2161 case '"':
2162 inquote = TRUE;
2163 if (funcdef != finlist && funcdef != fignore)
2164 funcdef = fnone;
2165 continue;
2166 case '\'':
2167 inchar = TRUE;
2168 if (funcdef != finlist && funcdef != fignore)
2169 funcdef = fnone;
2170 continue;
2171 case '/':
2172 if (*lp == '*')
2174 lp++;
2175 incomm = TRUE;
2176 continue;
2178 else if (/* cplpl && */ *lp == '/')
2180 c = '\0';
2181 break;
2183 else
2184 break;
2185 case '%':
2186 if ((c_ext & YACC) && *lp == '%')
2188 /* entering or exiting rules section in yacc file */
2189 lp++;
2190 definedef = dnone; funcdef = fnone;
2191 typdef = tnone; structdef = snone;
2192 next_token_is_func = FALSE;
2193 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2194 cblev = 0;
2195 yacc_rules = !yacc_rules;
2196 continue;
2198 else
2199 break;
2200 case '#':
2201 if (definedef == dnone)
2203 char *cp;
2204 logical cpptoken = TRUE;
2206 /* Look back on this line. If all blanks, or nonblanks
2207 followed by an end of comment, this is a preprocessor
2208 token. */
2209 for (cp = newlb.buffer; cp < lp-1; cp++)
2210 if (!iswhite (*cp))
2212 if (*cp == '*' && *(cp+1) == '/')
2214 cp++;
2215 cpptoken = TRUE;
2217 else
2218 cpptoken = FALSE;
2220 if (cpptoken)
2221 definedef = dsharpseen;
2222 } /* if (definedef == dnone) */
2224 continue;
2225 } /* switch (c) */
2228 /* Consider token only if some complicated conditions are satisfied. */
2229 if ((definedef != dnone
2230 || (cblev == 0 && structdef != scolonseen)
2231 || (cblev == 1 && cplpl && structdef == sinbody))
2232 && typdef != tignore
2233 && definedef != dignorerest
2234 && funcdef != finlist)
2236 if (midtoken)
2238 if (endtoken (c))
2240 if (c == ':' && cplpl && *lp == ':' && begtoken(*(lp + 1)))
2243 * This handles :: in the middle, but not at the
2244 * beginning of an identifier.
2246 lp += 2;
2247 toklen += 3;
2249 else
2251 logical is_func = FALSE;
2253 if (yacc_rules
2254 || consider_token (newlb.buffer + tokoff, toklen, c,
2255 c_ext, cblev, parlev, &is_func))
2257 if (structdef == sinbody
2258 && definedef == dnone
2259 && is_func)
2260 /* function defined in C++ class body */
2262 GROW_LINEBUFFER (token_name,
2263 strlen(structtag)+2+toklen+1);
2264 strcpy (token_name.buffer, structtag);
2265 strcat (token_name.buffer, "::");
2266 strncat (token_name.buffer,
2267 newlb.buffer+tokoff, toklen);
2268 tok.named = TRUE;
2270 else if (objdef == ocatseen)
2271 /* Objective C category */
2273 GROW_LINEBUFFER (token_name,
2274 strlen(objtag)+2+toklen+1);
2275 strcpy (token_name.buffer, objtag);
2276 strcat (token_name.buffer, "(");
2277 strncat (token_name.buffer,
2278 newlb.buffer+tokoff, toklen);
2279 strcat (token_name.buffer, ")");
2280 tok.named = TRUE;
2282 else if (objdef == omethodtag
2283 || objdef == omethodparm)
2284 /* Objective C method */
2286 tok.named = TRUE;
2288 else
2290 GROW_LINEBUFFER (token_name, toklen+1);
2291 strncpy (token_name.buffer,
2292 newlb.buffer+tokoff, toklen);
2293 token_name.buffer[toklen] = '\0';
2294 if (structdef == stagseen
2295 || typdef == tend
2296 || (is_func
2297 && definedef == dignorerest)) /* macro */
2298 tok.named = TRUE;
2299 else
2300 tok.named = FALSE;
2302 tok.lineno = lineno;
2303 tok.linelen = tokoff + toklen + 1;
2304 tok.buffer = newlb.buffer;
2305 tok.linepos = newlinepos;
2306 tok.valid = TRUE;
2308 if (definedef == dnone
2309 && (funcdef == ftagseen
2310 || structdef == stagseen
2311 || typdef == tend
2312 || objdef != onone))
2314 if (current_lb_is_new)
2315 switch_line_buffers ();
2317 else
2318 make_tag (is_func);
2320 midtoken = FALSE;
2322 } /* if (endtoken (c)) */
2323 else if (intoken (c))
2325 toklen++;
2326 continue;
2328 } /* if (midtoken) */
2329 else if (begtoken (c))
2331 switch (definedef)
2333 case dnone:
2334 switch (funcdef)
2336 case fstartlist:
2337 funcdef = finlist;
2338 continue;
2339 case flistseen:
2340 make_tag (TRUE);
2341 funcdef = fignore;
2342 break;
2343 case ftagseen:
2344 funcdef = fnone;
2345 break;
2347 if (structdef == stagseen)
2348 structdef = snone;
2349 break;
2350 case dsharpseen:
2351 savetok = tok;
2353 if (!yacc_rules || lp == newlb.buffer + 1)
2355 tokoff = lp - 1 - newlb.buffer;
2356 toklen = 1;
2357 midtoken = TRUE;
2359 continue;
2360 } /* if (begtoken) */
2361 } /* if must look at token */
2364 /* Detect end of line, colon, comma, semicolon and various braces
2365 after having handled a token.*/
2366 switch (c)
2368 case ':':
2369 if (definedef != dnone)
2370 break;
2371 switch (objdef)
2373 case otagseen:
2374 objdef = oignore;
2375 make_tag (TRUE);
2376 break;
2377 case omethodtag:
2378 case omethodparm:
2379 objdef = omethodcolon;
2380 methodlen += 1;
2381 GROW_LINEBUFFER (token_name, methodlen+1);
2382 strcat (token_name.buffer, ":");
2383 break;
2385 if (structdef == stagseen)
2386 structdef = scolonseen;
2387 else
2388 switch (funcdef)
2390 case ftagseen:
2391 if (yacc_rules)
2393 make_tag (FALSE);
2394 funcdef = fignore;
2396 break;
2397 case fstartlist:
2398 funcdef = fnone;
2399 break;
2401 break;
2402 case ';':
2403 if (definedef != dnone)
2404 break;
2405 if (cblev == 0)
2406 switch (typdef)
2408 case tend:
2409 make_tag (FALSE);
2410 /* FALLTHRU */
2411 default:
2412 typdef = tnone;
2414 if (funcdef != fignore)
2416 funcdef = fnone;
2417 /* The following instruction invalidates the token.
2418 Probably the token should be invalidated in all
2419 other cases where some state machine is reset. */
2420 tok.valid = FALSE;
2422 if (structdef == stagseen)
2423 structdef = snone;
2424 break;
2425 case ',':
2426 if (definedef != dnone)
2427 break;
2428 switch (objdef)
2430 case omethodtag:
2431 case omethodparm:
2432 make_tag (TRUE);
2433 objdef = oinbody;
2434 break;
2436 if (funcdef != finlist && funcdef != fignore)
2437 funcdef = fnone;
2438 if (structdef == stagseen)
2439 structdef = snone;
2440 break;
2441 case '[':
2442 if (definedef != dnone)
2443 break;
2444 if (cblev == 0 && typdef == tend)
2446 typdef = tignore;
2447 make_tag (FALSE);
2448 break;
2450 if (funcdef != finlist && funcdef != fignore)
2451 funcdef = fnone;
2452 if (structdef == stagseen)
2453 structdef = snone;
2454 break;
2455 case '(':
2456 if (definedef != dnone)
2457 break;
2458 if (objdef == otagseen && parlev == 0)
2459 objdef = oparenseen;
2460 switch (funcdef)
2462 case fnone:
2463 switch (typdef)
2465 case ttypedseen:
2466 case tend:
2467 /* Make sure that the next char is not a '*'.
2468 This handles constructs like:
2469 typedef void OperatorFun (int fun); */
2470 if (*lp != '*')
2472 typdef = tignore;
2473 make_tag (FALSE);
2475 break;
2476 } /* switch (typdef) */
2477 break;
2478 case ftagseen:
2479 funcdef = fstartlist;
2480 break;
2481 case flistseen:
2482 funcdef = finlist;
2483 break;
2485 parlev++;
2486 break;
2487 case ')':
2488 if (definedef != dnone)
2489 break;
2490 if (objdef == ocatseen && parlev == 1)
2492 make_tag (TRUE);
2493 objdef = oignore;
2495 if (--parlev == 0)
2497 switch (funcdef)
2499 case fstartlist:
2500 case finlist:
2501 funcdef = flistseen;
2502 break;
2504 if (cblev == 0 && typdef == tend)
2506 typdef = tignore;
2507 make_tag (FALSE);
2510 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
2511 parlev = 0;
2512 break;
2513 case '{':
2514 if (definedef != dnone)
2515 break;
2516 if (typdef == ttypedseen)
2517 typdef = tinbody;
2518 switch (structdef)
2520 case skeyseen: /* unnamed struct */
2521 structtag = "_anonymous_";
2522 structdef = sinbody;
2523 break;
2524 case stagseen:
2525 case scolonseen: /* named struct */
2526 structdef = sinbody;
2527 make_tag (FALSE);
2528 break;
2530 switch (funcdef)
2532 case flistseen:
2533 make_tag (TRUE);
2534 /* FALLTHRU */
2535 case fignore:
2536 funcdef = fnone;
2537 break;
2538 case fnone:
2539 switch (objdef)
2541 case otagseen:
2542 make_tag (TRUE);
2543 objdef = oignore;
2544 break;
2545 case omethodtag:
2546 case omethodparm:
2547 make_tag (TRUE);
2548 objdef = oinbody;
2549 break;
2550 default:
2551 /* Neutralize `extern "C" {' grot and look inside structs. */
2552 if (cblev == 0 && structdef == snone && typdef == tnone)
2553 cblev = -1;
2556 cblev++;
2557 break;
2558 case '*':
2559 if (definedef != dnone)
2560 break;
2561 if (funcdef == fstartlist)
2562 funcdef = fnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
2563 break;
2564 case '}':
2565 if (definedef != dnone)
2566 break;
2567 if (!noindentypedefs && lp == newlb.buffer + 1)
2569 cblev = 0; /* reset curly brace level if first column */
2570 parlev = 0; /* also reset paren level, just in case... */
2572 else if (cblev > 0)
2573 cblev--;
2574 if (cblev == 0)
2576 if (typdef == tinbody)
2577 typdef = tend;
2578 /* Memory leakage here: the string pointed by structtag is
2579 never released, because I fear to miss something and
2580 break things while freeing the area. The amount of
2581 memory leaked here is the sum of the lengths of the
2582 struct tags.
2583 if (structdef == sinbody)
2584 free (structtag); */
2586 structdef = snone;
2587 structtag = "<error>";
2589 break;
2590 case '+':
2591 case '-':
2592 if (objdef == oinbody && cblev == 0)
2594 objdef = omethodsign;
2595 break;
2597 /* FALLTHRU */
2598 case '=': case '#': case '~': case '&': case '%': case '/':
2599 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2600 if (definedef != dnone)
2601 break;
2602 /* These surely cannot follow a function tag. */
2603 if (funcdef != finlist && funcdef != fignore)
2604 funcdef = fnone;
2605 break;
2606 case '\0':
2607 if (objdef == otagseen)
2609 make_tag (TRUE);
2610 objdef = oignore;
2612 /* If a macro spans multiple lines don't reset its state. */
2613 if (quotednl)
2614 CNL_SAVE_DEFINEDEF;
2615 else
2616 CNL;
2617 break;
2618 } /* switch (c) */
2620 } /* while not eof */
2624 * Process either a C++ file or a C file depending on the setting
2625 * of a global flag.
2627 void
2628 default_C_entries (inf)
2629 FILE *inf;
2631 C_entries (cplusplus ? C_PLPL : 0, inf);
2634 /* Always do plain ANSI C. */
2635 void
2636 plain_C_entries (inf)
2637 FILE *inf;
2639 C_entries (0, inf);
2642 /* Always do C++. */
2643 void
2644 Cplusplus_entries (inf)
2645 FILE *inf;
2647 C_entries (C_PLPL, inf);
2650 /* Always do C*. */
2651 void
2652 Cstar_entries (inf)
2653 FILE *inf;
2655 C_entries (C_STAR, inf);
2658 /* Always do Yacc. */
2659 void
2660 Yacc_entries (inf)
2661 FILE *inf;
2663 C_entries (YACC, inf);
2666 /* Fortran parsing */
2668 char *dbp;
2670 logical
2671 tail (cp)
2672 char *cp;
2674 register int len = 0;
2676 while (*cp && lowcase(*cp) == lowcase(dbp[len]))
2677 cp++, len++;
2678 if (*cp == '\0' && !intoken(dbp[len]))
2680 dbp += len;
2681 return TRUE;
2683 return FALSE;
2686 void
2687 takeprec ()
2689 while (isspace (*dbp))
2690 dbp++;
2691 if (*dbp != '*')
2692 return;
2693 dbp++;
2694 while (isspace (*dbp))
2695 dbp++;
2696 if (strneq (dbp, "(*)", 3))
2698 dbp += 3;
2699 return;
2701 if (!isdigit (*dbp))
2703 --dbp; /* force failure */
2704 return;
2707 dbp++;
2708 while (isdigit (*dbp));
2711 void
2712 getit (inf)
2713 FILE *inf;
2715 register char *cp;
2717 while (isspace (*dbp))
2718 dbp++;
2719 if (*dbp == '\0')
2721 lineno++;
2722 linecharno = charno;
2723 charno += readline (&lb, inf);
2724 dbp = lb.buffer;
2725 if (dbp[5] != '&')
2726 return;
2727 dbp += 6;
2728 while (isspace (*dbp))
2729 dbp++;
2731 if (!isalpha (*dbp)
2732 && *dbp != '_'
2733 && *dbp != '$')
2734 return;
2735 for (cp = dbp + 1;
2736 (*cp
2737 && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
2738 cp++)
2739 continue;
2740 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
2741 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2744 void
2745 Fortran_functions (inf)
2746 FILE *inf;
2748 lineno = 0;
2749 charno = 0;
2751 while (!feof (inf))
2753 lineno++;
2754 linecharno = charno;
2755 charno += readline (&lb, inf);
2756 dbp = lb.buffer;
2757 if (*dbp == '%')
2758 dbp++; /* Ratfor escape to fortran */
2759 while (isspace (*dbp))
2760 dbp++;
2761 if (*dbp == '\0')
2762 continue;
2763 switch (lowcase (*dbp))
2765 case 'i':
2766 if (tail ("integer"))
2767 takeprec ();
2768 break;
2769 case 'r':
2770 if (tail ("real"))
2771 takeprec ();
2772 break;
2773 case 'l':
2774 if (tail ("logical"))
2775 takeprec ();
2776 break;
2777 case 'c':
2778 if (tail ("complex") || tail ("character"))
2779 takeprec ();
2780 break;
2781 case 'd':
2782 if (tail ("double"))
2784 while (isspace (*dbp))
2785 dbp++;
2786 if (*dbp == '\0')
2787 continue;
2788 if (tail ("precision"))
2789 break;
2790 continue;
2792 break;
2794 while (isspace (*dbp))
2795 dbp++;
2796 if (*dbp == '\0')
2797 continue;
2798 switch (lowcase (*dbp))
2800 case 'f':
2801 if (tail ("function"))
2802 getit (inf);
2803 continue;
2804 case 's':
2805 if (tail ("subroutine"))
2806 getit (inf);
2807 continue;
2808 case 'e':
2809 if (tail ("entry"))
2810 getit (inf);
2811 continue;
2812 case 'p':
2813 if (tail ("program"))
2815 getit (inf);
2816 continue;
2818 if (tail ("procedure"))
2819 getit (inf);
2820 continue;
2826 * Bob Weiner, Motorola Inc., 4/3/94
2827 * Unix and microcontroller assembly tag handling
2828 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2830 void
2831 Asm_labels (inf)
2832 FILE *inf;
2834 register char *cp;
2836 lineno = 0;
2837 charno = 0;
2839 while (!feof (inf))
2841 lineno++;
2842 linecharno = charno;
2843 charno += readline (&lb, inf);
2844 cp = lb.buffer;
2846 /* If first char is alphabetic or one of [_.$], test for colon
2847 following identifier. */
2848 if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2850 /* Read past label. */
2851 cp++;
2852 while (isalnum (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2853 cp++;
2854 if (*cp == ':' || isspace (*cp))
2856 /* Found end of label, so copy it and add it to the table. */
2857 pfnote ((CTAGS) ? savenstr(lb.buffer, cp-lb.buffer) : NULL, TRUE,
2858 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2865 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2866 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2868 void
2869 Perl_functions (inf)
2870 FILE *inf;
2872 register char *cp;
2874 lineno = 0;
2875 charno = 0;
2877 while (!feof (inf))
2879 lineno++;
2880 linecharno = charno;
2881 charno += readline (&lb, inf);
2882 cp = lb.buffer;
2884 if (*cp++ == 's' && *cp++ == 'u' && *cp++ == 'b' && isspace(*cp++))
2886 while (*cp && isspace(*cp))
2887 cp++;
2888 while (*cp && ! isspace(*cp) && *cp != '{')
2889 cp++;
2890 pfnote ((CTAGS) ? savenstr (lb.buffer, cp-lb.buffer) : NULL, TRUE,
2891 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2896 /* Added by Mosur Mohan, 4/22/88 */
2897 /* Pascal parsing */
2899 #define GET_NEW_LINE \
2901 linecharno = charno; lineno++; \
2902 charno += 1 + readline (&lb, inf); \
2903 dbp = lb.buffer; \
2907 * Locates tags for procedures & functions. Doesn't do any type- or
2908 * var-definitions. It does look for the keyword "extern" or
2909 * "forward" immediately following the procedure statement; if found,
2910 * the tag is skipped.
2912 void
2913 Pascal_functions (inf)
2914 FILE *inf;
2916 struct linebuffer tline; /* mostly copied from C_entries */
2917 long save_lcno;
2918 int save_lineno, save_len;
2919 char c, *cp, *namebuf;
2921 logical /* each of these flags is TRUE iff: */
2922 incomment, /* point is inside a comment */
2923 inquote, /* point is inside '..' string */
2924 get_tagname, /* point is after PROCEDURE/FUNCTION
2925 keyword, so next item = potential tag */
2926 found_tag, /* point is after a potential tag */
2927 inparms, /* point is within parameter-list */
2928 verify_tag; /* point has passed the parm-list, so the
2929 next token will determine whether this
2930 is a FORWARD/EXTERN to be ignored, or
2931 whether it is a real tag */
2933 lineno = 0;
2934 charno = 0;
2935 dbp = lb.buffer;
2936 *dbp = '\0';
2937 save_len = 0;
2938 initbuffer (&tline);
2940 incomment = inquote = FALSE;
2941 found_tag = FALSE; /* have a proc name; check if extern */
2942 get_tagname = FALSE; /* have found "procedure" keyword */
2943 inparms = FALSE; /* found '(' after "proc" */
2944 verify_tag = FALSE; /* check if "extern" is ahead */
2946 /* long main loop to get next char */
2947 while (!feof (inf))
2949 c = *dbp++;
2950 if (c == '\0') /* if end of line */
2952 GET_NEW_LINE;
2953 if (*dbp == '\0')
2954 continue;
2955 if (!((found_tag && verify_tag) ||
2956 get_tagname))
2957 c = *dbp++; /* only if don't need *dbp pointing
2958 to the beginning of the name of
2959 the procedure or function */
2961 if (incomment)
2963 if (c == '}') /* within { } comments */
2964 incomment = FALSE;
2965 else if (c == '*' && *dbp == ')') /* within (* *) comments */
2967 dbp++;
2968 incomment = FALSE;
2970 continue;
2972 else if (inquote)
2974 if (c == '\'')
2975 inquote = FALSE;
2976 continue;
2978 else
2979 switch (c)
2981 case '\'':
2982 inquote = TRUE; /* found first quote */
2983 continue;
2984 case '{': /* found open { comment */
2985 incomment = TRUE;
2986 continue;
2987 case '(':
2988 if (*dbp == '*') /* found open (* comment */
2990 incomment = TRUE;
2991 dbp++;
2993 else if (found_tag) /* found '(' after tag, i.e., parm-list */
2994 inparms = TRUE;
2995 continue;
2996 case ')': /* end of parms list */
2997 if (inparms)
2998 inparms = FALSE;
2999 continue;
3000 case ';':
3001 if (found_tag && !inparms) /* end of proc or fn stmt */
3003 verify_tag = TRUE;
3004 break;
3006 continue;
3008 if (found_tag && verify_tag && (*dbp != ' '))
3010 /* check if this is an "extern" declaration */
3011 if (*dbp == '\0')
3012 continue;
3013 if (lowcase (*dbp == 'e'))
3015 if (tail ("extern")) /* superfluous, really! */
3017 found_tag = FALSE;
3018 verify_tag = FALSE;
3021 else if (lowcase (*dbp) == 'f')
3023 if (tail ("forward")) /* check for forward reference */
3025 found_tag = FALSE;
3026 verify_tag = FALSE;
3029 if (found_tag && verify_tag) /* not external proc, so make tag */
3031 found_tag = FALSE;
3032 verify_tag = FALSE;
3033 pfnote (namebuf, TRUE,
3034 tline.buffer, save_len, save_lineno, save_lcno);
3035 continue;
3038 if (get_tagname) /* grab name of proc or fn */
3040 if (*dbp == '\0')
3041 continue;
3043 /* save all values for later tagging */
3044 GROW_LINEBUFFER (tline, strlen (lb.buffer) + 1);
3045 strcpy (tline.buffer, lb.buffer);
3046 save_lineno = lineno;
3047 save_lcno = linecharno;
3049 /* grab block name */
3050 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
3051 continue;
3052 namebuf = (CTAGS) ? savenstr (dbp, cp-dbp) : NULL;
3053 dbp = cp; /* set dbp to e-o-token */
3054 save_len = dbp - lb.buffer + 1;
3055 get_tagname = FALSE;
3056 found_tag = TRUE;
3057 continue;
3059 /* and proceed to check for "extern" */
3061 else if (!incomment && !inquote && !found_tag)
3063 /* check for proc/fn keywords */
3064 switch (lowcase (c))
3066 case 'p':
3067 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3068 get_tagname = TRUE;
3069 continue;
3070 case 'f':
3071 if (tail ("unction"))
3072 get_tagname = TRUE;
3073 continue;
3076 } /* while not eof */
3078 free (tline.buffer);
3082 * lisp tag functions
3083 * look for (def or (DEF, quote or QUOTE
3086 L_isdef (strp)
3087 register char *strp;
3089 return ((strp[1] == 'd' || strp[1] == 'D')
3090 && (strp[2] == 'e' || strp[2] == 'E')
3091 && (strp[3] == 'f' || strp[3] == 'F'));
3095 L_isquote (strp)
3096 register char *strp;
3098 return ((*(++strp) == 'q' || *strp == 'Q')
3099 && (*(++strp) == 'u' || *strp == 'U')
3100 && (*(++strp) == 'o' || *strp == 'O')
3101 && (*(++strp) == 't' || *strp == 'T')
3102 && (*(++strp) == 'e' || *strp == 'E')
3103 && isspace(*(++strp)));
3106 void
3107 L_getit ()
3109 register char *cp;
3111 if (*dbp == '\'') /* Skip prefix quote */
3112 dbp++;
3113 else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
3115 dbp += 7;
3116 while (isspace(*dbp))
3117 dbp++;
3119 for (cp = dbp /*+1*/;
3120 *cp && *cp != '(' && *cp != ' ' && *cp != ')';
3121 cp++)
3122 continue;
3123 if (cp == dbp)
3124 return;
3126 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3127 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3130 void
3131 Lisp_functions (inf)
3132 FILE *inf;
3134 lineno = 0;
3135 charno = 0;
3137 while (!feof (inf))
3139 lineno++;
3140 linecharno = charno;
3141 charno += readline (&lb, inf);
3142 dbp = lb.buffer;
3143 if (dbp[0] == '(')
3145 if (L_isdef (dbp))
3147 while (!isspace (*dbp))
3148 dbp++;
3149 while (isspace (*dbp))
3150 dbp++;
3151 L_getit ();
3153 else
3155 /* Check for (foo::defmumble name-defined ... */
3157 dbp++;
3158 while (*dbp && !isspace (*dbp)
3159 && *dbp != ':' && *dbp != '(' && *dbp != ')');
3160 if (*dbp == ':')
3163 dbp++;
3164 while (*dbp == ':');
3166 if (L_isdef (dbp - 1))
3168 while (!isspace (*dbp))
3169 dbp++;
3170 while (isspace (*dbp))
3171 dbp++;
3172 L_getit ();
3181 * Scheme tag functions
3182 * look for (def... xyzzy
3183 * look for (def... (xyzzy
3184 * look for (def ... ((...(xyzzy ....
3185 * look for (set! xyzzy
3188 void get_scheme ();
3190 void
3191 Scheme_functions (inf)
3192 FILE *inf;
3194 lineno = 0;
3195 charno = 0;
3197 while (!feof (inf))
3199 lineno++;
3200 linecharno = charno;
3201 charno += readline (&lb, inf);
3202 dbp = lb.buffer;
3203 if (dbp[0] == '(' &&
3204 (dbp[1] == 'D' || dbp[1] == 'd') &&
3205 (dbp[2] == 'E' || dbp[2] == 'e') &&
3206 (dbp[3] == 'F' || dbp[3] == 'f'))
3208 while (!isspace (*dbp))
3209 dbp++;
3210 /* Skip over open parens and white space */
3211 while (*dbp && (isspace (*dbp) || *dbp == '('))
3212 dbp++;
3213 get_scheme ();
3215 if (dbp[0] == '(' &&
3216 (dbp[1] == 'S' || dbp[1] == 's') &&
3217 (dbp[2] == 'E' || dbp[2] == 'e') &&
3218 (dbp[3] == 'T' || dbp[3] == 't') &&
3219 (dbp[4] == '!' || dbp[4] == '!') &&
3220 (isspace (dbp[5])))
3222 while (!isspace (*dbp))
3223 dbp++;
3224 /* Skip over white space */
3225 while (isspace (*dbp))
3226 dbp++;
3227 get_scheme ();
3232 void
3233 get_scheme ()
3235 register char *cp;
3237 if (*dbp == '\0')
3238 return;
3239 /* Go till you get to white space or a syntactic break */
3240 for (cp = dbp + 1;
3241 *cp && *cp != '(' && *cp != ')' && !isspace (*cp);
3242 cp++)
3243 continue;
3244 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3245 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3248 /* Find tags in TeX and LaTeX input files. */
3250 /* TEX_toktab is a table of TeX control sequences that define tags.
3251 Each TEX_tabent records one such control sequence.
3252 CONVERT THIS TO USE THE Stab TYPE!! */
3253 struct TEX_tabent
3255 char *name;
3256 int len;
3259 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
3261 /* Default set of control sequences to put into TEX_toktab.
3262 The value of environment var TEXTAGS is prepended to this. */
3264 char *TEX_defenv = "\
3265 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3266 :part:appendix:entry:index";
3268 void TEX_mode ();
3269 struct TEX_tabent *TEX_decode_env ();
3270 int TEX_Token ();
3271 #if TeX_named_tokens
3272 void TEX_getit ();
3273 #endif
3275 char TEX_esc = '\\';
3276 char TEX_opgrp = '{';
3277 char TEX_clgrp = '}';
3280 * TeX/LaTeX scanning loop.
3282 void
3283 TeX_functions (inf)
3284 FILE *inf;
3286 char *lasthit;
3288 lineno = 0;
3289 charno = 0;
3291 /* Select either \ or ! as escape character. */
3292 TEX_mode (inf);
3294 /* Initialize token table once from environment. */
3295 if (!TEX_toktab)
3296 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
3298 while (!feof (inf))
3299 { /* Scan each line in file */
3300 lineno++;
3301 linecharno = charno;
3302 charno += readline (&lb, inf);
3303 dbp = lb.buffer;
3304 lasthit = dbp;
3305 while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
3307 register int i;
3309 if (!*(++dbp))
3310 break;
3311 linecharno += dbp - lasthit;
3312 lasthit = dbp;
3313 i = TEX_Token (lasthit);
3314 if (0 <= i)
3316 pfnote (NULL, TRUE,
3317 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3318 #if TeX_named_tokens
3319 TEX_getit (lasthit, TEX_toktab[i].len);
3320 #endif
3321 break; /* We only save a line once */
3327 #define TEX_LESC '\\'
3328 #define TEX_SESC '!'
3329 #define TEX_cmt '%'
3331 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3332 chars accordingly. */
3333 void
3334 TEX_mode (inf)
3335 FILE *inf;
3337 int c;
3339 while ((c = getc (inf)) != EOF)
3341 /* Skip to next line if we hit the TeX comment char. */
3342 if (c == TEX_cmt)
3343 while (c != '\n')
3344 c = getc (inf);
3345 else if (c == TEX_LESC || c == TEX_SESC )
3346 break;
3349 if (c == TEX_LESC)
3351 TEX_esc = TEX_LESC;
3352 TEX_opgrp = '{';
3353 TEX_clgrp = '}';
3355 else
3357 TEX_esc = TEX_SESC;
3358 TEX_opgrp = '<';
3359 TEX_clgrp = '>';
3361 rewind (inf);
3364 /* Read environment and prepend it to the default string.
3365 Build token table. */
3366 struct TEX_tabent *
3367 TEX_decode_env (evarname, defenv)
3368 char *evarname;
3369 char *defenv;
3371 register char *env, *p;
3373 struct TEX_tabent *tab;
3374 int size, i;
3376 /* Append default string to environment. */
3377 env = getenv (evarname);
3378 if (!env)
3379 env = defenv;
3380 else
3381 env = concat (env, defenv, "");
3383 /* Allocate a token table */
3384 for (size = 1, p = env; p;)
3385 if ((p = etags_strchr (p, ':')) && *(++p))
3386 size++;
3387 /* Add 1 to leave room for null terminator. */
3388 tab = xnew (size + 1, struct TEX_tabent);
3390 /* Unpack environment string into token table. Be careful about */
3391 /* zero-length strings (leading ':', "::" and trailing ':') */
3392 for (i = 0; *env;)
3394 p = etags_strchr (env, ':');
3395 if (!p) /* End of environment string. */
3396 p = env + strlen (env);
3397 if (p - env > 0)
3398 { /* Only non-zero strings. */
3399 tab[i].name = savenstr (env, p - env);
3400 tab[i].len = strlen (tab[i].name);
3401 i++;
3403 if (*p)
3404 env = p + 1;
3405 else
3407 tab[i].name = NULL; /* Mark end of table. */
3408 tab[i].len = 0;
3409 break;
3412 return tab;
3415 #if TeX_named_tokens
3416 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3417 The name being defined actually starts at (NAME + LEN + 1).
3418 But we seem to include the TeX command in the tag name. */
3419 void
3420 TEX_getit (name, len)
3421 char *name;
3422 int len;
3424 char *p = name + len;
3426 if (*name == '\0')
3427 return;
3429 /* Let tag name extend to next group close (or end of line) */
3430 while (*p && *p != TEX_clgrp)
3431 p++;
3432 pfnote (savenstr (name, p-name), TRUE,
3433 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3435 #endif
3437 /* If the text at CP matches one of the tag-defining TeX command names,
3438 return the pointer to the first occurrence of that command in TEX_toktab.
3439 Otherwise return -1.
3440 Keep the capital `T' in `Token' for dumb truncating compilers
3441 (this distinguishes it from `TEX_toktab' */
3443 TEX_Token (cp)
3444 char *cp;
3446 int i;
3448 for (i = 0; TEX_toktab[i].len > 0; i++)
3449 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
3450 return i;
3451 return -1;
3454 /* Support for Prolog. */
3456 /* Whole head (not only functor, but also arguments)
3457 is gotten in compound term. */
3458 void
3459 prolog_getit (s)
3460 char *s;
3462 char *save_s;
3463 int insquote, npar;
3465 save_s = s;
3466 insquote = FALSE;
3467 npar = 0;
3468 while (1)
3470 if (s[0] == '\0') /* syntax error. */
3471 return;
3472 else if (insquote && s[0] == '\'' && s[1] == '\'')
3473 s += 2;
3474 else if (s[0] == '\'')
3476 insquote = !insquote;
3477 s++;
3479 else if (!insquote && s[0] == '(')
3481 npar++;
3482 s++;
3484 else if (!insquote && s[0] == ')')
3486 npar--;
3487 s++;
3488 if (npar == 0)
3489 break;
3490 else if (npar < 0) /* syntax error. */
3491 return;
3493 else if (!insquote && s[0] == '.'
3494 && (isspace (s[1]) || s[1] == '\0'))
3495 { /* fullstop. */
3496 if (npar != 0) /* syntax error. */
3497 return;
3498 s++;
3499 break;
3501 else
3502 s++;
3504 pfnote ((CTAGS) ? savenstr (save_s, s-save_s) : NULL, TRUE,
3505 save_s, s-save_s, lineno, linecharno);
3508 /* It is assumed that prolog predicate starts from column 0. */
3509 void
3510 Prolog_functions (inf)
3511 FILE *inf;
3513 void skip_comment (), prolog_getit ();
3515 lineno = linecharno = charno = 0;
3516 while (!feof (inf))
3518 lineno++;
3519 linecharno += charno;
3520 charno = readline (&lb, inf) + 1; /* 1 for newline. */
3521 dbp = lb.buffer;
3522 if (isspace (dbp[0])) /* not predicate header. */
3523 continue;
3524 else if (dbp[0] == '%') /* comment. */
3525 continue;
3526 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
3527 skip_comment (&lb, inf, &lineno, &linecharno);
3528 else /* found. */
3529 prolog_getit (dbp);
3533 void
3534 skip_comment (plb, inf, plineno, plinecharno)
3535 struct linebuffer *plb;
3536 FILE *inf;
3537 int *plineno; /* result */
3538 long *plinecharno; /* result */
3540 char *cp;
3544 for (cp = plb->buffer; *cp != '\0'; cp++)
3545 if (cp[0] == '*' && cp[1] == '/')
3546 return;
3547 (*plineno)++;
3548 *plinecharno += readline (plb, inf) + 1; /* 1 for newline. */
3550 while (!feof(inf));
3553 #ifdef ETAGS_REGEXPS
3554 /* Take a string like "/blah/" and turn it into "blah", making sure
3555 that the first and last characters are the same, and handling
3556 quoted separator characters. Actually, stops on the occurrence of
3557 an unquoted separator. Also turns "\t" into a Tab character.
3558 Returns pointer to terminating separator. Works in place. Null
3559 terminates name string. */
3560 char *
3561 scan_separators (name)
3562 char *name;
3564 char sep = name[0];
3565 char *copyto = name;
3566 logical quoted = FALSE;
3568 for (++name; *name != '\0'; ++name)
3570 if (quoted)
3572 if (*name == 't')
3573 *copyto++ = '\t';
3574 else if (*name == sep)
3575 *copyto++ = sep;
3576 else
3578 /* Something else is quoted, so preserve the quote. */
3579 *copyto++ = '\\';
3580 *copyto++ = *name;
3582 quoted = FALSE;
3584 else if (*name == '\\')
3585 quoted = TRUE;
3586 else if (*name == sep)
3587 break;
3588 else
3589 *copyto++ = *name;
3592 /* Terminate copied string. */
3593 *copyto = '\0';
3594 return name;
3597 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3598 expression, into a real regular expression by compiling it. */
3599 void
3600 add_regex (regexp_pattern)
3601 char *regexp_pattern;
3603 char *name;
3604 const char *err;
3605 struct re_pattern_buffer *patbuf;
3607 if (regexp_pattern == NULL)
3609 /* Remove existing regexps. */
3610 num_patterns = 0;
3611 patterns = NULL;
3612 return;
3615 if (regexp_pattern[0] == '\0')
3617 error ("missing regexp", 0);
3618 return;
3620 if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
3622 error ("%s: unterminated regexp", regexp_pattern);
3623 return;
3625 name = scan_separators (regexp_pattern);
3626 if (regexp_pattern[0] == '\0')
3628 error ("null regexp", 0);
3629 return;
3631 (void) scan_separators (name);
3633 patbuf = xnew (1, struct re_pattern_buffer);
3634 patbuf->translate = NULL;
3635 patbuf->fastmap = NULL;
3636 patbuf->buffer = NULL;
3637 patbuf->allocated = 0;
3639 err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
3640 if (err != NULL)
3642 error ("%s while compiling pattern", err);
3643 return;
3646 num_patterns += 1;
3647 if (num_patterns == 1)
3648 patterns = xnew (1, struct pattern);
3649 else
3650 patterns = ((struct pattern *)
3651 xrealloc (patterns,
3652 (num_patterns * sizeof (struct pattern))));
3653 patterns[num_patterns - 1].pattern = patbuf;
3654 patterns[num_patterns - 1].name_pattern = savestr (name);
3655 patterns[num_patterns - 1].error_signaled = FALSE;
3659 * Do the substitutions indicated by the regular expression and
3660 * arguments.
3662 char *
3663 substitute (in, out, regs)
3664 char *in, *out;
3665 struct re_registers *regs;
3667 char *result = NULL, *t;
3668 int size = 0;
3670 /* Pass 1: figure out how much size to allocate. */
3671 for (t = out; *t; ++t)
3673 if (*t == '\\')
3675 ++t;
3676 if (!*t)
3678 fprintf (stderr, "%s: pattern substitution ends prematurely\n",
3679 progname);
3680 return NULL;
3682 if (isdigit (*t))
3684 int dig = *t - '0';
3685 size += regs->end[dig] - regs->start[dig];
3690 /* Allocate space and do the substitutions. */
3691 result = xnew (size + 1, char);
3692 size = 0;
3693 for (; *out; ++out)
3695 if (*out == '\\')
3697 ++out;
3698 if (isdigit (*out))
3700 /* Using "dig2" satisfies my debugger. Bleah. */
3701 int dig2 = *out - '0';
3702 strncpy (result + size, in + regs->start[dig2],
3703 regs->end[dig2] - regs->start[dig2]);
3704 size += regs->end[dig2] - regs->start[dig2];
3706 else
3707 result[size++] = *out;
3709 else
3710 result[size++] = *out;
3712 result[size] = '\0';
3714 return result;
3717 #endif /* ETAGS_REGEXPS */
3718 /* Initialize a linebuffer for use */
3719 void
3720 initbuffer (linebuffer)
3721 struct linebuffer *linebuffer;
3723 linebuffer->size = 200;
3724 linebuffer->buffer = xnew (200, char);
3728 * Read a line of text from `stream' into `linebuffer'.
3729 * Return the number of characters read from `stream',
3730 * which is the length of the line including the newline, if any.
3732 long
3733 readline_internal (linebuffer, stream)
3734 struct linebuffer *linebuffer;
3735 register FILE *stream;
3737 char *buffer = linebuffer->buffer;
3738 register char *p = linebuffer->buffer;
3739 register char *pend;
3740 int chars_deleted;
3742 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
3744 while (1)
3746 register int c = getc (stream);
3747 if (p == pend)
3749 linebuffer->size *= 2;
3750 buffer = (char *) xrealloc (buffer, linebuffer->size);
3751 p += buffer - linebuffer->buffer;
3752 pend = buffer + linebuffer->size;
3753 linebuffer->buffer = buffer;
3755 if (c == EOF)
3757 chars_deleted = 0;
3758 break;
3760 if (c == '\n')
3762 if (p > buffer && p[-1] == '\r')
3764 *--p = '\0';
3765 chars_deleted = 2;
3767 else
3769 *p = '\0';
3770 chars_deleted = 1;
3772 break;
3774 *p++ = c;
3777 return p - buffer + chars_deleted;
3781 * Like readline_internal, above, but try to match the input
3782 * line against any existing regular expressions.
3784 long
3785 readline (linebuffer, stream)
3786 struct linebuffer *linebuffer;
3787 FILE *stream;
3789 /* Read new line. */
3790 long result = readline_internal (linebuffer, stream);
3791 #ifdef ETAGS_REGEXPS
3792 int i;
3794 /* Match against all listed patterns. */
3795 for (i = 0; i < num_patterns; ++i)
3797 int match = re_match (patterns[i].pattern, linebuffer->buffer,
3798 (int)result, 0, &patterns[i].regs);
3799 switch (match)
3801 case -2:
3802 /* Some error. */
3803 if (!patterns[i].error_signaled)
3805 error ("error while matching pattern %d", i);
3806 patterns[i].error_signaled = TRUE;
3808 break;
3809 case -1:
3810 /* No match. */
3811 break;
3812 default:
3813 /* Match occurred. Construct a tag. */
3814 if (patterns[i].name_pattern[0] != '\0')
3816 /* Make a named tag. */
3817 char *name = substitute (linebuffer->buffer,
3818 patterns[i].name_pattern,
3819 &patterns[i].regs);
3820 if (name != NULL)
3821 pfnote (name, TRUE,
3822 linebuffer->buffer, match, lineno, linecharno);
3824 else
3826 /* Make an unnamed tag. */
3827 pfnote (NULL, TRUE,
3828 linebuffer->buffer, match, lineno, linecharno);
3830 break;
3833 #endif /* ETAGS_REGEXPS */
3835 return result;
3839 * Read a file, but do no processing. This is used to do regexp
3840 * matching on files that have no language defined.
3842 void
3843 just_read_file (inf)
3844 FILE *inf;
3846 while (!feof (inf))
3848 ++lineno;
3849 linecharno = charno;
3850 charno += readline (&lb, inf) + 1;
3856 * Return a pointer to a space of size strlen(cp)+1 allocated
3857 * with xnew where the string CP has been copied.
3859 char *
3860 savestr (cp)
3861 char *cp;
3863 return savenstr (cp, strlen (cp));
3867 * Return a pointer to a space of size LEN+1 allocated with xnew where
3868 * the string CP has been copied for at most the first LEN characters.
3870 char *
3871 savenstr (cp, len)
3872 char *cp;
3873 int len;
3875 register char *dp;
3877 dp = xnew (len + 1, char);
3878 strncpy (dp, cp, len);
3879 dp[len] = '\0';
3880 return dp;
3884 * Return the ptr in sp at which the character c last
3885 * appears; NULL if not found
3887 * Identical to System V strrchr, included for portability.
3889 char *
3890 etags_strrchr (sp, c)
3891 register char *sp, c;
3893 register char *r;
3895 r = NULL;
3898 if (*sp == c)
3899 r = sp;
3900 } while (*sp++);
3901 return r;
3906 * Return the ptr in sp at which the character c first
3907 * appears; NULL if not found
3909 * Identical to System V strchr, included for portability.
3911 char *
3912 etags_strchr (sp, c)
3913 register char *sp, c;
3917 if (*sp == c)
3918 return sp;
3919 } while (*sp++);
3920 return NULL;
3923 /* Print error message and exit. */
3924 void
3925 fatal (s1, s2)
3926 char *s1, *s2;
3928 error (s1, s2);
3929 exit (BAD);
3932 void
3933 pfatal (s1)
3934 char *s1;
3936 perror (s1);
3937 exit (BAD);
3940 void
3941 suggest_asking_for_help ()
3943 fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
3944 progname);
3945 exit (BAD);
3948 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3949 void
3950 error (s1, s2)
3951 char *s1, *s2;
3953 fprintf (stderr, "%s: ", progname);
3954 fprintf (stderr, s1, s2);
3955 fprintf (stderr, "\n");
3958 /* Return a newly-allocated string whose contents
3959 concatenate those of s1, s2, s3. */
3960 char *
3961 concat (s1, s2, s3)
3962 char *s1, *s2, *s3;
3964 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3965 char *result = xnew (len1 + len2 + len3 + 1, char);
3967 strcpy (result, s1);
3968 strcpy (result + len1, s2);
3969 strcpy (result + len1 + len2, s3);
3970 result[len1 + len2 + len3] = '\0';
3972 return result;
3975 /* Does the same work as the system V getcwd, but does not need to
3976 guess the buffer size in advance. */
3977 char *
3978 etags_getcwd ()
3980 #ifdef DOS_NT
3981 char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
3983 getwd (path);
3984 p = path;
3985 while (*p)
3986 if (*p == '\\')
3987 *p++ = '/';
3988 else
3989 *p++ = lowcase (*p);
3991 return strdup (path);
3992 #else /* not DOS_NT */
3993 #if HAVE_GETCWD
3994 int bufsize = 200;
3995 char *path = xnew (bufsize, char);
3997 while (getcwd (path, bufsize) == NULL)
3999 if (errno != ERANGE)
4000 pfatal ("getcwd");
4001 bufsize *= 2;
4002 path = xnew (bufsize, char);
4005 return path;
4006 #else /* not DOS_NT and not HAVE_GETCWD */
4007 struct linebuffer path;
4008 FILE *pipe;
4010 initbuffer (&path);
4011 pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
4012 if (pipe == NULL || readline_internal (&path, pipe) == 0)
4013 pfatal ("pwd");
4014 pclose (pipe);
4016 return path.buffer;
4017 #endif /* not HAVE_GETCWD */
4018 #endif /* not DOS_NT */
4021 /* Return a newly allocated string containing the filename
4022 of FILE relative to the absolute directory DIR (which
4023 should end with a slash). */
4024 char *
4025 relative_filename (file, dir)
4026 char *file, *dir;
4028 char *fp, *dp, *abs, *res;
4030 /* Find the common root of file and dir. */
4031 abs = absolute_filename (file, cwd);
4032 fp = abs;
4033 dp = dir;
4034 while (*fp++ == *dp++)
4035 continue;
4038 fp--;
4039 dp--;
4041 while (*fp != '/');
4043 /* Build a sequence of "../" strings for the resulting relative filename. */
4044 for (dp = etags_strchr (dp + 1, '/'), res = "";
4045 dp != NULL;
4046 dp = etags_strchr (dp + 1, '/'))
4048 res = concat (res, "../", "");
4051 /* Add the filename relative to the common root of file and dir. */
4052 res = concat (res, fp + 1, "");
4053 free (abs);
4055 return res;
4058 /* Return a newly allocated string containing the
4059 absolute filename of FILE given CWD (which should
4060 end with a slash). */
4061 char *
4062 absolute_filename (file, cwd)
4063 char *file, *cwd;
4065 char *slashp, *cp, *res;
4067 if (absolutefn (file))
4068 res = concat (file, "", "");
4069 else
4070 res = concat (cwd, file, "");
4072 /* Delete the "/dirname/.." and "/." substrings. */
4073 slashp = etags_strchr (res, '/');
4074 while (slashp != NULL && slashp[0] != '\0')
4076 if (slashp[1] == '.')
4078 if (slashp[2] == '.'
4079 && (slashp[3] == '/' || slashp[3] == '\0'))
4081 cp = slashp;
4083 cp--;
4084 while (cp >= res && *cp != '/');
4085 if (*cp == '/')
4087 strcpy (cp, slashp + 3);
4089 else /* else (cp == res) */
4091 if (slashp[3] != '\0')
4092 strcpy (cp, slashp + 4);
4093 else
4094 return ".";
4096 slashp = cp;
4097 continue;
4099 else if (slashp[2] == '/' || slashp[2] == '\0')
4101 strcpy (slashp, slashp + 2);
4102 continue;
4106 slashp = etags_strchr (slashp + 1, '/');
4109 return res;
4112 /* Return a newly allocated string containing the absolute
4113 filename of dir where FILE resides given CWD (which should
4114 end with a slash). */
4115 char *
4116 absolute_dirname (file, cwd)
4117 char *file, *cwd;
4119 char *slashp, *res;
4120 char save;
4122 slashp = etags_strrchr (file, '/');
4123 if (slashp == NULL)
4124 return cwd;
4125 save = slashp[1];
4126 slashp[1] = '\0';
4127 res = absolute_filename (file, cwd);
4128 slashp[1] = save;
4130 return res;
4133 /* Like malloc but get fatal error if memory is exhausted. */
4134 long *
4135 xmalloc (size)
4136 unsigned int size;
4138 long *result = (long *) malloc (size);
4139 if (result == NULL)
4140 fatal ("virtual memory exhausted", 0);
4141 return result;
4144 long *
4145 xrealloc (ptr, size)
4146 char *ptr;
4147 unsigned int size;
4149 long *result = (long *) realloc (ptr, size);
4150 if (result == NULL)
4151 fatal ("virtual memory exhausted");
4152 return result;