(PC-try-load-many-files): Set truename.
[emacs.git] / lib-src / etags.c
blobd05962503ecc6518d7881966afe91742d9cf4baf
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.82";
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 #if !defined (MSDOS) && !defined (WINDOWSNT) && defined (STDC_HEADERS)
58 #include <stdlib.h>
59 #include <string.h>
60 #endif
62 #ifdef HAVE_CONFIG_H
63 # include <config.h>
64 /* On some systems, Emacs defines static as nothing for the sake
65 of unexec. We don't want that here since we don't use unexec. */
66 # undef static
67 #endif
69 #include <stdio.h>
70 #include <ctype.h>
71 #include <errno.h>
72 #ifndef errno
73 extern int errno;
74 #endif
75 #include <sys/types.h>
76 #include <sys/stat.h>
78 #if !defined (S_ISREG) && defined (S_IFREG)
79 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
80 #endif
82 #include <getopt.h>
84 #ifdef ETAGS_REGEXPS
85 # include <regex.h>
86 #endif /* ETAGS_REGEXPS */
88 /* Define CTAGS to make the program "ctags" compatible with the usual one.
89 Leave it undefined to make the program "etags", which makes emacs-style
90 tag tables and tags typedefs, #defines and struct/union/enum by default. */
91 #ifdef CTAGS
92 # undef CTAGS
93 # define CTAGS TRUE
94 #else
95 # define CTAGS FALSE
96 #endif
98 /* Exit codes for success and failure. */
99 #ifdef VMS
100 # define GOOD 1
101 # define BAD 0
102 #else
103 # define GOOD 0
104 # define BAD 1
105 #endif
107 /* C extensions. */
108 #define C_PLPL 0x00001 /* C++ */
109 #define C_STAR 0x00003 /* C* */
110 #define YACC 0x10000 /* yacc file */
112 #define streq(s,t) ((DEBUG && (s) == NULL && (t) == NULL \
113 && (abort (), 1)) || !strcmp (s, t))
114 #define strneq(s,t,n) ((DEBUG && (s) == NULL && (t) == NULL \
115 && (abort (), 1)) || !strncmp (s, t, n))
117 #define lowcase(c) tolower ((char)c)
119 #define iswhite(arg) (_wht[arg]) /* T if char is white */
120 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
121 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
122 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
124 #ifdef DOS_NT
125 # define absolutefn(fn) (fn[0] == '/' \
126 || (fn[1] == ':' && fn[2] == '/'))
127 #else
128 # define absolutefn(fn) (fn[0] == '/')
129 #endif
133 * xnew -- allocate storage
135 * SYNOPSIS: Type *xnew (int n, Type);
137 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
139 typedef int logical;
141 typedef struct nd_st
142 { /* sorting structure */
143 char *name; /* function or type name */
144 char *file; /* file name */
145 logical is_func; /* use pattern or line no */
146 logical been_warned; /* set if noticed dup */
147 int lno; /* line number tag is on */
148 long cno; /* character number line starts on */
149 char *pat; /* search pattern */
150 struct nd_st *left, *right; /* left and right sons */
151 } NODE;
153 extern char *getenv ();
155 char *concat ();
156 char *savenstr (), *savestr ();
157 char *etags_strchr (), *etags_strrchr ();
158 char *etags_getcwd ();
159 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
160 void grow_linebuffer ();
161 long *xmalloc (), *xrealloc ();
163 typedef void Lang_function ();
164 #if FALSE /* many compilers barf on this */
165 Lang_function Asm_labels;
166 Lang_function default_C_entries;
167 Lang_function C_entries;
168 Lang_function Cplusplus_entries;
169 Lang_function Cstar_entries;
170 Lang_function Erlang_functions;
171 Lang_function Fortran_functions;
172 Lang_function Yacc_entries;
173 Lang_function Lisp_functions;
174 Lang_function Pascal_functions;
175 Lang_function Perl_functions;
176 Lang_function Prolog_functions;
177 Lang_function Scheme_functions;
178 Lang_function TeX_functions;
179 Lang_function just_read_file;
180 #else /* so let's write it this way */
181 void Asm_labels ();
182 void C_entries ();
183 void default_C_entries ();
184 void plain_C_entries ();
185 void Cplusplus_entries ();
186 void Cstar_entries ();
187 void Erlang_functions ();
188 void Fortran_functions ();
189 void Yacc_entries ();
190 void Lisp_functions ();
191 void Pascal_functions ();
192 void Perl_functions ();
193 void Prolog_functions ();
194 void Scheme_functions ();
195 void TeX_functions ();
196 void just_read_file ();
197 #endif
199 Lang_function *get_language_from_name ();
200 Lang_function *get_language_from_interpreter ();
201 Lang_function *get_language_from_suffix ();
202 int total_size_of_entries ();
203 long readline ();
204 long readline_internal ();
205 #ifdef ETAGS_REGEXPS
206 void add_regex ();
207 #endif
208 void add_node ();
209 void error ();
210 void suggest_asking_for_help ();
211 void fatal (), pfatal ();
212 void find_entries ();
213 void free_tree ();
214 void getit ();
215 void init ();
216 void initbuffer ();
217 void pfnote ();
218 void process_file ();
219 void put_entries ();
220 void takeprec ();
223 char searchar = '/'; /* use /.../ searches */
225 int lineno; /* line number of current line */
226 long charno; /* current character number */
227 long linecharno; /* charno of start of line */
229 char *curfile; /* current input file name */
230 char *tagfile; /* output file */
231 char *progname; /* name this program was invoked with */
232 char *cwd; /* current working directory */
233 char *tagfiledir; /* directory of tagfile */
235 FILE *tagf; /* ioptr for tags file */
236 NODE *head; /* the head of the binary tree of tags */
239 * A `struct linebuffer' is a structure which holds a line of text.
240 * `readline' reads a line from a stream into a linebuffer and works
241 * regardless of the length of the line.
243 struct linebuffer
245 long size;
246 char *buffer;
249 struct linebuffer lb; /* the current line */
250 struct linebuffer token_name; /* used by C_entries as a temporary area */
251 struct
253 long linepos;
254 struct linebuffer lb; /* used by C_entries instead of lb */
255 } lbs[2];
257 /* boolean "functions" (see init) */
258 logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
259 char
260 /* white chars */
261 *white = " \f\t\n\013",
262 /* token ending chars */
263 *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
264 /* token starting chars */
265 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
266 /* valid in-token chars */
267 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
269 logical append_to_tagfile; /* -a: append to tags */
270 /* The following three default to TRUE for etags, but to FALSE for ctags. */
271 logical typedefs; /* -t: create tags for typedefs */
272 logical typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
273 /* 0 struct/enum/union decls, and C++ */
274 /* member functions. */
275 logical constantypedefs; /* -d: create tags for C #define and enum */
276 /* constants. */
277 /* -D: opposite of -d. Default under ctags. */
278 logical update; /* -u: update tags */
279 logical vgrind_style; /* -v: create vgrind style index output */
280 logical no_warnings; /* -w: suppress warnings */
281 logical cxref_style; /* -x: create cxref style output */
282 logical cplusplus; /* .[hc] means C++, not C */
283 logical noindentypedefs; /* -I: ignore indentation in C */
285 struct option longopts[] =
287 { "append", no_argument, NULL, 'a' },
288 { "backward-search", no_argument, NULL, 'B' },
289 { "c++", no_argument, NULL, 'C' },
290 { "cxref", no_argument, NULL, 'x' },
291 { "defines", no_argument, NULL, 'd' },
292 { "help", no_argument, NULL, 'h' },
293 { "help", no_argument, NULL, 'H' },
294 { "ignore-indentation", no_argument, NULL, 'I' },
295 { "include", required_argument, NULL, 'i' },
296 { "language", required_argument, NULL, 'l' },
297 { "no-defines", no_argument, NULL, 'D' },
298 { "no-regex", no_argument, NULL, 'R' },
299 { "no-warn", no_argument, NULL, 'w' },
300 { "output", required_argument, NULL, 'o' },
301 { "regex", required_argument, NULL, 'r' },
302 { "typedefs", no_argument, NULL, 't' },
303 { "typedefs-and-c++", no_argument, NULL, 'T' },
304 { "update", no_argument, NULL, 'u' },
305 { "version", no_argument, NULL, 'V' },
306 { "vgrind", no_argument, NULL, 'v' },
307 { 0 }
310 #ifdef ETAGS_REGEXPS
311 /* Structure defining a regular expression. Elements are
312 the compiled pattern, and the name string. */
313 struct pattern
315 struct re_pattern_buffer *pattern;
316 struct re_registers regs;
317 char *name_pattern;
318 logical error_signaled;
321 /* Number of regexps found. */
322 int num_patterns = 0;
324 /* Array of all regexps. */
325 struct pattern *patterns = NULL;
326 #endif /* ETAGS_REGEXPS */
329 * Language stuff.
332 /* Non-NULL if language fixed. */
333 Lang_function *lang_func = NULL;
335 /* Assembly code */
336 char *Asm_suffixes [] = { "a", /* Unix assembler */
337 "asm", /* Microcontroller assembly */
338 "def", /* BSO/Tasking definition includes */
339 "inc", /* Microcontroller include files */
340 "ins", /* Microcontroller include files */
341 "s", "sa", /* Unix assembler */
342 "src", /* BSO/Tasking C compiler output */
343 NULL
346 /* Note that .c and .h can be considered C++, if the --c++ flag was
347 given. That is why default_C_entries is called here. */
348 char *default_C_suffixes [] =
349 { "c", "h", NULL };
351 /* .M is for Objective C++ files. */
352 char *Cplusplus_suffixes [] =
353 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL};
355 char *Cstar_suffixes [] =
356 { "cs", "hs", NULL };
358 char *Erlang_suffixes [] =
359 { "erl", "hrl", NULL };
361 char *Fortran_suffixes [] =
362 { "F", "f", "f90", "for", NULL };
364 char *Lisp_suffixes [] =
365 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL };
367 char *Pascal_suffixes [] =
368 { "p", "pas", NULL };
370 char *Perl_suffixes [] =
371 { "pl", "pm", NULL };
372 char *Perl_interpreters [] =
373 { "perl", "@PERL@", NULL };
375 char *plain_C_suffixes [] =
376 { "pc", /* Pro*C file */
377 "m", /* Objective C file */
378 "lm", /* Objective lex file */
379 NULL };
381 char *Prolog_suffixes [] =
382 { "prolog", NULL };
384 /* Can't do the `SCM' or `scm' prefix with a version number. */
385 char *Scheme_suffixes [] =
386 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL };
388 char *TeX_suffixes [] =
389 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL };
391 char *Yacc_suffixes [] =
392 { "y", "ym", NULL }; /* .ym is Objective yacc file */
394 /* Table of language names and corresponding functions, file suffixes
395 and interpreter names.
396 It is ok for a given function to be listed under more than one
397 name. I just didn't. */
398 struct lang_entry
400 char *name;
401 Lang_function *function;
402 char **suffixes;
403 char **interpreters;
406 struct lang_entry lang_names [] =
408 { "asm", Asm_labels, Asm_suffixes, NULL },
409 { "c", default_C_entries, default_C_suffixes, NULL },
410 { "c++", Cplusplus_entries, Cplusplus_suffixes, NULL },
411 { "c*", Cstar_entries, Cstar_suffixes, NULL },
412 { "erlang", Erlang_functions, Erlang_suffixes, NULL },
413 { "fortran", Fortran_functions, Fortran_suffixes, NULL },
414 { "lisp", Lisp_functions, Lisp_suffixes, NULL },
415 { "pascal", Pascal_functions, Pascal_suffixes, NULL },
416 { "perl", Perl_functions, Perl_suffixes, Perl_interpreters },
417 { "proc", plain_C_entries, plain_C_suffixes, NULL },
418 { "prolog", Prolog_functions, Prolog_suffixes, NULL },
419 { "scheme", Scheme_functions, Scheme_suffixes, NULL },
420 { "tex", TeX_functions, TeX_suffixes, NULL },
421 { "yacc", Yacc_entries, Yacc_suffixes, NULL },
422 { "auto", NULL }, /* default guessing scheme */
423 { "none", just_read_file }, /* regexp matching only */
424 { NULL, NULL } /* end of list */
428 void
429 print_language_names ()
431 struct lang_entry *lang;
432 char **ext;
434 puts ("\nThese are the currently supported languages, along with the\n\
435 default file name suffixes:");
436 for (lang = lang_names; lang->name != NULL; lang++)
438 printf ("\t%s\t", lang->name);
439 if (lang->suffixes != NULL)
440 for (ext = lang->suffixes; *ext != NULL; ext++)
441 printf (" .%s", *ext);
442 puts ("");
444 puts ("Where `auto' means use default language for files based on file\n\
445 name suffix, and `none' means only do regexp processing on files.\n\
446 If no language is specified and no matching suffix is found,\n\
447 the first line of the file is read for a sharp-bang (#!) sequence\n\
448 followed by the name of an interpreter. If no such sequence is found,\n\
449 Fortran is tried first; if no tags are found, C is tried next.");
452 #ifndef VERSION
453 # define VERSION "19"
454 #endif
455 void
456 print_version ()
458 printf ("%s (GNU Emacs %s)\n", (CTAGS) ? "ctags" : "etags", VERSION);
459 puts ("Copyright (C) 1996 Free Software Foundation, Inc. and Ken Arnold");
460 puts ("This program is distributed under the same terms as Emacs");
462 exit (GOOD);
465 void
466 print_help ()
468 printf ("These are the options accepted by %s. You may use unambiguous\n\
469 abbreviations for the long option names. A - as file name means read\n\
470 names from stdin.", progname);
471 if (!CTAGS)
472 printf (" Absolute names are stored in the output file as they\n\
473 are. Relative ones are stored relative to the output file's directory.");
474 puts ("\n");
476 puts ("-a, --append\n\
477 Append tag entries to existing tags file.");
479 if (CTAGS)
480 puts ("-B, --backward-search\n\
481 Write the search commands for the tag entries using '?', the\n\
482 backward-search command instead of '/', the forward-search command.");
484 puts ("-C, --c++\n\
485 Treat files whose name suffix defaults to C language as C++ files.");
487 if (CTAGS)
488 puts ("-d, --defines\n\
489 Create tag entries for C #define constants and enum constants, too.");
490 else
491 puts ("-D, --no-defines\n\
492 Don't create tag entries for C #define constants and enum constants.\n\
493 This makes the tags file smaller.");
495 if (!CTAGS)
497 puts ("-i FILE, --include=FILE\n\
498 Include a note in tag file indicating that, when searching for\n\
499 a tag, one should also consult the tags file FILE after\n\
500 checking the current file.");
501 puts ("-l LANG, --language=LANG\n\
502 Force the following files to be considered as written in the\n\
503 named language up to the next --language=LANG option.");
506 #ifdef ETAGS_REGEXPS
507 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
508 Make a tag for each line matching pattern REGEXP in the\n\
509 following files. REGEXP is anchored (as if preceded by ^).\n\
510 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
511 named tags can be created with:\n\
512 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
513 puts ("-R, --no-regex\n\
514 Don't create tags from regexps for the following files.");
515 #endif /* ETAGS_REGEXPS */
516 puts ("-o FILE, --output=FILE\n\
517 Write the tags to FILE.");
518 puts ("-I, --ignore-indentation\n\
519 Don't rely on indentation quite as much as normal. Currently,\n\
520 this means not to assume that a closing brace in the first\n\
521 column is the final brace of a function or structure\n\
522 definition in C and C++.");
524 if (CTAGS)
526 puts ("-t, --typedefs\n\
527 Generate tag entries for C typedefs.");
528 puts ("-T, --typedefs-and-c++\n\
529 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
530 and C++ member functions.");
531 puts ("-u, --update\n\
532 Update the tag entries for the given files, leaving tag\n\
533 entries for other files in place. Currently, this is\n\
534 implemented by deleting the existing entries for the given\n\
535 files and then rewriting the new entries at the end of the\n\
536 tags file. It is often faster to simply rebuild the entire\n\
537 tag file than to use this.");
538 puts ("-v, --vgrind\n\
539 Generates an index of items intended for human consumption,\n\
540 similar to the output of vgrind. The index is sorted, and\n\
541 gives the page number of each item.");
542 puts ("-w, --no-warn\n\
543 Suppress warning messages about entries defined in multiple\n\
544 files.");
545 puts ("-x, --cxref\n\
546 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
547 The output uses line numbers instead of page numbers, but\n\
548 beyond that the differences are cosmetic; try both to see\n\
549 which you like.");
552 puts ("-V, --version\n\
553 Print the version of the program.\n\
554 -h, --help\n\
555 Print this help message.");
557 print_language_names ();
559 puts ("");
560 puts ("Report bugs to bug-gnu-emacs@prep.ai.mit.edu");
562 exit (GOOD);
566 enum argument_type
568 at_language,
569 at_regexp,
570 at_filename
573 /* This structure helps us allow mixing of --lang and filenames. */
574 typedef struct
576 enum argument_type arg_type;
577 char *what;
578 Lang_function *function;
579 } argument;
581 #ifdef VMS /* VMS specific functions */
583 #define EOS '\0'
585 /* This is a BUG! ANY arbitrary limit is a BUG!
586 Won't someone please fix this? */
587 #define MAX_FILE_SPEC_LEN 255
588 typedef struct {
589 short curlen;
590 char body[MAX_FILE_SPEC_LEN + 1];
591 } vspec;
594 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
595 returning in each successive call the next filename matching the input
596 spec. The function expects that each in_spec passed
597 to it will be processed to completion; in particular, up to and
598 including the call following that in which the last matching name
599 is returned, the function ignores the value of in_spec, and will
600 only start processing a new spec with the following call.
601 If an error occurs, on return out_spec contains the value
602 of in_spec when the error occurred.
604 With each successive filename returned in out_spec, the
605 function's return value is one. When there are no more matching
606 names the function returns zero. If on the first call no file
607 matches in_spec, or there is any other error, -1 is returned.
610 #include <rmsdef.h>
611 #include <descrip.h>
612 #define OUTSIZE MAX_FILE_SPEC_LEN
613 short
614 fn_exp (out, in)
615 vspec *out;
616 char *in;
618 static long context = 0;
619 static struct dsc$descriptor_s o;
620 static struct dsc$descriptor_s i;
621 static logical pass1 = TRUE;
622 long status;
623 short retval;
625 if (pass1)
627 pass1 = FALSE;
628 o.dsc$a_pointer = (char *) out;
629 o.dsc$w_length = (short)OUTSIZE;
630 i.dsc$a_pointer = in;
631 i.dsc$w_length = (short)strlen(in);
632 i.dsc$b_dtype = DSC$K_DTYPE_T;
633 i.dsc$b_class = DSC$K_CLASS_S;
634 o.dsc$b_dtype = DSC$K_DTYPE_VT;
635 o.dsc$b_class = DSC$K_CLASS_VS;
637 if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL)
639 out->body[out->curlen] = EOS;
640 return 1;
642 else if (status == RMS$_NMF)
643 retval = 0;
644 else
646 strcpy(out->body, in);
647 retval = -1;
649 lib$find_file_end(&context);
650 pass1 = TRUE;
651 return retval;
655 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
656 name of each file specified by the provided arg expanding wildcards.
658 char *
659 gfnames (arg, p_error)
660 char *arg;
661 logical *p_error;
663 static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
665 switch (fn_exp (&filename, arg))
667 case 1:
668 *p_error = FALSE;
669 return filename.body;
670 case 0:
671 *p_error = FALSE;
672 return NULL;
673 default:
674 *p_error = TRUE;
675 return filename.body;
679 #ifndef OLD /* Newer versions of VMS do provide `system'. */
680 system (cmd)
681 char *cmd;
683 fprintf (stderr, "system() function not implemented under VMS\n");
685 #endif
687 #define VERSION_DELIM ';'
688 char *massage_name (s)
689 char *s;
691 char *start = s;
693 for ( ; *s; s++)
694 if (*s == VERSION_DELIM)
696 *s = EOS;
697 break;
699 else
700 *s = lowcase (*s);
701 return start;
703 #endif /* VMS */
707 main (argc, argv)
708 int argc;
709 char *argv[];
711 int i;
712 unsigned int nincluded_files = 0;
713 char **included_files = xnew (argc, char *);
714 char *this_file;
715 argument *argbuffer;
716 int current_arg = 0, file_count = 0;
717 struct linebuffer filename_lb;
718 #ifdef VMS
719 logical got_err;
720 #endif
722 #ifdef DOS_NT
723 _fmode = O_BINARY; /* all of files are treated as binary files */
724 #endif /* DOS_NT */
726 progname = argv[0];
728 /* Allocate enough no matter what happens. Overkill, but each one
729 is small. */
730 argbuffer = xnew (argc, argument);
732 #ifdef ETAGS_REGEXPS
733 /* Set syntax for regular expression routines. */
734 re_set_syntax (RE_SYNTAX_EMACS);
735 #endif /* ETAGS_REGEXPS */
738 * If etags, always find typedefs and structure tags. Why not?
739 * Also default is to find macro constants and enum constants.
741 if (!CTAGS)
742 typedefs = typedefs_and_cplusplus = constantypedefs = TRUE;
744 while (1)
746 int opt = getopt_long (argc, argv,
747 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts, 0);
749 if (opt == EOF)
750 break;
752 switch (opt)
754 case 0:
755 /* If getopt returns 0, then it has already processed a
756 long-named option. We should do nothing. */
757 break;
759 case 1:
760 /* This means that a filename has been seen. Record it. */
761 argbuffer[current_arg].arg_type = at_filename;
762 argbuffer[current_arg].what = optarg;
763 ++current_arg;
764 ++file_count;
765 break;
767 /* Common options. */
768 case 'a':
769 append_to_tagfile = TRUE;
770 break;
771 case 'C':
772 cplusplus = TRUE;
773 break;
774 case 'd':
775 constantypedefs = TRUE;
776 break;
777 case 'D':
778 constantypedefs = FALSE;
779 break;
780 case 'f': /* for compatibility with old makefiles */
781 case 'o':
782 if (tagfile)
784 fprintf (stderr, "%s: -%c option may only be given once.\n",
785 progname, opt);
786 suggest_asking_for_help ();
788 tagfile = optarg;
789 break;
790 case 'I':
791 case 'S': /* for backward compatibility */
792 noindentypedefs = TRUE;
793 break;
794 case 'l':
795 argbuffer[current_arg].function = get_language_from_name (optarg);
796 argbuffer[current_arg].arg_type = at_language;
797 ++current_arg;
798 break;
799 #ifdef ETAGS_REGEXPS
800 case 'r':
801 argbuffer[current_arg].arg_type = at_regexp;
802 argbuffer[current_arg].what = optarg;
803 ++current_arg;
804 break;
805 case 'R':
806 argbuffer[current_arg].arg_type = at_regexp;
807 argbuffer[current_arg].what = NULL;
808 ++current_arg;
809 break;
810 #endif /* ETAGS_REGEXPS */
811 case 'V':
812 print_version ();
813 break;
814 case 'h':
815 case 'H':
816 print_help ();
817 break;
818 case 't':
819 typedefs = TRUE;
820 break;
821 case 'T':
822 typedefs = typedefs_and_cplusplus = TRUE;
823 break;
824 #if (!CTAGS)
825 /* Etags options */
826 case 'i':
827 included_files[nincluded_files++] = optarg;
828 break;
829 #else /* CTAGS */
830 /* Ctags options. */
831 case 'B':
832 searchar = '?';
833 break;
834 case 'u':
835 update = TRUE;
836 break;
837 case 'v':
838 vgrind_style = TRUE;
839 /*FALLTHRU*/
840 case 'x':
841 cxref_style = TRUE;
842 break;
843 case 'w':
844 no_warnings = TRUE;
845 break;
846 #endif /* CTAGS */
847 default:
848 suggest_asking_for_help ();
852 for (; optind < argc; ++optind)
854 argbuffer[current_arg].arg_type = at_filename;
855 argbuffer[current_arg].what = argv[optind];
856 ++current_arg;
857 ++file_count;
860 if (nincluded_files == 0 && file_count == 0)
862 fprintf (stderr, "%s: No input files specified.\n", progname);
863 suggest_asking_for_help ();
866 if (tagfile == NULL)
867 tagfile = CTAGS ? "tags" : "TAGS";
868 cwd = etags_getcwd (); /* the current working directory */
869 if (cwd[strlen (cwd) - 1] != '/')
870 cwd = concat (cwd, "/", "");
871 if (streq (tagfile, "-"))
872 tagfiledir = cwd;
873 else
874 tagfiledir = absolute_dirname (tagfile, cwd);
876 init (); /* set up boolean "functions" */
878 initbuffer (&lb);
879 initbuffer (&token_name);
880 initbuffer (&lbs[0].lb);
881 initbuffer (&lbs[1].lb);
882 initbuffer (&filename_lb);
884 if (!CTAGS)
886 if (streq (tagfile, "-"))
888 tagf = stdout;
889 #ifdef DOS_NT
890 /* Switch redirected `stdout' to binary mode (setting `_fmode'
891 doesn't take effect until after `stdout' is already open). */
892 if (!isatty (fileno (stdout)))
893 setmode (fileno (stdout), O_BINARY);
894 #endif /* DOS_NT */
896 else
897 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
898 if (tagf == NULL)
899 pfatal (tagfile);
903 * Loop through files finding functions.
905 for (i = 0; i < current_arg; ++i)
907 switch (argbuffer[i].arg_type)
909 case at_language:
910 lang_func = argbuffer[i].function;
911 break;
912 #ifdef ETAGS_REGEXPS
913 case at_regexp:
914 add_regex (argbuffer[i].what);
915 break;
916 #endif
917 case at_filename:
918 #ifdef VMS
919 while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL)
921 if (got_err)
923 error ("Can't find file %s\n", this_file);
924 argc--, argv++;
926 else
928 this_file = massage_name (this_file);
930 #else
931 this_file = argbuffer[i].what;
932 #endif
933 /* Input file named "-" means read file names from stdin
934 and use them. */
935 if (streq (this_file, "-"))
936 while (readline_internal (&filename_lb, stdin) > 0)
937 process_file (filename_lb.buffer);
938 else
939 process_file (this_file);
940 #ifdef VMS
942 #endif
943 break;
947 if (!CTAGS)
949 while (nincluded_files-- > 0)
950 fprintf (tagf, "\f\n%s,include\n", *included_files++);
952 fclose (tagf);
953 exit (GOOD);
956 /* If CTAGS, we are here. process_file did not write the tags yet,
957 because we want them ordered. Let's do it now. */
958 if (cxref_style)
960 put_entries (head);
961 exit (GOOD);
964 if (update)
966 char cmd[BUFSIZ];
967 for (i = 0; i < current_arg; ++i)
969 if (argbuffer[i].arg_type != at_filename)
970 continue;
971 sprintf (cmd,
972 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
973 tagfile, argbuffer[i].what, tagfile);
974 if (system (cmd) != GOOD)
975 fatal ("failed to execute shell command", (char *)NULL);
977 append_to_tagfile = TRUE;
980 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
981 if (tagf == NULL)
982 pfatal (tagfile);
983 put_entries (head);
984 fclose (tagf);
986 if (update)
988 char cmd[BUFSIZ];
989 sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
990 exit (system (cmd));
992 return GOOD;
997 * Return a Lang_function given the name.
999 Lang_function *
1000 get_language_from_name (name)
1001 char *name;
1003 struct lang_entry *lang;
1005 if (name != NULL)
1006 for (lang = lang_names; lang->name != NULL; lang++)
1008 if (streq (name, lang->name))
1009 return lang->function;
1012 fprintf (stderr, "%s: language \"%s\" not recognized.\n",
1013 progname, optarg);
1014 suggest_asking_for_help ();
1016 /* This point should never be reached. The function should either
1017 return a function pointer or never return. Note that a NULL
1018 pointer cannot be considered as an error, as it means that the
1019 language has not been explicitely imposed by the user ("auto"). */
1020 return NULL; /* avoid warnings from compiler */
1025 * Return a Lang_function given the interpreter name.
1027 Lang_function *
1028 get_language_from_interpreter (interpreter)
1029 char *interpreter;
1031 struct lang_entry *lang;
1032 char **iname;
1034 if (interpreter == NULL)
1035 return NULL;
1036 for (lang = lang_names; lang->name != NULL; lang++)
1037 if (lang->interpreters != NULL)
1038 for (iname = lang->interpreters; *iname != NULL; iname++)
1039 if (streq (*iname, interpreter))
1040 return lang->function;
1042 return NULL;
1048 * Return a Lang_function given the file suffix.
1050 Lang_function *
1051 get_language_from_suffix (suffix)
1052 char *suffix;
1054 struct lang_entry *lang;
1055 char **ext;
1057 if (suffix == NULL)
1058 return NULL;
1059 for (lang = lang_names; lang->name != NULL; lang++)
1060 if (lang->suffixes != NULL)
1061 for (ext = lang->suffixes; *ext != NULL; ext++)
1062 if (streq (*ext, suffix))
1063 return lang->function;
1065 return NULL;
1070 * This routine is called on each file argument.
1072 void
1073 process_file (file)
1074 char *file;
1076 struct stat stat_buf;
1077 FILE *inf;
1078 #ifdef DOS_NT
1079 char *p;
1081 for (p = file; *p != '\0'; p++)
1082 if (*p == '\\')
1083 *p = '/';
1084 #endif
1086 if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
1088 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
1089 return;
1091 if (streq (file, tagfile) && !streq (tagfile, "-"))
1093 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
1094 return;
1096 inf = fopen (file, "r");
1097 if (inf == NULL)
1099 perror (file);
1100 return;
1103 find_entries (file, inf);
1105 if (!CTAGS)
1107 char *filename;
1109 if (absolutefn (file))
1111 /* file is an absolute filename. Canonicalise it. */
1112 filename = absolute_filename (file, cwd);
1114 else
1116 /* file is a filename relative to cwd. Make it relative
1117 to the directory of the tags file. */
1118 filename = relative_filename (file, tagfiledir);
1120 fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
1121 free (filename);
1122 put_entries (head);
1123 free_tree (head);
1124 head = NULL;
1129 * This routine sets up the boolean pseudo-functions which work
1130 * by setting boolean flags dependent upon the corresponding character
1131 * Every char which is NOT in that string is not a white char. Therefore,
1132 * all of the array "_wht" is set to FALSE, and then the elements
1133 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1134 * of a char is TRUE if it is the string "white", else FALSE.
1136 void
1137 init ()
1139 register char *sp;
1140 register int i;
1142 for (i = 0; i < 0177; i++)
1143 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
1144 for (sp = white; *sp; sp++)
1145 _wht[*sp] = TRUE;
1146 for (sp = endtk; *sp; sp++)
1147 _etk[*sp] = TRUE;
1148 for (sp = intk; *sp; sp++)
1149 _itk[*sp] = TRUE;
1150 for (sp = begtk; *sp; sp++)
1151 _btk[*sp] = TRUE;
1152 _wht[0] = _wht['\n'];
1153 _etk[0] = _etk['\n'];
1154 _btk[0] = _btk['\n'];
1155 _itk[0] = _itk['\n'];
1159 * This routine opens the specified file and calls the function
1160 * which finds the function and type definitions.
1162 void
1163 find_entries (file, inf)
1164 char *file;
1165 FILE *inf;
1167 char *cp;
1168 Lang_function *function;
1169 NODE *old_last_node;
1170 extern NODE *last_node;
1173 /* Memory leakage here: the memory block pointed by curfile is never
1174 released. The amount of memory leaked here is the sum of the
1175 lengths of the input file names. */
1176 curfile = savestr (file);
1178 /* If user specified a language, use it. */
1179 function = lang_func;
1180 if (function != NULL)
1182 function (inf);
1183 fclose (inf);
1184 return;
1187 cp = etags_strrchr (file, '.');
1188 if (cp != NULL)
1190 cp += 1;
1191 function = get_language_from_suffix (cp);
1192 if (function != NULL)
1194 function (inf);
1195 fclose (inf);
1196 return;
1200 /* Look for sharp-bang as the first two characters. */
1201 if (readline_internal (&lb, inf) > 2
1202 && lb.buffer[0] == '#'
1203 && lb.buffer[1] == '!')
1205 char *lp;
1207 /* Set lp to point at the first char after the last slash in the
1208 line or, if no slashes, at the first nonblank. Then set cp to
1209 the first successive blank and terminate the string. */
1210 lp = etags_strrchr (lb.buffer+2, '/');
1211 if (lp != NULL)
1212 lp += 1;
1213 else
1214 for (lp = lb.buffer+2; *lp != '\0' && isspace (*lp); lp++)
1215 continue;
1216 for (cp = lp; *cp != '\0' && !isspace (*cp); cp++)
1217 continue;
1218 *cp = '\0';
1220 if (strlen (lp) > 0)
1222 function = get_language_from_interpreter (lp);
1223 if (function != NULL)
1225 function (inf);
1226 fclose (inf);
1227 return;
1231 rewind (inf);
1233 /* Try Fortran. */
1234 old_last_node = last_node;
1235 Fortran_functions (inf);
1237 /* No Fortran entries found. Try C. */
1238 if (old_last_node == last_node)
1240 rewind (inf);
1241 default_C_entries (inf);
1243 fclose (inf);
1244 return;
1247 /* Record a tag. */
1248 void
1249 pfnote (name, is_func, linestart, linelen, lno, cno)
1250 char *name; /* tag name, or NULL if unnamed */
1251 logical is_func; /* tag is a function */
1252 char *linestart; /* start of the line where tag is */
1253 int linelen; /* length of the line where tag is */
1254 int lno; /* line number */
1255 long cno; /* character number */
1257 register NODE *np;
1259 if (CTAGS && name == NULL)
1260 return;
1262 np = xnew (1, NODE);
1264 /* If ctags mode, change name "main" to M<thisfilename>. */
1265 if (CTAGS && !cxref_style && streq (name, "main"))
1267 register char *fp = etags_strrchr (curfile, '/');
1268 np->name = concat ("M", fp == 0 ? curfile : fp + 1, "");
1269 fp = etags_strrchr (np->name, '.');
1270 if (fp && fp[1] != '\0' && fp[2] == '\0')
1271 fp[0] = 0;
1273 else
1274 np->name = name;
1275 np->been_warned = FALSE;
1276 np->file = curfile;
1277 np->is_func = is_func;
1278 np->lno = lno;
1279 /* Our char numbers are 0-base, because of C language tradition?
1280 ctags compatibility? old versions compatibility? I don't know.
1281 Anyway, since emacs's are 1-base we expect etags.el to take care
1282 of the difference. If we wanted to have 1-based numbers, we would
1283 uncomment the +1 below. */
1284 np->cno = cno /* + 1 */ ;
1285 np->left = np->right = NULL;
1286 if (CTAGS && !cxref_style)
1288 if (strlen (linestart) < 50)
1289 np->pat = concat (linestart, "$", "");
1290 else
1291 np->pat = savenstr (linestart, 50);
1293 else
1294 np->pat = savenstr (linestart, linelen);
1296 add_node (np, &head);
1300 * free_tree ()
1301 * recurse on left children, iterate on right children.
1303 void
1304 free_tree (node)
1305 register NODE *node;
1307 while (node)
1309 register NODE *node_right = node->right;
1310 free_tree (node->left);
1311 if (node->name != NULL)
1312 free (node->name);
1313 free (node->pat);
1314 free ((char *) node);
1315 node = node_right;
1320 * add_node ()
1321 * Adds a node to the tree of nodes. In etags mode, we don't keep
1322 * it sorted; we just keep a linear list. In ctags mode, maintain
1323 * an ordered tree, with no attempt at balancing.
1325 * add_node is the only function allowed to add nodes, so it can
1326 * maintain state.
1328 NODE *last_node = NULL;
1329 void
1330 add_node (node, cur_node_p)
1331 NODE *node, **cur_node_p;
1333 register int dif;
1334 register NODE *cur_node = *cur_node_p;
1336 if (cur_node == NULL)
1338 *cur_node_p = node;
1339 last_node = node;
1340 return;
1343 if (!CTAGS)
1345 /* Etags Mode */
1346 if (last_node == NULL)
1347 fatal ("internal error in add_node", (char *)NULL);
1348 last_node->right = node;
1349 last_node = node;
1351 else
1353 /* Ctags Mode */
1354 dif = strcmp (node->name, cur_node->name);
1357 * If this tag name matches an existing one, then
1358 * do not add the node, but maybe print a warning.
1360 if (!dif)
1362 if (streq (node->file, cur_node->file))
1364 if (!no_warnings)
1366 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1367 node->file, lineno, node->name);
1368 fprintf (stderr, "Second entry ignored\n");
1371 else if (!cur_node->been_warned && !no_warnings)
1373 fprintf
1374 (stderr,
1375 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1376 node->file, cur_node->file, node->name);
1377 cur_node->been_warned = TRUE;
1379 return;
1382 /* Actually add the node */
1383 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1387 void
1388 put_entries (node)
1389 register NODE *node;
1391 register char *sp;
1393 if (node == NULL)
1394 return;
1396 /* Output subentries that precede this one */
1397 put_entries (node->left);
1399 /* Output this entry */
1401 if (!CTAGS)
1403 if (node->name != NULL)
1404 fprintf (tagf, "%s\177%s\001%d,%d\n",
1405 node->pat, node->name, node->lno, node->cno);
1406 else
1407 fprintf (tagf, "%s\177%d,%d\n",
1408 node->pat, node->lno, node->cno);
1410 else
1412 if (node->name == NULL)
1413 error ("internal error: NULL name in ctags mode.", (char *)NULL);
1415 if (cxref_style)
1417 if (vgrind_style)
1418 fprintf (stdout, "%s %s %d\n",
1419 node->name, node->file, (node->lno + 63) / 64);
1420 else
1421 fprintf (stdout, "%-16s %3d %-16s %s\n",
1422 node->name, node->lno, node->file, node->pat);
1424 else
1426 fprintf (tagf, "%s\t%s\t", node->name, node->file);
1428 if (node->is_func)
1429 { /* a function */
1430 putc (searchar, tagf);
1431 putc ('^', tagf);
1433 for (sp = node->pat; *sp; sp++)
1435 if (*sp == '\\' || *sp == searchar)
1436 putc ('\\', tagf);
1437 putc (*sp, tagf);
1439 putc (searchar, tagf);
1441 else
1442 { /* a typedef; text pattern inadequate */
1443 fprintf (tagf, "%d", node->lno);
1445 putc ('\n', tagf);
1449 /* Output subentries that follow this one */
1450 put_entries (node->right);
1453 /* Length of a number's decimal representation. */
1455 number_len (num)
1456 long num;
1458 int len = 0;
1459 if (!num)
1460 return 1;
1461 for (; num; num /= 10)
1462 ++len;
1463 return len;
1467 * Return total number of characters that put_entries will output for
1468 * the nodes in the subtree of the specified node. Works only if
1469 * we are not ctags, but called only in that case. This count
1470 * is irrelevant with the new tags.el, but is still supplied for
1471 * backward compatibility.
1474 total_size_of_entries (node)
1475 register NODE *node;
1477 register int total;
1479 if (node == NULL)
1480 return 0;
1482 total = 0;
1483 for (; node; node = node->right)
1485 /* Count left subentries. */
1486 total += total_size_of_entries (node->left);
1488 /* Count this entry */
1489 total += strlen (node->pat) + 1;
1490 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1491 if (node->name != NULL)
1492 total += 1 + strlen (node->name); /* \001name */
1495 return total;
1499 * The C symbol tables.
1501 enum sym_type
1503 st_none, st_C_objprot, st_C_objimpl, st_C_objend, st_C_gnumacro,
1504 st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
1507 /* Feed stuff between (but not including) %[ and %] lines to:
1508 gperf -c -k 1,3 -o -p -r -t
1510 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1512 @interface, 0, st_C_objprot
1513 @protocol, 0, st_C_objprot
1514 @implementation,0, st_C_objimpl
1515 @end, 0, st_C_objend
1516 class, C_PLPL, st_C_struct
1517 namespace, C_PLPL, st_C_struct
1518 domain, C_STAR, st_C_struct
1519 union, 0, st_C_struct
1520 struct, 0, st_C_struct
1521 enum, 0, st_C_enum
1522 typedef, 0, st_C_typedef
1523 define, 0, st_C_define
1524 bool, C_PLPL, st_C_typespec
1525 long, 0, st_C_typespec
1526 short, 0, st_C_typespec
1527 int, 0, st_C_typespec
1528 char, 0, st_C_typespec
1529 float, 0, st_C_typespec
1530 double, 0, st_C_typespec
1531 signed, 0, st_C_typespec
1532 unsigned, 0, st_C_typespec
1533 auto, 0, st_C_typespec
1534 void, 0, st_C_typespec
1535 extern, 0, st_C_typespec
1536 static, 0, st_C_typespec
1537 const, 0, st_C_typespec
1538 volatile, 0, st_C_typespec
1539 explicit, C_PLPL, st_C_typespec
1540 mutable, C_PLPL, st_C_typespec
1541 typename, C_PLPL, st_C_typespec
1542 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1543 DEFUN, 0, st_C_gnumacro
1544 SYSCALL, 0, st_C_gnumacro
1545 ENTRY, 0, st_C_gnumacro
1546 PSEUDO, 0, st_C_gnumacro
1547 # These are defined inside C functions, so currently they are not met.
1548 # EXFUN used in glibc, DEFVAR_* in emacs.
1549 #EXFUN, 0, st_C_gnumacro
1550 #DEFVAR_, 0, st_C_gnumacro
1552 and replace lines between %< and %> with its output. */
1553 /*%<*/
1554 /* C code produced by gperf version 2.1 (K&R C version) */
1555 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
1558 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
1560 #define MIN_WORD_LENGTH 3
1561 #define MAX_WORD_LENGTH 15
1562 #define MIN_HASH_VALUE 34
1563 #define MAX_HASH_VALUE 121
1565 34 keywords
1566 88 is the maximum key range
1569 static int
1570 hash (str, len)
1571 register char *str;
1572 register unsigned int len;
1574 static unsigned char hash_table[] =
1576 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1577 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1578 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1579 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1580 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1581 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1582 121, 121, 121, 121, 45, 121, 121, 121, 16, 19,
1583 61, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1584 10, 121, 121, 20, 53, 121, 121, 121, 121, 121,
1585 121, 121, 121, 121, 121, 121, 121, 41, 45, 22,
1586 60, 47, 37, 28, 121, 55, 121, 121, 20, 14,
1587 29, 30, 5, 121, 50, 59, 30, 54, 6, 121,
1588 121, 121, 121, 121, 121, 121, 121, 121,
1590 return len + hash_table[str[2]] + hash_table[str[0]];
1593 struct C_stab_entry *
1594 in_word_set (str, len)
1595 register char *str;
1596 register unsigned int len;
1599 static struct C_stab_entry wordlist[] =
1601 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1602 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1603 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1604 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1605 {"volatile", 0, st_C_typespec},
1606 {"PSEUDO", 0, st_C_gnumacro},
1607 {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1608 {"typedef", 0, st_C_typedef},
1609 {"typename", C_PLPL, st_C_typespec},
1610 {"",}, {"",}, {"",},
1611 {"SYSCALL", 0, st_C_gnumacro},
1612 {"",}, {"",}, {"",},
1613 {"mutable", C_PLPL, st_C_typespec},
1614 {"namespace", C_PLPL, st_C_struct},
1615 {"long", 0, st_C_typespec},
1616 {"",}, {"",},
1617 {"const", 0, st_C_typespec},
1618 {"",}, {"",}, {"",},
1619 {"explicit", C_PLPL, st_C_typespec},
1620 {"",}, {"",}, {"",}, {"",},
1621 {"void", 0, st_C_typespec},
1622 {"",},
1623 {"char", 0, st_C_typespec},
1624 {"class", C_PLPL, st_C_struct},
1625 {"",}, {"",}, {"",},
1626 {"float", 0, st_C_typespec},
1627 {"",},
1628 {"@implementation", 0, st_C_objimpl},
1629 {"auto", 0, st_C_typespec},
1630 {"",},
1631 {"ENTRY", 0, st_C_gnumacro},
1632 {"@end", 0, st_C_objend},
1633 {"bool", C_PLPL, st_C_typespec},
1634 {"domain", C_STAR, st_C_struct},
1635 {"",},
1636 {"DEFUN", 0, st_C_gnumacro},
1637 {"extern", 0, st_C_typespec},
1638 {"@interface", 0, st_C_objprot},
1639 {"",}, {"",}, {"",},
1640 {"int", 0, st_C_typespec},
1641 {"",}, {"",}, {"",}, {"",},
1642 {"signed", 0, st_C_typespec},
1643 {"short", 0, st_C_typespec},
1644 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1645 {"define", 0, st_C_define},
1646 {"@protocol", 0, st_C_objprot},
1647 {"enum", 0, st_C_enum},
1648 {"static", 0, st_C_typespec},
1649 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1650 {"union", 0, st_C_struct},
1651 {"struct", 0, st_C_struct},
1652 {"",}, {"",}, {"",}, {"",},
1653 {"double", 0, st_C_typespec},
1654 {"unsigned", 0, st_C_typespec},
1657 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
1659 register int key = hash (str, len);
1661 if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
1663 register char *s = wordlist[key].name;
1665 if (*s == *str && !strncmp (str + 1, s + 1, len - 1))
1666 return &wordlist[key];
1669 return 0;
1671 /*%>*/
1673 enum sym_type
1674 C_symtype (str, len, c_ext)
1675 char *str;
1676 int len;
1677 int c_ext;
1679 register struct C_stab_entry *se = in_word_set (str, len);
1681 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
1682 return st_none;
1683 return se->type;
1687 * C functions are recognized using a simple finite automaton.
1688 * funcdef is its state variable.
1690 enum
1692 fnone, /* nothing seen */
1693 ftagseen, /* function-like tag seen */
1694 fstartlist, /* just after open parenthesis */
1695 finlist, /* in parameter list */
1696 flistseen, /* after parameter list */
1697 fignore /* before open brace */
1698 } funcdef;
1702 * typedefs are recognized using a simple finite automaton.
1703 * typdef is its state variable.
1705 enum
1707 tnone, /* nothing seen */
1708 ttypedseen, /* typedef keyword seen */
1709 tinbody, /* inside typedef body */
1710 tend, /* just before typedef tag */
1711 tignore /* junk after typedef tag */
1712 } typdef;
1716 * struct-like structures (enum, struct and union) are recognized
1717 * using another simple finite automaton. `structdef' is its state
1718 * variable.
1720 enum
1722 snone, /* nothing seen yet */
1723 skeyseen, /* struct-like keyword seen */
1724 stagseen, /* struct-like tag seen */
1725 scolonseen, /* colon seen after struct-like tag */
1726 sinbody /* in struct body: recognize member func defs*/
1727 } structdef;
1730 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1731 * struct tag, and structtype is the type of the preceding struct-like
1732 * keyword.
1734 char *structtag = "<uninited>";
1735 enum sym_type structtype;
1738 * When objdef is different from onone, objtag is the name of the class.
1740 char *objtag = "<uninited>";
1743 * Yet another little state machine to deal with preprocessor lines.
1745 enum
1747 dnone, /* nothing seen */
1748 dsharpseen, /* '#' seen as first char on line */
1749 ddefineseen, /* '#' and 'define' seen */
1750 dignorerest /* ignore rest of line */
1751 } definedef;
1754 * State machine for Objective C protocols and implementations.
1756 enum
1758 onone, /* nothing seen */
1759 oprotocol, /* @interface or @protocol seen */
1760 oimplementation, /* @implementations seen */
1761 otagseen, /* class name seen */
1762 oparenseen, /* parenthesis before category seen */
1763 ocatseen, /* category name seen */
1764 oinbody, /* in @implementation body */
1765 omethodsign, /* in @implementation body, after +/- */
1766 omethodtag, /* after method name */
1767 omethodcolon, /* after method colon */
1768 omethodparm, /* after method parameter */
1769 oignore /* wait for @end */
1770 } objdef;
1773 * Set this to TRUE, and the next token considered is called a function.
1774 * Used only for GNU emacs's function-defining macros.
1776 logical next_token_is_func;
1779 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1781 logical yacc_rules;
1784 * methodlen is the length of the method name stored in token_name.
1786 int methodlen;
1789 * consider_token ()
1790 * checks to see if the current token is at the start of a
1791 * function, or corresponds to a typedef, or is a struct/union/enum
1792 * tag, or #define, or an enum constant.
1794 * *IS_FUNC gets TRUE iff the token is a function or #define macro
1795 * with args. C_EXT is which language we are looking at.
1797 * In the future we will need some way to adjust where the end of
1798 * the token is; for instance, implementing the C++ keyword
1799 * `operator' properly will adjust the end of the token to be after
1800 * whatever follows `operator'.
1802 * Globals
1803 * funcdef IN OUT
1804 * structdef IN OUT
1805 * definedef IN OUT
1806 * typdef IN OUT
1807 * objdef IN OUT
1808 * next_token_is_func IN OUT
1811 logical
1812 consider_token (str, len, c, c_ext, cblev, parlev, is_func)
1813 register char *str; /* IN: token pointer */
1814 register int len; /* IN: token length */
1815 register char c; /* IN: first char after the token */
1816 int c_ext; /* IN: C extensions mask */
1817 int cblev; /* IN: curly brace level */
1818 int parlev; /* IN: parenthesis level */
1819 logical *is_func; /* OUT: function found */
1821 enum sym_type toktype = C_symtype (str, len, c_ext);
1824 * Advance the definedef state machine.
1826 switch (definedef)
1828 case dnone:
1829 /* We're not on a preprocessor line. */
1830 break;
1831 case dsharpseen:
1832 if (toktype == st_C_define)
1834 definedef = ddefineseen;
1836 else
1838 definedef = dignorerest;
1840 return FALSE;
1841 case ddefineseen:
1843 * Make a tag for any macro, unless it is a constant
1844 * and constantypedefs is FALSE.
1846 definedef = dignorerest;
1847 *is_func = (c == '(');
1848 if (!*is_func && !constantypedefs)
1849 return FALSE;
1850 else
1851 return TRUE;
1852 case dignorerest:
1853 return FALSE;
1854 default:
1855 error ("internal error: definedef value.", (char *)NULL);
1859 * Now typedefs
1861 switch (typdef)
1863 case tnone:
1864 if (toktype == st_C_typedef)
1866 if (typedefs)
1867 typdef = ttypedseen;
1868 funcdef = fnone;
1869 return FALSE;
1871 break;
1872 case ttypedseen:
1873 switch (toktype)
1875 case st_none:
1876 case st_C_typespec:
1877 typdef = tend;
1878 break;
1879 case st_C_struct:
1880 case st_C_enum:
1881 break;
1883 /* Do not return here, so the structdef stuff has a chance. */
1884 break;
1885 case tend:
1886 switch (toktype)
1888 case st_C_typespec:
1889 case st_C_struct:
1890 case st_C_enum:
1891 return FALSE;
1893 return TRUE;
1897 * This structdef business is currently only invoked when cblev==0.
1898 * It should be recursively invoked whatever the curly brace level,
1899 * and a stack of states kept, to allow for definitions of structs
1900 * within structs.
1902 * This structdef business is NOT invoked when we are ctags and the
1903 * file is plain C. This is because a struct tag may have the same
1904 * name as another tag, and this loses with ctags.
1906 switch (toktype)
1908 case st_C_struct:
1909 case st_C_enum:
1910 if (typdef == ttypedseen
1911 || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
1913 structdef = skeyseen;
1914 structtype = toktype;
1916 return FALSE;
1919 if (structdef == skeyseen)
1921 /* Save the tag for struct/union/class, for functions that may be
1922 defined inside. */
1923 if (structtype == st_C_struct)
1924 structtag = savenstr (str, len);
1925 else
1926 structtag = "<enum>";
1927 structdef = stagseen;
1928 return TRUE;
1931 /* Avoid entering funcdef stuff if typdef is going on. */
1932 if (typdef != tnone)
1934 definedef = dnone;
1935 return FALSE;
1938 /* Detect GNU macros.
1940 DEFUN note for writers of emacs C code:
1941 The DEFUN macro, used in emacs C source code, has a first arg
1942 that is a string (the lisp function name), and a second arg that
1943 is a C function name. Since etags skips strings, the second arg
1944 is tagged. This is unfortunate, as it would be better to tag the
1945 first arg. The simplest way to deal with this problem would be
1946 to name the tag with a name built from the function name, by
1947 removing the initial 'F' character and substituting '-' for '_'.
1948 Anyway, this assumes that the conventions of naming lisp
1949 functions will never change. Currently, this method is not
1950 implemented, so writers of emacs code are recommended to put the
1951 first two args of a DEFUN on the same line. */
1952 if (definedef == dnone && toktype == st_C_gnumacro)
1954 next_token_is_func = TRUE;
1955 return FALSE;
1957 if (next_token_is_func)
1959 next_token_is_func = FALSE;
1960 funcdef = fignore;
1961 *is_func = TRUE;
1962 return TRUE;
1965 /* Detect Objective C constructs. */
1966 switch (objdef)
1968 case onone:
1969 switch (toktype)
1971 case st_C_objprot:
1972 objdef = oprotocol;
1973 return FALSE;
1974 case st_C_objimpl:
1975 objdef = oimplementation;
1976 return FALSE;
1978 break;
1979 case oimplementation:
1980 /* Save the class tag for functions that may be defined inside. */
1981 objtag = savenstr (str, len);
1982 objdef = oinbody;
1983 return FALSE;
1984 case oprotocol:
1985 /* Save the class tag for categories. */
1986 objtag = savenstr (str, len);
1987 objdef = otagseen;
1988 *is_func = TRUE;
1989 return TRUE;
1990 case oparenseen:
1991 objdef = ocatseen;
1992 *is_func = TRUE;
1993 return TRUE;
1994 case oinbody:
1995 break;
1996 case omethodsign:
1997 if (parlev == 0)
1999 objdef = omethodtag;
2000 methodlen = len;
2001 grow_linebuffer (&token_name, methodlen+1);
2002 strncpy (token_name.buffer, str, len);
2003 token_name.buffer[methodlen] = '\0';
2004 return TRUE;
2006 return FALSE;
2007 case omethodcolon:
2008 if (parlev == 0)
2009 objdef = omethodparm;
2010 return FALSE;
2011 case omethodparm:
2012 if (parlev == 0)
2014 objdef = omethodtag;
2015 methodlen += len;
2016 grow_linebuffer (&token_name, methodlen+1);
2017 strncat (token_name.buffer, str, len);
2018 return TRUE;
2020 return FALSE;
2021 case oignore:
2022 if (toktype == st_C_objend)
2024 /* Memory leakage here: the string pointed by objtag is
2025 never released, because many tests would be needed to
2026 avoid breaking on incorrect input code. The amount of
2027 memory leaked here is the sum of the lengths of the
2028 class tags.
2029 free (objtag); */
2030 objdef = onone;
2032 return FALSE;
2035 /* A function or enum constant? */
2036 switch (toktype)
2038 case st_C_typespec:
2039 if (funcdef != finlist && funcdef != fignore)
2040 funcdef = fnone; /* should be useless */
2041 return FALSE;
2042 case st_none:
2043 if (constantypedefs && structdef == sinbody && structtype == st_C_enum)
2044 return TRUE;
2045 if (funcdef == fnone)
2047 funcdef = ftagseen;
2048 *is_func = TRUE;
2049 return TRUE;
2053 return FALSE;
2057 * C_entries ()
2058 * This routine finds functions, typedefs, #define's, enum
2059 * constants and struct/union/enum definitions in C syntax
2060 * and adds them to the list.
2062 typedef struct
2064 logical valid;
2065 char *str;
2066 logical named;
2067 int linelen;
2068 int lineno;
2069 long linepos;
2070 char *buffer;
2071 } TOKEN;
2073 #define current_lb_is_new (newndx == curndx)
2074 #define switch_line_buffers() (curndx = 1 - curndx)
2076 #define curlb (lbs[curndx].lb)
2077 #define othlb (lbs[1-curndx].lb)
2078 #define newlb (lbs[newndx].lb)
2079 #define curlinepos (lbs[curndx].linepos)
2080 #define othlinepos (lbs[1-curndx].linepos)
2081 #define newlinepos (lbs[newndx].linepos)
2083 #define CNL_SAVE_DEFINEDEF \
2084 do { \
2085 curlinepos = charno; \
2086 lineno++; \
2087 linecharno = charno; \
2088 charno += readline (&curlb, inf); \
2089 lp = curlb.buffer; \
2090 quotednl = FALSE; \
2091 newndx = curndx; \
2092 } while (0)
2094 #define CNL \
2095 do { \
2096 CNL_SAVE_DEFINEDEF; \
2097 if (savetok.valid) \
2099 tok = savetok; \
2100 savetok.valid = FALSE; \
2102 definedef = dnone; \
2103 } while (0)
2106 void
2107 make_C_tag (isfun, tokp)
2108 logical isfun;
2109 TOKEN *tokp;
2111 char *name = NULL;
2113 /* This function should never be called when tok.valid is FALSE, but
2114 we must protect against invalid input or internal errors. */
2115 if (tokp->valid)
2117 if (CTAGS || tokp->named)
2118 name = savestr (token_name.buffer);
2119 pfnote (name, isfun,
2120 tokp->buffer, tokp->linelen, tokp->lineno, tokp->linepos);
2121 tokp->valid = FALSE;
2123 else if (DEBUG)
2124 abort ();
2128 void
2129 C_entries (c_ext, inf)
2130 int c_ext; /* extension of C */
2131 FILE *inf; /* input file */
2133 register char c; /* latest char read; '\0' for end of line */
2134 register char *lp; /* pointer one beyond the character `c' */
2135 int curndx, newndx; /* indices for current and new lb */
2136 TOKEN tok; /* latest token read */
2137 register int tokoff; /* offset in line of start of current token */
2138 register int toklen; /* length of current token */
2139 int cblev; /* current curly brace level */
2140 int parlev; /* current parenthesis level */
2141 logical incomm, inquote, inchar, quotednl, midtoken;
2142 logical cplpl;
2143 TOKEN savetok; /* token saved during preprocessor handling */
2146 curndx = newndx = 0;
2147 lineno = 0;
2148 charno = 0;
2149 lp = curlb.buffer;
2150 *lp = 0;
2152 funcdef = fnone; typdef = tnone; structdef = snone;
2153 definedef = dnone; objdef = onone;
2154 next_token_is_func = yacc_rules = FALSE;
2155 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2156 tok.valid = savetok.valid = FALSE;
2157 cblev = 0;
2158 parlev = 0;
2159 cplpl = c_ext & C_PLPL;
2161 while (!feof (inf))
2163 c = *lp++;
2164 if (c == '\\')
2166 /* If we're at the end of the line, the next character is a
2167 '\0'; don't skip it, because it's the thing that tells us
2168 to read the next line. */
2169 if (*lp == '\0')
2171 quotednl = TRUE;
2172 continue;
2174 lp++;
2175 c = ' ';
2177 else if (incomm)
2179 switch (c)
2181 case '*':
2182 if (*lp == '/')
2184 c = *lp++;
2185 incomm = FALSE;
2187 break;
2188 case '\0':
2189 /* Newlines inside comments do not end macro definitions in
2190 traditional cpp. */
2191 CNL_SAVE_DEFINEDEF;
2192 break;
2194 continue;
2196 else if (inquote)
2198 switch (c)
2200 case '"':
2201 inquote = FALSE;
2202 break;
2203 case '\0':
2204 /* Newlines inside strings do not end macro definitions
2205 in traditional cpp, even though compilers don't
2206 usually accept them. */
2207 CNL_SAVE_DEFINEDEF;
2208 break;
2210 continue;
2212 else if (inchar)
2214 switch (c)
2216 case '\0':
2217 /* Hmmm, something went wrong. */
2218 CNL;
2219 /* FALLTHRU */
2220 case '\'':
2221 inchar = FALSE;
2222 break;
2224 continue;
2226 else
2227 switch (c)
2229 case '"':
2230 inquote = TRUE;
2231 if (funcdef != finlist && funcdef != fignore)
2232 funcdef = fnone;
2233 continue;
2234 case '\'':
2235 inchar = TRUE;
2236 if (funcdef != finlist && funcdef != fignore)
2237 funcdef = fnone;
2238 continue;
2239 case '/':
2240 if (*lp == '*')
2242 lp++;
2243 incomm = TRUE;
2244 continue;
2246 else if (/* cplpl && */ *lp == '/')
2248 c = '\0';
2249 break;
2251 else
2252 break;
2253 case '%':
2254 if ((c_ext & YACC) && *lp == '%')
2256 /* entering or exiting rules section in yacc file */
2257 lp++;
2258 definedef = dnone; funcdef = fnone;
2259 typdef = tnone; structdef = snone;
2260 next_token_is_func = FALSE;
2261 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2262 cblev = 0;
2263 yacc_rules = !yacc_rules;
2264 continue;
2266 else
2267 break;
2268 case '#':
2269 if (definedef == dnone)
2271 char *cp;
2272 logical cpptoken = TRUE;
2274 /* Look back on this line. If all blanks, or nonblanks
2275 followed by an end of comment, this is a preprocessor
2276 token. */
2277 for (cp = newlb.buffer; cp < lp-1; cp++)
2278 if (!iswhite (*cp))
2280 if (*cp == '*' && *(cp+1) == '/')
2282 cp++;
2283 cpptoken = TRUE;
2285 else
2286 cpptoken = FALSE;
2288 if (cpptoken)
2289 definedef = dsharpseen;
2290 } /* if (definedef == dnone) */
2292 continue;
2293 } /* switch (c) */
2296 /* Consider token only if some complicated conditions are satisfied. */
2297 if ((definedef != dnone
2298 || (cblev == 0 && structdef != scolonseen)
2299 || (cblev == 1 && cplpl && structdef == sinbody)
2300 || (structdef == sinbody && structtype == st_C_enum))
2301 && typdef != tignore
2302 && definedef != dignorerest
2303 && funcdef != finlist)
2305 if (midtoken)
2307 if (endtoken (c))
2309 if (c == ':' && cplpl && *lp == ':' && begtoken(*(lp + 1)))
2312 * This handles :: in the middle, but not at the
2313 * beginning of an identifier.
2315 lp += 2;
2316 toklen += 3;
2318 else
2320 logical is_func = FALSE;
2322 if (yacc_rules
2323 || consider_token (newlb.buffer + tokoff, toklen, c,
2324 c_ext, cblev, parlev, &is_func))
2326 if (structdef == sinbody
2327 && definedef == dnone
2328 && is_func)
2329 /* function defined in C++ class body */
2331 grow_linebuffer (&token_name,
2332 strlen(structtag)+2+toklen+1);
2333 strcpy (token_name.buffer, structtag);
2334 strcat (token_name.buffer, "::");
2335 strncat (token_name.buffer,
2336 newlb.buffer+tokoff, toklen);
2337 tok.named = TRUE;
2339 else if (objdef == ocatseen)
2340 /* Objective C category */
2342 grow_linebuffer (&token_name,
2343 strlen(objtag)+2+toklen+1);
2344 strcpy (token_name.buffer, objtag);
2345 strcat (token_name.buffer, "(");
2346 strncat (token_name.buffer,
2347 newlb.buffer+tokoff, toklen);
2348 strcat (token_name.buffer, ")");
2349 tok.named = TRUE;
2351 else if (objdef == omethodtag
2352 || objdef == omethodparm)
2353 /* Objective C method */
2355 tok.named = TRUE;
2357 else
2359 grow_linebuffer (&token_name, toklen+1);
2360 strncpy (token_name.buffer,
2361 newlb.buffer+tokoff, toklen);
2362 token_name.buffer[toklen] = '\0';
2363 if (structdef == stagseen
2364 || typdef == tend
2365 || (is_func
2366 && definedef == dignorerest)) /* macro */
2367 tok.named = TRUE;
2368 else
2369 tok.named = FALSE;
2371 tok.lineno = lineno;
2372 tok.linelen = tokoff + toklen + 1;
2373 tok.buffer = newlb.buffer;
2374 tok.linepos = newlinepos;
2375 tok.valid = TRUE;
2377 if (definedef == dnone
2378 && (funcdef == ftagseen
2379 || structdef == stagseen
2380 || typdef == tend
2381 || objdef != onone))
2383 if (current_lb_is_new)
2384 switch_line_buffers ();
2386 else
2387 make_C_tag (is_func, &tok);
2389 midtoken = FALSE;
2391 } /* if (endtoken (c)) */
2392 else if (intoken (c))
2394 toklen++;
2395 continue;
2397 } /* if (midtoken) */
2398 else if (begtoken (c))
2400 switch (definedef)
2402 case dnone:
2403 switch (funcdef)
2405 case fstartlist:
2406 funcdef = finlist;
2407 continue;
2408 case flistseen:
2409 make_C_tag (TRUE, &tok);
2410 funcdef = fignore;
2411 break;
2412 case ftagseen:
2413 funcdef = fnone;
2414 break;
2416 if (structdef == stagseen)
2417 structdef = snone;
2418 break;
2419 case dsharpseen:
2420 savetok = tok;
2422 if (!yacc_rules || lp == newlb.buffer + 1)
2424 tokoff = lp - 1 - newlb.buffer;
2425 toklen = 1;
2426 midtoken = TRUE;
2428 continue;
2429 } /* if (begtoken) */
2430 } /* if must look at token */
2433 /* Detect end of line, colon, comma, semicolon and various braces
2434 after having handled a token.*/
2435 switch (c)
2437 case ':':
2438 if (definedef != dnone)
2439 break;
2440 switch (objdef)
2442 case otagseen:
2443 objdef = oignore;
2444 make_C_tag (TRUE, &tok);
2445 break;
2446 case omethodtag:
2447 case omethodparm:
2448 objdef = omethodcolon;
2449 methodlen += 1;
2450 grow_linebuffer (&token_name, methodlen+1);
2451 strcat (token_name.buffer, ":");
2452 break;
2454 if (structdef == stagseen)
2455 structdef = scolonseen;
2456 else
2457 switch (funcdef)
2459 case ftagseen:
2460 if (yacc_rules)
2462 make_C_tag (FALSE, &tok);
2463 funcdef = fignore;
2465 break;
2466 case fstartlist:
2467 funcdef = fnone;
2468 break;
2470 break;
2471 case ';':
2472 if (definedef != dnone)
2473 break;
2474 if (cblev == 0)
2475 switch (typdef)
2477 case tend:
2478 make_C_tag (FALSE, &tok);
2479 /* FALLTHRU */
2480 default:
2481 typdef = tnone;
2483 if (funcdef != fignore)
2485 funcdef = fnone;
2486 /* The following instruction invalidates the token.
2487 Probably the token should be invalidated in all
2488 other cases where some state machine is reset. */
2489 tok.valid = FALSE;
2491 if (structdef == stagseen)
2492 structdef = snone;
2493 break;
2494 case ',':
2495 if (definedef != dnone)
2496 break;
2497 switch (objdef)
2499 case omethodtag:
2500 case omethodparm:
2501 make_C_tag (TRUE, &tok);
2502 objdef = oinbody;
2503 break;
2505 if (funcdef != finlist && funcdef != fignore)
2506 funcdef = fnone;
2507 if (structdef == stagseen)
2508 structdef = snone;
2509 break;
2510 case '[':
2511 if (definedef != dnone)
2512 break;
2513 if (cblev == 0 && typdef == tend)
2515 typdef = tignore;
2516 make_C_tag (FALSE, &tok);
2517 break;
2519 if (funcdef != finlist && funcdef != fignore)
2520 funcdef = fnone;
2521 if (structdef == stagseen)
2522 structdef = snone;
2523 break;
2524 case '(':
2525 if (definedef != dnone)
2526 break;
2527 if (objdef == otagseen && parlev == 0)
2528 objdef = oparenseen;
2529 switch (funcdef)
2531 case fnone:
2532 switch (typdef)
2534 case ttypedseen:
2535 case tend:
2536 /* Make sure that the next char is not a '*'.
2537 This handles constructs like:
2538 typedef void OperatorFun (int fun); */
2539 if (tok.valid && *lp != '*')
2541 typdef = tignore;
2542 make_C_tag (FALSE, &tok);
2544 break;
2545 } /* switch (typdef) */
2546 break;
2547 case ftagseen:
2548 funcdef = fstartlist;
2549 break;
2550 case flistseen:
2551 funcdef = finlist;
2552 break;
2554 parlev++;
2555 break;
2556 case ')':
2557 if (definedef != dnone)
2558 break;
2559 if (objdef == ocatseen && parlev == 1)
2561 make_C_tag (TRUE, &tok);
2562 objdef = oignore;
2564 if (--parlev == 0)
2566 switch (funcdef)
2568 case fstartlist:
2569 case finlist:
2570 funcdef = flistseen;
2571 break;
2573 if (cblev == 0 && typdef == tend)
2575 typdef = tignore;
2576 make_C_tag (FALSE, &tok);
2579 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
2580 parlev = 0;
2581 break;
2582 case '{':
2583 if (definedef != dnone)
2584 break;
2585 if (typdef == ttypedseen)
2586 typdef = tinbody;
2587 switch (structdef)
2589 case skeyseen: /* unnamed struct */
2590 structdef = sinbody;
2591 structtag = "_anonymous_";
2592 break;
2593 case stagseen:
2594 case scolonseen: /* named struct */
2595 structdef = sinbody;
2596 make_C_tag (FALSE, &tok);
2597 break;
2599 switch (funcdef)
2601 case flistseen:
2602 make_C_tag (TRUE, &tok);
2603 /* FALLTHRU */
2604 case fignore:
2605 funcdef = fnone;
2606 break;
2607 case fnone:
2608 switch (objdef)
2610 case otagseen:
2611 make_C_tag (TRUE, &tok);
2612 objdef = oignore;
2613 break;
2614 case omethodtag:
2615 case omethodparm:
2616 make_C_tag (TRUE, &tok);
2617 objdef = oinbody;
2618 break;
2619 default:
2620 /* Neutralize `extern "C" {' grot. */
2621 if (cblev == 0 && structdef == snone && typdef == tnone)
2622 cblev = -1;
2625 cblev++;
2626 break;
2627 case '*':
2628 if (definedef != dnone)
2629 break;
2630 if (funcdef == fstartlist)
2631 funcdef = fnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
2632 break;
2633 case '}':
2634 if (definedef != dnone)
2635 break;
2636 if (!noindentypedefs && lp == newlb.buffer + 1)
2638 cblev = 0; /* reset curly brace level if first column */
2639 parlev = 0; /* also reset paren level, just in case... */
2641 else if (cblev > 0)
2642 cblev--;
2643 if (cblev == 0)
2645 if (typdef == tinbody)
2646 typdef = tend;
2647 /* Memory leakage here: the string pointed by structtag is
2648 never released, because I fear to miss something and
2649 break things while freeing the area. The amount of
2650 memory leaked here is the sum of the lengths of the
2651 struct tags.
2652 if (structdef == sinbody)
2653 free (structtag); */
2655 structdef = snone;
2656 structtag = "<error>";
2658 break;
2659 case '+':
2660 case '-':
2661 if (objdef == oinbody && cblev == 0)
2663 objdef = omethodsign;
2664 break;
2666 /* FALLTHRU */
2667 case '=': case '#': case '~': case '&': case '%': case '/':
2668 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2669 if (definedef != dnone)
2670 break;
2671 /* These surely cannot follow a function tag. */
2672 if (funcdef != finlist && funcdef != fignore)
2673 funcdef = fnone;
2674 break;
2675 case '\0':
2676 if (objdef == otagseen)
2678 make_C_tag (TRUE, &tok);
2679 objdef = oignore;
2681 /* If a macro spans multiple lines don't reset its state. */
2682 if (quotednl)
2683 CNL_SAVE_DEFINEDEF;
2684 else
2685 CNL;
2686 break;
2687 } /* switch (c) */
2689 } /* while not eof */
2693 * Process either a C++ file or a C file depending on the setting
2694 * of a global flag.
2696 void
2697 default_C_entries (inf)
2698 FILE *inf;
2700 C_entries (cplusplus ? C_PLPL : 0, inf);
2703 /* Always do plain ANSI C. */
2704 void
2705 plain_C_entries (inf)
2706 FILE *inf;
2708 C_entries (0, inf);
2711 /* Always do C++. */
2712 void
2713 Cplusplus_entries (inf)
2714 FILE *inf;
2716 C_entries (C_PLPL, inf);
2719 /* Always do C*. */
2720 void
2721 Cstar_entries (inf)
2722 FILE *inf;
2724 C_entries (C_STAR, inf);
2727 /* Always do Yacc. */
2728 void
2729 Yacc_entries (inf)
2730 FILE *inf;
2732 C_entries (YACC, inf);
2735 /* Fortran parsing */
2737 char *dbp;
2739 logical
2740 tail (cp)
2741 char *cp;
2743 register int len = 0;
2745 while (*cp && lowcase(*cp) == lowcase(dbp[len]))
2746 cp++, len++;
2747 if (*cp == '\0' && !intoken(dbp[len]))
2749 dbp += len;
2750 return TRUE;
2752 return FALSE;
2755 void
2756 takeprec ()
2758 while (isspace (*dbp))
2759 dbp++;
2760 if (*dbp != '*')
2761 return;
2762 dbp++;
2763 while (isspace (*dbp))
2764 dbp++;
2765 if (strneq (dbp, "(*)", 3))
2767 dbp += 3;
2768 return;
2770 if (!isdigit (*dbp))
2772 --dbp; /* force failure */
2773 return;
2776 dbp++;
2777 while (isdigit (*dbp));
2780 void
2781 getit (inf)
2782 FILE *inf;
2784 register char *cp;
2786 while (isspace (*dbp))
2787 dbp++;
2788 if (*dbp == '\0')
2790 lineno++;
2791 linecharno = charno;
2792 charno += readline (&lb, inf);
2793 dbp = lb.buffer;
2794 if (dbp[5] != '&')
2795 return;
2796 dbp += 6;
2797 while (isspace (*dbp))
2798 dbp++;
2800 if (!isalpha (*dbp)
2801 && *dbp != '_'
2802 && *dbp != '$')
2803 return;
2804 for (cp = dbp + 1;
2805 (*cp
2806 && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
2807 cp++)
2808 continue;
2809 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
2810 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2813 void
2814 Fortran_functions (inf)
2815 FILE *inf;
2817 lineno = 0;
2818 charno = 0;
2820 while (!feof (inf))
2822 lineno++;
2823 linecharno = charno;
2824 charno += readline (&lb, inf);
2825 dbp = lb.buffer;
2826 if (*dbp == '%')
2827 dbp++; /* Ratfor escape to fortran */
2828 while (isspace (*dbp))
2829 dbp++;
2830 if (*dbp == '\0')
2831 continue;
2832 switch (lowcase (*dbp))
2834 case 'i':
2835 if (tail ("integer"))
2836 takeprec ();
2837 break;
2838 case 'r':
2839 if (tail ("real"))
2840 takeprec ();
2841 break;
2842 case 'l':
2843 if (tail ("logical"))
2844 takeprec ();
2845 break;
2846 case 'c':
2847 if (tail ("complex") || tail ("character"))
2848 takeprec ();
2849 break;
2850 case 'd':
2851 if (tail ("double"))
2853 while (isspace (*dbp))
2854 dbp++;
2855 if (*dbp == '\0')
2856 continue;
2857 if (tail ("precision"))
2858 break;
2859 continue;
2861 break;
2863 while (isspace (*dbp))
2864 dbp++;
2865 if (*dbp == '\0')
2866 continue;
2867 switch (lowcase (*dbp))
2869 case 'f':
2870 if (tail ("function"))
2871 getit (inf);
2872 continue;
2873 case 's':
2874 if (tail ("subroutine"))
2875 getit (inf);
2876 continue;
2877 case 'e':
2878 if (tail ("entry"))
2879 getit (inf);
2880 continue;
2881 case 'p':
2882 if (tail ("program"))
2884 getit (inf);
2885 continue;
2887 if (tail ("procedure"))
2888 getit (inf);
2889 continue;
2895 * Bob Weiner, Motorola Inc., 4/3/94
2896 * Unix and microcontroller assembly tag handling
2897 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2899 void
2900 Asm_labels (inf)
2901 FILE *inf;
2903 register char *cp;
2905 lineno = 0;
2906 charno = 0;
2908 while (!feof (inf))
2910 lineno++;
2911 linecharno = charno;
2912 charno += readline (&lb, inf);
2913 cp = lb.buffer;
2915 /* If first char is alphabetic or one of [_.$], test for colon
2916 following identifier. */
2917 if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2919 /* Read past label. */
2920 cp++;
2921 while (isalnum (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2922 cp++;
2923 if (*cp == ':' || isspace (*cp))
2925 /* Found end of label, so copy it and add it to the table. */
2926 pfnote ((CTAGS) ? savenstr(lb.buffer, cp-lb.buffer) : NULL, TRUE,
2927 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2934 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2935 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2937 void
2938 Perl_functions (inf)
2939 FILE *inf;
2941 register char *cp;
2943 lineno = 0;
2944 charno = 0;
2946 while (!feof (inf))
2948 lineno++;
2949 linecharno = charno;
2950 charno += readline (&lb, inf);
2951 cp = lb.buffer;
2953 if (*cp++ == 's' && *cp++ == 'u' && *cp++ == 'b' && isspace(*cp++))
2955 while (*cp && isspace(*cp))
2956 cp++;
2957 while (*cp && ! isspace(*cp) && *cp != '{')
2958 cp++;
2959 pfnote ((CTAGS) ? savenstr (lb.buffer, cp-lb.buffer) : NULL, TRUE,
2960 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2965 /* Added by Mosur Mohan, 4/22/88 */
2966 /* Pascal parsing */
2969 * Locates tags for procedures & functions. Doesn't do any type- or
2970 * var-definitions. It does look for the keyword "extern" or
2971 * "forward" immediately following the procedure statement; if found,
2972 * the tag is skipped.
2974 void
2975 Pascal_functions (inf)
2976 FILE *inf;
2978 struct linebuffer tline; /* mostly copied from C_entries */
2979 long save_lcno;
2980 int save_lineno, save_len;
2981 char c, *cp, *namebuf;
2983 logical /* each of these flags is TRUE iff: */
2984 incomment, /* point is inside a comment */
2985 inquote, /* point is inside '..' string */
2986 get_tagname, /* point is after PROCEDURE/FUNCTION
2987 keyword, so next item = potential tag */
2988 found_tag, /* point is after a potential tag */
2989 inparms, /* point is within parameter-list */
2990 verify_tag; /* point has passed the parm-list, so the
2991 next token will determine whether this
2992 is a FORWARD/EXTERN to be ignored, or
2993 whether it is a real tag */
2995 lineno = 0;
2996 charno = 0;
2997 dbp = lb.buffer;
2998 *dbp = '\0';
2999 save_len = 0;
3000 initbuffer (&tline);
3002 incomment = inquote = FALSE;
3003 found_tag = FALSE; /* have a proc name; check if extern */
3004 get_tagname = FALSE; /* have found "procedure" keyword */
3005 inparms = FALSE; /* found '(' after "proc" */
3006 verify_tag = FALSE; /* check if "extern" is ahead */
3008 /* long main loop to get next char */
3009 while (!feof (inf))
3011 c = *dbp++;
3012 if (c == '\0') /* if end of line */
3014 lineno++;
3015 linecharno = charno;
3016 charno += readline (&lb, inf);
3017 dbp = lb.buffer;
3018 if (*dbp == '\0')
3019 continue;
3020 if (!((found_tag && verify_tag) ||
3021 get_tagname))
3022 c = *dbp++; /* only if don't need *dbp pointing
3023 to the beginning of the name of
3024 the procedure or function */
3026 if (incomment)
3028 if (c == '}') /* within { } comments */
3029 incomment = FALSE;
3030 else if (c == '*' && *dbp == ')') /* within (* *) comments */
3032 dbp++;
3033 incomment = FALSE;
3035 continue;
3037 else if (inquote)
3039 if (c == '\'')
3040 inquote = FALSE;
3041 continue;
3043 else
3044 switch (c)
3046 case '\'':
3047 inquote = TRUE; /* found first quote */
3048 continue;
3049 case '{': /* found open { comment */
3050 incomment = TRUE;
3051 continue;
3052 case '(':
3053 if (*dbp == '*') /* found open (* comment */
3055 incomment = TRUE;
3056 dbp++;
3058 else if (found_tag) /* found '(' after tag, i.e., parm-list */
3059 inparms = TRUE;
3060 continue;
3061 case ')': /* end of parms list */
3062 if (inparms)
3063 inparms = FALSE;
3064 continue;
3065 case ';':
3066 if (found_tag && !inparms) /* end of proc or fn stmt */
3068 verify_tag = TRUE;
3069 break;
3071 continue;
3073 if (found_tag && verify_tag && (*dbp != ' '))
3075 /* check if this is an "extern" declaration */
3076 if (*dbp == '\0')
3077 continue;
3078 if (lowcase (*dbp == 'e'))
3080 if (tail ("extern")) /* superfluous, really! */
3082 found_tag = FALSE;
3083 verify_tag = FALSE;
3086 else if (lowcase (*dbp) == 'f')
3088 if (tail ("forward")) /* check for forward reference */
3090 found_tag = FALSE;
3091 verify_tag = FALSE;
3094 if (found_tag && verify_tag) /* not external proc, so make tag */
3096 found_tag = FALSE;
3097 verify_tag = FALSE;
3098 pfnote (namebuf, TRUE,
3099 tline.buffer, save_len, save_lineno, save_lcno);
3100 continue;
3103 if (get_tagname) /* grab name of proc or fn */
3105 if (*dbp == '\0')
3106 continue;
3108 /* save all values for later tagging */
3109 grow_linebuffer (&tline, strlen (lb.buffer) + 1);
3110 strcpy (tline.buffer, lb.buffer);
3111 save_lineno = lineno;
3112 save_lcno = linecharno;
3114 /* grab block name */
3115 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
3116 continue;
3117 namebuf = (CTAGS) ? savenstr (dbp, cp-dbp) : NULL;
3118 dbp = cp; /* set dbp to e-o-token */
3119 save_len = dbp - lb.buffer + 1;
3120 get_tagname = FALSE;
3121 found_tag = TRUE;
3122 continue;
3124 /* and proceed to check for "extern" */
3126 else if (!incomment && !inquote && !found_tag)
3128 /* check for proc/fn keywords */
3129 switch (lowcase (c))
3131 case 'p':
3132 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3133 get_tagname = TRUE;
3134 continue;
3135 case 'f':
3136 if (tail ("unction"))
3137 get_tagname = TRUE;
3138 continue;
3141 } /* while not eof */
3143 free (tline.buffer);
3147 * lisp tag functions
3148 * look for (def or (DEF, quote or QUOTE
3151 L_isdef (strp)
3152 register char *strp;
3154 return ((strp[1] == 'd' || strp[1] == 'D')
3155 && (strp[2] == 'e' || strp[2] == 'E')
3156 && (strp[3] == 'f' || strp[3] == 'F'));
3160 L_isquote (strp)
3161 register char *strp;
3163 return ((*(++strp) == 'q' || *strp == 'Q')
3164 && (*(++strp) == 'u' || *strp == 'U')
3165 && (*(++strp) == 'o' || *strp == 'O')
3166 && (*(++strp) == 't' || *strp == 'T')
3167 && (*(++strp) == 'e' || *strp == 'E')
3168 && isspace(*(++strp)));
3171 void
3172 L_getit ()
3174 register char *cp;
3176 if (*dbp == '\'') /* Skip prefix quote */
3177 dbp++;
3178 else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
3180 dbp += 7;
3181 while (isspace(*dbp))
3182 dbp++;
3184 for (cp = dbp /*+1*/;
3185 *cp && *cp != '(' && *cp != ' ' && *cp != ')';
3186 cp++)
3187 continue;
3188 if (cp == dbp)
3189 return;
3191 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3192 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3195 void
3196 Lisp_functions (inf)
3197 FILE *inf;
3199 lineno = 0;
3200 charno = 0;
3202 while (!feof (inf))
3204 lineno++;
3205 linecharno = charno;
3206 charno += readline (&lb, inf);
3207 dbp = lb.buffer;
3208 if (dbp[0] == '(')
3210 if (L_isdef (dbp))
3212 while (!isspace (*dbp))
3213 dbp++;
3214 while (isspace (*dbp))
3215 dbp++;
3216 L_getit ();
3218 else
3220 /* Check for (foo::defmumble name-defined ... */
3222 dbp++;
3223 while (*dbp && !isspace (*dbp)
3224 && *dbp != ':' && *dbp != '(' && *dbp != ')');
3225 if (*dbp == ':')
3228 dbp++;
3229 while (*dbp == ':');
3231 if (L_isdef (dbp - 1))
3233 while (!isspace (*dbp))
3234 dbp++;
3235 while (isspace (*dbp))
3236 dbp++;
3237 L_getit ();
3246 * Scheme tag functions
3247 * look for (def... xyzzy
3248 * look for (def... (xyzzy
3249 * look for (def ... ((...(xyzzy ....
3250 * look for (set! xyzzy
3253 void get_scheme ();
3255 void
3256 Scheme_functions (inf)
3257 FILE *inf;
3259 lineno = 0;
3260 charno = 0;
3262 while (!feof (inf))
3264 lineno++;
3265 linecharno = charno;
3266 charno += readline (&lb, inf);
3267 dbp = lb.buffer;
3268 if (dbp[0] == '(' &&
3269 (dbp[1] == 'D' || dbp[1] == 'd') &&
3270 (dbp[2] == 'E' || dbp[2] == 'e') &&
3271 (dbp[3] == 'F' || dbp[3] == 'f'))
3273 while (!isspace (*dbp))
3274 dbp++;
3275 /* Skip over open parens and white space */
3276 while (*dbp && (isspace (*dbp) || *dbp == '('))
3277 dbp++;
3278 get_scheme ();
3280 if (dbp[0] == '(' &&
3281 (dbp[1] == 'S' || dbp[1] == 's') &&
3282 (dbp[2] == 'E' || dbp[2] == 'e') &&
3283 (dbp[3] == 'T' || dbp[3] == 't') &&
3284 (dbp[4] == '!' || dbp[4] == '!') &&
3285 (isspace (dbp[5])))
3287 while (!isspace (*dbp))
3288 dbp++;
3289 /* Skip over white space */
3290 while (isspace (*dbp))
3291 dbp++;
3292 get_scheme ();
3297 void
3298 get_scheme ()
3300 register char *cp;
3302 if (*dbp == '\0')
3303 return;
3304 /* Go till you get to white space or a syntactic break */
3305 for (cp = dbp + 1;
3306 *cp && *cp != '(' && *cp != ')' && !isspace (*cp);
3307 cp++)
3308 continue;
3309 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3310 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3313 /* Find tags in TeX and LaTeX input files. */
3315 /* TEX_toktab is a table of TeX control sequences that define tags.
3316 Each TEX_tabent records one such control sequence.
3317 CONVERT THIS TO USE THE Stab TYPE!! */
3318 struct TEX_tabent
3320 char *name;
3321 int len;
3324 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
3326 /* Default set of control sequences to put into TEX_toktab.
3327 The value of environment var TEXTAGS is prepended to this. */
3329 char *TEX_defenv = "\
3330 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3331 :part:appendix:entry:index";
3333 void TEX_mode ();
3334 struct TEX_tabent *TEX_decode_env ();
3335 int TEX_Token ();
3336 #if TeX_named_tokens
3337 void TEX_getit ();
3338 #endif
3340 char TEX_esc = '\\';
3341 char TEX_opgrp = '{';
3342 char TEX_clgrp = '}';
3345 * TeX/LaTeX scanning loop.
3347 void
3348 TeX_functions (inf)
3349 FILE *inf;
3351 char *lasthit;
3353 lineno = 0;
3354 charno = 0;
3356 /* Select either \ or ! as escape character. */
3357 TEX_mode (inf);
3359 /* Initialize token table once from environment. */
3360 if (!TEX_toktab)
3361 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
3363 while (!feof (inf))
3364 { /* Scan each line in file */
3365 lineno++;
3366 linecharno = charno;
3367 charno += readline (&lb, inf);
3368 dbp = lb.buffer;
3369 lasthit = dbp;
3370 while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
3372 register int i;
3374 if (!*(++dbp))
3375 break;
3376 linecharno += dbp - lasthit;
3377 lasthit = dbp;
3378 i = TEX_Token (lasthit);
3379 if (0 <= i)
3381 pfnote ((char *)NULL, TRUE,
3382 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3383 #if TeX_named_tokens
3384 TEX_getit (lasthit, TEX_toktab[i].len);
3385 #endif
3386 break; /* We only save a line once */
3392 #define TEX_LESC '\\'
3393 #define TEX_SESC '!'
3394 #define TEX_cmt '%'
3396 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3397 chars accordingly. */
3398 void
3399 TEX_mode (inf)
3400 FILE *inf;
3402 int c;
3404 while ((c = getc (inf)) != EOF)
3406 /* Skip to next line if we hit the TeX comment char. */
3407 if (c == TEX_cmt)
3408 while (c != '\n')
3409 c = getc (inf);
3410 else if (c == TEX_LESC || c == TEX_SESC )
3411 break;
3414 if (c == TEX_LESC)
3416 TEX_esc = TEX_LESC;
3417 TEX_opgrp = '{';
3418 TEX_clgrp = '}';
3420 else
3422 TEX_esc = TEX_SESC;
3423 TEX_opgrp = '<';
3424 TEX_clgrp = '>';
3426 rewind (inf);
3429 /* Read environment and prepend it to the default string.
3430 Build token table. */
3431 struct TEX_tabent *
3432 TEX_decode_env (evarname, defenv)
3433 char *evarname;
3434 char *defenv;
3436 register char *env, *p;
3438 struct TEX_tabent *tab;
3439 int size, i;
3441 /* Append default string to environment. */
3442 env = getenv (evarname);
3443 if (!env)
3444 env = defenv;
3445 else
3446 env = concat (env, defenv, "");
3448 /* Allocate a token table */
3449 for (size = 1, p = env; p;)
3450 if ((p = etags_strchr (p, ':')) && *(++p))
3451 size++;
3452 /* Add 1 to leave room for null terminator. */
3453 tab = xnew (size + 1, struct TEX_tabent);
3455 /* Unpack environment string into token table. Be careful about */
3456 /* zero-length strings (leading ':', "::" and trailing ':') */
3457 for (i = 0; *env;)
3459 p = etags_strchr (env, ':');
3460 if (!p) /* End of environment string. */
3461 p = env + strlen (env);
3462 if (p - env > 0)
3463 { /* Only non-zero strings. */
3464 tab[i].name = savenstr (env, p - env);
3465 tab[i].len = strlen (tab[i].name);
3466 i++;
3468 if (*p)
3469 env = p + 1;
3470 else
3472 tab[i].name = NULL; /* Mark end of table. */
3473 tab[i].len = 0;
3474 break;
3477 return tab;
3480 #if TeX_named_tokens
3481 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3482 The name being defined actually starts at (NAME + LEN + 1).
3483 But we seem to include the TeX command in the tag name. */
3484 void
3485 TEX_getit (name, len)
3486 char *name;
3487 int len;
3489 char *p = name + len;
3491 if (*name == '\0')
3492 return;
3494 /* Let tag name extend to next group close (or end of line) */
3495 while (*p && *p != TEX_clgrp)
3496 p++;
3497 pfnote (savenstr (name, p-name), TRUE,
3498 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3500 #endif
3502 /* If the text at CP matches one of the tag-defining TeX command names,
3503 return the pointer to the first occurrence of that command in TEX_toktab.
3504 Otherwise return -1.
3505 Keep the capital `T' in `Token' for dumb truncating compilers
3506 (this distinguishes it from `TEX_toktab' */
3508 TEX_Token (cp)
3509 char *cp;
3511 int i;
3513 for (i = 0; TEX_toktab[i].len > 0; i++)
3514 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
3515 return i;
3516 return -1;
3520 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
3522 * Assumes that the predicate starts at column 0.
3523 * Only the first clause of a predicate is added.
3525 void
3526 Prolog_functions (inf)
3527 FILE *inf;
3529 int prolog_pred ();
3530 void prolog_skip_comment ();
3532 char * last;
3533 int len;
3534 int allocated;
3536 allocated = 0;
3537 len = 0;
3538 last = NULL;
3540 lineno = 0;
3541 linecharno = 0;
3542 charno = 0;
3544 while (!feof (inf))
3546 lineno++;
3547 linecharno += charno;
3548 charno = readline (&lb, inf);
3549 dbp = lb.buffer;
3550 if (dbp[0] == '\0') /* Empty line */
3551 continue;
3552 else if (isspace (dbp[0])) /* Not a predicate */
3553 continue;
3554 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
3555 prolog_skip_comment (&lb, inf);
3556 else if (len = prolog_pred (dbp, last))
3558 /* Predicate. Store the function name so that we only
3559 generate a tag for the first clause. */
3560 if (last == NULL)
3561 last = xnew(len + 1, char);
3562 else if (len + 1 > allocated)
3563 last = (char *) xrealloc(last, len + 1);
3564 allocated = len + 1;
3565 strncpy (last, dbp, len);
3566 last[len] = '\0';
3572 void
3573 prolog_skip_comment (plb, inf)
3574 struct linebuffer *plb;
3575 FILE *inf;
3577 char *cp;
3581 for (cp = plb->buffer; *cp != '\0'; cp++)
3582 if (cp[0] == '*' && cp[1] == '/')
3583 return;
3584 lineno++;
3585 linecharno += readline (plb, inf);
3587 while (!feof(inf));
3591 * A predicate definition is added if it matches:
3592 * <beginning of line><Prolog Atom><whitespace>(
3594 * It is added to the tags database if it doesn't match the
3595 * name of the previous clause header.
3597 * Return the size of the name of the predicate, or 0 if no header
3598 * was found.
3601 prolog_pred (s, last)
3602 char *s;
3603 char *last; /* Name of last clause. */
3605 int prolog_atom();
3606 int prolog_white();
3608 int pos;
3609 int len;
3611 pos = prolog_atom(s, 0);
3612 if (pos < 1)
3613 return 0;
3615 len = pos;
3616 pos += prolog_white(s, pos);
3618 if ((s[pos] == '(') || (s[pos] == '.'))
3620 if (s[pos] == '(')
3621 pos++;
3623 /* Save only the first clause. */
3624 if ((last == NULL) ||
3625 (len != strlen(last)) ||
3626 (strncmp(s, last, len) != 0))
3628 pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
3629 s, pos, lineno, linecharno);
3630 return len;
3633 return 0;
3637 * Consume a Prolog atom.
3638 * Return the number of bytes consumed, or -1 if there was an error.
3640 * A prolog atom, in this context, could be one of:
3641 * - An alphanumeric sequence, starting with a lower case letter.
3642 * - A quoted arbitrary string. Single quotes can escape themselves.
3643 * Backslash quotes everything.
3646 prolog_atom (s, pos)
3647 char *s;
3648 int pos;
3650 int origpos;
3652 origpos = pos;
3654 if (islower(s[pos]) || (s[pos] == '_'))
3656 /* The atom is unquoted. */
3657 pos++;
3658 while (isalnum(s[pos]) || (s[pos] == '_'))
3660 pos++;
3662 return pos - origpos;
3664 else if (s[pos] == '\'')
3666 pos++;
3668 while (1)
3670 if (s[pos] == '\'')
3672 pos++;
3673 if (s[pos] != '\'')
3674 break;
3675 pos++; /* A double quote */
3677 else if (s[pos] == '\0')
3678 /* Multiline quoted atoms are ignored. */
3679 return -1;
3680 else if (s[pos] == '\\')
3682 if (s[pos+1] == '\0')
3683 return -1;
3684 pos += 2;
3686 else
3687 pos++;
3689 return pos - origpos;
3691 else
3692 return -1;
3695 /* Consume whitespace. Return the number of bytes eaten. */
3697 prolog_white (s, pos)
3698 char *s;
3699 int pos;
3701 int origpos;
3703 origpos = pos;
3705 while (isspace(s[pos]))
3706 pos++;
3708 return pos - origpos;
3712 * Support for Erlang -- Anders Lindgren, Feb 1996.
3714 * Generates tags for functions, defines, and records.
3716 * Assumes that Erlang functions start at column 0.
3718 void
3719 Erlang_functions (inf)
3720 FILE *inf;
3722 int erlang_func ();
3723 void erlang_attribute ();
3725 char * last;
3726 int len;
3727 int allocated;
3729 allocated = 0;
3730 len = 0;
3731 last = NULL;
3733 lineno = 0;
3734 linecharno = 0;
3735 charno = 0;
3737 while (!feof (inf))
3739 lineno++;
3740 linecharno += charno;
3741 charno = readline (&lb, inf);
3742 dbp = lb.buffer;
3743 if (dbp[0] == '\0') /* Empty line */
3744 continue;
3745 else if (isspace (dbp[0])) /* Not function nor attribute */
3746 continue;
3747 else if (dbp[0] == '%') /* comment */
3748 continue;
3749 else if (dbp[0] == '"') /* Sometimes, strings start in column one */
3750 continue;
3751 else if (dbp[0] == '-') /* attribute, e.g. "-define" */
3753 erlang_attribute(dbp);
3754 last = NULL;
3756 else if (len = erlang_func (dbp, last))
3759 * Function. Store the function name so that we only
3760 * generates a tag for the first clause.
3762 if (last == NULL)
3763 last = xnew(len + 1, char);
3764 else if (len + 1 > allocated)
3765 last = (char *) xrealloc(last, len + 1);
3766 allocated = len + 1;
3767 strncpy (last, dbp, len);
3768 last[len] = '\0';
3775 * A function definition is added if it matches:
3776 * <beginning of line><Erlang Atom><whitespace>(
3778 * It is added to the tags database if it doesn't match the
3779 * name of the previous clause header.
3781 * Return the size of the name of the function, or 0 if no function
3782 * was found.
3785 erlang_func (s, last)
3786 char *s;
3787 char *last; /* Name of last clause. */
3789 int erlang_atom ();
3790 int erlang_white ();
3792 int pos;
3793 int len;
3795 pos = erlang_atom(s, 0);
3796 if (pos < 1)
3797 return 0;
3799 len = pos;
3800 pos += erlang_white(s, pos);
3802 if (s[pos++] == '(')
3804 /* Save only the first clause. */
3805 if ((last == NULL) ||
3806 (len != strlen(last)) ||
3807 (strncmp(s, last, len) != 0))
3809 pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
3810 s, pos, lineno, linecharno);
3811 return len;
3814 return 0;
3819 * Handle attributes. Currently, tags are generated for defines
3820 * and records.
3822 * They are on the form:
3823 * -define(foo, bar).
3824 * -define(Foo(M, N), M+N).
3825 * -record(graph, {vtab = notable, cyclic = true}).
3827 void
3828 erlang_attribute (s)
3829 char *s;
3831 int erlang_atom ();
3832 int erlang_white ();
3834 int pos;
3835 int len;
3837 if ((strncmp(s, "-define", 7) == 0) ||
3838 (strncmp(s, "-record", 7) == 0))
3840 pos = 7;
3841 pos += erlang_white(s, pos);
3843 if (s[pos++] == '(')
3845 pos += erlang_white(s, pos);
3847 if (len = erlang_atom(s, pos))
3849 pfnote ((CTAGS) ? savenstr (& s[pos], len) : NULL, TRUE,
3850 s, pos + len, lineno, linecharno);
3854 return;
3859 * Consume an Erlang atom (or variable).
3860 * Return the number of bytes consumed, or -1 if there was an error.
3863 erlang_atom (s, pos)
3864 char *s;
3865 int pos;
3867 int origpos;
3869 origpos = pos;
3871 if (isalpha (s[pos]) || s[pos] == '_')
3873 /* The atom is unquoted. */
3874 pos++;
3875 while (isalnum (s[pos]) || s[pos] == '_')
3876 pos++;
3877 return pos - origpos;
3879 else if (s[pos] == '\'')
3881 pos++;
3883 while (1)
3885 if (s[pos] == '\'')
3887 pos++;
3888 break;
3890 else if (s[pos] == '\0')
3891 /* Multiline quoted atoms are ignored. */
3892 return -1;
3893 else if (s[pos] == '\\')
3895 if (s[pos+1] == '\0')
3896 return -1;
3897 pos += 2;
3899 else
3900 pos++;
3902 return pos - origpos;
3904 else
3905 return -1;
3908 /* Consume whitespace. Return the number of bytes eaten */
3910 erlang_white (s, pos)
3911 char *s;
3912 int pos;
3914 int origpos;
3916 origpos = pos;
3918 while (isspace (s[pos]))
3919 pos++;
3921 return pos - origpos;
3924 #ifdef ETAGS_REGEXPS
3925 /* Take a string like "/blah/" and turn it into "blah", making sure
3926 that the first and last characters are the same, and handling
3927 quoted separator characters. Actually, stops on the occurrence of
3928 an unquoted separator. Also turns "\t" into a Tab character.
3929 Returns pointer to terminating separator. Works in place. Null
3930 terminates name string. */
3931 char *
3932 scan_separators (name)
3933 char *name;
3935 char sep = name[0];
3936 char *copyto = name;
3937 logical quoted = FALSE;
3939 for (++name; *name != '\0'; ++name)
3941 if (quoted)
3943 if (*name == 't')
3944 *copyto++ = '\t';
3945 else if (*name == sep)
3946 *copyto++ = sep;
3947 else
3949 /* Something else is quoted, so preserve the quote. */
3950 *copyto++ = '\\';
3951 *copyto++ = *name;
3953 quoted = FALSE;
3955 else if (*name == '\\')
3956 quoted = TRUE;
3957 else if (*name == sep)
3958 break;
3959 else
3960 *copyto++ = *name;
3963 /* Terminate copied string. */
3964 *copyto = '\0';
3965 return name;
3968 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3969 expression, into a real regular expression by compiling it. */
3970 void
3971 add_regex (regexp_pattern)
3972 char *regexp_pattern;
3974 char *name;
3975 const char *err;
3976 struct re_pattern_buffer *patbuf, patbuf_init = { 0 };
3978 if (regexp_pattern == NULL)
3980 /* Remove existing regexps. */
3981 num_patterns = 0;
3982 patterns = NULL;
3983 return;
3986 if (regexp_pattern[0] == '\0')
3988 error ("missing regexp", (char *)NULL);
3989 return;
3991 if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
3993 error ("%s: unterminated regexp", regexp_pattern);
3994 return;
3996 name = scan_separators (regexp_pattern);
3997 if (regexp_pattern[0] == '\0')
3999 error ("null regexp", (char *)NULL);
4000 return;
4002 (void) scan_separators (name);
4004 patbuf = xnew (1, struct re_pattern_buffer);
4005 *patbuf = patbuf_init;
4007 err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
4008 if (err != NULL)
4010 error ("%s while compiling pattern", err);
4011 return;
4014 num_patterns += 1;
4015 if (num_patterns == 1)
4016 patterns = xnew (1, struct pattern);
4017 else
4018 patterns = ((struct pattern *)
4019 xrealloc (patterns,
4020 (num_patterns * sizeof (struct pattern))));
4021 patterns[num_patterns - 1].pattern = patbuf;
4022 patterns[num_patterns - 1].name_pattern = savestr (name);
4023 patterns[num_patterns - 1].error_signaled = FALSE;
4027 * Do the substitutions indicated by the regular expression and
4028 * arguments.
4030 char *
4031 substitute (in, out, regs)
4032 char *in, *out;
4033 struct re_registers *regs;
4035 char *result = NULL, *t;
4036 int size = 0;
4038 /* Pass 1: figure out how much size to allocate. */
4039 for (t = out; *t; ++t)
4041 if (*t == '\\')
4043 ++t;
4044 if (!*t)
4046 fprintf (stderr, "%s: pattern substitution ends prematurely\n",
4047 progname);
4048 return NULL;
4050 if (isdigit (*t))
4052 int dig = *t - '0';
4053 size += regs->end[dig] - regs->start[dig];
4058 /* Allocate space and do the substitutions. */
4059 result = xnew (size + 1, char);
4060 size = 0;
4061 for (; *out; ++out)
4063 if (*out == '\\')
4065 ++out;
4066 if (isdigit (*out))
4068 /* Using "dig2" satisfies my debugger. Bleah. */
4069 int dig2 = *out - '0';
4070 strncpy (result + size, in + regs->start[dig2],
4071 regs->end[dig2] - regs->start[dig2]);
4072 size += regs->end[dig2] - regs->start[dig2];
4074 else
4075 result[size++] = *out;
4077 else
4078 result[size++] = *out;
4080 result[size] = '\0';
4082 return result;
4085 #endif /* ETAGS_REGEXPS */
4086 /* Initialize a linebuffer for use */
4087 void
4088 initbuffer (linebuffer)
4089 struct linebuffer *linebuffer;
4091 linebuffer->size = 200;
4092 linebuffer->buffer = xnew (200, char);
4096 * Read a line of text from `stream' into `linebuffer'.
4097 * Return the number of characters read from `stream',
4098 * which is the length of the line including the newline, if any.
4100 long
4101 readline_internal (linebuffer, stream)
4102 struct linebuffer *linebuffer;
4103 register FILE *stream;
4105 char *buffer = linebuffer->buffer;
4106 register char *p = linebuffer->buffer;
4107 register char *pend;
4108 int chars_deleted;
4110 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
4112 while (1)
4114 register int c = getc (stream);
4115 if (p == pend)
4117 linebuffer->size *= 2;
4118 buffer = (char *) xrealloc (buffer, linebuffer->size);
4119 p += buffer - linebuffer->buffer;
4120 pend = buffer + linebuffer->size;
4121 linebuffer->buffer = buffer;
4123 if (c == EOF)
4125 *p = '\0';
4126 chars_deleted = 0;
4127 break;
4129 if (c == '\n')
4131 if (p > buffer && p[-1] == '\r')
4133 *--p = '\0';
4134 #ifdef DOS_NT
4135 /* Assume CRLF->LF translation will be performed by Emacs
4136 when loading this file, so CRs won't appear in the buffer.
4137 It would be cleaner to compensate within Emacs;
4138 however, Emacs does not know how many CRs were deleted
4139 before any given point in the file. */
4140 chars_deleted = 1;
4141 #else
4142 chars_deleted = 2;
4143 #endif
4145 else
4147 *p = '\0';
4148 chars_deleted = 1;
4150 break;
4152 *p++ = c;
4155 return p - buffer + chars_deleted;
4159 * Like readline_internal, above, but try to match the input
4160 * line against any existing regular expressions.
4162 long
4163 readline (linebuffer, stream)
4164 struct linebuffer *linebuffer;
4165 FILE *stream;
4167 /* Read new line. */
4168 long result = readline_internal (linebuffer, stream);
4169 #ifdef ETAGS_REGEXPS
4170 int i;
4172 /* Match against all listed patterns. */
4173 for (i = 0; i < num_patterns; ++i)
4175 int match = re_match (patterns[i].pattern, linebuffer->buffer,
4176 (int)result, 0, &patterns[i].regs);
4177 switch (match)
4179 case -2:
4180 /* Some error. */
4181 if (!patterns[i].error_signaled)
4183 error ("error while matching pattern %d", i);
4184 patterns[i].error_signaled = TRUE;
4186 break;
4187 case -1:
4188 /* No match. */
4189 break;
4190 default:
4191 /* Match occurred. Construct a tag. */
4192 if (patterns[i].name_pattern[0] != '\0')
4194 /* Make a named tag. */
4195 char *name = substitute (linebuffer->buffer,
4196 patterns[i].name_pattern,
4197 &patterns[i].regs);
4198 if (name != NULL)
4199 pfnote (name, TRUE,
4200 linebuffer->buffer, match, lineno, linecharno);
4202 else
4204 /* Make an unnamed tag. */
4205 pfnote ((char *)NULL, TRUE,
4206 linebuffer->buffer, match, lineno, linecharno);
4208 break;
4211 #endif /* ETAGS_REGEXPS */
4213 return result;
4217 * Read a file, but do no processing. This is used to do regexp
4218 * matching on files that have no language defined.
4220 void
4221 just_read_file (inf)
4222 FILE *inf;
4224 lineno = 0;
4225 charno = 0;
4227 while (!feof (inf))
4229 ++lineno;
4230 linecharno = charno;
4231 charno += readline (&lb, inf) + 1;
4237 * Return a pointer to a space of size strlen(cp)+1 allocated
4238 * with xnew where the string CP has been copied.
4240 char *
4241 savestr (cp)
4242 char *cp;
4244 return savenstr (cp, strlen (cp));
4248 * Return a pointer to a space of size LEN+1 allocated with xnew where
4249 * the string CP has been copied for at most the first LEN characters.
4251 char *
4252 savenstr (cp, len)
4253 char *cp;
4254 int len;
4256 register char *dp;
4258 dp = xnew (len + 1, char);
4259 strncpy (dp, cp, len);
4260 dp[len] = '\0';
4261 return dp;
4265 * Return the ptr in sp at which the character c last
4266 * appears; NULL if not found
4268 * Identical to System V strrchr, included for portability.
4270 char *
4271 etags_strrchr (sp, c)
4272 register char *sp, c;
4274 register char *r;
4276 r = NULL;
4279 if (*sp == c)
4280 r = sp;
4281 } while (*sp++);
4282 return r;
4287 * Return the ptr in sp at which the character c first
4288 * appears; NULL if not found
4290 * Identical to System V strchr, included for portability.
4292 char *
4293 etags_strchr (sp, c)
4294 register char *sp, c;
4298 if (*sp == c)
4299 return sp;
4300 } while (*sp++);
4301 return NULL;
4304 /* Print error message and exit. */
4305 void
4306 fatal (s1, s2)
4307 char *s1, *s2;
4309 error (s1, s2);
4310 exit (BAD);
4313 void
4314 pfatal (s1)
4315 char *s1;
4317 perror (s1);
4318 exit (BAD);
4321 void
4322 suggest_asking_for_help ()
4324 fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
4325 progname);
4326 exit (BAD);
4329 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
4330 void
4331 error (s1, s2)
4332 char *s1, *s2;
4334 fprintf (stderr, "%s: ", progname);
4335 fprintf (stderr, s1, s2);
4336 fprintf (stderr, "\n");
4339 /* Return a newly-allocated string whose contents
4340 concatenate those of s1, s2, s3. */
4341 char *
4342 concat (s1, s2, s3)
4343 char *s1, *s2, *s3;
4345 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
4346 char *result = xnew (len1 + len2 + len3 + 1, char);
4348 strcpy (result, s1);
4349 strcpy (result + len1, s2);
4350 strcpy (result + len1 + len2, s3);
4351 result[len1 + len2 + len3] = '\0';
4353 return result;
4356 /* Does the same work as the system V getcwd, but does not need to
4357 guess the buffer size in advance. */
4358 char *
4359 etags_getcwd ()
4361 #ifdef HAVE_GETCWD
4362 int bufsize = 200;
4363 char *path = xnew (bufsize, char);
4365 while (getcwd (path, bufsize) == NULL)
4367 if (errno != ERANGE)
4368 pfatal ("getcwd");
4369 bufsize *= 2;
4370 path = xnew (bufsize, char);
4373 #if WINDOWSNT
4375 /* Convert backslashes to slashes. */
4376 char *p;
4377 for (p = path; *p != '\0'; p++)
4378 if (*p == '\\')
4379 *p = '/';
4381 #endif
4383 return path;
4385 #else /* not HAVE_GETCWD */
4386 #ifdef MSDOS
4387 char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
4389 getwd (path);
4391 for (p = path; *p != '\0'; p++)
4392 if (*p == '\\')
4393 *p = '/';
4394 else
4395 *p = lowcase (*p);
4397 return strdup (path);
4398 #else /* not MSDOS */
4399 struct linebuffer path;
4400 FILE *pipe;
4402 initbuffer (&path);
4403 pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
4404 if (pipe == NULL || readline_internal (&path, pipe) == 0)
4405 pfatal ("pwd");
4406 pclose (pipe);
4408 return path.buffer;
4409 #endif /* not MSDOS */
4410 #endif /* not HAVE_GETCWD */
4413 /* Return a newly allocated string containing the filename
4414 of FILE relative to the absolute directory DIR (which
4415 should end with a slash). */
4416 char *
4417 relative_filename (file, dir)
4418 char *file, *dir;
4420 char *fp, *dp, *abs, *res;
4422 /* Find the common root of file and dir (with a trailing slash). */
4423 abs = absolute_filename (file, cwd);
4424 fp = abs;
4425 dp = dir;
4426 while (*fp++ == *dp++)
4427 continue;
4428 fp--, dp--; /* back to the first differing char */
4429 do /* look at the equal chars until / */
4430 fp--, dp--;
4431 while (*fp != '/');
4433 /* Build a sequence of "../" strings for the resulting relative filename. */
4434 for (dp = etags_strchr (dp + 1, '/'), res = "";
4435 dp != NULL;
4436 dp = etags_strchr (dp + 1, '/'))
4438 res = concat (res, "../", "");
4441 /* Add the filename relative to the common root of file and dir. */
4442 res = concat (res, fp + 1, "");
4443 free (abs);
4445 return res;
4448 /* Return a newly allocated string containing the
4449 absolute filename of FILE given CWD (which should
4450 end with a slash). */
4451 char *
4452 absolute_filename (file, cwd)
4453 char *file, *cwd;
4455 char *slashp, *cp, *res;
4457 if (absolutefn (file))
4458 res = concat (file, "", "");
4459 #ifdef DOS_NT
4460 /* We don't support non-absolute filenames with a drive
4461 letter, like `d:NAME' (it's too much hassle). */
4462 else if (file[1] == ':')
4463 fatal ("%s: relative filenames with drive letters not supported", file);
4464 #endif
4465 else
4466 res = concat (cwd, file, "");
4468 /* Delete the "/dirname/.." and "/." substrings. */
4469 slashp = etags_strchr (res, '/');
4470 while (slashp != NULL && slashp[0] != '\0')
4472 if (slashp[1] == '.')
4474 if (slashp[2] == '.'
4475 && (slashp[3] == '/' || slashp[3] == '\0'))
4477 cp = slashp;
4479 cp--;
4480 while (cp >= res && !absolutefn (cp));
4481 if (*cp == '/')
4483 strcpy (cp, slashp + 3);
4485 #ifdef DOS_NT
4486 /* Under MSDOS and NT we get `d:/NAME' as absolute
4487 filename, so the luser could say `d:/../NAME'.
4488 We silently treat this as `d:/NAME'. */
4489 else if (cp[1] == ':')
4490 strcpy (cp + 3, slashp + 4);
4491 #endif
4492 else /* else (cp == res) */
4494 if (slashp[3] != '\0')
4495 strcpy (cp, slashp + 4);
4496 else
4497 return ".";
4499 slashp = cp;
4500 continue;
4502 else if (slashp[2] == '/' || slashp[2] == '\0')
4504 strcpy (slashp, slashp + 2);
4505 continue;
4509 slashp = etags_strchr (slashp + 1, '/');
4512 return res;
4515 /* Return a newly allocated string containing the absolute
4516 filename of dir where FILE resides given CWD (which should
4517 end with a slash). */
4518 char *
4519 absolute_dirname (file, cwd)
4520 char *file, *cwd;
4522 char *slashp, *res;
4523 char save;
4524 #ifdef DOS_NT
4525 char *p;
4527 for (p = file; *p != '\0'; p++)
4528 if (*p == '\\')
4529 *p = '/';
4530 #endif
4532 slashp = etags_strrchr (file, '/');
4533 if (slashp == NULL)
4534 return cwd;
4535 save = slashp[1];
4536 slashp[1] = '\0';
4537 res = absolute_filename (file, cwd);
4538 slashp[1] = save;
4540 return res;
4543 /* Increase the size of a linebuffer. */
4544 void
4545 grow_linebuffer (bufp, toksize)
4546 struct linebuffer *bufp;
4547 int toksize;
4549 while (bufp->size < toksize)
4550 bufp->size *= 2;
4551 bufp->buffer = (char *) xrealloc (bufp->buffer, bufp->size);
4554 /* Like malloc but get fatal error if memory is exhausted. */
4555 long *
4556 xmalloc (size)
4557 unsigned int size;
4559 long *result = (long *) malloc (size);
4560 if (result == NULL)
4561 fatal ("virtual memory exhausted", (char *)NULL);
4562 return result;
4565 long *
4566 xrealloc (ptr, size)
4567 char *ptr;
4568 unsigned int size;
4570 long *result = (long *) realloc (ptr, size);
4571 if (result == NULL)
4572 fatal ("virtual memory exhausted", (char *)NULL);
4573 return result;