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.77";
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 Let 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)&&!(t)&&(abort(),1)) || !strcmp(s,t))
113 #define strneq(s,t,n) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strncmp(s,t,n))
115 #define lowcase(c) tolower ((char)c)
117 #define iswhite(arg) (_wht[arg]) /* T if char is white */
118 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
119 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
120 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
123 # define absolutefn(fn) (fn[0] == '/' \
124 || (fn[1] == ':' && fn[2] == '/'))
126 # define absolutefn(fn) (fn[0] == '/')
131 * xnew -- allocate storage
133 * SYNOPSIS: Type *xnew (int n, Type);
135 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
140 { /* sorting structure */
141 char *name
; /* function or type name */
142 char *file
; /* file name */
143 logical is_func
; /* use pattern or line no */
144 logical been_warned
; /* set if noticed dup */
145 int lno
; /* line number tag is on */
146 long cno
; /* character number line starts on */
147 char *pat
; /* search pattern */
148 struct nd_st
*left
, *right
; /* left and right sons */
151 extern char *getenv ();
154 char *savenstr (), *savestr ();
155 char *etags_strchr (), *etags_strrchr ();
156 char *etags_getcwd ();
157 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
158 void grow_linebuffer ();
159 long *xmalloc (), *xrealloc ();
161 typedef void Lang_function ();
162 #if FALSE /* many compilers barf on this */
163 Lang_function Asm_labels
;
164 Lang_function default_C_entries
;
165 Lang_function C_entries
;
166 Lang_function Cplusplus_entries
;
167 Lang_function Cstar_entries
;
168 Lang_function Erlang_functions
;
169 Lang_function Fortran_functions
;
170 Lang_function Yacc_entries
;
171 Lang_function Lisp_functions
;
172 Lang_function Pascal_functions
;
173 Lang_function Perl_functions
;
174 Lang_function Prolog_functions
;
175 Lang_function Scheme_functions
;
176 Lang_function TeX_functions
;
177 Lang_function just_read_file
;
178 #else /* so let's write it this way */
181 void default_C_entries ();
182 void plain_C_entries ();
183 void Cplusplus_entries ();
184 void Cstar_entries ();
185 void Erlang_functions ();
186 void Fortran_functions ();
187 void Yacc_entries ();
188 void Lisp_functions ();
189 void Pascal_functions ();
190 void Perl_functions ();
191 void Prolog_functions ();
192 void Scheme_functions ();
193 void TeX_functions ();
194 void just_read_file ();
197 Lang_function
*get_language_from_name ();
198 Lang_function
*get_language_from_interpreter ();
199 Lang_function
*get_language_from_suffix ();
200 int total_size_of_entries ();
202 long readline_internal ();
208 void suggest_asking_for_help ();
209 void fatal (), pfatal ();
210 void find_entries ();
216 void process_file ();
221 char searchar
= '/'; /* use /.../ searches */
223 int lineno
; /* line number of current line */
224 long charno
; /* current character number */
225 long linecharno
; /* charno of start of line */
227 char *curfile
; /* current input file name */
228 char *tagfile
; /* output file */
229 char *progname
; /* name this program was invoked with */
230 char *cwd
; /* current working directory */
231 char *tagfiledir
; /* directory of tagfile */
233 FILE *tagf
; /* ioptr for tags file */
234 NODE
*head
; /* the head of the binary tree of tags */
237 * A `struct linebuffer' is a structure which holds a line of text.
238 * `readline' reads a line from a stream into a linebuffer and works
239 * regardless of the length of the line.
247 struct linebuffer lb
; /* the current line */
248 struct linebuffer token_name
; /* used by C_entries as a temporary area */
252 struct linebuffer lb
; /* used by C_entries instead of lb */
255 /* boolean "functions" (see init) */
256 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
259 *white
= " \f\t\n\013",
260 /* token ending chars */
261 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
262 /* token starting chars */
263 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
264 /* valid in-token chars */
265 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
267 logical append_to_tagfile
; /* -a: append to tags */
268 /* The following three default to TRUE for etags, but to FALSE for ctags. */
269 logical typedefs
; /* -t: create tags for typedefs */
270 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
271 /* 0 struct/enum/union decls, and C++ */
272 /* member functions. */
273 logical constantypedefs
; /* -d: create tags for C #define and enum */
275 /* -D: opposite of -d. Default under ctags. */
276 logical update
; /* -u: update tags */
277 logical vgrind_style
; /* -v: create vgrind style index output */
278 logical no_warnings
; /* -w: suppress warnings */
279 logical cxref_style
; /* -x: create cxref style output */
280 logical cplusplus
; /* .[hc] means C++, not C */
281 logical noindentypedefs
; /* -I: ignore indentation in C */
283 struct option longopts
[] =
285 { "append", no_argument
, NULL
, 'a' },
286 { "backward-search", no_argument
, NULL
, 'B' },
287 { "c++", no_argument
, NULL
, 'C' },
288 { "cxref", no_argument
, NULL
, 'x' },
289 { "defines", no_argument
, NULL
, 'd' },
290 { "help", no_argument
, NULL
, 'h' },
291 { "help", no_argument
, NULL
, 'H' },
292 { "ignore-indentation", no_argument
, NULL
, 'I' },
293 { "include", required_argument
, NULL
, 'i' },
294 { "language", required_argument
, NULL
, 'l' },
295 { "no-defines", no_argument
, NULL
, 'D' },
296 { "no-regex", no_argument
, NULL
, 'R' },
297 { "no-warn", no_argument
, NULL
, 'w' },
298 { "output", required_argument
, NULL
, 'o' },
299 { "regex", required_argument
, NULL
, 'r' },
300 { "typedefs", no_argument
, NULL
, 't' },
301 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
302 { "update", no_argument
, NULL
, 'u' },
303 { "version", no_argument
, NULL
, 'V' },
304 { "vgrind", no_argument
, NULL
, 'v' },
309 /* Structure defining a regular expression. Elements are
310 the compiled pattern, and the name string. */
313 struct re_pattern_buffer
*pattern
;
314 struct re_registers regs
;
316 logical error_signaled
;
319 /* Number of regexps found. */
320 int num_patterns
= 0;
322 /* Array of all regexps. */
323 struct pattern
*patterns
= NULL
;
324 #endif /* ETAGS_REGEXPS */
330 /* Non-NULL if language fixed. */
331 Lang_function
*lang_func
= NULL
;
334 char *Asm_suffixes
[] = { "a", /* Unix assembler */
335 "asm", /* Microcontroller assembly */
336 "def", /* BSO/Tasking definition includes */
337 "inc", /* Microcontroller include files */
338 "ins", /* Microcontroller include files */
339 "s", "sa", /* Unix assembler */
340 "src", /* BSO/Tasking C compiler output */
344 /* Note that .c and .h can be considered C++, if the --c++ flag was
345 given. That is why default_C_entries is called here. */
346 char *default_C_suffixes
[] =
349 /* .M is for Objective C++ files. */
350 char *Cplusplus_suffixes
[] =
351 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL
};
353 char *Cstar_suffixes
[] =
354 { "cs", "hs", NULL
};
356 char *Erlang_suffixes
[] =
357 { "erl", "hrl", NULL
};
359 char *Fortran_suffixes
[] =
360 { "F", "f", "f90", "for", NULL
};
362 char *Lisp_suffixes
[] =
363 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL
};
365 char *Pascal_suffixes
[] =
366 { "p", "pas", NULL
};
368 char *Perl_suffixes
[] =
369 { "pl", "pm", NULL
};
370 char *Perl_interpreters
[] =
371 { "perl", "@PERL@", NULL
};
373 char *plain_C_suffixes
[] =
374 { "pc", /* Pro*C file */
375 "m", /* Objective C file */
376 "lm", /* Objective lex file */
379 char *Prolog_suffixes
[] =
382 /* Can't do the `SCM' or `scm' prefix with a version number. */
383 char *Scheme_suffixes
[] =
384 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL
};
386 char *TeX_suffixes
[] =
387 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL
};
389 char *Yacc_suffixes
[] =
390 { "y", "ym", NULL
}; /* .ym is Objective yacc file */
392 /* Table of language names and corresponding functions, file suffixes
393 and interpreter names.
394 It is ok for a given function to be listed under more than one
395 name. I just didn't. */
399 Lang_function
*function
;
404 struct lang_entry lang_names
[] =
406 { "asm", Asm_labels
, Asm_suffixes
, NULL
},
407 { "c", default_C_entries
, default_C_suffixes
, NULL
},
408 { "c++", Cplusplus_entries
, Cplusplus_suffixes
, NULL
},
409 { "c*", Cstar_entries
, Cstar_suffixes
, NULL
},
410 { "erlang", Erlang_functions
, Erlang_suffixes
, NULL
},
411 { "fortran", Fortran_functions
, Fortran_suffixes
, NULL
},
412 { "lisp", Lisp_functions
, Lisp_suffixes
, NULL
},
413 { "pascal", Pascal_functions
, Pascal_suffixes
, NULL
},
414 { "perl", Perl_functions
, Perl_suffixes
, Perl_interpreters
},
415 { "proc", plain_C_entries
, plain_C_suffixes
, NULL
},
416 { "prolog", Prolog_functions
, Prolog_suffixes
, NULL
},
417 { "scheme", Scheme_functions
, Scheme_suffixes
, NULL
},
418 { "tex", TeX_functions
, TeX_suffixes
, NULL
},
419 { "yacc", Yacc_entries
, Yacc_suffixes
, NULL
},
420 { "auto", NULL
}, /* default guessing scheme */
421 { "none", just_read_file
}, /* regexp matching only */
422 { NULL
, NULL
} /* end of list */
427 print_language_names ()
429 struct lang_entry
*lang
;
432 puts ("\nThese are the currently supported languages, along with the\n\
433 default file name suffixes:");
434 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
436 printf ("\t%s\t", lang
->name
);
437 if (lang
->suffixes
!= NULL
)
438 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
439 printf (" .%s", *ext
);
442 puts ("Where `auto' means use default language for files based on file\n\
443 name suffix, and `none' means only do regexp processing on files.\n\
444 If no language is specified and no matching suffix is found,\n\
445 the first line of the file is read for a sharp-bang (#!) sequence\n\
446 followed by the name of an interpreter. If no such sequence is found,\n\
447 Fortran is tried first; if no tags are found, C is tried next.");
451 # define VERSION "19"
456 printf ("%s (GNU Emacs %s)\n", (CTAGS
) ? "ctags" : "etags", VERSION
);
457 puts ("Copyright (C) 1996 Free Software Foundation, Inc. and Ken Arnold");
458 puts ("This program is distributed under the same terms as Emacs");
466 printf ("These are the options accepted by %s. You may use unambiguous\n\
467 abbreviations for the long option names. A - as file name means read\n\
468 names from stdin.", progname
);
470 printf (" Absolute names are stored in the output file as they\n\
471 are. Relative ones are stored relative to the output file's directory.");
474 puts ("-a, --append\n\
475 Append tag entries to existing tags file.");
478 puts ("-B, --backward-search\n\
479 Write the search commands for the tag entries using '?', the\n\
480 backward-search command instead of '/', the forward-search command.");
483 Treat files whose name suffix defaults to C language as C++ files.");
486 puts ("-d, --defines\n\
487 Create tag entries for C #define constants and enum constants, too.");
489 puts ("-D, --no-defines\n\
490 Don't create tag entries for C #define constants and enum constants.\n\
491 This makes the tags file smaller.");
495 puts ("-i FILE, --include=FILE\n\
496 Include a note in tag file indicating that, when searching for\n\
497 a tag, one should also consult the tags file FILE after\n\
498 checking the current file.");
499 puts ("-l LANG, --language=LANG\n\
500 Force the following files to be considered as written in the\n\
501 named language up to the next --language=LANG option.");
505 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
506 Make a tag for each line matching pattern REGEXP in the\n\
507 following files. REGEXP is anchored (as if preceded by ^).\n\
508 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
509 named tags can be created with:\n\
510 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
511 puts ("-R, --no-regex\n\
512 Don't create tags from regexps for the following files.");
513 #endif /* ETAGS_REGEXPS */
514 puts ("-o FILE, --output=FILE\n\
515 Write the tags to FILE.");
516 puts ("-I, --ignore-indentation\n\
517 Don't rely on indentation quite as much as normal. Currently,\n\
518 this means not to assume that a closing brace in the first\n\
519 column is the final brace of a function or structure\n\
520 definition in C and C++.");
524 puts ("-t, --typedefs\n\
525 Generate tag entries for C typedefs.");
526 puts ("-T, --typedefs-and-c++\n\
527 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
528 and C++ member functions.");
529 puts ("-u, --update\n\
530 Update the tag entries for the given files, leaving tag\n\
531 entries for other files in place. Currently, this is\n\
532 implemented by deleting the existing entries for the given\n\
533 files and then rewriting the new entries at the end of the\n\
534 tags file. It is often faster to simply rebuild the entire\n\
535 tag file than to use this.");
536 puts ("-v, --vgrind\n\
537 Generates an index of items intended for human consumption,\n\
538 similar to the output of vgrind. The index is sorted, and\n\
539 gives the page number of each item.");
540 puts ("-w, --no-warn\n\
541 Suppress warning messages about entries defined in multiple\n\
543 puts ("-x, --cxref\n\
544 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
545 The output uses line numbers instead of page numbers, but\n\
546 beyond that the differences are cosmetic; try both to see\n\
550 puts ("-V, --version\n\
551 Print the version of the program.\n\
553 Print this help message.");
555 print_language_names ();
558 puts ("Report bugs to bug-gnu-emacs@prep.ai.mit.edu");
571 /* This structure helps us allow mixing of --lang and filenames. */
574 enum argument_type arg_type
;
576 Lang_function
*function
;
579 #ifdef VMS /* VMS specific functions */
583 /* This is a BUG! ANY arbitrary limit is a BUG!
584 Won't someone please fix this? */
585 #define MAX_FILE_SPEC_LEN 255
588 char body
[MAX_FILE_SPEC_LEN
+ 1];
592 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
593 returning in each successive call the next filename matching the input
594 spec. The function expects that each in_spec passed
595 to it will be processed to completion; in particular, up to and
596 including the call following that in which the last matching name
597 is returned, the function ignores the value of in_spec, and will
598 only start processing a new spec with the following call.
599 If an error occurs, on return out_spec contains the value
600 of in_spec when the error occurred.
602 With each successive filename returned in out_spec, the
603 function's return value is one. When there are no more matching
604 names the function returns zero. If on the first call no file
605 matches in_spec, or there is any other error, -1 is returned.
610 #define OUTSIZE MAX_FILE_SPEC_LEN
616 static long context
= 0;
617 static struct dsc$descriptor_s o
;
618 static struct dsc$descriptor_s i
;
619 static logical pass1
= TRUE
;
626 o
.dsc$a_pointer
= (char *) out
;
627 o
.dsc$w_length
= (short)OUTSIZE
;
628 i
.dsc$a_pointer
= in
;
629 i
.dsc$w_length
= (short)strlen(in
);
630 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
631 i
.dsc$b_class
= DSC$K_CLASS_S
;
632 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
633 o
.dsc$b_class
= DSC$K_CLASS_VS
;
635 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
637 out
->body
[out
->curlen
] = EOS
;
640 else if (status
== RMS$_NMF
)
644 strcpy(out
->body
, in
);
647 lib$
find_file_end(&context
);
653 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
654 name of each file specified by the provided arg expanding wildcards.
657 gfnames (arg
, p_error
)
661 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
663 switch (fn_exp (&filename
, arg
))
667 return filename
.body
;
673 return filename
.body
;
677 #ifndef OLD /* Newer versions of VMS do provide `system'. */
681 fprintf (stderr
, "system() function not implemented under VMS\n");
685 #define VERSION_DELIM ';'
686 char *massage_name (s
)
692 if (*s
== VERSION_DELIM
)
710 unsigned int nincluded_files
= 0;
711 char **included_files
= xnew (argc
, char *);
714 int current_arg
= 0, file_count
= 0;
715 struct linebuffer filename_lb
;
721 _fmode
= O_BINARY
; /* all of files are treated as binary files */
726 /* Allocate enough no matter what happens. Overkill, but each one
728 argbuffer
= xnew (argc
, argument
);
731 /* Set syntax for regular expression routines. */
732 re_set_syntax (RE_SYNTAX_EMACS
);
733 #endif /* ETAGS_REGEXPS */
736 * If etags, always find typedefs and structure tags. Why not?
737 * Also default is to find macro constants and enum constants.
740 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
744 int opt
= getopt_long (argc
, argv
,
745 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
753 /* If getopt returns 0, then it has already processed a
754 long-named option. We should do nothing. */
758 /* This means that a filename has been seen. Record it. */
759 argbuffer
[current_arg
].arg_type
= at_filename
;
760 argbuffer
[current_arg
].what
= optarg
;
765 /* Common options. */
767 append_to_tagfile
= TRUE
;
773 constantypedefs
= TRUE
;
776 constantypedefs
= FALSE
;
778 case 'f': /* for compatibility with old makefiles */
782 fprintf (stderr
, "%s: -%c option may only be given once.\n",
784 suggest_asking_for_help ();
789 case 'S': /* for backward compatibility */
790 noindentypedefs
= TRUE
;
793 argbuffer
[current_arg
].function
= get_language_from_name (optarg
);
794 argbuffer
[current_arg
].arg_type
= at_language
;
799 argbuffer
[current_arg
].arg_type
= at_regexp
;
800 argbuffer
[current_arg
].what
= optarg
;
804 argbuffer
[current_arg
].arg_type
= at_regexp
;
805 argbuffer
[current_arg
].what
= NULL
;
808 #endif /* ETAGS_REGEXPS */
820 typedefs
= typedefs_and_cplusplus
= TRUE
;
825 included_files
[nincluded_files
++] = optarg
;
846 suggest_asking_for_help ();
850 for (; optind
< argc
; ++optind
)
852 argbuffer
[current_arg
].arg_type
= at_filename
;
853 argbuffer
[current_arg
].what
= argv
[optind
];
858 if (nincluded_files
== 0 && file_count
== 0)
860 fprintf (stderr
, "%s: No input files specified.\n", progname
);
861 suggest_asking_for_help ();
865 tagfile
= CTAGS
? "tags" : "TAGS";
866 cwd
= etags_getcwd (); /* the current working directory */
867 if (cwd
[strlen (cwd
) - 1] != '/')
868 cwd
= concat (cwd
, "/", "");
869 if (streq (tagfile
, "-"))
872 tagfiledir
= absolute_dirname (tagfile
, cwd
);
874 init (); /* set up boolean "functions" */
877 initbuffer (&token_name
);
878 initbuffer (&lbs
[0].lb
);
879 initbuffer (&lbs
[1].lb
);
880 initbuffer (&filename_lb
);
884 if (streq (tagfile
, "-"))
888 /* Switch redirected `stdout' to binary mode (setting `_fmode'
889 doesn't take effect until after `stdout' is already open). */
890 if (!isatty (fileno (stdout
)))
891 setmode (fileno (stdout
), O_BINARY
);
895 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
901 * Loop through files finding functions.
903 for (i
= 0; i
< current_arg
; ++i
)
905 switch (argbuffer
[i
].arg_type
)
908 lang_func
= argbuffer
[i
].function
;
912 add_regex (argbuffer
[i
].what
);
917 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
921 error ("Can't find file %s\n", this_file
);
926 this_file
= massage_name (this_file
);
929 this_file
= argbuffer
[i
].what
;
931 /* Input file named "-" means read file names from stdin
933 if (streq (this_file
, "-"))
934 while (readline_internal (&filename_lb
, stdin
) > 0)
935 process_file (filename_lb
.buffer
);
937 process_file (this_file
);
947 while (nincluded_files
-- > 0)
948 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
954 /* If CTAGS, we are here. process_file did not write the tags yet,
955 because we want them ordered. Let's do it now. */
965 for (i
= 0; i
< current_arg
; ++i
)
967 if (argbuffer
[i
].arg_type
!= at_filename
)
970 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
971 tagfile
, argbuffer
[i
].what
, tagfile
);
972 if (system (cmd
) != GOOD
)
973 fatal ("failed to execute shell command", NULL
);
975 append_to_tagfile
= TRUE
;
978 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
987 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
995 * Return a Lang_function given the name.
998 get_language_from_name (name
)
1001 struct lang_entry
*lang
;
1004 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1006 if (streq (name
, lang
->name
))
1007 return lang
->function
;
1010 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
1012 suggest_asking_for_help ();
1014 /* This point should never be reached. The function should either
1015 return a function pointer or never return. Note that a NULL
1016 pointer cannot be considered as an error, as it means that the
1017 language has not been explicitely imposed by the user ("auto"). */
1018 return NULL
; /* avoid warnings from compiler */
1023 * Return a Lang_function given the interpreter name.
1026 get_language_from_interpreter (interpreter
)
1029 struct lang_entry
*lang
;
1032 if (interpreter
== NULL
)
1034 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1035 if (lang
->interpreters
!= NULL
)
1036 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1037 if (streq (*iname
, interpreter
))
1038 return lang
->function
;
1046 * Return a Lang_function given the file suffix.
1049 get_language_from_suffix (suffix
)
1052 struct lang_entry
*lang
;
1057 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1058 if (lang
->suffixes
!= NULL
)
1059 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1060 if (streq (*ext
, suffix
))
1061 return lang
->function
;
1068 * This routine is called on each file argument.
1074 struct stat stat_buf
;
1079 for (p
= file
; *p
!= '\0'; p
++)
1084 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1086 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1089 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1091 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1094 inf
= fopen (file
, "r");
1101 find_entries (file
, inf
);
1107 if (absolutefn (file
))
1109 /* file is an absolute filename. Canonicalise it. */
1110 filename
= absolute_filename (file
, cwd
);
1114 /* file is a filename relative to cwd. Make it relative
1115 to the directory of the tags file. */
1116 filename
= relative_filename (file
, tagfiledir
);
1118 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1127 * This routine sets up the boolean pseudo-functions which work
1128 * by setting boolean flags dependent upon the corresponding character
1129 * Every char which is NOT in that string is not a white char. Therefore,
1130 * all of the array "_wht" is set to FALSE, and then the elements
1131 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1132 * of a char is TRUE if it is the string "white", else FALSE.
1140 for (i
= 0; i
< 0177; i
++)
1141 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1142 for (sp
= white
; *sp
; sp
++)
1144 for (sp
= endtk
; *sp
; sp
++)
1146 for (sp
= intk
; *sp
; sp
++)
1148 for (sp
= begtk
; *sp
; sp
++)
1150 _wht
[0] = _wht
['\n'];
1151 _etk
[0] = _etk
['\n'];
1152 _btk
[0] = _btk
['\n'];
1153 _itk
[0] = _itk
['\n'];
1157 * This routine opens the specified file and calls the function
1158 * which finds the function and type definitions.
1161 find_entries (file
, inf
)
1166 Lang_function
*function
;
1167 NODE
*old_last_node
;
1168 extern NODE
*last_node
;
1171 /* Memory leakage here: the memory block pointed by curfile is never
1172 released. The amount of memory leaked here is the sum of the
1173 lengths of the input file names. */
1174 curfile
= savestr (file
);
1176 /* If user specified a language, use it. */
1177 function
= lang_func
;
1178 if (function
!= NULL
)
1185 cp
= etags_strrchr (file
, '.');
1189 function
= get_language_from_suffix (cp
);
1190 if (function
!= NULL
)
1198 /* Look for sharp-bang as the first two characters. */
1199 if (readline_internal (&lb
, inf
) > 2
1200 && lb
.buffer
[0] == '#'
1201 && lb
.buffer
[1] == '!')
1205 /* Set lp to point at the first char after the last slash in the
1206 line or, if no slashes, at the first nonblank. Then set cp to
1207 the first successive blank and terminate the string. */
1208 lp
= etags_strrchr (lb
.buffer
+2, '/');
1212 for (lp
= lb
.buffer
+2; *lp
!= '\0' && isspace (*lp
); lp
++)
1214 for (cp
= lp
; *cp
!= '\0' && !isspace (*cp
); cp
++)
1218 if (strlen (lp
) > 0)
1220 function
= get_language_from_interpreter (lp
);
1221 if (function
!= NULL
)
1232 old_last_node
= last_node
;
1233 Fortran_functions (inf
);
1235 /* No Fortran entries found. Try C. */
1236 if (old_last_node
== last_node
)
1239 default_C_entries (inf
);
1247 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1248 char *name
; /* tag name, or NULL if unnamed */
1249 logical is_func
; /* tag is a function */
1250 char *linestart
; /* start of the line where tag is */
1251 int linelen
; /* length of the line where tag is */
1252 int lno
; /* line number */
1253 long cno
; /* character number */
1257 if (CTAGS
&& name
== NULL
)
1260 np
= xnew (1, NODE
);
1262 /* If ctags mode, change name "main" to M<thisfilename>. */
1263 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1265 register char *fp
= etags_strrchr (curfile
, '/');
1266 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1267 fp
= etags_strrchr (np
->name
, '.');
1268 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1273 np
->been_warned
= FALSE
;
1275 np
->is_func
= is_func
;
1277 /* Our char numbers are 0-base, because of C language tradition?
1278 ctags compatibility? old versions compatibility? I don't know.
1279 Anyway, since emacs's are 1-base we expect etags.el to take care
1280 of the difference. If we wanted to have 1-based numbers, we would
1281 uncomment the +1 below. */
1282 np
->cno
= cno
/* + 1 */ ;
1283 np
->left
= np
->right
= NULL
;
1284 if (CTAGS
&& !cxref_style
)
1286 if (strlen (linestart
) < 50)
1287 np
->pat
= concat (linestart
, "$", "");
1289 np
->pat
= savenstr (linestart
, 50);
1292 np
->pat
= savenstr (linestart
, linelen
);
1294 add_node (np
, &head
);
1299 * recurse on left children, iterate on right children.
1303 register NODE
*node
;
1307 register NODE
*node_right
= node
->right
;
1308 free_tree (node
->left
);
1309 if (node
->name
!= NULL
)
1312 free ((char *) node
);
1319 * Adds a node to the tree of nodes. In etags mode, we don't keep
1320 * it sorted; we just keep a linear list. In ctags mode, maintain
1321 * an ordered tree, with no attempt at balancing.
1323 * add_node is the only function allowed to add nodes, so it can
1326 NODE
*last_node
= NULL
;
1328 add_node (node
, cur_node_p
)
1329 NODE
*node
, **cur_node_p
;
1332 register NODE
*cur_node
= *cur_node_p
;
1334 if (cur_node
== NULL
)
1344 if (last_node
== NULL
)
1345 fatal ("internal error in add_node", NULL
);
1346 last_node
->right
= node
;
1352 dif
= strcmp (node
->name
, cur_node
->name
);
1355 * If this tag name matches an existing one, then
1356 * do not add the node, but maybe print a warning.
1360 if (streq (node
->file
, cur_node
->file
))
1364 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1365 node
->file
, lineno
, node
->name
);
1366 fprintf (stderr
, "Second entry ignored\n");
1369 else if (!cur_node
->been_warned
&& !no_warnings
)
1373 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1374 node
->file
, cur_node
->file
, node
->name
);
1375 cur_node
->been_warned
= TRUE
;
1380 /* Actually add the node */
1381 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1387 register NODE
*node
;
1394 /* Output subentries that precede this one */
1395 put_entries (node
->left
);
1397 /* Output this entry */
1401 if (node
->name
!= NULL
)
1402 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1403 node
->pat
, node
->name
, node
->lno
, node
->cno
);
1405 fprintf (tagf
, "%s\177%d,%d\n",
1406 node
->pat
, node
->lno
, node
->cno
);
1410 if (node
->name
== NULL
)
1411 error ("internal error: NULL name in ctags mode.", NULL
);
1416 fprintf (stdout
, "%s %s %d\n",
1417 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1419 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1420 node
->name
, node
->lno
, node
->file
, node
->pat
);
1424 fprintf (tagf
, "%s\t%s\t", node
->name
, node
->file
);
1428 putc (searchar
, tagf
);
1431 for (sp
= node
->pat
; *sp
; sp
++)
1433 if (*sp
== '\\' || *sp
== searchar
)
1437 putc (searchar
, tagf
);
1440 { /* a typedef; text pattern inadequate */
1441 fprintf (tagf
, "%d", node
->lno
);
1447 /* Output subentries that follow this one */
1448 put_entries (node
->right
);
1451 /* Length of a number's decimal representation. */
1459 for (; num
; num
/= 10)
1465 * Return total number of characters that put_entries will output for
1466 * the nodes in the subtree of the specified node. Works only if
1467 * we are not ctags, but called only in that case. This count
1468 * is irrelevant with the new tags.el, but is still supplied for
1469 * backward compatibility.
1472 total_size_of_entries (node
)
1473 register NODE
*node
;
1481 for (; node
; node
= node
->right
)
1483 /* Count left subentries. */
1484 total
+= total_size_of_entries (node
->left
);
1486 /* Count this entry */
1487 total
+= strlen (node
->pat
) + 1;
1488 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1489 if (node
->name
!= NULL
)
1490 total
+= 1 + strlen (node
->name
); /* \001name */
1497 * The C symbol tables.
1501 st_none
, st_C_objprot
, st_C_objimpl
, st_C_objend
, st_C_gnumacro
,
1502 st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1505 /* Feed stuff between (but not including) %[ and %] lines to:
1506 gperf -c -k 1,3 -o -p -r -t
1508 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1510 @interface, 0, st_C_objprot
1511 @protocol, 0, st_C_objprot
1512 @implementation,0, st_C_objimpl
1513 @end, 0, st_C_objend
1514 class, C_PLPL, st_C_struct
1515 namespace, C_PLPL, st_C_struct
1516 domain, C_STAR, st_C_struct
1517 union, 0, st_C_struct
1518 struct, 0, st_C_struct
1520 typedef, 0, st_C_typedef
1521 define, 0, st_C_define
1522 bool, C_PLPL, st_C_typespec
1523 long, 0, st_C_typespec
1524 short, 0, st_C_typespec
1525 int, 0, st_C_typespec
1526 char, 0, st_C_typespec
1527 float, 0, st_C_typespec
1528 double, 0, st_C_typespec
1529 signed, 0, st_C_typespec
1530 unsigned, 0, st_C_typespec
1531 auto, 0, st_C_typespec
1532 void, 0, st_C_typespec
1533 extern, 0, st_C_typespec
1534 static, 0, st_C_typespec
1535 const, 0, st_C_typespec
1536 volatile, 0, st_C_typespec
1537 explicit, C_PLPL, st_C_typespec
1538 mutable, C_PLPL, st_C_typespec
1539 typename, C_PLPL, st_C_typespec
1540 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1541 DEFUN, 0, st_C_gnumacro
1542 SYSCALL, 0, st_C_gnumacro
1543 ENTRY, 0, st_C_gnumacro
1544 PSEUDO, 0, st_C_gnumacro
1545 # These are defined inside C functions, so currently they are not met.
1546 # EXFUN used in glibc, DEFVAR_* in emacs.
1547 #EXFUN, 0, st_C_gnumacro
1548 #DEFVAR_, 0, st_C_gnumacro
1550 and replace lines between %< and %> with its output. */
1552 /* C code produced by gperf version 2.1 (K&R C version) */
1553 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
1556 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1558 #define MIN_WORD_LENGTH 3
1559 #define MAX_WORD_LENGTH 15
1560 #define MIN_HASH_VALUE 34
1561 #define MAX_HASH_VALUE 121
1564 88 is the maximum key range
1570 register unsigned int len
;
1572 static unsigned char hash_table
[] =
1574 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1575 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
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, 45, 121, 121, 121, 16, 19,
1581 61, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1582 10, 121, 121, 20, 53, 121, 121, 121, 121, 121,
1583 121, 121, 121, 121, 121, 121, 121, 41, 45, 22,
1584 60, 47, 37, 28, 121, 55, 121, 121, 20, 14,
1585 29, 30, 5, 121, 50, 59, 30, 54, 6, 121,
1586 121, 121, 121, 121, 121, 121, 121, 121,
1588 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1591 struct C_stab_entry
*
1592 in_word_set (str
, len
)
1594 register unsigned int len
;
1597 static struct C_stab_entry wordlist
[] =
1599 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1600 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1601 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1602 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1603 {"volatile", 0, st_C_typespec
},
1604 {"PSEUDO", 0, st_C_gnumacro
},
1605 {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1606 {"typedef", 0, st_C_typedef
},
1607 {"typename", C_PLPL
, st_C_typespec
},
1608 {"",}, {"",}, {"",},
1609 {"SYSCALL", 0, st_C_gnumacro
},
1610 {"",}, {"",}, {"",},
1611 {"mutable", C_PLPL
, st_C_typespec
},
1612 {"namespace", C_PLPL
, st_C_struct
},
1613 {"long", 0, st_C_typespec
},
1615 {"const", 0, st_C_typespec
},
1616 {"",}, {"",}, {"",},
1617 {"explicit", C_PLPL
, st_C_typespec
},
1618 {"",}, {"",}, {"",}, {"",},
1619 {"void", 0, st_C_typespec
},
1621 {"char", 0, st_C_typespec
},
1622 {"class", C_PLPL
, st_C_struct
},
1623 {"",}, {"",}, {"",},
1624 {"float", 0, st_C_typespec
},
1626 {"@implementation", 0, st_C_objimpl
},
1627 {"auto", 0, st_C_typespec
},
1629 {"ENTRY", 0, st_C_gnumacro
},
1630 {"@end", 0, st_C_objend
},
1631 {"bool", C_PLPL
, st_C_typespec
},
1632 {"domain", C_STAR
, st_C_struct
},
1634 {"DEFUN", 0, st_C_gnumacro
},
1635 {"extern", 0, st_C_typespec
},
1636 {"@interface", 0, st_C_objprot
},
1637 {"",}, {"",}, {"",},
1638 {"int", 0, st_C_typespec
},
1639 {"",}, {"",}, {"",}, {"",},
1640 {"signed", 0, st_C_typespec
},
1641 {"short", 0, st_C_typespec
},
1642 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1643 {"define", 0, st_C_define
},
1644 {"@protocol", 0, st_C_objprot
},
1645 {"enum", 0, st_C_enum
},
1646 {"static", 0, st_C_typespec
},
1647 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1648 {"union", 0, st_C_struct
},
1649 {"struct", 0, st_C_struct
},
1650 {"",}, {"",}, {"",}, {"",},
1651 {"double", 0, st_C_typespec
},
1652 {"unsigned", 0, st_C_typespec
},
1655 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1657 register int key
= hash (str
, len
);
1659 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1661 register char *s
= wordlist
[key
].name
;
1663 if (*s
== *str
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
1664 return &wordlist
[key
];
1672 C_symtype (str
, len
, c_ext
)
1677 register struct C_stab_entry
*se
= in_word_set (str
, len
);
1679 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1685 * C functions are recognized using a simple finite automaton.
1686 * funcdef is its state variable.
1690 fnone
, /* nothing seen */
1691 ftagseen
, /* function-like tag seen */
1692 fstartlist
, /* just after open parenthesis */
1693 finlist
, /* in parameter list */
1694 flistseen
, /* after parameter list */
1695 fignore
/* before open brace */
1700 * typedefs are recognized using a simple finite automaton.
1701 * typdef is its state variable.
1705 tnone
, /* nothing seen */
1706 ttypedseen
, /* typedef keyword seen */
1707 tinbody
, /* inside typedef body */
1708 tend
, /* just before typedef tag */
1709 tignore
/* junk after typedef tag */
1714 * struct-like structures (enum, struct and union) are recognized
1715 * using another simple finite automaton. `structdef' is its state
1720 snone
, /* nothing seen yet */
1721 skeyseen
, /* struct-like keyword seen */
1722 stagseen
, /* struct-like tag seen */
1723 scolonseen
, /* colon seen after struct-like tag */
1724 sinbody
/* in struct body: recognize member func defs*/
1728 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1729 * struct tag, and structtype is the type of the preceding struct-like
1732 char *structtag
= "<uninited>";
1733 enum sym_type structtype
;
1736 * When objdef is different from onone, objtag is the name of the class.
1738 char *objtag
= "<uninited>";
1741 * Yet another little state machine to deal with preprocessor lines.
1745 dnone
, /* nothing seen */
1746 dsharpseen
, /* '#' seen as first char on line */
1747 ddefineseen
, /* '#' and 'define' seen */
1748 dignorerest
/* ignore rest of line */
1752 * State machine for Objective C protocols and implementations.
1756 onone
, /* nothing seen */
1757 oprotocol
, /* @interface or @protocol seen */
1758 oimplementation
, /* @implementations seen */
1759 otagseen
, /* class name seen */
1760 oparenseen
, /* parenthesis before category seen */
1761 ocatseen
, /* category name seen */
1762 oinbody
, /* in @implementation body */
1763 omethodsign
, /* in @implementation body, after +/- */
1764 omethodtag
, /* after method name */
1765 omethodcolon
, /* after method colon */
1766 omethodparm
, /* after method parameter */
1767 oignore
/* wait for @end */
1771 * Set this to TRUE, and the next token considered is called a function.
1772 * Used only for GNU emacs's function-defining macros.
1774 logical next_token_is_func
;
1777 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1782 * methodlen is the length of the method name stored in token_name.
1788 * checks to see if the current token is at the start of a
1789 * function, or corresponds to a typedef, or is a struct/union/enum
1790 * tag, or #define, or an enum constant.
1792 * *IS_FUNC gets TRUE iff the token is a function or #define macro
1793 * with args. C_EXT is which language we are looking at.
1795 * In the future we will need some way to adjust where the end of
1796 * the token is; for instance, implementing the C++ keyword
1797 * `operator' properly will adjust the end of the token to be after
1798 * whatever follows `operator'.
1806 * next_token_is_func IN OUT
1810 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func
)
1811 register char *str
; /* IN: token pointer */
1812 register int len
; /* IN: token length */
1813 register char c
; /* IN: first char after the token */
1814 int c_ext
; /* IN: C extensions mask */
1815 int cblev
; /* IN: curly brace level */
1816 int parlev
; /* IN: parenthesis level */
1817 logical
*is_func
; /* OUT: function found */
1819 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1822 * Advance the definedef state machine.
1827 /* We're not on a preprocessor line. */
1830 if (toktype
== st_C_define
)
1832 definedef
= ddefineseen
;
1836 definedef
= dignorerest
;
1841 * Make a tag for any macro, unless it is a constant
1842 * and constantypedefs is FALSE.
1844 definedef
= dignorerest
;
1845 *is_func
= (c
== '(');
1846 if (!*is_func
&& !constantypedefs
)
1853 error ("internal error: definedef value.", NULL
);
1862 if (toktype
== st_C_typedef
)
1865 typdef
= ttypedseen
;
1881 /* Do not return here, so the structdef stuff has a chance. */
1895 * This structdef business is currently only invoked when cblev==0.
1896 * It should be recursively invoked whatever the curly brace level,
1897 * and a stack of states kept, to allow for definitions of structs
1900 * This structdef business is NOT invoked when we are ctags and the
1901 * file is plain C. This is because a struct tag may have the same
1902 * name as another tag, and this loses with ctags.
1908 if (typdef
== ttypedseen
1909 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1911 structdef
= skeyseen
;
1912 structtype
= toktype
;
1917 if (structdef
== skeyseen
)
1919 /* Save the tag for struct/union/class, for functions that may be
1921 if (structtype
== st_C_struct
)
1922 structtag
= savenstr (str
, len
);
1924 structtag
= "<enum>";
1925 structdef
= stagseen
;
1929 /* Avoid entering funcdef stuff if typdef is going on. */
1930 if (typdef
!= tnone
)
1936 /* Detect GNU macros.
1938 DEFUN note for writers of emacs C code:
1939 The DEFUN macro, used in emacs C source code, has a first arg
1940 that is a string (the lisp function name), and a second arg that
1941 is a C function name. Since etags skips strings, the second arg
1942 is tagged. This is unfortunate, as it would be better to tag the
1943 first arg. The simplest way to deal with this problem would be
1944 to name the tag with a name built from the function name, by
1945 removing the initial 'F' character and substituting '-' for '_'.
1946 Anyway, this assumes that the conventions of naming lisp
1947 functions will never change. Currently, this method is not
1948 implemented, so writers of emacs code are recommended to put the
1949 first two args of a DEFUN on the same line. */
1950 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
1952 next_token_is_func
= TRUE
;
1955 if (next_token_is_func
)
1957 next_token_is_func
= FALSE
;
1963 /* Detect Objective C constructs. */
1973 objdef
= oimplementation
;
1977 case oimplementation
:
1978 /* Save the class tag for functions that may be defined inside. */
1979 objtag
= savenstr (str
, len
);
1983 /* Save the class tag for categories. */
1984 objtag
= savenstr (str
, len
);
1997 objdef
= omethodtag
;
1999 grow_linebuffer (&token_name
, methodlen
+1);
2000 strncpy (token_name
.buffer
, str
, len
);
2001 token_name
.buffer
[methodlen
] = '\0';
2007 objdef
= omethodparm
;
2012 objdef
= omethodtag
;
2014 grow_linebuffer (&token_name
, methodlen
+1);
2015 strncat (token_name
.buffer
, str
, len
);
2020 if (toktype
== st_C_objend
)
2022 /* Memory leakage here: the string pointed by objtag is
2023 never released, because many tests would be needed to
2024 avoid breaking on incorrect input code. The amount of
2025 memory leaked here is the sum of the lengths of the
2033 /* A function or enum constant? */
2037 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2038 funcdef
= fnone
; /* should be useless */
2041 if (constantypedefs
&& structdef
== sinbody
&& structtype
== st_C_enum
)
2043 if (funcdef
== fnone
)
2056 * This routine finds functions, typedefs, #define's, enum
2057 * constants and struct/union/enum definitions in C syntax
2058 * and adds them to the list.
2071 #define current_lb_is_new (newndx == curndx)
2072 #define switch_line_buffers() (curndx = 1 - curndx)
2074 #define curlb (lbs[curndx].lb)
2075 #define othlb (lbs[1-curndx].lb)
2076 #define newlb (lbs[newndx].lb)
2077 #define curlinepos (lbs[curndx].linepos)
2078 #define othlinepos (lbs[1-curndx].linepos)
2079 #define newlinepos (lbs[newndx].linepos)
2081 #define CNL_SAVE_DEFINEDEF \
2083 curlinepos = charno; \
2085 linecharno = charno; \
2086 charno += readline (&curlb, inf); \
2087 lp = curlb.buffer; \
2094 CNL_SAVE_DEFINEDEF; \
2095 if (savetok.valid) \
2098 savetok.valid = FALSE; \
2100 definedef = dnone; \
2103 /* This macro should never be called when tok.valid is FALSE, but
2104 we must protect about both invalid input and internal errors. */
2105 #define make_C_tag(isfun) do \
2107 char *name = NULL; \
2108 if (CTAGS || tok.named) \
2109 name = savestr (token_name.buffer); \
2110 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
2111 tok.valid = FALSE; \
2112 } /* else if (DEBUG) abort (); */ while (0)
2115 C_entries (c_ext
, inf
)
2116 int c_ext
; /* extension of C */
2117 FILE *inf
; /* input file */
2119 register char c
; /* latest char read; '\0' for end of line */
2120 register char *lp
; /* pointer one beyond the character `c' */
2121 int curndx
, newndx
; /* indices for current and new lb */
2122 TOKEN tok
; /* latest token read */
2123 register int tokoff
; /* offset in line of start of current token */
2124 register int toklen
; /* length of current token */
2125 int cblev
; /* current curly brace level */
2126 int parlev
; /* current parenthesis level */
2127 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
2129 TOKEN savetok
; /* token saved during preprocessor handling */
2132 curndx
= newndx
= 0;
2138 funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
2139 definedef
= dnone
; objdef
= onone
;
2140 next_token_is_func
= yacc_rules
= FALSE
;
2141 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2142 tok
.valid
= savetok
.valid
= FALSE
;
2145 cplpl
= c_ext
& C_PLPL
;
2152 /* If we're at the end of the line, the next character is a
2153 '\0'; don't skip it, because it's the thing that tells us
2154 to read the next line. */
2175 /* Newlines inside comments do not end macro definitions in
2190 /* Newlines inside strings do not end macro definitions
2191 in traditional cpp, even though compilers don't
2192 usually accept them. */
2203 /* Hmmm, something went wrong. */
2217 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2222 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2232 else if (/* cplpl && */ *lp
== '/')
2240 if ((c_ext
& YACC
) && *lp
== '%')
2242 /* entering or exiting rules section in yacc file */
2244 definedef
= dnone
; funcdef
= fnone
;
2245 typdef
= tnone
; structdef
= snone
;
2246 next_token_is_func
= FALSE
;
2247 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2249 yacc_rules
= !yacc_rules
;
2255 if (definedef
== dnone
)
2258 logical cpptoken
= TRUE
;
2260 /* Look back on this line. If all blanks, or nonblanks
2261 followed by an end of comment, this is a preprocessor
2263 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2266 if (*cp
== '*' && *(cp
+1) == '/')
2275 definedef
= dsharpseen
;
2276 } /* if (definedef == dnone) */
2282 /* Consider token only if some complicated conditions are satisfied. */
2283 if ((definedef
!= dnone
2284 || (cblev
== 0 && structdef
!= scolonseen
)
2285 || (cblev
== 1 && cplpl
&& structdef
== sinbody
)
2286 || (structdef
== sinbody
&& structtype
== st_C_enum
))
2287 && typdef
!= tignore
2288 && definedef
!= dignorerest
2289 && funcdef
!= finlist
)
2295 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken(*(lp
+ 1)))
2298 * This handles :: in the middle, but not at the
2299 * beginning of an identifier.
2306 logical is_func
= FALSE
;
2309 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2310 c_ext
, cblev
, parlev
, &is_func
))
2312 if (structdef
== sinbody
2313 && definedef
== dnone
2315 /* function defined in C++ class body */
2317 grow_linebuffer (&token_name
,
2318 strlen(structtag
)+2+toklen
+1);
2319 strcpy (token_name
.buffer
, structtag
);
2320 strcat (token_name
.buffer
, "::");
2321 strncat (token_name
.buffer
,
2322 newlb
.buffer
+tokoff
, toklen
);
2325 else if (objdef
== ocatseen
)
2326 /* Objective C category */
2328 grow_linebuffer (&token_name
,
2329 strlen(objtag
)+2+toklen
+1);
2330 strcpy (token_name
.buffer
, objtag
);
2331 strcat (token_name
.buffer
, "(");
2332 strncat (token_name
.buffer
,
2333 newlb
.buffer
+tokoff
, toklen
);
2334 strcat (token_name
.buffer
, ")");
2337 else if (objdef
== omethodtag
2338 || objdef
== omethodparm
)
2339 /* Objective C method */
2345 grow_linebuffer (&token_name
, toklen
+1);
2346 strncpy (token_name
.buffer
,
2347 newlb
.buffer
+tokoff
, toklen
);
2348 token_name
.buffer
[toklen
] = '\0';
2349 if (structdef
== stagseen
2352 && definedef
== dignorerest
)) /* macro */
2357 tok
.lineno
= lineno
;
2358 tok
.linelen
= tokoff
+ toklen
+ 1;
2359 tok
.buffer
= newlb
.buffer
;
2360 tok
.linepos
= newlinepos
;
2363 if (definedef
== dnone
2364 && (funcdef
== ftagseen
2365 || structdef
== stagseen
2367 || objdef
!= onone
))
2369 if (current_lb_is_new
)
2370 switch_line_buffers ();
2373 make_C_tag (is_func
);
2377 } /* if (endtoken (c)) */
2378 else if (intoken (c
))
2383 } /* if (midtoken) */
2384 else if (begtoken (c
))
2402 if (structdef
== stagseen
)
2408 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2410 tokoff
= lp
- 1 - newlb
.buffer
;
2415 } /* if (begtoken) */
2416 } /* if must look at token */
2419 /* Detect end of line, colon, comma, semicolon and various braces
2420 after having handled a token.*/
2424 if (definedef
!= dnone
)
2434 objdef
= omethodcolon
;
2436 grow_linebuffer (&token_name
, methodlen
+1);
2437 strcat (token_name
.buffer
, ":");
2440 if (structdef
== stagseen
)
2441 structdef
= scolonseen
;
2458 if (definedef
!= dnone
)
2469 if (funcdef
!= fignore
)
2472 /* The following instruction invalidates the token.
2473 Probably the token should be invalidated in all
2474 other cases where some state machine is reset. */
2477 if (structdef
== stagseen
)
2481 if (definedef
!= dnone
)
2491 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2493 if (structdef
== stagseen
)
2497 if (definedef
!= dnone
)
2499 if (cblev
== 0 && typdef
== tend
)
2505 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2507 if (structdef
== stagseen
)
2511 if (definedef
!= dnone
)
2513 if (objdef
== otagseen
&& parlev
== 0)
2514 objdef
= oparenseen
;
2522 /* Make sure that the next char is not a '*'.
2523 This handles constructs like:
2524 typedef void OperatorFun (int fun); */
2531 } /* switch (typdef) */
2534 funcdef
= fstartlist
;
2543 if (definedef
!= dnone
)
2545 if (objdef
== ocatseen
&& parlev
== 1)
2556 funcdef
= flistseen
;
2559 if (cblev
== 0 && typdef
== tend
)
2565 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2569 if (definedef
!= dnone
)
2571 if (typdef
== ttypedseen
)
2575 case skeyseen
: /* unnamed struct */
2576 structdef
= sinbody
;
2577 structtag
= "_anonymous_";
2580 case scolonseen
: /* named struct */
2581 structdef
= sinbody
;
2606 /* Neutralize `extern "C" {' grot. */
2607 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2614 if (definedef
!= dnone
)
2616 if (funcdef
== fstartlist
)
2617 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2620 if (definedef
!= dnone
)
2622 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2624 cblev
= 0; /* reset curly brace level if first column */
2625 parlev
= 0; /* also reset paren level, just in case... */
2631 if (typdef
== tinbody
)
2633 /* Memory leakage here: the string pointed by structtag is
2634 never released, because I fear to miss something and
2635 break things while freeing the area. The amount of
2636 memory leaked here is the sum of the lengths of the
2638 if (structdef == sinbody)
2639 free (structtag); */
2642 structtag
= "<error>";
2647 if (objdef
== oinbody
&& cblev
== 0)
2649 objdef
= omethodsign
;
2653 case '=': case '#': case '~': case '&': case '%': case '/':
2654 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2655 if (definedef
!= dnone
)
2657 /* These surely cannot follow a function tag. */
2658 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2662 if (objdef
== otagseen
)
2667 /* If a macro spans multiple lines don't reset its state. */
2675 } /* while not eof */
2679 * Process either a C++ file or a C file depending on the setting
2683 default_C_entries (inf
)
2686 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2689 /* Always do plain ANSI C. */
2691 plain_C_entries (inf
)
2697 /* Always do C++. */
2699 Cplusplus_entries (inf
)
2702 C_entries (C_PLPL
, inf
);
2710 C_entries (C_STAR
, inf
);
2713 /* Always do Yacc. */
2718 C_entries (YACC
, inf
);
2721 /* Fortran parsing */
2729 register int len
= 0;
2731 while (*cp
&& lowcase(*cp
) == lowcase(dbp
[len
]))
2733 if (*cp
== '\0' && !intoken(dbp
[len
]))
2744 while (isspace (*dbp
))
2749 while (isspace (*dbp
))
2751 if (strneq (dbp
, "(*)", 3))
2756 if (!isdigit (*dbp
))
2758 --dbp
; /* force failure */
2763 while (isdigit (*dbp
));
2772 while (isspace (*dbp
))
2777 linecharno
= charno
;
2778 charno
+= readline (&lb
, inf
);
2783 while (isspace (*dbp
))
2792 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2795 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
2796 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2800 Fortran_functions (inf
)
2809 linecharno
= charno
;
2810 charno
+= readline (&lb
, inf
);
2813 dbp
++; /* Ratfor escape to fortran */
2814 while (isspace (*dbp
))
2818 switch (lowcase (*dbp
))
2821 if (tail ("integer"))
2829 if (tail ("logical"))
2833 if (tail ("complex") || tail ("character"))
2837 if (tail ("double"))
2839 while (isspace (*dbp
))
2843 if (tail ("precision"))
2849 while (isspace (*dbp
))
2853 switch (lowcase (*dbp
))
2856 if (tail ("function"))
2860 if (tail ("subroutine"))
2868 if (tail ("program"))
2873 if (tail ("procedure"))
2881 * Bob Weiner, Motorola Inc., 4/3/94
2882 * Unix and microcontroller assembly tag handling
2883 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2897 linecharno
= charno
;
2898 charno
+= readline (&lb
, inf
);
2901 /* If first char is alphabetic or one of [_.$], test for colon
2902 following identifier. */
2903 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2905 /* Read past label. */
2907 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2909 if (*cp
== ':' || isspace (*cp
))
2911 /* Found end of label, so copy it and add it to the table. */
2912 pfnote ((CTAGS
) ? savenstr(lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2913 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2920 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2921 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2924 Perl_functions (inf
)
2935 linecharno
= charno
;
2936 charno
+= readline (&lb
, inf
);
2939 if (*cp
++ == 's' && *cp
++ == 'u' && *cp
++ == 'b' && isspace(*cp
++))
2941 while (*cp
&& isspace(*cp
))
2943 while (*cp
&& ! isspace(*cp
) && *cp
!= '{')
2945 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2946 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2951 /* Added by Mosur Mohan, 4/22/88 */
2952 /* Pascal parsing */
2955 * Locates tags for procedures & functions. Doesn't do any type- or
2956 * var-definitions. It does look for the keyword "extern" or
2957 * "forward" immediately following the procedure statement; if found,
2958 * the tag is skipped.
2961 Pascal_functions (inf
)
2964 struct linebuffer tline
; /* mostly copied from C_entries */
2966 int save_lineno
, save_len
;
2967 char c
, *cp
, *namebuf
;
2969 logical
/* each of these flags is TRUE iff: */
2970 incomment
, /* point is inside a comment */
2971 inquote
, /* point is inside '..' string */
2972 get_tagname
, /* point is after PROCEDURE/FUNCTION
2973 keyword, so next item = potential tag */
2974 found_tag
, /* point is after a potential tag */
2975 inparms
, /* point is within parameter-list */
2976 verify_tag
; /* point has passed the parm-list, so the
2977 next token will determine whether this
2978 is a FORWARD/EXTERN to be ignored, or
2979 whether it is a real tag */
2986 initbuffer (&tline
);
2988 incomment
= inquote
= FALSE
;
2989 found_tag
= FALSE
; /* have a proc name; check if extern */
2990 get_tagname
= FALSE
; /* have found "procedure" keyword */
2991 inparms
= FALSE
; /* found '(' after "proc" */
2992 verify_tag
= FALSE
; /* check if "extern" is ahead */
2994 /* long main loop to get next char */
2998 if (c
== '\0') /* if end of line */
3001 linecharno
= charno
;
3002 charno
+= readline (&lb
, inf
);
3006 if (!((found_tag
&& verify_tag
) ||
3008 c
= *dbp
++; /* only if don't need *dbp pointing
3009 to the beginning of the name of
3010 the procedure or function */
3014 if (c
== '}') /* within { } comments */
3016 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
3033 inquote
= TRUE
; /* found first quote */
3035 case '{': /* found open { comment */
3039 if (*dbp
== '*') /* found open (* comment */
3044 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
3047 case ')': /* end of parms list */
3052 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
3059 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
3061 /* check if this is an "extern" declaration */
3064 if (lowcase (*dbp
== 'e'))
3066 if (tail ("extern")) /* superfluous, really! */
3072 else if (lowcase (*dbp
) == 'f')
3074 if (tail ("forward")) /* check for forward reference */
3080 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
3084 pfnote (namebuf
, TRUE
,
3085 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
3089 if (get_tagname
) /* grab name of proc or fn */
3094 /* save all values for later tagging */
3095 grow_linebuffer (&tline
, strlen (lb
.buffer
) + 1);
3096 strcpy (tline
.buffer
, lb
.buffer
);
3097 save_lineno
= lineno
;
3098 save_lcno
= linecharno
;
3100 /* grab block name */
3101 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
3103 namebuf
= (CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
;
3104 dbp
= cp
; /* set dbp to e-o-token */
3105 save_len
= dbp
- lb
.buffer
+ 1;
3106 get_tagname
= FALSE
;
3110 /* and proceed to check for "extern" */
3112 else if (!incomment
&& !inquote
&& !found_tag
)
3114 /* check for proc/fn keywords */
3115 switch (lowcase (c
))
3118 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3122 if (tail ("unction"))
3127 } /* while not eof */
3129 free (tline
.buffer
);
3133 * lisp tag functions
3134 * look for (def or (DEF, quote or QUOTE
3138 register char *strp
;
3140 return ((strp
[1] == 'd' || strp
[1] == 'D')
3141 && (strp
[2] == 'e' || strp
[2] == 'E')
3142 && (strp
[3] == 'f' || strp
[3] == 'F'));
3147 register char *strp
;
3149 return ((*(++strp
) == 'q' || *strp
== 'Q')
3150 && (*(++strp
) == 'u' || *strp
== 'U')
3151 && (*(++strp
) == 'o' || *strp
== 'O')
3152 && (*(++strp
) == 't' || *strp
== 'T')
3153 && (*(++strp
) == 'e' || *strp
== 'E')
3154 && isspace(*(++strp
)));
3162 if (*dbp
== '\'') /* Skip prefix quote */
3164 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
3167 while (isspace(*dbp
))
3170 for (cp
= dbp
/*+1*/;
3171 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
3177 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3178 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3182 Lisp_functions (inf
)
3191 linecharno
= charno
;
3192 charno
+= readline (&lb
, inf
);
3198 while (!isspace (*dbp
))
3200 while (isspace (*dbp
))
3206 /* Check for (foo::defmumble name-defined ... */
3209 while (*dbp
&& !isspace (*dbp
)
3210 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
3215 while (*dbp
== ':');
3217 if (L_isdef (dbp
- 1))
3219 while (!isspace (*dbp
))
3221 while (isspace (*dbp
))
3232 * Scheme tag functions
3233 * look for (def... xyzzy
3234 * look for (def... (xyzzy
3235 * look for (def ... ((...(xyzzy ....
3236 * look for (set! xyzzy
3242 Scheme_functions (inf
)
3251 linecharno
= charno
;
3252 charno
+= readline (&lb
, inf
);
3254 if (dbp
[0] == '(' &&
3255 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
3256 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3257 (dbp
[3] == 'F' || dbp
[3] == 'f'))
3259 while (!isspace (*dbp
))
3261 /* Skip over open parens and white space */
3262 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
3266 if (dbp
[0] == '(' &&
3267 (dbp
[1] == 'S' || dbp
[1] == 's') &&
3268 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3269 (dbp
[3] == 'T' || dbp
[3] == 't') &&
3270 (dbp
[4] == '!' || dbp
[4] == '!') &&
3273 while (!isspace (*dbp
))
3275 /* Skip over white space */
3276 while (isspace (*dbp
))
3290 /* Go till you get to white space or a syntactic break */
3292 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
3295 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3296 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3299 /* Find tags in TeX and LaTeX input files. */
3301 /* TEX_toktab is a table of TeX control sequences that define tags.
3302 Each TEX_tabent records one such control sequence.
3303 CONVERT THIS TO USE THE Stab TYPE!! */
3310 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
3312 /* Default set of control sequences to put into TEX_toktab.
3313 The value of environment var TEXTAGS is prepended to this. */
3315 char *TEX_defenv
= "\
3316 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3317 :part:appendix:entry:index";
3320 struct TEX_tabent
*TEX_decode_env ();
3322 #if TeX_named_tokens
3326 char TEX_esc
= '\\';
3327 char TEX_opgrp
= '{';
3328 char TEX_clgrp
= '}';
3331 * TeX/LaTeX scanning loop.
3342 /* Select either \ or ! as escape character. */
3345 /* Initialize token table once from environment. */
3347 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
3350 { /* Scan each line in file */
3352 linecharno
= charno
;
3353 charno
+= readline (&lb
, inf
);
3356 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
3362 linecharno
+= dbp
- lasthit
;
3364 i
= TEX_Token (lasthit
);
3368 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3369 #if TeX_named_tokens
3370 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
3372 break; /* We only save a line once */
3378 #define TEX_LESC '\\'
3379 #define TEX_SESC '!'
3382 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3383 chars accordingly. */
3390 while ((c
= getc (inf
)) != EOF
)
3392 /* Skip to next line if we hit the TeX comment char. */
3396 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3415 /* Read environment and prepend it to the default string.
3416 Build token table. */
3418 TEX_decode_env (evarname
, defenv
)
3422 register char *env
, *p
;
3424 struct TEX_tabent
*tab
;
3427 /* Append default string to environment. */
3428 env
= getenv (evarname
);
3432 env
= concat (env
, defenv
, "");
3434 /* Allocate a token table */
3435 for (size
= 1, p
= env
; p
;)
3436 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3438 /* Add 1 to leave room for null terminator. */
3439 tab
= xnew (size
+ 1, struct TEX_tabent
);
3441 /* Unpack environment string into token table. Be careful about */
3442 /* zero-length strings (leading ':', "::" and trailing ':') */
3445 p
= etags_strchr (env
, ':');
3446 if (!p
) /* End of environment string. */
3447 p
= env
+ strlen (env
);
3449 { /* Only non-zero strings. */
3450 tab
[i
].name
= savenstr (env
, p
- env
);
3451 tab
[i
].len
= strlen (tab
[i
].name
);
3458 tab
[i
].name
= NULL
; /* Mark end of table. */
3466 #if TeX_named_tokens
3467 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3468 The name being defined actually starts at (NAME + LEN + 1).
3469 But we seem to include the TeX command in the tag name. */
3471 TEX_getit (name
, len
)
3475 char *p
= name
+ len
;
3480 /* Let tag name extend to next group close (or end of line) */
3481 while (*p
&& *p
!= TEX_clgrp
)
3483 pfnote (savenstr (name
, p
-name
), TRUE
,
3484 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3488 /* If the text at CP matches one of the tag-defining TeX command names,
3489 return the pointer to the first occurrence of that command in TEX_toktab.
3490 Otherwise return -1.
3491 Keep the capital `T' in `Token' for dumb truncating compilers
3492 (this distinguishes it from `TEX_toktab' */
3499 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3500 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3506 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
3508 * Assumes that the predicate starts at column 0.
3509 * Only the first clause of a predicate is added.
3512 Prolog_functions (inf
)
3516 void prolog_skip_comment ();
3533 linecharno
+= charno
;
3534 charno
= readline (&lb
, inf
);
3536 if (dbp
[0] == '\0') /* Empty line */
3538 else if (isspace (dbp
[0])) /* Not a predicate */
3540 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3541 prolog_skip_comment (&lb
, inf
);
3542 else if (len
= prolog_pred (dbp
, last
))
3544 /* Predicate. Store the function name so that we only
3545 * generates a tag for the first clause. */
3547 last
= xnew(len
+ 1, char);
3548 else if (len
+ 1 > allocated
)
3549 last
= (char *) xrealloc(last
, len
+ 1);
3550 allocated
= len
+ 1;
3551 strncpy (last
, dbp
, len
);
3559 prolog_skip_comment (plb
, inf
)
3560 struct linebuffer
*plb
;
3567 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3568 if (cp
[0] == '*' && cp
[1] == '/')
3571 linecharno
+= readline (plb
, inf
);
3577 * A predicate definition is added if it matches:
3578 * <beginning of line><Prolog Atom><whitespace>(
3580 * It is added to the tags database if it doesn't match the
3581 * name of the previous clause header.
3583 * Return the size of the name of the predicate, or 0 if no header
3587 prolog_pred (s
, last
)
3589 char *last
; /* Name of last clause. */
3597 pos
= prolog_atom(s
, 0);
3602 pos
+= prolog_white(s
, pos
);
3604 if ((s
[pos
] == '(') || (s
[pos
] == '.'))
3609 /* Save only the first clause. */
3610 if ((last
== NULL
) ||
3611 (len
!= strlen(last
)) ||
3612 (strncmp(s
, last
, len
) != 0))
3614 pfnote ((CTAGS
) ? savenstr (s
, len
) : NULL
, TRUE
,
3615 s
, pos
, lineno
, linecharno
);
3623 * Consume a Prolog atom.
3624 * Return the number of bytes consumed, or -1 if there was an error.
3626 * A prolog atom, in this context, could be one of:
3627 * - An alphanumeric sequence, starting with a lower case letter.
3628 * - A quoted arbitrary string. Single quotes can escape themselves.
3629 * Backslash quotes everything.
3632 prolog_atom (s
, pos
)
3640 if (islower(s
[pos
]) || (s
[pos
] == '_'))
3642 /* The atom is unquoted. */
3644 while (isalnum(s
[pos
]) || (s
[pos
] == '_'))
3648 return pos
- origpos
;
3650 else if (s
[pos
] == '\'')
3661 pos
++; /* A double quote */
3663 else if (s
[pos
] == '\0')
3664 /* Multiline quoted atoms are ignored. */
3666 else if (s
[pos
] == '\\')
3668 if (s
[pos
+1] == '\0')
3675 return pos
- origpos
;
3681 /* Consume whitespace. Return the number of bytes eaten. */
3683 prolog_white (s
, pos
)
3691 while (isspace(s
[pos
]))
3694 return pos
- origpos
;
3698 * Support for Erlang -- Anders Lindgren, Feb 1996.
3700 * Generates tags for functions, defines, and records.
3702 * Assumes that Erlang functions start at column 0.
3705 Erlang_functions (inf
)
3709 void erlang_attribute ();
3726 linecharno
+= charno
;
3727 charno
= readline (&lb
, inf
);
3729 if (dbp
[0] == '\0') /* Empty line */
3731 else if (isspace (dbp
[0])) /* Not function nor attribute */
3733 else if (dbp
[0] == '%') /* comment */
3735 else if (dbp
[0] == '"') /* Sometimes, strings start in column one */
3737 else if (dbp
[0] == '-') /* attribute, e.g. "-define" */
3739 erlang_attribute(dbp
);
3742 else if (len
= erlang_func (dbp
, last
))
3745 * Function. Store the function name so that we only
3746 * generates a tag for the first clause.
3749 last
= xnew(len
+ 1, char);
3750 else if (len
+ 1 > allocated
)
3751 last
= (char *) xrealloc(last
, len
+ 1);
3752 allocated
= len
+ 1;
3753 strncpy (last
, dbp
, len
);
3761 * A function definition is added if it matches:
3762 * <beginning of line><Erlang Atom><whitespace>(
3764 * It is added to the tags database if it doesn't match the
3765 * name of the previous clause header.
3767 * Return the size of the name of the function, or 0 if no function
3771 erlang_func (s
, last
)
3773 char *last
; /* Name of last clause. */
3776 int erlang_white ();
3781 pos
= erlang_atom(s
, 0);
3786 pos
+= erlang_white(s
, pos
);
3788 if (s
[pos
++] == '(')
3790 /* Save only the first clause. */
3791 if ((last
== NULL
) ||
3792 (len
!= strlen(last
)) ||
3793 (strncmp(s
, last
, len
) != 0))
3795 pfnote ((CTAGS
) ? savenstr (s
, len
) : NULL
, TRUE
,
3796 s
, pos
, lineno
, linecharno
);
3805 * Handle attributes. Currently, tags are generated for defines
3808 * They are on the form:
3809 * -define(foo, bar).
3810 * -define(Foo(M, N), M+N).
3811 * -record(graph, {vtab = notable, cyclic = true}).
3814 erlang_attribute (s
)
3818 int erlang_white ();
3823 if ((strncmp(s
, "-define", 7) == 0) ||
3824 (strncmp(s
, "-record", 7) == 0))
3827 pos
+= erlang_white(s
, pos
);
3829 if (s
[pos
++] == '(')
3831 pos
+= erlang_white(s
, pos
);
3833 if (len
= erlang_atom(s
, pos
))
3835 pfnote ((CTAGS
) ? savenstr (& s
[pos
], len
) : NULL
, TRUE
,
3836 s
, pos
+ len
, lineno
, linecharno
);
3845 * Consume an Erlang atom (or variable).
3846 * Return the number of bytes consumed, or -1 if there was an error.
3849 erlang_atom (s
, pos
)
3857 if (isalpha (s
[pos
]) || s
[pos
] == '_')
3859 /* The atom is unquoted. */
3861 while (isalnum (s
[pos
]) || s
[pos
] == '_')
3863 return pos
- origpos
;
3865 else if (s
[pos
] == '\'')
3876 else if (s
[pos
] == '\0')
3877 /* Multiline quoted atoms are ignored. */
3879 else if (s
[pos
] == '\\')
3881 if (s
[pos
+1] == '\0')
3888 return pos
- origpos
;
3894 /* Consume whitespace. Return the number of bytes eaten */
3896 erlang_white (s
, pos
)
3904 while (isspace (s
[pos
]))
3907 return pos
- origpos
;
3910 #ifdef ETAGS_REGEXPS
3911 /* Take a string like "/blah/" and turn it into "blah", making sure
3912 that the first and last characters are the same, and handling
3913 quoted separator characters. Actually, stops on the occurrence of
3914 an unquoted separator. Also turns "\t" into a Tab character.
3915 Returns pointer to terminating separator. Works in place. Null
3916 terminates name string. */
3918 scan_separators (name
)
3922 char *copyto
= name
;
3923 logical quoted
= FALSE
;
3925 for (++name
; *name
!= '\0'; ++name
)
3931 else if (*name
== sep
)
3935 /* Something else is quoted, so preserve the quote. */
3941 else if (*name
== '\\')
3943 else if (*name
== sep
)
3949 /* Terminate copied string. */
3954 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3955 expression, into a real regular expression by compiling it. */
3957 add_regex (regexp_pattern
)
3958 char *regexp_pattern
;
3962 struct re_pattern_buffer
*patbuf
;
3964 if (regexp_pattern
== NULL
)
3966 /* Remove existing regexps. */
3972 if (regexp_pattern
[0] == '\0')
3974 error ("missing regexp", NULL
);
3977 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3979 error ("%s: unterminated regexp", regexp_pattern
);
3982 name
= scan_separators (regexp_pattern
);
3983 if (regexp_pattern
[0] == '\0')
3985 error ("null regexp", NULL
);
3988 (void) scan_separators (name
);
3990 patbuf
= xnew (1, struct re_pattern_buffer
);
3991 patbuf
->translate
= NULL
;
3992 patbuf
->fastmap
= NULL
;
3993 patbuf
->buffer
= NULL
;
3994 patbuf
->allocated
= 0;
3996 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3999 error ("%s while compiling pattern", err
);
4004 if (num_patterns
== 1)
4005 patterns
= xnew (1, struct pattern
);
4007 patterns
= ((struct pattern
*)
4009 (num_patterns
* sizeof (struct pattern
))));
4010 patterns
[num_patterns
- 1].pattern
= patbuf
;
4011 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
4012 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
4016 * Do the substitutions indicated by the regular expression and
4020 substitute (in
, out
, regs
)
4022 struct re_registers
*regs
;
4024 char *result
= NULL
, *t
;
4027 /* Pass 1: figure out how much size to allocate. */
4028 for (t
= out
; *t
; ++t
)
4035 fprintf (stderr
, "%s: pattern substitution ends prematurely\n",
4042 size
+= regs
->end
[dig
] - regs
->start
[dig
];
4047 /* Allocate space and do the substitutions. */
4048 result
= xnew (size
+ 1, char);
4057 /* Using "dig2" satisfies my debugger. Bleah. */
4058 int dig2
= *out
- '0';
4059 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
4060 regs
->end
[dig2
] - regs
->start
[dig2
]);
4061 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
4064 result
[size
++] = *out
;
4067 result
[size
++] = *out
;
4069 result
[size
] = '\0';
4074 #endif /* ETAGS_REGEXPS */
4075 /* Initialize a linebuffer for use */
4077 initbuffer (linebuffer
)
4078 struct linebuffer
*linebuffer
;
4080 linebuffer
->size
= 200;
4081 linebuffer
->buffer
= xnew (200, char);
4085 * Read a line of text from `stream' into `linebuffer'.
4086 * Return the number of characters read from `stream',
4087 * which is the length of the line including the newline, if any.
4090 readline_internal (linebuffer
, stream
)
4091 struct linebuffer
*linebuffer
;
4092 register FILE *stream
;
4094 char *buffer
= linebuffer
->buffer
;
4095 register char *p
= linebuffer
->buffer
;
4096 register char *pend
;
4099 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
4103 register int c
= getc (stream
);
4106 linebuffer
->size
*= 2;
4107 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
4108 p
+= buffer
- linebuffer
->buffer
;
4109 pend
= buffer
+ linebuffer
->size
;
4110 linebuffer
->buffer
= buffer
;
4120 if (p
> buffer
&& p
[-1] == '\r')
4124 /* Assume CRLF->LF translation will be performed by Emacs
4125 when loading this file, so CRs won't appear in the buffer.
4126 It would be cleaner to compensate within Emacs;
4127 however, Emacs does not know how many CRs were deleted
4128 before any given point in the file. */
4144 return p
- buffer
+ chars_deleted
;
4148 * Like readline_internal, above, but try to match the input
4149 * line against any existing regular expressions.
4152 readline (linebuffer
, stream
)
4153 struct linebuffer
*linebuffer
;
4156 /* Read new line. */
4157 long result
= readline_internal (linebuffer
, stream
);
4158 #ifdef ETAGS_REGEXPS
4161 /* Match against all listed patterns. */
4162 for (i
= 0; i
< num_patterns
; ++i
)
4164 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
4165 (int)result
, 0, &patterns
[i
].regs
);
4170 if (!patterns
[i
].error_signaled
)
4172 error ("error while matching pattern %d", i
);
4173 patterns
[i
].error_signaled
= TRUE
;
4180 /* Match occurred. Construct a tag. */
4181 if (patterns
[i
].name_pattern
[0] != '\0')
4183 /* Make a named tag. */
4184 char *name
= substitute (linebuffer
->buffer
,
4185 patterns
[i
].name_pattern
,
4189 linebuffer
->buffer
, match
, lineno
, linecharno
);
4193 /* Make an unnamed tag. */
4195 linebuffer
->buffer
, match
, lineno
, linecharno
);
4200 #endif /* ETAGS_REGEXPS */
4206 * Read a file, but do no processing. This is used to do regexp
4207 * matching on files that have no language defined.
4210 just_read_file (inf
)
4219 linecharno
= charno
;
4220 charno
+= readline (&lb
, inf
) + 1;
4226 * Return a pointer to a space of size strlen(cp)+1 allocated
4227 * with xnew where the string CP has been copied.
4233 return savenstr (cp
, strlen (cp
));
4237 * Return a pointer to a space of size LEN+1 allocated with xnew where
4238 * the string CP has been copied for at most the first LEN characters.
4247 dp
= xnew (len
+ 1, char);
4248 strncpy (dp
, cp
, len
);
4254 * Return the ptr in sp at which the character c last
4255 * appears; NULL if not found
4257 * Identical to System V strrchr, included for portability.
4260 etags_strrchr (sp
, c
)
4261 register char *sp
, c
;
4276 * Return the ptr in sp at which the character c first
4277 * appears; NULL if not found
4279 * Identical to System V strchr, included for portability.
4282 etags_strchr (sp
, c
)
4283 register char *sp
, c
;
4293 /* Print error message and exit. */
4311 suggest_asking_for_help ()
4313 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
4318 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
4323 fprintf (stderr
, "%s: ", progname
);
4324 fprintf (stderr
, s1
, s2
);
4325 fprintf (stderr
, "\n");
4328 /* Return a newly-allocated string whose contents
4329 concatenate those of s1, s2, s3. */
4334 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
4335 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
4337 strcpy (result
, s1
);
4338 strcpy (result
+ len1
, s2
);
4339 strcpy (result
+ len1
+ len2
, s3
);
4340 result
[len1
+ len2
+ len3
] = '\0';
4345 /* Does the same work as the system V getcwd, but does not need to
4346 guess the buffer size in advance. */
4352 char *path
= xnew (bufsize
, char);
4354 while (getcwd (path
, bufsize
) == NULL
)
4356 if (errno
!= ERANGE
)
4359 path
= xnew (bufsize
, char);
4364 /* Convert backslashes to slashes. */
4366 for (p
= path
; *p
!= '\0'; p
++)
4374 #else /* not HAVE_GETCWD */
4376 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
4380 for (p
= path
; *p
!= '\0'; p
++)
4386 return strdup (path
);
4387 #else /* not MSDOS */
4388 struct linebuffer path
;
4392 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
4393 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
4398 #endif /* not MSDOS */
4399 #endif /* not HAVE_GETCWD */
4402 /* Return a newly allocated string containing the filename
4403 of FILE relative to the absolute directory DIR (which
4404 should end with a slash). */
4406 relative_filename (file
, dir
)
4409 char *fp
, *dp
, *abs
, *res
;
4411 /* Find the common root of file and dir (with a trailing slash). */
4412 abs
= absolute_filename (file
, cwd
);
4415 while (*fp
++ == *dp
++)
4417 fp
--, dp
--; /* back to the first differing char */
4418 do /* look at the equal chars until / */
4422 /* Build a sequence of "../" strings for the resulting relative filename. */
4423 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
4425 dp
= etags_strchr (dp
+ 1, '/'))
4427 res
= concat (res
, "../", "");
4430 /* Add the filename relative to the common root of file and dir. */
4431 res
= concat (res
, fp
+ 1, "");
4437 /* Return a newly allocated string containing the
4438 absolute filename of FILE given CWD (which should
4439 end with a slash). */
4441 absolute_filename (file
, cwd
)
4444 char *slashp
, *cp
, *res
;
4446 if (absolutefn (file
))
4447 res
= concat (file
, "", "");
4449 /* We don't support non-absolute filenames with a drive
4450 letter, like `d:NAME' (it's too much hassle). */
4451 else if (file
[1] == ':')
4452 fatal ("%s: relative filenames with drive letters not supported", file
);
4455 res
= concat (cwd
, file
, "");
4457 /* Delete the "/dirname/.." and "/." substrings. */
4458 slashp
= etags_strchr (res
, '/');
4459 while (slashp
!= NULL
&& slashp
[0] != '\0')
4461 if (slashp
[1] == '.')
4463 if (slashp
[2] == '.'
4464 && (slashp
[3] == '/' || slashp
[3] == '\0'))
4469 while (cp
>= res
&& !absolutefn (cp
));
4472 strcpy (cp
, slashp
+ 3);
4475 /* Under MSDOS and NT we get `d:/NAME' as absolute
4476 filename, so the luser could say `d:/../NAME'.
4477 We silently treat this as `d:/NAME'. */
4478 else if (cp
[1] == ':')
4479 strcpy (cp
+ 3, slashp
+ 4);
4481 else /* else (cp == res) */
4483 if (slashp
[3] != '\0')
4484 strcpy (cp
, slashp
+ 4);
4491 else if (slashp
[2] == '/' || slashp
[2] == '\0')
4493 strcpy (slashp
, slashp
+ 2);
4498 slashp
= etags_strchr (slashp
+ 1, '/');
4504 /* Return a newly allocated string containing the absolute
4505 filename of dir where FILE resides given CWD (which should
4506 end with a slash). */
4508 absolute_dirname (file
, cwd
)
4516 for (p
= file
; *p
!= '\0'; p
++)
4521 slashp
= etags_strrchr (file
, '/');
4526 res
= absolute_filename (file
, cwd
);
4532 /* Increase the size of a linebuffer. */
4534 grow_linebuffer (bufp
, toksize
)
4535 struct linebuffer
*bufp
;
4538 while (bufp
->size
< toksize
)
4540 bufp
->buffer
= (char *) xrealloc (bufp
->buffer
, bufp
->size
);
4543 /* Like malloc but get fatal error if memory is exhausted. */
4548 long *result
= (long *) malloc (size
);
4550 fatal ("virtual memory exhausted", NULL
);
4555 xrealloc (ptr
, size
)
4559 long *result
= (long *) realloc (ptr
, size
);
4561 fatal ("virtual memory exhausted", NULL
);