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.25";
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 # ifndef PTR /* for XEmacs */
65 # ifndef __P /* for XEmacs */
66 # define __P(args) args
68 #else /* no config.h */
69 # if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
70 # define __P(args) args /* use prototypes */
71 # define PTR void * /* for generic pointers */
72 # else /* not standard C */
73 # define __P(args) () /* no prototypes */
74 # define const /* remove const for old compilers' sake */
75 # define PTR long * /* don't use void* */
77 #endif /* !HAVE_CONFIG_H */
80 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
83 /* WIN32_NATIVE is for XEmacs.
84 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
89 #endif /* WIN32_NATIVE */
95 # include <sys/param.h>
97 # ifndef HAVE_CONFIG_H
99 # include <sys/config.h>
111 # define MAXPATHLEN _MAX_PATH
117 # endif /* undef HAVE_GETCWD */
118 #else /* not WINDOWSNT */
122 # else /* no standard C headers */
123 extern char *getenv ();
125 # define EXIT_SUCCESS 1
126 # define EXIT_FAILURE 0
128 # define EXIT_SUCCESS 0
129 # define EXIT_FAILURE 1
132 #endif /* !WINDOWSNT */
137 # if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
138 extern char *getcwd (char *buf
, size_t size
);
140 #endif /* HAVE_UNISTD_H */
148 #include <sys/types.h>
149 #include <sys/stat.h>
153 # undef assert /* some systems have a buggy assert.h */
154 # define assert(x) ((void) 0)
157 #if !defined (S_ISREG) && defined (S_IFREG)
158 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
161 #ifdef NO_LONG_OPTIONS /* define this if you don't have GNU getopt */
162 # define NO_LONG_OPTIONS TRUE
163 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
165 extern int optind
, opterr
;
167 # define NO_LONG_OPTIONS FALSE
169 #endif /* NO_LONG_OPTIONS */
171 #ifndef HAVE_CONFIG_H /* this is a standalone compilation */
172 # ifdef __CYGWIN__ /* compiling on Cygwin */
174 the regex
.h distributed with Cygwin is
not compatible with etags
, alas
!
175 If you want regular expression support
, you should
delete this notice
and
176 arrange to use the GNU regex
.h
and regex
.c
.
181 /* Define CTAGS to make the program "ctags" compatible with the usual one.
182 Leave it undefined to make the program "etags", which makes emacs-style
183 tag tables and tags typedefs, #defines and struct/union/enum by default. */
191 #define streq(s,t) (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t))
192 #define strcaseeq(s,t) (assert((s)!=NULL && (t)!=NULL), !etags_strcasecmp (s, t))
193 #define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
194 #define strncaseeq(s,t,n) (assert((s)!=NULL && (t)!=NULL), !etags_strncasecmp (s, t, n))
196 #define CHARS 256 /* 2^sizeof(char) */
197 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
198 #define iswhite(c) (_wht[CHAR(c)]) /* c is white (see white) */
199 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name (see nonam) */
200 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token (see begtk) */
201 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token (see midtk) */
202 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens (see endtk) */
204 #define ISALNUM(c) isalnum (CHAR(c))
205 #define ISALPHA(c) isalpha (CHAR(c))
206 #define ISDIGIT(c) isdigit (CHAR(c))
207 #define ISLOWER(c) islower (CHAR(c))
209 #define lowcase(c) tolower (CHAR(c))
210 #define upcase(c) toupper (CHAR(c))
214 * xnew, xrnew -- allocate, reallocate storage
216 * SYNOPSIS: Type *xnew (int n, Type);
217 * void xrnew (OldPointer, int n, Type);
220 # include "chkmalloc.h"
221 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
222 (n) * sizeof (Type)))
223 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
224 (char *) (op), (n) * sizeof (Type)))
226 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
227 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
228 (char *) (op), (n) * sizeof (Type)))
233 typedef void Lang_function
__P((FILE *));
237 char *suffix
; /* file name suffix for this compressor */
238 char *command
; /* takes one arg and decompresses to stdout */
243 char *name
; /* language name */
244 char *help
; /* detailed help for the language */
245 Lang_function
*function
; /* parse function */
246 char **suffixes
; /* name suffixes of this language's files */
247 char **filenames
; /* names of this language's files */
248 char **interpreters
; /* interpreters for this language */
249 bool metasource
; /* source used to generate other sources */
254 struct fdesc
*next
; /* for the linked list */
255 char *infname
; /* uncompressed input file name */
256 char *infabsname
; /* absolute uncompressed input file name */
257 char *infabsdir
; /* absolute dir of input file */
258 char *taggedfname
; /* file name to write in tagfile */
259 language
*lang
; /* language of file */
260 char *prop
; /* file properties to write in tagfile */
261 bool usecharno
; /* etags tags shall contain char number */
262 bool written
; /* entry written in the tags file */
265 typedef struct node_st
266 { /* sorting structure */
267 struct node_st
*left
, *right
; /* left and right sons */
268 fdesc
*fdp
; /* description of file to whom tag belongs */
269 char *name
; /* tag name */
270 char *regex
; /* search regexp */
271 bool valid
; /* write this tag on the tag file */
272 bool is_func
; /* function tag: use regexp in CTAGS mode */
273 bool been_warned
; /* warning already given for duplicated tag */
274 int lno
; /* line number tag is on */
275 long cno
; /* character number line starts on */
279 * A `linebuffer' is a structure which holds a line of text.
280 * `readline_internal' reads a line from a stream into a linebuffer
281 * and works regardless of the length of the line.
282 * SIZE is the size of BUFFER, LEN is the length of the string in
283 * BUFFER after readline reads it.
292 /* Used to support mixing of --lang and file names. */
296 at_language
, /* a language specification */
297 at_regexp
, /* a regular expression */
298 at_filename
, /* a file name */
299 at_stdin
, /* read from stdin here */
300 at_end
/* stop parsing the list */
301 } arg_type
; /* argument type */
302 language
*lang
; /* language associated with the argument */
303 char *what
; /* the argument itself */
306 /* Structure defining a regular expression. */
307 typedef struct regexp
309 struct regexp
*p_next
; /* pointer to next in list */
310 language
*lang
; /* if set, use only for this language */
311 char *pattern
; /* the regexp pattern */
312 char *name
; /* tag name */
313 struct re_pattern_buffer
*pat
; /* the compiled pattern */
314 struct re_registers regs
; /* re registers */
315 bool error_signaled
; /* already signaled for this regexp */
316 bool force_explicit_name
; /* do not allow implict tag name */
317 bool ignore_case
; /* ignore case when matching */
318 bool multi_line
; /* do a multi-line match on the whole file */
322 /* Many compilers barf on this:
323 Lang_function Ada_funcs;
324 so let's write it this way */
325 static void Ada_funcs
__P((FILE *));
326 static void Asm_labels
__P((FILE *));
327 static void C_entries
__P((int c_ext
, FILE *));
328 static void default_C_entries
__P((FILE *));
329 static void plain_C_entries
__P((FILE *));
330 static void Cjava_entries
__P((FILE *));
331 static void Cobol_paragraphs
__P((FILE *));
332 static void Cplusplus_entries
__P((FILE *));
333 static void Cstar_entries
__P((FILE *));
334 static void Erlang_functions
__P((FILE *));
335 static void Forth_words
__P((FILE *));
336 static void Fortran_functions
__P((FILE *));
337 static void HTML_labels
__P((FILE *));
338 static void Lisp_functions
__P((FILE *));
339 static void Lua_functions
__P((FILE *));
340 static void Makefile_targets
__P((FILE *));
341 static void Pascal_functions
__P((FILE *));
342 static void Perl_functions
__P((FILE *));
343 static void PHP_functions
__P((FILE *));
344 static void PS_functions
__P((FILE *));
345 static void Prolog_functions
__P((FILE *));
346 static void Python_functions
__P((FILE *));
347 static void Scheme_functions
__P((FILE *));
348 static void TeX_commands
__P((FILE *));
349 static void Texinfo_nodes
__P((FILE *));
350 static void Yacc_entries
__P((FILE *));
351 static void just_read_file
__P((FILE *));
353 static void print_language_names
__P((void));
354 static void print_version
__P((void));
355 static void print_help
__P((argument
*));
356 int main
__P((int, char **));
358 static compressor
*get_compressor_from_suffix
__P((char *, char **));
359 static language
*get_language_from_langname
__P((const char *));
360 static language
*get_language_from_interpreter
__P((char *));
361 static language
*get_language_from_filename
__P((char *, bool));
362 static void readline
__P((linebuffer
*, FILE *));
363 static long readline_internal
__P((linebuffer
*, FILE *));
364 static bool nocase_tail
__P((char *));
365 static void get_tag
__P((char *, char **));
367 static void analyse_regex
__P((char *));
368 static void free_regexps
__P((void));
369 static void regex_tag_multiline
__P((void));
370 static void error
__P((const char *, const char *));
371 static void suggest_asking_for_help
__P((void));
372 void fatal
__P((char *, char *));
373 static void pfatal
__P((char *));
374 static void add_node
__P((node
*, node
**));
376 static void init
__P((void));
377 static void process_file_name
__P((char *, language
*));
378 static void process_file
__P((FILE *, char *, language
*));
379 static void find_entries
__P((FILE *));
380 static void free_tree
__P((node
*));
381 static void free_fdesc
__P((fdesc
*));
382 static void pfnote
__P((char *, bool, char *, int, int, long));
383 static void make_tag
__P((char *, int, bool, char *, int, int, long));
384 static void invalidate_nodes
__P((fdesc
*, node
**));
385 static void put_entries
__P((node
*));
387 static char *concat
__P((char *, char *, char *));
388 static char *skip_spaces
__P((char *));
389 static char *skip_non_spaces
__P((char *));
390 static char *savenstr
__P((char *, int));
391 static char *savestr
__P((char *));
392 static char *etags_strchr
__P((const char *, int));
393 static char *etags_strrchr
__P((const char *, int));
394 static int etags_strcasecmp
__P((const char *, const char *));
395 static int etags_strncasecmp
__P((const char *, const char *, int));
396 static char *etags_getcwd
__P((void));
397 static char *relative_filename
__P((char *, char *));
398 static char *absolute_filename
__P((char *, char *));
399 static char *absolute_dirname
__P((char *, char *));
400 static bool filename_is_absolute
__P((char *f
));
401 static void canonicalize_filename
__P((char *));
402 static void linebuffer_init
__P((linebuffer
*));
403 static void linebuffer_setlen
__P((linebuffer
*, int));
404 static PTR xmalloc
__P((unsigned int));
405 static PTR xrealloc
__P((char *, unsigned int));
408 static char searchar
= '/'; /* use /.../ searches */
410 static char *tagfile
; /* output file */
411 static char *progname
; /* name this program was invoked with */
412 static char *cwd
; /* current working directory */
413 static char *tagfiledir
; /* directory of tagfile */
414 static FILE *tagf
; /* ioptr for tags file */
416 static fdesc
*fdhead
; /* head of file description list */
417 static fdesc
*curfdp
; /* current file description */
418 static int lineno
; /* line number of current line */
419 static long charno
; /* current character number */
420 static long linecharno
; /* charno of start of current line */
421 static char *dbp
; /* pointer to start of current tag */
423 static const int invalidcharno
= -1;
425 static node
*nodehead
; /* the head of the binary tree of tags */
426 static node
*last_node
; /* the last node created */
428 static linebuffer lb
; /* the current line */
429 static linebuffer filebuf
; /* a buffer containing the whole file */
430 static linebuffer token_name
; /* a buffer containing a tag name */
432 /* boolean "functions" (see init) */
433 static bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
436 *white
= " \f\t\n\r\v",
438 *nonam
= " \f\t\n\r()=,;", /* look at make_tag before modifying! */
439 /* token ending chars */
440 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
441 /* token starting chars */
442 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
443 /* valid in-token chars */
444 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
446 static bool append_to_tagfile
; /* -a: append to tags */
447 /* The next four default to TRUE for etags, but to FALSE for ctags. */
448 static bool typedefs
; /* -t: create tags for C and Ada typedefs */
449 static bool typedefs_or_cplusplus
; /* -T: create tags for C typedefs, level */
450 /* 0 struct/enum/union decls, and C++ */
451 /* member functions. */
452 static bool constantypedefs
; /* -d: create tags for C #define, enum */
453 /* constants and variables. */
454 /* -D: opposite of -d. Default under ctags. */
455 static bool globals
; /* create tags for global variables */
456 static bool declarations
; /* --declarations: tag them and extern in C&Co*/
457 static bool members
; /* create tags for C member variables */
458 static bool no_line_directive
; /* ignore #line directives (undocumented) */
459 static bool update
; /* -u: update tags */
460 static bool vgrind_style
; /* -v: create vgrind style index output */
461 static bool no_warnings
; /* -w: suppress warnings */
462 static bool cxref_style
; /* -x: create cxref style output */
463 static bool cplusplus
; /* .[hc] means C++, not C */
464 static bool ignoreindent
; /* -I: ignore indentation in C */
465 static bool packages_only
; /* --packages-only: in Ada, only tag packages*/
467 /* STDIN is defined in LynxOS system headers */
472 #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
473 static bool parsing_stdin
; /* --parse-stdin used */
475 static regexp
*p_head
; /* list of all regexps */
476 static bool need_filebuf
; /* some regexes are multi-line */
478 static struct option longopts
[] =
480 { "append", no_argument
, NULL
, 'a' },
481 { "packages-only", no_argument
, &packages_only
, TRUE
},
482 { "c++", no_argument
, NULL
, 'C' },
483 { "declarations", no_argument
, &declarations
, TRUE
},
484 { "no-line-directive", no_argument
, &no_line_directive
, TRUE
},
485 { "help", no_argument
, NULL
, 'h' },
486 { "help", no_argument
, NULL
, 'H' },
487 { "ignore-indentation", no_argument
, NULL
, 'I' },
488 { "language", required_argument
, NULL
, 'l' },
489 { "members", no_argument
, &members
, TRUE
},
490 { "no-members", no_argument
, &members
, FALSE
},
491 { "output", required_argument
, NULL
, 'o' },
492 { "regex", required_argument
, NULL
, 'r' },
493 { "no-regex", no_argument
, NULL
, 'R' },
494 { "ignore-case-regex", required_argument
, NULL
, 'c' },
495 { "parse-stdin", required_argument
, NULL
, STDIN
},
496 { "version", no_argument
, NULL
, 'V' },
498 #if CTAGS /* Ctags options */
499 { "backward-search", no_argument
, NULL
, 'B' },
500 { "cxref", no_argument
, NULL
, 'x' },
501 { "defines", no_argument
, NULL
, 'd' },
502 { "globals", no_argument
, &globals
, TRUE
},
503 { "typedefs", no_argument
, NULL
, 't' },
504 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
505 { "update", no_argument
, NULL
, 'u' },
506 { "vgrind", no_argument
, NULL
, 'v' },
507 { "no-warn", no_argument
, NULL
, 'w' },
509 #else /* Etags options */
510 { "no-defines", no_argument
, NULL
, 'D' },
511 { "no-globals", no_argument
, &globals
, FALSE
},
512 { "include", required_argument
, NULL
, 'i' },
517 static compressor compressors
[] =
519 { "z", "gzip -d -c"},
520 { "Z", "gzip -d -c"},
521 { "gz", "gzip -d -c"},
522 { "GZ", "gzip -d -c"},
523 { "bz2", "bzip2 -d -c" },
532 static char *Ada_suffixes
[] =
533 { "ads", "adb", "ada", NULL
};
534 static char Ada_help
[] =
535 "In Ada code, functions, procedures, packages, tasks and types are\n\
536 tags. Use the `--packages-only' option to create tags for\n\
538 Ada tag names have suffixes indicating the type of entity:\n\
539 Entity type: Qualifier:\n\
540 ------------ ----------\n\
547 Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
548 body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
549 will just search for any tag `bidule'.";
552 static char *Asm_suffixes
[] =
553 { "a", /* Unix assembler */
554 "asm", /* Microcontroller assembly */
555 "def", /* BSO/Tasking definition includes */
556 "inc", /* Microcontroller include files */
557 "ins", /* Microcontroller include files */
558 "s", "sa", /* Unix assembler */
559 "S", /* cpp-processed Unix assembler */
560 "src", /* BSO/Tasking C compiler output */
563 static char Asm_help
[] =
564 "In assembler code, labels appearing at the beginning of a line,\n\
565 followed by a colon, are tags.";
568 /* Note that .c and .h can be considered C++, if the --c++ flag was
569 given, or if the `class' or `template' keyowrds are met inside the file.
570 That is why default_C_entries is called for these. */
571 static char *default_C_suffixes
[] =
573 static char default_C_help
[] =
574 "In C code, any C function or typedef is a tag, and so are\n\
575 definitions of `struct', `union' and `enum'. `#define' macro\n\
576 definitions and `enum' constants are tags unless you specify\n\
577 `--no-defines'. Global variables are tags unless you specify\n\
578 `--no-globals'. Use of `--no-globals' and `--no-defines'\n\
579 can make the tags table file much smaller.\n\
580 You can tag function declarations and external variables by\n\
581 using `--declarations', and struct members by using `--members'.";
583 static char *Cplusplus_suffixes
[] =
584 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
585 "M", /* Objective C++ */
586 "pdb", /* Postscript with C syntax */
588 static char Cplusplus_help
[] =
589 "In C++ code, all the tag constructs of C code are tagged. (Use\n\
590 --help --lang=c --lang=c++ for full help.)\n\
591 In addition to C tags, member functions are also recognized, and\n\
592 optionally member variables if you use the `--members' option.\n\
593 Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
594 and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
597 static char *Cjava_suffixes
[] =
599 static char Cjava_help
[] =
600 "In Java code, all the tags constructs of C and C++ code are\n\
601 tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
604 static char *Cobol_suffixes
[] =
605 { "COB", "cob", NULL
};
606 static char Cobol_help
[] =
607 "In Cobol code, tags are paragraph names; that is, any word\n\
608 starting in column 8 and followed by a period.";
610 static char *Cstar_suffixes
[] =
611 { "cs", "hs", NULL
};
613 static char *Erlang_suffixes
[] =
614 { "erl", "hrl", NULL
};
615 static char Erlang_help
[] =
616 "In Erlang code, the tags are the functions, records and macros\n\
617 defined in the file.";
619 char *Forth_suffixes
[] =
620 { "fth", "tok", NULL
};
621 static char Forth_help
[] =
622 "In Forth code, tags are words defined by `:',\n\
623 constant, code, create, defer, value, variable, buffer:, field.";
625 static char *Fortran_suffixes
[] =
626 { "F", "f", "f90", "for", NULL
};
627 static char Fortran_help
[] =
628 "In Fortran code, functions, subroutines and block data are tags.";
630 static char *HTML_suffixes
[] =
631 { "htm", "html", "shtml", NULL
};
632 static char HTML_help
[] =
633 "In HTML input files, the tags are the `title' and the `h1', `h2',\n\
634 `h3' headers. Also, tags are `name=' in anchors and all\n\
635 occurrences of `id='.";
637 static char *Lisp_suffixes
[] =
638 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
639 static char Lisp_help
[] =
640 "In Lisp code, any function defined with `defun', any variable\n\
641 defined with `defvar' or `defconst', and in general the first\n\
642 argument of any expression that starts with `(def' in column zero\n\
645 static char *Lua_suffixes
[] =
646 { "lua", "LUA", NULL
};
647 static char Lua_help
[] =
648 "In Lua scripts, all functions are tags.";
650 static char *Makefile_filenames
[] =
651 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
652 static char Makefile_help
[] =
653 "In makefiles, targets are tags; additionally, variables are tags\n\
654 unless you specify `--no-globals'.";
656 static char *Objc_suffixes
[] =
657 { "lm", /* Objective lex file */
658 "m", /* Objective C file */
660 static char Objc_help
[] =
661 "In Objective C code, tags include Objective C definitions for classes,\n\
662 class categories, methods and protocols. Tags for variables and\n\
663 functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.\n\
664 (Use --help --lang=c --lang=objc --lang=java for full help.)";
666 static char *Pascal_suffixes
[] =
667 { "p", "pas", NULL
};
668 static char Pascal_help
[] =
669 "In Pascal code, the tags are the functions and procedures defined\n\
671 /* " // this is for working around an Emacs highlighting bug... */
673 static char *Perl_suffixes
[] =
674 { "pl", "pm", NULL
};
675 static char *Perl_interpreters
[] =
676 { "perl", "@PERL@", NULL
};
677 static char Perl_help
[] =
678 "In Perl code, the tags are the packages, subroutines and variables\n\
679 defined by the `package', `sub', `my' and `local' keywords. Use\n\
680 `--globals' if you want to tag global variables. Tags for\n\
681 subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
682 defined in the default package is `main::SUB'.";
684 static char *PHP_suffixes
[] =
685 { "php", "php3", "php4", NULL
};
686 static char PHP_help
[] =
687 "In PHP code, tags are functions, classes and defines. When using\n\
688 the `--members' option, vars are tags too.";
690 static char *plain_C_suffixes
[] =
691 { "pc", /* Pro*C file */
694 static char *PS_suffixes
[] =
695 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
696 static char PS_help
[] =
697 "In PostScript code, the tags are the functions.";
699 static char *Prolog_suffixes
[] =
701 static char Prolog_help
[] =
702 "In Prolog code, tags are predicates and rules at the beginning of\n\
705 static char *Python_suffixes
[] =
707 static char Python_help
[] =
708 "In Python code, `def' or `class' at the beginning of a line\n\
711 /* Can't do the `SCM' or `scm' prefix with a version number. */
712 static char *Scheme_suffixes
[] =
713 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
714 static char Scheme_help
[] =
715 "In Scheme code, tags include anything defined with `def' or with a\n\
716 construct whose name starts with `def'. They also include\n\
717 variables set with `set!' at top level in the file.";
719 static char *TeX_suffixes
[] =
720 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
721 static char TeX_help
[] =
722 "In LaTeX text, the argument of any of the commands `\\chapter',\n\
723 `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
724 `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
725 `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
726 `\\newenvironment' or `\\renewenvironment' is a tag.\n\
728 Other commands can be specified by setting the environment variable\n\
729 `TEXTAGS' to a colon-separated list like, for example,\n\
730 TEXTAGS=\"mycommand:myothercommand\".";
733 static char *Texinfo_suffixes
[] =
734 { "texi", "texinfo", "txi", NULL
};
735 static char Texinfo_help
[] =
736 "for texinfo files, lines starting with @node are tagged.";
738 static char *Yacc_suffixes
[] =
739 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
740 static char Yacc_help
[] =
741 "In Bison or Yacc input files, each rule defines as a tag the\n\
742 nonterminal it constructs. The portions of the file that contain\n\
743 C code are parsed as C code (use --help --lang=c --lang=yacc\n\
746 static char auto_help
[] =
747 "`auto' is not a real language, it indicates to use\n\
748 a default language for files base on file name suffix and file contents.";
750 static char none_help
[] =
751 "`none' is not a real language, it indicates to only do\n\
752 regexp processing on files.";
754 static char no_lang_help
[] =
755 "No detailed help available for this language.";
759 * Table of languages.
761 * It is ok for a given function to be listed under more than one
762 * name. I just didn't.
765 static language lang_names
[] =
767 { "ada", Ada_help
, Ada_funcs
, Ada_suffixes
},
768 { "asm", Asm_help
, Asm_labels
, Asm_suffixes
},
769 { "c", default_C_help
, default_C_entries
, default_C_suffixes
},
770 { "c++", Cplusplus_help
, Cplusplus_entries
, Cplusplus_suffixes
},
771 { "c*", no_lang_help
, Cstar_entries
, Cstar_suffixes
},
772 { "cobol", Cobol_help
, Cobol_paragraphs
, Cobol_suffixes
},
773 { "erlang", Erlang_help
, Erlang_functions
, Erlang_suffixes
},
774 { "forth", Forth_help
, Forth_words
, Forth_suffixes
},
775 { "fortran", Fortran_help
, Fortran_functions
, Fortran_suffixes
},
776 { "html", HTML_help
, HTML_labels
, HTML_suffixes
},
777 { "java", Cjava_help
, Cjava_entries
, Cjava_suffixes
},
778 { "lisp", Lisp_help
, Lisp_functions
, Lisp_suffixes
},
779 { "lua", Lua_help
, Lua_functions
, Lua_suffixes
},
780 { "makefile", Makefile_help
,Makefile_targets
,NULL
,Makefile_filenames
},
781 { "objc", Objc_help
, plain_C_entries
, Objc_suffixes
},
782 { "pascal", Pascal_help
, Pascal_functions
, Pascal_suffixes
},
783 { "perl",Perl_help
,Perl_functions
,Perl_suffixes
,NULL
,Perl_interpreters
},
784 { "php", PHP_help
, PHP_functions
, PHP_suffixes
},
785 { "postscript",PS_help
, PS_functions
, PS_suffixes
},
786 { "proc", no_lang_help
, plain_C_entries
, plain_C_suffixes
},
787 { "prolog", Prolog_help
, Prolog_functions
, Prolog_suffixes
},
788 { "python", Python_help
, Python_functions
, Python_suffixes
},
789 { "scheme", Scheme_help
, Scheme_functions
, Scheme_suffixes
},
790 { "tex", TeX_help
, TeX_commands
, TeX_suffixes
},
791 { "texinfo", Texinfo_help
, Texinfo_nodes
, Texinfo_suffixes
},
792 { "yacc", Yacc_help
,Yacc_entries
,Yacc_suffixes
,NULL
,NULL
,TRUE
},
793 { "auto", auto_help
}, /* default guessing scheme */
794 { "none", none_help
, just_read_file
}, /* regexp matching only */
795 { NULL
} /* end of list */
800 print_language_names ()
805 puts ("\nThese are the currently supported languages, along with the\n\
806 default file names and dot suffixes:");
807 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
809 printf (" %-*s", 10, lang
->name
);
810 if (lang
->filenames
!= NULL
)
811 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
812 printf (" %s", *name
);
813 if (lang
->suffixes
!= NULL
)
814 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
815 printf (" .%s", *ext
);
818 puts ("where `auto' means use default language for files based on file\n\
819 name suffix, and `none' means only do regexp processing on files.\n\
820 If no language is specified and no matching suffix is found,\n\
821 the first line of the file is read for a sharp-bang (#!) sequence\n\
822 followed by the name of an interpreter. If no such sequence is found,\n\
823 Fortran is tried first; if no tags are found, C is tried next.\n\
824 When parsing any C file, a \"class\" or \"template\" keyword\n\
826 puts ("Compressed files are supported using gzip and bzip2.\n\
828 For detailed help on a given language use, for example,\n\
829 etags --help --lang=ada.");
833 # define EMACS_NAME "standalone"
836 # define VERSION "version"
841 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
842 puts ("Copyright (C) 2006 Free Software Foundation, Inc. and Ken Arnold");
843 puts ("This program is distributed under the same terms as Emacs");
849 print_help (argbuffer
)
852 bool help_for_lang
= FALSE
;
854 for (; argbuffer
->arg_type
!= at_end
; argbuffer
++)
855 if (argbuffer
->arg_type
== at_language
)
859 puts (argbuffer
->lang
->help
);
860 help_for_lang
= TRUE
;
866 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
868 These are the options accepted by %s.\n", progname
, progname
);
870 puts ("WARNING: long option names do not work with this executable,\n\
871 as it is not linked with GNU getopt.");
873 puts ("You may use unambiguous abbreviations for the long option names.");
874 puts (" A - as file name means read names from stdin (one per line).\n\
875 Absolute names are stored in the output file as they are.\n\
876 Relative ones are stored relative to the output file's directory.\n");
878 puts ("-a, --append\n\
879 Append tag entries to existing tags file.");
881 puts ("--packages-only\n\
882 For Ada files, only generate tags for packages.");
885 puts ("-B, --backward-search\n\
886 Write the search commands for the tag entries using '?', the\n\
887 backward-search command instead of '/', the forward-search command.");
889 /* This option is mostly obsolete, because etags can now automatically
890 detect C++. Retained for backward compatibility and for debugging and
891 experimentation. In principle, we could want to tag as C++ even
892 before any "class" or "template" keyword.
894 Treat files whose name suffix defaults to C language as C++ files.");
897 puts ("--declarations\n\
898 In C and derived languages, create tags for function declarations,");
900 puts ("\tand create tags for extern variables if --globals is used.");
903 ("\tand create tags for extern variables unless --no-globals is used.");
906 puts ("-d, --defines\n\
907 Create tag entries for C #define constants and enum constants, too.");
909 puts ("-D, --no-defines\n\
910 Don't create tag entries for C #define constants and enum constants.\n\
911 This makes the tags file smaller.");
914 puts ("-i FILE, --include=FILE\n\
915 Include a note in tag file indicating that, when searching for\n\
916 a tag, one should also consult the tags file FILE after\n\
917 checking the current file.");
919 puts ("-l LANG, --language=LANG\n\
920 Force the following files to be considered as written in the\n\
921 named language up to the next --language=LANG option.");
925 Create tag entries for global variables in some languages.");
927 puts ("--no-globals\n\
928 Do not create tag entries for global variables in some\n\
929 languages. This makes the tags file smaller.");
931 Create tag entries for members of structures in some languages.");
933 puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
934 Make a tag for each line matching a regular expression pattern\n\
935 in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
936 files only. REGEXFILE is a file containing one REGEXP per line.\n\
937 REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
938 optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
939 puts (" If TAGNAME/ is present, the tags created are named.\n\
940 For example Tcl named tags can be created with:\n\
941 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
942 MODS are optional one-letter modifiers: `i' means to ignore case,\n\
943 `m' means to allow multi-line matches, `s' implies `m' and\n\
944 causes dot to match any character, including newline.");
945 puts ("-R, --no-regex\n\
946 Don't create tags from regexps for the following files.");
947 puts ("-I, --ignore-indentation\n\
948 In C and C++ do not assume that a closing brace in the first\n\
949 column is the final brace of a function or structure definition.");
950 puts ("-o FILE, --output=FILE\n\
951 Write the tags to FILE.");
952 puts ("--parse-stdin=NAME\n\
953 Read from standard input and record tags as belonging to file NAME.");
957 puts ("-t, --typedefs\n\
958 Generate tag entries for C and Ada typedefs.");
959 puts ("-T, --typedefs-and-c++\n\
960 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
961 and C++ member functions.");
965 puts ("-u, --update\n\
966 Update the tag entries for the given files, leaving tag\n\
967 entries for other files in place. Currently, this is\n\
968 implemented by deleting the existing entries for the given\n\
969 files and then rewriting the new entries at the end of the\n\
970 tags file. It is often faster to simply rebuild the entire\n\
971 tag file than to use this.");
975 puts ("-v, --vgrind\n\
976 Print on the standard output an index of items intended for\n\
977 human consumption, similar to the output of vgrind. The index\n\
978 is sorted, and gives the page number of each item.");
979 puts ("-w, --no-warn\n\
980 Suppress warning messages about entries defined in multiple\n\
982 puts ("-x, --cxref\n\
983 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
984 The output uses line numbers instead of page numbers, but\n\
985 beyond that the differences are cosmetic; try both to see\n\
989 puts ("-V, --version\n\
990 Print the version of the program.\n\
992 Print this help message.\n\
993 Followed by one or more `--language' options prints detailed\n\
994 help about tag generation for the specified languages.");
996 print_language_names ();
999 puts ("Report bugs to bug-gnu-emacs@gnu.org");
1001 exit (EXIT_SUCCESS
);
1005 #ifdef VMS /* VMS specific functions */
1009 /* This is a BUG! ANY arbitrary limit is a BUG!
1010 Won't someone please fix this? */
1011 #define MAX_FILE_SPEC_LEN 255
1014 char body
[MAX_FILE_SPEC_LEN
+ 1];
1018 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
1019 returning in each successive call the next file name matching the input
1020 spec. The function expects that each in_spec passed
1021 to it will be processed to completion; in particular, up to and
1022 including the call following that in which the last matching name
1023 is returned, the function ignores the value of in_spec, and will
1024 only start processing a new spec with the following call.
1025 If an error occurs, on return out_spec contains the value
1026 of in_spec when the error occurred.
1028 With each successive file name returned in out_spec, the
1029 function's return value is one. When there are no more matching
1030 names the function returns zero. If on the first call no file
1031 matches in_spec, or there is any other error, -1 is returned.
1035 #include <descrip.h>
1036 #define OUTSIZE MAX_FILE_SPEC_LEN
1042 static long context
= 0;
1043 static struct dsc$descriptor_s o
;
1044 static struct dsc$descriptor_s i
;
1045 static bool pass1
= TRUE
;
1052 o
.dsc$a_pointer
= (char *) out
;
1053 o
.dsc$w_length
= (short)OUTSIZE
;
1054 i
.dsc$a_pointer
= in
;
1055 i
.dsc$w_length
= (short)strlen(in
);
1056 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
1057 i
.dsc$b_class
= DSC$K_CLASS_S
;
1058 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
1059 o
.dsc$b_class
= DSC$K_CLASS_VS
;
1061 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
1063 out
->body
[out
->curlen
] = EOS
;
1066 else if (status
== RMS$_NMF
)
1070 strcpy(out
->body
, in
);
1073 lib$
find_file_end(&context
);
1079 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
1080 name of each file specified by the provided arg expanding wildcards.
1083 gfnames (arg
, p_error
)
1087 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
1089 switch (fn_exp (&filename
, arg
))
1093 return filename
.body
;
1099 return filename
.body
;
1103 #ifndef OLD /* Newer versions of VMS do provide `system'. */
1107 error ("%s", "system() function not implemented under VMS");
1111 #define VERSION_DELIM ';'
1112 char *massage_name (s
)
1118 if (*s
== VERSION_DELIM
)
1136 unsigned int nincluded_files
;
1137 char **included_files
;
1138 argument
*argbuffer
;
1139 int current_arg
, file_count
;
1140 linebuffer filename_lb
;
1141 bool help_asked
= FALSE
;
1150 _fmode
= O_BINARY
; /* all of files are treated as binary files */
1154 nincluded_files
= 0;
1155 included_files
= xnew (argc
, char *);
1159 /* Allocate enough no matter what happens. Overkill, but each one
1161 argbuffer
= xnew (argc
, argument
);
1164 * If etags, always find typedefs and structure tags. Why not?
1165 * Also default to find macro constants, enum constants and
1170 typedefs
= typedefs_or_cplusplus
= constantypedefs
= TRUE
;
1174 /* When the optstring begins with a '-' getopt_long does not rearrange the
1175 non-options arguments to be at the end, but leaves them alone. */
1176 optstring
= concat (NO_LONG_OPTIONS
? "" : "-",
1177 "ac:Cf:Il:o:r:RSVhH",
1178 (CTAGS
) ? "BxdtTuvw" : "Di:");
1180 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, NULL
)) != EOF
)
1184 /* If getopt returns 0, then it has already processed a
1185 long-named option. We should do nothing. */
1189 /* This means that a file name has been seen. Record it. */
1190 argbuffer
[current_arg
].arg_type
= at_filename
;
1191 argbuffer
[current_arg
].what
= optarg
;
1197 /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
1198 argbuffer
[current_arg
].arg_type
= at_stdin
;
1199 argbuffer
[current_arg
].what
= optarg
;
1203 fatal ("cannot parse standard input more than once", (char *)NULL
);
1204 parsing_stdin
= TRUE
;
1207 /* Common options. */
1208 case 'a': append_to_tagfile
= TRUE
; break;
1209 case 'C': cplusplus
= TRUE
; break;
1210 case 'f': /* for compatibility with old makefiles */
1214 error ("-o option may only be given once.", (char *)NULL
);
1215 suggest_asking_for_help ();
1221 case 'S': /* for backward compatibility */
1222 ignoreindent
= TRUE
;
1226 language
*lang
= get_language_from_langname (optarg
);
1229 argbuffer
[current_arg
].lang
= lang
;
1230 argbuffer
[current_arg
].arg_type
= at_language
;
1236 /* Backward compatibility: support obsolete --ignore-case-regexp. */
1237 optarg
= concat (optarg
, "i", ""); /* memory leak here */
1240 argbuffer
[current_arg
].arg_type
= at_regexp
;
1241 argbuffer
[current_arg
].what
= optarg
;
1245 argbuffer
[current_arg
].arg_type
= at_regexp
;
1246 argbuffer
[current_arg
].what
= NULL
;
1258 case 'D': constantypedefs
= FALSE
; break;
1259 case 'i': included_files
[nincluded_files
++] = optarg
; break;
1261 /* Ctags options. */
1262 case 'B': searchar
= '?'; break;
1263 case 'd': constantypedefs
= TRUE
; break;
1264 case 't': typedefs
= TRUE
; break;
1265 case 'T': typedefs
= typedefs_or_cplusplus
= TRUE
; break;
1266 case 'u': update
= TRUE
; break;
1267 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1268 case 'x': cxref_style
= TRUE
; break;
1269 case 'w': no_warnings
= TRUE
; break;
1271 suggest_asking_for_help ();
1275 /* No more options. Store the rest of arguments. */
1276 for (; optind
< argc
; optind
++)
1278 argbuffer
[current_arg
].arg_type
= at_filename
;
1279 argbuffer
[current_arg
].what
= argv
[optind
];
1284 argbuffer
[current_arg
].arg_type
= at_end
;
1287 print_help (argbuffer
);
1290 if (nincluded_files
== 0 && file_count
== 0)
1292 error ("no input files specified.", (char *)NULL
);
1293 suggest_asking_for_help ();
1297 if (tagfile
== NULL
)
1298 tagfile
= CTAGS
? "tags" : "TAGS";
1299 cwd
= etags_getcwd (); /* the current working directory */
1300 if (cwd
[strlen (cwd
) - 1] != '/')
1303 cwd
= concat (oldcwd
, "/", "");
1306 /* Relative file names are made relative to the current directory. */
1307 if (streq (tagfile
, "-")
1308 || strneq (tagfile
, "/dev/", 5))
1311 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1313 init (); /* set up boolean "functions" */
1315 linebuffer_init (&lb
);
1316 linebuffer_init (&filename_lb
);
1317 linebuffer_init (&filebuf
);
1318 linebuffer_init (&token_name
);
1322 if (streq (tagfile
, "-"))
1326 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1327 doesn't take effect until after `stdout' is already open). */
1328 if (!isatty (fileno (stdout
)))
1329 setmode (fileno (stdout
), O_BINARY
);
1333 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1339 * Loop through files finding functions.
1341 for (i
= 0; i
< current_arg
; i
++)
1343 static language
*lang
; /* non-NULL if language is forced */
1346 switch (argbuffer
[i
].arg_type
)
1349 lang
= argbuffer
[i
].lang
;
1352 analyse_regex (argbuffer
[i
].what
);
1356 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1360 error ("can't find file %s\n", this_file
);
1365 this_file
= massage_name (this_file
);
1368 this_file
= argbuffer
[i
].what
;
1370 /* Input file named "-" means read file names from stdin
1371 (one per line) and use them. */
1372 if (streq (this_file
, "-"))
1375 fatal ("cannot parse standard input AND read file names from it",
1377 while (readline_internal (&filename_lb
, stdin
) > 0)
1378 process_file_name (filename_lb
.buffer
, lang
);
1381 process_file_name (this_file
, lang
);
1387 this_file
= argbuffer
[i
].what
;
1388 process_file (stdin
, this_file
, lang
);
1395 free (filebuf
.buffer
);
1396 free (token_name
.buffer
);
1398 if (!CTAGS
|| cxref_style
)
1400 /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */
1401 put_entries (nodehead
);
1402 free_tree (nodehead
);
1408 /* Output file entries that have no tags. */
1409 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1411 fprintf (tagf
, "\f\n%s,0\n", fdp
->taggedfname
);
1413 while (nincluded_files
-- > 0)
1414 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1416 if (fclose (tagf
) == EOF
)
1420 exit (EXIT_SUCCESS
);
1426 for (i
= 0; i
< current_arg
; ++i
)
1428 switch (argbuffer
[i
].arg_type
)
1434 continue; /* the for loop */
1437 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1438 tagfile
, argbuffer
[i
].what
, tagfile
);
1439 if (system (cmd
) != EXIT_SUCCESS
)
1440 fatal ("failed to execute shell command", (char *)NULL
);
1442 append_to_tagfile
= TRUE
;
1445 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1448 put_entries (nodehead
); /* write all the tags (CTAGS) */
1449 free_tree (nodehead
);
1451 if (fclose (tagf
) == EOF
)
1455 if (append_to_tagfile
|| update
)
1457 char cmd
[2*BUFSIZ
+10];
1458 sprintf (cmd
, "sort -o %.*s %.*s", BUFSIZ
, tagfile
, BUFSIZ
, tagfile
);
1459 exit (system (cmd
));
1461 return EXIT_SUCCESS
;
1466 * Return a compressor given the file name. If EXTPTR is non-zero,
1467 * return a pointer into FILE where the compressor-specific
1468 * extension begins. If no compressor is found, NULL is returned
1469 * and EXTPTR is not significant.
1470 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1473 get_compressor_from_suffix (file
, extptr
)
1478 char *slash
, *suffix
;
1480 /* This relies on FN to be after canonicalize_filename,
1481 so we don't need to consider backslashes on DOS_NT. */
1482 slash
= etags_strrchr (file
, '/');
1483 suffix
= etags_strrchr (file
, '.');
1484 if (suffix
== NULL
|| suffix
< slash
)
1489 /* Let those poor souls who live with DOS 8+3 file name limits get
1490 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1491 Only the first do loop is run if not MSDOS */
1494 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1495 if (streq (compr
->suffix
, suffix
))
1498 break; /* do it only once: not really a loop */
1501 } while (*suffix
!= '\0');
1508 * Return a language given the name.
1511 get_language_from_langname (name
)
1517 error ("empty language name", (char *)NULL
);
1520 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1521 if (streq (name
, lang
->name
))
1523 error ("unknown language \"%s\"", name
);
1531 * Return a language given the interpreter name.
1534 get_language_from_interpreter (interpreter
)
1540 if (interpreter
== NULL
)
1542 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1543 if (lang
->interpreters
!= NULL
)
1544 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1545 if (streq (*iname
, interpreter
))
1554 * Return a language given the file name.
1557 get_language_from_filename (file
, case_sensitive
)
1559 bool case_sensitive
;
1562 char **name
, **ext
, *suffix
;
1564 /* Try whole file name first. */
1565 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1566 if (lang
->filenames
!= NULL
)
1567 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1568 if ((case_sensitive
)
1569 ? streq (*name
, file
)
1570 : strcaseeq (*name
, file
))
1573 /* If not found, try suffix after last dot. */
1574 suffix
= etags_strrchr (file
, '.');
1578 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1579 if (lang
->suffixes
!= NULL
)
1580 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1581 if ((case_sensitive
)
1582 ? streq (*ext
, suffix
)
1583 : strcaseeq (*ext
, suffix
))
1590 * This routine is called on each file argument.
1593 process_file_name (file
, lang
)
1597 struct stat stat_buf
;
1601 char *compressed_name
, *uncompressed_name
;
1602 char *ext
, *real_name
;
1605 canonicalize_filename (file
);
1606 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1608 error ("skipping inclusion of %s in self.", file
);
1611 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1613 compressed_name
= NULL
;
1614 real_name
= uncompressed_name
= savestr (file
);
1618 real_name
= compressed_name
= savestr (file
);
1619 uncompressed_name
= savenstr (file
, ext
- file
);
1622 /* If the canonicalized uncompressed name
1623 has already been dealt with, skip it silently. */
1624 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1626 assert (fdp
->infname
!= NULL
);
1627 if (streq (uncompressed_name
, fdp
->infname
))
1631 if (stat (real_name
, &stat_buf
) != 0)
1633 /* Reset real_name and try with a different name. */
1635 if (compressed_name
!= NULL
) /* try with the given suffix */
1637 if (stat (uncompressed_name
, &stat_buf
) == 0)
1638 real_name
= uncompressed_name
;
1640 else /* try all possible suffixes */
1642 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1644 compressed_name
= concat (file
, ".", compr
->suffix
);
1645 if (stat (compressed_name
, &stat_buf
) != 0)
1649 char *suf
= compressed_name
+ strlen (file
);
1650 size_t suflen
= strlen (compr
->suffix
) + 1;
1651 for ( ; suf
[1]; suf
++, suflen
--)
1653 memmove (suf
, suf
+ 1, suflen
);
1654 if (stat (compressed_name
, &stat_buf
) == 0)
1656 real_name
= compressed_name
;
1660 if (real_name
!= NULL
)
1663 free (compressed_name
);
1664 compressed_name
= NULL
;
1668 real_name
= compressed_name
;
1673 if (real_name
== NULL
)
1678 } /* try with a different name */
1680 if (!S_ISREG (stat_buf
.st_mode
))
1682 error ("skipping %s: it is not a regular file.", real_name
);
1685 if (real_name
== compressed_name
)
1687 char *cmd
= concat (compr
->command
, " ", real_name
);
1688 inf
= (FILE *) popen (cmd
, "r");
1692 inf
= fopen (real_name
, "r");
1699 process_file (inf
, uncompressed_name
, lang
);
1701 if (real_name
== compressed_name
)
1702 retval
= pclose (inf
);
1704 retval
= fclose (inf
);
1709 if (compressed_name
) free (compressed_name
);
1710 if (uncompressed_name
) free (uncompressed_name
);
1717 process_file (fh
, fn
, lang
)
1722 static const fdesc emptyfdesc
;
1725 /* Create a new input file description entry. */
1726 fdp
= xnew (1, fdesc
);
1729 fdp
->infname
= savestr (fn
);
1731 fdp
->infabsname
= absolute_filename (fn
, cwd
);
1732 fdp
->infabsdir
= absolute_dirname (fn
, cwd
);
1733 if (filename_is_absolute (fn
))
1735 /* An absolute file name. Canonicalize it. */
1736 fdp
->taggedfname
= absolute_filename (fn
, NULL
);
1740 /* A file name relative to cwd. Make it relative
1741 to the directory of the tags file. */
1742 fdp
->taggedfname
= relative_filename (fn
, tagfiledir
);
1744 fdp
->usecharno
= TRUE
; /* use char position when making tags */
1746 fdp
->written
= FALSE
; /* not written on tags file yet */
1749 curfdp
= fdhead
; /* the current file description */
1753 /* If not Ctags, and if this is not metasource and if it contained no #line
1754 directives, we can write the tags and free all nodes pointing to
1757 && curfdp
->usecharno
/* no #line directives in this file */
1758 && !curfdp
->lang
->metasource
)
1762 /* Look for the head of the sublist relative to this file. See add_node
1763 for the structure of the node tree. */
1765 for (np
= nodehead
; np
!= NULL
; prev
= np
, np
= np
->left
)
1766 if (np
->fdp
== curfdp
)
1769 /* If we generated tags for this file, write and delete them. */
1772 /* This is the head of the last sublist, if any. The following
1773 instructions depend on this being true. */
1774 assert (np
->left
== NULL
);
1776 assert (fdhead
== curfdp
);
1777 assert (last_node
->fdp
== curfdp
);
1778 put_entries (np
); /* write tags for file curfdp->taggedfname */
1779 free_tree (np
); /* remove the written nodes */
1781 nodehead
= NULL
; /* no nodes left */
1783 prev
->left
= NULL
; /* delete the pointer to the sublist */
1789 * This routine sets up the boolean pseudo-functions which work
1790 * by setting boolean flags dependent upon the corresponding character.
1791 * Every char which is NOT in that string is not a white char. Therefore,
1792 * all of the array "_wht" is set to FALSE, and then the elements
1793 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1794 * of a char is TRUE if it is the string "white", else FALSE.
1802 for (i
= 0; i
< CHARS
; i
++)
1803 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1804 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1805 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1806 notinname('\0') = notinname('\n');
1807 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1808 begtoken('\0') = begtoken('\n');
1809 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1810 intoken('\0') = intoken('\n');
1811 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1812 endtoken('\0') = endtoken('\n');
1816 * This routine opens the specified file and calls the function
1817 * which finds the function and type definitions.
1824 language
*lang
= curfdp
->lang
;
1825 Lang_function
*parser
= NULL
;
1827 /* If user specified a language, use it. */
1828 if (lang
!= NULL
&& lang
->function
!= NULL
)
1830 parser
= lang
->function
;
1833 /* Else try to guess the language given the file name. */
1836 lang
= get_language_from_filename (curfdp
->infname
, TRUE
);
1837 if (lang
!= NULL
&& lang
->function
!= NULL
)
1839 curfdp
->lang
= lang
;
1840 parser
= lang
->function
;
1844 /* Else look for sharp-bang as the first two characters. */
1846 && readline_internal (&lb
, inf
) > 0
1848 && lb
.buffer
[0] == '#'
1849 && lb
.buffer
[1] == '!')
1853 /* Set lp to point at the first char after the last slash in the
1854 line or, if no slashes, at the first nonblank. Then set cp to
1855 the first successive blank and terminate the string. */
1856 lp
= etags_strrchr (lb
.buffer
+2, '/');
1860 lp
= skip_spaces (lb
.buffer
+ 2);
1861 cp
= skip_non_spaces (lp
);
1864 if (strlen (lp
) > 0)
1866 lang
= get_language_from_interpreter (lp
);
1867 if (lang
!= NULL
&& lang
->function
!= NULL
)
1869 curfdp
->lang
= lang
;
1870 parser
= lang
->function
;
1875 /* We rewind here, even if inf may be a pipe. We fail if the
1876 length of the first line is longer than the pipe block size,
1877 which is unlikely. */
1880 /* Else try to guess the language given the case insensitive file name. */
1883 lang
= get_language_from_filename (curfdp
->infname
, FALSE
);
1884 if (lang
!= NULL
&& lang
->function
!= NULL
)
1886 curfdp
->lang
= lang
;
1887 parser
= lang
->function
;
1891 /* Else try Fortran or C. */
1894 node
*old_last_node
= last_node
;
1896 curfdp
->lang
= get_language_from_langname ("fortran");
1899 if (old_last_node
== last_node
)
1900 /* No Fortran entries found. Try C. */
1902 /* We do not tag if rewind fails.
1903 Only the file name will be recorded in the tags file. */
1905 curfdp
->lang
= get_language_from_langname (cplusplus
? "c++" : "c");
1911 if (!no_line_directive
1912 && curfdp
->lang
!= NULL
&& curfdp
->lang
->metasource
)
1913 /* It may be that this is a bingo.y file, and we already parsed a bingo.c
1914 file, or anyway we parsed a file that is automatically generated from
1915 this one. If this is the case, the bingo.c file contained #line
1916 directives that generated tags pointing to this file. Let's delete
1917 them all before parsing this file, which is the real source. */
1919 fdesc
**fdpp
= &fdhead
;
1920 while (*fdpp
!= NULL
)
1922 && streq ((*fdpp
)->taggedfname
, curfdp
->taggedfname
))
1923 /* We found one of those! We must delete both the file description
1924 and all tags referring to it. */
1926 fdesc
*badfdp
= *fdpp
;
1928 /* Delete the tags referring to badfdp->taggedfname
1929 that were obtained from badfdp->infname. */
1930 invalidate_nodes (badfdp
, &nodehead
);
1932 *fdpp
= badfdp
->next
; /* remove the bad description from the list */
1933 free_fdesc (badfdp
);
1936 fdpp
= &(*fdpp
)->next
; /* advance the list pointer */
1939 assert (parser
!= NULL
);
1941 /* Generic initialisations before reading from file. */
1942 linebuffer_setlen (&filebuf
, 0); /* reset the file buffer */
1944 /* Generic initialisations before parsing file with readline. */
1945 lineno
= 0; /* reset global line number */
1946 charno
= 0; /* reset global char number */
1947 linecharno
= 0; /* reset global char number of line start */
1951 regex_tag_multiline ();
1956 * Check whether an implicitly named tag should be created,
1957 * then call `pfnote'.
1958 * NAME is a string that is internally copied by this function.
1960 * TAGS format specification
1961 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1962 * The following is explained in some more detail in etc/ETAGS.EBNF.
1964 * make_tag creates tags with "implicit tag names" (unnamed tags)
1965 * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
1966 * 1. NAME does not contain any of the characters in NONAM;
1967 * 2. LINESTART contains name as either a rightmost, or rightmost but
1968 * one character, substring;
1969 * 3. the character, if any, immediately before NAME in LINESTART must
1970 * be a character in NONAM;
1971 * 4. the character, if any, immediately after NAME in LINESTART must
1972 * also be a character in NONAM.
1974 * The implementation uses the notinname() macro, which recognises the
1975 * characters stored in the string `nonam'.
1976 * etags.el needs to use the same characters that are in NONAM.
1979 make_tag (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1980 char *name
; /* tag name, or NULL if unnamed */
1981 int namelen
; /* tag length */
1982 bool is_func
; /* tag is a function */
1983 char *linestart
; /* start of the line where tag is */
1984 int linelen
; /* length of the line where tag is */
1985 int lno
; /* line number */
1986 long cno
; /* character number */
1988 bool named
= (name
!= NULL
&& namelen
> 0);
1990 if (!CTAGS
&& named
) /* maybe set named to false */
1991 /* Let's try to make an implicit tag name, that is, create an unnamed tag
1992 such that etags.el can guess a name from it. */
1995 register char *cp
= name
;
1997 for (i
= 0; i
< namelen
; i
++)
1998 if (notinname (*cp
++))
2000 if (i
== namelen
) /* rule #1 */
2002 cp
= linestart
+ linelen
- namelen
;
2003 if (notinname (linestart
[linelen
-1]))
2004 cp
-= 1; /* rule #4 */
2005 if (cp
>= linestart
/* rule #2 */
2007 || notinname (cp
[-1])) /* rule #3 */
2008 && strneq (name
, cp
, namelen
)) /* rule #2 */
2009 named
= FALSE
; /* use implicit tag name */
2014 name
= savenstr (name
, namelen
);
2017 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
2022 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
2023 char *name
; /* tag name, or NULL if unnamed */
2024 bool is_func
; /* tag is a function */
2025 char *linestart
; /* start of the line where tag is */
2026 int linelen
; /* length of the line where tag is */
2027 int lno
; /* line number */
2028 long cno
; /* character number */
2032 assert (name
== NULL
|| name
[0] != '\0');
2033 if (CTAGS
&& name
== NULL
)
2036 np
= xnew (1, node
);
2038 /* If ctags mode, change name "main" to M<thisfilename>. */
2039 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
2041 register char *fp
= etags_strrchr (curfdp
->taggedfname
, '/');
2042 np
->name
= concat ("M", fp
== NULL
? curfdp
->taggedfname
: fp
+ 1, "");
2043 fp
= etags_strrchr (np
->name
, '.');
2044 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
2050 np
->been_warned
= FALSE
;
2052 np
->is_func
= is_func
;
2054 if (np
->fdp
->usecharno
)
2055 /* Our char numbers are 0-base, because of C language tradition?
2056 ctags compatibility? old versions compatibility? I don't know.
2057 Anyway, since emacs's are 1-base we expect etags.el to take care
2058 of the difference. If we wanted to have 1-based numbers, we would
2059 uncomment the +1 below. */
2060 np
->cno
= cno
/* + 1 */ ;
2062 np
->cno
= invalidcharno
;
2063 np
->left
= np
->right
= NULL
;
2064 if (CTAGS
&& !cxref_style
)
2066 if (strlen (linestart
) < 50)
2067 np
->regex
= concat (linestart
, "$", "");
2069 np
->regex
= savenstr (linestart
, 50);
2072 np
->regex
= savenstr (linestart
, linelen
);
2074 add_node (np
, &nodehead
);
2079 * recurse on left children, iterate on right children.
2087 register node
*node_right
= np
->right
;
2088 free_tree (np
->left
);
2089 if (np
->name
!= NULL
)
2099 * delete a file description
2103 register fdesc
*fdp
;
2105 if (fdp
->infname
!= NULL
) free (fdp
->infname
);
2106 if (fdp
->infabsname
!= NULL
) free (fdp
->infabsname
);
2107 if (fdp
->infabsdir
!= NULL
) free (fdp
->infabsdir
);
2108 if (fdp
->taggedfname
!= NULL
) free (fdp
->taggedfname
);
2109 if (fdp
->prop
!= NULL
) free (fdp
->prop
);
2115 * Adds a node to the tree of nodes. In etags mode, sort by file
2116 * name. In ctags mode, sort by tag name. Make no attempt at
2119 * add_node is the only function allowed to add nodes, so it can
2123 add_node (np
, cur_node_p
)
2124 node
*np
, **cur_node_p
;
2127 register node
*cur_node
= *cur_node_p
;
2129 if (cur_node
== NULL
)
2139 /* For each file name, tags are in a linked sublist on the right
2140 pointer. The first tags of different files are a linked list
2141 on the left pointer. last_node points to the end of the last
2143 if (last_node
!= NULL
&& last_node
->fdp
== np
->fdp
)
2145 /* Let's use the same sublist as the last added node. */
2146 assert (last_node
->right
== NULL
);
2147 last_node
->right
= np
;
2150 else if (cur_node
->fdp
== np
->fdp
)
2152 /* Scanning the list we found the head of a sublist which is
2153 good for us. Let's scan this sublist. */
2154 add_node (np
, &cur_node
->right
);
2157 /* The head of this sublist is not good for us. Let's try the
2159 add_node (np
, &cur_node
->left
);
2160 } /* if ETAGS mode */
2165 dif
= strcmp (np
->name
, cur_node
->name
);
2168 * If this tag name matches an existing one, then
2169 * do not add the node, but maybe print a warning.
2173 if (np
->fdp
== cur_node
->fdp
)
2177 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
2178 np
->fdp
->infname
, lineno
, np
->name
);
2179 fprintf (stderr
, "Second entry ignored\n");
2182 else if (!cur_node
->been_warned
&& !no_warnings
)
2186 "Duplicate entry in files %s and %s: %s (Warning only)\n",
2187 np
->fdp
->infname
, cur_node
->fdp
->infname
, np
->name
);
2188 cur_node
->been_warned
= TRUE
;
2193 /* Actually add the node */
2194 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
2195 } /* if CTAGS mode */
2199 * invalidate_nodes ()
2200 * Scan the node tree and invalidate all nodes pointing to the
2201 * given file description (CTAGS case) or free them (ETAGS case).
2204 invalidate_nodes (badfdp
, npp
)
2215 if (np
->left
!= NULL
)
2216 invalidate_nodes (badfdp
, &np
->left
);
2217 if (np
->fdp
== badfdp
)
2219 if (np
->right
!= NULL
)
2220 invalidate_nodes (badfdp
, &np
->right
);
2224 assert (np
->fdp
!= NULL
);
2225 if (np
->fdp
== badfdp
)
2227 *npp
= np
->left
; /* detach the sublist from the list */
2228 np
->left
= NULL
; /* isolate it */
2229 free_tree (np
); /* free it */
2230 invalidate_nodes (badfdp
, npp
);
2233 invalidate_nodes (badfdp
, &np
->left
);
2238 static int total_size_of_entries
__P((node
*));
2239 static int number_len
__P((long));
2241 /* Length of a non-negative number's decimal representation. */
2247 while ((num
/= 10) > 0)
2253 * Return total number of characters that put_entries will output for
2254 * the nodes in the linked list at the right of the specified node.
2255 * This count is irrelevant with etags.el since emacs 19.34 at least,
2256 * but is still supplied for backward compatibility.
2259 total_size_of_entries (np
)
2262 register int total
= 0;
2264 for (; np
!= NULL
; np
= np
->right
)
2267 total
+= strlen (np
->regex
) + 1; /* pat\177 */
2268 if (np
->name
!= NULL
)
2269 total
+= strlen (np
->name
) + 1; /* name\001 */
2270 total
+= number_len ((long) np
->lno
) + 1; /* lno, */
2271 if (np
->cno
!= invalidcharno
) /* cno */
2272 total
+= number_len (np
->cno
);
2273 total
+= 1; /* newline */
2284 static fdesc
*fdp
= NULL
;
2289 /* Output subentries that precede this one */
2291 put_entries (np
->left
);
2293 /* Output this entry */
2302 fprintf (tagf
, "\f\n%s,%d\n",
2303 fdp
->taggedfname
, total_size_of_entries (np
));
2304 fdp
->written
= TRUE
;
2306 fputs (np
->regex
, tagf
);
2307 fputc ('\177', tagf
);
2308 if (np
->name
!= NULL
)
2310 fputs (np
->name
, tagf
);
2311 fputc ('\001', tagf
);
2313 fprintf (tagf
, "%d,", np
->lno
);
2314 if (np
->cno
!= invalidcharno
)
2315 fprintf (tagf
, "%ld", np
->cno
);
2321 if (np
->name
== NULL
)
2322 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
2327 fprintf (stdout
, "%s %s %d\n",
2328 np
->name
, np
->fdp
->taggedfname
, (np
->lno
+ 63) / 64);
2330 fprintf (stdout
, "%-16s %3d %-16s %s\n",
2331 np
->name
, np
->lno
, np
->fdp
->taggedfname
, np
->regex
);
2335 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->fdp
->taggedfname
);
2338 { /* function or #define macro with args */
2339 putc (searchar
, tagf
);
2342 for (sp
= np
->regex
; *sp
; sp
++)
2344 if (*sp
== '\\' || *sp
== searchar
)
2348 putc (searchar
, tagf
);
2351 { /* anything else; text pattern inadequate */
2352 fprintf (tagf
, "%d", np
->lno
);
2357 } /* if this node contains a valid tag */
2359 /* Output subentries that follow this one */
2360 put_entries (np
->right
);
2362 put_entries (np
->left
);
2367 #define C_EXT 0x00fff /* C extensions */
2368 #define C_PLAIN 0x00000 /* C */
2369 #define C_PLPL 0x00001 /* C++ */
2370 #define C_STAR 0x00003 /* C* */
2371 #define C_JAVA 0x00005 /* JAVA */
2372 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
2373 #define YACC 0x10000 /* yacc file */
2376 * The C symbol tables.
2381 st_C_objprot
, st_C_objimpl
, st_C_objend
,
2383 st_C_ignore
, st_C_attribute
,
2386 st_C_class
, st_C_template
,
2387 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
2390 static unsigned int hash
__P((const char *, unsigned int));
2391 static struct C_stab_entry
* in_word_set
__P((const char *, unsigned int));
2392 static enum sym_type C_symtype
__P((char *, int, int));
2394 /* Feed stuff between (but not including) %[ and %] lines to:
2400 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2404 while, 0, st_C_ignore
2405 switch, 0, st_C_ignore
2406 return, 0, st_C_ignore
2407 __attribute__, 0, st_C_attribute
2408 @interface, 0, st_C_objprot
2409 @protocol, 0, st_C_objprot
2410 @implementation,0, st_C_objimpl
2411 @end, 0, st_C_objend
2412 import, (C_JAVA & !C_PLPL), st_C_ignore
2413 package, (C_JAVA & !C_PLPL), st_C_ignore
2414 friend, C_PLPL, st_C_ignore
2415 extends, (C_JAVA & !C_PLPL), st_C_javastruct
2416 implements, (C_JAVA & !C_PLPL), st_C_javastruct
2417 interface, (C_JAVA & !C_PLPL), st_C_struct
2418 class, 0, st_C_class
2419 namespace, C_PLPL, st_C_struct
2420 domain, C_STAR, st_C_struct
2421 union, 0, st_C_struct
2422 struct, 0, st_C_struct
2423 extern, 0, st_C_extern
2425 typedef, 0, st_C_typedef
2426 define, 0, st_C_define
2427 undef, 0, st_C_define
2428 operator, C_PLPL, st_C_operator
2429 template, 0, st_C_template
2430 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2431 DEFUN, 0, st_C_gnumacro
2432 SYSCALL, 0, st_C_gnumacro
2433 ENTRY, 0, st_C_gnumacro
2434 PSEUDO, 0, st_C_gnumacro
2435 # These are defined inside C functions, so currently they are not met.
2436 # EXFUN used in glibc, DEFVAR_* in emacs.
2437 #EXFUN, 0, st_C_gnumacro
2438 #DEFVAR_, 0, st_C_gnumacro
2440 and replace lines between %< and %> with its output, then:
2441 - remove the #if characterset check
2442 - make in_word_set static and not inline. */
2444 /* C code produced by gperf version 3.0.1 */
2445 /* Command-line: gperf -m 5 */
2446 /* Computed positions: -k'2-3' */
2448 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
2449 /* maximum key range = 33, duplicates = 0 */
2460 register const char *str
;
2461 register unsigned int len
;
2463 static unsigned char asso_values
[] =
2465 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2466 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2467 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2468 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2469 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2470 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2471 35, 35, 35, 35, 35, 35, 35, 35, 35, 15,
2472 14, 35, 35, 35, 35, 35, 35, 35, 14, 35,
2473 35, 35, 35, 12, 13, 35, 35, 35, 35, 12,
2474 35, 35, 35, 35, 35, 1, 35, 16, 35, 6,
2475 23, 0, 0, 35, 22, 0, 35, 35, 5, 0,
2476 0, 15, 1, 35, 6, 35, 8, 19, 35, 16,
2477 4, 5, 35, 35, 35, 35, 35, 35, 35, 35,
2478 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2479 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2480 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2481 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2482 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2483 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2484 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2485 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2486 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2487 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2488 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2489 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2490 35, 35, 35, 35, 35, 35
2492 register int hval
= len
;
2497 hval
+= asso_values
[(unsigned char)str
[2]];
2500 hval
+= asso_values
[(unsigned char)str
[1]];
2506 static struct C_stab_entry
*
2507 in_word_set (str
, len
)
2508 register const char *str
;
2509 register unsigned int len
;
2513 TOTAL_KEYWORDS
= 32,
2514 MIN_WORD_LENGTH
= 2,
2515 MAX_WORD_LENGTH
= 15,
2520 static struct C_stab_entry wordlist
[] =
2523 {"if", 0, st_C_ignore
},
2525 {"@end", 0, st_C_objend
},
2526 {"union", 0, st_C_struct
},
2527 {"define", 0, st_C_define
},
2528 {"import", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2529 {"template", 0, st_C_template
},
2530 {"operator", C_PLPL
, st_C_operator
},
2531 {"@interface", 0, st_C_objprot
},
2532 {"implements", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2533 {"friend", C_PLPL
, st_C_ignore
},
2534 {"typedef", 0, st_C_typedef
},
2535 {"return", 0, st_C_ignore
},
2536 {"@implementation",0, st_C_objimpl
},
2537 {"@protocol", 0, st_C_objprot
},
2538 {"interface", (C_JAVA
& !C_PLPL
), st_C_struct
},
2539 {"extern", 0, st_C_extern
},
2540 {"extends", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2541 {"struct", 0, st_C_struct
},
2542 {"domain", C_STAR
, st_C_struct
},
2543 {"switch", 0, st_C_ignore
},
2544 {"enum", 0, st_C_enum
},
2545 {"for", 0, st_C_ignore
},
2546 {"namespace", C_PLPL
, st_C_struct
},
2547 {"class", 0, st_C_class
},
2548 {"while", 0, st_C_ignore
},
2549 {"undef", 0, st_C_define
},
2550 {"package", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2551 {"__attribute__", 0, st_C_attribute
},
2552 {"SYSCALL", 0, st_C_gnumacro
},
2553 {"ENTRY", 0, st_C_gnumacro
},
2554 {"PSEUDO", 0, st_C_gnumacro
},
2555 {"DEFUN", 0, st_C_gnumacro
}
2558 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2560 register int key
= hash (str
, len
);
2562 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2564 register const char *s
= wordlist
[key
].name
;
2566 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1) && s
[len
] == '\0')
2567 return &wordlist
[key
];
2574 static enum sym_type
2575 C_symtype (str
, len
, c_ext
)
2580 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2582 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2589 * Ignoring __attribute__ ((list))
2591 static bool inattribute
; /* looking at an __attribute__ construct */
2594 * C functions and variables are recognized using a simple
2595 * finite automaton. fvdef is its state variable.
2599 fvnone
, /* nothing seen */
2600 fdefunkey
, /* Emacs DEFUN keyword seen */
2601 fdefunname
, /* Emacs DEFUN name seen */
2602 foperator
, /* func: operator keyword seen (cplpl) */
2603 fvnameseen
, /* function or variable name seen */
2604 fstartlist
, /* func: just after open parenthesis */
2605 finlist
, /* func: in parameter list */
2606 flistseen
, /* func: after parameter list */
2607 fignore
, /* func: before open brace */
2608 vignore
/* var-like: ignore until ';' */
2611 static bool fvextern
; /* func or var: extern keyword seen; */
2614 * typedefs are recognized using a simple finite automaton.
2615 * typdef is its state variable.
2619 tnone
, /* nothing seen */
2620 tkeyseen
, /* typedef keyword seen */
2621 ttypeseen
, /* defined type seen */
2622 tinbody
, /* inside typedef body */
2623 tend
, /* just before typedef tag */
2624 tignore
/* junk after typedef tag */
2628 * struct-like structures (enum, struct and union) are recognized
2629 * using another simple finite automaton. `structdef' is its state
2634 snone
, /* nothing seen yet,
2635 or in struct body if bracelev > 0 */
2636 skeyseen
, /* struct-like keyword seen */
2637 stagseen
, /* struct-like tag seen */
2638 scolonseen
/* colon seen after struct-like tag */
2642 * When objdef is different from onone, objtag is the name of the class.
2644 static char *objtag
= "<uninited>";
2647 * Yet another little state machine to deal with preprocessor lines.
2651 dnone
, /* nothing seen */
2652 dsharpseen
, /* '#' seen as first char on line */
2653 ddefineseen
, /* '#' and 'define' seen */
2654 dignorerest
/* ignore rest of line */
2658 * State machine for Objective C protocols and implementations.
2659 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2663 onone
, /* nothing seen */
2664 oprotocol
, /* @interface or @protocol seen */
2665 oimplementation
, /* @implementations seen */
2666 otagseen
, /* class name seen */
2667 oparenseen
, /* parenthesis before category seen */
2668 ocatseen
, /* category name seen */
2669 oinbody
, /* in @implementation body */
2670 omethodsign
, /* in @implementation body, after +/- */
2671 omethodtag
, /* after method name */
2672 omethodcolon
, /* after method colon */
2673 omethodparm
, /* after method parameter */
2674 oignore
/* wait for @end */
2679 * Use this structure to keep info about the token read, and how it
2680 * should be tagged. Used by the make_C_tag function to build a tag.
2684 char *line
; /* string containing the token */
2685 int offset
; /* where the token starts in LINE */
2686 int length
; /* token length */
2688 The previous members can be used to pass strings around for generic
2689 purposes. The following ones specifically refer to creating tags. In this
2690 case the token contained here is the pattern that will be used to create a
2693 bool valid
; /* do not create a tag; the token should be
2694 invalidated whenever a state machine is
2695 reset prematurely */
2696 bool named
; /* create a named tag */
2697 int lineno
; /* source line number of tag */
2698 long linepos
; /* source char number of tag */
2699 } token
; /* latest token read */
2702 * Variables and functions for dealing with nested structures.
2703 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2705 static void pushclass_above
__P((int, char *, int));
2706 static void popclass_above
__P((int));
2707 static void write_classname
__P((linebuffer
*, char *qualifier
));
2710 char **cname
; /* nested class names */
2711 int *bracelev
; /* nested class brace level */
2712 int nl
; /* class nesting level (elements used) */
2713 int size
; /* length of the array */
2714 } cstack
; /* stack for nested declaration tags */
2715 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2716 #define nestlev (cstack.nl)
2717 /* After struct keyword or in struct body, not inside a nested function. */
2718 #define instruct (structdef == snone && nestlev > 0 \
2719 && bracelev == cstack.bracelev[nestlev-1] + 1)
2722 pushclass_above (bracelev
, str
, len
)
2729 popclass_above (bracelev
);
2731 if (nl
>= cstack
.size
)
2733 int size
= cstack
.size
*= 2;
2734 xrnew (cstack
.cname
, size
, char *);
2735 xrnew (cstack
.bracelev
, size
, int);
2737 assert (nl
== 0 || cstack
.bracelev
[nl
-1] < bracelev
);
2738 cstack
.cname
[nl
] = (str
== NULL
) ? NULL
: savenstr (str
, len
);
2739 cstack
.bracelev
[nl
] = bracelev
;
2744 popclass_above (bracelev
)
2749 for (nl
= cstack
.nl
- 1;
2750 nl
>= 0 && cstack
.bracelev
[nl
] >= bracelev
;
2753 if (cstack
.cname
[nl
] != NULL
)
2754 free (cstack
.cname
[nl
]);
2760 write_classname (cn
, qualifier
)
2765 int qlen
= strlen (qualifier
);
2767 if (cstack
.nl
== 0 || cstack
.cname
[0] == NULL
)
2771 cn
->buffer
[0] = '\0';
2775 len
= strlen (cstack
.cname
[0]);
2776 linebuffer_setlen (cn
, len
);
2777 strcpy (cn
->buffer
, cstack
.cname
[0]);
2779 for (i
= 1; i
< cstack
.nl
; i
++)
2784 s
= cstack
.cname
[i
];
2789 linebuffer_setlen (cn
, len
);
2790 strncat (cn
->buffer
, qualifier
, qlen
);
2791 strncat (cn
->buffer
, s
, slen
);
2796 static bool consider_token
__P((char *, int, int, int *, int, int, bool *));
2797 static void make_C_tag
__P((bool));
2801 * checks to see if the current token is at the start of a
2802 * function or variable, or corresponds to a typedef, or
2803 * is a struct/union/enum tag, or #define, or an enum constant.
2805 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2806 * with args. C_EXTP points to which language we are looking at.
2817 consider_token (str
, len
, c
, c_extp
, bracelev
, parlev
, is_func_or_var
)
2818 register char *str
; /* IN: token pointer */
2819 register int len
; /* IN: token length */
2820 register int c
; /* IN: first char after the token */
2821 int *c_extp
; /* IN, OUT: C extensions mask */
2822 int bracelev
; /* IN: brace level */
2823 int parlev
; /* IN: parenthesis level */
2824 bool *is_func_or_var
; /* OUT: function or variable found */
2826 /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
2827 structtype is the type of the preceding struct-like keyword, and
2828 structbracelev is the brace level where it has been seen. */
2829 static enum sym_type structtype
;
2830 static int structbracelev
;
2831 static enum sym_type toktype
;
2834 toktype
= C_symtype (str
, len
, *c_extp
);
2837 * Skip __attribute__
2839 if (toktype
== st_C_attribute
)
2846 * Advance the definedef state machine.
2851 /* We're not on a preprocessor line. */
2852 if (toktype
== st_C_gnumacro
)
2859 if (toktype
== st_C_define
)
2861 definedef
= ddefineseen
;
2865 definedef
= dignorerest
;
2870 * Make a tag for any macro, unless it is a constant
2871 * and constantypedefs is FALSE.
2873 definedef
= dignorerest
;
2874 *is_func_or_var
= (c
== '(');
2875 if (!*is_func_or_var
&& !constantypedefs
)
2882 error ("internal error: definedef value.", (char *)NULL
);
2891 if (toktype
== st_C_typedef
)
2911 if (structdef
== snone
&& fvdef
== fvnone
)
2929 * This structdef business is NOT invoked when we are ctags and the
2930 * file is plain C. This is because a struct tag may have the same
2931 * name as another tag, and this loses with ctags.
2935 case st_C_javastruct
:
2936 if (structdef
== stagseen
)
2937 structdef
= scolonseen
;
2941 if ((*c_extp
& C_AUTO
) /* automatic detection of C++ language */
2943 && definedef
== dnone
&& structdef
== snone
2944 && typdef
== tnone
&& fvdef
== fvnone
)
2945 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2946 if (toktype
== st_C_template
)
2953 && (typdef
== tkeyseen
2954 || (typedefs_or_cplusplus
&& structdef
== snone
)))
2956 structdef
= skeyseen
;
2957 structtype
= toktype
;
2958 structbracelev
= bracelev
;
2959 if (fvdef
== fvnameseen
)
2965 if (structdef
== skeyseen
)
2967 structdef
= stagseen
;
2971 if (typdef
!= tnone
)
2974 /* Detect Objective C constructs. */
2984 objdef
= oimplementation
;
2988 case oimplementation
:
2989 /* Save the class tag for functions or variables defined inside. */
2990 objtag
= savenstr (str
, len
);
2994 /* Save the class tag for categories. */
2995 objtag
= savenstr (str
, len
);
2997 *is_func_or_var
= TRUE
;
3001 *is_func_or_var
= TRUE
;
3009 objdef
= omethodtag
;
3010 linebuffer_setlen (&token_name
, len
);
3011 strncpy (token_name
.buffer
, str
, len
);
3012 token_name
.buffer
[len
] = '\0';
3018 objdef
= omethodparm
;
3024 objdef
= omethodtag
;
3025 linebuffer_setlen (&token_name
, token_name
.len
+ len
);
3026 strncat (token_name
.buffer
, str
, len
);
3031 if (toktype
== st_C_objend
)
3033 /* Memory leakage here: the string pointed by objtag is
3034 never released, because many tests would be needed to
3035 avoid breaking on incorrect input code. The amount of
3036 memory leaked here is the sum of the lengths of the
3044 /* A function, variable or enum constant? */
3066 *is_func_or_var
= TRUE
;
3070 && structdef
== snone
3071 && structtype
== st_C_enum
&& bracelev
> structbracelev
)
3072 return TRUE
; /* enum constant */
3078 fvdef
= fdefunname
; /* GNU macro */
3079 *is_func_or_var
= TRUE
;
3087 if ((strneq (str
, "asm", 3) && endtoken (str
[3]))
3088 || (strneq (str
, "__asm__", 7) && endtoken (str
[7])))
3097 if (len
>= 10 && strneq (str
+len
-10, "::operator", 10))
3099 if (*c_extp
& C_AUTO
) /* automatic detection of C++ */
3100 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
3102 *is_func_or_var
= TRUE
;
3105 if (bracelev
> 0 && !instruct
)
3107 fvdef
= fvnameseen
; /* function or variable */
3108 *is_func_or_var
= TRUE
;
3119 * C_entries often keeps pointers to tokens or lines which are older than
3120 * the line currently read. By keeping two line buffers, and switching
3121 * them at end of line, it is possible to use those pointers.
3129 #define current_lb_is_new (newndx == curndx)
3130 #define switch_line_buffers() (curndx = 1 - curndx)
3132 #define curlb (lbs[curndx].lb)
3133 #define newlb (lbs[newndx].lb)
3134 #define curlinepos (lbs[curndx].linepos)
3135 #define newlinepos (lbs[newndx].linepos)
3137 #define plainc ((c_ext & C_EXT) == C_PLAIN)
3138 #define cplpl (c_ext & C_PLPL)
3139 #define cjava ((c_ext & C_JAVA) == C_JAVA)
3141 #define CNL_SAVE_DEFINEDEF() \
3143 curlinepos = charno; \
3144 readline (&curlb, inf); \
3145 lp = curlb.buffer; \
3152 CNL_SAVE_DEFINEDEF(); \
3153 if (savetoken.valid) \
3155 token = savetoken; \
3156 savetoken.valid = FALSE; \
3158 definedef = dnone; \
3166 /* This function is never called when token.valid is FALSE, but
3167 we must protect against invalid input or internal errors. */
3168 if (!DEBUG
&& !token
.valid
)
3172 make_tag (token_name
.buffer
, token_name
.len
, isfun
, token
.line
,
3173 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3174 else /* this case is optimised away if !DEBUG */
3175 make_tag (concat ("INVALID TOKEN:-->", token_name
.buffer
, ""),
3176 token_name
.len
+ 17, isfun
, token
.line
,
3177 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3179 token
.valid
= FALSE
;
3185 * This routine finds functions, variables, typedefs,
3186 * #define's, enum constants and struct/union/enum definitions in
3187 * C syntax and adds them to the list.
3190 C_entries (c_ext
, inf
)
3191 int c_ext
; /* extension of C */
3192 FILE *inf
; /* input file */
3194 register char c
; /* latest char read; '\0' for end of line */
3195 register char *lp
; /* pointer one beyond the character `c' */
3196 int curndx
, newndx
; /* indices for current and new lb */
3197 register int tokoff
; /* offset in line of start of current token */
3198 register int toklen
; /* length of current token */
3199 char *qualifier
; /* string used to qualify names */
3200 int qlen
; /* length of qualifier */
3201 int bracelev
; /* current brace level */
3202 int bracketlev
; /* current bracket level */
3203 int parlev
; /* current parenthesis level */
3204 int attrparlev
; /* __attribute__ parenthesis level */
3205 int templatelev
; /* current template level */
3206 int typdefbracelev
; /* bracelev where a typedef struct body begun */
3207 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
3208 bool yacc_rules
; /* in the rules part of a yacc file */
3209 struct tok savetoken
= {0}; /* token saved during preprocessor handling */
3212 linebuffer_init (&lbs
[0].lb
);
3213 linebuffer_init (&lbs
[1].lb
);
3214 if (cstack
.size
== 0)
3216 cstack
.size
= (DEBUG
) ? 1 : 4;
3218 cstack
.cname
= xnew (cstack
.size
, char *);
3219 cstack
.bracelev
= xnew (cstack
.size
, int);
3222 tokoff
= toklen
= typdefbracelev
= 0; /* keep compiler quiet */
3223 curndx
= newndx
= 0;
3227 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
3228 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
3230 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3231 token
.valid
= savetoken
.valid
= FALSE
;
3232 bracelev
= bracketlev
= parlev
= attrparlev
= templatelev
= 0;
3234 { qualifier
= "."; qlen
= 1; }
3236 { qualifier
= "::"; qlen
= 2; }
3244 /* If we are at the end of the line, the next character is a
3245 '\0'; do not skip it, because it is what tells us
3246 to read the next line. */
3267 /* Newlines inside comments do not end macro definitions in
3269 CNL_SAVE_DEFINEDEF ();
3282 /* Newlines inside strings do not end macro definitions
3283 in traditional cpp, even though compilers don't
3284 usually accept them. */
3285 CNL_SAVE_DEFINEDEF ();
3295 /* Hmmm, something went wrong. */
3304 else if (bracketlev
> 0)
3309 if (--bracketlev
> 0)
3313 CNL_SAVE_DEFINEDEF ();
3341 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
3354 else if (/* cplpl && */ *lp
== '/')
3362 if ((c_ext
& YACC
) && *lp
== '%')
3364 /* Entering or exiting rules section in yacc file. */
3366 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
3367 typdef
= tnone
; structdef
= snone
;
3368 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3370 yacc_rules
= !yacc_rules
;
3376 if (definedef
== dnone
)
3379 bool cpptoken
= TRUE
;
3381 /* Look back on this line. If all blanks, or nonblanks
3382 followed by an end of comment, this is a preprocessor
3384 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
3387 if (*cp
== '*' && *(cp
+1) == '/')
3396 definedef
= dsharpseen
;
3397 } /* if (definedef == dnone) */
3405 /* Consider token only if some involved conditions are satisfied. */
3406 if (typdef
!= tignore
3407 && definedef
!= dignorerest
3410 && (definedef
!= dnone
3411 || structdef
!= scolonseen
)
3418 if (c
== ':' && *lp
== ':' && begtoken (lp
[1]))
3419 /* This handles :: in the middle,
3420 but not at the beginning of an identifier.
3421 Also, space-separated :: is not recognised. */
3423 if (c_ext
& C_AUTO
) /* automatic detection of C++ */
3424 c_ext
= (c_ext
| C_PLPL
) & ~C_AUTO
;
3428 goto still_in_token
;
3432 bool funorvar
= FALSE
;
3435 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
3436 &c_ext
, bracelev
, parlev
,
3439 if (fvdef
== foperator
)
3442 lp
= skip_spaces (lp
-1);
3446 && !iswhite (*lp
) && *lp
!= '(')
3449 toklen
+= lp
- oldlp
;
3451 token
.named
= FALSE
;
3453 && nestlev
> 0 && definedef
== dnone
)
3454 /* in struct body */
3456 write_classname (&token_name
, qualifier
);
3457 linebuffer_setlen (&token_name
,
3458 token_name
.len
+qlen
+toklen
);
3459 strcat (token_name
.buffer
, qualifier
);
3460 strncat (token_name
.buffer
,
3461 newlb
.buffer
+ tokoff
, toklen
);
3464 else if (objdef
== ocatseen
)
3465 /* Objective C category */
3467 int len
= strlen (objtag
) + 2 + toklen
;
3468 linebuffer_setlen (&token_name
, len
);
3469 strcpy (token_name
.buffer
, objtag
);
3470 strcat (token_name
.buffer
, "(");
3471 strncat (token_name
.buffer
,
3472 newlb
.buffer
+ tokoff
, toklen
);
3473 strcat (token_name
.buffer
, ")");
3476 else if (objdef
== omethodtag
3477 || objdef
== omethodparm
)
3478 /* Objective C method */
3482 else if (fvdef
== fdefunname
)
3483 /* GNU DEFUN and similar macros */
3485 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
3489 /* Rewrite the tag so that emacs lisp DEFUNs
3490 can be found by their elisp name */
3496 linebuffer_setlen (&token_name
, len
);
3497 strncpy (token_name
.buffer
,
3498 newlb
.buffer
+ off
, len
);
3499 token_name
.buffer
[len
] = '\0';
3502 if (token_name
.buffer
[len
] == '_')
3503 token_name
.buffer
[len
] = '-';
3504 token
.named
= defun
;
3508 linebuffer_setlen (&token_name
, toklen
);
3509 strncpy (token_name
.buffer
,
3510 newlb
.buffer
+ tokoff
, toklen
);
3511 token_name
.buffer
[toklen
] = '\0';
3512 /* Name macros and members. */
3513 token
.named
= (structdef
== stagseen
3514 || typdef
== ttypeseen
3517 && definedef
== dignorerest
)
3519 && definedef
== dnone
3520 && structdef
== snone
3523 token
.lineno
= lineno
;
3524 token
.offset
= tokoff
;
3525 token
.length
= toklen
;
3526 token
.line
= newlb
.buffer
;
3527 token
.linepos
= newlinepos
;
3530 if (definedef
== dnone
3531 && (fvdef
== fvnameseen
3532 || fvdef
== foperator
3533 || structdef
== stagseen
3535 || typdef
== ttypeseen
3536 || objdef
!= onone
))
3538 if (current_lb_is_new
)
3539 switch_line_buffers ();
3541 else if (definedef
!= dnone
3542 || fvdef
== fdefunname
3544 make_C_tag (funorvar
);
3546 else /* not yacc and consider_token failed */
3548 if (inattribute
&& fvdef
== fignore
)
3550 /* We have just met __attribute__ after a
3551 function parameter list: do not tag the
3558 } /* if (endtoken (c)) */
3559 else if (intoken (c
))
3565 } /* if (midtoken) */
3566 else if (begtoken (c
))
3574 /* This prevents tagging fb in
3575 void (__attribute__((noreturn)) *fb) (void);
3576 Fixing this is not easy and not very important. */
3580 if (plainc
|| declarations
)
3582 make_C_tag (TRUE
); /* a function */
3587 if (structdef
== stagseen
&& !cjava
)
3589 popclass_above (bracelev
);
3597 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
3599 tokoff
= lp
- 1 - newlb
.buffer
;
3604 } /* if (begtoken) */
3605 } /* if must look at token */
3608 /* Detect end of line, colon, comma, semicolon and various braces
3609 after having handled a token.*/
3615 if (yacc_rules
&& token
.offset
== 0 && token
.valid
)
3617 make_C_tag (FALSE
); /* a yacc function */
3620 if (definedef
!= dnone
)
3626 make_C_tag (TRUE
); /* an Objective C class */
3630 objdef
= omethodcolon
;
3631 linebuffer_setlen (&token_name
, token_name
.len
+ 1);
3632 strcat (token_name
.buffer
, ":");
3635 if (structdef
== stagseen
)
3637 structdef
= scolonseen
;
3640 /* Should be useless, but may be work as a safety net. */
3641 if (cplpl
&& fvdef
== flistseen
)
3643 make_C_tag (TRUE
); /* a function */
3649 if (definedef
!= dnone
|| inattribute
)
3655 make_C_tag (FALSE
); /* a typedef */
3665 if (typdef
== tignore
|| cplpl
)
3669 if ((globals
&& bracelev
== 0 && (!fvextern
|| declarations
))
3670 || (members
&& instruct
))
3671 make_C_tag (FALSE
); /* a variable */
3674 token
.valid
= FALSE
;
3678 && (cplpl
|| !instruct
)
3679 && (typdef
== tnone
|| (typdef
!= tignore
&& instruct
)))
3681 && plainc
&& instruct
))
3682 make_C_tag (TRUE
); /* a function */
3688 && cplpl
&& structdef
== stagseen
)
3689 make_C_tag (FALSE
); /* forward declaration */
3691 token
.valid
= FALSE
;
3692 } /* switch (fvdef) */
3698 if (structdef
== stagseen
)
3702 if (definedef
!= dnone
|| inattribute
)
3708 make_C_tag (TRUE
); /* an Objective C method */
3729 && (!fvextern
|| declarations
))
3730 || (members
&& instruct
)))
3731 make_C_tag (FALSE
); /* a variable */
3734 if ((declarations
&& typdef
== tnone
&& !instruct
)
3735 || (members
&& typdef
!= tignore
&& instruct
))
3737 make_C_tag (TRUE
); /* a function */
3740 else if (!declarations
)
3742 token
.valid
= FALSE
;
3747 if (structdef
== stagseen
)
3751 if (definedef
!= dnone
|| inattribute
)
3753 if (structdef
== stagseen
)
3760 make_C_tag (FALSE
); /* a typedef */
3772 if ((members
&& bracelev
== 1)
3773 || (globals
&& bracelev
== 0
3774 && (!fvextern
|| declarations
)))
3775 make_C_tag (FALSE
); /* a variable */
3789 if (definedef
!= dnone
)
3791 if (objdef
== otagseen
&& parlev
== 0)
3792 objdef
= oparenseen
;
3796 if (typdef
== ttypeseen
3800 /* This handles constructs like:
3801 typedef void OperatorFun (int fun); */
3820 if (--attrparlev
== 0)
3821 inattribute
= FALSE
;
3824 if (definedef
!= dnone
)
3826 if (objdef
== ocatseen
&& parlev
== 1)
3828 make_C_tag (TRUE
); /* an Objective C category */
3842 || typdef
== ttypeseen
))
3845 make_C_tag (FALSE
); /* a typedef */
3848 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3852 if (definedef
!= dnone
)
3854 if (typdef
== ttypeseen
)
3856 /* Whenever typdef is set to tinbody (currently only
3857 here), typdefbracelev should be set to bracelev. */
3859 typdefbracelev
= bracelev
;
3864 make_C_tag (TRUE
); /* a function */
3873 make_C_tag (TRUE
); /* an Objective C class */
3878 make_C_tag (TRUE
); /* an Objective C method */
3882 /* Neutralize `extern "C" {' grot. */
3883 if (bracelev
== 0 && structdef
== snone
&& nestlev
== 0
3891 case skeyseen
: /* unnamed struct */
3892 pushclass_above (bracelev
, NULL
, 0);
3895 case stagseen
: /* named struct or enum */
3896 case scolonseen
: /* a class */
3897 pushclass_above (bracelev
,token
.line
+token
.offset
, token
.length
);
3899 make_C_tag (FALSE
); /* a struct or enum */
3905 if (definedef
!= dnone
)
3907 if (fvdef
== fstartlist
)
3909 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3910 token
.valid
= FALSE
;
3914 if (definedef
!= dnone
)
3916 if (!ignoreindent
&& lp
== newlb
.buffer
+ 1)
3919 token
.valid
= FALSE
;
3920 bracelev
= 0; /* reset brace level if first column */
3921 parlev
= 0; /* also reset paren level, just in case... */
3923 else if (bracelev
> 0)
3926 token
.valid
= FALSE
; /* something gone amiss, token unreliable */
3927 popclass_above (bracelev
);
3929 /* Only if typdef == tinbody is typdefbracelev significant. */
3930 if (typdef
== tinbody
&& bracelev
<= typdefbracelev
)
3932 assert (bracelev
== typdefbracelev
);
3937 if (definedef
!= dnone
)
3947 if ((members
&& bracelev
== 1)
3948 || (globals
&& bracelev
== 0 && (!fvextern
|| declarations
)))
3949 make_C_tag (FALSE
); /* a variable */
3957 && (structdef
== stagseen
|| fvdef
== fvnameseen
))
3964 if (templatelev
> 0)
3972 if (objdef
== oinbody
&& bracelev
== 0)
3974 objdef
= omethodsign
;
3979 case '#': case '~': case '&': case '%': case '/':
3980 case '|': case '^': case '!': case '.': case '?':
3981 if (definedef
!= dnone
)
3983 /* These surely cannot follow a function tag in C. */
3996 if (objdef
== otagseen
)
3998 make_C_tag (TRUE
); /* an Objective C class */
4001 /* If a macro spans multiple lines don't reset its state. */
4003 CNL_SAVE_DEFINEDEF ();
4009 } /* while not eof */
4011 free (lbs
[0].lb
.buffer
);
4012 free (lbs
[1].lb
.buffer
);
4016 * Process either a C++ file or a C file depending on the setting
4020 default_C_entries (inf
)
4023 C_entries (cplusplus
? C_PLPL
: C_AUTO
, inf
);
4026 /* Always do plain C. */
4028 plain_C_entries (inf
)
4034 /* Always do C++. */
4036 Cplusplus_entries (inf
)
4039 C_entries (C_PLPL
, inf
);
4042 /* Always do Java. */
4047 C_entries (C_JAVA
, inf
);
4055 C_entries (C_STAR
, inf
);
4058 /* Always do Yacc. */
4063 C_entries (YACC
, inf
);
4067 /* Useful macros. */
4068 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
4069 for (; /* loop initialization */ \
4070 !feof (file_pointer) /* loop test */ \
4071 && /* instructions at start of loop */ \
4072 (readline (&line_buffer, file_pointer), \
4073 char_pointer = line_buffer.buffer, \
4077 #define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \
4078 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
4079 && strneq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
4080 && notinname ((cp)[sizeof(kw)-1]) /* end of kw */ \
4081 && ((cp) = skip_spaces((cp)+sizeof(kw)-1))) /* skip spaces */
4083 /* Similar to LOOKING_AT but does not use notinname, does not skip */
4084 #define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \
4085 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
4086 && strncaseeq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
4087 && ((cp) += sizeof(kw)-1)) /* skip spaces */
4090 * Read a file, but do no processing. This is used to do regexp
4091 * matching on files that have no language defined.
4094 just_read_file (inf
)
4097 register char *dummy
;
4099 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
4104 /* Fortran parsing */
4106 static void F_takeprec
__P((void));
4107 static void F_getit
__P((FILE *));
4112 dbp
= skip_spaces (dbp
);
4116 dbp
= skip_spaces (dbp
);
4117 if (strneq (dbp
, "(*)", 3))
4122 if (!ISDIGIT (*dbp
))
4124 --dbp
; /* force failure */
4129 while (ISDIGIT (*dbp
));
4138 dbp
= skip_spaces (dbp
);
4141 readline (&lb
, inf
);
4146 dbp
= skip_spaces (dbp
);
4148 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
4150 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
4152 make_tag (dbp
, cp
-dbp
, TRUE
,
4153 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4158 Fortran_functions (inf
)
4161 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4164 dbp
++; /* Ratfor escape to fortran */
4165 dbp
= skip_spaces (dbp
);
4168 switch (lowcase (*dbp
))
4171 if (nocase_tail ("integer"))
4175 if (nocase_tail ("real"))
4179 if (nocase_tail ("logical"))
4183 if (nocase_tail ("complex") || nocase_tail ("character"))
4187 if (nocase_tail ("double"))
4189 dbp
= skip_spaces (dbp
);
4192 if (nocase_tail ("precision"))
4198 dbp
= skip_spaces (dbp
);
4201 switch (lowcase (*dbp
))
4204 if (nocase_tail ("function"))
4208 if (nocase_tail ("subroutine"))
4212 if (nocase_tail ("entry"))
4216 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
4218 dbp
= skip_spaces (dbp
);
4219 if (*dbp
== '\0') /* assume un-named */
4220 make_tag ("blockdata", 9, TRUE
,
4221 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
4223 F_getit (inf
); /* look for name */
4234 * Philippe Waroquiers (1998)
4237 static void Ada_getit
__P((FILE *, char *));
4239 /* Once we are positioned after an "interesting" keyword, let's get
4240 the real tag value necessary. */
4242 Ada_getit (inf
, name_qualifier
)
4244 char *name_qualifier
;
4252 dbp
= skip_spaces (dbp
);
4254 || (dbp
[0] == '-' && dbp
[1] == '-'))
4256 readline (&lb
, inf
);
4259 switch (lowcase(*dbp
))
4262 if (nocase_tail ("body"))
4264 /* Skipping body of procedure body or package body or ....
4265 resetting qualifier to body instead of spec. */
4266 name_qualifier
= "/b";
4271 /* Skipping type of task type or protected type ... */
4272 if (nocase_tail ("type"))
4279 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
4284 dbp
= skip_spaces (dbp
);
4287 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
4295 name
= concat (dbp
, name_qualifier
, "");
4297 make_tag (name
, strlen (name
), TRUE
,
4298 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4310 bool inquote
= FALSE
;
4311 bool skip_till_semicolumn
= FALSE
;
4313 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4315 while (*dbp
!= '\0')
4317 /* Skip a string i.e. "abcd". */
4318 if (inquote
|| (*dbp
== '"'))
4320 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
4325 continue; /* advance char */
4330 break; /* advance line */
4334 /* Skip comments. */
4335 if (dbp
[0] == '-' && dbp
[1] == '-')
4336 break; /* advance line */
4338 /* Skip character enclosed in single quote i.e. 'a'
4339 and skip single quote starting an attribute i.e. 'Image. */
4348 if (skip_till_semicolumn
)
4351 skip_till_semicolumn
= FALSE
;
4353 continue; /* advance char */
4356 /* Search for beginning of a token. */
4357 if (!begtoken (*dbp
))
4360 continue; /* advance char */
4363 /* We are at the beginning of a token. */
4364 switch (lowcase(*dbp
))
4367 if (!packages_only
&& nocase_tail ("function"))
4368 Ada_getit (inf
, "/f");
4370 break; /* from switch */
4371 continue; /* advance char */
4373 if (!packages_only
&& nocase_tail ("procedure"))
4374 Ada_getit (inf
, "/p");
4375 else if (nocase_tail ("package"))
4376 Ada_getit (inf
, "/s");
4377 else if (nocase_tail ("protected")) /* protected type */
4378 Ada_getit (inf
, "/t");
4380 break; /* from switch */
4381 continue; /* advance char */
4384 if (typedefs
&& !packages_only
&& nocase_tail ("use"))
4386 /* when tagging types, avoid tagging use type Pack.Typename;
4387 for this, we will skip everything till a ; */
4388 skip_till_semicolumn
= TRUE
;
4389 continue; /* advance char */
4393 if (!packages_only
&& nocase_tail ("task"))
4394 Ada_getit (inf
, "/k");
4395 else if (typedefs
&& !packages_only
&& nocase_tail ("type"))
4397 Ada_getit (inf
, "/t");
4398 while (*dbp
!= '\0')
4402 break; /* from switch */
4403 continue; /* advance char */
4406 /* Look for the end of the token. */
4407 while (!endtoken (*dbp
))
4410 } /* advance char */
4411 } /* advance line */
4416 * Unix and microcontroller assembly tag handling
4417 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
4418 * Idea by Bob Weiner, Motorola Inc. (1994)
4426 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4428 /* If first char is alphabetic or one of [_.$], test for colon
4429 following identifier. */
4430 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4432 /* Read past label. */
4434 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4436 if (*cp
== ':' || iswhite (*cp
))
4437 /* Found end of label, so copy it and add it to the table. */
4438 make_tag (lb
.buffer
, cp
- lb
.buffer
, TRUE
,
4439 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4447 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
4448 * Perl variable names: /^(my|local).../
4449 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
4450 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
4451 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
4454 Perl_functions (inf
)
4457 char *package
= savestr ("main"); /* current package name */
4460 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4464 if (LOOKING_AT (cp
, "package"))
4467 get_tag (cp
, &package
);
4469 else if (LOOKING_AT (cp
, "sub"))
4474 while (!notinname (*cp
))
4477 continue; /* nothing found */
4478 if ((pos
= etags_strchr (sp
, ':')) != NULL
4479 && pos
< cp
&& pos
[1] == ':')
4480 /* The name is already qualified. */
4481 make_tag (sp
, cp
- sp
, TRUE
,
4482 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4486 char savechar
, *name
;
4490 name
= concat (package
, "::", sp
);
4492 make_tag (name
, strlen(name
), TRUE
,
4493 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4497 else if (globals
) /* only if we are tagging global vars */
4499 /* Skip a qualifier, if any. */
4500 bool qual
= LOOKING_AT (cp
, "my") || LOOKING_AT (cp
, "local");
4501 /* After "my" or "local", but before any following paren or space. */
4502 char *varstart
= cp
;
4504 if (qual
/* should this be removed? If yes, how? */
4505 && (*cp
== '$' || *cp
== '@' || *cp
== '%'))
4510 while (ISALNUM (*cp
) || *cp
== '_');
4514 /* Should be examining a variable list at this point;
4515 could insist on seeing an open parenthesis. */
4516 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
4522 make_tag (varstart
, cp
- varstart
, FALSE
,
4523 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4532 * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4533 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4534 * More ideas by seb bacon <seb@jamkit.com> (2002)
4537 Python_functions (inf
)
4542 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4544 cp
= skip_spaces (cp
);
4545 if (LOOKING_AT (cp
, "def") || LOOKING_AT (cp
, "class"))
4548 while (!notinname (*cp
) && *cp
!= ':')
4550 make_tag (name
, cp
- name
, TRUE
,
4551 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4560 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4561 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4562 * - /^[ \t]*define\(\"[^\"]+/
4563 * Only with --members:
4564 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4565 * Idea by Diez B. Roggisch (2001)
4571 register char *cp
, *name
;
4572 bool search_identifier
= FALSE
;
4574 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4576 cp
= skip_spaces (cp
);
4578 if (search_identifier
4581 while (!notinname (*cp
))
4583 make_tag (name
, cp
- name
, TRUE
,
4584 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4585 search_identifier
= FALSE
;
4587 else if (LOOKING_AT (cp
, "function"))
4590 cp
= skip_spaces (cp
+1);
4594 while (!notinname (*cp
))
4596 make_tag (name
, cp
- name
, TRUE
,
4597 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4600 search_identifier
= TRUE
;
4602 else if (LOOKING_AT (cp
, "class"))
4607 while (*cp
!= '\0' && !iswhite (*cp
))
4609 make_tag (name
, cp
- name
, FALSE
,
4610 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4613 search_identifier
= TRUE
;
4615 else if (strneq (cp
, "define", 6)
4616 && (cp
= skip_spaces (cp
+6))
4618 && (*cp
== '"' || *cp
== '\''))
4622 while (*cp
!= quote
&& *cp
!= '\0')
4624 make_tag (name
, cp
- name
, FALSE
,
4625 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4628 && LOOKING_AT (cp
, "var")
4632 while (!notinname(*cp
))
4634 make_tag (name
, cp
- name
, FALSE
,
4635 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4642 * Cobol tag functions
4643 * We could look for anything that could be a paragraph name.
4644 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4645 * Idea by Corny de Souza (1993)
4648 Cobol_paragraphs (inf
)
4651 register char *bp
, *ep
;
4653 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4659 /* If eoln, compiler option or comment ignore whole line. */
4660 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
4663 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
4666 make_tag (bp
, ep
- bp
, TRUE
,
4667 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4674 * Ideas by Assar Westerlund <assar@sics.se> (2001)
4677 Makefile_targets (inf
)
4682 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4684 if (*bp
== '\t' || *bp
== '#')
4686 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
4688 if (*bp
== ':' || (globals
&& *bp
== '='))
4690 /* We should detect if there is more than one tag, but we do not.
4691 We just skip initial and final spaces. */
4692 char * namestart
= skip_spaces (lb
.buffer
);
4693 while (--bp
> namestart
)
4694 if (!notinname (*bp
))
4696 make_tag (namestart
, bp
- namestart
+ 1, TRUE
,
4697 lb
.buffer
, bp
- lb
.buffer
+ 2, lineno
, linecharno
);
4705 * Original code by Mosur K. Mohan (1989)
4707 * Locates tags for procedures & functions. Doesn't do any type- or
4708 * var-definitions. It does look for the keyword "extern" or
4709 * "forward" immediately following the procedure statement; if found,
4710 * the tag is skipped.
4713 Pascal_functions (inf
)
4716 linebuffer tline
; /* mostly copied from C_entries */
4718 int save_lineno
, namelen
, taglen
;
4721 bool /* each of these flags is TRUE iff: */
4722 incomment
, /* point is inside a comment */
4723 inquote
, /* point is inside '..' string */
4724 get_tagname
, /* point is after PROCEDURE/FUNCTION
4725 keyword, so next item = potential tag */
4726 found_tag
, /* point is after a potential tag */
4727 inparms
, /* point is within parameter-list */
4728 verify_tag
; /* point has passed the parm-list, so the
4729 next token will determine whether this
4730 is a FORWARD/EXTERN to be ignored, or
4731 whether it is a real tag */
4733 save_lcno
= save_lineno
= namelen
= taglen
= 0; /* keep compiler quiet */
4734 name
= NULL
; /* keep compiler quiet */
4737 linebuffer_init (&tline
);
4739 incomment
= inquote
= FALSE
;
4740 found_tag
= FALSE
; /* have a proc name; check if extern */
4741 get_tagname
= FALSE
; /* found "procedure" keyword */
4742 inparms
= FALSE
; /* found '(' after "proc" */
4743 verify_tag
= FALSE
; /* check if "extern" is ahead */
4746 while (!feof (inf
)) /* long main loop to get next char */
4749 if (c
== '\0') /* if end of line */
4751 readline (&lb
, inf
);
4755 if (!((found_tag
&& verify_tag
)
4757 c
= *dbp
++; /* only if don't need *dbp pointing
4758 to the beginning of the name of
4759 the procedure or function */
4763 if (c
== '}') /* within { } comments */
4765 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4782 inquote
= TRUE
; /* found first quote */
4784 case '{': /* found open { comment */
4788 if (*dbp
== '*') /* found open (* comment */
4793 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4796 case ')': /* end of parms list */
4801 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4808 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4810 /* Check if this is an "extern" declaration. */
4813 if (lowcase (*dbp
== 'e'))
4815 if (nocase_tail ("extern")) /* superfluous, really! */
4821 else if (lowcase (*dbp
) == 'f')
4823 if (nocase_tail ("forward")) /* check for forward reference */
4829 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4833 make_tag (name
, namelen
, TRUE
,
4834 tline
.buffer
, taglen
, save_lineno
, save_lcno
);
4838 if (get_tagname
) /* grab name of proc or fn */
4845 /* Find block name. */
4846 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4849 /* Save all values for later tagging. */
4850 linebuffer_setlen (&tline
, lb
.len
);
4851 strcpy (tline
.buffer
, lb
.buffer
);
4852 save_lineno
= lineno
;
4853 save_lcno
= linecharno
;
4854 name
= tline
.buffer
+ (dbp
- lb
.buffer
);
4856 taglen
= cp
- lb
.buffer
+ 1;
4858 dbp
= cp
; /* set dbp to e-o-token */
4859 get_tagname
= FALSE
;
4863 /* And proceed to check for "extern". */
4865 else if (!incomment
&& !inquote
&& !found_tag
)
4867 /* Check for proc/fn keywords. */
4868 switch (lowcase (c
))
4871 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4875 if (nocase_tail ("unction"))
4880 } /* while not eof */
4882 free (tline
.buffer
);
4887 * Lisp tag functions
4888 * look for (def or (DEF, quote or QUOTE
4891 static void L_getit
__P((void));
4896 if (*dbp
== '\'') /* Skip prefix quote */
4898 else if (*dbp
== '(')
4901 /* Try to skip "(quote " */
4902 if (!LOOKING_AT (dbp
, "quote") && !LOOKING_AT (dbp
, "QUOTE"))
4903 /* Ok, then skip "(" before name in (defstruct (foo)) */
4904 dbp
= skip_spaces (dbp
);
4906 get_tag (dbp
, NULL
);
4910 Lisp_functions (inf
)
4913 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4918 if (strneq (dbp
+1, "def", 3) || strneq (dbp
+1, "DEF", 3))
4920 dbp
= skip_non_spaces (dbp
);
4921 dbp
= skip_spaces (dbp
);
4926 /* Check for (foo::defmumble name-defined ... */
4929 while (!notinname (*dbp
) && *dbp
!= ':');
4934 while (*dbp
== ':');
4936 if (strneq (dbp
, "def", 3) || strneq (dbp
, "DEF", 3))
4938 dbp
= skip_non_spaces (dbp
);
4939 dbp
= skip_spaces (dbp
);
4949 * Lua script language parsing
4950 * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
4952 * "function" and "local function" are tags if they start at column 1.
4960 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4962 if (bp
[0] != 'f' && bp
[0] != 'l')
4965 (void)LOOKING_AT (bp
, "local"); /* skip possible "local" */
4967 if (LOOKING_AT (bp
, "function"))
4975 * Just look for lines where the first character is '/'
4976 * Also look at "defineps" for PSWrap
4978 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4979 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4985 register char *bp
, *ep
;
4987 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4992 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4995 make_tag (bp
, ep
- bp
, TRUE
,
4996 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4998 else if (LOOKING_AT (bp
, "defineps"))
5006 * Ignore anything after \ followed by space or in ( )
5007 * Look for words defined by :
5008 * Look for constant, code, create, defer, value, and variable
5009 * OBP extensions: Look for buffer:, field,
5010 * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004)
5018 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5019 while ((bp
= skip_spaces (bp
))[0] != '\0')
5020 if (bp
[0] == '\\' && iswhite(bp
[1]))
5021 break; /* read next line */
5022 else if (bp
[0] == '(' && iswhite(bp
[1]))
5023 do /* skip to ) or eol */
5025 while (*bp
!= ')' && *bp
!= '\0');
5026 else if ((bp
[0] == ':' && iswhite(bp
[1]) && bp
++)
5027 || LOOKING_AT_NOCASE (bp
, "constant")
5028 || LOOKING_AT_NOCASE (bp
, "code")
5029 || LOOKING_AT_NOCASE (bp
, "create")
5030 || LOOKING_AT_NOCASE (bp
, "defer")
5031 || LOOKING_AT_NOCASE (bp
, "value")
5032 || LOOKING_AT_NOCASE (bp
, "variable")
5033 || LOOKING_AT_NOCASE (bp
, "buffer:")
5034 || LOOKING_AT_NOCASE (bp
, "field"))
5035 get_tag (skip_spaces (bp
), NULL
); /* Yay! A definition! */
5037 bp
= skip_non_spaces (bp
);
5042 * Scheme tag functions
5043 * look for (def... xyzzy
5045 * (def ... ((...(xyzzy ....
5047 * Original code by Ken Haase (1985?)
5050 Scheme_functions (inf
)
5055 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5057 if (strneq (bp
, "(def", 4) || strneq (bp
, "(DEF", 4))
5059 bp
= skip_non_spaces (bp
+4);
5060 /* Skip over open parens and white space */
5061 while (notinname (*bp
))
5065 if (LOOKING_AT (bp
, "(SET!") || LOOKING_AT (bp
, "(set!"))
5071 /* Find tags in TeX and LaTeX input files. */
5073 /* TEX_toktab is a table of TeX control sequences that define tags.
5074 * Each entry records one such control sequence.
5076 * Original code from who knows whom.
5078 * Stefan Monnier (2002)
5081 static linebuffer
*TEX_toktab
= NULL
; /* Table with tag tokens */
5083 /* Default set of control sequences to put into TEX_toktab.
5084 The value of environment var TEXTAGS is prepended to this. */
5085 static char *TEX_defenv
= "\
5086 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
5087 :part:appendix:entry:index:def\
5088 :newcommand:renewcommand:newenvironment:renewenvironment";
5090 static void TEX_mode
__P((FILE *));
5091 static void TEX_decode_env
__P((char *, char *));
5093 static char TEX_esc
= '\\';
5094 static char TEX_opgrp
= '{';
5095 static char TEX_clgrp
= '}';
5098 * TeX/LaTeX scanning loop.
5107 /* Select either \ or ! as escape character. */
5110 /* Initialize token table once from environment. */
5111 if (TEX_toktab
== NULL
)
5112 TEX_decode_env ("TEXTAGS", TEX_defenv
);
5114 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5116 /* Look at each TEX keyword in line. */
5119 /* Look for a TEX escape. */
5120 while (*cp
++ != TEX_esc
)
5121 if (cp
[-1] == '\0' || cp
[-1] == '%')
5124 for (key
= TEX_toktab
; key
->buffer
!= NULL
; key
++)
5125 if (strneq (cp
, key
->buffer
, key
->len
))
5128 int namelen
, linelen
;
5131 cp
= skip_spaces (cp
+ key
->len
);
5132 if (*cp
== TEX_opgrp
)
5138 (!iswhite (*p
) && *p
!= '#' &&
5139 *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
);
5144 if (!opgrp
|| *p
== TEX_clgrp
)
5146 while (*p
!= '\0' && *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
)
5148 linelen
= p
- lb
.buffer
+ 1;
5150 make_tag (cp
, namelen
, TRUE
,
5151 lb
.buffer
, linelen
, lineno
, linecharno
);
5152 goto tex_next_line
; /* We only tag a line once */
5160 #define TEX_LESC '\\'
5161 #define TEX_SESC '!'
5163 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
5164 chars accordingly. */
5171 while ((c
= getc (inf
)) != EOF
)
5173 /* Skip to next line if we hit the TeX comment char. */
5175 while (c
!= '\n' && c
!= EOF
)
5177 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
5193 /* If the input file is compressed, inf is a pipe, and rewind may fail.
5194 No attempt is made to correct the situation. */
5198 /* Read environment and prepend it to the default string.
5199 Build token table. */
5201 TEX_decode_env (evarname
, defenv
)
5205 register char *env
, *p
;
5208 /* Append default string to environment. */
5209 env
= getenv (evarname
);
5215 env
= concat (oldenv
, defenv
, "");
5218 /* Allocate a token table */
5219 for (len
= 1, p
= env
; p
;)
5220 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
5222 TEX_toktab
= xnew (len
, linebuffer
);
5224 /* Unpack environment string into token table. Be careful about */
5225 /* zero-length strings (leading ':', "::" and trailing ':') */
5226 for (i
= 0; *env
!= '\0';)
5228 p
= etags_strchr (env
, ':');
5229 if (!p
) /* End of environment string. */
5230 p
= env
+ strlen (env
);
5232 { /* Only non-zero strings. */
5233 TEX_toktab
[i
].buffer
= savenstr (env
, p
- env
);
5234 TEX_toktab
[i
].len
= p
- env
;
5241 TEX_toktab
[i
].buffer
= NULL
; /* Mark end of table. */
5242 TEX_toktab
[i
].len
= 0;
5249 /* Texinfo support. Dave Love, Mar. 2000. */
5255 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5256 if (LOOKING_AT (cp
, "@node"))
5259 while (*cp
!= '\0' && *cp
!= ',')
5261 make_tag (start
, cp
- start
, TRUE
,
5262 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5269 * Contents of <title>, <h1>, <h2>, <h3> are tags.
5270 * Contents of <a name=xxx> are tags with name xxx.
5272 * Francesco Potortì, 2002.
5278 bool getnext
= FALSE
; /* next text outside of HTML tags is a tag */
5279 bool skiptag
= FALSE
; /* skip to the end of the current HTML tag */
5280 bool intag
= FALSE
; /* inside an html tag, looking for ID= */
5281 bool inanchor
= FALSE
; /* when INTAG, is an anchor, look for NAME= */
5285 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5287 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
5288 for (;;) /* loop on the same line */
5290 if (skiptag
) /* skip HTML tag */
5292 while (*dbp
!= '\0' && *dbp
!= '>')
5298 continue; /* look on the same line */
5300 break; /* go to next line */
5303 else if (intag
) /* look for "name=" or "id=" */
5305 while (*dbp
!= '\0' && *dbp
!= '>'
5306 && lowcase (*dbp
) != 'n' && lowcase (*dbp
) != 'i')
5309 break; /* go to next line */
5314 continue; /* look on the same line */
5316 if ((inanchor
&& LOOKING_AT_NOCASE (dbp
, "name="))
5317 || LOOKING_AT_NOCASE (dbp
, "id="))
5319 bool quoted
= (dbp
[0] == '"');
5322 for (end
= ++dbp
; *end
!= '\0' && *end
!= '"'; end
++)
5325 for (end
= dbp
; *end
!= '\0' && intoken (*end
); end
++)
5327 linebuffer_setlen (&token_name
, end
- dbp
);
5328 strncpy (token_name
.buffer
, dbp
, end
- dbp
);
5329 token_name
.buffer
[end
- dbp
] = '\0';
5332 intag
= FALSE
; /* we found what we looked for */
5333 skiptag
= TRUE
; /* skip to the end of the tag */
5334 getnext
= TRUE
; /* then grab the text */
5335 continue; /* look on the same line */
5340 else if (getnext
) /* grab next tokens and tag them */
5342 dbp
= skip_spaces (dbp
);
5344 break; /* go to next line */
5348 inanchor
= (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]));
5349 continue; /* look on the same line */
5352 for (end
= dbp
+ 1; *end
!= '\0' && *end
!= '<'; end
++)
5354 make_tag (token_name
.buffer
, token_name
.len
, TRUE
,
5355 dbp
, end
- dbp
, lineno
, linecharno
);
5356 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5358 break; /* go to next line */
5361 else /* look for an interesting HTML tag */
5363 while (*dbp
!= '\0' && *dbp
!= '<')
5366 break; /* go to next line */
5368 if (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]))
5371 continue; /* look on the same line */
5373 else if (LOOKING_AT_NOCASE (dbp
, "<title>")
5374 || LOOKING_AT_NOCASE (dbp
, "<h1>")
5375 || LOOKING_AT_NOCASE (dbp
, "<h2>")
5376 || LOOKING_AT_NOCASE (dbp
, "<h3>"))
5380 continue; /* look on the same line */
5391 * Assumes that the predicate or rule starts at column 0.
5392 * Only the first clause of a predicate or rule is added.
5393 * Original code by Sunichirou Sugou (1989)
5394 * Rewritten by Anders Lindgren (1996)
5396 static int prolog_pr
__P((char *, char *));
5397 static void prolog_skip_comment
__P((linebuffer
*, FILE *));
5398 static int prolog_atom
__P((char *, int));
5401 Prolog_functions (inf
)
5412 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5414 if (cp
[0] == '\0') /* Empty line */
5416 else if (iswhite (cp
[0])) /* Not a predicate */
5418 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
5419 prolog_skip_comment (&lb
, inf
);
5420 else if ((len
= prolog_pr (cp
, last
)) > 0)
5422 /* Predicate or rule. Store the function name so that we
5423 only generate a tag for the first clause. */
5425 last
= xnew(len
+ 1, char);
5426 else if (len
+ 1 > allocated
)
5427 xrnew (last
, len
+ 1, char);
5428 allocated
= len
+ 1;
5429 strncpy (last
, cp
, len
);
5439 prolog_skip_comment (plb
, inf
)
5447 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
5448 if (cp
[0] == '*' && cp
[1] == '/')
5450 readline (plb
, inf
);
5456 * A predicate or rule definition is added if it matches:
5457 * <beginning of line><Prolog Atom><whitespace>(
5458 * or <beginning of line><Prolog Atom><whitespace>:-
5460 * It is added to the tags database if it doesn't match the
5461 * name of the previous clause header.
5463 * Return the size of the name of the predicate or rule, or 0 if no
5469 char *last
; /* Name of last clause. */
5474 pos
= prolog_atom (s
, 0);
5479 pos
= skip_spaces (s
+ pos
) - s
;
5482 || (s
[pos
] == '(' && (pos
+= 1))
5483 || (s
[pos
] == ':' && s
[pos
+ 1] == '-' && (pos
+= 2)))
5484 && (last
== NULL
/* save only the first clause */
5485 || len
!= (int)strlen (last
)
5486 || !strneq (s
, last
, len
)))
5488 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5496 * Consume a Prolog atom.
5497 * Return the number of bytes consumed, or -1 if there was an error.
5499 * A prolog atom, in this context, could be one of:
5500 * - An alphanumeric sequence, starting with a lower case letter.
5501 * - A quoted arbitrary string. Single quotes can escape themselves.
5502 * Backslash quotes everything.
5505 prolog_atom (s
, pos
)
5513 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
5515 /* The atom is unquoted. */
5517 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
5521 return pos
- origpos
;
5523 else if (s
[pos
] == '\'')
5534 pos
++; /* A double quote */
5536 else if (s
[pos
] == '\0')
5537 /* Multiline quoted atoms are ignored. */
5539 else if (s
[pos
] == '\\')
5541 if (s
[pos
+1] == '\0')
5548 return pos
- origpos
;
5556 * Support for Erlang
5558 * Generates tags for functions, defines, and records.
5559 * Assumes that Erlang functions start at column 0.
5560 * Original code by Anders Lindgren (1996)
5562 static int erlang_func
__P((char *, char *));
5563 static void erlang_attribute
__P((char *));
5564 static int erlang_atom
__P((char *));
5567 Erlang_functions (inf
)
5578 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5580 if (cp
[0] == '\0') /* Empty line */
5582 else if (iswhite (cp
[0])) /* Not function nor attribute */
5584 else if (cp
[0] == '%') /* comment */
5586 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
5588 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
5590 erlang_attribute (cp
);
5597 else if ((len
= erlang_func (cp
, last
)) > 0)
5600 * Function. Store the function name so that we only
5601 * generates a tag for the first clause.
5604 last
= xnew (len
+ 1, char);
5605 else if (len
+ 1 > allocated
)
5606 xrnew (last
, len
+ 1, char);
5607 allocated
= len
+ 1;
5608 strncpy (last
, cp
, len
);
5618 * A function definition is added if it matches:
5619 * <beginning of line><Erlang Atom><whitespace>(
5621 * It is added to the tags database if it doesn't match the
5622 * name of the previous clause header.
5624 * Return the size of the name of the function, or 0 if no function
5628 erlang_func (s
, last
)
5630 char *last
; /* Name of last clause. */
5635 pos
= erlang_atom (s
);
5640 pos
= skip_spaces (s
+ pos
) - s
;
5642 /* Save only the first clause. */
5645 || len
!= (int)strlen (last
)
5646 || !strneq (s
, last
, len
)))
5648 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5657 * Handle attributes. Currently, tags are generated for defines
5660 * They are on the form:
5661 * -define(foo, bar).
5662 * -define(Foo(M, N), M+N).
5663 * -record(graph, {vtab = notable, cyclic = true}).
5666 erlang_attribute (s
)
5671 if ((LOOKING_AT (cp
, "-define") || LOOKING_AT (cp
, "-record"))
5674 int len
= erlang_atom (skip_spaces (cp
));
5676 make_tag (cp
, len
, TRUE
, s
, cp
+ len
- s
, lineno
, linecharno
);
5683 * Consume an Erlang atom (or variable).
5684 * Return the number of bytes consumed, or -1 if there was an error.
5692 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
5694 /* The atom is unquoted. */
5697 while (ISALNUM (s
[pos
]) || s
[pos
] == '_');
5699 else if (s
[pos
] == '\'')
5701 for (pos
++; s
[pos
] != '\''; pos
++)
5702 if (s
[pos
] == '\0' /* multiline quoted atoms are ignored */
5703 || (s
[pos
] == '\\' && s
[++pos
] == '\0'))
5712 static char *scan_separators
__P((char *));
5713 static void add_regex
__P((char *, language
*));
5714 static char *substitute
__P((char *, char *, struct re_registers
*));
5717 * Take a string like "/blah/" and turn it into "blah", verifying
5718 * that the first and last characters are the same, and handling
5719 * quoted separator characters. Actually, stops on the occurrence of
5720 * an unquoted separator. Also process \t, \n, etc. and turn into
5721 * appropriate characters. Works in place. Null terminates name string.
5722 * Returns pointer to terminating separator, or NULL for
5723 * unterminated regexps.
5726 scan_separators (name
)
5730 char *copyto
= name
;
5731 bool quoted
= FALSE
;
5733 for (++name
; *name
!= '\0'; ++name
)
5739 case 'a': *copyto
++ = '\007'; break; /* BEL (bell) */
5740 case 'b': *copyto
++ = '\b'; break; /* BS (back space) */
5741 case 'd': *copyto
++ = 0177; break; /* DEL (delete) */
5742 case 'e': *copyto
++ = 033; break; /* ESC (delete) */
5743 case 'f': *copyto
++ = '\f'; break; /* FF (form feed) */
5744 case 'n': *copyto
++ = '\n'; break; /* NL (new line) */
5745 case 'r': *copyto
++ = '\r'; break; /* CR (carriage return) */
5746 case 't': *copyto
++ = '\t'; break; /* TAB (horizontal tab) */
5747 case 'v': *copyto
++ = '\v'; break; /* VT (vertical tab) */
5753 /* Something else is quoted, so preserve the quote. */
5761 else if (*name
== '\\')
5763 else if (*name
== sep
)
5769 name
= NULL
; /* signal unterminated regexp */
5771 /* Terminate copied string. */
5776 /* Look at the argument of --regex or --no-regex and do the right
5777 thing. Same for each line of a regexp file. */
5779 analyse_regex (regex_arg
)
5782 if (regex_arg
== NULL
)
5784 free_regexps (); /* --no-regex: remove existing regexps */
5788 /* A real --regexp option or a line in a regexp file. */
5789 switch (regex_arg
[0])
5791 /* Comments in regexp file or null arg to --regex. */
5797 /* Read a regex file. This is recursive and may result in a
5798 loop, which will stop when the file descriptors are exhausted. */
5802 linebuffer regexbuf
;
5803 char *regexfile
= regex_arg
+ 1;
5805 /* regexfile is a file containing regexps, one per line. */
5806 regexfp
= fopen (regexfile
, "r");
5807 if (regexfp
== NULL
)
5812 linebuffer_init (®exbuf
);
5813 while (readline_internal (®exbuf
, regexfp
) > 0)
5814 analyse_regex (regexbuf
.buffer
);
5815 free (regexbuf
.buffer
);
5820 /* Regexp to be used for a specific language only. */
5824 char *lang_name
= regex_arg
+ 1;
5827 for (cp
= lang_name
; *cp
!= '}'; cp
++)
5830 error ("unterminated language name in regex: %s", regex_arg
);
5834 lang
= get_language_from_langname (lang_name
);
5837 add_regex (cp
, lang
);
5841 /* Regexp to be used for any language. */
5843 add_regex (regex_arg
, NULL
);
5848 /* Separate the regexp pattern, compile it,
5849 and care for optional name and modifiers. */
5851 add_regex (regexp_pattern
, lang
)
5852 char *regexp_pattern
;
5855 static struct re_pattern_buffer zeropattern
;
5856 char sep
, *pat
, *name
, *modifiers
;
5858 struct re_pattern_buffer
*patbuf
;
5861 force_explicit_name
= TRUE
, /* do not use implicit tag names */
5862 ignore_case
= FALSE
, /* case is significant */
5863 multi_line
= FALSE
, /* matches are done one line at a time */
5864 single_line
= FALSE
; /* dot does not match newline */
5867 if (strlen(regexp_pattern
) < 3)
5869 error ("null regexp", (char *)NULL
);
5872 sep
= regexp_pattern
[0];
5873 name
= scan_separators (regexp_pattern
);
5876 error ("%s: unterminated regexp", regexp_pattern
);
5881 error ("null name for regexp \"%s\"", regexp_pattern
);
5884 modifiers
= scan_separators (name
);
5885 if (modifiers
== NULL
) /* no terminating separator --> no name */
5891 modifiers
+= 1; /* skip separator */
5893 /* Parse regex modifiers. */
5894 for (; modifiers
[0] != '\0'; modifiers
++)
5895 switch (modifiers
[0])
5898 if (modifiers
== name
)
5899 error ("forcing explicit tag name but no name, ignoring", NULL
);
5900 force_explicit_name
= TRUE
;
5910 need_filebuf
= TRUE
;
5915 wrongmod
[0] = modifiers
[0];
5917 error ("invalid regexp modifier `%s', ignoring", wrongmod
);
5922 patbuf
= xnew (1, struct re_pattern_buffer
);
5923 *patbuf
= zeropattern
;
5926 static char lc_trans
[CHARS
];
5928 for (i
= 0; i
< CHARS
; i
++)
5929 lc_trans
[i
] = lowcase (i
);
5930 patbuf
->translate
= lc_trans
; /* translation table to fold case */
5934 pat
= concat ("^", regexp_pattern
, ""); /* anchor to beginning of line */
5936 pat
= regexp_pattern
;
5939 re_set_syntax (RE_SYNTAX_EMACS
| RE_DOT_NEWLINE
);
5941 re_set_syntax (RE_SYNTAX_EMACS
);
5943 err
= re_compile_pattern (pat
, strlen (regexp_pattern
), patbuf
);
5948 error ("%s while compiling pattern", err
);
5953 p_head
= xnew (1, regexp
);
5954 p_head
->pattern
= savestr (regexp_pattern
);
5955 p_head
->p_next
= rp
;
5956 p_head
->lang
= lang
;
5957 p_head
->pat
= patbuf
;
5958 p_head
->name
= savestr (name
);
5959 p_head
->error_signaled
= FALSE
;
5960 p_head
->force_explicit_name
= force_explicit_name
;
5961 p_head
->ignore_case
= ignore_case
;
5962 p_head
->multi_line
= multi_line
;
5966 * Do the substitutions indicated by the regular expression and
5970 substitute (in
, out
, regs
)
5972 struct re_registers
*regs
;
5975 int size
, dig
, diglen
;
5978 size
= strlen (out
);
5980 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5981 if (out
[size
- 1] == '\\')
5982 fatal ("pattern error in \"%s\"", out
);
5983 for (t
= etags_strchr (out
, '\\');
5985 t
= etags_strchr (t
+ 2, '\\'))
5989 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5995 /* Allocate space and do the substitutions. */
5997 result
= xnew (size
+ 1, char);
5999 for (t
= result
; *out
!= '\0'; out
++)
6000 if (*out
== '\\' && ISDIGIT (*++out
))
6003 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
6004 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
6011 assert (t
<= result
+ size
);
6012 assert (t
- result
== (int)strlen (result
));
6017 /* Deallocate all regexps. */
6022 while (p_head
!= NULL
)
6024 rp
= p_head
->p_next
;
6025 free (p_head
->pattern
);
6026 free (p_head
->name
);
6034 * Reads the whole file as a single string from `filebuf' and looks for
6035 * multi-line regular expressions, creating tags on matches.
6036 * readline already dealt with normal regexps.
6038 * Idea by Ben Wing <ben@666.com> (2002).
6041 regex_tag_multiline ()
6043 char *buffer
= filebuf
.buffer
;
6047 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6051 if (!rp
->multi_line
)
6052 continue; /* skip normal regexps */
6054 /* Generic initialisations before parsing file from memory. */
6055 lineno
= 1; /* reset global line number */
6056 charno
= 0; /* reset global char number */
6057 linecharno
= 0; /* reset global char number of line start */
6059 /* Only use generic regexps or those for the current language. */
6060 if (rp
->lang
!= NULL
&& rp
->lang
!= curfdp
->lang
)
6063 while (match
>= 0 && match
< filebuf
.len
)
6065 match
= re_search (rp
->pat
, buffer
, filebuf
.len
, charno
,
6066 filebuf
.len
- match
, &rp
->regs
);
6071 if (!rp
->error_signaled
)
6073 error ("regexp stack overflow while matching \"%s\"",
6075 rp
->error_signaled
= TRUE
;
6082 if (match
== rp
->regs
.end
[0])
6084 if (!rp
->error_signaled
)
6086 error ("regexp matches the empty string: \"%s\"",
6088 rp
->error_signaled
= TRUE
;
6090 match
= -3; /* exit from while loop */
6094 /* Match occurred. Construct a tag. */
6095 while (charno
< rp
->regs
.end
[0])
6096 if (buffer
[charno
++] == '\n')
6097 lineno
++, linecharno
= charno
;
6099 if (name
[0] == '\0')
6101 else /* make a named tag */
6102 name
= substitute (buffer
, rp
->name
, &rp
->regs
);
6103 if (rp
->force_explicit_name
)
6104 /* Force explicit tag name, if a name is there. */
6105 pfnote (name
, TRUE
, buffer
+ linecharno
,
6106 charno
- linecharno
+ 1, lineno
, linecharno
);
6108 make_tag (name
, strlen (name
), TRUE
, buffer
+ linecharno
,
6109 charno
- linecharno
+ 1, lineno
, linecharno
);
6121 register int len
= 0;
6123 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
6125 if (*cp
== '\0' && !intoken (dbp
[len
]))
6134 get_tag (bp
, namepp
)
6138 register char *cp
= bp
;
6142 /* Go till you get to white space or a syntactic break */
6143 for (cp
= bp
+ 1; !notinname (*cp
); cp
++)
6145 make_tag (bp
, cp
- bp
, TRUE
,
6146 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
6150 *namepp
= savenstr (bp
, cp
- bp
);
6154 * Read a line of text from `stream' into `lbp', excluding the
6155 * newline or CR-NL, if any. Return the number of characters read from
6156 * `stream', which is the length of the line including the newline.
6158 * On DOS or Windows we do not count the CR character, if any before the
6159 * NL, in the returned length; this mirrors the behavior of Emacs on those
6160 * platforms (for text files, it translates CR-NL to NL as it reads in the
6163 * If multi-line regular expressions are requested, each line read is
6164 * appended to `filebuf'.
6167 readline_internal (lbp
, stream
)
6169 register FILE *stream
;
6171 char *buffer
= lbp
->buffer
;
6172 register char *p
= lbp
->buffer
;
6173 register char *pend
;
6176 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
6180 register int c
= getc (stream
);
6183 /* We're at the end of linebuffer: expand it. */
6185 xrnew (buffer
, lbp
->size
, char);
6186 p
+= buffer
- lbp
->buffer
;
6187 pend
= buffer
+ lbp
->size
;
6188 lbp
->buffer
= buffer
;
6198 if (p
> buffer
&& p
[-1] == '\r')
6202 /* Assume CRLF->LF translation will be performed by Emacs
6203 when loading this file, so CRs won't appear in the buffer.
6204 It would be cleaner to compensate within Emacs;
6205 however, Emacs does not know how many CRs were deleted
6206 before any given point in the file. */
6221 lbp
->len
= p
- buffer
;
6223 if (need_filebuf
/* we need filebuf for multi-line regexps */
6224 && chars_deleted
> 0) /* not at EOF */
6226 while (filebuf
.size
<= filebuf
.len
+ lbp
->len
+ 1) /* +1 for \n */
6228 /* Expand filebuf. */
6230 xrnew (filebuf
.buffer
, filebuf
.size
, char);
6232 strncpy (filebuf
.buffer
+ filebuf
.len
, lbp
->buffer
, lbp
->len
);
6233 filebuf
.len
+= lbp
->len
;
6234 filebuf
.buffer
[filebuf
.len
++] = '\n';
6235 filebuf
.buffer
[filebuf
.len
] = '\0';
6238 return lbp
->len
+ chars_deleted
;
6242 * Like readline_internal, above, but in addition try to match the
6243 * input line against relevant regular expressions and manage #line
6247 readline (lbp
, stream
)
6253 linecharno
= charno
; /* update global char number of line start */
6254 result
= readline_internal (lbp
, stream
); /* read line */
6255 lineno
+= 1; /* increment global line number */
6256 charno
+= result
; /* increment global char number */
6258 /* Honour #line directives. */
6259 if (!no_line_directive
)
6261 static bool discard_until_line_directive
;
6263 /* Check whether this is a #line directive. */
6264 if (result
> 12 && strneq (lbp
->buffer
, "#line ", 6))
6269 if (sscanf (lbp
->buffer
, "#line %u \"%n", &lno
, &start
) >= 1
6270 && start
> 0) /* double quote character found */
6272 char *endp
= lbp
->buffer
+ start
;
6274 while ((endp
= etags_strchr (endp
, '"')) != NULL
6275 && endp
[-1] == '\\')
6278 /* Ok, this is a real #line directive. Let's deal with it. */
6280 char *taggedabsname
; /* absolute name of original file */
6281 char *taggedfname
; /* name of original file as given */
6282 char *name
; /* temp var */
6284 discard_until_line_directive
= FALSE
; /* found it */
6285 name
= lbp
->buffer
+ start
;
6287 canonicalize_filename (name
); /* for DOS */
6288 taggedabsname
= absolute_filename (name
, tagfiledir
);
6289 if (filename_is_absolute (name
)
6290 || filename_is_absolute (curfdp
->infname
))
6291 taggedfname
= savestr (taggedabsname
);
6293 taggedfname
= relative_filename (taggedabsname
,tagfiledir
);
6295 if (streq (curfdp
->taggedfname
, taggedfname
))
6296 /* The #line directive is only a line number change. We
6297 deal with this afterwards. */
6300 /* The tags following this #line directive should be
6301 attributed to taggedfname. In order to do this, set
6302 curfdp accordingly. */
6304 fdesc
*fdp
; /* file description pointer */
6306 /* Go look for a file description already set up for the
6307 file indicated in the #line directive. If there is
6308 one, use it from now until the next #line
6310 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6311 if (streq (fdp
->infname
, curfdp
->infname
)
6312 && streq (fdp
->taggedfname
, taggedfname
))
6313 /* If we remove the second test above (after the &&)
6314 then all entries pertaining to the same file are
6315 coalesced in the tags file. If we use it, then
6316 entries pertaining to the same file but generated
6317 from different files (via #line directives) will
6318 go into separate sections in the tags file. These
6319 alternatives look equivalent. The first one
6320 destroys some apparently useless information. */
6326 /* Else, if we already tagged the real file, skip all
6327 input lines until the next #line directive. */
6328 if (fdp
== NULL
) /* not found */
6329 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6330 if (streq (fdp
->infabsname
, taggedabsname
))
6332 discard_until_line_directive
= TRUE
;
6336 /* Else create a new file description and use that from
6337 now on, until the next #line directive. */
6338 if (fdp
== NULL
) /* not found */
6341 fdhead
= xnew (1, fdesc
);
6342 *fdhead
= *curfdp
; /* copy curr. file description */
6344 fdhead
->infname
= savestr (curfdp
->infname
);
6345 fdhead
->infabsname
= savestr (curfdp
->infabsname
);
6346 fdhead
->infabsdir
= savestr (curfdp
->infabsdir
);
6347 fdhead
->taggedfname
= taggedfname
;
6348 fdhead
->usecharno
= FALSE
;
6349 fdhead
->prop
= NULL
;
6350 fdhead
->written
= FALSE
;
6354 free (taggedabsname
);
6356 readline (lbp
, stream
);
6358 } /* if a real #line directive */
6359 } /* if #line is followed by a a number */
6360 } /* if line begins with "#line " */
6362 /* If we are here, no #line directive was found. */
6363 if (discard_until_line_directive
)
6367 /* Do a tail recursion on ourselves, thus discarding the contents
6368 of the line buffer. */
6369 readline (lbp
, stream
);
6373 discard_until_line_directive
= FALSE
;
6376 } /* if #line directives should be considered */
6383 /* Match against relevant regexps. */
6385 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6387 /* Only use generic regexps or those for the current language.
6388 Also do not use multiline regexps, which is the job of
6389 regex_tag_multiline. */
6390 if ((rp
->lang
!= NULL
&& rp
->lang
!= fdhead
->lang
)
6394 match
= re_match (rp
->pat
, lbp
->buffer
, lbp
->len
, 0, &rp
->regs
);
6399 if (!rp
->error_signaled
)
6401 error ("regexp stack overflow while matching \"%s\"",
6403 rp
->error_signaled
= TRUE
;
6410 /* Empty string matched. */
6411 if (!rp
->error_signaled
)
6413 error ("regexp matches the empty string: \"%s\"", rp
->pattern
);
6414 rp
->error_signaled
= TRUE
;
6418 /* Match occurred. Construct a tag. */
6420 if (name
[0] == '\0')
6422 else /* make a named tag */
6423 name
= substitute (lbp
->buffer
, rp
->name
, &rp
->regs
);
6424 if (rp
->force_explicit_name
)
6425 /* Force explicit tag name, if a name is there. */
6426 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
6428 make_tag (name
, strlen (name
), TRUE
,
6429 lbp
->buffer
, match
, lineno
, linecharno
);
6438 * Return a pointer to a space of size strlen(cp)+1 allocated
6439 * with xnew where the string CP has been copied.
6445 return savenstr (cp
, strlen (cp
));
6449 * Return a pointer to a space of size LEN+1 allocated with xnew where
6450 * the string CP has been copied for at most the first LEN characters.
6459 dp
= xnew (len
+ 1, char);
6460 strncpy (dp
, cp
, len
);
6466 * Return the ptr in sp at which the character c last
6467 * appears; NULL if not found
6469 * Identical to POSIX strrchr, included for portability.
6472 etags_strrchr (sp
, c
)
6473 register const char *sp
;
6476 register const char *r
;
6488 * Return the ptr in sp at which the character c first
6489 * appears; NULL if not found
6491 * Identical to POSIX strchr, included for portability.
6494 etags_strchr (sp
, c
)
6495 register const char *sp
;
6507 * Compare two strings, ignoring case for alphabetic characters.
6509 * Same as BSD's strcasecmp, included for portability.
6512 etags_strcasecmp (s1
, s2
)
6513 register const char *s1
;
6514 register const char *s2
;
6517 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6518 ? lowcase (*s1
) == lowcase (*s2
)
6522 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6523 ? lowcase (*s1
) - lowcase (*s2
)
6528 * Compare two strings, ignoring case for alphabetic characters.
6529 * Stop after a given number of characters
6531 * Same as BSD's strncasecmp, included for portability.
6534 etags_strncasecmp (s1
, s2
, n
)
6535 register const char *s1
;
6536 register const char *s2
;
6539 while (*s1
!= '\0' && n
-- > 0
6540 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6541 ? lowcase (*s1
) == lowcase (*s2
)
6548 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6549 ? lowcase (*s1
) - lowcase (*s2
)
6553 /* Skip spaces (end of string is not space), return new pointer. */
6558 while (iswhite (*cp
))
6563 /* Skip non spaces, except end of string, return new pointer. */
6565 skip_non_spaces (cp
)
6568 while (*cp
!= '\0' && !iswhite (*cp
))
6573 /* Print error message and exit. */
6579 exit (EXIT_FAILURE
);
6587 exit (EXIT_FAILURE
);
6591 suggest_asking_for_help ()
6593 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
6594 progname
, NO_LONG_OPTIONS
? "-h" : "--help");
6595 exit (EXIT_FAILURE
);
6598 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
6601 const char *s1
, *s2
;
6603 fprintf (stderr
, "%s: ", progname
);
6604 fprintf (stderr
, s1
, s2
);
6605 fprintf (stderr
, "\n");
6608 /* Return a newly-allocated string whose contents
6609 concatenate those of s1, s2, s3. */
6614 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
6615 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
6617 strcpy (result
, s1
);
6618 strcpy (result
+ len1
, s2
);
6619 strcpy (result
+ len1
+ len2
, s3
);
6620 result
[len1
+ len2
+ len3
] = '\0';
6626 /* Does the same work as the system V getcwd, but does not need to
6627 guess the buffer size in advance. */
6633 char *path
= xnew (bufsize
, char);
6635 while (getcwd (path
, bufsize
) == NULL
)
6637 if (errno
!= ERANGE
)
6641 path
= xnew (bufsize
, char);
6644 canonicalize_filename (path
);
6647 #else /* not HAVE_GETCWD */
6650 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
6654 for (p
= path
; *p
!= '\0'; p
++)
6660 return strdup (path
);
6661 #else /* not MSDOS */
6665 linebuffer_init (&path
);
6666 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
6667 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
6672 #endif /* not MSDOS */
6673 #endif /* not HAVE_GETCWD */
6676 /* Return a newly allocated string containing the file name of FILE
6677 relative to the absolute directory DIR (which should end with a slash). */
6679 relative_filename (file
, dir
)
6682 char *fp
, *dp
, *afn
, *res
;
6685 /* Find the common root of file and dir (with a trailing slash). */
6686 afn
= absolute_filename (file
, cwd
);
6689 while (*fp
++ == *dp
++)
6691 fp
--, dp
--; /* back to the first differing char */
6693 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
6696 do /* look at the equal chars until '/' */
6700 /* Build a sequence of "../" strings for the resulting relative file name. */
6702 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
6704 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
6707 strcat (res
, "../");
6709 /* Add the file name relative to the common root of file and dir. */
6710 strcat (res
, fp
+ 1);
6716 /* Return a newly allocated string containing the absolute file name
6717 of FILE given DIR (which should end with a slash). */
6719 absolute_filename (file
, dir
)
6722 char *slashp
, *cp
, *res
;
6724 if (filename_is_absolute (file
))
6725 res
= savestr (file
);
6727 /* We don't support non-absolute file names with a drive
6728 letter, like `d:NAME' (it's too much hassle). */
6729 else if (file
[1] == ':')
6730 fatal ("%s: relative file names with drive letters not supported", file
);
6733 res
= concat (dir
, file
, "");
6735 /* Delete the "/dirname/.." and "/." substrings. */
6736 slashp
= etags_strchr (res
, '/');
6737 while (slashp
!= NULL
&& slashp
[0] != '\0')
6739 if (slashp
[1] == '.')
6741 if (slashp
[2] == '.'
6742 && (slashp
[3] == '/' || slashp
[3] == '\0'))
6747 while (cp
>= res
&& !filename_is_absolute (cp
));
6749 cp
= slashp
; /* the absolute name begins with "/.." */
6751 /* Under MSDOS and NT we get `d:/NAME' as absolute
6752 file name, so the luser could say `d:/../NAME'.
6753 We silently treat this as `d:/NAME'. */
6754 else if (cp
[0] != '/')
6757 strcpy (cp
, slashp
+ 3);
6761 else if (slashp
[2] == '/' || slashp
[2] == '\0')
6763 strcpy (slashp
, slashp
+ 2);
6768 slashp
= etags_strchr (slashp
+ 1, '/');
6771 if (res
[0] == '\0') /* just a safety net: should never happen */
6774 return savestr ("/");
6780 /* Return a newly allocated string containing the absolute
6781 file name of dir where FILE resides given DIR (which should
6782 end with a slash). */
6784 absolute_dirname (file
, dir
)
6790 canonicalize_filename (file
);
6791 slashp
= etags_strrchr (file
, '/');
6793 return savestr (dir
);
6796 res
= absolute_filename (file
, dir
);
6802 /* Whether the argument string is an absolute file name. The argument
6803 string must have been canonicalized with canonicalize_filename. */
6805 filename_is_absolute (fn
)
6808 return (fn
[0] == '/'
6810 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
6815 /* Translate backslashes into slashes. Works in place. */
6817 canonicalize_filename (fn
)
6821 /* Canonicalize drive letter case. */
6822 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
6823 fn
[0] = upcase (fn
[0]);
6824 /* Convert backslashes to slashes. */
6825 for (; *fn
!= '\0'; fn
++)
6830 fn
= NULL
; /* shut up the compiler */
6835 /* Initialize a linebuffer for use */
6837 linebuffer_init (lbp
)
6840 lbp
->size
= (DEBUG
) ? 3 : 200;
6841 lbp
->buffer
= xnew (lbp
->size
, char);
6842 lbp
->buffer
[0] = '\0';
6846 /* Set the minimum size of a string contained in a linebuffer. */
6848 linebuffer_setlen (lbp
, toksize
)
6852 while (lbp
->size
<= toksize
)
6855 xrnew (lbp
->buffer
, lbp
->size
, char);
6860 /* Like malloc but get fatal error if memory is exhausted. */
6865 PTR result
= (PTR
) malloc (size
);
6867 fatal ("virtual memory exhausted", (char *)NULL
);
6872 xrealloc (ptr
, size
)
6876 PTR result
= (PTR
) realloc (ptr
, size
);
6878 fatal ("virtual memory exhausted", (char *)NULL
);
6884 * indent-tabs-mode: t
6887 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
6891 /* arch-tag: 8a9b748d-390c-4922-99db-2eeefa921051
6892 (do not change this comment) */
6894 /* etags.c ends here */