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. */
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";
46 # include <sys/param.h>
54 # define MAXPATHLEN _MAX_PATH
57 #if !defined (MSDOS) && !defined (WINDOWSNT) && defined (STDC_HEADERS)
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. */
75 #include <sys/types.h>
78 #if !defined (S_ISREG) && defined (S_IFREG)
79 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
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. */
98 /* Exit codes for success and failure. */
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 */
125 # define absolutefn(fn) (fn[0] == '/' \
126 || (fn[1] == ':' && fn[2] == '/'))
128 # define absolutefn(fn) (fn[0] == '/')
133 * xnew -- allocate storage
135 * SYNOPSIS: Type *xnew (int n, Type);
137 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
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 */
153 extern char *getenv ();
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 */
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 ();
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 ();
204 long readline_internal ();
210 void suggest_asking_for_help ();
211 void fatal (), pfatal ();
212 void find_entries ();
218 void process_file ();
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.
249 struct linebuffer lb
; /* the current line */
250 struct linebuffer token_name
; /* used by C_entries as a temporary area */
254 struct linebuffer lb
; /* used by C_entries instead of lb */
257 /* boolean "functions" (see init) */
258 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
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 */
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' },
311 /* Structure defining a regular expression. Elements are
312 the compiled pattern, and the name string. */
315 struct re_pattern_buffer
*pattern
;
316 struct re_registers regs
;
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 */
332 /* Non-NULL if language fixed. */
333 Lang_function
*lang_func
= NULL
;
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 */
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
[] =
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 */
381 char *Prolog_suffixes
[] =
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. */
401 Lang_function
*function
;
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 */
429 print_language_names ()
431 struct lang_entry
*lang
;
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
);
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.");
453 # define VERSION "19"
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");
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
);
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.");
476 puts ("-a, --append\n\
477 Append tag entries to existing tags file.");
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.");
485 Treat files whose name suffix defaults to C language as C++ files.");
488 puts ("-d, --defines\n\
489 Create tag entries for C #define constants and enum constants, too.");
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.");
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.");
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++.");
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\
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\
552 puts ("-V, --version\n\
553 Print the version of the program.\n\
555 Print this help message.");
557 print_language_names ();
560 puts ("Report bugs to bug-gnu-emacs@prep.ai.mit.edu");
573 /* This structure helps us allow mixing of --lang and filenames. */
576 enum argument_type arg_type
;
578 Lang_function
*function
;
581 #ifdef VMS /* VMS specific functions */
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
590 char body
[MAX_FILE_SPEC_LEN
+ 1];
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.
612 #define OUTSIZE MAX_FILE_SPEC_LEN
618 static long context
= 0;
619 static struct dsc$descriptor_s o
;
620 static struct dsc$descriptor_s i
;
621 static logical pass1
= TRUE
;
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
;
642 else if (status
== RMS$_NMF
)
646 strcpy(out
->body
, in
);
649 lib$
find_file_end(&context
);
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.
659 gfnames (arg
, p_error
)
663 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
665 switch (fn_exp (&filename
, arg
))
669 return filename
.body
;
675 return filename
.body
;
679 #ifndef OLD /* Newer versions of VMS do provide `system'. */
683 fprintf (stderr
, "system() function not implemented under VMS\n");
687 #define VERSION_DELIM ';'
688 char *massage_name (s
)
694 if (*s
== VERSION_DELIM
)
712 unsigned int nincluded_files
= 0;
713 char **included_files
= xnew (argc
, char *);
716 int current_arg
= 0, file_count
= 0;
717 struct linebuffer filename_lb
;
723 _fmode
= O_BINARY
; /* all of files are treated as binary files */
728 /* Allocate enough no matter what happens. Overkill, but each one
730 argbuffer
= xnew (argc
, argument
);
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.
742 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
746 int opt
= getopt_long (argc
, argv
,
747 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
755 /* If getopt returns 0, then it has already processed a
756 long-named option. We should do nothing. */
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
;
767 /* Common options. */
769 append_to_tagfile
= TRUE
;
775 constantypedefs
= TRUE
;
778 constantypedefs
= FALSE
;
780 case 'f': /* for compatibility with old makefiles */
784 fprintf (stderr
, "%s: -%c option may only be given once.\n",
786 suggest_asking_for_help ();
791 case 'S': /* for backward compatibility */
792 noindentypedefs
= TRUE
;
795 argbuffer
[current_arg
].function
= get_language_from_name (optarg
);
796 argbuffer
[current_arg
].arg_type
= at_language
;
801 argbuffer
[current_arg
].arg_type
= at_regexp
;
802 argbuffer
[current_arg
].what
= optarg
;
806 argbuffer
[current_arg
].arg_type
= at_regexp
;
807 argbuffer
[current_arg
].what
= NULL
;
810 #endif /* ETAGS_REGEXPS */
822 typedefs
= typedefs_and_cplusplus
= TRUE
;
827 included_files
[nincluded_files
++] = optarg
;
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
];
860 if (nincluded_files
== 0 && file_count
== 0)
862 fprintf (stderr
, "%s: No input files specified.\n", progname
);
863 suggest_asking_for_help ();
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
, "-"))
874 tagfiledir
= absolute_dirname (tagfile
, cwd
);
876 init (); /* set up boolean "functions" */
879 initbuffer (&token_name
);
880 initbuffer (&lbs
[0].lb
);
881 initbuffer (&lbs
[1].lb
);
882 initbuffer (&filename_lb
);
886 if (streq (tagfile
, "-"))
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
);
897 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
903 * Loop through files finding functions.
905 for (i
= 0; i
< current_arg
; ++i
)
907 switch (argbuffer
[i
].arg_type
)
910 lang_func
= argbuffer
[i
].function
;
914 add_regex (argbuffer
[i
].what
);
919 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
923 error ("Can't find file %s\n", this_file
);
928 this_file
= massage_name (this_file
);
931 this_file
= argbuffer
[i
].what
;
933 /* Input file named "-" means read file names from stdin
935 if (streq (this_file
, "-"))
936 while (readline_internal (&filename_lb
, stdin
) > 0)
937 process_file (filename_lb
.buffer
);
939 process_file (this_file
);
949 while (nincluded_files
-- > 0)
950 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
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. */
967 for (i
= 0; i
< current_arg
; ++i
)
969 if (argbuffer
[i
].arg_type
!= at_filename
)
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");
989 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
997 * Return a Lang_function given the name.
1000 get_language_from_name (name
)
1003 struct lang_entry
*lang
;
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",
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.
1028 get_language_from_interpreter (interpreter
)
1031 struct lang_entry
*lang
;
1034 if (interpreter
== 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
;
1048 * Return a Lang_function given the file suffix.
1051 get_language_from_suffix (suffix
)
1054 struct lang_entry
*lang
;
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
;
1070 * This routine is called on each file argument.
1076 struct stat stat_buf
;
1081 for (p
= file
; *p
!= '\0'; p
++)
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
);
1091 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1093 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1096 inf
= fopen (file
, "r");
1103 find_entries (file
, inf
);
1109 if (absolutefn (file
))
1111 /* file is an absolute filename. Canonicalise it. */
1112 filename
= absolute_filename (file
, cwd
);
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
));
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.
1142 for (i
= 0; i
< 0177; i
++)
1143 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1144 for (sp
= white
; *sp
; sp
++)
1146 for (sp
= endtk
; *sp
; sp
++)
1148 for (sp
= intk
; *sp
; sp
++)
1150 for (sp
= begtk
; *sp
; sp
++)
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.
1163 find_entries (file
, inf
)
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
)
1187 cp
= etags_strrchr (file
, '.');
1191 function
= get_language_from_suffix (cp
);
1192 if (function
!= NULL
)
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] == '!')
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, '/');
1214 for (lp
= lb
.buffer
+2; *lp
!= '\0' && isspace (*lp
); lp
++)
1216 for (cp
= lp
; *cp
!= '\0' && !isspace (*cp
); cp
++)
1220 if (strlen (lp
) > 0)
1222 function
= get_language_from_interpreter (lp
);
1223 if (function
!= NULL
)
1234 old_last_node
= last_node
;
1235 Fortran_functions (inf
);
1237 /* No Fortran entries found. Try C. */
1238 if (old_last_node
== last_node
)
1241 default_C_entries (inf
);
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 */
1259 if (CTAGS
&& name
== NULL
)
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')
1275 np
->been_warned
= FALSE
;
1277 np
->is_func
= is_func
;
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
, "$", "");
1291 np
->pat
= savenstr (linestart
, 50);
1294 np
->pat
= savenstr (linestart
, linelen
);
1296 add_node (np
, &head
);
1301 * recurse on left children, iterate on right children.
1305 register NODE
*node
;
1309 register NODE
*node_right
= node
->right
;
1310 free_tree (node
->left
);
1311 if (node
->name
!= NULL
)
1314 free ((char *) 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
1328 NODE
*last_node
= NULL
;
1330 add_node (node
, cur_node_p
)
1331 NODE
*node
, **cur_node_p
;
1334 register NODE
*cur_node
= *cur_node_p
;
1336 if (cur_node
== NULL
)
1346 if (last_node
== NULL
)
1347 fatal ("internal error in add_node", (char *)NULL
);
1348 last_node
->right
= node
;
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.
1362 if (streq (node
->file
, cur_node
->file
))
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
)
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
;
1382 /* Actually add the node */
1383 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1389 register NODE
*node
;
1396 /* Output subentries that precede this one */
1397 put_entries (node
->left
);
1399 /* Output this entry */
1403 if (node
->name
!= NULL
)
1404 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1405 node
->pat
, node
->name
, node
->lno
, node
->cno
);
1407 fprintf (tagf
, "%s\177%d,%d\n",
1408 node
->pat
, node
->lno
, node
->cno
);
1412 if (node
->name
== NULL
)
1413 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
1418 fprintf (stdout
, "%s %s %d\n",
1419 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1421 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1422 node
->name
, node
->lno
, node
->file
, node
->pat
);
1426 fprintf (tagf
, "%s\t%s\t", node
->name
, node
->file
);
1430 putc (searchar
, tagf
);
1433 for (sp
= node
->pat
; *sp
; sp
++)
1435 if (*sp
== '\\' || *sp
== searchar
)
1439 putc (searchar
, tagf
);
1442 { /* a typedef; text pattern inadequate */
1443 fprintf (tagf
, "%d", node
->lno
);
1449 /* Output subentries that follow this one */
1450 put_entries (node
->right
);
1453 /* Length of a number's decimal representation. */
1461 for (; num
; num
/= 10)
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
;
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 */
1499 * The C symbol tables.
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
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. */
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
1566 88 is the maximum key range
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
)
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
},
1617 {"const", 0, st_C_typespec
},
1618 {"",}, {"",}, {"",},
1619 {"explicit", C_PLPL
, st_C_typespec
},
1620 {"",}, {"",}, {"",}, {"",},
1621 {"void", 0, st_C_typespec
},
1623 {"char", 0, st_C_typespec
},
1624 {"class", C_PLPL
, st_C_struct
},
1625 {"",}, {"",}, {"",},
1626 {"float", 0, st_C_typespec
},
1628 {"@implementation", 0, st_C_objimpl
},
1629 {"auto", 0, st_C_typespec
},
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
},
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
];
1674 C_symtype (str
, len
, 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
)))
1687 * C functions are recognized using a simple finite automaton.
1688 * funcdef is its state variable.
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 */
1702 * typedefs are recognized using a simple finite automaton.
1703 * typdef is its state variable.
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 */
1716 * struct-like structures (enum, struct and union) are recognized
1717 * using another simple finite automaton. `structdef' is its state
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*/
1730 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1731 * struct tag, and structtype is the type of the preceding struct-like
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.
1747 dnone
, /* nothing seen */
1748 dsharpseen
, /* '#' seen as first char on line */
1749 ddefineseen
, /* '#' and 'define' seen */
1750 dignorerest
/* ignore rest of line */
1754 * State machine for Objective C protocols and implementations.
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 */
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).
1784 * methodlen is the length of the method name stored in token_name.
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'.
1808 * next_token_is_func IN OUT
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.
1829 /* We're not on a preprocessor line. */
1832 if (toktype
== st_C_define
)
1834 definedef
= ddefineseen
;
1838 definedef
= dignorerest
;
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
)
1855 error ("internal error: definedef value.", (char *)NULL
);
1864 if (toktype
== st_C_typedef
)
1867 typdef
= ttypedseen
;
1883 /* Do not return here, so the structdef stuff has a chance. */
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
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.
1910 if (typdef
== ttypedseen
1911 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1913 structdef
= skeyseen
;
1914 structtype
= toktype
;
1919 if (structdef
== skeyseen
)
1921 /* Save the tag for struct/union/class, for functions that may be
1923 if (structtype
== st_C_struct
)
1924 structtag
= savenstr (str
, len
);
1926 structtag
= "<enum>";
1927 structdef
= stagseen
;
1931 /* Avoid entering funcdef stuff if typdef is going on. */
1932 if (typdef
!= tnone
)
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
;
1957 if (next_token_is_func
)
1959 next_token_is_func
= FALSE
;
1965 /* Detect Objective C constructs. */
1975 objdef
= oimplementation
;
1979 case oimplementation
:
1980 /* Save the class tag for functions that may be defined inside. */
1981 objtag
= savenstr (str
, len
);
1985 /* Save the class tag for categories. */
1986 objtag
= savenstr (str
, len
);
1999 objdef
= omethodtag
;
2001 grow_linebuffer (&token_name
, methodlen
+1);
2002 strncpy (token_name
.buffer
, str
, len
);
2003 token_name
.buffer
[methodlen
] = '\0';
2009 objdef
= omethodparm
;
2014 objdef
= omethodtag
;
2016 grow_linebuffer (&token_name
, methodlen
+1);
2017 strncat (token_name
.buffer
, str
, len
);
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
2035 /* A function or enum constant? */
2039 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2040 funcdef
= fnone
; /* should be useless */
2043 if (constantypedefs
&& structdef
== sinbody
&& structtype
== st_C_enum
)
2045 if (funcdef
== fnone
)
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.
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 \
2085 curlinepos = charno; \
2087 linecharno = charno; \
2088 charno += readline (&curlb, inf); \
2089 lp = curlb.buffer; \
2096 CNL_SAVE_DEFINEDEF; \
2097 if (savetok.valid) \
2100 savetok.valid = FALSE; \
2102 definedef = dnone; \
2107 make_C_tag (isfun
, tokp
)
2113 /* This function should never be called when tok.valid is FALSE, but
2114 we must protect against invalid input or internal errors. */
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
;
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
;
2143 TOKEN savetok
; /* token saved during preprocessor handling */
2146 curndx
= newndx
= 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
;
2159 cplpl
= c_ext
& C_PLPL
;
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. */
2189 /* Newlines inside comments do not end macro definitions in
2204 /* Newlines inside strings do not end macro definitions
2205 in traditional cpp, even though compilers don't
2206 usually accept them. */
2217 /* Hmmm, something went wrong. */
2231 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2236 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2246 else if (/* cplpl && */ *lp
== '/')
2254 if ((c_ext
& YACC
) && *lp
== '%')
2256 /* entering or exiting rules section in yacc file */
2258 definedef
= dnone
; funcdef
= fnone
;
2259 typdef
= tnone
; structdef
= snone
;
2260 next_token_is_func
= FALSE
;
2261 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2263 yacc_rules
= !yacc_rules
;
2269 if (definedef
== dnone
)
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
2277 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2280 if (*cp
== '*' && *(cp
+1) == '/')
2289 definedef
= dsharpseen
;
2290 } /* if (definedef == dnone) */
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
)
2309 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken(*(lp
+ 1)))
2312 * This handles :: in the middle, but not at the
2313 * beginning of an identifier.
2320 logical is_func
= FALSE
;
2323 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2324 c_ext
, cblev
, parlev
, &is_func
))
2326 if (structdef
== sinbody
2327 && definedef
== dnone
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
);
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
, ")");
2351 else if (objdef
== omethodtag
2352 || objdef
== omethodparm
)
2353 /* Objective C method */
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
2366 && definedef
== dignorerest
)) /* macro */
2371 tok
.lineno
= lineno
;
2372 tok
.linelen
= tokoff
+ toklen
+ 1;
2373 tok
.buffer
= newlb
.buffer
;
2374 tok
.linepos
= newlinepos
;
2377 if (definedef
== dnone
2378 && (funcdef
== ftagseen
2379 || structdef
== stagseen
2381 || objdef
!= onone
))
2383 if (current_lb_is_new
)
2384 switch_line_buffers ();
2387 make_C_tag (is_func
, &tok
);
2391 } /* if (endtoken (c)) */
2392 else if (intoken (c
))
2397 } /* if (midtoken) */
2398 else if (begtoken (c
))
2409 make_C_tag (TRUE
, &tok
);
2416 if (structdef
== stagseen
)
2422 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2424 tokoff
= lp
- 1 - newlb
.buffer
;
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.*/
2438 if (definedef
!= dnone
)
2444 make_C_tag (TRUE
, &tok
);
2448 objdef
= omethodcolon
;
2450 grow_linebuffer (&token_name
, methodlen
+1);
2451 strcat (token_name
.buffer
, ":");
2454 if (structdef
== stagseen
)
2455 structdef
= scolonseen
;
2462 make_C_tag (FALSE
, &tok
);
2472 if (definedef
!= dnone
)
2478 make_C_tag (FALSE
, &tok
);
2483 if (funcdef
!= fignore
)
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. */
2491 if (structdef
== stagseen
)
2495 if (definedef
!= dnone
)
2501 make_C_tag (TRUE
, &tok
);
2505 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2507 if (structdef
== stagseen
)
2511 if (definedef
!= dnone
)
2513 if (cblev
== 0 && typdef
== tend
)
2516 make_C_tag (FALSE
, &tok
);
2519 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2521 if (structdef
== stagseen
)
2525 if (definedef
!= dnone
)
2527 if (objdef
== otagseen
&& parlev
== 0)
2528 objdef
= oparenseen
;
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
!= '*')
2542 make_C_tag (FALSE
, &tok
);
2545 } /* switch (typdef) */
2548 funcdef
= fstartlist
;
2557 if (definedef
!= dnone
)
2559 if (objdef
== ocatseen
&& parlev
== 1)
2561 make_C_tag (TRUE
, &tok
);
2570 funcdef
= flistseen
;
2573 if (cblev
== 0 && typdef
== tend
)
2576 make_C_tag (FALSE
, &tok
);
2579 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2583 if (definedef
!= dnone
)
2585 if (typdef
== ttypedseen
)
2589 case skeyseen
: /* unnamed struct */
2590 structdef
= sinbody
;
2591 structtag
= "_anonymous_";
2594 case scolonseen
: /* named struct */
2595 structdef
= sinbody
;
2596 make_C_tag (FALSE
, &tok
);
2602 make_C_tag (TRUE
, &tok
);
2611 make_C_tag (TRUE
, &tok
);
2616 make_C_tag (TRUE
, &tok
);
2620 /* Neutralize `extern "C" {' grot. */
2621 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2628 if (definedef
!= dnone
)
2630 if (funcdef
== fstartlist
)
2631 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2634 if (definedef
!= dnone
)
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... */
2645 if (typdef
== tinbody
)
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
2652 if (structdef == sinbody)
2653 free (structtag); */
2656 structtag
= "<error>";
2661 if (objdef
== oinbody
&& cblev
== 0)
2663 objdef
= omethodsign
;
2667 case '=': case '#': case '~': case '&': case '%': case '/':
2668 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2669 if (definedef
!= dnone
)
2671 /* These surely cannot follow a function tag. */
2672 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2676 if (objdef
== otagseen
)
2678 make_C_tag (TRUE
, &tok
);
2681 /* If a macro spans multiple lines don't reset its state. */
2689 } /* while not eof */
2693 * Process either a C++ file or a C file depending on the setting
2697 default_C_entries (inf
)
2700 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2703 /* Always do plain ANSI C. */
2705 plain_C_entries (inf
)
2711 /* Always do C++. */
2713 Cplusplus_entries (inf
)
2716 C_entries (C_PLPL
, inf
);
2724 C_entries (C_STAR
, inf
);
2727 /* Always do Yacc. */
2732 C_entries (YACC
, inf
);
2735 /* Fortran parsing */
2743 register int len
= 0;
2745 while (*cp
&& lowcase(*cp
) == lowcase(dbp
[len
]))
2747 if (*cp
== '\0' && !intoken(dbp
[len
]))
2758 while (isspace (*dbp
))
2763 while (isspace (*dbp
))
2765 if (strneq (dbp
, "(*)", 3))
2770 if (!isdigit (*dbp
))
2772 --dbp
; /* force failure */
2777 while (isdigit (*dbp
));
2786 while (isspace (*dbp
))
2791 linecharno
= charno
;
2792 charno
+= readline (&lb
, inf
);
2797 while (isspace (*dbp
))
2806 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2809 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
2810 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2814 Fortran_functions (inf
)
2823 linecharno
= charno
;
2824 charno
+= readline (&lb
, inf
);
2827 dbp
++; /* Ratfor escape to fortran */
2828 while (isspace (*dbp
))
2832 switch (lowcase (*dbp
))
2835 if (tail ("integer"))
2843 if (tail ("logical"))
2847 if (tail ("complex") || tail ("character"))
2851 if (tail ("double"))
2853 while (isspace (*dbp
))
2857 if (tail ("precision"))
2863 while (isspace (*dbp
))
2867 switch (lowcase (*dbp
))
2870 if (tail ("function"))
2874 if (tail ("subroutine"))
2882 if (tail ("program"))
2887 if (tail ("procedure"))
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]'
2911 linecharno
= charno
;
2912 charno
+= readline (&lb
, inf
);
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. */
2921 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *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{]+/
2938 Perl_functions (inf
)
2949 linecharno
= charno
;
2950 charno
+= readline (&lb
, inf
);
2953 if (*cp
++ == 's' && *cp
++ == 'u' && *cp
++ == 'b' && isspace(*cp
++))
2955 while (*cp
&& isspace(*cp
))
2957 while (*cp
&& ! isspace(*cp
) && *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.
2975 Pascal_functions (inf
)
2978 struct linebuffer tline
; /* mostly copied from C_entries */
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 */
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 */
3012 if (c
== '\0') /* if end of line */
3015 linecharno
= charno
;
3016 charno
+= readline (&lb
, inf
);
3020 if (!((found_tag
&& verify_tag
) ||
3022 c
= *dbp
++; /* only if don't need *dbp pointing
3023 to the beginning of the name of
3024 the procedure or function */
3028 if (c
== '}') /* within { } comments */
3030 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
3047 inquote
= TRUE
; /* found first quote */
3049 case '{': /* found open { comment */
3053 if (*dbp
== '*') /* found open (* comment */
3058 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
3061 case ')': /* end of parms list */
3066 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
3073 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
3075 /* check if this is an "extern" declaration */
3078 if (lowcase (*dbp
== 'e'))
3080 if (tail ("extern")) /* superfluous, really! */
3086 else if (lowcase (*dbp
) == 'f')
3088 if (tail ("forward")) /* check for forward reference */
3094 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
3098 pfnote (namebuf
, TRUE
,
3099 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
3103 if (get_tagname
) /* grab name of proc or fn */
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
++)
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
;
3124 /* and proceed to check for "extern" */
3126 else if (!incomment
&& !inquote
&& !found_tag
)
3128 /* check for proc/fn keywords */
3129 switch (lowcase (c
))
3132 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3136 if (tail ("unction"))
3141 } /* while not eof */
3143 free (tline
.buffer
);
3147 * lisp tag functions
3148 * look for (def or (DEF, quote or QUOTE
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'));
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
)));
3176 if (*dbp
== '\'') /* Skip prefix quote */
3178 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
3181 while (isspace(*dbp
))
3184 for (cp
= dbp
/*+1*/;
3185 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
3191 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3192 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3196 Lisp_functions (inf
)
3205 linecharno
= charno
;
3206 charno
+= readline (&lb
, inf
);
3212 while (!isspace (*dbp
))
3214 while (isspace (*dbp
))
3220 /* Check for (foo::defmumble name-defined ... */
3223 while (*dbp
&& !isspace (*dbp
)
3224 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
3229 while (*dbp
== ':');
3231 if (L_isdef (dbp
- 1))
3233 while (!isspace (*dbp
))
3235 while (isspace (*dbp
))
3246 * Scheme tag functions
3247 * look for (def... xyzzy
3248 * look for (def... (xyzzy
3249 * look for (def ... ((...(xyzzy ....
3250 * look for (set! xyzzy
3256 Scheme_functions (inf
)
3265 linecharno
= charno
;
3266 charno
+= readline (&lb
, inf
);
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
))
3275 /* Skip over open parens and white space */
3276 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
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] == '!') &&
3287 while (!isspace (*dbp
))
3289 /* Skip over white space */
3290 while (isspace (*dbp
))
3304 /* Go till you get to white space or a syntactic break */
3306 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
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!! */
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";
3334 struct TEX_tabent
*TEX_decode_env ();
3336 #if TeX_named_tokens
3340 char TEX_esc
= '\\';
3341 char TEX_opgrp
= '{';
3342 char TEX_clgrp
= '}';
3345 * TeX/LaTeX scanning loop.
3356 /* Select either \ or ! as escape character. */
3359 /* Initialize token table once from environment. */
3361 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
3364 { /* Scan each line in file */
3366 linecharno
= charno
;
3367 charno
+= readline (&lb
, inf
);
3370 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
3376 linecharno
+= dbp
- lasthit
;
3378 i
= TEX_Token (lasthit
);
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
);
3386 break; /* We only save a line once */
3392 #define TEX_LESC '\\'
3393 #define TEX_SESC '!'
3396 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3397 chars accordingly. */
3404 while ((c
= getc (inf
)) != EOF
)
3406 /* Skip to next line if we hit the TeX comment char. */
3410 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3429 /* Read environment and prepend it to the default string.
3430 Build token table. */
3432 TEX_decode_env (evarname
, defenv
)
3436 register char *env
, *p
;
3438 struct TEX_tabent
*tab
;
3441 /* Append default string to environment. */
3442 env
= getenv (evarname
);
3446 env
= concat (env
, defenv
, "");
3448 /* Allocate a token table */
3449 for (size
= 1, p
= env
; p
;)
3450 if ((p
= etags_strchr (p
, ':')) && *(++p
))
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 ':') */
3459 p
= etags_strchr (env
, ':');
3460 if (!p
) /* End of environment string. */
3461 p
= env
+ strlen (env
);
3463 { /* Only non-zero strings. */
3464 tab
[i
].name
= savenstr (env
, p
- env
);
3465 tab
[i
].len
= strlen (tab
[i
].name
);
3472 tab
[i
].name
= NULL
; /* Mark end of table. */
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. */
3485 TEX_getit (name
, len
)
3489 char *p
= name
+ len
;
3494 /* Let tag name extend to next group close (or end of line) */
3495 while (*p
&& *p
!= TEX_clgrp
)
3497 pfnote (savenstr (name
, p
-name
), TRUE
,
3498 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
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' */
3513 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3514 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
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.
3526 Prolog_functions (inf
)
3530 void prolog_skip_comment ();
3547 linecharno
+= charno
;
3548 charno
= readline (&lb
, inf
);
3550 if (dbp
[0] == '\0') /* Empty line */
3552 else if (isspace (dbp
[0])) /* Not a predicate */
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. */
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
);
3573 prolog_skip_comment (plb
, inf
)
3574 struct linebuffer
*plb
;
3581 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3582 if (cp
[0] == '*' && cp
[1] == '/')
3585 linecharno
+= readline (plb
, 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
3601 prolog_pred (s
, last
)
3603 char *last
; /* Name of last clause. */
3611 pos
= prolog_atom(s
, 0);
3616 pos
+= prolog_white(s
, pos
);
3618 if ((s
[pos
] == '(') || (s
[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
);
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
)
3654 if (islower(s
[pos
]) || (s
[pos
] == '_'))
3656 /* The atom is unquoted. */
3658 while (isalnum(s
[pos
]) || (s
[pos
] == '_'))
3662 return pos
- origpos
;
3664 else if (s
[pos
] == '\'')
3675 pos
++; /* A double quote */
3677 else if (s
[pos
] == '\0')
3678 /* Multiline quoted atoms are ignored. */
3680 else if (s
[pos
] == '\\')
3682 if (s
[pos
+1] == '\0')
3689 return pos
- origpos
;
3695 /* Consume whitespace. Return the number of bytes eaten. */
3697 prolog_white (s
, pos
)
3705 while (isspace(s
[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.
3719 Erlang_functions (inf
)
3723 void erlang_attribute ();
3740 linecharno
+= charno
;
3741 charno
= readline (&lb
, inf
);
3743 if (dbp
[0] == '\0') /* Empty line */
3745 else if (isspace (dbp
[0])) /* Not function nor attribute */
3747 else if (dbp
[0] == '%') /* comment */
3749 else if (dbp
[0] == '"') /* Sometimes, strings start in column one */
3751 else if (dbp
[0] == '-') /* attribute, e.g. "-define" */
3753 erlang_attribute(dbp
);
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.
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
);
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
3785 erlang_func (s
, last
)
3787 char *last
; /* Name of last clause. */
3790 int erlang_white ();
3795 pos
= erlang_atom(s
, 0);
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
);
3819 * Handle attributes. Currently, tags are generated for defines
3822 * They are on the form:
3823 * -define(foo, bar).
3824 * -define(Foo(M, N), M+N).
3825 * -record(graph, {vtab = notable, cyclic = true}).
3828 erlang_attribute (s
)
3832 int erlang_white ();
3837 if ((strncmp(s
, "-define", 7) == 0) ||
3838 (strncmp(s
, "-record", 7) == 0))
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
);
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
)
3871 if (isalpha (s
[pos
]) || s
[pos
] == '_')
3873 /* The atom is unquoted. */
3875 while (isalnum (s
[pos
]) || s
[pos
] == '_')
3877 return pos
- origpos
;
3879 else if (s
[pos
] == '\'')
3890 else if (s
[pos
] == '\0')
3891 /* Multiline quoted atoms are ignored. */
3893 else if (s
[pos
] == '\\')
3895 if (s
[pos
+1] == '\0')
3902 return pos
- origpos
;
3908 /* Consume whitespace. Return the number of bytes eaten */
3910 erlang_white (s
, pos
)
3918 while (isspace (s
[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. */
3932 scan_separators (name
)
3936 char *copyto
= name
;
3937 logical quoted
= FALSE
;
3939 for (++name
; *name
!= '\0'; ++name
)
3945 else if (*name
== sep
)
3949 /* Something else is quoted, so preserve the quote. */
3955 else if (*name
== '\\')
3957 else if (*name
== sep
)
3963 /* Terminate copied string. */
3968 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3969 expression, into a real regular expression by compiling it. */
3971 add_regex (regexp_pattern
)
3972 char *regexp_pattern
;
3976 struct re_pattern_buffer
*patbuf
, patbuf_init
= { 0 };
3978 if (regexp_pattern
== NULL
)
3980 /* Remove existing regexps. */
3986 if (regexp_pattern
[0] == '\0')
3988 error ("missing regexp", (char *)NULL
);
3991 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3993 error ("%s: unterminated regexp", regexp_pattern
);
3996 name
= scan_separators (regexp_pattern
);
3997 if (regexp_pattern
[0] == '\0')
3999 error ("null regexp", (char *)NULL
);
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
);
4010 error ("%s while compiling pattern", err
);
4015 if (num_patterns
== 1)
4016 patterns
= xnew (1, struct pattern
);
4018 patterns
= ((struct pattern
*)
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
4031 substitute (in
, out
, regs
)
4033 struct re_registers
*regs
;
4035 char *result
= NULL
, *t
;
4038 /* Pass 1: figure out how much size to allocate. */
4039 for (t
= out
; *t
; ++t
)
4046 fprintf (stderr
, "%s: pattern substitution ends prematurely\n",
4053 size
+= regs
->end
[dig
] - regs
->start
[dig
];
4058 /* Allocate space and do the substitutions. */
4059 result
= xnew (size
+ 1, char);
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
];
4075 result
[size
++] = *out
;
4078 result
[size
++] = *out
;
4080 result
[size
] = '\0';
4085 #endif /* ETAGS_REGEXPS */
4086 /* Initialize a linebuffer for use */
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.
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
;
4110 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
4114 register int c
= getc (stream
);
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
;
4131 if (p
> buffer
&& p
[-1] == '\r')
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. */
4155 return p
- buffer
+ chars_deleted
;
4159 * Like readline_internal, above, but try to match the input
4160 * line against any existing regular expressions.
4163 readline (linebuffer
, stream
)
4164 struct linebuffer
*linebuffer
;
4167 /* Read new line. */
4168 long result
= readline_internal (linebuffer
, stream
);
4169 #ifdef ETAGS_REGEXPS
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
);
4181 if (!patterns
[i
].error_signaled
)
4183 error ("error while matching pattern %d", i
);
4184 patterns
[i
].error_signaled
= TRUE
;
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
,
4200 linebuffer
->buffer
, match
, lineno
, linecharno
);
4204 /* Make an unnamed tag. */
4205 pfnote ((char *)NULL
, TRUE
,
4206 linebuffer
->buffer
, match
, lineno
, linecharno
);
4211 #endif /* ETAGS_REGEXPS */
4217 * Read a file, but do no processing. This is used to do regexp
4218 * matching on files that have no language defined.
4221 just_read_file (inf
)
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.
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.
4258 dp
= xnew (len
+ 1, char);
4259 strncpy (dp
, cp
, len
);
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.
4271 etags_strrchr (sp
, c
)
4272 register char *sp
, c
;
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.
4293 etags_strchr (sp
, c
)
4294 register char *sp
, c
;
4304 /* Print error message and exit. */
4322 suggest_asking_for_help ()
4324 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
4329 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
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. */
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';
4356 /* Does the same work as the system V getcwd, but does not need to
4357 guess the buffer size in advance. */
4363 char *path
= xnew (bufsize
, char);
4365 while (getcwd (path
, bufsize
) == NULL
)
4367 if (errno
!= ERANGE
)
4370 path
= xnew (bufsize
, char);
4375 /* Convert backslashes to slashes. */
4377 for (p
= path
; *p
!= '\0'; p
++)
4385 #else /* not HAVE_GETCWD */
4387 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
4391 for (p
= path
; *p
!= '\0'; p
++)
4397 return strdup (path
);
4398 #else /* not MSDOS */
4399 struct linebuffer path
;
4403 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
4404 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
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). */
4417 relative_filename (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
);
4426 while (*fp
++ == *dp
++)
4428 fp
--, dp
--; /* back to the first differing char */
4429 do /* look at the equal chars until / */
4433 /* Build a sequence of "../" strings for the resulting relative filename. */
4434 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
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, "");
4448 /* Return a newly allocated string containing the
4449 absolute filename of FILE given CWD (which should
4450 end with a slash). */
4452 absolute_filename (file
, cwd
)
4455 char *slashp
, *cp
, *res
;
4457 if (absolutefn (file
))
4458 res
= concat (file
, "", "");
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
);
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'))
4480 while (cp
>= res
&& !absolutefn (cp
));
4483 strcpy (cp
, slashp
+ 3);
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);
4492 else /* else (cp == res) */
4494 if (slashp
[3] != '\0')
4495 strcpy (cp
, slashp
+ 4);
4502 else if (slashp
[2] == '/' || slashp
[2] == '\0')
4504 strcpy (slashp
, slashp
+ 2);
4509 slashp
= etags_strchr (slashp
+ 1, '/');
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). */
4519 absolute_dirname (file
, cwd
)
4527 for (p
= file
; *p
!= '\0'; p
++)
4532 slashp
= etags_strrchr (file
, '/');
4537 res
= absolute_filename (file
, cwd
);
4543 /* Increase the size of a linebuffer. */
4545 grow_linebuffer (bufp
, toksize
)
4546 struct linebuffer
*bufp
;
4549 while (bufp
->size
< toksize
)
4551 bufp
->buffer
= (char *) xrealloc (bufp
->buffer
, bufp
->size
);
4554 /* Like malloc but get fatal error if memory is exhausted. */
4559 long *result
= (long *) malloc (size
);
4561 fatal ("virtual memory exhausted", (char *)NULL
);
4566 xrealloc (ptr
, size
)
4570 long *result
= (long *) realloc (ptr
, size
);
4572 fatal ("virtual memory exhausted", (char *)NULL
);