1 /* Tags file maker to go with GNU Emacs -*- coding: utf-8 -*-
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-2015 Free Software
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ì.
71 * Francesco Potortì maintained and improved it for many years
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";
88 # define NDEBUG /* disable assert */
94 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
97 /* WIN32_NATIVE is for XEmacs.
98 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
103 #endif /* WIN32_NATIVE */
108 # include <sys/param.h>
115 # define MAXPATHLEN _MAX_PATH
119 #endif /* WINDOWSNT */
125 #include <sysstdio.h>
128 #include <sys/types.h>
129 #include <sys/stat.h>
130 #include <binary-io.h>
131 #include <c-strcase.h>
135 # undef assert /* some systems have a buggy assert.h */
136 # define assert(x) ((void) 0)
142 /* Define CTAGS to make the program "ctags" compatible with the usual one.
143 Leave it undefined to make the program "etags", which makes emacs-style
144 tag tables and tags typedefs, #defines and struct/union/enum by default. */
152 #define streq(s,t) (assert ((s)!=NULL || (t)!=NULL), !strcmp (s, t))
153 #define strcaseeq(s,t) (assert ((s)!=NULL && (t)!=NULL), !c_strcasecmp (s, t))
154 #define strneq(s,t,n) (assert ((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
155 #define strncaseeq(s,t,n) (assert ((s)!=NULL && (t)!=NULL), !c_strncasecmp (s, t, n))
157 #define CHARS 256 /* 2^sizeof(char) */
158 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
159 #define iswhite(c) (_wht[CHAR (c)]) /* c is white (see white) */
160 #define notinname(c) (_nin[CHAR (c)]) /* c is not in a name (see nonam) */
161 #define begtoken(c) (_btk[CHAR (c)]) /* c can start token (see begtk) */
162 #define intoken(c) (_itk[CHAR (c)]) /* c can be in token (see midtk) */
163 #define endtoken(c) (_etk[CHAR (c)]) /* c ends tokens (see endtk) */
165 #define ISALNUM(c) isalnum (CHAR (c))
166 #define ISALPHA(c) isalpha (CHAR (c))
167 #define ISDIGIT(c) isdigit (CHAR (c))
168 #define ISLOWER(c) islower (CHAR (c))
170 #define lowcase(c) tolower (CHAR (c))
174 * xnew, xrnew -- allocate, reallocate storage
176 * SYNOPSIS: Type *xnew (int n, Type);
177 * void xrnew (OldPointer, int n, Type);
179 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
180 #define xrnew(op, n, Type) ((op) = (Type *) xrealloc (op, (n) * sizeof (Type)))
182 typedef void Lang_function (FILE *);
186 const char *suffix
; /* file name suffix for this compressor */
187 const char *command
; /* takes one arg and decompresses to stdout */
192 const char *name
; /* language name */
193 const char *help
; /* detailed help for the language */
194 Lang_function
*function
; /* parse function */
195 const char **suffixes
; /* name suffixes of this language's files */
196 const char **filenames
; /* names of this language's files */
197 const char **interpreters
; /* interpreters for this language */
198 bool metasource
; /* source used to generate other sources */
203 struct fdesc
*next
; /* for the linked list */
204 char *infname
; /* uncompressed input file name */
205 char *infabsname
; /* absolute uncompressed input file name */
206 char *infabsdir
; /* absolute dir of input file */
207 char *taggedfname
; /* file name to write in tagfile */
208 language
*lang
; /* language of file */
209 char *prop
; /* file properties to write in tagfile */
210 bool usecharno
; /* etags tags shall contain char number */
211 bool written
; /* entry written in the tags file */
214 typedef struct node_st
215 { /* sorting structure */
216 struct node_st
*left
, *right
; /* left and right sons */
217 fdesc
*fdp
; /* description of file to whom tag belongs */
218 char *name
; /* tag name */
219 char *regex
; /* search regexp */
220 bool valid
; /* write this tag on the tag file */
221 bool is_func
; /* function tag: use regexp in CTAGS mode */
222 bool been_warned
; /* warning already given for duplicated tag */
223 int lno
; /* line number tag is on */
224 long cno
; /* character number line starts on */
228 * A `linebuffer' is a structure which holds a line of text.
229 * `readline_internal' reads a line from a stream into a linebuffer
230 * and works regardless of the length of the line.
231 * SIZE is the size of BUFFER, LEN is the length of the string in
232 * BUFFER after readline reads it.
241 /* Used to support mixing of --lang and file names. */
245 at_language
, /* a language specification */
246 at_regexp
, /* a regular expression */
247 at_filename
, /* a file name */
248 at_stdin
, /* read from stdin here */
249 at_end
/* stop parsing the list */
250 } arg_type
; /* argument type */
251 language
*lang
; /* language associated with the argument */
252 char *what
; /* the argument itself */
255 /* Structure defining a regular expression. */
256 typedef struct regexp
258 struct regexp
*p_next
; /* pointer to next in list */
259 language
*lang
; /* if set, use only for this language */
260 char *pattern
; /* the regexp pattern */
261 char *name
; /* tag name */
262 struct re_pattern_buffer
*pat
; /* the compiled pattern */
263 struct re_registers regs
; /* re registers */
264 bool error_signaled
; /* already signaled for this regexp */
265 bool force_explicit_name
; /* do not allow implicit tag name */
266 bool ignore_case
; /* ignore case when matching */
267 bool multi_line
; /* do a multi-line match on the whole file */
271 /* Many compilers barf on this:
272 Lang_function Ada_funcs;
273 so let's write it this way */
274 static void Ada_funcs (FILE *);
275 static void Asm_labels (FILE *);
276 static void C_entries (int c_ext
, FILE *);
277 static void default_C_entries (FILE *);
278 static void plain_C_entries (FILE *);
279 static void Cjava_entries (FILE *);
280 static void Cobol_paragraphs (FILE *);
281 static void Cplusplus_entries (FILE *);
282 static void Cstar_entries (FILE *);
283 static void Erlang_functions (FILE *);
284 static void Forth_words (FILE *);
285 static void Fortran_functions (FILE *);
286 static void HTML_labels (FILE *);
287 static void Lisp_functions (FILE *);
288 static void Lua_functions (FILE *);
289 static void Makefile_targets (FILE *);
290 static void Pascal_functions (FILE *);
291 static void Perl_functions (FILE *);
292 static void PHP_functions (FILE *);
293 static void PS_functions (FILE *);
294 static void Prolog_functions (FILE *);
295 static void Python_functions (FILE *);
296 static void Scheme_functions (FILE *);
297 static void TeX_commands (FILE *);
298 static void Texinfo_nodes (FILE *);
299 static void Yacc_entries (FILE *);
300 static void just_read_file (FILE *);
302 static language
*get_language_from_langname (const char *);
303 static void readline (linebuffer
*, FILE *);
304 static long readline_internal (linebuffer
*, FILE *);
305 static bool nocase_tail (const char *);
306 static void get_tag (char *, char **);
308 static void analyze_regex (char *);
309 static void free_regexps (void);
310 static void regex_tag_multiline (void);
311 static void error (const char *, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
312 static _Noreturn
void suggest_asking_for_help (void);
313 _Noreturn
void fatal (const char *, const char *);
314 static _Noreturn
void pfatal (const char *);
315 static void add_node (node
*, node
**);
317 static void init (void);
318 static void process_file_name (char *, language
*);
319 static void process_file (FILE *, char *, language
*);
320 static void find_entries (FILE *);
321 static void free_tree (node
*);
322 static void free_fdesc (fdesc
*);
323 static void pfnote (char *, bool, char *, int, int, long);
324 static void invalidate_nodes (fdesc
*, node
**);
325 static void put_entries (node
*);
327 static char *concat (const char *, const char *, const char *);
328 static char *skip_spaces (char *);
329 static char *skip_non_spaces (char *);
330 static char *skip_name (char *);
331 static char *savenstr (const char *, int);
332 static char *savestr (const char *);
333 static char *etags_getcwd (void);
334 static char *relative_filename (char *, char *);
335 static char *absolute_filename (char *, char *);
336 static char *absolute_dirname (char *, char *);
337 static bool filename_is_absolute (char *f
);
338 static void canonicalize_filename (char *);
339 static void linebuffer_init (linebuffer
*);
340 static void linebuffer_setlen (linebuffer
*, int);
341 static void *xmalloc (size_t);
342 static void *xrealloc (void *, size_t);
345 static char searchar
= '/'; /* use /.../ searches */
347 static char *tagfile
; /* output file */
348 static char *progname
; /* name this program was invoked with */
349 static char *cwd
; /* current working directory */
350 static char *tagfiledir
; /* directory of tagfile */
351 static FILE *tagf
; /* ioptr for tags file */
352 static ptrdiff_t whatlen_max
; /* maximum length of any 'what' member */
354 static fdesc
*fdhead
; /* head of file description list */
355 static fdesc
*curfdp
; /* current file description */
356 static int lineno
; /* line number of current line */
357 static long charno
; /* current character number */
358 static long linecharno
; /* charno of start of current line */
359 static char *dbp
; /* pointer to start of current tag */
361 static const int invalidcharno
= -1;
363 static node
*nodehead
; /* the head of the binary tree of tags */
364 static node
*last_node
; /* the last node created */
366 static linebuffer lb
; /* the current line */
367 static linebuffer filebuf
; /* a buffer containing the whole file */
368 static linebuffer token_name
; /* a buffer containing a tag name */
370 /* boolean "functions" (see init) */
371 static bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
374 *white
= " \f\t\n\r\v",
376 *nonam
= " \f\t\n\r()=,;", /* look at make_tag before modifying! */
377 /* token ending chars */
378 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
379 /* token starting chars */
380 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
381 /* valid in-token chars */
382 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
384 static bool append_to_tagfile
; /* -a: append to tags */
385 /* The next five default to true in C and derived languages. */
386 static bool typedefs
; /* -t: create tags for C and Ada typedefs */
387 static bool typedefs_or_cplusplus
; /* -T: create tags for C typedefs, level */
388 /* 0 struct/enum/union decls, and C++ */
389 /* member functions. */
390 static bool constantypedefs
; /* -d: create tags for C #define, enum */
391 /* constants and variables. */
392 /* -D: opposite of -d. Default under ctags. */
393 static int globals
; /* create tags for global variables */
394 static int members
; /* create tags for C member variables */
395 static int declarations
; /* --declarations: tag them and extern in C&Co*/
396 static int no_line_directive
; /* ignore #line directives (undocumented) */
397 static int no_duplicates
; /* no duplicate tags for ctags (undocumented) */
398 static bool update
; /* -u: update tags */
399 static bool vgrind_style
; /* -v: create vgrind style index output */
400 static bool no_warnings
; /* -w: suppress warnings (undocumented) */
401 static bool cxref_style
; /* -x: create cxref style output */
402 static bool cplusplus
; /* .[hc] means C++, not C (undocumented) */
403 static bool ignoreindent
; /* -I: ignore indentation in C */
404 static int packages_only
; /* --packages-only: in Ada, only tag packages*/
406 /* STDIN is defined in LynxOS system headers */
411 #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
412 static bool parsing_stdin
; /* --parse-stdin used */
414 static regexp
*p_head
; /* list of all regexps */
415 static bool need_filebuf
; /* some regexes are multi-line */
417 static struct option longopts
[] =
419 { "append", no_argument
, NULL
, 'a' },
420 { "packages-only", no_argument
, &packages_only
, 1 },
421 { "c++", no_argument
, NULL
, 'C' },
422 { "declarations", no_argument
, &declarations
, 1 },
423 { "no-line-directive", no_argument
, &no_line_directive
, 1 },
424 { "no-duplicates", no_argument
, &no_duplicates
, 1 },
425 { "help", no_argument
, NULL
, 'h' },
426 { "help", no_argument
, NULL
, 'H' },
427 { "ignore-indentation", no_argument
, NULL
, 'I' },
428 { "language", required_argument
, NULL
, 'l' },
429 { "members", no_argument
, &members
, 1 },
430 { "no-members", no_argument
, &members
, 0 },
431 { "output", required_argument
, NULL
, 'o' },
432 { "regex", required_argument
, NULL
, 'r' },
433 { "no-regex", no_argument
, NULL
, 'R' },
434 { "ignore-case-regex", required_argument
, NULL
, 'c' },
435 { "parse-stdin", required_argument
, NULL
, STDIN
},
436 { "version", no_argument
, NULL
, 'V' },
438 #if CTAGS /* Ctags options */
439 { "backward-search", no_argument
, NULL
, 'B' },
440 { "cxref", no_argument
, NULL
, 'x' },
441 { "defines", no_argument
, NULL
, 'd' },
442 { "globals", no_argument
, &globals
, 1 },
443 { "typedefs", no_argument
, NULL
, 't' },
444 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
445 { "update", no_argument
, NULL
, 'u' },
446 { "vgrind", no_argument
, NULL
, 'v' },
447 { "no-warn", no_argument
, NULL
, 'w' },
449 #else /* Etags options */
450 { "no-defines", no_argument
, NULL
, 'D' },
451 { "no-globals", no_argument
, &globals
, 0 },
452 { "include", required_argument
, NULL
, 'i' },
457 static compressor compressors
[] =
459 { "z", "gzip -d -c"},
460 { "Z", "gzip -d -c"},
461 { "gz", "gzip -d -c"},
462 { "GZ", "gzip -d -c"},
463 { "bz2", "bzip2 -d -c" },
464 { "xz", "xz -d -c" },
473 static const char *Ada_suffixes
[] =
474 { "ads", "adb", "ada", NULL
};
475 static const char Ada_help
[] =
476 "In Ada code, functions, procedures, packages, tasks and types are\n\
477 tags. Use the `--packages-only' option to create tags for\n\
479 Ada tag names have suffixes indicating the type of entity:\n\
480 Entity type: Qualifier:\n\
481 ------------ ----------\n\
488 Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
489 body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
490 will just search for any tag `bidule'.";
493 static const char *Asm_suffixes
[] =
494 { "a", /* Unix assembler */
495 "asm", /* Microcontroller assembly */
496 "def", /* BSO/Tasking definition includes */
497 "inc", /* Microcontroller include files */
498 "ins", /* Microcontroller include files */
499 "s", "sa", /* Unix assembler */
500 "S", /* cpp-processed Unix assembler */
501 "src", /* BSO/Tasking C compiler output */
504 static const char Asm_help
[] =
505 "In assembler code, labels appearing at the beginning of a line,\n\
506 followed by a colon, are tags.";
509 /* Note that .c and .h can be considered C++, if the --c++ flag was
510 given, or if the `class' or `template' keywords are met inside the file.
511 That is why default_C_entries is called for these. */
512 static const char *default_C_suffixes
[] =
514 #if CTAGS /* C help for Ctags */
515 static const char default_C_help
[] =
516 "In C code, any C function is a tag. Use -t to tag typedefs.\n\
517 Use -T to tag definitions of `struct', `union' and `enum'.\n\
518 Use -d to tag `#define' macro definitions and `enum' constants.\n\
519 Use --globals to tag global variables.\n\
520 You can tag function declarations and external variables by\n\
521 using `--declarations', and struct members by using `--members'.";
522 #else /* C help for Etags */
523 static const char default_C_help
[] =
524 "In C code, any C function or typedef is a tag, and so are\n\
525 definitions of `struct', `union' and `enum'. `#define' macro\n\
526 definitions and `enum' constants are tags unless you specify\n\
527 `--no-defines'. Global variables are tags unless you specify\n\
528 `--no-globals' and so are struct members unless you specify\n\
529 `--no-members'. Use of `--no-globals', `--no-defines' and\n\
530 `--no-members' can make the tags table file much smaller.\n\
531 You can tag function declarations and external variables by\n\
532 using `--declarations'.";
533 #endif /* C help for Ctags and Etags */
535 static const char *Cplusplus_suffixes
[] =
536 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
537 "M", /* Objective C++ */
538 "pdb", /* PostScript with C syntax */
540 static const char Cplusplus_help
[] =
541 "In C++ code, all the tag constructs of C code are tagged. (Use\n\
542 --help --lang=c --lang=c++ for full help.)\n\
543 In addition to C tags, member functions are also recognized. Member\n\
544 variables are recognized unless you use the `--no-members' option.\n\
545 Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
546 and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
549 static const char *Cjava_suffixes
[] =
551 static char Cjava_help
[] =
552 "In Java code, all the tags constructs of C and C++ code are\n\
553 tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
556 static const char *Cobol_suffixes
[] =
557 { "COB", "cob", NULL
};
558 static char Cobol_help
[] =
559 "In Cobol code, tags are paragraph names; that is, any word\n\
560 starting in column 8 and followed by a period.";
562 static const char *Cstar_suffixes
[] =
563 { "cs", "hs", NULL
};
565 static const char *Erlang_suffixes
[] =
566 { "erl", "hrl", NULL
};
567 static const char Erlang_help
[] =
568 "In Erlang code, the tags are the functions, records and macros\n\
569 defined in the file.";
571 const char *Forth_suffixes
[] =
572 { "fth", "tok", NULL
};
573 static const char Forth_help
[] =
574 "In Forth code, tags are words defined by `:',\n\
575 constant, code, create, defer, value, variable, buffer:, field.";
577 static const char *Fortran_suffixes
[] =
578 { "F", "f", "f90", "for", NULL
};
579 static const char Fortran_help
[] =
580 "In Fortran code, functions, subroutines and block data are tags.";
582 static const char *HTML_suffixes
[] =
583 { "htm", "html", "shtml", NULL
};
584 static const char HTML_help
[] =
585 "In HTML input files, the tags are the `title' and the `h1', `h2',\n\
586 `h3' headers. Also, tags are `name=' in anchors and all\n\
587 occurrences of `id='.";
589 static const char *Lisp_suffixes
[] =
590 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
591 static const char Lisp_help
[] =
592 "In Lisp code, any function defined with `defun', any variable\n\
593 defined with `defvar' or `defconst', and in general the first\n\
594 argument of any expression that starts with `(def' in column zero\n\
596 The `--declarations' option tags \"(defvar foo)\" constructs too.";
598 static const char *Lua_suffixes
[] =
599 { "lua", "LUA", NULL
};
600 static const char Lua_help
[] =
601 "In Lua scripts, all functions are tags.";
603 static const char *Makefile_filenames
[] =
604 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
605 static const char Makefile_help
[] =
606 "In makefiles, targets are tags; additionally, variables are tags\n\
607 unless you specify `--no-globals'.";
609 static const char *Objc_suffixes
[] =
610 { "lm", /* Objective lex file */
611 "m", /* Objective C file */
613 static const char Objc_help
[] =
614 "In Objective C code, tags include Objective C definitions for classes,\n\
615 class categories, methods and protocols. Tags for variables and\n\
616 functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.\n\
617 (Use --help --lang=c --lang=objc --lang=java for full help.)";
619 static const char *Pascal_suffixes
[] =
620 { "p", "pas", NULL
};
621 static const char Pascal_help
[] =
622 "In Pascal code, the tags are the functions and procedures defined\n\
624 /* " // this is for working around an Emacs highlighting bug... */
626 static const char *Perl_suffixes
[] =
627 { "pl", "pm", NULL
};
628 static const char *Perl_interpreters
[] =
629 { "perl", "@PERL@", NULL
};
630 static const char Perl_help
[] =
631 "In Perl code, the tags are the packages, subroutines and variables\n\
632 defined by the `package', `sub', `my' and `local' keywords. Use\n\
633 `--globals' if you want to tag global variables. Tags for\n\
634 subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
635 defined in the default package is `main::SUB'.";
637 static const char *PHP_suffixes
[] =
638 { "php", "php3", "php4", NULL
};
639 static const char PHP_help
[] =
640 "In PHP code, tags are functions, classes and defines. Unless you use\n\
641 the `--no-members' option, vars are tags too.";
643 static const char *plain_C_suffixes
[] =
644 { "pc", /* Pro*C file */
647 static const char *PS_suffixes
[] =
648 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
649 static const char PS_help
[] =
650 "In PostScript code, the tags are the functions.";
652 static const char *Prolog_suffixes
[] =
654 static const char Prolog_help
[] =
655 "In Prolog code, tags are predicates and rules at the beginning of\n\
658 static const char *Python_suffixes
[] =
660 static const char Python_help
[] =
661 "In Python code, `def' or `class' at the beginning of a line\n\
664 /* Can't do the `SCM' or `scm' prefix with a version number. */
665 static const char *Scheme_suffixes
[] =
666 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
667 static const char Scheme_help
[] =
668 "In Scheme code, tags include anything defined with `def' or with a\n\
669 construct whose name starts with `def'. They also include\n\
670 variables set with `set!' at top level in the file.";
672 static const char *TeX_suffixes
[] =
673 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
674 static const char TeX_help
[] =
675 "In LaTeX text, the argument of any of the commands `\\chapter',\n\
676 `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
677 `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
678 `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
679 `\\newenvironment' or `\\renewenvironment' is a tag.\n\
681 Other commands can be specified by setting the environment variable\n\
682 `TEXTAGS' to a colon-separated list like, for example,\n\
683 TEXTAGS=\"mycommand:myothercommand\".";
686 static const char *Texinfo_suffixes
[] =
687 { "texi", "texinfo", "txi", NULL
};
688 static const char Texinfo_help
[] =
689 "for texinfo files, lines starting with @node are tagged.";
691 static const char *Yacc_suffixes
[] =
692 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
693 static const char Yacc_help
[] =
694 "In Bison or Yacc input files, each rule defines as a tag the\n\
695 nonterminal it constructs. The portions of the file that contain\n\
696 C code are parsed as C code (use --help --lang=c --lang=yacc\n\
699 static const char auto_help
[] =
700 "`auto' is not a real language, it indicates to use\n\
701 a default language for files base on file name suffix and file contents.";
703 static const char none_help
[] =
704 "`none' is not a real language, it indicates to only do\n\
705 regexp processing on files.";
707 static const char no_lang_help
[] =
708 "No detailed help available for this language.";
712 * Table of languages.
714 * It is ok for a given function to be listed under more than one
715 * name. I just didn't.
718 static language lang_names
[] =
720 { "ada", Ada_help
, Ada_funcs
, Ada_suffixes
},
721 { "asm", Asm_help
, Asm_labels
, Asm_suffixes
},
722 { "c", default_C_help
, default_C_entries
, default_C_suffixes
},
723 { "c++", Cplusplus_help
, Cplusplus_entries
, Cplusplus_suffixes
},
724 { "c*", no_lang_help
, Cstar_entries
, Cstar_suffixes
},
725 { "cobol", Cobol_help
, Cobol_paragraphs
, Cobol_suffixes
},
726 { "erlang", Erlang_help
, Erlang_functions
, Erlang_suffixes
},
727 { "forth", Forth_help
, Forth_words
, Forth_suffixes
},
728 { "fortran", Fortran_help
, Fortran_functions
, Fortran_suffixes
},
729 { "html", HTML_help
, HTML_labels
, HTML_suffixes
},
730 { "java", Cjava_help
, Cjava_entries
, Cjava_suffixes
},
731 { "lisp", Lisp_help
, Lisp_functions
, Lisp_suffixes
},
732 { "lua", Lua_help
, Lua_functions
, Lua_suffixes
},
733 { "makefile", Makefile_help
,Makefile_targets
,NULL
,Makefile_filenames
},
734 { "objc", Objc_help
, plain_C_entries
, Objc_suffixes
},
735 { "pascal", Pascal_help
, Pascal_functions
, Pascal_suffixes
},
736 { "perl",Perl_help
,Perl_functions
,Perl_suffixes
,NULL
,Perl_interpreters
},
737 { "php", PHP_help
, PHP_functions
, PHP_suffixes
},
738 { "postscript",PS_help
, PS_functions
, PS_suffixes
},
739 { "proc", no_lang_help
, plain_C_entries
, plain_C_suffixes
},
740 { "prolog", Prolog_help
, Prolog_functions
, Prolog_suffixes
},
741 { "python", Python_help
, Python_functions
, Python_suffixes
},
742 { "scheme", Scheme_help
, Scheme_functions
, Scheme_suffixes
},
743 { "tex", TeX_help
, TeX_commands
, TeX_suffixes
},
744 { "texinfo", Texinfo_help
, Texinfo_nodes
, Texinfo_suffixes
},
745 { "yacc", Yacc_help
,Yacc_entries
,Yacc_suffixes
,NULL
,NULL
,true},
746 { "auto", auto_help
}, /* default guessing scheme */
747 { "none", none_help
, just_read_file
}, /* regexp matching only */
748 { NULL
} /* end of list */
753 print_language_names (void)
756 const char **name
, **ext
;
758 puts ("\nThese are the currently supported languages, along with the\n\
759 default file names and dot suffixes:");
760 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
762 printf (" %-*s", 10, lang
->name
);
763 if (lang
->filenames
!= NULL
)
764 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
765 printf (" %s", *name
);
766 if (lang
->suffixes
!= NULL
)
767 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
768 printf (" .%s", *ext
);
771 puts ("where `auto' means use default language for files based on file\n\
772 name suffix, and `none' means only do regexp processing on files.\n\
773 If no language is specified and no matching suffix is found,\n\
774 the first line of the file is read for a sharp-bang (#!) sequence\n\
775 followed by the name of an interpreter. If no such sequence is found,\n\
776 Fortran is tried first; if no tags are found, C is tried next.\n\
777 When parsing any C file, a \"class\" or \"template\" keyword\n\
779 puts ("Compressed files are supported using gzip, bzip2, and xz.\n\
781 For detailed help on a given language use, for example,\n\
782 etags --help --lang=ada.");
786 # define EMACS_NAME "standalone"
789 # define VERSION "17.38.1.4"
791 static _Noreturn
void
794 char emacs_copyright
[] = COPYRIGHT
;
796 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
797 puts (emacs_copyright
);
798 puts ("This program is distributed under the terms in ETAGS.README");
803 #ifndef PRINT_UNDOCUMENTED_OPTIONS_HELP
804 # define PRINT_UNDOCUMENTED_OPTIONS_HELP false
807 static _Noreturn
void
808 print_help (argument
*argbuffer
)
810 bool help_for_lang
= false;
812 for (; argbuffer
->arg_type
!= at_end
; argbuffer
++)
813 if (argbuffer
->arg_type
== at_language
)
817 puts (argbuffer
->lang
->help
);
818 help_for_lang
= true;
824 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
826 These are the options accepted by %s.\n", progname
, progname
);
827 puts ("You may use unambiguous abbreviations for the long option names.");
828 puts (" A - as file name means read names from stdin (one per line).\n\
829 Absolute names are stored in the output file as they are.\n\
830 Relative ones are stored relative to the output file's directory.\n");
832 puts ("-a, --append\n\
833 Append tag entries to existing tags file.");
835 puts ("--packages-only\n\
836 For Ada files, only generate tags for packages.");
839 puts ("-B, --backward-search\n\
840 Write the search commands for the tag entries using '?', the\n\
841 backward-search command instead of '/', the forward-search command.");
843 /* This option is mostly obsolete, because etags can now automatically
844 detect C++. Retained for backward compatibility and for debugging and
845 experimentation. In principle, we could want to tag as C++ even
846 before any "class" or "template" keyword.
848 Treat files whose name suffix defaults to C language as C++ files.");
851 puts ("--declarations\n\
852 In C and derived languages, create tags for function declarations,");
854 puts ("\tand create tags for extern variables if --globals is used.");
857 ("\tand create tags for extern variables unless --no-globals is used.");
860 puts ("-d, --defines\n\
861 Create tag entries for C #define constants and enum constants, too.");
863 puts ("-D, --no-defines\n\
864 Don't create tag entries for C #define constants and enum constants.\n\
865 This makes the tags file smaller.");
868 puts ("-i FILE, --include=FILE\n\
869 Include a note in tag file indicating that, when searching for\n\
870 a tag, one should also consult the tags file FILE after\n\
871 checking the current file.");
873 puts ("-l LANG, --language=LANG\n\
874 Force the following files to be considered as written in the\n\
875 named language up to the next --language=LANG option.");
879 Create tag entries for global variables in some languages.");
881 puts ("--no-globals\n\
882 Do not create tag entries for global variables in some\n\
883 languages. This makes the tags file smaller.");
885 if (PRINT_UNDOCUMENTED_OPTIONS_HELP
)
886 puts ("--no-line-directive\n\
887 Ignore #line preprocessor directives in C and derived languages.");
891 Create tag entries for members of structures in some languages.");
893 puts ("--no-members\n\
894 Do not create tag entries for members of structures\n\
895 in some languages.");
897 puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
898 Make a tag for each line matching a regular expression pattern\n\
899 in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
900 files only. REGEXFILE is a file containing one REGEXP per line.\n\
901 REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
902 optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
903 puts (" If TAGNAME/ is present, the tags created are named.\n\
904 For example Tcl named tags can be created with:\n\
905 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
906 MODS are optional one-letter modifiers: `i' means to ignore case,\n\
907 `m' means to allow multi-line matches, `s' implies `m' and\n\
908 causes dot to match any character, including newline.");
910 puts ("-R, --no-regex\n\
911 Don't create tags from regexps for the following files.");
913 puts ("-I, --ignore-indentation\n\
914 In C and C++ do not assume that a closing brace in the first\n\
915 column is the final brace of a function or structure definition.");
917 puts ("-o FILE, --output=FILE\n\
918 Write the tags to FILE.");
920 puts ("--parse-stdin=NAME\n\
921 Read from standard input and record tags as belonging to file NAME.");
925 puts ("-t, --typedefs\n\
926 Generate tag entries for C and Ada typedefs.");
927 puts ("-T, --typedefs-and-c++\n\
928 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
929 and C++ member functions.");
933 puts ("-u, --update\n\
934 Update the tag entries for the given files, leaving tag\n\
935 entries for other files in place. Currently, this is\n\
936 implemented by deleting the existing entries for the given\n\
937 files and then rewriting the new entries at the end of the\n\
938 tags file. It is often faster to simply rebuild the entire\n\
939 tag file than to use this.");
943 puts ("-v, --vgrind\n\
944 Print on the standard output an index of items intended for\n\
945 human consumption, similar to the output of vgrind. The index\n\
946 is sorted, and gives the page number of each item.");
948 if (PRINT_UNDOCUMENTED_OPTIONS_HELP
)
949 puts ("-w, --no-duplicates\n\
950 Do not create duplicate tag entries, for compatibility with\n\
951 traditional ctags.");
953 if (PRINT_UNDOCUMENTED_OPTIONS_HELP
)
954 puts ("-w, --no-warn\n\
955 Suppress warning messages about duplicate tag entries.");
957 puts ("-x, --cxref\n\
958 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
959 The output uses line numbers instead of page numbers, but\n\
960 beyond that the differences are cosmetic; try both to see\n\
964 puts ("-V, --version\n\
965 Print the version of the program.\n\
967 Print this help message.\n\
968 Followed by one or more `--language' options prints detailed\n\
969 help about tag generation for the specified languages.");
971 print_language_names ();
974 puts ("Report bugs to bug-gnu-emacs@gnu.org");
981 main (int argc
, char **argv
)
984 unsigned int nincluded_files
;
985 char **included_files
;
987 int current_arg
, file_count
;
988 linebuffer filename_lb
;
989 bool help_asked
= false;
996 included_files
= xnew (argc
, char *);
1000 /* Allocate enough no matter what happens. Overkill, but each one
1002 argbuffer
= xnew (argc
, argument
);
1005 * Always find typedefs and structure tags.
1006 * Also default to find macro constants, enum constants, struct
1007 * members and global variables. Do it for both etags and ctags.
1009 typedefs
= typedefs_or_cplusplus
= constantypedefs
= true;
1010 globals
= members
= true;
1012 /* When the optstring begins with a '-' getopt_long does not rearrange the
1013 non-options arguments to be at the end, but leaves them alone. */
1014 optstring
= concat ("-ac:Cf:Il:o:r:RSVhH",
1015 (CTAGS
) ? "BxdtTuvw" : "Di:",
1018 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, NULL
)) != EOF
)
1022 /* If getopt returns 0, then it has already processed a
1023 long-named option. We should do nothing. */
1027 /* This means that a file name has been seen. Record it. */
1028 argbuffer
[current_arg
].arg_type
= at_filename
;
1029 argbuffer
[current_arg
].what
= optarg
;
1030 len
= strlen (optarg
);
1031 if (whatlen_max
< len
)
1038 /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
1039 argbuffer
[current_arg
].arg_type
= at_stdin
;
1040 argbuffer
[current_arg
].what
= optarg
;
1041 len
= strlen (optarg
);
1042 if (whatlen_max
< len
)
1047 fatal ("cannot parse standard input more than once", (char *)NULL
);
1048 parsing_stdin
= true;
1051 /* Common options. */
1052 case 'a': append_to_tagfile
= true; break;
1053 case 'C': cplusplus
= true; break;
1054 case 'f': /* for compatibility with old makefiles */
1058 error ("-o option may only be given once.");
1059 suggest_asking_for_help ();
1065 case 'S': /* for backward compatibility */
1066 ignoreindent
= true;
1070 language
*lang
= get_language_from_langname (optarg
);
1073 argbuffer
[current_arg
].lang
= lang
;
1074 argbuffer
[current_arg
].arg_type
= at_language
;
1080 /* Backward compatibility: support obsolete --ignore-case-regexp. */
1081 optarg
= concat (optarg
, "i", ""); /* memory leak here */
1084 argbuffer
[current_arg
].arg_type
= at_regexp
;
1085 argbuffer
[current_arg
].what
= optarg
;
1086 len
= strlen (optarg
);
1087 if (whatlen_max
< len
)
1092 argbuffer
[current_arg
].arg_type
= at_regexp
;
1093 argbuffer
[current_arg
].what
= NULL
;
1105 case 'D': constantypedefs
= false; break;
1106 case 'i': included_files
[nincluded_files
++] = optarg
; break;
1108 /* Ctags options. */
1109 case 'B': searchar
= '?'; break;
1110 case 'd': constantypedefs
= true; break;
1111 case 't': typedefs
= true; break;
1112 case 'T': typedefs
= typedefs_or_cplusplus
= true; break;
1113 case 'u': update
= true; break;
1114 case 'v': vgrind_style
= true; /*FALLTHRU*/
1115 case 'x': cxref_style
= true; break;
1116 case 'w': no_warnings
= true; break;
1118 suggest_asking_for_help ();
1122 /* No more options. Store the rest of arguments. */
1123 for (; optind
< argc
; optind
++)
1125 argbuffer
[current_arg
].arg_type
= at_filename
;
1126 argbuffer
[current_arg
].what
= argv
[optind
];
1127 len
= strlen (argv
[optind
]);
1128 if (whatlen_max
< len
)
1134 argbuffer
[current_arg
].arg_type
= at_end
;
1137 print_help (argbuffer
);
1140 if (nincluded_files
== 0 && file_count
== 0)
1142 error ("no input files specified.");
1143 suggest_asking_for_help ();
1147 if (tagfile
== NULL
)
1148 tagfile
= savestr (CTAGS
? "tags" : "TAGS");
1149 cwd
= etags_getcwd (); /* the current working directory */
1150 if (cwd
[strlen (cwd
) - 1] != '/')
1153 cwd
= concat (oldcwd
, "/", "");
1157 /* Compute base directory for relative file names. */
1158 if (streq (tagfile
, "-")
1159 || strneq (tagfile
, "/dev/", 5))
1160 tagfiledir
= cwd
; /* relative file names are relative to cwd */
1163 canonicalize_filename (tagfile
);
1164 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1167 init (); /* set up boolean "functions" */
1169 linebuffer_init (&lb
);
1170 linebuffer_init (&filename_lb
);
1171 linebuffer_init (&filebuf
);
1172 linebuffer_init (&token_name
);
1176 if (streq (tagfile
, "-"))
1179 SET_BINARY (fileno (stdout
));
1182 tagf
= fopen (tagfile
, append_to_tagfile
? "ab" : "wb");
1188 * Loop through files finding functions.
1190 for (i
= 0; i
< current_arg
; i
++)
1192 static language
*lang
; /* non-NULL if language is forced */
1195 switch (argbuffer
[i
].arg_type
)
1198 lang
= argbuffer
[i
].lang
;
1201 analyze_regex (argbuffer
[i
].what
);
1204 this_file
= argbuffer
[i
].what
;
1205 /* Input file named "-" means read file names from stdin
1206 (one per line) and use them. */
1207 if (streq (this_file
, "-"))
1210 fatal ("cannot parse standard input AND read file names from it",
1212 while (readline_internal (&filename_lb
, stdin
) > 0)
1213 process_file_name (filename_lb
.buffer
, lang
);
1216 process_file_name (this_file
, lang
);
1219 this_file
= argbuffer
[i
].what
;
1220 process_file (stdin
, this_file
, lang
);
1227 free (filebuf
.buffer
);
1228 free (token_name
.buffer
);
1230 if (!CTAGS
|| cxref_style
)
1232 /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */
1233 put_entries (nodehead
);
1234 free_tree (nodehead
);
1240 /* Output file entries that have no tags. */
1241 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1243 fprintf (tagf
, "\f\n%s,0\n", fdp
->taggedfname
);
1245 while (nincluded_files
-- > 0)
1246 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1248 if (fclose (tagf
) == EOF
)
1252 exit (EXIT_SUCCESS
);
1255 /* From here on, we are in (CTAGS && !cxref_style) */
1259 xmalloc (strlen (tagfile
) + whatlen_max
+
1260 sizeof "mv..OTAGS;fgrep -v '\t\t' OTAGS >;rm OTAGS");
1261 for (i
= 0; i
< current_arg
; ++i
)
1263 switch (argbuffer
[i
].arg_type
)
1269 continue; /* the for loop */
1271 char *z
= stpcpy (cmd
, "mv ");
1272 z
= stpcpy (z
, tagfile
);
1273 z
= stpcpy (z
, " OTAGS;fgrep -v '\t");
1274 z
= stpcpy (z
, argbuffer
[i
].what
);
1275 z
= stpcpy (z
, "\t' OTAGS >");
1276 z
= stpcpy (z
, tagfile
);
1277 strcpy (z
, ";rm OTAGS");
1278 if (system (cmd
) != EXIT_SUCCESS
)
1279 fatal ("failed to execute shell command", (char *)NULL
);
1282 append_to_tagfile
= true;
1285 tagf
= fopen (tagfile
, append_to_tagfile
? "ab" : "wb");
1288 put_entries (nodehead
); /* write all the tags (CTAGS) */
1289 free_tree (nodehead
);
1291 if (fclose (tagf
) == EOF
)
1295 if (append_to_tagfile
|| update
)
1297 char *cmd
= xmalloc (2 * strlen (tagfile
) + sizeof "sort -u -o..");
1298 /* Maybe these should be used:
1299 setenv ("LC_COLLATE", "C", 1);
1300 setenv ("LC_ALL", "C", 1); */
1301 char *z
= stpcpy (cmd
, "sort -u -o ");
1302 z
= stpcpy (z
, tagfile
);
1304 strcpy (z
, tagfile
);
1305 exit (system (cmd
));
1307 return EXIT_SUCCESS
;
1312 * Return a compressor given the file name. If EXTPTR is non-zero,
1313 * return a pointer into FILE where the compressor-specific
1314 * extension begins. If no compressor is found, NULL is returned
1315 * and EXTPTR is not significant.
1316 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1319 get_compressor_from_suffix (char *file
, char **extptr
)
1322 char *slash
, *suffix
;
1324 /* File has been processed by canonicalize_filename,
1325 so we don't need to consider backslashes on DOS_NT. */
1326 slash
= strrchr (file
, '/');
1327 suffix
= strrchr (file
, '.');
1328 if (suffix
== NULL
|| suffix
< slash
)
1333 /* Let those poor souls who live with DOS 8+3 file name limits get
1334 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1335 Only the first do loop is run if not MSDOS */
1338 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1339 if (streq (compr
->suffix
, suffix
))
1342 break; /* do it only once: not really a loop */
1345 } while (*suffix
!= '\0');
1352 * Return a language given the name.
1355 get_language_from_langname (const char *name
)
1360 error ("empty language name");
1363 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1364 if (streq (name
, lang
->name
))
1366 error ("unknown language \"%s\"", name
);
1374 * Return a language given the interpreter name.
1377 get_language_from_interpreter (char *interpreter
)
1382 if (interpreter
== NULL
)
1384 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1385 if (lang
->interpreters
!= NULL
)
1386 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1387 if (streq (*iname
, interpreter
))
1396 * Return a language given the file name.
1399 get_language_from_filename (char *file
, int case_sensitive
)
1402 const char **name
, **ext
, *suffix
;
1404 /* Try whole file name first. */
1405 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1406 if (lang
->filenames
!= NULL
)
1407 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1408 if ((case_sensitive
)
1409 ? streq (*name
, file
)
1410 : strcaseeq (*name
, file
))
1413 /* If not found, try suffix after last dot. */
1414 suffix
= strrchr (file
, '.');
1418 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1419 if (lang
->suffixes
!= NULL
)
1420 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1421 if ((case_sensitive
)
1422 ? streq (*ext
, suffix
)
1423 : strcaseeq (*ext
, suffix
))
1430 * This routine is called on each file argument.
1433 process_file_name (char *file
, language
*lang
)
1435 struct stat stat_buf
;
1439 char *compressed_name
, *uncompressed_name
;
1440 char *ext
, *real_name
;
1443 canonicalize_filename (file
);
1444 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1446 error ("skipping inclusion of %s in self.", file
);
1449 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1451 compressed_name
= NULL
;
1452 real_name
= uncompressed_name
= savestr (file
);
1456 real_name
= compressed_name
= savestr (file
);
1457 uncompressed_name
= savenstr (file
, ext
- file
);
1460 /* If the canonicalized uncompressed name
1461 has already been dealt with, skip it silently. */
1462 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1464 assert (fdp
->infname
!= NULL
);
1465 if (streq (uncompressed_name
, fdp
->infname
))
1469 if (stat (real_name
, &stat_buf
) != 0)
1471 /* Reset real_name and try with a different name. */
1473 if (compressed_name
!= NULL
) /* try with the given suffix */
1475 if (stat (uncompressed_name
, &stat_buf
) == 0)
1476 real_name
= uncompressed_name
;
1478 else /* try all possible suffixes */
1480 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1482 compressed_name
= concat (file
, ".", compr
->suffix
);
1483 if (stat (compressed_name
, &stat_buf
) != 0)
1487 char *suf
= compressed_name
+ strlen (file
);
1488 size_t suflen
= strlen (compr
->suffix
) + 1;
1489 for ( ; suf
[1]; suf
++, suflen
--)
1491 memmove (suf
, suf
+ 1, suflen
);
1492 if (stat (compressed_name
, &stat_buf
) == 0)
1494 real_name
= compressed_name
;
1498 if (real_name
!= NULL
)
1501 free (compressed_name
);
1502 compressed_name
= NULL
;
1506 real_name
= compressed_name
;
1511 if (real_name
== NULL
)
1516 } /* try with a different name */
1518 if (!S_ISREG (stat_buf
.st_mode
))
1520 error ("skipping %s: it is not a regular file.", real_name
);
1523 if (real_name
== compressed_name
)
1525 char *cmd
= concat (compr
->command
, " ", real_name
);
1526 inf
= popen (cmd
, "r" FOPEN_BINARY
);
1530 inf
= fopen (real_name
, "r" FOPEN_BINARY
);
1537 process_file (inf
, uncompressed_name
, lang
);
1539 if (real_name
== compressed_name
)
1540 retval
= pclose (inf
);
1542 retval
= fclose (inf
);
1547 free (compressed_name
);
1548 free (uncompressed_name
);
1555 process_file (FILE *fh
, char *fn
, language
*lang
)
1557 static const fdesc emptyfdesc
;
1560 /* Create a new input file description entry. */
1561 fdp
= xnew (1, fdesc
);
1564 fdp
->infname
= savestr (fn
);
1566 fdp
->infabsname
= absolute_filename (fn
, cwd
);
1567 fdp
->infabsdir
= absolute_dirname (fn
, cwd
);
1568 if (filename_is_absolute (fn
))
1570 /* An absolute file name. Canonicalize it. */
1571 fdp
->taggedfname
= absolute_filename (fn
, NULL
);
1575 /* A file name relative to cwd. Make it relative
1576 to the directory of the tags file. */
1577 fdp
->taggedfname
= relative_filename (fn
, tagfiledir
);
1579 fdp
->usecharno
= true; /* use char position when making tags */
1581 fdp
->written
= false; /* not written on tags file yet */
1584 curfdp
= fdhead
; /* the current file description */
1588 /* If not Ctags, and if this is not metasource and if it contained no #line
1589 directives, we can write the tags and free all nodes pointing to
1592 && curfdp
->usecharno
/* no #line directives in this file */
1593 && !curfdp
->lang
->metasource
)
1597 /* Look for the head of the sublist relative to this file. See add_node
1598 for the structure of the node tree. */
1600 for (np
= nodehead
; np
!= NULL
; prev
= np
, np
= np
->left
)
1601 if (np
->fdp
== curfdp
)
1604 /* If we generated tags for this file, write and delete them. */
1607 /* This is the head of the last sublist, if any. The following
1608 instructions depend on this being true. */
1609 assert (np
->left
== NULL
);
1611 assert (fdhead
== curfdp
);
1612 assert (last_node
->fdp
== curfdp
);
1613 put_entries (np
); /* write tags for file curfdp->taggedfname */
1614 free_tree (np
); /* remove the written nodes */
1616 nodehead
= NULL
; /* no nodes left */
1618 prev
->left
= NULL
; /* delete the pointer to the sublist */
1624 * This routine sets up the boolean pseudo-functions which work
1625 * by setting boolean flags dependent upon the corresponding character.
1626 * Every char which is NOT in that string is not a white char. Therefore,
1627 * all of the array "_wht" is set to false, and then the elements
1628 * subscripted by the chars in "white" are set to true. Thus "_wht"
1629 * of a char is true if it is the string "white", else false.
1637 for (i
= 0; i
< CHARS
; i
++)
1638 iswhite (i
) = notinname (i
) = begtoken (i
) = intoken (i
) = endtoken (i
)
1640 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = true;
1641 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = true;
1642 notinname ('\0') = notinname ('\n');
1643 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = true;
1644 begtoken ('\0') = begtoken ('\n');
1645 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = true;
1646 intoken ('\0') = intoken ('\n');
1647 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = true;
1648 endtoken ('\0') = endtoken ('\n');
1652 * This routine opens the specified file and calls the function
1653 * which finds the function and type definitions.
1656 find_entries (FILE *inf
)
1659 language
*lang
= curfdp
->lang
;
1660 Lang_function
*parser
= NULL
;
1662 /* If user specified a language, use it. */
1663 if (lang
!= NULL
&& lang
->function
!= NULL
)
1665 parser
= lang
->function
;
1668 /* Else try to guess the language given the file name. */
1671 lang
= get_language_from_filename (curfdp
->infname
, true);
1672 if (lang
!= NULL
&& lang
->function
!= NULL
)
1674 curfdp
->lang
= lang
;
1675 parser
= lang
->function
;
1679 /* Else look for sharp-bang as the first two characters. */
1681 && readline_internal (&lb
, inf
) > 0
1683 && lb
.buffer
[0] == '#'
1684 && lb
.buffer
[1] == '!')
1688 /* Set lp to point at the first char after the last slash in the
1689 line or, if no slashes, at the first nonblank. Then set cp to
1690 the first successive blank and terminate the string. */
1691 lp
= strrchr (lb
.buffer
+2, '/');
1695 lp
= skip_spaces (lb
.buffer
+ 2);
1696 cp
= skip_non_spaces (lp
);
1699 if (strlen (lp
) > 0)
1701 lang
= get_language_from_interpreter (lp
);
1702 if (lang
!= NULL
&& lang
->function
!= NULL
)
1704 curfdp
->lang
= lang
;
1705 parser
= lang
->function
;
1710 /* We rewind here, even if inf may be a pipe. We fail if the
1711 length of the first line is longer than the pipe block size,
1712 which is unlikely. */
1715 /* Else try to guess the language given the case insensitive file name. */
1718 lang
= get_language_from_filename (curfdp
->infname
, false);
1719 if (lang
!= NULL
&& lang
->function
!= NULL
)
1721 curfdp
->lang
= lang
;
1722 parser
= lang
->function
;
1726 /* Else try Fortran or C. */
1729 node
*old_last_node
= last_node
;
1731 curfdp
->lang
= get_language_from_langname ("fortran");
1734 if (old_last_node
== last_node
)
1735 /* No Fortran entries found. Try C. */
1737 /* We do not tag if rewind fails.
1738 Only the file name will be recorded in the tags file. */
1740 curfdp
->lang
= get_language_from_langname (cplusplus
? "c++" : "c");
1746 if (!no_line_directive
1747 && curfdp
->lang
!= NULL
&& curfdp
->lang
->metasource
)
1748 /* It may be that this is a bingo.y file, and we already parsed a bingo.c
1749 file, or anyway we parsed a file that is automatically generated from
1750 this one. If this is the case, the bingo.c file contained #line
1751 directives that generated tags pointing to this file. Let's delete
1752 them all before parsing this file, which is the real source. */
1754 fdesc
**fdpp
= &fdhead
;
1755 while (*fdpp
!= NULL
)
1757 && streq ((*fdpp
)->taggedfname
, curfdp
->taggedfname
))
1758 /* We found one of those! We must delete both the file description
1759 and all tags referring to it. */
1761 fdesc
*badfdp
= *fdpp
;
1763 /* Delete the tags referring to badfdp->taggedfname
1764 that were obtained from badfdp->infname. */
1765 invalidate_nodes (badfdp
, &nodehead
);
1767 *fdpp
= badfdp
->next
; /* remove the bad description from the list */
1768 free_fdesc (badfdp
);
1771 fdpp
= &(*fdpp
)->next
; /* advance the list pointer */
1774 assert (parser
!= NULL
);
1776 /* Generic initializations before reading from file. */
1777 linebuffer_setlen (&filebuf
, 0); /* reset the file buffer */
1779 /* Generic initializations before parsing file with readline. */
1780 lineno
= 0; /* reset global line number */
1781 charno
= 0; /* reset global char number */
1782 linecharno
= 0; /* reset global char number of line start */
1786 regex_tag_multiline ();
1791 * Check whether an implicitly named tag should be created,
1792 * then call `pfnote'.
1793 * NAME is a string that is internally copied by this function.
1795 * TAGS format specification
1796 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1797 * The following is explained in some more detail in etc/ETAGS.EBNF.
1799 * make_tag creates tags with "implicit tag names" (unnamed tags)
1800 * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
1801 * 1. NAME does not contain any of the characters in NONAM;
1802 * 2. LINESTART contains name as either a rightmost, or rightmost but
1803 * one character, substring;
1804 * 3. the character, if any, immediately before NAME in LINESTART must
1805 * be a character in NONAM;
1806 * 4. the character, if any, immediately after NAME in LINESTART must
1807 * also be a character in NONAM.
1809 * The implementation uses the notinname() macro, which recognizes the
1810 * characters stored in the string `nonam'.
1811 * etags.el needs to use the same characters that are in NONAM.
1814 make_tag (const char *name
, /* tag name, or NULL if unnamed */
1815 int namelen
, /* tag length */
1816 bool is_func
, /* tag is a function */
1817 char *linestart
, /* start of the line where tag is */
1818 int linelen
, /* length of the line where tag is */
1819 int lno
, /* line number */
1820 long int cno
) /* character number */
1822 bool named
= (name
!= NULL
&& namelen
> 0);
1825 if (!CTAGS
&& named
) /* maybe set named to false */
1826 /* Let's try to make an implicit tag name, that is, create an unnamed tag
1827 such that etags.el can guess a name from it. */
1830 register const char *cp
= name
;
1832 for (i
= 0; i
< namelen
; i
++)
1833 if (notinname (*cp
++))
1835 if (i
== namelen
) /* rule #1 */
1837 cp
= linestart
+ linelen
- namelen
;
1838 if (notinname (linestart
[linelen
-1]))
1839 cp
-= 1; /* rule #4 */
1840 if (cp
>= linestart
/* rule #2 */
1842 || notinname (cp
[-1])) /* rule #3 */
1843 && strneq (name
, cp
, namelen
)) /* rule #2 */
1844 named
= false; /* use implicit tag name */
1849 nname
= savenstr (name
, namelen
);
1851 pfnote (nname
, is_func
, linestart
, linelen
, lno
, cno
);
1856 pfnote (char *name
, bool is_func
, char *linestart
, int linelen
, int lno
,
1858 /* tag name, or NULL if unnamed */
1859 /* tag is a function */
1860 /* start of the line where tag is */
1861 /* length of the line where tag is */
1863 /* character number */
1867 assert (name
== NULL
|| name
[0] != '\0');
1868 if (CTAGS
&& name
== NULL
)
1871 np
= xnew (1, node
);
1873 /* If ctags mode, change name "main" to M<thisfilename>. */
1874 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1876 char *fp
= strrchr (curfdp
->taggedfname
, '/');
1877 np
->name
= concat ("M", fp
== NULL
? curfdp
->taggedfname
: fp
+ 1, "");
1878 fp
= strrchr (np
->name
, '.');
1879 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
1885 np
->been_warned
= false;
1887 np
->is_func
= is_func
;
1889 if (np
->fdp
->usecharno
)
1890 /* Our char numbers are 0-base, because of C language tradition?
1891 ctags compatibility? old versions compatibility? I don't know.
1892 Anyway, since emacs's are 1-base we expect etags.el to take care
1893 of the difference. If we wanted to have 1-based numbers, we would
1894 uncomment the +1 below. */
1895 np
->cno
= cno
/* + 1 */ ;
1897 np
->cno
= invalidcharno
;
1898 np
->left
= np
->right
= NULL
;
1899 if (CTAGS
&& !cxref_style
)
1901 if (strlen (linestart
) < 50)
1902 np
->regex
= concat (linestart
, "$", "");
1904 np
->regex
= savenstr (linestart
, 50);
1907 np
->regex
= savenstr (linestart
, linelen
);
1909 add_node (np
, &nodehead
);
1914 * recurse on left children, iterate on right children.
1917 free_tree (register node
*np
)
1921 register node
*node_right
= np
->right
;
1922 free_tree (np
->left
);
1932 * delete a file description
1935 free_fdesc (register fdesc
*fdp
)
1937 free (fdp
->infname
);
1938 free (fdp
->infabsname
);
1939 free (fdp
->infabsdir
);
1940 free (fdp
->taggedfname
);
1947 * Adds a node to the tree of nodes. In etags mode, sort by file
1948 * name. In ctags mode, sort by tag name. Make no attempt at
1951 * add_node is the only function allowed to add nodes, so it can
1955 add_node (node
*np
, node
**cur_node_p
)
1958 register node
*cur_node
= *cur_node_p
;
1960 if (cur_node
== NULL
)
1970 /* For each file name, tags are in a linked sublist on the right
1971 pointer. The first tags of different files are a linked list
1972 on the left pointer. last_node points to the end of the last
1974 if (last_node
!= NULL
&& last_node
->fdp
== np
->fdp
)
1976 /* Let's use the same sublist as the last added node. */
1977 assert (last_node
->right
== NULL
);
1978 last_node
->right
= np
;
1981 else if (cur_node
->fdp
== np
->fdp
)
1983 /* Scanning the list we found the head of a sublist which is
1984 good for us. Let's scan this sublist. */
1985 add_node (np
, &cur_node
->right
);
1988 /* The head of this sublist is not good for us. Let's try the
1990 add_node (np
, &cur_node
->left
);
1991 } /* if ETAGS mode */
1996 dif
= strcmp (np
->name
, cur_node
->name
);
1999 * If this tag name matches an existing one, then
2000 * do not add the node, but maybe print a warning.
2002 if (no_duplicates
&& !dif
)
2004 if (np
->fdp
== cur_node
->fdp
)
2008 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
2009 np
->fdp
->infname
, lineno
, np
->name
);
2010 fprintf (stderr
, "Second entry ignored\n");
2013 else if (!cur_node
->been_warned
&& !no_warnings
)
2017 "Duplicate entry in files %s and %s: %s (Warning only)\n",
2018 np
->fdp
->infname
, cur_node
->fdp
->infname
, np
->name
);
2019 cur_node
->been_warned
= true;
2024 /* Actually add the node */
2025 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
2026 } /* if CTAGS mode */
2030 * invalidate_nodes ()
2031 * Scan the node tree and invalidate all nodes pointing to the
2032 * given file description (CTAGS case) or free them (ETAGS case).
2035 invalidate_nodes (fdesc
*badfdp
, node
**npp
)
2044 if (np
->left
!= NULL
)
2045 invalidate_nodes (badfdp
, &np
->left
);
2046 if (np
->fdp
== badfdp
)
2048 if (np
->right
!= NULL
)
2049 invalidate_nodes (badfdp
, &np
->right
);
2053 assert (np
->fdp
!= NULL
);
2054 if (np
->fdp
== badfdp
)
2056 *npp
= np
->left
; /* detach the sublist from the list */
2057 np
->left
= NULL
; /* isolate it */
2058 free_tree (np
); /* free it */
2059 invalidate_nodes (badfdp
, npp
);
2062 invalidate_nodes (badfdp
, &np
->left
);
2067 static int total_size_of_entries (node
*);
2068 static int number_len (long) ATTRIBUTE_CONST
;
2070 /* Length of a non-negative number's decimal representation. */
2072 number_len (long int num
)
2075 while ((num
/= 10) > 0)
2081 * Return total number of characters that put_entries will output for
2082 * the nodes in the linked list at the right of the specified node.
2083 * This count is irrelevant with etags.el since emacs 19.34 at least,
2084 * but is still supplied for backward compatibility.
2087 total_size_of_entries (register node
*np
)
2089 register int total
= 0;
2091 for (; np
!= NULL
; np
= np
->right
)
2094 total
+= strlen (np
->regex
) + 1; /* pat\177 */
2095 if (np
->name
!= NULL
)
2096 total
+= strlen (np
->name
) + 1; /* name\001 */
2097 total
+= number_len ((long) np
->lno
) + 1; /* lno, */
2098 if (np
->cno
!= invalidcharno
) /* cno */
2099 total
+= number_len (np
->cno
);
2100 total
+= 1; /* newline */
2107 put_entries (register node
*np
)
2110 static fdesc
*fdp
= NULL
;
2115 /* Output subentries that precede this one */
2117 put_entries (np
->left
);
2119 /* Output this entry */
2128 fprintf (tagf
, "\f\n%s,%d\n",
2129 fdp
->taggedfname
, total_size_of_entries (np
));
2130 fdp
->written
= true;
2132 fputs (np
->regex
, tagf
);
2133 fputc ('\177', tagf
);
2134 if (np
->name
!= NULL
)
2136 fputs (np
->name
, tagf
);
2137 fputc ('\001', tagf
);
2139 fprintf (tagf
, "%d,", np
->lno
);
2140 if (np
->cno
!= invalidcharno
)
2141 fprintf (tagf
, "%ld", np
->cno
);
2147 if (np
->name
== NULL
)
2148 error ("internal error: NULL name in ctags mode.");
2153 fprintf (stdout
, "%s %s %d\n",
2154 np
->name
, np
->fdp
->taggedfname
, (np
->lno
+ 63) / 64);
2156 fprintf (stdout
, "%-16s %3d %-16s %s\n",
2157 np
->name
, np
->lno
, np
->fdp
->taggedfname
, np
->regex
);
2161 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->fdp
->taggedfname
);
2164 { /* function or #define macro with args */
2165 putc (searchar
, tagf
);
2168 for (sp
= np
->regex
; *sp
; sp
++)
2170 if (*sp
== '\\' || *sp
== searchar
)
2174 putc (searchar
, tagf
);
2177 { /* anything else; text pattern inadequate */
2178 fprintf (tagf
, "%d", np
->lno
);
2183 } /* if this node contains a valid tag */
2185 /* Output subentries that follow this one */
2186 put_entries (np
->right
);
2188 put_entries (np
->left
);
2193 #define C_EXT 0x00fff /* C extensions */
2194 #define C_PLAIN 0x00000 /* C */
2195 #define C_PLPL 0x00001 /* C++ */
2196 #define C_STAR 0x00003 /* C* */
2197 #define C_JAVA 0x00005 /* JAVA */
2198 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
2199 #define YACC 0x10000 /* yacc file */
2202 * The C symbol tables.
2207 st_C_objprot
, st_C_objimpl
, st_C_objend
,
2209 st_C_ignore
, st_C_attribute
,
2212 st_C_class
, st_C_template
,
2213 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
2216 /* Feed stuff between (but not including) %[ and %] lines to:
2222 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2226 while, 0, st_C_ignore
2227 switch, 0, st_C_ignore
2228 return, 0, st_C_ignore
2229 __attribute__, 0, st_C_attribute
2230 GTY, 0, st_C_attribute
2231 @interface, 0, st_C_objprot
2232 @protocol, 0, st_C_objprot
2233 @implementation,0, st_C_objimpl
2234 @end, 0, st_C_objend
2235 import, (C_JAVA & ~C_PLPL), st_C_ignore
2236 package, (C_JAVA & ~C_PLPL), st_C_ignore
2237 friend, C_PLPL, st_C_ignore
2238 extends, (C_JAVA & ~C_PLPL), st_C_javastruct
2239 implements, (C_JAVA & ~C_PLPL), st_C_javastruct
2240 interface, (C_JAVA & ~C_PLPL), st_C_struct
2241 class, 0, st_C_class
2242 namespace, C_PLPL, st_C_struct
2243 domain, C_STAR, st_C_struct
2244 union, 0, st_C_struct
2245 struct, 0, st_C_struct
2246 extern, 0, st_C_extern
2248 typedef, 0, st_C_typedef
2249 define, 0, st_C_define
2250 undef, 0, st_C_define
2251 operator, C_PLPL, st_C_operator
2252 template, 0, st_C_template
2253 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2254 DEFUN, 0, st_C_gnumacro
2255 SYSCALL, 0, st_C_gnumacro
2256 ENTRY, 0, st_C_gnumacro
2257 PSEUDO, 0, st_C_gnumacro
2258 # These are defined inside C functions, so currently they are not met.
2259 # EXFUN used in glibc, DEFVAR_* in emacs.
2260 #EXFUN, 0, st_C_gnumacro
2261 #DEFVAR_, 0, st_C_gnumacro
2263 and replace lines between %< and %> with its output, then:
2264 - remove the #if characterset check
2265 - make in_word_set static and not inline. */
2267 /* C code produced by gperf version 3.0.1 */
2268 /* Command-line: gperf -m 5 */
2269 /* Computed positions: -k'2-3' */
2271 struct C_stab_entry
{ const char *name
; int c_ext
; enum sym_type type
; };
2272 /* maximum key range = 33, duplicates = 0 */
2275 hash (const char *str
, int len
)
2277 static char const asso_values
[] =
2279 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2280 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2281 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2282 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2283 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2284 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2285 35, 35, 35, 35, 35, 35, 35, 35, 35, 3,
2286 26, 35, 35, 35, 35, 35, 35, 35, 27, 35,
2287 35, 35, 35, 24, 0, 35, 35, 35, 35, 0,
2288 35, 35, 35, 35, 35, 1, 35, 16, 35, 6,
2289 23, 0, 0, 35, 22, 0, 35, 35, 5, 0,
2290 0, 15, 1, 35, 6, 35, 8, 19, 35, 16,
2291 4, 5, 35, 35, 35, 35, 35, 35, 35, 35,
2292 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2293 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2294 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2295 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2296 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2297 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2298 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2299 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2300 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2301 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2302 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2303 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
2304 35, 35, 35, 35, 35, 35
2311 hval
+= asso_values
[(unsigned char) str
[2]];
2314 hval
+= asso_values
[(unsigned char) str
[1]];
2320 static struct C_stab_entry
*
2321 in_word_set (register const char *str
, register unsigned int len
)
2325 TOTAL_KEYWORDS
= 33,
2326 MIN_WORD_LENGTH
= 2,
2327 MAX_WORD_LENGTH
= 15,
2332 static struct C_stab_entry wordlist
[] =
2335 {"if", 0, st_C_ignore
},
2336 {"GTY", 0, st_C_attribute
},
2337 {"@end", 0, st_C_objend
},
2338 {"union", 0, st_C_struct
},
2339 {"define", 0, st_C_define
},
2340 {"import", (C_JAVA
& ~C_PLPL
), st_C_ignore
},
2341 {"template", 0, st_C_template
},
2342 {"operator", C_PLPL
, st_C_operator
},
2343 {"@interface", 0, st_C_objprot
},
2344 {"implements", (C_JAVA
& ~C_PLPL
), st_C_javastruct
},
2345 {"friend", C_PLPL
, st_C_ignore
},
2346 {"typedef", 0, st_C_typedef
},
2347 {"return", 0, st_C_ignore
},
2348 {"@implementation",0, st_C_objimpl
},
2349 {"@protocol", 0, st_C_objprot
},
2350 {"interface", (C_JAVA
& ~C_PLPL
), st_C_struct
},
2351 {"extern", 0, st_C_extern
},
2352 {"extends", (C_JAVA
& ~C_PLPL
), st_C_javastruct
},
2353 {"struct", 0, st_C_struct
},
2354 {"domain", C_STAR
, st_C_struct
},
2355 {"switch", 0, st_C_ignore
},
2356 {"enum", 0, st_C_enum
},
2357 {"for", 0, st_C_ignore
},
2358 {"namespace", C_PLPL
, st_C_struct
},
2359 {"class", 0, st_C_class
},
2360 {"while", 0, st_C_ignore
},
2361 {"undef", 0, st_C_define
},
2362 {"package", (C_JAVA
& ~C_PLPL
), st_C_ignore
},
2363 {"__attribute__", 0, st_C_attribute
},
2364 {"SYSCALL", 0, st_C_gnumacro
},
2365 {"ENTRY", 0, st_C_gnumacro
},
2366 {"PSEUDO", 0, st_C_gnumacro
},
2367 {"DEFUN", 0, st_C_gnumacro
}
2370 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2372 int key
= hash (str
, len
);
2374 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2376 const char *s
= wordlist
[key
].name
;
2378 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1) && s
[len
] == '\0')
2379 return &wordlist
[key
];
2386 static enum sym_type
2387 C_symtype (char *str
, int len
, int c_ext
)
2389 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2391 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2398 * Ignoring __attribute__ ((list))
2400 static bool inattribute
; /* looking at an __attribute__ construct */
2403 * C functions and variables are recognized using a simple
2404 * finite automaton. fvdef is its state variable.
2408 fvnone
, /* nothing seen */
2409 fdefunkey
, /* Emacs DEFUN keyword seen */
2410 fdefunname
, /* Emacs DEFUN name seen */
2411 foperator
, /* func: operator keyword seen (cplpl) */
2412 fvnameseen
, /* function or variable name seen */
2413 fstartlist
, /* func: just after open parenthesis */
2414 finlist
, /* func: in parameter list */
2415 flistseen
, /* func: after parameter list */
2416 fignore
, /* func: before open brace */
2417 vignore
/* var-like: ignore until ';' */
2420 static bool fvextern
; /* func or var: extern keyword seen; */
2423 * typedefs are recognized using a simple finite automaton.
2424 * typdef is its state variable.
2428 tnone
, /* nothing seen */
2429 tkeyseen
, /* typedef keyword seen */
2430 ttypeseen
, /* defined type seen */
2431 tinbody
, /* inside typedef body */
2432 tend
, /* just before typedef tag */
2433 tignore
/* junk after typedef tag */
2437 * struct-like structures (enum, struct and union) are recognized
2438 * using another simple finite automaton. `structdef' is its state
2443 snone
, /* nothing seen yet,
2444 or in struct body if bracelev > 0 */
2445 skeyseen
, /* struct-like keyword seen */
2446 stagseen
, /* struct-like tag seen */
2447 scolonseen
/* colon seen after struct-like tag */
2451 * When objdef is different from onone, objtag is the name of the class.
2453 static const char *objtag
= "<uninited>";
2456 * Yet another little state machine to deal with preprocessor lines.
2460 dnone
, /* nothing seen */
2461 dsharpseen
, /* '#' seen as first char on line */
2462 ddefineseen
, /* '#' and 'define' seen */
2463 dignorerest
/* ignore rest of line */
2467 * State machine for Objective C protocols and implementations.
2468 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2472 onone
, /* nothing seen */
2473 oprotocol
, /* @interface or @protocol seen */
2474 oimplementation
, /* @implementations seen */
2475 otagseen
, /* class name seen */
2476 oparenseen
, /* parenthesis before category seen */
2477 ocatseen
, /* category name seen */
2478 oinbody
, /* in @implementation body */
2479 omethodsign
, /* in @implementation body, after +/- */
2480 omethodtag
, /* after method name */
2481 omethodcolon
, /* after method colon */
2482 omethodparm
, /* after method parameter */
2483 oignore
/* wait for @end */
2488 * Use this structure to keep info about the token read, and how it
2489 * should be tagged. Used by the make_C_tag function to build a tag.
2493 char *line
; /* string containing the token */
2494 int offset
; /* where the token starts in LINE */
2495 int length
; /* token length */
2497 The previous members can be used to pass strings around for generic
2498 purposes. The following ones specifically refer to creating tags. In this
2499 case the token contained here is the pattern that will be used to create a
2502 bool valid
; /* do not create a tag; the token should be
2503 invalidated whenever a state machine is
2504 reset prematurely */
2505 bool named
; /* create a named tag */
2506 int lineno
; /* source line number of tag */
2507 long linepos
; /* source char number of tag */
2508 } token
; /* latest token read */
2511 * Variables and functions for dealing with nested structures.
2512 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2514 static void pushclass_above (int, char *, int);
2515 static void popclass_above (int);
2516 static void write_classname (linebuffer
*, const char *qualifier
);
2519 char **cname
; /* nested class names */
2520 int *bracelev
; /* nested class brace level */
2521 int nl
; /* class nesting level (elements used) */
2522 int size
; /* length of the array */
2523 } cstack
; /* stack for nested declaration tags */
2524 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2525 #define nestlev (cstack.nl)
2526 /* After struct keyword or in struct body, not inside a nested function. */
2527 #define instruct (structdef == snone && nestlev > 0 \
2528 && bracelev == cstack.bracelev[nestlev-1] + 1)
2531 pushclass_above (int bracelev
, char *str
, int len
)
2535 popclass_above (bracelev
);
2537 if (nl
>= cstack
.size
)
2539 int size
= cstack
.size
*= 2;
2540 xrnew (cstack
.cname
, size
, char *);
2541 xrnew (cstack
.bracelev
, size
, int);
2543 assert (nl
== 0 || cstack
.bracelev
[nl
-1] < bracelev
);
2544 cstack
.cname
[nl
] = (str
== NULL
) ? NULL
: savenstr (str
, len
);
2545 cstack
.bracelev
[nl
] = bracelev
;
2550 popclass_above (int bracelev
)
2554 for (nl
= cstack
.nl
- 1;
2555 nl
>= 0 && cstack
.bracelev
[nl
] >= bracelev
;
2558 free (cstack
.cname
[nl
]);
2564 write_classname (linebuffer
*cn
, const char *qualifier
)
2567 int qlen
= strlen (qualifier
);
2569 if (cstack
.nl
== 0 || cstack
.cname
[0] == NULL
)
2573 cn
->buffer
[0] = '\0';
2577 len
= strlen (cstack
.cname
[0]);
2578 linebuffer_setlen (cn
, len
);
2579 strcpy (cn
->buffer
, cstack
.cname
[0]);
2581 for (i
= 1; i
< cstack
.nl
; i
++)
2583 char *s
= cstack
.cname
[i
];
2586 linebuffer_setlen (cn
, len
+ qlen
+ strlen (s
));
2587 len
+= sprintf (cn
->buffer
+ len
, "%s%s", qualifier
, s
);
2592 static bool consider_token (char *, int, int, int *, int, int, bool *);
2593 static void make_C_tag (bool);
2597 * checks to see if the current token is at the start of a
2598 * function or variable, or corresponds to a typedef, or
2599 * is a struct/union/enum tag, or #define, or an enum constant.
2601 * *IS_FUNC_OR_VAR gets true if the token is a function or #define macro
2602 * with args. C_EXTP points to which language we are looking at.
2613 consider_token (char *str
, int len
, int c
, int *c_extp
,
2614 int bracelev
, int parlev
, bool *is_func_or_var
)
2615 /* IN: token pointer */
2616 /* IN: token length */
2617 /* IN: first char after the token */
2618 /* IN, OUT: C extensions mask */
2619 /* IN: brace level */
2620 /* IN: parenthesis level */
2621 /* OUT: function or variable found */
2623 /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
2624 structtype is the type of the preceding struct-like keyword, and
2625 structbracelev is the brace level where it has been seen. */
2626 static enum sym_type structtype
;
2627 static int structbracelev
;
2628 static enum sym_type toktype
;
2631 toktype
= C_symtype (str
, len
, *c_extp
);
2634 * Skip __attribute__
2636 if (toktype
== st_C_attribute
)
2643 * Advance the definedef state machine.
2648 /* We're not on a preprocessor line. */
2649 if (toktype
== st_C_gnumacro
)
2656 if (toktype
== st_C_define
)
2658 definedef
= ddefineseen
;
2662 definedef
= dignorerest
;
2667 * Make a tag for any macro, unless it is a constant
2668 * and constantypedefs is false.
2670 definedef
= dignorerest
;
2671 *is_func_or_var
= (c
== '(');
2672 if (!*is_func_or_var
&& !constantypedefs
)
2679 error ("internal error: definedef value.");
2688 if (toktype
== st_C_typedef
)
2708 if (structdef
== snone
&& fvdef
== fvnone
)
2727 case st_C_javastruct
:
2728 if (structdef
== stagseen
)
2729 structdef
= scolonseen
;
2733 if ((*c_extp
& C_AUTO
) /* automatic detection of C++ language */
2735 && definedef
== dnone
&& structdef
== snone
2736 && typdef
== tnone
&& fvdef
== fvnone
)
2737 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2738 if (toktype
== st_C_template
)
2745 && (typdef
== tkeyseen
2746 || (typedefs_or_cplusplus
&& structdef
== snone
)))
2748 structdef
= skeyseen
;
2749 structtype
= toktype
;
2750 structbracelev
= bracelev
;
2751 if (fvdef
== fvnameseen
)
2757 if (structdef
== skeyseen
)
2759 structdef
= stagseen
;
2763 if (typdef
!= tnone
)
2766 /* Detect Objective C constructs. */
2776 objdef
= oimplementation
;
2780 case oimplementation
:
2781 /* Save the class tag for functions or variables defined inside. */
2782 objtag
= savenstr (str
, len
);
2786 /* Save the class tag for categories. */
2787 objtag
= savenstr (str
, len
);
2789 *is_func_or_var
= true;
2793 *is_func_or_var
= true;
2801 objdef
= omethodtag
;
2802 linebuffer_setlen (&token_name
, len
);
2803 memcpy (token_name
.buffer
, str
, len
);
2804 token_name
.buffer
[len
] = '\0';
2810 objdef
= omethodparm
;
2815 int oldlen
= token_name
.len
;
2817 objdef
= omethodtag
;
2818 linebuffer_setlen (&token_name
, oldlen
+ len
);
2819 memcpy (token_name
.buffer
+ oldlen
, str
, len
);
2820 token_name
.buffer
[oldlen
+ len
] = '\0';
2825 if (toktype
== st_C_objend
)
2827 /* Memory leakage here: the string pointed by objtag is
2828 never released, because many tests would be needed to
2829 avoid breaking on incorrect input code. The amount of
2830 memory leaked here is the sum of the lengths of the
2838 /* A function, variable or enum constant? */
2860 *is_func_or_var
= true;
2864 && structdef
== snone
2865 && structtype
== st_C_enum
&& bracelev
> structbracelev
2866 /* Don't tag tokens in expressions that assign values to enum
2868 && fvdef
!= vignore
)
2869 return true; /* enum constant */
2875 fvdef
= fdefunname
; /* GNU macro */
2876 *is_func_or_var
= true;
2884 if ((strneq (str
, "asm", 3) && endtoken (str
[3]))
2885 || (strneq (str
, "__asm__", 7) && endtoken (str
[7])))
2894 if (len
>= 10 && strneq (str
+len
-10, "::operator", 10))
2896 if (*c_extp
& C_AUTO
) /* automatic detection of C++ */
2897 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2899 *is_func_or_var
= true;
2902 if (bracelev
> 0 && !instruct
)
2904 fvdef
= fvnameseen
; /* function or variable */
2905 *is_func_or_var
= true;
2916 * C_entries often keeps pointers to tokens or lines which are older than
2917 * the line currently read. By keeping two line buffers, and switching
2918 * them at end of line, it is possible to use those pointers.
2926 #define current_lb_is_new (newndx == curndx)
2927 #define switch_line_buffers() (curndx = 1 - curndx)
2929 #define curlb (lbs[curndx].lb)
2930 #define newlb (lbs[newndx].lb)
2931 #define curlinepos (lbs[curndx].linepos)
2932 #define newlinepos (lbs[newndx].linepos)
2934 #define plainc ((c_ext & C_EXT) == C_PLAIN)
2935 #define cplpl (c_ext & C_PLPL)
2936 #define cjava ((c_ext & C_JAVA) == C_JAVA)
2938 #define CNL_SAVE_DEFINEDEF() \
2940 curlinepos = charno; \
2941 readline (&curlb, inf); \
2942 lp = curlb.buffer; \
2949 CNL_SAVE_DEFINEDEF(); \
2950 if (savetoken.valid) \
2952 token = savetoken; \
2953 savetoken.valid = false; \
2955 definedef = dnone; \
2960 make_C_tag (bool isfun
)
2962 /* This function is never called when token.valid is false, but
2963 we must protect against invalid input or internal errors. */
2965 make_tag (token_name
.buffer
, token_name
.len
, isfun
, token
.line
,
2966 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
2968 { /* this branch is optimized away if !DEBUG */
2969 make_tag (concat ("INVALID TOKEN:-->", token_name
.buffer
, ""),
2970 token_name
.len
+ 17, isfun
, token
.line
,
2971 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
2972 error ("INVALID TOKEN");
2975 token
.valid
= false;
2981 * This routine finds functions, variables, typedefs,
2982 * #define's, enum constants and struct/union/enum definitions in
2983 * C syntax and adds them to the list.
2986 C_entries (int c_ext
, FILE *inf
)
2987 /* extension of C */
2990 register char c
; /* latest char read; '\0' for end of line */
2991 register char *lp
; /* pointer one beyond the character `c' */
2992 int curndx
, newndx
; /* indices for current and new lb */
2993 register int tokoff
; /* offset in line of start of current token */
2994 register int toklen
; /* length of current token */
2995 const char *qualifier
; /* string used to qualify names */
2996 int qlen
; /* length of qualifier */
2997 int bracelev
; /* current brace level */
2998 int bracketlev
; /* current bracket level */
2999 int parlev
; /* current parenthesis level */
3000 int attrparlev
; /* __attribute__ parenthesis level */
3001 int templatelev
; /* current template level */
3002 int typdefbracelev
; /* bracelev where a typedef struct body begun */
3003 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
3004 bool yacc_rules
; /* in the rules part of a yacc file */
3005 struct tok savetoken
= {0}; /* token saved during preprocessor handling */
3008 linebuffer_init (&lbs
[0].lb
);
3009 linebuffer_init (&lbs
[1].lb
);
3010 if (cstack
.size
== 0)
3012 cstack
.size
= (DEBUG
) ? 1 : 4;
3014 cstack
.cname
= xnew (cstack
.size
, char *);
3015 cstack
.bracelev
= xnew (cstack
.size
, int);
3018 tokoff
= toklen
= typdefbracelev
= 0; /* keep compiler quiet */
3019 curndx
= newndx
= 0;
3023 fvdef
= fvnone
; fvextern
= false; typdef
= tnone
;
3024 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
3026 midtoken
= inquote
= inchar
= incomm
= quotednl
= false;
3027 token
.valid
= savetoken
.valid
= false;
3028 bracelev
= bracketlev
= parlev
= attrparlev
= templatelev
= 0;
3030 { qualifier
= "."; qlen
= 1; }
3032 { qualifier
= "::"; qlen
= 2; }
3040 /* If we are at the end of the line, the next character is a
3041 '\0'; do not skip it, because it is what tells us
3042 to read the next line. */
3063 /* Newlines inside comments do not end macro definitions in
3065 CNL_SAVE_DEFINEDEF ();
3078 /* Newlines inside strings do not end macro definitions
3079 in traditional cpp, even though compilers don't
3080 usually accept them. */
3081 CNL_SAVE_DEFINEDEF ();
3091 /* Hmmm, something went wrong. */
3127 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!= vignore
)
3142 else if (/* cplpl && */ *lp
== '/')
3148 if ((c_ext
& YACC
) && *lp
== '%')
3150 /* Entering or exiting rules section in yacc file. */
3152 definedef
= dnone
; fvdef
= fvnone
; fvextern
= false;
3153 typdef
= tnone
; structdef
= snone
;
3154 midtoken
= inquote
= inchar
= incomm
= quotednl
= false;
3156 yacc_rules
= !yacc_rules
;
3162 if (definedef
== dnone
)
3165 bool cpptoken
= true;
3167 /* Look back on this line. If all blanks, or nonblanks
3168 followed by an end of comment, this is a preprocessor
3170 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
3173 if (*cp
== '*' && cp
[1] == '/')
3183 definedef
= dsharpseen
;
3184 /* This is needed for tagging enum values: when there are
3185 preprocessor conditionals inside the enum, we need to
3186 reset the value of fvdef so that the next enum value is
3187 tagged even though the one before it did not end in a
3189 if (fvdef
== vignore
&& instruct
&& parlev
== 0)
3191 if (strneq (cp
, "#if", 3) || strneq (cp
, "#el", 3))
3195 } /* if (definedef == dnone) */
3206 CNL_SAVE_DEFINEDEF ();
3213 /* Consider token only if some involved conditions are satisfied. */
3214 if (typdef
!= tignore
3215 && definedef
!= dignorerest
3218 && (definedef
!= dnone
3219 || structdef
!= scolonseen
)
3226 if (c
== ':' && *lp
== ':' && begtoken (lp
[1]))
3227 /* This handles :: in the middle,
3228 but not at the beginning of an identifier.
3229 Also, space-separated :: is not recognized. */
3231 if (c_ext
& C_AUTO
) /* automatic detection of C++ */
3232 c_ext
= (c_ext
| C_PLPL
) & ~C_AUTO
;
3236 goto still_in_token
;
3240 bool funorvar
= false;
3243 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
3244 &c_ext
, bracelev
, parlev
,
3247 if (fvdef
== foperator
)
3250 lp
= skip_spaces (lp
-1);
3254 && !iswhite (*lp
) && *lp
!= '(')
3257 toklen
+= lp
- oldlp
;
3259 token
.named
= false;
3261 && nestlev
> 0 && definedef
== dnone
)
3262 /* in struct body */
3265 write_classname (&token_name
, qualifier
);
3266 len
= token_name
.len
;
3267 linebuffer_setlen (&token_name
, len
+qlen
+toklen
);
3268 sprintf (token_name
.buffer
+ len
, "%s%.*s",
3269 qualifier
, toklen
, newlb
.buffer
+ tokoff
);
3272 else if (objdef
== ocatseen
)
3273 /* Objective C category */
3275 int len
= strlen (objtag
) + 2 + toklen
;
3276 linebuffer_setlen (&token_name
, len
);
3277 sprintf (token_name
.buffer
, "%s(%.*s)",
3278 objtag
, toklen
, newlb
.buffer
+ tokoff
);
3281 else if (objdef
== omethodtag
3282 || objdef
== omethodparm
)
3283 /* Objective C method */
3287 else if (fvdef
== fdefunname
)
3288 /* GNU DEFUN and similar macros */
3290 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
3294 /* Rewrite the tag so that emacs lisp DEFUNs
3295 can be found by their elisp name */
3301 linebuffer_setlen (&token_name
, len
);
3302 memcpy (token_name
.buffer
,
3303 newlb
.buffer
+ off
, len
);
3304 token_name
.buffer
[len
] = '\0';
3307 if (token_name
.buffer
[len
] == '_')
3308 token_name
.buffer
[len
] = '-';
3309 token
.named
= defun
;
3313 linebuffer_setlen (&token_name
, toklen
);
3314 memcpy (token_name
.buffer
,
3315 newlb
.buffer
+ tokoff
, toklen
);
3316 token_name
.buffer
[toklen
] = '\0';
3317 /* Name macros and members. */
3318 token
.named
= (structdef
== stagseen
3319 || typdef
== ttypeseen
3322 && definedef
== dignorerest
)
3324 && definedef
== dnone
3325 && structdef
== snone
3328 token
.lineno
= lineno
;
3329 token
.offset
= tokoff
;
3330 token
.length
= toklen
;
3331 token
.line
= newlb
.buffer
;
3332 token
.linepos
= newlinepos
;
3335 if (definedef
== dnone
3336 && (fvdef
== fvnameseen
3337 || fvdef
== foperator
3338 || structdef
== stagseen
3340 || typdef
== ttypeseen
3341 || objdef
!= onone
))
3343 if (current_lb_is_new
)
3344 switch_line_buffers ();
3346 else if (definedef
!= dnone
3347 || fvdef
== fdefunname
3349 make_C_tag (funorvar
);
3351 else /* not yacc and consider_token failed */
3353 if (inattribute
&& fvdef
== fignore
)
3355 /* We have just met __attribute__ after a
3356 function parameter list: do not tag the
3363 } /* if (endtoken (c)) */
3364 else if (intoken (c
))
3370 } /* if (midtoken) */
3371 else if (begtoken (c
))
3379 /* This prevents tagging fb in
3380 void (__attribute__((noreturn)) *fb) (void);
3381 Fixing this is not easy and not very important. */
3385 if (plainc
|| declarations
)
3387 make_C_tag (true); /* a function */
3392 if (structdef
== stagseen
&& !cjava
)
3394 popclass_above (bracelev
);
3402 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
3404 tokoff
= lp
- 1 - newlb
.buffer
;
3409 } /* if (begtoken) */
3410 } /* if must look at token */
3413 /* Detect end of line, colon, comma, semicolon and various braces
3414 after having handled a token.*/
3420 if (yacc_rules
&& token
.offset
== 0 && token
.valid
)
3422 make_C_tag (false); /* a yacc function */
3425 if (definedef
!= dnone
)
3431 make_C_tag (true); /* an Objective C class */
3435 objdef
= omethodcolon
;
3436 int toklen
= token_name
.len
;
3437 linebuffer_setlen (&token_name
, toklen
+ 1);
3438 strcpy (token_name
.buffer
+ toklen
, ":");
3441 if (structdef
== stagseen
)
3443 structdef
= scolonseen
;
3446 /* Should be useless, but may be work as a safety net. */
3447 if (cplpl
&& fvdef
== flistseen
)
3449 make_C_tag (true); /* a function */
3455 if (definedef
!= dnone
|| inattribute
)
3461 make_C_tag (false); /* a typedef */
3471 if (typdef
== tignore
|| cplpl
)
3475 if ((globals
&& bracelev
== 0 && (!fvextern
|| declarations
))
3476 || (members
&& instruct
))
3477 make_C_tag (false); /* a variable */
3480 token
.valid
= false;
3484 && (cplpl
|| !instruct
)
3485 && (typdef
== tnone
|| (typdef
!= tignore
&& instruct
)))
3487 && plainc
&& instruct
))
3488 make_C_tag (true); /* a function */
3494 && cplpl
&& structdef
== stagseen
)
3495 make_C_tag (false); /* forward declaration */
3497 token
.valid
= false;
3498 } /* switch (fvdef) */
3504 if (structdef
== stagseen
)
3508 if (definedef
!= dnone
|| inattribute
)
3514 make_C_tag (true); /* an Objective C method */
3527 if (instruct
&& parlev
== 0)
3538 && (!fvextern
|| declarations
))
3539 || (members
&& instruct
)))
3540 make_C_tag (false); /* a variable */
3543 if ((declarations
&& typdef
== tnone
&& !instruct
)
3544 || (members
&& typdef
!= tignore
&& instruct
))
3546 make_C_tag (true); /* a function */
3549 else if (!declarations
)
3551 token
.valid
= false;
3556 if (structdef
== stagseen
)
3560 if (definedef
!= dnone
|| inattribute
)
3562 if (structdef
== stagseen
)
3569 make_C_tag (false); /* a typedef */
3581 if ((members
&& bracelev
== 1)
3582 || (globals
&& bracelev
== 0
3583 && (!fvextern
|| declarations
)))
3584 make_C_tag (false); /* a variable */
3598 if (definedef
!= dnone
)
3600 if (objdef
== otagseen
&& parlev
== 0)
3601 objdef
= oparenseen
;
3605 if (typdef
== ttypeseen
3609 /* This handles constructs like:
3610 typedef void OperatorFun (int fun); */
3629 if (--attrparlev
== 0)
3630 inattribute
= false;
3633 if (definedef
!= dnone
)
3635 if (objdef
== ocatseen
&& parlev
== 1)
3637 make_C_tag (true); /* an Objective C category */
3651 || typdef
== ttypeseen
))
3654 make_C_tag (false); /* a typedef */
3657 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3661 if (definedef
!= dnone
)
3663 if (typdef
== ttypeseen
)
3665 /* Whenever typdef is set to tinbody (currently only
3666 here), typdefbracelev should be set to bracelev. */
3668 typdefbracelev
= bracelev
;
3673 make_C_tag (true); /* a function */
3682 make_C_tag (true); /* an Objective C class */
3687 make_C_tag (true); /* an Objective C method */
3691 /* Neutralize `extern "C" {' grot. */
3692 if (bracelev
== 0 && structdef
== snone
&& nestlev
== 0
3700 case skeyseen
: /* unnamed struct */
3701 pushclass_above (bracelev
, NULL
, 0);
3704 case stagseen
: /* named struct or enum */
3705 case scolonseen
: /* a class */
3706 pushclass_above (bracelev
,token
.line
+token
.offset
, token
.length
);
3708 make_C_tag (false); /* a struct or enum */
3714 if (definedef
!= dnone
)
3716 if (fvdef
== fstartlist
)
3718 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3719 token
.valid
= false;
3723 if (definedef
!= dnone
)
3726 if (!ignoreindent
&& lp
== newlb
.buffer
+ 1)
3729 token
.valid
= false; /* unexpected value, token unreliable */
3730 bracelev
= 0; /* reset brace level if first column */
3731 parlev
= 0; /* also reset paren level, just in case... */
3733 else if (bracelev
< 0)
3735 token
.valid
= false; /* something gone amiss, token unreliable */
3738 if (bracelev
== 0 && fvdef
== vignore
)
3739 fvdef
= fvnone
; /* end of function */
3740 popclass_above (bracelev
);
3742 /* Only if typdef == tinbody is typdefbracelev significant. */
3743 if (typdef
== tinbody
&& bracelev
<= typdefbracelev
)
3745 assert (bracelev
== typdefbracelev
);
3750 if (definedef
!= dnone
)
3760 if ((members
&& bracelev
== 1)
3761 || (globals
&& bracelev
== 0 && (!fvextern
|| declarations
)))
3762 make_C_tag (false); /* a variable */
3770 && (structdef
== stagseen
|| fvdef
== fvnameseen
))
3777 if (templatelev
> 0)
3785 if (objdef
== oinbody
&& bracelev
== 0)
3787 objdef
= omethodsign
;
3792 case '#': case '~': case '&': case '%': case '/':
3793 case '|': case '^': case '!': case '.': case '?':
3794 if (definedef
!= dnone
)
3796 /* These surely cannot follow a function tag in C. */
3809 if (objdef
== otagseen
)
3811 make_C_tag (true); /* an Objective C class */
3814 /* If a macro spans multiple lines don't reset its state. */
3816 CNL_SAVE_DEFINEDEF ();
3822 } /* while not eof */
3824 free (lbs
[0].lb
.buffer
);
3825 free (lbs
[1].lb
.buffer
);
3829 * Process either a C++ file or a C file depending on the setting
3833 default_C_entries (FILE *inf
)
3835 C_entries (cplusplus
? C_PLPL
: C_AUTO
, inf
);
3838 /* Always do plain C. */
3840 plain_C_entries (FILE *inf
)
3845 /* Always do C++. */
3847 Cplusplus_entries (FILE *inf
)
3849 C_entries (C_PLPL
, inf
);
3852 /* Always do Java. */
3854 Cjava_entries (FILE *inf
)
3856 C_entries (C_JAVA
, inf
);
3861 Cstar_entries (FILE *inf
)
3863 C_entries (C_STAR
, inf
);
3866 /* Always do Yacc. */
3868 Yacc_entries (FILE *inf
)
3870 C_entries (YACC
, inf
);
3874 /* Useful macros. */
3875 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3876 for (; /* loop initialization */ \
3877 !feof (file_pointer) /* loop test */ \
3878 && /* instructions at start of loop */ \
3879 (readline (&line_buffer, file_pointer), \
3880 char_pointer = line_buffer.buffer, \
3884 #define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \
3885 ((assert ("" kw), true) /* syntax error if not a literal string */ \
3886 && strneq ((cp), kw, sizeof (kw)-1) /* cp points at kw */ \
3887 && notinname ((cp)[sizeof (kw)-1]) /* end of kw */ \
3888 && ((cp) = skip_spaces ((cp)+sizeof (kw)-1))) /* skip spaces */
3890 /* Similar to LOOKING_AT but does not use notinname, does not skip */
3891 #define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \
3892 ((assert ("" kw), true) /* syntax error if not a literal string */ \
3893 && strncaseeq ((cp), kw, sizeof (kw)-1) /* cp points at kw */ \
3894 && ((cp) += sizeof (kw)-1)) /* skip spaces */
3897 * Read a file, but do no processing. This is used to do regexp
3898 * matching on files that have no language defined.
3901 just_read_file (FILE *inf
)
3904 readline (&lb
, inf
);
3908 /* Fortran parsing */
3910 static void F_takeprec (void);
3911 static void F_getit (FILE *);
3916 dbp
= skip_spaces (dbp
);
3920 dbp
= skip_spaces (dbp
);
3921 if (strneq (dbp
, "(*)", 3))
3926 if (!ISDIGIT (*dbp
))
3928 --dbp
; /* force failure */
3933 while (ISDIGIT (*dbp
));
3941 dbp
= skip_spaces (dbp
);
3944 readline (&lb
, inf
);
3949 dbp
= skip_spaces (dbp
);
3951 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
3953 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
3955 make_tag (dbp
, cp
-dbp
, true,
3956 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3961 Fortran_functions (FILE *inf
)
3963 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3966 dbp
++; /* Ratfor escape to fortran */
3967 dbp
= skip_spaces (dbp
);
3971 if (LOOKING_AT_NOCASE (dbp
, "recursive"))
3972 dbp
= skip_spaces (dbp
);
3974 if (LOOKING_AT_NOCASE (dbp
, "pure"))
3975 dbp
= skip_spaces (dbp
);
3977 if (LOOKING_AT_NOCASE (dbp
, "elemental"))
3978 dbp
= skip_spaces (dbp
);
3980 switch (lowcase (*dbp
))
3983 if (nocase_tail ("integer"))
3987 if (nocase_tail ("real"))
3991 if (nocase_tail ("logical"))
3995 if (nocase_tail ("complex") || nocase_tail ("character"))
3999 if (nocase_tail ("double"))
4001 dbp
= skip_spaces (dbp
);
4004 if (nocase_tail ("precision"))
4010 dbp
= skip_spaces (dbp
);
4013 switch (lowcase (*dbp
))
4016 if (nocase_tail ("function"))
4020 if (nocase_tail ("subroutine"))
4024 if (nocase_tail ("entry"))
4028 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
4030 dbp
= skip_spaces (dbp
);
4031 if (*dbp
== '\0') /* assume un-named */
4032 make_tag ("blockdata", 9, true,
4033 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
4035 F_getit (inf
); /* look for name */
4046 * Philippe Waroquiers (1998)
4049 /* Once we are positioned after an "interesting" keyword, let's get
4050 the real tag value necessary. */
4052 Ada_getit (FILE *inf
, const char *name_qualifier
)
4060 dbp
= skip_spaces (dbp
);
4062 || (dbp
[0] == '-' && dbp
[1] == '-'))
4064 readline (&lb
, inf
);
4067 switch (lowcase (*dbp
))
4070 if (nocase_tail ("body"))
4072 /* Skipping body of procedure body or package body or ....
4073 resetting qualifier to body instead of spec. */
4074 name_qualifier
= "/b";
4079 /* Skipping type of task type or protected type ... */
4080 if (nocase_tail ("type"))
4087 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
4092 dbp
= skip_spaces (dbp
);
4095 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
4103 name
= concat (dbp
, name_qualifier
, "");
4105 make_tag (name
, strlen (name
), true,
4106 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4115 Ada_funcs (FILE *inf
)
4117 bool inquote
= false;
4118 bool skip_till_semicolumn
= false;
4120 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4122 while (*dbp
!= '\0')
4124 /* Skip a string i.e. "abcd". */
4125 if (inquote
|| (*dbp
== '"'))
4127 dbp
= strchr (dbp
+ !inquote
, '"');
4132 continue; /* advance char */
4137 break; /* advance line */
4141 /* Skip comments. */
4142 if (dbp
[0] == '-' && dbp
[1] == '-')
4143 break; /* advance line */
4145 /* Skip character enclosed in single quote i.e. 'a'
4146 and skip single quote starting an attribute i.e. 'Image. */
4155 if (skip_till_semicolumn
)
4158 skip_till_semicolumn
= false;
4160 continue; /* advance char */
4163 /* Search for beginning of a token. */
4164 if (!begtoken (*dbp
))
4167 continue; /* advance char */
4170 /* We are at the beginning of a token. */
4171 switch (lowcase (*dbp
))
4174 if (!packages_only
&& nocase_tail ("function"))
4175 Ada_getit (inf
, "/f");
4177 break; /* from switch */
4178 continue; /* advance char */
4180 if (!packages_only
&& nocase_tail ("procedure"))
4181 Ada_getit (inf
, "/p");
4182 else if (nocase_tail ("package"))
4183 Ada_getit (inf
, "/s");
4184 else if (nocase_tail ("protected")) /* protected type */
4185 Ada_getit (inf
, "/t");
4187 break; /* from switch */
4188 continue; /* advance char */
4191 if (typedefs
&& !packages_only
&& nocase_tail ("use"))
4193 /* when tagging types, avoid tagging use type Pack.Typename;
4194 for this, we will skip everything till a ; */
4195 skip_till_semicolumn
= true;
4196 continue; /* advance char */
4200 if (!packages_only
&& nocase_tail ("task"))
4201 Ada_getit (inf
, "/k");
4202 else if (typedefs
&& !packages_only
&& nocase_tail ("type"))
4204 Ada_getit (inf
, "/t");
4205 while (*dbp
!= '\0')
4209 break; /* from switch */
4210 continue; /* advance char */
4213 /* Look for the end of the token. */
4214 while (!endtoken (*dbp
))
4217 } /* advance char */
4218 } /* advance line */
4223 * Unix and microcontroller assembly tag handling
4224 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
4225 * Idea by Bob Weiner, Motorola Inc. (1994)
4228 Asm_labels (FILE *inf
)
4232 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4234 /* If first char is alphabetic or one of [_.$], test for colon
4235 following identifier. */
4236 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4238 /* Read past label. */
4240 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4242 if (*cp
== ':' || iswhite (*cp
))
4243 /* Found end of label, so copy it and add it to the table. */
4244 make_tag (lb
.buffer
, cp
- lb
.buffer
, true,
4245 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4253 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
4254 * /^use constant[ \t\n]+[^ \t\n{=,;]+/
4255 * Perl variable names: /^(my|local).../
4256 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
4257 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
4258 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
4261 Perl_functions (FILE *inf
)
4263 char *package
= savestr ("main"); /* current package name */
4266 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4268 cp
= skip_spaces (cp
);
4270 if (LOOKING_AT (cp
, "package"))
4273 get_tag (cp
, &package
);
4275 else if (LOOKING_AT (cp
, "sub"))
4281 while (!notinname (*cp
))
4284 continue; /* nothing found */
4285 if ((pos
= strchr (sp
, ':')) != NULL
4286 && pos
< cp
&& pos
[1] == ':')
4287 /* The name is already qualified. */
4288 make_tag (sp
, cp
- sp
, true,
4289 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4293 char savechar
, *name
;
4297 name
= concat (package
, "::", sp
);
4299 make_tag (name
, strlen (name
), true,
4300 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4304 else if (LOOKING_AT (cp
, "use constant")
4305 || LOOKING_AT (cp
, "use constant::defer"))
4307 /* For hash style multi-constant like
4308 use constant { FOO => 123,
4310 only the first FOO is picked up. Parsing across the value
4311 expressions would be difficult in general, due to possible nested
4312 hashes, here-documents, etc. */
4314 cp
= skip_spaces (cp
+1);
4317 else if (globals
) /* only if we are tagging global vars */
4319 /* Skip a qualifier, if any. */
4320 bool qual
= LOOKING_AT (cp
, "my") || LOOKING_AT (cp
, "local");
4321 /* After "my" or "local", but before any following paren or space. */
4322 char *varstart
= cp
;
4324 if (qual
/* should this be removed? If yes, how? */
4325 && (*cp
== '$' || *cp
== '@' || *cp
== '%'))
4330 while (ISALNUM (*cp
) || *cp
== '_');
4334 /* Should be examining a variable list at this point;
4335 could insist on seeing an open parenthesis. */
4336 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
4342 make_tag (varstart
, cp
- varstart
, false,
4343 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4352 * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4353 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4354 * More ideas by seb bacon <seb@jamkit.com> (2002)
4357 Python_functions (FILE *inf
)
4361 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4363 cp
= skip_spaces (cp
);
4364 if (LOOKING_AT (cp
, "def") || LOOKING_AT (cp
, "class"))
4367 while (!notinname (*cp
) && *cp
!= ':')
4369 make_tag (name
, cp
- name
, true,
4370 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4379 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4380 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4381 * - /^[ \t]*define\(\"[^\"]+/
4382 * Only with --members:
4383 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4384 * Idea by Diez B. Roggisch (2001)
4387 PHP_functions (FILE *inf
)
4390 bool search_identifier
= false;
4392 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4394 cp
= skip_spaces (cp
);
4396 if (search_identifier
4399 while (!notinname (*cp
))
4401 make_tag (name
, cp
- name
, true,
4402 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4403 search_identifier
= false;
4405 else if (LOOKING_AT (cp
, "function"))
4408 cp
= skip_spaces (cp
+1);
4412 while (!notinname (*cp
))
4414 make_tag (name
, cp
- name
, true,
4415 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4418 search_identifier
= true;
4420 else if (LOOKING_AT (cp
, "class"))
4425 while (*cp
!= '\0' && !iswhite (*cp
))
4427 make_tag (name
, cp
- name
, false,
4428 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4431 search_identifier
= true;
4433 else if (strneq (cp
, "define", 6)
4434 && (cp
= skip_spaces (cp
+6))
4436 && (*cp
== '"' || *cp
== '\''))
4440 while (*cp
!= quote
&& *cp
!= '\0')
4442 make_tag (name
, cp
- name
, false,
4443 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4446 && LOOKING_AT (cp
, "var")
4450 while (!notinname (*cp
))
4452 make_tag (name
, cp
- name
, false,
4453 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4460 * Cobol tag functions
4461 * We could look for anything that could be a paragraph name.
4462 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4463 * Idea by Corny de Souza (1993)
4466 Cobol_paragraphs (FILE *inf
)
4468 register char *bp
, *ep
;
4470 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4476 /* If eoln, compiler option or comment ignore whole line. */
4477 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
4480 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
4483 make_tag (bp
, ep
- bp
, true,
4484 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4491 * Ideas by Assar Westerlund <assar@sics.se> (2001)
4494 Makefile_targets (FILE *inf
)
4498 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4500 if (*bp
== '\t' || *bp
== '#')
4502 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
4504 if (*bp
== ':' || (globals
&& *bp
== '='))
4506 /* We should detect if there is more than one tag, but we do not.
4507 We just skip initial and final spaces. */
4508 char * namestart
= skip_spaces (lb
.buffer
);
4509 while (--bp
> namestart
)
4510 if (!notinname (*bp
))
4512 make_tag (namestart
, bp
- namestart
+ 1, true,
4513 lb
.buffer
, bp
- lb
.buffer
+ 2, lineno
, linecharno
);
4521 * Original code by Mosur K. Mohan (1989)
4523 * Locates tags for procedures & functions. Doesn't do any type- or
4524 * var-definitions. It does look for the keyword "extern" or
4525 * "forward" immediately following the procedure statement; if found,
4526 * the tag is skipped.
4529 Pascal_functions (FILE *inf
)
4531 linebuffer tline
; /* mostly copied from C_entries */
4533 int save_lineno
, namelen
, taglen
;
4536 bool /* each of these flags is true if: */
4537 incomment
, /* point is inside a comment */
4538 inquote
, /* point is inside '..' string */
4539 get_tagname
, /* point is after PROCEDURE/FUNCTION
4540 keyword, so next item = potential tag */
4541 found_tag
, /* point is after a potential tag */
4542 inparms
, /* point is within parameter-list */
4543 verify_tag
; /* point has passed the parm-list, so the
4544 next token will determine whether this
4545 is a FORWARD/EXTERN to be ignored, or
4546 whether it is a real tag */
4548 save_lcno
= save_lineno
= namelen
= taglen
= 0; /* keep compiler quiet */
4549 name
= NULL
; /* keep compiler quiet */
4552 linebuffer_init (&tline
);
4554 incomment
= inquote
= false;
4555 found_tag
= false; /* have a proc name; check if extern */
4556 get_tagname
= false; /* found "procedure" keyword */
4557 inparms
= false; /* found '(' after "proc" */
4558 verify_tag
= false; /* check if "extern" is ahead */
4561 while (!feof (inf
)) /* long main loop to get next char */
4564 if (c
== '\0') /* if end of line */
4566 readline (&lb
, inf
);
4570 if (!((found_tag
&& verify_tag
)
4572 c
= *dbp
++; /* only if don't need *dbp pointing
4573 to the beginning of the name of
4574 the procedure or function */
4578 if (c
== '}') /* within { } comments */
4580 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4597 inquote
= true; /* found first quote */
4599 case '{': /* found open { comment */
4603 if (*dbp
== '*') /* found open (* comment */
4608 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4611 case ')': /* end of parms list */
4616 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4623 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4625 /* Check if this is an "extern" declaration. */
4628 if (lowcase (*dbp
) == 'e')
4630 if (nocase_tail ("extern")) /* superfluous, really! */
4636 else if (lowcase (*dbp
) == 'f')
4638 if (nocase_tail ("forward")) /* check for forward reference */
4644 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4648 make_tag (name
, namelen
, true,
4649 tline
.buffer
, taglen
, save_lineno
, save_lcno
);
4653 if (get_tagname
) /* grab name of proc or fn */
4660 /* Find block name. */
4661 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4664 /* Save all values for later tagging. */
4665 linebuffer_setlen (&tline
, lb
.len
);
4666 strcpy (tline
.buffer
, lb
.buffer
);
4667 save_lineno
= lineno
;
4668 save_lcno
= linecharno
;
4669 name
= tline
.buffer
+ (dbp
- lb
.buffer
);
4671 taglen
= cp
- lb
.buffer
+ 1;
4673 dbp
= cp
; /* set dbp to e-o-token */
4674 get_tagname
= false;
4678 /* And proceed to check for "extern". */
4680 else if (!incomment
&& !inquote
&& !found_tag
)
4682 /* Check for proc/fn keywords. */
4683 switch (lowcase (c
))
4686 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4690 if (nocase_tail ("unction"))
4695 } /* while not eof */
4697 free (tline
.buffer
);
4702 * Lisp tag functions
4703 * look for (def or (DEF, quote or QUOTE
4706 static void L_getit (void);
4711 if (*dbp
== '\'') /* Skip prefix quote */
4713 else if (*dbp
== '(')
4716 /* Try to skip "(quote " */
4717 if (!LOOKING_AT (dbp
, "quote") && !LOOKING_AT (dbp
, "QUOTE"))
4718 /* Ok, then skip "(" before name in (defstruct (foo)) */
4719 dbp
= skip_spaces (dbp
);
4721 get_tag (dbp
, NULL
);
4725 Lisp_functions (FILE *inf
)
4727 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4732 /* "(defvar foo)" is a declaration rather than a definition. */
4736 if (LOOKING_AT (p
, "defvar"))
4738 p
= skip_name (p
); /* past var name */
4739 p
= skip_spaces (p
);
4745 if (strneq (dbp
+ 1, "cl-", 3) || strneq (dbp
+ 1, "CL-", 3))
4748 if (strneq (dbp
+1, "def", 3) || strneq (dbp
+1, "DEF", 3))
4750 dbp
= skip_non_spaces (dbp
);
4751 dbp
= skip_spaces (dbp
);
4756 /* Check for (foo::defmumble name-defined ... */
4759 while (!notinname (*dbp
) && *dbp
!= ':');
4764 while (*dbp
== ':');
4766 if (strneq (dbp
, "def", 3) || strneq (dbp
, "DEF", 3))
4768 dbp
= skip_non_spaces (dbp
);
4769 dbp
= skip_spaces (dbp
);
4779 * Lua script language parsing
4780 * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
4782 * "function" and "local function" are tags if they start at column 1.
4785 Lua_functions (FILE *inf
)
4789 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4791 if (bp
[0] != 'f' && bp
[0] != 'l')
4794 (void)LOOKING_AT (bp
, "local"); /* skip possible "local" */
4796 if (LOOKING_AT (bp
, "function"))
4804 * Just look for lines where the first character is '/'
4805 * Also look at "defineps" for PSWrap
4807 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4808 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4811 PS_functions (FILE *inf
)
4813 register char *bp
, *ep
;
4815 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4820 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4823 make_tag (bp
, ep
- bp
, true,
4824 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4826 else if (LOOKING_AT (bp
, "defineps"))
4834 * Ignore anything after \ followed by space or in ( )
4835 * Look for words defined by :
4836 * Look for constant, code, create, defer, value, and variable
4837 * OBP extensions: Look for buffer:, field,
4838 * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004)
4841 Forth_words (FILE *inf
)
4845 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4846 while ((bp
= skip_spaces (bp
))[0] != '\0')
4847 if (bp
[0] == '\\' && iswhite (bp
[1]))
4848 break; /* read next line */
4849 else if (bp
[0] == '(' && iswhite (bp
[1]))
4850 do /* skip to ) or eol */
4852 while (*bp
!= ')' && *bp
!= '\0');
4853 else if ((bp
[0] == ':' && iswhite (bp
[1]) && bp
++)
4854 || LOOKING_AT_NOCASE (bp
, "constant")
4855 || LOOKING_AT_NOCASE (bp
, "code")
4856 || LOOKING_AT_NOCASE (bp
, "create")
4857 || LOOKING_AT_NOCASE (bp
, "defer")
4858 || LOOKING_AT_NOCASE (bp
, "value")
4859 || LOOKING_AT_NOCASE (bp
, "variable")
4860 || LOOKING_AT_NOCASE (bp
, "buffer:")
4861 || LOOKING_AT_NOCASE (bp
, "field"))
4862 get_tag (skip_spaces (bp
), NULL
); /* Yay! A definition! */
4864 bp
= skip_non_spaces (bp
);
4869 * Scheme tag functions
4870 * look for (def... xyzzy
4872 * (def ... ((...(xyzzy ....
4874 * Original code by Ken Haase (1985?)
4877 Scheme_functions (FILE *inf
)
4881 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4883 if (strneq (bp
, "(def", 4) || strneq (bp
, "(DEF", 4))
4885 bp
= skip_non_spaces (bp
+4);
4886 /* Skip over open parens and white space. Don't continue past
4888 while (*bp
&& notinname (*bp
))
4892 if (LOOKING_AT (bp
, "(SET!") || LOOKING_AT (bp
, "(set!"))
4898 /* Find tags in TeX and LaTeX input files. */
4900 /* TEX_toktab is a table of TeX control sequences that define tags.
4901 * Each entry records one such control sequence.
4903 * Original code from who knows whom.
4905 * Stefan Monnier (2002)
4908 static linebuffer
*TEX_toktab
= NULL
; /* Table with tag tokens */
4910 /* Default set of control sequences to put into TEX_toktab.
4911 The value of environment var TEXTAGS is prepended to this. */
4912 static const char *TEX_defenv
= "\
4913 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4914 :part:appendix:entry:index:def\
4915 :newcommand:renewcommand:newenvironment:renewenvironment";
4917 static void TEX_mode (FILE *);
4918 static void TEX_decode_env (const char *, const char *);
4920 static char TEX_esc
= '\\';
4921 static char TEX_opgrp
= '{';
4922 static char TEX_clgrp
= '}';
4925 * TeX/LaTeX scanning loop.
4928 TeX_commands (FILE *inf
)
4933 /* Select either \ or ! as escape character. */
4936 /* Initialize token table once from environment. */
4937 if (TEX_toktab
== NULL
)
4938 TEX_decode_env ("TEXTAGS", TEX_defenv
);
4940 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4942 /* Look at each TEX keyword in line. */
4945 /* Look for a TEX escape. */
4946 while (*cp
++ != TEX_esc
)
4947 if (cp
[-1] == '\0' || cp
[-1] == '%')
4950 for (key
= TEX_toktab
; key
->buffer
!= NULL
; key
++)
4951 if (strneq (cp
, key
->buffer
, key
->len
))
4954 int namelen
, linelen
;
4957 cp
= skip_spaces (cp
+ key
->len
);
4958 if (*cp
== TEX_opgrp
)
4964 (!iswhite (*p
) && *p
!= '#' &&
4965 *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
);
4970 if (!opgrp
|| *p
== TEX_clgrp
)
4972 while (*p
!= '\0' && *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
)
4974 linelen
= p
- lb
.buffer
+ 1;
4976 make_tag (cp
, namelen
, true,
4977 lb
.buffer
, linelen
, lineno
, linecharno
);
4978 goto tex_next_line
; /* We only tag a line once */
4986 #define TEX_LESC '\\'
4987 #define TEX_SESC '!'
4989 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
4990 chars accordingly. */
4992 TEX_mode (FILE *inf
)
4996 while ((c
= getc (inf
)) != EOF
)
4998 /* Skip to next line if we hit the TeX comment char. */
5000 while (c
!= '\n' && c
!= EOF
)
5002 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
5018 /* If the input file is compressed, inf is a pipe, and rewind may fail.
5019 No attempt is made to correct the situation. */
5023 /* Read environment and prepend it to the default string.
5024 Build token table. */
5026 TEX_decode_env (const char *evarname
, const char *defenv
)
5028 register const char *env
, *p
;
5031 /* Append default string to environment. */
5032 env
= getenv (evarname
);
5036 env
= concat (env
, defenv
, "");
5038 /* Allocate a token table */
5039 for (len
= 1, p
= env
; p
;)
5040 if ((p
= strchr (p
, ':')) && *++p
!= '\0')
5042 TEX_toktab
= xnew (len
, linebuffer
);
5044 /* Unpack environment string into token table. Be careful about */
5045 /* zero-length strings (leading ':', "::" and trailing ':') */
5046 for (i
= 0; *env
!= '\0';)
5048 p
= strchr (env
, ':');
5049 if (!p
) /* End of environment string. */
5050 p
= env
+ strlen (env
);
5052 { /* Only non-zero strings. */
5053 TEX_toktab
[i
].buffer
= savenstr (env
, p
- env
);
5054 TEX_toktab
[i
].len
= p
- env
;
5061 TEX_toktab
[i
].buffer
= NULL
; /* Mark end of table. */
5062 TEX_toktab
[i
].len
= 0;
5069 /* Texinfo support. Dave Love, Mar. 2000. */
5071 Texinfo_nodes (FILE *inf
)
5074 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5075 if (LOOKING_AT (cp
, "@node"))
5078 while (*cp
!= '\0' && *cp
!= ',')
5080 make_tag (start
, cp
- start
, true,
5081 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5088 * Contents of <title>, <h1>, <h2>, <h3> are tags.
5089 * Contents of <a name=xxx> are tags with name xxx.
5091 * Francesco Potortì, 2002.
5094 HTML_labels (FILE *inf
)
5096 bool getnext
= false; /* next text outside of HTML tags is a tag */
5097 bool skiptag
= false; /* skip to the end of the current HTML tag */
5098 bool intag
= false; /* inside an html tag, looking for ID= */
5099 bool inanchor
= false; /* when INTAG, is an anchor, look for NAME= */
5103 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5105 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
5106 for (;;) /* loop on the same line */
5108 if (skiptag
) /* skip HTML tag */
5110 while (*dbp
!= '\0' && *dbp
!= '>')
5116 continue; /* look on the same line */
5118 break; /* go to next line */
5121 else if (intag
) /* look for "name=" or "id=" */
5123 while (*dbp
!= '\0' && *dbp
!= '>'
5124 && lowcase (*dbp
) != 'n' && lowcase (*dbp
) != 'i')
5127 break; /* go to next line */
5132 continue; /* look on the same line */
5134 if ((inanchor
&& LOOKING_AT_NOCASE (dbp
, "name="))
5135 || LOOKING_AT_NOCASE (dbp
, "id="))
5137 bool quoted
= (dbp
[0] == '"');
5140 for (end
= ++dbp
; *end
!= '\0' && *end
!= '"'; end
++)
5143 for (end
= dbp
; *end
!= '\0' && intoken (*end
); end
++)
5145 linebuffer_setlen (&token_name
, end
- dbp
);
5146 memcpy (token_name
.buffer
, dbp
, end
- dbp
);
5147 token_name
.buffer
[end
- dbp
] = '\0';
5150 intag
= false; /* we found what we looked for */
5151 skiptag
= true; /* skip to the end of the tag */
5152 getnext
= true; /* then grab the text */
5153 continue; /* look on the same line */
5158 else if (getnext
) /* grab next tokens and tag them */
5160 dbp
= skip_spaces (dbp
);
5162 break; /* go to next line */
5166 inanchor
= (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]));
5167 continue; /* look on the same line */
5170 for (end
= dbp
+ 1; *end
!= '\0' && *end
!= '<'; end
++)
5172 make_tag (token_name
.buffer
, token_name
.len
, true,
5173 dbp
, end
- dbp
, lineno
, linecharno
);
5174 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5176 break; /* go to next line */
5179 else /* look for an interesting HTML tag */
5181 while (*dbp
!= '\0' && *dbp
!= '<')
5184 break; /* go to next line */
5186 if (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]))
5189 continue; /* look on the same line */
5191 else if (LOOKING_AT_NOCASE (dbp
, "<title>")
5192 || LOOKING_AT_NOCASE (dbp
, "<h1>")
5193 || LOOKING_AT_NOCASE (dbp
, "<h2>")
5194 || LOOKING_AT_NOCASE (dbp
, "<h3>"))
5198 continue; /* look on the same line */
5209 * Assumes that the predicate or rule starts at column 0.
5210 * Only the first clause of a predicate or rule is added.
5211 * Original code by Sunichirou Sugou (1989)
5212 * Rewritten by Anders Lindgren (1996)
5214 static size_t prolog_pr (char *, char *);
5215 static void prolog_skip_comment (linebuffer
*, FILE *);
5216 static size_t prolog_atom (char *, size_t);
5219 Prolog_functions (FILE *inf
)
5229 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5231 if (cp
[0] == '\0') /* Empty line */
5233 else if (iswhite (cp
[0])) /* Not a predicate */
5235 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
5236 prolog_skip_comment (&lb
, inf
);
5237 else if ((len
= prolog_pr (cp
, last
)) > 0)
5239 /* Predicate or rule. Store the function name so that we
5240 only generate a tag for the first clause. */
5242 last
= xnew (len
+ 1, char);
5243 else if (len
+ 1 > allocated
)
5244 xrnew (last
, len
+ 1, char);
5245 allocated
= len
+ 1;
5246 memcpy (last
, cp
, len
);
5255 prolog_skip_comment (linebuffer
*plb
, FILE *inf
)
5261 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
5262 if (cp
[0] == '*' && cp
[1] == '/')
5264 readline (plb
, inf
);
5266 while (!feof (inf
));
5270 * A predicate or rule definition is added if it matches:
5271 * <beginning of line><Prolog Atom><whitespace>(
5272 * or <beginning of line><Prolog Atom><whitespace>:-
5274 * It is added to the tags database if it doesn't match the
5275 * name of the previous clause header.
5277 * Return the size of the name of the predicate or rule, or 0 if no
5281 prolog_pr (char *s
, char *last
)
5283 /* Name of last clause. */
5288 pos
= prolog_atom (s
, 0);
5293 pos
= skip_spaces (s
+ pos
) - s
;
5296 || (s
[pos
] == '(' && (pos
+= 1))
5297 || (s
[pos
] == ':' && s
[pos
+ 1] == '-' && (pos
+= 2)))
5298 && (last
== NULL
/* save only the first clause */
5299 || len
!= strlen (last
)
5300 || !strneq (s
, last
, len
)))
5302 make_tag (s
, len
, true, s
, pos
, lineno
, linecharno
);
5310 * Consume a Prolog atom.
5311 * Return the number of bytes consumed, or 0 if there was an error.
5313 * A prolog atom, in this context, could be one of:
5314 * - An alphanumeric sequence, starting with a lower case letter.
5315 * - A quoted arbitrary string. Single quotes can escape themselves.
5316 * Backslash quotes everything.
5319 prolog_atom (char *s
, size_t pos
)
5325 if (ISLOWER (s
[pos
]) || (s
[pos
] == '_'))
5327 /* The atom is unquoted. */
5329 while (ISALNUM (s
[pos
]) || (s
[pos
] == '_'))
5333 return pos
- origpos
;
5335 else if (s
[pos
] == '\'')
5346 pos
++; /* A double quote */
5348 else if (s
[pos
] == '\0')
5349 /* Multiline quoted atoms are ignored. */
5351 else if (s
[pos
] == '\\')
5353 if (s
[pos
+1] == '\0')
5360 return pos
- origpos
;
5368 * Support for Erlang
5370 * Generates tags for functions, defines, and records.
5371 * Assumes that Erlang functions start at column 0.
5372 * Original code by Anders Lindgren (1996)
5374 static int erlang_func (char *, char *);
5375 static void erlang_attribute (char *);
5376 static int erlang_atom (char *);
5379 Erlang_functions (FILE *inf
)
5389 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5391 if (cp
[0] == '\0') /* Empty line */
5393 else if (iswhite (cp
[0])) /* Not function nor attribute */
5395 else if (cp
[0] == '%') /* comment */
5397 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
5399 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
5401 erlang_attribute (cp
);
5408 else if ((len
= erlang_func (cp
, last
)) > 0)
5411 * Function. Store the function name so that we only
5412 * generates a tag for the first clause.
5415 last
= xnew (len
+ 1, char);
5416 else if (len
+ 1 > allocated
)
5417 xrnew (last
, len
+ 1, char);
5418 allocated
= len
+ 1;
5419 memcpy (last
, cp
, len
);
5428 * A function definition is added if it matches:
5429 * <beginning of line><Erlang Atom><whitespace>(
5431 * It is added to the tags database if it doesn't match the
5432 * name of the previous clause header.
5434 * Return the size of the name of the function, or 0 if no function
5438 erlang_func (char *s
, char *last
)
5440 /* Name of last clause. */
5445 pos
= erlang_atom (s
);
5450 pos
= skip_spaces (s
+ pos
) - s
;
5452 /* Save only the first clause. */
5455 || len
!= (int)strlen (last
)
5456 || !strneq (s
, last
, len
)))
5458 make_tag (s
, len
, true, s
, pos
, lineno
, linecharno
);
5467 * Handle attributes. Currently, tags are generated for defines
5470 * They are on the form:
5471 * -define(foo, bar).
5472 * -define(Foo(M, N), M+N).
5473 * -record(graph, {vtab = notable, cyclic = true}).
5476 erlang_attribute (char *s
)
5480 if ((LOOKING_AT (cp
, "-define") || LOOKING_AT (cp
, "-record"))
5483 int len
= erlang_atom (skip_spaces (cp
));
5485 make_tag (cp
, len
, true, s
, cp
+ len
- s
, lineno
, linecharno
);
5492 * Consume an Erlang atom (or variable).
5493 * Return the number of bytes consumed, or -1 if there was an error.
5496 erlang_atom (char *s
)
5500 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
5502 /* The atom is unquoted. */
5505 while (ISALNUM (s
[pos
]) || s
[pos
] == '_');
5507 else if (s
[pos
] == '\'')
5509 for (pos
++; s
[pos
] != '\''; pos
++)
5510 if (s
[pos
] == '\0' /* multiline quoted atoms are ignored */
5511 || (s
[pos
] == '\\' && s
[++pos
] == '\0'))
5520 static char *scan_separators (char *);
5521 static void add_regex (char *, language
*);
5522 static char *substitute (char *, char *, struct re_registers
*);
5525 * Take a string like "/blah/" and turn it into "blah", verifying
5526 * that the first and last characters are the same, and handling
5527 * quoted separator characters. Actually, stops on the occurrence of
5528 * an unquoted separator. Also process \t, \n, etc. and turn into
5529 * appropriate characters. Works in place. Null terminates name string.
5530 * Returns pointer to terminating separator, or NULL for
5531 * unterminated regexps.
5534 scan_separators (char *name
)
5537 char *copyto
= name
;
5538 bool quoted
= false;
5540 for (++name
; *name
!= '\0'; ++name
)
5546 case 'a': *copyto
++ = '\007'; break; /* BEL (bell) */
5547 case 'b': *copyto
++ = '\b'; break; /* BS (back space) */
5548 case 'd': *copyto
++ = 0177; break; /* DEL (delete) */
5549 case 'e': *copyto
++ = 033; break; /* ESC (delete) */
5550 case 'f': *copyto
++ = '\f'; break; /* FF (form feed) */
5551 case 'n': *copyto
++ = '\n'; break; /* NL (new line) */
5552 case 'r': *copyto
++ = '\r'; break; /* CR (carriage return) */
5553 case 't': *copyto
++ = '\t'; break; /* TAB (horizontal tab) */
5554 case 'v': *copyto
++ = '\v'; break; /* VT (vertical tab) */
5560 /* Something else is quoted, so preserve the quote. */
5568 else if (*name
== '\\')
5570 else if (*name
== sep
)
5576 name
= NULL
; /* signal unterminated regexp */
5578 /* Terminate copied string. */
5583 /* Look at the argument of --regex or --no-regex and do the right
5584 thing. Same for each line of a regexp file. */
5586 analyze_regex (char *regex_arg
)
5588 if (regex_arg
== NULL
)
5590 free_regexps (); /* --no-regex: remove existing regexps */
5594 /* A real --regexp option or a line in a regexp file. */
5595 switch (regex_arg
[0])
5597 /* Comments in regexp file or null arg to --regex. */
5603 /* Read a regex file. This is recursive and may result in a
5604 loop, which will stop when the file descriptors are exhausted. */
5608 linebuffer regexbuf
;
5609 char *regexfile
= regex_arg
+ 1;
5611 /* regexfile is a file containing regexps, one per line. */
5612 regexfp
= fopen (regexfile
, "r" FOPEN_BINARY
);
5613 if (regexfp
== NULL
)
5615 linebuffer_init (®exbuf
);
5616 while (readline_internal (®exbuf
, regexfp
) > 0)
5617 analyze_regex (regexbuf
.buffer
);
5618 free (regexbuf
.buffer
);
5623 /* Regexp to be used for a specific language only. */
5627 char *lang_name
= regex_arg
+ 1;
5630 for (cp
= lang_name
; *cp
!= '}'; cp
++)
5633 error ("unterminated language name in regex: %s", regex_arg
);
5637 lang
= get_language_from_langname (lang_name
);
5640 add_regex (cp
, lang
);
5644 /* Regexp to be used for any language. */
5646 add_regex (regex_arg
, NULL
);
5651 /* Separate the regexp pattern, compile it,
5652 and care for optional name and modifiers. */
5654 add_regex (char *regexp_pattern
, language
*lang
)
5656 static struct re_pattern_buffer zeropattern
;
5657 char sep
, *pat
, *name
, *modifiers
;
5660 struct re_pattern_buffer
*patbuf
;
5663 force_explicit_name
= true, /* do not use implicit tag names */
5664 ignore_case
= false, /* case is significant */
5665 multi_line
= false, /* matches are done one line at a time */
5666 single_line
= false; /* dot does not match newline */
5669 if (strlen (regexp_pattern
) < 3)
5671 error ("null regexp");
5674 sep
= regexp_pattern
[0];
5675 name
= scan_separators (regexp_pattern
);
5678 error ("%s: unterminated regexp", regexp_pattern
);
5683 error ("null name for regexp \"%s\"", regexp_pattern
);
5686 modifiers
= scan_separators (name
);
5687 if (modifiers
== NULL
) /* no terminating separator --> no name */
5693 modifiers
+= 1; /* skip separator */
5695 /* Parse regex modifiers. */
5696 for (; modifiers
[0] != '\0'; modifiers
++)
5697 switch (modifiers
[0])
5700 if (modifiers
== name
)
5701 error ("forcing explicit tag name but no name, ignoring");
5702 force_explicit_name
= true;
5712 need_filebuf
= true;
5715 error ("invalid regexp modifier `%c', ignoring", modifiers
[0]);
5719 patbuf
= xnew (1, struct re_pattern_buffer
);
5720 *patbuf
= zeropattern
;
5723 static char lc_trans
[CHARS
];
5725 for (i
= 0; i
< CHARS
; i
++)
5726 lc_trans
[i
] = lowcase (i
);
5727 patbuf
->translate
= lc_trans
; /* translation table to fold case */
5731 pat
= concat ("^", regexp_pattern
, ""); /* anchor to beginning of line */
5733 pat
= regexp_pattern
;
5736 re_set_syntax (RE_SYNTAX_EMACS
| RE_DOT_NEWLINE
);
5738 re_set_syntax (RE_SYNTAX_EMACS
);
5740 err
= re_compile_pattern (pat
, strlen (pat
), patbuf
);
5745 error ("%s while compiling pattern", err
);
5750 p_head
= xnew (1, regexp
);
5751 p_head
->pattern
= savestr (regexp_pattern
);
5752 p_head
->p_next
= rp
;
5753 p_head
->lang
= lang
;
5754 p_head
->pat
= patbuf
;
5755 p_head
->name
= savestr (name
);
5756 p_head
->error_signaled
= false;
5757 p_head
->force_explicit_name
= force_explicit_name
;
5758 p_head
->ignore_case
= ignore_case
;
5759 p_head
->multi_line
= multi_line
;
5763 * Do the substitutions indicated by the regular expression and
5767 substitute (char *in
, char *out
, struct re_registers
*regs
)
5770 int size
, dig
, diglen
;
5773 size
= strlen (out
);
5775 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5776 if (out
[size
- 1] == '\\')
5777 fatal ("pattern error in \"%s\"", out
);
5778 for (t
= strchr (out
, '\\');
5780 t
= strchr (t
+ 2, '\\'))
5784 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5790 /* Allocate space and do the substitutions. */
5792 result
= xnew (size
+ 1, char);
5794 for (t
= result
; *out
!= '\0'; out
++)
5795 if (*out
== '\\' && ISDIGIT (*++out
))
5798 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5799 memcpy (t
, in
+ regs
->start
[dig
], diglen
);
5806 assert (t
<= result
+ size
);
5807 assert (t
- result
== (int)strlen (result
));
5812 /* Deallocate all regexps. */
5817 while (p_head
!= NULL
)
5819 rp
= p_head
->p_next
;
5820 free (p_head
->pattern
);
5821 free (p_head
->name
);
5829 * Reads the whole file as a single string from `filebuf' and looks for
5830 * multi-line regular expressions, creating tags on matches.
5831 * readline already dealt with normal regexps.
5833 * Idea by Ben Wing <ben@666.com> (2002).
5836 regex_tag_multiline (void)
5838 char *buffer
= filebuf
.buffer
;
5842 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
5846 if (!rp
->multi_line
)
5847 continue; /* skip normal regexps */
5849 /* Generic initializations before parsing file from memory. */
5850 lineno
= 1; /* reset global line number */
5851 charno
= 0; /* reset global char number */
5852 linecharno
= 0; /* reset global char number of line start */
5854 /* Only use generic regexps or those for the current language. */
5855 if (rp
->lang
!= NULL
&& rp
->lang
!= curfdp
->lang
)
5858 while (match
>= 0 && match
< filebuf
.len
)
5860 match
= re_search (rp
->pat
, buffer
, filebuf
.len
, charno
,
5861 filebuf
.len
- match
, &rp
->regs
);
5866 if (!rp
->error_signaled
)
5868 error ("regexp stack overflow while matching \"%s\"",
5870 rp
->error_signaled
= true;
5877 if (match
== rp
->regs
.end
[0])
5879 if (!rp
->error_signaled
)
5881 error ("regexp matches the empty string: \"%s\"",
5883 rp
->error_signaled
= true;
5885 match
= -3; /* exit from while loop */
5889 /* Match occurred. Construct a tag. */
5890 while (charno
< rp
->regs
.end
[0])
5891 if (buffer
[charno
++] == '\n')
5892 lineno
++, linecharno
= charno
;
5894 if (name
[0] == '\0')
5896 else /* make a named tag */
5897 name
= substitute (buffer
, rp
->name
, &rp
->regs
);
5898 if (rp
->force_explicit_name
)
5899 /* Force explicit tag name, if a name is there. */
5900 pfnote (name
, true, buffer
+ linecharno
,
5901 charno
- linecharno
+ 1, lineno
, linecharno
);
5903 make_tag (name
, strlen (name
), true, buffer
+ linecharno
,
5904 charno
- linecharno
+ 1, lineno
, linecharno
);
5913 nocase_tail (const char *cp
)
5915 register int len
= 0;
5917 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
5919 if (*cp
== '\0' && !intoken (dbp
[len
]))
5928 get_tag (register char *bp
, char **namepp
)
5930 register char *cp
= bp
;
5934 /* Go till you get to white space or a syntactic break */
5935 for (cp
= bp
+ 1; !notinname (*cp
); cp
++)
5937 make_tag (bp
, cp
- bp
, true,
5938 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5942 *namepp
= savenstr (bp
, cp
- bp
);
5946 * Read a line of text from `stream' into `lbp', excluding the
5947 * newline or CR-NL, if any. Return the number of characters read from
5948 * `stream', which is the length of the line including the newline.
5950 * On DOS or Windows we do not count the CR character, if any before the
5951 * NL, in the returned length; this mirrors the behavior of Emacs on those
5952 * platforms (for text files, it translates CR-NL to NL as it reads in the
5955 * If multi-line regular expressions are requested, each line read is
5956 * appended to `filebuf'.
5959 readline_internal (linebuffer
*lbp
, register FILE *stream
)
5961 char *buffer
= lbp
->buffer
;
5962 register char *p
= lbp
->buffer
;
5963 register char *pend
;
5966 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
5970 register int c
= getc (stream
);
5973 /* We're at the end of linebuffer: expand it. */
5975 xrnew (buffer
, lbp
->size
, char);
5976 p
+= buffer
- lbp
->buffer
;
5977 pend
= buffer
+ lbp
->size
;
5978 lbp
->buffer
= buffer
;
5988 if (p
> buffer
&& p
[-1] == '\r')
5992 /* Assume CRLF->LF translation will be performed by Emacs
5993 when loading this file, so CRs won't appear in the buffer.
5994 It would be cleaner to compensate within Emacs;
5995 however, Emacs does not know how many CRs were deleted
5996 before any given point in the file. */
6011 lbp
->len
= p
- buffer
;
6013 if (need_filebuf
/* we need filebuf for multi-line regexps */
6014 && chars_deleted
> 0) /* not at EOF */
6016 while (filebuf
.size
<= filebuf
.len
+ lbp
->len
+ 1) /* +1 for \n */
6018 /* Expand filebuf. */
6020 xrnew (filebuf
.buffer
, filebuf
.size
, char);
6022 memcpy (filebuf
.buffer
+ filebuf
.len
, lbp
->buffer
, lbp
->len
);
6023 filebuf
.len
+= lbp
->len
;
6024 filebuf
.buffer
[filebuf
.len
++] = '\n';
6025 filebuf
.buffer
[filebuf
.len
] = '\0';
6028 return lbp
->len
+ chars_deleted
;
6032 * Like readline_internal, above, but in addition try to match the
6033 * input line against relevant regular expressions and manage #line
6037 readline (linebuffer
*lbp
, FILE *stream
)
6041 linecharno
= charno
; /* update global char number of line start */
6042 result
= readline_internal (lbp
, stream
); /* read line */
6043 lineno
+= 1; /* increment global line number */
6044 charno
+= result
; /* increment global char number */
6046 /* Honor #line directives. */
6047 if (!no_line_directive
)
6049 static bool discard_until_line_directive
;
6051 /* Check whether this is a #line directive. */
6052 if (result
> 12 && strneq (lbp
->buffer
, "#line ", 6))
6057 if (sscanf (lbp
->buffer
, "#line %u \"%n", &lno
, &start
) >= 1
6058 && start
> 0) /* double quote character found */
6060 char *endp
= lbp
->buffer
+ start
;
6062 while ((endp
= strchr (endp
, '"')) != NULL
6063 && endp
[-1] == '\\')
6066 /* Ok, this is a real #line directive. Let's deal with it. */
6068 char *taggedabsname
; /* absolute name of original file */
6069 char *taggedfname
; /* name of original file as given */
6070 char *name
; /* temp var */
6072 discard_until_line_directive
= false; /* found it */
6073 name
= lbp
->buffer
+ start
;
6075 canonicalize_filename (name
);
6076 taggedabsname
= absolute_filename (name
, tagfiledir
);
6077 if (filename_is_absolute (name
)
6078 || filename_is_absolute (curfdp
->infname
))
6079 taggedfname
= savestr (taggedabsname
);
6081 taggedfname
= relative_filename (taggedabsname
,tagfiledir
);
6083 if (streq (curfdp
->taggedfname
, taggedfname
))
6084 /* The #line directive is only a line number change. We
6085 deal with this afterwards. */
6088 /* The tags following this #line directive should be
6089 attributed to taggedfname. In order to do this, set
6090 curfdp accordingly. */
6092 fdesc
*fdp
; /* file description pointer */
6094 /* Go look for a file description already set up for the
6095 file indicated in the #line directive. If there is
6096 one, use it from now until the next #line
6098 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6099 if (streq (fdp
->infname
, curfdp
->infname
)
6100 && streq (fdp
->taggedfname
, taggedfname
))
6101 /* If we remove the second test above (after the &&)
6102 then all entries pertaining to the same file are
6103 coalesced in the tags file. If we use it, then
6104 entries pertaining to the same file but generated
6105 from different files (via #line directives) will
6106 go into separate sections in the tags file. These
6107 alternatives look equivalent. The first one
6108 destroys some apparently useless information. */
6114 /* Else, if we already tagged the real file, skip all
6115 input lines until the next #line directive. */
6116 if (fdp
== NULL
) /* not found */
6117 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6118 if (streq (fdp
->infabsname
, taggedabsname
))
6120 discard_until_line_directive
= true;
6124 /* Else create a new file description and use that from
6125 now on, until the next #line directive. */
6126 if (fdp
== NULL
) /* not found */
6129 fdhead
= xnew (1, fdesc
);
6130 *fdhead
= *curfdp
; /* copy curr. file description */
6132 fdhead
->infname
= savestr (curfdp
->infname
);
6133 fdhead
->infabsname
= savestr (curfdp
->infabsname
);
6134 fdhead
->infabsdir
= savestr (curfdp
->infabsdir
);
6135 fdhead
->taggedfname
= taggedfname
;
6136 fdhead
->usecharno
= false;
6137 fdhead
->prop
= NULL
;
6138 fdhead
->written
= false;
6142 free (taggedabsname
);
6144 readline (lbp
, stream
);
6146 } /* if a real #line directive */
6147 } /* if #line is followed by a number */
6148 } /* if line begins with "#line " */
6150 /* If we are here, no #line directive was found. */
6151 if (discard_until_line_directive
)
6155 /* Do a tail recursion on ourselves, thus discarding the contents
6156 of the line buffer. */
6157 readline (lbp
, stream
);
6161 discard_until_line_directive
= false;
6164 } /* if #line directives should be considered */
6171 /* Match against relevant regexps. */
6173 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6175 /* Only use generic regexps or those for the current language.
6176 Also do not use multiline regexps, which is the job of
6177 regex_tag_multiline. */
6178 if ((rp
->lang
!= NULL
&& rp
->lang
!= fdhead
->lang
)
6182 match
= re_match (rp
->pat
, lbp
->buffer
, lbp
->len
, 0, &rp
->regs
);
6187 if (!rp
->error_signaled
)
6189 error ("regexp stack overflow while matching \"%s\"",
6191 rp
->error_signaled
= true;
6198 /* Empty string matched. */
6199 if (!rp
->error_signaled
)
6201 error ("regexp matches the empty string: \"%s\"", rp
->pattern
);
6202 rp
->error_signaled
= true;
6206 /* Match occurred. Construct a tag. */
6208 if (name
[0] == '\0')
6210 else /* make a named tag */
6211 name
= substitute (lbp
->buffer
, rp
->name
, &rp
->regs
);
6212 if (rp
->force_explicit_name
)
6213 /* Force explicit tag name, if a name is there. */
6214 pfnote (name
, true, lbp
->buffer
, match
, lineno
, linecharno
);
6216 make_tag (name
, strlen (name
), true,
6217 lbp
->buffer
, match
, lineno
, linecharno
);
6226 * Return a pointer to a space of size strlen(cp)+1 allocated
6227 * with xnew where the string CP has been copied.
6230 savestr (const char *cp
)
6232 return savenstr (cp
, strlen (cp
));
6236 * Return a pointer to a space of size LEN+1 allocated with xnew where
6237 * the string CP has been copied for at most the first LEN characters.
6240 savenstr (const char *cp
, int len
)
6242 char *dp
= xnew (len
+ 1, char);
6244 return memcpy (dp
, cp
, len
);
6247 /* Skip spaces (end of string is not space), return new pointer. */
6249 skip_spaces (char *cp
)
6251 while (iswhite (*cp
))
6256 /* Skip non spaces, except end of string, return new pointer. */
6258 skip_non_spaces (char *cp
)
6260 while (*cp
!= '\0' && !iswhite (*cp
))
6265 /* Skip any chars in the "name" class.*/
6267 skip_name (char *cp
)
6269 /* '\0' is a notinname() so loop stops there too */
6270 while (! notinname (*cp
))
6275 /* Print error message and exit. */
6277 fatal (const char *s1
, const char *s2
)
6280 exit (EXIT_FAILURE
);
6284 pfatal (const char *s1
)
6287 exit (EXIT_FAILURE
);
6291 suggest_asking_for_help (void)
6293 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
6295 exit (EXIT_FAILURE
);
6298 /* Output a diagnostic with printf-style FORMAT and args. */
6300 error (const char *format
, ...)
6303 va_start (ap
, format
);
6304 fprintf (stderr
, "%s: ", progname
);
6305 vfprintf (stderr
, format
, ap
);
6306 fprintf (stderr
, "\n");
6310 /* Return a newly-allocated string whose contents
6311 concatenate those of s1, s2, s3. */
6313 concat (const char *s1
, const char *s2
, const char *s3
)
6315 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
6316 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
6318 strcpy (result
, s1
);
6319 strcpy (result
+ len1
, s2
);
6320 strcpy (result
+ len1
+ len2
, s3
);
6326 /* Does the same work as the system V getcwd, but does not need to
6327 guess the buffer size in advance. */
6332 char *path
= xnew (bufsize
, char);
6334 while (getcwd (path
, bufsize
) == NULL
)
6336 if (errno
!= ERANGE
)
6340 path
= xnew (bufsize
, char);
6343 canonicalize_filename (path
);
6347 /* Return a newly allocated string containing the file name of FILE
6348 relative to the absolute directory DIR (which should end with a slash). */
6350 relative_filename (char *file
, char *dir
)
6352 char *fp
, *dp
, *afn
, *res
;
6355 /* Find the common root of file and dir (with a trailing slash). */
6356 afn
= absolute_filename (file
, cwd
);
6359 while (*fp
++ == *dp
++)
6361 fp
--, dp
--; /* back to the first differing char */
6363 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
6366 do /* look at the equal chars until '/' */
6370 /* Build a sequence of "../" strings for the resulting relative file name. */
6372 while ((dp
= strchr (dp
+ 1, '/')) != NULL
)
6374 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
6377 z
= stpcpy (z
, "../");
6379 /* Add the file name relative to the common root of file and dir. */
6386 /* Return a newly allocated string containing the absolute file name
6387 of FILE given DIR (which should end with a slash). */
6389 absolute_filename (char *file
, char *dir
)
6391 char *slashp
, *cp
, *res
;
6393 if (filename_is_absolute (file
))
6394 res
= savestr (file
);
6396 /* We don't support non-absolute file names with a drive
6397 letter, like `d:NAME' (it's too much hassle). */
6398 else if (file
[1] == ':')
6399 fatal ("%s: relative file names with drive letters not supported", file
);
6402 res
= concat (dir
, file
, "");
6404 /* Delete the "/dirname/.." and "/." substrings. */
6405 slashp
= strchr (res
, '/');
6406 while (slashp
!= NULL
&& slashp
[0] != '\0')
6408 if (slashp
[1] == '.')
6410 if (slashp
[2] == '.'
6411 && (slashp
[3] == '/' || slashp
[3] == '\0'))
6416 while (cp
>= res
&& !filename_is_absolute (cp
));
6418 cp
= slashp
; /* the absolute name begins with "/.." */
6420 /* Under MSDOS and NT we get `d:/NAME' as absolute
6421 file name, so the luser could say `d:/../NAME'.
6422 We silently treat this as `d:/NAME'. */
6423 else if (cp
[0] != '/')
6426 memmove (cp
, slashp
+ 3, strlen (slashp
+ 2));
6430 else if (slashp
[2] == '/' || slashp
[2] == '\0')
6432 memmove (slashp
, slashp
+ 2, strlen (slashp
+ 1));
6437 slashp
= strchr (slashp
+ 1, '/');
6440 if (res
[0] == '\0') /* just a safety net: should never happen */
6443 return savestr ("/");
6449 /* Return a newly allocated string containing the absolute
6450 file name of dir where FILE resides given DIR (which should
6451 end with a slash). */
6453 absolute_dirname (char *file
, char *dir
)
6458 slashp
= strrchr (file
, '/');
6460 return savestr (dir
);
6463 res
= absolute_filename (file
, dir
);
6469 /* Whether the argument string is an absolute file name. The argument
6470 string must have been canonicalized with canonicalize_filename. */
6472 filename_is_absolute (char *fn
)
6474 return (fn
[0] == '/'
6476 || (ISALPHA (fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
6481 /* Downcase DOS drive letter and collapse separators into single slashes.
6484 canonicalize_filename (register char *fn
)
6490 /* Canonicalize drive letter case. */
6491 # define ISUPPER(c) isupper (CHAR (c))
6492 if (fn
[0] != '\0' && fn
[1] == ':' && ISUPPER (fn
[0]))
6493 fn
[0] = lowcase (fn
[0]);
6498 /* Collapse multiple separators into a single slash. */
6499 for (cp
= fn
; *cp
!= '\0'; cp
++, fn
++)
6503 while (cp
[1] == sep
)
6512 /* Initialize a linebuffer for use. */
6514 linebuffer_init (linebuffer
*lbp
)
6516 lbp
->size
= (DEBUG
) ? 3 : 200;
6517 lbp
->buffer
= xnew (lbp
->size
, char);
6518 lbp
->buffer
[0] = '\0';
6522 /* Set the minimum size of a string contained in a linebuffer. */
6524 linebuffer_setlen (linebuffer
*lbp
, int toksize
)
6526 while (lbp
->size
<= toksize
)
6529 xrnew (lbp
->buffer
, lbp
->size
, char);
6534 /* Like malloc but get fatal error if memory is exhausted. */
6536 xmalloc (size_t size
)
6538 void *result
= malloc (size
);
6540 fatal ("virtual memory exhausted", (char *)NULL
);
6545 xrealloc (void *ptr
, size_t size
)
6547 void *result
= realloc (ptr
, size
);
6549 fatal ("virtual memory exhausted", (char *)NULL
);
6555 * indent-tabs-mode: t
6558 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
6559 * c-file-style: "gnu"
6563 /* etags.c ends here */