*** empty log message ***
[emacs.git] / lib-src / etags.c
blob60a67ff80f83c0f5a078c6a48ed4696cbf5f357d
1 /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
2 Copyright (C) 1984, 1987-1989, 1993-1995, 1998-2001
3 Free Software Foundation, Inc. and Ken Arnold
5 This file is not considered part of GNU Emacs.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 * Authors:
23 * Ctags originally by Ken Arnold.
24 * Fortran added by Jim Kleckner.
25 * Ed Pelegri-Llopart added C typedefs.
26 * Gnu Emacs TAGS format and modifications by RMS?
27 * 1989 Sam Kendall added C++.
28 * 1992 Joseph B. Wells improved C and C++ parsing.
29 * 1993 Francesco Potortì reorganised C and C++.
30 * 1994 Regexp tags by Tom Tromey.
31 * 2001 Nested classes by Francesco Potortì (ideas by Mykola Dzyuba).
33 * Francesco Potortì <pot@gnu.org> has maintained it since 1993.
36 char pot_etags_version[] = "@(#) pot revision number is 14.39";
38 #define TRUE 1
39 #define FALSE 0
41 #ifdef DEBUG
42 # undef DEBUG
43 # define DEBUG TRUE
44 #else
45 # define DEBUG FALSE
46 # define NDEBUG /* disable assert */
47 #endif
49 #ifdef HAVE_CONFIG_H
50 # include <config.h>
51 /* On some systems, Emacs defines static as nothing for the sake
52 of unexec. We don't want that here since we don't use unexec. */
53 # undef static
54 # define ETAGS_REGEXPS /* use the regexp features */
55 # define LONG_OPTIONS /* accept long options */
56 # ifndef PTR /* for Xemacs */
57 # define PTR void *
58 # endif
59 # ifndef __P /* for Xemacs */
60 # define __P(args) args
61 # endif
62 #else
63 # if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
64 # define __P(args) args /* use prototypes */
65 # define PTR void * /* for generic pointers */
66 # else
67 # define __P(args) () /* no prototypes */
68 # define const /* remove const for old compilers' sake */
69 # define PTR long * /* don't use void* */
70 # endif
71 #endif /* !HAVE_CONFIG_H */
73 #ifndef _GNU_SOURCE
74 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
75 #endif
77 /* WIN32_NATIVE is for Xemacs.
78 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
79 #ifdef WIN32_NATIVE
80 # undef MSDOS
81 # undef WINDOWSNT
82 # define WINDOWSNT
83 #endif /* WIN32_NATIVE */
85 #ifdef MSDOS
86 # undef MSDOS
87 # define MSDOS TRUE
88 # include <fcntl.h>
89 # include <sys/param.h>
90 # include <io.h>
91 # ifndef HAVE_CONFIG_H
92 # define DOS_NT
93 # include <sys/config.h>
94 # endif
95 #else
96 # define MSDOS FALSE
97 #endif /* MSDOS */
99 #ifdef WINDOWSNT
100 # include <stdlib.h>
101 # include <fcntl.h>
102 # include <string.h>
103 # include <direct.h>
104 # include <io.h>
105 # define MAXPATHLEN _MAX_PATH
106 # undef HAVE_NTGUI
107 # undef DOS_NT
108 # define DOS_NT
109 # ifndef HAVE_GETCWD
110 # define HAVE_GETCWD
111 # endif /* undef HAVE_GETCWD */
112 #else /* !WINDOWSNT */
113 # ifdef STDC_HEADERS
114 # include <stdlib.h>
115 # include <string.h>
116 # else
117 extern char *getenv ();
118 # endif
119 #endif /* !WINDOWSNT */
121 #ifdef HAVE_UNISTD_H
122 # include <unistd.h>
123 #else
124 # if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
125 extern char *getcwd (char *buf, size_t size);
126 # endif
127 #endif /* HAVE_UNISTD_H */
129 #include <stdio.h>
130 #include <ctype.h>
131 #include <errno.h>
132 #ifndef errno
133 extern int errno;
134 #endif
135 #include <sys/types.h>
136 #include <sys/stat.h>
138 #include <assert.h>
139 #ifdef NDEBUG
140 # undef assert /* some systems have a buggy assert.h */
141 # define assert(x) ((void) 0)
142 #endif
144 #if !defined (S_ISREG) && defined (S_IFREG)
145 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
146 #endif
148 #ifdef LONG_OPTIONS
149 # include <getopt.h>
150 #else
151 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
152 extern char *optarg;
153 extern int optind, opterr;
154 #endif /* LONG_OPTIONS */
156 #ifdef ETAGS_REGEXPS
157 # ifndef HAVE_CONFIG_H /* this is a standalone compilation */
158 # ifdef __CYGWIN__ /* compiling on Cygwin */
159 !!! NOTICE !!!
160 the regex.h distributed with Cygwin is not compatible with etags, alas!
161 If you want regular expression support, you should delete this notice and
162 arrange to use the GNU regex.h and regex.c.
163 # endif
164 # endif
165 # include <regex.h>
166 #endif /* ETAGS_REGEXPS */
168 /* Define CTAGS to make the program "ctags" compatible with the usual one.
169 Leave it undefined to make the program "etags", which makes emacs-style
170 tag tables and tags typedefs, #defines and struct/union/enum by default. */
171 #ifdef CTAGS
172 # undef CTAGS
173 # define CTAGS TRUE
174 #else
175 # define CTAGS FALSE
176 #endif
178 /* Exit codes for success and failure. */
179 #ifdef VMS
180 # define GOOD 1
181 # define BAD 0
182 #else
183 # define GOOD 0
184 # define BAD 1
185 #endif
187 #define streq(s,t) (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t))
188 #define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
190 #define CHARS 256 /* 2^sizeof(char) */
191 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
192 #define iswhite(c) (_wht[CHAR(c)]) /* c is white */
193 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name */
194 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token */
195 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token */
196 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens */
198 #define ISALNUM(c) isalnum (CHAR(c))
199 #define ISALPHA(c) isalpha (CHAR(c))
200 #define ISDIGIT(c) isdigit (CHAR(c))
201 #define ISLOWER(c) islower (CHAR(c))
203 #define lowcase(c) tolower (CHAR(c))
204 #define upcase(c) toupper (CHAR(c))
208 * xnew, xrnew -- allocate, reallocate storage
210 * SYNOPSIS: Type *xnew (int n, Type);
211 * void xrnew (OldPointer, int n, Type);
213 #if DEBUG
214 # include "chkmalloc.h"
215 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
216 (n) * sizeof (Type)))
217 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
218 (char *) (op), (n) * sizeof (Type)))
219 #else
220 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
221 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
222 (char *) (op), (n) * sizeof (Type)))
223 #endif
225 #define bool int
227 typedef void Lang_function __P((FILE *));
229 typedef struct
231 char *suffix;
232 char *command; /* Takes one arg and decompresses to stdout */
233 } compressor;
235 typedef struct
237 char *name;
238 Lang_function *function;
239 char **filenames;
240 char **suffixes;
241 char **interpreters;
242 } language;
244 typedef struct node_st
245 { /* sorting structure */
246 char *name; /* function or type name */
247 char *file; /* file name */
248 bool is_func; /* use pattern or line no */
249 bool been_warned; /* set if noticed dup */
250 int lno; /* line number tag is on */
251 long cno; /* character number line starts on */
252 char *pat; /* search pattern */
253 struct node_st *left, *right; /* left and right sons */
254 } node;
257 * A `linebuffer' is a structure which holds a line of text.
258 * `readline_internal' reads a line from a stream into a linebuffer
259 * and works regardless of the length of the line.
260 * SIZE is the size of BUFFER, LEN is the length of the string in
261 * BUFFER after readline reads it.
263 typedef struct
265 long size;
266 int len;
267 char *buffer;
268 } linebuffer;
270 /* Many compilers barf on this:
271 Lang_function Ada_funcs;
272 so let's write it this way */
273 static void Ada_funcs __P((FILE *));
274 static void Asm_labels __P((FILE *));
275 static void C_entries __P((int c_ext, FILE *));
276 static void default_C_entries __P((FILE *));
277 static void plain_C_entries __P((FILE *));
278 static void Cjava_entries __P((FILE *));
279 static void Cobol_paragraphs __P((FILE *));
280 static void Cplusplus_entries __P((FILE *));
281 static void Cstar_entries __P((FILE *));
282 static void Erlang_functions __P((FILE *));
283 static void Fortran_functions __P((FILE *));
284 static void Yacc_entries __P((FILE *));
285 static void Lisp_functions __P((FILE *));
286 static void Makefile_targets __P((FILE *));
287 static void Pascal_functions __P((FILE *));
288 static void Perl_functions __P((FILE *));
289 static void PHP_functions __P((FILE *));
290 static void Postscript_functions __P((FILE *));
291 static void Prolog_functions __P((FILE *));
292 static void Python_functions __P((FILE *));
293 static void Scheme_functions __P((FILE *));
294 static void TeX_commands __P((FILE *));
295 static void Texinfo_nodes __P((FILE *));
296 static void just_read_file __P((FILE *));
298 static void print_language_names __P((void));
299 static void print_version __P((void));
300 static void print_help __P((void));
301 int main __P((int, char **));
303 static compressor *get_compressor_from_suffix __P((char *, char **));
304 static language *get_language_from_langname __P((const char *));
305 static language *get_language_from_interpreter __P((char *));
306 static language *get_language_from_filename __P((char *));
307 static long readline __P((linebuffer *, FILE *));
308 static long readline_internal __P((linebuffer *, FILE *));
309 static bool nocase_tail __P((char *));
310 static char *get_tag __P((char *));
312 #ifdef ETAGS_REGEXPS
313 static void analyse_regex __P((char *, bool));
314 static void add_regex __P((char *, bool, language *));
315 static void free_patterns __P((void));
316 #endif /* ETAGS_REGEXPS */
317 static void error __P((const char *, const char *));
318 static void suggest_asking_for_help __P((void));
319 void fatal __P((char *, char *));
320 static void pfatal __P((char *));
321 static void add_node __P((node *, node **));
323 static void init __P((void));
324 static void initbuffer __P((linebuffer *));
325 static void find_entries __P((char *, FILE *));
326 static void free_tree __P((node *));
327 static void pfnote __P((char *, bool, char *, int, int, long));
328 static void new_pfnote __P((char *, int, bool, char *, int, int, long));
329 static void process_file __P((char *));
330 static void put_entries __P((node *));
332 static char *concat __P((char *, char *, char *));
333 static char *skip_spaces __P((char *));
334 static char *skip_non_spaces __P((char *));
335 static char *savenstr __P((char *, int));
336 static char *savestr __P((char *));
337 static char *etags_strchr __P((const char *, int));
338 static char *etags_strrchr __P((const char *, int));
339 static char *etags_getcwd __P((void));
340 static char *relative_filename __P((char *, char *));
341 static char *absolute_filename __P((char *, char *));
342 static char *absolute_dirname __P((char *, char *));
343 static bool filename_is_absolute __P((char *f));
344 static void canonicalize_filename __P((char *));
345 static void linebuffer_setlen __P((linebuffer *, int));
346 static PTR xmalloc __P((unsigned int));
347 static PTR xrealloc __P((char *, unsigned int));
350 static char searchar = '/'; /* use /.../ searches */
352 static char *tagfile; /* output file */
353 static char *progname; /* name this program was invoked with */
354 static char *cwd; /* current working directory */
355 static char *tagfiledir; /* directory of tagfile */
356 static FILE *tagf; /* ioptr for tags file */
358 static char *curfile; /* current input file name */
359 static language *curlang; /* current language */
361 static int lineno; /* line number of current line */
362 static long charno; /* current character number */
363 static long linecharno; /* charno of start of current line */
364 static char *dbp; /* pointer to start of current tag */
365 static bool nocharno; /* only use line number when making tag */
366 static const int invalidcharno = -1;
368 static node *head; /* the head of the binary tree of tags */
370 static linebuffer lb; /* the current line */
372 /* boolean "functions" (see init) */
373 static bool _wht[CHARS], _nin[CHARS], _itk[CHARS], _btk[CHARS], _etk[CHARS];
374 static char
375 /* white chars */
376 *white = " \f\t\n\r\v",
377 /* not in a name */
378 *nonam = " \f\t\n\r()=,;",
379 /* token ending chars */
380 *endtk = " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
381 /* token starting chars */
382 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
383 /* valid in-token chars */
384 *midtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
386 static bool append_to_tagfile; /* -a: append to tags */
387 /* The following four default to TRUE for etags, but to FALSE for ctags. */
388 static bool typedefs; /* -t: create tags for C and Ada typedefs */
389 static bool typedefs_or_cplusplus; /* -T: create tags for C typedefs, level */
390 /* 0 struct/enum/union decls, and C++ */
391 /* member functions. */
392 static bool constantypedefs; /* -d: create tags for C #define, enum */
393 /* constants and variables. */
394 /* -D: opposite of -d. Default under ctags. */
395 static bool declarations; /* --declarations: tag them and extern in C&Co*/
396 static bool globals; /* create tags for global variables */
397 static bool no_line_directive; /* ignore #line directives */
398 static bool members; /* create tags for C member variables */
399 static bool update; /* -u: update tags */
400 static bool vgrind_style; /* -v: create vgrind style index output */
401 static bool no_warnings; /* -w: suppress warnings */
402 static bool cxref_style; /* -x: create cxref style output */
403 static bool cplusplus; /* .[hc] means C++, not C */
404 static bool noindentypedefs; /* -I: ignore indentation in C */
405 static bool packages_only; /* --packages-only: in Ada, only tag packages*/
407 #ifdef LONG_OPTIONS
408 static struct option longopts[] =
410 { "packages-only", no_argument, &packages_only, TRUE },
411 { "append", no_argument, NULL, 'a' },
412 { "backward-search", no_argument, NULL, 'B' },
413 { "c++", no_argument, NULL, 'C' },
414 { "cxref", no_argument, NULL, 'x' },
415 { "defines", no_argument, NULL, 'd' },
416 { "declarations", no_argument, &declarations, TRUE },
417 { "no-defines", no_argument, NULL, 'D' },
418 { "globals", no_argument, &globals, TRUE },
419 { "no-globals", no_argument, &globals, FALSE },
420 { "no-line-directive", no_argument, &no_line_directive, TRUE },
421 { "help", no_argument, NULL, 'h' },
422 { "help", no_argument, NULL, 'H' },
423 { "ignore-indentation", no_argument, NULL, 'I' },
424 { "include", required_argument, NULL, 'i' },
425 { "language", required_argument, NULL, 'l' },
426 { "members", no_argument, &members, TRUE },
427 { "no-members", no_argument, &members, FALSE },
428 { "no-warn", no_argument, NULL, 'w' },
429 { "output", required_argument, NULL, 'o' },
430 #ifdef ETAGS_REGEXPS
431 { "regex", required_argument, NULL, 'r' },
432 { "no-regex", no_argument, NULL, 'R' },
433 { "ignore-case-regex", required_argument, NULL, 'c' },
434 #endif /* ETAGS_REGEXPS */
435 { "typedefs", no_argument, NULL, 't' },
436 { "typedefs-and-c++", no_argument, NULL, 'T' },
437 { "update", no_argument, NULL, 'u' },
438 { "version", no_argument, NULL, 'V' },
439 { "vgrind", no_argument, NULL, 'v' },
440 { NULL }
442 #endif /* LONG_OPTIONS */
444 #ifdef ETAGS_REGEXPS
445 /* Structure defining a regular expression. Elements are
446 the compiled pattern, and the name string. */
447 typedef struct pattern
449 struct pattern *p_next;
450 language *lang;
451 char *regex;
452 struct re_pattern_buffer *pat;
453 struct re_registers regs;
454 char *name_pattern;
455 bool error_signaled;
456 } pattern;
458 /* List of all regexps. */
459 static pattern *p_head = NULL;
461 /* How many characters in the character set. (From regex.c.) */
462 #define CHAR_SET_SIZE 256
463 /* Translation table for case-insensitive matching. */
464 static char lc_trans[CHAR_SET_SIZE];
465 #endif /* ETAGS_REGEXPS */
467 static compressor compressors[] =
469 { "z", "gzip -d -c"},
470 { "Z", "gzip -d -c"},
471 { "gz", "gzip -d -c"},
472 { "GZ", "gzip -d -c"},
473 { "bz2", "bzip2 -d -c" },
474 { NULL }
478 * Language stuff.
481 /* Non-NULL if language fixed. */
482 static language *forced_lang = NULL;
484 /* Ada code */
485 static char *Ada_suffixes [] =
486 { "ads", "adb", "ada", NULL };
488 /* Assembly code */
489 static char *Asm_suffixes [] =
490 { "a", /* Unix assembler */
491 "asm", /* Microcontroller assembly */
492 "def", /* BSO/Tasking definition includes */
493 "inc", /* Microcontroller include files */
494 "ins", /* Microcontroller include files */
495 "s", "sa", /* Unix assembler */
496 "S", /* cpp-processed Unix assembler */
497 "src", /* BSO/Tasking C compiler output */
498 NULL
501 /* Note that .c and .h can be considered C++, if the --c++ flag was
502 given, or if the `class' keyowrd is met inside the file.
503 That is why default_C_entries is called for these. */
504 static char *default_C_suffixes [] =
505 { "c", "h", NULL };
507 static char *Cplusplus_suffixes [] =
508 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
509 "M", /* Objective C++ */
510 "pdb", /* Postscript with C syntax */
511 NULL };
513 static char *Cjava_suffixes [] =
514 { "java", NULL };
516 static char *Cobol_suffixes [] =
517 { "COB", "cob", NULL };
519 static char *Cstar_suffixes [] =
520 { "cs", "hs", NULL };
522 static char *Erlang_suffixes [] =
523 { "erl", "hrl", NULL };
525 static char *Fortran_suffixes [] =
526 { "F", "f", "f90", "for", NULL };
528 static char *Lisp_suffixes [] =
529 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL };
531 static char *Makefile_filenames [] =
532 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL};
534 static char *Pascal_suffixes [] =
535 { "p", "pas", NULL };
537 static char *Perl_suffixes [] =
538 { "pl", "pm", NULL };
540 static char *Perl_interpreters [] =
541 { "perl", "@PERL@", NULL };
543 static char *PHP_suffixes [] =
544 { "php", "php3", "php4", NULL };
546 static char *plain_C_suffixes [] =
547 { "lm", /* Objective lex file */
548 "m", /* Objective C file */
549 "pc", /* Pro*C file */
550 NULL };
552 static char *Postscript_suffixes [] =
553 { "ps", "psw", NULL }; /* .psw is for PSWrap */
555 static char *Prolog_suffixes [] =
556 { "prolog", NULL };
558 static char *Python_suffixes [] =
559 { "py", NULL };
561 /* Can't do the `SCM' or `scm' prefix with a version number. */
562 static char *Scheme_suffixes [] =
563 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL };
565 static char *TeX_suffixes [] =
566 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL };
568 static char *Texinfo_suffixes [] =
569 { "texi", "texinfo", "txi", NULL };
571 static char *Yacc_suffixes [] =
572 { "y", "y++", "ym", "yxx", "yy", NULL }; /* .ym is Objective yacc file */
575 * Table of languages.
577 * It is ok for a given function to be listed under more than one
578 * name. I just didn't.
581 static language lang_names [] =
583 { "ada", Ada_funcs, NULL, Ada_suffixes, NULL },
584 { "asm", Asm_labels, NULL, Asm_suffixes, NULL },
585 { "c", default_C_entries, NULL, default_C_suffixes, NULL },
586 { "c++", Cplusplus_entries, NULL, Cplusplus_suffixes, NULL },
587 { "c*", Cstar_entries, NULL, Cstar_suffixes, NULL },
588 { "cobol", Cobol_paragraphs, NULL, Cobol_suffixes, NULL },
589 { "erlang", Erlang_functions, NULL, Erlang_suffixes, NULL },
590 { "fortran", Fortran_functions, NULL, Fortran_suffixes, NULL },
591 { "java", Cjava_entries, NULL, Cjava_suffixes, NULL },
592 { "lisp", Lisp_functions, NULL, Lisp_suffixes, NULL },
593 { "makefile", Makefile_targets, Makefile_filenames, NULL, NULL },
594 { "pascal", Pascal_functions, NULL, Pascal_suffixes, NULL },
595 { "perl", Perl_functions, NULL, Perl_suffixes, Perl_interpreters },
596 { "php", PHP_functions, NULL, PHP_suffixes, NULL },
597 { "postscript", Postscript_functions, NULL, Postscript_suffixes, NULL },
598 { "proc", plain_C_entries, NULL, plain_C_suffixes, NULL },
599 { "prolog", Prolog_functions, NULL, Prolog_suffixes, NULL },
600 { "python", Python_functions, NULL, Python_suffixes, NULL },
601 { "scheme", Scheme_functions, NULL, Scheme_suffixes, NULL },
602 { "tex", TeX_commands, NULL, TeX_suffixes, NULL },
603 { "texinfo", Texinfo_nodes, NULL, Texinfo_suffixes, NULL },
604 { "yacc", Yacc_entries, NULL, Yacc_suffixes, NULL },
605 { "auto", NULL }, /* default guessing scheme */
606 { "none", just_read_file }, /* regexp matching only */
607 { NULL, NULL } /* end of list */
611 static void
612 print_language_names ()
614 language *lang;
615 char **name, **ext;
617 puts ("\nThese are the currently supported languages, along with the\n\
618 default file names and dot suffixes:");
619 for (lang = lang_names; lang->name != NULL; lang++)
621 printf (" %-*s", 10, lang->name);
622 if (lang->filenames != NULL)
623 for (name = lang->filenames; *name != NULL; name++)
624 printf (" %s", *name);
625 if (lang->suffixes != NULL)
626 for (ext = lang->suffixes; *ext != NULL; ext++)
627 printf (" .%s", *ext);
628 puts ("");
630 puts ("Where `auto' means use default language for files based on file\n\
631 name suffix, and `none' means only do regexp processing on files.\n\
632 If no language is specified and no matching suffix is found,\n\
633 the first line of the file is read for a sharp-bang (#!) sequence\n\
634 followed by the name of an interpreter. If no such sequence is found,\n\
635 Fortran is tried first; if no tags are found, C is tried next.\n\
636 When parsing any C file, a \"class\" keyword switches to C++.\n\
637 Compressed files are supported using gzip and bzip2.");
640 #ifndef EMACS_NAME
641 # define EMACS_NAME "GNU Emacs"
642 #endif
643 #ifndef VERSION
644 # define VERSION "21"
645 #endif
646 static void
647 print_version ()
649 printf ("%s (%s %s)\n", (CTAGS) ? "ctags" : "etags", EMACS_NAME, VERSION);
650 puts ("Copyright (C) 1999 Free Software Foundation, Inc. and Ken Arnold");
651 puts ("This program is distributed under the same terms as Emacs");
653 exit (GOOD);
656 static void
657 print_help ()
659 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
661 These are the options accepted by %s.\n", progname, progname);
662 #ifdef LONG_OPTIONS
663 puts ("You may use unambiguous abbreviations for the long option names.");
664 #else
665 puts ("Long option names do not work with this executable, as it is not\n\
666 linked with GNU getopt.");
667 #endif /* LONG_OPTIONS */
668 puts ("A - as file name means read names from stdin (one per line).");
669 if (!CTAGS)
670 printf (" Absolute names are stored in the output file as they are.\n\
671 Relative ones are stored relative to the output file's directory.");
672 puts ("\n");
674 puts ("-a, --append\n\
675 Append tag entries to existing tags file.");
677 puts ("--packages-only\n\
678 For Ada files, only generate tags for packages .");
680 if (CTAGS)
681 puts ("-B, --backward-search\n\
682 Write the search commands for the tag entries using '?', the\n\
683 backward-search command instead of '/', the forward-search command.");
685 /* This option is mostly obsolete, because etags can now automatically
686 detect C++. Retained for backward compatibility and for debugging and
687 experimentation. In principle, we could want to tag as C++ even
688 before any "class" keyword.
689 puts ("-C, --c++\n\
690 Treat files whose name suffix defaults to C language as C++ files.");
693 puts ("--declarations\n\
694 In C and derived languages, create tags for function declarations,");
695 if (CTAGS)
696 puts ("\tand create tags for extern variables if --globals is used.");
697 else
698 puts
699 ("\tand create tags for extern variables unless --no-globals is used.");
701 if (CTAGS)
702 puts ("-d, --defines\n\
703 Create tag entries for C #define constants and enum constants, too.");
704 else
705 puts ("-D, --no-defines\n\
706 Don't create tag entries for C #define constants and enum constants.\n\
707 This makes the tags file smaller.");
709 if (!CTAGS)
711 puts ("-i FILE, --include=FILE\n\
712 Include a note in tag file indicating that, when searching for\n\
713 a tag, one should also consult the tags file FILE after\n\
714 checking the current file.");
715 puts ("-l LANG, --language=LANG\n\
716 Force the following files to be considered as written in the\n\
717 named language up to the next --language=LANG option.");
720 if (CTAGS)
721 puts ("--globals\n\
722 Create tag entries for global variables in some languages.");
723 else
724 puts ("--no-globals\n\
725 Do not create tag entries for global variables in some\n\
726 languages. This makes the tags file smaller.");
727 puts ("--members\n\
728 Create tag entries for member variables in C and derived languages.");
730 #ifdef ETAGS_REGEXPS
731 puts ("-r /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
732 Make a tag for each line matching pattern REGEXP in the following\n\
733 files. {LANGUAGE}/REGEXP/ uses REGEXP for LANGUAGE files only.\n\
734 regexfile is a file containing one REGEXP per line.\n\
735 REGEXP is anchored (as if preceded by ^).\n\
736 The form /REGEXP/NAME/ creates a named tag.\n\
737 For example Tcl named tags can be created with:\n\
738 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\"");
739 puts ("-c /REGEXP/, --ignore-case-regex=/REGEXP/ or --ignore-case-regex=@regexfile\n\
740 Like -r, --regex but ignore case when matching expressions.");
741 puts ("-R, --no-regex\n\
742 Don't create tags from regexps for the following files.");
743 #endif /* ETAGS_REGEXPS */
744 puts ("-o FILE, --output=FILE\n\
745 Write the tags to FILE.");
746 puts ("-I, --ignore-indentation\n\
747 Don't rely on indentation quite as much as normal. Currently,\n\
748 this means not to assume that a closing brace in the first\n\
749 column is the final brace of a function or structure\n\
750 definition in C and C++.");
752 if (CTAGS)
754 puts ("-t, --typedefs\n\
755 Generate tag entries for C and Ada typedefs.");
756 puts ("-T, --typedefs-and-c++\n\
757 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
758 and C++ member functions.");
759 puts ("-u, --update\n\
760 Update the tag entries for the given files, leaving tag\n\
761 entries for other files in place. Currently, this is\n\
762 implemented by deleting the existing entries for the given\n\
763 files and then rewriting the new entries at the end of the\n\
764 tags file. It is often faster to simply rebuild the entire\n\
765 tag file than to use this.");
766 puts ("-v, --vgrind\n\
767 Generates an index of items intended for human consumption,\n\
768 similar to the output of vgrind. The index is sorted, and\n\
769 gives the page number of each item.");
770 puts ("-w, --no-warn\n\
771 Suppress warning messages about entries defined in multiple\n\
772 files.");
773 puts ("-x, --cxref\n\
774 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
775 The output uses line numbers instead of page numbers, but\n\
776 beyond that the differences are cosmetic; try both to see\n\
777 which you like.");
780 puts ("-V, --version\n\
781 Print the version of the program.\n\
782 -h, --help\n\
783 Print this help message.");
785 print_language_names ();
787 puts ("");
788 puts ("Report bugs to bug-gnu-emacs@gnu.org");
790 exit (GOOD);
794 enum argument_type
796 at_language,
797 at_regexp,
798 at_filename,
799 at_icregexp
802 /* This structure helps us allow mixing of --lang and file names. */
803 typedef struct
805 enum argument_type arg_type;
806 char *what;
807 language *lang; /* language of the regexp */
808 } argument;
810 #ifdef VMS /* VMS specific functions */
812 #define EOS '\0'
814 /* This is a BUG! ANY arbitrary limit is a BUG!
815 Won't someone please fix this? */
816 #define MAX_FILE_SPEC_LEN 255
817 typedef struct {
818 short curlen;
819 char body[MAX_FILE_SPEC_LEN + 1];
820 } vspec;
823 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
824 returning in each successive call the next file name matching the input
825 spec. The function expects that each in_spec passed
826 to it will be processed to completion; in particular, up to and
827 including the call following that in which the last matching name
828 is returned, the function ignores the value of in_spec, and will
829 only start processing a new spec with the following call.
830 If an error occurs, on return out_spec contains the value
831 of in_spec when the error occurred.
833 With each successive file name returned in out_spec, the
834 function's return value is one. When there are no more matching
835 names the function returns zero. If on the first call no file
836 matches in_spec, or there is any other error, -1 is returned.
839 #include <rmsdef.h>
840 #include <descrip.h>
841 #define OUTSIZE MAX_FILE_SPEC_LEN
842 static short
843 fn_exp (out, in)
844 vspec *out;
845 char *in;
847 static long context = 0;
848 static struct dsc$descriptor_s o;
849 static struct dsc$descriptor_s i;
850 static bool pass1 = TRUE;
851 long status;
852 short retval;
854 if (pass1)
856 pass1 = FALSE;
857 o.dsc$a_pointer = (char *) out;
858 o.dsc$w_length = (short)OUTSIZE;
859 i.dsc$a_pointer = in;
860 i.dsc$w_length = (short)strlen(in);
861 i.dsc$b_dtype = DSC$K_DTYPE_T;
862 i.dsc$b_class = DSC$K_CLASS_S;
863 o.dsc$b_dtype = DSC$K_DTYPE_VT;
864 o.dsc$b_class = DSC$K_CLASS_VS;
866 if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL)
868 out->body[out->curlen] = EOS;
869 return 1;
871 else if (status == RMS$_NMF)
872 retval = 0;
873 else
875 strcpy(out->body, in);
876 retval = -1;
878 lib$find_file_end(&context);
879 pass1 = TRUE;
880 return retval;
884 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
885 name of each file specified by the provided arg expanding wildcards.
887 static char *
888 gfnames (arg, p_error)
889 char *arg;
890 bool *p_error;
892 static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
894 switch (fn_exp (&filename, arg))
896 case 1:
897 *p_error = FALSE;
898 return filename.body;
899 case 0:
900 *p_error = FALSE;
901 return NULL;
902 default:
903 *p_error = TRUE;
904 return filename.body;
908 #ifndef OLD /* Newer versions of VMS do provide `system'. */
909 system (cmd)
910 char *cmd;
912 error ("%s", "system() function not implemented under VMS");
914 #endif
916 #define VERSION_DELIM ';'
917 char *massage_name (s)
918 char *s;
920 char *start = s;
922 for ( ; *s; s++)
923 if (*s == VERSION_DELIM)
925 *s = EOS;
926 break;
928 else
929 *s = lowcase (*s);
930 return start;
932 #endif /* VMS */
936 main (argc, argv)
937 int argc;
938 char *argv[];
940 int i;
941 unsigned int nincluded_files;
942 char **included_files;
943 char *this_file;
944 argument *argbuffer;
945 int current_arg, file_count;
946 linebuffer filename_lb;
947 #ifdef VMS
948 bool got_err;
949 #endif
951 #ifdef DOS_NT
952 _fmode = O_BINARY; /* all of files are treated as binary files */
953 #endif /* DOS_NT */
955 progname = argv[0];
956 nincluded_files = 0;
957 included_files = xnew (argc, char *);
958 current_arg = 0;
959 file_count = 0;
961 /* Allocate enough no matter what happens. Overkill, but each one
962 is small. */
963 argbuffer = xnew (argc, argument);
965 #ifdef ETAGS_REGEXPS
966 /* Set syntax for regular expression routines. */
967 re_set_syntax (RE_SYNTAX_EMACS | RE_INTERVALS);
968 /* Translation table for case-insensitive search. */
969 for (i = 0; i < CHAR_SET_SIZE; i++)
970 lc_trans[i] = lowcase (i);
971 #endif /* ETAGS_REGEXPS */
974 * If etags, always find typedefs and structure tags. Why not?
975 * Also default to find macro constants, enum constants and
976 * global variables.
978 if (!CTAGS)
980 typedefs = typedefs_or_cplusplus = constantypedefs = TRUE;
981 globals = TRUE;
982 declarations = FALSE;
983 members = FALSE;
986 while (1)
988 int opt;
989 char *optstring;
991 #ifdef ETAGS_REGEXPS
992 optstring = "-aCdDf:Il:o:r:c:RStTi:BuvxwVhH";
993 #else
994 optstring = "-aCdDf:Il:o:StTi:BuvxwVhH";
995 #endif /* ETAGS_REGEXPS */
997 #ifndef LONG_OPTIONS
998 optstring = optstring + 1;
999 #endif /* LONG_OPTIONS */
1001 opt = getopt_long (argc, argv, optstring, longopts, 0);
1002 if (opt == EOF)
1003 break;
1005 switch (opt)
1007 case 0:
1008 /* If getopt returns 0, then it has already processed a
1009 long-named option. We should do nothing. */
1010 break;
1012 case 1:
1013 /* This means that a file name has been seen. Record it. */
1014 argbuffer[current_arg].arg_type = at_filename;
1015 argbuffer[current_arg].what = optarg;
1016 ++current_arg;
1017 ++file_count;
1018 break;
1020 /* Common options. */
1021 case 'a': append_to_tagfile = TRUE; break;
1022 case 'C': cplusplus = TRUE; break;
1023 case 'd': constantypedefs = TRUE; break;
1024 case 'D': constantypedefs = FALSE; break;
1025 case 'f': /* for compatibility with old makefiles */
1026 case 'o':
1027 if (tagfile)
1029 error ("-o option may only be given once.", (char *)NULL);
1030 suggest_asking_for_help ();
1032 tagfile = optarg;
1033 break;
1034 case 'I':
1035 case 'S': /* for backward compatibility */
1036 noindentypedefs = TRUE;
1037 break;
1038 case 'l':
1040 language *lang = get_language_from_langname (optarg);
1041 if (lang != NULL)
1043 argbuffer[current_arg].lang = lang;
1044 argbuffer[current_arg].arg_type = at_language;
1045 ++current_arg;
1048 break;
1049 case 'r':
1050 argbuffer[current_arg].arg_type = at_regexp;
1051 argbuffer[current_arg].what = optarg;
1052 ++current_arg;
1053 break;
1054 case 'R':
1055 argbuffer[current_arg].arg_type = at_regexp;
1056 argbuffer[current_arg].what = NULL;
1057 ++current_arg;
1058 break;
1059 case 'c':
1060 argbuffer[current_arg].arg_type = at_icregexp;
1061 argbuffer[current_arg].what = optarg;
1062 ++current_arg;
1063 break;
1064 case 'V':
1065 print_version ();
1066 break;
1067 case 'h':
1068 case 'H':
1069 print_help ();
1070 break;
1071 case 't':
1072 typedefs = TRUE;
1073 break;
1074 case 'T':
1075 typedefs = typedefs_or_cplusplus = TRUE;
1076 break;
1077 /* Etags options */
1078 case 'i':
1079 included_files[nincluded_files++] = optarg;
1080 break;
1081 /* Ctags options. */
1082 case 'B': searchar = '?'; break;
1083 case 'u': update = TRUE; break;
1084 case 'v': vgrind_style = TRUE; /*FALLTHRU*/
1085 case 'x': cxref_style = TRUE; break;
1086 case 'w': no_warnings = TRUE; break;
1087 default:
1088 suggest_asking_for_help ();
1092 for (; optind < argc; ++optind)
1094 argbuffer[current_arg].arg_type = at_filename;
1095 argbuffer[current_arg].what = argv[optind];
1096 ++current_arg;
1097 ++file_count;
1100 if (nincluded_files == 0 && file_count == 0)
1102 error ("no input files specified.", (char *)NULL);
1103 suggest_asking_for_help ();
1106 if (tagfile == NULL)
1107 tagfile = CTAGS ? "tags" : "TAGS";
1108 cwd = etags_getcwd (); /* the current working directory */
1109 if (cwd[strlen (cwd) - 1] != '/')
1111 char *oldcwd = cwd;
1112 cwd = concat (oldcwd, "/", "");
1113 free (oldcwd);
1115 if (streq (tagfile, "-"))
1116 tagfiledir = cwd;
1117 else
1118 tagfiledir = absolute_dirname (tagfile, cwd);
1120 init (); /* set up boolean "functions" */
1122 initbuffer (&lb);
1123 initbuffer (&filename_lb);
1125 if (!CTAGS)
1127 if (streq (tagfile, "-"))
1129 tagf = stdout;
1130 #ifdef DOS_NT
1131 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1132 doesn't take effect until after `stdout' is already open). */
1133 if (!isatty (fileno (stdout)))
1134 setmode (fileno (stdout), O_BINARY);
1135 #endif /* DOS_NT */
1137 else
1138 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
1139 if (tagf == NULL)
1140 pfatal (tagfile);
1144 * Loop through files finding functions.
1146 for (i = 0; i < current_arg; ++i)
1148 switch (argbuffer[i].arg_type)
1150 case at_language:
1151 forced_lang = argbuffer[i].lang;
1152 break;
1153 #ifdef ETAGS_REGEXPS
1154 case at_regexp:
1155 analyse_regex (argbuffer[i].what, FALSE);
1156 break;
1157 case at_icregexp:
1158 analyse_regex (argbuffer[i].what, TRUE);
1159 break;
1160 #endif
1161 case at_filename:
1162 #ifdef VMS
1163 while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL)
1165 if (got_err)
1167 error ("can't find file %s\n", this_file);
1168 argc--, argv++;
1170 else
1172 this_file = massage_name (this_file);
1174 #else
1175 this_file = argbuffer[i].what;
1176 #endif
1177 /* Input file named "-" means read file names from stdin
1178 (one per line) and use them. */
1179 if (streq (this_file, "-"))
1180 while (readline_internal (&filename_lb, stdin) > 0)
1181 process_file (filename_lb.buffer);
1182 else
1183 process_file (this_file);
1184 #ifdef VMS
1186 #endif
1187 break;
1191 #ifdef ETAGS_REGEXPS
1192 free_patterns ();
1193 #endif /* ETAGS_REGEXPS */
1195 if (!CTAGS || cxref_style)
1197 put_entries (head);
1198 free_tree (head);
1199 head = NULL;
1200 if (!CTAGS)
1201 while (nincluded_files-- > 0)
1202 fprintf (tagf, "\f\n%s,include\n", *included_files++);
1204 if (fclose (tagf) == EOF)
1205 pfatal (tagfile);
1206 exit (GOOD);
1209 if (update)
1211 char cmd[BUFSIZ];
1212 for (i = 0; i < current_arg; ++i)
1214 if (argbuffer[i].arg_type != at_filename)
1215 continue;
1216 sprintf (cmd,
1217 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1218 tagfile, argbuffer[i].what, tagfile);
1219 if (system (cmd) != GOOD)
1220 fatal ("failed to execute shell command", (char *)NULL);
1222 append_to_tagfile = TRUE;
1225 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
1226 if (tagf == NULL)
1227 pfatal (tagfile);
1228 put_entries (head);
1229 free_tree (head);
1230 head = NULL;
1231 if (fclose (tagf) == EOF)
1232 pfatal (tagfile);
1234 if (update)
1236 char cmd[BUFSIZ];
1237 sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
1238 exit (system (cmd));
1240 return GOOD;
1246 * Return a compressor given the file name. If EXTPTR is non-zero,
1247 * return a pointer into FILE where the compressor-specific
1248 * extension begins. If no compressor is found, NULL is returned
1249 * and EXTPTR is not significant.
1250 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1252 static compressor *
1253 get_compressor_from_suffix (file, extptr)
1254 char *file;
1255 char **extptr;
1257 compressor *compr;
1258 char *slash, *suffix;
1260 /* This relies on FN to be after canonicalize_filename,
1261 so we don't need to consider backslashes on DOS_NT. */
1262 slash = etags_strrchr (file, '/');
1263 suffix = etags_strrchr (file, '.');
1264 if (suffix == NULL || suffix < slash)
1265 return NULL;
1266 if (extptr != NULL)
1267 *extptr = suffix;
1268 suffix += 1;
1269 /* Let those poor souls who live with DOS 8+3 file name limits get
1270 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1271 Only the first do loop is run if not MSDOS */
1274 for (compr = compressors; compr->suffix != NULL; compr++)
1275 if (streq (compr->suffix, suffix))
1276 return compr;
1277 if (!MSDOS)
1278 break; /* do it only once: not really a loop */
1279 if (extptr != NULL)
1280 *extptr = ++suffix;
1281 } while (*suffix != '\0');
1282 return NULL;
1288 * Return a language given the name.
1290 static language *
1291 get_language_from_langname (name)
1292 const char *name;
1294 language *lang;
1296 if (name == NULL)
1297 error ("empty language name", (char *)NULL);
1298 else
1300 for (lang = lang_names; lang->name != NULL; lang++)
1301 if (streq (name, lang->name))
1302 return lang;
1303 error ("unknown language \"%s\"", name);
1306 return NULL;
1311 * Return a language given the interpreter name.
1313 static language *
1314 get_language_from_interpreter (interpreter)
1315 char *interpreter;
1317 language *lang;
1318 char **iname;
1320 if (interpreter == NULL)
1321 return NULL;
1322 for (lang = lang_names; lang->name != NULL; lang++)
1323 if (lang->interpreters != NULL)
1324 for (iname = lang->interpreters; *iname != NULL; iname++)
1325 if (streq (*iname, interpreter))
1326 return lang;
1328 return NULL;
1334 * Return a language given the file name.
1336 static language *
1337 get_language_from_filename (file)
1338 char *file;
1340 language *lang;
1341 char **name, **ext, *suffix;
1343 /* Try whole file name first. */
1344 for (lang = lang_names; lang->name != NULL; lang++)
1345 if (lang->filenames != NULL)
1346 for (name = lang->filenames; *name != NULL; name++)
1347 if (streq (*name, file))
1348 return lang;
1350 /* If not found, try suffix after last dot. */
1351 suffix = etags_strrchr (file, '.');
1352 if (suffix == NULL)
1353 return NULL;
1354 suffix += 1;
1355 for (lang = lang_names; lang->name != NULL; lang++)
1356 if (lang->suffixes != NULL)
1357 for (ext = lang->suffixes; *ext != NULL; ext++)
1358 if (streq (*ext, suffix))
1359 return lang;
1360 return NULL;
1366 * This routine is called on each file argument.
1368 static void
1369 process_file (file)
1370 char *file;
1372 struct stat stat_buf;
1373 FILE *inf;
1374 compressor *compr;
1375 char *compressed_name, *uncompressed_name;
1376 char *ext, *real_name;
1377 int retval;
1380 canonicalize_filename (file);
1381 if (streq (file, tagfile) && !streq (tagfile, "-"))
1383 error ("skipping inclusion of %s in self.", file);
1384 return;
1386 if ((compr = get_compressor_from_suffix (file, &ext)) == NULL)
1388 compressed_name = NULL;
1389 real_name = uncompressed_name = savestr (file);
1391 else
1393 real_name = compressed_name = savestr (file);
1394 uncompressed_name = savenstr (file, ext - file);
1397 /* If the canonicalised uncompressed name has already be dealt with,
1398 skip it silently, else add it to the list. */
1400 typedef struct processed_file
1402 char *filename;
1403 struct processed_file *next;
1404 } processed_file;
1405 static processed_file *pf_head = NULL;
1406 register processed_file *fnp;
1408 for (fnp = pf_head; fnp != NULL; fnp = fnp->next)
1409 if (streq (uncompressed_name, fnp->filename))
1410 goto exit;
1411 fnp = pf_head;
1412 pf_head = xnew (1, struct processed_file);
1413 pf_head->filename = savestr (uncompressed_name);
1414 pf_head->next = fnp;
1417 if (stat (real_name, &stat_buf) != 0)
1419 /* Reset real_name and try with a different name. */
1420 real_name = NULL;
1421 if (compressed_name != NULL) /* try with the given suffix */
1423 if (stat (uncompressed_name, &stat_buf) == 0)
1424 real_name = uncompressed_name;
1426 else /* try all possible suffixes */
1428 for (compr = compressors; compr->suffix != NULL; compr++)
1430 compressed_name = concat (file, ".", compr->suffix);
1431 if (stat (compressed_name, &stat_buf) != 0)
1433 if (MSDOS)
1435 char *suf = compressed_name + strlen (file);
1436 size_t suflen = strlen (compr->suffix) + 1;
1437 for ( ; suf[1]; suf++, suflen--)
1439 memmove (suf, suf + 1, suflen);
1440 if (stat (compressed_name, &stat_buf) == 0)
1442 real_name = compressed_name;
1443 break;
1446 if (real_name != NULL)
1447 break;
1448 } /* MSDOS */
1449 free (compressed_name);
1450 compressed_name = NULL;
1452 else
1454 real_name = compressed_name;
1455 break;
1459 if (real_name == NULL)
1461 perror (file);
1462 goto exit;
1464 } /* try with a different name */
1466 if (!S_ISREG (stat_buf.st_mode))
1468 error ("skipping %s: it is not a regular file.", real_name);
1469 goto exit;
1471 if (real_name == compressed_name)
1473 char *cmd = concat (compr->command, " ", real_name);
1474 inf = (FILE *) popen (cmd, "r");
1475 free (cmd);
1477 else
1478 inf = fopen (real_name, "r");
1479 if (inf == NULL)
1481 perror (real_name);
1482 goto exit;
1485 if (filename_is_absolute (uncompressed_name))
1487 /* file is an absolute file name. Canonicalise it. */
1488 curfile = absolute_filename (uncompressed_name, cwd);
1490 else
1492 /* file is a file name relative to cwd. Make it relative
1493 to the directory of the tags file. */
1494 curfile = relative_filename (uncompressed_name, tagfiledir);
1496 nocharno = FALSE; /* use char position when making tags */
1497 find_entries (uncompressed_name, inf);
1499 if (real_name == compressed_name)
1500 retval = pclose (inf);
1501 else
1502 retval = fclose (inf);
1503 if (retval < 0)
1504 pfatal (file);
1506 exit:
1507 if (compressed_name) free(compressed_name);
1508 if (uncompressed_name) free(uncompressed_name);
1509 return;
1513 * This routine sets up the boolean pseudo-functions which work
1514 * by setting boolean flags dependent upon the corresponding character.
1515 * Every char which is NOT in that string is not a white char. Therefore,
1516 * all of the array "_wht" is set to FALSE, and then the elements
1517 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1518 * of a char is TRUE if it is the string "white", else FALSE.
1520 static void
1521 init ()
1523 register char *sp;
1524 register int i;
1526 for (i = 0; i < CHARS; i++)
1527 iswhite(i) = notinname(i) = begtoken(i) = intoken(i) = endtoken(i) = FALSE;
1528 for (sp = white; *sp != '\0'; sp++) iswhite (*sp) = TRUE;
1529 for (sp = nonam; *sp != '\0'; sp++) notinname (*sp) = TRUE;
1530 notinname('\0') = notinname('\n');
1531 for (sp = begtk; *sp != '\0'; sp++) begtoken (*sp) = TRUE;
1532 begtoken('\0') = begtoken('\n');
1533 for (sp = midtk; *sp != '\0'; sp++) intoken (*sp) = TRUE;
1534 intoken('\0') = intoken('\n');
1535 for (sp = endtk; *sp != '\0'; sp++) endtoken (*sp) = TRUE;
1536 endtoken('\0') = endtoken('\n');
1540 * This routine opens the specified file and calls the function
1541 * which finds the function and type definitions.
1543 static node *last_node = NULL;
1545 static void
1546 find_entries (file, inf)
1547 char *file;
1548 FILE *inf;
1550 char *cp;
1551 language *lang;
1552 node *old_last_node;
1554 /* If user specified a language, use it. */
1555 lang = forced_lang;
1556 if (lang != NULL && lang->function != NULL)
1558 curlang = lang;
1559 lang->function (inf);
1560 return;
1563 /* Try to guess the language given the file name. */
1564 lang = get_language_from_filename (file);
1565 if (lang != NULL && lang->function != NULL)
1567 curlang = lang;
1568 lang->function (inf);
1569 return;
1572 /* Look for sharp-bang as the first two characters. */
1573 if (readline_internal (&lb, inf) > 0
1574 && lb.len >= 2
1575 && lb.buffer[0] == '#'
1576 && lb.buffer[1] == '!')
1578 char *lp;
1580 /* Set lp to point at the first char after the last slash in the
1581 line or, if no slashes, at the first nonblank. Then set cp to
1582 the first successive blank and terminate the string. */
1583 lp = etags_strrchr (lb.buffer+2, '/');
1584 if (lp != NULL)
1585 lp += 1;
1586 else
1587 lp = skip_spaces (lb.buffer + 2);
1588 cp = skip_non_spaces (lp);
1589 *cp = '\0';
1591 if (strlen (lp) > 0)
1593 lang = get_language_from_interpreter (lp);
1594 if (lang != NULL && lang->function != NULL)
1596 curlang = lang;
1597 lang->function (inf);
1598 return;
1602 /* We rewind here, even if inf may be a pipe. We fail if the
1603 length of the first line is longer than the pipe block size,
1604 which is unlikely. */
1605 rewind (inf);
1607 /* Try Fortran. */
1608 old_last_node = last_node;
1609 curlang = get_language_from_langname ("fortran");
1610 Fortran_functions (inf);
1612 /* No Fortran entries found. Try C. */
1613 if (old_last_node == last_node)
1615 /* We do not tag if rewind fails.
1616 Only the file name will be recorded in the tags file. */
1617 rewind (inf);
1618 curlang = get_language_from_langname (cplusplus ? "c++" : "c");
1619 default_C_entries (inf);
1621 return;
1625 /* Record a tag. */
1626 static void
1627 pfnote (name, is_func, linestart, linelen, lno, cno)
1628 char *name; /* tag name, or NULL if unnamed */
1629 bool is_func; /* tag is a function */
1630 char *linestart; /* start of the line where tag is */
1631 int linelen; /* length of the line where tag is */
1632 int lno; /* line number */
1633 long cno; /* character number */
1635 register node *np;
1637 if (CTAGS && name == NULL)
1638 return;
1640 np = xnew (1, node);
1642 /* If ctags mode, change name "main" to M<thisfilename>. */
1643 if (CTAGS && !cxref_style && streq (name, "main"))
1645 register char *fp = etags_strrchr (curfile, '/');
1646 np->name = concat ("M", fp == NULL ? curfile : fp + 1, "");
1647 fp = etags_strrchr (np->name, '.');
1648 if (fp != NULL && fp[1] != '\0' && fp[2] == '\0')
1649 fp[0] = '\0';
1651 else
1652 np->name = name;
1653 np->been_warned = FALSE;
1654 np->file = curfile;
1655 np->is_func = is_func;
1656 np->lno = lno;
1657 if (nocharno)
1658 np->cno = invalidcharno;
1659 else
1660 /* Our char numbers are 0-base, because of C language tradition?
1661 ctags compatibility? old versions compatibility? I don't know.
1662 Anyway, since emacs's are 1-base we expect etags.el to take care
1663 of the difference. If we wanted to have 1-based numbers, we would
1664 uncomment the +1 below. */
1665 np->cno = cno /* + 1 */ ;
1666 np->left = np->right = NULL;
1667 if (CTAGS && !cxref_style)
1669 if (strlen (linestart) < 50)
1670 np->pat = concat (linestart, "$", "");
1671 else
1672 np->pat = savenstr (linestart, 50);
1674 else
1675 np->pat = savenstr (linestart, linelen);
1677 add_node (np, &head);
1681 * TAGS format specification
1682 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1684 * pfnote should emit the optimized form [unnamed tag] only if:
1685 * 1. name does not contain any of the characters " \t\r\n(),;";
1686 * 2. linestart contains name as either a rightmost, or rightmost but
1687 * one character, substring;
1688 * 3. the character, if any, immediately before name in linestart must
1689 * be one of the characters " \t(),;";
1690 * 4. the character, if any, immediately after name in linestart must
1691 * also be one of the characters " \t(),;".
1693 * The real implementation uses the notinname() macro, which recognises
1694 * characters slightly different from " \t\r\n(),;". See the variable
1695 * `nonam'.
1697 #define traditional_tag_style TRUE
1698 static void
1699 new_pfnote (name, namelen, is_func, linestart, linelen, lno, cno)
1700 char *name; /* tag name, or NULL if unnamed */
1701 int namelen; /* tag length */
1702 bool is_func; /* tag is a function */
1703 char *linestart; /* start of the line where tag is */
1704 int linelen; /* length of the line where tag is */
1705 int lno; /* line number */
1706 long cno; /* character number */
1708 register char *cp;
1709 bool named;
1711 named = TRUE;
1712 if (!CTAGS)
1714 for (cp = name; !notinname (*cp); cp++)
1715 continue;
1716 if (*cp == '\0') /* rule #1 */
1718 cp = linestart + linelen - namelen;
1719 if (notinname (linestart[linelen-1]))
1720 cp -= 1; /* rule #4 */
1721 if (cp >= linestart /* rule #2 */
1722 && (cp == linestart
1723 || notinname (cp[-1])) /* rule #3 */
1724 && strneq (name, cp, namelen)) /* rule #2 */
1725 named = FALSE; /* use unnamed tag */
1729 if (named)
1730 name = savenstr (name, namelen);
1731 else
1732 name = NULL;
1733 pfnote (name, is_func, linestart, linelen, lno, cno);
1737 * free_tree ()
1738 * recurse on left children, iterate on right children.
1740 static void
1741 free_tree (np)
1742 register node *np;
1744 while (np)
1746 register node *node_right = np->right;
1747 free_tree (np->left);
1748 if (np->name != NULL)
1749 free (np->name);
1750 free (np->pat);
1751 free (np);
1752 np = node_right;
1757 * add_node ()
1758 * Adds a node to the tree of nodes. In etags mode, sort by file
1759 * name. In ctags mode, sort by tag name. Make no attempt at
1760 * balancing.
1762 * add_node is the only function allowed to add nodes, so it can
1763 * maintain state.
1765 static void
1766 add_node (np, cur_node_p)
1767 node *np, **cur_node_p;
1769 register int dif;
1770 register node *cur_node = *cur_node_p;
1772 if (cur_node == NULL)
1774 *cur_node_p = np;
1775 last_node = np;
1776 return;
1779 if (!CTAGS)
1781 /* Etags Mode */
1782 assert (last_node != NULL);
1783 /* For each file name, tags are in a linked sublist on the right
1784 pointer. The first tags of different files are a linked list
1785 on the left pointer. last_node points to the end of the last
1786 used sublist. */
1787 if (last_node->file == np->file)
1789 /* Let's use the same sublist as the last added node. */
1790 last_node->right = np;
1791 last_node = np;
1793 else if (streq (cur_node->file, np->file))
1795 /* Scanning the list we found the head of a sublist which is
1796 good for us. Let's scan this sublist. */
1797 add_node (np, &cur_node->right);
1799 else
1800 /* The head of this sublist is not good for us. Let's try the
1801 next one. */
1802 add_node (np, &cur_node->left);
1804 else
1806 /* Ctags Mode */
1807 dif = strcmp (np->name, cur_node->name);
1810 * If this tag name matches an existing one, then
1811 * do not add the node, but maybe print a warning.
1813 if (!dif)
1815 if (streq (np->file, cur_node->file))
1817 if (!no_warnings)
1819 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1820 np->file, lineno, np->name);
1821 fprintf (stderr, "Second entry ignored\n");
1824 else if (!cur_node->been_warned && !no_warnings)
1826 fprintf
1827 (stderr,
1828 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1829 np->file, cur_node->file, np->name);
1830 cur_node->been_warned = TRUE;
1832 return;
1835 /* Actually add the node */
1836 add_node (np, dif < 0 ? &cur_node->left : &cur_node->right);
1841 #if !CTAGS
1842 static int total_size_of_entries __P((node *));
1843 static int number_len __P((long));
1845 /* Length of a number's decimal representation. */
1846 static int
1847 number_len (num)
1848 long num;
1850 int len = 1;
1851 while ((num /= 10) > 0)
1852 len += 1;
1853 return len;
1857 * Return total number of characters that put_entries will output for
1858 * the nodes in the linked list at the right of the specified node.
1859 * This count is irrelevant with etags.el since emacs 19.34 at least,
1860 * but is still supplied for backward compatibility.
1862 static int
1863 total_size_of_entries (np)
1864 register node *np;
1866 register int total = 0;
1868 for (; np != NULL; np = np->right)
1870 total += strlen (np->pat) + 1; /* pat\177 */
1871 if (np->name != NULL)
1872 total += strlen (np->name) + 1; /* name\001 */
1873 total += number_len ((long) np->lno) + 1; /* lno, */
1874 if (np->cno != invalidcharno) /* cno */
1875 total += number_len (np->cno);
1876 total += 1; /* newline */
1879 return total;
1881 #endif
1883 static void
1884 put_entries (np)
1885 register node *np;
1887 register char *sp;
1888 static char *file = NULL;
1890 if (np == NULL)
1891 return;
1893 /* Output subentries that precede this one */
1894 if (CTAGS)
1895 put_entries (np->left);
1897 /* Output this entry */
1898 #if !CTAGS
1900 /* Etags mode */
1901 if (file != np->file
1902 && (file == NULL || !streq (file, np->file)))
1904 file = np->file;
1905 fprintf (tagf, "\f\n%s,%d\n",
1906 file, total_size_of_entries (np));
1908 fputs (np->pat, tagf);
1909 fputc ('\177', tagf);
1910 if (np->name != NULL)
1912 fputs (np->name, tagf);
1913 fputc ('\001', tagf);
1915 fprintf (tagf, "%d,", np->lno);
1916 if (np->cno == invalidcharno)
1917 fputc ('\n', tagf);
1918 else
1919 fprintf (tagf, "%ld\n", np->cno);
1921 #else
1923 /* Ctags mode */
1924 if (np->name == NULL)
1925 error ("internal error: NULL name in ctags mode.", (char *)NULL);
1927 if (cxref_style)
1929 if (vgrind_style)
1930 fprintf (stdout, "%s %s %d\n",
1931 np->name, np->file, (np->lno + 63) / 64);
1932 else
1933 fprintf (stdout, "%-16s %3d %-16s %s\n",
1934 np->name, np->lno, np->file, np->pat);
1936 else
1938 fprintf (tagf, "%s\t%s\t", np->name, np->file);
1940 if (np->is_func)
1941 { /* a function */
1942 putc (searchar, tagf);
1943 putc ('^', tagf);
1945 for (sp = np->pat; *sp; sp++)
1947 if (*sp == '\\' || *sp == searchar)
1948 putc ('\\', tagf);
1949 putc (*sp, tagf);
1951 putc (searchar, tagf);
1953 else
1954 { /* a typedef; text pattern inadequate */
1955 fprintf (tagf, "%d", np->lno);
1957 putc ('\n', tagf);
1960 #endif
1962 /* Output subentries that follow this one */
1963 put_entries (np->right);
1964 if (!CTAGS)
1965 put_entries (np->left);
1969 /* C extensions. */
1970 #define C_EXT 0x00fff /* C extensions */
1971 #define C_PLAIN 0x00000 /* C */
1972 #define C_PLPL 0x00001 /* C++ */
1973 #define C_STAR 0x00003 /* C* */
1974 #define C_JAVA 0x00005 /* JAVA */
1975 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
1976 #define YACC 0x10000 /* yacc file */
1979 * The C symbol tables.
1981 enum sym_type
1983 st_none,
1984 st_C_objprot, st_C_objimpl, st_C_objend,
1985 st_C_gnumacro,
1986 st_C_ignore,
1987 st_C_javastruct,
1988 st_C_operator,
1989 st_C_class, st_C_template,
1990 st_C_struct, st_C_extern, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
1993 static unsigned int hash __P((const char *, unsigned int));
1994 static struct C_stab_entry * in_word_set __P((const char *, unsigned int));
1995 static enum sym_type C_symtype __P((char *, int, int));
1997 /* Feed stuff between (but not including) %[ and %] lines to:
1998 gperf -c -k 1,3 -o -p -r -t
2000 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2002 if, 0, st_C_ignore
2003 for, 0, st_C_ignore
2004 while, 0, st_C_ignore
2005 switch, 0, st_C_ignore
2006 return, 0, st_C_ignore
2007 @interface, 0, st_C_objprot
2008 @protocol, 0, st_C_objprot
2009 @implementation,0, st_C_objimpl
2010 @end, 0, st_C_objend
2011 import, C_JAVA, st_C_ignore
2012 package, C_JAVA, st_C_ignore
2013 friend, C_PLPL, st_C_ignore
2014 extends, C_JAVA, st_C_javastruct
2015 implements, C_JAVA, st_C_javastruct
2016 interface, C_JAVA, st_C_struct
2017 class, 0, st_C_class
2018 namespace, C_PLPL, st_C_struct
2019 domain, C_STAR, st_C_struct
2020 union, 0, st_C_struct
2021 struct, 0, st_C_struct
2022 extern, 0, st_C_extern
2023 enum, 0, st_C_enum
2024 typedef, 0, st_C_typedef
2025 define, 0, st_C_define
2026 operator, C_PLPL, st_C_operator
2027 template, 0, st_C_template
2028 bool, C_PLPL, st_C_typespec
2029 long, 0, st_C_typespec
2030 short, 0, st_C_typespec
2031 int, 0, st_C_typespec
2032 char, 0, st_C_typespec
2033 float, 0, st_C_typespec
2034 double, 0, st_C_typespec
2035 signed, 0, st_C_typespec
2036 unsigned, 0, st_C_typespec
2037 auto, 0, st_C_typespec
2038 void, 0, st_C_typespec
2039 static, 0, st_C_typespec
2040 const, 0, st_C_typespec
2041 volatile, 0, st_C_typespec
2042 explicit, C_PLPL, st_C_typespec
2043 mutable, C_PLPL, st_C_typespec
2044 typename, C_PLPL, st_C_typespec
2045 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2046 DEFUN, 0, st_C_gnumacro
2047 SYSCALL, 0, st_C_gnumacro
2048 ENTRY, 0, st_C_gnumacro
2049 PSEUDO, 0, st_C_gnumacro
2050 # These are defined inside C functions, so currently they are not met.
2051 # EXFUN used in glibc, DEFVAR_* in emacs.
2052 #EXFUN, 0, st_C_gnumacro
2053 #DEFVAR_, 0, st_C_gnumacro
2055 and replace lines between %< and %> with its output,
2056 then make in_word_set and C_stab_entry static. */
2057 /*%<*/
2058 /* C code produced by gperf version 2.7.1 (19981006 egcs) */
2059 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
2060 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
2062 #define TOTAL_KEYWORDS 47
2063 #define MIN_WORD_LENGTH 2
2064 #define MAX_WORD_LENGTH 15
2065 #define MIN_HASH_VALUE 18
2066 #define MAX_HASH_VALUE 138
2067 /* maximum key range = 121, duplicates = 0 */
2069 #ifdef __GNUC__
2070 __inline
2071 #endif
2072 static unsigned int
2073 hash (str, len)
2074 register const char *str;
2075 register unsigned int len;
2077 static unsigned char asso_values[] =
2079 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2080 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2081 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2082 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2083 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2084 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2085 139, 139, 139, 139, 63, 139, 139, 139, 33, 44,
2086 62, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2087 42, 139, 139, 12, 32, 139, 139, 139, 139, 139,
2088 139, 139, 139, 139, 139, 139, 139, 34, 59, 37,
2089 24, 58, 33, 3, 139, 16, 139, 139, 42, 60,
2090 18, 11, 39, 139, 23, 57, 4, 63, 6, 20,
2091 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2092 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2093 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2094 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2095 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2096 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2097 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2098 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2099 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2100 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2101 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2102 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2103 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2104 139, 139, 139, 139, 139, 139
2106 register int hval = len;
2108 switch (hval)
2110 default:
2111 case 3:
2112 hval += asso_values[(unsigned char)str[2]];
2113 case 2:
2114 case 1:
2115 hval += asso_values[(unsigned char)str[0]];
2116 break;
2118 return hval;
2121 #ifdef __GNUC__
2122 __inline
2123 #endif
2124 static struct C_stab_entry *
2125 in_word_set (str, len)
2126 register const char *str;
2127 register unsigned int len;
2129 static struct C_stab_entry wordlist[] =
2131 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2132 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2133 {"if", 0, st_C_ignore},
2134 {""}, {""}, {""}, {""},
2135 {"int", 0, st_C_typespec},
2136 {""}, {""},
2137 {"void", 0, st_C_typespec},
2138 {""}, {""},
2139 {"interface", C_JAVA, st_C_struct},
2140 {""},
2141 {"SYSCALL", 0, st_C_gnumacro},
2142 {""},
2143 {"return", 0, st_C_ignore},
2144 {""}, {""}, {""}, {""}, {""}, {""}, {""},
2145 {"while", 0, st_C_ignore},
2146 {"auto", 0, st_C_typespec},
2147 {""}, {""}, {""}, {""}, {""}, {""},
2148 {"float", 0, st_C_typespec},
2149 {"typedef", 0, st_C_typedef},
2150 {"typename", C_PLPL, st_C_typespec},
2151 {""}, {""}, {""},
2152 {"friend", C_PLPL, st_C_ignore},
2153 {"volatile", 0, st_C_typespec},
2154 {""}, {""},
2155 {"for", 0, st_C_ignore},
2156 {"const", 0, st_C_typespec},
2157 {"import", C_JAVA, st_C_ignore},
2158 {""},
2159 {"define", 0, st_C_define},
2160 {"long", 0, st_C_typespec},
2161 {"implements", C_JAVA, st_C_javastruct},
2162 {"signed", 0, st_C_typespec},
2163 {""},
2164 {"extern", 0, st_C_extern},
2165 {"extends", C_JAVA, st_C_javastruct},
2166 {""},
2167 {"mutable", C_PLPL, st_C_typespec},
2168 {"template", 0, st_C_template},
2169 {"short", 0, st_C_typespec},
2170 {"bool", C_PLPL, st_C_typespec},
2171 {"char", 0, st_C_typespec},
2172 {"class", 0, st_C_class},
2173 {"operator", C_PLPL, st_C_operator},
2174 {""},
2175 {"switch", 0, st_C_ignore},
2176 {""},
2177 {"ENTRY", 0, st_C_gnumacro},
2178 {""},
2179 {"package", C_JAVA, st_C_ignore},
2180 {"union", 0, st_C_struct},
2181 {"@end", 0, st_C_objend},
2182 {"struct", 0, st_C_struct},
2183 {"namespace", C_PLPL, st_C_struct},
2184 {""}, {""},
2185 {"domain", C_STAR, st_C_struct},
2186 {"@interface", 0, st_C_objprot},
2187 {"PSEUDO", 0, st_C_gnumacro},
2188 {"double", 0, st_C_typespec},
2189 {""},
2190 {"@protocol", 0, st_C_objprot},
2191 {""},
2192 {"static", 0, st_C_typespec},
2193 {""}, {""},
2194 {"DEFUN", 0, st_C_gnumacro},
2195 {""}, {""}, {""}, {""},
2196 {"explicit", C_PLPL, st_C_typespec},
2197 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2198 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2199 {""},
2200 {"enum", 0, st_C_enum},
2201 {""}, {""},
2202 {"unsigned", 0, st_C_typespec},
2203 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2204 {"@implementation",0, st_C_objimpl}
2207 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
2209 register int key = hash (str, len);
2211 if (key <= MAX_HASH_VALUE && key >= 0)
2213 register const char *s = wordlist[key].name;
2215 if (*str == *s && !strncmp (str + 1, s + 1, len - 1))
2216 return &wordlist[key];
2219 return 0;
2221 /*%>*/
2223 static enum sym_type
2224 C_symtype (str, len, c_ext)
2225 char *str;
2226 int len;
2227 int c_ext;
2229 register struct C_stab_entry *se = in_word_set (str, len);
2231 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
2232 return st_none;
2233 return se->type;
2238 * C functions and variables are recognized using a simple
2239 * finite automaton. fvdef is its state variable.
2241 static enum
2243 fvnone, /* nothing seen */
2244 fdefunkey, /* Emacs DEFUN keyword seen */
2245 fdefunname, /* Emacs DEFUN name seen */
2246 foperator, /* func: operator keyword seen (cplpl) */
2247 fvnameseen, /* function or variable name seen */
2248 fstartlist, /* func: just after open parenthesis */
2249 finlist, /* func: in parameter list */
2250 flistseen, /* func: after parameter list */
2251 fignore, /* func: before open brace */
2252 vignore /* var-like: ignore until ';' */
2253 } fvdef;
2255 static bool fvextern; /* func or var: extern keyword seen; */
2258 * typedefs are recognized using a simple finite automaton.
2259 * typdef is its state variable.
2261 static enum
2263 tnone, /* nothing seen */
2264 tkeyseen, /* typedef keyword seen */
2265 ttypeseen, /* defined type seen */
2266 tinbody, /* inside typedef body */
2267 tend, /* just before typedef tag */
2268 tignore /* junk after typedef tag */
2269 } typdef;
2272 * struct-like structures (enum, struct and union) are recognized
2273 * using another simple finite automaton. `structdef' is its state
2274 * variable.
2276 static enum
2278 snone, /* nothing seen yet,
2279 or in struct body if cblev > 0 */
2280 skeyseen, /* struct-like keyword seen */
2281 stagseen, /* struct-like tag seen */
2282 sintemplate, /* inside template (ignore) */
2283 scolonseen /* colon seen after struct-like tag */
2284 } structdef;
2287 * When objdef is different from onone, objtag is the name of the class.
2289 static char *objtag = "<uninited>";
2292 * Yet another little state machine to deal with preprocessor lines.
2294 static enum
2296 dnone, /* nothing seen */
2297 dsharpseen, /* '#' seen as first char on line */
2298 ddefineseen, /* '#' and 'define' seen */
2299 dignorerest /* ignore rest of line */
2300 } definedef;
2303 * State machine for Objective C protocols and implementations.
2304 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2306 static enum
2308 onone, /* nothing seen */
2309 oprotocol, /* @interface or @protocol seen */
2310 oimplementation, /* @implementations seen */
2311 otagseen, /* class name seen */
2312 oparenseen, /* parenthesis before category seen */
2313 ocatseen, /* category name seen */
2314 oinbody, /* in @implementation body */
2315 omethodsign, /* in @implementation body, after +/- */
2316 omethodtag, /* after method name */
2317 omethodcolon, /* after method colon */
2318 omethodparm, /* after method parameter */
2319 oignore /* wait for @end */
2320 } objdef;
2324 * Use this structure to keep info about the token read, and how it
2325 * should be tagged. Used by the make_C_tag function to build a tag.
2327 static struct tok
2329 bool valid;
2330 bool named;
2331 int offset;
2332 int length;
2333 int lineno;
2334 long linepos;
2335 char *line;
2336 } token; /* latest token read */
2337 static linebuffer token_name; /* its name */
2340 * Variables and functions for dealing with nested structures.
2341 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2343 static void pushclass_above __P((int, char *, int));
2344 static void popclass_above __P((int));
2345 static void write_classname __P((linebuffer *, char *qualifier));
2347 static struct {
2348 char **cname; /* nested class names */
2349 int *cblev; /* nested class curly brace level */
2350 int nl; /* class nesting level (elements used) */
2351 int size; /* length of the array */
2352 } cstack; /* stack for nested declaration tags */
2353 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2354 #define nestlev (cstack.nl)
2355 /* After struct keyword or in struct body, not inside an nested function. */
2356 #define instruct (structdef == snone && nestlev > 0 \
2357 && cblev == cstack.cblev[nestlev-1] + 1)
2359 static void
2360 pushclass_above (cblev, str, len)
2361 int cblev;
2362 char *str;
2363 int len;
2365 int nl;
2367 popclass_above (cblev);
2368 nl = cstack.nl;
2369 if (nl >= cstack.size)
2371 int size = cstack.size *= 2;
2372 xrnew (cstack.cname, size, char *);
2373 xrnew (cstack.cblev, size, int);
2375 assert (nl == 0 || cstack.cblev[nl-1] < cblev);
2376 cstack.cname[nl] = (str == NULL) ? NULL : savenstr (str, len);
2377 cstack.cblev[nl] = cblev;
2378 cstack.nl = nl + 1;
2381 static void
2382 popclass_above (cblev)
2383 int cblev;
2385 int nl;
2387 for (nl = cstack.nl - 1;
2388 nl >= 0 && cstack.cblev[nl] >= cblev;
2389 nl--)
2391 if (cstack.cname[nl] != NULL)
2392 free (cstack.cname[nl]);
2393 cstack.nl = nl;
2397 static void
2398 write_classname (cn, qualifier)
2399 linebuffer *cn;
2400 char *qualifier;
2402 int i, len;
2403 int qlen = strlen (qualifier);
2405 if (cstack.nl == 0 || cstack.cname[0] == NULL)
2407 len = 0;
2408 cn->len = 0;
2409 cn->buffer[0] = '\0';
2411 else
2413 len = strlen (cstack.cname[0]);
2414 linebuffer_setlen (cn, len);
2415 strcpy (cn->buffer, cstack.cname[0]);
2417 for (i = 1; i < cstack.nl; i++)
2419 char *s;
2420 int slen;
2422 s = cstack.cname[i];
2423 if (s == NULL)
2424 continue;
2425 slen = strlen (s);
2426 len += slen + qlen;
2427 linebuffer_setlen (cn, len);
2428 strncat (cn->buffer, qualifier, qlen);
2429 strncat (cn->buffer, s, slen);
2434 static bool consider_token __P((char *, int, int, int *, int, int, bool *));
2435 static void make_C_tag __P((bool));
2438 * consider_token ()
2439 * checks to see if the current token is at the start of a
2440 * function or variable, or corresponds to a typedef, or
2441 * is a struct/union/enum tag, or #define, or an enum constant.
2443 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2444 * with args. C_EXTP points to which language we are looking at.
2446 * Globals
2447 * fvdef IN OUT
2448 * structdef IN OUT
2449 * definedef IN OUT
2450 * typdef IN OUT
2451 * objdef IN OUT
2454 static bool
2455 consider_token (str, len, c, c_extp, cblev, parlev, is_func_or_var)
2456 register char *str; /* IN: token pointer */
2457 register int len; /* IN: token length */
2458 register int c; /* IN: first char after the token */
2459 int *c_extp; /* IN, OUT: C extensions mask */
2460 int cblev; /* IN: curly brace level */
2461 int parlev; /* IN: parenthesis level */
2462 bool *is_func_or_var; /* OUT: function or variable found */
2464 /* When structdef is stagseen, scolonseen, or snone with cblev > 0,
2465 structtype is the type of the preceding struct-like keyword, and
2466 structcblev is the curly brace level where it has been seen. */
2467 static enum sym_type structtype;
2468 static int structcblev;
2469 static enum sym_type toktype;
2472 toktype = C_symtype (str, len, *c_extp);
2475 * Advance the definedef state machine.
2477 switch (definedef)
2479 case dnone:
2480 /* We're not on a preprocessor line. */
2481 if (toktype == st_C_gnumacro)
2483 fvdef = fdefunkey;
2484 return FALSE;
2486 break;
2487 case dsharpseen:
2488 if (toktype == st_C_define)
2490 definedef = ddefineseen;
2492 else
2494 definedef = dignorerest;
2496 return FALSE;
2497 case ddefineseen:
2499 * Make a tag for any macro, unless it is a constant
2500 * and constantypedefs is FALSE.
2502 definedef = dignorerest;
2503 *is_func_or_var = (c == '(');
2504 if (!*is_func_or_var && !constantypedefs)
2505 return FALSE;
2506 else
2507 return TRUE;
2508 case dignorerest:
2509 return FALSE;
2510 default:
2511 error ("internal error: definedef value.", (char *)NULL);
2515 * Now typedefs
2517 switch (typdef)
2519 case tnone:
2520 if (toktype == st_C_typedef)
2522 if (typedefs)
2523 typdef = tkeyseen;
2524 fvextern = FALSE;
2525 fvdef = fvnone;
2526 return FALSE;
2528 break;
2529 case tkeyseen:
2530 switch (toktype)
2532 case st_none:
2533 case st_C_typespec:
2534 case st_C_class:
2535 case st_C_struct:
2536 case st_C_enum:
2537 typdef = ttypeseen;
2538 break;
2540 break;
2541 case ttypeseen:
2542 if (structdef == snone && fvdef == fvnone)
2544 fvdef = fvnameseen;
2545 return TRUE;
2547 break;
2548 case tend:
2549 switch (toktype)
2551 case st_C_typespec:
2552 case st_C_class:
2553 case st_C_struct:
2554 case st_C_enum:
2555 return FALSE;
2557 return TRUE;
2561 * This structdef business is NOT invoked when we are ctags and the
2562 * file is plain C. This is because a struct tag may have the same
2563 * name as another tag, and this loses with ctags.
2565 switch (toktype)
2567 case st_C_javastruct:
2568 if (structdef == stagseen)
2569 structdef = scolonseen;
2570 return FALSE;
2571 case st_C_template:
2572 case st_C_class:
2573 if (cblev == 0
2574 && (*c_extp & C_AUTO) /* automatic detection of C++ language */
2575 && definedef == dnone && structdef == snone
2576 && typdef == tnone && fvdef == fvnone)
2577 *c_extp = (*c_extp | C_PLPL) & ~C_AUTO;
2578 if (toktype == st_C_template)
2579 break;
2580 /* FALLTHRU */
2581 case st_C_struct:
2582 case st_C_enum:
2583 if (parlev == 0
2584 && fvdef != vignore
2585 && (typdef == tkeyseen
2586 || (typedefs_or_cplusplus && structdef == snone)))
2588 structdef = skeyseen;
2589 structtype = toktype;
2590 structcblev = cblev;
2592 return FALSE;
2595 if (structdef == skeyseen)
2597 structdef = stagseen;
2598 return TRUE;
2601 if (typdef != tnone)
2602 definedef = dnone;
2604 /* Detect Objective C constructs. */
2605 switch (objdef)
2607 case onone:
2608 switch (toktype)
2610 case st_C_objprot:
2611 objdef = oprotocol;
2612 return FALSE;
2613 case st_C_objimpl:
2614 objdef = oimplementation;
2615 return FALSE;
2617 break;
2618 case oimplementation:
2619 /* Save the class tag for functions or variables defined inside. */
2620 objtag = savenstr (str, len);
2621 objdef = oinbody;
2622 return FALSE;
2623 case oprotocol:
2624 /* Save the class tag for categories. */
2625 objtag = savenstr (str, len);
2626 objdef = otagseen;
2627 *is_func_or_var = TRUE;
2628 return TRUE;
2629 case oparenseen:
2630 objdef = ocatseen;
2631 *is_func_or_var = TRUE;
2632 return TRUE;
2633 case oinbody:
2634 break;
2635 case omethodsign:
2636 if (parlev == 0)
2638 objdef = omethodtag;
2639 linebuffer_setlen (&token_name, len);
2640 strncpy (token_name.buffer, str, len);
2641 token_name.buffer[len] = '\0';
2642 return TRUE;
2644 return FALSE;
2645 case omethodcolon:
2646 if (parlev == 0)
2647 objdef = omethodparm;
2648 return FALSE;
2649 case omethodparm:
2650 if (parlev == 0)
2652 objdef = omethodtag;
2653 linebuffer_setlen (&token_name, token_name.len + len);
2654 strncat (token_name.buffer, str, len);
2655 return TRUE;
2657 return FALSE;
2658 case oignore:
2659 if (toktype == st_C_objend)
2661 /* Memory leakage here: the string pointed by objtag is
2662 never released, because many tests would be needed to
2663 avoid breaking on incorrect input code. The amount of
2664 memory leaked here is the sum of the lengths of the
2665 class tags.
2666 free (objtag); */
2667 objdef = onone;
2669 return FALSE;
2672 /* A function, variable or enum constant? */
2673 switch (toktype)
2675 case st_C_extern:
2676 fvextern = TRUE;
2677 /* FALLTHRU */
2678 case st_C_typespec:
2679 if (fvdef != finlist && fvdef != fignore && fvdef != vignore)
2680 fvdef = fvnone; /* should be useless */
2681 return FALSE;
2682 case st_C_ignore:
2683 fvextern = FALSE;
2684 fvdef = vignore;
2685 return FALSE;
2686 case st_C_operator:
2687 fvdef = foperator;
2688 *is_func_or_var = TRUE;
2689 return TRUE;
2690 case st_none:
2691 if (constantypedefs
2692 && structdef == snone
2693 && structtype == st_C_enum && cblev > structcblev)
2694 return TRUE; /* enum constant */
2695 switch (fvdef)
2697 case fdefunkey:
2698 if (cblev > 0)
2699 break;
2700 fvdef = fdefunname; /* GNU macro */
2701 *is_func_or_var = TRUE;
2702 return TRUE;
2703 case fvnone:
2704 if ((strneq (str, "asm", 3) && endtoken (str[3]))
2705 || (strneq (str, "__asm__", 7) && endtoken (str[7])))
2707 fvdef = vignore;
2708 return FALSE;
2710 if ((*c_extp & C_PLPL) && strneq (str+len-10, "::operator", 10))
2712 fvdef = foperator;
2713 *is_func_or_var = TRUE;
2714 return TRUE;
2716 if (cblev > 0 && !instruct)
2717 break;
2718 fvdef = fvnameseen; /* function or variable */
2719 *is_func_or_var = TRUE;
2720 return TRUE;
2722 break;
2725 return FALSE;
2730 * C_entries often keeps pointers to tokens or lines which are older than
2731 * the line currently read. By keeping two line buffers, and switching
2732 * them at end of line, it is possible to use those pointers.
2734 static struct
2736 long linepos;
2737 linebuffer lb;
2738 } lbs[2];
2740 #define current_lb_is_new (newndx == curndx)
2741 #define switch_line_buffers() (curndx = 1 - curndx)
2743 #define curlb (lbs[curndx].lb)
2744 #define newlb (lbs[newndx].lb)
2745 #define curlinepos (lbs[curndx].linepos)
2746 #define newlinepos (lbs[newndx].linepos)
2748 #define CNL_SAVE_DEFINEDEF() \
2749 do { \
2750 curlinepos = charno; \
2751 lineno++; \
2752 linecharno = charno; \
2753 charno += readline (&curlb, inf); \
2754 lp = curlb.buffer; \
2755 quotednl = FALSE; \
2756 newndx = curndx; \
2757 } while (0)
2759 #define CNL() \
2760 do { \
2761 CNL_SAVE_DEFINEDEF(); \
2762 if (savetoken.valid) \
2764 token = savetoken; \
2765 savetoken.valid = FALSE; \
2767 definedef = dnone; \
2768 } while (0)
2771 static void
2772 make_C_tag (isfun)
2773 bool isfun;
2775 /* This function should never be called when token.valid is FALSE, but
2776 we must protect against invalid input or internal errors. */
2777 if (DEBUG || token.valid)
2779 if (traditional_tag_style)
2781 /* This was the original code. Now we call new_pfnote instead,
2782 which uses the new method for naming tags (see new_pfnote). */
2783 char *name = NULL;
2785 if (CTAGS || token.named)
2786 name = savestr (token_name.buffer);
2787 if (DEBUG && !token.valid)
2789 if (token.named)
2790 name = concat (name, "##invalid##", "");
2791 else
2792 name = savestr ("##invalid##");
2794 pfnote (name, isfun, token.line,
2795 token.offset+token.length+1, token.lineno, token.linepos);
2797 else
2798 new_pfnote (token_name.buffer, token_name.len, isfun, token.line,
2799 token.offset+token.length+1, token.lineno, token.linepos);
2800 token.valid = FALSE;
2806 * C_entries ()
2807 * This routine finds functions, variables, typedefs,
2808 * #define's, enum constants and struct/union/enum definitions in
2809 * C syntax and adds them to the list.
2811 static void
2812 C_entries (c_ext, inf)
2813 int c_ext; /* extension of C */
2814 FILE *inf; /* input file */
2816 register char c; /* latest char read; '\0' for end of line */
2817 register char *lp; /* pointer one beyond the character `c' */
2818 int curndx, newndx; /* indices for current and new lb */
2819 register int tokoff; /* offset in line of start of current token */
2820 register int toklen; /* length of current token */
2821 char *qualifier; /* string used to qualify names */
2822 int qlen; /* length of qualifier */
2823 int cblev; /* current curly brace level */
2824 int parlev; /* current parenthesis level */
2825 int typdefcblev; /* cblev where a typedef struct body begun */
2826 bool incomm, inquote, inchar, quotednl, midtoken;
2827 bool cplpl, cjava;
2828 bool yacc_rules; /* in the rules part of a yacc file */
2829 struct tok savetoken; /* token saved during preprocessor handling */
2832 initbuffer (&token_name);
2833 initbuffer (&lbs[0].lb);
2834 initbuffer (&lbs[1].lb);
2835 if (cstack.size == 0)
2837 cstack.size = (DEBUG) ? 1 : 4;
2838 cstack.nl = 0;
2839 cstack.cname = xnew (cstack.size, char *);
2840 cstack.cblev = xnew (cstack.size, int);
2843 tokoff = toklen = typdefcblev = 0; /* keep compiler quiet */
2844 curndx = newndx = 0;
2845 lineno = 0;
2846 charno = 0;
2847 lp = curlb.buffer;
2848 *lp = 0;
2850 fvdef = fvnone; fvextern = FALSE; typdef = tnone;
2851 structdef = snone; definedef = dnone; objdef = onone;
2852 yacc_rules = FALSE;
2853 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2854 token.valid = savetoken.valid = FALSE;
2855 cblev = 0;
2856 parlev = 0;
2857 cplpl = (c_ext & C_PLPL) == C_PLPL;
2858 cjava = (c_ext & C_JAVA) == C_JAVA;
2859 if (cjava)
2860 { qualifier = "."; qlen = 1; }
2861 else
2862 { qualifier = "::"; qlen = 2; }
2865 while (!feof (inf))
2867 c = *lp++;
2868 if (c == '\\')
2870 /* If we're at the end of the line, the next character is a
2871 '\0'; don't skip it, because it's the thing that tells us
2872 to read the next line. */
2873 if (*lp == '\0')
2875 quotednl = TRUE;
2876 continue;
2878 lp++;
2879 c = ' ';
2881 else if (incomm)
2883 switch (c)
2885 case '*':
2886 if (*lp == '/')
2888 c = *lp++;
2889 incomm = FALSE;
2891 break;
2892 case '\0':
2893 /* Newlines inside comments do not end macro definitions in
2894 traditional cpp. */
2895 CNL_SAVE_DEFINEDEF ();
2896 break;
2898 continue;
2900 else if (inquote)
2902 switch (c)
2904 case '"':
2905 inquote = FALSE;
2906 break;
2907 case '\0':
2908 /* Newlines inside strings do not end macro definitions
2909 in traditional cpp, even though compilers don't
2910 usually accept them. */
2911 CNL_SAVE_DEFINEDEF ();
2912 break;
2914 continue;
2916 else if (inchar)
2918 switch (c)
2920 case '\0':
2921 /* Hmmm, something went wrong. */
2922 CNL ();
2923 /* FALLTHRU */
2924 case '\'':
2925 inchar = FALSE;
2926 break;
2928 continue;
2930 else
2931 switch (c)
2933 case '"':
2934 inquote = TRUE;
2935 switch (fvdef)
2937 case fdefunkey:
2938 case fstartlist:
2939 case finlist:
2940 case fignore:
2941 case vignore:
2942 break;
2943 default:
2944 fvextern = FALSE;
2945 fvdef = fvnone;
2947 continue;
2948 case '\'':
2949 inchar = TRUE;
2950 if (fvdef != finlist && fvdef != fignore && fvdef !=vignore)
2952 fvextern = FALSE;
2953 fvdef = fvnone;
2955 continue;
2956 case '/':
2957 if (*lp == '*')
2959 lp++;
2960 incomm = TRUE;
2961 continue;
2963 else if (/* cplpl && */ *lp == '/')
2965 c = '\0';
2966 break;
2968 else
2969 break;
2970 case '%':
2971 if ((c_ext & YACC) && *lp == '%')
2973 /* Entering or exiting rules section in yacc file. */
2974 lp++;
2975 definedef = dnone; fvdef = fvnone; fvextern = FALSE;
2976 typdef = tnone; structdef = snone;
2977 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2978 cblev = 0;
2979 yacc_rules = !yacc_rules;
2980 continue;
2982 else
2983 break;
2984 case '#':
2985 if (definedef == dnone)
2987 char *cp;
2988 bool cpptoken = TRUE;
2990 /* Look back on this line. If all blanks, or nonblanks
2991 followed by an end of comment, this is a preprocessor
2992 token. */
2993 for (cp = newlb.buffer; cp < lp-1; cp++)
2994 if (!iswhite (*cp))
2996 if (*cp == '*' && *(cp+1) == '/')
2998 cp++;
2999 cpptoken = TRUE;
3001 else
3002 cpptoken = FALSE;
3004 if (cpptoken)
3005 definedef = dsharpseen;
3006 } /* if (definedef == dnone) */
3008 continue;
3009 } /* switch (c) */
3012 /* Consider token only if some involved conditions are satisfied. */
3013 if (typdef != tignore
3014 && definedef != dignorerest
3015 && fvdef != finlist
3016 && structdef != sintemplate
3017 && (definedef != dnone
3018 || structdef != scolonseen))
3020 if (midtoken)
3022 if (endtoken (c))
3024 if (c == ':' && cplpl && *lp == ':' && begtoken (lp[1]))
3027 * This handles :: in the middle, but not at the
3028 * beginning of an identifier. Also, space-separated
3029 * :: is not recognised.
3031 lp += 2;
3032 toklen += 2;
3033 c = lp[-1];
3034 goto still_in_token;
3036 else
3038 bool funorvar = FALSE;
3040 if (yacc_rules
3041 || consider_token (newlb.buffer + tokoff, toklen, c,
3042 &c_ext, cblev, parlev, &funorvar))
3044 if (fvdef == foperator)
3046 char *oldlp = lp;
3047 lp = skip_spaces (lp-1);
3048 if (*lp != '\0')
3049 lp += 1;
3050 while (*lp != '\0'
3051 && !iswhite (*lp) && *lp != '(')
3052 lp += 1;
3053 c = *lp++;
3054 toklen += lp - oldlp;
3056 token.named = FALSE;
3057 if ((c_ext & C_EXT) /* not pure C */
3058 && nestlev > 0 && definedef == dnone)
3059 /* in struct body */
3061 write_classname (&token_name, qualifier);
3062 linebuffer_setlen (&token_name,
3063 token_name.len+qlen+toklen);
3064 strcat (token_name.buffer, qualifier);
3065 strncat (token_name.buffer,
3066 newlb.buffer + tokoff, toklen);
3067 token.named = TRUE;
3069 else if (objdef == ocatseen)
3070 /* Objective C category */
3072 int len = strlen (objtag) + 2 + toklen;
3073 linebuffer_setlen (&token_name, len);
3074 strcpy (token_name.buffer, objtag);
3075 strcat (token_name.buffer, "(");
3076 strncat (token_name.buffer,
3077 newlb.buffer + tokoff, toklen);
3078 strcat (token_name.buffer, ")");
3079 token.named = TRUE;
3081 else if (objdef == omethodtag
3082 || objdef == omethodparm)
3083 /* Objective C method */
3085 token.named = TRUE;
3087 else if (fvdef == fdefunname)
3088 /* GNU DEFUN and similar macros */
3090 bool defun = (newlb.buffer[tokoff] == 'F');
3091 int off = tokoff;
3092 int len = toklen;
3094 /* Rewrite the tag so that emacs lisp DEFUNs
3095 can be found by their elisp name */
3096 if (defun)
3098 off += 1;
3099 len -= 1;
3101 len = toklen;
3102 linebuffer_setlen (&token_name, len);
3103 strncpy (token_name.buffer,
3104 newlb.buffer + off, len);
3105 token_name.buffer[len] = '\0';
3106 if (defun)
3107 while (--len >= 0)
3108 if (token_name.buffer[len] == '_')
3109 token_name.buffer[len] = '-';
3110 token.named = defun;
3112 else
3114 linebuffer_setlen (&token_name, toklen);
3115 strncpy (token_name.buffer,
3116 newlb.buffer + tokoff, toklen);
3117 token_name.buffer[toklen] = '\0';
3118 /* Name macros and members. */
3119 token.named = (structdef == stagseen
3120 || typdef == ttypeseen
3121 || typdef == tend
3122 || (funorvar
3123 && definedef == dignorerest)
3124 || (funorvar
3125 && definedef == dnone
3126 && structdef == snone
3127 && cblev > 0));
3129 token.lineno = lineno;
3130 token.offset = tokoff;
3131 token.length = toklen;
3132 token.line = newlb.buffer;
3133 token.linepos = newlinepos;
3134 token.valid = TRUE;
3136 if (definedef == dnone
3137 && (fvdef == fvnameseen
3138 || fvdef == foperator
3139 || structdef == stagseen
3140 || typdef == tend
3141 || typdef == ttypeseen
3142 || objdef != onone))
3144 if (current_lb_is_new)
3145 switch_line_buffers ();
3147 else if (definedef != dnone
3148 || fvdef == fdefunname
3149 || instruct)
3150 make_C_tag (funorvar);
3152 midtoken = FALSE;
3154 } /* if (endtoken (c)) */
3155 else if (intoken (c))
3156 still_in_token:
3158 toklen++;
3159 continue;
3161 } /* if (midtoken) */
3162 else if (begtoken (c))
3164 switch (definedef)
3166 case dnone:
3167 switch (fvdef)
3169 case fstartlist:
3170 fvdef = finlist;
3171 continue;
3172 case flistseen:
3173 make_C_tag (TRUE); /* a function */
3174 fvdef = fignore;
3175 break;
3176 case fvnameseen:
3177 fvdef = fvnone;
3178 break;
3180 if (structdef == stagseen && !cjava)
3182 popclass_above (cblev);
3183 structdef = snone;
3185 break;
3186 case dsharpseen:
3187 savetoken = token;
3189 if (!yacc_rules || lp == newlb.buffer + 1)
3191 tokoff = lp - 1 - newlb.buffer;
3192 toklen = 1;
3193 midtoken = TRUE;
3195 continue;
3196 } /* if (begtoken) */
3197 } /* if must look at token */
3200 /* Detect end of line, colon, comma, semicolon and various braces
3201 after having handled a token.*/
3202 switch (c)
3204 case ':':
3205 if (yacc_rules && token.offset == 0 && token.valid)
3207 make_C_tag (FALSE); /* a yacc function */
3208 break;
3210 if (definedef != dnone)
3211 break;
3212 switch (objdef)
3214 case otagseen:
3215 objdef = oignore;
3216 make_C_tag (TRUE); /* an Objective C class */
3217 break;
3218 case omethodtag:
3219 case omethodparm:
3220 objdef = omethodcolon;
3221 linebuffer_setlen (&token_name, token_name.len + 1);
3222 strcat (token_name.buffer, ":");
3223 break;
3225 if (structdef == stagseen)
3226 structdef = scolonseen;
3227 break;
3228 case ';':
3229 if (definedef != dnone)
3230 break;
3231 switch (typdef)
3233 case tend:
3234 case ttypeseen:
3235 make_C_tag (FALSE); /* a typedef */
3236 typdef = tnone;
3237 fvdef = fvnone;
3238 break;
3239 case tnone:
3240 case tinbody:
3241 case tignore:
3242 switch (fvdef)
3244 case fignore:
3245 if (typdef == tignore)
3246 fvdef = fvnone;
3247 break;
3248 case fvnameseen:
3249 if ((globals && cblev == 0 && (!fvextern || declarations))
3250 || (members && instruct))
3251 make_C_tag (FALSE); /* a variable */
3252 fvextern = FALSE;
3253 fvdef = fvnone;
3254 token.valid = FALSE;
3255 break;
3256 case flistseen:
3257 if ((declarations && typdef == tnone && !instruct)
3258 || (members && typdef != tignore && instruct))
3259 make_C_tag (TRUE); /* a function declaration */
3260 /* FALLTHRU */
3261 default:
3262 fvextern = FALSE;
3263 fvdef = fvnone;
3264 if (declarations
3265 && structdef == stagseen && (c_ext & C_PLPL))
3266 make_C_tag (FALSE); /* forward declaration */
3267 else
3268 /* The following instruction invalidates the token.
3269 Probably the token should be invalidated in all other
3270 cases where some state machine is reset prematurely. */
3271 token.valid = FALSE;
3272 } /* switch (fvdef) */
3273 /* FALLTHRU */
3274 default:
3275 if (!instruct)
3276 typdef = tnone;
3278 if (structdef == stagseen)
3279 structdef = snone;
3280 break;
3281 case ',':
3282 if (definedef != dnone)
3283 break;
3284 switch (objdef)
3286 case omethodtag:
3287 case omethodparm:
3288 make_C_tag (TRUE); /* an Objective C method */
3289 objdef = oinbody;
3290 break;
3292 switch (fvdef)
3294 case fdefunkey:
3295 case foperator:
3296 case fstartlist:
3297 case finlist:
3298 case fignore:
3299 case vignore:
3300 break;
3301 case fdefunname:
3302 fvdef = fignore;
3303 break;
3304 case fvnameseen: /* a variable */
3305 if ((globals && cblev == 0 && (!fvextern || declarations))
3306 || (members && instruct))
3307 make_C_tag (FALSE);
3308 break;
3309 case flistseen: /* a function */
3310 if ((declarations && typdef == tnone && !instruct)
3311 || (members && typdef != tignore && instruct))
3313 make_C_tag (TRUE); /* a function declaration */
3314 fvdef = fvnameseen;
3316 else if (!declarations)
3317 fvdef = fvnone;
3318 token.valid = FALSE;
3319 break;
3320 default:
3321 fvdef = fvnone;
3323 if (structdef == stagseen)
3324 structdef = snone;
3325 break;
3326 case '[':
3327 if (definedef != dnone)
3328 break;
3329 if (structdef == stagseen)
3330 structdef = snone;
3331 switch (typdef)
3333 case ttypeseen:
3334 case tend:
3335 typdef = tignore;
3336 make_C_tag (FALSE); /* a typedef */
3337 break;
3338 case tnone:
3339 case tinbody:
3340 switch (fvdef)
3342 case foperator:
3343 case finlist:
3344 case fignore:
3345 case vignore:
3346 break;
3347 case fvnameseen:
3348 if ((members && cblev == 1)
3349 || (globals && cblev == 0
3350 && (!fvextern || declarations)))
3351 make_C_tag (FALSE); /* a variable */
3352 /* FALLTHRU */
3353 default:
3354 fvdef = fvnone;
3356 break;
3358 break;
3359 case '(':
3360 if (definedef != dnone)
3361 break;
3362 if (objdef == otagseen && parlev == 0)
3363 objdef = oparenseen;
3364 switch (fvdef)
3366 case fvnameseen:
3367 if (typdef == ttypeseen
3368 && *lp != '*'
3369 && !instruct)
3371 /* This handles constructs like:
3372 typedef void OperatorFun (int fun); */
3373 make_C_tag (FALSE);
3374 typdef = tignore;
3375 fvdef = fignore;
3376 break;
3378 /* FALLTHRU */
3379 case foperator:
3380 fvdef = fstartlist;
3381 break;
3382 case flistseen:
3383 fvdef = finlist;
3384 break;
3386 parlev++;
3387 break;
3388 case ')':
3389 if (definedef != dnone)
3390 break;
3391 if (objdef == ocatseen && parlev == 1)
3393 make_C_tag (TRUE); /* an Objective C category */
3394 objdef = oignore;
3396 if (--parlev == 0)
3398 switch (fvdef)
3400 case fstartlist:
3401 case finlist:
3402 fvdef = flistseen;
3403 break;
3405 if (!instruct
3406 && (typdef == tend
3407 || typdef == ttypeseen))
3409 typdef = tignore;
3410 make_C_tag (FALSE); /* a typedef */
3413 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
3414 parlev = 0;
3415 break;
3416 case '{':
3417 if (definedef != dnone)
3418 break;
3419 if (typdef == ttypeseen)
3421 /* Whenever typdef is set to tinbody (currently only
3422 here), typdefcblev should be set to cblev. */
3423 typdef = tinbody;
3424 typdefcblev = cblev;
3426 switch (fvdef)
3428 case flistseen:
3429 make_C_tag (TRUE); /* a function */
3430 /* FALLTHRU */
3431 case fignore:
3432 fvdef = fvnone;
3433 break;
3434 case fvnone:
3435 switch (objdef)
3437 case otagseen:
3438 make_C_tag (TRUE); /* an Objective C class */
3439 objdef = oignore;
3440 break;
3441 case omethodtag:
3442 case omethodparm:
3443 make_C_tag (TRUE); /* an Objective C method */
3444 objdef = oinbody;
3445 break;
3446 default:
3447 /* Neutralize `extern "C" {' grot. */
3448 if (cblev == 0 && structdef == snone && nestlev == 0
3449 && typdef == tnone)
3450 cblev = -1;
3453 switch (structdef)
3455 case skeyseen: /* unnamed struct */
3456 pushclass_above (cblev, NULL, 0);
3457 structdef = snone;
3458 break;
3459 case stagseen: /* named struct or enum */
3460 case scolonseen: /* a class */
3461 pushclass_above (cblev, token.line+token.offset, token.length);
3462 structdef = snone;
3463 make_C_tag (FALSE); /* a struct or enum */
3464 break;
3466 cblev++;
3467 break;
3468 case '*':
3469 if (definedef != dnone)
3470 break;
3471 if (fvdef == fstartlist)
3472 fvdef = fvnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
3473 break;
3474 case '}':
3475 if (definedef != dnone)
3476 break;
3477 if (!noindentypedefs && lp == newlb.buffer + 1)
3479 cblev = 0; /* reset curly brace level if first column */
3480 parlev = 0; /* also reset paren level, just in case... */
3482 else if (cblev > 0)
3483 cblev--;
3484 popclass_above (cblev);
3485 structdef = snone;
3486 /* Only if typdef == tinbody is typdefcblev significant. */
3487 if (typdef == tinbody && cblev <= typdefcblev)
3489 assert (cblev == typdefcblev);
3490 typdef = tend;
3492 break;
3493 case '=':
3494 if (definedef != dnone)
3495 break;
3496 switch (fvdef)
3498 case foperator:
3499 case finlist:
3500 case fignore:
3501 case vignore:
3502 break;
3503 case fvnameseen:
3504 if ((members && cblev == 1)
3505 || (globals && cblev == 0 && (!fvextern || declarations)))
3506 make_C_tag (FALSE); /* a variable */
3507 /* FALLTHRU */
3508 default:
3509 fvdef = vignore;
3511 break;
3512 case '<':
3513 if (cplpl && structdef == stagseen)
3515 structdef = sintemplate;
3516 break;
3518 goto resetfvdef;
3519 case '>':
3520 if (structdef == sintemplate)
3522 structdef = stagseen;
3523 break;
3525 goto resetfvdef;
3526 case '+':
3527 case '-':
3528 if (objdef == oinbody && cblev == 0)
3530 objdef = omethodsign;
3531 break;
3533 /* FALLTHRU */
3534 resetfvdef:
3535 case '#': case '~': case '&': case '%': case '/': case '|':
3536 case '^': case '!': case '.': case '?': case ']':
3537 if (definedef != dnone)
3538 break;
3539 /* These surely cannot follow a function tag in C. */
3540 switch (fvdef)
3542 case foperator:
3543 case finlist:
3544 case fignore:
3545 case vignore:
3546 break;
3547 default:
3548 fvdef = fvnone;
3550 break;
3551 case '\0':
3552 if (objdef == otagseen)
3554 make_C_tag (TRUE); /* an Objective C class */
3555 objdef = oignore;
3557 /* If a macro spans multiple lines don't reset its state. */
3558 if (quotednl)
3559 CNL_SAVE_DEFINEDEF ();
3560 else
3561 CNL ();
3562 break;
3563 } /* switch (c) */
3565 } /* while not eof */
3567 free (token_name.buffer);
3568 free (lbs[0].lb.buffer);
3569 free (lbs[1].lb.buffer);
3573 * Process either a C++ file or a C file depending on the setting
3574 * of a global flag.
3576 static void
3577 default_C_entries (inf)
3578 FILE *inf;
3580 C_entries (cplusplus ? C_PLPL : C_AUTO, inf);
3583 /* Always do plain C. */
3584 static void
3585 plain_C_entries (inf)
3586 FILE *inf;
3588 C_entries (0, inf);
3591 /* Always do C++. */
3592 static void
3593 Cplusplus_entries (inf)
3594 FILE *inf;
3596 C_entries (C_PLPL, inf);
3599 /* Always do Java. */
3600 static void
3601 Cjava_entries (inf)
3602 FILE *inf;
3604 C_entries (C_JAVA, inf);
3607 /* Always do C*. */
3608 static void
3609 Cstar_entries (inf)
3610 FILE *inf;
3612 C_entries (C_STAR, inf);
3615 /* Always do Yacc. */
3616 static void
3617 Yacc_entries (inf)
3618 FILE *inf;
3620 C_entries (YACC, inf);
3624 /* Useful macros. */
3625 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3626 for (lineno = charno = 0; /* loop initialization */ \
3627 !feof (file_pointer) /* loop test */ \
3628 && (lineno++, /* instructions at start of loop */ \
3629 linecharno = charno, \
3630 charno += readline (&line_buffer, file_pointer), \
3631 char_pointer = lb.buffer, \
3632 TRUE); \
3634 #define LOOKING_AT(cp, keyword) /* keyword is a constant string */ \
3635 (strneq ((cp), keyword, sizeof(keyword)-1) /* cp points at keyword */ \
3636 && notinname ((cp)[sizeof(keyword)-1]) /* end of keyword */ \
3637 && ((cp) = skip_spaces((cp)+sizeof(keyword)-1))) /* skip spaces */
3640 * Read a file, but do no processing. This is used to do regexp
3641 * matching on files that have no language defined.
3643 static void
3644 just_read_file (inf)
3645 FILE *inf;
3647 register char *dummy;
3649 LOOP_ON_INPUT_LINES (inf, lb, dummy)
3650 continue;
3654 /* Fortran parsing */
3656 static void F_takeprec __P((void));
3657 static void F_getit __P((FILE *));
3659 static void
3660 F_takeprec ()
3662 dbp = skip_spaces (dbp);
3663 if (*dbp != '*')
3664 return;
3665 dbp++;
3666 dbp = skip_spaces (dbp);
3667 if (strneq (dbp, "(*)", 3))
3669 dbp += 3;
3670 return;
3672 if (!ISDIGIT (*dbp))
3674 --dbp; /* force failure */
3675 return;
3678 dbp++;
3679 while (ISDIGIT (*dbp));
3682 static void
3683 F_getit (inf)
3684 FILE *inf;
3686 register char *cp;
3688 dbp = skip_spaces (dbp);
3689 if (*dbp == '\0')
3691 lineno++;
3692 linecharno = charno;
3693 charno += readline (&lb, inf);
3694 dbp = lb.buffer;
3695 if (dbp[5] != '&')
3696 return;
3697 dbp += 6;
3698 dbp = skip_spaces (dbp);
3700 if (!ISALPHA (*dbp) && *dbp != '_' && *dbp != '$')
3701 return;
3702 for (cp = dbp + 1; *cp != '\0' && intoken (*cp); cp++)
3703 continue;
3704 pfnote (savenstr (dbp, cp-dbp), TRUE,
3705 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3709 static void
3710 Fortran_functions (inf)
3711 FILE *inf;
3713 LOOP_ON_INPUT_LINES (inf, lb, dbp)
3715 if (*dbp == '%')
3716 dbp++; /* Ratfor escape to fortran */
3717 dbp = skip_spaces (dbp);
3718 if (*dbp == '\0')
3719 continue;
3720 switch (lowcase (*dbp))
3722 case 'i':
3723 if (nocase_tail ("integer"))
3724 F_takeprec ();
3725 break;
3726 case 'r':
3727 if (nocase_tail ("real"))
3728 F_takeprec ();
3729 break;
3730 case 'l':
3731 if (nocase_tail ("logical"))
3732 F_takeprec ();
3733 break;
3734 case 'c':
3735 if (nocase_tail ("complex") || nocase_tail ("character"))
3736 F_takeprec ();
3737 break;
3738 case 'd':
3739 if (nocase_tail ("double"))
3741 dbp = skip_spaces (dbp);
3742 if (*dbp == '\0')
3743 continue;
3744 if (nocase_tail ("precision"))
3745 break;
3746 continue;
3748 break;
3750 dbp = skip_spaces (dbp);
3751 if (*dbp == '\0')
3752 continue;
3753 switch (lowcase (*dbp))
3755 case 'f':
3756 if (nocase_tail ("function"))
3757 F_getit (inf);
3758 continue;
3759 case 's':
3760 if (nocase_tail ("subroutine"))
3761 F_getit (inf);
3762 continue;
3763 case 'e':
3764 if (nocase_tail ("entry"))
3765 F_getit (inf);
3766 continue;
3767 case 'b':
3768 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
3770 dbp = skip_spaces (dbp);
3771 if (*dbp == '\0') /* assume un-named */
3772 pfnote (savestr ("blockdata"), TRUE,
3773 lb.buffer, dbp - lb.buffer, lineno, linecharno);
3774 else
3775 F_getit (inf); /* look for name */
3777 continue;
3784 * Ada parsing
3785 * Original code by
3786 * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be> (1998)
3789 static void Ada_getit __P((FILE *, char *));
3791 /* Once we are positioned after an "interesting" keyword, let's get
3792 the real tag value necessary. */
3793 static void
3794 Ada_getit (inf, name_qualifier)
3795 FILE *inf;
3796 char *name_qualifier;
3798 register char *cp;
3799 char *name;
3800 char c;
3802 while (!feof (inf))
3804 dbp = skip_spaces (dbp);
3805 if (*dbp == '\0'
3806 || (dbp[0] == '-' && dbp[1] == '-'))
3808 lineno++;
3809 linecharno = charno;
3810 charno += readline (&lb, inf);
3811 dbp = lb.buffer;
3813 switch (lowcase(*dbp))
3815 case 'b':
3816 if (nocase_tail ("body"))
3818 /* Skipping body of procedure body or package body or ....
3819 resetting qualifier to body instead of spec. */
3820 name_qualifier = "/b";
3821 continue;
3823 break;
3824 case 't':
3825 /* Skipping type of task type or protected type ... */
3826 if (nocase_tail ("type"))
3827 continue;
3828 break;
3830 if (*dbp == '"')
3832 dbp += 1;
3833 for (cp = dbp; *cp != '\0' && *cp != '"'; cp++)
3834 continue;
3836 else
3838 dbp = skip_spaces (dbp);
3839 for (cp = dbp;
3840 (*cp != '\0'
3841 && (ISALPHA (*cp) || ISDIGIT (*cp) || *cp == '_' || *cp == '.'));
3842 cp++)
3843 continue;
3844 if (cp == dbp)
3845 return;
3847 c = *cp;
3848 *cp = '\0';
3849 name = concat (dbp, name_qualifier, "");
3850 *cp = c;
3851 pfnote (name, TRUE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3852 if (c == '"')
3853 dbp = cp + 1;
3854 return;
3858 static void
3859 Ada_funcs (inf)
3860 FILE *inf;
3862 bool inquote = FALSE;
3864 LOOP_ON_INPUT_LINES (inf, lb, dbp)
3866 while (*dbp != '\0')
3868 /* Skip a string i.e. "abcd". */
3869 if (inquote || (*dbp == '"'))
3871 dbp = etags_strchr ((inquote) ? dbp : dbp+1, '"');
3872 if (dbp != NULL)
3874 inquote = FALSE;
3875 dbp += 1;
3876 continue; /* advance char */
3878 else
3880 inquote = TRUE;
3881 break; /* advance line */
3885 /* Skip comments. */
3886 if (dbp[0] == '-' && dbp[1] == '-')
3887 break; /* advance line */
3889 /* Skip character enclosed in single quote i.e. 'a'
3890 and skip single quote starting an attribute i.e. 'Image. */
3891 if (*dbp == '\'')
3893 dbp++ ;
3894 if (*dbp != '\0')
3895 dbp++;
3896 continue;
3899 /* Search for beginning of a token. */
3900 if (!begtoken (*dbp))
3902 dbp++;
3903 continue; /* advance char */
3906 /* We are at the beginning of a token. */
3907 switch (lowcase(*dbp))
3909 case 'f':
3910 if (!packages_only && nocase_tail ("function"))
3911 Ada_getit (inf, "/f");
3912 else
3913 break; /* from switch */
3914 continue; /* advance char */
3915 case 'p':
3916 if (!packages_only && nocase_tail ("procedure"))
3917 Ada_getit (inf, "/p");
3918 else if (nocase_tail ("package"))
3919 Ada_getit (inf, "/s");
3920 else if (nocase_tail ("protected")) /* protected type */
3921 Ada_getit (inf, "/t");
3922 else
3923 break; /* from switch */
3924 continue; /* advance char */
3925 case 't':
3926 if (!packages_only && nocase_tail ("task"))
3927 Ada_getit (inf, "/k");
3928 else if (typedefs && !packages_only && nocase_tail ("type"))
3930 Ada_getit (inf, "/t");
3931 while (*dbp != '\0')
3932 dbp += 1;
3934 else
3935 break; /* from switch */
3936 continue; /* advance char */
3939 /* Look for the end of the token. */
3940 while (!endtoken (*dbp))
3941 dbp++;
3943 } /* advance char */
3944 } /* advance line */
3949 * Unix and microcontroller assembly tag handling
3950 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
3951 * Idea by Bob Weiner, Motorola Inc. (1994)
3953 static void
3954 Asm_labels (inf)
3955 FILE *inf;
3957 register char *cp;
3959 LOOP_ON_INPUT_LINES (inf, lb, cp)
3961 /* If first char is alphabetic or one of [_.$], test for colon
3962 following identifier. */
3963 if (ISALPHA (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
3965 /* Read past label. */
3966 cp++;
3967 while (ISALNUM (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
3968 cp++;
3969 if (*cp == ':' || iswhite (*cp))
3971 /* Found end of label, so copy it and add it to the table. */
3972 pfnote (savenstr(lb.buffer, cp-lb.buffer), TRUE,
3973 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3981 * Perl support
3982 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
3983 * Perl variable names: /^(my|local).../
3984 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
3985 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
3986 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
3988 static void
3989 Perl_functions (inf)
3990 FILE *inf;
3992 char *package = savestr ("main"); /* current package name */
3993 register char *cp;
3995 LOOP_ON_INPUT_LINES (inf, lb, cp)
3997 skip_spaces(cp);
3999 if (LOOKING_AT (cp, "package"))
4001 free (package);
4002 package = get_tag (cp);
4003 if (package == NULL) /* can't parse package name */
4004 package = savestr ("");
4005 else
4006 package = savestr(package); /* make a copy */
4008 else if (LOOKING_AT (cp, "sub"))
4010 char *name, *fullname, *pos;
4011 char *sp = cp;
4013 while (!notinname (*cp))
4014 cp++;
4015 if (cp == sp)
4016 continue;
4017 name = savenstr (sp, cp-sp);
4018 if ((pos = etags_strchr (name, ':')) != NULL && pos[1] == ':')
4019 fullname = name;
4020 else
4021 fullname = concat (package, "::", name);
4022 pfnote (fullname, TRUE,
4023 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4024 if (name != fullname)
4025 free (name);
4027 else if (globals /* only if tagging global vars is enabled */
4028 && (LOOKING_AT (cp, "my") || LOOKING_AT (cp, "local")))
4030 /* After "my" or "local", but before any following paren or space. */
4031 char *varname = NULL;
4033 if (*cp == '$' || *cp == '@' || *cp == '%')
4035 char* varstart = ++cp;
4036 while (ISALNUM (*cp) || *cp == '_')
4037 cp++;
4038 varname = savenstr (varstart, cp-varstart);
4040 else
4042 /* Should be examining a variable list at this point;
4043 could insist on seeing an open parenthesis. */
4044 while (*cp != '\0' && *cp != ';' && *cp != '=' && *cp != ')')
4045 cp++;
4048 /* Perhaps I should back cp up one character, so the TAGS table
4049 doesn't mention (and so depend upon) the following char. */
4050 pfnote (varname, FALSE,
4051 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4058 * Python support
4059 * Look for /^def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4060 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4062 static void
4063 Python_functions (inf)
4064 FILE *inf;
4066 register char *cp;
4068 LOOP_ON_INPUT_LINES (inf, lb, cp)
4069 if (LOOKING_AT (cp, "def") || LOOKING_AT (cp, "class"))
4071 while (!notinname (*cp) && *cp != ':')
4072 cp++;
4073 pfnote (NULL, TRUE,
4074 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4080 * PHP support
4081 * Look for:
4082 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4083 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4084 * - /^[ \t]*define\(\"[^\"]+/
4085 * Only with --members:
4086 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4087 * Idea by Diez B. Roggisch (2001)
4089 static void
4090 PHP_functions (inf)
4091 FILE *inf;
4093 register char *cp;
4094 bool search_identifier = FALSE;
4096 LOOP_ON_INPUT_LINES (inf, lb, cp)
4098 cp = skip_spaces (cp);
4099 if (search_identifier
4100 && *cp != '\0')
4102 while (!notinname (*cp))
4103 cp++;
4104 pfnote (NULL, TRUE,
4105 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4106 search_identifier = FALSE;
4108 else if (LOOKING_AT (cp, "function"))
4110 if(*cp == '&')
4111 cp = skip_spaces (cp+1);
4112 if(*cp != '\0')
4114 while (!notinname (*cp))
4115 cp++;
4116 pfnote (NULL, TRUE,
4117 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4119 else
4120 search_identifier = TRUE;
4122 else if (LOOKING_AT (cp, "class"))
4124 if (*cp != '\0')
4126 while (*cp != '\0' && !iswhite (*cp))
4127 cp++;
4128 pfnote (NULL, FALSE,
4129 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4131 else
4132 search_identifier = TRUE;
4134 else if (strneq (cp, "define", 6)
4135 && (cp = skip_spaces (cp+6))
4136 && *cp++ == '('
4137 && (*cp == '"' || *cp == '\''))
4139 char quote = *cp++;
4140 while (*cp != quote && *cp != '\0')
4141 cp++;
4142 pfnote (NULL, FALSE,
4143 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4145 else if (members
4146 && LOOKING_AT (cp, "var")
4147 && *cp == '$')
4149 while (!notinname(*cp))
4150 cp++;
4151 pfnote (NULL, FALSE,
4152 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4159 * Cobol tag functions
4160 * We could look for anything that could be a paragraph name.
4161 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4162 * Idea by Corny de Souza (1993)
4164 static void
4165 Cobol_paragraphs (inf)
4166 FILE *inf;
4168 register char *bp, *ep;
4170 LOOP_ON_INPUT_LINES (inf, lb, bp)
4172 if (lb.len < 9)
4173 continue;
4174 bp += 8;
4176 /* If eoln, compiler option or comment ignore whole line. */
4177 if (bp[-1] != ' ' || !ISALNUM (bp[0]))
4178 continue;
4180 for (ep = bp; ISALNUM (*ep) || *ep == '-'; ep++)
4181 continue;
4182 if (*ep++ == '.')
4183 pfnote (savenstr (bp, ep-bp), TRUE,
4184 lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
4190 * Makefile support
4191 * Idea by Assar Westerlund <assar@sics.se> (2001)
4193 static void
4194 Makefile_targets (inf)
4195 FILE *inf;
4197 register char *bp;
4199 LOOP_ON_INPUT_LINES (inf, lb, bp)
4201 if (*bp == '\t' || *bp == '#')
4202 continue;
4203 while (*bp != '\0' && *bp != '=' && *bp != ':')
4204 bp++;
4205 if (*bp == ':')
4206 pfnote (savenstr (lb.buffer, bp - lb.buffer), TRUE,
4207 lb.buffer, bp - lb.buffer + 1, lineno, linecharno);
4213 * Pascal parsing
4214 * Original code by Mosur K. Mohan (1989)
4216 * Locates tags for procedures & functions. Doesn't do any type- or
4217 * var-definitions. It does look for the keyword "extern" or
4218 * "forward" immediately following the procedure statement; if found,
4219 * the tag is skipped.
4221 static void
4222 Pascal_functions (inf)
4223 FILE *inf;
4225 linebuffer tline; /* mostly copied from C_entries */
4226 long save_lcno;
4227 int save_lineno, save_len;
4228 char c, *cp, *namebuf;
4230 bool /* each of these flags is TRUE iff: */
4231 incomment, /* point is inside a comment */
4232 inquote, /* point is inside '..' string */
4233 get_tagname, /* point is after PROCEDURE/FUNCTION
4234 keyword, so next item = potential tag */
4235 found_tag, /* point is after a potential tag */
4236 inparms, /* point is within parameter-list */
4237 verify_tag; /* point has passed the parm-list, so the
4238 next token will determine whether this
4239 is a FORWARD/EXTERN to be ignored, or
4240 whether it is a real tag */
4242 save_lcno = save_lineno = save_len = 0; /* keep compiler quiet */
4243 namebuf = NULL; /* keep compiler quiet */
4244 lineno = 0;
4245 charno = 0;
4246 dbp = lb.buffer;
4247 *dbp = '\0';
4248 initbuffer (&tline);
4250 incomment = inquote = FALSE;
4251 found_tag = FALSE; /* have a proc name; check if extern */
4252 get_tagname = FALSE; /* have found "procedure" keyword */
4253 inparms = FALSE; /* found '(' after "proc" */
4254 verify_tag = FALSE; /* check if "extern" is ahead */
4257 while (!feof (inf)) /* long main loop to get next char */
4259 c = *dbp++;
4260 if (c == '\0') /* if end of line */
4262 lineno++;
4263 linecharno = charno;
4264 charno += readline (&lb, inf);
4265 dbp = lb.buffer;
4266 if (*dbp == '\0')
4267 continue;
4268 if (!((found_tag && verify_tag)
4269 || get_tagname))
4270 c = *dbp++; /* only if don't need *dbp pointing
4271 to the beginning of the name of
4272 the procedure or function */
4274 if (incomment)
4276 if (c == '}') /* within { } comments */
4277 incomment = FALSE;
4278 else if (c == '*' && *dbp == ')') /* within (* *) comments */
4280 dbp++;
4281 incomment = FALSE;
4283 continue;
4285 else if (inquote)
4287 if (c == '\'')
4288 inquote = FALSE;
4289 continue;
4291 else
4292 switch (c)
4294 case '\'':
4295 inquote = TRUE; /* found first quote */
4296 continue;
4297 case '{': /* found open { comment */
4298 incomment = TRUE;
4299 continue;
4300 case '(':
4301 if (*dbp == '*') /* found open (* comment */
4303 incomment = TRUE;
4304 dbp++;
4306 else if (found_tag) /* found '(' after tag, i.e., parm-list */
4307 inparms = TRUE;
4308 continue;
4309 case ')': /* end of parms list */
4310 if (inparms)
4311 inparms = FALSE;
4312 continue;
4313 case ';':
4314 if (found_tag && !inparms) /* end of proc or fn stmt */
4316 verify_tag = TRUE;
4317 break;
4319 continue;
4321 if (found_tag && verify_tag && (*dbp != ' '))
4323 /* check if this is an "extern" declaration */
4324 if (*dbp == '\0')
4325 continue;
4326 if (lowcase (*dbp == 'e'))
4328 if (nocase_tail ("extern")) /* superfluous, really! */
4330 found_tag = FALSE;
4331 verify_tag = FALSE;
4334 else if (lowcase (*dbp) == 'f')
4336 if (nocase_tail ("forward")) /* check for forward reference */
4338 found_tag = FALSE;
4339 verify_tag = FALSE;
4342 if (found_tag && verify_tag) /* not external proc, so make tag */
4344 found_tag = FALSE;
4345 verify_tag = FALSE;
4346 pfnote (namebuf, TRUE,
4347 tline.buffer, save_len, save_lineno, save_lcno);
4348 continue;
4351 if (get_tagname) /* grab name of proc or fn */
4353 if (*dbp == '\0')
4354 continue;
4356 /* save all values for later tagging */
4357 linebuffer_setlen (&tline, lb.len);
4358 strcpy (tline.buffer, lb.buffer);
4359 save_lineno = lineno;
4360 save_lcno = linecharno;
4362 /* grab block name */
4363 for (cp = dbp + 1; *cp != '\0' && !endtoken (*cp); cp++)
4364 continue;
4365 namebuf = savenstr (dbp, cp-dbp);
4366 dbp = cp; /* set dbp to e-o-token */
4367 save_len = dbp - lb.buffer + 1;
4368 get_tagname = FALSE;
4369 found_tag = TRUE;
4370 continue;
4372 /* and proceed to check for "extern" */
4374 else if (!incomment && !inquote && !found_tag)
4376 /* check for proc/fn keywords */
4377 switch (lowcase (c))
4379 case 'p':
4380 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4381 get_tagname = TRUE;
4382 continue;
4383 case 'f':
4384 if (nocase_tail ("unction"))
4385 get_tagname = TRUE;
4386 continue;
4389 } /* while not eof */
4391 free (tline.buffer);
4396 * Lisp tag functions
4397 * look for (def or (DEF, quote or QUOTE
4400 static void L_getit __P((void));
4402 static void
4403 L_getit ()
4405 if (*dbp == '\'') /* Skip prefix quote */
4406 dbp++;
4407 else if (*dbp == '(')
4409 dbp++;
4410 /* Try to skip "(quote " */
4411 if (!LOOKING_AT (dbp, "quote") && !LOOKING_AT (dbp, "QUOTE"))
4412 /* Ok, then skip "(" before name in (defstruct (foo)) */
4413 dbp = skip_spaces (dbp);
4415 get_tag (dbp);
4418 static void
4419 Lisp_functions (inf)
4420 FILE *inf;
4422 LOOP_ON_INPUT_LINES (inf, lb, dbp)
4424 if (dbp[0] != '(')
4425 continue;
4427 if (strneq (dbp+1, "def", 3) || strneq (dbp+1, "DEF", 3))
4429 dbp = skip_non_spaces (dbp);
4430 dbp = skip_spaces (dbp);
4431 L_getit ();
4433 else
4435 /* Check for (foo::defmumble name-defined ... */
4437 dbp++;
4438 while (!notinname (*dbp) && *dbp != ':');
4439 if (*dbp == ':')
4442 dbp++;
4443 while (*dbp == ':');
4445 if (strneq (dbp, "def", 3) || strneq (dbp, "DEF", 3))
4447 dbp = skip_non_spaces (dbp);
4448 dbp = skip_spaces (dbp);
4449 L_getit ();
4458 * Postscript tag functions
4459 * Just look for lines where the first character is '/'
4460 * Also look at "defineps" for PSWrap
4461 * Ideas by:
4462 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4463 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4465 static void
4466 Postscript_functions (inf)
4467 FILE *inf;
4469 register char *bp, *ep;
4471 LOOP_ON_INPUT_LINES (inf, lb, bp)
4473 if (bp[0] == '/')
4475 for (ep = bp+1;
4476 *ep != '\0' && *ep != ' ' && *ep != '{';
4477 ep++)
4478 continue;
4479 pfnote (savenstr (bp, ep-bp), TRUE,
4480 lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
4482 else if (LOOKING_AT (bp, "defineps"))
4483 get_tag (bp);
4489 * Scheme tag functions
4490 * look for (def... xyzzy
4491 * (def... (xyzzy
4492 * (def ... ((...(xyzzy ....
4493 * (set! xyzzy
4494 * Original code by Ken Haase (1985?)
4497 static void
4498 Scheme_functions (inf)
4499 FILE *inf;
4501 register char *bp;
4503 LOOP_ON_INPUT_LINES (inf, lb, bp)
4505 if (strneq (bp, "(def", 4) || strneq (bp, "(DEF", 4))
4507 bp = skip_non_spaces (bp+4);
4508 /* Skip over open parens and white space */
4509 while (notinname (*bp))
4510 bp++;
4511 get_tag (bp);
4513 if (LOOKING_AT (bp, "(SET!") || LOOKING_AT (bp, "(set!"))
4514 get_tag (bp);
4519 /* Find tags in TeX and LaTeX input files. */
4521 /* TEX_toktab is a table of TeX control sequences that define tags.
4522 Each TEX_tabent records one such control sequence.
4523 CONVERT THIS TO USE THE Stab TYPE!! */
4524 struct TEX_tabent
4526 char *name;
4527 int len;
4530 static struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
4532 /* Default set of control sequences to put into TEX_toktab.
4533 The value of environment var TEXTAGS is prepended to this. */
4535 static char *TEX_defenv = "\
4536 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4537 :part:appendix:entry:index";
4539 static void TEX_mode __P((FILE *));
4540 static struct TEX_tabent *TEX_decode_env __P((char *, char *));
4541 static int TEX_Token __P((char *));
4543 static char TEX_esc = '\\';
4544 static char TEX_opgrp = '{';
4545 static char TEX_clgrp = '}';
4548 * TeX/LaTeX scanning loop.
4550 static void
4551 TeX_commands (inf)
4552 FILE *inf;
4554 char *cp, *lasthit;
4555 register int i;
4557 /* Select either \ or ! as escape character. */
4558 TEX_mode (inf);
4560 /* Initialize token table once from environment. */
4561 if (!TEX_toktab)
4562 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
4564 LOOP_ON_INPUT_LINES (inf, lb, cp)
4566 lasthit = cp;
4567 /* Look at each esc in line. */
4568 while ((cp = etags_strchr (cp, TEX_esc)) != NULL)
4570 if (*++cp == '\0')
4571 break;
4572 linecharno += cp - lasthit;
4573 lasthit = cp;
4574 i = TEX_Token (lasthit);
4575 if (i >= 0)
4577 /* We seem to include the TeX command in the tag name.
4578 register char *p;
4579 for (p = lasthit + TEX_toktab[i].len;
4580 *p != '\0' && *p != TEX_clgrp;
4581 p++)
4582 continue; */
4583 pfnote (/*savenstr (lasthit, p-lasthit)*/ (char *)NULL, TRUE,
4584 lb.buffer, lb.len, lineno, linecharno);
4585 break; /* We only tag a line once */
4591 #define TEX_LESC '\\'
4592 #define TEX_SESC '!'
4593 #define TEX_cmt '%'
4595 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
4596 chars accordingly. */
4597 static void
4598 TEX_mode (inf)
4599 FILE *inf;
4601 int c;
4603 while ((c = getc (inf)) != EOF)
4605 /* Skip to next line if we hit the TeX comment char. */
4606 if (c == TEX_cmt)
4607 while (c != '\n')
4608 c = getc (inf);
4609 else if (c == TEX_LESC || c == TEX_SESC )
4610 break;
4613 if (c == TEX_LESC)
4615 TEX_esc = TEX_LESC;
4616 TEX_opgrp = '{';
4617 TEX_clgrp = '}';
4619 else
4621 TEX_esc = TEX_SESC;
4622 TEX_opgrp = '<';
4623 TEX_clgrp = '>';
4625 /* If the input file is compressed, inf is a pipe, and rewind may fail.
4626 No attempt is made to correct the situation. */
4627 rewind (inf);
4630 /* Read environment and prepend it to the default string.
4631 Build token table. */
4632 static struct TEX_tabent *
4633 TEX_decode_env (evarname, defenv)
4634 char *evarname;
4635 char *defenv;
4637 register char *env, *p;
4639 struct TEX_tabent *tab;
4640 int size, i;
4642 /* Append default string to environment. */
4643 env = getenv (evarname);
4644 if (!env)
4645 env = defenv;
4646 else
4648 char *oldenv = env;
4649 env = concat (oldenv, defenv, "");
4652 /* Allocate a token table */
4653 for (size = 1, p = env; p;)
4654 if ((p = etags_strchr (p, ':')) && *++p != '\0')
4655 size++;
4656 /* Add 1 to leave room for null terminator. */
4657 tab = xnew (size + 1, struct TEX_tabent);
4659 /* Unpack environment string into token table. Be careful about */
4660 /* zero-length strings (leading ':', "::" and trailing ':') */
4661 for (i = 0; *env;)
4663 p = etags_strchr (env, ':');
4664 if (!p) /* End of environment string. */
4665 p = env + strlen (env);
4666 if (p - env > 0)
4667 { /* Only non-zero strings. */
4668 tab[i].name = savenstr (env, p - env);
4669 tab[i].len = strlen (tab[i].name);
4670 i++;
4672 if (*p)
4673 env = p + 1;
4674 else
4676 tab[i].name = NULL; /* Mark end of table. */
4677 tab[i].len = 0;
4678 break;
4681 return tab;
4684 /* If the text at CP matches one of the tag-defining TeX command names,
4685 return the pointer to the first occurrence of that command in TEX_toktab.
4686 Otherwise return -1.
4687 Keep the capital `T' in `token' for dumb truncating compilers
4688 (this distinguishes it from `TEX_toktab' */
4689 static int
4690 TEX_Token (cp)
4691 char *cp;
4693 int i;
4695 for (i = 0; TEX_toktab[i].len > 0; i++)
4696 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
4697 return i;
4698 return -1;
4702 /* Texinfo support. Dave Love, Mar. 2000. */
4703 static void
4704 Texinfo_nodes (inf)
4705 FILE * inf;
4707 char *cp, *start;
4708 LOOP_ON_INPUT_LINES (inf, lb, cp)
4709 if (LOOKING_AT (cp, "@node"))
4711 start = cp;
4712 while (*cp != '\0' && *cp != ',')
4713 cp++;
4714 pfnote (savenstr (start, cp - start), TRUE,
4715 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
4721 * Prolog support
4723 * Assumes that the predicate or rule starts at column 0.
4724 * Only the first clause of a predicate or rule is added.
4725 * Original code by Sunichirou Sugou (1989)
4726 * Rewritten by Anders Lindgren (1996)
4728 static int prolog_pr __P((char *, char *));
4729 static void prolog_skip_comment __P((linebuffer *, FILE *));
4730 static int prolog_atom __P((char *, int));
4732 static void
4733 Prolog_functions (inf)
4734 FILE *inf;
4736 char *cp, *last;
4737 int len;
4738 int allocated;
4740 allocated = 0;
4741 len = 0;
4742 last = NULL;
4744 LOOP_ON_INPUT_LINES (inf, lb, cp)
4746 if (cp[0] == '\0') /* Empty line */
4747 continue;
4748 else if (iswhite (cp[0])) /* Not a predicate */
4749 continue;
4750 else if (cp[0] == '/' && cp[1] == '*') /* comment. */
4751 prolog_skip_comment (&lb, inf);
4752 else if ((len = prolog_pr (cp, last)) > 0)
4754 /* Predicate or rule. Store the function name so that we
4755 only generate a tag for the first clause. */
4756 if (last == NULL)
4757 last = xnew(len + 1, char);
4758 else if (len + 1 > allocated)
4759 xrnew (last, len + 1, char);
4760 allocated = len + 1;
4761 strncpy (last, cp, len);
4762 last[len] = '\0';
4768 static void
4769 prolog_skip_comment (plb, inf)
4770 linebuffer *plb;
4771 FILE *inf;
4773 char *cp;
4777 for (cp = plb->buffer; *cp != '\0'; cp++)
4778 if (cp[0] == '*' && cp[1] == '/')
4779 return;
4780 lineno++;
4781 linecharno += readline (plb, inf);
4783 while (!feof(inf));
4787 * A predicate or rule definition is added if it matches:
4788 * <beginning of line><Prolog Atom><whitespace>(
4789 * or <beginning of line><Prolog Atom><whitespace>:-
4791 * It is added to the tags database if it doesn't match the
4792 * name of the previous clause header.
4794 * Return the size of the name of the predicate or rule, or 0 if no
4795 * header was found.
4797 static int
4798 prolog_pr (s, last)
4799 char *s;
4800 char *last; /* Name of last clause. */
4802 int pos;
4803 int len;
4805 pos = prolog_atom (s, 0);
4806 if (pos < 1)
4807 return 0;
4809 len = pos;
4810 pos = skip_spaces (s + pos) - s;
4812 if ((s[pos] == '.'
4813 || (s[pos] == '(' && (pos += 1))
4814 || (s[pos] == ':' && s[pos + 1] == '-' && (pos += 2)))
4815 && (last == NULL /* save only the first clause */
4816 || len != strlen (last)
4817 || !strneq (s, last, len)))
4819 pfnote (savenstr (s, len), TRUE, s, pos, lineno, linecharno);
4820 return len;
4822 else
4823 return 0;
4827 * Consume a Prolog atom.
4828 * Return the number of bytes consumed, or -1 if there was an error.
4830 * A prolog atom, in this context, could be one of:
4831 * - An alphanumeric sequence, starting with a lower case letter.
4832 * - A quoted arbitrary string. Single quotes can escape themselves.
4833 * Backslash quotes everything.
4835 static int
4836 prolog_atom (s, pos)
4837 char *s;
4838 int pos;
4840 int origpos;
4842 origpos = pos;
4844 if (ISLOWER(s[pos]) || (s[pos] == '_'))
4846 /* The atom is unquoted. */
4847 pos++;
4848 while (ISALNUM(s[pos]) || (s[pos] == '_'))
4850 pos++;
4852 return pos - origpos;
4854 else if (s[pos] == '\'')
4856 pos++;
4858 while (1)
4860 if (s[pos] == '\'')
4862 pos++;
4863 if (s[pos] != '\'')
4864 break;
4865 pos++; /* A double quote */
4867 else if (s[pos] == '\0')
4868 /* Multiline quoted atoms are ignored. */
4869 return -1;
4870 else if (s[pos] == '\\')
4872 if (s[pos+1] == '\0')
4873 return -1;
4874 pos += 2;
4876 else
4877 pos++;
4879 return pos - origpos;
4881 else
4882 return -1;
4887 * Support for Erlang
4889 * Generates tags for functions, defines, and records.
4890 * Assumes that Erlang functions start at column 0.
4891 * Original code by Anders Lindgren (1996)
4893 static int erlang_func __P((char *, char *));
4894 static void erlang_attribute __P((char *));
4895 static int erlang_atom __P((char *, int));
4897 static void
4898 Erlang_functions (inf)
4899 FILE *inf;
4901 char *cp, *last;
4902 int len;
4903 int allocated;
4905 allocated = 0;
4906 len = 0;
4907 last = NULL;
4909 LOOP_ON_INPUT_LINES (inf, lb, cp)
4911 if (cp[0] == '\0') /* Empty line */
4912 continue;
4913 else if (iswhite (cp[0])) /* Not function nor attribute */
4914 continue;
4915 else if (cp[0] == '%') /* comment */
4916 continue;
4917 else if (cp[0] == '"') /* Sometimes, strings start in column one */
4918 continue;
4919 else if (cp[0] == '-') /* attribute, e.g. "-define" */
4921 erlang_attribute (cp);
4922 last = NULL;
4924 else if ((len = erlang_func (cp, last)) > 0)
4927 * Function. Store the function name so that we only
4928 * generates a tag for the first clause.
4930 if (last == NULL)
4931 last = xnew (len + 1, char);
4932 else if (len + 1 > allocated)
4933 xrnew (last, len + 1, char);
4934 allocated = len + 1;
4935 strncpy (last, cp, len);
4936 last[len] = '\0';
4943 * A function definition is added if it matches:
4944 * <beginning of line><Erlang Atom><whitespace>(
4946 * It is added to the tags database if it doesn't match the
4947 * name of the previous clause header.
4949 * Return the size of the name of the function, or 0 if no function
4950 * was found.
4952 static int
4953 erlang_func (s, last)
4954 char *s;
4955 char *last; /* Name of last clause. */
4957 int pos;
4958 int len;
4960 pos = erlang_atom (s, 0);
4961 if (pos < 1)
4962 return 0;
4964 len = pos;
4965 pos = skip_spaces (s + pos) - s;
4967 /* Save only the first clause. */
4968 if (s[pos++] == '('
4969 && (last == NULL
4970 || len != (int)strlen (last)
4971 || !strneq (s, last, len)))
4973 pfnote (savenstr (s, len), TRUE, s, pos, lineno, linecharno);
4974 return len;
4977 return 0;
4982 * Handle attributes. Currently, tags are generated for defines
4983 * and records.
4985 * They are on the form:
4986 * -define(foo, bar).
4987 * -define(Foo(M, N), M+N).
4988 * -record(graph, {vtab = notable, cyclic = true}).
4990 static void
4991 erlang_attribute (s)
4992 char *s;
4994 int pos;
4995 int len;
4997 if (LOOKING_AT (s, "-define") || LOOKING_AT (s, "-record"))
4999 if (s[pos++] == '(')
5001 pos = skip_spaces (s + pos) - s;
5002 len = erlang_atom (s, pos);
5003 if (len != 0)
5004 pfnote (savenstr (& s[pos], len), TRUE,
5005 s, pos + len, lineno, linecharno);
5008 return;
5013 * Consume an Erlang atom (or variable).
5014 * Return the number of bytes consumed, or -1 if there was an error.
5016 static int
5017 erlang_atom (s, pos)
5018 char *s;
5019 int pos;
5021 int origpos;
5023 origpos = pos;
5025 if (ISALPHA (s[pos]) || s[pos] == '_')
5027 /* The atom is unquoted. */
5028 pos++;
5029 while (ISALNUM (s[pos]) || s[pos] == '_')
5030 pos++;
5031 return pos - origpos;
5033 else if (s[pos] == '\'')
5035 pos++;
5037 while (1)
5039 if (s[pos] == '\'')
5041 pos++;
5042 break;
5044 else if (s[pos] == '\0')
5045 /* Multiline quoted atoms are ignored. */
5046 return -1;
5047 else if (s[pos] == '\\')
5049 if (s[pos+1] == '\0')
5050 return -1;
5051 pos += 2;
5053 else
5054 pos++;
5056 return pos - origpos;
5058 else
5059 return -1;
5063 #ifdef ETAGS_REGEXPS
5065 static char *scan_separators __P((char *));
5066 static void analyse_regex __P((char *, bool));
5067 static void add_regex __P((char *, bool, language *));
5068 static char *substitute __P((char *, char *, struct re_registers *));
5070 /* Take a string like "/blah/" and turn it into "blah", making sure
5071 that the first and last characters are the same, and handling
5072 quoted separator characters. Actually, stops on the occurrence of
5073 an unquoted separator. Also turns "\t" into a Tab character.
5074 Returns pointer to terminating separator. Works in place. Null
5075 terminates name string. */
5076 static char *
5077 scan_separators (name)
5078 char *name;
5080 char sep = name[0];
5081 char *copyto = name;
5082 bool quoted = FALSE;
5084 for (++name; *name != '\0'; ++name)
5086 if (quoted)
5088 if (*name == 't')
5089 *copyto++ = '\t';
5090 else if (*name == sep)
5091 *copyto++ = sep;
5092 else
5094 /* Something else is quoted, so preserve the quote. */
5095 *copyto++ = '\\';
5096 *copyto++ = *name;
5098 quoted = FALSE;
5100 else if (*name == '\\')
5101 quoted = TRUE;
5102 else if (*name == sep)
5103 break;
5104 else
5105 *copyto++ = *name;
5108 /* Terminate copied string. */
5109 *copyto = '\0';
5110 return name;
5113 /* Look at the argument of --regex or --no-regex and do the right
5114 thing. Same for each line of a regexp file. */
5115 static void
5116 analyse_regex (regex_arg, ignore_case)
5117 char *regex_arg;
5118 bool ignore_case;
5120 if (regex_arg == NULL)
5122 free_patterns (); /* --no-regex: remove existing regexps */
5123 return;
5126 /* A real --regexp option or a line in a regexp file. */
5127 switch (regex_arg[0])
5129 /* Comments in regexp file or null arg to --regex. */
5130 case '\0':
5131 case ' ':
5132 case '\t':
5133 break;
5135 /* Read a regex file. This is recursive and may result in a
5136 loop, which will stop when the file descriptors are exhausted. */
5137 case '@':
5139 FILE *regexfp;
5140 linebuffer regexbuf;
5141 char *regexfile = regex_arg + 1;
5143 /* regexfile is a file containing regexps, one per line. */
5144 regexfp = fopen (regexfile, "r");
5145 if (regexfp == NULL)
5147 pfatal (regexfile);
5148 return;
5150 initbuffer (&regexbuf);
5151 while (readline_internal (&regexbuf, regexfp) > 0)
5152 analyse_regex (regexbuf.buffer, ignore_case);
5153 free (regexbuf.buffer);
5154 fclose (regexfp);
5156 break;
5158 /* Regexp to be used for a specific language only. */
5159 case '{':
5161 language *lang;
5162 char *lang_name = regex_arg + 1;
5163 char *cp;
5165 for (cp = lang_name; *cp != '}'; cp++)
5166 if (*cp == '\0')
5168 error ("unterminated language name in regex: %s", regex_arg);
5169 return;
5171 *cp = '\0';
5172 lang = get_language_from_langname (lang_name);
5173 if (lang == NULL)
5174 return;
5175 add_regex (cp + 1, ignore_case, lang);
5177 break;
5179 /* Regexp to be used for any language. */
5180 default:
5181 add_regex (regex_arg, ignore_case, NULL);
5182 break;
5186 /* Turn a name, which is an ed-style (but Emacs syntax) regular
5187 expression, into a real regular expression by compiling it. */
5188 static void
5189 add_regex (regexp_pattern, ignore_case, lang)
5190 char *regexp_pattern;
5191 bool ignore_case;
5192 language *lang;
5194 static struct re_pattern_buffer zeropattern;
5195 char *name;
5196 const char *err;
5197 struct re_pattern_buffer *patbuf;
5198 pattern *pp;
5201 if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
5203 error ("%s: unterminated regexp", regexp_pattern);
5204 return;
5206 name = scan_separators (regexp_pattern);
5207 if (regexp_pattern[0] == '\0')
5209 error ("null regexp", (char *)NULL);
5210 return;
5212 (void) scan_separators (name);
5214 patbuf = xnew (1, struct re_pattern_buffer);
5215 *patbuf = zeropattern;
5216 if (ignore_case)
5217 patbuf->translate = lc_trans; /* translation table to fold case */
5219 err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
5220 if (err != NULL)
5222 error ("%s while compiling pattern", err);
5223 return;
5226 pp = p_head;
5227 p_head = xnew (1, pattern);
5228 p_head->regex = savestr (regexp_pattern);
5229 p_head->p_next = pp;
5230 p_head->lang = lang;
5231 p_head->pat = patbuf;
5232 p_head->name_pattern = savestr (name);
5233 p_head->error_signaled = FALSE;
5237 * Do the substitutions indicated by the regular expression and
5238 * arguments.
5240 static char *
5241 substitute (in, out, regs)
5242 char *in, *out;
5243 struct re_registers *regs;
5245 char *result, *t;
5246 int size, dig, diglen;
5248 result = NULL;
5249 size = strlen (out);
5251 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5252 if (out[size - 1] == '\\')
5253 fatal ("pattern error in \"%s\"", out);
5254 for (t = etags_strchr (out, '\\');
5255 t != NULL;
5256 t = etags_strchr (t + 2, '\\'))
5257 if (ISDIGIT (t[1]))
5259 dig = t[1] - '0';
5260 diglen = regs->end[dig] - regs->start[dig];
5261 size += diglen - 2;
5263 else
5264 size -= 1;
5266 /* Allocate space and do the substitutions. */
5267 result = xnew (size + 1, char);
5269 for (t = result; *out != '\0'; out++)
5270 if (*out == '\\' && ISDIGIT (*++out))
5272 dig = *out - '0';
5273 diglen = regs->end[dig] - regs->start[dig];
5274 strncpy (t, in + regs->start[dig], diglen);
5275 t += diglen;
5277 else
5278 *t++ = *out;
5279 *t = '\0';
5281 assert (t <= result + size && t - result == (int)strlen (result));
5283 return result;
5286 /* Deallocate all patterns. */
5287 static void
5288 free_patterns ()
5290 pattern *pp;
5291 while (p_head != NULL)
5293 pp = p_head->p_next;
5294 free (p_head->regex);
5295 free (p_head->name_pattern);
5296 free (p_head);
5297 p_head = pp;
5299 return;
5301 #endif /* ETAGS_REGEXPS */
5304 static bool
5305 nocase_tail (cp)
5306 char *cp;
5308 register int len = 0;
5310 while (*cp != '\0' && lowcase (*cp) == lowcase (dbp[len]))
5311 cp++, len++;
5312 if (*cp == '\0' && !intoken (dbp[len]))
5314 dbp += len;
5315 return TRUE;
5317 return FALSE;
5320 static char *
5321 get_tag (bp)
5322 register char *bp;
5324 register char *cp, *name;
5326 if (*bp == '\0')
5327 return NULL;
5328 /* Go till you get to white space or a syntactic break */
5329 for (cp = bp + 1; !notinname (*cp); cp++)
5330 continue;
5331 name = savenstr (bp, cp-bp);
5332 pfnote (name, TRUE,
5333 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
5334 return name;
5337 /* Initialize a linebuffer for use */
5338 static void
5339 initbuffer (lbp)
5340 linebuffer *lbp;
5342 lbp->size = (DEBUG) ? 3 : 200;
5343 lbp->buffer = xnew (lbp->size, char);
5344 lbp->buffer[0] = '\0';
5345 lbp->len = 0;
5349 * Read a line of text from `stream' into `lbp', excluding the
5350 * newline or CR-NL, if any. Return the number of characters read from
5351 * `stream', which is the length of the line including the newline.
5353 * On DOS or Windows we do not count the CR character, if any, before the
5354 * NL, in the returned length; this mirrors the behavior of emacs on those
5355 * platforms (for text files, it translates CR-NL to NL as it reads in the
5356 * file).
5358 static long
5359 readline_internal (lbp, stream)
5360 linebuffer *lbp;
5361 register FILE *stream;
5363 char *buffer = lbp->buffer;
5364 register char *p = lbp->buffer;
5365 register char *pend;
5366 int chars_deleted;
5368 pend = p + lbp->size; /* Separate to avoid 386/IX compiler bug. */
5370 while (1)
5372 register int c = getc (stream);
5373 if (p == pend)
5375 /* We're at the end of linebuffer: expand it. */
5376 lbp->size *= 2;
5377 xrnew (buffer, lbp->size, char);
5378 p += buffer - lbp->buffer;
5379 pend = buffer + lbp->size;
5380 lbp->buffer = buffer;
5382 if (c == EOF)
5384 *p = '\0';
5385 chars_deleted = 0;
5386 break;
5388 if (c == '\n')
5390 if (p > buffer && p[-1] == '\r')
5392 p -= 1;
5393 #ifdef DOS_NT
5394 /* Assume CRLF->LF translation will be performed by Emacs
5395 when loading this file, so CRs won't appear in the buffer.
5396 It would be cleaner to compensate within Emacs;
5397 however, Emacs does not know how many CRs were deleted
5398 before any given point in the file. */
5399 chars_deleted = 1;
5400 #else
5401 chars_deleted = 2;
5402 #endif
5404 else
5406 chars_deleted = 1;
5408 *p = '\0';
5409 break;
5411 *p++ = c;
5413 lbp->len = p - buffer;
5415 return lbp->len + chars_deleted;
5419 * Like readline_internal, above, but in addition try to match the
5420 * input line against relevant regular expressions.
5422 static long
5423 readline (lbp, stream)
5424 linebuffer *lbp;
5425 FILE *stream;
5427 /* Read new line. */
5428 long result = readline_internal (lbp, stream);
5430 if (!no_line_directive
5431 && result > 12 && strneq (lbp->buffer, "#line ", 6))
5433 int start, lno;
5435 if (sscanf (lbp->buffer, "#line %d \"%n", &lno, &start) == 1)
5437 char *endp = lbp->buffer + start;
5439 while ((endp = etags_strchr (endp, '"')) != NULL
5440 && endp[-1] == '\\')
5441 endp++;
5442 if (endp != NULL)
5444 int len = endp - (lbp->buffer + start);
5446 if (!strneq (curfile, lbp->buffer + start, len))
5447 curfile = savenstr (lbp->buffer + start, len);
5448 lineno = lno;
5449 nocharno = TRUE; /* do not use char position for tags */
5450 return readline (lbp, stream);
5454 #ifdef ETAGS_REGEXPS
5456 int match;
5457 pattern *pp;
5459 /* Match against relevant patterns. */
5460 if (lbp->len > 0)
5461 for (pp = p_head; pp != NULL; pp = pp->p_next)
5463 /* Only use generic regexps or those for the current language. */
5464 if (pp->lang != NULL && pp->lang != curlang)
5465 continue;
5467 match = re_match (pp->pat, lbp->buffer, lbp->len, 0, &pp->regs);
5468 switch (match)
5470 case -2:
5471 /* Some error. */
5472 if (!pp->error_signaled)
5474 error ("error while matching \"%s\"", pp->regex);
5475 pp->error_signaled = TRUE;
5477 break;
5478 case -1:
5479 /* No match. */
5480 break;
5481 default:
5482 /* Match occurred. Construct a tag. */
5483 if (pp->name_pattern[0] != '\0')
5485 /* Make a named tag. */
5486 char *name = substitute (lbp->buffer,
5487 pp->name_pattern, &pp->regs);
5488 if (name != NULL)
5489 pfnote (name, TRUE, lbp->buffer, match, lineno, linecharno);
5491 else
5493 /* Make an unnamed tag. */
5494 pfnote ((char *)NULL, TRUE,
5495 lbp->buffer, match, lineno, linecharno);
5497 break;
5501 #endif /* ETAGS_REGEXPS */
5503 return result;
5508 * Return a pointer to a space of size strlen(cp)+1 allocated
5509 * with xnew where the string CP has been copied.
5511 static char *
5512 savestr (cp)
5513 char *cp;
5515 return savenstr (cp, strlen (cp));
5519 * Return a pointer to a space of size LEN+1 allocated with xnew where
5520 * the string CP has been copied for at most the first LEN characters.
5522 static char *
5523 savenstr (cp, len)
5524 char *cp;
5525 int len;
5527 register char *dp;
5529 dp = xnew (len + 1, char);
5530 strncpy (dp, cp, len);
5531 dp[len] = '\0';
5532 return dp;
5536 * Return the ptr in sp at which the character c last
5537 * appears; NULL if not found
5539 * Identical to POSIX strrchr, included for portability.
5541 static char *
5542 etags_strrchr (sp, c)
5543 register const char *sp;
5544 register int c;
5546 register const char *r;
5548 r = NULL;
5551 if (*sp == c)
5552 r = sp;
5553 } while (*sp++);
5554 return (char *)r;
5559 * Return the ptr in sp at which the character c first
5560 * appears; NULL if not found
5562 * Identical to POSIX strchr, included for portability.
5564 static char *
5565 etags_strchr (sp, c)
5566 register const char *sp;
5567 register int c;
5571 if (*sp == c)
5572 return (char *)sp;
5573 } while (*sp++);
5574 return NULL;
5577 /* Skip spaces, return new pointer. */
5578 static char *
5579 skip_spaces (cp)
5580 char *cp;
5582 while (iswhite (*cp))
5583 cp++;
5584 return cp;
5587 /* Skip non spaces, return new pointer. */
5588 static char *
5589 skip_non_spaces (cp)
5590 char *cp;
5592 while (*cp != '\0' && !iswhite (*cp))
5593 cp++;
5594 return cp;
5597 /* Print error message and exit. */
5598 void
5599 fatal (s1, s2)
5600 char *s1, *s2;
5602 error (s1, s2);
5603 exit (BAD);
5606 static void
5607 pfatal (s1)
5608 char *s1;
5610 perror (s1);
5611 exit (BAD);
5614 static void
5615 suggest_asking_for_help ()
5617 fprintf (stderr, "\tTry `%s %s' for a complete list of options.\n",
5618 progname,
5619 #ifdef LONG_OPTIONS
5620 "--help"
5621 #else
5622 "-h"
5623 #endif
5625 exit (BAD);
5628 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
5629 static void
5630 error (s1, s2)
5631 const char *s1, *s2;
5633 fprintf (stderr, "%s: ", progname);
5634 fprintf (stderr, s1, s2);
5635 fprintf (stderr, "\n");
5638 /* Return a newly-allocated string whose contents
5639 concatenate those of s1, s2, s3. */
5640 static char *
5641 concat (s1, s2, s3)
5642 char *s1, *s2, *s3;
5644 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
5645 char *result = xnew (len1 + len2 + len3 + 1, char);
5647 strcpy (result, s1);
5648 strcpy (result + len1, s2);
5649 strcpy (result + len1 + len2, s3);
5650 result[len1 + len2 + len3] = '\0';
5652 return result;
5656 /* Does the same work as the system V getcwd, but does not need to
5657 guess the buffer size in advance. */
5658 static char *
5659 etags_getcwd ()
5661 #ifdef HAVE_GETCWD
5662 int bufsize = 200;
5663 char *path = xnew (bufsize, char);
5665 while (getcwd (path, bufsize) == NULL)
5667 if (errno != ERANGE)
5668 pfatal ("getcwd");
5669 bufsize *= 2;
5670 free (path);
5671 path = xnew (bufsize, char);
5674 canonicalize_filename (path);
5675 return path;
5677 #else /* not HAVE_GETCWD */
5678 #if MSDOS
5680 char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
5682 getwd (path);
5684 for (p = path; *p != '\0'; p++)
5685 if (*p == '\\')
5686 *p = '/';
5687 else
5688 *p = lowcase (*p);
5690 return strdup (path);
5691 #else /* not MSDOS */
5692 linebuffer path;
5693 FILE *pipe;
5695 initbuffer (&path);
5696 pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
5697 if (pipe == NULL || readline_internal (&path, pipe) == 0)
5698 pfatal ("pwd");
5699 pclose (pipe);
5701 return path.buffer;
5702 #endif /* not MSDOS */
5703 #endif /* not HAVE_GETCWD */
5706 /* Return a newly allocated string containing the file name of FILE
5707 relative to the absolute directory DIR (which should end with a slash). */
5708 static char *
5709 relative_filename (file, dir)
5710 char *file, *dir;
5712 char *fp, *dp, *afn, *res;
5713 int i;
5715 /* Find the common root of file and dir (with a trailing slash). */
5716 afn = absolute_filename (file, cwd);
5717 fp = afn;
5718 dp = dir;
5719 while (*fp++ == *dp++)
5720 continue;
5721 fp--, dp--; /* back to the first differing char */
5722 #ifdef DOS_NT
5723 if (fp == afn && afn[0] != '/') /* cannot build a relative name */
5724 return afn;
5725 #endif
5726 do /* look at the equal chars until '/' */
5727 fp--, dp--;
5728 while (*fp != '/');
5730 /* Build a sequence of "../" strings for the resulting relative file name. */
5731 i = 0;
5732 while ((dp = etags_strchr (dp + 1, '/')) != NULL)
5733 i += 1;
5734 res = xnew (3*i + strlen (fp + 1) + 1, char);
5735 res[0] = '\0';
5736 while (i-- > 0)
5737 strcat (res, "../");
5739 /* Add the file name relative to the common root of file and dir. */
5740 strcat (res, fp + 1);
5741 free (afn);
5743 return res;
5746 /* Return a newly allocated string containing the absolute file name
5747 of FILE given DIR (which should end with a slash). */
5748 static char *
5749 absolute_filename (file, dir)
5750 char *file, *dir;
5752 char *slashp, *cp, *res;
5754 if (filename_is_absolute (file))
5755 res = savestr (file);
5756 #ifdef DOS_NT
5757 /* We don't support non-absolute file names with a drive
5758 letter, like `d:NAME' (it's too much hassle). */
5759 else if (file[1] == ':')
5760 fatal ("%s: relative file names with drive letters not supported", file);
5761 #endif
5762 else
5763 res = concat (dir, file, "");
5765 /* Delete the "/dirname/.." and "/." substrings. */
5766 slashp = etags_strchr (res, '/');
5767 while (slashp != NULL && slashp[0] != '\0')
5769 if (slashp[1] == '.')
5771 if (slashp[2] == '.'
5772 && (slashp[3] == '/' || slashp[3] == '\0'))
5774 cp = slashp;
5776 cp--;
5777 while (cp >= res && !filename_is_absolute (cp));
5778 if (cp < res)
5779 cp = slashp; /* the absolute name begins with "/.." */
5780 #ifdef DOS_NT
5781 /* Under MSDOS and NT we get `d:/NAME' as absolute
5782 file name, so the luser could say `d:/../NAME'.
5783 We silently treat this as `d:/NAME'. */
5784 else if (cp[0] != '/')
5785 cp = slashp;
5786 #endif
5787 strcpy (cp, slashp + 3);
5788 slashp = cp;
5789 continue;
5791 else if (slashp[2] == '/' || slashp[2] == '\0')
5793 strcpy (slashp, slashp + 2);
5794 continue;
5798 slashp = etags_strchr (slashp + 1, '/');
5801 if (res[0] == '\0')
5802 return savestr ("/");
5803 else
5804 return res;
5807 /* Return a newly allocated string containing the absolute
5808 file name of dir where FILE resides given DIR (which should
5809 end with a slash). */
5810 static char *
5811 absolute_dirname (file, dir)
5812 char *file, *dir;
5814 char *slashp, *res;
5815 char save;
5817 canonicalize_filename (file);
5818 slashp = etags_strrchr (file, '/');
5819 if (slashp == NULL)
5820 return savestr (dir);
5821 save = slashp[1];
5822 slashp[1] = '\0';
5823 res = absolute_filename (file, dir);
5824 slashp[1] = save;
5826 return res;
5829 /* Whether the argument string is an absolute file name. The argument
5830 string must have been canonicalized with canonicalize_filename. */
5831 static bool
5832 filename_is_absolute (fn)
5833 char *fn;
5835 return (fn[0] == '/'
5836 #ifdef DOS_NT
5837 || (ISALPHA(fn[0]) && fn[1] == ':' && fn[2] == '/')
5838 #endif
5842 /* Translate backslashes into slashes. Works in place. */
5843 static void
5844 canonicalize_filename (fn)
5845 register char *fn;
5847 #ifdef DOS_NT
5848 /* Canonicalize drive letter case. */
5849 if (fn[0] != '\0' && fn[1] == ':' && ISLOWER (fn[0]))
5850 fn[0] = upcase (fn[0]);
5851 /* Convert backslashes to slashes. */
5852 for (; *fn != '\0'; fn++)
5853 if (*fn == '\\')
5854 *fn = '/';
5855 #else
5856 /* No action. */
5857 fn = NULL; /* shut up the compiler */
5858 #endif
5861 /* Set the minimum size of a string contained in a linebuffer. */
5862 static void
5863 linebuffer_setlen (lbp, toksize)
5864 linebuffer *lbp;
5865 int toksize;
5867 while (lbp->size <= toksize)
5869 lbp->size *= 2;
5870 xrnew (lbp->buffer, lbp->size, char);
5872 lbp->len = toksize;
5875 /* Like malloc but get fatal error if memory is exhausted. */
5876 static PTR
5877 xmalloc (size)
5878 unsigned int size;
5880 PTR result = (PTR) malloc (size);
5881 if (result == NULL)
5882 fatal ("virtual memory exhausted", (char *)NULL);
5883 return result;
5886 static PTR
5887 xrealloc (ptr, size)
5888 char *ptr;
5889 unsigned int size;
5891 PTR result = (PTR) realloc (ptr, size);
5892 if (result == NULL)
5893 fatal ("virtual memory exhausted", (char *)NULL);
5894 return result;
5898 * Local Variables:
5899 * c-indentation-style: gnu
5900 * indent-tabs-mode: t
5901 * tab-width: 8
5902 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer")
5903 * End: