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.26";
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 no_duplicates
; /* no duplicate tags for ctags (undocumented) */
460 static bool update
; /* -u: update tags */
461 static bool vgrind_style
; /* -v: create vgrind style index output */
462 static bool no_warnings
; /* -w: suppress warnings (undocumented) */
463 static bool cxref_style
; /* -x: create cxref style output */
464 static bool cplusplus
; /* .[hc] means C++, not C */
465 static bool ignoreindent
; /* -I: ignore indentation in C */
466 static bool packages_only
; /* --packages-only: in Ada, only tag packages*/
468 /* STDIN is defined in LynxOS system headers */
473 #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
474 static bool parsing_stdin
; /* --parse-stdin used */
476 static regexp
*p_head
; /* list of all regexps */
477 static bool need_filebuf
; /* some regexes are multi-line */
479 static struct option longopts
[] =
481 { "append", no_argument
, NULL
, 'a' },
482 { "packages-only", no_argument
, &packages_only
, TRUE
},
483 { "c++", no_argument
, NULL
, 'C' },
484 { "declarations", no_argument
, &declarations
, TRUE
},
485 { "no-line-directive", no_argument
, &no_line_directive
, TRUE
},
486 { "no-duplicates", no_argument
, &no_duplicates
, TRUE
},
487 { "help", no_argument
, NULL
, 'h' },
488 { "help", no_argument
, NULL
, 'H' },
489 { "ignore-indentation", no_argument
, NULL
, 'I' },
490 { "language", required_argument
, NULL
, 'l' },
491 { "members", no_argument
, &members
, TRUE
},
492 { "no-members", no_argument
, &members
, FALSE
},
493 { "output", required_argument
, NULL
, 'o' },
494 { "regex", required_argument
, NULL
, 'r' },
495 { "no-regex", no_argument
, NULL
, 'R' },
496 { "ignore-case-regex", required_argument
, NULL
, 'c' },
497 { "parse-stdin", required_argument
, NULL
, STDIN
},
498 { "version", no_argument
, NULL
, 'V' },
500 #if CTAGS /* Ctags options */
501 { "backward-search", no_argument
, NULL
, 'B' },
502 { "cxref", no_argument
, NULL
, 'x' },
503 { "defines", no_argument
, NULL
, 'd' },
504 { "globals", no_argument
, &globals
, TRUE
},
505 { "typedefs", no_argument
, NULL
, 't' },
506 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
507 { "update", no_argument
, NULL
, 'u' },
508 { "vgrind", no_argument
, NULL
, 'v' },
509 { "no-warn", no_argument
, NULL
, 'w' },
511 #else /* Etags options */
512 { "no-defines", no_argument
, NULL
, 'D' },
513 { "no-globals", no_argument
, &globals
, FALSE
},
514 { "include", required_argument
, NULL
, 'i' },
519 static compressor compressors
[] =
521 { "z", "gzip -d -c"},
522 { "Z", "gzip -d -c"},
523 { "gz", "gzip -d -c"},
524 { "GZ", "gzip -d -c"},
525 { "bz2", "bzip2 -d -c" },
534 static char *Ada_suffixes
[] =
535 { "ads", "adb", "ada", NULL
};
536 static char Ada_help
[] =
537 "In Ada code, functions, procedures, packages, tasks and types are\n\
538 tags. Use the `--packages-only' option to create tags for\n\
540 Ada tag names have suffixes indicating the type of entity:\n\
541 Entity type: Qualifier:\n\
542 ------------ ----------\n\
549 Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
550 body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
551 will just search for any tag `bidule'.";
554 static char *Asm_suffixes
[] =
555 { "a", /* Unix assembler */
556 "asm", /* Microcontroller assembly */
557 "def", /* BSO/Tasking definition includes */
558 "inc", /* Microcontroller include files */
559 "ins", /* Microcontroller include files */
560 "s", "sa", /* Unix assembler */
561 "S", /* cpp-processed Unix assembler */
562 "src", /* BSO/Tasking C compiler output */
565 static char Asm_help
[] =
566 "In assembler code, labels appearing at the beginning of a line,\n\
567 followed by a colon, are tags.";
570 /* Note that .c and .h can be considered C++, if the --c++ flag was
571 given, or if the `class' or `template' keyowrds are met inside the file.
572 That is why default_C_entries is called for these. */
573 static char *default_C_suffixes
[] =
575 static char default_C_help
[] =
576 "In C code, any C function or typedef is a tag, and so are\n\
577 definitions of `struct', `union' and `enum'. `#define' macro\n\
578 definitions and `enum' constants are tags unless you specify\n\
579 `--no-defines'. Global variables are tags unless you specify\n\
580 `--no-globals'. Use of `--no-globals' and `--no-defines'\n\
581 can make the tags table file much smaller.\n\
582 You can tag function declarations and external variables by\n\
583 using `--declarations', and struct members by using `--members'.";
585 static char *Cplusplus_suffixes
[] =
586 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
587 "M", /* Objective C++ */
588 "pdb", /* Postscript with C syntax */
590 static char Cplusplus_help
[] =
591 "In C++ code, all the tag constructs of C code are tagged. (Use\n\
592 --help --lang=c --lang=c++ for full help.)\n\
593 In addition to C tags, member functions are also recognized, and\n\
594 optionally member variables if you use the `--members' option.\n\
595 Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
596 and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
599 static char *Cjava_suffixes
[] =
601 static char Cjava_help
[] =
602 "In Java code, all the tags constructs of C and C++ code are\n\
603 tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
606 static char *Cobol_suffixes
[] =
607 { "COB", "cob", NULL
};
608 static char Cobol_help
[] =
609 "In Cobol code, tags are paragraph names; that is, any word\n\
610 starting in column 8 and followed by a period.";
612 static char *Cstar_suffixes
[] =
613 { "cs", "hs", NULL
};
615 static char *Erlang_suffixes
[] =
616 { "erl", "hrl", NULL
};
617 static char Erlang_help
[] =
618 "In Erlang code, the tags are the functions, records and macros\n\
619 defined in the file.";
621 char *Forth_suffixes
[] =
622 { "fth", "tok", NULL
};
623 static char Forth_help
[] =
624 "In Forth code, tags are words defined by `:',\n\
625 constant, code, create, defer, value, variable, buffer:, field.";
627 static char *Fortran_suffixes
[] =
628 { "F", "f", "f90", "for", NULL
};
629 static char Fortran_help
[] =
630 "In Fortran code, functions, subroutines and block data are tags.";
632 static char *HTML_suffixes
[] =
633 { "htm", "html", "shtml", NULL
};
634 static char HTML_help
[] =
635 "In HTML input files, the tags are the `title' and the `h1', `h2',\n\
636 `h3' headers. Also, tags are `name=' in anchors and all\n\
637 occurrences of `id='.";
639 static char *Lisp_suffixes
[] =
640 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
641 static char Lisp_help
[] =
642 "In Lisp code, any function defined with `defun', any variable\n\
643 defined with `defvar' or `defconst', and in general the first\n\
644 argument of any expression that starts with `(def' in column zero\n\
647 static char *Lua_suffixes
[] =
648 { "lua", "LUA", NULL
};
649 static char Lua_help
[] =
650 "In Lua scripts, all functions are tags.";
652 static char *Makefile_filenames
[] =
653 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
654 static char Makefile_help
[] =
655 "In makefiles, targets are tags; additionally, variables are tags\n\
656 unless you specify `--no-globals'.";
658 static char *Objc_suffixes
[] =
659 { "lm", /* Objective lex file */
660 "m", /* Objective C file */
662 static char Objc_help
[] =
663 "In Objective C code, tags include Objective C definitions for classes,\n\
664 class categories, methods and protocols. Tags for variables and\n\
665 functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.\n\
666 (Use --help --lang=c --lang=objc --lang=java for full help.)";
668 static char *Pascal_suffixes
[] =
669 { "p", "pas", NULL
};
670 static char Pascal_help
[] =
671 "In Pascal code, the tags are the functions and procedures defined\n\
673 /* " // this is for working around an Emacs highlighting bug... */
675 static char *Perl_suffixes
[] =
676 { "pl", "pm", NULL
};
677 static char *Perl_interpreters
[] =
678 { "perl", "@PERL@", NULL
};
679 static char Perl_help
[] =
680 "In Perl code, the tags are the packages, subroutines and variables\n\
681 defined by the `package', `sub', `my' and `local' keywords. Use\n\
682 `--globals' if you want to tag global variables. Tags for\n\
683 subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
684 defined in the default package is `main::SUB'.";
686 static char *PHP_suffixes
[] =
687 { "php", "php3", "php4", NULL
};
688 static char PHP_help
[] =
689 "In PHP code, tags are functions, classes and defines. When using\n\
690 the `--members' option, vars are tags too.";
692 static char *plain_C_suffixes
[] =
693 { "pc", /* Pro*C file */
696 static char *PS_suffixes
[] =
697 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
698 static char PS_help
[] =
699 "In PostScript code, the tags are the functions.";
701 static char *Prolog_suffixes
[] =
703 static char Prolog_help
[] =
704 "In Prolog code, tags are predicates and rules at the beginning of\n\
707 static char *Python_suffixes
[] =
709 static char Python_help
[] =
710 "In Python code, `def' or `class' at the beginning of a line\n\
713 /* Can't do the `SCM' or `scm' prefix with a version number. */
714 static char *Scheme_suffixes
[] =
715 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
716 static char Scheme_help
[] =
717 "In Scheme code, tags include anything defined with `def' or with a\n\
718 construct whose name starts with `def'. They also include\n\
719 variables set with `set!' at top level in the file.";
721 static char *TeX_suffixes
[] =
722 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
723 static char TeX_help
[] =
724 "In LaTeX text, the argument of any of the commands `\\chapter',\n\
725 `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
726 `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
727 `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
728 `\\newenvironment' or `\\renewenvironment' is a tag.\n\
730 Other commands can be specified by setting the environment variable\n\
731 `TEXTAGS' to a colon-separated list like, for example,\n\
732 TEXTAGS=\"mycommand:myothercommand\".";
735 static char *Texinfo_suffixes
[] =
736 { "texi", "texinfo", "txi", NULL
};
737 static char Texinfo_help
[] =
738 "for texinfo files, lines starting with @node are tagged.";
740 static char *Yacc_suffixes
[] =
741 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
742 static char Yacc_help
[] =
743 "In Bison or Yacc input files, each rule defines as a tag the\n\
744 nonterminal it constructs. The portions of the file that contain\n\
745 C code are parsed as C code (use --help --lang=c --lang=yacc\n\
748 static char auto_help
[] =
749 "`auto' is not a real language, it indicates to use\n\
750 a default language for files base on file name suffix and file contents.";
752 static char none_help
[] =
753 "`none' is not a real language, it indicates to only do\n\
754 regexp processing on files.";
756 static char no_lang_help
[] =
757 "No detailed help available for this language.";
761 * Table of languages.
763 * It is ok for a given function to be listed under more than one
764 * name. I just didn't.
767 static language lang_names
[] =
769 { "ada", Ada_help
, Ada_funcs
, Ada_suffixes
},
770 { "asm", Asm_help
, Asm_labels
, Asm_suffixes
},
771 { "c", default_C_help
, default_C_entries
, default_C_suffixes
},
772 { "c++", Cplusplus_help
, Cplusplus_entries
, Cplusplus_suffixes
},
773 { "c*", no_lang_help
, Cstar_entries
, Cstar_suffixes
},
774 { "cobol", Cobol_help
, Cobol_paragraphs
, Cobol_suffixes
},
775 { "erlang", Erlang_help
, Erlang_functions
, Erlang_suffixes
},
776 { "forth", Forth_help
, Forth_words
, Forth_suffixes
},
777 { "fortran", Fortran_help
, Fortran_functions
, Fortran_suffixes
},
778 { "html", HTML_help
, HTML_labels
, HTML_suffixes
},
779 { "java", Cjava_help
, Cjava_entries
, Cjava_suffixes
},
780 { "lisp", Lisp_help
, Lisp_functions
, Lisp_suffixes
},
781 { "lua", Lua_help
, Lua_functions
, Lua_suffixes
},
782 { "makefile", Makefile_help
,Makefile_targets
,NULL
,Makefile_filenames
},
783 { "objc", Objc_help
, plain_C_entries
, Objc_suffixes
},
784 { "pascal", Pascal_help
, Pascal_functions
, Pascal_suffixes
},
785 { "perl",Perl_help
,Perl_functions
,Perl_suffixes
,NULL
,Perl_interpreters
},
786 { "php", PHP_help
, PHP_functions
, PHP_suffixes
},
787 { "postscript",PS_help
, PS_functions
, PS_suffixes
},
788 { "proc", no_lang_help
, plain_C_entries
, plain_C_suffixes
},
789 { "prolog", Prolog_help
, Prolog_functions
, Prolog_suffixes
},
790 { "python", Python_help
, Python_functions
, Python_suffixes
},
791 { "scheme", Scheme_help
, Scheme_functions
, Scheme_suffixes
},
792 { "tex", TeX_help
, TeX_commands
, TeX_suffixes
},
793 { "texinfo", Texinfo_help
, Texinfo_nodes
, Texinfo_suffixes
},
794 { "yacc", Yacc_help
,Yacc_entries
,Yacc_suffixes
,NULL
,NULL
,TRUE
},
795 { "auto", auto_help
}, /* default guessing scheme */
796 { "none", none_help
, just_read_file
}, /* regexp matching only */
797 { NULL
} /* end of list */
802 print_language_names ()
807 puts ("\nThese are the currently supported languages, along with the\n\
808 default file names and dot suffixes:");
809 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
811 printf (" %-*s", 10, lang
->name
);
812 if (lang
->filenames
!= NULL
)
813 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
814 printf (" %s", *name
);
815 if (lang
->suffixes
!= NULL
)
816 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
817 printf (" .%s", *ext
);
820 puts ("where `auto' means use default language for files based on file\n\
821 name suffix, and `none' means only do regexp processing on files.\n\
822 If no language is specified and no matching suffix is found,\n\
823 the first line of the file is read for a sharp-bang (#!) sequence\n\
824 followed by the name of an interpreter. If no such sequence is found,\n\
825 Fortran is tried first; if no tags are found, C is tried next.\n\
826 When parsing any C file, a \"class\" or \"template\" keyword\n\
828 puts ("Compressed files are supported using gzip and bzip2.\n\
830 For detailed help on a given language use, for example,\n\
831 etags --help --lang=ada.");
835 # define EMACS_NAME "standalone"
838 # define VERSION "version"
843 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
844 puts ("Copyright (C) 2006 Free Software Foundation, Inc. and Ken Arnold");
845 puts ("This program is distributed under the same terms as Emacs");
851 print_help (argbuffer
)
854 bool help_for_lang
= FALSE
;
856 for (; argbuffer
->arg_type
!= at_end
; argbuffer
++)
857 if (argbuffer
->arg_type
== at_language
)
861 puts (argbuffer
->lang
->help
);
862 help_for_lang
= TRUE
;
868 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
870 These are the options accepted by %s.\n", progname
, progname
);
872 puts ("WARNING: long option names do not work with this executable,\n\
873 as it is not linked with GNU getopt.");
875 puts ("You may use unambiguous abbreviations for the long option names.");
876 puts (" A - as file name means read names from stdin (one per line).\n\
877 Absolute names are stored in the output file as they are.\n\
878 Relative ones are stored relative to the output file's directory.\n");
880 puts ("-a, --append\n\
881 Append tag entries to existing tags file.");
883 puts ("--packages-only\n\
884 For Ada files, only generate tags for packages.");
887 puts ("-B, --backward-search\n\
888 Write the search commands for the tag entries using '?', the\n\
889 backward-search command instead of '/', the forward-search command.");
891 /* This option is mostly obsolete, because etags can now automatically
892 detect C++. Retained for backward compatibility and for debugging and
893 experimentation. In principle, we could want to tag as C++ even
894 before any "class" or "template" keyword.
896 Treat files whose name suffix defaults to C language as C++ files.");
899 puts ("--declarations\n\
900 In C and derived languages, create tags for function declarations,");
902 puts ("\tand create tags for extern variables if --globals is used.");
905 ("\tand create tags for extern variables unless --no-globals is used.");
908 puts ("-d, --defines\n\
909 Create tag entries for C #define constants and enum constants, too.");
911 puts ("-D, --no-defines\n\
912 Don't create tag entries for C #define constants and enum constants.\n\
913 This makes the tags file smaller.");
916 puts ("-i FILE, --include=FILE\n\
917 Include a note in tag file indicating that, when searching for\n\
918 a tag, one should also consult the tags file FILE after\n\
919 checking the current file.");
921 puts ("-l LANG, --language=LANG\n\
922 Force the following files to be considered as written in the\n\
923 named language up to the next --language=LANG option.");
927 Create tag entries for global variables in some languages.");
929 puts ("--no-globals\n\
930 Do not create tag entries for global variables in some\n\
931 languages. This makes the tags file smaller.");
933 Create tag entries for members of structures in some languages.");
935 puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
936 Make a tag for each line matching a regular expression pattern\n\
937 in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
938 files only. REGEXFILE is a file containing one REGEXP per line.\n\
939 REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
940 optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
941 puts (" If TAGNAME/ is present, the tags created are named.\n\
942 For example Tcl named tags can be created with:\n\
943 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
944 MODS are optional one-letter modifiers: `i' means to ignore case,\n\
945 `m' means to allow multi-line matches, `s' implies `m' and\n\
946 causes dot to match any character, including newline.");
947 puts ("-R, --no-regex\n\
948 Don't create tags from regexps for the following files.");
949 puts ("-I, --ignore-indentation\n\
950 In C and C++ do not assume that a closing brace in the first\n\
951 column is the final brace of a function or structure definition.");
952 puts ("-o FILE, --output=FILE\n\
953 Write the tags to FILE.");
954 puts ("--parse-stdin=NAME\n\
955 Read from standard input and record tags as belonging to file NAME.");
959 puts ("-t, --typedefs\n\
960 Generate tag entries for C and Ada typedefs.");
961 puts ("-T, --typedefs-and-c++\n\
962 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
963 and C++ member functions.");
967 puts ("-u, --update\n\
968 Update the tag entries for the given files, leaving tag\n\
969 entries for other files in place. Currently, this is\n\
970 implemented by deleting the existing entries for the given\n\
971 files and then rewriting the new entries at the end of the\n\
972 tags file. It is often faster to simply rebuild the entire\n\
973 tag file than to use this.");
977 puts ("-v, --vgrind\n\
978 Print on the standard output an index of items intended for\n\
979 human consumption, similar to the output of vgrind. The index\n\
980 is sorted, and gives the page number of each item.");
981 # if PRINT_UNDOCUMENTED_OPTIONS_HELP
982 puts ("-w, --no-duplicates\n\
983 Do not create duplicate tag entries, for compatibility with\n\
984 traditional ctags.");
985 puts ("-w, --no-warn\n\
986 Suppress warning messages about duplicate tag entries.");
987 # endif /* PRINT_UNDOCUMENTED_OPTIONS_HELP */
988 puts ("-x, --cxref\n\
989 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
990 The output uses line numbers instead of page numbers, but\n\
991 beyond that the differences are cosmetic; try both to see\n\
995 puts ("-V, --version\n\
996 Print the version of the program.\n\
998 Print this help message.\n\
999 Followed by one or more `--language' options prints detailed\n\
1000 help about tag generation for the specified languages.");
1002 print_language_names ();
1005 puts ("Report bugs to bug-gnu-emacs@gnu.org");
1007 exit (EXIT_SUCCESS
);
1011 #ifdef VMS /* VMS specific functions */
1015 /* This is a BUG! ANY arbitrary limit is a BUG!
1016 Won't someone please fix this? */
1017 #define MAX_FILE_SPEC_LEN 255
1020 char body
[MAX_FILE_SPEC_LEN
+ 1];
1024 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
1025 returning in each successive call the next file name matching the input
1026 spec. The function expects that each in_spec passed
1027 to it will be processed to completion; in particular, up to and
1028 including the call following that in which the last matching name
1029 is returned, the function ignores the value of in_spec, and will
1030 only start processing a new spec with the following call.
1031 If an error occurs, on return out_spec contains the value
1032 of in_spec when the error occurred.
1034 With each successive file name returned in out_spec, the
1035 function's return value is one. When there are no more matching
1036 names the function returns zero. If on the first call no file
1037 matches in_spec, or there is any other error, -1 is returned.
1041 #include <descrip.h>
1042 #define OUTSIZE MAX_FILE_SPEC_LEN
1048 static long context
= 0;
1049 static struct dsc$descriptor_s o
;
1050 static struct dsc$descriptor_s i
;
1051 static bool pass1
= TRUE
;
1058 o
.dsc$a_pointer
= (char *) out
;
1059 o
.dsc$w_length
= (short)OUTSIZE
;
1060 i
.dsc$a_pointer
= in
;
1061 i
.dsc$w_length
= (short)strlen(in
);
1062 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
1063 i
.dsc$b_class
= DSC$K_CLASS_S
;
1064 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
1065 o
.dsc$b_class
= DSC$K_CLASS_VS
;
1067 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
1069 out
->body
[out
->curlen
] = EOS
;
1072 else if (status
== RMS$_NMF
)
1076 strcpy(out
->body
, in
);
1079 lib$
find_file_end(&context
);
1085 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
1086 name of each file specified by the provided arg expanding wildcards.
1089 gfnames (arg
, p_error
)
1093 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
1095 switch (fn_exp (&filename
, arg
))
1099 return filename
.body
;
1105 return filename
.body
;
1109 #ifndef OLD /* Newer versions of VMS do provide `system'. */
1113 error ("%s", "system() function not implemented under VMS");
1117 #define VERSION_DELIM ';'
1118 char *massage_name (s
)
1124 if (*s
== VERSION_DELIM
)
1142 unsigned int nincluded_files
;
1143 char **included_files
;
1144 argument
*argbuffer
;
1145 int current_arg
, file_count
;
1146 linebuffer filename_lb
;
1147 bool help_asked
= FALSE
;
1156 _fmode
= O_BINARY
; /* all of files are treated as binary files */
1160 nincluded_files
= 0;
1161 included_files
= xnew (argc
, char *);
1165 /* Allocate enough no matter what happens. Overkill, but each one
1167 argbuffer
= xnew (argc
, argument
);
1170 * If etags, always find typedefs and structure tags. Why not?
1171 * Also default to find macro constants, enum constants and
1176 typedefs
= typedefs_or_cplusplus
= constantypedefs
= TRUE
;
1180 /* When the optstring begins with a '-' getopt_long does not rearrange the
1181 non-options arguments to be at the end, but leaves them alone. */
1182 optstring
= concat (NO_LONG_OPTIONS
? "" : "-",
1183 "ac:Cf:Il:o:r:RSVhH",
1184 (CTAGS
) ? "BxdtTuvw" : "Di:");
1186 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, NULL
)) != EOF
)
1190 /* If getopt returns 0, then it has already processed a
1191 long-named option. We should do nothing. */
1195 /* This means that a file name has been seen. Record it. */
1196 argbuffer
[current_arg
].arg_type
= at_filename
;
1197 argbuffer
[current_arg
].what
= optarg
;
1203 /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
1204 argbuffer
[current_arg
].arg_type
= at_stdin
;
1205 argbuffer
[current_arg
].what
= optarg
;
1209 fatal ("cannot parse standard input more than once", (char *)NULL
);
1210 parsing_stdin
= TRUE
;
1213 /* Common options. */
1214 case 'a': append_to_tagfile
= TRUE
; break;
1215 case 'C': cplusplus
= TRUE
; break;
1216 case 'f': /* for compatibility with old makefiles */
1220 error ("-o option may only be given once.", (char *)NULL
);
1221 suggest_asking_for_help ();
1227 case 'S': /* for backward compatibility */
1228 ignoreindent
= TRUE
;
1232 language
*lang
= get_language_from_langname (optarg
);
1235 argbuffer
[current_arg
].lang
= lang
;
1236 argbuffer
[current_arg
].arg_type
= at_language
;
1242 /* Backward compatibility: support obsolete --ignore-case-regexp. */
1243 optarg
= concat (optarg
, "i", ""); /* memory leak here */
1246 argbuffer
[current_arg
].arg_type
= at_regexp
;
1247 argbuffer
[current_arg
].what
= optarg
;
1251 argbuffer
[current_arg
].arg_type
= at_regexp
;
1252 argbuffer
[current_arg
].what
= NULL
;
1264 case 'D': constantypedefs
= FALSE
; break;
1265 case 'i': included_files
[nincluded_files
++] = optarg
; break;
1267 /* Ctags options. */
1268 case 'B': searchar
= '?'; break;
1269 case 'd': constantypedefs
= TRUE
; break;
1270 case 't': typedefs
= TRUE
; break;
1271 case 'T': typedefs
= typedefs_or_cplusplus
= TRUE
; break;
1272 case 'u': update
= TRUE
; break;
1273 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1274 case 'x': cxref_style
= TRUE
; break;
1275 case 'w': no_warnings
= TRUE
; break;
1277 suggest_asking_for_help ();
1281 /* No more options. Store the rest of arguments. */
1282 for (; optind
< argc
; optind
++)
1284 argbuffer
[current_arg
].arg_type
= at_filename
;
1285 argbuffer
[current_arg
].what
= argv
[optind
];
1290 argbuffer
[current_arg
].arg_type
= at_end
;
1293 print_help (argbuffer
);
1296 if (nincluded_files
== 0 && file_count
== 0)
1298 error ("no input files specified.", (char *)NULL
);
1299 suggest_asking_for_help ();
1303 if (tagfile
== NULL
)
1304 tagfile
= CTAGS
? "tags" : "TAGS";
1305 cwd
= etags_getcwd (); /* the current working directory */
1306 if (cwd
[strlen (cwd
) - 1] != '/')
1309 cwd
= concat (oldcwd
, "/", "");
1312 /* Relative file names are made relative to the current directory. */
1313 if (streq (tagfile
, "-")
1314 || strneq (tagfile
, "/dev/", 5))
1317 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1319 init (); /* set up boolean "functions" */
1321 linebuffer_init (&lb
);
1322 linebuffer_init (&filename_lb
);
1323 linebuffer_init (&filebuf
);
1324 linebuffer_init (&token_name
);
1328 if (streq (tagfile
, "-"))
1332 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1333 doesn't take effect until after `stdout' is already open). */
1334 if (!isatty (fileno (stdout
)))
1335 setmode (fileno (stdout
), O_BINARY
);
1339 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1345 * Loop through files finding functions.
1347 for (i
= 0; i
< current_arg
; i
++)
1349 static language
*lang
; /* non-NULL if language is forced */
1352 switch (argbuffer
[i
].arg_type
)
1355 lang
= argbuffer
[i
].lang
;
1358 analyse_regex (argbuffer
[i
].what
);
1362 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1366 error ("can't find file %s\n", this_file
);
1371 this_file
= massage_name (this_file
);
1374 this_file
= argbuffer
[i
].what
;
1376 /* Input file named "-" means read file names from stdin
1377 (one per line) and use them. */
1378 if (streq (this_file
, "-"))
1381 fatal ("cannot parse standard input AND read file names from it",
1383 while (readline_internal (&filename_lb
, stdin
) > 0)
1384 process_file_name (filename_lb
.buffer
, lang
);
1387 process_file_name (this_file
, lang
);
1393 this_file
= argbuffer
[i
].what
;
1394 process_file (stdin
, this_file
, lang
);
1401 free (filebuf
.buffer
);
1402 free (token_name
.buffer
);
1404 if (!CTAGS
|| cxref_style
)
1406 /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */
1407 put_entries (nodehead
);
1408 free_tree (nodehead
);
1414 /* Output file entries that have no tags. */
1415 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1417 fprintf (tagf
, "\f\n%s,0\n", fdp
->taggedfname
);
1419 while (nincluded_files
-- > 0)
1420 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1422 if (fclose (tagf
) == EOF
)
1426 exit (EXIT_SUCCESS
);
1432 for (i
= 0; i
< current_arg
; ++i
)
1434 switch (argbuffer
[i
].arg_type
)
1440 continue; /* the for loop */
1443 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1444 tagfile
, argbuffer
[i
].what
, tagfile
);
1445 if (system (cmd
) != EXIT_SUCCESS
)
1446 fatal ("failed to execute shell command", (char *)NULL
);
1448 append_to_tagfile
= TRUE
;
1451 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1454 put_entries (nodehead
); /* write all the tags (CTAGS) */
1455 free_tree (nodehead
);
1457 if (fclose (tagf
) == EOF
)
1461 if (append_to_tagfile
|| update
)
1463 char cmd
[2*BUFSIZ
+10];
1464 sprintf (cmd
, "sort -o %.*s %.*s", BUFSIZ
, tagfile
, BUFSIZ
, tagfile
);
1465 exit (system (cmd
));
1467 return EXIT_SUCCESS
;
1472 * Return a compressor given the file name. If EXTPTR is non-zero,
1473 * return a pointer into FILE where the compressor-specific
1474 * extension begins. If no compressor is found, NULL is returned
1475 * and EXTPTR is not significant.
1476 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1479 get_compressor_from_suffix (file
, extptr
)
1484 char *slash
, *suffix
;
1486 /* This relies on FN to be after canonicalize_filename,
1487 so we don't need to consider backslashes on DOS_NT. */
1488 slash
= etags_strrchr (file
, '/');
1489 suffix
= etags_strrchr (file
, '.');
1490 if (suffix
== NULL
|| suffix
< slash
)
1495 /* Let those poor souls who live with DOS 8+3 file name limits get
1496 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1497 Only the first do loop is run if not MSDOS */
1500 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1501 if (streq (compr
->suffix
, suffix
))
1504 break; /* do it only once: not really a loop */
1507 } while (*suffix
!= '\0');
1514 * Return a language given the name.
1517 get_language_from_langname (name
)
1523 error ("empty language name", (char *)NULL
);
1526 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1527 if (streq (name
, lang
->name
))
1529 error ("unknown language \"%s\"", name
);
1537 * Return a language given the interpreter name.
1540 get_language_from_interpreter (interpreter
)
1546 if (interpreter
== NULL
)
1548 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1549 if (lang
->interpreters
!= NULL
)
1550 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1551 if (streq (*iname
, interpreter
))
1560 * Return a language given the file name.
1563 get_language_from_filename (file
, case_sensitive
)
1565 bool case_sensitive
;
1568 char **name
, **ext
, *suffix
;
1570 /* Try whole file name first. */
1571 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1572 if (lang
->filenames
!= NULL
)
1573 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1574 if ((case_sensitive
)
1575 ? streq (*name
, file
)
1576 : strcaseeq (*name
, file
))
1579 /* If not found, try suffix after last dot. */
1580 suffix
= etags_strrchr (file
, '.');
1584 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1585 if (lang
->suffixes
!= NULL
)
1586 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1587 if ((case_sensitive
)
1588 ? streq (*ext
, suffix
)
1589 : strcaseeq (*ext
, suffix
))
1596 * This routine is called on each file argument.
1599 process_file_name (file
, lang
)
1603 struct stat stat_buf
;
1607 char *compressed_name
, *uncompressed_name
;
1608 char *ext
, *real_name
;
1611 canonicalize_filename (file
);
1612 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1614 error ("skipping inclusion of %s in self.", file
);
1617 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1619 compressed_name
= NULL
;
1620 real_name
= uncompressed_name
= savestr (file
);
1624 real_name
= compressed_name
= savestr (file
);
1625 uncompressed_name
= savenstr (file
, ext
- file
);
1628 /* If the canonicalized uncompressed name
1629 has already been dealt with, skip it silently. */
1630 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1632 assert (fdp
->infname
!= NULL
);
1633 if (streq (uncompressed_name
, fdp
->infname
))
1637 if (stat (real_name
, &stat_buf
) != 0)
1639 /* Reset real_name and try with a different name. */
1641 if (compressed_name
!= NULL
) /* try with the given suffix */
1643 if (stat (uncompressed_name
, &stat_buf
) == 0)
1644 real_name
= uncompressed_name
;
1646 else /* try all possible suffixes */
1648 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1650 compressed_name
= concat (file
, ".", compr
->suffix
);
1651 if (stat (compressed_name
, &stat_buf
) != 0)
1655 char *suf
= compressed_name
+ strlen (file
);
1656 size_t suflen
= strlen (compr
->suffix
) + 1;
1657 for ( ; suf
[1]; suf
++, suflen
--)
1659 memmove (suf
, suf
+ 1, suflen
);
1660 if (stat (compressed_name
, &stat_buf
) == 0)
1662 real_name
= compressed_name
;
1666 if (real_name
!= NULL
)
1669 free (compressed_name
);
1670 compressed_name
= NULL
;
1674 real_name
= compressed_name
;
1679 if (real_name
== NULL
)
1684 } /* try with a different name */
1686 if (!S_ISREG (stat_buf
.st_mode
))
1688 error ("skipping %s: it is not a regular file.", real_name
);
1691 if (real_name
== compressed_name
)
1693 char *cmd
= concat (compr
->command
, " ", real_name
);
1694 inf
= (FILE *) popen (cmd
, "r");
1698 inf
= fopen (real_name
, "r");
1705 process_file (inf
, uncompressed_name
, lang
);
1707 if (real_name
== compressed_name
)
1708 retval
= pclose (inf
);
1710 retval
= fclose (inf
);
1715 if (compressed_name
) free (compressed_name
);
1716 if (uncompressed_name
) free (uncompressed_name
);
1723 process_file (fh
, fn
, lang
)
1728 static const fdesc emptyfdesc
;
1731 /* Create a new input file description entry. */
1732 fdp
= xnew (1, fdesc
);
1735 fdp
->infname
= savestr (fn
);
1737 fdp
->infabsname
= absolute_filename (fn
, cwd
);
1738 fdp
->infabsdir
= absolute_dirname (fn
, cwd
);
1739 if (filename_is_absolute (fn
))
1741 /* An absolute file name. Canonicalize it. */
1742 fdp
->taggedfname
= absolute_filename (fn
, NULL
);
1746 /* A file name relative to cwd. Make it relative
1747 to the directory of the tags file. */
1748 fdp
->taggedfname
= relative_filename (fn
, tagfiledir
);
1750 fdp
->usecharno
= TRUE
; /* use char position when making tags */
1752 fdp
->written
= FALSE
; /* not written on tags file yet */
1755 curfdp
= fdhead
; /* the current file description */
1759 /* If not Ctags, and if this is not metasource and if it contained no #line
1760 directives, we can write the tags and free all nodes pointing to
1763 && curfdp
->usecharno
/* no #line directives in this file */
1764 && !curfdp
->lang
->metasource
)
1768 /* Look for the head of the sublist relative to this file. See add_node
1769 for the structure of the node tree. */
1771 for (np
= nodehead
; np
!= NULL
; prev
= np
, np
= np
->left
)
1772 if (np
->fdp
== curfdp
)
1775 /* If we generated tags for this file, write and delete them. */
1778 /* This is the head of the last sublist, if any. The following
1779 instructions depend on this being true. */
1780 assert (np
->left
== NULL
);
1782 assert (fdhead
== curfdp
);
1783 assert (last_node
->fdp
== curfdp
);
1784 put_entries (np
); /* write tags for file curfdp->taggedfname */
1785 free_tree (np
); /* remove the written nodes */
1787 nodehead
= NULL
; /* no nodes left */
1789 prev
->left
= NULL
; /* delete the pointer to the sublist */
1795 * This routine sets up the boolean pseudo-functions which work
1796 * by setting boolean flags dependent upon the corresponding character.
1797 * Every char which is NOT in that string is not a white char. Therefore,
1798 * all of the array "_wht" is set to FALSE, and then the elements
1799 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1800 * of a char is TRUE if it is the string "white", else FALSE.
1808 for (i
= 0; i
< CHARS
; i
++)
1809 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1810 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1811 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1812 notinname('\0') = notinname('\n');
1813 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1814 begtoken('\0') = begtoken('\n');
1815 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1816 intoken('\0') = intoken('\n');
1817 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1818 endtoken('\0') = endtoken('\n');
1822 * This routine opens the specified file and calls the function
1823 * which finds the function and type definitions.
1830 language
*lang
= curfdp
->lang
;
1831 Lang_function
*parser
= NULL
;
1833 /* If user specified a language, use it. */
1834 if (lang
!= NULL
&& lang
->function
!= NULL
)
1836 parser
= lang
->function
;
1839 /* Else try to guess the language given the file name. */
1842 lang
= get_language_from_filename (curfdp
->infname
, TRUE
);
1843 if (lang
!= NULL
&& lang
->function
!= NULL
)
1845 curfdp
->lang
= lang
;
1846 parser
= lang
->function
;
1850 /* Else look for sharp-bang as the first two characters. */
1852 && readline_internal (&lb
, inf
) > 0
1854 && lb
.buffer
[0] == '#'
1855 && lb
.buffer
[1] == '!')
1859 /* Set lp to point at the first char after the last slash in the
1860 line or, if no slashes, at the first nonblank. Then set cp to
1861 the first successive blank and terminate the string. */
1862 lp
= etags_strrchr (lb
.buffer
+2, '/');
1866 lp
= skip_spaces (lb
.buffer
+ 2);
1867 cp
= skip_non_spaces (lp
);
1870 if (strlen (lp
) > 0)
1872 lang
= get_language_from_interpreter (lp
);
1873 if (lang
!= NULL
&& lang
->function
!= NULL
)
1875 curfdp
->lang
= lang
;
1876 parser
= lang
->function
;
1881 /* We rewind here, even if inf may be a pipe. We fail if the
1882 length of the first line is longer than the pipe block size,
1883 which is unlikely. */
1886 /* Else try to guess the language given the case insensitive file name. */
1889 lang
= get_language_from_filename (curfdp
->infname
, FALSE
);
1890 if (lang
!= NULL
&& lang
->function
!= NULL
)
1892 curfdp
->lang
= lang
;
1893 parser
= lang
->function
;
1897 /* Else try Fortran or C. */
1900 node
*old_last_node
= last_node
;
1902 curfdp
->lang
= get_language_from_langname ("fortran");
1905 if (old_last_node
== last_node
)
1906 /* No Fortran entries found. Try C. */
1908 /* We do not tag if rewind fails.
1909 Only the file name will be recorded in the tags file. */
1911 curfdp
->lang
= get_language_from_langname (cplusplus
? "c++" : "c");
1917 if (!no_line_directive
1918 && curfdp
->lang
!= NULL
&& curfdp
->lang
->metasource
)
1919 /* It may be that this is a bingo.y file, and we already parsed a bingo.c
1920 file, or anyway we parsed a file that is automatically generated from
1921 this one. If this is the case, the bingo.c file contained #line
1922 directives that generated tags pointing to this file. Let's delete
1923 them all before parsing this file, which is the real source. */
1925 fdesc
**fdpp
= &fdhead
;
1926 while (*fdpp
!= NULL
)
1928 && streq ((*fdpp
)->taggedfname
, curfdp
->taggedfname
))
1929 /* We found one of those! We must delete both the file description
1930 and all tags referring to it. */
1932 fdesc
*badfdp
= *fdpp
;
1934 /* Delete the tags referring to badfdp->taggedfname
1935 that were obtained from badfdp->infname. */
1936 invalidate_nodes (badfdp
, &nodehead
);
1938 *fdpp
= badfdp
->next
; /* remove the bad description from the list */
1939 free_fdesc (badfdp
);
1942 fdpp
= &(*fdpp
)->next
; /* advance the list pointer */
1945 assert (parser
!= NULL
);
1947 /* Generic initialisations before reading from file. */
1948 linebuffer_setlen (&filebuf
, 0); /* reset the file buffer */
1950 /* Generic initialisations before parsing file with readline. */
1951 lineno
= 0; /* reset global line number */
1952 charno
= 0; /* reset global char number */
1953 linecharno
= 0; /* reset global char number of line start */
1957 regex_tag_multiline ();
1962 * Check whether an implicitly named tag should be created,
1963 * then call `pfnote'.
1964 * NAME is a string that is internally copied by this function.
1966 * TAGS format specification
1967 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1968 * The following is explained in some more detail in etc/ETAGS.EBNF.
1970 * make_tag creates tags with "implicit tag names" (unnamed tags)
1971 * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
1972 * 1. NAME does not contain any of the characters in NONAM;
1973 * 2. LINESTART contains name as either a rightmost, or rightmost but
1974 * one character, substring;
1975 * 3. the character, if any, immediately before NAME in LINESTART must
1976 * be a character in NONAM;
1977 * 4. the character, if any, immediately after NAME in LINESTART must
1978 * also be a character in NONAM.
1980 * The implementation uses the notinname() macro, which recognises the
1981 * characters stored in the string `nonam'.
1982 * etags.el needs to use the same characters that are in NONAM.
1985 make_tag (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1986 char *name
; /* tag name, or NULL if unnamed */
1987 int namelen
; /* tag length */
1988 bool is_func
; /* tag is a function */
1989 char *linestart
; /* start of the line where tag is */
1990 int linelen
; /* length of the line where tag is */
1991 int lno
; /* line number */
1992 long cno
; /* character number */
1994 bool named
= (name
!= NULL
&& namelen
> 0);
1996 if (!CTAGS
&& named
) /* maybe set named to false */
1997 /* Let's try to make an implicit tag name, that is, create an unnamed tag
1998 such that etags.el can guess a name from it. */
2001 register char *cp
= name
;
2003 for (i
= 0; i
< namelen
; i
++)
2004 if (notinname (*cp
++))
2006 if (i
== namelen
) /* rule #1 */
2008 cp
= linestart
+ linelen
- namelen
;
2009 if (notinname (linestart
[linelen
-1]))
2010 cp
-= 1; /* rule #4 */
2011 if (cp
>= linestart
/* rule #2 */
2013 || notinname (cp
[-1])) /* rule #3 */
2014 && strneq (name
, cp
, namelen
)) /* rule #2 */
2015 named
= FALSE
; /* use implicit tag name */
2020 name
= savenstr (name
, namelen
);
2023 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
2028 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
2029 char *name
; /* tag name, or NULL if unnamed */
2030 bool is_func
; /* tag is a function */
2031 char *linestart
; /* start of the line where tag is */
2032 int linelen
; /* length of the line where tag is */
2033 int lno
; /* line number */
2034 long cno
; /* character number */
2038 assert (name
== NULL
|| name
[0] != '\0');
2039 if (CTAGS
&& name
== NULL
)
2042 np
= xnew (1, node
);
2044 /* If ctags mode, change name "main" to M<thisfilename>. */
2045 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
2047 register char *fp
= etags_strrchr (curfdp
->taggedfname
, '/');
2048 np
->name
= concat ("M", fp
== NULL
? curfdp
->taggedfname
: fp
+ 1, "");
2049 fp
= etags_strrchr (np
->name
, '.');
2050 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
2056 np
->been_warned
= FALSE
;
2058 np
->is_func
= is_func
;
2060 if (np
->fdp
->usecharno
)
2061 /* Our char numbers are 0-base, because of C language tradition?
2062 ctags compatibility? old versions compatibility? I don't know.
2063 Anyway, since emacs's are 1-base we expect etags.el to take care
2064 of the difference. If we wanted to have 1-based numbers, we would
2065 uncomment the +1 below. */
2066 np
->cno
= cno
/* + 1 */ ;
2068 np
->cno
= invalidcharno
;
2069 np
->left
= np
->right
= NULL
;
2070 if (CTAGS
&& !cxref_style
)
2072 if (strlen (linestart
) < 50)
2073 np
->regex
= concat (linestart
, "$", "");
2075 np
->regex
= savenstr (linestart
, 50);
2078 np
->regex
= savenstr (linestart
, linelen
);
2080 add_node (np
, &nodehead
);
2085 * recurse on left children, iterate on right children.
2093 register node
*node_right
= np
->right
;
2094 free_tree (np
->left
);
2095 if (np
->name
!= NULL
)
2105 * delete a file description
2109 register fdesc
*fdp
;
2111 if (fdp
->infname
!= NULL
) free (fdp
->infname
);
2112 if (fdp
->infabsname
!= NULL
) free (fdp
->infabsname
);
2113 if (fdp
->infabsdir
!= NULL
) free (fdp
->infabsdir
);
2114 if (fdp
->taggedfname
!= NULL
) free (fdp
->taggedfname
);
2115 if (fdp
->prop
!= NULL
) free (fdp
->prop
);
2121 * Adds a node to the tree of nodes. In etags mode, sort by file
2122 * name. In ctags mode, sort by tag name. Make no attempt at
2125 * add_node is the only function allowed to add nodes, so it can
2129 add_node (np
, cur_node_p
)
2130 node
*np
, **cur_node_p
;
2133 register node
*cur_node
= *cur_node_p
;
2135 if (cur_node
== NULL
)
2145 /* For each file name, tags are in a linked sublist on the right
2146 pointer. The first tags of different files are a linked list
2147 on the left pointer. last_node points to the end of the last
2149 if (last_node
!= NULL
&& last_node
->fdp
== np
->fdp
)
2151 /* Let's use the same sublist as the last added node. */
2152 assert (last_node
->right
== NULL
);
2153 last_node
->right
= np
;
2156 else if (cur_node
->fdp
== np
->fdp
)
2158 /* Scanning the list we found the head of a sublist which is
2159 good for us. Let's scan this sublist. */
2160 add_node (np
, &cur_node
->right
);
2163 /* The head of this sublist is not good for us. Let's try the
2165 add_node (np
, &cur_node
->left
);
2166 } /* if ETAGS mode */
2171 dif
= strcmp (np
->name
, cur_node
->name
);
2174 * If this tag name matches an existing one, then
2175 * do not add the node, but maybe print a warning.
2177 if (no_duplicates
&& !dif
)
2179 if (np
->fdp
== cur_node
->fdp
)
2183 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
2184 np
->fdp
->infname
, lineno
, np
->name
);
2185 fprintf (stderr
, "Second entry ignored\n");
2188 else if (!cur_node
->been_warned
&& !no_warnings
)
2192 "Duplicate entry in files %s and %s: %s (Warning only)\n",
2193 np
->fdp
->infname
, cur_node
->fdp
->infname
, np
->name
);
2194 cur_node
->been_warned
= TRUE
;
2199 /* Actually add the node */
2200 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
2201 } /* if CTAGS mode */
2205 * invalidate_nodes ()
2206 * Scan the node tree and invalidate all nodes pointing to the
2207 * given file description (CTAGS case) or free them (ETAGS case).
2210 invalidate_nodes (badfdp
, npp
)
2221 if (np
->left
!= NULL
)
2222 invalidate_nodes (badfdp
, &np
->left
);
2223 if (np
->fdp
== badfdp
)
2225 if (np
->right
!= NULL
)
2226 invalidate_nodes (badfdp
, &np
->right
);
2230 assert (np
->fdp
!= NULL
);
2231 if (np
->fdp
== badfdp
)
2233 *npp
= np
->left
; /* detach the sublist from the list */
2234 np
->left
= NULL
; /* isolate it */
2235 free_tree (np
); /* free it */
2236 invalidate_nodes (badfdp
, npp
);
2239 invalidate_nodes (badfdp
, &np
->left
);
2244 static int total_size_of_entries
__P((node
*));
2245 static int number_len
__P((long));
2247 /* Length of a non-negative number's decimal representation. */
2253 while ((num
/= 10) > 0)
2259 * Return total number of characters that put_entries will output for
2260 * the nodes in the linked list at the right of the specified node.
2261 * This count is irrelevant with etags.el since emacs 19.34 at least,
2262 * but is still supplied for backward compatibility.
2265 total_size_of_entries (np
)
2268 register int total
= 0;
2270 for (; np
!= NULL
; np
= np
->right
)
2273 total
+= strlen (np
->regex
) + 1; /* pat\177 */
2274 if (np
->name
!= NULL
)
2275 total
+= strlen (np
->name
) + 1; /* name\001 */
2276 total
+= number_len ((long) np
->lno
) + 1; /* lno, */
2277 if (np
->cno
!= invalidcharno
) /* cno */
2278 total
+= number_len (np
->cno
);
2279 total
+= 1; /* newline */
2290 static fdesc
*fdp
= NULL
;
2295 /* Output subentries that precede this one */
2297 put_entries (np
->left
);
2299 /* Output this entry */
2308 fprintf (tagf
, "\f\n%s,%d\n",
2309 fdp
->taggedfname
, total_size_of_entries (np
));
2310 fdp
->written
= TRUE
;
2312 fputs (np
->regex
, tagf
);
2313 fputc ('\177', tagf
);
2314 if (np
->name
!= NULL
)
2316 fputs (np
->name
, tagf
);
2317 fputc ('\001', tagf
);
2319 fprintf (tagf
, "%d,", np
->lno
);
2320 if (np
->cno
!= invalidcharno
)
2321 fprintf (tagf
, "%ld", np
->cno
);
2327 if (np
->name
== NULL
)
2328 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
2333 fprintf (stdout
, "%s %s %d\n",
2334 np
->name
, np
->fdp
->taggedfname
, (np
->lno
+ 63) / 64);
2336 fprintf (stdout
, "%-16s %3d %-16s %s\n",
2337 np
->name
, np
->lno
, np
->fdp
->taggedfname
, np
->regex
);
2341 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->fdp
->taggedfname
);
2344 { /* function or #define macro with args */
2345 putc (searchar
, tagf
);
2348 for (sp
= np
->regex
; *sp
; sp
++)
2350 if (*sp
== '\\' || *sp
== searchar
)
2354 putc (searchar
, tagf
);
2357 { /* anything else; text pattern inadequate */
2358 fprintf (tagf
, "%d", np
->lno
);
2363 } /* if this node contains a valid tag */
2365 /* Output subentries that follow this one */
2366 put_entries (np
->right
);
2368 put_entries (np
->left
);
2373 #define C_EXT 0x00fff /* C extensions */
2374 #define C_PLAIN 0x00000 /* C */
2375 #define C_PLPL 0x00001 /* C++ */
2376 #define C_STAR 0x00003 /* C* */
2377 #define C_JAVA 0x00005 /* JAVA */
2378 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
2379 #define YACC 0x10000 /* yacc file */
2382 * The C symbol tables.
2387 st_C_objprot
, st_C_objimpl
, st_C_objend
,
2389 st_C_ignore
, st_C_attribute
,
2392 st_C_class
, st_C_template
,
2393 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
2396 static unsigned int hash
__P((const char *, unsigned int));
2397 static struct C_stab_entry
* in_word_set
__P((const char *, unsigned int));
2398 static enum sym_type C_symtype
__P((char *, int, int));
2400 /* Feed stuff between (but not including) %[ and %] lines to:
2406 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2410 while, 0, st_C_ignore
2411 switch, 0, st_C_ignore
2412 return, 0, st_C_ignore
2413 __attribute__, 0, st_C_attribute
2414 @interface, 0, st_C_objprot
2415 @protocol, 0, st_C_objprot
2416 @implementation,0, st_C_objimpl
2417 @end, 0, st_C_objend
2418 import, (C_JAVA & !C_PLPL), st_C_ignore
2419 package, (C_JAVA & !C_PLPL), st_C_ignore
2420 friend, C_PLPL, st_C_ignore
2421 extends, (C_JAVA & !C_PLPL), st_C_javastruct
2422 implements, (C_JAVA & !C_PLPL), st_C_javastruct
2423 interface, (C_JAVA & !C_PLPL), st_C_struct
2424 class, 0, st_C_class
2425 namespace, C_PLPL, st_C_struct
2426 domain, C_STAR, st_C_struct
2427 union, 0, st_C_struct
2428 struct, 0, st_C_struct
2429 extern, 0, st_C_extern
2431 typedef, 0, st_C_typedef
2432 define, 0, st_C_define
2433 undef, 0, st_C_define
2434 operator, C_PLPL, st_C_operator
2435 template, 0, st_C_template
2436 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2437 DEFUN, 0, st_C_gnumacro
2438 SYSCALL, 0, st_C_gnumacro
2439 ENTRY, 0, st_C_gnumacro
2440 PSEUDO, 0, st_C_gnumacro
2441 # These are defined inside C functions, so currently they are not met.
2442 # EXFUN used in glibc, DEFVAR_* in emacs.
2443 #EXFUN, 0, st_C_gnumacro
2444 #DEFVAR_, 0, st_C_gnumacro
2446 and replace lines between %< and %> with its output, then:
2447 - remove the #if characterset check
2448 - make in_word_set static and not inline. */
2450 /* C code produced by gperf version 3.0.1 */
2451 /* Command-line: gperf -m 5 */
2452 /* Computed positions: -k'2-3' */
2454 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
2455 /* maximum key range = 33, duplicates = 0 */
2466 register const char *str
;
2467 register unsigned int len
;
2469 static unsigned char asso_values
[] =
2471 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2472 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2473 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2474 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2475 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2476 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2477 35, 35, 35, 35, 35, 35, 35, 35, 35, 15,
2478 14, 35, 35, 35, 35, 35, 35, 35, 14, 35,
2479 35, 35, 35, 12, 13, 35, 35, 35, 35, 12,
2480 35, 35, 35, 35, 35, 1, 35, 16, 35, 6,
2481 23, 0, 0, 35, 22, 0, 35, 35, 5, 0,
2482 0, 15, 1, 35, 6, 35, 8, 19, 35, 16,
2483 4, 5, 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, 35, 35, 35, 35,
2491 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2492 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2493 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2494 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2495 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2496 35, 35, 35, 35, 35, 35
2498 register int hval
= len
;
2503 hval
+= asso_values
[(unsigned char)str
[2]];
2506 hval
+= asso_values
[(unsigned char)str
[1]];
2512 static struct C_stab_entry
*
2513 in_word_set (str
, len
)
2514 register const char *str
;
2515 register unsigned int len
;
2519 TOTAL_KEYWORDS
= 32,
2520 MIN_WORD_LENGTH
= 2,
2521 MAX_WORD_LENGTH
= 15,
2526 static struct C_stab_entry wordlist
[] =
2529 {"if", 0, st_C_ignore
},
2531 {"@end", 0, st_C_objend
},
2532 {"union", 0, st_C_struct
},
2533 {"define", 0, st_C_define
},
2534 {"import", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2535 {"template", 0, st_C_template
},
2536 {"operator", C_PLPL
, st_C_operator
},
2537 {"@interface", 0, st_C_objprot
},
2538 {"implements", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2539 {"friend", C_PLPL
, st_C_ignore
},
2540 {"typedef", 0, st_C_typedef
},
2541 {"return", 0, st_C_ignore
},
2542 {"@implementation",0, st_C_objimpl
},
2543 {"@protocol", 0, st_C_objprot
},
2544 {"interface", (C_JAVA
& !C_PLPL
), st_C_struct
},
2545 {"extern", 0, st_C_extern
},
2546 {"extends", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2547 {"struct", 0, st_C_struct
},
2548 {"domain", C_STAR
, st_C_struct
},
2549 {"switch", 0, st_C_ignore
},
2550 {"enum", 0, st_C_enum
},
2551 {"for", 0, st_C_ignore
},
2552 {"namespace", C_PLPL
, st_C_struct
},
2553 {"class", 0, st_C_class
},
2554 {"while", 0, st_C_ignore
},
2555 {"undef", 0, st_C_define
},
2556 {"package", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2557 {"__attribute__", 0, st_C_attribute
},
2558 {"SYSCALL", 0, st_C_gnumacro
},
2559 {"ENTRY", 0, st_C_gnumacro
},
2560 {"PSEUDO", 0, st_C_gnumacro
},
2561 {"DEFUN", 0, st_C_gnumacro
}
2564 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2566 register int key
= hash (str
, len
);
2568 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2570 register const char *s
= wordlist
[key
].name
;
2572 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1) && s
[len
] == '\0')
2573 return &wordlist
[key
];
2580 static enum sym_type
2581 C_symtype (str
, len
, c_ext
)
2586 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2588 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2595 * Ignoring __attribute__ ((list))
2597 static bool inattribute
; /* looking at an __attribute__ construct */
2600 * C functions and variables are recognized using a simple
2601 * finite automaton. fvdef is its state variable.
2605 fvnone
, /* nothing seen */
2606 fdefunkey
, /* Emacs DEFUN keyword seen */
2607 fdefunname
, /* Emacs DEFUN name seen */
2608 foperator
, /* func: operator keyword seen (cplpl) */
2609 fvnameseen
, /* function or variable name seen */
2610 fstartlist
, /* func: just after open parenthesis */
2611 finlist
, /* func: in parameter list */
2612 flistseen
, /* func: after parameter list */
2613 fignore
, /* func: before open brace */
2614 vignore
/* var-like: ignore until ';' */
2617 static bool fvextern
; /* func or var: extern keyword seen; */
2620 * typedefs are recognized using a simple finite automaton.
2621 * typdef is its state variable.
2625 tnone
, /* nothing seen */
2626 tkeyseen
, /* typedef keyword seen */
2627 ttypeseen
, /* defined type seen */
2628 tinbody
, /* inside typedef body */
2629 tend
, /* just before typedef tag */
2630 tignore
/* junk after typedef tag */
2634 * struct-like structures (enum, struct and union) are recognized
2635 * using another simple finite automaton. `structdef' is its state
2640 snone
, /* nothing seen yet,
2641 or in struct body if bracelev > 0 */
2642 skeyseen
, /* struct-like keyword seen */
2643 stagseen
, /* struct-like tag seen */
2644 scolonseen
/* colon seen after struct-like tag */
2648 * When objdef is different from onone, objtag is the name of the class.
2650 static char *objtag
= "<uninited>";
2653 * Yet another little state machine to deal with preprocessor lines.
2657 dnone
, /* nothing seen */
2658 dsharpseen
, /* '#' seen as first char on line */
2659 ddefineseen
, /* '#' and 'define' seen */
2660 dignorerest
/* ignore rest of line */
2664 * State machine for Objective C protocols and implementations.
2665 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2669 onone
, /* nothing seen */
2670 oprotocol
, /* @interface or @protocol seen */
2671 oimplementation
, /* @implementations seen */
2672 otagseen
, /* class name seen */
2673 oparenseen
, /* parenthesis before category seen */
2674 ocatseen
, /* category name seen */
2675 oinbody
, /* in @implementation body */
2676 omethodsign
, /* in @implementation body, after +/- */
2677 omethodtag
, /* after method name */
2678 omethodcolon
, /* after method colon */
2679 omethodparm
, /* after method parameter */
2680 oignore
/* wait for @end */
2685 * Use this structure to keep info about the token read, and how it
2686 * should be tagged. Used by the make_C_tag function to build a tag.
2690 char *line
; /* string containing the token */
2691 int offset
; /* where the token starts in LINE */
2692 int length
; /* token length */
2694 The previous members can be used to pass strings around for generic
2695 purposes. The following ones specifically refer to creating tags. In this
2696 case the token contained here is the pattern that will be used to create a
2699 bool valid
; /* do not create a tag; the token should be
2700 invalidated whenever a state machine is
2701 reset prematurely */
2702 bool named
; /* create a named tag */
2703 int lineno
; /* source line number of tag */
2704 long linepos
; /* source char number of tag */
2705 } token
; /* latest token read */
2708 * Variables and functions for dealing with nested structures.
2709 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2711 static void pushclass_above
__P((int, char *, int));
2712 static void popclass_above
__P((int));
2713 static void write_classname
__P((linebuffer
*, char *qualifier
));
2716 char **cname
; /* nested class names */
2717 int *bracelev
; /* nested class brace level */
2718 int nl
; /* class nesting level (elements used) */
2719 int size
; /* length of the array */
2720 } cstack
; /* stack for nested declaration tags */
2721 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2722 #define nestlev (cstack.nl)
2723 /* After struct keyword or in struct body, not inside a nested function. */
2724 #define instruct (structdef == snone && nestlev > 0 \
2725 && bracelev == cstack.bracelev[nestlev-1] + 1)
2728 pushclass_above (bracelev
, str
, len
)
2735 popclass_above (bracelev
);
2737 if (nl
>= cstack
.size
)
2739 int size
= cstack
.size
*= 2;
2740 xrnew (cstack
.cname
, size
, char *);
2741 xrnew (cstack
.bracelev
, size
, int);
2743 assert (nl
== 0 || cstack
.bracelev
[nl
-1] < bracelev
);
2744 cstack
.cname
[nl
] = (str
== NULL
) ? NULL
: savenstr (str
, len
);
2745 cstack
.bracelev
[nl
] = bracelev
;
2750 popclass_above (bracelev
)
2755 for (nl
= cstack
.nl
- 1;
2756 nl
>= 0 && cstack
.bracelev
[nl
] >= bracelev
;
2759 if (cstack
.cname
[nl
] != NULL
)
2760 free (cstack
.cname
[nl
]);
2766 write_classname (cn
, qualifier
)
2771 int qlen
= strlen (qualifier
);
2773 if (cstack
.nl
== 0 || cstack
.cname
[0] == NULL
)
2777 cn
->buffer
[0] = '\0';
2781 len
= strlen (cstack
.cname
[0]);
2782 linebuffer_setlen (cn
, len
);
2783 strcpy (cn
->buffer
, cstack
.cname
[0]);
2785 for (i
= 1; i
< cstack
.nl
; i
++)
2790 s
= cstack
.cname
[i
];
2795 linebuffer_setlen (cn
, len
);
2796 strncat (cn
->buffer
, qualifier
, qlen
);
2797 strncat (cn
->buffer
, s
, slen
);
2802 static bool consider_token
__P((char *, int, int, int *, int, int, bool *));
2803 static void make_C_tag
__P((bool));
2807 * checks to see if the current token is at the start of a
2808 * function or variable, or corresponds to a typedef, or
2809 * is a struct/union/enum tag, or #define, or an enum constant.
2811 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2812 * with args. C_EXTP points to which language we are looking at.
2823 consider_token (str
, len
, c
, c_extp
, bracelev
, parlev
, is_func_or_var
)
2824 register char *str
; /* IN: token pointer */
2825 register int len
; /* IN: token length */
2826 register int c
; /* IN: first char after the token */
2827 int *c_extp
; /* IN, OUT: C extensions mask */
2828 int bracelev
; /* IN: brace level */
2829 int parlev
; /* IN: parenthesis level */
2830 bool *is_func_or_var
; /* OUT: function or variable found */
2832 /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
2833 structtype is the type of the preceding struct-like keyword, and
2834 structbracelev is the brace level where it has been seen. */
2835 static enum sym_type structtype
;
2836 static int structbracelev
;
2837 static enum sym_type toktype
;
2840 toktype
= C_symtype (str
, len
, *c_extp
);
2843 * Skip __attribute__
2845 if (toktype
== st_C_attribute
)
2852 * Advance the definedef state machine.
2857 /* We're not on a preprocessor line. */
2858 if (toktype
== st_C_gnumacro
)
2865 if (toktype
== st_C_define
)
2867 definedef
= ddefineseen
;
2871 definedef
= dignorerest
;
2876 * Make a tag for any macro, unless it is a constant
2877 * and constantypedefs is FALSE.
2879 definedef
= dignorerest
;
2880 *is_func_or_var
= (c
== '(');
2881 if (!*is_func_or_var
&& !constantypedefs
)
2888 error ("internal error: definedef value.", (char *)NULL
);
2897 if (toktype
== st_C_typedef
)
2917 if (structdef
== snone
&& fvdef
== fvnone
)
2935 * This structdef business is NOT invoked when we are ctags and the
2936 * file is plain C. This is because a struct tag may have the same
2937 * name as another tag, and this loses with ctags.
2941 case st_C_javastruct
:
2942 if (structdef
== stagseen
)
2943 structdef
= scolonseen
;
2947 if ((*c_extp
& C_AUTO
) /* automatic detection of C++ language */
2949 && definedef
== dnone
&& structdef
== snone
2950 && typdef
== tnone
&& fvdef
== fvnone
)
2951 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2952 if (toktype
== st_C_template
)
2959 && (typdef
== tkeyseen
2960 || (typedefs_or_cplusplus
&& structdef
== snone
)))
2962 structdef
= skeyseen
;
2963 structtype
= toktype
;
2964 structbracelev
= bracelev
;
2965 if (fvdef
== fvnameseen
)
2971 if (structdef
== skeyseen
)
2973 structdef
= stagseen
;
2977 if (typdef
!= tnone
)
2980 /* Detect Objective C constructs. */
2990 objdef
= oimplementation
;
2994 case oimplementation
:
2995 /* Save the class tag for functions or variables defined inside. */
2996 objtag
= savenstr (str
, len
);
3000 /* Save the class tag for categories. */
3001 objtag
= savenstr (str
, len
);
3003 *is_func_or_var
= TRUE
;
3007 *is_func_or_var
= TRUE
;
3015 objdef
= omethodtag
;
3016 linebuffer_setlen (&token_name
, len
);
3017 strncpy (token_name
.buffer
, str
, len
);
3018 token_name
.buffer
[len
] = '\0';
3024 objdef
= omethodparm
;
3030 objdef
= omethodtag
;
3031 linebuffer_setlen (&token_name
, token_name
.len
+ len
);
3032 strncat (token_name
.buffer
, str
, len
);
3037 if (toktype
== st_C_objend
)
3039 /* Memory leakage here: the string pointed by objtag is
3040 never released, because many tests would be needed to
3041 avoid breaking on incorrect input code. The amount of
3042 memory leaked here is the sum of the lengths of the
3050 /* A function, variable or enum constant? */
3072 *is_func_or_var
= TRUE
;
3076 && structdef
== snone
3077 && structtype
== st_C_enum
&& bracelev
> structbracelev
)
3078 return TRUE
; /* enum constant */
3084 fvdef
= fdefunname
; /* GNU macro */
3085 *is_func_or_var
= TRUE
;
3093 if ((strneq (str
, "asm", 3) && endtoken (str
[3]))
3094 || (strneq (str
, "__asm__", 7) && endtoken (str
[7])))
3103 if (len
>= 10 && strneq (str
+len
-10, "::operator", 10))
3105 if (*c_extp
& C_AUTO
) /* automatic detection of C++ */
3106 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
3108 *is_func_or_var
= TRUE
;
3111 if (bracelev
> 0 && !instruct
)
3113 fvdef
= fvnameseen
; /* function or variable */
3114 *is_func_or_var
= TRUE
;
3125 * C_entries often keeps pointers to tokens or lines which are older than
3126 * the line currently read. By keeping two line buffers, and switching
3127 * them at end of line, it is possible to use those pointers.
3135 #define current_lb_is_new (newndx == curndx)
3136 #define switch_line_buffers() (curndx = 1 - curndx)
3138 #define curlb (lbs[curndx].lb)
3139 #define newlb (lbs[newndx].lb)
3140 #define curlinepos (lbs[curndx].linepos)
3141 #define newlinepos (lbs[newndx].linepos)
3143 #define plainc ((c_ext & C_EXT) == C_PLAIN)
3144 #define cplpl (c_ext & C_PLPL)
3145 #define cjava ((c_ext & C_JAVA) == C_JAVA)
3147 #define CNL_SAVE_DEFINEDEF() \
3149 curlinepos = charno; \
3150 readline (&curlb, inf); \
3151 lp = curlb.buffer; \
3158 CNL_SAVE_DEFINEDEF(); \
3159 if (savetoken.valid) \
3161 token = savetoken; \
3162 savetoken.valid = FALSE; \
3164 definedef = dnone; \
3172 /* This function is never called when token.valid is FALSE, but
3173 we must protect against invalid input or internal errors. */
3174 if (!DEBUG
&& !token
.valid
)
3178 make_tag (token_name
.buffer
, token_name
.len
, isfun
, token
.line
,
3179 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3180 else /* this case is optimised away if !DEBUG */
3181 make_tag (concat ("INVALID TOKEN:-->", token_name
.buffer
, ""),
3182 token_name
.len
+ 17, isfun
, token
.line
,
3183 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3185 token
.valid
= FALSE
;
3191 * This routine finds functions, variables, typedefs,
3192 * #define's, enum constants and struct/union/enum definitions in
3193 * C syntax and adds them to the list.
3196 C_entries (c_ext
, inf
)
3197 int c_ext
; /* extension of C */
3198 FILE *inf
; /* input file */
3200 register char c
; /* latest char read; '\0' for end of line */
3201 register char *lp
; /* pointer one beyond the character `c' */
3202 int curndx
, newndx
; /* indices for current and new lb */
3203 register int tokoff
; /* offset in line of start of current token */
3204 register int toklen
; /* length of current token */
3205 char *qualifier
; /* string used to qualify names */
3206 int qlen
; /* length of qualifier */
3207 int bracelev
; /* current brace level */
3208 int bracketlev
; /* current bracket level */
3209 int parlev
; /* current parenthesis level */
3210 int attrparlev
; /* __attribute__ parenthesis level */
3211 int templatelev
; /* current template level */
3212 int typdefbracelev
; /* bracelev where a typedef struct body begun */
3213 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
3214 bool yacc_rules
; /* in the rules part of a yacc file */
3215 struct tok savetoken
= {0}; /* token saved during preprocessor handling */
3218 linebuffer_init (&lbs
[0].lb
);
3219 linebuffer_init (&lbs
[1].lb
);
3220 if (cstack
.size
== 0)
3222 cstack
.size
= (DEBUG
) ? 1 : 4;
3224 cstack
.cname
= xnew (cstack
.size
, char *);
3225 cstack
.bracelev
= xnew (cstack
.size
, int);
3228 tokoff
= toklen
= typdefbracelev
= 0; /* keep compiler quiet */
3229 curndx
= newndx
= 0;
3233 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
3234 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
3236 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3237 token
.valid
= savetoken
.valid
= FALSE
;
3238 bracelev
= bracketlev
= parlev
= attrparlev
= templatelev
= 0;
3240 { qualifier
= "."; qlen
= 1; }
3242 { qualifier
= "::"; qlen
= 2; }
3250 /* If we are at the end of the line, the next character is a
3251 '\0'; do not skip it, because it is what tells us
3252 to read the next line. */
3273 /* Newlines inside comments do not end macro definitions in
3275 CNL_SAVE_DEFINEDEF ();
3288 /* Newlines inside strings do not end macro definitions
3289 in traditional cpp, even though compilers don't
3290 usually accept them. */
3291 CNL_SAVE_DEFINEDEF ();
3301 /* Hmmm, something went wrong. */
3310 else if (bracketlev
> 0)
3315 if (--bracketlev
> 0)
3319 CNL_SAVE_DEFINEDEF ();
3347 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
3360 else if (/* cplpl && */ *lp
== '/')
3368 if ((c_ext
& YACC
) && *lp
== '%')
3370 /* Entering or exiting rules section in yacc file. */
3372 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
3373 typdef
= tnone
; structdef
= snone
;
3374 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3376 yacc_rules
= !yacc_rules
;
3382 if (definedef
== dnone
)
3385 bool cpptoken
= TRUE
;
3387 /* Look back on this line. If all blanks, or nonblanks
3388 followed by an end of comment, this is a preprocessor
3390 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
3393 if (*cp
== '*' && *(cp
+1) == '/')
3402 definedef
= dsharpseen
;
3403 } /* if (definedef == dnone) */
3411 /* Consider token only if some involved conditions are satisfied. */
3412 if (typdef
!= tignore
3413 && definedef
!= dignorerest
3416 && (definedef
!= dnone
3417 || structdef
!= scolonseen
)
3424 if (c
== ':' && *lp
== ':' && begtoken (lp
[1]))
3425 /* This handles :: in the middle,
3426 but not at the beginning of an identifier.
3427 Also, space-separated :: is not recognised. */
3429 if (c_ext
& C_AUTO
) /* automatic detection of C++ */
3430 c_ext
= (c_ext
| C_PLPL
) & ~C_AUTO
;
3434 goto still_in_token
;
3438 bool funorvar
= FALSE
;
3441 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
3442 &c_ext
, bracelev
, parlev
,
3445 if (fvdef
== foperator
)
3448 lp
= skip_spaces (lp
-1);
3452 && !iswhite (*lp
) && *lp
!= '(')
3455 toklen
+= lp
- oldlp
;
3457 token
.named
= FALSE
;
3459 && nestlev
> 0 && definedef
== dnone
)
3460 /* in struct body */
3462 write_classname (&token_name
, qualifier
);
3463 linebuffer_setlen (&token_name
,
3464 token_name
.len
+qlen
+toklen
);
3465 strcat (token_name
.buffer
, qualifier
);
3466 strncat (token_name
.buffer
,
3467 newlb
.buffer
+ tokoff
, toklen
);
3470 else if (objdef
== ocatseen
)
3471 /* Objective C category */
3473 int len
= strlen (objtag
) + 2 + toklen
;
3474 linebuffer_setlen (&token_name
, len
);
3475 strcpy (token_name
.buffer
, objtag
);
3476 strcat (token_name
.buffer
, "(");
3477 strncat (token_name
.buffer
,
3478 newlb
.buffer
+ tokoff
, toklen
);
3479 strcat (token_name
.buffer
, ")");
3482 else if (objdef
== omethodtag
3483 || objdef
== omethodparm
)
3484 /* Objective C method */
3488 else if (fvdef
== fdefunname
)
3489 /* GNU DEFUN and similar macros */
3491 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
3495 /* Rewrite the tag so that emacs lisp DEFUNs
3496 can be found by their elisp name */
3502 linebuffer_setlen (&token_name
, len
);
3503 strncpy (token_name
.buffer
,
3504 newlb
.buffer
+ off
, len
);
3505 token_name
.buffer
[len
] = '\0';
3508 if (token_name
.buffer
[len
] == '_')
3509 token_name
.buffer
[len
] = '-';
3510 token
.named
= defun
;
3514 linebuffer_setlen (&token_name
, toklen
);
3515 strncpy (token_name
.buffer
,
3516 newlb
.buffer
+ tokoff
, toklen
);
3517 token_name
.buffer
[toklen
] = '\0';
3518 /* Name macros and members. */
3519 token
.named
= (structdef
== stagseen
3520 || typdef
== ttypeseen
3523 && definedef
== dignorerest
)
3525 && definedef
== dnone
3526 && structdef
== snone
3529 token
.lineno
= lineno
;
3530 token
.offset
= tokoff
;
3531 token
.length
= toklen
;
3532 token
.line
= newlb
.buffer
;
3533 token
.linepos
= newlinepos
;
3536 if (definedef
== dnone
3537 && (fvdef
== fvnameseen
3538 || fvdef
== foperator
3539 || structdef
== stagseen
3541 || typdef
== ttypeseen
3542 || objdef
!= onone
))
3544 if (current_lb_is_new
)
3545 switch_line_buffers ();
3547 else if (definedef
!= dnone
3548 || fvdef
== fdefunname
3550 make_C_tag (funorvar
);
3552 else /* not yacc and consider_token failed */
3554 if (inattribute
&& fvdef
== fignore
)
3556 /* We have just met __attribute__ after a
3557 function parameter list: do not tag the
3564 } /* if (endtoken (c)) */
3565 else if (intoken (c
))
3571 } /* if (midtoken) */
3572 else if (begtoken (c
))
3580 /* This prevents tagging fb in
3581 void (__attribute__((noreturn)) *fb) (void);
3582 Fixing this is not easy and not very important. */
3586 if (plainc
|| declarations
)
3588 make_C_tag (TRUE
); /* a function */
3593 if (structdef
== stagseen
&& !cjava
)
3595 popclass_above (bracelev
);
3603 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
3605 tokoff
= lp
- 1 - newlb
.buffer
;
3610 } /* if (begtoken) */
3611 } /* if must look at token */
3614 /* Detect end of line, colon, comma, semicolon and various braces
3615 after having handled a token.*/
3621 if (yacc_rules
&& token
.offset
== 0 && token
.valid
)
3623 make_C_tag (FALSE
); /* a yacc function */
3626 if (definedef
!= dnone
)
3632 make_C_tag (TRUE
); /* an Objective C class */
3636 objdef
= omethodcolon
;
3637 linebuffer_setlen (&token_name
, token_name
.len
+ 1);
3638 strcat (token_name
.buffer
, ":");
3641 if (structdef
== stagseen
)
3643 structdef
= scolonseen
;
3646 /* Should be useless, but may be work as a safety net. */
3647 if (cplpl
&& fvdef
== flistseen
)
3649 make_C_tag (TRUE
); /* a function */
3655 if (definedef
!= dnone
|| inattribute
)
3661 make_C_tag (FALSE
); /* a typedef */
3671 if (typdef
== tignore
|| cplpl
)
3675 if ((globals
&& bracelev
== 0 && (!fvextern
|| declarations
))
3676 || (members
&& instruct
))
3677 make_C_tag (FALSE
); /* a variable */
3680 token
.valid
= FALSE
;
3684 && (cplpl
|| !instruct
)
3685 && (typdef
== tnone
|| (typdef
!= tignore
&& instruct
)))
3687 && plainc
&& instruct
))
3688 make_C_tag (TRUE
); /* a function */
3694 && cplpl
&& structdef
== stagseen
)
3695 make_C_tag (FALSE
); /* forward declaration */
3697 token
.valid
= FALSE
;
3698 } /* switch (fvdef) */
3704 if (structdef
== stagseen
)
3708 if (definedef
!= dnone
|| inattribute
)
3714 make_C_tag (TRUE
); /* an Objective C method */
3735 && (!fvextern
|| declarations
))
3736 || (members
&& instruct
)))
3737 make_C_tag (FALSE
); /* a variable */
3740 if ((declarations
&& typdef
== tnone
&& !instruct
)
3741 || (members
&& typdef
!= tignore
&& instruct
))
3743 make_C_tag (TRUE
); /* a function */
3746 else if (!declarations
)
3748 token
.valid
= FALSE
;
3753 if (structdef
== stagseen
)
3757 if (definedef
!= dnone
|| inattribute
)
3759 if (structdef
== stagseen
)
3766 make_C_tag (FALSE
); /* a typedef */
3778 if ((members
&& bracelev
== 1)
3779 || (globals
&& bracelev
== 0
3780 && (!fvextern
|| declarations
)))
3781 make_C_tag (FALSE
); /* a variable */
3795 if (definedef
!= dnone
)
3797 if (objdef
== otagseen
&& parlev
== 0)
3798 objdef
= oparenseen
;
3802 if (typdef
== ttypeseen
3806 /* This handles constructs like:
3807 typedef void OperatorFun (int fun); */
3826 if (--attrparlev
== 0)
3827 inattribute
= FALSE
;
3830 if (definedef
!= dnone
)
3832 if (objdef
== ocatseen
&& parlev
== 1)
3834 make_C_tag (TRUE
); /* an Objective C category */
3848 || typdef
== ttypeseen
))
3851 make_C_tag (FALSE
); /* a typedef */
3854 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3858 if (definedef
!= dnone
)
3860 if (typdef
== ttypeseen
)
3862 /* Whenever typdef is set to tinbody (currently only
3863 here), typdefbracelev should be set to bracelev. */
3865 typdefbracelev
= bracelev
;
3870 make_C_tag (TRUE
); /* a function */
3879 make_C_tag (TRUE
); /* an Objective C class */
3884 make_C_tag (TRUE
); /* an Objective C method */
3888 /* Neutralize `extern "C" {' grot. */
3889 if (bracelev
== 0 && structdef
== snone
&& nestlev
== 0
3897 case skeyseen
: /* unnamed struct */
3898 pushclass_above (bracelev
, NULL
, 0);
3901 case stagseen
: /* named struct or enum */
3902 case scolonseen
: /* a class */
3903 pushclass_above (bracelev
,token
.line
+token
.offset
, token
.length
);
3905 make_C_tag (FALSE
); /* a struct or enum */
3911 if (definedef
!= dnone
)
3913 if (fvdef
== fstartlist
)
3915 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3916 token
.valid
= FALSE
;
3920 if (definedef
!= dnone
)
3922 if (!ignoreindent
&& lp
== newlb
.buffer
+ 1)
3925 token
.valid
= FALSE
;
3926 bracelev
= 0; /* reset brace level if first column */
3927 parlev
= 0; /* also reset paren level, just in case... */
3929 else if (bracelev
> 0)
3932 token
.valid
= FALSE
; /* something gone amiss, token unreliable */
3933 popclass_above (bracelev
);
3935 /* Only if typdef == tinbody is typdefbracelev significant. */
3936 if (typdef
== tinbody
&& bracelev
<= typdefbracelev
)
3938 assert (bracelev
== typdefbracelev
);
3943 if (definedef
!= dnone
)
3953 if ((members
&& bracelev
== 1)
3954 || (globals
&& bracelev
== 0 && (!fvextern
|| declarations
)))
3955 make_C_tag (FALSE
); /* a variable */
3963 && (structdef
== stagseen
|| fvdef
== fvnameseen
))
3970 if (templatelev
> 0)
3978 if (objdef
== oinbody
&& bracelev
== 0)
3980 objdef
= omethodsign
;
3985 case '#': case '~': case '&': case '%': case '/':
3986 case '|': case '^': case '!': case '.': case '?':
3987 if (definedef
!= dnone
)
3989 /* These surely cannot follow a function tag in C. */
4002 if (objdef
== otagseen
)
4004 make_C_tag (TRUE
); /* an Objective C class */
4007 /* If a macro spans multiple lines don't reset its state. */
4009 CNL_SAVE_DEFINEDEF ();
4015 } /* while not eof */
4017 free (lbs
[0].lb
.buffer
);
4018 free (lbs
[1].lb
.buffer
);
4022 * Process either a C++ file or a C file depending on the setting
4026 default_C_entries (inf
)
4029 C_entries (cplusplus
? C_PLPL
: C_AUTO
, inf
);
4032 /* Always do plain C. */
4034 plain_C_entries (inf
)
4040 /* Always do C++. */
4042 Cplusplus_entries (inf
)
4045 C_entries (C_PLPL
, inf
);
4048 /* Always do Java. */
4053 C_entries (C_JAVA
, inf
);
4061 C_entries (C_STAR
, inf
);
4064 /* Always do Yacc. */
4069 C_entries (YACC
, inf
);
4073 /* Useful macros. */
4074 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
4075 for (; /* loop initialization */ \
4076 !feof (file_pointer) /* loop test */ \
4077 && /* instructions at start of loop */ \
4078 (readline (&line_buffer, file_pointer), \
4079 char_pointer = line_buffer.buffer, \
4083 #define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \
4084 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
4085 && strneq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
4086 && notinname ((cp)[sizeof(kw)-1]) /* end of kw */ \
4087 && ((cp) = skip_spaces((cp)+sizeof(kw)-1))) /* skip spaces */
4089 /* Similar to LOOKING_AT but does not use notinname, does not skip */
4090 #define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \
4091 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
4092 && strncaseeq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
4093 && ((cp) += sizeof(kw)-1)) /* skip spaces */
4096 * Read a file, but do no processing. This is used to do regexp
4097 * matching on files that have no language defined.
4100 just_read_file (inf
)
4103 register char *dummy
;
4105 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
4110 /* Fortran parsing */
4112 static void F_takeprec
__P((void));
4113 static void F_getit
__P((FILE *));
4118 dbp
= skip_spaces (dbp
);
4122 dbp
= skip_spaces (dbp
);
4123 if (strneq (dbp
, "(*)", 3))
4128 if (!ISDIGIT (*dbp
))
4130 --dbp
; /* force failure */
4135 while (ISDIGIT (*dbp
));
4144 dbp
= skip_spaces (dbp
);
4147 readline (&lb
, inf
);
4152 dbp
= skip_spaces (dbp
);
4154 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
4156 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
4158 make_tag (dbp
, cp
-dbp
, TRUE
,
4159 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4164 Fortran_functions (inf
)
4167 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4170 dbp
++; /* Ratfor escape to fortran */
4171 dbp
= skip_spaces (dbp
);
4174 switch (lowcase (*dbp
))
4177 if (nocase_tail ("integer"))
4181 if (nocase_tail ("real"))
4185 if (nocase_tail ("logical"))
4189 if (nocase_tail ("complex") || nocase_tail ("character"))
4193 if (nocase_tail ("double"))
4195 dbp
= skip_spaces (dbp
);
4198 if (nocase_tail ("precision"))
4204 dbp
= skip_spaces (dbp
);
4207 switch (lowcase (*dbp
))
4210 if (nocase_tail ("function"))
4214 if (nocase_tail ("subroutine"))
4218 if (nocase_tail ("entry"))
4222 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
4224 dbp
= skip_spaces (dbp
);
4225 if (*dbp
== '\0') /* assume un-named */
4226 make_tag ("blockdata", 9, TRUE
,
4227 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
4229 F_getit (inf
); /* look for name */
4240 * Philippe Waroquiers (1998)
4243 static void Ada_getit
__P((FILE *, char *));
4245 /* Once we are positioned after an "interesting" keyword, let's get
4246 the real tag value necessary. */
4248 Ada_getit (inf
, name_qualifier
)
4250 char *name_qualifier
;
4258 dbp
= skip_spaces (dbp
);
4260 || (dbp
[0] == '-' && dbp
[1] == '-'))
4262 readline (&lb
, inf
);
4265 switch (lowcase(*dbp
))
4268 if (nocase_tail ("body"))
4270 /* Skipping body of procedure body or package body or ....
4271 resetting qualifier to body instead of spec. */
4272 name_qualifier
= "/b";
4277 /* Skipping type of task type or protected type ... */
4278 if (nocase_tail ("type"))
4285 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
4290 dbp
= skip_spaces (dbp
);
4293 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
4301 name
= concat (dbp
, name_qualifier
, "");
4303 make_tag (name
, strlen (name
), TRUE
,
4304 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4316 bool inquote
= FALSE
;
4317 bool skip_till_semicolumn
= FALSE
;
4319 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4321 while (*dbp
!= '\0')
4323 /* Skip a string i.e. "abcd". */
4324 if (inquote
|| (*dbp
== '"'))
4326 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
4331 continue; /* advance char */
4336 break; /* advance line */
4340 /* Skip comments. */
4341 if (dbp
[0] == '-' && dbp
[1] == '-')
4342 break; /* advance line */
4344 /* Skip character enclosed in single quote i.e. 'a'
4345 and skip single quote starting an attribute i.e. 'Image. */
4354 if (skip_till_semicolumn
)
4357 skip_till_semicolumn
= FALSE
;
4359 continue; /* advance char */
4362 /* Search for beginning of a token. */
4363 if (!begtoken (*dbp
))
4366 continue; /* advance char */
4369 /* We are at the beginning of a token. */
4370 switch (lowcase(*dbp
))
4373 if (!packages_only
&& nocase_tail ("function"))
4374 Ada_getit (inf
, "/f");
4376 break; /* from switch */
4377 continue; /* advance char */
4379 if (!packages_only
&& nocase_tail ("procedure"))
4380 Ada_getit (inf
, "/p");
4381 else if (nocase_tail ("package"))
4382 Ada_getit (inf
, "/s");
4383 else if (nocase_tail ("protected")) /* protected type */
4384 Ada_getit (inf
, "/t");
4386 break; /* from switch */
4387 continue; /* advance char */
4390 if (typedefs
&& !packages_only
&& nocase_tail ("use"))
4392 /* when tagging types, avoid tagging use type Pack.Typename;
4393 for this, we will skip everything till a ; */
4394 skip_till_semicolumn
= TRUE
;
4395 continue; /* advance char */
4399 if (!packages_only
&& nocase_tail ("task"))
4400 Ada_getit (inf
, "/k");
4401 else if (typedefs
&& !packages_only
&& nocase_tail ("type"))
4403 Ada_getit (inf
, "/t");
4404 while (*dbp
!= '\0')
4408 break; /* from switch */
4409 continue; /* advance char */
4412 /* Look for the end of the token. */
4413 while (!endtoken (*dbp
))
4416 } /* advance char */
4417 } /* advance line */
4422 * Unix and microcontroller assembly tag handling
4423 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
4424 * Idea by Bob Weiner, Motorola Inc. (1994)
4432 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4434 /* If first char is alphabetic or one of [_.$], test for colon
4435 following identifier. */
4436 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4438 /* Read past label. */
4440 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4442 if (*cp
== ':' || iswhite (*cp
))
4443 /* Found end of label, so copy it and add it to the table. */
4444 make_tag (lb
.buffer
, cp
- lb
.buffer
, TRUE
,
4445 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4453 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
4454 * Perl variable names: /^(my|local).../
4455 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
4456 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
4457 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
4460 Perl_functions (inf
)
4463 char *package
= savestr ("main"); /* current package name */
4466 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4470 if (LOOKING_AT (cp
, "package"))
4473 get_tag (cp
, &package
);
4475 else if (LOOKING_AT (cp
, "sub"))
4480 while (!notinname (*cp
))
4483 continue; /* nothing found */
4484 if ((pos
= etags_strchr (sp
, ':')) != NULL
4485 && pos
< cp
&& pos
[1] == ':')
4486 /* The name is already qualified. */
4487 make_tag (sp
, cp
- sp
, TRUE
,
4488 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4492 char savechar
, *name
;
4496 name
= concat (package
, "::", sp
);
4498 make_tag (name
, strlen(name
), TRUE
,
4499 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4503 else if (globals
) /* only if we are tagging global vars */
4505 /* Skip a qualifier, if any. */
4506 bool qual
= LOOKING_AT (cp
, "my") || LOOKING_AT (cp
, "local");
4507 /* After "my" or "local", but before any following paren or space. */
4508 char *varstart
= cp
;
4510 if (qual
/* should this be removed? If yes, how? */
4511 && (*cp
== '$' || *cp
== '@' || *cp
== '%'))
4516 while (ISALNUM (*cp
) || *cp
== '_');
4520 /* Should be examining a variable list at this point;
4521 could insist on seeing an open parenthesis. */
4522 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
4528 make_tag (varstart
, cp
- varstart
, FALSE
,
4529 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4538 * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4539 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4540 * More ideas by seb bacon <seb@jamkit.com> (2002)
4543 Python_functions (inf
)
4548 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4550 cp
= skip_spaces (cp
);
4551 if (LOOKING_AT (cp
, "def") || LOOKING_AT (cp
, "class"))
4554 while (!notinname (*cp
) && *cp
!= ':')
4556 make_tag (name
, cp
- name
, TRUE
,
4557 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4566 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4567 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4568 * - /^[ \t]*define\(\"[^\"]+/
4569 * Only with --members:
4570 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4571 * Idea by Diez B. Roggisch (2001)
4577 register char *cp
, *name
;
4578 bool search_identifier
= FALSE
;
4580 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4582 cp
= skip_spaces (cp
);
4584 if (search_identifier
4587 while (!notinname (*cp
))
4589 make_tag (name
, cp
- name
, TRUE
,
4590 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4591 search_identifier
= FALSE
;
4593 else if (LOOKING_AT (cp
, "function"))
4596 cp
= skip_spaces (cp
+1);
4600 while (!notinname (*cp
))
4602 make_tag (name
, cp
- name
, TRUE
,
4603 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4606 search_identifier
= TRUE
;
4608 else if (LOOKING_AT (cp
, "class"))
4613 while (*cp
!= '\0' && !iswhite (*cp
))
4615 make_tag (name
, cp
- name
, FALSE
,
4616 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4619 search_identifier
= TRUE
;
4621 else if (strneq (cp
, "define", 6)
4622 && (cp
= skip_spaces (cp
+6))
4624 && (*cp
== '"' || *cp
== '\''))
4628 while (*cp
!= quote
&& *cp
!= '\0')
4630 make_tag (name
, cp
- name
, FALSE
,
4631 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4634 && LOOKING_AT (cp
, "var")
4638 while (!notinname(*cp
))
4640 make_tag (name
, cp
- name
, FALSE
,
4641 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4648 * Cobol tag functions
4649 * We could look for anything that could be a paragraph name.
4650 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4651 * Idea by Corny de Souza (1993)
4654 Cobol_paragraphs (inf
)
4657 register char *bp
, *ep
;
4659 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4665 /* If eoln, compiler option or comment ignore whole line. */
4666 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
4669 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
4672 make_tag (bp
, ep
- bp
, TRUE
,
4673 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4680 * Ideas by Assar Westerlund <assar@sics.se> (2001)
4683 Makefile_targets (inf
)
4688 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4690 if (*bp
== '\t' || *bp
== '#')
4692 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
4694 if (*bp
== ':' || (globals
&& *bp
== '='))
4696 /* We should detect if there is more than one tag, but we do not.
4697 We just skip initial and final spaces. */
4698 char * namestart
= skip_spaces (lb
.buffer
);
4699 while (--bp
> namestart
)
4700 if (!notinname (*bp
))
4702 make_tag (namestart
, bp
- namestart
+ 1, TRUE
,
4703 lb
.buffer
, bp
- lb
.buffer
+ 2, lineno
, linecharno
);
4711 * Original code by Mosur K. Mohan (1989)
4713 * Locates tags for procedures & functions. Doesn't do any type- or
4714 * var-definitions. It does look for the keyword "extern" or
4715 * "forward" immediately following the procedure statement; if found,
4716 * the tag is skipped.
4719 Pascal_functions (inf
)
4722 linebuffer tline
; /* mostly copied from C_entries */
4724 int save_lineno
, namelen
, taglen
;
4727 bool /* each of these flags is TRUE iff: */
4728 incomment
, /* point is inside a comment */
4729 inquote
, /* point is inside '..' string */
4730 get_tagname
, /* point is after PROCEDURE/FUNCTION
4731 keyword, so next item = potential tag */
4732 found_tag
, /* point is after a potential tag */
4733 inparms
, /* point is within parameter-list */
4734 verify_tag
; /* point has passed the parm-list, so the
4735 next token will determine whether this
4736 is a FORWARD/EXTERN to be ignored, or
4737 whether it is a real tag */
4739 save_lcno
= save_lineno
= namelen
= taglen
= 0; /* keep compiler quiet */
4740 name
= NULL
; /* keep compiler quiet */
4743 linebuffer_init (&tline
);
4745 incomment
= inquote
= FALSE
;
4746 found_tag
= FALSE
; /* have a proc name; check if extern */
4747 get_tagname
= FALSE
; /* found "procedure" keyword */
4748 inparms
= FALSE
; /* found '(' after "proc" */
4749 verify_tag
= FALSE
; /* check if "extern" is ahead */
4752 while (!feof (inf
)) /* long main loop to get next char */
4755 if (c
== '\0') /* if end of line */
4757 readline (&lb
, inf
);
4761 if (!((found_tag
&& verify_tag
)
4763 c
= *dbp
++; /* only if don't need *dbp pointing
4764 to the beginning of the name of
4765 the procedure or function */
4769 if (c
== '}') /* within { } comments */
4771 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4788 inquote
= TRUE
; /* found first quote */
4790 case '{': /* found open { comment */
4794 if (*dbp
== '*') /* found open (* comment */
4799 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4802 case ')': /* end of parms list */
4807 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4814 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4816 /* Check if this is an "extern" declaration. */
4819 if (lowcase (*dbp
== 'e'))
4821 if (nocase_tail ("extern")) /* superfluous, really! */
4827 else if (lowcase (*dbp
) == 'f')
4829 if (nocase_tail ("forward")) /* check for forward reference */
4835 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4839 make_tag (name
, namelen
, TRUE
,
4840 tline
.buffer
, taglen
, save_lineno
, save_lcno
);
4844 if (get_tagname
) /* grab name of proc or fn */
4851 /* Find block name. */
4852 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4855 /* Save all values for later tagging. */
4856 linebuffer_setlen (&tline
, lb
.len
);
4857 strcpy (tline
.buffer
, lb
.buffer
);
4858 save_lineno
= lineno
;
4859 save_lcno
= linecharno
;
4860 name
= tline
.buffer
+ (dbp
- lb
.buffer
);
4862 taglen
= cp
- lb
.buffer
+ 1;
4864 dbp
= cp
; /* set dbp to e-o-token */
4865 get_tagname
= FALSE
;
4869 /* And proceed to check for "extern". */
4871 else if (!incomment
&& !inquote
&& !found_tag
)
4873 /* Check for proc/fn keywords. */
4874 switch (lowcase (c
))
4877 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4881 if (nocase_tail ("unction"))
4886 } /* while not eof */
4888 free (tline
.buffer
);
4893 * Lisp tag functions
4894 * look for (def or (DEF, quote or QUOTE
4897 static void L_getit
__P((void));
4902 if (*dbp
== '\'') /* Skip prefix quote */
4904 else if (*dbp
== '(')
4907 /* Try to skip "(quote " */
4908 if (!LOOKING_AT (dbp
, "quote") && !LOOKING_AT (dbp
, "QUOTE"))
4909 /* Ok, then skip "(" before name in (defstruct (foo)) */
4910 dbp
= skip_spaces (dbp
);
4912 get_tag (dbp
, NULL
);
4916 Lisp_functions (inf
)
4919 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4924 if (strneq (dbp
+1, "def", 3) || strneq (dbp
+1, "DEF", 3))
4926 dbp
= skip_non_spaces (dbp
);
4927 dbp
= skip_spaces (dbp
);
4932 /* Check for (foo::defmumble name-defined ... */
4935 while (!notinname (*dbp
) && *dbp
!= ':');
4940 while (*dbp
== ':');
4942 if (strneq (dbp
, "def", 3) || strneq (dbp
, "DEF", 3))
4944 dbp
= skip_non_spaces (dbp
);
4945 dbp
= skip_spaces (dbp
);
4955 * Lua script language parsing
4956 * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
4958 * "function" and "local function" are tags if they start at column 1.
4966 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4968 if (bp
[0] != 'f' && bp
[0] != 'l')
4971 (void)LOOKING_AT (bp
, "local"); /* skip possible "local" */
4973 if (LOOKING_AT (bp
, "function"))
4981 * Just look for lines where the first character is '/'
4982 * Also look at "defineps" for PSWrap
4984 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4985 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4991 register char *bp
, *ep
;
4993 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4998 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
5001 make_tag (bp
, ep
- bp
, TRUE
,
5002 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
5004 else if (LOOKING_AT (bp
, "defineps"))
5012 * Ignore anything after \ followed by space or in ( )
5013 * Look for words defined by :
5014 * Look for constant, code, create, defer, value, and variable
5015 * OBP extensions: Look for buffer:, field,
5016 * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004)
5024 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5025 while ((bp
= skip_spaces (bp
))[0] != '\0')
5026 if (bp
[0] == '\\' && iswhite(bp
[1]))
5027 break; /* read next line */
5028 else if (bp
[0] == '(' && iswhite(bp
[1]))
5029 do /* skip to ) or eol */
5031 while (*bp
!= ')' && *bp
!= '\0');
5032 else if ((bp
[0] == ':' && iswhite(bp
[1]) && bp
++)
5033 || LOOKING_AT_NOCASE (bp
, "constant")
5034 || LOOKING_AT_NOCASE (bp
, "code")
5035 || LOOKING_AT_NOCASE (bp
, "create")
5036 || LOOKING_AT_NOCASE (bp
, "defer")
5037 || LOOKING_AT_NOCASE (bp
, "value")
5038 || LOOKING_AT_NOCASE (bp
, "variable")
5039 || LOOKING_AT_NOCASE (bp
, "buffer:")
5040 || LOOKING_AT_NOCASE (bp
, "field"))
5041 get_tag (skip_spaces (bp
), NULL
); /* Yay! A definition! */
5043 bp
= skip_non_spaces (bp
);
5048 * Scheme tag functions
5049 * look for (def... xyzzy
5051 * (def ... ((...(xyzzy ....
5053 * Original code by Ken Haase (1985?)
5056 Scheme_functions (inf
)
5061 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5063 if (strneq (bp
, "(def", 4) || strneq (bp
, "(DEF", 4))
5065 bp
= skip_non_spaces (bp
+4);
5066 /* Skip over open parens and white space */
5067 while (notinname (*bp
))
5071 if (LOOKING_AT (bp
, "(SET!") || LOOKING_AT (bp
, "(set!"))
5077 /* Find tags in TeX and LaTeX input files. */
5079 /* TEX_toktab is a table of TeX control sequences that define tags.
5080 * Each entry records one such control sequence.
5082 * Original code from who knows whom.
5084 * Stefan Monnier (2002)
5087 static linebuffer
*TEX_toktab
= NULL
; /* Table with tag tokens */
5089 /* Default set of control sequences to put into TEX_toktab.
5090 The value of environment var TEXTAGS is prepended to this. */
5091 static char *TEX_defenv
= "\
5092 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
5093 :part:appendix:entry:index:def\
5094 :newcommand:renewcommand:newenvironment:renewenvironment";
5096 static void TEX_mode
__P((FILE *));
5097 static void TEX_decode_env
__P((char *, char *));
5099 static char TEX_esc
= '\\';
5100 static char TEX_opgrp
= '{';
5101 static char TEX_clgrp
= '}';
5104 * TeX/LaTeX scanning loop.
5113 /* Select either \ or ! as escape character. */
5116 /* Initialize token table once from environment. */
5117 if (TEX_toktab
== NULL
)
5118 TEX_decode_env ("TEXTAGS", TEX_defenv
);
5120 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5122 /* Look at each TEX keyword in line. */
5125 /* Look for a TEX escape. */
5126 while (*cp
++ != TEX_esc
)
5127 if (cp
[-1] == '\0' || cp
[-1] == '%')
5130 for (key
= TEX_toktab
; key
->buffer
!= NULL
; key
++)
5131 if (strneq (cp
, key
->buffer
, key
->len
))
5134 int namelen
, linelen
;
5137 cp
= skip_spaces (cp
+ key
->len
);
5138 if (*cp
== TEX_opgrp
)
5144 (!iswhite (*p
) && *p
!= '#' &&
5145 *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
);
5150 if (!opgrp
|| *p
== TEX_clgrp
)
5152 while (*p
!= '\0' && *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
)
5154 linelen
= p
- lb
.buffer
+ 1;
5156 make_tag (cp
, namelen
, TRUE
,
5157 lb
.buffer
, linelen
, lineno
, linecharno
);
5158 goto tex_next_line
; /* We only tag a line once */
5166 #define TEX_LESC '\\'
5167 #define TEX_SESC '!'
5169 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
5170 chars accordingly. */
5177 while ((c
= getc (inf
)) != EOF
)
5179 /* Skip to next line if we hit the TeX comment char. */
5181 while (c
!= '\n' && c
!= EOF
)
5183 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
5199 /* If the input file is compressed, inf is a pipe, and rewind may fail.
5200 No attempt is made to correct the situation. */
5204 /* Read environment and prepend it to the default string.
5205 Build token table. */
5207 TEX_decode_env (evarname
, defenv
)
5211 register char *env
, *p
;
5214 /* Append default string to environment. */
5215 env
= getenv (evarname
);
5221 env
= concat (oldenv
, defenv
, "");
5224 /* Allocate a token table */
5225 for (len
= 1, p
= env
; p
;)
5226 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
5228 TEX_toktab
= xnew (len
, linebuffer
);
5230 /* Unpack environment string into token table. Be careful about */
5231 /* zero-length strings (leading ':', "::" and trailing ':') */
5232 for (i
= 0; *env
!= '\0';)
5234 p
= etags_strchr (env
, ':');
5235 if (!p
) /* End of environment string. */
5236 p
= env
+ strlen (env
);
5238 { /* Only non-zero strings. */
5239 TEX_toktab
[i
].buffer
= savenstr (env
, p
- env
);
5240 TEX_toktab
[i
].len
= p
- env
;
5247 TEX_toktab
[i
].buffer
= NULL
; /* Mark end of table. */
5248 TEX_toktab
[i
].len
= 0;
5255 /* Texinfo support. Dave Love, Mar. 2000. */
5261 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5262 if (LOOKING_AT (cp
, "@node"))
5265 while (*cp
!= '\0' && *cp
!= ',')
5267 make_tag (start
, cp
- start
, TRUE
,
5268 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5275 * Contents of <title>, <h1>, <h2>, <h3> are tags.
5276 * Contents of <a name=xxx> are tags with name xxx.
5278 * Francesco Potortì, 2002.
5284 bool getnext
= FALSE
; /* next text outside of HTML tags is a tag */
5285 bool skiptag
= FALSE
; /* skip to the end of the current HTML tag */
5286 bool intag
= FALSE
; /* inside an html tag, looking for ID= */
5287 bool inanchor
= FALSE
; /* when INTAG, is an anchor, look for NAME= */
5291 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5293 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
5294 for (;;) /* loop on the same line */
5296 if (skiptag
) /* skip HTML tag */
5298 while (*dbp
!= '\0' && *dbp
!= '>')
5304 continue; /* look on the same line */
5306 break; /* go to next line */
5309 else if (intag
) /* look for "name=" or "id=" */
5311 while (*dbp
!= '\0' && *dbp
!= '>'
5312 && lowcase (*dbp
) != 'n' && lowcase (*dbp
) != 'i')
5315 break; /* go to next line */
5320 continue; /* look on the same line */
5322 if ((inanchor
&& LOOKING_AT_NOCASE (dbp
, "name="))
5323 || LOOKING_AT_NOCASE (dbp
, "id="))
5325 bool quoted
= (dbp
[0] == '"');
5328 for (end
= ++dbp
; *end
!= '\0' && *end
!= '"'; end
++)
5331 for (end
= dbp
; *end
!= '\0' && intoken (*end
); end
++)
5333 linebuffer_setlen (&token_name
, end
- dbp
);
5334 strncpy (token_name
.buffer
, dbp
, end
- dbp
);
5335 token_name
.buffer
[end
- dbp
] = '\0';
5338 intag
= FALSE
; /* we found what we looked for */
5339 skiptag
= TRUE
; /* skip to the end of the tag */
5340 getnext
= TRUE
; /* then grab the text */
5341 continue; /* look on the same line */
5346 else if (getnext
) /* grab next tokens and tag them */
5348 dbp
= skip_spaces (dbp
);
5350 break; /* go to next line */
5354 inanchor
= (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]));
5355 continue; /* look on the same line */
5358 for (end
= dbp
+ 1; *end
!= '\0' && *end
!= '<'; end
++)
5360 make_tag (token_name
.buffer
, token_name
.len
, TRUE
,
5361 dbp
, end
- dbp
, lineno
, linecharno
);
5362 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5364 break; /* go to next line */
5367 else /* look for an interesting HTML tag */
5369 while (*dbp
!= '\0' && *dbp
!= '<')
5372 break; /* go to next line */
5374 if (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]))
5377 continue; /* look on the same line */
5379 else if (LOOKING_AT_NOCASE (dbp
, "<title>")
5380 || LOOKING_AT_NOCASE (dbp
, "<h1>")
5381 || LOOKING_AT_NOCASE (dbp
, "<h2>")
5382 || LOOKING_AT_NOCASE (dbp
, "<h3>"))
5386 continue; /* look on the same line */
5397 * Assumes that the predicate or rule starts at column 0.
5398 * Only the first clause of a predicate or rule is added.
5399 * Original code by Sunichirou Sugou (1989)
5400 * Rewritten by Anders Lindgren (1996)
5402 static int prolog_pr
__P((char *, char *));
5403 static void prolog_skip_comment
__P((linebuffer
*, FILE *));
5404 static int prolog_atom
__P((char *, int));
5407 Prolog_functions (inf
)
5418 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5420 if (cp
[0] == '\0') /* Empty line */
5422 else if (iswhite (cp
[0])) /* Not a predicate */
5424 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
5425 prolog_skip_comment (&lb
, inf
);
5426 else if ((len
= prolog_pr (cp
, last
)) > 0)
5428 /* Predicate or rule. Store the function name so that we
5429 only generate a tag for the first clause. */
5431 last
= xnew(len
+ 1, char);
5432 else if (len
+ 1 > allocated
)
5433 xrnew (last
, len
+ 1, char);
5434 allocated
= len
+ 1;
5435 strncpy (last
, cp
, len
);
5445 prolog_skip_comment (plb
, inf
)
5453 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
5454 if (cp
[0] == '*' && cp
[1] == '/')
5456 readline (plb
, inf
);
5462 * A predicate or rule definition is added if it matches:
5463 * <beginning of line><Prolog Atom><whitespace>(
5464 * or <beginning of line><Prolog Atom><whitespace>:-
5466 * It is added to the tags database if it doesn't match the
5467 * name of the previous clause header.
5469 * Return the size of the name of the predicate or rule, or 0 if no
5475 char *last
; /* Name of last clause. */
5480 pos
= prolog_atom (s
, 0);
5485 pos
= skip_spaces (s
+ pos
) - s
;
5488 || (s
[pos
] == '(' && (pos
+= 1))
5489 || (s
[pos
] == ':' && s
[pos
+ 1] == '-' && (pos
+= 2)))
5490 && (last
== NULL
/* save only the first clause */
5491 || len
!= (int)strlen (last
)
5492 || !strneq (s
, last
, len
)))
5494 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5502 * Consume a Prolog atom.
5503 * Return the number of bytes consumed, or -1 if there was an error.
5505 * A prolog atom, in this context, could be one of:
5506 * - An alphanumeric sequence, starting with a lower case letter.
5507 * - A quoted arbitrary string. Single quotes can escape themselves.
5508 * Backslash quotes everything.
5511 prolog_atom (s
, pos
)
5519 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
5521 /* The atom is unquoted. */
5523 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
5527 return pos
- origpos
;
5529 else if (s
[pos
] == '\'')
5540 pos
++; /* A double quote */
5542 else if (s
[pos
] == '\0')
5543 /* Multiline quoted atoms are ignored. */
5545 else if (s
[pos
] == '\\')
5547 if (s
[pos
+1] == '\0')
5554 return pos
- origpos
;
5562 * Support for Erlang
5564 * Generates tags for functions, defines, and records.
5565 * Assumes that Erlang functions start at column 0.
5566 * Original code by Anders Lindgren (1996)
5568 static int erlang_func
__P((char *, char *));
5569 static void erlang_attribute
__P((char *));
5570 static int erlang_atom
__P((char *));
5573 Erlang_functions (inf
)
5584 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5586 if (cp
[0] == '\0') /* Empty line */
5588 else if (iswhite (cp
[0])) /* Not function nor attribute */
5590 else if (cp
[0] == '%') /* comment */
5592 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
5594 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
5596 erlang_attribute (cp
);
5603 else if ((len
= erlang_func (cp
, last
)) > 0)
5606 * Function. Store the function name so that we only
5607 * generates a tag for the first clause.
5610 last
= xnew (len
+ 1, char);
5611 else if (len
+ 1 > allocated
)
5612 xrnew (last
, len
+ 1, char);
5613 allocated
= len
+ 1;
5614 strncpy (last
, cp
, len
);
5624 * A function definition is added if it matches:
5625 * <beginning of line><Erlang Atom><whitespace>(
5627 * It is added to the tags database if it doesn't match the
5628 * name of the previous clause header.
5630 * Return the size of the name of the function, or 0 if no function
5634 erlang_func (s
, last
)
5636 char *last
; /* Name of last clause. */
5641 pos
= erlang_atom (s
);
5646 pos
= skip_spaces (s
+ pos
) - s
;
5648 /* Save only the first clause. */
5651 || len
!= (int)strlen (last
)
5652 || !strneq (s
, last
, len
)))
5654 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5663 * Handle attributes. Currently, tags are generated for defines
5666 * They are on the form:
5667 * -define(foo, bar).
5668 * -define(Foo(M, N), M+N).
5669 * -record(graph, {vtab = notable, cyclic = true}).
5672 erlang_attribute (s
)
5677 if ((LOOKING_AT (cp
, "-define") || LOOKING_AT (cp
, "-record"))
5680 int len
= erlang_atom (skip_spaces (cp
));
5682 make_tag (cp
, len
, TRUE
, s
, cp
+ len
- s
, lineno
, linecharno
);
5689 * Consume an Erlang atom (or variable).
5690 * Return the number of bytes consumed, or -1 if there was an error.
5698 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
5700 /* The atom is unquoted. */
5703 while (ISALNUM (s
[pos
]) || s
[pos
] == '_');
5705 else if (s
[pos
] == '\'')
5707 for (pos
++; s
[pos
] != '\''; pos
++)
5708 if (s
[pos
] == '\0' /* multiline quoted atoms are ignored */
5709 || (s
[pos
] == '\\' && s
[++pos
] == '\0'))
5718 static char *scan_separators
__P((char *));
5719 static void add_regex
__P((char *, language
*));
5720 static char *substitute
__P((char *, char *, struct re_registers
*));
5723 * Take a string like "/blah/" and turn it into "blah", verifying
5724 * that the first and last characters are the same, and handling
5725 * quoted separator characters. Actually, stops on the occurrence of
5726 * an unquoted separator. Also process \t, \n, etc. and turn into
5727 * appropriate characters. Works in place. Null terminates name string.
5728 * Returns pointer to terminating separator, or NULL for
5729 * unterminated regexps.
5732 scan_separators (name
)
5736 char *copyto
= name
;
5737 bool quoted
= FALSE
;
5739 for (++name
; *name
!= '\0'; ++name
)
5745 case 'a': *copyto
++ = '\007'; break; /* BEL (bell) */
5746 case 'b': *copyto
++ = '\b'; break; /* BS (back space) */
5747 case 'd': *copyto
++ = 0177; break; /* DEL (delete) */
5748 case 'e': *copyto
++ = 033; break; /* ESC (delete) */
5749 case 'f': *copyto
++ = '\f'; break; /* FF (form feed) */
5750 case 'n': *copyto
++ = '\n'; break; /* NL (new line) */
5751 case 'r': *copyto
++ = '\r'; break; /* CR (carriage return) */
5752 case 't': *copyto
++ = '\t'; break; /* TAB (horizontal tab) */
5753 case 'v': *copyto
++ = '\v'; break; /* VT (vertical tab) */
5759 /* Something else is quoted, so preserve the quote. */
5767 else if (*name
== '\\')
5769 else if (*name
== sep
)
5775 name
= NULL
; /* signal unterminated regexp */
5777 /* Terminate copied string. */
5782 /* Look at the argument of --regex or --no-regex and do the right
5783 thing. Same for each line of a regexp file. */
5785 analyse_regex (regex_arg
)
5788 if (regex_arg
== NULL
)
5790 free_regexps (); /* --no-regex: remove existing regexps */
5794 /* A real --regexp option or a line in a regexp file. */
5795 switch (regex_arg
[0])
5797 /* Comments in regexp file or null arg to --regex. */
5803 /* Read a regex file. This is recursive and may result in a
5804 loop, which will stop when the file descriptors are exhausted. */
5808 linebuffer regexbuf
;
5809 char *regexfile
= regex_arg
+ 1;
5811 /* regexfile is a file containing regexps, one per line. */
5812 regexfp
= fopen (regexfile
, "r");
5813 if (regexfp
== NULL
)
5818 linebuffer_init (®exbuf
);
5819 while (readline_internal (®exbuf
, regexfp
) > 0)
5820 analyse_regex (regexbuf
.buffer
);
5821 free (regexbuf
.buffer
);
5826 /* Regexp to be used for a specific language only. */
5830 char *lang_name
= regex_arg
+ 1;
5833 for (cp
= lang_name
; *cp
!= '}'; cp
++)
5836 error ("unterminated language name in regex: %s", regex_arg
);
5840 lang
= get_language_from_langname (lang_name
);
5843 add_regex (cp
, lang
);
5847 /* Regexp to be used for any language. */
5849 add_regex (regex_arg
, NULL
);
5854 /* Separate the regexp pattern, compile it,
5855 and care for optional name and modifiers. */
5857 add_regex (regexp_pattern
, lang
)
5858 char *regexp_pattern
;
5861 static struct re_pattern_buffer zeropattern
;
5862 char sep
, *pat
, *name
, *modifiers
;
5864 struct re_pattern_buffer
*patbuf
;
5867 force_explicit_name
= TRUE
, /* do not use implicit tag names */
5868 ignore_case
= FALSE
, /* case is significant */
5869 multi_line
= FALSE
, /* matches are done one line at a time */
5870 single_line
= FALSE
; /* dot does not match newline */
5873 if (strlen(regexp_pattern
) < 3)
5875 error ("null regexp", (char *)NULL
);
5878 sep
= regexp_pattern
[0];
5879 name
= scan_separators (regexp_pattern
);
5882 error ("%s: unterminated regexp", regexp_pattern
);
5887 error ("null name for regexp \"%s\"", regexp_pattern
);
5890 modifiers
= scan_separators (name
);
5891 if (modifiers
== NULL
) /* no terminating separator --> no name */
5897 modifiers
+= 1; /* skip separator */
5899 /* Parse regex modifiers. */
5900 for (; modifiers
[0] != '\0'; modifiers
++)
5901 switch (modifiers
[0])
5904 if (modifiers
== name
)
5905 error ("forcing explicit tag name but no name, ignoring", NULL
);
5906 force_explicit_name
= TRUE
;
5916 need_filebuf
= TRUE
;
5921 wrongmod
[0] = modifiers
[0];
5923 error ("invalid regexp modifier `%s', ignoring", wrongmod
);
5928 patbuf
= xnew (1, struct re_pattern_buffer
);
5929 *patbuf
= zeropattern
;
5932 static char lc_trans
[CHARS
];
5934 for (i
= 0; i
< CHARS
; i
++)
5935 lc_trans
[i
] = lowcase (i
);
5936 patbuf
->translate
= lc_trans
; /* translation table to fold case */
5940 pat
= concat ("^", regexp_pattern
, ""); /* anchor to beginning of line */
5942 pat
= regexp_pattern
;
5945 re_set_syntax (RE_SYNTAX_EMACS
| RE_DOT_NEWLINE
);
5947 re_set_syntax (RE_SYNTAX_EMACS
);
5949 err
= re_compile_pattern (pat
, strlen (regexp_pattern
), patbuf
);
5954 error ("%s while compiling pattern", err
);
5959 p_head
= xnew (1, regexp
);
5960 p_head
->pattern
= savestr (regexp_pattern
);
5961 p_head
->p_next
= rp
;
5962 p_head
->lang
= lang
;
5963 p_head
->pat
= patbuf
;
5964 p_head
->name
= savestr (name
);
5965 p_head
->error_signaled
= FALSE
;
5966 p_head
->force_explicit_name
= force_explicit_name
;
5967 p_head
->ignore_case
= ignore_case
;
5968 p_head
->multi_line
= multi_line
;
5972 * Do the substitutions indicated by the regular expression and
5976 substitute (in
, out
, regs
)
5978 struct re_registers
*regs
;
5981 int size
, dig
, diglen
;
5984 size
= strlen (out
);
5986 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5987 if (out
[size
- 1] == '\\')
5988 fatal ("pattern error in \"%s\"", out
);
5989 for (t
= etags_strchr (out
, '\\');
5991 t
= etags_strchr (t
+ 2, '\\'))
5995 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
6001 /* Allocate space and do the substitutions. */
6003 result
= xnew (size
+ 1, char);
6005 for (t
= result
; *out
!= '\0'; out
++)
6006 if (*out
== '\\' && ISDIGIT (*++out
))
6009 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
6010 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
6017 assert (t
<= result
+ size
);
6018 assert (t
- result
== (int)strlen (result
));
6023 /* Deallocate all regexps. */
6028 while (p_head
!= NULL
)
6030 rp
= p_head
->p_next
;
6031 free (p_head
->pattern
);
6032 free (p_head
->name
);
6040 * Reads the whole file as a single string from `filebuf' and looks for
6041 * multi-line regular expressions, creating tags on matches.
6042 * readline already dealt with normal regexps.
6044 * Idea by Ben Wing <ben@666.com> (2002).
6047 regex_tag_multiline ()
6049 char *buffer
= filebuf
.buffer
;
6053 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6057 if (!rp
->multi_line
)
6058 continue; /* skip normal regexps */
6060 /* Generic initialisations before parsing file from memory. */
6061 lineno
= 1; /* reset global line number */
6062 charno
= 0; /* reset global char number */
6063 linecharno
= 0; /* reset global char number of line start */
6065 /* Only use generic regexps or those for the current language. */
6066 if (rp
->lang
!= NULL
&& rp
->lang
!= curfdp
->lang
)
6069 while (match
>= 0 && match
< filebuf
.len
)
6071 match
= re_search (rp
->pat
, buffer
, filebuf
.len
, charno
,
6072 filebuf
.len
- match
, &rp
->regs
);
6077 if (!rp
->error_signaled
)
6079 error ("regexp stack overflow while matching \"%s\"",
6081 rp
->error_signaled
= TRUE
;
6088 if (match
== rp
->regs
.end
[0])
6090 if (!rp
->error_signaled
)
6092 error ("regexp matches the empty string: \"%s\"",
6094 rp
->error_signaled
= TRUE
;
6096 match
= -3; /* exit from while loop */
6100 /* Match occurred. Construct a tag. */
6101 while (charno
< rp
->regs
.end
[0])
6102 if (buffer
[charno
++] == '\n')
6103 lineno
++, linecharno
= charno
;
6105 if (name
[0] == '\0')
6107 else /* make a named tag */
6108 name
= substitute (buffer
, rp
->name
, &rp
->regs
);
6109 if (rp
->force_explicit_name
)
6110 /* Force explicit tag name, if a name is there. */
6111 pfnote (name
, TRUE
, buffer
+ linecharno
,
6112 charno
- linecharno
+ 1, lineno
, linecharno
);
6114 make_tag (name
, strlen (name
), TRUE
, buffer
+ linecharno
,
6115 charno
- linecharno
+ 1, lineno
, linecharno
);
6127 register int len
= 0;
6129 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
6131 if (*cp
== '\0' && !intoken (dbp
[len
]))
6140 get_tag (bp
, namepp
)
6144 register char *cp
= bp
;
6148 /* Go till you get to white space or a syntactic break */
6149 for (cp
= bp
+ 1; !notinname (*cp
); cp
++)
6151 make_tag (bp
, cp
- bp
, TRUE
,
6152 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
6156 *namepp
= savenstr (bp
, cp
- bp
);
6160 * Read a line of text from `stream' into `lbp', excluding the
6161 * newline or CR-NL, if any. Return the number of characters read from
6162 * `stream', which is the length of the line including the newline.
6164 * On DOS or Windows we do not count the CR character, if any before the
6165 * NL, in the returned length; this mirrors the behavior of Emacs on those
6166 * platforms (for text files, it translates CR-NL to NL as it reads in the
6169 * If multi-line regular expressions are requested, each line read is
6170 * appended to `filebuf'.
6173 readline_internal (lbp
, stream
)
6175 register FILE *stream
;
6177 char *buffer
= lbp
->buffer
;
6178 register char *p
= lbp
->buffer
;
6179 register char *pend
;
6182 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
6186 register int c
= getc (stream
);
6189 /* We're at the end of linebuffer: expand it. */
6191 xrnew (buffer
, lbp
->size
, char);
6192 p
+= buffer
- lbp
->buffer
;
6193 pend
= buffer
+ lbp
->size
;
6194 lbp
->buffer
= buffer
;
6204 if (p
> buffer
&& p
[-1] == '\r')
6208 /* Assume CRLF->LF translation will be performed by Emacs
6209 when loading this file, so CRs won't appear in the buffer.
6210 It would be cleaner to compensate within Emacs;
6211 however, Emacs does not know how many CRs were deleted
6212 before any given point in the file. */
6227 lbp
->len
= p
- buffer
;
6229 if (need_filebuf
/* we need filebuf for multi-line regexps */
6230 && chars_deleted
> 0) /* not at EOF */
6232 while (filebuf
.size
<= filebuf
.len
+ lbp
->len
+ 1) /* +1 for \n */
6234 /* Expand filebuf. */
6236 xrnew (filebuf
.buffer
, filebuf
.size
, char);
6238 strncpy (filebuf
.buffer
+ filebuf
.len
, lbp
->buffer
, lbp
->len
);
6239 filebuf
.len
+= lbp
->len
;
6240 filebuf
.buffer
[filebuf
.len
++] = '\n';
6241 filebuf
.buffer
[filebuf
.len
] = '\0';
6244 return lbp
->len
+ chars_deleted
;
6248 * Like readline_internal, above, but in addition try to match the
6249 * input line against relevant regular expressions and manage #line
6253 readline (lbp
, stream
)
6259 linecharno
= charno
; /* update global char number of line start */
6260 result
= readline_internal (lbp
, stream
); /* read line */
6261 lineno
+= 1; /* increment global line number */
6262 charno
+= result
; /* increment global char number */
6264 /* Honour #line directives. */
6265 if (!no_line_directive
)
6267 static bool discard_until_line_directive
;
6269 /* Check whether this is a #line directive. */
6270 if (result
> 12 && strneq (lbp
->buffer
, "#line ", 6))
6275 if (sscanf (lbp
->buffer
, "#line %u \"%n", &lno
, &start
) >= 1
6276 && start
> 0) /* double quote character found */
6278 char *endp
= lbp
->buffer
+ start
;
6280 while ((endp
= etags_strchr (endp
, '"')) != NULL
6281 && endp
[-1] == '\\')
6284 /* Ok, this is a real #line directive. Let's deal with it. */
6286 char *taggedabsname
; /* absolute name of original file */
6287 char *taggedfname
; /* name of original file as given */
6288 char *name
; /* temp var */
6290 discard_until_line_directive
= FALSE
; /* found it */
6291 name
= lbp
->buffer
+ start
;
6293 canonicalize_filename (name
); /* for DOS */
6294 taggedabsname
= absolute_filename (name
, tagfiledir
);
6295 if (filename_is_absolute (name
)
6296 || filename_is_absolute (curfdp
->infname
))
6297 taggedfname
= savestr (taggedabsname
);
6299 taggedfname
= relative_filename (taggedabsname
,tagfiledir
);
6301 if (streq (curfdp
->taggedfname
, taggedfname
))
6302 /* The #line directive is only a line number change. We
6303 deal with this afterwards. */
6306 /* The tags following this #line directive should be
6307 attributed to taggedfname. In order to do this, set
6308 curfdp accordingly. */
6310 fdesc
*fdp
; /* file description pointer */
6312 /* Go look for a file description already set up for the
6313 file indicated in the #line directive. If there is
6314 one, use it from now until the next #line
6316 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6317 if (streq (fdp
->infname
, curfdp
->infname
)
6318 && streq (fdp
->taggedfname
, taggedfname
))
6319 /* If we remove the second test above (after the &&)
6320 then all entries pertaining to the same file are
6321 coalesced in the tags file. If we use it, then
6322 entries pertaining to the same file but generated
6323 from different files (via #line directives) will
6324 go into separate sections in the tags file. These
6325 alternatives look equivalent. The first one
6326 destroys some apparently useless information. */
6332 /* Else, if we already tagged the real file, skip all
6333 input lines until the next #line directive. */
6334 if (fdp
== NULL
) /* not found */
6335 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6336 if (streq (fdp
->infabsname
, taggedabsname
))
6338 discard_until_line_directive
= TRUE
;
6342 /* Else create a new file description and use that from
6343 now on, until the next #line directive. */
6344 if (fdp
== NULL
) /* not found */
6347 fdhead
= xnew (1, fdesc
);
6348 *fdhead
= *curfdp
; /* copy curr. file description */
6350 fdhead
->infname
= savestr (curfdp
->infname
);
6351 fdhead
->infabsname
= savestr (curfdp
->infabsname
);
6352 fdhead
->infabsdir
= savestr (curfdp
->infabsdir
);
6353 fdhead
->taggedfname
= taggedfname
;
6354 fdhead
->usecharno
= FALSE
;
6355 fdhead
->prop
= NULL
;
6356 fdhead
->written
= FALSE
;
6360 free (taggedabsname
);
6362 readline (lbp
, stream
);
6364 } /* if a real #line directive */
6365 } /* if #line is followed by a a number */
6366 } /* if line begins with "#line " */
6368 /* If we are here, no #line directive was found. */
6369 if (discard_until_line_directive
)
6373 /* Do a tail recursion on ourselves, thus discarding the contents
6374 of the line buffer. */
6375 readline (lbp
, stream
);
6379 discard_until_line_directive
= FALSE
;
6382 } /* if #line directives should be considered */
6389 /* Match against relevant regexps. */
6391 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6393 /* Only use generic regexps or those for the current language.
6394 Also do not use multiline regexps, which is the job of
6395 regex_tag_multiline. */
6396 if ((rp
->lang
!= NULL
&& rp
->lang
!= fdhead
->lang
)
6400 match
= re_match (rp
->pat
, lbp
->buffer
, lbp
->len
, 0, &rp
->regs
);
6405 if (!rp
->error_signaled
)
6407 error ("regexp stack overflow while matching \"%s\"",
6409 rp
->error_signaled
= TRUE
;
6416 /* Empty string matched. */
6417 if (!rp
->error_signaled
)
6419 error ("regexp matches the empty string: \"%s\"", rp
->pattern
);
6420 rp
->error_signaled
= TRUE
;
6424 /* Match occurred. Construct a tag. */
6426 if (name
[0] == '\0')
6428 else /* make a named tag */
6429 name
= substitute (lbp
->buffer
, rp
->name
, &rp
->regs
);
6430 if (rp
->force_explicit_name
)
6431 /* Force explicit tag name, if a name is there. */
6432 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
6434 make_tag (name
, strlen (name
), TRUE
,
6435 lbp
->buffer
, match
, lineno
, linecharno
);
6444 * Return a pointer to a space of size strlen(cp)+1 allocated
6445 * with xnew where the string CP has been copied.
6451 return savenstr (cp
, strlen (cp
));
6455 * Return a pointer to a space of size LEN+1 allocated with xnew where
6456 * the string CP has been copied for at most the first LEN characters.
6465 dp
= xnew (len
+ 1, char);
6466 strncpy (dp
, cp
, len
);
6472 * Return the ptr in sp at which the character c last
6473 * appears; NULL if not found
6475 * Identical to POSIX strrchr, included for portability.
6478 etags_strrchr (sp
, c
)
6479 register const char *sp
;
6482 register const char *r
;
6494 * Return the ptr in sp at which the character c first
6495 * appears; NULL if not found
6497 * Identical to POSIX strchr, included for portability.
6500 etags_strchr (sp
, c
)
6501 register const char *sp
;
6513 * Compare two strings, ignoring case for alphabetic characters.
6515 * Same as BSD's strcasecmp, included for portability.
6518 etags_strcasecmp (s1
, s2
)
6519 register const char *s1
;
6520 register const char *s2
;
6523 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6524 ? lowcase (*s1
) == lowcase (*s2
)
6528 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6529 ? lowcase (*s1
) - lowcase (*s2
)
6534 * Compare two strings, ignoring case for alphabetic characters.
6535 * Stop after a given number of characters
6537 * Same as BSD's strncasecmp, included for portability.
6540 etags_strncasecmp (s1
, s2
, n
)
6541 register const char *s1
;
6542 register const char *s2
;
6545 while (*s1
!= '\0' && n
-- > 0
6546 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6547 ? lowcase (*s1
) == lowcase (*s2
)
6554 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6555 ? lowcase (*s1
) - lowcase (*s2
)
6559 /* Skip spaces (end of string is not space), return new pointer. */
6564 while (iswhite (*cp
))
6569 /* Skip non spaces, except end of string, return new pointer. */
6571 skip_non_spaces (cp
)
6574 while (*cp
!= '\0' && !iswhite (*cp
))
6579 /* Print error message and exit. */
6585 exit (EXIT_FAILURE
);
6593 exit (EXIT_FAILURE
);
6597 suggest_asking_for_help ()
6599 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
6600 progname
, NO_LONG_OPTIONS
? "-h" : "--help");
6601 exit (EXIT_FAILURE
);
6604 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
6607 const char *s1
, *s2
;
6609 fprintf (stderr
, "%s: ", progname
);
6610 fprintf (stderr
, s1
, s2
);
6611 fprintf (stderr
, "\n");
6614 /* Return a newly-allocated string whose contents
6615 concatenate those of s1, s2, s3. */
6620 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
6621 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
6623 strcpy (result
, s1
);
6624 strcpy (result
+ len1
, s2
);
6625 strcpy (result
+ len1
+ len2
, s3
);
6626 result
[len1
+ len2
+ len3
] = '\0';
6632 /* Does the same work as the system V getcwd, but does not need to
6633 guess the buffer size in advance. */
6639 char *path
= xnew (bufsize
, char);
6641 while (getcwd (path
, bufsize
) == NULL
)
6643 if (errno
!= ERANGE
)
6647 path
= xnew (bufsize
, char);
6650 canonicalize_filename (path
);
6653 #else /* not HAVE_GETCWD */
6656 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
6660 for (p
= path
; *p
!= '\0'; p
++)
6666 return strdup (path
);
6667 #else /* not MSDOS */
6671 linebuffer_init (&path
);
6672 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
6673 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
6678 #endif /* not MSDOS */
6679 #endif /* not HAVE_GETCWD */
6682 /* Return a newly allocated string containing the file name of FILE
6683 relative to the absolute directory DIR (which should end with a slash). */
6685 relative_filename (file
, dir
)
6688 char *fp
, *dp
, *afn
, *res
;
6691 /* Find the common root of file and dir (with a trailing slash). */
6692 afn
= absolute_filename (file
, cwd
);
6695 while (*fp
++ == *dp
++)
6697 fp
--, dp
--; /* back to the first differing char */
6699 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
6702 do /* look at the equal chars until '/' */
6706 /* Build a sequence of "../" strings for the resulting relative file name. */
6708 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
6710 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
6713 strcat (res
, "../");
6715 /* Add the file name relative to the common root of file and dir. */
6716 strcat (res
, fp
+ 1);
6722 /* Return a newly allocated string containing the absolute file name
6723 of FILE given DIR (which should end with a slash). */
6725 absolute_filename (file
, dir
)
6728 char *slashp
, *cp
, *res
;
6730 if (filename_is_absolute (file
))
6731 res
= savestr (file
);
6733 /* We don't support non-absolute file names with a drive
6734 letter, like `d:NAME' (it's too much hassle). */
6735 else if (file
[1] == ':')
6736 fatal ("%s: relative file names with drive letters not supported", file
);
6739 res
= concat (dir
, file
, "");
6741 /* Delete the "/dirname/.." and "/." substrings. */
6742 slashp
= etags_strchr (res
, '/');
6743 while (slashp
!= NULL
&& slashp
[0] != '\0')
6745 if (slashp
[1] == '.')
6747 if (slashp
[2] == '.'
6748 && (slashp
[3] == '/' || slashp
[3] == '\0'))
6753 while (cp
>= res
&& !filename_is_absolute (cp
));
6755 cp
= slashp
; /* the absolute name begins with "/.." */
6757 /* Under MSDOS and NT we get `d:/NAME' as absolute
6758 file name, so the luser could say `d:/../NAME'.
6759 We silently treat this as `d:/NAME'. */
6760 else if (cp
[0] != '/')
6763 strcpy (cp
, slashp
+ 3);
6767 else if (slashp
[2] == '/' || slashp
[2] == '\0')
6769 strcpy (slashp
, slashp
+ 2);
6774 slashp
= etags_strchr (slashp
+ 1, '/');
6777 if (res
[0] == '\0') /* just a safety net: should never happen */
6780 return savestr ("/");
6786 /* Return a newly allocated string containing the absolute
6787 file name of dir where FILE resides given DIR (which should
6788 end with a slash). */
6790 absolute_dirname (file
, dir
)
6796 canonicalize_filename (file
);
6797 slashp
= etags_strrchr (file
, '/');
6799 return savestr (dir
);
6802 res
= absolute_filename (file
, dir
);
6808 /* Whether the argument string is an absolute file name. The argument
6809 string must have been canonicalized with canonicalize_filename. */
6811 filename_is_absolute (fn
)
6814 return (fn
[0] == '/'
6816 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
6821 /* Translate backslashes into slashes. Works in place. */
6823 canonicalize_filename (fn
)
6827 /* Canonicalize drive letter case. */
6828 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
6829 fn
[0] = upcase (fn
[0]);
6830 /* Convert backslashes to slashes. */
6831 for (; *fn
!= '\0'; fn
++)
6836 fn
= NULL
; /* shut up the compiler */
6841 /* Initialize a linebuffer for use */
6843 linebuffer_init (lbp
)
6846 lbp
->size
= (DEBUG
) ? 3 : 200;
6847 lbp
->buffer
= xnew (lbp
->size
, char);
6848 lbp
->buffer
[0] = '\0';
6852 /* Set the minimum size of a string contained in a linebuffer. */
6854 linebuffer_setlen (lbp
, toksize
)
6858 while (lbp
->size
<= toksize
)
6861 xrnew (lbp
->buffer
, lbp
->size
, char);
6866 /* Like malloc but get fatal error if memory is exhausted. */
6871 PTR result
= (PTR
) malloc (size
);
6873 fatal ("virtual memory exhausted", (char *)NULL
);
6878 xrealloc (ptr
, size
)
6882 PTR result
= (PTR
) realloc (ptr
, size
);
6884 fatal ("virtual memory exhausted", (char *)NULL
);
6890 * indent-tabs-mode: t
6893 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
6897 /* arch-tag: 8a9b748d-390c-4922-99db-2eeefa921051
6898 (do not change this comment) */
6900 /* etags.c ends here */