(bookmark-locate): ;;;###autoload this alias.
[emacs.git] / lib-src / etags.c
blobba1ac984156e4029cddb35ecc6b23cf4147a3f1c
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` (F.Potorti@cnuce.cnr.it) is the current maintainer.
34 char pot_etags_version[] = "@(#) pot revision number is 11.59";
36 #define TRUE 1
37 #define FALSE 0
39 #ifndef DEBUG
40 # define DEBUG FALSE
41 #endif
43 #ifdef MSDOS
44 # include <string.h>
45 # include <fcntl.h>
46 # include <sys/param.h>
47 #endif /* MSDOS */
49 #ifdef WINDOWSNT
50 # include <stdlib.h>
51 # include <fcntl.h>
52 # include <string.h>
53 # include <io.h>
54 # define MAXPATHLEN _MAX_PATH
55 #endif
57 #ifdef HAVE_CONFIG_H
58 # include <config.h>
59 /* On some systems, Emacs defines static as nothing for the sake
60 of unexec. We don't want that here since we don't use unexec. */
61 # undef static
62 #endif
64 #include <stdio.h>
65 #include <ctype.h>
66 #include <errno.h>
67 #ifndef errno
68 extern int errno;
69 #endif
70 #include <sys/types.h>
71 #include <sys/stat.h>
73 #if !defined (S_ISREG) && defined (S_IFREG)
74 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
75 #endif
77 #include <getopt.h>
79 #ifdef ETAGS_REGEXPS
80 # include <regex.h>
81 #endif /* ETAGS_REGEXPS */
83 /* Define CTAGS to make the program "ctags" compatible with the usual one.
84 Let it undefined to make the program "etags", which makes emacs-style
85 tag tables and tags typedefs, #defines and struct/union/enum by default. */
86 #ifdef CTAGS
87 # undef CTAGS
88 # define CTAGS TRUE
89 #else
90 # define CTAGS FALSE
91 #endif
93 /* Exit codes for success and failure. */
94 #ifdef VMS
95 # define GOOD 1
96 # define BAD 0
97 #else
98 # define GOOD 0
99 # define BAD 1
100 #endif
102 /* C extensions. */
103 #define C_PLPL 0x00001 /* C++ */
104 #define C_STAR 0x00003 /* C* */
105 #define YACC 0x10000 /* yacc file */
107 #define streq(s,t) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strcmp(s,t))
108 #define strneq(s,t,n) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strncmp(s,t,n))
110 #define lowcase(c) tolower ((unsigned char)c)
112 #define iswhite(arg) (_wht[arg]) /* T if char is white */
113 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
114 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
115 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
117 #ifdef DOS_NT
118 # define absolutefn(fn) (fn[0] == '/' \
119 || (fn[1] == ':' && fn[2] == '/'))
120 #else
121 # define absolutefn(fn) (fn[0] == '/')
122 #endif
126 * xnew -- allocate storage
128 * SYNOPSIS: Type *xnew (int n, Type);
130 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
132 typedef int logical;
134 typedef struct nd_st
135 { /* sorting structure */
136 char *name; /* function or type name */
137 char *file; /* file name */
138 logical is_func; /* use pattern or line no */
139 logical been_warned; /* set if noticed dup */
140 int lno; /* line number tag is on */
141 long cno; /* character number line starts on */
142 char *pat; /* search pattern */
143 struct nd_st *left, *right; /* left and right sons */
144 } NODE;
146 extern char *getenv ();
148 char *concat ();
149 char *savenstr (), *savestr ();
150 char *etags_strchr (), *etags_strrchr ();
151 char *etags_getcwd ();
152 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
153 long *xmalloc (), *xrealloc ();
155 typedef void Lang_function ();
156 #if FALSE /* many compilers barf on this */
157 Lang_function Asm_labels;
158 Lang_function default_C_entries;
159 Lang_function C_entries;
160 Lang_function Cplusplus_entries;
161 Lang_function Cstar_entries;
162 Lang_function Erlang_functions;
163 Lang_function Fortran_functions;
164 Lang_function Yacc_entries;
165 Lang_function Lisp_functions;
166 Lang_function Pascal_functions;
167 Lang_function Perl_functions;
168 Lang_function Prolog_functions;
169 Lang_function Scheme_functions;
170 Lang_function TeX_functions;
171 Lang_function just_read_file;
172 #else /* so let's write it this way */
173 void Asm_labels ();
174 void C_entries ();
175 void default_C_entries ();
176 void plain_C_entries ();
177 void Cplusplus_entries ();
178 void Cstar_entries ();
179 void Erlang_functions ();
180 void Fortran_functions ();
181 void Yacc_entries ();
182 void Lisp_functions ();
183 void Pascal_functions ();
184 void Perl_functions ();
185 void Prolog_functions ();
186 void Scheme_functions ();
187 void TeX_functions ();
188 void just_read_file ();
189 #endif
191 Lang_function *get_language_from_name ();
192 Lang_function *get_language_from_interpreter ();
193 Lang_function *get_language_from_suffix ();
194 int total_size_of_entries ();
195 long readline ();
196 long readline_internal ();
197 #ifdef ETAGS_REGEXPS
198 void add_regex ();
199 #endif
200 void add_node ();
201 void error ();
202 void suggest_asking_for_help ();
203 void fatal (), pfatal ();
204 void find_entries ();
205 void free_tree ();
206 void getit ();
207 void init ();
208 void initbuffer ();
209 void pfnote ();
210 void process_file ();
211 void put_entries ();
212 void takeprec ();
215 char searchar = '/'; /* use /.../ searches */
217 int lineno; /* line number of current line */
218 long charno; /* current character number */
220 long linecharno; /* charno of start of line; not used by C,
221 but by every other language. */
223 char *curfile; /* current input file name */
224 char *tagfile; /* output file */
225 char *progname; /* name this program was invoked with */
226 char *cwd; /* current working directory */
227 char *tagfiledir; /* directory of tagfile */
229 FILE *tagf; /* ioptr for tags file */
230 NODE *head; /* the head of the binary tree of tags */
233 * A `struct linebuffer' is a structure which holds a line of text.
234 * `readline' reads a line from a stream into a linebuffer and works
235 * regardless of the length of the line.
237 #define GROW_LINEBUFFER(buf,toksize) \
238 while (buf.size < toksize) \
239 buf.buffer = (char *) xrealloc (buf.buffer, buf.size *= 2)
240 struct linebuffer
242 long size;
243 char *buffer;
246 struct linebuffer lb; /* the current line */
247 struct linebuffer token_name; /* used by C_entries as a temporary area */
248 struct
250 long linepos;
251 struct linebuffer lb; /* used by C_entries instead of lb */
252 } lbs[2];
254 /* boolean "functions" (see init) */
255 logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
256 char
257 /* white chars */
258 *white = " \f\t\n\013",
259 /* token ending chars */
260 *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
261 /* token starting chars */
262 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
263 /* valid in-token chars */
264 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
266 logical append_to_tagfile; /* -a: append to tags */
267 /* The following three default to TRUE for etags, but to FALSE for ctags. */
268 logical typedefs; /* -t: create tags for typedefs */
269 logical typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
270 /* 0 struct/enum/union decls, and C++ */
271 /* member functions. */
272 logical constantypedefs; /* -d: create tags for C #define and enum */
273 /* constants. Enum consts not implemented. */
274 /* -D: opposite of -d. Default under ctags. */
275 logical update; /* -u: update tags */
276 logical vgrind_style; /* -v: create vgrind style index output */
277 logical no_warnings; /* -w: suppress warnings */
278 logical cxref_style; /* -x: create cxref style output */
279 logical cplusplus; /* .[hc] means C++, not C */
280 logical noindentypedefs; /* -I: ignore indentation in C */
282 struct option longopts[] =
284 { "append", no_argument, NULL, 'a' },
285 { "backward-search", no_argument, NULL, 'B' },
286 { "c++", no_argument, NULL, 'C' },
287 { "cxref", no_argument, NULL, 'x' },
288 { "defines", no_argument, NULL, 'd' },
289 { "help", no_argument, NULL, 'h' },
290 { "help", no_argument, NULL, 'H' },
291 { "ignore-indentation", no_argument, NULL, 'I' },
292 { "include", required_argument, NULL, 'i' },
293 { "language", required_argument, NULL, 'l' },
294 { "no-defines", no_argument, NULL, 'D' },
295 { "no-regex", no_argument, NULL, 'R' },
296 { "no-warn", no_argument, NULL, 'w' },
297 { "output", required_argument, NULL, 'o' },
298 { "regex", required_argument, NULL, 'r' },
299 { "typedefs", no_argument, NULL, 't' },
300 { "typedefs-and-c++", no_argument, NULL, 'T' },
301 { "update", no_argument, NULL, 'u' },
302 { "version", no_argument, NULL, 'V' },
303 { "vgrind", no_argument, NULL, 'v' },
304 { 0 }
307 #ifdef ETAGS_REGEXPS
308 /* Structure defining a regular expression. Elements are
309 the compiled pattern, and the name string. */
310 struct pattern
312 struct re_pattern_buffer *pattern;
313 struct re_registers regs;
314 char *name_pattern;
315 logical error_signaled;
318 /* Number of regexps found. */
319 int num_patterns = 0;
321 /* Array of all regexps. */
322 struct pattern *patterns = NULL;
323 #endif /* ETAGS_REGEXPS */
326 * Language stuff.
329 /* Non-NULL if language fixed. */
330 Lang_function *lang_func = NULL;
332 /* Assembly code */
333 char *Asm_suffixes [] = { "a", /* Unix assembler */
334 "asm", /* Microcontroller assembly */
335 "def", /* BSO/Tasking definition includes */
336 "inc", /* Microcontroller include files */
337 "ins", /* Microcontroller include files */
338 "s", "sa", /* Unix assembler */
339 "src", /* BSO/Tasking C compiler output */
340 NULL
343 /* Note that .c and .h can be considered C++, if the --c++ flag was
344 given. That is why default_C_entries is called here. */
345 char *default_C_suffixes [] =
346 { "c", "h", NULL };
348 /* .M is for Objective C++ files. */
349 char *Cplusplus_suffixes [] =
350 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL};
352 char *Cstar_suffixes [] =
353 { "cs", "hs", NULL };
355 char *Erlang_suffixes [] =
356 { "erl", "hrl", NULL };
358 char *Fortran_suffixes [] =
359 { "F", "f", "f90", "for", NULL };
361 char *Lisp_suffixes [] =
362 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL };
364 char *Pascal_suffixes [] =
365 { "p", "pas", NULL };
367 char *Perl_suffixes [] =
368 { "pl", "pm", NULL };
369 char *Perl_interpreters [] =
370 { "perl", "@PERL@", NULL };
372 char *plain_C_suffixes [] =
373 { "pc", /* Pro*C file */
374 "m", /* Objective C file */
375 "lm", /* Objective lex file */
376 NULL };
378 char *Prolog_suffixes [] =
379 { "prolog", NULL };
381 /* Can't do the `SCM' or `scm' prefix with a version number. */
382 char *Scheme_suffixes [] =
383 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL };
385 char *TeX_suffixes [] =
386 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL };
388 char *Yacc_suffixes [] =
389 { "y", "ym", NULL }; /* .ym is Objective yacc file */
391 /* Table of language names and corresponding functions, file suffixes
392 and interpreter names.
393 It is ok for a given function to be listed under more than one
394 name. I just didn't. */
395 struct lang_entry
397 char *name;
398 Lang_function *function;
399 char **suffixes;
400 char **interpreters;
403 struct lang_entry lang_names [] =
405 { "asm", Asm_labels, Asm_suffixes, NULL },
406 { "c", default_C_entries, default_C_suffixes, NULL },
407 { "c++", Cplusplus_entries, Cplusplus_suffixes, NULL },
408 { "c*", Cstar_entries, Cstar_suffixes, NULL },
409 { "erlang", Erlang_functions, Erlang_suffixes, NULL },
410 { "fortran", Fortran_functions, Fortran_suffixes, NULL },
411 { "lisp", Lisp_functions, Lisp_suffixes, NULL },
412 { "pascal", Pascal_functions, Pascal_suffixes, NULL },
413 { "perl", Perl_functions, Perl_suffixes, Perl_interpreters },
414 { "proc", plain_C_entries, plain_C_suffixes, NULL },
415 { "prolog", Prolog_functions, Prolog_suffixes, NULL },
416 { "scheme", Scheme_functions, Scheme_suffixes, NULL },
417 { "tex", TeX_functions, TeX_suffixes, NULL },
418 { "yacc", Yacc_entries, Yacc_suffixes, NULL },
419 { "auto", NULL }, /* default guessing scheme */
420 { "none", just_read_file }, /* regexp matching only */
421 { NULL, NULL } /* end of list */
425 void
426 print_language_names ()
428 struct lang_entry *lang;
429 char **ext;
431 puts ("\nThese are the currently supported languages, along with the\n\
432 default file name suffixes:");
433 for (lang = lang_names; lang->name != NULL; lang++)
435 printf ("\t%s\t", lang->name);
436 if (lang->suffixes != NULL)
437 for (ext = lang->suffixes; *ext != NULL; ext++)
438 printf (" .%s", *ext);
439 puts ("");
441 puts ("Where `auto' means use default language for files based on file\n\
442 name suffix, and `none' means only do regexp processing on files.\n\
443 If no language is specified and no matching suffix is found,\n\
444 the first line of the file is read for a sharp-bang (#!) sequence\n\
445 followed by the name of an interpreter. If no such sequence is found,\n\
446 Fortran is tried first; if no tags are found, C is tried next.");
449 #ifndef VERSION
450 # define VERSION "19"
451 #endif
452 void
453 print_version ()
455 printf ("%s for Emacs version %s\n", (CTAGS) ? "ctags" : "etags", VERSION);
457 exit (GOOD);
460 void
461 print_help ()
463 printf ("These are the options accepted by %s. You may use unambiguous\n\
464 abbreviations for the long option names. A - as file name means read\n\
465 names from stdin.", progname);
466 if (!CTAGS)
467 printf (" Absolute names are stored in the output file as they\n\
468 are. Relative ones are stored relative to the output file's directory.");
469 puts ("\n");
471 puts ("-a, --append\n\
472 Append tag entries to existing tags file.");
474 if (CTAGS)
475 puts ("-B, --backward-search\n\
476 Write the search commands for the tag entries using '?', the\n\
477 backward-search command instead of '/', the forward-search command.");
479 puts ("-C, --c++\n\
480 Treat files whose name suffix defaults to C language as C++ files.");
482 if (CTAGS)
483 puts ("-d, --defines\n\
484 Create tag entries for constant C #defines, too.");
485 else
486 puts ("-D, --no-defines\n\
487 Don't create tag entries for constant C #defines. This makes\n\
488 the tags file smaller.");
490 if (!CTAGS)
492 puts ("-i FILE, --include=FILE\n\
493 Include a note in tag file indicating that, when searching for\n\
494 a tag, one should also consult the tags file FILE after\n\
495 checking the current file.");
496 puts ("-l LANG, --language=LANG\n\
497 Force the following files to be considered as written in the\n\
498 named language up to the next --language=LANG option.");
501 #ifdef ETAGS_REGEXPS
502 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
503 Make a tag for each line matching pattern REGEXP in the\n\
504 following files. REGEXP is anchored (as if preceded by ^).\n\
505 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
506 named tags can be created with:\n\
507 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
508 puts ("-R, --no-regex\n\
509 Don't create tags from regexps for the following files.");
510 #endif /* ETAGS_REGEXPS */
511 puts ("-o FILE, --output=FILE\n\
512 Write the tags to FILE.");
513 puts ("-I, --ignore-indentation\n\
514 Don't rely on indentation quite as much as normal. Currently,\n\
515 this means not to assume that a closing brace in the first\n\
516 column is the final brace of a function or structure\n\
517 definition in C and C++.");
519 if (CTAGS)
521 puts ("-t, --typedefs\n\
522 Generate tag entries for C typedefs.");
523 puts ("-T, --typedefs-and-c++\n\
524 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
525 and C++ member functions.");
526 puts ("-u, --update\n\
527 Update the tag entries for the given files, leaving tag\n\
528 entries for other files in place. Currently, this is\n\
529 implemented by deleting the existing entries for the given\n\
530 files and then rewriting the new entries at the end of the\n\
531 tags file. It is often faster to simply rebuild the entire\n\
532 tag file than to use this.");
533 puts ("-v, --vgrind\n\
534 Generates an index of items intended for human consumption,\n\
535 similar to the output of vgrind. The index is sorted, and\n\
536 gives the page number of each item.");
537 puts ("-w, --no-warn\n\
538 Suppress warning messages about entries defined in multiple\n\
539 files.");
540 puts ("-x, --cxref\n\
541 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
542 The output uses line numbers instead of page numbers, but\n\
543 beyond that the differences are cosmetic; try both to see\n\
544 which you like.");
547 puts ("-V, --version\n\
548 Print the version of the program.\n\
549 -h, --help\n\
550 Print this help message.");
552 print_language_names ();
554 exit (GOOD);
558 enum argument_type
560 at_language,
561 at_regexp,
562 at_filename
565 /* This structure helps us allow mixing of --lang and filenames. */
566 typedef struct
568 enum argument_type arg_type;
569 char *what;
570 Lang_function *function;
571 } argument;
573 #ifdef VMS /* VMS specific functions */
575 #define EOS '\0'
577 /* This is a BUG! ANY arbitrary limit is a BUG!
578 Won't someone please fix this? */
579 #define MAX_FILE_SPEC_LEN 255
580 typedef struct {
581 short curlen;
582 char body[MAX_FILE_SPEC_LEN + 1];
583 } vspec;
586 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
587 returning in each successive call the next filename matching the input
588 spec. The function expects that each in_spec passed
589 to it will be processed to completion; in particular, up to and
590 including the call following that in which the last matching name
591 is returned, the function ignores the value of in_spec, and will
592 only start processing a new spec with the following call.
593 If an error occurs, on return out_spec contains the value
594 of in_spec when the error occurred.
596 With each successive filename returned in out_spec, the
597 function's return value is one. When there are no more matching
598 names the function returns zero. If on the first call no file
599 matches in_spec, or there is any other error, -1 is returned.
602 #include <rmsdef.h>
603 #include <descrip.h>
604 #define OUTSIZE MAX_FILE_SPEC_LEN
605 short
606 fn_exp (out, in)
607 vspec *out;
608 char *in;
610 static long context = 0;
611 static struct dsc$descriptor_s o;
612 static struct dsc$descriptor_s i;
613 static logical pass1 = TRUE;
614 long status;
615 short retval;
617 if (pass1)
619 pass1 = FALSE;
620 o.dsc$a_pointer = (char *) out;
621 o.dsc$w_length = (short)OUTSIZE;
622 i.dsc$a_pointer = in;
623 i.dsc$w_length = (short)strlen(in);
624 i.dsc$b_dtype = DSC$K_DTYPE_T;
625 i.dsc$b_class = DSC$K_CLASS_S;
626 o.dsc$b_dtype = DSC$K_DTYPE_VT;
627 o.dsc$b_class = DSC$K_CLASS_VS;
629 if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL)
631 out->body[out->curlen] = EOS;
632 return 1;
634 else if (status == RMS$_NMF)
635 retval = 0;
636 else
638 strcpy(out->body, in);
639 retval = -1;
641 lib$find_file_end(&context);
642 pass1 = TRUE;
643 return retval;
647 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
648 name of each file specified by the provided arg expanding wildcards.
650 char *
651 gfnames (arg, p_error)
652 char *arg;
653 logical *p_error;
655 static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
657 switch (fn_exp (&filename, arg))
659 case 1:
660 *p_error = FALSE;
661 return filename.body;
662 case 0:
663 *p_error = FALSE;
664 return NULL;
665 default:
666 *p_error = TRUE;
667 return filename.body;
671 #ifndef OLD /* Newer versions of VMS do provide `system'. */
672 system (cmd)
673 char *cmd;
675 fprintf (stderr, "system() function not implemented under VMS\n");
677 #endif
679 #define VERSION_DELIM ';'
680 char *massage_name (s)
681 char *s;
683 char *start = s;
685 for ( ; *s; s++)
686 if (*s == VERSION_DELIM)
688 *s = EOS;
689 break;
691 else
692 *s = lowcase (*s);
693 return start;
695 #endif /* VMS */
698 void
699 main (argc, argv)
700 int argc;
701 char *argv[];
703 int i;
704 unsigned int nincluded_files = 0;
705 char **included_files = xnew (argc, char *);
706 char *this_file;
707 argument *argbuffer;
708 int current_arg = 0, file_count = 0;
709 struct linebuffer filename_lb;
710 #ifdef VMS
711 logical got_err;
712 #endif
714 #ifdef DOS_NT
715 _fmode = O_BINARY; /* all of files are treated as binary files */
716 #endif /* DOS_NT */
718 progname = argv[0];
720 /* Allocate enough no matter what happens. Overkill, but each one
721 is small. */
722 argbuffer = xnew (argc, argument);
724 #ifdef ETAGS_REGEXPS
725 /* Set syntax for regular expression routines. */
726 re_set_syntax (RE_SYNTAX_EMACS);
727 #endif /* ETAGS_REGEXPS */
730 * If etags, always find typedefs and structure tags. Why not?
731 * Also default is to find macro constants.
733 if (!CTAGS)
734 typedefs = typedefs_and_cplusplus = constantypedefs = TRUE;
736 while (1)
738 int opt = getopt_long (argc, argv,
739 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts, 0);
741 if (opt == EOF)
742 break;
744 switch (opt)
746 case 0:
747 /* If getopt returns 0, then it has already processed a
748 long-named option. We should do nothing. */
749 break;
751 case 1:
752 /* This means that a filename has been seen. Record it. */
753 argbuffer[current_arg].arg_type = at_filename;
754 argbuffer[current_arg].what = optarg;
755 ++current_arg;
756 ++file_count;
757 break;
759 /* Common options. */
760 case 'a':
761 append_to_tagfile = TRUE;
762 break;
763 case 'C':
764 cplusplus = TRUE;
765 break;
766 case 'd':
767 constantypedefs = TRUE;
768 break;
769 case 'D':
770 constantypedefs = FALSE;
771 break;
772 case 'f': /* for compatibility with old makefiles */
773 case 'o':
774 if (tagfile)
776 fprintf (stderr, "%s: -%c option may only be given once.\n",
777 progname, opt);
778 suggest_asking_for_help ();
780 tagfile = optarg;
781 break;
782 case 'I':
783 case 'S': /* for backward compatibility */
784 noindentypedefs = TRUE;
785 break;
786 case 'l':
787 argbuffer[current_arg].function = get_language_from_name (optarg);
788 argbuffer[current_arg].arg_type = at_language;
789 ++current_arg;
790 break;
791 #ifdef ETAGS_REGEXPS
792 case 'r':
793 argbuffer[current_arg].arg_type = at_regexp;
794 argbuffer[current_arg].what = optarg;
795 ++current_arg;
796 break;
797 case 'R':
798 argbuffer[current_arg].arg_type = at_regexp;
799 argbuffer[current_arg].what = NULL;
800 ++current_arg;
801 break;
802 #endif /* ETAGS_REGEXPS */
803 case 'V':
804 print_version ();
805 break;
806 case 'h':
807 case 'H':
808 print_help ();
809 break;
810 case 't':
811 typedefs = TRUE;
812 break;
813 case 'T':
814 typedefs = typedefs_and_cplusplus = TRUE;
815 break;
816 #if (!CTAGS)
817 /* Etags options */
818 case 'i':
819 included_files[nincluded_files++] = optarg;
820 break;
821 #else /* CTAGS */
822 /* Ctags options. */
823 case 'B':
824 searchar = '?';
825 break;
826 case 'u':
827 update = TRUE;
828 break;
829 case 'v':
830 vgrind_style = TRUE;
831 /*FALLTHRU*/
832 case 'x':
833 cxref_style = TRUE;
834 break;
835 case 'w':
836 no_warnings = TRUE;
837 break;
838 #endif /* CTAGS */
839 default:
840 suggest_asking_for_help ();
844 for (; optind < argc; ++optind)
846 argbuffer[current_arg].arg_type = at_filename;
847 argbuffer[current_arg].what = argv[optind];
848 ++current_arg;
849 ++file_count;
852 if (nincluded_files == 0 && file_count == 0)
854 fprintf (stderr, "%s: No input files specified.\n", progname);
855 suggest_asking_for_help ();
858 if (tagfile == NULL)
859 tagfile = CTAGS ? "tags" : "TAGS";
860 cwd = etags_getcwd (); /* the current working directory */
861 if (cwd[strlen(cwd)-1] != '/')
862 strcat (cwd, "/");
863 if (streq (tagfile, "-"))
864 tagfiledir = cwd;
865 else
866 tagfiledir = absolute_dirname (tagfile, cwd);
868 init (); /* set up boolean "functions" */
870 initbuffer (&lb);
871 initbuffer (&token_name);
872 initbuffer (&lbs[0].lb);
873 initbuffer (&lbs[1].lb);
874 initbuffer (&filename_lb);
876 if (!CTAGS)
878 if (streq (tagfile, "-"))
880 tagf = stdout;
881 #ifdef DOS_NT
882 /* Switch redirected `stdout' to binary mode (setting `_fmode'
883 doesn't take effect until after `stdout' is already open), */
884 if (!isatty (fileno (stdout)))
885 setmode (fileno (stdout), O_BINARY);
886 #endif /* DOS_NT */
888 else
889 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
890 if (tagf == NULL)
891 pfatal (tagfile);
895 * Loop through files finding functions.
897 for (i = 0; i < current_arg; ++i)
899 switch (argbuffer[i].arg_type)
901 case at_language:
902 lang_func = argbuffer[i].function;
903 break;
904 #ifdef ETAGS_REGEXPS
905 case at_regexp:
906 add_regex (argbuffer[i].what);
907 break;
908 #endif
909 case at_filename:
910 #ifdef VMS
911 while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL)
913 if (got_err)
915 error ("Can't find file %s\n", this_file);
916 argc--, argv++;
918 else
920 this_file = massage_name (this_file);
922 #else
923 this_file = argbuffer[i].what;
924 #endif
925 /* Input file named "-" means read file names from stdin
926 and use them. */
927 if (streq (this_file, "-"))
928 while (readline_internal (&filename_lb, stdin) > 0)
929 process_file (filename_lb.buffer);
930 else
931 process_file (this_file);
932 #ifdef VMS
934 #endif
935 break;
939 if (!CTAGS)
941 while (nincluded_files-- > 0)
942 fprintf (tagf, "\f\n%s,include\n", *included_files++);
944 fclose (tagf);
945 exit (GOOD);
948 /* If CTAGS, we are here. process_file did not write the tags yet,
949 because we want them ordered. Let's do it now. */
950 if (cxref_style)
952 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
953 if (tagf == NULL)
954 pfatal (tagfile);
955 put_entries (head);
956 exit (GOOD);
959 if (update)
961 char cmd[BUFSIZ];
962 for (i = 0; i < current_arg; ++i)
964 if (argbuffer[i].arg_type != at_filename)
965 continue;
966 sprintf (cmd,
967 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
968 tagfile, argbuffer[i].what, tagfile);
969 if (system (cmd) != GOOD)
970 fatal ("failed to execute shell command");
972 append_to_tagfile = TRUE;
975 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
976 if (tagf == NULL)
977 pfatal (tagfile);
978 put_entries (head);
979 fclose (tagf);
981 if (update)
983 char cmd[BUFSIZ];
984 sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
985 exit (system (cmd));
987 exit (GOOD);
992 * Return a Lang_function given the name.
994 Lang_function *
995 get_language_from_name (name)
996 char *name;
998 struct lang_entry *lang;
1000 if (name != NULL)
1001 for (lang = lang_names; lang->name != NULL; lang++)
1003 if (streq (name, lang->name))
1004 return lang->function;
1007 fprintf (stderr, "%s: language \"%s\" not recognized.\n",
1008 progname, optarg);
1009 suggest_asking_for_help ();
1011 /* This point should never be reached. The function should either
1012 return a function pointer or never return. Note that a NULL
1013 pointer cannot be considered as an error, as it means that the
1014 language has not been explicitely imposed by the user ("auto"). */
1015 return NULL; /* avoid warnings from compiler */
1020 * Return a Lang_function given the interpreter name.
1022 Lang_function *
1023 get_language_from_interpreter (interpreter)
1024 char *interpreter;
1026 struct lang_entry *lang;
1027 char **iname;
1029 if (interpreter == NULL)
1030 return NULL;
1031 for (lang = lang_names; lang->name != NULL; lang++)
1032 if (lang->interpreters != NULL)
1033 for (iname = lang->interpreters; *iname != NULL; iname++)
1034 if (streq (*iname, interpreter))
1035 return lang->function;
1037 return NULL;
1043 * Return a Lang_function given the file suffix.
1045 Lang_function *
1046 get_language_from_suffix (suffix)
1047 char *suffix;
1049 struct lang_entry *lang;
1050 char **ext;
1052 if (suffix == NULL)
1053 return NULL;
1054 for (lang = lang_names; lang->name != NULL; lang++)
1055 if (lang->suffixes != NULL)
1056 for (ext = lang->suffixes; *ext != NULL; ext++)
1057 if (streq (*ext, suffix))
1058 return lang->function;
1060 return NULL;
1065 * This routine is called on each file argument.
1067 void
1068 process_file (file)
1069 char *file;
1071 struct stat stat_buf;
1072 FILE *inf;
1073 #ifdef DOS_NT
1074 /* The rest of the program can't grok `\\'-style slashes. */
1075 char *p = file;
1077 while (*p)
1079 if (*p == '\\')
1080 *p = '/';
1081 ++p;
1083 #endif
1085 if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
1087 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
1088 return;
1090 if (streq (file, tagfile) && !streq (tagfile, "-"))
1092 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
1093 return;
1095 inf = fopen (file, "r");
1096 if (inf == NULL)
1098 perror (file);
1099 return;
1102 find_entries (file, inf);
1104 if (!CTAGS)
1106 char *filename;
1108 if (absolutefn (file))
1110 /* file is an absolute filename. Canonicalise it. */
1111 filename = absolute_filename (file, cwd);
1113 else
1115 /* file is a filename relative to cwd. Make it relative
1116 to the directory of the tags file. */
1117 filename = relative_filename (file, tagfiledir);
1119 fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
1120 free (filename);
1121 put_entries (head);
1122 free_tree (head);
1123 head = NULL;
1128 * This routine sets up the boolean pseudo-functions which work
1129 * by setting boolean flags dependent upon the corresponding character
1130 * Every char which is NOT in that string is not a white char. Therefore,
1131 * all of the array "_wht" is set to FALSE, and then the elements
1132 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1133 * of a char is TRUE if it is the string "white", else FALSE.
1135 void
1136 init ()
1138 register char *sp;
1139 register int i;
1141 for (i = 0; i < 0177; i++)
1142 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
1143 for (sp = white; *sp; sp++)
1144 _wht[*sp] = TRUE;
1145 for (sp = endtk; *sp; sp++)
1146 _etk[*sp] = TRUE;
1147 for (sp = intk; *sp; sp++)
1148 _itk[*sp] = TRUE;
1149 for (sp = begtk; *sp; sp++)
1150 _btk[*sp] = TRUE;
1151 _wht[0] = _wht['\n'];
1152 _etk[0] = _etk['\n'];
1153 _btk[0] = _btk['\n'];
1154 _itk[0] = _itk['\n'];
1158 * This routine opens the specified file and calls the function
1159 * which finds the function and type definitions.
1161 void
1162 find_entries (file, inf)
1163 char *file;
1164 FILE *inf;
1166 char *cp;
1167 Lang_function *function;
1168 NODE *old_last_node;
1169 extern NODE *last_node;
1172 /* Memory leakage here: the memory block pointed by curfile is never
1173 released. The amount of memory leaked here is the sum of the
1174 lengths of the input file names. */
1175 curfile = savestr (file);
1177 /* If user specified a language, use it. */
1178 function = lang_func;
1179 if (function != NULL)
1181 function (inf);
1182 fclose (inf);
1183 return;
1186 cp = etags_strrchr (file, '.');
1187 if (cp != NULL)
1189 cp += 1;
1190 function = get_language_from_suffix (cp);
1191 if (function != NULL)
1193 function (inf);
1194 fclose (inf);
1195 return;
1199 /* Look for sharp-bang as the first two characters. */
1200 if (readline_internal (&lb, inf) > 2
1201 && lb.buffer[0] == '#'
1202 && lb.buffer[1] == '!')
1204 char *lp;
1206 /* Set lp to point at the first char after the last slash in the
1207 line or, if no slashes, at the first nonblank. Then set cp to
1208 the first successive blank and terminate the string. */
1209 lp = etags_strrchr (lb.buffer+2, '/');
1210 if (lp != NULL)
1211 lp += 1;
1212 else
1213 for (lp = lb.buffer+2; *lp != '\0' && isspace (*lp); lp++)
1214 continue;
1215 for (cp = lp; *cp != '\0' && !isspace (*cp); cp++)
1216 continue;
1217 *cp = '\0';
1219 if (strlen (lp) > 0)
1221 function = get_language_from_interpreter (lp);
1222 if (function != NULL)
1224 function (inf);
1225 fclose (inf);
1226 return;
1230 rewind (inf);
1232 /* Try Fortran. */
1233 old_last_node = last_node;
1234 Fortran_functions (inf);
1236 /* No Fortran entries found. Try C. */
1237 if (old_last_node == last_node)
1239 rewind (inf);
1240 default_C_entries (inf);
1242 fclose (inf);
1243 return;
1246 /* Record a tag. */
1247 void
1248 pfnote (name, is_func, linestart, linelen, lno, cno)
1249 char *name; /* tag name, or NULL if unnamed */
1250 logical is_func; /* tag is a function */
1251 char *linestart; /* start of the line where tag is */
1252 int linelen; /* length of the line where tag is */
1253 int lno; /* line number */
1254 long cno; /* character number */
1256 register NODE *np;
1258 if (CTAGS && name == NULL)
1259 return;
1261 np = xnew (1, NODE);
1263 /* If ctags mode, change name "main" to M<thisfilename>. */
1264 if (CTAGS && !cxref_style && streq (name, "main"))
1266 register char *fp = etags_strrchr (curfile, '/');
1267 np->name = concat ("M", fp == 0 ? curfile : fp + 1, "");
1268 fp = etags_strrchr (np->name, '.');
1269 if (fp && fp[1] != '\0' && fp[2] == '\0')
1270 fp[0] = 0;
1272 else
1273 np->name = name;
1274 np->been_warned = FALSE;
1275 np->file = curfile;
1276 np->is_func = is_func;
1277 np->lno = lno;
1278 /* Our char numbers are 0-base, because of C language tradition?
1279 ctags compatibility? old versions compatibility? I don't know.
1280 Anyway, since emacs's are 1-base we expect etags.el to take care
1281 of the difference. If we wanted to have 1-based numbers, we would
1282 uncomment the +1 below. */
1283 np->cno = cno /* + 1 */ ;
1284 np->left = np->right = NULL;
1285 if (CTAGS && !cxref_style)
1287 if (strlen (linestart) < 50)
1288 np->pat = concat (linestart, "$", "");
1289 else
1290 np->pat = savenstr (linestart, 50);
1292 else
1293 np->pat = savenstr (linestart, linelen);
1295 add_node (np, &head);
1299 * free_tree ()
1300 * recurse on left children, iterate on right children.
1302 void
1303 free_tree (node)
1304 register NODE *node;
1306 while (node)
1308 register NODE *node_right = node->right;
1309 free_tree (node->left);
1310 if (node->name != NULL)
1311 free (node->name);
1312 free (node->pat);
1313 free ((char *) node);
1314 node = node_right;
1319 * add_node ()
1320 * Adds a node to the tree of nodes. In etags mode, we don't keep
1321 * it sorted; we just keep a linear list. In ctags mode, maintain
1322 * an ordered tree, with no attempt at balancing.
1324 * add_node is the only function allowed to add nodes, so it can
1325 * maintain state.
1327 NODE *last_node = NULL;
1328 void
1329 add_node (node, cur_node_p)
1330 NODE *node, **cur_node_p;
1332 register int dif;
1333 register NODE *cur_node = *cur_node_p;
1335 if (cur_node == NULL)
1337 *cur_node_p = node;
1338 last_node = node;
1339 return;
1342 if (!CTAGS)
1344 /* Etags Mode */
1345 if (last_node == NULL)
1346 fatal ("internal error in add_node", 0);
1347 last_node->right = node;
1348 last_node = node;
1350 else
1352 /* Ctags Mode */
1353 dif = strcmp (node->name, cur_node->name);
1356 * If this tag name matches an existing one, then
1357 * do not add the node, but maybe print a warning.
1359 if (!dif)
1361 if (streq (node->file, cur_node->file))
1363 if (!no_warnings)
1365 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1366 node->file, lineno, node->name);
1367 fprintf (stderr, "Second entry ignored\n");
1370 else if (!cur_node->been_warned && !no_warnings)
1372 fprintf
1373 (stderr,
1374 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1375 node->file, cur_node->file, node->name);
1376 cur_node->been_warned = TRUE;
1378 return;
1381 /* Actually add the node */
1382 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1386 void
1387 put_entries (node)
1388 register NODE *node;
1390 register char *sp;
1392 if (node == NULL)
1393 return;
1395 /* Output subentries that precede this one */
1396 put_entries (node->left);
1398 /* Output this entry */
1400 if (!CTAGS)
1402 if (node->name != NULL)
1403 fprintf (tagf, "%s\177%s\001%d,%d\n",
1404 node->pat, node->name, node->lno, node->cno);
1405 else
1406 fprintf (tagf, "%s\177%d,%d\n",
1407 node->pat, node->lno, node->cno);
1409 else
1411 if (node->name == NULL)
1412 error ("internal error: NULL name in ctags mode.", 0);
1414 if (cxref_style)
1416 if (vgrind_style)
1417 fprintf (stdout, "%s %s %d\n",
1418 node->name, node->file, (node->lno + 63) / 64);
1419 else
1420 fprintf (stdout, "%-16s %3d %-16s %s\n",
1421 node->name, node->lno, node->file, node->pat);
1423 else
1425 fprintf (tagf, "%s\t%s\t", node->name, node->file);
1427 if (node->is_func)
1428 { /* a function */
1429 putc (searchar, tagf);
1430 putc ('^', tagf);
1432 for (sp = node->pat; *sp; sp++)
1434 if (*sp == '\\' || *sp == searchar)
1435 putc ('\\', tagf);
1436 putc (*sp, tagf);
1438 putc (searchar, tagf);
1440 else
1441 { /* a typedef; text pattern inadequate */
1442 fprintf (tagf, "%d", node->lno);
1444 putc ('\n', tagf);
1448 /* Output subentries that follow this one */
1449 put_entries (node->right);
1452 /* Length of a number's decimal representation. */
1454 number_len (num)
1455 long num;
1457 int len = 0;
1458 if (!num)
1459 return 1;
1460 for (; num; num /= 10)
1461 ++len;
1462 return len;
1466 * Return total number of characters that put_entries will output for
1467 * the nodes in the subtree of the specified node. Works only if
1468 * we are not ctags, but called only in that case. This count
1469 * is irrelevant with the new tags.el, but is still supplied for
1470 * backward compatibility.
1473 total_size_of_entries (node)
1474 register NODE *node;
1476 register int total;
1478 if (node == NULL)
1479 return 0;
1481 total = 0;
1482 for (; node; node = node->right)
1484 /* Count left subentries. */
1485 total += total_size_of_entries (node->left);
1487 /* Count this entry */
1488 total += strlen (node->pat) + 1;
1489 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1490 if (node->name != NULL)
1491 total += 1 + strlen (node->name); /* \001name */
1494 return total;
1498 * The C symbol tables.
1500 enum sym_type
1502 st_none, st_C_objprot, st_C_objimpl, st_C_objend, st_C_gnumacro,
1503 st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
1506 /* Feed stuff between (but not including) %[ and %] lines to:
1507 gperf -c -k1,3 -o -p -r -t
1509 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1511 @interface, 0, st_C_objprot
1512 @protocol, 0, st_C_objprot
1513 @implementation,0, st_C_objimpl
1514 @end, 0, st_C_objend
1515 class, C_PLPL, st_C_struct
1516 domain, C_STAR, st_C_struct
1517 union, 0, st_C_struct
1518 struct, 0, st_C_struct
1519 enum, 0, st_C_enum
1520 typedef, 0, st_C_typedef
1521 define, 0, st_C_define
1522 long, 0, st_C_typespec
1523 short, 0, st_C_typespec
1524 int, 0, st_C_typespec
1525 char, 0, st_C_typespec
1526 float, 0, st_C_typespec
1527 double, 0, st_C_typespec
1528 signed, 0, st_C_typespec
1529 unsigned, 0, st_C_typespec
1530 auto, 0, st_C_typespec
1531 void, 0, st_C_typespec
1532 extern, 0, st_C_typespec
1533 static, 0, st_C_typespec
1534 const, 0, st_C_typespec
1535 volatile, 0, st_C_typespec
1536 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1537 DEFUN, 0, st_C_gnumacro
1538 SYSCALL, 0, st_C_gnumacro
1539 ENTRY, 0, st_C_gnumacro
1540 PSEUDO, 0, st_C_gnumacro
1541 # These are defined inside C functions, so currently they are not met.
1542 # EXFUN used in glibc, DEFVAR_* in emacs.
1543 #EXFUN, 0, st_C_gnumacro
1544 #DEFVAR_, 0, st_C_gnumacro
1546 and replace lines between %< and %> with its output. */
1547 /*%<*/
1548 /* C code produced by gperf version 1.8.1 (K&R C version) */
1549 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1552 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
1554 #define MIN_WORD_LENGTH 3
1555 #define MAX_WORD_LENGTH 15
1556 #define MIN_HASH_VALUE 7
1557 #define MAX_HASH_VALUE 63
1559 29 keywords
1560 57 is the maximum key range
1563 static int
1564 hash (str, len)
1565 register char *str;
1566 register int len;
1568 static unsigned char hash_table[] =
1570 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1571 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1572 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1573 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1574 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1575 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1576 63, 63, 63, 63, 17, 63, 63, 63, 4, 14,
1577 4, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1578 8, 63, 63, 0, 23, 63, 63, 63, 63, 63,
1579 63, 63, 63, 63, 63, 63, 63, 28, 63, 28,
1580 10, 31, 27, 18, 63, 6, 63, 63, 26, 1,
1581 11, 2, 29, 63, 29, 16, 26, 13, 15, 63,
1582 63, 63, 63, 63, 63, 63, 63, 63,
1584 return len + hash_table[str[2]] + hash_table[str[0]];
1587 struct C_stab_entry *
1588 in_word_set (str, len)
1589 register char *str;
1590 register int len;
1593 static struct C_stab_entry wordlist[] =
1595 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1596 {"SYSCALL", 0, st_C_gnumacro},
1597 {"",}, {"",}, {"",}, {"",}, {"",},
1598 {"DEFUN", 0, st_C_gnumacro},
1599 {"",}, {"",}, {"",},
1600 {"domain", C_STAR, st_C_struct},
1601 {"",}, {"",}, {"",}, {"",}, {"",},
1602 {"short", 0, st_C_typespec},
1603 {"union", 0, st_C_struct},
1604 {"void", 0, st_C_typespec},
1605 {"",}, {"",},
1606 {"PSEUDO", 0, st_C_gnumacro},
1607 {"double", 0, st_C_typespec},
1608 {"",}, {"",},
1609 {"@end", 0, st_C_objend},
1610 {"@implementation", 0, st_C_objimpl},
1611 {"float", 0, st_C_typespec},
1612 {"int", 0, st_C_typespec},
1613 {"",},
1614 {"unsigned", 0, st_C_typespec},
1615 {"@interface", 0, st_C_objprot},
1616 {"",},
1617 {"signed", 0, st_C_typespec},
1618 {"long", 0, st_C_typespec},
1619 {"ENTRY", 0, st_C_gnumacro},
1620 {"define", 0, st_C_define},
1621 {"const", 0, st_C_typespec},
1622 {"",}, {"",}, {"",},
1623 {"enum", 0, st_C_enum},
1624 {"volatile", 0, st_C_typespec},
1625 {"static", 0, st_C_typespec},
1626 {"struct", 0, st_C_struct},
1627 {"",}, {"",}, {"",},
1628 {"@protocol", 0, st_C_objprot},
1629 {"",}, {"",},
1630 {"auto", 0, st_C_typespec},
1631 {"",},
1632 {"char", 0, st_C_typespec},
1633 {"class", C_PLPL, st_C_struct},
1634 {"typedef", 0, st_C_typedef},
1635 {"extern", 0, st_C_typespec},
1638 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
1640 register int key = hash (str, len);
1642 if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
1644 register char *s = wordlist[key].name;
1646 if (*s == *str && !strncmp (str + 1, s + 1, len - 1))
1647 return &wordlist[key];
1650 return 0;
1652 /*%>*/
1654 enum sym_type
1655 C_symtype(str, len, c_ext)
1656 char *str;
1657 int len;
1658 int c_ext;
1660 register struct C_stab_entry *se = in_word_set(str, len);
1662 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
1663 return st_none;
1664 return se->type;
1668 * C functions are recognized using a simple finite automaton.
1669 * funcdef is its state variable.
1671 enum
1673 fnone, /* nothing seen */
1674 ftagseen, /* function-like tag seen */
1675 fstartlist, /* just after open parenthesis */
1676 finlist, /* in parameter list */
1677 flistseen, /* after parameter list */
1678 fignore /* before open brace */
1679 } funcdef;
1683 * typedefs are recognized using a simple finite automaton.
1684 * typdef is its state variable.
1686 enum
1688 tnone, /* nothing seen */
1689 ttypedseen, /* typedef keyword seen */
1690 tinbody, /* inside typedef body */
1691 tend, /* just before typedef tag */
1692 tignore /* junk after typedef tag */
1693 } typdef;
1697 * struct-like structures (enum, struct and union) are recognized
1698 * using another simple finite automaton. `structdef' is its state
1699 * variable.
1701 enum
1703 snone, /* nothing seen yet */
1704 skeyseen, /* struct-like keyword seen */
1705 stagseen, /* struct-like tag seen */
1706 scolonseen, /* colon seen after struct-like tag */
1707 sinbody /* in struct body: recognize member func defs*/
1708 } structdef;
1711 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1712 * struct tag, and structtype is the type of the preceding struct-like
1713 * keyword.
1715 char *structtag = "<uninited>";
1716 enum sym_type structtype;
1719 * When objdef is different from onone, objtag is the name of the class.
1721 char *objtag = "<uninited>";
1724 * Yet another little state machine to deal with preprocessor lines.
1726 enum
1728 dnone, /* nothing seen */
1729 dsharpseen, /* '#' seen as first char on line */
1730 ddefineseen, /* '#' and 'define' seen */
1731 dignorerest /* ignore rest of line */
1732 } definedef;
1735 * State machine for Objective C protocols and implementations.
1737 enum
1739 onone, /* nothing seen */
1740 oprotocol, /* @interface or @protocol seen */
1741 oimplementation, /* @implementations seen */
1742 otagseen, /* class name seen */
1743 oparenseen, /* parenthesis before category seen */
1744 ocatseen, /* category name seen */
1745 oinbody, /* in @implementation body */
1746 omethodsign, /* in @implementation body, after +/- */
1747 omethodtag, /* after method name */
1748 omethodcolon, /* after method colon */
1749 omethodparm, /* after method parameter */
1750 oignore /* wait for @end */
1751 } objdef;
1754 * Set this to TRUE, and the next token considered is called a function.
1755 * Used only for GNU emacs's function-defining macros.
1757 logical next_token_is_func;
1760 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1762 logical yacc_rules;
1765 * methodlen is the length of the method name stored in token_name.
1767 int methodlen;
1770 * consider_token ()
1771 * checks to see if the current token is at the start of a
1772 * function, or corresponds to a typedef, or is a struct/union/enum
1773 * tag.
1775 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1776 * C_EXT is which language we are looking at.
1778 * In the future we will need some way to adjust where the end of
1779 * the token is; for instance, implementing the C++ keyword
1780 * `operator' properly will adjust the end of the token to be after
1781 * whatever follows `operator'.
1783 * Globals
1784 * funcdef IN OUT
1785 * structdef IN OUT
1786 * definedef IN OUT
1787 * typdef IN OUT
1788 * objdef IN OUT
1789 * next_token_is_func IN OUT
1792 logical
1793 consider_token (str, len, c, c_ext, cblev, parlev, is_func)
1794 register char *str; /* IN: token pointer */
1795 register int len; /* IN: token length */
1796 register char c; /* IN: first char after the token */
1797 int c_ext; /* IN: C extensions mask */
1798 int cblev; /* IN: curly brace level */
1799 int parlev; /* IN: parenthesis level */
1800 logical *is_func; /* OUT: function found */
1802 enum sym_type toktype = C_symtype (str, len, c_ext);
1805 * Advance the definedef state machine.
1807 switch (definedef)
1809 case dnone:
1810 /* We're not on a preprocessor line. */
1811 break;
1812 case dsharpseen:
1813 if (toktype == st_C_define)
1815 definedef = ddefineseen;
1817 else
1819 definedef = dignorerest;
1821 return FALSE;
1822 case ddefineseen:
1824 * Make a tag for any macro, unless it is a constant
1825 * and constantypedefs is FALSE.
1827 definedef = dignorerest;
1828 *is_func = (c == '(');
1829 if (!*is_func && !constantypedefs)
1830 return FALSE;
1831 else
1832 return TRUE;
1833 case dignorerest:
1834 return FALSE;
1835 default:
1836 error ("internal error: definedef value.", 0);
1840 * Now typedefs
1842 switch (typdef)
1844 case tnone:
1845 if (toktype == st_C_typedef)
1847 if (typedefs)
1848 typdef = ttypedseen;
1849 funcdef = fnone;
1850 return FALSE;
1852 break;
1853 case ttypedseen:
1854 switch (toktype)
1856 case st_none:
1857 case st_C_typespec:
1858 typdef = tend;
1859 break;
1860 case st_C_struct:
1861 case st_C_enum:
1862 break;
1864 /* Do not return here, so the structdef stuff has a chance. */
1865 break;
1866 case tend:
1867 switch (toktype)
1869 case st_C_typespec:
1870 case st_C_struct:
1871 case st_C_enum:
1872 return FALSE;
1874 return TRUE;
1878 * This structdef business is currently only invoked when cblev==0.
1879 * It should be recursively invoked whatever the curly brace level,
1880 * and a stack of states kept, to allow for definitions of structs
1881 * within structs.
1883 * This structdef business is NOT invoked when we are ctags and the
1884 * file is plain C. This is because a struct tag may have the same
1885 * name as another tag, and this loses with ctags.
1887 * This if statement deals with the typdef state machine as
1888 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1889 * return FALSE. All the other code here is for the structdef
1890 * state machine.
1892 switch (toktype)
1894 case st_C_struct:
1895 case st_C_enum:
1896 if (typdef == ttypedseen
1897 || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
1899 structdef = skeyseen;
1900 structtype = toktype;
1902 return FALSE;
1904 if (structdef == skeyseen)
1906 /* Save the tag for struct/union/class, for functions that may be
1907 defined inside. */
1908 if (structtype == st_C_struct)
1909 structtag = savenstr (str, len);
1910 else
1911 structtag = "<enum>";
1912 structdef = stagseen;
1913 return TRUE;
1916 /* Avoid entering funcdef stuff if typdef is going on. */
1917 if (typdef != tnone)
1919 definedef = dnone;
1920 return FALSE;
1923 /* Detect GNU macros. */
1924 if (definedef == dnone && toktype == st_C_gnumacro)
1926 next_token_is_func = TRUE;
1927 return FALSE;
1929 if (next_token_is_func)
1931 next_token_is_func = FALSE;
1932 funcdef = fignore;
1933 *is_func = TRUE;
1934 return TRUE;
1938 * Detecting Objective C constructs.
1940 switch (objdef)
1942 case onone:
1943 switch (toktype)
1945 case st_C_objprot:
1946 objdef = oprotocol;
1947 return FALSE;
1948 case st_C_objimpl:
1949 objdef = oimplementation;
1950 return FALSE;
1952 break;
1953 case oimplementation:
1954 /* Save the class tag for functions that may be defined inside. */
1955 objtag = savenstr (str, len);
1956 objdef = oinbody;
1957 return FALSE;
1958 case oprotocol:
1959 /* Save the class tag for categories. */
1960 objtag = savenstr (str, len);
1961 objdef = otagseen;
1962 *is_func = TRUE;
1963 return TRUE;
1964 case oparenseen:
1965 objdef = ocatseen;
1966 *is_func = TRUE;
1967 return TRUE;
1968 case oinbody:
1969 break;
1970 case omethodsign:
1971 if (parlev == 0)
1973 objdef = omethodtag;
1974 methodlen = len;
1975 GROW_LINEBUFFER (token_name, methodlen+1);
1976 strncpy (token_name.buffer, str, len);
1977 token_name.buffer[methodlen] = '\0';
1978 return TRUE;
1980 return FALSE;
1981 case omethodcolon:
1982 if (parlev == 0)
1983 objdef = omethodparm;
1984 return FALSE;
1985 case omethodparm:
1986 if (parlev == 0)
1988 objdef = omethodtag;
1989 methodlen += len;
1990 GROW_LINEBUFFER (token_name, methodlen+1);
1991 strncat (token_name.buffer, str, len);
1992 return TRUE;
1994 return FALSE;
1995 case oignore:
1996 if (toktype == st_C_objend)
1998 /* Memory leakage here: the string pointed by objtag is
1999 never released, because many tests would be needed to
2000 avoid breaking on incorrect input code. The amount of
2001 memory leaked here is the sum of the lengths of the
2002 class tags.
2003 free (objtag); */
2004 objdef = onone;
2006 return FALSE;
2009 /* A function? */
2010 switch (toktype)
2012 case st_C_typespec:
2013 if (funcdef != finlist && funcdef != fignore)
2014 funcdef = fnone; /* should be useless */
2015 return FALSE;
2016 default:
2017 if (funcdef == fnone)
2019 funcdef = ftagseen;
2020 *is_func = TRUE;
2021 return TRUE;
2025 return FALSE;
2029 * C_entries ()
2030 * This routine finds functions, typedefs, #define's and
2031 * struct/union/enum definitions in C syntax and adds them
2032 * to the list.
2034 typedef struct
2036 logical valid;
2037 char *str;
2038 logical named;
2039 int linelen;
2040 int lineno;
2041 long linepos;
2042 char *buffer;
2043 } TOKEN;
2045 #define current_lb_is_new (newndx == curndx)
2046 #define switch_line_buffers() (curndx = 1 - curndx)
2048 #define curlb (lbs[curndx].lb)
2049 #define othlb (lbs[1-curndx].lb)
2050 #define newlb (lbs[newndx].lb)
2051 #define curlinepos (lbs[curndx].linepos)
2052 #define othlinepos (lbs[1-curndx].linepos)
2053 #define newlinepos (lbs[newndx].linepos)
2055 #define CNL_SAVE_DEFINEDEF \
2056 do { \
2057 curlinepos = charno; \
2058 lineno++; \
2059 charno += readline (&curlb, inf); \
2060 lp = curlb.buffer; \
2061 quotednl = FALSE; \
2062 newndx = curndx; \
2063 } while (0)
2065 #define CNL \
2066 do { \
2067 CNL_SAVE_DEFINEDEF; \
2068 if (savetok.valid) \
2070 tok = savetok; \
2071 savetok.valid = FALSE; \
2073 definedef = dnone; \
2074 } while (0)
2076 /* Ideally this macro should never be called wihen tok.valid is FALSE,
2077 but this would mean that the state machines always guess right. */
2078 #define make_tag(isfun) do \
2079 if (tok.valid) { \
2080 char *name = NULL; \
2081 if (CTAGS || tok.named) \
2082 name = savestr (token_name.buffer); \
2083 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
2084 tok.valid = FALSE; \
2085 } while (0)
2087 void
2088 C_entries (c_ext, inf)
2089 int c_ext; /* extension of C */
2090 FILE *inf; /* input file */
2092 register char c; /* latest char read; '\0' for end of line */
2093 register char *lp; /* pointer one beyond the character `c' */
2094 int curndx, newndx; /* indices for current and new lb */
2095 TOKEN tok; /* latest token read */
2096 register int tokoff; /* offset in line of start of current token */
2097 register int toklen; /* length of current token */
2098 int cblev; /* current curly brace level */
2099 int parlev; /* current parenthesis level */
2100 logical incomm, inquote, inchar, quotednl, midtoken;
2101 logical cplpl;
2102 TOKEN savetok; /* token saved during preprocessor handling */
2105 curndx = newndx = 0;
2106 lineno = 0;
2107 charno = 0;
2108 lp = curlb.buffer;
2109 *lp = 0;
2111 funcdef = fnone; typdef = tnone; structdef = snone;
2112 definedef = dnone; objdef = onone;
2113 next_token_is_func = yacc_rules = FALSE;
2114 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2115 tok.valid = savetok.valid = FALSE;
2116 cblev = 0;
2117 parlev = 0;
2118 cplpl = c_ext & C_PLPL;
2120 while (!feof (inf))
2122 c = *lp++;
2123 if (c == '\\')
2125 /* If we're at the end of the line, the next character is a
2126 '\0'; don't skip it, because it's the thing that tells us
2127 to read the next line. */
2128 if (*lp == '\0')
2130 quotednl = TRUE;
2131 continue;
2133 lp++;
2134 c = ' ';
2136 else if (incomm)
2138 switch (c)
2140 case '*':
2141 if (*lp == '/')
2143 c = *lp++;
2144 incomm = FALSE;
2146 break;
2147 case '\0':
2148 /* Newlines inside comments do not end macro definitions in
2149 traditional cpp. */
2150 CNL_SAVE_DEFINEDEF;
2151 break;
2153 continue;
2155 else if (inquote)
2157 switch (c)
2159 case '"':
2160 inquote = FALSE;
2161 break;
2162 case '\0':
2163 /* Newlines inside strings do not end macro definitions
2164 in traditional cpp, even though compilers don't
2165 usually accept them. */
2166 CNL_SAVE_DEFINEDEF;
2167 break;
2169 continue;
2171 else if (inchar)
2173 switch (c)
2175 case '\0':
2176 /* Hmmm, something went wrong. */
2177 CNL;
2178 /* FALLTHRU */
2179 case '\'':
2180 inchar = FALSE;
2181 break;
2183 continue;
2185 else
2186 switch (c)
2188 case '"':
2189 inquote = TRUE;
2190 if (funcdef != finlist && funcdef != fignore)
2191 funcdef = fnone;
2192 continue;
2193 case '\'':
2194 inchar = TRUE;
2195 if (funcdef != finlist && funcdef != fignore)
2196 funcdef = fnone;
2197 continue;
2198 case '/':
2199 if (*lp == '*')
2201 lp++;
2202 incomm = TRUE;
2203 continue;
2205 else if (/* cplpl && */ *lp == '/')
2207 c = '\0';
2208 break;
2210 else
2211 break;
2212 case '%':
2213 if ((c_ext & YACC) && *lp == '%')
2215 /* entering or exiting rules section in yacc file */
2216 lp++;
2217 definedef = dnone; funcdef = fnone;
2218 typdef = tnone; structdef = snone;
2219 next_token_is_func = FALSE;
2220 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2221 cblev = 0;
2222 yacc_rules = !yacc_rules;
2223 continue;
2225 else
2226 break;
2227 case '#':
2228 if (definedef == dnone)
2230 char *cp;
2231 logical cpptoken = TRUE;
2233 /* Look back on this line. If all blanks, or nonblanks
2234 followed by an end of comment, this is a preprocessor
2235 token. */
2236 for (cp = newlb.buffer; cp < lp-1; cp++)
2237 if (!iswhite (*cp))
2239 if (*cp == '*' && *(cp+1) == '/')
2241 cp++;
2242 cpptoken = TRUE;
2244 else
2245 cpptoken = FALSE;
2247 if (cpptoken)
2248 definedef = dsharpseen;
2249 } /* if (definedef == dnone) */
2251 continue;
2252 } /* switch (c) */
2255 /* Consider token only if some complicated conditions are satisfied. */
2256 if ((definedef != dnone
2257 || (cblev == 0 && structdef != scolonseen)
2258 || (cblev == 1 && cplpl && structdef == sinbody))
2259 && typdef != tignore
2260 && definedef != dignorerest
2261 && funcdef != finlist)
2263 if (midtoken)
2265 if (endtoken (c))
2267 if (c == ':' && cplpl && *lp == ':' && begtoken(*(lp + 1)))
2270 * This handles :: in the middle, but not at the
2271 * beginning of an identifier.
2273 lp += 2;
2274 toklen += 3;
2276 else
2278 logical is_func = FALSE;
2280 if (yacc_rules
2281 || consider_token (newlb.buffer + tokoff, toklen, c,
2282 c_ext, cblev, parlev, &is_func))
2284 if (structdef == sinbody
2285 && definedef == dnone
2286 && is_func)
2287 /* function defined in C++ class body */
2289 GROW_LINEBUFFER (token_name,
2290 strlen(structtag)+2+toklen+1);
2291 strcpy (token_name.buffer, structtag);
2292 strcat (token_name.buffer, "::");
2293 strncat (token_name.buffer,
2294 newlb.buffer+tokoff, toklen);
2295 tok.named = TRUE;
2297 else if (objdef == ocatseen)
2298 /* Objective C category */
2300 GROW_LINEBUFFER (token_name,
2301 strlen(objtag)+2+toklen+1);
2302 strcpy (token_name.buffer, objtag);
2303 strcat (token_name.buffer, "(");
2304 strncat (token_name.buffer,
2305 newlb.buffer+tokoff, toklen);
2306 strcat (token_name.buffer, ")");
2307 tok.named = TRUE;
2309 else if (objdef == omethodtag
2310 || objdef == omethodparm)
2311 /* Objective C method */
2313 tok.named = TRUE;
2315 else
2317 GROW_LINEBUFFER (token_name, toklen+1);
2318 strncpy (token_name.buffer,
2319 newlb.buffer+tokoff, toklen);
2320 token_name.buffer[toklen] = '\0';
2321 if (structdef == stagseen
2322 || typdef == tend
2323 || (is_func
2324 && definedef == dignorerest)) /* macro */
2325 tok.named = TRUE;
2326 else
2327 tok.named = FALSE;
2329 tok.lineno = lineno;
2330 tok.linelen = tokoff + toklen + 1;
2331 tok.buffer = newlb.buffer;
2332 tok.linepos = newlinepos;
2333 tok.valid = TRUE;
2335 if (definedef == dnone
2336 && (funcdef == ftagseen
2337 || structdef == stagseen
2338 || typdef == tend
2339 || objdef != onone))
2341 if (current_lb_is_new)
2342 switch_line_buffers ();
2344 else
2345 make_tag (is_func);
2347 midtoken = FALSE;
2349 } /* if (endtoken (c)) */
2350 else if (intoken (c))
2352 toklen++;
2353 continue;
2355 } /* if (midtoken) */
2356 else if (begtoken (c))
2358 switch (definedef)
2360 case dnone:
2361 switch (funcdef)
2363 case fstartlist:
2364 funcdef = finlist;
2365 continue;
2366 case flistseen:
2367 make_tag (TRUE);
2368 funcdef = fignore;
2369 break;
2370 case ftagseen:
2371 funcdef = fnone;
2372 break;
2374 if (structdef == stagseen)
2375 structdef = snone;
2376 break;
2377 case dsharpseen:
2378 savetok = tok;
2380 if (!yacc_rules || lp == newlb.buffer + 1)
2382 tokoff = lp - 1 - newlb.buffer;
2383 toklen = 1;
2384 midtoken = TRUE;
2386 continue;
2387 } /* if (begtoken) */
2388 } /* if must look at token */
2391 /* Detect end of line, colon, comma, semicolon and various braces
2392 after having handled a token.*/
2393 switch (c)
2395 case ':':
2396 if (definedef != dnone)
2397 break;
2398 switch (objdef)
2400 case otagseen:
2401 objdef = oignore;
2402 make_tag (TRUE);
2403 break;
2404 case omethodtag:
2405 case omethodparm:
2406 objdef = omethodcolon;
2407 methodlen += 1;
2408 GROW_LINEBUFFER (token_name, methodlen+1);
2409 strcat (token_name.buffer, ":");
2410 break;
2412 if (structdef == stagseen)
2413 structdef = scolonseen;
2414 else
2415 switch (funcdef)
2417 case ftagseen:
2418 if (yacc_rules)
2420 make_tag (FALSE);
2421 funcdef = fignore;
2423 break;
2424 case fstartlist:
2425 funcdef = fnone;
2426 break;
2428 break;
2429 case ';':
2430 if (definedef != dnone)
2431 break;
2432 if (cblev == 0)
2433 switch (typdef)
2435 case tend:
2436 make_tag (FALSE);
2437 /* FALLTHRU */
2438 default:
2439 typdef = tnone;
2441 if (funcdef != fignore)
2443 funcdef = fnone;
2444 /* The following instruction invalidates the token.
2445 Probably the token should be invalidated in all
2446 other cases where some state machine is reset. */
2447 tok.valid = FALSE;
2449 if (structdef == stagseen)
2450 structdef = snone;
2451 break;
2452 case ',':
2453 if (definedef != dnone)
2454 break;
2455 switch (objdef)
2457 case omethodtag:
2458 case omethodparm:
2459 make_tag (TRUE);
2460 objdef = oinbody;
2461 break;
2463 if (funcdef != finlist && funcdef != fignore)
2464 funcdef = fnone;
2465 if (structdef == stagseen)
2466 structdef = snone;
2467 break;
2468 case '[':
2469 if (definedef != dnone)
2470 break;
2471 if (cblev == 0 && typdef == tend)
2473 typdef = tignore;
2474 make_tag (FALSE);
2475 break;
2477 if (funcdef != finlist && funcdef != fignore)
2478 funcdef = fnone;
2479 if (structdef == stagseen)
2480 structdef = snone;
2481 break;
2482 case '(':
2483 if (definedef != dnone)
2484 break;
2485 if (objdef == otagseen && parlev == 0)
2486 objdef = oparenseen;
2487 switch (funcdef)
2489 case fnone:
2490 switch (typdef)
2492 case ttypedseen:
2493 case tend:
2494 /* Make sure that the next char is not a '*'.
2495 This handles constructs like:
2496 typedef void OperatorFun (int fun); */
2497 if (*lp != '*')
2499 typdef = tignore;
2500 make_tag (FALSE);
2502 break;
2503 } /* switch (typdef) */
2504 break;
2505 case ftagseen:
2506 funcdef = fstartlist;
2507 break;
2508 case flistseen:
2509 funcdef = finlist;
2510 break;
2512 parlev++;
2513 break;
2514 case ')':
2515 if (definedef != dnone)
2516 break;
2517 if (objdef == ocatseen && parlev == 1)
2519 make_tag (TRUE);
2520 objdef = oignore;
2522 if (--parlev == 0)
2524 switch (funcdef)
2526 case fstartlist:
2527 case finlist:
2528 funcdef = flistseen;
2529 break;
2531 if (cblev == 0 && typdef == tend)
2533 typdef = tignore;
2534 make_tag (FALSE);
2537 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
2538 parlev = 0;
2539 break;
2540 case '{':
2541 if (definedef != dnone)
2542 break;
2543 if (typdef == ttypedseen)
2544 typdef = tinbody;
2545 switch (structdef)
2547 case skeyseen: /* unnamed struct */
2548 structtag = "_anonymous_";
2549 structdef = sinbody;
2550 break;
2551 case stagseen:
2552 case scolonseen: /* named struct */
2553 structdef = sinbody;
2554 make_tag (FALSE);
2555 break;
2557 switch (funcdef)
2559 case flistseen:
2560 make_tag (TRUE);
2561 /* FALLTHRU */
2562 case fignore:
2563 funcdef = fnone;
2564 break;
2565 case fnone:
2566 switch (objdef)
2568 case otagseen:
2569 make_tag (TRUE);
2570 objdef = oignore;
2571 break;
2572 case omethodtag:
2573 case omethodparm:
2574 make_tag (TRUE);
2575 objdef = oinbody;
2576 break;
2577 default:
2578 /* Neutralize `extern "C" {' grot and look inside structs. */
2579 if (cblev == 0 && structdef == snone && typdef == tnone)
2580 cblev = -1;
2583 cblev++;
2584 break;
2585 case '*':
2586 if (definedef != dnone)
2587 break;
2588 if (funcdef == fstartlist)
2589 funcdef = fnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
2590 break;
2591 case '}':
2592 if (definedef != dnone)
2593 break;
2594 if (!noindentypedefs && lp == newlb.buffer + 1)
2596 cblev = 0; /* reset curly brace level if first column */
2597 parlev = 0; /* also reset paren level, just in case... */
2599 else if (cblev > 0)
2600 cblev--;
2601 if (cblev == 0)
2603 if (typdef == tinbody)
2604 typdef = tend;
2605 /* Memory leakage here: the string pointed by structtag is
2606 never released, because I fear to miss something and
2607 break things while freeing the area. The amount of
2608 memory leaked here is the sum of the lengths of the
2609 struct tags.
2610 if (structdef == sinbody)
2611 free (structtag); */
2613 structdef = snone;
2614 structtag = "<error>";
2616 break;
2617 case '+':
2618 case '-':
2619 if (objdef == oinbody && cblev == 0)
2621 objdef = omethodsign;
2622 break;
2624 /* FALLTHRU */
2625 case '=': case '#': case '~': case '&': case '%': case '/':
2626 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2627 if (definedef != dnone)
2628 break;
2629 /* These surely cannot follow a function tag. */
2630 if (funcdef != finlist && funcdef != fignore)
2631 funcdef = fnone;
2632 break;
2633 case '\0':
2634 if (objdef == otagseen)
2636 make_tag (TRUE);
2637 objdef = oignore;
2639 /* If a macro spans multiple lines don't reset its state. */
2640 if (quotednl)
2641 CNL_SAVE_DEFINEDEF;
2642 else
2643 CNL;
2644 break;
2645 } /* switch (c) */
2647 } /* while not eof */
2651 * Process either a C++ file or a C file depending on the setting
2652 * of a global flag.
2654 void
2655 default_C_entries (inf)
2656 FILE *inf;
2658 C_entries (cplusplus ? C_PLPL : 0, inf);
2661 /* Always do plain ANSI C. */
2662 void
2663 plain_C_entries (inf)
2664 FILE *inf;
2666 C_entries (0, inf);
2669 /* Always do C++. */
2670 void
2671 Cplusplus_entries (inf)
2672 FILE *inf;
2674 C_entries (C_PLPL, inf);
2677 /* Always do C*. */
2678 void
2679 Cstar_entries (inf)
2680 FILE *inf;
2682 C_entries (C_STAR, inf);
2685 /* Always do Yacc. */
2686 void
2687 Yacc_entries (inf)
2688 FILE *inf;
2690 C_entries (YACC, inf);
2693 /* Fortran parsing */
2695 char *dbp;
2697 logical
2698 tail (cp)
2699 char *cp;
2701 register int len = 0;
2703 while (*cp && lowcase(*cp) == lowcase(dbp[len]))
2704 cp++, len++;
2705 if (*cp == '\0' && !intoken(dbp[len]))
2707 dbp += len;
2708 return TRUE;
2710 return FALSE;
2713 void
2714 takeprec ()
2716 while (isspace (*dbp))
2717 dbp++;
2718 if (*dbp != '*')
2719 return;
2720 dbp++;
2721 while (isspace (*dbp))
2722 dbp++;
2723 if (strneq (dbp, "(*)", 3))
2725 dbp += 3;
2726 return;
2728 if (!isdigit (*dbp))
2730 --dbp; /* force failure */
2731 return;
2734 dbp++;
2735 while (isdigit (*dbp));
2738 void
2739 getit (inf)
2740 FILE *inf;
2742 register char *cp;
2744 while (isspace (*dbp))
2745 dbp++;
2746 if (*dbp == '\0')
2748 lineno++;
2749 linecharno = charno;
2750 charno += readline (&lb, inf);
2751 dbp = lb.buffer;
2752 if (dbp[5] != '&')
2753 return;
2754 dbp += 6;
2755 while (isspace (*dbp))
2756 dbp++;
2758 if (!isalpha (*dbp)
2759 && *dbp != '_'
2760 && *dbp != '$')
2761 return;
2762 for (cp = dbp + 1;
2763 (*cp
2764 && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
2765 cp++)
2766 continue;
2767 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
2768 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2771 void
2772 Fortran_functions (inf)
2773 FILE *inf;
2775 lineno = 0;
2776 charno = 0;
2778 while (!feof (inf))
2780 lineno++;
2781 linecharno = charno;
2782 charno += readline (&lb, inf);
2783 dbp = lb.buffer;
2784 if (*dbp == '%')
2785 dbp++; /* Ratfor escape to fortran */
2786 while (isspace (*dbp))
2787 dbp++;
2788 if (*dbp == '\0')
2789 continue;
2790 switch (lowcase (*dbp))
2792 case 'i':
2793 if (tail ("integer"))
2794 takeprec ();
2795 break;
2796 case 'r':
2797 if (tail ("real"))
2798 takeprec ();
2799 break;
2800 case 'l':
2801 if (tail ("logical"))
2802 takeprec ();
2803 break;
2804 case 'c':
2805 if (tail ("complex") || tail ("character"))
2806 takeprec ();
2807 break;
2808 case 'd':
2809 if (tail ("double"))
2811 while (isspace (*dbp))
2812 dbp++;
2813 if (*dbp == '\0')
2814 continue;
2815 if (tail ("precision"))
2816 break;
2817 continue;
2819 break;
2821 while (isspace (*dbp))
2822 dbp++;
2823 if (*dbp == '\0')
2824 continue;
2825 switch (lowcase (*dbp))
2827 case 'f':
2828 if (tail ("function"))
2829 getit (inf);
2830 continue;
2831 case 's':
2832 if (tail ("subroutine"))
2833 getit (inf);
2834 continue;
2835 case 'e':
2836 if (tail ("entry"))
2837 getit (inf);
2838 continue;
2839 case 'p':
2840 if (tail ("program"))
2842 getit (inf);
2843 continue;
2845 if (tail ("procedure"))
2846 getit (inf);
2847 continue;
2853 * Bob Weiner, Motorola Inc., 4/3/94
2854 * Unix and microcontroller assembly tag handling
2855 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2857 void
2858 Asm_labels (inf)
2859 FILE *inf;
2861 register char *cp;
2863 lineno = 0;
2864 charno = 0;
2866 while (!feof (inf))
2868 lineno++;
2869 linecharno = charno;
2870 charno += readline (&lb, inf);
2871 cp = lb.buffer;
2873 /* If first char is alphabetic or one of [_.$], test for colon
2874 following identifier. */
2875 if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2877 /* Read past label. */
2878 cp++;
2879 while (isalnum (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2880 cp++;
2881 if (*cp == ':' || isspace (*cp))
2883 /* Found end of label, so copy it and add it to the table. */
2884 pfnote ((CTAGS) ? savenstr(lb.buffer, cp-lb.buffer) : NULL, TRUE,
2885 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2892 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2893 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2895 void
2896 Perl_functions (inf)
2897 FILE *inf;
2899 register char *cp;
2901 lineno = 0;
2902 charno = 0;
2904 while (!feof (inf))
2906 lineno++;
2907 linecharno = charno;
2908 charno += readline (&lb, inf);
2909 cp = lb.buffer;
2911 if (*cp++ == 's' && *cp++ == 'u' && *cp++ == 'b' && isspace(*cp++))
2913 while (*cp && isspace(*cp))
2914 cp++;
2915 while (*cp && ! isspace(*cp) && *cp != '{')
2916 cp++;
2917 pfnote ((CTAGS) ? savenstr (lb.buffer, cp-lb.buffer) : NULL, TRUE,
2918 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2923 /* Added by Mosur Mohan, 4/22/88 */
2924 /* Pascal parsing */
2926 #define GET_NEW_LINE \
2928 linecharno = charno; lineno++; \
2929 charno += 1 + readline (&lb, inf); \
2930 dbp = lb.buffer; \
2934 * Locates tags for procedures & functions. Doesn't do any type- or
2935 * var-definitions. It does look for the keyword "extern" or
2936 * "forward" immediately following the procedure statement; if found,
2937 * the tag is skipped.
2939 void
2940 Pascal_functions (inf)
2941 FILE *inf;
2943 struct linebuffer tline; /* mostly copied from C_entries */
2944 long save_lcno;
2945 int save_lineno, save_len;
2946 char c, *cp, *namebuf;
2948 logical /* each of these flags is TRUE iff: */
2949 incomment, /* point is inside a comment */
2950 inquote, /* point is inside '..' string */
2951 get_tagname, /* point is after PROCEDURE/FUNCTION
2952 keyword, so next item = potential tag */
2953 found_tag, /* point is after a potential tag */
2954 inparms, /* point is within parameter-list */
2955 verify_tag; /* point has passed the parm-list, so the
2956 next token will determine whether this
2957 is a FORWARD/EXTERN to be ignored, or
2958 whether it is a real tag */
2960 lineno = 0;
2961 charno = 0;
2962 dbp = lb.buffer;
2963 *dbp = '\0';
2964 save_len = 0;
2965 initbuffer (&tline);
2967 incomment = inquote = FALSE;
2968 found_tag = FALSE; /* have a proc name; check if extern */
2969 get_tagname = FALSE; /* have found "procedure" keyword */
2970 inparms = FALSE; /* found '(' after "proc" */
2971 verify_tag = FALSE; /* check if "extern" is ahead */
2973 /* long main loop to get next char */
2974 while (!feof (inf))
2976 c = *dbp++;
2977 if (c == '\0') /* if end of line */
2979 GET_NEW_LINE;
2980 if (*dbp == '\0')
2981 continue;
2982 if (!((found_tag && verify_tag) ||
2983 get_tagname))
2984 c = *dbp++; /* only if don't need *dbp pointing
2985 to the beginning of the name of
2986 the procedure or function */
2988 if (incomment)
2990 if (c == '}') /* within { } comments */
2991 incomment = FALSE;
2992 else if (c == '*' && *dbp == ')') /* within (* *) comments */
2994 dbp++;
2995 incomment = FALSE;
2997 continue;
2999 else if (inquote)
3001 if (c == '\'')
3002 inquote = FALSE;
3003 continue;
3005 else
3006 switch (c)
3008 case '\'':
3009 inquote = TRUE; /* found first quote */
3010 continue;
3011 case '{': /* found open { comment */
3012 incomment = TRUE;
3013 continue;
3014 case '(':
3015 if (*dbp == '*') /* found open (* comment */
3017 incomment = TRUE;
3018 dbp++;
3020 else if (found_tag) /* found '(' after tag, i.e., parm-list */
3021 inparms = TRUE;
3022 continue;
3023 case ')': /* end of parms list */
3024 if (inparms)
3025 inparms = FALSE;
3026 continue;
3027 case ';':
3028 if (found_tag && !inparms) /* end of proc or fn stmt */
3030 verify_tag = TRUE;
3031 break;
3033 continue;
3035 if (found_tag && verify_tag && (*dbp != ' '))
3037 /* check if this is an "extern" declaration */
3038 if (*dbp == '\0')
3039 continue;
3040 if (lowcase (*dbp == 'e'))
3042 if (tail ("extern")) /* superfluous, really! */
3044 found_tag = FALSE;
3045 verify_tag = FALSE;
3048 else if (lowcase (*dbp) == 'f')
3050 if (tail ("forward")) /* check for forward reference */
3052 found_tag = FALSE;
3053 verify_tag = FALSE;
3056 if (found_tag && verify_tag) /* not external proc, so make tag */
3058 found_tag = FALSE;
3059 verify_tag = FALSE;
3060 pfnote (namebuf, TRUE,
3061 tline.buffer, save_len, save_lineno, save_lcno);
3062 continue;
3065 if (get_tagname) /* grab name of proc or fn */
3067 if (*dbp == '\0')
3068 continue;
3070 /* save all values for later tagging */
3071 GROW_LINEBUFFER (tline, strlen (lb.buffer) + 1);
3072 strcpy (tline.buffer, lb.buffer);
3073 save_lineno = lineno;
3074 save_lcno = linecharno;
3076 /* grab block name */
3077 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
3078 continue;
3079 namebuf = (CTAGS) ? savenstr (dbp, cp-dbp) : NULL;
3080 dbp = cp; /* set dbp to e-o-token */
3081 save_len = dbp - lb.buffer + 1;
3082 get_tagname = FALSE;
3083 found_tag = TRUE;
3084 continue;
3086 /* and proceed to check for "extern" */
3088 else if (!incomment && !inquote && !found_tag)
3090 /* check for proc/fn keywords */
3091 switch (lowcase (c))
3093 case 'p':
3094 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3095 get_tagname = TRUE;
3096 continue;
3097 case 'f':
3098 if (tail ("unction"))
3099 get_tagname = TRUE;
3100 continue;
3103 } /* while not eof */
3105 free (tline.buffer);
3109 * lisp tag functions
3110 * look for (def or (DEF, quote or QUOTE
3113 L_isdef (strp)
3114 register char *strp;
3116 return ((strp[1] == 'd' || strp[1] == 'D')
3117 && (strp[2] == 'e' || strp[2] == 'E')
3118 && (strp[3] == 'f' || strp[3] == 'F'));
3122 L_isquote (strp)
3123 register char *strp;
3125 return ((*(++strp) == 'q' || *strp == 'Q')
3126 && (*(++strp) == 'u' || *strp == 'U')
3127 && (*(++strp) == 'o' || *strp == 'O')
3128 && (*(++strp) == 't' || *strp == 'T')
3129 && (*(++strp) == 'e' || *strp == 'E')
3130 && isspace(*(++strp)));
3133 void
3134 L_getit ()
3136 register char *cp;
3138 if (*dbp == '\'') /* Skip prefix quote */
3139 dbp++;
3140 else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
3142 dbp += 7;
3143 while (isspace(*dbp))
3144 dbp++;
3146 for (cp = dbp /*+1*/;
3147 *cp && *cp != '(' && *cp != ' ' && *cp != ')';
3148 cp++)
3149 continue;
3150 if (cp == dbp)
3151 return;
3153 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3154 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3157 void
3158 Lisp_functions (inf)
3159 FILE *inf;
3161 lineno = 0;
3162 charno = 0;
3164 while (!feof (inf))
3166 lineno++;
3167 linecharno = charno;
3168 charno += readline (&lb, inf);
3169 dbp = lb.buffer;
3170 if (dbp[0] == '(')
3172 if (L_isdef (dbp))
3174 while (!isspace (*dbp))
3175 dbp++;
3176 while (isspace (*dbp))
3177 dbp++;
3178 L_getit ();
3180 else
3182 /* Check for (foo::defmumble name-defined ... */
3184 dbp++;
3185 while (*dbp && !isspace (*dbp)
3186 && *dbp != ':' && *dbp != '(' && *dbp != ')');
3187 if (*dbp == ':')
3190 dbp++;
3191 while (*dbp == ':');
3193 if (L_isdef (dbp - 1))
3195 while (!isspace (*dbp))
3196 dbp++;
3197 while (isspace (*dbp))
3198 dbp++;
3199 L_getit ();
3208 * Scheme tag functions
3209 * look for (def... xyzzy
3210 * look for (def... (xyzzy
3211 * look for (def ... ((...(xyzzy ....
3212 * look for (set! xyzzy
3215 void get_scheme ();
3217 void
3218 Scheme_functions (inf)
3219 FILE *inf;
3221 lineno = 0;
3222 charno = 0;
3224 while (!feof (inf))
3226 lineno++;
3227 linecharno = charno;
3228 charno += readline (&lb, inf);
3229 dbp = lb.buffer;
3230 if (dbp[0] == '(' &&
3231 (dbp[1] == 'D' || dbp[1] == 'd') &&
3232 (dbp[2] == 'E' || dbp[2] == 'e') &&
3233 (dbp[3] == 'F' || dbp[3] == 'f'))
3235 while (!isspace (*dbp))
3236 dbp++;
3237 /* Skip over open parens and white space */
3238 while (*dbp && (isspace (*dbp) || *dbp == '('))
3239 dbp++;
3240 get_scheme ();
3242 if (dbp[0] == '(' &&
3243 (dbp[1] == 'S' || dbp[1] == 's') &&
3244 (dbp[2] == 'E' || dbp[2] == 'e') &&
3245 (dbp[3] == 'T' || dbp[3] == 't') &&
3246 (dbp[4] == '!' || dbp[4] == '!') &&
3247 (isspace (dbp[5])))
3249 while (!isspace (*dbp))
3250 dbp++;
3251 /* Skip over white space */
3252 while (isspace (*dbp))
3253 dbp++;
3254 get_scheme ();
3259 void
3260 get_scheme ()
3262 register char *cp;
3264 if (*dbp == '\0')
3265 return;
3266 /* Go till you get to white space or a syntactic break */
3267 for (cp = dbp + 1;
3268 *cp && *cp != '(' && *cp != ')' && !isspace (*cp);
3269 cp++)
3270 continue;
3271 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3272 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3275 /* Find tags in TeX and LaTeX input files. */
3277 /* TEX_toktab is a table of TeX control sequences that define tags.
3278 Each TEX_tabent records one such control sequence.
3279 CONVERT THIS TO USE THE Stab TYPE!! */
3280 struct TEX_tabent
3282 char *name;
3283 int len;
3286 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
3288 /* Default set of control sequences to put into TEX_toktab.
3289 The value of environment var TEXTAGS is prepended to this. */
3291 char *TEX_defenv = "\
3292 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3293 :part:appendix:entry:index";
3295 void TEX_mode ();
3296 struct TEX_tabent *TEX_decode_env ();
3297 int TEX_Token ();
3298 #if TeX_named_tokens
3299 void TEX_getit ();
3300 #endif
3302 char TEX_esc = '\\';
3303 char TEX_opgrp = '{';
3304 char TEX_clgrp = '}';
3307 * TeX/LaTeX scanning loop.
3309 void
3310 TeX_functions (inf)
3311 FILE *inf;
3313 char *lasthit;
3315 lineno = 0;
3316 charno = 0;
3318 /* Select either \ or ! as escape character. */
3319 TEX_mode (inf);
3321 /* Initialize token table once from environment. */
3322 if (!TEX_toktab)
3323 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
3325 while (!feof (inf))
3326 { /* Scan each line in file */
3327 lineno++;
3328 linecharno = charno;
3329 charno += readline (&lb, inf);
3330 dbp = lb.buffer;
3331 lasthit = dbp;
3332 while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
3334 register int i;
3336 if (!*(++dbp))
3337 break;
3338 linecharno += dbp - lasthit;
3339 lasthit = dbp;
3340 i = TEX_Token (lasthit);
3341 if (0 <= i)
3343 pfnote (NULL, TRUE,
3344 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3345 #if TeX_named_tokens
3346 TEX_getit (lasthit, TEX_toktab[i].len);
3347 #endif
3348 break; /* We only save a line once */
3354 #define TEX_LESC '\\'
3355 #define TEX_SESC '!'
3356 #define TEX_cmt '%'
3358 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3359 chars accordingly. */
3360 void
3361 TEX_mode (inf)
3362 FILE *inf;
3364 int c;
3366 while ((c = getc (inf)) != EOF)
3368 /* Skip to next line if we hit the TeX comment char. */
3369 if (c == TEX_cmt)
3370 while (c != '\n')
3371 c = getc (inf);
3372 else if (c == TEX_LESC || c == TEX_SESC )
3373 break;
3376 if (c == TEX_LESC)
3378 TEX_esc = TEX_LESC;
3379 TEX_opgrp = '{';
3380 TEX_clgrp = '}';
3382 else
3384 TEX_esc = TEX_SESC;
3385 TEX_opgrp = '<';
3386 TEX_clgrp = '>';
3388 rewind (inf);
3391 /* Read environment and prepend it to the default string.
3392 Build token table. */
3393 struct TEX_tabent *
3394 TEX_decode_env (evarname, defenv)
3395 char *evarname;
3396 char *defenv;
3398 register char *env, *p;
3400 struct TEX_tabent *tab;
3401 int size, i;
3403 /* Append default string to environment. */
3404 env = getenv (evarname);
3405 if (!env)
3406 env = defenv;
3407 else
3408 env = concat (env, defenv, "");
3410 /* Allocate a token table */
3411 for (size = 1, p = env; p;)
3412 if ((p = etags_strchr (p, ':')) && *(++p))
3413 size++;
3414 /* Add 1 to leave room for null terminator. */
3415 tab = xnew (size + 1, struct TEX_tabent);
3417 /* Unpack environment string into token table. Be careful about */
3418 /* zero-length strings (leading ':', "::" and trailing ':') */
3419 for (i = 0; *env;)
3421 p = etags_strchr (env, ':');
3422 if (!p) /* End of environment string. */
3423 p = env + strlen (env);
3424 if (p - env > 0)
3425 { /* Only non-zero strings. */
3426 tab[i].name = savenstr (env, p - env);
3427 tab[i].len = strlen (tab[i].name);
3428 i++;
3430 if (*p)
3431 env = p + 1;
3432 else
3434 tab[i].name = NULL; /* Mark end of table. */
3435 tab[i].len = 0;
3436 break;
3439 return tab;
3442 #if TeX_named_tokens
3443 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3444 The name being defined actually starts at (NAME + LEN + 1).
3445 But we seem to include the TeX command in the tag name. */
3446 void
3447 TEX_getit (name, len)
3448 char *name;
3449 int len;
3451 char *p = name + len;
3453 if (*name == '\0')
3454 return;
3456 /* Let tag name extend to next group close (or end of line) */
3457 while (*p && *p != TEX_clgrp)
3458 p++;
3459 pfnote (savenstr (name, p-name), TRUE,
3460 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3462 #endif
3464 /* If the text at CP matches one of the tag-defining TeX command names,
3465 return the pointer to the first occurrence of that command in TEX_toktab.
3466 Otherwise return -1.
3467 Keep the capital `T' in `Token' for dumb truncating compilers
3468 (this distinguishes it from `TEX_toktab' */
3470 TEX_Token (cp)
3471 char *cp;
3473 int i;
3475 for (i = 0; TEX_toktab[i].len > 0; i++)
3476 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
3477 return i;
3478 return -1;
3482 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
3484 * Assumes that the predicate starts at column 0.
3485 * Only the first clause of a predicate is added.
3487 void
3488 Prolog_functions (inf)
3489 FILE *inf;
3491 int prolog_pred ();
3492 void prolog_skip_comment ();
3494 char * last;
3495 int len;
3496 int allocated;
3498 allocated = 0;
3499 len = 0;
3500 last = NULL;
3502 lineno = 0;
3503 linecharno = 0;
3504 charno = 0;
3506 while (!feof (inf))
3508 lineno++;
3509 linecharno += charno;
3510 charno = readline (&lb, inf);
3511 dbp = lb.buffer;
3512 if (dbp[0] == '\0') /* Empty line */
3513 continue;
3514 else if (isspace (dbp[0])) /* Not a predicate */
3515 continue;
3516 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
3517 prolog_skip_comment (&lb, inf, &lineno, &linecharno);
3518 else if (len = prolog_pred (dbp, last))
3520 /* Predicate. Store the function name so that we only
3521 * generates a tag for the first clause. */
3522 if (last == NULL)
3523 last = xnew(len + 1, char);
3524 else if (len + 1 > allocated)
3525 last = (char *) xrealloc(last, len + 1);
3526 allocated = len + 1;
3527 strncpy (last, dbp, len);
3528 last[len] = '\0';
3534 void
3535 prolog_skip_comment (plb, inf)
3536 struct linebuffer *plb;
3537 FILE *inf;
3539 char *cp;
3543 for (cp = plb->buffer; *cp != '\0'; cp++)
3544 if (cp[0] == '*' && cp[1] == '/')
3545 return;
3546 lineno++;
3547 linecharno += readline (plb, inf);
3549 while (!feof(inf));
3553 * A predicate definition is added if it matches:
3554 * <beginning of line><Prolog Atom><whitespace>(
3556 * It is added to the tags database if it doesn't match the
3557 * name of the previous clause header.
3559 * Return the size of the name of the predicate, or 0 if no header
3560 * was found.
3563 prolog_pred (s, last)
3564 char *s;
3565 char *last; /* Name of last clause. */
3567 int prolog_atom();
3568 int prolog_white();
3570 int pos;
3571 int len;
3573 pos = prolog_atom(s, 0);
3574 if (pos < 1)
3575 return 0;
3577 len = pos;
3578 pos += prolog_white(s, pos);
3580 if ((s[pos] == '(') || (s[pos] == '.'))
3582 if (s[pos] == '(')
3583 pos++;
3585 /* Save only the first clause. */
3586 if ((last == NULL) ||
3587 (len != strlen(last)) ||
3588 (strncmp(s, last, len) != 0))
3590 pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
3591 s, pos, lineno, linecharno);
3592 return len;
3595 return 0;
3599 * Consume a Prolog atom.
3600 * Return the number of bytes consumed, or -1 if there was an error.
3602 * A prolog atom, in this context, could be one of:
3603 * - An alphanumeric sequence, starting with a lower case letter.
3604 * - A quoted arbitrary string. Single quotes can escape themselves.
3605 * Backslash quotes everything.
3608 prolog_atom (s, pos)
3609 char *s;
3610 int pos;
3612 int origpos;
3614 origpos = pos;
3616 if (islower(s[pos]) || (s[pos] == '_'))
3618 /* The atom is unquoted. */
3619 pos++;
3620 while (isalnum(s[pos]) || (s[pos] == '_'))
3622 pos++;
3624 return pos - origpos;
3626 else if (s[pos] == '\'')
3628 pos++;
3630 while (1)
3632 if (s[pos] == '\'')
3634 pos++;
3635 if (s[pos] != '\'')
3636 break;
3637 pos++; /* A double quote */
3639 else if (s[pos] == '\0')
3640 /* Multiline quoted atoms are ignored. */
3641 return -1;
3642 else if (s[pos] == '\\')
3644 if (s[pos+1] == '\0')
3645 return -1;
3646 pos += 2;
3648 else
3649 pos++;
3651 return pos - origpos;
3653 else
3654 return -1;
3657 /* Consume whitespace. Return the number of bytes eaten. */
3659 prolog_white (s, pos)
3660 char *s;
3661 int pos;
3663 int origpos;
3665 origpos = pos;
3667 while (isspace(s[pos]))
3668 pos++;
3670 return pos - origpos;
3674 * Support for Erlang -- Anders Lindgren, Feb 1996.
3676 * Generates tags for functions, defines, and records.
3678 * Assumes that Erlang functions start at column 0.
3680 void
3681 Erlang_functions (inf)
3682 FILE *inf;
3684 int erlang_func ();
3685 void erlang_attribute ();
3687 char * last;
3688 int len;
3689 int allocated;
3691 allocated = 0;
3692 len = 0;
3693 last = NULL;
3695 lineno = 0;
3696 linecharno = 0;
3697 charno = 0;
3699 while (!feof (inf))
3701 lineno++;
3702 linecharno += charno;
3703 charno = readline (&lb, inf);
3704 dbp = lb.buffer;
3705 if (dbp[0] == '\0') /* Empty line */
3706 continue;
3707 else if (isspace (dbp[0])) /* Not function nor attribute */
3708 continue;
3709 else if (dbp[0] == '%') /* comment */
3710 continue;
3711 else if (dbp[0] == '"') /* Sometimes, strings start in column one */
3712 continue;
3713 else if (dbp[0] == '-') /* attribute, e.g. "-define" */
3715 erlang_attribute(dbp);
3716 last = NULL;
3718 else if (len = erlang_func (dbp, last))
3721 * Function. Store the function name so that we only
3722 * generates a tag for the first clause.
3724 if (last == NULL)
3725 last = xnew(len + 1, char);
3726 else if (len + 1 > allocated)
3727 last = (char *) xrealloc(last, len + 1);
3728 allocated = len + 1;
3729 strncpy (last, dbp, len);
3730 last[len] = '\0';
3737 * A function definition is added if it matches:
3738 * <beginning of line><Erlang Atom><whitespace>(
3740 * It is added to the tags database if it doesn't match the
3741 * name of the previous clause header.
3743 * Return the size of the name of the function, or 0 if no function
3744 * was found.
3747 erlang_func (s, last)
3748 char *s;
3749 char *last; /* Name of last clause. */
3751 int erlang_atom ();
3752 int erlang_white ();
3754 int pos;
3755 int len;
3757 pos = erlang_atom(s, 0);
3758 if (pos < 1)
3759 return 0;
3761 len = pos;
3762 pos += erlang_white(s, pos);
3764 if (s[pos++] == '(')
3766 /* Save only the first clause. */
3767 if ((last == NULL) ||
3768 (len != strlen(last)) ||
3769 (strncmp(s, last, len) != 0))
3771 pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
3772 s, pos, lineno, linecharno);
3773 return len;
3776 return 0;
3781 * Handle attributes. Currently, tags are generated for defines
3782 * and records.
3784 * They are on the form:
3785 * -define(foo, bar).
3786 * -define(Foo(M, N), M+N).
3787 * -record(graph, {vtab = notable, cyclic = true}).
3789 void
3790 erlang_attribute (s)
3791 char *s;
3793 int erlang_atom ();
3794 int erlang_white ();
3796 int pos;
3797 int len;
3799 if ((strncmp(s, "-define", 7) == 0) ||
3800 (strncmp(s, "-record", 7) == 0))
3802 pos = 7;
3803 pos += erlang_white(s, pos);
3805 if (s[pos++] == '(')
3807 pos += erlang_white(s, pos);
3809 if (len = erlang_atom(s, pos))
3811 pfnote ((CTAGS) ? savenstr (& s[pos], len) : NULL, TRUE,
3812 s, pos + len, lineno, linecharno);
3816 return;
3821 * Consume an Erlang atom (or variable).
3822 * Return the number of bytes consumed, or -1 if there was an error.
3825 erlang_atom (s, pos)
3826 char *s;
3827 int pos;
3829 int origpos;
3831 origpos = pos;
3833 if (isalpha (s[pos]) || s[pos] == '_')
3835 /* The atom is unquoted. */
3836 pos++;
3837 while (isalnum (s[pos]) || s[pos] == '_')
3838 pos++;
3839 return pos - origpos;
3841 else if (s[pos] == '\'')
3843 pos++;
3845 while (1)
3847 if (s[pos] == '\'')
3849 pos++;
3850 break;
3852 else if (s[pos] == '\0')
3853 /* Multiline quoted atoms are ignored. */
3854 return -1;
3855 else if (s[pos] == '\\')
3857 if (s[pos+1] == '\0')
3858 return -1;
3859 pos += 2;
3861 else
3862 pos++;
3864 return pos - origpos;
3866 else
3867 return -1;
3870 /* Consume whitespace. Return the number of bytes eaten */
3872 erlang_white (s, pos)
3873 char *s;
3874 int pos;
3876 int origpos;
3878 origpos = pos;
3880 while (isspace (s[pos]))
3881 pos++;
3883 return pos - origpos;
3886 #ifdef ETAGS_REGEXPS
3887 /* Take a string like "/blah/" and turn it into "blah", making sure
3888 that the first and last characters are the same, and handling
3889 quoted separator characters. Actually, stops on the occurrence of
3890 an unquoted separator. Also turns "\t" into a Tab character.
3891 Returns pointer to terminating separator. Works in place. Null
3892 terminates name string. */
3893 char *
3894 scan_separators (name)
3895 char *name;
3897 char sep = name[0];
3898 char *copyto = name;
3899 logical quoted = FALSE;
3901 for (++name; *name != '\0'; ++name)
3903 if (quoted)
3905 if (*name == 't')
3906 *copyto++ = '\t';
3907 else if (*name == sep)
3908 *copyto++ = sep;
3909 else
3911 /* Something else is quoted, so preserve the quote. */
3912 *copyto++ = '\\';
3913 *copyto++ = *name;
3915 quoted = FALSE;
3917 else if (*name == '\\')
3918 quoted = TRUE;
3919 else if (*name == sep)
3920 break;
3921 else
3922 *copyto++ = *name;
3925 /* Terminate copied string. */
3926 *copyto = '\0';
3927 return name;
3930 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3931 expression, into a real regular expression by compiling it. */
3932 void
3933 add_regex (regexp_pattern)
3934 char *regexp_pattern;
3936 char *name;
3937 const char *err;
3938 struct re_pattern_buffer *patbuf;
3940 if (regexp_pattern == NULL)
3942 /* Remove existing regexps. */
3943 num_patterns = 0;
3944 patterns = NULL;
3945 return;
3948 if (regexp_pattern[0] == '\0')
3950 error ("missing regexp", 0);
3951 return;
3953 if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
3955 error ("%s: unterminated regexp", regexp_pattern);
3956 return;
3958 name = scan_separators (regexp_pattern);
3959 if (regexp_pattern[0] == '\0')
3961 error ("null regexp", 0);
3962 return;
3964 (void) scan_separators (name);
3966 patbuf = xnew (1, struct re_pattern_buffer);
3967 patbuf->translate = NULL;
3968 patbuf->fastmap = NULL;
3969 patbuf->buffer = NULL;
3970 patbuf->allocated = 0;
3972 err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
3973 if (err != NULL)
3975 error ("%s while compiling pattern", err);
3976 return;
3979 num_patterns += 1;
3980 if (num_patterns == 1)
3981 patterns = xnew (1, struct pattern);
3982 else
3983 patterns = ((struct pattern *)
3984 xrealloc (patterns,
3985 (num_patterns * sizeof (struct pattern))));
3986 patterns[num_patterns - 1].pattern = patbuf;
3987 patterns[num_patterns - 1].name_pattern = savestr (name);
3988 patterns[num_patterns - 1].error_signaled = FALSE;
3992 * Do the substitutions indicated by the regular expression and
3993 * arguments.
3995 char *
3996 substitute (in, out, regs)
3997 char *in, *out;
3998 struct re_registers *regs;
4000 char *result = NULL, *t;
4001 int size = 0;
4003 /* Pass 1: figure out how much size to allocate. */
4004 for (t = out; *t; ++t)
4006 if (*t == '\\')
4008 ++t;
4009 if (!*t)
4011 fprintf (stderr, "%s: pattern substitution ends prematurely\n",
4012 progname);
4013 return NULL;
4015 if (isdigit (*t))
4017 int dig = *t - '0';
4018 size += regs->end[dig] - regs->start[dig];
4023 /* Allocate space and do the substitutions. */
4024 result = xnew (size + 1, char);
4025 size = 0;
4026 for (; *out; ++out)
4028 if (*out == '\\')
4030 ++out;
4031 if (isdigit (*out))
4033 /* Using "dig2" satisfies my debugger. Bleah. */
4034 int dig2 = *out - '0';
4035 strncpy (result + size, in + regs->start[dig2],
4036 regs->end[dig2] - regs->start[dig2]);
4037 size += regs->end[dig2] - regs->start[dig2];
4039 else
4040 result[size++] = *out;
4042 else
4043 result[size++] = *out;
4045 result[size] = '\0';
4047 return result;
4050 #endif /* ETAGS_REGEXPS */
4051 /* Initialize a linebuffer for use */
4052 void
4053 initbuffer (linebuffer)
4054 struct linebuffer *linebuffer;
4056 linebuffer->size = 200;
4057 linebuffer->buffer = xnew (200, char);
4061 * Read a line of text from `stream' into `linebuffer'.
4062 * Return the number of characters read from `stream',
4063 * which is the length of the line including the newline, if any.
4065 long
4066 readline_internal (linebuffer, stream)
4067 struct linebuffer *linebuffer;
4068 register FILE *stream;
4070 char *buffer = linebuffer->buffer;
4071 register char *p = linebuffer->buffer;
4072 register char *pend;
4073 int chars_deleted;
4075 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
4077 while (1)
4079 register int c = getc (stream);
4080 if (p == pend)
4082 linebuffer->size *= 2;
4083 buffer = (char *) xrealloc (buffer, linebuffer->size);
4084 p += buffer - linebuffer->buffer;
4085 pend = buffer + linebuffer->size;
4086 linebuffer->buffer = buffer;
4088 if (c == EOF)
4090 *p = '\0';
4091 chars_deleted = 0;
4092 break;
4094 if (c == '\n')
4096 if (p > buffer && p[-1] == '\r')
4098 *--p = '\0';
4099 chars_deleted = 2;
4101 else
4103 *p = '\0';
4104 chars_deleted = 1;
4106 break;
4108 *p++ = c;
4111 return p - buffer + chars_deleted;
4115 * Like readline_internal, above, but try to match the input
4116 * line against any existing regular expressions.
4118 long
4119 readline (linebuffer, stream)
4120 struct linebuffer *linebuffer;
4121 FILE *stream;
4123 /* Read new line. */
4124 long result = readline_internal (linebuffer, stream);
4125 #ifdef ETAGS_REGEXPS
4126 int i;
4128 /* Match against all listed patterns. */
4129 for (i = 0; i < num_patterns; ++i)
4131 int match = re_match (patterns[i].pattern, linebuffer->buffer,
4132 (int)result, 0, &patterns[i].regs);
4133 switch (match)
4135 case -2:
4136 /* Some error. */
4137 if (!patterns[i].error_signaled)
4139 error ("error while matching pattern %d", i);
4140 patterns[i].error_signaled = TRUE;
4142 break;
4143 case -1:
4144 /* No match. */
4145 break;
4146 default:
4147 /* Match occurred. Construct a tag. */
4148 if (patterns[i].name_pattern[0] != '\0')
4150 /* Make a named tag. */
4151 char *name = substitute (linebuffer->buffer,
4152 patterns[i].name_pattern,
4153 &patterns[i].regs);
4154 if (name != NULL)
4155 pfnote (name, TRUE,
4156 linebuffer->buffer, match, lineno, linecharno);
4158 else
4160 /* Make an unnamed tag. */
4161 pfnote (NULL, TRUE,
4162 linebuffer->buffer, match, lineno, linecharno);
4164 break;
4167 #endif /* ETAGS_REGEXPS */
4169 return result;
4173 * Read a file, but do no processing. This is used to do regexp
4174 * matching on files that have no language defined.
4176 void
4177 just_read_file (inf)
4178 FILE *inf;
4180 lineno = 0;
4181 charno = 0;
4183 while (!feof (inf))
4185 ++lineno;
4186 linecharno = charno;
4187 charno += readline (&lb, inf) + 1;
4193 * Return a pointer to a space of size strlen(cp)+1 allocated
4194 * with xnew where the string CP has been copied.
4196 char *
4197 savestr (cp)
4198 char *cp;
4200 return savenstr (cp, strlen (cp));
4204 * Return a pointer to a space of size LEN+1 allocated with xnew where
4205 * the string CP has been copied for at most the first LEN characters.
4207 char *
4208 savenstr (cp, len)
4209 char *cp;
4210 int len;
4212 register char *dp;
4214 dp = xnew (len + 1, char);
4215 strncpy (dp, cp, len);
4216 dp[len] = '\0';
4217 return dp;
4221 * Return the ptr in sp at which the character c last
4222 * appears; NULL if not found
4224 * Identical to System V strrchr, included for portability.
4226 char *
4227 etags_strrchr (sp, c)
4228 register char *sp, c;
4230 register char *r;
4232 r = NULL;
4235 if (*sp == c)
4236 r = sp;
4237 } while (*sp++);
4238 return r;
4243 * Return the ptr in sp at which the character c first
4244 * appears; NULL if not found
4246 * Identical to System V strchr, included for portability.
4248 char *
4249 etags_strchr (sp, c)
4250 register char *sp, c;
4254 if (*sp == c)
4255 return sp;
4256 } while (*sp++);
4257 return NULL;
4260 /* Print error message and exit. */
4261 void
4262 fatal (s1, s2)
4263 char *s1, *s2;
4265 error (s1, s2);
4266 exit (BAD);
4269 void
4270 pfatal (s1)
4271 char *s1;
4273 perror (s1);
4274 exit (BAD);
4277 void
4278 suggest_asking_for_help ()
4280 fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
4281 progname);
4282 exit (BAD);
4285 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
4286 void
4287 error (s1, s2)
4288 char *s1, *s2;
4290 fprintf (stderr, "%s: ", progname);
4291 fprintf (stderr, s1, s2);
4292 fprintf (stderr, "\n");
4295 /* Return a newly-allocated string whose contents
4296 concatenate those of s1, s2, s3. */
4297 char *
4298 concat (s1, s2, s3)
4299 char *s1, *s2, *s3;
4301 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
4302 char *result = xnew (len1 + len2 + len3 + 1, char);
4304 strcpy (result, s1);
4305 strcpy (result + len1, s2);
4306 strcpy (result + len1 + len2, s3);
4307 result[len1 + len2 + len3] = '\0';
4309 return result;
4312 /* Does the same work as the system V getcwd, but does not need to
4313 guess the buffer size in advance. */
4314 char *
4315 etags_getcwd ()
4317 #ifdef DOS_NT
4318 char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
4320 getwd (path);
4321 p = path;
4322 while (*p)
4324 if (*p == '\\')
4325 *p++ = '/';
4326 else
4327 *p++ = lowcase (*p);
4330 return strdup (path);
4331 #else /* not DOS_NT */
4332 #if HAVE_GETCWD
4333 int bufsize = 200;
4334 char *path = xnew (bufsize, char);
4336 while (getcwd (path, bufsize) == NULL)
4338 if (errno != ERANGE)
4339 pfatal ("getcwd");
4340 bufsize *= 2;
4341 path = xnew (bufsize, char);
4344 return path;
4345 #else /* not DOS_NT and not HAVE_GETCWD */
4346 struct linebuffer path;
4347 FILE *pipe;
4349 initbuffer (&path);
4350 pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
4351 if (pipe == NULL || readline_internal (&path, pipe) == 0)
4352 pfatal ("pwd");
4353 pclose (pipe);
4355 return path.buffer;
4356 #endif /* not HAVE_GETCWD */
4357 #endif /* not DOS_NT */
4360 /* Return a newly allocated string containing the filename
4361 of FILE relative to the absolute directory DIR (which
4362 should end with a slash). */
4363 char *
4364 relative_filename (file, dir)
4365 char *file, *dir;
4367 char *fp, *dp, *abs, *res;
4369 /* Find the common root of file and dir. */
4370 abs = absolute_filename (file, cwd);
4371 fp = abs;
4372 dp = dir;
4373 while (*fp++ == *dp++)
4374 continue;
4377 fp--;
4378 dp--;
4380 while (*fp != '/');
4382 /* Build a sequence of "../" strings for the resulting relative filename. */
4383 for (dp = etags_strchr (dp + 1, '/'), res = "";
4384 dp != NULL;
4385 dp = etags_strchr (dp + 1, '/'))
4387 res = concat (res, "../", "");
4390 /* Add the filename relative to the common root of file and dir. */
4391 res = concat (res, fp + 1, "");
4392 free (abs);
4394 return res;
4397 /* Return a newly allocated string containing the
4398 absolute filename of FILE given CWD (which should
4399 end with a slash). */
4400 char *
4401 absolute_filename (file, cwd)
4402 char *file, *cwd;
4404 char *slashp, *cp, *res;
4406 if (absolutefn (file))
4407 res = concat (file, "", "");
4408 #ifdef DOS_NT
4409 /* We don't support non-absolute filenames with a drive
4410 letter, like `d:NAME' (it's too much hassle). */
4411 else if (file[1] == ':')
4412 fatal ("%s: relative filenames with drive letters not supported", file);
4413 #endif
4414 else
4415 res = concat (cwd, file, "");
4417 /* Delete the "/dirname/.." and "/." substrings. */
4418 slashp = etags_strchr (res, '/');
4419 while (slashp != NULL && slashp[0] != '\0')
4421 if (slashp[1] == '.')
4423 if (slashp[2] == '.'
4424 && (slashp[3] == '/' || slashp[3] == '\0'))
4426 cp = slashp;
4428 cp--;
4429 while (cp >= res && !absolutefn (cp));
4430 if (*cp == '/')
4432 strcpy (cp, slashp + 3);
4434 #ifdef DOS_NT
4435 /* Under MSDOS and NT we get `d:/NAME' as absolute
4436 filename, so the luser could say `d:/../NAME'.
4437 We silently treat this as `d:/NAME'. */
4438 else if (cp[1] == ':')
4439 strcpy (cp + 3, slashp + 4);
4440 #endif
4441 else /* else (cp == res) */
4443 if (slashp[3] != '\0')
4444 strcpy (cp, slashp + 4);
4445 else
4446 return ".";
4448 slashp = cp;
4449 continue;
4451 else if (slashp[2] == '/' || slashp[2] == '\0')
4453 strcpy (slashp, slashp + 2);
4454 continue;
4458 slashp = etags_strchr (slashp + 1, '/');
4461 return res;
4464 /* Return a newly allocated string containing the absolute
4465 filename of dir where FILE resides given CWD (which should
4466 end with a slash). */
4467 char *
4468 absolute_dirname (file, cwd)
4469 char *file, *cwd;
4471 char *slashp, *res;
4472 char save;
4473 #ifdef DOS_NT
4474 char *p = file;
4476 while (*p)
4478 if (*p == '\\')
4479 *p = '/';
4480 ++p;
4482 #endif
4484 slashp = etags_strrchr (file, '/');
4485 if (slashp == NULL)
4486 return cwd;
4487 save = slashp[1];
4488 slashp[1] = '\0';
4489 res = absolute_filename (file, cwd);
4490 slashp[1] = save;
4492 return res;
4495 /* Like malloc but get fatal error if memory is exhausted. */
4496 long *
4497 xmalloc (size)
4498 unsigned int size;
4500 long *result = (long *) malloc (size);
4501 if (result == NULL)
4502 fatal ("virtual memory exhausted", 0);
4503 return result;
4506 long *
4507 xrealloc (ptr, size)
4508 char *ptr;
4509 unsigned int size;
4511 long *result = (long *) realloc (ptr, size);
4512 if (result == NULL)
4513 fatal ("virtual memory exhausted");
4514 return result;