1 /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
3 Copyright (C) 1984 The Regents of the University of California
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the
14 3. Neither the name of the University nor the names of its
15 contributors may be used to endorse or promote products derived
16 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 Copyright (C) 1984, 1987-1989, 1993-1995, 1998-2012
32 Free Software Foundation, Inc.
34 This file is not considered part of GNU Emacs.
36 This program is free software: you can redistribute it and/or modify
37 it under the terms of the GNU General Public License as published by
38 the Free Software Foundation, either version 3 of the License, or
39 (at your option) any later version.
41 This program is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 GNU General Public License for more details.
46 You should have received a copy of the GNU General Public License
47 along with this program. If not, see <http://www.gnu.org/licenses/>. */
50 /* NB To comply with the above BSD license, copyright information is
51 reproduced in etc/ETAGS.README. That file should be updated when the
54 To the best of our knowledge, this code was originally based on the
55 ctags.c distributed with BSD4.2, which was copyrighted by the
56 University of California, as described above. */
61 * 1983 Ctags originally by Ken Arnold.
62 * 1984 Fortran added by Jim Kleckner.
63 * 1984 Ed Pelegri-Llopart added C typedefs.
64 * 1985 Emacs TAGS format by Richard Stallman.
65 * 1989 Sam Kendall added C++.
66 * 1992 Joseph B. Wells improved C and C++ parsing.
67 * 1993 Francesco Potortì reorganized C and C++.
68 * 1994 Line-by-line regexp tags by Tom Tromey.
69 * 2001 Nested classes by Francesco Potortì (concept by Mykola Dzyuba).
70 * 2002 #line directives by Francesco Potortì.
72 * Francesco Potortì <pot@gnu.org> has maintained and improved it since 1993.
76 * If you want to add support for a new language, start by looking at the LUA
77 * language, which is the simplest. Alternatively, consider distributing etags
78 * together with a configuration file containing regexp definitions for etags.
81 char pot_etags_version
[] = "@(#) pot revision number is 17.38.1.4";
91 # define NDEBUG /* disable assert */
97 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
100 /* WIN32_NATIVE is for XEmacs.
101 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
106 #endif /* WIN32_NATIVE */
112 # include <sys/param.h>
122 # define MAXPATHLEN _MAX_PATH
126 #endif /* WINDOWSNT */
135 #include <sys/types.h>
136 #include <sys/stat.h>
137 #include <c-strcase.h>
141 # undef assert /* some systems have a buggy assert.h */
142 # define assert(x) ((void) 0)
148 /* Define CTAGS to make the program "ctags" compatible with the usual one.
149 Leave it undefined to make the program "etags", which makes emacs-style
150 tag tables and tags typedefs, #defines and struct/union/enum by default. */
158 #define streq(s,t) (assert ((s)!=NULL || (t)!=NULL), !strcmp (s, t))
159 #define strcaseeq(s,t) (assert ((s)!=NULL && (t)!=NULL), !c_strcasecmp (s, t))
160 #define strneq(s,t,n) (assert ((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
161 #define strncaseeq(s,t,n) (assert ((s)!=NULL && (t)!=NULL), !c_strncasecmp (s, t, n))
163 #define CHARS 256 /* 2^sizeof(char) */
164 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
165 #define iswhite(c) (_wht[CHAR (c)]) /* c is white (see white) */
166 #define notinname(c) (_nin[CHAR (c)]) /* c is not in a name (see nonam) */
167 #define begtoken(c) (_btk[CHAR (c)]) /* c can start token (see begtk) */
168 #define intoken(c) (_itk[CHAR (c)]) /* c can be in token (see midtk) */
169 #define endtoken(c) (_etk[CHAR (c)]) /* c ends tokens (see endtk) */
171 #define ISALNUM(c) isalnum (CHAR (c))
172 #define ISALPHA(c) isalpha (CHAR (c))
173 #define ISDIGIT(c) isdigit (CHAR (c))
174 #define ISLOWER(c) islower (CHAR (c))
176 #define lowcase(c) tolower (CHAR (c))
180 * xnew, xrnew -- allocate, reallocate storage
182 * SYNOPSIS: Type *xnew (int n, Type);
183 * void xrnew (OldPointer, int n, Type);
186 # include "chkmalloc.h"
187 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
188 (n) * sizeof (Type)))
189 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
190 (char *) (op), (n) * sizeof (Type)))
192 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
193 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
194 (char *) (op), (n) * sizeof (Type)))
199 typedef void Lang_function (FILE *);
203 const char *suffix
; /* file name suffix for this compressor */
204 const char *command
; /* takes one arg and decompresses to stdout */
209 const char *name
; /* language name */
210 const char *help
; /* detailed help for the language */
211 Lang_function
*function
; /* parse function */
212 const char **suffixes
; /* name suffixes of this language's files */
213 const char **filenames
; /* names of this language's files */
214 const char **interpreters
; /* interpreters for this language */
215 bool metasource
; /* source used to generate other sources */
220 struct fdesc
*next
; /* for the linked list */
221 char *infname
; /* uncompressed input file name */
222 char *infabsname
; /* absolute uncompressed input file name */
223 char *infabsdir
; /* absolute dir of input file */
224 char *taggedfname
; /* file name to write in tagfile */
225 language
*lang
; /* language of file */
226 char *prop
; /* file properties to write in tagfile */
227 bool usecharno
; /* etags tags shall contain char number */
228 bool written
; /* entry written in the tags file */
231 typedef struct node_st
232 { /* sorting structure */
233 struct node_st
*left
, *right
; /* left and right sons */
234 fdesc
*fdp
; /* description of file to whom tag belongs */
235 char *name
; /* tag name */
236 char *regex
; /* search regexp */
237 bool valid
; /* write this tag on the tag file */
238 bool is_func
; /* function tag: use regexp in CTAGS mode */
239 bool been_warned
; /* warning already given for duplicated tag */
240 int lno
; /* line number tag is on */
241 long cno
; /* character number line starts on */
245 * A `linebuffer' is a structure which holds a line of text.
246 * `readline_internal' reads a line from a stream into a linebuffer
247 * and works regardless of the length of the line.
248 * SIZE is the size of BUFFER, LEN is the length of the string in
249 * BUFFER after readline reads it.
258 /* Used to support mixing of --lang and file names. */
262 at_language
, /* a language specification */
263 at_regexp
, /* a regular expression */
264 at_filename
, /* a file name */
265 at_stdin
, /* read from stdin here */
266 at_end
/* stop parsing the list */
267 } arg_type
; /* argument type */
268 language
*lang
; /* language associated with the argument */
269 char *what
; /* the argument itself */
272 /* Structure defining a regular expression. */
273 typedef struct regexp
275 struct regexp
*p_next
; /* pointer to next in list */
276 language
*lang
; /* if set, use only for this language */
277 char *pattern
; /* the regexp pattern */
278 char *name
; /* tag name */
279 struct re_pattern_buffer
*pat
; /* the compiled pattern */
280 struct re_registers regs
; /* re registers */
281 bool error_signaled
; /* already signaled for this regexp */
282 bool force_explicit_name
; /* do not allow implicit tag name */
283 bool ignore_case
; /* ignore case when matching */
284 bool multi_line
; /* do a multi-line match on the whole file */
288 /* Many compilers barf on this:
289 Lang_function Ada_funcs;
290 so let's write it this way */
291 static void Ada_funcs (FILE *);
292 static void Asm_labels (FILE *);
293 static void C_entries (int c_ext
, FILE *);
294 static void default_C_entries (FILE *);
295 static void plain_C_entries (FILE *);
296 static void Cjava_entries (FILE *);
297 static void Cobol_paragraphs (FILE *);
298 static void Cplusplus_entries (FILE *);
299 static void Cstar_entries (FILE *);
300 static void Erlang_functions (FILE *);
301 static void Forth_words (FILE *);
302 static void Fortran_functions (FILE *);
303 static void HTML_labels (FILE *);
304 static void Lisp_functions (FILE *);
305 static void Lua_functions (FILE *);
306 static void Makefile_targets (FILE *);
307 static void Pascal_functions (FILE *);
308 static void Perl_functions (FILE *);
309 static void PHP_functions (FILE *);
310 static void PS_functions (FILE *);
311 static void Prolog_functions (FILE *);
312 static void Python_functions (FILE *);
313 static void Scheme_functions (FILE *);
314 static void TeX_commands (FILE *);
315 static void Texinfo_nodes (FILE *);
316 static void Yacc_entries (FILE *);
317 static void just_read_file (FILE *);
319 static void print_language_names (void);
320 static void print_version (void);
321 static void print_help (argument
*);
322 int main (int, char **);
324 static compressor
*get_compressor_from_suffix (char *, char **);
325 static language
*get_language_from_langname (const char *);
326 static language
*get_language_from_interpreter (char *);
327 static language
*get_language_from_filename (char *, bool);
328 static void readline (linebuffer
*, FILE *);
329 static long readline_internal (linebuffer
*, FILE *);
330 static bool nocase_tail (const char *);
331 static void get_tag (char *, char **);
333 static void analyse_regex (char *);
334 static void free_regexps (void);
335 static void regex_tag_multiline (void);
336 static void error (const char *, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
337 static _Noreturn
void suggest_asking_for_help (void);
338 _Noreturn
void fatal (const char *, const char *);
339 static _Noreturn
void pfatal (const char *);
340 static void add_node (node
*, node
**);
342 static void init (void);
343 static void process_file_name (char *, language
*);
344 static void process_file (FILE *, char *, language
*);
345 static void find_entries (FILE *);
346 static void free_tree (node
*);
347 static void free_fdesc (fdesc
*);
348 static void pfnote (char *, bool, char *, int, int, long);
349 static void make_tag (const char *, int, bool, char *, int, int, long);
350 static void invalidate_nodes (fdesc
*, node
**);
351 static void put_entries (node
*);
353 static char *concat (const char *, const char *, const char *);
354 static char *skip_spaces (char *);
355 static char *skip_non_spaces (char *);
356 static char *skip_name (char *);
357 static char *savenstr (const char *, int);
358 static char *savestr (const char *);
359 static char *etags_strchr (const char *, int);
360 static char *etags_strrchr (const char *, int);
361 static char *etags_getcwd (void);
362 static char *relative_filename (char *, char *);
363 static char *absolute_filename (char *, char *);
364 static char *absolute_dirname (char *, char *);
365 static bool filename_is_absolute (char *f
);
366 static void canonicalize_filename (char *);
367 static void linebuffer_init (linebuffer
*);
368 static void linebuffer_setlen (linebuffer
*, int);
369 static void *xmalloc (size_t);
370 static void *xrealloc (char *, size_t);
373 static char searchar
= '/'; /* use /.../ searches */
375 static char *tagfile
; /* output file */
376 static char *progname
; /* name this program was invoked with */
377 static char *cwd
; /* current working directory */
378 static char *tagfiledir
; /* directory of tagfile */
379 static FILE *tagf
; /* ioptr for tags file */
380 static ptrdiff_t whatlen_max
; /* maximum length of any 'what' member */
382 static fdesc
*fdhead
; /* head of file description list */
383 static fdesc
*curfdp
; /* current file description */
384 static int lineno
; /* line number of current line */
385 static long charno
; /* current character number */
386 static long linecharno
; /* charno of start of current line */
387 static char *dbp
; /* pointer to start of current tag */
389 static const int invalidcharno
= -1;
391 static node
*nodehead
; /* the head of the binary tree of tags */
392 static node
*last_node
; /* the last node created */
394 static linebuffer lb
; /* the current line */
395 static linebuffer filebuf
; /* a buffer containing the whole file */
396 static linebuffer token_name
; /* a buffer containing a tag name */
398 /* boolean "functions" (see init) */
399 static bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
402 *white
= " \f\t\n\r\v",
404 *nonam
= " \f\t\n\r()=,;", /* look at make_tag before modifying! */
405 /* token ending chars */
406 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
407 /* token starting chars */
408 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
409 /* valid in-token chars */
410 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
412 static bool append_to_tagfile
; /* -a: append to tags */
413 /* The next five default to TRUE in C and derived languages. */
414 static bool typedefs
; /* -t: create tags for C and Ada typedefs */
415 static bool typedefs_or_cplusplus
; /* -T: create tags for C typedefs, level */
416 /* 0 struct/enum/union decls, and C++ */
417 /* member functions. */
418 static bool constantypedefs
; /* -d: create tags for C #define, enum */
419 /* constants and variables. */
420 /* -D: opposite of -d. Default under ctags. */
421 static bool globals
; /* create tags for global variables */
422 static bool members
; /* create tags for C member variables */
423 static bool declarations
; /* --declarations: tag them and extern in C&Co*/
424 static bool no_line_directive
; /* ignore #line directives (undocumented) */
425 static bool no_duplicates
; /* no duplicate tags for ctags (undocumented) */
426 static bool update
; /* -u: update tags */
427 static bool vgrind_style
; /* -v: create vgrind style index output */
428 static bool no_warnings
; /* -w: suppress warnings (undocumented) */
429 static bool cxref_style
; /* -x: create cxref style output */
430 static bool cplusplus
; /* .[hc] means C++, not C (undocumented) */
431 static bool ignoreindent
; /* -I: ignore indentation in C */
432 static bool packages_only
; /* --packages-only: in Ada, only tag packages*/
434 /* STDIN is defined in LynxOS system headers */
439 #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
440 static bool parsing_stdin
; /* --parse-stdin used */
442 static regexp
*p_head
; /* list of all regexps */
443 static bool need_filebuf
; /* some regexes are multi-line */
445 static struct option longopts
[] =
447 { "append", no_argument
, NULL
, 'a' },
448 { "packages-only", no_argument
, &packages_only
, TRUE
},
449 { "c++", no_argument
, NULL
, 'C' },
450 { "declarations", no_argument
, &declarations
, TRUE
},
451 { "no-line-directive", no_argument
, &no_line_directive
, TRUE
},
452 { "no-duplicates", no_argument
, &no_duplicates
, TRUE
},
453 { "help", no_argument
, NULL
, 'h' },
454 { "help", no_argument
, NULL
, 'H' },
455 { "ignore-indentation", no_argument
, NULL
, 'I' },
456 { "language", required_argument
, NULL
, 'l' },
457 { "members", no_argument
, &members
, TRUE
},
458 { "no-members", no_argument
, &members
, FALSE
},
459 { "output", required_argument
, NULL
, 'o' },
460 { "regex", required_argument
, NULL
, 'r' },
461 { "no-regex", no_argument
, NULL
, 'R' },
462 { "ignore-case-regex", required_argument
, NULL
, 'c' },
463 { "parse-stdin", required_argument
, NULL
, STDIN
},
464 { "version", no_argument
, NULL
, 'V' },
466 #if CTAGS /* Ctags options */
467 { "backward-search", no_argument
, NULL
, 'B' },
468 { "cxref", no_argument
, NULL
, 'x' },
469 { "defines", no_argument
, NULL
, 'd' },
470 { "globals", no_argument
, &globals
, TRUE
},
471 { "typedefs", no_argument
, NULL
, 't' },
472 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
473 { "update", no_argument
, NULL
, 'u' },
474 { "vgrind", no_argument
, NULL
, 'v' },
475 { "no-warn", no_argument
, NULL
, 'w' },
477 #else /* Etags options */
478 { "no-defines", no_argument
, NULL
, 'D' },
479 { "no-globals", no_argument
, &globals
, FALSE
},
480 { "include", required_argument
, NULL
, 'i' },
485 static compressor compressors
[] =
487 { "z", "gzip -d -c"},
488 { "Z", "gzip -d -c"},
489 { "gz", "gzip -d -c"},
490 { "GZ", "gzip -d -c"},
491 { "bz2", "bzip2 -d -c" },
492 { "xz", "xz -d -c" },
501 static const char *Ada_suffixes
[] =
502 { "ads", "adb", "ada", NULL
};
503 static const char Ada_help
[] =
504 "In Ada code, functions, procedures, packages, tasks and types are\n\
505 tags. Use the `--packages-only' option to create tags for\n\
507 Ada tag names have suffixes indicating the type of entity:\n\
508 Entity type: Qualifier:\n\
509 ------------ ----------\n\
516 Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
517 body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
518 will just search for any tag `bidule'.";
521 static const char *Asm_suffixes
[] =
522 { "a", /* Unix assembler */
523 "asm", /* Microcontroller assembly */
524 "def", /* BSO/Tasking definition includes */
525 "inc", /* Microcontroller include files */
526 "ins", /* Microcontroller include files */
527 "s", "sa", /* Unix assembler */
528 "S", /* cpp-processed Unix assembler */
529 "src", /* BSO/Tasking C compiler output */
532 static const char Asm_help
[] =
533 "In assembler code, labels appearing at the beginning of a line,\n\
534 followed by a colon, are tags.";
537 /* Note that .c and .h can be considered C++, if the --c++ flag was
538 given, or if the `class' or `template' keywords are met inside the file.
539 That is why default_C_entries is called for these. */
540 static const char *default_C_suffixes
[] =
542 #if CTAGS /* C help for Ctags */
543 static const char default_C_help
[] =
544 "In C code, any C function is a tag. Use -t to tag typedefs.\n\
545 Use -T to tag definitions of `struct', `union' and `enum'.\n\
546 Use -d to tag `#define' macro definitions and `enum' constants.\n\
547 Use --globals to tag global variables.\n\
548 You can tag function declarations and external variables by\n\
549 using `--declarations', and struct members by using `--members'.";
550 #else /* C help for Etags */
551 static const char default_C_help
[] =
552 "In C code, any C function or typedef is a tag, and so are\n\
553 definitions of `struct', `union' and `enum'. `#define' macro\n\
554 definitions and `enum' constants are tags unless you specify\n\
555 `--no-defines'. Global variables are tags unless you specify\n\
556 `--no-globals' and so are struct members unless you specify\n\
557 `--no-members'. Use of `--no-globals', `--no-defines' and\n\
558 `--no-members' can make the tags table file much smaller.\n\
559 You can tag function declarations and external variables by\n\
560 using `--declarations'.";
561 #endif /* C help for Ctags and Etags */
563 static const char *Cplusplus_suffixes
[] =
564 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
565 "M", /* Objective C++ */
566 "pdb", /* PostScript with C syntax */
568 static const char Cplusplus_help
[] =
569 "In C++ code, all the tag constructs of C code are tagged. (Use\n\
570 --help --lang=c --lang=c++ for full help.)\n\
571 In addition to C tags, member functions are also recognized. Member\n\
572 variables are recognized unless you use the `--no-members' option.\n\
573 Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
574 and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
577 static const char *Cjava_suffixes
[] =
579 static char Cjava_help
[] =
580 "In Java code, all the tags constructs of C and C++ code are\n\
581 tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
584 static const char *Cobol_suffixes
[] =
585 { "COB", "cob", NULL
};
586 static char Cobol_help
[] =
587 "In Cobol code, tags are paragraph names; that is, any word\n\
588 starting in column 8 and followed by a period.";
590 static const char *Cstar_suffixes
[] =
591 { "cs", "hs", NULL
};
593 static const char *Erlang_suffixes
[] =
594 { "erl", "hrl", NULL
};
595 static const char Erlang_help
[] =
596 "In Erlang code, the tags are the functions, records and macros\n\
597 defined in the file.";
599 const char *Forth_suffixes
[] =
600 { "fth", "tok", NULL
};
601 static const char Forth_help
[] =
602 "In Forth code, tags are words defined by `:',\n\
603 constant, code, create, defer, value, variable, buffer:, field.";
605 static const char *Fortran_suffixes
[] =
606 { "F", "f", "f90", "for", NULL
};
607 static const char Fortran_help
[] =
608 "In Fortran code, functions, subroutines and block data are tags.";
610 static const char *HTML_suffixes
[] =
611 { "htm", "html", "shtml", NULL
};
612 static const char HTML_help
[] =
613 "In HTML input files, the tags are the `title' and the `h1', `h2',\n\
614 `h3' headers. Also, tags are `name=' in anchors and all\n\
615 occurrences of `id='.";
617 static const char *Lisp_suffixes
[] =
618 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
619 static const char Lisp_help
[] =
620 "In Lisp code, any function defined with `defun', any variable\n\
621 defined with `defvar' or `defconst', and in general the first\n\
622 argument of any expression that starts with `(def' in column zero\n\
624 The `--declarations' option tags \"(defvar foo)\" constructs too.";
626 static const char *Lua_suffixes
[] =
627 { "lua", "LUA", NULL
};
628 static const char Lua_help
[] =
629 "In Lua scripts, all functions are tags.";
631 static const char *Makefile_filenames
[] =
632 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
633 static const char Makefile_help
[] =
634 "In makefiles, targets are tags; additionally, variables are tags\n\
635 unless you specify `--no-globals'.";
637 static const char *Objc_suffixes
[] =
638 { "lm", /* Objective lex file */
639 "m", /* Objective C file */
641 static const char Objc_help
[] =
642 "In Objective C code, tags include Objective C definitions for classes,\n\
643 class categories, methods and protocols. Tags for variables and\n\
644 functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.\n\
645 (Use --help --lang=c --lang=objc --lang=java for full help.)";
647 static const char *Pascal_suffixes
[] =
648 { "p", "pas", NULL
};
649 static const char Pascal_help
[] =
650 "In Pascal code, the tags are the functions and procedures defined\n\
652 /* " // this is for working around an Emacs highlighting bug... */
654 static const char *Perl_suffixes
[] =
655 { "pl", "pm", NULL
};
656 static const char *Perl_interpreters
[] =
657 { "perl", "@PERL@", NULL
};
658 static const char Perl_help
[] =
659 "In Perl code, the tags are the packages, subroutines and variables\n\
660 defined by the `package', `sub', `my' and `local' keywords. Use\n\
661 `--globals' if you want to tag global variables. Tags for\n\
662 subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
663 defined in the default package is `main::SUB'.";
665 static const char *PHP_suffixes
[] =
666 { "php", "php3", "php4", NULL
};
667 static const char PHP_help
[] =
668 "In PHP code, tags are functions, classes and defines. Unless you use\n\
669 the `--no-members' option, vars are tags too.";
671 static const char *plain_C_suffixes
[] =
672 { "pc", /* Pro*C file */
675 static const char *PS_suffixes
[] =
676 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
677 static const char PS_help
[] =
678 "In PostScript code, the tags are the functions.";
680 static const char *Prolog_suffixes
[] =
682 static const char Prolog_help
[] =
683 "In Prolog code, tags are predicates and rules at the beginning of\n\
686 static const char *Python_suffixes
[] =
688 static const char Python_help
[] =
689 "In Python code, `def' or `class' at the beginning of a line\n\
692 /* Can't do the `SCM' or `scm' prefix with a version number. */
693 static const char *Scheme_suffixes
[] =
694 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
695 static const char Scheme_help
[] =
696 "In Scheme code, tags include anything defined with `def' or with a\n\
697 construct whose name starts with `def'. They also include\n\
698 variables set with `set!' at top level in the file.";
700 static const char *TeX_suffixes
[] =
701 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
702 static const char TeX_help
[] =
703 "In LaTeX text, the argument of any of the commands `\\chapter',\n\
704 `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
705 `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
706 `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
707 `\\newenvironment' or `\\renewenvironment' is a tag.\n\
709 Other commands can be specified by setting the environment variable\n\
710 `TEXTAGS' to a colon-separated list like, for example,\n\
711 TEXTAGS=\"mycommand:myothercommand\".";
714 static const char *Texinfo_suffixes
[] =
715 { "texi", "texinfo", "txi", NULL
};
716 static const char Texinfo_help
[] =
717 "for texinfo files, lines starting with @node are tagged.";
719 static const char *Yacc_suffixes
[] =
720 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
721 static const char Yacc_help
[] =
722 "In Bison or Yacc input files, each rule defines as a tag the\n\
723 nonterminal it constructs. The portions of the file that contain\n\
724 C code are parsed as C code (use --help --lang=c --lang=yacc\n\
727 static const char auto_help
[] =
728 "`auto' is not a real language, it indicates to use\n\
729 a default language for files base on file name suffix and file contents.";
731 static const char none_help
[] =
732 "`none' is not a real language, it indicates to only do\n\
733 regexp processing on files.";
735 static const char no_lang_help
[] =
736 "No detailed help available for this language.";
740 * Table of languages.
742 * It is ok for a given function to be listed under more than one
743 * name. I just didn't.
746 static language lang_names
[] =
748 { "ada", Ada_help
, Ada_funcs
, Ada_suffixes
},
749 { "asm", Asm_help
, Asm_labels
, Asm_suffixes
},
750 { "c", default_C_help
, default_C_entries
, default_C_suffixes
},
751 { "c++", Cplusplus_help
, Cplusplus_entries
, Cplusplus_suffixes
},
752 { "c*", no_lang_help
, Cstar_entries
, Cstar_suffixes
},
753 { "cobol", Cobol_help
, Cobol_paragraphs
, Cobol_suffixes
},
754 { "erlang", Erlang_help
, Erlang_functions
, Erlang_suffixes
},
755 { "forth", Forth_help
, Forth_words
, Forth_suffixes
},
756 { "fortran", Fortran_help
, Fortran_functions
, Fortran_suffixes
},
757 { "html", HTML_help
, HTML_labels
, HTML_suffixes
},
758 { "java", Cjava_help
, Cjava_entries
, Cjava_suffixes
},
759 { "lisp", Lisp_help
, Lisp_functions
, Lisp_suffixes
},
760 { "lua", Lua_help
, Lua_functions
, Lua_suffixes
},
761 { "makefile", Makefile_help
,Makefile_targets
,NULL
,Makefile_filenames
},
762 { "objc", Objc_help
, plain_C_entries
, Objc_suffixes
},
763 { "pascal", Pascal_help
, Pascal_functions
, Pascal_suffixes
},
764 { "perl",Perl_help
,Perl_functions
,Perl_suffixes
,NULL
,Perl_interpreters
},
765 { "php", PHP_help
, PHP_functions
, PHP_suffixes
},
766 { "postscript",PS_help
, PS_functions
, PS_suffixes
},
767 { "proc", no_lang_help
, plain_C_entries
, plain_C_suffixes
},
768 { "prolog", Prolog_help
, Prolog_functions
, Prolog_suffixes
},
769 { "python", Python_help
, Python_functions
, Python_suffixes
},
770 { "scheme", Scheme_help
, Scheme_functions
, Scheme_suffixes
},
771 { "tex", TeX_help
, TeX_commands
, TeX_suffixes
},
772 { "texinfo", Texinfo_help
, Texinfo_nodes
, Texinfo_suffixes
},
773 { "yacc", Yacc_help
,Yacc_entries
,Yacc_suffixes
,NULL
,NULL
,TRUE
},
774 { "auto", auto_help
}, /* default guessing scheme */
775 { "none", none_help
, just_read_file
}, /* regexp matching only */
776 { NULL
} /* end of list */
781 print_language_names (void)
784 const char **name
, **ext
;
786 puts ("\nThese are the currently supported languages, along with the\n\
787 default file names and dot suffixes:");
788 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
790 printf (" %-*s", 10, lang
->name
);
791 if (lang
->filenames
!= NULL
)
792 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
793 printf (" %s", *name
);
794 if (lang
->suffixes
!= NULL
)
795 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
796 printf (" .%s", *ext
);
799 puts ("where `auto' means use default language for files based on file\n\
800 name suffix, and `none' means only do regexp processing on files.\n\
801 If no language is specified and no matching suffix is found,\n\
802 the first line of the file is read for a sharp-bang (#!) sequence\n\
803 followed by the name of an interpreter. If no such sequence is found,\n\
804 Fortran is tried first; if no tags are found, C is tried next.\n\
805 When parsing any C file, a \"class\" or \"template\" keyword\n\
807 puts ("Compressed files are supported using gzip, bzip2, and xz.\n\
809 For detailed help on a given language use, for example,\n\
810 etags --help --lang=ada.");
814 # define EMACS_NAME "standalone"
817 # define VERSION "17.38.1.4"
822 char emacs_copyright
[] = COPYRIGHT
;
824 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
825 puts (emacs_copyright
);
826 puts ("This program is distributed under the terms in ETAGS.README");
831 #ifndef PRINT_UNDOCUMENTED_OPTIONS_HELP
832 # define PRINT_UNDOCUMENTED_OPTIONS_HELP FALSE
836 print_help (argument
*argbuffer
)
838 bool help_for_lang
= FALSE
;
840 for (; argbuffer
->arg_type
!= at_end
; argbuffer
++)
841 if (argbuffer
->arg_type
== at_language
)
845 puts (argbuffer
->lang
->help
);
846 help_for_lang
= TRUE
;
852 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
854 These are the options accepted by %s.\n", progname
, progname
);
855 puts ("You may use unambiguous abbreviations for the long option names.");
856 puts (" A - as file name means read names from stdin (one per line).\n\
857 Absolute names are stored in the output file as they are.\n\
858 Relative ones are stored relative to the output file's directory.\n");
860 puts ("-a, --append\n\
861 Append tag entries to existing tags file.");
863 puts ("--packages-only\n\
864 For Ada files, only generate tags for packages.");
867 puts ("-B, --backward-search\n\
868 Write the search commands for the tag entries using '?', the\n\
869 backward-search command instead of '/', the forward-search command.");
871 /* This option is mostly obsolete, because etags can now automatically
872 detect C++. Retained for backward compatibility and for debugging and
873 experimentation. In principle, we could want to tag as C++ even
874 before any "class" or "template" keyword.
876 Treat files whose name suffix defaults to C language as C++ files.");
879 puts ("--declarations\n\
880 In C and derived languages, create tags for function declarations,");
882 puts ("\tand create tags for extern variables if --globals is used.");
885 ("\tand create tags for extern variables unless --no-globals is used.");
888 puts ("-d, --defines\n\
889 Create tag entries for C #define constants and enum constants, too.");
891 puts ("-D, --no-defines\n\
892 Don't create tag entries for C #define constants and enum constants.\n\
893 This makes the tags file smaller.");
896 puts ("-i FILE, --include=FILE\n\
897 Include a note in tag file indicating that, when searching for\n\
898 a tag, one should also consult the tags file FILE after\n\
899 checking the current file.");
901 puts ("-l LANG, --language=LANG\n\
902 Force the following files to be considered as written in the\n\
903 named language up to the next --language=LANG option.");
907 Create tag entries for global variables in some languages.");
909 puts ("--no-globals\n\
910 Do not create tag entries for global variables in some\n\
911 languages. This makes the tags file smaller.");
913 if (PRINT_UNDOCUMENTED_OPTIONS_HELP
)
914 puts ("--no-line-directive\n\
915 Ignore #line preprocessor directives in C and derived languages.");
919 Create tag entries for members of structures in some languages.");
921 puts ("--no-members\n\
922 Do not create tag entries for members of structures\n\
923 in some languages.");
925 puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
926 Make a tag for each line matching a regular expression pattern\n\
927 in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
928 files only. REGEXFILE is a file containing one REGEXP per line.\n\
929 REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
930 optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
931 puts (" If TAGNAME/ is present, the tags created are named.\n\
932 For example Tcl named tags can be created with:\n\
933 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
934 MODS are optional one-letter modifiers: `i' means to ignore case,\n\
935 `m' means to allow multi-line matches, `s' implies `m' and\n\
936 causes dot to match any character, including newline.");
938 puts ("-R, --no-regex\n\
939 Don't create tags from regexps for the following files.");
941 puts ("-I, --ignore-indentation\n\
942 In C and C++ do not assume that a closing brace in the first\n\
943 column is the final brace of a function or structure definition.");
945 puts ("-o FILE, --output=FILE\n\
946 Write the tags to FILE.");
948 puts ("--parse-stdin=NAME\n\
949 Read from standard input and record tags as belonging to file NAME.");
953 puts ("-t, --typedefs\n\
954 Generate tag entries for C and Ada typedefs.");
955 puts ("-T, --typedefs-and-c++\n\
956 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
957 and C++ member functions.");
961 puts ("-u, --update\n\
962 Update the tag entries for the given files, leaving tag\n\
963 entries for other files in place. Currently, this is\n\
964 implemented by deleting the existing entries for the given\n\
965 files and then rewriting the new entries at the end of the\n\
966 tags file. It is often faster to simply rebuild the entire\n\
967 tag file than to use this.");
971 puts ("-v, --vgrind\n\
972 Print on the standard output an index of items intended for\n\
973 human consumption, similar to the output of vgrind. The index\n\
974 is sorted, and gives the page number of each item.");
976 if (PRINT_UNDOCUMENTED_OPTIONS_HELP
)
977 puts ("-w, --no-duplicates\n\
978 Do not create duplicate tag entries, for compatibility with\n\
979 traditional ctags.");
981 if (PRINT_UNDOCUMENTED_OPTIONS_HELP
)
982 puts ("-w, --no-warn\n\
983 Suppress warning messages about duplicate tag entries.");
985 puts ("-x, --cxref\n\
986 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
987 The output uses line numbers instead of page numbers, but\n\
988 beyond that the differences are cosmetic; try both to see\n\
992 puts ("-V, --version\n\
993 Print the version of the program.\n\
995 Print this help message.\n\
996 Followed by one or more `--language' options prints detailed\n\
997 help about tag generation for the specified languages.");
999 print_language_names ();
1002 puts ("Report bugs to bug-gnu-emacs@gnu.org");
1004 exit (EXIT_SUCCESS
);
1009 main (int argc
, char **argv
)
1012 unsigned int nincluded_files
;
1013 char **included_files
;
1014 argument
*argbuffer
;
1015 int current_arg
, file_count
;
1016 linebuffer filename_lb
;
1017 bool help_asked
= FALSE
;
1024 _fmode
= O_BINARY
; /* all of files are treated as binary files */
1028 nincluded_files
= 0;
1029 included_files
= xnew (argc
, char *);
1033 /* Allocate enough no matter what happens. Overkill, but each one
1035 argbuffer
= xnew (argc
, argument
);
1038 * Always find typedefs and structure tags.
1039 * Also default to find macro constants, enum constants, struct
1040 * members and global variables. Do it for both etags and ctags.
1042 typedefs
= typedefs_or_cplusplus
= constantypedefs
= TRUE
;
1043 globals
= members
= TRUE
;
1045 /* When the optstring begins with a '-' getopt_long does not rearrange the
1046 non-options arguments to be at the end, but leaves them alone. */
1047 optstring
= concat ("-ac:Cf:Il:o:r:RSVhH",
1048 (CTAGS
) ? "BxdtTuvw" : "Di:",
1051 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, NULL
)) != EOF
)
1055 /* If getopt returns 0, then it has already processed a
1056 long-named option. We should do nothing. */
1060 /* This means that a file name has been seen. Record it. */
1061 argbuffer
[current_arg
].arg_type
= at_filename
;
1062 argbuffer
[current_arg
].what
= optarg
;
1063 len
= strlen (optarg
);
1064 if (whatlen_max
< len
)
1071 /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
1072 argbuffer
[current_arg
].arg_type
= at_stdin
;
1073 argbuffer
[current_arg
].what
= optarg
;
1074 len
= strlen (optarg
);
1075 if (whatlen_max
< len
)
1080 fatal ("cannot parse standard input more than once", (char *)NULL
);
1081 parsing_stdin
= TRUE
;
1084 /* Common options. */
1085 case 'a': append_to_tagfile
= TRUE
; break;
1086 case 'C': cplusplus
= TRUE
; break;
1087 case 'f': /* for compatibility with old makefiles */
1091 error ("-o option may only be given once.");
1092 suggest_asking_for_help ();
1098 case 'S': /* for backward compatibility */
1099 ignoreindent
= TRUE
;
1103 language
*lang
= get_language_from_langname (optarg
);
1106 argbuffer
[current_arg
].lang
= lang
;
1107 argbuffer
[current_arg
].arg_type
= at_language
;
1113 /* Backward compatibility: support obsolete --ignore-case-regexp. */
1114 optarg
= concat (optarg
, "i", ""); /* memory leak here */
1117 argbuffer
[current_arg
].arg_type
= at_regexp
;
1118 argbuffer
[current_arg
].what
= optarg
;
1119 len
= strlen (optarg
);
1120 if (whatlen_max
< len
)
1125 argbuffer
[current_arg
].arg_type
= at_regexp
;
1126 argbuffer
[current_arg
].what
= NULL
;
1138 case 'D': constantypedefs
= FALSE
; break;
1139 case 'i': included_files
[nincluded_files
++] = optarg
; break;
1141 /* Ctags options. */
1142 case 'B': searchar
= '?'; break;
1143 case 'd': constantypedefs
= TRUE
; break;
1144 case 't': typedefs
= TRUE
; break;
1145 case 'T': typedefs
= typedefs_or_cplusplus
= TRUE
; break;
1146 case 'u': update
= TRUE
; break;
1147 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1148 case 'x': cxref_style
= TRUE
; break;
1149 case 'w': no_warnings
= TRUE
; break;
1151 suggest_asking_for_help ();
1155 /* No more options. Store the rest of arguments. */
1156 for (; optind
< argc
; optind
++)
1158 argbuffer
[current_arg
].arg_type
= at_filename
;
1159 argbuffer
[current_arg
].what
= argv
[optind
];
1160 len
= strlen (argv
[optind
]);
1161 if (whatlen_max
< len
)
1167 argbuffer
[current_arg
].arg_type
= at_end
;
1170 print_help (argbuffer
);
1173 if (nincluded_files
== 0 && file_count
== 0)
1175 error ("no input files specified.");
1176 suggest_asking_for_help ();
1180 if (tagfile
== NULL
)
1181 tagfile
= savestr (CTAGS
? "tags" : "TAGS");
1182 cwd
= etags_getcwd (); /* the current working directory */
1183 if (cwd
[strlen (cwd
) - 1] != '/')
1186 cwd
= concat (oldcwd
, "/", "");
1190 /* Compute base directory for relative file names. */
1191 if (streq (tagfile
, "-")
1192 || strneq (tagfile
, "/dev/", 5))
1193 tagfiledir
= cwd
; /* relative file names are relative to cwd */
1196 canonicalize_filename (tagfile
);
1197 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1200 init (); /* set up boolean "functions" */
1202 linebuffer_init (&lb
);
1203 linebuffer_init (&filename_lb
);
1204 linebuffer_init (&filebuf
);
1205 linebuffer_init (&token_name
);
1209 if (streq (tagfile
, "-"))
1213 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1214 doesn't take effect until after `stdout' is already open). */
1215 if (!isatty (fileno (stdout
)))
1216 setmode (fileno (stdout
), O_BINARY
);
1220 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1226 * Loop through files finding functions.
1228 for (i
= 0; i
< current_arg
; i
++)
1230 static language
*lang
; /* non-NULL if language is forced */
1233 switch (argbuffer
[i
].arg_type
)
1236 lang
= argbuffer
[i
].lang
;
1239 analyse_regex (argbuffer
[i
].what
);
1242 this_file
= argbuffer
[i
].what
;
1243 /* Input file named "-" means read file names from stdin
1244 (one per line) and use them. */
1245 if (streq (this_file
, "-"))
1248 fatal ("cannot parse standard input AND read file names from it",
1250 while (readline_internal (&filename_lb
, stdin
) > 0)
1251 process_file_name (filename_lb
.buffer
, lang
);
1254 process_file_name (this_file
, lang
);
1257 this_file
= argbuffer
[i
].what
;
1258 process_file (stdin
, this_file
, lang
);
1265 free (filebuf
.buffer
);
1266 free (token_name
.buffer
);
1268 if (!CTAGS
|| cxref_style
)
1270 /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */
1271 put_entries (nodehead
);
1272 free_tree (nodehead
);
1278 /* Output file entries that have no tags. */
1279 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1281 fprintf (tagf
, "\f\n%s,0\n", fdp
->taggedfname
);
1283 while (nincluded_files
-- > 0)
1284 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1286 if (fclose (tagf
) == EOF
)
1290 exit (EXIT_SUCCESS
);
1293 /* From here on, we are in (CTAGS && !cxref_style) */
1297 xmalloc (strlen (tagfile
) + whatlen_max
+
1298 sizeof "mv..OTAGS;fgrep -v '\t\t' OTAGS >;rm OTAGS");
1299 for (i
= 0; i
< current_arg
; ++i
)
1301 switch (argbuffer
[i
].arg_type
)
1307 continue; /* the for loop */
1309 strcpy (cmd
, "mv ");
1310 strcat (cmd
, tagfile
);
1311 strcat (cmd
, " OTAGS;fgrep -v '\t");
1312 strcat (cmd
, argbuffer
[i
].what
);
1313 strcat (cmd
, "\t' OTAGS >");
1314 strcat (cmd
, tagfile
);
1315 strcat (cmd
, ";rm OTAGS");
1316 if (system (cmd
) != EXIT_SUCCESS
)
1317 fatal ("failed to execute shell command", (char *)NULL
);
1320 append_to_tagfile
= TRUE
;
1323 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1326 put_entries (nodehead
); /* write all the tags (CTAGS) */
1327 free_tree (nodehead
);
1329 if (fclose (tagf
) == EOF
)
1333 if (append_to_tagfile
|| update
)
1335 char *cmd
= xmalloc (2 * strlen (tagfile
) + sizeof "sort -u -o..");
1336 /* Maybe these should be used:
1337 setenv ("LC_COLLATE", "C", 1);
1338 setenv ("LC_ALL", "C", 1); */
1339 strcpy (cmd
, "sort -u -o ");
1340 strcat (cmd
, tagfile
);
1342 strcat (cmd
, tagfile
);
1343 exit (system (cmd
));
1345 return EXIT_SUCCESS
;
1350 * Return a compressor given the file name. If EXTPTR is non-zero,
1351 * return a pointer into FILE where the compressor-specific
1352 * extension begins. If no compressor is found, NULL is returned
1353 * and EXTPTR is not significant.
1354 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1357 get_compressor_from_suffix (char *file
, char **extptr
)
1360 char *slash
, *suffix
;
1362 /* File has been processed by canonicalize_filename,
1363 so we don't need to consider backslashes on DOS_NT. */
1364 slash
= etags_strrchr (file
, '/');
1365 suffix
= etags_strrchr (file
, '.');
1366 if (suffix
== NULL
|| suffix
< slash
)
1371 /* Let those poor souls who live with DOS 8+3 file name limits get
1372 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1373 Only the first do loop is run if not MSDOS */
1376 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1377 if (streq (compr
->suffix
, suffix
))
1380 break; /* do it only once: not really a loop */
1383 } while (*suffix
!= '\0');
1390 * Return a language given the name.
1393 get_language_from_langname (const char *name
)
1398 error ("empty language name");
1401 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1402 if (streq (name
, lang
->name
))
1404 error ("unknown language \"%s\"", name
);
1412 * Return a language given the interpreter name.
1415 get_language_from_interpreter (char *interpreter
)
1420 if (interpreter
== NULL
)
1422 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1423 if (lang
->interpreters
!= NULL
)
1424 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1425 if (streq (*iname
, interpreter
))
1434 * Return a language given the file name.
1437 get_language_from_filename (char *file
, int case_sensitive
)
1440 const char **name
, **ext
, *suffix
;
1442 /* Try whole file name first. */
1443 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1444 if (lang
->filenames
!= NULL
)
1445 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1446 if ((case_sensitive
)
1447 ? streq (*name
, file
)
1448 : strcaseeq (*name
, file
))
1451 /* If not found, try suffix after last dot. */
1452 suffix
= etags_strrchr (file
, '.');
1456 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1457 if (lang
->suffixes
!= NULL
)
1458 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1459 if ((case_sensitive
)
1460 ? streq (*ext
, suffix
)
1461 : strcaseeq (*ext
, suffix
))
1468 * This routine is called on each file argument.
1471 process_file_name (char *file
, language
*lang
)
1473 struct stat stat_buf
;
1477 char *compressed_name
, *uncompressed_name
;
1478 char *ext
, *real_name
;
1481 canonicalize_filename (file
);
1482 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1484 error ("skipping inclusion of %s in self.", file
);
1487 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1489 compressed_name
= NULL
;
1490 real_name
= uncompressed_name
= savestr (file
);
1494 real_name
= compressed_name
= savestr (file
);
1495 uncompressed_name
= savenstr (file
, ext
- file
);
1498 /* If the canonicalized uncompressed name
1499 has already been dealt with, skip it silently. */
1500 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1502 assert (fdp
->infname
!= NULL
);
1503 if (streq (uncompressed_name
, fdp
->infname
))
1507 if (stat (real_name
, &stat_buf
) != 0)
1509 /* Reset real_name and try with a different name. */
1511 if (compressed_name
!= NULL
) /* try with the given suffix */
1513 if (stat (uncompressed_name
, &stat_buf
) == 0)
1514 real_name
= uncompressed_name
;
1516 else /* try all possible suffixes */
1518 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1520 compressed_name
= concat (file
, ".", compr
->suffix
);
1521 if (stat (compressed_name
, &stat_buf
) != 0)
1525 char *suf
= compressed_name
+ strlen (file
);
1526 size_t suflen
= strlen (compr
->suffix
) + 1;
1527 for ( ; suf
[1]; suf
++, suflen
--)
1529 memmove (suf
, suf
+ 1, suflen
);
1530 if (stat (compressed_name
, &stat_buf
) == 0)
1532 real_name
= compressed_name
;
1536 if (real_name
!= NULL
)
1539 free (compressed_name
);
1540 compressed_name
= NULL
;
1544 real_name
= compressed_name
;
1549 if (real_name
== NULL
)
1554 } /* try with a different name */
1556 if (!S_ISREG (stat_buf
.st_mode
))
1558 error ("skipping %s: it is not a regular file.", real_name
);
1561 if (real_name
== compressed_name
)
1563 char *cmd
= concat (compr
->command
, " ", real_name
);
1564 inf
= (FILE *) popen (cmd
, "r");
1568 inf
= fopen (real_name
, "r");
1575 process_file (inf
, uncompressed_name
, lang
);
1577 if (real_name
== compressed_name
)
1578 retval
= pclose (inf
);
1580 retval
= fclose (inf
);
1585 free (compressed_name
);
1586 free (uncompressed_name
);
1593 process_file (FILE *fh
, char *fn
, language
*lang
)
1595 static const fdesc emptyfdesc
;
1598 /* Create a new input file description entry. */
1599 fdp
= xnew (1, fdesc
);
1602 fdp
->infname
= savestr (fn
);
1604 fdp
->infabsname
= absolute_filename (fn
, cwd
);
1605 fdp
->infabsdir
= absolute_dirname (fn
, cwd
);
1606 if (filename_is_absolute (fn
))
1608 /* An absolute file name. Canonicalize it. */
1609 fdp
->taggedfname
= absolute_filename (fn
, NULL
);
1613 /* A file name relative to cwd. Make it relative
1614 to the directory of the tags file. */
1615 fdp
->taggedfname
= relative_filename (fn
, tagfiledir
);
1617 fdp
->usecharno
= TRUE
; /* use char position when making tags */
1619 fdp
->written
= FALSE
; /* not written on tags file yet */
1622 curfdp
= fdhead
; /* the current file description */
1626 /* If not Ctags, and if this is not metasource and if it contained no #line
1627 directives, we can write the tags and free all nodes pointing to
1630 && curfdp
->usecharno
/* no #line directives in this file */
1631 && !curfdp
->lang
->metasource
)
1635 /* Look for the head of the sublist relative to this file. See add_node
1636 for the structure of the node tree. */
1638 for (np
= nodehead
; np
!= NULL
; prev
= np
, np
= np
->left
)
1639 if (np
->fdp
== curfdp
)
1642 /* If we generated tags for this file, write and delete them. */
1645 /* This is the head of the last sublist, if any. The following
1646 instructions depend on this being true. */
1647 assert (np
->left
== NULL
);
1649 assert (fdhead
== curfdp
);
1650 assert (last_node
->fdp
== curfdp
);
1651 put_entries (np
); /* write tags for file curfdp->taggedfname */
1652 free_tree (np
); /* remove the written nodes */
1654 nodehead
= NULL
; /* no nodes left */
1656 prev
->left
= NULL
; /* delete the pointer to the sublist */
1662 * This routine sets up the boolean pseudo-functions which work
1663 * by setting boolean flags dependent upon the corresponding character.
1664 * Every char which is NOT in that string is not a white char. Therefore,
1665 * all of the array "_wht" is set to FALSE, and then the elements
1666 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1667 * of a char is TRUE if it is the string "white", else FALSE.
1672 register const char *sp
;
1675 for (i
= 0; i
< CHARS
; i
++)
1676 iswhite (i
) = notinname (i
) = begtoken (i
) = intoken (i
) = endtoken (i
) = FALSE
;
1677 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1678 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1679 notinname ('\0') = notinname ('\n');
1680 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1681 begtoken ('\0') = begtoken ('\n');
1682 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1683 intoken ('\0') = intoken ('\n');
1684 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1685 endtoken ('\0') = endtoken ('\n');
1689 * This routine opens the specified file and calls the function
1690 * which finds the function and type definitions.
1693 find_entries (FILE *inf
)
1696 language
*lang
= curfdp
->lang
;
1697 Lang_function
*parser
= NULL
;
1699 /* If user specified a language, use it. */
1700 if (lang
!= NULL
&& lang
->function
!= NULL
)
1702 parser
= lang
->function
;
1705 /* Else try to guess the language given the file name. */
1708 lang
= get_language_from_filename (curfdp
->infname
, TRUE
);
1709 if (lang
!= NULL
&& lang
->function
!= NULL
)
1711 curfdp
->lang
= lang
;
1712 parser
= lang
->function
;
1716 /* Else look for sharp-bang as the first two characters. */
1718 && readline_internal (&lb
, inf
) > 0
1720 && lb
.buffer
[0] == '#'
1721 && lb
.buffer
[1] == '!')
1725 /* Set lp to point at the first char after the last slash in the
1726 line or, if no slashes, at the first nonblank. Then set cp to
1727 the first successive blank and terminate the string. */
1728 lp
= etags_strrchr (lb
.buffer
+2, '/');
1732 lp
= skip_spaces (lb
.buffer
+ 2);
1733 cp
= skip_non_spaces (lp
);
1736 if (strlen (lp
) > 0)
1738 lang
= get_language_from_interpreter (lp
);
1739 if (lang
!= NULL
&& lang
->function
!= NULL
)
1741 curfdp
->lang
= lang
;
1742 parser
= lang
->function
;
1747 /* We rewind here, even if inf may be a pipe. We fail if the
1748 length of the first line is longer than the pipe block size,
1749 which is unlikely. */
1752 /* Else try to guess the language given the case insensitive file name. */
1755 lang
= get_language_from_filename (curfdp
->infname
, FALSE
);
1756 if (lang
!= NULL
&& lang
->function
!= NULL
)
1758 curfdp
->lang
= lang
;
1759 parser
= lang
->function
;
1763 /* Else try Fortran or C. */
1766 node
*old_last_node
= last_node
;
1768 curfdp
->lang
= get_language_from_langname ("fortran");
1771 if (old_last_node
== last_node
)
1772 /* No Fortran entries found. Try C. */
1774 /* We do not tag if rewind fails.
1775 Only the file name will be recorded in the tags file. */
1777 curfdp
->lang
= get_language_from_langname (cplusplus
? "c++" : "c");
1783 if (!no_line_directive
1784 && curfdp
->lang
!= NULL
&& curfdp
->lang
->metasource
)
1785 /* It may be that this is a bingo.y file, and we already parsed a bingo.c
1786 file, or anyway we parsed a file that is automatically generated from
1787 this one. If this is the case, the bingo.c file contained #line
1788 directives that generated tags pointing to this file. Let's delete
1789 them all before parsing this file, which is the real source. */
1791 fdesc
**fdpp
= &fdhead
;
1792 while (*fdpp
!= NULL
)
1794 && streq ((*fdpp
)->taggedfname
, curfdp
->taggedfname
))
1795 /* We found one of those! We must delete both the file description
1796 and all tags referring to it. */
1798 fdesc
*badfdp
= *fdpp
;
1800 /* Delete the tags referring to badfdp->taggedfname
1801 that were obtained from badfdp->infname. */
1802 invalidate_nodes (badfdp
, &nodehead
);
1804 *fdpp
= badfdp
->next
; /* remove the bad description from the list */
1805 free_fdesc (badfdp
);
1808 fdpp
= &(*fdpp
)->next
; /* advance the list pointer */
1811 assert (parser
!= NULL
);
1813 /* Generic initializations before reading from file. */
1814 linebuffer_setlen (&filebuf
, 0); /* reset the file buffer */
1816 /* Generic initializations before parsing file with readline. */
1817 lineno
= 0; /* reset global line number */
1818 charno
= 0; /* reset global char number */
1819 linecharno
= 0; /* reset global char number of line start */
1823 regex_tag_multiline ();
1828 * Check whether an implicitly named tag should be created,
1829 * then call `pfnote'.
1830 * NAME is a string that is internally copied by this function.
1832 * TAGS format specification
1833 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1834 * The following is explained in some more detail in etc/ETAGS.EBNF.
1836 * make_tag creates tags with "implicit tag names" (unnamed tags)
1837 * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
1838 * 1. NAME does not contain any of the characters in NONAM;
1839 * 2. LINESTART contains name as either a rightmost, or rightmost but
1840 * one character, substring;
1841 * 3. the character, if any, immediately before NAME in LINESTART must
1842 * be a character in NONAM;
1843 * 4. the character, if any, immediately after NAME in LINESTART must
1844 * also be a character in NONAM.
1846 * The implementation uses the notinname() macro, which recognizes the
1847 * characters stored in the string `nonam'.
1848 * etags.el needs to use the same characters that are in NONAM.
1851 make_tag (const char *name
, /* tag name, or NULL if unnamed */
1852 int namelen
, /* tag length */
1853 int is_func
, /* tag is a function */
1854 char *linestart
, /* start of the line where tag is */
1855 int linelen
, /* length of the line where tag is */
1856 int lno
, /* line number */
1857 long int cno
) /* character number */
1859 bool named
= (name
!= NULL
&& namelen
> 0);
1862 if (!CTAGS
&& named
) /* maybe set named to false */
1863 /* Let's try to make an implicit tag name, that is, create an unnamed tag
1864 such that etags.el can guess a name from it. */
1867 register const char *cp
= name
;
1869 for (i
= 0; i
< namelen
; i
++)
1870 if (notinname (*cp
++))
1872 if (i
== namelen
) /* rule #1 */
1874 cp
= linestart
+ linelen
- namelen
;
1875 if (notinname (linestart
[linelen
-1]))
1876 cp
-= 1; /* rule #4 */
1877 if (cp
>= linestart
/* rule #2 */
1879 || notinname (cp
[-1])) /* rule #3 */
1880 && strneq (name
, cp
, namelen
)) /* rule #2 */
1881 named
= FALSE
; /* use implicit tag name */
1886 nname
= savenstr (name
, namelen
);
1888 pfnote (nname
, is_func
, linestart
, linelen
, lno
, cno
);
1893 pfnote (char *name
, int is_func
, char *linestart
, int linelen
, int lno
, long int cno
)
1894 /* tag name, or NULL if unnamed */
1895 /* tag is a function */
1896 /* start of the line where tag is */
1897 /* length of the line where tag is */
1899 /* character number */
1903 assert (name
== NULL
|| name
[0] != '\0');
1904 if (CTAGS
&& name
== NULL
)
1907 np
= xnew (1, node
);
1909 /* If ctags mode, change name "main" to M<thisfilename>. */
1910 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1912 register char *fp
= etags_strrchr (curfdp
->taggedfname
, '/');
1913 np
->name
= concat ("M", fp
== NULL
? curfdp
->taggedfname
: fp
+ 1, "");
1914 fp
= etags_strrchr (np
->name
, '.');
1915 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
1921 np
->been_warned
= FALSE
;
1923 np
->is_func
= is_func
;
1925 if (np
->fdp
->usecharno
)
1926 /* Our char numbers are 0-base, because of C language tradition?
1927 ctags compatibility? old versions compatibility? I don't know.
1928 Anyway, since emacs's are 1-base we expect etags.el to take care
1929 of the difference. If we wanted to have 1-based numbers, we would
1930 uncomment the +1 below. */
1931 np
->cno
= cno
/* + 1 */ ;
1933 np
->cno
= invalidcharno
;
1934 np
->left
= np
->right
= NULL
;
1935 if (CTAGS
&& !cxref_style
)
1937 if (strlen (linestart
) < 50)
1938 np
->regex
= concat (linestart
, "$", "");
1940 np
->regex
= savenstr (linestart
, 50);
1943 np
->regex
= savenstr (linestart
, linelen
);
1945 add_node (np
, &nodehead
);
1950 * recurse on left children, iterate on right children.
1953 free_tree (register node
*np
)
1957 register node
*node_right
= np
->right
;
1958 free_tree (np
->left
);
1968 * delete a file description
1971 free_fdesc (register fdesc
*fdp
)
1973 free (fdp
->infname
);
1974 free (fdp
->infabsname
);
1975 free (fdp
->infabsdir
);
1976 free (fdp
->taggedfname
);
1983 * Adds a node to the tree of nodes. In etags mode, sort by file
1984 * name. In ctags mode, sort by tag name. Make no attempt at
1987 * add_node is the only function allowed to add nodes, so it can
1991 add_node (node
*np
, node
**cur_node_p
)
1994 register node
*cur_node
= *cur_node_p
;
1996 if (cur_node
== NULL
)
2006 /* For each file name, tags are in a linked sublist on the right
2007 pointer. The first tags of different files are a linked list
2008 on the left pointer. last_node points to the end of the last
2010 if (last_node
!= NULL
&& last_node
->fdp
== np
->fdp
)
2012 /* Let's use the same sublist as the last added node. */
2013 assert (last_node
->right
== NULL
);
2014 last_node
->right
= np
;
2017 else if (cur_node
->fdp
== np
->fdp
)
2019 /* Scanning the list we found the head of a sublist which is
2020 good for us. Let's scan this sublist. */
2021 add_node (np
, &cur_node
->right
);
2024 /* The head of this sublist is not good for us. Let's try the
2026 add_node (np
, &cur_node
->left
);
2027 } /* if ETAGS mode */
2032 dif
= strcmp (np
->name
, cur_node
->name
);
2035 * If this tag name matches an existing one, then
2036 * do not add the node, but maybe print a warning.
2038 if (no_duplicates
&& !dif
)
2040 if (np
->fdp
== cur_node
->fdp
)
2044 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
2045 np
->fdp
->infname
, lineno
, np
->name
);
2046 fprintf (stderr
, "Second entry ignored\n");
2049 else if (!cur_node
->been_warned
&& !no_warnings
)
2053 "Duplicate entry in files %s and %s: %s (Warning only)\n",
2054 np
->fdp
->infname
, cur_node
->fdp
->infname
, np
->name
);
2055 cur_node
->been_warned
= TRUE
;
2060 /* Actually add the node */
2061 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
2062 } /* if CTAGS mode */
2066 * invalidate_nodes ()
2067 * Scan the node tree and invalidate all nodes pointing to the
2068 * given file description (CTAGS case) or free them (ETAGS case).
2071 invalidate_nodes (fdesc
*badfdp
, node
**npp
)
2080 if (np
->left
!= NULL
)
2081 invalidate_nodes (badfdp
, &np
->left
);
2082 if (np
->fdp
== badfdp
)
2084 if (np
->right
!= NULL
)
2085 invalidate_nodes (badfdp
, &np
->right
);
2089 assert (np
->fdp
!= NULL
);
2090 if (np
->fdp
== badfdp
)
2092 *npp
= np
->left
; /* detach the sublist from the list */
2093 np
->left
= NULL
; /* isolate it */
2094 free_tree (np
); /* free it */
2095 invalidate_nodes (badfdp
, npp
);
2098 invalidate_nodes (badfdp
, &np
->left
);
2103 static int total_size_of_entries (node
*);
2104 static int number_len (long) ATTRIBUTE_CONST
;
2106 /* Length of a non-negative number's decimal representation. */
2108 number_len (long int num
)
2111 while ((num
/= 10) > 0)
2117 * Return total number of characters that put_entries will output for
2118 * the nodes in the linked list at the right of the specified node.
2119 * This count is irrelevant with etags.el since emacs 19.34 at least,
2120 * but is still supplied for backward compatibility.
2123 total_size_of_entries (register node
*np
)
2125 register int total
= 0;
2127 for (; np
!= NULL
; np
= np
->right
)
2130 total
+= strlen (np
->regex
) + 1; /* pat\177 */
2131 if (np
->name
!= NULL
)
2132 total
+= strlen (np
->name
) + 1; /* name\001 */
2133 total
+= number_len ((long) np
->lno
) + 1; /* lno, */
2134 if (np
->cno
!= invalidcharno
) /* cno */
2135 total
+= number_len (np
->cno
);
2136 total
+= 1; /* newline */
2143 put_entries (register node
*np
)
2146 static fdesc
*fdp
= NULL
;
2151 /* Output subentries that precede this one */
2153 put_entries (np
->left
);
2155 /* Output this entry */
2164 fprintf (tagf
, "\f\n%s,%d\n",
2165 fdp
->taggedfname
, total_size_of_entries (np
));
2166 fdp
->written
= TRUE
;
2168 fputs (np
->regex
, tagf
);
2169 fputc ('\177', tagf
);
2170 if (np
->name
!= NULL
)
2172 fputs (np
->name
, tagf
);
2173 fputc ('\001', tagf
);
2175 fprintf (tagf
, "%d,", np
->lno
);
2176 if (np
->cno
!= invalidcharno
)
2177 fprintf (tagf
, "%ld", np
->cno
);
2183 if (np
->name
== NULL
)
2184 error ("internal error: NULL name in ctags mode.");
2189 fprintf (stdout
, "%s %s %d\n",
2190 np
->name
, np
->fdp
->taggedfname
, (np
->lno
+ 63) / 64);
2192 fprintf (stdout
, "%-16s %3d %-16s %s\n",
2193 np
->name
, np
->lno
, np
->fdp
->taggedfname
, np
->regex
);
2197 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->fdp
->taggedfname
);
2200 { /* function or #define macro with args */
2201 putc (searchar
, tagf
);
2204 for (sp
= np
->regex
; *sp
; sp
++)
2206 if (*sp
== '\\' || *sp
== searchar
)
2210 putc (searchar
, tagf
);
2213 { /* anything else; text pattern inadequate */
2214 fprintf (tagf
, "%d", np
->lno
);
2219 } /* if this node contains a valid tag */
2221 /* Output subentries that follow this one */
2222 put_entries (np
->right
);
2224 put_entries (np
->left
);
2229 #define C_EXT 0x00fff /* C extensions */
2230 #define C_PLAIN 0x00000 /* C */
2231 #define C_PLPL 0x00001 /* C++ */
2232 #define C_STAR 0x00003 /* C* */
2233 #define C_JAVA 0x00005 /* JAVA */
2234 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
2235 #define YACC 0x10000 /* yacc file */
2238 * The C symbol tables.
2243 st_C_objprot
, st_C_objimpl
, st_C_objend
,
2245 st_C_ignore
, st_C_attribute
,
2248 st_C_class
, st_C_template
,
2249 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
2252 static unsigned int hash (const char *, unsigned int);
2253 static struct C_stab_entry
* in_word_set (const char *, unsigned int);
2254 static enum sym_type
C_symtype (char *, int, int);
2256 /* Feed stuff between (but not including) %[ and %] lines to:
2262 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2266 while, 0, st_C_ignore
2267 switch, 0, st_C_ignore
2268 return, 0, st_C_ignore
2269 __attribute__, 0, st_C_attribute
2270 GTY, 0, st_C_attribute
2271 @interface, 0, st_C_objprot
2272 @protocol, 0, st_C_objprot
2273 @implementation,0, st_C_objimpl
2274 @end, 0, st_C_objend
2275 import, (C_JAVA & ~C_PLPL), st_C_ignore
2276 package, (C_JAVA & ~C_PLPL), st_C_ignore
2277 friend, C_PLPL, st_C_ignore
2278 extends, (C_JAVA & ~C_PLPL), st_C_javastruct
2279 implements, (C_JAVA & ~C_PLPL), st_C_javastruct
2280 interface, (C_JAVA & ~C_PLPL), st_C_struct
2281 class, 0, st_C_class
2282 namespace, C_PLPL, st_C_struct
2283 domain, C_STAR, st_C_struct
2284 union, 0, st_C_struct
2285 struct, 0, st_C_struct
2286 extern, 0, st_C_extern
2288 typedef, 0, st_C_typedef
2289 define, 0, st_C_define
2290 undef, 0, st_C_define
2291 operator, C_PLPL, st_C_operator
2292 template, 0, st_C_template
2293 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2294 DEFUN, 0, st_C_gnumacro
2295 SYSCALL, 0, st_C_gnumacro
2296 ENTRY, 0, st_C_gnumacro
2297 PSEUDO, 0, st_C_gnumacro
2298 # These are defined inside C functions, so currently they are not met.
2299 # EXFUN used in glibc, DEFVAR_* in emacs.
2300 #EXFUN, 0, st_C_gnumacro
2301 #DEFVAR_, 0, st_C_gnumacro
2303 and replace lines between %< and %> with its output, then:
2304 - remove the #if characterset check
2305 - make in_word_set static and not inline. */
2307 /* C code produced by gperf version 3.0.1 */
2308 /* Command-line: gperf -m 5 */
2309 /* Computed positions: -k'2-3' */
2311 struct C_stab_entry
{ const char *name
; int c_ext
; enum sym_type type
; };
2312 /* maximum key range = 33, duplicates = 0 */
2314 static inline unsigned int
2315 hash (register const char *str
, register unsigned int len
)
2317 static unsigned char asso_values
[] =
2319 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2320 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2321 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2322 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2323 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2324 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2325 35, 35, 35, 35, 35, 35, 35, 35, 35, 3,
2326 26, 35, 35, 35, 35, 35, 35, 35, 27, 35,
2327 35, 35, 35, 24, 0, 35, 35, 35, 35, 0,
2328 35, 35, 35, 35, 35, 1, 35, 16, 35, 6,
2329 23, 0, 0, 35, 22, 0, 35, 35, 5, 0,
2330 0, 15, 1, 35, 6, 35, 8, 19, 35, 16,
2331 4, 5, 35, 35, 35, 35, 35, 35, 35, 35,
2332 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2333 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2334 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2335 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2336 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2337 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2338 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2339 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2340 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2341 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2342 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2343 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2344 35, 35, 35, 35, 35, 35
2346 register int hval
= len
;
2351 hval
+= asso_values
[(unsigned char)str
[2]];
2354 hval
+= asso_values
[(unsigned char)str
[1]];
2360 static struct C_stab_entry
*
2361 in_word_set (register const char *str
, register unsigned int len
)
2365 TOTAL_KEYWORDS
= 33,
2366 MIN_WORD_LENGTH
= 2,
2367 MAX_WORD_LENGTH
= 15,
2372 static struct C_stab_entry wordlist
[] =
2375 {"if", 0, st_C_ignore
},
2376 {"GTY", 0, st_C_attribute
},
2377 {"@end", 0, st_C_objend
},
2378 {"union", 0, st_C_struct
},
2379 {"define", 0, st_C_define
},
2380 {"import", (C_JAVA
& ~C_PLPL
), st_C_ignore
},
2381 {"template", 0, st_C_template
},
2382 {"operator", C_PLPL
, st_C_operator
},
2383 {"@interface", 0, st_C_objprot
},
2384 {"implements", (C_JAVA
& ~C_PLPL
), st_C_javastruct
},
2385 {"friend", C_PLPL
, st_C_ignore
},
2386 {"typedef", 0, st_C_typedef
},
2387 {"return", 0, st_C_ignore
},
2388 {"@implementation",0, st_C_objimpl
},
2389 {"@protocol", 0, st_C_objprot
},
2390 {"interface", (C_JAVA
& ~C_PLPL
), st_C_struct
},
2391 {"extern", 0, st_C_extern
},
2392 {"extends", (C_JAVA
& ~C_PLPL
), st_C_javastruct
},
2393 {"struct", 0, st_C_struct
},
2394 {"domain", C_STAR
, st_C_struct
},
2395 {"switch", 0, st_C_ignore
},
2396 {"enum", 0, st_C_enum
},
2397 {"for", 0, st_C_ignore
},
2398 {"namespace", C_PLPL
, st_C_struct
},
2399 {"class", 0, st_C_class
},
2400 {"while", 0, st_C_ignore
},
2401 {"undef", 0, st_C_define
},
2402 {"package", (C_JAVA
& ~C_PLPL
), st_C_ignore
},
2403 {"__attribute__", 0, st_C_attribute
},
2404 {"SYSCALL", 0, st_C_gnumacro
},
2405 {"ENTRY", 0, st_C_gnumacro
},
2406 {"PSEUDO", 0, st_C_gnumacro
},
2407 {"DEFUN", 0, st_C_gnumacro
}
2410 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2412 register int key
= hash (str
, len
);
2414 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2416 register const char *s
= wordlist
[key
].name
;
2418 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1) && s
[len
] == '\0')
2419 return &wordlist
[key
];
2426 static enum sym_type
2427 C_symtype (char *str
, int len
, int c_ext
)
2429 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2431 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2438 * Ignoring __attribute__ ((list))
2440 static bool inattribute
; /* looking at an __attribute__ construct */
2443 * C functions and variables are recognized using a simple
2444 * finite automaton. fvdef is its state variable.
2448 fvnone
, /* nothing seen */
2449 fdefunkey
, /* Emacs DEFUN keyword seen */
2450 fdefunname
, /* Emacs DEFUN name seen */
2451 foperator
, /* func: operator keyword seen (cplpl) */
2452 fvnameseen
, /* function or variable name seen */
2453 fstartlist
, /* func: just after open parenthesis */
2454 finlist
, /* func: in parameter list */
2455 flistseen
, /* func: after parameter list */
2456 fignore
, /* func: before open brace */
2457 vignore
/* var-like: ignore until ';' */
2460 static bool fvextern
; /* func or var: extern keyword seen; */
2463 * typedefs are recognized using a simple finite automaton.
2464 * typdef is its state variable.
2468 tnone
, /* nothing seen */
2469 tkeyseen
, /* typedef keyword seen */
2470 ttypeseen
, /* defined type seen */
2471 tinbody
, /* inside typedef body */
2472 tend
, /* just before typedef tag */
2473 tignore
/* junk after typedef tag */
2477 * struct-like structures (enum, struct and union) are recognized
2478 * using another simple finite automaton. `structdef' is its state
2483 snone
, /* nothing seen yet,
2484 or in struct body if bracelev > 0 */
2485 skeyseen
, /* struct-like keyword seen */
2486 stagseen
, /* struct-like tag seen */
2487 scolonseen
/* colon seen after struct-like tag */
2491 * When objdef is different from onone, objtag is the name of the class.
2493 static const char *objtag
= "<uninited>";
2496 * Yet another little state machine to deal with preprocessor lines.
2500 dnone
, /* nothing seen */
2501 dsharpseen
, /* '#' seen as first char on line */
2502 ddefineseen
, /* '#' and 'define' seen */
2503 dignorerest
/* ignore rest of line */
2507 * State machine for Objective C protocols and implementations.
2508 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2512 onone
, /* nothing seen */
2513 oprotocol
, /* @interface or @protocol seen */
2514 oimplementation
, /* @implementations seen */
2515 otagseen
, /* class name seen */
2516 oparenseen
, /* parenthesis before category seen */
2517 ocatseen
, /* category name seen */
2518 oinbody
, /* in @implementation body */
2519 omethodsign
, /* in @implementation body, after +/- */
2520 omethodtag
, /* after method name */
2521 omethodcolon
, /* after method colon */
2522 omethodparm
, /* after method parameter */
2523 oignore
/* wait for @end */
2528 * Use this structure to keep info about the token read, and how it
2529 * should be tagged. Used by the make_C_tag function to build a tag.
2533 char *line
; /* string containing the token */
2534 int offset
; /* where the token starts in LINE */
2535 int length
; /* token length */
2537 The previous members can be used to pass strings around for generic
2538 purposes. The following ones specifically refer to creating tags. In this
2539 case the token contained here is the pattern that will be used to create a
2542 bool valid
; /* do not create a tag; the token should be
2543 invalidated whenever a state machine is
2544 reset prematurely */
2545 bool named
; /* create a named tag */
2546 int lineno
; /* source line number of tag */
2547 long linepos
; /* source char number of tag */
2548 } token
; /* latest token read */
2551 * Variables and functions for dealing with nested structures.
2552 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2554 static void pushclass_above (int, char *, int);
2555 static void popclass_above (int);
2556 static void write_classname (linebuffer
*, const char *qualifier
);
2559 char **cname
; /* nested class names */
2560 int *bracelev
; /* nested class brace level */
2561 int nl
; /* class nesting level (elements used) */
2562 int size
; /* length of the array */
2563 } cstack
; /* stack for nested declaration tags */
2564 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2565 #define nestlev (cstack.nl)
2566 /* After struct keyword or in struct body, not inside a nested function. */
2567 #define instruct (structdef == snone && nestlev > 0 \
2568 && bracelev == cstack.bracelev[nestlev-1] + 1)
2571 pushclass_above (int bracelev
, char *str
, int len
)
2575 popclass_above (bracelev
);
2577 if (nl
>= cstack
.size
)
2579 int size
= cstack
.size
*= 2;
2580 xrnew (cstack
.cname
, size
, char *);
2581 xrnew (cstack
.bracelev
, size
, int);
2583 assert (nl
== 0 || cstack
.bracelev
[nl
-1] < bracelev
);
2584 cstack
.cname
[nl
] = (str
== NULL
) ? NULL
: savenstr (str
, len
);
2585 cstack
.bracelev
[nl
] = bracelev
;
2590 popclass_above (int bracelev
)
2594 for (nl
= cstack
.nl
- 1;
2595 nl
>= 0 && cstack
.bracelev
[nl
] >= bracelev
;
2598 free (cstack
.cname
[nl
]);
2604 write_classname (linebuffer
*cn
, const char *qualifier
)
2607 int qlen
= strlen (qualifier
);
2609 if (cstack
.nl
== 0 || cstack
.cname
[0] == NULL
)
2613 cn
->buffer
[0] = '\0';
2617 len
= strlen (cstack
.cname
[0]);
2618 linebuffer_setlen (cn
, len
);
2619 strcpy (cn
->buffer
, cstack
.cname
[0]);
2621 for (i
= 1; i
< cstack
.nl
; i
++)
2623 char *s
= cstack
.cname
[i
];
2626 linebuffer_setlen (cn
, len
+ qlen
+ strlen (s
));
2627 len
+= sprintf (cn
->buffer
+ len
, "%s%s", qualifier
, s
);
2632 static bool consider_token (char *, int, int, int *, int, int, bool *);
2633 static void make_C_tag (bool);
2637 * checks to see if the current token is at the start of a
2638 * function or variable, or corresponds to a typedef, or
2639 * is a struct/union/enum tag, or #define, or an enum constant.
2641 * *IS_FUNC gets TRUE if the token is a function or #define macro
2642 * with args. C_EXTP points to which language we are looking at.
2653 consider_token (register char *str
, register int len
, register int c
, int *c_extp
, int bracelev
, int parlev
, int *is_func_or_var
)
2654 /* IN: token pointer */
2655 /* IN: token length */
2656 /* IN: first char after the token */
2657 /* IN, OUT: C extensions mask */
2658 /* IN: brace level */
2659 /* IN: parenthesis level */
2660 /* OUT: function or variable found */
2662 /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
2663 structtype is the type of the preceding struct-like keyword, and
2664 structbracelev is the brace level where it has been seen. */
2665 static enum sym_type structtype
;
2666 static int structbracelev
;
2667 static enum sym_type toktype
;
2670 toktype
= C_symtype (str
, len
, *c_extp
);
2673 * Skip __attribute__
2675 if (toktype
== st_C_attribute
)
2682 * Advance the definedef state machine.
2687 /* We're not on a preprocessor line. */
2688 if (toktype
== st_C_gnumacro
)
2695 if (toktype
== st_C_define
)
2697 definedef
= ddefineseen
;
2701 definedef
= dignorerest
;
2706 * Make a tag for any macro, unless it is a constant
2707 * and constantypedefs is FALSE.
2709 definedef
= dignorerest
;
2710 *is_func_or_var
= (c
== '(');
2711 if (!*is_func_or_var
&& !constantypedefs
)
2718 error ("internal error: definedef value.");
2727 if (toktype
== st_C_typedef
)
2747 if (structdef
== snone
&& fvdef
== fvnone
)
2766 case st_C_javastruct
:
2767 if (structdef
== stagseen
)
2768 structdef
= scolonseen
;
2772 if ((*c_extp
& C_AUTO
) /* automatic detection of C++ language */
2774 && definedef
== dnone
&& structdef
== snone
2775 && typdef
== tnone
&& fvdef
== fvnone
)
2776 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2777 if (toktype
== st_C_template
)
2784 && (typdef
== tkeyseen
2785 || (typedefs_or_cplusplus
&& structdef
== snone
)))
2787 structdef
= skeyseen
;
2788 structtype
= toktype
;
2789 structbracelev
= bracelev
;
2790 if (fvdef
== fvnameseen
)
2796 if (structdef
== skeyseen
)
2798 structdef
= stagseen
;
2802 if (typdef
!= tnone
)
2805 /* Detect Objective C constructs. */
2815 objdef
= oimplementation
;
2819 case oimplementation
:
2820 /* Save the class tag for functions or variables defined inside. */
2821 objtag
= savenstr (str
, len
);
2825 /* Save the class tag for categories. */
2826 objtag
= savenstr (str
, len
);
2828 *is_func_or_var
= TRUE
;
2832 *is_func_or_var
= TRUE
;
2840 objdef
= omethodtag
;
2841 linebuffer_setlen (&token_name
, len
);
2842 memcpy (token_name
.buffer
, str
, len
);
2843 token_name
.buffer
[len
] = '\0';
2849 objdef
= omethodparm
;
2854 int oldlen
= token_name
.len
;
2856 objdef
= omethodtag
;
2857 linebuffer_setlen (&token_name
, oldlen
+ len
);
2858 memcpy (token_name
.buffer
+ oldlen
, str
, len
);
2859 token_name
.buffer
[oldlen
+ len
] = '\0';
2864 if (toktype
== st_C_objend
)
2866 /* Memory leakage here: the string pointed by objtag is
2867 never released, because many tests would be needed to
2868 avoid breaking on incorrect input code. The amount of
2869 memory leaked here is the sum of the lengths of the
2877 /* A function, variable or enum constant? */
2899 *is_func_or_var
= TRUE
;
2903 && structdef
== snone
2904 && structtype
== st_C_enum
&& bracelev
> structbracelev
)
2905 return TRUE
; /* enum constant */
2911 fvdef
= fdefunname
; /* GNU macro */
2912 *is_func_or_var
= TRUE
;
2920 if ((strneq (str
, "asm", 3) && endtoken (str
[3]))
2921 || (strneq (str
, "__asm__", 7) && endtoken (str
[7])))
2930 if (len
>= 10 && strneq (str
+len
-10, "::operator", 10))
2932 if (*c_extp
& C_AUTO
) /* automatic detection of C++ */
2933 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2935 *is_func_or_var
= TRUE
;
2938 if (bracelev
> 0 && !instruct
)
2940 fvdef
= fvnameseen
; /* function or variable */
2941 *is_func_or_var
= TRUE
;
2952 * C_entries often keeps pointers to tokens or lines which are older than
2953 * the line currently read. By keeping two line buffers, and switching
2954 * them at end of line, it is possible to use those pointers.
2962 #define current_lb_is_new (newndx == curndx)
2963 #define switch_line_buffers() (curndx = 1 - curndx)
2965 #define curlb (lbs[curndx].lb)
2966 #define newlb (lbs[newndx].lb)
2967 #define curlinepos (lbs[curndx].linepos)
2968 #define newlinepos (lbs[newndx].linepos)
2970 #define plainc ((c_ext & C_EXT) == C_PLAIN)
2971 #define cplpl (c_ext & C_PLPL)
2972 #define cjava ((c_ext & C_JAVA) == C_JAVA)
2974 #define CNL_SAVE_DEFINEDEF() \
2976 curlinepos = charno; \
2977 readline (&curlb, inf); \
2978 lp = curlb.buffer; \
2985 CNL_SAVE_DEFINEDEF(); \
2986 if (savetoken.valid) \
2988 token = savetoken; \
2989 savetoken.valid = FALSE; \
2991 definedef = dnone; \
2996 make_C_tag (int isfun
)
2998 /* This function is never called when token.valid is FALSE, but
2999 we must protect against invalid input or internal errors. */
3001 make_tag (token_name
.buffer
, token_name
.len
, isfun
, token
.line
,
3002 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3004 { /* this branch is optimized away if !DEBUG */
3005 make_tag (concat ("INVALID TOKEN:-->", token_name
.buffer
, ""),
3006 token_name
.len
+ 17, isfun
, token
.line
,
3007 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3008 error ("INVALID TOKEN");
3011 token
.valid
= FALSE
;
3017 * This routine finds functions, variables, typedefs,
3018 * #define's, enum constants and struct/union/enum definitions in
3019 * C syntax and adds them to the list.
3022 C_entries (int c_ext
, FILE *inf
)
3023 /* extension of C */
3026 register char c
; /* latest char read; '\0' for end of line */
3027 register char *lp
; /* pointer one beyond the character `c' */
3028 int curndx
, newndx
; /* indices for current and new lb */
3029 register int tokoff
; /* offset in line of start of current token */
3030 register int toklen
; /* length of current token */
3031 const char *qualifier
; /* string used to qualify names */
3032 int qlen
; /* length of qualifier */
3033 int bracelev
; /* current brace level */
3034 int bracketlev
; /* current bracket level */
3035 int parlev
; /* current parenthesis level */
3036 int attrparlev
; /* __attribute__ parenthesis level */
3037 int templatelev
; /* current template level */
3038 int typdefbracelev
; /* bracelev where a typedef struct body begun */
3039 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
3040 bool yacc_rules
; /* in the rules part of a yacc file */
3041 struct tok savetoken
= {0}; /* token saved during preprocessor handling */
3044 linebuffer_init (&lbs
[0].lb
);
3045 linebuffer_init (&lbs
[1].lb
);
3046 if (cstack
.size
== 0)
3048 cstack
.size
= (DEBUG
) ? 1 : 4;
3050 cstack
.cname
= xnew (cstack
.size
, char *);
3051 cstack
.bracelev
= xnew (cstack
.size
, int);
3054 tokoff
= toklen
= typdefbracelev
= 0; /* keep compiler quiet */
3055 curndx
= newndx
= 0;
3059 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
3060 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
3062 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3063 token
.valid
= savetoken
.valid
= FALSE
;
3064 bracelev
= bracketlev
= parlev
= attrparlev
= templatelev
= 0;
3066 { qualifier
= "."; qlen
= 1; }
3068 { qualifier
= "::"; qlen
= 2; }
3076 /* If we are at the end of the line, the next character is a
3077 '\0'; do not skip it, because it is what tells us
3078 to read the next line. */
3099 /* Newlines inside comments do not end macro definitions in
3101 CNL_SAVE_DEFINEDEF ();
3114 /* Newlines inside strings do not end macro definitions
3115 in traditional cpp, even though compilers don't
3116 usually accept them. */
3117 CNL_SAVE_DEFINEDEF ();
3127 /* Hmmm, something went wrong. */
3163 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!= vignore
)
3178 else if (/* cplpl && */ *lp
== '/')
3184 if ((c_ext
& YACC
) && *lp
== '%')
3186 /* Entering or exiting rules section in yacc file. */
3188 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
3189 typdef
= tnone
; structdef
= snone
;
3190 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3192 yacc_rules
= !yacc_rules
;
3198 if (definedef
== dnone
)
3201 bool cpptoken
= TRUE
;
3203 /* Look back on this line. If all blanks, or nonblanks
3204 followed by an end of comment, this is a preprocessor
3206 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
3209 if (*cp
== '*' && cp
[1] == '/')
3218 definedef
= dsharpseen
;
3219 } /* if (definedef == dnone) */
3230 CNL_SAVE_DEFINEDEF ();
3237 /* Consider token only if some involved conditions are satisfied. */
3238 if (typdef
!= tignore
3239 && definedef
!= dignorerest
3242 && (definedef
!= dnone
3243 || structdef
!= scolonseen
)
3250 if (c
== ':' && *lp
== ':' && begtoken (lp
[1]))
3251 /* This handles :: in the middle,
3252 but not at the beginning of an identifier.
3253 Also, space-separated :: is not recognized. */
3255 if (c_ext
& C_AUTO
) /* automatic detection of C++ */
3256 c_ext
= (c_ext
| C_PLPL
) & ~C_AUTO
;
3260 goto still_in_token
;
3264 bool funorvar
= FALSE
;
3267 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
3268 &c_ext
, bracelev
, parlev
,
3271 if (fvdef
== foperator
)
3274 lp
= skip_spaces (lp
-1);
3278 && !iswhite (*lp
) && *lp
!= '(')
3281 toklen
+= lp
- oldlp
;
3283 token
.named
= FALSE
;
3285 && nestlev
> 0 && definedef
== dnone
)
3286 /* in struct body */
3289 write_classname (&token_name
, qualifier
);
3290 len
= token_name
.len
;
3291 linebuffer_setlen (&token_name
, len
+qlen
+toklen
);
3292 sprintf (token_name
.buffer
+ len
, "%s%.*s",
3293 qualifier
, toklen
, newlb
.buffer
+ tokoff
);
3296 else if (objdef
== ocatseen
)
3297 /* Objective C category */
3299 int len
= strlen (objtag
) + 2 + toklen
;
3300 linebuffer_setlen (&token_name
, len
);
3301 sprintf (token_name
.buffer
, "%s(%.*s)",
3302 objtag
, toklen
, newlb
.buffer
+ tokoff
);
3305 else if (objdef
== omethodtag
3306 || objdef
== omethodparm
)
3307 /* Objective C method */
3311 else if (fvdef
== fdefunname
)
3312 /* GNU DEFUN and similar macros */
3314 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
3318 /* Rewrite the tag so that emacs lisp DEFUNs
3319 can be found by their elisp name */
3325 linebuffer_setlen (&token_name
, len
);
3326 memcpy (token_name
.buffer
,
3327 newlb
.buffer
+ off
, len
);
3328 token_name
.buffer
[len
] = '\0';
3331 if (token_name
.buffer
[len
] == '_')
3332 token_name
.buffer
[len
] = '-';
3333 token
.named
= defun
;
3337 linebuffer_setlen (&token_name
, toklen
);
3338 memcpy (token_name
.buffer
,
3339 newlb
.buffer
+ tokoff
, toklen
);
3340 token_name
.buffer
[toklen
] = '\0';
3341 /* Name macros and members. */
3342 token
.named
= (structdef
== stagseen
3343 || typdef
== ttypeseen
3346 && definedef
== dignorerest
)
3348 && definedef
== dnone
3349 && structdef
== snone
3352 token
.lineno
= lineno
;
3353 token
.offset
= tokoff
;
3354 token
.length
= toklen
;
3355 token
.line
= newlb
.buffer
;
3356 token
.linepos
= newlinepos
;
3359 if (definedef
== dnone
3360 && (fvdef
== fvnameseen
3361 || fvdef
== foperator
3362 || structdef
== stagseen
3364 || typdef
== ttypeseen
3365 || objdef
!= onone
))
3367 if (current_lb_is_new
)
3368 switch_line_buffers ();
3370 else if (definedef
!= dnone
3371 || fvdef
== fdefunname
3373 make_C_tag (funorvar
);
3375 else /* not yacc and consider_token failed */
3377 if (inattribute
&& fvdef
== fignore
)
3379 /* We have just met __attribute__ after a
3380 function parameter list: do not tag the
3387 } /* if (endtoken (c)) */
3388 else if (intoken (c
))
3394 } /* if (midtoken) */
3395 else if (begtoken (c
))
3403 /* This prevents tagging fb in
3404 void (__attribute__((noreturn)) *fb) (void);
3405 Fixing this is not easy and not very important. */
3409 if (plainc
|| declarations
)
3411 make_C_tag (TRUE
); /* a function */
3416 if (structdef
== stagseen
&& !cjava
)
3418 popclass_above (bracelev
);
3426 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
3428 tokoff
= lp
- 1 - newlb
.buffer
;
3433 } /* if (begtoken) */
3434 } /* if must look at token */
3437 /* Detect end of line, colon, comma, semicolon and various braces
3438 after having handled a token.*/
3444 if (yacc_rules
&& token
.offset
== 0 && token
.valid
)
3446 make_C_tag (FALSE
); /* a yacc function */
3449 if (definedef
!= dnone
)
3455 make_C_tag (TRUE
); /* an Objective C class */
3459 objdef
= omethodcolon
;
3460 linebuffer_setlen (&token_name
, token_name
.len
+ 1);
3461 strcat (token_name
.buffer
, ":");
3464 if (structdef
== stagseen
)
3466 structdef
= scolonseen
;
3469 /* Should be useless, but may be work as a safety net. */
3470 if (cplpl
&& fvdef
== flistseen
)
3472 make_C_tag (TRUE
); /* a function */
3478 if (definedef
!= dnone
|| inattribute
)
3484 make_C_tag (FALSE
); /* a typedef */
3494 if (typdef
== tignore
|| cplpl
)
3498 if ((globals
&& bracelev
== 0 && (!fvextern
|| declarations
))
3499 || (members
&& instruct
))
3500 make_C_tag (FALSE
); /* a variable */
3503 token
.valid
= FALSE
;
3507 && (cplpl
|| !instruct
)
3508 && (typdef
== tnone
|| (typdef
!= tignore
&& instruct
)))
3510 && plainc
&& instruct
))
3511 make_C_tag (TRUE
); /* a function */
3517 && cplpl
&& structdef
== stagseen
)
3518 make_C_tag (FALSE
); /* forward declaration */
3520 token
.valid
= FALSE
;
3521 } /* switch (fvdef) */
3527 if (structdef
== stagseen
)
3531 if (definedef
!= dnone
|| inattribute
)
3537 make_C_tag (TRUE
); /* an Objective C method */
3558 && (!fvextern
|| declarations
))
3559 || (members
&& instruct
)))
3560 make_C_tag (FALSE
); /* a variable */
3563 if ((declarations
&& typdef
== tnone
&& !instruct
)
3564 || (members
&& typdef
!= tignore
&& instruct
))
3566 make_C_tag (TRUE
); /* a function */
3569 else if (!declarations
)
3571 token
.valid
= FALSE
;
3576 if (structdef
== stagseen
)
3580 if (definedef
!= dnone
|| inattribute
)
3582 if (structdef
== stagseen
)
3589 make_C_tag (FALSE
); /* a typedef */
3601 if ((members
&& bracelev
== 1)
3602 || (globals
&& bracelev
== 0
3603 && (!fvextern
|| declarations
)))
3604 make_C_tag (FALSE
); /* a variable */
3618 if (definedef
!= dnone
)
3620 if (objdef
== otagseen
&& parlev
== 0)
3621 objdef
= oparenseen
;
3625 if (typdef
== ttypeseen
3629 /* This handles constructs like:
3630 typedef void OperatorFun (int fun); */
3649 if (--attrparlev
== 0)
3650 inattribute
= FALSE
;
3653 if (definedef
!= dnone
)
3655 if (objdef
== ocatseen
&& parlev
== 1)
3657 make_C_tag (TRUE
); /* an Objective C category */
3671 || typdef
== ttypeseen
))
3674 make_C_tag (FALSE
); /* a typedef */
3677 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3681 if (definedef
!= dnone
)
3683 if (typdef
== ttypeseen
)
3685 /* Whenever typdef is set to tinbody (currently only
3686 here), typdefbracelev should be set to bracelev. */
3688 typdefbracelev
= bracelev
;
3693 make_C_tag (TRUE
); /* a function */
3702 make_C_tag (TRUE
); /* an Objective C class */
3707 make_C_tag (TRUE
); /* an Objective C method */
3711 /* Neutralize `extern "C" {' grot. */
3712 if (bracelev
== 0 && structdef
== snone
&& nestlev
== 0
3720 case skeyseen
: /* unnamed struct */
3721 pushclass_above (bracelev
, NULL
, 0);
3724 case stagseen
: /* named struct or enum */
3725 case scolonseen
: /* a class */
3726 pushclass_above (bracelev
,token
.line
+token
.offset
, token
.length
);
3728 make_C_tag (FALSE
); /* a struct or enum */
3734 if (definedef
!= dnone
)
3736 if (fvdef
== fstartlist
)
3738 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3739 token
.valid
= FALSE
;
3743 if (definedef
!= dnone
)
3746 if (!ignoreindent
&& lp
== newlb
.buffer
+ 1)
3749 token
.valid
= FALSE
; /* unexpected value, token unreliable */
3750 bracelev
= 0; /* reset brace level if first column */
3751 parlev
= 0; /* also reset paren level, just in case... */
3753 else if (bracelev
< 0)
3755 token
.valid
= FALSE
; /* something gone amiss, token unreliable */
3758 if (bracelev
== 0 && fvdef
== vignore
)
3759 fvdef
= fvnone
; /* end of function */
3760 popclass_above (bracelev
);
3762 /* Only if typdef == tinbody is typdefbracelev significant. */
3763 if (typdef
== tinbody
&& bracelev
<= typdefbracelev
)
3765 assert (bracelev
== typdefbracelev
);
3770 if (definedef
!= dnone
)
3780 if ((members
&& bracelev
== 1)
3781 || (globals
&& bracelev
== 0 && (!fvextern
|| declarations
)))
3782 make_C_tag (FALSE
); /* a variable */
3790 && (structdef
== stagseen
|| fvdef
== fvnameseen
))
3797 if (templatelev
> 0)
3805 if (objdef
== oinbody
&& bracelev
== 0)
3807 objdef
= omethodsign
;
3812 case '#': case '~': case '&': case '%': case '/':
3813 case '|': case '^': case '!': case '.': case '?':
3814 if (definedef
!= dnone
)
3816 /* These surely cannot follow a function tag in C. */
3829 if (objdef
== otagseen
)
3831 make_C_tag (TRUE
); /* an Objective C class */
3834 /* If a macro spans multiple lines don't reset its state. */
3836 CNL_SAVE_DEFINEDEF ();
3842 } /* while not eof */
3844 free (lbs
[0].lb
.buffer
);
3845 free (lbs
[1].lb
.buffer
);
3849 * Process either a C++ file or a C file depending on the setting
3853 default_C_entries (FILE *inf
)
3855 C_entries (cplusplus
? C_PLPL
: C_AUTO
, inf
);
3858 /* Always do plain C. */
3860 plain_C_entries (FILE *inf
)
3865 /* Always do C++. */
3867 Cplusplus_entries (FILE *inf
)
3869 C_entries (C_PLPL
, inf
);
3872 /* Always do Java. */
3874 Cjava_entries (FILE *inf
)
3876 C_entries (C_JAVA
, inf
);
3881 Cstar_entries (FILE *inf
)
3883 C_entries (C_STAR
, inf
);
3886 /* Always do Yacc. */
3888 Yacc_entries (FILE *inf
)
3890 C_entries (YACC
, inf
);
3894 /* Useful macros. */
3895 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3896 for (; /* loop initialization */ \
3897 !feof (file_pointer) /* loop test */ \
3898 && /* instructions at start of loop */ \
3899 (readline (&line_buffer, file_pointer), \
3900 char_pointer = line_buffer.buffer, \
3904 #define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \
3905 ((assert ("" kw), TRUE) /* syntax error if not a literal string */ \
3906 && strneq ((cp), kw, sizeof (kw)-1) /* cp points at kw */ \
3907 && notinname ((cp)[sizeof (kw)-1]) /* end of kw */ \
3908 && ((cp) = skip_spaces ((cp)+sizeof (kw)-1))) /* skip spaces */
3910 /* Similar to LOOKING_AT but does not use notinname, does not skip */
3911 #define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \
3912 ((assert ("" kw), TRUE) /* syntax error if not a literal string */ \
3913 && strncaseeq ((cp), kw, sizeof (kw)-1) /* cp points at kw */ \
3914 && ((cp) += sizeof (kw)-1)) /* skip spaces */
3917 * Read a file, but do no processing. This is used to do regexp
3918 * matching on files that have no language defined.
3921 just_read_file (FILE *inf
)
3924 readline (&lb
, inf
);
3928 /* Fortran parsing */
3930 static void F_takeprec (void);
3931 static void F_getit (FILE *);
3936 dbp
= skip_spaces (dbp
);
3940 dbp
= skip_spaces (dbp
);
3941 if (strneq (dbp
, "(*)", 3))
3946 if (!ISDIGIT (*dbp
))
3948 --dbp
; /* force failure */
3953 while (ISDIGIT (*dbp
));
3961 dbp
= skip_spaces (dbp
);
3964 readline (&lb
, inf
);
3969 dbp
= skip_spaces (dbp
);
3971 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
3973 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
3975 make_tag (dbp
, cp
-dbp
, TRUE
,
3976 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3981 Fortran_functions (FILE *inf
)
3983 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3986 dbp
++; /* Ratfor escape to fortran */
3987 dbp
= skip_spaces (dbp
);
3991 if (LOOKING_AT_NOCASE (dbp
, "recursive"))
3992 dbp
= skip_spaces (dbp
);
3994 if (LOOKING_AT_NOCASE (dbp
, "pure"))
3995 dbp
= skip_spaces (dbp
);
3997 if (LOOKING_AT_NOCASE (dbp
, "elemental"))
3998 dbp
= skip_spaces (dbp
);
4000 switch (lowcase (*dbp
))
4003 if (nocase_tail ("integer"))
4007 if (nocase_tail ("real"))
4011 if (nocase_tail ("logical"))
4015 if (nocase_tail ("complex") || nocase_tail ("character"))
4019 if (nocase_tail ("double"))
4021 dbp
= skip_spaces (dbp
);
4024 if (nocase_tail ("precision"))
4030 dbp
= skip_spaces (dbp
);
4033 switch (lowcase (*dbp
))
4036 if (nocase_tail ("function"))
4040 if (nocase_tail ("subroutine"))
4044 if (nocase_tail ("entry"))
4048 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
4050 dbp
= skip_spaces (dbp
);
4051 if (*dbp
== '\0') /* assume un-named */
4052 make_tag ("blockdata", 9, TRUE
,
4053 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
4055 F_getit (inf
); /* look for name */
4066 * Philippe Waroquiers (1998)
4069 /* Once we are positioned after an "interesting" keyword, let's get
4070 the real tag value necessary. */
4072 Ada_getit (FILE *inf
, const char *name_qualifier
)
4080 dbp
= skip_spaces (dbp
);
4082 || (dbp
[0] == '-' && dbp
[1] == '-'))
4084 readline (&lb
, inf
);
4087 switch (lowcase (*dbp
))
4090 if (nocase_tail ("body"))
4092 /* Skipping body of procedure body or package body or ....
4093 resetting qualifier to body instead of spec. */
4094 name_qualifier
= "/b";
4099 /* Skipping type of task type or protected type ... */
4100 if (nocase_tail ("type"))
4107 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
4112 dbp
= skip_spaces (dbp
);
4115 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
4123 name
= concat (dbp
, name_qualifier
, "");
4125 make_tag (name
, strlen (name
), TRUE
,
4126 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4135 Ada_funcs (FILE *inf
)
4137 bool inquote
= FALSE
;
4138 bool skip_till_semicolumn
= FALSE
;
4140 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4142 while (*dbp
!= '\0')
4144 /* Skip a string i.e. "abcd". */
4145 if (inquote
|| (*dbp
== '"'))
4147 dbp
= etags_strchr (dbp
+ !inquote
, '"');
4152 continue; /* advance char */
4157 break; /* advance line */
4161 /* Skip comments. */
4162 if (dbp
[0] == '-' && dbp
[1] == '-')
4163 break; /* advance line */
4165 /* Skip character enclosed in single quote i.e. 'a'
4166 and skip single quote starting an attribute i.e. 'Image. */
4175 if (skip_till_semicolumn
)
4178 skip_till_semicolumn
= FALSE
;
4180 continue; /* advance char */
4183 /* Search for beginning of a token. */
4184 if (!begtoken (*dbp
))
4187 continue; /* advance char */
4190 /* We are at the beginning of a token. */
4191 switch (lowcase (*dbp
))
4194 if (!packages_only
&& nocase_tail ("function"))
4195 Ada_getit (inf
, "/f");
4197 break; /* from switch */
4198 continue; /* advance char */
4200 if (!packages_only
&& nocase_tail ("procedure"))
4201 Ada_getit (inf
, "/p");
4202 else if (nocase_tail ("package"))
4203 Ada_getit (inf
, "/s");
4204 else if (nocase_tail ("protected")) /* protected type */
4205 Ada_getit (inf
, "/t");
4207 break; /* from switch */
4208 continue; /* advance char */
4211 if (typedefs
&& !packages_only
&& nocase_tail ("use"))
4213 /* when tagging types, avoid tagging use type Pack.Typename;
4214 for this, we will skip everything till a ; */
4215 skip_till_semicolumn
= TRUE
;
4216 continue; /* advance char */
4220 if (!packages_only
&& nocase_tail ("task"))
4221 Ada_getit (inf
, "/k");
4222 else if (typedefs
&& !packages_only
&& nocase_tail ("type"))
4224 Ada_getit (inf
, "/t");
4225 while (*dbp
!= '\0')
4229 break; /* from switch */
4230 continue; /* advance char */
4233 /* Look for the end of the token. */
4234 while (!endtoken (*dbp
))
4237 } /* advance char */
4238 } /* advance line */
4243 * Unix and microcontroller assembly tag handling
4244 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
4245 * Idea by Bob Weiner, Motorola Inc. (1994)
4248 Asm_labels (FILE *inf
)
4252 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4254 /* If first char is alphabetic or one of [_.$], test for colon
4255 following identifier. */
4256 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4258 /* Read past label. */
4260 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4262 if (*cp
== ':' || iswhite (*cp
))
4263 /* Found end of label, so copy it and add it to the table. */
4264 make_tag (lb
.buffer
, cp
- lb
.buffer
, TRUE
,
4265 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4273 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
4274 * /^use constant[ \t\n]+[^ \t\n{=,;]+/
4275 * Perl variable names: /^(my|local).../
4276 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
4277 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
4278 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
4281 Perl_functions (FILE *inf
)
4283 char *package
= savestr ("main"); /* current package name */
4286 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4288 cp
= skip_spaces (cp
);
4290 if (LOOKING_AT (cp
, "package"))
4293 get_tag (cp
, &package
);
4295 else if (LOOKING_AT (cp
, "sub"))
4301 while (!notinname (*cp
))
4304 continue; /* nothing found */
4305 if ((pos
= etags_strchr (sp
, ':')) != NULL
4306 && pos
< cp
&& pos
[1] == ':')
4307 /* The name is already qualified. */
4308 make_tag (sp
, cp
- sp
, TRUE
,
4309 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4313 char savechar
, *name
;
4317 name
= concat (package
, "::", sp
);
4319 make_tag (name
, strlen (name
), TRUE
,
4320 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4324 else if (LOOKING_AT (cp
, "use constant")
4325 || LOOKING_AT (cp
, "use constant::defer"))
4327 /* For hash style multi-constant like
4328 use constant { FOO => 123,
4330 only the first FOO is picked up. Parsing across the value
4331 expressions would be difficult in general, due to possible nested
4332 hashes, here-documents, etc. */
4334 cp
= skip_spaces (cp
+1);
4337 else if (globals
) /* only if we are tagging global vars */
4339 /* Skip a qualifier, if any. */
4340 bool qual
= LOOKING_AT (cp
, "my") || LOOKING_AT (cp
, "local");
4341 /* After "my" or "local", but before any following paren or space. */
4342 char *varstart
= cp
;
4344 if (qual
/* should this be removed? If yes, how? */
4345 && (*cp
== '$' || *cp
== '@' || *cp
== '%'))
4350 while (ISALNUM (*cp
) || *cp
== '_');
4354 /* Should be examining a variable list at this point;
4355 could insist on seeing an open parenthesis. */
4356 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
4362 make_tag (varstart
, cp
- varstart
, FALSE
,
4363 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4372 * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4373 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4374 * More ideas by seb bacon <seb@jamkit.com> (2002)
4377 Python_functions (FILE *inf
)
4381 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4383 cp
= skip_spaces (cp
);
4384 if (LOOKING_AT (cp
, "def") || LOOKING_AT (cp
, "class"))
4387 while (!notinname (*cp
) && *cp
!= ':')
4389 make_tag (name
, cp
- name
, TRUE
,
4390 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4399 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4400 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4401 * - /^[ \t]*define\(\"[^\"]+/
4402 * Only with --members:
4403 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4404 * Idea by Diez B. Roggisch (2001)
4407 PHP_functions (FILE *inf
)
4409 register char *cp
, *name
;
4410 bool search_identifier
= FALSE
;
4412 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4414 cp
= skip_spaces (cp
);
4416 if (search_identifier
4419 while (!notinname (*cp
))
4421 make_tag (name
, cp
- name
, TRUE
,
4422 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4423 search_identifier
= FALSE
;
4425 else if (LOOKING_AT (cp
, "function"))
4428 cp
= skip_spaces (cp
+1);
4432 while (!notinname (*cp
))
4434 make_tag (name
, cp
- name
, TRUE
,
4435 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4438 search_identifier
= TRUE
;
4440 else if (LOOKING_AT (cp
, "class"))
4445 while (*cp
!= '\0' && !iswhite (*cp
))
4447 make_tag (name
, cp
- name
, FALSE
,
4448 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4451 search_identifier
= TRUE
;
4453 else if (strneq (cp
, "define", 6)
4454 && (cp
= skip_spaces (cp
+6))
4456 && (*cp
== '"' || *cp
== '\''))
4460 while (*cp
!= quote
&& *cp
!= '\0')
4462 make_tag (name
, cp
- name
, FALSE
,
4463 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4466 && LOOKING_AT (cp
, "var")
4470 while (!notinname (*cp
))
4472 make_tag (name
, cp
- name
, FALSE
,
4473 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4480 * Cobol tag functions
4481 * We could look for anything that could be a paragraph name.
4482 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4483 * Idea by Corny de Souza (1993)
4486 Cobol_paragraphs (FILE *inf
)
4488 register char *bp
, *ep
;
4490 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4496 /* If eoln, compiler option or comment ignore whole line. */
4497 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
4500 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
4503 make_tag (bp
, ep
- bp
, TRUE
,
4504 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4511 * Ideas by Assar Westerlund <assar@sics.se> (2001)
4514 Makefile_targets (FILE *inf
)
4518 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4520 if (*bp
== '\t' || *bp
== '#')
4522 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
4524 if (*bp
== ':' || (globals
&& *bp
== '='))
4526 /* We should detect if there is more than one tag, but we do not.
4527 We just skip initial and final spaces. */
4528 char * namestart
= skip_spaces (lb
.buffer
);
4529 while (--bp
> namestart
)
4530 if (!notinname (*bp
))
4532 make_tag (namestart
, bp
- namestart
+ 1, TRUE
,
4533 lb
.buffer
, bp
- lb
.buffer
+ 2, lineno
, linecharno
);
4541 * Original code by Mosur K. Mohan (1989)
4543 * Locates tags for procedures & functions. Doesn't do any type- or
4544 * var-definitions. It does look for the keyword "extern" or
4545 * "forward" immediately following the procedure statement; if found,
4546 * the tag is skipped.
4549 Pascal_functions (FILE *inf
)
4551 linebuffer tline
; /* mostly copied from C_entries */
4553 int save_lineno
, namelen
, taglen
;
4556 bool /* each of these flags is TRUE if: */
4557 incomment
, /* point is inside a comment */
4558 inquote
, /* point is inside '..' string */
4559 get_tagname
, /* point is after PROCEDURE/FUNCTION
4560 keyword, so next item = potential tag */
4561 found_tag
, /* point is after a potential tag */
4562 inparms
, /* point is within parameter-list */
4563 verify_tag
; /* point has passed the parm-list, so the
4564 next token will determine whether this
4565 is a FORWARD/EXTERN to be ignored, or
4566 whether it is a real tag */
4568 save_lcno
= save_lineno
= namelen
= taglen
= 0; /* keep compiler quiet */
4569 name
= NULL
; /* keep compiler quiet */
4572 linebuffer_init (&tline
);
4574 incomment
= inquote
= FALSE
;
4575 found_tag
= FALSE
; /* have a proc name; check if extern */
4576 get_tagname
= FALSE
; /* found "procedure" keyword */
4577 inparms
= FALSE
; /* found '(' after "proc" */
4578 verify_tag
= FALSE
; /* check if "extern" is ahead */
4581 while (!feof (inf
)) /* long main loop to get next char */
4584 if (c
== '\0') /* if end of line */
4586 readline (&lb
, inf
);
4590 if (!((found_tag
&& verify_tag
)
4592 c
= *dbp
++; /* only if don't need *dbp pointing
4593 to the beginning of the name of
4594 the procedure or function */
4598 if (c
== '}') /* within { } comments */
4600 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4617 inquote
= TRUE
; /* found first quote */
4619 case '{': /* found open { comment */
4623 if (*dbp
== '*') /* found open (* comment */
4628 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4631 case ')': /* end of parms list */
4636 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4643 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4645 /* Check if this is an "extern" declaration. */
4648 if (lowcase (*dbp
) == 'e')
4650 if (nocase_tail ("extern")) /* superfluous, really! */
4656 else if (lowcase (*dbp
) == 'f')
4658 if (nocase_tail ("forward")) /* check for forward reference */
4664 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4668 make_tag (name
, namelen
, TRUE
,
4669 tline
.buffer
, taglen
, save_lineno
, save_lcno
);
4673 if (get_tagname
) /* grab name of proc or fn */
4680 /* Find block name. */
4681 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4684 /* Save all values for later tagging. */
4685 linebuffer_setlen (&tline
, lb
.len
);
4686 strcpy (tline
.buffer
, lb
.buffer
);
4687 save_lineno
= lineno
;
4688 save_lcno
= linecharno
;
4689 name
= tline
.buffer
+ (dbp
- lb
.buffer
);
4691 taglen
= cp
- lb
.buffer
+ 1;
4693 dbp
= cp
; /* set dbp to e-o-token */
4694 get_tagname
= FALSE
;
4698 /* And proceed to check for "extern". */
4700 else if (!incomment
&& !inquote
&& !found_tag
)
4702 /* Check for proc/fn keywords. */
4703 switch (lowcase (c
))
4706 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4710 if (nocase_tail ("unction"))
4715 } /* while not eof */
4717 free (tline
.buffer
);
4722 * Lisp tag functions
4723 * look for (def or (DEF, quote or QUOTE
4726 static void L_getit (void);
4731 if (*dbp
== '\'') /* Skip prefix quote */
4733 else if (*dbp
== '(')
4736 /* Try to skip "(quote " */
4737 if (!LOOKING_AT (dbp
, "quote") && !LOOKING_AT (dbp
, "QUOTE"))
4738 /* Ok, then skip "(" before name in (defstruct (foo)) */
4739 dbp
= skip_spaces (dbp
);
4741 get_tag (dbp
, NULL
);
4745 Lisp_functions (FILE *inf
)
4747 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4752 /* "(defvar foo)" is a declaration rather than a definition. */
4756 if (LOOKING_AT (p
, "defvar"))
4758 p
= skip_name (p
); /* past var name */
4759 p
= skip_spaces (p
);
4765 if (strneq (dbp
+1, "def", 3) || strneq (dbp
+1, "DEF", 3))
4767 dbp
= skip_non_spaces (dbp
);
4768 dbp
= skip_spaces (dbp
);
4773 /* Check for (foo::defmumble name-defined ... */
4776 while (!notinname (*dbp
) && *dbp
!= ':');
4781 while (*dbp
== ':');
4783 if (strneq (dbp
, "def", 3) || strneq (dbp
, "DEF", 3))
4785 dbp
= skip_non_spaces (dbp
);
4786 dbp
= skip_spaces (dbp
);
4796 * Lua script language parsing
4797 * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
4799 * "function" and "local function" are tags if they start at column 1.
4802 Lua_functions (FILE *inf
)
4806 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4808 if (bp
[0] != 'f' && bp
[0] != 'l')
4811 (void)LOOKING_AT (bp
, "local"); /* skip possible "local" */
4813 if (LOOKING_AT (bp
, "function"))
4821 * Just look for lines where the first character is '/'
4822 * Also look at "defineps" for PSWrap
4824 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4825 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4828 PS_functions (FILE *inf
)
4830 register char *bp
, *ep
;
4832 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4837 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4840 make_tag (bp
, ep
- bp
, TRUE
,
4841 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4843 else if (LOOKING_AT (bp
, "defineps"))
4851 * Ignore anything after \ followed by space or in ( )
4852 * Look for words defined by :
4853 * Look for constant, code, create, defer, value, and variable
4854 * OBP extensions: Look for buffer:, field,
4855 * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004)
4858 Forth_words (FILE *inf
)
4862 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4863 while ((bp
= skip_spaces (bp
))[0] != '\0')
4864 if (bp
[0] == '\\' && iswhite (bp
[1]))
4865 break; /* read next line */
4866 else if (bp
[0] == '(' && iswhite (bp
[1]))
4867 do /* skip to ) or eol */
4869 while (*bp
!= ')' && *bp
!= '\0');
4870 else if ((bp
[0] == ':' && iswhite (bp
[1]) && bp
++)
4871 || LOOKING_AT_NOCASE (bp
, "constant")
4872 || LOOKING_AT_NOCASE (bp
, "code")
4873 || LOOKING_AT_NOCASE (bp
, "create")
4874 || LOOKING_AT_NOCASE (bp
, "defer")
4875 || LOOKING_AT_NOCASE (bp
, "value")
4876 || LOOKING_AT_NOCASE (bp
, "variable")
4877 || LOOKING_AT_NOCASE (bp
, "buffer:")
4878 || LOOKING_AT_NOCASE (bp
, "field"))
4879 get_tag (skip_spaces (bp
), NULL
); /* Yay! A definition! */
4881 bp
= skip_non_spaces (bp
);
4886 * Scheme tag functions
4887 * look for (def... xyzzy
4889 * (def ... ((...(xyzzy ....
4891 * Original code by Ken Haase (1985?)
4894 Scheme_functions (FILE *inf
)
4898 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4900 if (strneq (bp
, "(def", 4) || strneq (bp
, "(DEF", 4))
4902 bp
= skip_non_spaces (bp
+4);
4903 /* Skip over open parens and white space. Don't continue past
4905 while (*bp
&& notinname (*bp
))
4909 if (LOOKING_AT (bp
, "(SET!") || LOOKING_AT (bp
, "(set!"))
4915 /* Find tags in TeX and LaTeX input files. */
4917 /* TEX_toktab is a table of TeX control sequences that define tags.
4918 * Each entry records one such control sequence.
4920 * Original code from who knows whom.
4922 * Stefan Monnier (2002)
4925 static linebuffer
*TEX_toktab
= NULL
; /* Table with tag tokens */
4927 /* Default set of control sequences to put into TEX_toktab.
4928 The value of environment var TEXTAGS is prepended to this. */
4929 static const char *TEX_defenv
= "\
4930 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4931 :part:appendix:entry:index:def\
4932 :newcommand:renewcommand:newenvironment:renewenvironment";
4934 static void TEX_mode (FILE *);
4935 static void TEX_decode_env (const char *, const char *);
4937 static char TEX_esc
= '\\';
4938 static char TEX_opgrp
= '{';
4939 static char TEX_clgrp
= '}';
4942 * TeX/LaTeX scanning loop.
4945 TeX_commands (FILE *inf
)
4950 /* Select either \ or ! as escape character. */
4953 /* Initialize token table once from environment. */
4954 if (TEX_toktab
== NULL
)
4955 TEX_decode_env ("TEXTAGS", TEX_defenv
);
4957 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4959 /* Look at each TEX keyword in line. */
4962 /* Look for a TEX escape. */
4963 while (*cp
++ != TEX_esc
)
4964 if (cp
[-1] == '\0' || cp
[-1] == '%')
4967 for (key
= TEX_toktab
; key
->buffer
!= NULL
; key
++)
4968 if (strneq (cp
, key
->buffer
, key
->len
))
4971 int namelen
, linelen
;
4974 cp
= skip_spaces (cp
+ key
->len
);
4975 if (*cp
== TEX_opgrp
)
4981 (!iswhite (*p
) && *p
!= '#' &&
4982 *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
);
4987 if (!opgrp
|| *p
== TEX_clgrp
)
4989 while (*p
!= '\0' && *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
)
4991 linelen
= p
- lb
.buffer
+ 1;
4993 make_tag (cp
, namelen
, TRUE
,
4994 lb
.buffer
, linelen
, lineno
, linecharno
);
4995 goto tex_next_line
; /* We only tag a line once */
5003 #define TEX_LESC '\\'
5004 #define TEX_SESC '!'
5006 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
5007 chars accordingly. */
5009 TEX_mode (FILE *inf
)
5013 while ((c
= getc (inf
)) != EOF
)
5015 /* Skip to next line if we hit the TeX comment char. */
5017 while (c
!= '\n' && c
!= EOF
)
5019 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
5035 /* If the input file is compressed, inf is a pipe, and rewind may fail.
5036 No attempt is made to correct the situation. */
5040 /* Read environment and prepend it to the default string.
5041 Build token table. */
5043 TEX_decode_env (const char *evarname
, const char *defenv
)
5045 register const char *env
, *p
;
5048 /* Append default string to environment. */
5049 env
= getenv (evarname
);
5053 env
= concat (env
, defenv
, "");
5055 /* Allocate a token table */
5056 for (len
= 1, p
= env
; p
;)
5057 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
5059 TEX_toktab
= xnew (len
, linebuffer
);
5061 /* Unpack environment string into token table. Be careful about */
5062 /* zero-length strings (leading ':', "::" and trailing ':') */
5063 for (i
= 0; *env
!= '\0';)
5065 p
= etags_strchr (env
, ':');
5066 if (!p
) /* End of environment string. */
5067 p
= env
+ strlen (env
);
5069 { /* Only non-zero strings. */
5070 TEX_toktab
[i
].buffer
= savenstr (env
, p
- env
);
5071 TEX_toktab
[i
].len
= p
- env
;
5078 TEX_toktab
[i
].buffer
= NULL
; /* Mark end of table. */
5079 TEX_toktab
[i
].len
= 0;
5086 /* Texinfo support. Dave Love, Mar. 2000. */
5088 Texinfo_nodes (FILE *inf
)
5091 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5092 if (LOOKING_AT (cp
, "@node"))
5095 while (*cp
!= '\0' && *cp
!= ',')
5097 make_tag (start
, cp
- start
, TRUE
,
5098 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5105 * Contents of <title>, <h1>, <h2>, <h3> are tags.
5106 * Contents of <a name=xxx> are tags with name xxx.
5108 * Francesco Potortì, 2002.
5111 HTML_labels (FILE *inf
)
5113 bool getnext
= FALSE
; /* next text outside of HTML tags is a tag */
5114 bool skiptag
= FALSE
; /* skip to the end of the current HTML tag */
5115 bool intag
= FALSE
; /* inside an html tag, looking for ID= */
5116 bool inanchor
= FALSE
; /* when INTAG, is an anchor, look for NAME= */
5120 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5122 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
5123 for (;;) /* loop on the same line */
5125 if (skiptag
) /* skip HTML tag */
5127 while (*dbp
!= '\0' && *dbp
!= '>')
5133 continue; /* look on the same line */
5135 break; /* go to next line */
5138 else if (intag
) /* look for "name=" or "id=" */
5140 while (*dbp
!= '\0' && *dbp
!= '>'
5141 && lowcase (*dbp
) != 'n' && lowcase (*dbp
) != 'i')
5144 break; /* go to next line */
5149 continue; /* look on the same line */
5151 if ((inanchor
&& LOOKING_AT_NOCASE (dbp
, "name="))
5152 || LOOKING_AT_NOCASE (dbp
, "id="))
5154 bool quoted
= (dbp
[0] == '"');
5157 for (end
= ++dbp
; *end
!= '\0' && *end
!= '"'; end
++)
5160 for (end
= dbp
; *end
!= '\0' && intoken (*end
); end
++)
5162 linebuffer_setlen (&token_name
, end
- dbp
);
5163 memcpy (token_name
.buffer
, dbp
, end
- dbp
);
5164 token_name
.buffer
[end
- dbp
] = '\0';
5167 intag
= FALSE
; /* we found what we looked for */
5168 skiptag
= TRUE
; /* skip to the end of the tag */
5169 getnext
= TRUE
; /* then grab the text */
5170 continue; /* look on the same line */
5175 else if (getnext
) /* grab next tokens and tag them */
5177 dbp
= skip_spaces (dbp
);
5179 break; /* go to next line */
5183 inanchor
= (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]));
5184 continue; /* look on the same line */
5187 for (end
= dbp
+ 1; *end
!= '\0' && *end
!= '<'; end
++)
5189 make_tag (token_name
.buffer
, token_name
.len
, TRUE
,
5190 dbp
, end
- dbp
, lineno
, linecharno
);
5191 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5193 break; /* go to next line */
5196 else /* look for an interesting HTML tag */
5198 while (*dbp
!= '\0' && *dbp
!= '<')
5201 break; /* go to next line */
5203 if (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]))
5206 continue; /* look on the same line */
5208 else if (LOOKING_AT_NOCASE (dbp
, "<title>")
5209 || LOOKING_AT_NOCASE (dbp
, "<h1>")
5210 || LOOKING_AT_NOCASE (dbp
, "<h2>")
5211 || LOOKING_AT_NOCASE (dbp
, "<h3>"))
5215 continue; /* look on the same line */
5226 * Assumes that the predicate or rule starts at column 0.
5227 * Only the first clause of a predicate or rule is added.
5228 * Original code by Sunichirou Sugou (1989)
5229 * Rewritten by Anders Lindgren (1996)
5231 static size_t prolog_pr (char *, char *);
5232 static void prolog_skip_comment (linebuffer
*, FILE *);
5233 static size_t prolog_atom (char *, size_t);
5236 Prolog_functions (FILE *inf
)
5246 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5248 if (cp
[0] == '\0') /* Empty line */
5250 else if (iswhite (cp
[0])) /* Not a predicate */
5252 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
5253 prolog_skip_comment (&lb
, inf
);
5254 else if ((len
= prolog_pr (cp
, last
)) > 0)
5256 /* Predicate or rule. Store the function name so that we
5257 only generate a tag for the first clause. */
5259 last
= xnew (len
+ 1, char);
5260 else if (len
+ 1 > allocated
)
5261 xrnew (last
, len
+ 1, char);
5262 allocated
= len
+ 1;
5263 memcpy (last
, cp
, len
);
5272 prolog_skip_comment (linebuffer
*plb
, FILE *inf
)
5278 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
5279 if (cp
[0] == '*' && cp
[1] == '/')
5281 readline (plb
, inf
);
5283 while (!feof (inf
));
5287 * A predicate or rule definition is added if it matches:
5288 * <beginning of line><Prolog Atom><whitespace>(
5289 * or <beginning of line><Prolog Atom><whitespace>:-
5291 * It is added to the tags database if it doesn't match the
5292 * name of the previous clause header.
5294 * Return the size of the name of the predicate or rule, or 0 if no
5298 prolog_pr (char *s
, char *last
)
5300 /* Name of last clause. */
5305 pos
= prolog_atom (s
, 0);
5310 pos
= skip_spaces (s
+ pos
) - s
;
5313 || (s
[pos
] == '(' && (pos
+= 1))
5314 || (s
[pos
] == ':' && s
[pos
+ 1] == '-' && (pos
+= 2)))
5315 && (last
== NULL
/* save only the first clause */
5316 || len
!= strlen (last
)
5317 || !strneq (s
, last
, len
)))
5319 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5327 * Consume a Prolog atom.
5328 * Return the number of bytes consumed, or 0 if there was an error.
5330 * A prolog atom, in this context, could be one of:
5331 * - An alphanumeric sequence, starting with a lower case letter.
5332 * - A quoted arbitrary string. Single quotes can escape themselves.
5333 * Backslash quotes everything.
5336 prolog_atom (char *s
, size_t pos
)
5342 if (ISLOWER (s
[pos
]) || (s
[pos
] == '_'))
5344 /* The atom is unquoted. */
5346 while (ISALNUM (s
[pos
]) || (s
[pos
] == '_'))
5350 return pos
- origpos
;
5352 else if (s
[pos
] == '\'')
5363 pos
++; /* A double quote */
5365 else if (s
[pos
] == '\0')
5366 /* Multiline quoted atoms are ignored. */
5368 else if (s
[pos
] == '\\')
5370 if (s
[pos
+1] == '\0')
5377 return pos
- origpos
;
5385 * Support for Erlang
5387 * Generates tags for functions, defines, and records.
5388 * Assumes that Erlang functions start at column 0.
5389 * Original code by Anders Lindgren (1996)
5391 static int erlang_func (char *, char *);
5392 static void erlang_attribute (char *);
5393 static int erlang_atom (char *);
5396 Erlang_functions (FILE *inf
)
5406 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5408 if (cp
[0] == '\0') /* Empty line */
5410 else if (iswhite (cp
[0])) /* Not function nor attribute */
5412 else if (cp
[0] == '%') /* comment */
5414 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
5416 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
5418 erlang_attribute (cp
);
5425 else if ((len
= erlang_func (cp
, last
)) > 0)
5428 * Function. Store the function name so that we only
5429 * generates a tag for the first clause.
5432 last
= xnew (len
+ 1, char);
5433 else if (len
+ 1 > allocated
)
5434 xrnew (last
, len
+ 1, char);
5435 allocated
= len
+ 1;
5436 memcpy (last
, cp
, len
);
5445 * A function definition is added if it matches:
5446 * <beginning of line><Erlang Atom><whitespace>(
5448 * It is added to the tags database if it doesn't match the
5449 * name of the previous clause header.
5451 * Return the size of the name of the function, or 0 if no function
5455 erlang_func (char *s
, char *last
)
5457 /* Name of last clause. */
5462 pos
= erlang_atom (s
);
5467 pos
= skip_spaces (s
+ pos
) - s
;
5469 /* Save only the first clause. */
5472 || len
!= (int)strlen (last
)
5473 || !strneq (s
, last
, len
)))
5475 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5484 * Handle attributes. Currently, tags are generated for defines
5487 * They are on the form:
5488 * -define(foo, bar).
5489 * -define(Foo(M, N), M+N).
5490 * -record(graph, {vtab = notable, cyclic = true}).
5493 erlang_attribute (char *s
)
5497 if ((LOOKING_AT (cp
, "-define") || LOOKING_AT (cp
, "-record"))
5500 int len
= erlang_atom (skip_spaces (cp
));
5502 make_tag (cp
, len
, TRUE
, s
, cp
+ len
- s
, lineno
, linecharno
);
5509 * Consume an Erlang atom (or variable).
5510 * Return the number of bytes consumed, or -1 if there was an error.
5513 erlang_atom (char *s
)
5517 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
5519 /* The atom is unquoted. */
5522 while (ISALNUM (s
[pos
]) || s
[pos
] == '_');
5524 else if (s
[pos
] == '\'')
5526 for (pos
++; s
[pos
] != '\''; pos
++)
5527 if (s
[pos
] == '\0' /* multiline quoted atoms are ignored */
5528 || (s
[pos
] == '\\' && s
[++pos
] == '\0'))
5537 static char *scan_separators (char *);
5538 static void add_regex (char *, language
*);
5539 static char *substitute (char *, char *, struct re_registers
*);
5542 * Take a string like "/blah/" and turn it into "blah", verifying
5543 * that the first and last characters are the same, and handling
5544 * quoted separator characters. Actually, stops on the occurrence of
5545 * an unquoted separator. Also process \t, \n, etc. and turn into
5546 * appropriate characters. Works in place. Null terminates name string.
5547 * Returns pointer to terminating separator, or NULL for
5548 * unterminated regexps.
5551 scan_separators (char *name
)
5554 char *copyto
= name
;
5555 bool quoted
= FALSE
;
5557 for (++name
; *name
!= '\0'; ++name
)
5563 case 'a': *copyto
++ = '\007'; break; /* BEL (bell) */
5564 case 'b': *copyto
++ = '\b'; break; /* BS (back space) */
5565 case 'd': *copyto
++ = 0177; break; /* DEL (delete) */
5566 case 'e': *copyto
++ = 033; break; /* ESC (delete) */
5567 case 'f': *copyto
++ = '\f'; break; /* FF (form feed) */
5568 case 'n': *copyto
++ = '\n'; break; /* NL (new line) */
5569 case 'r': *copyto
++ = '\r'; break; /* CR (carriage return) */
5570 case 't': *copyto
++ = '\t'; break; /* TAB (horizontal tab) */
5571 case 'v': *copyto
++ = '\v'; break; /* VT (vertical tab) */
5577 /* Something else is quoted, so preserve the quote. */
5585 else if (*name
== '\\')
5587 else if (*name
== sep
)
5593 name
= NULL
; /* signal unterminated regexp */
5595 /* Terminate copied string. */
5600 /* Look at the argument of --regex or --no-regex and do the right
5601 thing. Same for each line of a regexp file. */
5603 analyse_regex (char *regex_arg
)
5605 if (regex_arg
== NULL
)
5607 free_regexps (); /* --no-regex: remove existing regexps */
5611 /* A real --regexp option or a line in a regexp file. */
5612 switch (regex_arg
[0])
5614 /* Comments in regexp file or null arg to --regex. */
5620 /* Read a regex file. This is recursive and may result in a
5621 loop, which will stop when the file descriptors are exhausted. */
5625 linebuffer regexbuf
;
5626 char *regexfile
= regex_arg
+ 1;
5628 /* regexfile is a file containing regexps, one per line. */
5629 regexfp
= fopen (regexfile
, "r");
5630 if (regexfp
== NULL
)
5635 linebuffer_init (®exbuf
);
5636 while (readline_internal (®exbuf
, regexfp
) > 0)
5637 analyse_regex (regexbuf
.buffer
);
5638 free (regexbuf
.buffer
);
5643 /* Regexp to be used for a specific language only. */
5647 char *lang_name
= regex_arg
+ 1;
5650 for (cp
= lang_name
; *cp
!= '}'; cp
++)
5653 error ("unterminated language name in regex: %s", regex_arg
);
5657 lang
= get_language_from_langname (lang_name
);
5660 add_regex (cp
, lang
);
5664 /* Regexp to be used for any language. */
5666 add_regex (regex_arg
, NULL
);
5671 /* Separate the regexp pattern, compile it,
5672 and care for optional name and modifiers. */
5674 add_regex (char *regexp_pattern
, language
*lang
)
5676 static struct re_pattern_buffer zeropattern
;
5677 char sep
, *pat
, *name
, *modifiers
;
5680 struct re_pattern_buffer
*patbuf
;
5683 force_explicit_name
= TRUE
, /* do not use implicit tag names */
5684 ignore_case
= FALSE
, /* case is significant */
5685 multi_line
= FALSE
, /* matches are done one line at a time */
5686 single_line
= FALSE
; /* dot does not match newline */
5689 if (strlen (regexp_pattern
) < 3)
5691 error ("null regexp");
5694 sep
= regexp_pattern
[0];
5695 name
= scan_separators (regexp_pattern
);
5698 error ("%s: unterminated regexp", regexp_pattern
);
5703 error ("null name for regexp \"%s\"", regexp_pattern
);
5706 modifiers
= scan_separators (name
);
5707 if (modifiers
== NULL
) /* no terminating separator --> no name */
5713 modifiers
+= 1; /* skip separator */
5715 /* Parse regex modifiers. */
5716 for (; modifiers
[0] != '\0'; modifiers
++)
5717 switch (modifiers
[0])
5720 if (modifiers
== name
)
5721 error ("forcing explicit tag name but no name, ignoring");
5722 force_explicit_name
= TRUE
;
5732 need_filebuf
= TRUE
;
5735 error ("invalid regexp modifier `%c', ignoring", modifiers
[0]);
5739 patbuf
= xnew (1, struct re_pattern_buffer
);
5740 *patbuf
= zeropattern
;
5743 static char lc_trans
[CHARS
];
5745 for (i
= 0; i
< CHARS
; i
++)
5746 lc_trans
[i
] = lowcase (i
);
5747 patbuf
->translate
= lc_trans
; /* translation table to fold case */
5751 pat
= concat ("^", regexp_pattern
, ""); /* anchor to beginning of line */
5753 pat
= regexp_pattern
;
5756 re_set_syntax (RE_SYNTAX_EMACS
| RE_DOT_NEWLINE
);
5758 re_set_syntax (RE_SYNTAX_EMACS
);
5760 err
= re_compile_pattern (pat
, strlen (pat
), patbuf
);
5765 error ("%s while compiling pattern", err
);
5770 p_head
= xnew (1, regexp
);
5771 p_head
->pattern
= savestr (regexp_pattern
);
5772 p_head
->p_next
= rp
;
5773 p_head
->lang
= lang
;
5774 p_head
->pat
= patbuf
;
5775 p_head
->name
= savestr (name
);
5776 p_head
->error_signaled
= FALSE
;
5777 p_head
->force_explicit_name
= force_explicit_name
;
5778 p_head
->ignore_case
= ignore_case
;
5779 p_head
->multi_line
= multi_line
;
5783 * Do the substitutions indicated by the regular expression and
5787 substitute (char *in
, char *out
, struct re_registers
*regs
)
5790 int size
, dig
, diglen
;
5793 size
= strlen (out
);
5795 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5796 if (out
[size
- 1] == '\\')
5797 fatal ("pattern error in \"%s\"", out
);
5798 for (t
= etags_strchr (out
, '\\');
5800 t
= etags_strchr (t
+ 2, '\\'))
5804 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5810 /* Allocate space and do the substitutions. */
5812 result
= xnew (size
+ 1, char);
5814 for (t
= result
; *out
!= '\0'; out
++)
5815 if (*out
== '\\' && ISDIGIT (*++out
))
5818 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5819 memcpy (t
, in
+ regs
->start
[dig
], diglen
);
5826 assert (t
<= result
+ size
);
5827 assert (t
- result
== (int)strlen (result
));
5832 /* Deallocate all regexps. */
5837 while (p_head
!= NULL
)
5839 rp
= p_head
->p_next
;
5840 free (p_head
->pattern
);
5841 free (p_head
->name
);
5849 * Reads the whole file as a single string from `filebuf' and looks for
5850 * multi-line regular expressions, creating tags on matches.
5851 * readline already dealt with normal regexps.
5853 * Idea by Ben Wing <ben@666.com> (2002).
5856 regex_tag_multiline (void)
5858 char *buffer
= filebuf
.buffer
;
5862 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
5866 if (!rp
->multi_line
)
5867 continue; /* skip normal regexps */
5869 /* Generic initializations before parsing file from memory. */
5870 lineno
= 1; /* reset global line number */
5871 charno
= 0; /* reset global char number */
5872 linecharno
= 0; /* reset global char number of line start */
5874 /* Only use generic regexps or those for the current language. */
5875 if (rp
->lang
!= NULL
&& rp
->lang
!= curfdp
->lang
)
5878 while (match
>= 0 && match
< filebuf
.len
)
5880 match
= re_search (rp
->pat
, buffer
, filebuf
.len
, charno
,
5881 filebuf
.len
- match
, &rp
->regs
);
5886 if (!rp
->error_signaled
)
5888 error ("regexp stack overflow while matching \"%s\"",
5890 rp
->error_signaled
= TRUE
;
5897 if (match
== rp
->regs
.end
[0])
5899 if (!rp
->error_signaled
)
5901 error ("regexp matches the empty string: \"%s\"",
5903 rp
->error_signaled
= TRUE
;
5905 match
= -3; /* exit from while loop */
5909 /* Match occurred. Construct a tag. */
5910 while (charno
< rp
->regs
.end
[0])
5911 if (buffer
[charno
++] == '\n')
5912 lineno
++, linecharno
= charno
;
5914 if (name
[0] == '\0')
5916 else /* make a named tag */
5917 name
= substitute (buffer
, rp
->name
, &rp
->regs
);
5918 if (rp
->force_explicit_name
)
5919 /* Force explicit tag name, if a name is there. */
5920 pfnote (name
, TRUE
, buffer
+ linecharno
,
5921 charno
- linecharno
+ 1, lineno
, linecharno
);
5923 make_tag (name
, strlen (name
), TRUE
, buffer
+ linecharno
,
5924 charno
- linecharno
+ 1, lineno
, linecharno
);
5933 nocase_tail (const char *cp
)
5935 register int len
= 0;
5937 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
5939 if (*cp
== '\0' && !intoken (dbp
[len
]))
5948 get_tag (register char *bp
, char **namepp
)
5950 register char *cp
= bp
;
5954 /* Go till you get to white space or a syntactic break */
5955 for (cp
= bp
+ 1; !notinname (*cp
); cp
++)
5957 make_tag (bp
, cp
- bp
, TRUE
,
5958 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5962 *namepp
= savenstr (bp
, cp
- bp
);
5966 * Read a line of text from `stream' into `lbp', excluding the
5967 * newline or CR-NL, if any. Return the number of characters read from
5968 * `stream', which is the length of the line including the newline.
5970 * On DOS or Windows we do not count the CR character, if any before the
5971 * NL, in the returned length; this mirrors the behavior of Emacs on those
5972 * platforms (for text files, it translates CR-NL to NL as it reads in the
5975 * If multi-line regular expressions are requested, each line read is
5976 * appended to `filebuf'.
5979 readline_internal (linebuffer
*lbp
, register FILE *stream
)
5981 char *buffer
= lbp
->buffer
;
5982 register char *p
= lbp
->buffer
;
5983 register char *pend
;
5986 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
5990 register int c
= getc (stream
);
5993 /* We're at the end of linebuffer: expand it. */
5995 xrnew (buffer
, lbp
->size
, char);
5996 p
+= buffer
- lbp
->buffer
;
5997 pend
= buffer
+ lbp
->size
;
5998 lbp
->buffer
= buffer
;
6008 if (p
> buffer
&& p
[-1] == '\r')
6012 /* Assume CRLF->LF translation will be performed by Emacs
6013 when loading this file, so CRs won't appear in the buffer.
6014 It would be cleaner to compensate within Emacs;
6015 however, Emacs does not know how many CRs were deleted
6016 before any given point in the file. */
6031 lbp
->len
= p
- buffer
;
6033 if (need_filebuf
/* we need filebuf for multi-line regexps */
6034 && chars_deleted
> 0) /* not at EOF */
6036 while (filebuf
.size
<= filebuf
.len
+ lbp
->len
+ 1) /* +1 for \n */
6038 /* Expand filebuf. */
6040 xrnew (filebuf
.buffer
, filebuf
.size
, char);
6042 memcpy (filebuf
.buffer
+ filebuf
.len
, lbp
->buffer
, lbp
->len
);
6043 filebuf
.len
+= lbp
->len
;
6044 filebuf
.buffer
[filebuf
.len
++] = '\n';
6045 filebuf
.buffer
[filebuf
.len
] = '\0';
6048 return lbp
->len
+ chars_deleted
;
6052 * Like readline_internal, above, but in addition try to match the
6053 * input line against relevant regular expressions and manage #line
6057 readline (linebuffer
*lbp
, FILE *stream
)
6061 linecharno
= charno
; /* update global char number of line start */
6062 result
= readline_internal (lbp
, stream
); /* read line */
6063 lineno
+= 1; /* increment global line number */
6064 charno
+= result
; /* increment global char number */
6066 /* Honor #line directives. */
6067 if (!no_line_directive
)
6069 static bool discard_until_line_directive
;
6071 /* Check whether this is a #line directive. */
6072 if (result
> 12 && strneq (lbp
->buffer
, "#line ", 6))
6077 if (sscanf (lbp
->buffer
, "#line %u \"%n", &lno
, &start
) >= 1
6078 && start
> 0) /* double quote character found */
6080 char *endp
= lbp
->buffer
+ start
;
6082 while ((endp
= etags_strchr (endp
, '"')) != NULL
6083 && endp
[-1] == '\\')
6086 /* Ok, this is a real #line directive. Let's deal with it. */
6088 char *taggedabsname
; /* absolute name of original file */
6089 char *taggedfname
; /* name of original file as given */
6090 char *name
; /* temp var */
6092 discard_until_line_directive
= FALSE
; /* found it */
6093 name
= lbp
->buffer
+ start
;
6095 canonicalize_filename (name
);
6096 taggedabsname
= absolute_filename (name
, tagfiledir
);
6097 if (filename_is_absolute (name
)
6098 || filename_is_absolute (curfdp
->infname
))
6099 taggedfname
= savestr (taggedabsname
);
6101 taggedfname
= relative_filename (taggedabsname
,tagfiledir
);
6103 if (streq (curfdp
->taggedfname
, taggedfname
))
6104 /* The #line directive is only a line number change. We
6105 deal with this afterwards. */
6108 /* The tags following this #line directive should be
6109 attributed to taggedfname. In order to do this, set
6110 curfdp accordingly. */
6112 fdesc
*fdp
; /* file description pointer */
6114 /* Go look for a file description already set up for the
6115 file indicated in the #line directive. If there is
6116 one, use it from now until the next #line
6118 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6119 if (streq (fdp
->infname
, curfdp
->infname
)
6120 && streq (fdp
->taggedfname
, taggedfname
))
6121 /* If we remove the second test above (after the &&)
6122 then all entries pertaining to the same file are
6123 coalesced in the tags file. If we use it, then
6124 entries pertaining to the same file but generated
6125 from different files (via #line directives) will
6126 go into separate sections in the tags file. These
6127 alternatives look equivalent. The first one
6128 destroys some apparently useless information. */
6134 /* Else, if we already tagged the real file, skip all
6135 input lines until the next #line directive. */
6136 if (fdp
== NULL
) /* not found */
6137 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6138 if (streq (fdp
->infabsname
, taggedabsname
))
6140 discard_until_line_directive
= TRUE
;
6144 /* Else create a new file description and use that from
6145 now on, until the next #line directive. */
6146 if (fdp
== NULL
) /* not found */
6149 fdhead
= xnew (1, fdesc
);
6150 *fdhead
= *curfdp
; /* copy curr. file description */
6152 fdhead
->infname
= savestr (curfdp
->infname
);
6153 fdhead
->infabsname
= savestr (curfdp
->infabsname
);
6154 fdhead
->infabsdir
= savestr (curfdp
->infabsdir
);
6155 fdhead
->taggedfname
= taggedfname
;
6156 fdhead
->usecharno
= FALSE
;
6157 fdhead
->prop
= NULL
;
6158 fdhead
->written
= FALSE
;
6162 free (taggedabsname
);
6164 readline (lbp
, stream
);
6166 } /* if a real #line directive */
6167 } /* if #line is followed by a number */
6168 } /* if line begins with "#line " */
6170 /* If we are here, no #line directive was found. */
6171 if (discard_until_line_directive
)
6175 /* Do a tail recursion on ourselves, thus discarding the contents
6176 of the line buffer. */
6177 readline (lbp
, stream
);
6181 discard_until_line_directive
= FALSE
;
6184 } /* if #line directives should be considered */
6191 /* Match against relevant regexps. */
6193 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6195 /* Only use generic regexps or those for the current language.
6196 Also do not use multiline regexps, which is the job of
6197 regex_tag_multiline. */
6198 if ((rp
->lang
!= NULL
&& rp
->lang
!= fdhead
->lang
)
6202 match
= re_match (rp
->pat
, lbp
->buffer
, lbp
->len
, 0, &rp
->regs
);
6207 if (!rp
->error_signaled
)
6209 error ("regexp stack overflow while matching \"%s\"",
6211 rp
->error_signaled
= TRUE
;
6218 /* Empty string matched. */
6219 if (!rp
->error_signaled
)
6221 error ("regexp matches the empty string: \"%s\"", rp
->pattern
);
6222 rp
->error_signaled
= TRUE
;
6226 /* Match occurred. Construct a tag. */
6228 if (name
[0] == '\0')
6230 else /* make a named tag */
6231 name
= substitute (lbp
->buffer
, rp
->name
, &rp
->regs
);
6232 if (rp
->force_explicit_name
)
6233 /* Force explicit tag name, if a name is there. */
6234 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
6236 make_tag (name
, strlen (name
), TRUE
,
6237 lbp
->buffer
, match
, lineno
, linecharno
);
6246 * Return a pointer to a space of size strlen(cp)+1 allocated
6247 * with xnew where the string CP has been copied.
6250 savestr (const char *cp
)
6252 return savenstr (cp
, strlen (cp
));
6256 * Return a pointer to a space of size LEN+1 allocated with xnew where
6257 * the string CP has been copied for at most the first LEN characters.
6260 savenstr (const char *cp
, int len
)
6264 dp
= xnew (len
+ 1, char);
6265 memcpy (dp
, cp
, len
);
6271 * Return the ptr in sp at which the character c last
6272 * appears; NULL if not found
6274 * Identical to POSIX strrchr, included for portability.
6277 etags_strrchr (register const char *sp
, register int c
)
6279 register const char *r
;
6291 * Return the ptr in sp at which the character c first
6292 * appears; NULL if not found
6294 * Identical to POSIX strchr, included for portability.
6297 etags_strchr (register const char *sp
, register int c
)
6307 /* Skip spaces (end of string is not space), return new pointer. */
6309 skip_spaces (char *cp
)
6311 while (iswhite (*cp
))
6316 /* Skip non spaces, except end of string, return new pointer. */
6318 skip_non_spaces (char *cp
)
6320 while (*cp
!= '\0' && !iswhite (*cp
))
6325 /* Skip any chars in the "name" class.*/
6327 skip_name (char *cp
)
6329 /* '\0' is a notinname() so loop stops there too */
6330 while (! notinname (*cp
))
6335 /* Print error message and exit. */
6337 fatal (const char *s1
, const char *s2
)
6340 exit (EXIT_FAILURE
);
6344 pfatal (const char *s1
)
6347 exit (EXIT_FAILURE
);
6351 suggest_asking_for_help (void)
6353 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
6355 exit (EXIT_FAILURE
);
6358 /* Output a diagnostic with printf-style FORMAT and args. */
6360 error (const char *format
, ...)
6363 va_start (ap
, format
);
6364 fprintf (stderr
, "%s: ", progname
);
6365 vfprintf (stderr
, format
, ap
);
6366 fprintf (stderr
, "\n");
6370 /* Return a newly-allocated string whose contents
6371 concatenate those of s1, s2, s3. */
6373 concat (const char *s1
, const char *s2
, const char *s3
)
6375 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
6376 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
6378 strcpy (result
, s1
);
6379 strcpy (result
+ len1
, s2
);
6380 strcpy (result
+ len1
+ len2
, s3
);
6381 result
[len1
+ len2
+ len3
] = '\0';
6387 /* Does the same work as the system V getcwd, but does not need to
6388 guess the buffer size in advance. */
6393 char *path
= xnew (bufsize
, char);
6395 while (getcwd (path
, bufsize
) == NULL
)
6397 if (errno
!= ERANGE
)
6401 path
= xnew (bufsize
, char);
6404 canonicalize_filename (path
);
6408 /* Return a newly allocated string containing the file name of FILE
6409 relative to the absolute directory DIR (which should end with a slash). */
6411 relative_filename (char *file
, char *dir
)
6413 char *fp
, *dp
, *afn
, *res
;
6416 /* Find the common root of file and dir (with a trailing slash). */
6417 afn
= absolute_filename (file
, cwd
);
6420 while (*fp
++ == *dp
++)
6422 fp
--, dp
--; /* back to the first differing char */
6424 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
6427 do /* look at the equal chars until '/' */
6431 /* Build a sequence of "../" strings for the resulting relative file name. */
6433 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
6435 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
6438 strcat (res
, "../");
6440 /* Add the file name relative to the common root of file and dir. */
6441 strcat (res
, fp
+ 1);
6447 /* Return a newly allocated string containing the absolute file name
6448 of FILE given DIR (which should end with a slash). */
6450 absolute_filename (char *file
, char *dir
)
6452 char *slashp
, *cp
, *res
;
6454 if (filename_is_absolute (file
))
6455 res
= savestr (file
);
6457 /* We don't support non-absolute file names with a drive
6458 letter, like `d:NAME' (it's too much hassle). */
6459 else if (file
[1] == ':')
6460 fatal ("%s: relative file names with drive letters not supported", file
);
6463 res
= concat (dir
, file
, "");
6465 /* Delete the "/dirname/.." and "/." substrings. */
6466 slashp
= etags_strchr (res
, '/');
6467 while (slashp
!= NULL
&& slashp
[0] != '\0')
6469 if (slashp
[1] == '.')
6471 if (slashp
[2] == '.'
6472 && (slashp
[3] == '/' || slashp
[3] == '\0'))
6477 while (cp
>= res
&& !filename_is_absolute (cp
));
6479 cp
= slashp
; /* the absolute name begins with "/.." */
6481 /* Under MSDOS and NT we get `d:/NAME' as absolute
6482 file name, so the luser could say `d:/../NAME'.
6483 We silently treat this as `d:/NAME'. */
6484 else if (cp
[0] != '/')
6487 memmove (cp
, slashp
+ 3, strlen (slashp
+ 2));
6491 else if (slashp
[2] == '/' || slashp
[2] == '\0')
6493 memmove (slashp
, slashp
+ 2, strlen (slashp
+ 1));
6498 slashp
= etags_strchr (slashp
+ 1, '/');
6501 if (res
[0] == '\0') /* just a safety net: should never happen */
6504 return savestr ("/");
6510 /* Return a newly allocated string containing the absolute
6511 file name of dir where FILE resides given DIR (which should
6512 end with a slash). */
6514 absolute_dirname (char *file
, char *dir
)
6519 slashp
= etags_strrchr (file
, '/');
6521 return savestr (dir
);
6524 res
= absolute_filename (file
, dir
);
6530 /* Whether the argument string is an absolute file name. The argument
6531 string must have been canonicalized with canonicalize_filename. */
6533 filename_is_absolute (char *fn
)
6535 return (fn
[0] == '/'
6537 || (ISALPHA (fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
6542 /* Downcase DOS drive letter and collapse separators into single slashes.
6545 canonicalize_filename (register char *fn
)
6551 /* Canonicalize drive letter case. */
6552 # define ISUPPER(c) isupper (CHAR (c))
6553 if (fn
[0] != '\0' && fn
[1] == ':' && ISUPPER (fn
[0]))
6554 fn
[0] = lowcase (fn
[0]);
6559 /* Collapse multiple separators into a single slash. */
6560 for (cp
= fn
; *cp
!= '\0'; cp
++, fn
++)
6564 while (cp
[1] == sep
)
6573 /* Initialize a linebuffer for use. */
6575 linebuffer_init (linebuffer
*lbp
)
6577 lbp
->size
= (DEBUG
) ? 3 : 200;
6578 lbp
->buffer
= xnew (lbp
->size
, char);
6579 lbp
->buffer
[0] = '\0';
6583 /* Set the minimum size of a string contained in a linebuffer. */
6585 linebuffer_setlen (linebuffer
*lbp
, int toksize
)
6587 while (lbp
->size
<= toksize
)
6590 xrnew (lbp
->buffer
, lbp
->size
, char);
6595 /* Like malloc but get fatal error if memory is exhausted. */
6597 xmalloc (size_t size
)
6599 void *result
= malloc (size
);
6601 fatal ("virtual memory exhausted", (char *)NULL
);
6606 xrealloc (char *ptr
, size_t size
)
6608 void *result
= realloc (ptr
, size
);
6610 fatal ("virtual memory exhausted", (char *)NULL
);
6616 * indent-tabs-mode: t
6619 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
6620 * c-file-style: "gnu"
6624 /* etags.c ends here */