1 /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
2 Copyright (C) 1984, 1987, 1988, 1989, 1993, 1994, 1995,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006 Free Software Foundation, Inc. and Ken Arnold
6 This file is not considered part of GNU Emacs.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 * Ctags originally by Ken Arnold.
25 * Fortran added by Jim Kleckner.
26 * Ed Pelegri-Llopart added C typedefs.
27 * Gnu Emacs TAGS format and modifications by RMS?
28 * 1989 Sam Kendall added C++.
29 * 1992 Joseph B. Wells improved C and C++ parsing.
30 * 1993 Francesco Potortì reorganised C and C++.
31 * 1994 Line-by-line regexp tags by Tom Tromey.
32 * 2001 Nested classes by Francesco Potortì (concept by Mykola Dzyuba).
33 * 2002 #line directives by Francesco Potortì.
35 * Francesco Potortì <pot@gnu.org> has maintained and improved it since 1993.
39 * If you want to add support for a new language, start by looking at the LUA
40 * language, which is the simplest. Alternatively, consider shipping a
41 * configuration file containing regexp definitions for etags.
44 char pot_etags_version
[] = "@(#) pot revision number is 17.17";
54 # define NDEBUG /* disable assert */
59 /* On some systems, Emacs defines static as nothing for the sake
60 of unexec. We don't want that here since we don't use unexec. */
62 # define ETAGS_REGEXPS /* use the regexp features */
63 # define LONG_OPTIONS /* accept long options */
64 # ifndef PTR /* for Xemacs */
67 # ifndef __P /* for Xemacs */
68 # define __P(args) args
70 #else /* no config.h */
71 # if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
72 # define __P(args) args /* use prototypes */
73 # define PTR void * /* for generic pointers */
74 # else /* not standard C */
75 # define __P(args) () /* no prototypes */
76 # define const /* remove const for old compilers' sake */
77 # define PTR long * /* don't use void* */
79 #endif /* !HAVE_CONFIG_H */
82 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
87 # define LONG_OPTIONS TRUE
89 # define LONG_OPTIONS FALSE
92 /* WIN32_NATIVE is for Xemacs.
93 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
98 #endif /* WIN32_NATIVE */
104 # include <sys/param.h>
106 # ifndef HAVE_CONFIG_H
108 # include <sys/config.h>
120 # define MAXPATHLEN _MAX_PATH
126 # endif /* undef HAVE_GETCWD */
127 #else /* not WINDOWSNT */
131 # else /* no standard C headers */
132 extern char *getenv ();
134 # define EXIT_SUCCESS 1
135 # define EXIT_FAILURE 0
137 # define EXIT_SUCCESS 0
138 # define EXIT_FAILURE 1
141 #endif /* !WINDOWSNT */
146 # if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
147 extern char *getcwd (char *buf
, size_t size
);
149 #endif /* HAVE_UNISTD_H */
157 #include <sys/types.h>
158 #include <sys/stat.h>
162 # undef assert /* some systems have a buggy assert.h */
163 # define assert(x) ((void) 0)
166 #if !defined (S_ISREG) && defined (S_IFREG)
167 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
173 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
175 extern int optind
, opterr
;
176 #endif /* LONG_OPTIONS */
179 # ifndef HAVE_CONFIG_H /* this is a standalone compilation */
180 # ifdef __CYGWIN__ /* compiling on Cygwin */
182 the regex
.h distributed with Cygwin is
not compatible with etags
, alas
!
183 If you want regular expression support
, you should
delete this notice
and
184 arrange to use the GNU regex
.h
and regex
.c
.
188 #endif /* ETAGS_REGEXPS */
190 /* Define CTAGS to make the program "ctags" compatible with the usual one.
191 Leave it undefined to make the program "etags", which makes emacs-style
192 tag tables and tags typedefs, #defines and struct/union/enum by default. */
200 #define streq(s,t) (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t))
201 #define strcaseeq(s,t) (assert((s)!=NULL && (t)!=NULL), !etags_strcasecmp (s, t))
202 #define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
203 #define strncaseeq(s,t,n) (assert((s)!=NULL && (t)!=NULL), !etags_strncasecmp (s, t, n))
205 #define CHARS 256 /* 2^sizeof(char) */
206 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
207 #define iswhite(c) (_wht[CHAR(c)]) /* c is white (see white) */
208 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name (see nonam) */
209 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token (see begtk) */
210 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token (see midtk) */
211 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens (see endtk) */
213 #define ISALNUM(c) isalnum (CHAR(c))
214 #define ISALPHA(c) isalpha (CHAR(c))
215 #define ISDIGIT(c) isdigit (CHAR(c))
216 #define ISLOWER(c) islower (CHAR(c))
218 #define lowcase(c) tolower (CHAR(c))
219 #define upcase(c) toupper (CHAR(c))
223 * xnew, xrnew -- allocate, reallocate storage
225 * SYNOPSIS: Type *xnew (int n, Type);
226 * void xrnew (OldPointer, int n, Type);
229 # include "chkmalloc.h"
230 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
231 (n) * sizeof (Type)))
232 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
233 (char *) (op), (n) * sizeof (Type)))
235 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
236 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
237 (char *) (op), (n) * sizeof (Type)))
242 typedef void Lang_function
__P((FILE *));
246 char *suffix
; /* file name suffix for this compressor */
247 char *command
; /* takes one arg and decompresses to stdout */
252 char *name
; /* language name */
253 char *help
; /* detailed help for the language */
254 Lang_function
*function
; /* parse function */
255 char **suffixes
; /* name suffixes of this language's files */
256 char **filenames
; /* names of this language's files */
257 char **interpreters
; /* interpreters for this language */
258 bool metasource
; /* source used to generate other sources */
263 struct fdesc
*next
; /* for the linked list */
264 char *infname
; /* uncompressed input file name */
265 char *infabsname
; /* absolute uncompressed input file name */
266 char *infabsdir
; /* absolute dir of input file */
267 char *taggedfname
; /* file name to write in tagfile */
268 language
*lang
; /* language of file */
269 char *prop
; /* file properties to write in tagfile */
270 bool usecharno
; /* etags tags shall contain char number */
271 bool written
; /* entry written in the tags file */
274 typedef struct node_st
275 { /* sorting structure */
276 struct node_st
*left
, *right
; /* left and right sons */
277 fdesc
*fdp
; /* description of file to whom tag belongs */
278 char *name
; /* tag name */
279 char *regex
; /* search regexp */
280 bool valid
; /* write this tag on the tag file */
281 bool is_func
; /* function tag: use regexp in CTAGS mode */
282 bool been_warned
; /* warning already given for duplicated tag */
283 int lno
; /* line number tag is on */
284 long cno
; /* character number line starts on */
288 * A `linebuffer' is a structure which holds a line of text.
289 * `readline_internal' reads a line from a stream into a linebuffer
290 * and works regardless of the length of the line.
291 * SIZE is the size of BUFFER, LEN is the length of the string in
292 * BUFFER after readline reads it.
301 /* Used to support mixing of --lang and file names. */
305 at_language
, /* a language specification */
306 at_regexp
, /* a regular expression */
307 at_filename
, /* a file name */
308 at_stdin
, /* read from stdin here */
309 at_end
/* stop parsing the list */
310 } arg_type
; /* argument type */
311 language
*lang
; /* language associated with the argument */
312 char *what
; /* the argument itself */
316 /* Structure defining a regular expression. */
317 typedef struct regexp
319 struct regexp
*p_next
; /* pointer to next in list */
320 language
*lang
; /* if set, use only for this language */
321 char *pattern
; /* the regexp pattern */
322 char *name
; /* tag name */
323 struct re_pattern_buffer
*pat
; /* the compiled pattern */
324 struct re_registers regs
; /* re registers */
325 bool error_signaled
; /* already signaled for this regexp */
326 bool force_explicit_name
; /* do not allow implict tag name */
327 bool ignore_case
; /* ignore case when matching */
328 bool multi_line
; /* do a multi-line match on the whole file */
330 #endif /* ETAGS_REGEXPS */
333 /* Many compilers barf on this:
334 Lang_function Ada_funcs;
335 so let's write it this way */
336 static void Ada_funcs
__P((FILE *));
337 static void Asm_labels
__P((FILE *));
338 static void C_entries
__P((int c_ext
, FILE *));
339 static void default_C_entries
__P((FILE *));
340 static void plain_C_entries
__P((FILE *));
341 static void Cjava_entries
__P((FILE *));
342 static void Cobol_paragraphs
__P((FILE *));
343 static void Cplusplus_entries
__P((FILE *));
344 static void Cstar_entries
__P((FILE *));
345 static void Erlang_functions
__P((FILE *));
346 static void Forth_words
__P((FILE *));
347 static void Fortran_functions
__P((FILE *));
348 static void HTML_labels
__P((FILE *));
349 static void Lisp_functions
__P((FILE *));
350 static void Lua_functions
__P((FILE *));
351 static void Makefile_targets
__P((FILE *));
352 static void Pascal_functions
__P((FILE *));
353 static void Perl_functions
__P((FILE *));
354 static void PHP_functions
__P((FILE *));
355 static void PS_functions
__P((FILE *));
356 static void Prolog_functions
__P((FILE *));
357 static void Python_functions
__P((FILE *));
358 static void Scheme_functions
__P((FILE *));
359 static void TeX_commands
__P((FILE *));
360 static void Texinfo_nodes
__P((FILE *));
361 static void Yacc_entries
__P((FILE *));
362 static void just_read_file
__P((FILE *));
364 static void print_language_names
__P((void));
365 static void print_version
__P((void));
366 static void print_help
__P((argument
*));
367 int main
__P((int, char **));
369 static compressor
*get_compressor_from_suffix
__P((char *, char **));
370 static language
*get_language_from_langname
__P((const char *));
371 static language
*get_language_from_interpreter
__P((char *));
372 static language
*get_language_from_filename
__P((char *, bool));
373 static void readline
__P((linebuffer
*, FILE *));
374 static long readline_internal
__P((linebuffer
*, FILE *));
375 static bool nocase_tail
__P((char *));
376 static void get_tag
__P((char *, char **));
379 static void analyse_regex
__P((char *));
380 static void free_regexps
__P((void));
381 static void regex_tag_multiline
__P((void));
382 #endif /* ETAGS_REGEXPS */
383 static void error
__P((const char *, const char *));
384 static void suggest_asking_for_help
__P((void));
385 void fatal
__P((char *, char *));
386 static void pfatal
__P((char *));
387 static void add_node
__P((node
*, node
**));
389 static void init
__P((void));
390 static void process_file_name
__P((char *, language
*));
391 static void process_file
__P((FILE *, char *, language
*));
392 static void find_entries
__P((FILE *));
393 static void free_tree
__P((node
*));
394 static void free_fdesc
__P((fdesc
*));
395 static void pfnote
__P((char *, bool, char *, int, int, long));
396 static void make_tag
__P((char *, int, bool, char *, int, int, long));
397 static void invalidate_nodes
__P((fdesc
*, node
**));
398 static void put_entries
__P((node
*));
400 static char *concat
__P((char *, char *, char *));
401 static char *skip_spaces
__P((char *));
402 static char *skip_non_spaces
__P((char *));
403 static char *savenstr
__P((char *, int));
404 static char *savestr
__P((char *));
405 static char *etags_strchr
__P((const char *, int));
406 static char *etags_strrchr
__P((const char *, int));
407 static int etags_strcasecmp
__P((const char *, const char *));
408 static int etags_strncasecmp
__P((const char *, const char *, int));
409 static char *etags_getcwd
__P((void));
410 static char *relative_filename
__P((char *, char *));
411 static char *absolute_filename
__P((char *, char *));
412 static char *absolute_dirname
__P((char *, char *));
413 static bool filename_is_absolute
__P((char *f
));
414 static void canonicalize_filename
__P((char *));
415 static void linebuffer_init
__P((linebuffer
*));
416 static void linebuffer_setlen
__P((linebuffer
*, int));
417 static PTR xmalloc
__P((unsigned int));
418 static PTR xrealloc
__P((char *, unsigned int));
421 static char searchar
= '/'; /* use /.../ searches */
423 static char *tagfile
; /* output file */
424 static char *progname
; /* name this program was invoked with */
425 static char *cwd
; /* current working directory */
426 static char *tagfiledir
; /* directory of tagfile */
427 static FILE *tagf
; /* ioptr for tags file */
429 static fdesc
*fdhead
; /* head of file description list */
430 static fdesc
*curfdp
; /* current file description */
431 static int lineno
; /* line number of current line */
432 static long charno
; /* current character number */
433 static long linecharno
; /* charno of start of current line */
434 static char *dbp
; /* pointer to start of current tag */
436 static const int invalidcharno
= -1;
438 static node
*nodehead
; /* the head of the binary tree of tags */
439 static node
*last_node
; /* the last node created */
441 static linebuffer lb
; /* the current line */
442 static linebuffer filebuf
; /* a buffer containing the whole file */
443 static linebuffer token_name
; /* a buffer containing a tag name */
445 /* boolean "functions" (see init) */
446 static bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
449 *white
= " \f\t\n\r\v",
451 *nonam
= " \f\t\n\r()=,;", /* look at make_tag before modifying! */
452 /* token ending chars */
453 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
454 /* token starting chars */
455 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
456 /* valid in-token chars */
457 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
459 static bool append_to_tagfile
; /* -a: append to tags */
460 /* The next four default to TRUE for etags, but to FALSE for ctags. */
461 static bool typedefs
; /* -t: create tags for C and Ada typedefs */
462 static bool typedefs_or_cplusplus
; /* -T: create tags for C typedefs, level */
463 /* 0 struct/enum/union decls, and C++ */
464 /* member functions. */
465 static bool constantypedefs
; /* -d: create tags for C #define, enum */
466 /* constants and variables. */
467 /* -D: opposite of -d. Default under ctags. */
468 static bool globals
; /* create tags for global variables */
469 static bool declarations
; /* --declarations: tag them and extern in C&Co*/
470 static bool members
; /* create tags for C member variables */
471 static bool no_line_directive
; /* ignore #line directives (undocumented) */
472 static bool update
; /* -u: update tags */
473 static bool vgrind_style
; /* -v: create vgrind style index output */
474 static bool no_warnings
; /* -w: suppress warnings */
475 static bool cxref_style
; /* -x: create cxref style output */
476 static bool cplusplus
; /* .[hc] means C++, not C */
477 static bool ignoreindent
; /* -I: ignore indentation in C */
478 static bool packages_only
; /* --packages-only: in Ada, only tag packages*/
480 /* STDIN is defined in LynxOS system headers */
485 #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
486 static bool parsing_stdin
; /* --parse-stdin used */
489 static regexp
*p_head
; /* list of all regexps */
490 static bool need_filebuf
; /* some regexes are multi-line */
492 # define need_filebuf FALSE
493 #endif /* ETAGS_REGEXPS */
496 static struct option longopts
[] =
498 { "append", no_argument
, NULL
, 'a' },
499 { "packages-only", no_argument
, &packages_only
, TRUE
},
500 { "c++", no_argument
, NULL
, 'C' },
501 { "declarations", no_argument
, &declarations
, TRUE
},
502 { "no-line-directive", no_argument
, &no_line_directive
, TRUE
},
503 { "help", no_argument
, NULL
, 'h' },
504 { "help", no_argument
, NULL
, 'H' },
505 { "ignore-indentation", no_argument
, NULL
, 'I' },
506 { "language", required_argument
, NULL
, 'l' },
507 { "members", no_argument
, &members
, TRUE
},
508 { "no-members", no_argument
, &members
, FALSE
},
509 { "output", required_argument
, NULL
, 'o' },
511 { "regex", required_argument
, NULL
, 'r' },
512 { "no-regex", no_argument
, NULL
, 'R' },
513 { "ignore-case-regex", required_argument
, NULL
, 'c' },
514 #endif /* ETAGS_REGEXPS */
515 { "parse-stdin", required_argument
, NULL
, STDIN
},
516 { "version", no_argument
, NULL
, 'V' },
518 #if CTAGS /* Ctags options */
519 { "backward-search", no_argument
, NULL
, 'B' },
520 { "cxref", no_argument
, NULL
, 'x' },
521 { "defines", no_argument
, NULL
, 'd' },
522 { "globals", no_argument
, &globals
, TRUE
},
523 { "typedefs", no_argument
, NULL
, 't' },
524 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
525 { "update", no_argument
, NULL
, 'u' },
526 { "vgrind", no_argument
, NULL
, 'v' },
527 { "no-warn", no_argument
, NULL
, 'w' },
529 #else /* Etags options */
530 { "no-defines", no_argument
, NULL
, 'D' },
531 { "no-globals", no_argument
, &globals
, FALSE
},
532 { "include", required_argument
, NULL
, 'i' },
536 #endif /* LONG_OPTIONS */
538 static compressor compressors
[] =
540 { "z", "gzip -d -c"},
541 { "Z", "gzip -d -c"},
542 { "gz", "gzip -d -c"},
543 { "GZ", "gzip -d -c"},
544 { "bz2", "bzip2 -d -c" },
553 static char *Ada_suffixes
[] =
554 { "ads", "adb", "ada", NULL
};
555 static char Ada_help
[] =
556 "In Ada code, functions, procedures, packages, tasks and types are\n\
557 tags. Use the `--packages-only' option to create tags for\n\
559 Ada tag names have suffixes indicating the type of entity:\n\
560 Entity type: Qualifier:\n\
561 ------------ ----------\n\
568 Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
569 body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
570 will just search for any tag `bidule'.";
573 static char *Asm_suffixes
[] =
574 { "a", /* Unix assembler */
575 "asm", /* Microcontroller assembly */
576 "def", /* BSO/Tasking definition includes */
577 "inc", /* Microcontroller include files */
578 "ins", /* Microcontroller include files */
579 "s", "sa", /* Unix assembler */
580 "S", /* cpp-processed Unix assembler */
581 "src", /* BSO/Tasking C compiler output */
584 static char Asm_help
[] =
585 "In assembler code, labels appearing at the beginning of a line,\n\
586 followed by a colon, are tags.";
589 /* Note that .c and .h can be considered C++, if the --c++ flag was
590 given, or if the `class' or `template' keyowrds are met inside the file.
591 That is why default_C_entries is called for these. */
592 static char *default_C_suffixes
[] =
594 static char default_C_help
[] =
595 "In C code, any C function or typedef is a tag, and so are\n\
596 definitions of `struct', `union' and `enum'. `#define' macro\n\
597 definitions and `enum' constants are tags unless you specify\n\
598 `--no-defines'. Global variables are tags unless you specify\n\
599 `--no-globals'. Use of `--no-globals' and `--no-defines'\n\
600 can make the tags table file much smaller.\n\
601 You can tag function declarations and external variables by\n\
602 using `--declarations', and struct members by using `--members'.";
604 static char *Cplusplus_suffixes
[] =
605 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
606 "M", /* Objective C++ */
607 "pdb", /* Postscript with C syntax */
609 static char Cplusplus_help
[] =
610 "In C++ code, all the tag constructs of C code are tagged. (Use\n\
611 --help --lang=c --lang=c++ for full help.)\n\
612 In addition to C tags, member functions are also recognized, and\n\
613 optionally member variables if you use the `--members' option.\n\
614 Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
615 and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
618 static char *Cjava_suffixes
[] =
620 static char Cjava_help
[] =
621 "In Java code, all the tags constructs of C and C++ code are\n\
622 tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
625 static char *Cobol_suffixes
[] =
626 { "COB", "cob", NULL
};
627 static char Cobol_help
[] =
628 "In Cobol code, tags are paragraph names; that is, any word\n\
629 starting in column 8 and followed by a period.";
631 static char *Cstar_suffixes
[] =
632 { "cs", "hs", NULL
};
634 static char *Erlang_suffixes
[] =
635 { "erl", "hrl", NULL
};
636 static char Erlang_help
[] =
637 "In Erlang code, the tags are the functions, records and macros\n\
638 defined in the file.";
640 char *Forth_suffixes
[] =
641 { "fth", "tok", NULL
};
642 static char Forth_help
[] =
643 "In Forth code, tags are words defined by `:',\n\
644 constant, code, create, defer, value, variable, buffer:, field.";
646 static char *Fortran_suffixes
[] =
647 { "F", "f", "f90", "for", NULL
};
648 static char Fortran_help
[] =
649 "In Fortran code, functions, subroutines and block data are tags.";
651 static char *HTML_suffixes
[] =
652 { "htm", "html", "shtml", NULL
};
653 static char HTML_help
[] =
654 "In HTML input files, the tags are the `title' and the `h1', `h2',\n\
655 `h3' headers. Also, tags are `name=' in anchors and all\n\
656 occurrences of `id='.";
658 static char *Lisp_suffixes
[] =
659 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
660 static char Lisp_help
[] =
661 "In Lisp code, any function defined with `defun', any variable\n\
662 defined with `defvar' or `defconst', and in general the first\n\
663 argument of any expression that starts with `(def' in column zero\n\
666 static char *Lua_suffixes
[] =
667 { "lua", "LUA", NULL
};
668 static char Lua_help
[] =
669 "In Lua scripts, all functions are tags.";
671 static char *Makefile_filenames
[] =
672 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
673 static char Makefile_help
[] =
674 "In makefiles, targets are tags; additionally, variables are tags\n\
675 unless you specify `--no-globals'.";
677 static char *Objc_suffixes
[] =
678 { "lm", /* Objective lex file */
679 "m", /* Objective C file */
681 static char Objc_help
[] =
682 "In Objective C code, tags include Objective C definitions for classes,\n\
683 class categories, methods and protocols. Tags for variables and\n\
684 functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.";
686 static char *Pascal_suffixes
[] =
687 { "p", "pas", NULL
};
688 static char Pascal_help
[] =
689 "In Pascal code, the tags are the functions and procedures defined\n\
692 static char *Perl_suffixes
[] =
693 { "pl", "pm", NULL
};
694 static char *Perl_interpreters
[] =
695 { "perl", "@PERL@", NULL
};
696 static char Perl_help
[] =
697 "In Perl code, the tags are the packages, subroutines and variables\n\
698 defined by the `package', `sub', `my' and `local' keywords. Use\n\
699 `--globals' if you want to tag global variables. Tags for\n\
700 subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
701 defined in the default package is `main::SUB'.";
703 static char *PHP_suffixes
[] =
704 { "php", "php3", "php4", NULL
};
705 static char PHP_help
[] =
706 "In PHP code, tags are functions, classes and defines. When using\n\
707 the `--members' option, vars are tags too.";
709 static char *plain_C_suffixes
[] =
710 { "pc", /* Pro*C file */
713 static char *PS_suffixes
[] =
714 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
715 static char PS_help
[] =
716 "In PostScript code, the tags are the functions.";
718 static char *Prolog_suffixes
[] =
720 static char Prolog_help
[] =
721 "In Prolog code, tags are predicates and rules at the beginning of\n\
724 static char *Python_suffixes
[] =
726 static char Python_help
[] =
727 "In Python code, `def' or `class' at the beginning of a line\n\
730 /* Can't do the `SCM' or `scm' prefix with a version number. */
731 static char *Scheme_suffixes
[] =
732 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
733 static char Scheme_help
[] =
734 "In Scheme code, tags include anything defined with `def' or with a\n\
735 construct whose name starts with `def'. They also include\n\
736 variables set with `set!' at top level in the file.";
738 static char *TeX_suffixes
[] =
739 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
740 static char TeX_help
[] =
741 "In LaTeX text, the argument of any of the commands `\\chapter',\n\
742 `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
743 `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
744 `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
745 `\\newenvironment' or `\\renewenvironment' is a tag.\n\
747 Other commands can be specified by setting the environment variable\n\
748 `TEXTAGS' to a colon-separated list like, for example,\n\
749 TEXTAGS=\"mycommand:myothercommand\".";
752 static char *Texinfo_suffixes
[] =
753 { "texi", "texinfo", "txi", NULL
};
754 static char Texinfo_help
[] =
755 "for texinfo files, lines starting with @node are tagged.";
757 static char *Yacc_suffixes
[] =
758 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
759 static char Yacc_help
[] =
760 "In Bison or Yacc input files, each rule defines as a tag the\n\
761 nonterminal it constructs. The portions of the file that contain\n\
762 C code are parsed as C code (use --help --lang=c --lang=yacc\n\
765 static char auto_help
[] =
766 "`auto' is not a real language, it indicates to use\n\
767 a default language for files base on file name suffix and file contents.";
769 static char none_help
[] =
770 "`none' is not a real language, it indicates to only do\n\
771 regexp processing on files.";
773 static char no_lang_help
[] =
774 "No detailed help available for this language.";
778 * Table of languages.
780 * It is ok for a given function to be listed under more than one
781 * name. I just didn't.
784 static language lang_names
[] =
786 { "ada", Ada_help
, Ada_funcs
, Ada_suffixes
},
787 { "asm", Asm_help
, Asm_labels
, Asm_suffixes
},
788 { "c", default_C_help
, default_C_entries
, default_C_suffixes
},
789 { "c++", Cplusplus_help
, Cplusplus_entries
, Cplusplus_suffixes
},
790 { "c*", no_lang_help
, Cstar_entries
, Cstar_suffixes
},
791 { "cobol", Cobol_help
, Cobol_paragraphs
, Cobol_suffixes
},
792 { "erlang", Erlang_help
, Erlang_functions
, Erlang_suffixes
},
793 { "forth", Forth_help
, Forth_words
, Forth_suffixes
},
794 { "fortran", Fortran_help
, Fortran_functions
, Fortran_suffixes
},
795 { "html", HTML_help
, HTML_labels
, HTML_suffixes
},
796 { "java", Cjava_help
, Cjava_entries
, Cjava_suffixes
},
797 { "lisp", Lisp_help
, Lisp_functions
, Lisp_suffixes
},
798 { "lua", Lua_help
, Lua_functions
, Lua_suffixes
},
799 { "makefile", Makefile_help
,Makefile_targets
,NULL
,Makefile_filenames
},
800 { "objc", Objc_help
, plain_C_entries
, Objc_suffixes
},
801 { "pascal", Pascal_help
, Pascal_functions
, Pascal_suffixes
},
802 { "perl",Perl_help
,Perl_functions
,Perl_suffixes
,NULL
,Perl_interpreters
},
803 { "php", PHP_help
, PHP_functions
, PHP_suffixes
},
804 { "postscript",PS_help
, PS_functions
, PS_suffixes
},
805 { "proc", no_lang_help
, plain_C_entries
, plain_C_suffixes
},
806 { "prolog", Prolog_help
, Prolog_functions
, Prolog_suffixes
},
807 { "python", Python_help
, Python_functions
, Python_suffixes
},
808 { "scheme", Scheme_help
, Scheme_functions
, Scheme_suffixes
},
809 { "tex", TeX_help
, TeX_commands
, TeX_suffixes
},
810 { "texinfo", Texinfo_help
, Texinfo_nodes
, Texinfo_suffixes
},
811 { "yacc", Yacc_help
,Yacc_entries
,Yacc_suffixes
,NULL
,NULL
,TRUE
},
812 { "auto", auto_help
}, /* default guessing scheme */
813 { "none", none_help
, just_read_file
}, /* regexp matching only */
814 { NULL
} /* end of list */
819 print_language_names ()
824 puts ("\nThese are the currently supported languages, along with the\n\
825 default file names and dot suffixes:");
826 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
828 printf (" %-*s", 10, lang
->name
);
829 if (lang
->filenames
!= NULL
)
830 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
831 printf (" %s", *name
);
832 if (lang
->suffixes
!= NULL
)
833 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
834 printf (" .%s", *ext
);
837 puts ("where `auto' means use default language for files based on file\n\
838 name suffix, and `none' means only do regexp processing on files.\n\
839 If no language is specified and no matching suffix is found,\n\
840 the first line of the file is read for a sharp-bang (#!) sequence\n\
841 followed by the name of an interpreter. If no such sequence is found,\n\
842 Fortran is tried first; if no tags are found, C is tried next.\n\
843 When parsing any C file, a \"class\" or \"template\" keyword\n\
845 puts ("Compressed files are supported using gzip and bzip2.\n\
847 For detailed help on a given language use, for example,\n\
848 etags --help --lang=ada.");
852 # define EMACS_NAME "standalone"
855 # define VERSION "version"
860 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
861 puts ("Copyright (C) 2006 Free Software Foundation, Inc. and Ken Arnold");
862 puts ("This program is distributed under the same terms as Emacs");
868 print_help (argbuffer
)
871 bool help_for_lang
= FALSE
;
873 for (; argbuffer
->arg_type
!= at_end
; argbuffer
++)
874 if (argbuffer
->arg_type
== at_language
)
878 puts (argbuffer
->lang
->help
);
879 help_for_lang
= TRUE
;
885 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
887 These are the options accepted by %s.\n", progname
, progname
);
889 puts ("You may use unambiguous abbreviations for the long option names.");
891 puts ("Long option names do not work with this executable, as it is not\n\
892 linked with GNU getopt.");
893 puts (" A - as file name means read names from stdin (one per line).\n\
894 Absolute names are stored in the output file as they are.\n\
895 Relative ones are stored relative to the output file's directory.\n");
897 puts ("-a, --append\n\
898 Append tag entries to existing tags file.");
900 puts ("--packages-only\n\
901 For Ada files, only generate tags for packages.");
904 puts ("-B, --backward-search\n\
905 Write the search commands for the tag entries using '?', the\n\
906 backward-search command instead of '/', the forward-search command.");
908 /* This option is mostly obsolete, because etags can now automatically
909 detect C++. Retained for backward compatibility and for debugging and
910 experimentation. In principle, we could want to tag as C++ even
911 before any "class" or "template" keyword.
913 Treat files whose name suffix defaults to C language as C++ files.");
916 puts ("--declarations\n\
917 In C and derived languages, create tags for function declarations,");
919 puts ("\tand create tags for extern variables if --globals is used.");
922 ("\tand create tags for extern variables unless --no-globals is used.");
925 puts ("-d, --defines\n\
926 Create tag entries for C #define constants and enum constants, too.");
928 puts ("-D, --no-defines\n\
929 Don't create tag entries for C #define constants and enum constants.\n\
930 This makes the tags file smaller.");
933 puts ("-i FILE, --include=FILE\n\
934 Include a note in tag file indicating that, when searching for\n\
935 a tag, one should also consult the tags file FILE after\n\
936 checking the current file.");
938 puts ("-l LANG, --language=LANG\n\
939 Force the following files to be considered as written in the\n\
940 named language up to the next --language=LANG option.");
944 Create tag entries for global variables in some languages.");
946 puts ("--no-globals\n\
947 Do not create tag entries for global variables in some\n\
948 languages. This makes the tags file smaller.");
950 Create tag entries for members of structures in some languages.");
953 puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
954 Make a tag for each line matching a regular expression pattern\n\
955 in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
956 files only. REGEXFILE is a file containing one REGEXP per line.\n\
957 REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
958 optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
959 puts (" If TAGNAME/ is present, the tags created are named.\n\
960 For example Tcl named tags can be created with:\n\
961 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
962 MODS are optional one-letter modifiers: `i' means to ignore case,\n\
963 `m' means to allow multi-line matches, `s' implies `m' and\n\
964 causes dot to match any character, including newline.");
965 puts ("-R, --no-regex\n\
966 Don't create tags from regexps for the following files.");
967 #endif /* ETAGS_REGEXPS */
968 puts ("-I, --ignore-indentation\n\
969 In C and C++ do not assume that a closing brace in the first\n\
970 column is the final brace of a function or structure definition.");
971 puts ("-o FILE, --output=FILE\n\
972 Write the tags to FILE.");
973 puts ("--parse-stdin=NAME\n\
974 Read from standard input and record tags as belonging to file NAME.");
978 puts ("-t, --typedefs\n\
979 Generate tag entries for C and Ada typedefs.");
980 puts ("-T, --typedefs-and-c++\n\
981 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
982 and C++ member functions.");
986 puts ("-u, --update\n\
987 Update the tag entries for the given files, leaving tag\n\
988 entries for other files in place. Currently, this is\n\
989 implemented by deleting the existing entries for the given\n\
990 files and then rewriting the new entries at the end of the\n\
991 tags file. It is often faster to simply rebuild the entire\n\
992 tag file than to use this.");
996 puts ("-v, --vgrind\n\
997 Print on the standard output an index of items intended for\n\
998 human consumption, similar to the output of vgrind. The index\n\
999 is sorted, and gives the page number of each item.");
1000 puts ("-w, --no-warn\n\
1001 Suppress warning messages about entries defined in multiple\n\
1003 puts ("-x, --cxref\n\
1004 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
1005 The output uses line numbers instead of page numbers, but\n\
1006 beyond that the differences are cosmetic; try both to see\n\
1010 puts ("-V, --version\n\
1011 Print the version of the program.\n\
1013 Print this help message.\n\
1014 Followed by one or more `--language' options prints detailed\n\
1015 help about tag generation for the specified languages.");
1017 print_language_names ();
1020 puts ("Report bugs to bug-gnu-emacs@gnu.org");
1022 exit (EXIT_SUCCESS
);
1026 #ifdef VMS /* VMS specific functions */
1030 /* This is a BUG! ANY arbitrary limit is a BUG!
1031 Won't someone please fix this? */
1032 #define MAX_FILE_SPEC_LEN 255
1035 char body
[MAX_FILE_SPEC_LEN
+ 1];
1039 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
1040 returning in each successive call the next file name matching the input
1041 spec. The function expects that each in_spec passed
1042 to it will be processed to completion; in particular, up to and
1043 including the call following that in which the last matching name
1044 is returned, the function ignores the value of in_spec, and will
1045 only start processing a new spec with the following call.
1046 If an error occurs, on return out_spec contains the value
1047 of in_spec when the error occurred.
1049 With each successive file name returned in out_spec, the
1050 function's return value is one. When there are no more matching
1051 names the function returns zero. If on the first call no file
1052 matches in_spec, or there is any other error, -1 is returned.
1056 #include <descrip.h>
1057 #define OUTSIZE MAX_FILE_SPEC_LEN
1063 static long context
= 0;
1064 static struct dsc$descriptor_s o
;
1065 static struct dsc$descriptor_s i
;
1066 static bool pass1
= TRUE
;
1073 o
.dsc$a_pointer
= (char *) out
;
1074 o
.dsc$w_length
= (short)OUTSIZE
;
1075 i
.dsc$a_pointer
= in
;
1076 i
.dsc$w_length
= (short)strlen(in
);
1077 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
1078 i
.dsc$b_class
= DSC$K_CLASS_S
;
1079 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
1080 o
.dsc$b_class
= DSC$K_CLASS_VS
;
1082 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
1084 out
->body
[out
->curlen
] = EOS
;
1087 else if (status
== RMS$_NMF
)
1091 strcpy(out
->body
, in
);
1094 lib$
find_file_end(&context
);
1100 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
1101 name of each file specified by the provided arg expanding wildcards.
1104 gfnames (arg
, p_error
)
1108 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
1110 switch (fn_exp (&filename
, arg
))
1114 return filename
.body
;
1120 return filename
.body
;
1124 #ifndef OLD /* Newer versions of VMS do provide `system'. */
1128 error ("%s", "system() function not implemented under VMS");
1132 #define VERSION_DELIM ';'
1133 char *massage_name (s
)
1139 if (*s
== VERSION_DELIM
)
1157 unsigned int nincluded_files
;
1158 char **included_files
;
1159 argument
*argbuffer
;
1160 int current_arg
, file_count
;
1161 linebuffer filename_lb
;
1162 bool help_asked
= FALSE
;
1171 _fmode
= O_BINARY
; /* all of files are treated as binary files */
1175 nincluded_files
= 0;
1176 included_files
= xnew (argc
, char *);
1180 /* Allocate enough no matter what happens. Overkill, but each one
1182 argbuffer
= xnew (argc
, argument
);
1185 * If etags, always find typedefs and structure tags. Why not?
1186 * Also default to find macro constants, enum constants and
1191 typedefs
= typedefs_or_cplusplus
= constantypedefs
= TRUE
;
1195 /* When the optstring begins with a '-' getopt_long does not rearrange the
1196 non-options arguments to be at the end, but leaves them alone. */
1198 #ifdef ETAGS_REGEXPS
1199 optstring
= "-r:Rc:";
1200 #endif /* ETAGS_REGEXPS */
1202 optstring
+= 1; /* remove the initial '-' */
1203 optstring
= concat (optstring
,
1205 (CTAGS
) ? "BxdtTuvw" : "Di:");
1207 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, NULL
)) != EOF
)
1211 /* If getopt returns 0, then it has already processed a
1212 long-named option. We should do nothing. */
1216 /* This means that a file name has been seen. Record it. */
1217 argbuffer
[current_arg
].arg_type
= at_filename
;
1218 argbuffer
[current_arg
].what
= optarg
;
1224 /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
1225 argbuffer
[current_arg
].arg_type
= at_stdin
;
1226 argbuffer
[current_arg
].what
= optarg
;
1230 fatal ("cannot parse standard input more than once", (char *)NULL
);
1231 parsing_stdin
= TRUE
;
1234 /* Common options. */
1235 case 'a': append_to_tagfile
= TRUE
; break;
1236 case 'C': cplusplus
= TRUE
; break;
1237 case 'f': /* for compatibility with old makefiles */
1241 error ("-o option may only be given once.", (char *)NULL
);
1242 suggest_asking_for_help ();
1248 case 'S': /* for backward compatibility */
1249 ignoreindent
= TRUE
;
1253 language
*lang
= get_language_from_langname (optarg
);
1256 argbuffer
[current_arg
].lang
= lang
;
1257 argbuffer
[current_arg
].arg_type
= at_language
;
1263 /* Backward compatibility: support obsolete --ignore-case-regexp. */
1264 optarg
= concat (optarg
, "i", ""); /* memory leak here */
1267 argbuffer
[current_arg
].arg_type
= at_regexp
;
1268 argbuffer
[current_arg
].what
= optarg
;
1272 argbuffer
[current_arg
].arg_type
= at_regexp
;
1273 argbuffer
[current_arg
].what
= NULL
;
1285 case 'D': constantypedefs
= FALSE
; break;
1286 case 'i': included_files
[nincluded_files
++] = optarg
; break;
1288 /* Ctags options. */
1289 case 'B': searchar
= '?'; break;
1290 case 'd': constantypedefs
= TRUE
; break;
1291 case 't': typedefs
= TRUE
; break;
1292 case 'T': typedefs
= typedefs_or_cplusplus
= TRUE
; break;
1293 case 'u': update
= TRUE
; break;
1294 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1295 case 'x': cxref_style
= TRUE
; break;
1296 case 'w': no_warnings
= TRUE
; break;
1298 suggest_asking_for_help ();
1302 /* No more options. Store the rest of arguments. */
1303 for (; optind
< argc
; optind
++)
1305 argbuffer
[current_arg
].arg_type
= at_filename
;
1306 argbuffer
[current_arg
].what
= argv
[optind
];
1311 argbuffer
[current_arg
].arg_type
= at_end
;
1314 print_help (argbuffer
);
1317 if (nincluded_files
== 0 && file_count
== 0)
1319 error ("no input files specified.", (char *)NULL
);
1320 suggest_asking_for_help ();
1324 if (tagfile
== NULL
)
1325 tagfile
= CTAGS
? "tags" : "TAGS";
1326 cwd
= etags_getcwd (); /* the current working directory */
1327 if (cwd
[strlen (cwd
) - 1] != '/')
1330 cwd
= concat (oldcwd
, "/", "");
1333 /* Relative file names are made relative to the current directory. */
1334 if (streq (tagfile
, "-")
1335 || strneq (tagfile
, "/dev/", 5))
1338 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1340 init (); /* set up boolean "functions" */
1342 linebuffer_init (&lb
);
1343 linebuffer_init (&filename_lb
);
1344 linebuffer_init (&filebuf
);
1345 linebuffer_init (&token_name
);
1349 if (streq (tagfile
, "-"))
1353 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1354 doesn't take effect until after `stdout' is already open). */
1355 if (!isatty (fileno (stdout
)))
1356 setmode (fileno (stdout
), O_BINARY
);
1360 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1366 * Loop through files finding functions.
1368 for (i
= 0; i
< current_arg
; i
++)
1370 static language
*lang
; /* non-NULL if language is forced */
1373 switch (argbuffer
[i
].arg_type
)
1376 lang
= argbuffer
[i
].lang
;
1378 #ifdef ETAGS_REGEXPS
1380 analyse_regex (argbuffer
[i
].what
);
1385 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1389 error ("can't find file %s\n", this_file
);
1394 this_file
= massage_name (this_file
);
1397 this_file
= argbuffer
[i
].what
;
1399 /* Input file named "-" means read file names from stdin
1400 (one per line) and use them. */
1401 if (streq (this_file
, "-"))
1404 fatal ("cannot parse standard input AND read file names from it",
1406 while (readline_internal (&filename_lb
, stdin
) > 0)
1407 process_file_name (filename_lb
.buffer
, lang
);
1410 process_file_name (this_file
, lang
);
1416 this_file
= argbuffer
[i
].what
;
1417 process_file (stdin
, this_file
, lang
);
1422 #ifdef ETAGS_REGEXPS
1424 #endif /* ETAGS_REGEXPS */
1426 free (filebuf
.buffer
);
1427 free (token_name
.buffer
);
1429 if (!CTAGS
|| cxref_style
)
1431 /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */
1432 put_entries (nodehead
);
1433 free_tree (nodehead
);
1439 /* Output file entries that have no tags. */
1440 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1442 fprintf (tagf
, "\f\n%s,0\n", fdp
->taggedfname
);
1444 while (nincluded_files
-- > 0)
1445 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1447 if (fclose (tagf
) == EOF
)
1451 exit (EXIT_SUCCESS
);
1457 for (i
= 0; i
< current_arg
; ++i
)
1459 switch (argbuffer
[i
].arg_type
)
1465 continue; /* the for loop */
1468 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1469 tagfile
, argbuffer
[i
].what
, tagfile
);
1470 if (system (cmd
) != EXIT_SUCCESS
)
1471 fatal ("failed to execute shell command", (char *)NULL
);
1473 append_to_tagfile
= TRUE
;
1476 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1479 put_entries (nodehead
); /* write all the tags (CTAGS) */
1480 free_tree (nodehead
);
1482 if (fclose (tagf
) == EOF
)
1486 if (append_to_tagfile
|| update
)
1488 char cmd
[2*BUFSIZ
+10];
1489 sprintf (cmd
, "sort -o %.*s %.*s", BUFSIZ
, tagfile
, BUFSIZ
, tagfile
);
1490 exit (system (cmd
));
1492 return EXIT_SUCCESS
;
1497 * Return a compressor given the file name. If EXTPTR is non-zero,
1498 * return a pointer into FILE where the compressor-specific
1499 * extension begins. If no compressor is found, NULL is returned
1500 * and EXTPTR is not significant.
1501 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1504 get_compressor_from_suffix (file
, extptr
)
1509 char *slash
, *suffix
;
1511 /* This relies on FN to be after canonicalize_filename,
1512 so we don't need to consider backslashes on DOS_NT. */
1513 slash
= etags_strrchr (file
, '/');
1514 suffix
= etags_strrchr (file
, '.');
1515 if (suffix
== NULL
|| suffix
< slash
)
1520 /* Let those poor souls who live with DOS 8+3 file name limits get
1521 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1522 Only the first do loop is run if not MSDOS */
1525 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1526 if (streq (compr
->suffix
, suffix
))
1529 break; /* do it only once: not really a loop */
1532 } while (*suffix
!= '\0');
1539 * Return a language given the name.
1542 get_language_from_langname (name
)
1548 error ("empty language name", (char *)NULL
);
1551 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1552 if (streq (name
, lang
->name
))
1554 error ("unknown language \"%s\"", name
);
1562 * Return a language given the interpreter name.
1565 get_language_from_interpreter (interpreter
)
1571 if (interpreter
== NULL
)
1573 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1574 if (lang
->interpreters
!= NULL
)
1575 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1576 if (streq (*iname
, interpreter
))
1585 * Return a language given the file name.
1588 get_language_from_filename (file
, case_sensitive
)
1590 bool case_sensitive
;
1593 char **name
, **ext
, *suffix
;
1595 /* Try whole file name first. */
1596 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1597 if (lang
->filenames
!= NULL
)
1598 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1599 if ((case_sensitive
)
1600 ? streq (*name
, file
)
1601 : strcaseeq (*name
, file
))
1604 /* If not found, try suffix after last dot. */
1605 suffix
= etags_strrchr (file
, '.');
1609 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1610 if (lang
->suffixes
!= NULL
)
1611 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1612 if ((case_sensitive
)
1613 ? streq (*ext
, suffix
)
1614 : strcaseeq (*ext
, suffix
))
1621 * This routine is called on each file argument.
1624 process_file_name (file
, lang
)
1628 struct stat stat_buf
;
1632 char *compressed_name
, *uncompressed_name
;
1633 char *ext
, *real_name
;
1636 canonicalize_filename (file
);
1637 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1639 error ("skipping inclusion of %s in self.", file
);
1642 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1644 compressed_name
= NULL
;
1645 real_name
= uncompressed_name
= savestr (file
);
1649 real_name
= compressed_name
= savestr (file
);
1650 uncompressed_name
= savenstr (file
, ext
- file
);
1653 /* If the canonicalized uncompressed name
1654 has already been dealt with, skip it silently. */
1655 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1657 assert (fdp
->infname
!= NULL
);
1658 if (streq (uncompressed_name
, fdp
->infname
))
1662 if (stat (real_name
, &stat_buf
) != 0)
1664 /* Reset real_name and try with a different name. */
1666 if (compressed_name
!= NULL
) /* try with the given suffix */
1668 if (stat (uncompressed_name
, &stat_buf
) == 0)
1669 real_name
= uncompressed_name
;
1671 else /* try all possible suffixes */
1673 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1675 compressed_name
= concat (file
, ".", compr
->suffix
);
1676 if (stat (compressed_name
, &stat_buf
) != 0)
1680 char *suf
= compressed_name
+ strlen (file
);
1681 size_t suflen
= strlen (compr
->suffix
) + 1;
1682 for ( ; suf
[1]; suf
++, suflen
--)
1684 memmove (suf
, suf
+ 1, suflen
);
1685 if (stat (compressed_name
, &stat_buf
) == 0)
1687 real_name
= compressed_name
;
1691 if (real_name
!= NULL
)
1694 free (compressed_name
);
1695 compressed_name
= NULL
;
1699 real_name
= compressed_name
;
1704 if (real_name
== NULL
)
1709 } /* try with a different name */
1711 if (!S_ISREG (stat_buf
.st_mode
))
1713 error ("skipping %s: it is not a regular file.", real_name
);
1716 if (real_name
== compressed_name
)
1718 char *cmd
= concat (compr
->command
, " ", real_name
);
1719 inf
= (FILE *) popen (cmd
, "r");
1723 inf
= fopen (real_name
, "r");
1730 process_file (inf
, uncompressed_name
, lang
);
1732 if (real_name
== compressed_name
)
1733 retval
= pclose (inf
);
1735 retval
= fclose (inf
);
1740 if (compressed_name
) free (compressed_name
);
1741 if (uncompressed_name
) free (uncompressed_name
);
1748 process_file (fh
, fn
, lang
)
1753 static const fdesc emptyfdesc
;
1756 /* Create a new input file description entry. */
1757 fdp
= xnew (1, fdesc
);
1760 fdp
->infname
= savestr (fn
);
1762 fdp
->infabsname
= absolute_filename (fn
, cwd
);
1763 fdp
->infabsdir
= absolute_dirname (fn
, cwd
);
1764 if (filename_is_absolute (fn
))
1766 /* An absolute file name. Canonicalize it. */
1767 fdp
->taggedfname
= absolute_filename (fn
, NULL
);
1771 /* A file name relative to cwd. Make it relative
1772 to the directory of the tags file. */
1773 fdp
->taggedfname
= relative_filename (fn
, tagfiledir
);
1775 fdp
->usecharno
= TRUE
; /* use char position when making tags */
1777 fdp
->written
= FALSE
; /* not written on tags file yet */
1780 curfdp
= fdhead
; /* the current file description */
1784 /* If not Ctags, and if this is not metasource and if it contained no #line
1785 directives, we can write the tags and free all nodes pointing to
1788 && curfdp
->usecharno
/* no #line directives in this file */
1789 && !curfdp
->lang
->metasource
)
1793 /* Look for the head of the sublist relative to this file. See add_node
1794 for the structure of the node tree. */
1796 for (np
= nodehead
; np
!= NULL
; prev
= np
, np
= np
->left
)
1797 if (np
->fdp
== curfdp
)
1800 /* If we generated tags for this file, write and delete them. */
1803 /* This is the head of the last sublist, if any. The following
1804 instructions depend on this being true. */
1805 assert (np
->left
== NULL
);
1807 assert (fdhead
== curfdp
);
1808 assert (last_node
->fdp
== curfdp
);
1809 put_entries (np
); /* write tags for file curfdp->taggedfname */
1810 free_tree (np
); /* remove the written nodes */
1812 nodehead
= NULL
; /* no nodes left */
1814 prev
->left
= NULL
; /* delete the pointer to the sublist */
1820 * This routine sets up the boolean pseudo-functions which work
1821 * by setting boolean flags dependent upon the corresponding character.
1822 * Every char which is NOT in that string is not a white char. Therefore,
1823 * all of the array "_wht" is set to FALSE, and then the elements
1824 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1825 * of a char is TRUE if it is the string "white", else FALSE.
1833 for (i
= 0; i
< CHARS
; i
++)
1834 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1835 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1836 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1837 notinname('\0') = notinname('\n');
1838 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1839 begtoken('\0') = begtoken('\n');
1840 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1841 intoken('\0') = intoken('\n');
1842 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1843 endtoken('\0') = endtoken('\n');
1847 * This routine opens the specified file and calls the function
1848 * which finds the function and type definitions.
1855 language
*lang
= curfdp
->lang
;
1856 Lang_function
*parser
= NULL
;
1858 /* If user specified a language, use it. */
1859 if (lang
!= NULL
&& lang
->function
!= NULL
)
1861 parser
= lang
->function
;
1864 /* Else try to guess the language given the file name. */
1867 lang
= get_language_from_filename (curfdp
->infname
, TRUE
);
1868 if (lang
!= NULL
&& lang
->function
!= NULL
)
1870 curfdp
->lang
= lang
;
1871 parser
= lang
->function
;
1875 /* Else look for sharp-bang as the first two characters. */
1877 && readline_internal (&lb
, inf
) > 0
1879 && lb
.buffer
[0] == '#'
1880 && lb
.buffer
[1] == '!')
1884 /* Set lp to point at the first char after the last slash in the
1885 line or, if no slashes, at the first nonblank. Then set cp to
1886 the first successive blank and terminate the string. */
1887 lp
= etags_strrchr (lb
.buffer
+2, '/');
1891 lp
= skip_spaces (lb
.buffer
+ 2);
1892 cp
= skip_non_spaces (lp
);
1895 if (strlen (lp
) > 0)
1897 lang
= get_language_from_interpreter (lp
);
1898 if (lang
!= NULL
&& lang
->function
!= NULL
)
1900 curfdp
->lang
= lang
;
1901 parser
= lang
->function
;
1906 /* We rewind here, even if inf may be a pipe. We fail if the
1907 length of the first line is longer than the pipe block size,
1908 which is unlikely. */
1911 /* Else try to guess the language given the case insensitive file name. */
1914 lang
= get_language_from_filename (curfdp
->infname
, FALSE
);
1915 if (lang
!= NULL
&& lang
->function
!= NULL
)
1917 curfdp
->lang
= lang
;
1918 parser
= lang
->function
;
1922 /* Else try Fortran or C. */
1925 node
*old_last_node
= last_node
;
1927 curfdp
->lang
= get_language_from_langname ("fortran");
1930 if (old_last_node
== last_node
)
1931 /* No Fortran entries found. Try C. */
1933 /* We do not tag if rewind fails.
1934 Only the file name will be recorded in the tags file. */
1936 curfdp
->lang
= get_language_from_langname (cplusplus
? "c++" : "c");
1942 if (!no_line_directive
1943 && curfdp
->lang
!= NULL
&& curfdp
->lang
->metasource
)
1944 /* It may be that this is a bingo.y file, and we already parsed a bingo.c
1945 file, or anyway we parsed a file that is automatically generated from
1946 this one. If this is the case, the bingo.c file contained #line
1947 directives that generated tags pointing to this file. Let's delete
1948 them all before parsing this file, which is the real source. */
1950 fdesc
**fdpp
= &fdhead
;
1951 while (*fdpp
!= NULL
)
1953 && streq ((*fdpp
)->taggedfname
, curfdp
->taggedfname
))
1954 /* We found one of those! We must delete both the file description
1955 and all tags referring to it. */
1957 fdesc
*badfdp
= *fdpp
;
1959 /* Delete the tags referring to badfdp->taggedfname
1960 that were obtained from badfdp->infname. */
1961 invalidate_nodes (badfdp
, &nodehead
);
1963 *fdpp
= badfdp
->next
; /* remove the bad description from the list */
1964 free_fdesc (badfdp
);
1967 fdpp
= &(*fdpp
)->next
; /* advance the list pointer */
1970 assert (parser
!= NULL
);
1972 /* Generic initialisations before reading from file. */
1973 linebuffer_setlen (&filebuf
, 0); /* reset the file buffer */
1975 /* Generic initialisations before parsing file with readline. */
1976 lineno
= 0; /* reset global line number */
1977 charno
= 0; /* reset global char number */
1978 linecharno
= 0; /* reset global char number of line start */
1982 #ifdef ETAGS_REGEXPS
1983 regex_tag_multiline ();
1984 #endif /* ETAGS_REGEXPS */
1989 * Check whether an implicitly named tag should be created,
1990 * then call `pfnote'.
1991 * NAME is a string that is internally copied by this function.
1993 * TAGS format specification
1994 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1995 * The following is explained in some more detail in etc/ETAGS.EBNF.
1997 * make_tag creates tags with "implicit tag names" (unnamed tags)
1998 * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
1999 * 1. NAME does not contain any of the characters in NONAM;
2000 * 2. LINESTART contains name as either a rightmost, or rightmost but
2001 * one character, substring;
2002 * 3. the character, if any, immediately before NAME in LINESTART must
2003 * be a character in NONAM;
2004 * 4. the character, if any, immediately after NAME in LINESTART must
2005 * also be a character in NONAM.
2007 * The implementation uses the notinname() macro, which recognises the
2008 * characters stored in the string `nonam'.
2009 * etags.el needs to use the same characters that are in NONAM.
2012 make_tag (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
2013 char *name
; /* tag name, or NULL if unnamed */
2014 int namelen
; /* tag length */
2015 bool is_func
; /* tag is a function */
2016 char *linestart
; /* start of the line where tag is */
2017 int linelen
; /* length of the line where tag is */
2018 int lno
; /* line number */
2019 long cno
; /* character number */
2021 bool named
= (name
!= NULL
&& namelen
> 0);
2023 if (!CTAGS
&& named
) /* maybe set named to false */
2024 /* Let's try to make an implicit tag name, that is, create an unnamed tag
2025 such that etags.el can guess a name from it. */
2028 register char *cp
= name
;
2030 for (i
= 0; i
< namelen
; i
++)
2031 if (notinname (*cp
++))
2033 if (i
== namelen
) /* rule #1 */
2035 cp
= linestart
+ linelen
- namelen
;
2036 if (notinname (linestart
[linelen
-1]))
2037 cp
-= 1; /* rule #4 */
2038 if (cp
>= linestart
/* rule #2 */
2040 || notinname (cp
[-1])) /* rule #3 */
2041 && strneq (name
, cp
, namelen
)) /* rule #2 */
2042 named
= FALSE
; /* use implicit tag name */
2047 name
= savenstr (name
, namelen
);
2050 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
2055 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
2056 char *name
; /* tag name, or NULL if unnamed */
2057 bool is_func
; /* tag is a function */
2058 char *linestart
; /* start of the line where tag is */
2059 int linelen
; /* length of the line where tag is */
2060 int lno
; /* line number */
2061 long cno
; /* character number */
2065 assert (name
== NULL
|| name
[0] != '\0');
2066 if (CTAGS
&& name
== NULL
)
2069 np
= xnew (1, node
);
2071 /* If ctags mode, change name "main" to M<thisfilename>. */
2072 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
2074 register char *fp
= etags_strrchr (curfdp
->taggedfname
, '/');
2075 np
->name
= concat ("M", fp
== NULL
? curfdp
->taggedfname
: fp
+ 1, "");
2076 fp
= etags_strrchr (np
->name
, '.');
2077 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
2083 np
->been_warned
= FALSE
;
2085 np
->is_func
= is_func
;
2087 if (np
->fdp
->usecharno
)
2088 /* Our char numbers are 0-base, because of C language tradition?
2089 ctags compatibility? old versions compatibility? I don't know.
2090 Anyway, since emacs's are 1-base we expect etags.el to take care
2091 of the difference. If we wanted to have 1-based numbers, we would
2092 uncomment the +1 below. */
2093 np
->cno
= cno
/* + 1 */ ;
2095 np
->cno
= invalidcharno
;
2096 np
->left
= np
->right
= NULL
;
2097 if (CTAGS
&& !cxref_style
)
2099 if (strlen (linestart
) < 50)
2100 np
->regex
= concat (linestart
, "$", "");
2102 np
->regex
= savenstr (linestart
, 50);
2105 np
->regex
= savenstr (linestart
, linelen
);
2107 add_node (np
, &nodehead
);
2112 * recurse on left children, iterate on right children.
2120 register node
*node_right
= np
->right
;
2121 free_tree (np
->left
);
2122 if (np
->name
!= NULL
)
2132 * delete a file description
2136 register fdesc
*fdp
;
2138 if (fdp
->infname
!= NULL
) free (fdp
->infname
);
2139 if (fdp
->infabsname
!= NULL
) free (fdp
->infabsname
);
2140 if (fdp
->infabsdir
!= NULL
) free (fdp
->infabsdir
);
2141 if (fdp
->taggedfname
!= NULL
) free (fdp
->taggedfname
);
2142 if (fdp
->prop
!= NULL
) free (fdp
->prop
);
2148 * Adds a node to the tree of nodes. In etags mode, sort by file
2149 * name. In ctags mode, sort by tag name. Make no attempt at
2152 * add_node is the only function allowed to add nodes, so it can
2156 add_node (np
, cur_node_p
)
2157 node
*np
, **cur_node_p
;
2160 register node
*cur_node
= *cur_node_p
;
2162 if (cur_node
== NULL
)
2172 /* For each file name, tags are in a linked sublist on the right
2173 pointer. The first tags of different files are a linked list
2174 on the left pointer. last_node points to the end of the last
2176 if (last_node
!= NULL
&& last_node
->fdp
== np
->fdp
)
2178 /* Let's use the same sublist as the last added node. */
2179 assert (last_node
->right
== NULL
);
2180 last_node
->right
= np
;
2183 else if (cur_node
->fdp
== np
->fdp
)
2185 /* Scanning the list we found the head of a sublist which is
2186 good for us. Let's scan this sublist. */
2187 add_node (np
, &cur_node
->right
);
2190 /* The head of this sublist is not good for us. Let's try the
2192 add_node (np
, &cur_node
->left
);
2193 } /* if ETAGS mode */
2198 dif
= strcmp (np
->name
, cur_node
->name
);
2201 * If this tag name matches an existing one, then
2202 * do not add the node, but maybe print a warning.
2206 if (np
->fdp
== cur_node
->fdp
)
2210 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
2211 np
->fdp
->infname
, lineno
, np
->name
);
2212 fprintf (stderr
, "Second entry ignored\n");
2215 else if (!cur_node
->been_warned
&& !no_warnings
)
2219 "Duplicate entry in files %s and %s: %s (Warning only)\n",
2220 np
->fdp
->infname
, cur_node
->fdp
->infname
, np
->name
);
2221 cur_node
->been_warned
= TRUE
;
2226 /* Actually add the node */
2227 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
2228 } /* if CTAGS mode */
2232 * invalidate_nodes ()
2233 * Scan the node tree and invalidate all nodes pointing to the
2234 * given file description (CTAGS case) or free them (ETAGS case).
2237 invalidate_nodes (badfdp
, npp
)
2248 if (np
->left
!= NULL
)
2249 invalidate_nodes (badfdp
, &np
->left
);
2250 if (np
->fdp
== badfdp
)
2252 if (np
->right
!= NULL
)
2253 invalidate_nodes (badfdp
, &np
->right
);
2257 assert (np
->fdp
!= NULL
);
2258 if (np
->fdp
== badfdp
)
2260 *npp
= np
->left
; /* detach the sublist from the list */
2261 np
->left
= NULL
; /* isolate it */
2262 free_tree (np
); /* free it */
2263 invalidate_nodes (badfdp
, npp
);
2266 invalidate_nodes (badfdp
, &np
->left
);
2271 static int total_size_of_entries
__P((node
*));
2272 static int number_len
__P((long));
2274 /* Length of a non-negative number's decimal representation. */
2280 while ((num
/= 10) > 0)
2286 * Return total number of characters that put_entries will output for
2287 * the nodes in the linked list at the right of the specified node.
2288 * This count is irrelevant with etags.el since emacs 19.34 at least,
2289 * but is still supplied for backward compatibility.
2292 total_size_of_entries (np
)
2295 register int total
= 0;
2297 for (; np
!= NULL
; np
= np
->right
)
2300 total
+= strlen (np
->regex
) + 1; /* pat\177 */
2301 if (np
->name
!= NULL
)
2302 total
+= strlen (np
->name
) + 1; /* name\001 */
2303 total
+= number_len ((long) np
->lno
) + 1; /* lno, */
2304 if (np
->cno
!= invalidcharno
) /* cno */
2305 total
+= number_len (np
->cno
);
2306 total
+= 1; /* newline */
2317 static fdesc
*fdp
= NULL
;
2322 /* Output subentries that precede this one */
2324 put_entries (np
->left
);
2326 /* Output this entry */
2335 fprintf (tagf
, "\f\n%s,%d\n",
2336 fdp
->taggedfname
, total_size_of_entries (np
));
2337 fdp
->written
= TRUE
;
2339 fputs (np
->regex
, tagf
);
2340 fputc ('\177', tagf
);
2341 if (np
->name
!= NULL
)
2343 fputs (np
->name
, tagf
);
2344 fputc ('\001', tagf
);
2346 fprintf (tagf
, "%d,", np
->lno
);
2347 if (np
->cno
!= invalidcharno
)
2348 fprintf (tagf
, "%ld", np
->cno
);
2354 if (np
->name
== NULL
)
2355 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
2360 fprintf (stdout
, "%s %s %d\n",
2361 np
->name
, np
->fdp
->taggedfname
, (np
->lno
+ 63) / 64);
2363 fprintf (stdout
, "%-16s %3d %-16s %s\n",
2364 np
->name
, np
->lno
, np
->fdp
->taggedfname
, np
->regex
);
2368 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->fdp
->taggedfname
);
2371 { /* function or #define macro with args */
2372 putc (searchar
, tagf
);
2375 for (sp
= np
->regex
; *sp
; sp
++)
2377 if (*sp
== '\\' || *sp
== searchar
)
2381 putc (searchar
, tagf
);
2384 { /* anything else; text pattern inadequate */
2385 fprintf (tagf
, "%d", np
->lno
);
2390 } /* if this node contains a valid tag */
2392 /* Output subentries that follow this one */
2393 put_entries (np
->right
);
2395 put_entries (np
->left
);
2400 #define C_EXT 0x00fff /* C extensions */
2401 #define C_PLAIN 0x00000 /* C */
2402 #define C_PLPL 0x00001 /* C++ */
2403 #define C_STAR 0x00003 /* C* */
2404 #define C_JAVA 0x00005 /* JAVA */
2405 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
2406 #define YACC 0x10000 /* yacc file */
2409 * The C symbol tables.
2414 st_C_objprot
, st_C_objimpl
, st_C_objend
,
2416 st_C_ignore
, st_C_attribute
,
2419 st_C_class
, st_C_template
,
2420 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
2423 static unsigned int hash
__P((const char *, unsigned int));
2424 static struct C_stab_entry
* in_word_set
__P((const char *, unsigned int));
2425 static enum sym_type C_symtype
__P((char *, int, int));
2427 /* Feed stuff between (but not including) %[ and %] lines to:
2433 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2437 while, 0, st_C_ignore
2438 switch, 0, st_C_ignore
2439 return, 0, st_C_ignore
2440 __attribute__, 0, st_C_attribute
2441 @interface, 0, st_C_objprot
2442 @protocol, 0, st_C_objprot
2443 @implementation,0, st_C_objimpl
2444 @end, 0, st_C_objend
2445 import, (C_JAVA & !C_PLPL), st_C_ignore
2446 package, (C_JAVA & !C_PLPL), st_C_ignore
2447 friend, C_PLPL, st_C_ignore
2448 extends, (C_JAVA & !C_PLPL), st_C_javastruct
2449 implements, (C_JAVA & !C_PLPL), st_C_javastruct
2450 interface, (C_JAVA & !C_PLPL), st_C_struct
2451 class, 0, st_C_class
2452 namespace, C_PLPL, st_C_struct
2453 domain, C_STAR, st_C_struct
2454 union, 0, st_C_struct
2455 struct, 0, st_C_struct
2456 extern, 0, st_C_extern
2458 typedef, 0, st_C_typedef
2459 define, 0, st_C_define
2460 operator, C_PLPL, st_C_operator
2461 template, 0, st_C_template
2462 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2463 DEFUN, 0, st_C_gnumacro
2464 SYSCALL, 0, st_C_gnumacro
2465 ENTRY, 0, st_C_gnumacro
2466 PSEUDO, 0, st_C_gnumacro
2467 # These are defined inside C functions, so currently they are not met.
2468 # EXFUN used in glibc, DEFVAR_* in emacs.
2469 #EXFUN, 0, st_C_gnumacro
2470 #DEFVAR_, 0, st_C_gnumacro
2472 and replace lines between %< and %> with its output, then:
2473 - remove the #if characterset check
2474 - make in_word_set static and not inline. */
2476 /* C code produced by gperf version 3.0.1 */
2477 /* Command-line: gperf -m 5 */
2478 /* Computed positions: -k'1-2' */
2480 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
2481 /* maximum key range = 31, duplicates = 0 */
2492 register const char *str
;
2493 register unsigned int len
;
2495 static unsigned char asso_values
[] =
2497 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2498 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2499 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2500 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2501 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2502 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2503 34, 34, 34, 34, 1, 34, 34, 34, 14, 14,
2504 34, 34, 34, 34, 34, 34, 34, 34, 13, 34,
2505 13, 34, 34, 12, 34, 34, 34, 34, 34, 11,
2506 34, 34, 34, 34, 34, 8, 34, 11, 34, 12,
2507 11, 0, 1, 34, 7, 0, 34, 34, 11, 9,
2508 0, 4, 0, 34, 7, 4, 14, 21, 34, 15,
2509 0, 2, 34, 34, 34, 34, 34, 34, 34, 34,
2510 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2511 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2512 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2513 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2514 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2515 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2516 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2517 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2518 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2519 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2520 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2521 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2522 34, 34, 34, 34, 34, 34
2524 return len
+ asso_values
[(unsigned char)str
[1]] + asso_values
[(unsigned char)str
[0]];
2527 static struct C_stab_entry
*
2528 in_word_set (str
, len
)
2529 register const char *str
;
2530 register unsigned int len
;
2534 TOTAL_KEYWORDS
= 31,
2535 MIN_WORD_LENGTH
= 2,
2536 MAX_WORD_LENGTH
= 15,
2541 static struct C_stab_entry wordlist
[] =
2544 {"if", 0, st_C_ignore
},
2545 {"enum", 0, st_C_enum
},
2546 {"@end", 0, st_C_objend
},
2547 {"extern", 0, st_C_extern
},
2548 {"extends", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2549 {"for", 0, st_C_ignore
},
2550 {"interface", (C_JAVA
& !C_PLPL
), st_C_struct
},
2551 {"@protocol", 0, st_C_objprot
},
2552 {"@interface", 0, st_C_objprot
},
2553 {"operator", C_PLPL
, st_C_operator
},
2554 {"return", 0, st_C_ignore
},
2555 {"friend", C_PLPL
, st_C_ignore
},
2556 {"import", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2557 {"@implementation",0, st_C_objimpl
},
2558 {"define", 0, st_C_define
},
2559 {"package", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2560 {"implements", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2561 {"namespace", C_PLPL
, st_C_struct
},
2562 {"domain", C_STAR
, st_C_struct
},
2563 {"template", 0, st_C_template
},
2564 {"typedef", 0, st_C_typedef
},
2565 {"struct", 0, st_C_struct
},
2566 {"switch", 0, st_C_ignore
},
2567 {"union", 0, st_C_struct
},
2568 {"while", 0, st_C_ignore
},
2569 {"class", 0, st_C_class
},
2570 {"__attribute__", 0, st_C_attribute
},
2571 {"SYSCALL", 0, st_C_gnumacro
},
2572 {"PSEUDO", 0, st_C_gnumacro
},
2573 {"ENTRY", 0, st_C_gnumacro
},
2574 {"DEFUN", 0, st_C_gnumacro
}
2577 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2579 register int key
= hash (str
, len
);
2581 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2583 register const char *s
= wordlist
[key
].name
;
2585 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1) && s
[len
] == '\0')
2586 return &wordlist
[key
];
2593 static enum sym_type
2594 C_symtype (str
, len
, c_ext
)
2599 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2601 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2608 * Ignoring __attribute__ ((list))
2610 static bool inattribute
; /* looking at an __attribute__ construct */
2613 * C functions and variables are recognized using a simple
2614 * finite automaton. fvdef is its state variable.
2618 fvnone
, /* nothing seen */
2619 fdefunkey
, /* Emacs DEFUN keyword seen */
2620 fdefunname
, /* Emacs DEFUN name seen */
2621 foperator
, /* func: operator keyword seen (cplpl) */
2622 fvnameseen
, /* function or variable name seen */
2623 fstartlist
, /* func: just after open parenthesis */
2624 finlist
, /* func: in parameter list */
2625 flistseen
, /* func: after parameter list */
2626 fignore
, /* func: before open brace */
2627 vignore
/* var-like: ignore until ';' */
2630 static bool fvextern
; /* func or var: extern keyword seen; */
2633 * typedefs are recognized using a simple finite automaton.
2634 * typdef is its state variable.
2638 tnone
, /* nothing seen */
2639 tkeyseen
, /* typedef keyword seen */
2640 ttypeseen
, /* defined type seen */
2641 tinbody
, /* inside typedef body */
2642 tend
, /* just before typedef tag */
2643 tignore
/* junk after typedef tag */
2647 * struct-like structures (enum, struct and union) are recognized
2648 * using another simple finite automaton. `structdef' is its state
2653 snone
, /* nothing seen yet,
2654 or in struct body if bracelev > 0 */
2655 skeyseen
, /* struct-like keyword seen */
2656 stagseen
, /* struct-like tag seen */
2657 scolonseen
/* colon seen after struct-like tag */
2661 * When objdef is different from onone, objtag is the name of the class.
2663 static char *objtag
= "<uninited>";
2666 * Yet another little state machine to deal with preprocessor lines.
2670 dnone
, /* nothing seen */
2671 dsharpseen
, /* '#' seen as first char on line */
2672 ddefineseen
, /* '#' and 'define' seen */
2673 dignorerest
/* ignore rest of line */
2677 * State machine for Objective C protocols and implementations.
2678 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2682 onone
, /* nothing seen */
2683 oprotocol
, /* @interface or @protocol seen */
2684 oimplementation
, /* @implementations seen */
2685 otagseen
, /* class name seen */
2686 oparenseen
, /* parenthesis before category seen */
2687 ocatseen
, /* category name seen */
2688 oinbody
, /* in @implementation body */
2689 omethodsign
, /* in @implementation body, after +/- */
2690 omethodtag
, /* after method name */
2691 omethodcolon
, /* after method colon */
2692 omethodparm
, /* after method parameter */
2693 oignore
/* wait for @end */
2698 * Use this structure to keep info about the token read, and how it
2699 * should be tagged. Used by the make_C_tag function to build a tag.
2703 char *line
; /* string containing the token */
2704 int offset
; /* where the token starts in LINE */
2705 int length
; /* token length */
2707 The previous members can be used to pass strings around for generic
2708 purposes. The following ones specifically refer to creating tags. In this
2709 case the token contained here is the pattern that will be used to create a
2712 bool valid
; /* do not create a tag; the token should be
2713 invalidated whenever a state machine is
2714 reset prematurely */
2715 bool named
; /* create a named tag */
2716 int lineno
; /* source line number of tag */
2717 long linepos
; /* source char number of tag */
2718 } token
; /* latest token read */
2721 * Variables and functions for dealing with nested structures.
2722 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2724 static void pushclass_above
__P((int, char *, int));
2725 static void popclass_above
__P((int));
2726 static void write_classname
__P((linebuffer
*, char *qualifier
));
2729 char **cname
; /* nested class names */
2730 int *bracelev
; /* nested class brace level */
2731 int nl
; /* class nesting level (elements used) */
2732 int size
; /* length of the array */
2733 } cstack
; /* stack for nested declaration tags */
2734 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2735 #define nestlev (cstack.nl)
2736 /* After struct keyword or in struct body, not inside a nested function. */
2737 #define instruct (structdef == snone && nestlev > 0 \
2738 && bracelev == cstack.bracelev[nestlev-1] + 1)
2741 pushclass_above (bracelev
, str
, len
)
2748 popclass_above (bracelev
);
2750 if (nl
>= cstack
.size
)
2752 int size
= cstack
.size
*= 2;
2753 xrnew (cstack
.cname
, size
, char *);
2754 xrnew (cstack
.bracelev
, size
, int);
2756 assert (nl
== 0 || cstack
.bracelev
[nl
-1] < bracelev
);
2757 cstack
.cname
[nl
] = (str
== NULL
) ? NULL
: savenstr (str
, len
);
2758 cstack
.bracelev
[nl
] = bracelev
;
2763 popclass_above (bracelev
)
2768 for (nl
= cstack
.nl
- 1;
2769 nl
>= 0 && cstack
.bracelev
[nl
] >= bracelev
;
2772 if (cstack
.cname
[nl
] != NULL
)
2773 free (cstack
.cname
[nl
]);
2779 write_classname (cn
, qualifier
)
2784 int qlen
= strlen (qualifier
);
2786 if (cstack
.nl
== 0 || cstack
.cname
[0] == NULL
)
2790 cn
->buffer
[0] = '\0';
2794 len
= strlen (cstack
.cname
[0]);
2795 linebuffer_setlen (cn
, len
);
2796 strcpy (cn
->buffer
, cstack
.cname
[0]);
2798 for (i
= 1; i
< cstack
.nl
; i
++)
2803 s
= cstack
.cname
[i
];
2808 linebuffer_setlen (cn
, len
);
2809 strncat (cn
->buffer
, qualifier
, qlen
);
2810 strncat (cn
->buffer
, s
, slen
);
2815 static bool consider_token
__P((char *, int, int, int *, int, int, bool *));
2816 static void make_C_tag
__P((bool));
2820 * checks to see if the current token is at the start of a
2821 * function or variable, or corresponds to a typedef, or
2822 * is a struct/union/enum tag, or #define, or an enum constant.
2824 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2825 * with args. C_EXTP points to which language we are looking at.
2836 consider_token (str
, len
, c
, c_extp
, bracelev
, parlev
, is_func_or_var
)
2837 register char *str
; /* IN: token pointer */
2838 register int len
; /* IN: token length */
2839 register int c
; /* IN: first char after the token */
2840 int *c_extp
; /* IN, OUT: C extensions mask */
2841 int bracelev
; /* IN: brace level */
2842 int parlev
; /* IN: parenthesis level */
2843 bool *is_func_or_var
; /* OUT: function or variable found */
2845 /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
2846 structtype is the type of the preceding struct-like keyword, and
2847 structbracelev is the brace level where it has been seen. */
2848 static enum sym_type structtype
;
2849 static int structbracelev
;
2850 static enum sym_type toktype
;
2853 toktype
= C_symtype (str
, len
, *c_extp
);
2856 * Skip __attribute__
2858 if (toktype
== st_C_attribute
)
2865 * Advance the definedef state machine.
2870 /* We're not on a preprocessor line. */
2871 if (toktype
== st_C_gnumacro
)
2878 if (toktype
== st_C_define
)
2880 definedef
= ddefineseen
;
2884 definedef
= dignorerest
;
2889 * Make a tag for any macro, unless it is a constant
2890 * and constantypedefs is FALSE.
2892 definedef
= dignorerest
;
2893 *is_func_or_var
= (c
== '(');
2894 if (!*is_func_or_var
&& !constantypedefs
)
2901 error ("internal error: definedef value.", (char *)NULL
);
2910 if (toktype
== st_C_typedef
)
2930 if (structdef
== snone
&& fvdef
== fvnone
)
2948 * This structdef business is NOT invoked when we are ctags and the
2949 * file is plain C. This is because a struct tag may have the same
2950 * name as another tag, and this loses with ctags.
2954 case st_C_javastruct
:
2955 if (structdef
== stagseen
)
2956 structdef
= scolonseen
;
2960 if ((*c_extp
& C_AUTO
) /* automatic detection of C++ language */
2962 && definedef
== dnone
&& structdef
== snone
2963 && typdef
== tnone
&& fvdef
== fvnone
)
2964 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2965 if (toktype
== st_C_template
)
2972 && (typdef
== tkeyseen
2973 || (typedefs_or_cplusplus
&& structdef
== snone
)))
2975 structdef
= skeyseen
;
2976 structtype
= toktype
;
2977 structbracelev
= bracelev
;
2978 if (fvdef
== fvnameseen
)
2984 if (structdef
== skeyseen
)
2986 structdef
= stagseen
;
2990 if (typdef
!= tnone
)
2993 /* Detect Objective C constructs. */
3003 objdef
= oimplementation
;
3007 case oimplementation
:
3008 /* Save the class tag for functions or variables defined inside. */
3009 objtag
= savenstr (str
, len
);
3013 /* Save the class tag for categories. */
3014 objtag
= savenstr (str
, len
);
3016 *is_func_or_var
= TRUE
;
3020 *is_func_or_var
= TRUE
;
3028 objdef
= omethodtag
;
3029 linebuffer_setlen (&token_name
, len
);
3030 strncpy (token_name
.buffer
, str
, len
);
3031 token_name
.buffer
[len
] = '\0';
3037 objdef
= omethodparm
;
3043 objdef
= omethodtag
;
3044 linebuffer_setlen (&token_name
, token_name
.len
+ len
);
3045 strncat (token_name
.buffer
, str
, len
);
3050 if (toktype
== st_C_objend
)
3052 /* Memory leakage here: the string pointed by objtag is
3053 never released, because many tests would be needed to
3054 avoid breaking on incorrect input code. The amount of
3055 memory leaked here is the sum of the lengths of the
3063 /* A function, variable or enum constant? */
3085 *is_func_or_var
= TRUE
;
3089 && structdef
== snone
3090 && structtype
== st_C_enum
&& bracelev
> structbracelev
)
3091 return TRUE
; /* enum constant */
3097 fvdef
= fdefunname
; /* GNU macro */
3098 *is_func_or_var
= TRUE
;
3106 if ((strneq (str
, "asm", 3) && endtoken (str
[3]))
3107 || (strneq (str
, "__asm__", 7) && endtoken (str
[7])))
3116 if (len
>= 10 && strneq (str
+len
-10, "::operator", 10))
3118 if (*c_extp
& C_AUTO
) /* automatic detection of C++ */
3119 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
3121 *is_func_or_var
= TRUE
;
3124 if (bracelev
> 0 && !instruct
)
3126 fvdef
= fvnameseen
; /* function or variable */
3127 *is_func_or_var
= TRUE
;
3138 * C_entries often keeps pointers to tokens or lines which are older than
3139 * the line currently read. By keeping two line buffers, and switching
3140 * them at end of line, it is possible to use those pointers.
3148 #define current_lb_is_new (newndx == curndx)
3149 #define switch_line_buffers() (curndx = 1 - curndx)
3151 #define curlb (lbs[curndx].lb)
3152 #define newlb (lbs[newndx].lb)
3153 #define curlinepos (lbs[curndx].linepos)
3154 #define newlinepos (lbs[newndx].linepos)
3156 #define plainc ((c_ext & C_EXT) == C_PLAIN)
3157 #define cplpl (c_ext & C_PLPL)
3158 #define cjava ((c_ext & C_JAVA) == C_JAVA)
3160 #define CNL_SAVE_DEFINEDEF() \
3162 curlinepos = charno; \
3163 readline (&curlb, inf); \
3164 lp = curlb.buffer; \
3171 CNL_SAVE_DEFINEDEF(); \
3172 if (savetoken.valid) \
3174 token = savetoken; \
3175 savetoken.valid = FALSE; \
3177 definedef = dnone; \
3185 /* This function should never be called when token.valid is FALSE, but
3186 we must protect against invalid input or internal errors. */
3187 if (!DEBUG
&& !token
.valid
)
3191 make_tag (token_name
.buffer
, token_name
.len
, isfun
, token
.line
,
3192 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3193 else /* this case is optimised away if !DEBUG */
3194 make_tag (concat ("INVALID TOKEN:-->", token_name
.buffer
, ""),
3195 token_name
.len
+ 17, isfun
, token
.line
,
3196 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3198 token
.valid
= FALSE
;
3204 * This routine finds functions, variables, typedefs,
3205 * #define's, enum constants and struct/union/enum definitions in
3206 * C syntax and adds them to the list.
3209 C_entries (c_ext
, inf
)
3210 int c_ext
; /* extension of C */
3211 FILE *inf
; /* input file */
3213 register char c
; /* latest char read; '\0' for end of line */
3214 register char *lp
; /* pointer one beyond the character `c' */
3215 int curndx
, newndx
; /* indices for current and new lb */
3216 register int tokoff
; /* offset in line of start of current token */
3217 register int toklen
; /* length of current token */
3218 char *qualifier
; /* string used to qualify names */
3219 int qlen
; /* length of qualifier */
3220 int bracelev
; /* current brace level */
3221 int bracketlev
; /* current bracket level */
3222 int parlev
; /* current parenthesis level */
3223 int attrparlev
; /* __attribute__ parenthesis level */
3224 int templatelev
; /* current template level */
3225 int typdefbracelev
; /* bracelev where a typedef struct body begun */
3226 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
3227 bool yacc_rules
; /* in the rules part of a yacc file */
3228 struct tok savetoken
; /* token saved during preprocessor handling */
3231 linebuffer_init (&lbs
[0].lb
);
3232 linebuffer_init (&lbs
[1].lb
);
3233 if (cstack
.size
== 0)
3235 cstack
.size
= (DEBUG
) ? 1 : 4;
3237 cstack
.cname
= xnew (cstack
.size
, char *);
3238 cstack
.bracelev
= xnew (cstack
.size
, int);
3241 tokoff
= toklen
= typdefbracelev
= 0; /* keep compiler quiet */
3242 curndx
= newndx
= 0;
3246 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
3247 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
3249 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3250 token
.valid
= savetoken
.valid
= FALSE
;
3251 bracelev
= bracketlev
= parlev
= attrparlev
= templatelev
= 0;
3253 { qualifier
= "."; qlen
= 1; }
3255 { qualifier
= "::"; qlen
= 2; }
3263 /* If we are at the end of the line, the next character is a
3264 '\0'; do not skip it, because it is what tells us
3265 to read the next line. */
3286 /* Newlines inside comments do not end macro definitions in
3288 CNL_SAVE_DEFINEDEF ();
3301 /* Newlines inside strings do not end macro definitions
3302 in traditional cpp, even though compilers don't
3303 usually accept them. */
3304 CNL_SAVE_DEFINEDEF ();
3314 /* Hmmm, something went wrong. */
3323 else if (bracketlev
> 0)
3328 if (--bracketlev
> 0)
3332 CNL_SAVE_DEFINEDEF ();
3360 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
3373 else if (/* cplpl && */ *lp
== '/')
3381 if ((c_ext
& YACC
) && *lp
== '%')
3383 /* Entering or exiting rules section in yacc file. */
3385 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
3386 typdef
= tnone
; structdef
= snone
;
3387 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3389 yacc_rules
= !yacc_rules
;
3395 if (definedef
== dnone
)
3398 bool cpptoken
= TRUE
;
3400 /* Look back on this line. If all blanks, or nonblanks
3401 followed by an end of comment, this is a preprocessor
3403 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
3406 if (*cp
== '*' && *(cp
+1) == '/')
3415 definedef
= dsharpseen
;
3416 } /* if (definedef == dnone) */
3424 /* Consider token only if some involved conditions are satisfied. */
3425 if (typdef
!= tignore
3426 && definedef
!= dignorerest
3429 && (definedef
!= dnone
3430 || structdef
!= scolonseen
)
3437 if (c
== ':' && *lp
== ':' && begtoken (lp
[1]))
3438 /* This handles :: in the middle,
3439 but not at the beginning of an identifier.
3440 Also, space-separated :: is not recognised. */
3442 if (c_ext
& C_AUTO
) /* automatic detection of C++ */
3443 c_ext
= (c_ext
| C_PLPL
) & ~C_AUTO
;
3447 goto still_in_token
;
3451 bool funorvar
= FALSE
;
3454 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
3455 &c_ext
, bracelev
, parlev
,
3458 if (fvdef
== foperator
)
3461 lp
= skip_spaces (lp
-1);
3465 && !iswhite (*lp
) && *lp
!= '(')
3468 toklen
+= lp
- oldlp
;
3470 token
.named
= FALSE
;
3472 && nestlev
> 0 && definedef
== dnone
)
3473 /* in struct body */
3475 write_classname (&token_name
, qualifier
);
3476 linebuffer_setlen (&token_name
,
3477 token_name
.len
+qlen
+toklen
);
3478 strcat (token_name
.buffer
, qualifier
);
3479 strncat (token_name
.buffer
,
3480 newlb
.buffer
+ tokoff
, toklen
);
3483 else if (objdef
== ocatseen
)
3484 /* Objective C category */
3486 int len
= strlen (objtag
) + 2 + toklen
;
3487 linebuffer_setlen (&token_name
, len
);
3488 strcpy (token_name
.buffer
, objtag
);
3489 strcat (token_name
.buffer
, "(");
3490 strncat (token_name
.buffer
,
3491 newlb
.buffer
+ tokoff
, toklen
);
3492 strcat (token_name
.buffer
, ")");
3495 else if (objdef
== omethodtag
3496 || objdef
== omethodparm
)
3497 /* Objective C method */
3501 else if (fvdef
== fdefunname
)
3502 /* GNU DEFUN and similar macros */
3504 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
3508 /* Rewrite the tag so that emacs lisp DEFUNs
3509 can be found by their elisp name */
3516 linebuffer_setlen (&token_name
, len
);
3517 strncpy (token_name
.buffer
,
3518 newlb
.buffer
+ off
, len
);
3519 token_name
.buffer
[len
] = '\0';
3522 if (token_name
.buffer
[len
] == '_')
3523 token_name
.buffer
[len
] = '-';
3524 token
.named
= defun
;
3528 linebuffer_setlen (&token_name
, toklen
);
3529 strncpy (token_name
.buffer
,
3530 newlb
.buffer
+ tokoff
, toklen
);
3531 token_name
.buffer
[toklen
] = '\0';
3532 /* Name macros and members. */
3533 token
.named
= (structdef
== stagseen
3534 || typdef
== ttypeseen
3537 && definedef
== dignorerest
)
3539 && definedef
== dnone
3540 && structdef
== snone
3543 token
.lineno
= lineno
;
3544 token
.offset
= tokoff
;
3545 token
.length
= toklen
;
3546 token
.line
= newlb
.buffer
;
3547 token
.linepos
= newlinepos
;
3550 if (definedef
== dnone
3551 && (fvdef
== fvnameseen
3552 || fvdef
== foperator
3553 || structdef
== stagseen
3555 || typdef
== ttypeseen
3556 || objdef
!= onone
))
3558 if (current_lb_is_new
)
3559 switch_line_buffers ();
3561 else if (definedef
!= dnone
3562 || fvdef
== fdefunname
3564 make_C_tag (funorvar
);
3566 else /* not yacc and consider_token failed */
3568 if (inattribute
&& fvdef
== fignore
)
3570 /* We have just met __attribute__ after a
3571 function parameter list: do not tag the
3578 } /* if (endtoken (c)) */
3579 else if (intoken (c
))
3585 } /* if (midtoken) */
3586 else if (begtoken (c
))
3594 /* This prevents tagging fb in
3595 void (__attribute__((noreturn)) *fb) (void);
3596 Fixing this is not easy and not very important. */
3600 if (plainc
|| declarations
)
3602 make_C_tag (TRUE
); /* a function */
3607 if (structdef
== stagseen
&& !cjava
)
3609 popclass_above (bracelev
);
3617 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
3619 tokoff
= lp
- 1 - newlb
.buffer
;
3624 } /* if (begtoken) */
3625 } /* if must look at token */
3628 /* Detect end of line, colon, comma, semicolon and various braces
3629 after having handled a token.*/
3635 if (yacc_rules
&& token
.offset
== 0 && token
.valid
)
3637 make_C_tag (FALSE
); /* a yacc function */
3640 if (definedef
!= dnone
)
3646 make_C_tag (TRUE
); /* an Objective C class */
3650 objdef
= omethodcolon
;
3651 linebuffer_setlen (&token_name
, token_name
.len
+ 1);
3652 strcat (token_name
.buffer
, ":");
3655 if (structdef
== stagseen
)
3657 structdef
= scolonseen
;
3660 /* Should be useless, but may be work as a safety net. */
3661 if (cplpl
&& fvdef
== flistseen
)
3663 make_C_tag (TRUE
); /* a function */
3669 if (definedef
!= dnone
|| inattribute
)
3675 make_C_tag (FALSE
); /* a typedef */
3685 if (typdef
== tignore
|| cplpl
)
3689 if ((globals
&& bracelev
== 0 && (!fvextern
|| declarations
))
3690 || (members
&& instruct
))
3691 make_C_tag (FALSE
); /* a variable */
3694 token
.valid
= FALSE
;
3698 && (cplpl
|| !instruct
)
3699 && (typdef
== tnone
|| (typdef
!= tignore
&& instruct
)))
3701 && plainc
&& instruct
))
3702 make_C_tag (TRUE
); /* a function */
3708 && cplpl
&& structdef
== stagseen
)
3709 make_C_tag (FALSE
); /* forward declaration */
3711 token
.valid
= FALSE
;
3712 } /* switch (fvdef) */
3718 if (structdef
== stagseen
)
3722 if (definedef
!= dnone
|| inattribute
)
3728 make_C_tag (TRUE
); /* an Objective C method */
3749 && (!fvextern
|| declarations
))
3750 || (members
&& instruct
)))
3751 make_C_tag (FALSE
); /* a variable */
3754 if ((declarations
&& typdef
== tnone
&& !instruct
)
3755 || (members
&& typdef
!= tignore
&& instruct
))
3757 make_C_tag (TRUE
); /* a function */
3760 else if (!declarations
)
3762 token
.valid
= FALSE
;
3767 if (structdef
== stagseen
)
3771 if (definedef
!= dnone
|| inattribute
)
3773 if (structdef
== stagseen
)
3780 make_C_tag (FALSE
); /* a typedef */
3792 if ((members
&& bracelev
== 1)
3793 || (globals
&& bracelev
== 0
3794 && (!fvextern
|| declarations
)))
3795 make_C_tag (FALSE
); /* a variable */
3809 if (definedef
!= dnone
)
3811 if (objdef
== otagseen
&& parlev
== 0)
3812 objdef
= oparenseen
;
3816 if (typdef
== ttypeseen
3820 /* This handles constructs like:
3821 typedef void OperatorFun (int fun); */
3840 if (--attrparlev
== 0)
3841 inattribute
= FALSE
;
3844 if (definedef
!= dnone
)
3846 if (objdef
== ocatseen
&& parlev
== 1)
3848 make_C_tag (TRUE
); /* an Objective C category */
3862 || typdef
== ttypeseen
))
3865 make_C_tag (FALSE
); /* a typedef */
3868 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3872 if (definedef
!= dnone
)
3874 if (typdef
== ttypeseen
)
3876 /* Whenever typdef is set to tinbody (currently only
3877 here), typdefbracelev should be set to bracelev. */
3879 typdefbracelev
= bracelev
;
3884 make_C_tag (TRUE
); /* a function */
3893 make_C_tag (TRUE
); /* an Objective C class */
3898 make_C_tag (TRUE
); /* an Objective C method */
3902 /* Neutralize `extern "C" {' grot. */
3903 if (bracelev
== 0 && structdef
== snone
&& nestlev
== 0
3911 case skeyseen
: /* unnamed struct */
3912 pushclass_above (bracelev
, NULL
, 0);
3915 case stagseen
: /* named struct or enum */
3916 case scolonseen
: /* a class */
3917 pushclass_above (bracelev
,token
.line
+token
.offset
, token
.length
);
3919 make_C_tag (FALSE
); /* a struct or enum */
3925 if (definedef
!= dnone
)
3927 if (fvdef
== fstartlist
)
3929 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3930 token
.valid
= FALSE
;
3934 if (definedef
!= dnone
)
3936 if (!ignoreindent
&& lp
== newlb
.buffer
+ 1)
3939 token
.valid
= FALSE
;
3940 bracelev
= 0; /* reset brace level if first column */
3941 parlev
= 0; /* also reset paren level, just in case... */
3943 else if (bracelev
> 0)
3946 token
.valid
= FALSE
; /* something gone amiss, token unreliable */
3947 popclass_above (bracelev
);
3949 /* Only if typdef == tinbody is typdefbracelev significant. */
3950 if (typdef
== tinbody
&& bracelev
<= typdefbracelev
)
3952 assert (bracelev
== typdefbracelev
);
3957 if (definedef
!= dnone
)
3967 if ((members
&& bracelev
== 1)
3968 || (globals
&& bracelev
== 0 && (!fvextern
|| declarations
)))
3969 make_C_tag (FALSE
); /* a variable */
3977 && (structdef
== stagseen
|| fvdef
== fvnameseen
))
3984 if (templatelev
> 0)
3992 if (objdef
== oinbody
&& bracelev
== 0)
3994 objdef
= omethodsign
;
3999 case '#': case '~': case '&': case '%': case '/':
4000 case '|': case '^': case '!': case '.': case '?':
4001 if (definedef
!= dnone
)
4003 /* These surely cannot follow a function tag in C. */
4016 if (objdef
== otagseen
)
4018 make_C_tag (TRUE
); /* an Objective C class */
4021 /* If a macro spans multiple lines don't reset its state. */
4023 CNL_SAVE_DEFINEDEF ();
4029 } /* while not eof */
4031 free (lbs
[0].lb
.buffer
);
4032 free (lbs
[1].lb
.buffer
);
4036 * Process either a C++ file or a C file depending on the setting
4040 default_C_entries (inf
)
4043 C_entries (cplusplus
? C_PLPL
: C_AUTO
, inf
);
4046 /* Always do plain C. */
4048 plain_C_entries (inf
)
4054 /* Always do C++. */
4056 Cplusplus_entries (inf
)
4059 C_entries (C_PLPL
, inf
);
4062 /* Always do Java. */
4067 C_entries (C_JAVA
, inf
);
4075 C_entries (C_STAR
, inf
);
4078 /* Always do Yacc. */
4083 C_entries (YACC
, inf
);
4087 /* Useful macros. */
4088 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
4089 for (; /* loop initialization */ \
4090 !feof (file_pointer) /* loop test */ \
4091 && /* instructions at start of loop */ \
4092 (readline (&line_buffer, file_pointer), \
4093 char_pointer = line_buffer.buffer, \
4097 #define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \
4098 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
4099 && strneq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
4100 && notinname ((cp)[sizeof(kw)-1]) /* end of kw */ \
4101 && ((cp) = skip_spaces((cp)+sizeof(kw)-1))) /* skip spaces */
4103 /* Similar to LOOKING_AT but does not use notinname, does not skip */
4104 #define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \
4105 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
4106 && strncaseeq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
4107 && ((cp) += sizeof(kw)-1)) /* skip spaces */
4110 * Read a file, but do no processing. This is used to do regexp
4111 * matching on files that have no language defined.
4114 just_read_file (inf
)
4117 register char *dummy
;
4119 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
4124 /* Fortran parsing */
4126 static void F_takeprec
__P((void));
4127 static void F_getit
__P((FILE *));
4132 dbp
= skip_spaces (dbp
);
4136 dbp
= skip_spaces (dbp
);
4137 if (strneq (dbp
, "(*)", 3))
4142 if (!ISDIGIT (*dbp
))
4144 --dbp
; /* force failure */
4149 while (ISDIGIT (*dbp
));
4158 dbp
= skip_spaces (dbp
);
4161 readline (&lb
, inf
);
4166 dbp
= skip_spaces (dbp
);
4168 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
4170 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
4172 make_tag (dbp
, cp
-dbp
, TRUE
,
4173 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4178 Fortran_functions (inf
)
4181 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4184 dbp
++; /* Ratfor escape to fortran */
4185 dbp
= skip_spaces (dbp
);
4188 switch (lowcase (*dbp
))
4191 if (nocase_tail ("integer"))
4195 if (nocase_tail ("real"))
4199 if (nocase_tail ("logical"))
4203 if (nocase_tail ("complex") || nocase_tail ("character"))
4207 if (nocase_tail ("double"))
4209 dbp
= skip_spaces (dbp
);
4212 if (nocase_tail ("precision"))
4218 dbp
= skip_spaces (dbp
);
4221 switch (lowcase (*dbp
))
4224 if (nocase_tail ("function"))
4228 if (nocase_tail ("subroutine"))
4232 if (nocase_tail ("entry"))
4236 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
4238 dbp
= skip_spaces (dbp
);
4239 if (*dbp
== '\0') /* assume un-named */
4240 make_tag ("blockdata", 9, TRUE
,
4241 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
4243 F_getit (inf
); /* look for name */
4254 * Philippe Waroquiers (1998)
4257 static void Ada_getit
__P((FILE *, char *));
4259 /* Once we are positioned after an "interesting" keyword, let's get
4260 the real tag value necessary. */
4262 Ada_getit (inf
, name_qualifier
)
4264 char *name_qualifier
;
4272 dbp
= skip_spaces (dbp
);
4274 || (dbp
[0] == '-' && dbp
[1] == '-'))
4276 readline (&lb
, inf
);
4279 switch (lowcase(*dbp
))
4282 if (nocase_tail ("body"))
4284 /* Skipping body of procedure body or package body or ....
4285 resetting qualifier to body instead of spec. */
4286 name_qualifier
= "/b";
4291 /* Skipping type of task type or protected type ... */
4292 if (nocase_tail ("type"))
4299 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
4304 dbp
= skip_spaces (dbp
);
4307 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
4315 name
= concat (dbp
, name_qualifier
, "");
4317 make_tag (name
, strlen (name
), TRUE
,
4318 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4330 bool inquote
= FALSE
;
4331 bool skip_till_semicolumn
= FALSE
;
4333 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4335 while (*dbp
!= '\0')
4337 /* Skip a string i.e. "abcd". */
4338 if (inquote
|| (*dbp
== '"'))
4340 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
4345 continue; /* advance char */
4350 break; /* advance line */
4354 /* Skip comments. */
4355 if (dbp
[0] == '-' && dbp
[1] == '-')
4356 break; /* advance line */
4358 /* Skip character enclosed in single quote i.e. 'a'
4359 and skip single quote starting an attribute i.e. 'Image. */
4368 if (skip_till_semicolumn
)
4371 skip_till_semicolumn
= FALSE
;
4373 continue; /* advance char */
4376 /* Search for beginning of a token. */
4377 if (!begtoken (*dbp
))
4380 continue; /* advance char */
4383 /* We are at the beginning of a token. */
4384 switch (lowcase(*dbp
))
4387 if (!packages_only
&& nocase_tail ("function"))
4388 Ada_getit (inf
, "/f");
4390 break; /* from switch */
4391 continue; /* advance char */
4393 if (!packages_only
&& nocase_tail ("procedure"))
4394 Ada_getit (inf
, "/p");
4395 else if (nocase_tail ("package"))
4396 Ada_getit (inf
, "/s");
4397 else if (nocase_tail ("protected")) /* protected type */
4398 Ada_getit (inf
, "/t");
4400 break; /* from switch */
4401 continue; /* advance char */
4404 if (typedefs
&& !packages_only
&& nocase_tail ("use"))
4406 /* when tagging types, avoid tagging use type Pack.Typename;
4407 for this, we will skip everything till a ; */
4408 skip_till_semicolumn
= TRUE
;
4409 continue; /* advance char */
4413 if (!packages_only
&& nocase_tail ("task"))
4414 Ada_getit (inf
, "/k");
4415 else if (typedefs
&& !packages_only
&& nocase_tail ("type"))
4417 Ada_getit (inf
, "/t");
4418 while (*dbp
!= '\0')
4422 break; /* from switch */
4423 continue; /* advance char */
4426 /* Look for the end of the token. */
4427 while (!endtoken (*dbp
))
4430 } /* advance char */
4431 } /* advance line */
4436 * Unix and microcontroller assembly tag handling
4437 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
4438 * Idea by Bob Weiner, Motorola Inc. (1994)
4446 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4448 /* If first char is alphabetic or one of [_.$], test for colon
4449 following identifier. */
4450 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4452 /* Read past label. */
4454 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4456 if (*cp
== ':' || iswhite (*cp
))
4457 /* Found end of label, so copy it and add it to the table. */
4458 make_tag (lb
.buffer
, cp
- lb
.buffer
, TRUE
,
4459 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4467 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
4468 * Perl variable names: /^(my|local).../
4469 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
4470 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
4471 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
4474 Perl_functions (inf
)
4477 char *package
= savestr ("main"); /* current package name */
4480 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4484 if (LOOKING_AT (cp
, "package"))
4487 get_tag (cp
, &package
);
4489 else if (LOOKING_AT (cp
, "sub"))
4494 while (!notinname (*cp
))
4497 continue; /* nothing found */
4498 if ((pos
= etags_strchr (sp
, ':')) != NULL
4499 && pos
< cp
&& pos
[1] == ':')
4500 /* The name is already qualified. */
4501 make_tag (sp
, cp
- sp
, TRUE
,
4502 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4506 char savechar
, *name
;
4510 name
= concat (package
, "::", sp
);
4512 make_tag (name
, strlen(name
), TRUE
,
4513 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4517 else if (globals
) /* only if we are tagging global vars */
4519 /* Skip a qualifier, if any. */
4520 bool qual
= LOOKING_AT (cp
, "my") || LOOKING_AT (cp
, "local");
4521 /* After "my" or "local", but before any following paren or space. */
4522 char *varstart
= cp
;
4524 if (qual
/* should this be removed? If yes, how? */
4525 && (*cp
== '$' || *cp
== '@' || *cp
== '%'))
4530 while (ISALNUM (*cp
) || *cp
== '_');
4534 /* Should be examining a variable list at this point;
4535 could insist on seeing an open parenthesis. */
4536 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
4542 make_tag (varstart
, cp
- varstart
, FALSE
,
4543 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4552 * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4553 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4554 * More ideas by seb bacon <seb@jamkit.com> (2002)
4557 Python_functions (inf
)
4562 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4564 cp
= skip_spaces (cp
);
4565 if (LOOKING_AT (cp
, "def") || LOOKING_AT (cp
, "class"))
4568 while (!notinname (*cp
) && *cp
!= ':')
4570 make_tag (name
, cp
- name
, TRUE
,
4571 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4580 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4581 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4582 * - /^[ \t]*define\(\"[^\"]+/
4583 * Only with --members:
4584 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4585 * Idea by Diez B. Roggisch (2001)
4591 register char *cp
, *name
;
4592 bool search_identifier
= FALSE
;
4594 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4596 cp
= skip_spaces (cp
);
4598 if (search_identifier
4601 while (!notinname (*cp
))
4603 make_tag (name
, cp
- name
, TRUE
,
4604 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4605 search_identifier
= FALSE
;
4607 else if (LOOKING_AT (cp
, "function"))
4610 cp
= skip_spaces (cp
+1);
4614 while (!notinname (*cp
))
4616 make_tag (name
, cp
- name
, TRUE
,
4617 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4620 search_identifier
= TRUE
;
4622 else if (LOOKING_AT (cp
, "class"))
4627 while (*cp
!= '\0' && !iswhite (*cp
))
4629 make_tag (name
, cp
- name
, FALSE
,
4630 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4633 search_identifier
= TRUE
;
4635 else if (strneq (cp
, "define", 6)
4636 && (cp
= skip_spaces (cp
+6))
4638 && (*cp
== '"' || *cp
== '\''))
4642 while (*cp
!= quote
&& *cp
!= '\0')
4644 make_tag (name
, cp
- name
, FALSE
,
4645 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4648 && LOOKING_AT (cp
, "var")
4652 while (!notinname(*cp
))
4654 make_tag (name
, cp
- name
, FALSE
,
4655 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4662 * Cobol tag functions
4663 * We could look for anything that could be a paragraph name.
4664 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4665 * Idea by Corny de Souza (1993)
4668 Cobol_paragraphs (inf
)
4671 register char *bp
, *ep
;
4673 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4679 /* If eoln, compiler option or comment ignore whole line. */
4680 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
4683 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
4686 make_tag (bp
, ep
- bp
, TRUE
,
4687 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4694 * Ideas by Assar Westerlund <assar@sics.se> (2001)
4697 Makefile_targets (inf
)
4702 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4704 if (*bp
== '\t' || *bp
== '#')
4706 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
4708 if (*bp
== ':' || (globals
&& *bp
== '='))
4709 make_tag (lb
.buffer
, bp
- lb
.buffer
, TRUE
,
4710 lb
.buffer
, bp
- lb
.buffer
+ 1, lineno
, linecharno
);
4717 * Original code by Mosur K. Mohan (1989)
4719 * Locates tags for procedures & functions. Doesn't do any type- or
4720 * var-definitions. It does look for the keyword "extern" or
4721 * "forward" immediately following the procedure statement; if found,
4722 * the tag is skipped.
4725 Pascal_functions (inf
)
4728 linebuffer tline
; /* mostly copied from C_entries */
4730 int save_lineno
, namelen
, taglen
;
4733 bool /* each of these flags is TRUE iff: */
4734 incomment
, /* point is inside a comment */
4735 inquote
, /* point is inside '..' string */
4736 get_tagname
, /* point is after PROCEDURE/FUNCTION
4737 keyword, so next item = potential tag */
4738 found_tag
, /* point is after a potential tag */
4739 inparms
, /* point is within parameter-list */
4740 verify_tag
; /* point has passed the parm-list, so the
4741 next token will determine whether this
4742 is a FORWARD/EXTERN to be ignored, or
4743 whether it is a real tag */
4745 save_lcno
= save_lineno
= namelen
= taglen
= 0; /* keep compiler quiet */
4746 name
= NULL
; /* keep compiler quiet */
4749 linebuffer_init (&tline
);
4751 incomment
= inquote
= FALSE
;
4752 found_tag
= FALSE
; /* have a proc name; check if extern */
4753 get_tagname
= FALSE
; /* found "procedure" keyword */
4754 inparms
= FALSE
; /* found '(' after "proc" */
4755 verify_tag
= FALSE
; /* check if "extern" is ahead */
4758 while (!feof (inf
)) /* long main loop to get next char */
4761 if (c
== '\0') /* if end of line */
4763 readline (&lb
, inf
);
4767 if (!((found_tag
&& verify_tag
)
4769 c
= *dbp
++; /* only if don't need *dbp pointing
4770 to the beginning of the name of
4771 the procedure or function */
4775 if (c
== '}') /* within { } comments */
4777 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4794 inquote
= TRUE
; /* found first quote */
4796 case '{': /* found open { comment */
4800 if (*dbp
== '*') /* found open (* comment */
4805 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4808 case ')': /* end of parms list */
4813 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4820 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4822 /* Check if this is an "extern" declaration. */
4825 if (lowcase (*dbp
== 'e'))
4827 if (nocase_tail ("extern")) /* superfluous, really! */
4833 else if (lowcase (*dbp
) == 'f')
4835 if (nocase_tail ("forward")) /* check for forward reference */
4841 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4845 make_tag (name
, namelen
, TRUE
,
4846 tline
.buffer
, taglen
, save_lineno
, save_lcno
);
4850 if (get_tagname
) /* grab name of proc or fn */
4857 /* Find block name. */
4858 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4861 /* Save all values for later tagging. */
4862 linebuffer_setlen (&tline
, lb
.len
);
4863 strcpy (tline
.buffer
, lb
.buffer
);
4864 save_lineno
= lineno
;
4865 save_lcno
= linecharno
;
4866 name
= tline
.buffer
+ (dbp
- lb
.buffer
);
4868 taglen
= cp
- lb
.buffer
+ 1;
4870 dbp
= cp
; /* set dbp to e-o-token */
4871 get_tagname
= FALSE
;
4875 /* And proceed to check for "extern". */
4877 else if (!incomment
&& !inquote
&& !found_tag
)
4879 /* Check for proc/fn keywords. */
4880 switch (lowcase (c
))
4883 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4887 if (nocase_tail ("unction"))
4892 } /* while not eof */
4894 free (tline
.buffer
);
4899 * Lisp tag functions
4900 * look for (def or (DEF, quote or QUOTE
4903 static void L_getit
__P((void));
4908 if (*dbp
== '\'') /* Skip prefix quote */
4910 else if (*dbp
== '(')
4913 /* Try to skip "(quote " */
4914 if (!LOOKING_AT (dbp
, "quote") && !LOOKING_AT (dbp
, "QUOTE"))
4915 /* Ok, then skip "(" before name in (defstruct (foo)) */
4916 dbp
= skip_spaces (dbp
);
4918 get_tag (dbp
, NULL
);
4922 Lisp_functions (inf
)
4925 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4930 if (strneq (dbp
+1, "def", 3) || strneq (dbp
+1, "DEF", 3))
4932 dbp
= skip_non_spaces (dbp
);
4933 dbp
= skip_spaces (dbp
);
4938 /* Check for (foo::defmumble name-defined ... */
4941 while (!notinname (*dbp
) && *dbp
!= ':');
4946 while (*dbp
== ':');
4948 if (strneq (dbp
, "def", 3) || strneq (dbp
, "DEF", 3))
4950 dbp
= skip_non_spaces (dbp
);
4951 dbp
= skip_spaces (dbp
);
4961 * Lua script language parsing
4962 * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
4964 * "function" and "local function" are tags if they start at column 1.
4972 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4974 if (bp
[0] != 'f' && bp
[0] != 'l')
4977 LOOKING_AT (bp
, "local"); /* skip possible "local" */
4979 if (LOOKING_AT (bp
, "function"))
4987 * Just look for lines where the first character is '/'
4988 * Also look at "defineps" for PSWrap
4990 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4991 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4997 register char *bp
, *ep
;
4999 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5004 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
5007 make_tag (bp
, ep
- bp
, TRUE
,
5008 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
5010 else if (LOOKING_AT (bp
, "defineps"))
5018 * Ignore anything after \ followed by space or in ( )
5019 * Look for words defined by :
5020 * Look for constant, code, create, defer, value, and variable
5021 * OBP extensions: Look for buffer:, field,
5022 * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004)
5030 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5031 while ((bp
= skip_spaces (bp
))[0] != '\0')
5032 if (bp
[0] == '\\' && iswhite(bp
[1]))
5033 break; /* read next line */
5034 else if (bp
[0] == '(' && iswhite(bp
[1]))
5035 do /* skip to ) or eol */
5037 while (*bp
!= ')' && *bp
!= '\0');
5038 else if ((bp
[0] == ':' && iswhite(bp
[1]) && bp
++)
5039 || LOOKING_AT_NOCASE (bp
, "constant")
5040 || LOOKING_AT_NOCASE (bp
, "code")
5041 || LOOKING_AT_NOCASE (bp
, "create")
5042 || LOOKING_AT_NOCASE (bp
, "defer")
5043 || LOOKING_AT_NOCASE (bp
, "value")
5044 || LOOKING_AT_NOCASE (bp
, "variable")
5045 || LOOKING_AT_NOCASE (bp
, "buffer:")
5046 || LOOKING_AT_NOCASE (bp
, "field"))
5047 get_tag (skip_spaces (bp
), NULL
); /* Yay! A definition! */
5049 bp
= skip_non_spaces (bp
);
5054 * Scheme tag functions
5055 * look for (def... xyzzy
5057 * (def ... ((...(xyzzy ....
5059 * Original code by Ken Haase (1985?)
5062 Scheme_functions (inf
)
5067 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5069 if (strneq (bp
, "(def", 4) || strneq (bp
, "(DEF", 4))
5071 bp
= skip_non_spaces (bp
+4);
5072 /* Skip over open parens and white space */
5073 while (notinname (*bp
))
5077 if (LOOKING_AT (bp
, "(SET!") || LOOKING_AT (bp
, "(set!"))
5083 /* Find tags in TeX and LaTeX input files. */
5085 /* TEX_toktab is a table of TeX control sequences that define tags.
5086 * Each entry records one such control sequence.
5088 * Original code from who knows whom.
5090 * Stefan Monnier (2002)
5093 static linebuffer
*TEX_toktab
= NULL
; /* Table with tag tokens */
5095 /* Default set of control sequences to put into TEX_toktab.
5096 The value of environment var TEXTAGS is prepended to this. */
5097 static char *TEX_defenv
= "\
5098 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
5099 :part:appendix:entry:index:def\
5100 :newcommand:renewcommand:newenvironment:renewenvironment";
5102 static void TEX_mode
__P((FILE *));
5103 static void TEX_decode_env
__P((char *, char *));
5105 static char TEX_esc
= '\\';
5106 static char TEX_opgrp
= '{';
5107 static char TEX_clgrp
= '}';
5110 * TeX/LaTeX scanning loop.
5119 /* Select either \ or ! as escape character. */
5122 /* Initialize token table once from environment. */
5123 if (TEX_toktab
== NULL
)
5124 TEX_decode_env ("TEXTAGS", TEX_defenv
);
5126 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5128 /* Look at each TEX keyword in line. */
5131 /* Look for a TEX escape. */
5132 while (*cp
++ != TEX_esc
)
5133 if (cp
[-1] == '\0' || cp
[-1] == '%')
5136 for (key
= TEX_toktab
; key
->buffer
!= NULL
; key
++)
5137 if (strneq (cp
, key
->buffer
, key
->len
))
5140 int namelen
, linelen
;
5143 cp
= skip_spaces (cp
+ key
->len
);
5144 if (*cp
== TEX_opgrp
)
5150 (!iswhite (*p
) && *p
!= '#' &&
5151 *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
);
5156 if (!opgrp
|| *p
== TEX_clgrp
)
5158 while (*p
!= '\0' && *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
)
5160 linelen
= p
- lb
.buffer
+ 1;
5162 make_tag (cp
, namelen
, TRUE
,
5163 lb
.buffer
, linelen
, lineno
, linecharno
);
5164 goto tex_next_line
; /* We only tag a line once */
5172 #define TEX_LESC '\\'
5173 #define TEX_SESC '!'
5175 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
5176 chars accordingly. */
5183 while ((c
= getc (inf
)) != EOF
)
5185 /* Skip to next line if we hit the TeX comment char. */
5189 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
5205 /* If the input file is compressed, inf is a pipe, and rewind may fail.
5206 No attempt is made to correct the situation. */
5210 /* Read environment and prepend it to the default string.
5211 Build token table. */
5213 TEX_decode_env (evarname
, defenv
)
5217 register char *env
, *p
;
5220 /* Append default string to environment. */
5221 env
= getenv (evarname
);
5227 env
= concat (oldenv
, defenv
, "");
5230 /* Allocate a token table */
5231 for (len
= 1, p
= env
; p
;)
5232 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
5234 TEX_toktab
= xnew (len
, linebuffer
);
5236 /* Unpack environment string into token table. Be careful about */
5237 /* zero-length strings (leading ':', "::" and trailing ':') */
5238 for (i
= 0; *env
!= '\0';)
5240 p
= etags_strchr (env
, ':');
5241 if (!p
) /* End of environment string. */
5242 p
= env
+ strlen (env
);
5244 { /* Only non-zero strings. */
5245 TEX_toktab
[i
].buffer
= savenstr (env
, p
- env
);
5246 TEX_toktab
[i
].len
= p
- env
;
5253 TEX_toktab
[i
].buffer
= NULL
; /* Mark end of table. */
5254 TEX_toktab
[i
].len
= 0;
5261 /* Texinfo support. Dave Love, Mar. 2000. */
5267 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5268 if (LOOKING_AT (cp
, "@node"))
5271 while (*cp
!= '\0' && *cp
!= ',')
5273 make_tag (start
, cp
- start
, TRUE
,
5274 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5281 * Contents of <title>, <h1>, <h2>, <h3> are tags.
5282 * Contents of <a name=xxx> are tags with name xxx.
5284 * Francesco Potortì, 2002.
5290 bool getnext
= FALSE
; /* next text outside of HTML tags is a tag */
5291 bool skiptag
= FALSE
; /* skip to the end of the current HTML tag */
5292 bool intag
= FALSE
; /* inside an html tag, looking for ID= */
5293 bool inanchor
= FALSE
; /* when INTAG, is an anchor, look for NAME= */
5297 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5299 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
5300 for (;;) /* loop on the same line */
5302 if (skiptag
) /* skip HTML tag */
5304 while (*dbp
!= '\0' && *dbp
!= '>')
5310 continue; /* look on the same line */
5312 break; /* go to next line */
5315 else if (intag
) /* look for "name=" or "id=" */
5317 while (*dbp
!= '\0' && *dbp
!= '>'
5318 && lowcase (*dbp
) != 'n' && lowcase (*dbp
) != 'i')
5321 break; /* go to next line */
5326 continue; /* look on the same line */
5328 if ((inanchor
&& LOOKING_AT_NOCASE (dbp
, "name="))
5329 || LOOKING_AT_NOCASE (dbp
, "id="))
5331 bool quoted
= (dbp
[0] == '"');
5334 for (end
= ++dbp
; *end
!= '\0' && *end
!= '"'; end
++)
5337 for (end
= dbp
; *end
!= '\0' && intoken (*end
); end
++)
5339 linebuffer_setlen (&token_name
, end
- dbp
);
5340 strncpy (token_name
.buffer
, dbp
, end
- dbp
);
5341 token_name
.buffer
[end
- dbp
] = '\0';
5344 intag
= FALSE
; /* we found what we looked for */
5345 skiptag
= TRUE
; /* skip to the end of the tag */
5346 getnext
= TRUE
; /* then grab the text */
5347 continue; /* look on the same line */
5352 else if (getnext
) /* grab next tokens and tag them */
5354 dbp
= skip_spaces (dbp
);
5356 break; /* go to next line */
5360 inanchor
= (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]));
5361 continue; /* look on the same line */
5364 for (end
= dbp
+ 1; *end
!= '\0' && *end
!= '<'; end
++)
5366 make_tag (token_name
.buffer
, token_name
.len
, TRUE
,
5367 dbp
, end
- dbp
, lineno
, linecharno
);
5368 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5370 break; /* go to next line */
5373 else /* look for an interesting HTML tag */
5375 while (*dbp
!= '\0' && *dbp
!= '<')
5378 break; /* go to next line */
5380 if (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]))
5383 continue; /* look on the same line */
5385 else if (LOOKING_AT_NOCASE (dbp
, "<title>")
5386 || LOOKING_AT_NOCASE (dbp
, "<h1>")
5387 || LOOKING_AT_NOCASE (dbp
, "<h2>")
5388 || LOOKING_AT_NOCASE (dbp
, "<h3>"))
5392 continue; /* look on the same line */
5403 * Assumes that the predicate or rule starts at column 0.
5404 * Only the first clause of a predicate or rule is added.
5405 * Original code by Sunichirou Sugou (1989)
5406 * Rewritten by Anders Lindgren (1996)
5408 static int prolog_pr
__P((char *, char *));
5409 static void prolog_skip_comment
__P((linebuffer
*, FILE *));
5410 static int prolog_atom
__P((char *, int));
5413 Prolog_functions (inf
)
5424 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5426 if (cp
[0] == '\0') /* Empty line */
5428 else if (iswhite (cp
[0])) /* Not a predicate */
5430 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
5431 prolog_skip_comment (&lb
, inf
);
5432 else if ((len
= prolog_pr (cp
, last
)) > 0)
5434 /* Predicate or rule. Store the function name so that we
5435 only generate a tag for the first clause. */
5437 last
= xnew(len
+ 1, char);
5438 else if (len
+ 1 > allocated
)
5439 xrnew (last
, len
+ 1, char);
5440 allocated
= len
+ 1;
5441 strncpy (last
, cp
, len
);
5451 prolog_skip_comment (plb
, inf
)
5459 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
5460 if (cp
[0] == '*' && cp
[1] == '/')
5462 readline (plb
, inf
);
5468 * A predicate or rule definition is added if it matches:
5469 * <beginning of line><Prolog Atom><whitespace>(
5470 * or <beginning of line><Prolog Atom><whitespace>:-
5472 * It is added to the tags database if it doesn't match the
5473 * name of the previous clause header.
5475 * Return the size of the name of the predicate or rule, or 0 if no
5481 char *last
; /* Name of last clause. */
5486 pos
= prolog_atom (s
, 0);
5491 pos
= skip_spaces (s
+ pos
) - s
;
5494 || (s
[pos
] == '(' && (pos
+= 1))
5495 || (s
[pos
] == ':' && s
[pos
+ 1] == '-' && (pos
+= 2)))
5496 && (last
== NULL
/* save only the first clause */
5497 || len
!= (int)strlen (last
)
5498 || !strneq (s
, last
, len
)))
5500 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5508 * Consume a Prolog atom.
5509 * Return the number of bytes consumed, or -1 if there was an error.
5511 * A prolog atom, in this context, could be one of:
5512 * - An alphanumeric sequence, starting with a lower case letter.
5513 * - A quoted arbitrary string. Single quotes can escape themselves.
5514 * Backslash quotes everything.
5517 prolog_atom (s
, pos
)
5525 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
5527 /* The atom is unquoted. */
5529 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
5533 return pos
- origpos
;
5535 else if (s
[pos
] == '\'')
5546 pos
++; /* A double quote */
5548 else if (s
[pos
] == '\0')
5549 /* Multiline quoted atoms are ignored. */
5551 else if (s
[pos
] == '\\')
5553 if (s
[pos
+1] == '\0')
5560 return pos
- origpos
;
5568 * Support for Erlang
5570 * Generates tags for functions, defines, and records.
5571 * Assumes that Erlang functions start at column 0.
5572 * Original code by Anders Lindgren (1996)
5574 static int erlang_func
__P((char *, char *));
5575 static void erlang_attribute
__P((char *));
5576 static int erlang_atom
__P((char *));
5579 Erlang_functions (inf
)
5590 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5592 if (cp
[0] == '\0') /* Empty line */
5594 else if (iswhite (cp
[0])) /* Not function nor attribute */
5596 else if (cp
[0] == '%') /* comment */
5598 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
5600 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
5602 erlang_attribute (cp
);
5609 else if ((len
= erlang_func (cp
, last
)) > 0)
5612 * Function. Store the function name so that we only
5613 * generates a tag for the first clause.
5616 last
= xnew (len
+ 1, char);
5617 else if (len
+ 1 > allocated
)
5618 xrnew (last
, len
+ 1, char);
5619 allocated
= len
+ 1;
5620 strncpy (last
, cp
, len
);
5630 * A function definition is added if it matches:
5631 * <beginning of line><Erlang Atom><whitespace>(
5633 * It is added to the tags database if it doesn't match the
5634 * name of the previous clause header.
5636 * Return the size of the name of the function, or 0 if no function
5640 erlang_func (s
, last
)
5642 char *last
; /* Name of last clause. */
5647 pos
= erlang_atom (s
);
5652 pos
= skip_spaces (s
+ pos
) - s
;
5654 /* Save only the first clause. */
5657 || len
!= (int)strlen (last
)
5658 || !strneq (s
, last
, len
)))
5660 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5669 * Handle attributes. Currently, tags are generated for defines
5672 * They are on the form:
5673 * -define(foo, bar).
5674 * -define(Foo(M, N), M+N).
5675 * -record(graph, {vtab = notable, cyclic = true}).
5678 erlang_attribute (s
)
5683 if ((LOOKING_AT (cp
, "-define") || LOOKING_AT (cp
, "-record"))
5686 int len
= erlang_atom (skip_spaces (cp
));
5688 make_tag (cp
, len
, TRUE
, s
, cp
+ len
- s
, lineno
, linecharno
);
5695 * Consume an Erlang atom (or variable).
5696 * Return the number of bytes consumed, or -1 if there was an error.
5704 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
5706 /* The atom is unquoted. */
5709 while (ISALNUM (s
[pos
]) || s
[pos
] == '_');
5711 else if (s
[pos
] == '\'')
5713 for (pos
++; s
[pos
] != '\''; pos
++)
5714 if (s
[pos
] == '\0' /* multiline quoted atoms are ignored */
5715 || (s
[pos
] == '\\' && s
[++pos
] == '\0'))
5724 #ifdef ETAGS_REGEXPS
5726 static char *scan_separators
__P((char *));
5727 static void add_regex
__P((char *, language
*));
5728 static char *substitute
__P((char *, char *, struct re_registers
*));
5731 * Take a string like "/blah/" and turn it into "blah", verifying
5732 * that the first and last characters are the same, and handling
5733 * quoted separator characters. Actually, stops on the occurrence of
5734 * an unquoted separator. Also process \t, \n, etc. and turn into
5735 * appropriate characters. Works in place. Null terminates name string.
5736 * Returns pointer to terminating separator, or NULL for
5737 * unterminated regexps.
5740 scan_separators (name
)
5744 char *copyto
= name
;
5745 bool quoted
= FALSE
;
5747 for (++name
; *name
!= '\0'; ++name
)
5753 case 'a': *copyto
++ = '\007'; break; /* BEL (bell) */
5754 case 'b': *copyto
++ = '\b'; break; /* BS (back space) */
5755 case 'd': *copyto
++ = 0177; break; /* DEL (delete) */
5756 case 'e': *copyto
++ = 033; break; /* ESC (delete) */
5757 case 'f': *copyto
++ = '\f'; break; /* FF (form feed) */
5758 case 'n': *copyto
++ = '\n'; break; /* NL (new line) */
5759 case 'r': *copyto
++ = '\r'; break; /* CR (carriage return) */
5760 case 't': *copyto
++ = '\t'; break; /* TAB (horizontal tab) */
5761 case 'v': *copyto
++ = '\v'; break; /* VT (vertical tab) */
5767 /* Something else is quoted, so preserve the quote. */
5775 else if (*name
== '\\')
5777 else if (*name
== sep
)
5783 name
= NULL
; /* signal unterminated regexp */
5785 /* Terminate copied string. */
5790 /* Look at the argument of --regex or --no-regex and do the right
5791 thing. Same for each line of a regexp file. */
5793 analyse_regex (regex_arg
)
5796 if (regex_arg
== NULL
)
5798 free_regexps (); /* --no-regex: remove existing regexps */
5802 /* A real --regexp option or a line in a regexp file. */
5803 switch (regex_arg
[0])
5805 /* Comments in regexp file or null arg to --regex. */
5811 /* Read a regex file. This is recursive and may result in a
5812 loop, which will stop when the file descriptors are exhausted. */
5816 linebuffer regexbuf
;
5817 char *regexfile
= regex_arg
+ 1;
5819 /* regexfile is a file containing regexps, one per line. */
5820 regexfp
= fopen (regexfile
, "r");
5821 if (regexfp
== NULL
)
5826 linebuffer_init (®exbuf
);
5827 while (readline_internal (®exbuf
, regexfp
) > 0)
5828 analyse_regex (regexbuf
.buffer
);
5829 free (regexbuf
.buffer
);
5834 /* Regexp to be used for a specific language only. */
5838 char *lang_name
= regex_arg
+ 1;
5841 for (cp
= lang_name
; *cp
!= '}'; cp
++)
5844 error ("unterminated language name in regex: %s", regex_arg
);
5848 lang
= get_language_from_langname (lang_name
);
5851 add_regex (cp
, lang
);
5855 /* Regexp to be used for any language. */
5857 add_regex (regex_arg
, NULL
);
5862 /* Separate the regexp pattern, compile it,
5863 and care for optional name and modifiers. */
5865 add_regex (regexp_pattern
, lang
)
5866 char *regexp_pattern
;
5869 static struct re_pattern_buffer zeropattern
;
5870 char sep
, *pat
, *name
, *modifiers
;
5872 struct re_pattern_buffer
*patbuf
;
5875 force_explicit_name
= TRUE
, /* do not use implicit tag names */
5876 ignore_case
= FALSE
, /* case is significant */
5877 multi_line
= FALSE
, /* matches are done one line at a time */
5878 single_line
= FALSE
; /* dot does not match newline */
5881 if (strlen(regexp_pattern
) < 3)
5883 error ("null regexp", (char *)NULL
);
5886 sep
= regexp_pattern
[0];
5887 name
= scan_separators (regexp_pattern
);
5890 error ("%s: unterminated regexp", regexp_pattern
);
5895 error ("null name for regexp \"%s\"", regexp_pattern
);
5898 modifiers
= scan_separators (name
);
5899 if (modifiers
== NULL
) /* no terminating separator --> no name */
5905 modifiers
+= 1; /* skip separator */
5907 /* Parse regex modifiers. */
5908 for (; modifiers
[0] != '\0'; modifiers
++)
5909 switch (modifiers
[0])
5912 if (modifiers
== name
)
5913 error ("forcing explicit tag name but no name, ignoring", NULL
);
5914 force_explicit_name
= TRUE
;
5924 need_filebuf
= TRUE
;
5929 wrongmod
[0] = modifiers
[0];
5931 error ("invalid regexp modifier `%s', ignoring", wrongmod
);
5936 patbuf
= xnew (1, struct re_pattern_buffer
);
5937 *patbuf
= zeropattern
;
5940 static char lc_trans
[CHARS
];
5942 for (i
= 0; i
< CHARS
; i
++)
5943 lc_trans
[i
] = lowcase (i
);
5944 patbuf
->translate
= lc_trans
; /* translation table to fold case */
5948 pat
= concat ("^", regexp_pattern
, ""); /* anchor to beginning of line */
5950 pat
= regexp_pattern
;
5953 re_set_syntax (RE_SYNTAX_EMACS
| RE_DOT_NEWLINE
);
5955 re_set_syntax (RE_SYNTAX_EMACS
);
5957 err
= re_compile_pattern (pat
, strlen (regexp_pattern
), patbuf
);
5962 error ("%s while compiling pattern", err
);
5967 p_head
= xnew (1, regexp
);
5968 p_head
->pattern
= savestr (regexp_pattern
);
5969 p_head
->p_next
= rp
;
5970 p_head
->lang
= lang
;
5971 p_head
->pat
= patbuf
;
5972 p_head
->name
= savestr (name
);
5973 p_head
->error_signaled
= FALSE
;
5974 p_head
->force_explicit_name
= force_explicit_name
;
5975 p_head
->ignore_case
= ignore_case
;
5976 p_head
->multi_line
= multi_line
;
5980 * Do the substitutions indicated by the regular expression and
5984 substitute (in
, out
, regs
)
5986 struct re_registers
*regs
;
5989 int size
, dig
, diglen
;
5992 size
= strlen (out
);
5994 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5995 if (out
[size
- 1] == '\\')
5996 fatal ("pattern error in \"%s\"", out
);
5997 for (t
= etags_strchr (out
, '\\');
5999 t
= etags_strchr (t
+ 2, '\\'))
6003 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
6009 /* Allocate space and do the substitutions. */
6011 result
= xnew (size
+ 1, char);
6013 for (t
= result
; *out
!= '\0'; out
++)
6014 if (*out
== '\\' && ISDIGIT (*++out
))
6017 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
6018 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
6025 assert (t
<= result
+ size
);
6026 assert (t
- result
== (int)strlen (result
));
6031 /* Deallocate all regexps. */
6036 while (p_head
!= NULL
)
6038 rp
= p_head
->p_next
;
6039 free (p_head
->pattern
);
6040 free (p_head
->name
);
6048 * Reads the whole file as a single string from `filebuf' and looks for
6049 * multi-line regular expressions, creating tags on matches.
6050 * readline already dealt with normal regexps.
6052 * Idea by Ben Wing <ben@666.com> (2002).
6055 regex_tag_multiline ()
6057 char *buffer
= filebuf
.buffer
;
6061 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6065 if (!rp
->multi_line
)
6066 continue; /* skip normal regexps */
6068 /* Generic initialisations before parsing file from memory. */
6069 lineno
= 1; /* reset global line number */
6070 charno
= 0; /* reset global char number */
6071 linecharno
= 0; /* reset global char number of line start */
6073 /* Only use generic regexps or those for the current language. */
6074 if (rp
->lang
!= NULL
&& rp
->lang
!= curfdp
->lang
)
6077 while (match
>= 0 && match
< filebuf
.len
)
6079 match
= re_search (rp
->pat
, buffer
, filebuf
.len
, charno
,
6080 filebuf
.len
- match
, &rp
->regs
);
6085 if (!rp
->error_signaled
)
6087 error ("regexp stack overflow while matching \"%s\"",
6089 rp
->error_signaled
= TRUE
;
6096 if (match
== rp
->regs
.end
[0])
6098 if (!rp
->error_signaled
)
6100 error ("regexp matches the empty string: \"%s\"",
6102 rp
->error_signaled
= TRUE
;
6104 match
= -3; /* exit from while loop */
6108 /* Match occurred. Construct a tag. */
6109 while (charno
< rp
->regs
.end
[0])
6110 if (buffer
[charno
++] == '\n')
6111 lineno
++, linecharno
= charno
;
6113 if (name
[0] == '\0')
6115 else /* make a named tag */
6116 name
= substitute (buffer
, rp
->name
, &rp
->regs
);
6117 if (rp
->force_explicit_name
)
6118 /* Force explicit tag name, if a name is there. */
6119 pfnote (name
, TRUE
, buffer
+ linecharno
,
6120 charno
- linecharno
+ 1, lineno
, linecharno
);
6122 make_tag (name
, strlen (name
), TRUE
, buffer
+ linecharno
,
6123 charno
- linecharno
+ 1, lineno
, linecharno
);
6130 #endif /* ETAGS_REGEXPS */
6137 register int len
= 0;
6139 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
6141 if (*cp
== '\0' && !intoken (dbp
[len
]))
6150 get_tag (bp
, namepp
)
6154 register char *cp
= bp
;
6158 /* Go till you get to white space or a syntactic break */
6159 for (cp
= bp
+ 1; !notinname (*cp
); cp
++)
6161 make_tag (bp
, cp
- bp
, TRUE
,
6162 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
6166 *namepp
= savenstr (bp
, cp
- bp
);
6170 * Read a line of text from `stream' into `lbp', excluding the
6171 * newline or CR-NL, if any. Return the number of characters read from
6172 * `stream', which is the length of the line including the newline.
6174 * On DOS or Windows we do not count the CR character, if any before the
6175 * NL, in the returned length; this mirrors the behavior of Emacs on those
6176 * platforms (for text files, it translates CR-NL to NL as it reads in the
6179 * If multi-line regular expressions are requested, each line read is
6180 * appended to `filebuf'.
6183 readline_internal (lbp
, stream
)
6185 register FILE *stream
;
6187 char *buffer
= lbp
->buffer
;
6188 register char *p
= lbp
->buffer
;
6189 register char *pend
;
6192 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
6196 register int c
= getc (stream
);
6199 /* We're at the end of linebuffer: expand it. */
6201 xrnew (buffer
, lbp
->size
, char);
6202 p
+= buffer
- lbp
->buffer
;
6203 pend
= buffer
+ lbp
->size
;
6204 lbp
->buffer
= buffer
;
6214 if (p
> buffer
&& p
[-1] == '\r')
6218 /* Assume CRLF->LF translation will be performed by Emacs
6219 when loading this file, so CRs won't appear in the buffer.
6220 It would be cleaner to compensate within Emacs;
6221 however, Emacs does not know how many CRs were deleted
6222 before any given point in the file. */
6237 lbp
->len
= p
- buffer
;
6239 if (need_filebuf
/* we need filebuf for multi-line regexps */
6240 && chars_deleted
> 0) /* not at EOF */
6242 while (filebuf
.size
<= filebuf
.len
+ lbp
->len
+ 1) /* +1 for \n */
6244 /* Expand filebuf. */
6246 xrnew (filebuf
.buffer
, filebuf
.size
, char);
6248 strncpy (filebuf
.buffer
+ filebuf
.len
, lbp
->buffer
, lbp
->len
);
6249 filebuf
.len
+= lbp
->len
;
6250 filebuf
.buffer
[filebuf
.len
++] = '\n';
6251 filebuf
.buffer
[filebuf
.len
] = '\0';
6254 return lbp
->len
+ chars_deleted
;
6258 * Like readline_internal, above, but in addition try to match the
6259 * input line against relevant regular expressions and manage #line
6263 readline (lbp
, stream
)
6269 linecharno
= charno
; /* update global char number of line start */
6270 result
= readline_internal (lbp
, stream
); /* read line */
6271 lineno
+= 1; /* increment global line number */
6272 charno
+= result
; /* increment global char number */
6274 /* Honour #line directives. */
6275 if (!no_line_directive
)
6277 static bool discard_until_line_directive
;
6279 /* Check whether this is a #line directive. */
6280 if (result
> 12 && strneq (lbp
->buffer
, "#line ", 6))
6284 if (DEBUG
) start
= 0; /* shut up the compiler */
6285 if (sscanf (lbp
->buffer
, "#line %d \"%n", &lno
, &start
) == 1)
6287 char *endp
= lbp
->buffer
+ start
;
6290 while ((endp
= etags_strchr (endp
, '"')) != NULL
6291 && endp
[-1] == '\\')
6294 /* Ok, this is a real #line directive. Let's deal with it. */
6296 char *taggedabsname
; /* absolute name of original file */
6297 char *taggedfname
; /* name of original file as given */
6298 char *name
; /* temp var */
6300 discard_until_line_directive
= FALSE
; /* found it */
6301 name
= lbp
->buffer
+ start
;
6303 canonicalize_filename (name
); /* for DOS */
6304 taggedabsname
= absolute_filename (name
, curfdp
->infabsdir
);
6305 if (filename_is_absolute (name
)
6306 || filename_is_absolute (curfdp
->infname
))
6307 taggedfname
= savestr (taggedabsname
);
6309 taggedfname
= relative_filename (taggedabsname
,tagfiledir
);
6311 if (streq (curfdp
->taggedfname
, taggedfname
))
6312 /* The #line directive is only a line number change. We
6313 deal with this afterwards. */
6316 /* The tags following this #line directive should be
6317 attributed to taggedfname. In order to do this, set
6318 curfdp accordingly. */
6320 fdesc
*fdp
; /* file description pointer */
6322 /* Go look for a file description already set up for the
6323 file indicated in the #line directive. If there is
6324 one, use it from now until the next #line
6326 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6327 if (streq (fdp
->infname
, curfdp
->infname
)
6328 && streq (fdp
->taggedfname
, taggedfname
))
6329 /* If we remove the second test above (after the &&)
6330 then all entries pertaining to the same file are
6331 coalesced in the tags file. If we use it, then
6332 entries pertaining to the same file but generated
6333 from different files (via #line directives) will
6334 go into separate sections in the tags file. These
6335 alternatives look equivalent. The first one
6336 destroys some apparently useless information. */
6342 /* Else, if we already tagged the real file, skip all
6343 input lines until the next #line directive. */
6344 if (fdp
== NULL
) /* not found */
6345 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6346 if (streq (fdp
->infabsname
, taggedabsname
))
6348 discard_until_line_directive
= TRUE
;
6352 /* Else create a new file description and use that from
6353 now on, until the next #line directive. */
6354 if (fdp
== NULL
) /* not found */
6357 fdhead
= xnew (1, fdesc
);
6358 *fdhead
= *curfdp
; /* copy curr. file description */
6360 fdhead
->infname
= savestr (curfdp
->infname
);
6361 fdhead
->infabsname
= savestr (curfdp
->infabsname
);
6362 fdhead
->infabsdir
= savestr (curfdp
->infabsdir
);
6363 fdhead
->taggedfname
= taggedfname
;
6364 fdhead
->usecharno
= FALSE
;
6365 fdhead
->prop
= NULL
;
6366 fdhead
->written
= FALSE
;
6370 free (taggedabsname
);
6372 readline (lbp
, stream
);
6374 } /* if a real #line directive */
6375 } /* if #line is followed by a a number */
6376 } /* if line begins with "#line " */
6378 /* If we are here, no #line directive was found. */
6379 if (discard_until_line_directive
)
6383 /* Do a tail recursion on ourselves, thus discarding the contents
6384 of the line buffer. */
6385 readline (lbp
, stream
);
6389 discard_until_line_directive
= FALSE
;
6392 } /* if #line directives should be considered */
6394 #ifdef ETAGS_REGEXPS
6400 /* Match against relevant regexps. */
6402 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6404 /* Only use generic regexps or those for the current language.
6405 Also do not use multiline regexps, which is the job of
6406 regex_tag_multiline. */
6407 if ((rp
->lang
!= NULL
&& rp
->lang
!= fdhead
->lang
)
6411 match
= re_match (rp
->pat
, lbp
->buffer
, lbp
->len
, 0, &rp
->regs
);
6416 if (!rp
->error_signaled
)
6418 error ("regexp stack overflow while matching \"%s\"",
6420 rp
->error_signaled
= TRUE
;
6427 /* Empty string matched. */
6428 if (!rp
->error_signaled
)
6430 error ("regexp matches the empty string: \"%s\"", rp
->pattern
);
6431 rp
->error_signaled
= TRUE
;
6435 /* Match occurred. Construct a tag. */
6437 if (name
[0] == '\0')
6439 else /* make a named tag */
6440 name
= substitute (lbp
->buffer
, rp
->name
, &rp
->regs
);
6441 if (rp
->force_explicit_name
)
6442 /* Force explicit tag name, if a name is there. */
6443 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
6445 make_tag (name
, strlen (name
), TRUE
,
6446 lbp
->buffer
, match
, lineno
, linecharno
);
6451 #endif /* ETAGS_REGEXPS */
6456 * Return a pointer to a space of size strlen(cp)+1 allocated
6457 * with xnew where the string CP has been copied.
6463 return savenstr (cp
, strlen (cp
));
6467 * Return a pointer to a space of size LEN+1 allocated with xnew where
6468 * the string CP has been copied for at most the first LEN characters.
6477 dp
= xnew (len
+ 1, char);
6478 strncpy (dp
, cp
, len
);
6484 * Return the ptr in sp at which the character c last
6485 * appears; NULL if not found
6487 * Identical to POSIX strrchr, included for portability.
6490 etags_strrchr (sp
, c
)
6491 register const char *sp
;
6494 register const char *r
;
6506 * Return the ptr in sp at which the character c first
6507 * appears; NULL if not found
6509 * Identical to POSIX strchr, included for portability.
6512 etags_strchr (sp
, c
)
6513 register const char *sp
;
6525 * Compare two strings, ignoring case for alphabetic characters.
6527 * Same as BSD's strcasecmp, included for portability.
6530 etags_strcasecmp (s1
, s2
)
6531 register const char *s1
;
6532 register const char *s2
;
6535 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6536 ? lowcase (*s1
) == lowcase (*s2
)
6540 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6541 ? lowcase (*s1
) - lowcase (*s2
)
6546 * Compare two strings, ignoring case for alphabetic characters.
6547 * Stop after a given number of characters
6549 * Same as BSD's strncasecmp, included for portability.
6552 etags_strncasecmp (s1
, s2
, n
)
6553 register const char *s1
;
6554 register const char *s2
;
6557 while (*s1
!= '\0' && n
-- > 0
6558 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6559 ? lowcase (*s1
) == lowcase (*s2
)
6566 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6567 ? lowcase (*s1
) - lowcase (*s2
)
6571 /* Skip spaces (end of string is not space), return new pointer. */
6576 while (iswhite (*cp
))
6581 /* Skip non spaces, except end of string, return new pointer. */
6583 skip_non_spaces (cp
)
6586 while (*cp
!= '\0' && !iswhite (*cp
))
6591 /* Print error message and exit. */
6597 exit (EXIT_FAILURE
);
6605 exit (EXIT_FAILURE
);
6609 suggest_asking_for_help ()
6611 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
6612 progname
, LONG_OPTIONS
? "--help" : "-h");
6613 exit (EXIT_FAILURE
);
6616 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
6619 const char *s1
, *s2
;
6621 fprintf (stderr
, "%s: ", progname
);
6622 fprintf (stderr
, s1
, s2
);
6623 fprintf (stderr
, "\n");
6626 /* Return a newly-allocated string whose contents
6627 concatenate those of s1, s2, s3. */
6632 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
6633 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
6635 strcpy (result
, s1
);
6636 strcpy (result
+ len1
, s2
);
6637 strcpy (result
+ len1
+ len2
, s3
);
6638 result
[len1
+ len2
+ len3
] = '\0';
6644 /* Does the same work as the system V getcwd, but does not need to
6645 guess the buffer size in advance. */
6651 char *path
= xnew (bufsize
, char);
6653 while (getcwd (path
, bufsize
) == NULL
)
6655 if (errno
!= ERANGE
)
6659 path
= xnew (bufsize
, char);
6662 canonicalize_filename (path
);
6665 #else /* not HAVE_GETCWD */
6668 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
6672 for (p
= path
; *p
!= '\0'; p
++)
6678 return strdup (path
);
6679 #else /* not MSDOS */
6683 linebuffer_init (&path
);
6684 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
6685 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
6690 #endif /* not MSDOS */
6691 #endif /* not HAVE_GETCWD */
6694 /* Return a newly allocated string containing the file name of FILE
6695 relative to the absolute directory DIR (which should end with a slash). */
6697 relative_filename (file
, dir
)
6700 char *fp
, *dp
, *afn
, *res
;
6703 /* Find the common root of file and dir (with a trailing slash). */
6704 afn
= absolute_filename (file
, cwd
);
6707 while (*fp
++ == *dp
++)
6709 fp
--, dp
--; /* back to the first differing char */
6711 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
6714 do /* look at the equal chars until '/' */
6718 /* Build a sequence of "../" strings for the resulting relative file name. */
6720 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
6722 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
6725 strcat (res
, "../");
6727 /* Add the file name relative to the common root of file and dir. */
6728 strcat (res
, fp
+ 1);
6734 /* Return a newly allocated string containing the absolute file name
6735 of FILE given DIR (which should end with a slash). */
6737 absolute_filename (file
, dir
)
6740 char *slashp
, *cp
, *res
;
6742 if (filename_is_absolute (file
))
6743 res
= savestr (file
);
6745 /* We don't support non-absolute file names with a drive
6746 letter, like `d:NAME' (it's too much hassle). */
6747 else if (file
[1] == ':')
6748 fatal ("%s: relative file names with drive letters not supported", file
);
6751 res
= concat (dir
, file
, "");
6753 /* Delete the "/dirname/.." and "/." substrings. */
6754 slashp
= etags_strchr (res
, '/');
6755 while (slashp
!= NULL
&& slashp
[0] != '\0')
6757 if (slashp
[1] == '.')
6759 if (slashp
[2] == '.'
6760 && (slashp
[3] == '/' || slashp
[3] == '\0'))
6765 while (cp
>= res
&& !filename_is_absolute (cp
));
6767 cp
= slashp
; /* the absolute name begins with "/.." */
6769 /* Under MSDOS and NT we get `d:/NAME' as absolute
6770 file name, so the luser could say `d:/../NAME'.
6771 We silently treat this as `d:/NAME'. */
6772 else if (cp
[0] != '/')
6775 strcpy (cp
, slashp
+ 3);
6779 else if (slashp
[2] == '/' || slashp
[2] == '\0')
6781 strcpy (slashp
, slashp
+ 2);
6786 slashp
= etags_strchr (slashp
+ 1, '/');
6790 return savestr ("/");
6795 /* Return a newly allocated string containing the absolute
6796 file name of dir where FILE resides given DIR (which should
6797 end with a slash). */
6799 absolute_dirname (file
, dir
)
6805 canonicalize_filename (file
);
6806 slashp
= etags_strrchr (file
, '/');
6808 return savestr (dir
);
6811 res
= absolute_filename (file
, dir
);
6817 /* Whether the argument string is an absolute file name. The argument
6818 string must have been canonicalized with canonicalize_filename. */
6820 filename_is_absolute (fn
)
6823 return (fn
[0] == '/'
6825 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
6830 /* Translate backslashes into slashes. Works in place. */
6832 canonicalize_filename (fn
)
6836 /* Canonicalize drive letter case. */
6837 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
6838 fn
[0] = upcase (fn
[0]);
6839 /* Convert backslashes to slashes. */
6840 for (; *fn
!= '\0'; fn
++)
6845 fn
= NULL
; /* shut up the compiler */
6850 /* Initialize a linebuffer for use */
6852 linebuffer_init (lbp
)
6855 lbp
->size
= (DEBUG
) ? 3 : 200;
6856 lbp
->buffer
= xnew (lbp
->size
, char);
6857 lbp
->buffer
[0] = '\0';
6861 /* Set the minimum size of a string contained in a linebuffer. */
6863 linebuffer_setlen (lbp
, toksize
)
6867 while (lbp
->size
<= toksize
)
6870 xrnew (lbp
->buffer
, lbp
->size
, char);
6875 /* Like malloc but get fatal error if memory is exhausted. */
6880 PTR result
= (PTR
) malloc (size
);
6882 fatal ("virtual memory exhausted", (char *)NULL
);
6887 xrealloc (ptr
, size
)
6891 PTR result
= (PTR
) realloc (ptr
, size
);
6893 fatal ("virtual memory exhausted", (char *)NULL
);
6899 * indent-tabs-mode: t
6902 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
6906 /* arch-tag: 8a9b748d-390c-4922-99db-2eeefa921051
6907 (do not change this comment) */
6909 /* etags.c ends here */