1 /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
2 Copyright (C) 1984, 1987, 1988, 1989, 1993, 1994, 1995,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005 Free Software Foundation, Inc. and Ken Arnold
6 This file is not considered part of GNU Emacs.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 * Ctags originally by Ken Arnold.
25 * Fortran added by Jim Kleckner.
26 * Ed Pelegri-Llopart added C typedefs.
27 * Gnu Emacs TAGS format and modifications by RMS?
28 * 1989 Sam Kendall added C++.
29 * 1992 Joseph B. Wells improved C and C++ parsing.
30 * 1993 Francesco Potortì reorganised C and C++.
31 * 1994 Line-by-line regexp tags by Tom Tromey.
32 * 2001 Nested classes by Francesco Potortì (concept by Mykola Dzyuba).
33 * 2002 #line directives by Francesco Potortì.
35 * Francesco Potortì <pot@gnu.org> has maintained and improved it since 1993.
39 * If you want to add support for a new language, start by looking at the LUA
40 * language, which is the simplest. Alternatively, consider shipping a
41 * configuration file containing regexp definitions for etags.
44 char pot_etags_version
[] = "@(#) pot revision number is 17.5";
54 # define NDEBUG /* disable assert */
59 /* On some systems, Emacs defines static as nothing for the sake
60 of unexec. We don't want that here since we don't use unexec. */
62 # define ETAGS_REGEXPS /* use the regexp features */
63 # define LONG_OPTIONS /* accept long options */
64 # ifndef PTR /* for Xemacs */
67 # ifndef __P /* for Xemacs */
68 # define __P(args) args
70 #else /* no config.h */
71 # if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
72 # define __P(args) args /* use prototypes */
73 # define PTR void * /* for generic pointers */
74 # else /* not standard C */
75 # define __P(args) () /* no prototypes */
76 # define const /* remove const for old compilers' sake */
77 # define PTR long * /* don't use void* */
79 #endif /* !HAVE_CONFIG_H */
82 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
87 # define LONG_OPTIONS TRUE
89 # define LONG_OPTIONS FALSE
92 /* WIN32_NATIVE is for Xemacs.
93 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
98 #endif /* WIN32_NATIVE */
104 # include <sys/param.h>
106 # ifndef HAVE_CONFIG_H
108 # include <sys/config.h>
120 # define MAXPATHLEN _MAX_PATH
126 # endif /* undef HAVE_GETCWD */
127 #else /* not WINDOWSNT */
131 # else /* no standard C headers */
132 extern char *getenv ();
134 # define EXIT_SUCCESS 1
135 # define EXIT_FAILURE 0
137 # define EXIT_SUCCESS 0
138 # define EXIT_FAILURE 1
141 #endif /* !WINDOWSNT */
146 # if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
147 extern char *getcwd (char *buf
, size_t size
);
149 #endif /* HAVE_UNISTD_H */
157 #include <sys/types.h>
158 #include <sys/stat.h>
162 # undef assert /* some systems have a buggy assert.h */
163 # define assert(x) ((void) 0)
166 #if !defined (S_ISREG) && defined (S_IFREG)
167 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
173 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
175 extern int optind
, opterr
;
176 #endif /* LONG_OPTIONS */
179 # ifndef HAVE_CONFIG_H /* this is a standalone compilation */
180 # ifdef __CYGWIN__ /* compiling on Cygwin */
182 the regex
.h distributed with Cygwin is
not compatible with etags
, alas
!
183 If you want regular expression support
, you should
delete this notice
and
184 arrange to use the GNU regex
.h
and regex
.c
.
188 #endif /* ETAGS_REGEXPS */
190 /* Define CTAGS to make the program "ctags" compatible with the usual one.
191 Leave it undefined to make the program "etags", which makes emacs-style
192 tag tables and tags typedefs, #defines and struct/union/enum by default. */
200 #define streq(s,t) (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t))
201 #define strcaseeq(s,t) (assert((s)!=NULL && (t)!=NULL), !etags_strcasecmp (s, t))
202 #define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
203 #define strncaseeq(s,t,n) (assert((s)!=NULL && (t)!=NULL), !etags_strncasecmp (s, t, n))
205 #define CHARS 256 /* 2^sizeof(char) */
206 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
207 #define iswhite(c) (_wht[CHAR(c)]) /* c is white (see white) */
208 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name (see nonam) */
209 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token (see begtk) */
210 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token (see midtk) */
211 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens (see endtk) */
213 #define ISALNUM(c) isalnum (CHAR(c))
214 #define ISALPHA(c) isalpha (CHAR(c))
215 #define ISDIGIT(c) isdigit (CHAR(c))
216 #define ISLOWER(c) islower (CHAR(c))
218 #define lowcase(c) tolower (CHAR(c))
219 #define upcase(c) toupper (CHAR(c))
223 * xnew, xrnew -- allocate, reallocate storage
225 * SYNOPSIS: Type *xnew (int n, Type);
226 * void xrnew (OldPointer, int n, Type);
229 # include "chkmalloc.h"
230 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
231 (n) * sizeof (Type)))
232 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
233 (char *) (op), (n) * sizeof (Type)))
235 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
236 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
237 (char *) (op), (n) * sizeof (Type)))
242 typedef void Lang_function
__P((FILE *));
246 char *suffix
; /* file name suffix for this compressor */
247 char *command
; /* takes one arg and decompresses to stdout */
252 char *name
; /* language name */
253 char *help
; /* detailed help for the language */
254 Lang_function
*function
; /* parse function */
255 char **suffixes
; /* name suffixes of this language's files */
256 char **filenames
; /* names of this language's files */
257 char **interpreters
; /* interpreters for this language */
258 bool metasource
; /* source used to generate other sources */
263 struct fdesc
*next
; /* for the linked list */
264 char *infname
; /* uncompressed input file name */
265 char *infabsname
; /* absolute uncompressed input file name */
266 char *infabsdir
; /* absolute dir of input file */
267 char *taggedfname
; /* file name to write in tagfile */
268 language
*lang
; /* language of file */
269 char *prop
; /* file properties to write in tagfile */
270 bool usecharno
; /* etags tags shall contain char number */
271 bool written
; /* entry written in the tags file */
274 typedef struct node_st
275 { /* sorting structure */
276 struct node_st
*left
, *right
; /* left and right sons */
277 fdesc
*fdp
; /* description of file to whom tag belongs */
278 char *name
; /* tag name */
279 char *regex
; /* search regexp */
280 bool valid
; /* write this tag on the tag file */
281 bool is_func
; /* function tag: use regexp in CTAGS mode */
282 bool been_warned
; /* warning already given for duplicated tag */
283 int lno
; /* line number tag is on */
284 long cno
; /* character number line starts on */
288 * A `linebuffer' is a structure which holds a line of text.
289 * `readline_internal' reads a line from a stream into a linebuffer
290 * and works regardless of the length of the line.
291 * SIZE is the size of BUFFER, LEN is the length of the string in
292 * BUFFER after readline reads it.
301 /* Used to support mixing of --lang and file names. */
305 at_language
, /* a language specification */
306 at_regexp
, /* a regular expression */
307 at_filename
, /* a file name */
308 at_stdin
, /* read from stdin here */
309 at_end
/* stop parsing the list */
310 } arg_type
; /* argument type */
311 language
*lang
; /* language associated with the argument */
312 char *what
; /* the argument itself */
316 /* Structure defining a regular expression. */
317 typedef struct regexp
319 struct regexp
*p_next
; /* pointer to next in list */
320 language
*lang
; /* if set, use only for this language */
321 char *pattern
; /* the regexp pattern */
322 char *name
; /* tag name */
323 struct re_pattern_buffer
*pat
; /* the compiled pattern */
324 struct re_registers regs
; /* re registers */
325 bool error_signaled
; /* already signaled for this regexp */
326 bool force_explicit_name
; /* do not allow implict tag name */
327 bool ignore_case
; /* ignore case when matching */
328 bool multi_line
; /* do a multi-line match on the whole file */
330 #endif /* ETAGS_REGEXPS */
333 /* Many compilers barf on this:
334 Lang_function Ada_funcs;
335 so let's write it this way */
336 static void Ada_funcs
__P((FILE *));
337 static void Asm_labels
__P((FILE *));
338 static void C_entries
__P((int c_ext
, FILE *));
339 static void default_C_entries
__P((FILE *));
340 static void plain_C_entries
__P((FILE *));
341 static void Cjava_entries
__P((FILE *));
342 static void Cobol_paragraphs
__P((FILE *));
343 static void Cplusplus_entries
__P((FILE *));
344 static void Cstar_entries
__P((FILE *));
345 static void Erlang_functions
__P((FILE *));
346 static void Fortran_functions
__P((FILE *));
347 static void HTML_labels
__P((FILE *));
348 static void Lisp_functions
__P((FILE *));
349 static void Lua_functions
__P((FILE *));
350 static void Makefile_targets
__P((FILE *));
351 static void Pascal_functions
__P((FILE *));
352 static void Perl_functions
__P((FILE *));
353 static void PHP_functions
__P((FILE *));
354 static void PS_functions
__P((FILE *));
355 static void Prolog_functions
__P((FILE *));
356 static void Python_functions
__P((FILE *));
357 static void Scheme_functions
__P((FILE *));
358 static void TeX_commands
__P((FILE *));
359 static void Texinfo_nodes
__P((FILE *));
360 static void Yacc_entries
__P((FILE *));
361 static void just_read_file
__P((FILE *));
363 static void print_language_names
__P((void));
364 static void print_version
__P((void));
365 static void print_help
__P((argument
*));
366 int main
__P((int, char **));
368 static compressor
*get_compressor_from_suffix
__P((char *, char **));
369 static language
*get_language_from_langname
__P((const char *));
370 static language
*get_language_from_interpreter
__P((char *));
371 static language
*get_language_from_filename
__P((char *, bool));
372 static void readline
__P((linebuffer
*, FILE *));
373 static long readline_internal
__P((linebuffer
*, FILE *));
374 static bool nocase_tail
__P((char *));
375 static void get_tag
__P((char *, char **));
378 static void analyse_regex
__P((char *));
379 static void free_regexps
__P((void));
380 static void regex_tag_multiline
__P((void));
381 #endif /* ETAGS_REGEXPS */
382 static void error
__P((const char *, const char *));
383 static void suggest_asking_for_help
__P((void));
384 void fatal
__P((char *, char *));
385 static void pfatal
__P((char *));
386 static void add_node
__P((node
*, node
**));
388 static void init
__P((void));
389 static void process_file_name
__P((char *, language
*));
390 static void process_file
__P((FILE *, char *, language
*));
391 static void find_entries
__P((FILE *));
392 static void free_tree
__P((node
*));
393 static void free_fdesc
__P((fdesc
*));
394 static void pfnote
__P((char *, bool, char *, int, int, long));
395 static void make_tag
__P((char *, int, bool, char *, int, int, long));
396 static void invalidate_nodes
__P((fdesc
*, node
**));
397 static void put_entries
__P((node
*));
399 static char *concat
__P((char *, char *, char *));
400 static char *skip_spaces
__P((char *));
401 static char *skip_non_spaces
__P((char *));
402 static char *savenstr
__P((char *, int));
403 static char *savestr
__P((char *));
404 static char *etags_strchr
__P((const char *, int));
405 static char *etags_strrchr
__P((const char *, int));
406 static int etags_strcasecmp
__P((const char *, const char *));
407 static int etags_strncasecmp
__P((const char *, const char *, int));
408 static char *etags_getcwd
__P((void));
409 static char *relative_filename
__P((char *, char *));
410 static char *absolute_filename
__P((char *, char *));
411 static char *absolute_dirname
__P((char *, char *));
412 static bool filename_is_absolute
__P((char *f
));
413 static void canonicalize_filename
__P((char *));
414 static void linebuffer_init
__P((linebuffer
*));
415 static void linebuffer_setlen
__P((linebuffer
*, int));
416 static PTR xmalloc
__P((unsigned int));
417 static PTR xrealloc
__P((char *, unsigned int));
420 static char searchar
= '/'; /* use /.../ searches */
422 static char *tagfile
; /* output file */
423 static char *progname
; /* name this program was invoked with */
424 static char *cwd
; /* current working directory */
425 static char *tagfiledir
; /* directory of tagfile */
426 static FILE *tagf
; /* ioptr for tags file */
428 static fdesc
*fdhead
; /* head of file description list */
429 static fdesc
*curfdp
; /* current file description */
430 static int lineno
; /* line number of current line */
431 static long charno
; /* current character number */
432 static long linecharno
; /* charno of start of current line */
433 static char *dbp
; /* pointer to start of current tag */
435 static const int invalidcharno
= -1;
437 static node
*nodehead
; /* the head of the binary tree of tags */
438 static node
*last_node
; /* the last node created */
440 static linebuffer lb
; /* the current line */
441 static linebuffer filebuf
; /* a buffer containing the whole file */
442 static linebuffer token_name
; /* a buffer containing a tag name */
444 /* boolean "functions" (see init) */
445 static bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
448 *white
= " \f\t\n\r\v",
450 *nonam
= " \f\t\n\r()=,;", /* look at make_tag before modifying! */
451 /* token ending chars */
452 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
453 /* token starting chars */
454 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
455 /* valid in-token chars */
456 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
458 static bool append_to_tagfile
; /* -a: append to tags */
459 /* The next four default to TRUE for etags, but to FALSE for ctags. */
460 static bool typedefs
; /* -t: create tags for C and Ada typedefs */
461 static bool typedefs_or_cplusplus
; /* -T: create tags for C typedefs, level */
462 /* 0 struct/enum/union decls, and C++ */
463 /* member functions. */
464 static bool constantypedefs
; /* -d: create tags for C #define, enum */
465 /* constants and variables. */
466 /* -D: opposite of -d. Default under ctags. */
467 static bool globals
; /* create tags for global variables */
468 static bool declarations
; /* --declarations: tag them and extern in C&Co*/
469 static bool members
; /* create tags for C member variables */
470 static bool no_line_directive
; /* ignore #line directives (undocumented) */
471 static bool update
; /* -u: update tags */
472 static bool vgrind_style
; /* -v: create vgrind style index output */
473 static bool no_warnings
; /* -w: suppress warnings */
474 static bool cxref_style
; /* -x: create cxref style output */
475 static bool cplusplus
; /* .[hc] means C++, not C */
476 static bool ignoreindent
; /* -I: ignore indentation in C */
477 static bool packages_only
; /* --packages-only: in Ada, only tag packages*/
479 #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
480 static bool parsing_stdin
; /* --parse-stdin used */
483 static regexp
*p_head
; /* list of all regexps */
484 static bool need_filebuf
; /* some regexes are multi-line */
486 # define need_filebuf FALSE
487 #endif /* ETAGS_REGEXPS */
490 static struct option longopts
[] =
492 { "packages-only", no_argument
, &packages_only
, TRUE
},
493 { "c++", no_argument
, NULL
, 'C' },
494 { "declarations", no_argument
, &declarations
, TRUE
},
495 { "no-line-directive", no_argument
, &no_line_directive
, TRUE
},
496 { "help", no_argument
, NULL
, 'h' },
497 { "help", no_argument
, NULL
, 'H' },
498 { "ignore-indentation", no_argument
, NULL
, 'I' },
499 { "language", required_argument
, NULL
, 'l' },
500 { "members", no_argument
, &members
, TRUE
},
501 { "no-members", no_argument
, &members
, FALSE
},
502 { "output", required_argument
, NULL
, 'o' },
504 { "regex", required_argument
, NULL
, 'r' },
505 { "no-regex", no_argument
, NULL
, 'R' },
506 { "ignore-case-regex", required_argument
, NULL
, 'c' },
507 #endif /* ETAGS_REGEXPS */
508 { "parse-stdin", required_argument
, NULL
, STDIN
},
509 { "version", no_argument
, NULL
, 'V' },
511 #if CTAGS /* Etags options */
512 { "backward-search", no_argument
, NULL
, 'B' },
513 { "cxref", no_argument
, NULL
, 'x' },
514 { "defines", no_argument
, NULL
, 'd' },
515 { "globals", no_argument
, &globals
, TRUE
},
516 { "typedefs", no_argument
, NULL
, 't' },
517 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
518 { "update", no_argument
, NULL
, 'u' },
519 { "vgrind", no_argument
, NULL
, 'v' },
520 { "no-warn", no_argument
, NULL
, 'w' },
522 #else /* Ctags options */
523 { "append", no_argument
, NULL
, 'a' },
524 { "no-defines", no_argument
, NULL
, 'D' },
525 { "no-globals", no_argument
, &globals
, FALSE
},
526 { "include", required_argument
, NULL
, 'i' },
530 #endif /* LONG_OPTIONS */
532 static compressor compressors
[] =
534 { "z", "gzip -d -c"},
535 { "Z", "gzip -d -c"},
536 { "gz", "gzip -d -c"},
537 { "GZ", "gzip -d -c"},
538 { "bz2", "bzip2 -d -c" },
547 static char *Ada_suffixes
[] =
548 { "ads", "adb", "ada", NULL
};
549 static char Ada_help
[] =
550 "In Ada code, functions, procedures, packages, tasks and types are\n\
551 tags. Use the `--packages-only' option to create tags for\n\
553 Ada tag names have suffixes indicating the type of entity:\n\
554 Entity type: Qualifier:\n\
555 ------------ ----------\n\
562 Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
563 body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
564 will just search for any tag `bidule'.";
567 static char *Asm_suffixes
[] =
568 { "a", /* Unix assembler */
569 "asm", /* Microcontroller assembly */
570 "def", /* BSO/Tasking definition includes */
571 "inc", /* Microcontroller include files */
572 "ins", /* Microcontroller include files */
573 "s", "sa", /* Unix assembler */
574 "S", /* cpp-processed Unix assembler */
575 "src", /* BSO/Tasking C compiler output */
578 static char Asm_help
[] =
579 "In assembler code, labels appearing at the beginning of a line,\n\
580 followed by a colon, are tags.";
583 /* Note that .c and .h can be considered C++, if the --c++ flag was
584 given, or if the `class' or `template' keyowrds are met inside the file.
585 That is why default_C_entries is called for these. */
586 static char *default_C_suffixes
[] =
588 static char default_C_help
[] =
589 "In C code, any C function or typedef is a tag, and so are\n\
590 definitions of `struct', `union' and `enum'. `#define' macro\n\
591 definitions and `enum' constants are tags unless you specify\n\
592 `--no-defines'. Global variables are tags unless you specify\n\
593 `--no-globals'. Use of `--no-globals' and `--no-defines'\n\
594 can make the tags table file much smaller.\n\
595 You can tag function declarations and external variables by\n\
596 using `--declarations', and struct members by using `--members'.";
598 static char *Cplusplus_suffixes
[] =
599 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
600 "M", /* Objective C++ */
601 "pdb", /* Postscript with C syntax */
603 static char Cplusplus_help
[] =
604 "In C++ code, all the tag constructs of C code are tagged. (Use\n\
605 --help --lang=c --lang=c++ for full help.)\n\
606 In addition to C tags, member functions are also recognized, and\n\
607 optionally member variables if you use the `--members' option.\n\
608 Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
609 and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
612 static char *Cjava_suffixes
[] =
614 static char Cjava_help
[] =
615 "In Java code, all the tags constructs of C and C++ code are\n\
616 tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
619 static char *Cobol_suffixes
[] =
620 { "COB", "cob", NULL
};
621 static char Cobol_help
[] =
622 "In Cobol code, tags are paragraph names; that is, any word\n\
623 starting in column 8 and followed by a period.";
625 static char *Cstar_suffixes
[] =
626 { "cs", "hs", NULL
};
628 static char *Erlang_suffixes
[] =
629 { "erl", "hrl", NULL
};
630 static char Erlang_help
[] =
631 "In Erlang code, the tags are the functions, records and macros\n\
632 defined in the file.";
634 static char *Fortran_suffixes
[] =
635 { "F", "f", "f90", "for", NULL
};
636 static char Fortran_help
[] =
637 "In Fortran code, functions, subroutines and block data are tags.";
639 static char *HTML_suffixes
[] =
640 { "htm", "html", "shtml", NULL
};
641 static char HTML_help
[] =
642 "In HTML input files, the tags are the `title' and the `h1', `h2',\n\
643 `h3' headers. Also, tags are `name=' in anchors and all\n\
644 occurrences of `id='.";
646 static char *Lisp_suffixes
[] =
647 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
648 static char Lisp_help
[] =
649 "In Lisp code, any function defined with `defun', any variable\n\
650 defined with `defvar' or `defconst', and in general the first\n\
651 argument of any expression that starts with `(def' in column zero\n\
654 static char *Lua_suffixes
[] =
655 { "lua", "LUA", NULL
};
656 static char Lua_help
[] =
657 "In Lua scripts, all functions are tags.";
659 static char *Makefile_filenames
[] =
660 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
661 static char Makefile_help
[] =
662 "In makefiles, targets are tags; additionally, variables are tags\n\
663 unless you specify `--no-globals'.";
665 static char *Objc_suffixes
[] =
666 { "lm", /* Objective lex file */
667 "m", /* Objective C file */
669 static char Objc_help
[] =
670 "In Objective C code, tags include Objective C definitions for classes,\n\
671 class categories, methods and protocols. Tags for variables and\n\
672 functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.";
674 static char *Pascal_suffixes
[] =
675 { "p", "pas", NULL
};
676 static char Pascal_help
[] =
677 "In Pascal code, the tags are the functions and procedures defined\n\
680 static char *Perl_suffixes
[] =
681 { "pl", "pm", NULL
};
682 static char *Perl_interpreters
[] =
683 { "perl", "@PERL@", NULL
};
684 static char Perl_help
[] =
685 "In Perl code, the tags are the packages, subroutines and variables\n\
686 defined by the `package', `sub', `my' and `local' keywords. Use\n\
687 `--globals' if you want to tag global variables. Tags for\n\
688 subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
689 defined in the default package is `main::SUB'.";
691 static char *PHP_suffixes
[] =
692 { "php", "php3", "php4", NULL
};
693 static char PHP_help
[] =
694 "In PHP code, tags are functions, classes and defines. When using\n\
695 the `--members' option, vars are tags too.";
697 static char *plain_C_suffixes
[] =
698 { "pc", /* Pro*C file */
701 static char *PS_suffixes
[] =
702 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
703 static char PS_help
[] =
704 "In PostScript code, the tags are the functions.";
706 static char *Prolog_suffixes
[] =
708 static char Prolog_help
[] =
709 "In Prolog code, tags are predicates and rules at the beginning of\n\
712 static char *Python_suffixes
[] =
714 static char Python_help
[] =
715 "In Python code, `def' or `class' at the beginning of a line\n\
718 /* Can't do the `SCM' or `scm' prefix with a version number. */
719 static char *Scheme_suffixes
[] =
720 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
721 static char Scheme_help
[] =
722 "In Scheme code, tags include anything defined with `def' or with a\n\
723 construct whose name starts with `def'. They also include\n\
724 variables set with `set!' at top level in the file.";
726 static char *TeX_suffixes
[] =
727 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
728 static char TeX_help
[] =
729 "In LaTeX text, the argument of any of the commands `\\chapter',\n\
730 `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
731 `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
732 `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
733 `\\newenvironment' or `\\renewenvironment' is a tag.\n\
735 Other commands can be specified by setting the environment variable\n\
736 `TEXTAGS' to a colon-separated list like, for example,\n\
737 TEXTAGS=\"mycommand:myothercommand\".";
740 static char *Texinfo_suffixes
[] =
741 { "texi", "texinfo", "txi", NULL
};
742 static char Texinfo_help
[] =
743 "for texinfo files, lines starting with @node are tagged.";
745 static char *Yacc_suffixes
[] =
746 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
747 static char Yacc_help
[] =
748 "In Bison or Yacc input files, each rule defines as a tag the\n\
749 nonterminal it constructs. The portions of the file that contain\n\
750 C code are parsed as C code (use --help --lang=c --lang=yacc\n\
753 static char auto_help
[] =
754 "`auto' is not a real language, it indicates to use\n\
755 a default language for files base on file name suffix and file contents.";
757 static char none_help
[] =
758 "`none' is not a real language, it indicates to only do\n\
759 regexp processing on files.";
761 static char no_lang_help
[] =
762 "No detailed help available for this language.";
766 * Table of languages.
768 * It is ok for a given function to be listed under more than one
769 * name. I just didn't.
772 static language lang_names
[] =
774 { "ada", Ada_help
, Ada_funcs
, Ada_suffixes
},
775 { "asm", Asm_help
, Asm_labels
, Asm_suffixes
},
776 { "c", default_C_help
, default_C_entries
, default_C_suffixes
},
777 { "c++", Cplusplus_help
, Cplusplus_entries
, Cplusplus_suffixes
},
778 { "c*", no_lang_help
, Cstar_entries
, Cstar_suffixes
},
779 { "cobol", Cobol_help
, Cobol_paragraphs
, Cobol_suffixes
},
780 { "erlang", Erlang_help
, Erlang_functions
, Erlang_suffixes
},
781 { "fortran", Fortran_help
, Fortran_functions
, Fortran_suffixes
},
782 { "html", HTML_help
, HTML_labels
, HTML_suffixes
},
783 { "java", Cjava_help
, Cjava_entries
, Cjava_suffixes
},
784 { "lisp", Lisp_help
, Lisp_functions
, Lisp_suffixes
},
785 { "lua", Lua_help
, Lua_functions
, Lua_suffixes
},
786 { "makefile", Makefile_help
,Makefile_targets
,NULL
,Makefile_filenames
},
787 { "objc", Objc_help
, plain_C_entries
, Objc_suffixes
},
788 { "pascal", Pascal_help
, Pascal_functions
, Pascal_suffixes
},
789 { "perl",Perl_help
,Perl_functions
,Perl_suffixes
,NULL
,Perl_interpreters
},
790 { "php", PHP_help
, PHP_functions
, PHP_suffixes
},
791 { "postscript",PS_help
, PS_functions
, PS_suffixes
},
792 { "proc", no_lang_help
, plain_C_entries
, plain_C_suffixes
},
793 { "prolog", Prolog_help
, Prolog_functions
, Prolog_suffixes
},
794 { "python", Python_help
, Python_functions
, Python_suffixes
},
795 { "scheme", Scheme_help
, Scheme_functions
, Scheme_suffixes
},
796 { "tex", TeX_help
, TeX_commands
, TeX_suffixes
},
797 { "texinfo", Texinfo_help
, Texinfo_nodes
, Texinfo_suffixes
},
798 { "yacc", Yacc_help
,Yacc_entries
,Yacc_suffixes
,NULL
,NULL
,TRUE
},
799 { "auto", auto_help
}, /* default guessing scheme */
800 { "none", none_help
, just_read_file
}, /* regexp matching only */
801 { NULL
} /* end of list */
806 print_language_names ()
811 puts ("\nThese are the currently supported languages, along with the\n\
812 default file names and dot suffixes:");
813 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
815 printf (" %-*s", 10, lang
->name
);
816 if (lang
->filenames
!= NULL
)
817 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
818 printf (" %s", *name
);
819 if (lang
->suffixes
!= NULL
)
820 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
821 printf (" .%s", *ext
);
824 puts ("where `auto' means use default language for files based on file\n\
825 name suffix, and `none' means only do regexp processing on files.\n\
826 If no language is specified and no matching suffix is found,\n\
827 the first line of the file is read for a sharp-bang (#!) sequence\n\
828 followed by the name of an interpreter. If no such sequence is found,\n\
829 Fortran is tried first; if no tags are found, C is tried next.\n\
830 When parsing any C file, a \"class\" or \"template\" keyword\n\
832 puts ("Compressed files are supported using gzip and bzip2.\n\
834 For detailed help on a given language use, for example,\n\
835 etags --help --lang=ada.");
839 # define EMACS_NAME "standalone"
842 # define VERSION "version"
847 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
848 puts ("Copyright (C) 2002 Free Software Foundation, Inc. and Ken Arnold");
849 puts ("This program is distributed under the same terms as Emacs");
855 print_help (argbuffer
)
858 bool help_for_lang
= FALSE
;
860 for (; argbuffer
->arg_type
!= at_end
; argbuffer
++)
861 if (argbuffer
->arg_type
== at_language
)
865 puts (argbuffer
->lang
->help
);
866 help_for_lang
= TRUE
;
872 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
874 These are the options accepted by %s.\n", progname
, progname
);
876 puts ("You may use unambiguous abbreviations for the long option names.");
878 puts ("Long option names do not work with this executable, as it is not\n\
879 linked with GNU getopt.");
880 puts (" A - as file name means read names from stdin (one per line).\n\
881 Absolute names are stored in the output file as they are.\n\
882 Relative ones are stored relative to the output file's directory.\n");
885 puts ("-a, --append\n\
886 Append tag entries to existing tags file.");
888 puts ("--packages-only\n\
889 For Ada files, only generate tags for packages.");
892 puts ("-B, --backward-search\n\
893 Write the search commands for the tag entries using '?', the\n\
894 backward-search command instead of '/', the forward-search command.");
896 /* This option is mostly obsolete, because etags can now automatically
897 detect C++. Retained for backward compatibility and for debugging and
898 experimentation. In principle, we could want to tag as C++ even
899 before any "class" or "template" keyword.
901 Treat files whose name suffix defaults to C language as C++ files.");
904 puts ("--declarations\n\
905 In C and derived languages, create tags for function declarations,");
907 puts ("\tand create tags for extern variables if --globals is used.");
910 ("\tand create tags for extern variables unless --no-globals is used.");
913 puts ("-d, --defines\n\
914 Create tag entries for C #define constants and enum constants, too.");
916 puts ("-D, --no-defines\n\
917 Don't create tag entries for C #define constants and enum constants.\n\
918 This makes the tags file smaller.");
921 puts ("-i FILE, --include=FILE\n\
922 Include a note in tag file indicating that, when searching for\n\
923 a tag, one should also consult the tags file FILE after\n\
924 checking the current file.");
926 puts ("-l LANG, --language=LANG\n\
927 Force the following files to be considered as written in the\n\
928 named language up to the next --language=LANG option.");
932 Create tag entries for global variables in some languages.");
934 puts ("--no-globals\n\
935 Do not create tag entries for global variables in some\n\
936 languages. This makes the tags file smaller.");
938 Create tag entries for members of structures in some languages.");
941 puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
942 Make a tag for each line matching a regular expression pattern\n\
943 in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
944 files only. REGEXFILE is a file containing one REGEXP per line.\n\
945 REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
946 optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
947 puts (" If TAGNAME/ is present, the tags created are named.\n\
948 For example Tcl named tags can be created with:\n\
949 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
950 MODS are optional one-letter modifiers: `i' means to ignore case,\n\
951 `m' means to allow multi-line matches, `s' implies `m' and\n\
952 causes dot to match any character, including newline.");
953 puts ("-R, --no-regex\n\
954 Don't create tags from regexps for the following files.");
955 #endif /* ETAGS_REGEXPS */
956 puts ("-I, --ignore-indentation\n\
957 In C and C++ do not assume that a closing brace in the first\n\
958 column is the final brace of a function or structure definition.");
959 puts ("-o FILE, --output=FILE\n\
960 Write the tags to FILE.");
961 puts ("--parse-stdin=NAME\n\
962 Read from standard input and record tags as belonging to file NAME.");
966 puts ("-t, --typedefs\n\
967 Generate tag entries for C and Ada typedefs.");
968 puts ("-T, --typedefs-and-c++\n\
969 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
970 and C++ member functions.");
974 puts ("-u, --update\n\
975 Update the tag entries for the given files, leaving tag\n\
976 entries for other files in place. Currently, this is\n\
977 implemented by deleting the existing entries for the given\n\
978 files and then rewriting the new entries at the end of the\n\
979 tags file. It is often faster to simply rebuild the entire\n\
980 tag file than to use this.");
984 puts ("-v, --vgrind\n\
985 Generates an index of items intended for human consumption,\n\
986 similar to the output of vgrind. The index is sorted, and\n\
987 gives the page number of each item.");
988 puts ("-w, --no-warn\n\
989 Suppress warning messages about entries defined in multiple\n\
991 puts ("-x, --cxref\n\
992 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
993 The output uses line numbers instead of page numbers, but\n\
994 beyond that the differences are cosmetic; try both to see\n\
998 puts ("-V, --version\n\
999 Print the version of the program.\n\
1001 Print this help message.\n\
1002 Followed by one or more `--language' options prints detailed\n\
1003 help about tag generation for the specified languages.");
1005 print_language_names ();
1008 puts ("Report bugs to bug-gnu-emacs@gnu.org");
1010 exit (EXIT_SUCCESS
);
1014 #ifdef VMS /* VMS specific functions */
1018 /* This is a BUG! ANY arbitrary limit is a BUG!
1019 Won't someone please fix this? */
1020 #define MAX_FILE_SPEC_LEN 255
1023 char body
[MAX_FILE_SPEC_LEN
+ 1];
1027 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
1028 returning in each successive call the next file name matching the input
1029 spec. The function expects that each in_spec passed
1030 to it will be processed to completion; in particular, up to and
1031 including the call following that in which the last matching name
1032 is returned, the function ignores the value of in_spec, and will
1033 only start processing a new spec with the following call.
1034 If an error occurs, on return out_spec contains the value
1035 of in_spec when the error occurred.
1037 With each successive file name returned in out_spec, the
1038 function's return value is one. When there are no more matching
1039 names the function returns zero. If on the first call no file
1040 matches in_spec, or there is any other error, -1 is returned.
1044 #include <descrip.h>
1045 #define OUTSIZE MAX_FILE_SPEC_LEN
1051 static long context
= 0;
1052 static struct dsc$descriptor_s o
;
1053 static struct dsc$descriptor_s i
;
1054 static bool pass1
= TRUE
;
1061 o
.dsc$a_pointer
= (char *) out
;
1062 o
.dsc$w_length
= (short)OUTSIZE
;
1063 i
.dsc$a_pointer
= in
;
1064 i
.dsc$w_length
= (short)strlen(in
);
1065 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
1066 i
.dsc$b_class
= DSC$K_CLASS_S
;
1067 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
1068 o
.dsc$b_class
= DSC$K_CLASS_VS
;
1070 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
1072 out
->body
[out
->curlen
] = EOS
;
1075 else if (status
== RMS$_NMF
)
1079 strcpy(out
->body
, in
);
1082 lib$
find_file_end(&context
);
1088 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
1089 name of each file specified by the provided arg expanding wildcards.
1092 gfnames (arg
, p_error
)
1096 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
1098 switch (fn_exp (&filename
, arg
))
1102 return filename
.body
;
1108 return filename
.body
;
1112 #ifndef OLD /* Newer versions of VMS do provide `system'. */
1116 error ("%s", "system() function not implemented under VMS");
1120 #define VERSION_DELIM ';'
1121 char *massage_name (s
)
1127 if (*s
== VERSION_DELIM
)
1145 unsigned int nincluded_files
;
1146 char **included_files
;
1147 argument
*argbuffer
;
1148 int current_arg
, file_count
;
1149 linebuffer filename_lb
;
1150 bool help_asked
= FALSE
;
1159 _fmode
= O_BINARY
; /* all of files are treated as binary files */
1163 nincluded_files
= 0;
1164 included_files
= xnew (argc
, char *);
1168 /* Allocate enough no matter what happens. Overkill, but each one
1170 argbuffer
= xnew (argc
, argument
);
1173 * If etags, always find typedefs and structure tags. Why not?
1174 * Also default to find macro constants, enum constants and
1179 typedefs
= typedefs_or_cplusplus
= constantypedefs
= TRUE
;
1184 #ifdef ETAGS_REGEXPS
1185 optstring
= "-r:Rc:";
1186 #endif /* ETAGS_REGEXPS */
1189 optstring
= concat (optstring
,
1191 (CTAGS
) ? "BxdtTuvw" : "aDi:");
1193 while ((opt
= getopt_long (argc
, argv
, optstring
, longopts
, 0)) != EOF
)
1197 /* If getopt returns 0, then it has already processed a
1198 long-named option. We should do nothing. */
1202 /* This means that a file name has been seen. Record it. */
1203 argbuffer
[current_arg
].arg_type
= at_filename
;
1204 argbuffer
[current_arg
].what
= optarg
;
1210 /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
1211 argbuffer
[current_arg
].arg_type
= at_stdin
;
1212 argbuffer
[current_arg
].what
= optarg
;
1216 fatal ("cannot parse standard input more than once", (char *)NULL
);
1217 parsing_stdin
= TRUE
;
1220 /* Common options. */
1221 case 'C': cplusplus
= TRUE
; break;
1222 case 'f': /* for compatibility with old makefiles */
1226 error ("-o option may only be given once.", (char *)NULL
);
1227 suggest_asking_for_help ();
1233 case 'S': /* for backward compatibility */
1234 ignoreindent
= TRUE
;
1238 language
*lang
= get_language_from_langname (optarg
);
1241 argbuffer
[current_arg
].lang
= lang
;
1242 argbuffer
[current_arg
].arg_type
= at_language
;
1248 /* Backward compatibility: support obsolete --ignore-case-regexp. */
1249 optarg
= concat (optarg
, "i", ""); /* memory leak here */
1252 argbuffer
[current_arg
].arg_type
= at_regexp
;
1253 argbuffer
[current_arg
].what
= optarg
;
1257 argbuffer
[current_arg
].arg_type
= at_regexp
;
1258 argbuffer
[current_arg
].what
= NULL
;
1270 case 'a': append_to_tagfile
= TRUE
; break;
1271 case 'D': constantypedefs
= FALSE
; break;
1272 case 'i': included_files
[nincluded_files
++] = optarg
; break;
1274 /* Ctags options. */
1275 case 'B': searchar
= '?'; break;
1276 case 'd': constantypedefs
= TRUE
; break;
1277 case 't': typedefs
= TRUE
; break;
1278 case 'T': typedefs
= typedefs_or_cplusplus
= TRUE
; break;
1279 case 'u': update
= TRUE
; break;
1280 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1281 case 'x': cxref_style
= TRUE
; break;
1282 case 'w': no_warnings
= TRUE
; break;
1284 suggest_asking_for_help ();
1288 for (; optind
< argc
; optind
++)
1290 argbuffer
[current_arg
].arg_type
= at_filename
;
1291 argbuffer
[current_arg
].what
= argv
[optind
];
1296 argbuffer
[current_arg
].arg_type
= at_end
;
1299 print_help (argbuffer
);
1302 if (nincluded_files
== 0 && file_count
== 0)
1304 error ("no input files specified.", (char *)NULL
);
1305 suggest_asking_for_help ();
1309 if (tagfile
== NULL
)
1310 tagfile
= CTAGS
? "tags" : "TAGS";
1311 cwd
= etags_getcwd (); /* the current working directory */
1312 if (cwd
[strlen (cwd
) - 1] != '/')
1315 cwd
= concat (oldcwd
, "/", "");
1318 /* Relative file names are made relative to the current directory. */
1319 if (streq (tagfile
, "-")
1320 || strneq (tagfile
, "/dev/", 5))
1323 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1325 init (); /* set up boolean "functions" */
1327 linebuffer_init (&lb
);
1328 linebuffer_init (&filename_lb
);
1329 linebuffer_init (&filebuf
);
1330 linebuffer_init (&token_name
);
1334 if (streq (tagfile
, "-"))
1338 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1339 doesn't take effect until after `stdout' is already open). */
1340 if (!isatty (fileno (stdout
)))
1341 setmode (fileno (stdout
), O_BINARY
);
1345 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1351 * Loop through files finding functions.
1353 for (i
= 0; i
< current_arg
; i
++)
1355 static language
*lang
; /* non-NULL if language is forced */
1358 switch (argbuffer
[i
].arg_type
)
1361 lang
= argbuffer
[i
].lang
;
1363 #ifdef ETAGS_REGEXPS
1365 analyse_regex (argbuffer
[i
].what
);
1370 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1374 error ("can't find file %s\n", this_file
);
1379 this_file
= massage_name (this_file
);
1382 this_file
= argbuffer
[i
].what
;
1384 /* Input file named "-" means read file names from stdin
1385 (one per line) and use them. */
1386 if (streq (this_file
, "-"))
1389 fatal ("cannot parse standard input AND read file names from it",
1391 while (readline_internal (&filename_lb
, stdin
) > 0)
1392 process_file_name (filename_lb
.buffer
, lang
);
1395 process_file_name (this_file
, lang
);
1401 this_file
= argbuffer
[i
].what
;
1402 process_file (stdin
, this_file
, lang
);
1407 #ifdef ETAGS_REGEXPS
1409 #endif /* ETAGS_REGEXPS */
1411 free (filebuf
.buffer
);
1412 free (token_name
.buffer
);
1414 if (!CTAGS
|| cxref_style
)
1416 put_entries (nodehead
); /* write the remainig tags (ETAGS) */
1417 free_tree (nodehead
);
1423 /* Output file entries that have no tags. */
1424 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1426 fprintf (tagf
, "\f\n%s,0\n", fdp
->taggedfname
);
1428 while (nincluded_files
-- > 0)
1429 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1432 if (fclose (tagf
) == EOF
)
1434 exit (EXIT_SUCCESS
);
1440 for (i
= 0; i
< current_arg
; ++i
)
1442 switch (argbuffer
[i
].arg_type
)
1448 continue; /* the for loop */
1451 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1452 tagfile
, argbuffer
[i
].what
, tagfile
);
1453 if (system (cmd
) != EXIT_SUCCESS
)
1454 fatal ("failed to execute shell command", (char *)NULL
);
1456 append_to_tagfile
= TRUE
;
1459 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1462 put_entries (nodehead
); /* write all the tags (CTAGS) */
1463 free_tree (nodehead
);
1465 if (fclose (tagf
) == EOF
)
1470 char cmd
[2*BUFSIZ
+10];
1471 sprintf (cmd
, "sort -o %.*s %.*s", BUFSIZ
, tagfile
, BUFSIZ
, tagfile
);
1472 exit (system (cmd
));
1474 return EXIT_SUCCESS
;
1479 * Return a compressor given the file name. If EXTPTR is non-zero,
1480 * return a pointer into FILE where the compressor-specific
1481 * extension begins. If no compressor is found, NULL is returned
1482 * and EXTPTR is not significant.
1483 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1486 get_compressor_from_suffix (file
, extptr
)
1491 char *slash
, *suffix
;
1493 /* This relies on FN to be after canonicalize_filename,
1494 so we don't need to consider backslashes on DOS_NT. */
1495 slash
= etags_strrchr (file
, '/');
1496 suffix
= etags_strrchr (file
, '.');
1497 if (suffix
== NULL
|| suffix
< slash
)
1502 /* Let those poor souls who live with DOS 8+3 file name limits get
1503 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1504 Only the first do loop is run if not MSDOS */
1507 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1508 if (streq (compr
->suffix
, suffix
))
1511 break; /* do it only once: not really a loop */
1514 } while (*suffix
!= '\0');
1521 * Return a language given the name.
1524 get_language_from_langname (name
)
1530 error ("empty language name", (char *)NULL
);
1533 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1534 if (streq (name
, lang
->name
))
1536 error ("unknown language \"%s\"", name
);
1544 * Return a language given the interpreter name.
1547 get_language_from_interpreter (interpreter
)
1553 if (interpreter
== NULL
)
1555 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1556 if (lang
->interpreters
!= NULL
)
1557 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1558 if (streq (*iname
, interpreter
))
1567 * Return a language given the file name.
1570 get_language_from_filename (file
, case_sensitive
)
1572 bool case_sensitive
;
1575 char **name
, **ext
, *suffix
;
1577 /* Try whole file name first. */
1578 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1579 if (lang
->filenames
!= NULL
)
1580 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1581 if ((case_sensitive
)
1582 ? streq (*name
, file
)
1583 : strcaseeq (*name
, file
))
1586 /* If not found, try suffix after last dot. */
1587 suffix
= etags_strrchr (file
, '.');
1591 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1592 if (lang
->suffixes
!= NULL
)
1593 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1594 if ((case_sensitive
)
1595 ? streq (*ext
, suffix
)
1596 : strcaseeq (*ext
, suffix
))
1603 * This routine is called on each file argument.
1606 process_file_name (file
, lang
)
1610 struct stat stat_buf
;
1614 char *compressed_name
, *uncompressed_name
;
1615 char *ext
, *real_name
;
1618 canonicalize_filename (file
);
1619 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1621 error ("skipping inclusion of %s in self.", file
);
1624 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1626 compressed_name
= NULL
;
1627 real_name
= uncompressed_name
= savestr (file
);
1631 real_name
= compressed_name
= savestr (file
);
1632 uncompressed_name
= savenstr (file
, ext
- file
);
1635 /* If the canonicalized uncompressed name
1636 has already been dealt with, skip it silently. */
1637 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
1639 assert (fdp
->infname
!= NULL
);
1640 if (streq (uncompressed_name
, fdp
->infname
))
1644 if (stat (real_name
, &stat_buf
) != 0)
1646 /* Reset real_name and try with a different name. */
1648 if (compressed_name
!= NULL
) /* try with the given suffix */
1650 if (stat (uncompressed_name
, &stat_buf
) == 0)
1651 real_name
= uncompressed_name
;
1653 else /* try all possible suffixes */
1655 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1657 compressed_name
= concat (file
, ".", compr
->suffix
);
1658 if (stat (compressed_name
, &stat_buf
) != 0)
1662 char *suf
= compressed_name
+ strlen (file
);
1663 size_t suflen
= strlen (compr
->suffix
) + 1;
1664 for ( ; suf
[1]; suf
++, suflen
--)
1666 memmove (suf
, suf
+ 1, suflen
);
1667 if (stat (compressed_name
, &stat_buf
) == 0)
1669 real_name
= compressed_name
;
1673 if (real_name
!= NULL
)
1676 free (compressed_name
);
1677 compressed_name
= NULL
;
1681 real_name
= compressed_name
;
1686 if (real_name
== NULL
)
1691 } /* try with a different name */
1693 if (!S_ISREG (stat_buf
.st_mode
))
1695 error ("skipping %s: it is not a regular file.", real_name
);
1698 if (real_name
== compressed_name
)
1700 char *cmd
= concat (compr
->command
, " ", real_name
);
1701 inf
= (FILE *) popen (cmd
, "r");
1705 inf
= fopen (real_name
, "r");
1712 process_file (inf
, uncompressed_name
, lang
);
1714 if (real_name
== compressed_name
)
1715 retval
= pclose (inf
);
1717 retval
= fclose (inf
);
1722 if (compressed_name
) free (compressed_name
);
1723 if (uncompressed_name
) free (uncompressed_name
);
1730 process_file (fh
, fn
, lang
)
1735 static const fdesc emptyfdesc
;
1738 /* Create a new input file description entry. */
1739 fdp
= xnew (1, fdesc
);
1742 fdp
->infname
= savestr (fn
);
1744 fdp
->infabsname
= absolute_filename (fn
, cwd
);
1745 fdp
->infabsdir
= absolute_dirname (fn
, cwd
);
1746 if (filename_is_absolute (fn
))
1748 /* An absolute file name. Canonicalize it. */
1749 fdp
->taggedfname
= absolute_filename (fn
, NULL
);
1753 /* A file name relative to cwd. Make it relative
1754 to the directory of the tags file. */
1755 fdp
->taggedfname
= relative_filename (fn
, tagfiledir
);
1757 fdp
->usecharno
= TRUE
; /* use char position when making tags */
1759 fdp
->written
= FALSE
; /* not written on tags file yet */
1762 curfdp
= fdhead
; /* the current file description */
1766 /* If not Ctags, and if this is not metasource and if it contained no #line
1767 directives, we can write the tags and free all nodes pointing to
1770 && curfdp
->usecharno
/* no #line directives in this file */
1771 && !curfdp
->lang
->metasource
)
1775 /* Look for the head of the sublist relative to this file. See add_node
1776 for the structure of the node tree. */
1778 for (np
= nodehead
; np
!= NULL
; prev
= np
, np
= np
->left
)
1779 if (np
->fdp
== curfdp
)
1782 /* If we generated tags for this file, write and delete them. */
1785 /* This is the head of the last sublist, if any. The following
1786 instructions depend on this being true. */
1787 assert (np
->left
== NULL
);
1789 assert (fdhead
== curfdp
);
1790 assert (last_node
->fdp
== curfdp
);
1791 put_entries (np
); /* write tags for file curfdp->taggedfname */
1792 free_tree (np
); /* remove the written nodes */
1794 nodehead
= NULL
; /* no nodes left */
1796 prev
->left
= NULL
; /* delete the pointer to the sublist */
1802 * This routine sets up the boolean pseudo-functions which work
1803 * by setting boolean flags dependent upon the corresponding character.
1804 * Every char which is NOT in that string is not a white char. Therefore,
1805 * all of the array "_wht" is set to FALSE, and then the elements
1806 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1807 * of a char is TRUE if it is the string "white", else FALSE.
1815 for (i
= 0; i
< CHARS
; i
++)
1816 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1817 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1818 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1819 notinname('\0') = notinname('\n');
1820 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1821 begtoken('\0') = begtoken('\n');
1822 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1823 intoken('\0') = intoken('\n');
1824 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1825 endtoken('\0') = endtoken('\n');
1829 * This routine opens the specified file and calls the function
1830 * which finds the function and type definitions.
1837 language
*lang
= curfdp
->lang
;
1838 Lang_function
*parser
= NULL
;
1840 /* If user specified a language, use it. */
1841 if (lang
!= NULL
&& lang
->function
!= NULL
)
1843 parser
= lang
->function
;
1846 /* Else try to guess the language given the file name. */
1849 lang
= get_language_from_filename (curfdp
->infname
, TRUE
);
1850 if (lang
!= NULL
&& lang
->function
!= NULL
)
1852 curfdp
->lang
= lang
;
1853 parser
= lang
->function
;
1857 /* Else look for sharp-bang as the first two characters. */
1859 && readline_internal (&lb
, inf
) > 0
1861 && lb
.buffer
[0] == '#'
1862 && lb
.buffer
[1] == '!')
1866 /* Set lp to point at the first char after the last slash in the
1867 line or, if no slashes, at the first nonblank. Then set cp to
1868 the first successive blank and terminate the string. */
1869 lp
= etags_strrchr (lb
.buffer
+2, '/');
1873 lp
= skip_spaces (lb
.buffer
+ 2);
1874 cp
= skip_non_spaces (lp
);
1877 if (strlen (lp
) > 0)
1879 lang
= get_language_from_interpreter (lp
);
1880 if (lang
!= NULL
&& lang
->function
!= NULL
)
1882 curfdp
->lang
= lang
;
1883 parser
= lang
->function
;
1888 /* We rewind here, even if inf may be a pipe. We fail if the
1889 length of the first line is longer than the pipe block size,
1890 which is unlikely. */
1893 /* Else try to guess the language given the case insensitive file name. */
1896 lang
= get_language_from_filename (curfdp
->infname
, FALSE
);
1897 if (lang
!= NULL
&& lang
->function
!= NULL
)
1899 curfdp
->lang
= lang
;
1900 parser
= lang
->function
;
1904 /* Else try Fortran or C. */
1907 node
*old_last_node
= last_node
;
1909 curfdp
->lang
= get_language_from_langname ("fortran");
1912 if (old_last_node
== last_node
)
1913 /* No Fortran entries found. Try C. */
1915 /* We do not tag if rewind fails.
1916 Only the file name will be recorded in the tags file. */
1918 curfdp
->lang
= get_language_from_langname (cplusplus
? "c++" : "c");
1924 if (!no_line_directive
1925 && curfdp
->lang
!= NULL
&& curfdp
->lang
->metasource
)
1926 /* It may be that this is a bingo.y file, and we already parsed a bingo.c
1927 file, or anyway we parsed a file that is automatically generated from
1928 this one. If this is the case, the bingo.c file contained #line
1929 directives that generated tags pointing to this file. Let's delete
1930 them all before parsing this file, which is the real source. */
1932 fdesc
**fdpp
= &fdhead
;
1933 while (*fdpp
!= NULL
)
1935 && streq ((*fdpp
)->taggedfname
, curfdp
->taggedfname
))
1936 /* We found one of those! We must delete both the file description
1937 and all tags referring to it. */
1939 fdesc
*badfdp
= *fdpp
;
1941 /* Delete the tags referring to badfdp->taggedfname
1942 that were obtained from badfdp->infname. */
1943 invalidate_nodes (badfdp
, &nodehead
);
1945 *fdpp
= badfdp
->next
; /* remove the bad description from the list */
1946 free_fdesc (badfdp
);
1949 fdpp
= &(*fdpp
)->next
; /* advance the list pointer */
1952 assert (parser
!= NULL
);
1954 /* Generic initialisations before reading from file. */
1955 linebuffer_setlen (&filebuf
, 0); /* reset the file buffer */
1957 /* Generic initialisations before parsing file with readline. */
1958 lineno
= 0; /* reset global line number */
1959 charno
= 0; /* reset global char number */
1960 linecharno
= 0; /* reset global char number of line start */
1964 #ifdef ETAGS_REGEXPS
1965 regex_tag_multiline ();
1966 #endif /* ETAGS_REGEXPS */
1971 * Check whether an implicitly named tag should be created,
1972 * then call `pfnote'.
1973 * NAME is a string that is internally copied by this function.
1975 * TAGS format specification
1976 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1977 * The following is explained in some more detail in etc/ETAGS.EBNF.
1979 * make_tag creates tags with "implicit tag names" (unnamed tags)
1980 * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
1981 * 1. NAME does not contain any of the characters in NONAM;
1982 * 2. LINESTART contains name as either a rightmost, or rightmost but
1983 * one character, substring;
1984 * 3. the character, if any, immediately before NAME in LINESTART must
1985 * be a character in NONAM;
1986 * 4. the character, if any, immediately after NAME in LINESTART must
1987 * also be a character in NONAM.
1989 * The implementation uses the notinname() macro, which recognises the
1990 * characters stored in the string `nonam'.
1991 * etags.el needs to use the same characters that are in NONAM.
1994 make_tag (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1995 char *name
; /* tag name, or NULL if unnamed */
1996 int namelen
; /* tag length */
1997 bool is_func
; /* tag is a function */
1998 char *linestart
; /* start of the line where tag is */
1999 int linelen
; /* length of the line where tag is */
2000 int lno
; /* line number */
2001 long cno
; /* character number */
2003 bool named
= (name
!= NULL
&& namelen
> 0);
2005 if (!CTAGS
&& named
) /* maybe set named to false */
2006 /* Let's try to make an implicit tag name, that is, create an unnamed tag
2007 such that etags.el can guess a name from it. */
2010 register char *cp
= name
;
2012 for (i
= 0; i
< namelen
; i
++)
2013 if (notinname (*cp
++))
2015 if (i
== namelen
) /* rule #1 */
2017 cp
= linestart
+ linelen
- namelen
;
2018 if (notinname (linestart
[linelen
-1]))
2019 cp
-= 1; /* rule #4 */
2020 if (cp
>= linestart
/* rule #2 */
2022 || notinname (cp
[-1])) /* rule #3 */
2023 && strneq (name
, cp
, namelen
)) /* rule #2 */
2024 named
= FALSE
; /* use implicit tag name */
2029 name
= savenstr (name
, namelen
);
2032 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
2037 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
2038 char *name
; /* tag name, or NULL if unnamed */
2039 bool is_func
; /* tag is a function */
2040 char *linestart
; /* start of the line where tag is */
2041 int linelen
; /* length of the line where tag is */
2042 int lno
; /* line number */
2043 long cno
; /* character number */
2047 assert (name
== NULL
|| name
[0] != '\0');
2048 if (CTAGS
&& name
== NULL
)
2051 np
= xnew (1, node
);
2053 /* If ctags mode, change name "main" to M<thisfilename>. */
2054 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
2056 register char *fp
= etags_strrchr (curfdp
->taggedfname
, '/');
2057 np
->name
= concat ("M", fp
== NULL
? curfdp
->taggedfname
: fp
+ 1, "");
2058 fp
= etags_strrchr (np
->name
, '.');
2059 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
2065 np
->been_warned
= FALSE
;
2067 np
->is_func
= is_func
;
2069 if (np
->fdp
->usecharno
)
2070 /* Our char numbers are 0-base, because of C language tradition?
2071 ctags compatibility? old versions compatibility? I don't know.
2072 Anyway, since emacs's are 1-base we expect etags.el to take care
2073 of the difference. If we wanted to have 1-based numbers, we would
2074 uncomment the +1 below. */
2075 np
->cno
= cno
/* + 1 */ ;
2077 np
->cno
= invalidcharno
;
2078 np
->left
= np
->right
= NULL
;
2079 if (CTAGS
&& !cxref_style
)
2081 if (strlen (linestart
) < 50)
2082 np
->regex
= concat (linestart
, "$", "");
2084 np
->regex
= savenstr (linestart
, 50);
2087 np
->regex
= savenstr (linestart
, linelen
);
2089 add_node (np
, &nodehead
);
2094 * recurse on left children, iterate on right children.
2102 register node
*node_right
= np
->right
;
2103 free_tree (np
->left
);
2104 if (np
->name
!= NULL
)
2114 * delete a file description
2118 register fdesc
*fdp
;
2120 if (fdp
->infname
!= NULL
) free (fdp
->infname
);
2121 if (fdp
->infabsname
!= NULL
) free (fdp
->infabsname
);
2122 if (fdp
->infabsdir
!= NULL
) free (fdp
->infabsdir
);
2123 if (fdp
->taggedfname
!= NULL
) free (fdp
->taggedfname
);
2124 if (fdp
->prop
!= NULL
) free (fdp
->prop
);
2130 * Adds a node to the tree of nodes. In etags mode, sort by file
2131 * name. In ctags mode, sort by tag name. Make no attempt at
2134 * add_node is the only function allowed to add nodes, so it can
2138 add_node (np
, cur_node_p
)
2139 node
*np
, **cur_node_p
;
2142 register node
*cur_node
= *cur_node_p
;
2144 if (cur_node
== NULL
)
2154 /* For each file name, tags are in a linked sublist on the right
2155 pointer. The first tags of different files are a linked list
2156 on the left pointer. last_node points to the end of the last
2158 if (last_node
!= NULL
&& last_node
->fdp
== np
->fdp
)
2160 /* Let's use the same sublist as the last added node. */
2161 assert (last_node
->right
== NULL
);
2162 last_node
->right
= np
;
2165 else if (cur_node
->fdp
== np
->fdp
)
2167 /* Scanning the list we found the head of a sublist which is
2168 good for us. Let's scan this sublist. */
2169 add_node (np
, &cur_node
->right
);
2172 /* The head of this sublist is not good for us. Let's try the
2174 add_node (np
, &cur_node
->left
);
2175 } /* if ETAGS mode */
2180 dif
= strcmp (np
->name
, cur_node
->name
);
2183 * If this tag name matches an existing one, then
2184 * do not add the node, but maybe print a warning.
2188 if (np
->fdp
== cur_node
->fdp
)
2192 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
2193 np
->fdp
->infname
, lineno
, np
->name
);
2194 fprintf (stderr
, "Second entry ignored\n");
2197 else if (!cur_node
->been_warned
&& !no_warnings
)
2201 "Duplicate entry in files %s and %s: %s (Warning only)\n",
2202 np
->fdp
->infname
, cur_node
->fdp
->infname
, np
->name
);
2203 cur_node
->been_warned
= TRUE
;
2208 /* Actually add the node */
2209 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
2210 } /* if CTAGS mode */
2214 * invalidate_nodes ()
2215 * Scan the node tree and invalidate all nodes pointing to the
2216 * given file description (CTAGS case) or free them (ETAGS case).
2219 invalidate_nodes (badfdp
, npp
)
2230 if (np
->left
!= NULL
)
2231 invalidate_nodes (badfdp
, &np
->left
);
2232 if (np
->fdp
== badfdp
)
2234 if (np
->right
!= NULL
)
2235 invalidate_nodes (badfdp
, &np
->right
);
2239 assert (np
->fdp
!= NULL
);
2240 if (np
->fdp
== badfdp
)
2242 *npp
= np
->left
; /* detach the sublist from the list */
2243 np
->left
= NULL
; /* isolate it */
2244 free_tree (np
); /* free it */
2245 invalidate_nodes (badfdp
, npp
);
2248 invalidate_nodes (badfdp
, &np
->left
);
2253 static int total_size_of_entries
__P((node
*));
2254 static int number_len
__P((long));
2256 /* Length of a non-negative number's decimal representation. */
2262 while ((num
/= 10) > 0)
2268 * Return total number of characters that put_entries will output for
2269 * the nodes in the linked list at the right of the specified node.
2270 * This count is irrelevant with etags.el since emacs 19.34 at least,
2271 * but is still supplied for backward compatibility.
2274 total_size_of_entries (np
)
2277 register int total
= 0;
2279 for (; np
!= NULL
; np
= np
->right
)
2282 total
+= strlen (np
->regex
) + 1; /* pat\177 */
2283 if (np
->name
!= NULL
)
2284 total
+= strlen (np
->name
) + 1; /* name\001 */
2285 total
+= number_len ((long) np
->lno
) + 1; /* lno, */
2286 if (np
->cno
!= invalidcharno
) /* cno */
2287 total
+= number_len (np
->cno
);
2288 total
+= 1; /* newline */
2299 static fdesc
*fdp
= NULL
;
2304 /* Output subentries that precede this one */
2306 put_entries (np
->left
);
2308 /* Output this entry */
2317 fprintf (tagf
, "\f\n%s,%d\n",
2318 fdp
->taggedfname
, total_size_of_entries (np
));
2319 fdp
->written
= TRUE
;
2321 fputs (np
->regex
, tagf
);
2322 fputc ('\177', tagf
);
2323 if (np
->name
!= NULL
)
2325 fputs (np
->name
, tagf
);
2326 fputc ('\001', tagf
);
2328 fprintf (tagf
, "%d,", np
->lno
);
2329 if (np
->cno
!= invalidcharno
)
2330 fprintf (tagf
, "%ld", np
->cno
);
2336 if (np
->name
== NULL
)
2337 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
2342 fprintf (stdout
, "%s %s %d\n",
2343 np
->name
, np
->fdp
->taggedfname
, (np
->lno
+ 63) / 64);
2345 fprintf (stdout
, "%-16s %3d %-16s %s\n",
2346 np
->name
, np
->lno
, np
->fdp
->taggedfname
, np
->regex
);
2350 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->fdp
->taggedfname
);
2353 { /* function or #define macro with args */
2354 putc (searchar
, tagf
);
2357 for (sp
= np
->regex
; *sp
; sp
++)
2359 if (*sp
== '\\' || *sp
== searchar
)
2363 putc (searchar
, tagf
);
2366 { /* anything else; text pattern inadequate */
2367 fprintf (tagf
, "%d", np
->lno
);
2372 } /* if this node contains a valid tag */
2374 /* Output subentries that follow this one */
2375 put_entries (np
->right
);
2377 put_entries (np
->left
);
2382 #define C_EXT 0x00fff /* C extensions */
2383 #define C_PLAIN 0x00000 /* C */
2384 #define C_PLPL 0x00001 /* C++ */
2385 #define C_STAR 0x00003 /* C* */
2386 #define C_JAVA 0x00005 /* JAVA */
2387 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
2388 #define YACC 0x10000 /* yacc file */
2391 * The C symbol tables.
2396 st_C_objprot
, st_C_objimpl
, st_C_objend
,
2398 st_C_ignore
, st_C_attribute
,
2401 st_C_class
, st_C_template
,
2402 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
2405 static unsigned int hash
__P((const char *, unsigned int));
2406 static struct C_stab_entry
* in_word_set
__P((const char *, unsigned int));
2407 static enum sym_type C_symtype
__P((char *, int, int));
2409 /* Feed stuff between (but not including) %[ and %] lines to:
2415 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2419 while, 0, st_C_ignore
2420 switch, 0, st_C_ignore
2421 return, 0, st_C_ignore
2422 __attribute__, 0, st_C_attribute
2423 @interface, 0, st_C_objprot
2424 @protocol, 0, st_C_objprot
2425 @implementation,0, st_C_objimpl
2426 @end, 0, st_C_objend
2427 import, (C_JAVA & !C_PLPL), st_C_ignore
2428 package, (C_JAVA & !C_PLPL), st_C_ignore
2429 friend, C_PLPL, st_C_ignore
2430 extends, (C_JAVA & !C_PLPL), st_C_javastruct
2431 implements, (C_JAVA & !C_PLPL), st_C_javastruct
2432 interface, (C_JAVA & !C_PLPL), st_C_struct
2433 class, 0, st_C_class
2434 namespace, C_PLPL, st_C_struct
2435 domain, C_STAR, st_C_struct
2436 union, 0, st_C_struct
2437 struct, 0, st_C_struct
2438 extern, 0, st_C_extern
2440 typedef, 0, st_C_typedef
2441 define, 0, st_C_define
2442 operator, C_PLPL, st_C_operator
2443 template, 0, st_C_template
2444 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2445 DEFUN, 0, st_C_gnumacro
2446 SYSCALL, 0, st_C_gnumacro
2447 ENTRY, 0, st_C_gnumacro
2448 PSEUDO, 0, st_C_gnumacro
2449 # These are defined inside C functions, so currently they are not met.
2450 # EXFUN used in glibc, DEFVAR_* in emacs.
2451 #EXFUN, 0, st_C_gnumacro
2452 #DEFVAR_, 0, st_C_gnumacro
2454 and replace lines between %< and %> with its output, then:
2455 - remove the #if characterset check
2456 - make in_word_set static and not inline. */
2458 /* C code produced by gperf version 3.0.1 */
2459 /* Command-line: gperf -m 5 */
2460 /* Computed positions: -k'1-2' */
2462 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
2463 /* maximum key range = 31, duplicates = 0 */
2474 register const char *str
;
2475 register unsigned int len
;
2477 static unsigned char asso_values
[] =
2479 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2480 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2481 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2482 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2483 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2484 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2485 34, 34, 34, 34, 1, 34, 34, 34, 14, 14,
2486 34, 34, 34, 34, 34, 34, 34, 34, 13, 34,
2487 13, 34, 34, 12, 34, 34, 34, 34, 34, 11,
2488 34, 34, 34, 34, 34, 8, 34, 11, 34, 12,
2489 11, 0, 1, 34, 7, 0, 34, 34, 11, 9,
2490 0, 4, 0, 34, 7, 4, 14, 21, 34, 15,
2491 0, 2, 34, 34, 34, 34, 34, 34, 34, 34,
2492 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2493 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2494 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2495 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2496 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2497 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2498 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2499 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2500 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2501 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2502 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2503 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
2504 34, 34, 34, 34, 34, 34
2506 return len
+ asso_values
[(unsigned char)str
[1]] + asso_values
[(unsigned char)str
[0]];
2509 static struct C_stab_entry
*
2510 in_word_set (str
, len
)
2511 register const char *str
;
2512 register unsigned int len
;
2516 TOTAL_KEYWORDS
= 31,
2517 MIN_WORD_LENGTH
= 2,
2518 MAX_WORD_LENGTH
= 15,
2523 static struct C_stab_entry wordlist
[] =
2526 {"if", 0, st_C_ignore
},
2527 {"enum", 0, st_C_enum
},
2528 {"@end", 0, st_C_objend
},
2529 {"extern", 0, st_C_extern
},
2530 {"extends", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2531 {"for", 0, st_C_ignore
},
2532 {"interface", (C_JAVA
& !C_PLPL
), st_C_struct
},
2533 {"@protocol", 0, st_C_objprot
},
2534 {"@interface", 0, st_C_objprot
},
2535 {"operator", C_PLPL
, st_C_operator
},
2536 {"return", 0, st_C_ignore
},
2537 {"friend", C_PLPL
, st_C_ignore
},
2538 {"import", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2539 {"@implementation",0, st_C_objimpl
},
2540 {"define", 0, st_C_define
},
2541 {"package", (C_JAVA
& !C_PLPL
), st_C_ignore
},
2542 {"implements", (C_JAVA
& !C_PLPL
), st_C_javastruct
},
2543 {"namespace", C_PLPL
, st_C_struct
},
2544 {"domain", C_STAR
, st_C_struct
},
2545 {"template", 0, st_C_template
},
2546 {"typedef", 0, st_C_typedef
},
2547 {"struct", 0, st_C_struct
},
2548 {"switch", 0, st_C_ignore
},
2549 {"union", 0, st_C_struct
},
2550 {"while", 0, st_C_ignore
},
2551 {"class", 0, st_C_class
},
2552 {"__attribute__", 0, st_C_attribute
},
2553 {"SYSCALL", 0, st_C_gnumacro
},
2554 {"PSEUDO", 0, st_C_gnumacro
},
2555 {"ENTRY", 0, st_C_gnumacro
},
2556 {"DEFUN", 0, st_C_gnumacro
}
2559 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2561 register int key
= hash (str
, len
);
2563 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2565 register const char *s
= wordlist
[key
].name
;
2567 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1) && s
[len
] == '\0')
2568 return &wordlist
[key
];
2575 static enum sym_type
2576 C_symtype (str
, len
, c_ext
)
2581 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2583 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2590 * Ignoring __attribute__ ((list))
2592 static bool inattribute
; /* looking at an __attribute__ construct */
2595 * C functions and variables are recognized using a simple
2596 * finite automaton. fvdef is its state variable.
2600 fvnone
, /* nothing seen */
2601 fdefunkey
, /* Emacs DEFUN keyword seen */
2602 fdefunname
, /* Emacs DEFUN name seen */
2603 foperator
, /* func: operator keyword seen (cplpl) */
2604 fvnameseen
, /* function or variable name seen */
2605 fstartlist
, /* func: just after open parenthesis */
2606 finlist
, /* func: in parameter list */
2607 flistseen
, /* func: after parameter list */
2608 fignore
, /* func: before open brace */
2609 vignore
/* var-like: ignore until ';' */
2612 static bool fvextern
; /* func or var: extern keyword seen; */
2615 * typedefs are recognized using a simple finite automaton.
2616 * typdef is its state variable.
2620 tnone
, /* nothing seen */
2621 tkeyseen
, /* typedef keyword seen */
2622 ttypeseen
, /* defined type seen */
2623 tinbody
, /* inside typedef body */
2624 tend
, /* just before typedef tag */
2625 tignore
/* junk after typedef tag */
2629 * struct-like structures (enum, struct and union) are recognized
2630 * using another simple finite automaton. `structdef' is its state
2635 snone
, /* nothing seen yet,
2636 or in struct body if bracelev > 0 */
2637 skeyseen
, /* struct-like keyword seen */
2638 stagseen
, /* struct-like tag seen */
2639 scolonseen
/* colon seen after struct-like tag */
2643 * When objdef is different from onone, objtag is the name of the class.
2645 static char *objtag
= "<uninited>";
2648 * Yet another little state machine to deal with preprocessor lines.
2652 dnone
, /* nothing seen */
2653 dsharpseen
, /* '#' seen as first char on line */
2654 ddefineseen
, /* '#' and 'define' seen */
2655 dignorerest
/* ignore rest of line */
2659 * State machine for Objective C protocols and implementations.
2660 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2664 onone
, /* nothing seen */
2665 oprotocol
, /* @interface or @protocol seen */
2666 oimplementation
, /* @implementations seen */
2667 otagseen
, /* class name seen */
2668 oparenseen
, /* parenthesis before category seen */
2669 ocatseen
, /* category name seen */
2670 oinbody
, /* in @implementation body */
2671 omethodsign
, /* in @implementation body, after +/- */
2672 omethodtag
, /* after method name */
2673 omethodcolon
, /* after method colon */
2674 omethodparm
, /* after method parameter */
2675 oignore
/* wait for @end */
2680 * Use this structure to keep info about the token read, and how it
2681 * should be tagged. Used by the make_C_tag function to build a tag.
2685 char *line
; /* string containing the token */
2686 int offset
; /* where the token starts in LINE */
2687 int length
; /* token length */
2689 The previous members can be used to pass strings around for generic
2690 purposes. The following ones specifically refer to creating tags. In this
2691 case the token contained here is the pattern that will be used to create a
2694 bool valid
; /* do not create a tag; the token should be
2695 invalidated whenever a state machine is
2696 reset prematurely */
2697 bool named
; /* create a named tag */
2698 int lineno
; /* source line number of tag */
2699 long linepos
; /* source char number of tag */
2700 } token
; /* latest token read */
2703 * Variables and functions for dealing with nested structures.
2704 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2706 static void pushclass_above
__P((int, char *, int));
2707 static void popclass_above
__P((int));
2708 static void write_classname
__P((linebuffer
*, char *qualifier
));
2711 char **cname
; /* nested class names */
2712 int *bracelev
; /* nested class brace level */
2713 int nl
; /* class nesting level (elements used) */
2714 int size
; /* length of the array */
2715 } cstack
; /* stack for nested declaration tags */
2716 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2717 #define nestlev (cstack.nl)
2718 /* After struct keyword or in struct body, not inside a nested function. */
2719 #define instruct (structdef == snone && nestlev > 0 \
2720 && bracelev == cstack.bracelev[nestlev-1] + 1)
2723 pushclass_above (bracelev
, str
, len
)
2730 popclass_above (bracelev
);
2732 if (nl
>= cstack
.size
)
2734 int size
= cstack
.size
*= 2;
2735 xrnew (cstack
.cname
, size
, char *);
2736 xrnew (cstack
.bracelev
, size
, int);
2738 assert (nl
== 0 || cstack
.bracelev
[nl
-1] < bracelev
);
2739 cstack
.cname
[nl
] = (str
== NULL
) ? NULL
: savenstr (str
, len
);
2740 cstack
.bracelev
[nl
] = bracelev
;
2745 popclass_above (bracelev
)
2750 for (nl
= cstack
.nl
- 1;
2751 nl
>= 0 && cstack
.bracelev
[nl
] >= bracelev
;
2754 if (cstack
.cname
[nl
] != NULL
)
2755 free (cstack
.cname
[nl
]);
2761 write_classname (cn
, qualifier
)
2766 int qlen
= strlen (qualifier
);
2768 if (cstack
.nl
== 0 || cstack
.cname
[0] == NULL
)
2772 cn
->buffer
[0] = '\0';
2776 len
= strlen (cstack
.cname
[0]);
2777 linebuffer_setlen (cn
, len
);
2778 strcpy (cn
->buffer
, cstack
.cname
[0]);
2780 for (i
= 1; i
< cstack
.nl
; i
++)
2785 s
= cstack
.cname
[i
];
2790 linebuffer_setlen (cn
, len
);
2791 strncat (cn
->buffer
, qualifier
, qlen
);
2792 strncat (cn
->buffer
, s
, slen
);
2797 static bool consider_token
__P((char *, int, int, int *, int, int, bool *));
2798 static void make_C_tag
__P((bool));
2802 * checks to see if the current token is at the start of a
2803 * function or variable, or corresponds to a typedef, or
2804 * is a struct/union/enum tag, or #define, or an enum constant.
2806 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2807 * with args. C_EXTP points to which language we are looking at.
2818 consider_token (str
, len
, c
, c_extp
, bracelev
, parlev
, is_func_or_var
)
2819 register char *str
; /* IN: token pointer */
2820 register int len
; /* IN: token length */
2821 register int c
; /* IN: first char after the token */
2822 int *c_extp
; /* IN, OUT: C extensions mask */
2823 int bracelev
; /* IN: brace level */
2824 int parlev
; /* IN: parenthesis level */
2825 bool *is_func_or_var
; /* OUT: function or variable found */
2827 /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
2828 structtype is the type of the preceding struct-like keyword, and
2829 structbracelev is the brace level where it has been seen. */
2830 static enum sym_type structtype
;
2831 static int structbracelev
;
2832 static enum sym_type toktype
;
2835 toktype
= C_symtype (str
, len
, *c_extp
);
2838 * Skip __attribute__
2840 if (toktype
== st_C_attribute
)
2847 * Advance the definedef state machine.
2852 /* We're not on a preprocessor line. */
2853 if (toktype
== st_C_gnumacro
)
2860 if (toktype
== st_C_define
)
2862 definedef
= ddefineseen
;
2866 definedef
= dignorerest
;
2871 * Make a tag for any macro, unless it is a constant
2872 * and constantypedefs is FALSE.
2874 definedef
= dignorerest
;
2875 *is_func_or_var
= (c
== '(');
2876 if (!*is_func_or_var
&& !constantypedefs
)
2883 error ("internal error: definedef value.", (char *)NULL
);
2892 if (toktype
== st_C_typedef
)
2912 if (structdef
== snone
&& fvdef
== fvnone
)
2930 * This structdef business is NOT invoked when we are ctags and the
2931 * file is plain C. This is because a struct tag may have the same
2932 * name as another tag, and this loses with ctags.
2936 case st_C_javastruct
:
2937 if (structdef
== stagseen
)
2938 structdef
= scolonseen
;
2942 if ((*c_extp
& C_AUTO
) /* automatic detection of C++ language */
2944 && definedef
== dnone
&& structdef
== snone
2945 && typdef
== tnone
&& fvdef
== fvnone
)
2946 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2947 if (toktype
== st_C_template
)
2954 && (typdef
== tkeyseen
2955 || (typedefs_or_cplusplus
&& structdef
== snone
)))
2957 structdef
= skeyseen
;
2958 structtype
= toktype
;
2959 structbracelev
= bracelev
;
2960 if (fvdef
== fvnameseen
)
2966 if (structdef
== skeyseen
)
2968 structdef
= stagseen
;
2972 if (typdef
!= tnone
)
2975 /* Detect Objective C constructs. */
2985 objdef
= oimplementation
;
2989 case oimplementation
:
2990 /* Save the class tag for functions or variables defined inside. */
2991 objtag
= savenstr (str
, len
);
2995 /* Save the class tag for categories. */
2996 objtag
= savenstr (str
, len
);
2998 *is_func_or_var
= TRUE
;
3002 *is_func_or_var
= TRUE
;
3010 objdef
= omethodtag
;
3011 linebuffer_setlen (&token_name
, len
);
3012 strncpy (token_name
.buffer
, str
, len
);
3013 token_name
.buffer
[len
] = '\0';
3019 objdef
= omethodparm
;
3025 objdef
= omethodtag
;
3026 linebuffer_setlen (&token_name
, token_name
.len
+ len
);
3027 strncat (token_name
.buffer
, str
, len
);
3032 if (toktype
== st_C_objend
)
3034 /* Memory leakage here: the string pointed by objtag is
3035 never released, because many tests would be needed to
3036 avoid breaking on incorrect input code. The amount of
3037 memory leaked here is the sum of the lengths of the
3045 /* A function, variable or enum constant? */
3067 *is_func_or_var
= TRUE
;
3071 && structdef
== snone
3072 && structtype
== st_C_enum
&& bracelev
> structbracelev
)
3073 return TRUE
; /* enum constant */
3079 fvdef
= fdefunname
; /* GNU macro */
3080 *is_func_or_var
= TRUE
;
3088 if ((strneq (str
, "asm", 3) && endtoken (str
[3]))
3089 || (strneq (str
, "__asm__", 7) && endtoken (str
[7])))
3098 if (len
>= 10 && strneq (str
+len
-10, "::operator", 10))
3100 if (*c_extp
& C_AUTO
) /* automatic detection of C++ */
3101 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
3103 *is_func_or_var
= TRUE
;
3106 if (bracelev
> 0 && !instruct
)
3108 fvdef
= fvnameseen
; /* function or variable */
3109 *is_func_or_var
= TRUE
;
3120 * C_entries often keeps pointers to tokens or lines which are older than
3121 * the line currently read. By keeping two line buffers, and switching
3122 * them at end of line, it is possible to use those pointers.
3130 #define current_lb_is_new (newndx == curndx)
3131 #define switch_line_buffers() (curndx = 1 - curndx)
3133 #define curlb (lbs[curndx].lb)
3134 #define newlb (lbs[newndx].lb)
3135 #define curlinepos (lbs[curndx].linepos)
3136 #define newlinepos (lbs[newndx].linepos)
3138 #define plainc ((c_ext & C_EXT) == C_PLAIN)
3139 #define cplpl (c_ext & C_PLPL)
3140 #define cjava ((c_ext & C_JAVA) == C_JAVA)
3142 #define CNL_SAVE_DEFINEDEF() \
3144 curlinepos = charno; \
3145 readline (&curlb, inf); \
3146 lp = curlb.buffer; \
3153 CNL_SAVE_DEFINEDEF(); \
3154 if (savetoken.valid) \
3156 token = savetoken; \
3157 savetoken.valid = FALSE; \
3159 definedef = dnone; \
3167 /* This function should never be called when token.valid is FALSE, but
3168 we must protect against invalid input or internal errors. */
3169 if (!DEBUG
&& !token
.valid
)
3173 make_tag (token_name
.buffer
, token_name
.len
, isfun
, token
.line
,
3174 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3175 else /* this case is optimised away if !DEBUG */
3176 make_tag (concat ("INVALID TOKEN:-->", token_name
.buffer
, ""),
3177 token_name
.len
+ 17, isfun
, token
.line
,
3178 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
3180 token
.valid
= FALSE
;
3186 * This routine finds functions, variables, typedefs,
3187 * #define's, enum constants and struct/union/enum definitions in
3188 * C syntax and adds them to the list.
3191 C_entries (c_ext
, inf
)
3192 int c_ext
; /* extension of C */
3193 FILE *inf
; /* input file */
3195 register char c
; /* latest char read; '\0' for end of line */
3196 register char *lp
; /* pointer one beyond the character `c' */
3197 int curndx
, newndx
; /* indices for current and new lb */
3198 register int tokoff
; /* offset in line of start of current token */
3199 register int toklen
; /* length of current token */
3200 char *qualifier
; /* string used to qualify names */
3201 int qlen
; /* length of qualifier */
3202 int bracelev
; /* current brace level */
3203 int bracketlev
; /* current bracket level */
3204 int parlev
; /* current parenthesis level */
3205 int attrparlev
; /* __attribute__ parenthesis level */
3206 int templatelev
; /* current template level */
3207 int typdefbracelev
; /* bracelev where a typedef struct body begun */
3208 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
3209 bool yacc_rules
; /* in the rules part of a yacc file */
3210 struct tok savetoken
; /* token saved during preprocessor handling */
3213 linebuffer_init (&lbs
[0].lb
);
3214 linebuffer_init (&lbs
[1].lb
);
3215 if (cstack
.size
== 0)
3217 cstack
.size
= (DEBUG
) ? 1 : 4;
3219 cstack
.cname
= xnew (cstack
.size
, char *);
3220 cstack
.bracelev
= xnew (cstack
.size
, int);
3223 tokoff
= toklen
= typdefbracelev
= 0; /* keep compiler quiet */
3224 curndx
= newndx
= 0;
3228 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
3229 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
3231 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3232 token
.valid
= savetoken
.valid
= FALSE
;
3233 bracelev
= bracketlev
= parlev
= attrparlev
= templatelev
= 0;
3235 { qualifier
= "."; qlen
= 1; }
3237 { qualifier
= "::"; qlen
= 2; }
3245 /* If we are at the end of the line, the next character is a
3246 '\0'; do not skip it, because it is what tells us
3247 to read the next line. */
3268 /* Newlines inside comments do not end macro definitions in
3270 CNL_SAVE_DEFINEDEF ();
3283 /* Newlines inside strings do not end macro definitions
3284 in traditional cpp, even though compilers don't
3285 usually accept them. */
3286 CNL_SAVE_DEFINEDEF ();
3296 /* Hmmm, something went wrong. */
3305 else if (bracketlev
> 0)
3310 if (--bracketlev
> 0)
3314 CNL_SAVE_DEFINEDEF ();
3342 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
3355 else if (/* cplpl && */ *lp
== '/')
3363 if ((c_ext
& YACC
) && *lp
== '%')
3365 /* Entering or exiting rules section in yacc file. */
3367 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
3368 typdef
= tnone
; structdef
= snone
;
3369 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
3371 yacc_rules
= !yacc_rules
;
3377 if (definedef
== dnone
)
3380 bool cpptoken
= TRUE
;
3382 /* Look back on this line. If all blanks, or nonblanks
3383 followed by an end of comment, this is a preprocessor
3385 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
3388 if (*cp
== '*' && *(cp
+1) == '/')
3397 definedef
= dsharpseen
;
3398 } /* if (definedef == dnone) */
3406 /* Consider token only if some involved conditions are satisfied. */
3407 if (typdef
!= tignore
3408 && definedef
!= dignorerest
3411 && (definedef
!= dnone
3412 || structdef
!= scolonseen
)
3419 if (c
== ':' && *lp
== ':' && begtoken (lp
[1]))
3420 /* This handles :: in the middle,
3421 but not at the beginning of an identifier.
3422 Also, space-separated :: is not recognised. */
3424 if (c_ext
& C_AUTO
) /* automatic detection of C++ */
3425 c_ext
= (c_ext
| C_PLPL
) & ~C_AUTO
;
3429 goto still_in_token
;
3433 bool funorvar
= FALSE
;
3436 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
3437 &c_ext
, bracelev
, parlev
,
3440 if (fvdef
== foperator
)
3443 lp
= skip_spaces (lp
-1);
3447 && !iswhite (*lp
) && *lp
!= '(')
3450 toklen
+= lp
- oldlp
;
3452 token
.named
= FALSE
;
3454 && nestlev
> 0 && definedef
== dnone
)
3455 /* in struct body */
3457 write_classname (&token_name
, qualifier
);
3458 linebuffer_setlen (&token_name
,
3459 token_name
.len
+qlen
+toklen
);
3460 strcat (token_name
.buffer
, qualifier
);
3461 strncat (token_name
.buffer
,
3462 newlb
.buffer
+ tokoff
, toklen
);
3465 else if (objdef
== ocatseen
)
3466 /* Objective C category */
3468 int len
= strlen (objtag
) + 2 + toklen
;
3469 linebuffer_setlen (&token_name
, len
);
3470 strcpy (token_name
.buffer
, objtag
);
3471 strcat (token_name
.buffer
, "(");
3472 strncat (token_name
.buffer
,
3473 newlb
.buffer
+ tokoff
, toklen
);
3474 strcat (token_name
.buffer
, ")");
3477 else if (objdef
== omethodtag
3478 || objdef
== omethodparm
)
3479 /* Objective C method */
3483 else if (fvdef
== fdefunname
)
3484 /* GNU DEFUN and similar macros */
3486 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
3490 /* Rewrite the tag so that emacs lisp DEFUNs
3491 can be found by their elisp name */
3498 linebuffer_setlen (&token_name
, len
);
3499 strncpy (token_name
.buffer
,
3500 newlb
.buffer
+ off
, len
);
3501 token_name
.buffer
[len
] = '\0';
3504 if (token_name
.buffer
[len
] == '_')
3505 token_name
.buffer
[len
] = '-';
3506 token
.named
= defun
;
3510 linebuffer_setlen (&token_name
, toklen
);
3511 strncpy (token_name
.buffer
,
3512 newlb
.buffer
+ tokoff
, toklen
);
3513 token_name
.buffer
[toklen
] = '\0';
3514 /* Name macros and members. */
3515 token
.named
= (structdef
== stagseen
3516 || typdef
== ttypeseen
3519 && definedef
== dignorerest
)
3521 && definedef
== dnone
3522 && structdef
== snone
3525 token
.lineno
= lineno
;
3526 token
.offset
= tokoff
;
3527 token
.length
= toklen
;
3528 token
.line
= newlb
.buffer
;
3529 token
.linepos
= newlinepos
;
3532 if (definedef
== dnone
3533 && (fvdef
== fvnameseen
3534 || fvdef
== foperator
3535 || structdef
== stagseen
3537 || typdef
== ttypeseen
3538 || objdef
!= onone
))
3540 if (current_lb_is_new
)
3541 switch_line_buffers ();
3543 else if (definedef
!= dnone
3544 || fvdef
== fdefunname
3546 make_C_tag (funorvar
);
3548 else /* not yacc and consider_token failed */
3550 if (inattribute
&& fvdef
== fignore
)
3552 /* We have just met __attribute__ after a
3553 function parameter list: do not tag the
3560 } /* if (endtoken (c)) */
3561 else if (intoken (c
))
3567 } /* if (midtoken) */
3568 else if (begtoken (c
))
3576 /* This prevents tagging fb in
3577 void (__attribute__((noreturn)) *fb) (void);
3578 Fixing this is not easy and not very important. */
3582 if (plainc
|| declarations
)
3584 make_C_tag (TRUE
); /* a function */
3589 if (structdef
== stagseen
&& !cjava
)
3591 popclass_above (bracelev
);
3599 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
3601 tokoff
= lp
- 1 - newlb
.buffer
;
3606 } /* if (begtoken) */
3607 } /* if must look at token */
3610 /* Detect end of line, colon, comma, semicolon and various braces
3611 after having handled a token.*/
3617 if (yacc_rules
&& token
.offset
== 0 && token
.valid
)
3619 make_C_tag (FALSE
); /* a yacc function */
3622 if (definedef
!= dnone
)
3628 make_C_tag (TRUE
); /* an Objective C class */
3632 objdef
= omethodcolon
;
3633 linebuffer_setlen (&token_name
, token_name
.len
+ 1);
3634 strcat (token_name
.buffer
, ":");
3637 if (structdef
== stagseen
)
3639 structdef
= scolonseen
;
3642 /* Should be useless, but may be work as a safety net. */
3643 if (cplpl
&& fvdef
== flistseen
)
3645 make_C_tag (TRUE
); /* a function */
3651 if (definedef
!= dnone
|| inattribute
)
3657 make_C_tag (FALSE
); /* a typedef */
3667 if (typdef
== tignore
|| cplpl
)
3671 if ((globals
&& bracelev
== 0 && (!fvextern
|| declarations
))
3672 || (members
&& instruct
))
3673 make_C_tag (FALSE
); /* a variable */
3676 token
.valid
= FALSE
;
3680 && (cplpl
|| !instruct
)
3681 && (typdef
== tnone
|| (typdef
!= tignore
&& instruct
)))
3683 && plainc
&& instruct
))
3684 make_C_tag (TRUE
); /* a function */
3690 && cplpl
&& structdef
== stagseen
)
3691 make_C_tag (FALSE
); /* forward declaration */
3693 token
.valid
= FALSE
;
3694 } /* switch (fvdef) */
3700 if (structdef
== stagseen
)
3704 if (definedef
!= dnone
|| inattribute
)
3710 make_C_tag (TRUE
); /* an Objective C method */
3731 && (!fvextern
|| declarations
))
3732 || (members
&& instruct
)))
3733 make_C_tag (FALSE
); /* a variable */
3736 if ((declarations
&& typdef
== tnone
&& !instruct
)
3737 || (members
&& typdef
!= tignore
&& instruct
))
3739 make_C_tag (TRUE
); /* a function */
3742 else if (!declarations
)
3744 token
.valid
= FALSE
;
3749 if (structdef
== stagseen
)
3753 if (definedef
!= dnone
|| inattribute
)
3755 if (structdef
== stagseen
)
3762 make_C_tag (FALSE
); /* a typedef */
3774 if ((members
&& bracelev
== 1)
3775 || (globals
&& bracelev
== 0
3776 && (!fvextern
|| declarations
)))
3777 make_C_tag (FALSE
); /* a variable */
3791 if (definedef
!= dnone
)
3793 if (objdef
== otagseen
&& parlev
== 0)
3794 objdef
= oparenseen
;
3798 if (typdef
== ttypeseen
3802 /* This handles constructs like:
3803 typedef void OperatorFun (int fun); */
3822 if (--attrparlev
== 0)
3823 inattribute
= FALSE
;
3826 if (definedef
!= dnone
)
3828 if (objdef
== ocatseen
&& parlev
== 1)
3830 make_C_tag (TRUE
); /* an Objective C category */
3844 || typdef
== ttypeseen
))
3847 make_C_tag (FALSE
); /* a typedef */
3850 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3854 if (definedef
!= dnone
)
3856 if (typdef
== ttypeseen
)
3858 /* Whenever typdef is set to tinbody (currently only
3859 here), typdefbracelev should be set to bracelev. */
3861 typdefbracelev
= bracelev
;
3866 make_C_tag (TRUE
); /* a function */
3875 make_C_tag (TRUE
); /* an Objective C class */
3880 make_C_tag (TRUE
); /* an Objective C method */
3884 /* Neutralize `extern "C" {' grot. */
3885 if (bracelev
== 0 && structdef
== snone
&& nestlev
== 0
3893 case skeyseen
: /* unnamed struct */
3894 pushclass_above (bracelev
, NULL
, 0);
3897 case stagseen
: /* named struct or enum */
3898 case scolonseen
: /* a class */
3899 pushclass_above (bracelev
,token
.line
+token
.offset
, token
.length
);
3901 make_C_tag (FALSE
); /* a struct or enum */
3907 if (definedef
!= dnone
)
3909 if (fvdef
== fstartlist
)
3911 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3912 token
.valid
= FALSE
;
3916 if (definedef
!= dnone
)
3918 if (!ignoreindent
&& lp
== newlb
.buffer
+ 1)
3921 token
.valid
= FALSE
;
3922 bracelev
= 0; /* reset brace level if first column */
3923 parlev
= 0; /* also reset paren level, just in case... */
3925 else if (bracelev
> 0)
3928 token
.valid
= FALSE
; /* something gone amiss, token unreliable */
3929 popclass_above (bracelev
);
3931 /* Only if typdef == tinbody is typdefbracelev significant. */
3932 if (typdef
== tinbody
&& bracelev
<= typdefbracelev
)
3934 assert (bracelev
== typdefbracelev
);
3939 if (definedef
!= dnone
)
3949 if ((members
&& bracelev
== 1)
3950 || (globals
&& bracelev
== 0 && (!fvextern
|| declarations
)))
3951 make_C_tag (FALSE
); /* a variable */
3959 && (structdef
== stagseen
|| fvdef
== fvnameseen
))
3966 if (templatelev
> 0)
3974 if (objdef
== oinbody
&& bracelev
== 0)
3976 objdef
= omethodsign
;
3981 case '#': case '~': case '&': case '%': case '/':
3982 case '|': case '^': case '!': case '.': case '?':
3983 if (definedef
!= dnone
)
3985 /* These surely cannot follow a function tag in C. */
3998 if (objdef
== otagseen
)
4000 make_C_tag (TRUE
); /* an Objective C class */
4003 /* If a macro spans multiple lines don't reset its state. */
4005 CNL_SAVE_DEFINEDEF ();
4011 } /* while not eof */
4013 free (lbs
[0].lb
.buffer
);
4014 free (lbs
[1].lb
.buffer
);
4018 * Process either a C++ file or a C file depending on the setting
4022 default_C_entries (inf
)
4025 C_entries (cplusplus
? C_PLPL
: C_AUTO
, inf
);
4028 /* Always do plain C. */
4030 plain_C_entries (inf
)
4036 /* Always do C++. */
4038 Cplusplus_entries (inf
)
4041 C_entries (C_PLPL
, inf
);
4044 /* Always do Java. */
4049 C_entries (C_JAVA
, inf
);
4057 C_entries (C_STAR
, inf
);
4060 /* Always do Yacc. */
4065 C_entries (YACC
, inf
);
4069 /* Useful macros. */
4070 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
4071 for (; /* loop initialization */ \
4072 !feof (file_pointer) /* loop test */ \
4073 && /* instructions at start of loop */ \
4074 (readline (&line_buffer, file_pointer), \
4075 char_pointer = line_buffer.buffer, \
4078 #define LOOKING_AT(cp, keyword) /* keyword is a constant string */ \
4079 (strneq ((cp), keyword, sizeof(keyword)-1) /* cp points at keyword */ \
4080 && notinname ((cp)[sizeof(keyword)-1]) /* end of keyword */ \
4081 && ((cp) = skip_spaces((cp)+sizeof(keyword)-1))) /* skip spaces */
4084 * Read a file, but do no processing. This is used to do regexp
4085 * matching on files that have no language defined.
4088 just_read_file (inf
)
4091 register char *dummy
;
4093 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
4098 /* Fortran parsing */
4100 static void F_takeprec
__P((void));
4101 static void F_getit
__P((FILE *));
4106 dbp
= skip_spaces (dbp
);
4110 dbp
= skip_spaces (dbp
);
4111 if (strneq (dbp
, "(*)", 3))
4116 if (!ISDIGIT (*dbp
))
4118 --dbp
; /* force failure */
4123 while (ISDIGIT (*dbp
));
4132 dbp
= skip_spaces (dbp
);
4135 readline (&lb
, inf
);
4140 dbp
= skip_spaces (dbp
);
4142 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
4144 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
4146 make_tag (dbp
, cp
-dbp
, TRUE
,
4147 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4152 Fortran_functions (inf
)
4155 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4158 dbp
++; /* Ratfor escape to fortran */
4159 dbp
= skip_spaces (dbp
);
4162 switch (lowcase (*dbp
))
4165 if (nocase_tail ("integer"))
4169 if (nocase_tail ("real"))
4173 if (nocase_tail ("logical"))
4177 if (nocase_tail ("complex") || nocase_tail ("character"))
4181 if (nocase_tail ("double"))
4183 dbp
= skip_spaces (dbp
);
4186 if (nocase_tail ("precision"))
4192 dbp
= skip_spaces (dbp
);
4195 switch (lowcase (*dbp
))
4198 if (nocase_tail ("function"))
4202 if (nocase_tail ("subroutine"))
4206 if (nocase_tail ("entry"))
4210 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
4212 dbp
= skip_spaces (dbp
);
4213 if (*dbp
== '\0') /* assume un-named */
4214 make_tag ("blockdata", 9, TRUE
,
4215 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
4217 F_getit (inf
); /* look for name */
4228 * Philippe Waroquiers (1998)
4231 static void Ada_getit
__P((FILE *, char *));
4233 /* Once we are positioned after an "interesting" keyword, let's get
4234 the real tag value necessary. */
4236 Ada_getit (inf
, name_qualifier
)
4238 char *name_qualifier
;
4246 dbp
= skip_spaces (dbp
);
4248 || (dbp
[0] == '-' && dbp
[1] == '-'))
4250 readline (&lb
, inf
);
4253 switch (lowcase(*dbp
))
4256 if (nocase_tail ("body"))
4258 /* Skipping body of procedure body or package body or ....
4259 resetting qualifier to body instead of spec. */
4260 name_qualifier
= "/b";
4265 /* Skipping type of task type or protected type ... */
4266 if (nocase_tail ("type"))
4273 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
4278 dbp
= skip_spaces (dbp
);
4281 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
4289 name
= concat (dbp
, name_qualifier
, "");
4291 make_tag (name
, strlen (name
), TRUE
,
4292 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4304 bool inquote
= FALSE
;
4305 bool skip_till_semicolumn
= FALSE
;
4307 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4309 while (*dbp
!= '\0')
4311 /* Skip a string i.e. "abcd". */
4312 if (inquote
|| (*dbp
== '"'))
4314 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
4319 continue; /* advance char */
4324 break; /* advance line */
4328 /* Skip comments. */
4329 if (dbp
[0] == '-' && dbp
[1] == '-')
4330 break; /* advance line */
4332 /* Skip character enclosed in single quote i.e. 'a'
4333 and skip single quote starting an attribute i.e. 'Image. */
4342 if (skip_till_semicolumn
)
4345 skip_till_semicolumn
= FALSE
;
4347 continue; /* advance char */
4350 /* Search for beginning of a token. */
4351 if (!begtoken (*dbp
))
4354 continue; /* advance char */
4357 /* We are at the beginning of a token. */
4358 switch (lowcase(*dbp
))
4361 if (!packages_only
&& nocase_tail ("function"))
4362 Ada_getit (inf
, "/f");
4364 break; /* from switch */
4365 continue; /* advance char */
4367 if (!packages_only
&& nocase_tail ("procedure"))
4368 Ada_getit (inf
, "/p");
4369 else if (nocase_tail ("package"))
4370 Ada_getit (inf
, "/s");
4371 else if (nocase_tail ("protected")) /* protected type */
4372 Ada_getit (inf
, "/t");
4374 break; /* from switch */
4375 continue; /* advance char */
4378 if (typedefs
&& !packages_only
&& nocase_tail ("use"))
4380 /* when tagging types, avoid tagging use type Pack.Typename;
4381 for this, we will skip everything till a ; */
4382 skip_till_semicolumn
= TRUE
;
4383 continue; /* advance char */
4387 if (!packages_only
&& nocase_tail ("task"))
4388 Ada_getit (inf
, "/k");
4389 else if (typedefs
&& !packages_only
&& nocase_tail ("type"))
4391 Ada_getit (inf
, "/t");
4392 while (*dbp
!= '\0')
4396 break; /* from switch */
4397 continue; /* advance char */
4400 /* Look for the end of the token. */
4401 while (!endtoken (*dbp
))
4404 } /* advance char */
4405 } /* advance line */
4410 * Unix and microcontroller assembly tag handling
4411 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
4412 * Idea by Bob Weiner, Motorola Inc. (1994)
4420 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4422 /* If first char is alphabetic or one of [_.$], test for colon
4423 following identifier. */
4424 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4426 /* Read past label. */
4428 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
4430 if (*cp
== ':' || iswhite (*cp
))
4431 /* Found end of label, so copy it and add it to the table. */
4432 make_tag (lb
.buffer
, cp
- lb
.buffer
, TRUE
,
4433 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4441 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
4442 * Perl variable names: /^(my|local).../
4443 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
4444 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
4445 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
4448 Perl_functions (inf
)
4451 char *package
= savestr ("main"); /* current package name */
4454 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4458 if (LOOKING_AT (cp
, "package"))
4461 get_tag (cp
, &package
);
4463 else if (LOOKING_AT (cp
, "sub"))
4468 while (!notinname (*cp
))
4471 continue; /* nothing found */
4472 if ((pos
= etags_strchr (sp
, ':')) != NULL
4473 && pos
< cp
&& pos
[1] == ':')
4474 /* The name is already qualified. */
4475 make_tag (sp
, cp
- sp
, TRUE
,
4476 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4480 char savechar
, *name
;
4484 name
= concat (package
, "::", sp
);
4486 make_tag (name
, strlen(name
), TRUE
,
4487 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4491 else if (globals
) /* only if we are tagging global vars */
4493 /* Skip a qualifier, if any. */
4494 bool qual
= LOOKING_AT (cp
, "my") || LOOKING_AT (cp
, "local");
4495 /* After "my" or "local", but before any following paren or space. */
4496 char *varstart
= cp
;
4498 if (qual
/* should this be removed? If yes, how? */
4499 && (*cp
== '$' || *cp
== '@' || *cp
== '%'))
4504 while (ISALNUM (*cp
) || *cp
== '_');
4508 /* Should be examining a variable list at this point;
4509 could insist on seeing an open parenthesis. */
4510 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
4516 make_tag (varstart
, cp
- varstart
, FALSE
,
4517 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4525 * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4526 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4527 * More ideas by seb bacon <seb@jamkit.com> (2002)
4530 Python_functions (inf
)
4535 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4537 cp
= skip_spaces (cp
);
4538 if (LOOKING_AT (cp
, "def") || LOOKING_AT (cp
, "class"))
4541 while (!notinname (*cp
) && *cp
!= ':')
4543 make_tag (name
, cp
- name
, TRUE
,
4544 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4553 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4554 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4555 * - /^[ \t]*define\(\"[^\"]+/
4556 * Only with --members:
4557 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4558 * Idea by Diez B. Roggisch (2001)
4564 register char *cp
, *name
;
4565 bool search_identifier
= FALSE
;
4567 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4569 cp
= skip_spaces (cp
);
4571 if (search_identifier
4574 while (!notinname (*cp
))
4576 make_tag (name
, cp
- name
, TRUE
,
4577 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4578 search_identifier
= FALSE
;
4580 else if (LOOKING_AT (cp
, "function"))
4583 cp
= skip_spaces (cp
+1);
4587 while (!notinname (*cp
))
4589 make_tag (name
, cp
- name
, TRUE
,
4590 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4593 search_identifier
= TRUE
;
4595 else if (LOOKING_AT (cp
, "class"))
4600 while (*cp
!= '\0' && !iswhite (*cp
))
4602 make_tag (name
, cp
- name
, FALSE
,
4603 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4606 search_identifier
= TRUE
;
4608 else if (strneq (cp
, "define", 6)
4609 && (cp
= skip_spaces (cp
+6))
4611 && (*cp
== '"' || *cp
== '\''))
4615 while (*cp
!= quote
&& *cp
!= '\0')
4617 make_tag (name
, cp
- name
, FALSE
,
4618 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4621 && LOOKING_AT (cp
, "var")
4625 while (!notinname(*cp
))
4627 make_tag (name
, cp
- name
, FALSE
,
4628 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4635 * Cobol tag functions
4636 * We could look for anything that could be a paragraph name.
4637 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4638 * Idea by Corny de Souza (1993)
4641 Cobol_paragraphs (inf
)
4644 register char *bp
, *ep
;
4646 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4652 /* If eoln, compiler option or comment ignore whole line. */
4653 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
4656 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
4659 make_tag (bp
, ep
- bp
, TRUE
,
4660 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4667 * Ideas by Assar Westerlund <assar@sics.se> (2001)
4670 Makefile_targets (inf
)
4675 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4677 if (*bp
== '\t' || *bp
== '#')
4679 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
4681 if (*bp
== ':' || (globals
&& *bp
== '='))
4682 make_tag (lb
.buffer
, bp
- lb
.buffer
, TRUE
,
4683 lb
.buffer
, bp
- lb
.buffer
+ 1, lineno
, linecharno
);
4690 * Original code by Mosur K. Mohan (1989)
4692 * Locates tags for procedures & functions. Doesn't do any type- or
4693 * var-definitions. It does look for the keyword "extern" or
4694 * "forward" immediately following the procedure statement; if found,
4695 * the tag is skipped.
4698 Pascal_functions (inf
)
4701 linebuffer tline
; /* mostly copied from C_entries */
4703 int save_lineno
, namelen
, taglen
;
4706 bool /* each of these flags is TRUE iff: */
4707 incomment
, /* point is inside a comment */
4708 inquote
, /* point is inside '..' string */
4709 get_tagname
, /* point is after PROCEDURE/FUNCTION
4710 keyword, so next item = potential tag */
4711 found_tag
, /* point is after a potential tag */
4712 inparms
, /* point is within parameter-list */
4713 verify_tag
; /* point has passed the parm-list, so the
4714 next token will determine whether this
4715 is a FORWARD/EXTERN to be ignored, or
4716 whether it is a real tag */
4718 save_lcno
= save_lineno
= namelen
= taglen
= 0; /* keep compiler quiet */
4719 name
= NULL
; /* keep compiler quiet */
4722 linebuffer_init (&tline
);
4724 incomment
= inquote
= FALSE
;
4725 found_tag
= FALSE
; /* have a proc name; check if extern */
4726 get_tagname
= FALSE
; /* found "procedure" keyword */
4727 inparms
= FALSE
; /* found '(' after "proc" */
4728 verify_tag
= FALSE
; /* check if "extern" is ahead */
4731 while (!feof (inf
)) /* long main loop to get next char */
4734 if (c
== '\0') /* if end of line */
4736 readline (&lb
, inf
);
4740 if (!((found_tag
&& verify_tag
)
4742 c
= *dbp
++; /* only if don't need *dbp pointing
4743 to the beginning of the name of
4744 the procedure or function */
4748 if (c
== '}') /* within { } comments */
4750 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4767 inquote
= TRUE
; /* found first quote */
4769 case '{': /* found open { comment */
4773 if (*dbp
== '*') /* found open (* comment */
4778 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4781 case ')': /* end of parms list */
4786 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4793 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4795 /* Check if this is an "extern" declaration. */
4798 if (lowcase (*dbp
== 'e'))
4800 if (nocase_tail ("extern")) /* superfluous, really! */
4806 else if (lowcase (*dbp
) == 'f')
4808 if (nocase_tail ("forward")) /* check for forward reference */
4814 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4818 make_tag (name
, namelen
, TRUE
,
4819 tline
.buffer
, taglen
, save_lineno
, save_lcno
);
4823 if (get_tagname
) /* grab name of proc or fn */
4830 /* Find block name. */
4831 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4834 /* Save all values for later tagging. */
4835 linebuffer_setlen (&tline
, lb
.len
);
4836 strcpy (tline
.buffer
, lb
.buffer
);
4837 save_lineno
= lineno
;
4838 save_lcno
= linecharno
;
4839 name
= tline
.buffer
+ (dbp
- lb
.buffer
);
4841 taglen
= cp
- lb
.buffer
+ 1;
4843 dbp
= cp
; /* set dbp to e-o-token */
4844 get_tagname
= FALSE
;
4848 /* And proceed to check for "extern". */
4850 else if (!incomment
&& !inquote
&& !found_tag
)
4852 /* Check for proc/fn keywords. */
4853 switch (lowcase (c
))
4856 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4860 if (nocase_tail ("unction"))
4865 } /* while not eof */
4867 free (tline
.buffer
);
4872 * Lisp tag functions
4873 * look for (def or (DEF, quote or QUOTE
4876 static void L_getit
__P((void));
4881 if (*dbp
== '\'') /* Skip prefix quote */
4883 else if (*dbp
== '(')
4886 /* Try to skip "(quote " */
4887 if (!LOOKING_AT (dbp
, "quote") && !LOOKING_AT (dbp
, "QUOTE"))
4888 /* Ok, then skip "(" before name in (defstruct (foo)) */
4889 dbp
= skip_spaces (dbp
);
4891 get_tag (dbp
, NULL
);
4895 Lisp_functions (inf
)
4898 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4903 if (strneq (dbp
+1, "def", 3) || strneq (dbp
+1, "DEF", 3))
4905 dbp
= skip_non_spaces (dbp
);
4906 dbp
= skip_spaces (dbp
);
4911 /* Check for (foo::defmumble name-defined ... */
4914 while (!notinname (*dbp
) && *dbp
!= ':');
4919 while (*dbp
== ':');
4921 if (strneq (dbp
, "def", 3) || strneq (dbp
, "DEF", 3))
4923 dbp
= skip_non_spaces (dbp
);
4924 dbp
= skip_spaces (dbp
);
4934 * Lua script language parsing
4935 * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
4937 * "function" and "local function" are tags if they start at column 1.
4945 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4947 if (bp
[0] != 'f' && bp
[0] != 'l')
4950 LOOKING_AT (bp
, "local"); /* skip possible "local" */
4952 if (LOOKING_AT (bp
, "function"))
4959 * Postscript tag functions
4960 * Just look for lines where the first character is '/'
4961 * Also look at "defineps" for PSWrap
4963 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4964 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4970 register char *bp
, *ep
;
4972 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4977 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4980 make_tag (bp
, ep
- bp
, TRUE
,
4981 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4983 else if (LOOKING_AT (bp
, "defineps"))
4990 * Scheme tag functions
4991 * look for (def... xyzzy
4993 * (def ... ((...(xyzzy ....
4995 * Original code by Ken Haase (1985?)
4999 Scheme_functions (inf
)
5004 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
5006 if (strneq (bp
, "(def", 4) || strneq (bp
, "(DEF", 4))
5008 bp
= skip_non_spaces (bp
+4);
5009 /* Skip over open parens and white space */
5010 while (notinname (*bp
))
5014 if (LOOKING_AT (bp
, "(SET!") || LOOKING_AT (bp
, "(set!"))
5020 /* Find tags in TeX and LaTeX input files. */
5022 /* TEX_toktab is a table of TeX control sequences that define tags.
5023 * Each entry records one such control sequence.
5025 * Original code from who knows whom.
5027 * Stefan Monnier (2002)
5030 static linebuffer
*TEX_toktab
= NULL
; /* Table with tag tokens */
5032 /* Default set of control sequences to put into TEX_toktab.
5033 The value of environment var TEXTAGS is prepended to this. */
5034 static char *TEX_defenv
= "\
5035 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
5036 :part:appendix:entry:index:def\
5037 :newcommand:renewcommand:newenvironment:renewenvironment";
5039 static void TEX_mode
__P((FILE *));
5040 static void TEX_decode_env
__P((char *, char *));
5042 static char TEX_esc
= '\\';
5043 static char TEX_opgrp
= '{';
5044 static char TEX_clgrp
= '}';
5047 * TeX/LaTeX scanning loop.
5056 /* Select either \ or ! as escape character. */
5059 /* Initialize token table once from environment. */
5060 if (TEX_toktab
== NULL
)
5061 TEX_decode_env ("TEXTAGS", TEX_defenv
);
5063 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5065 /* Look at each TEX keyword in line. */
5068 /* Look for a TEX escape. */
5069 while (*cp
++ != TEX_esc
)
5070 if (cp
[-1] == '\0' || cp
[-1] == '%')
5073 for (key
= TEX_toktab
; key
->buffer
!= NULL
; key
++)
5074 if (strneq (cp
, key
->buffer
, key
->len
))
5077 int namelen
, linelen
;
5080 cp
= skip_spaces (cp
+ key
->len
);
5081 if (*cp
== TEX_opgrp
)
5087 (!iswhite (*p
) && *p
!= '#' &&
5088 *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
);
5093 if (!opgrp
|| *p
== TEX_clgrp
)
5095 while (*p
!= '\0' && *p
!= TEX_opgrp
&& *p
!= TEX_clgrp
)
5097 linelen
= p
- lb
.buffer
+ 1;
5099 make_tag (cp
, namelen
, TRUE
,
5100 lb
.buffer
, linelen
, lineno
, linecharno
);
5101 goto tex_next_line
; /* We only tag a line once */
5109 #define TEX_LESC '\\'
5110 #define TEX_SESC '!'
5112 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
5113 chars accordingly. */
5120 while ((c
= getc (inf
)) != EOF
)
5122 /* Skip to next line if we hit the TeX comment char. */
5126 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
5142 /* If the input file is compressed, inf is a pipe, and rewind may fail.
5143 No attempt is made to correct the situation. */
5147 /* Read environment and prepend it to the default string.
5148 Build token table. */
5150 TEX_decode_env (evarname
, defenv
)
5154 register char *env
, *p
;
5157 /* Append default string to environment. */
5158 env
= getenv (evarname
);
5164 env
= concat (oldenv
, defenv
, "");
5167 /* Allocate a token table */
5168 for (len
= 1, p
= env
; p
;)
5169 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
5171 TEX_toktab
= xnew (len
, linebuffer
);
5173 /* Unpack environment string into token table. Be careful about */
5174 /* zero-length strings (leading ':', "::" and trailing ':') */
5175 for (i
= 0; *env
!= '\0';)
5177 p
= etags_strchr (env
, ':');
5178 if (!p
) /* End of environment string. */
5179 p
= env
+ strlen (env
);
5181 { /* Only non-zero strings. */
5182 TEX_toktab
[i
].buffer
= savenstr (env
, p
- env
);
5183 TEX_toktab
[i
].len
= p
- env
;
5190 TEX_toktab
[i
].buffer
= NULL
; /* Mark end of table. */
5191 TEX_toktab
[i
].len
= 0;
5198 /* Texinfo support. Dave Love, Mar. 2000. */
5204 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5205 if (LOOKING_AT (cp
, "@node"))
5208 while (*cp
!= '\0' && *cp
!= ',')
5210 make_tag (start
, cp
- start
, TRUE
,
5211 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5216 /* Similar to LOOKING_AT but does not use notinname, does not skip */
5217 #define LOOKING_AT_NOCASE(cp, kw) /* kw is a constant string */ \
5218 (strncaseeq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
5219 && ((cp) += sizeof(kw)-1)) /* skip spaces */
5223 * Contents of <title>, <h1>, <h2>, <h3> are tags.
5224 * Contents of <a name=xxx> are tags with name xxx.
5226 * Francesco Potortì, 2002.
5232 bool getnext
= FALSE
; /* next text outside of HTML tags is a tag */
5233 bool skiptag
= FALSE
; /* skip to the end of the current HTML tag */
5234 bool intag
= FALSE
; /* inside an html tag, looking for ID= */
5235 bool inanchor
= FALSE
; /* when INTAG, is an anchor, look for NAME= */
5239 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5241 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
5242 for (;;) /* loop on the same line */
5244 if (skiptag
) /* skip HTML tag */
5246 while (*dbp
!= '\0' && *dbp
!= '>')
5252 continue; /* look on the same line */
5254 break; /* go to next line */
5257 else if (intag
) /* look for "name=" or "id=" */
5259 while (*dbp
!= '\0' && *dbp
!= '>'
5260 && lowcase (*dbp
) != 'n' && lowcase (*dbp
) != 'i')
5263 break; /* go to next line */
5268 continue; /* look on the same line */
5270 if ((inanchor
&& LOOKING_AT_NOCASE (dbp
, "name="))
5271 || LOOKING_AT_NOCASE (dbp
, "id="))
5273 bool quoted
= (dbp
[0] == '"');
5276 for (end
= ++dbp
; *end
!= '\0' && *end
!= '"'; end
++)
5279 for (end
= dbp
; *end
!= '\0' && intoken (*end
); end
++)
5281 linebuffer_setlen (&token_name
, end
- dbp
);
5282 strncpy (token_name
.buffer
, dbp
, end
- dbp
);
5283 token_name
.buffer
[end
- dbp
] = '\0';
5286 intag
= FALSE
; /* we found what we looked for */
5287 skiptag
= TRUE
; /* skip to the end of the tag */
5288 getnext
= TRUE
; /* then grab the text */
5289 continue; /* look on the same line */
5294 else if (getnext
) /* grab next tokens and tag them */
5296 dbp
= skip_spaces (dbp
);
5298 break; /* go to next line */
5302 inanchor
= (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]));
5303 continue; /* look on the same line */
5306 for (end
= dbp
+ 1; *end
!= '\0' && *end
!= '<'; end
++)
5308 make_tag (token_name
.buffer
, token_name
.len
, TRUE
,
5309 dbp
, end
- dbp
, lineno
, linecharno
);
5310 linebuffer_setlen (&token_name
, 0); /* no name in buffer */
5312 break; /* go to next line */
5315 else /* look for an interesting HTML tag */
5317 while (*dbp
!= '\0' && *dbp
!= '<')
5320 break; /* go to next line */
5322 if (lowcase (dbp
[1]) == 'a' && !intoken (dbp
[2]))
5325 continue; /* look on the same line */
5327 else if (LOOKING_AT_NOCASE (dbp
, "<title>")
5328 || LOOKING_AT_NOCASE (dbp
, "<h1>")
5329 || LOOKING_AT_NOCASE (dbp
, "<h2>")
5330 || LOOKING_AT_NOCASE (dbp
, "<h3>"))
5334 continue; /* look on the same line */
5345 * Assumes that the predicate or rule starts at column 0.
5346 * Only the first clause of a predicate or rule is added.
5347 * Original code by Sunichirou Sugou (1989)
5348 * Rewritten by Anders Lindgren (1996)
5350 static int prolog_pr
__P((char *, char *));
5351 static void prolog_skip_comment
__P((linebuffer
*, FILE *));
5352 static int prolog_atom
__P((char *, int));
5355 Prolog_functions (inf
)
5366 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5368 if (cp
[0] == '\0') /* Empty line */
5370 else if (iswhite (cp
[0])) /* Not a predicate */
5372 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
5373 prolog_skip_comment (&lb
, inf
);
5374 else if ((len
= prolog_pr (cp
, last
)) > 0)
5376 /* Predicate or rule. Store the function name so that we
5377 only generate a tag for the first clause. */
5379 last
= xnew(len
+ 1, char);
5380 else if (len
+ 1 > allocated
)
5381 xrnew (last
, len
+ 1, char);
5382 allocated
= len
+ 1;
5383 strncpy (last
, cp
, len
);
5391 prolog_skip_comment (plb
, inf
)
5399 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
5400 if (cp
[0] == '*' && cp
[1] == '/')
5402 readline (plb
, inf
);
5408 * A predicate or rule definition is added if it matches:
5409 * <beginning of line><Prolog Atom><whitespace>(
5410 * or <beginning of line><Prolog Atom><whitespace>:-
5412 * It is added to the tags database if it doesn't match the
5413 * name of the previous clause header.
5415 * Return the size of the name of the predicate or rule, or 0 if no
5421 char *last
; /* Name of last clause. */
5426 pos
= prolog_atom (s
, 0);
5431 pos
= skip_spaces (s
+ pos
) - s
;
5434 || (s
[pos
] == '(' && (pos
+= 1))
5435 || (s
[pos
] == ':' && s
[pos
+ 1] == '-' && (pos
+= 2)))
5436 && (last
== NULL
/* save only the first clause */
5437 || len
!= strlen (last
)
5438 || !strneq (s
, last
, len
)))
5440 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5448 * Consume a Prolog atom.
5449 * Return the number of bytes consumed, or -1 if there was an error.
5451 * A prolog atom, in this context, could be one of:
5452 * - An alphanumeric sequence, starting with a lower case letter.
5453 * - A quoted arbitrary string. Single quotes can escape themselves.
5454 * Backslash quotes everything.
5457 prolog_atom (s
, pos
)
5465 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
5467 /* The atom is unquoted. */
5469 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
5473 return pos
- origpos
;
5475 else if (s
[pos
] == '\'')
5486 pos
++; /* A double quote */
5488 else if (s
[pos
] == '\0')
5489 /* Multiline quoted atoms are ignored. */
5491 else if (s
[pos
] == '\\')
5493 if (s
[pos
+1] == '\0')
5500 return pos
- origpos
;
5508 * Support for Erlang
5510 * Generates tags for functions, defines, and records.
5511 * Assumes that Erlang functions start at column 0.
5512 * Original code by Anders Lindgren (1996)
5514 static int erlang_func
__P((char *, char *));
5515 static void erlang_attribute
__P((char *));
5516 static int erlang_atom
__P((char *));
5519 Erlang_functions (inf
)
5530 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
5532 if (cp
[0] == '\0') /* Empty line */
5534 else if (iswhite (cp
[0])) /* Not function nor attribute */
5536 else if (cp
[0] == '%') /* comment */
5538 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
5540 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
5542 erlang_attribute (cp
);
5545 else if ((len
= erlang_func (cp
, last
)) > 0)
5548 * Function. Store the function name so that we only
5549 * generates a tag for the first clause.
5552 last
= xnew (len
+ 1, char);
5553 else if (len
+ 1 > allocated
)
5554 xrnew (last
, len
+ 1, char);
5555 allocated
= len
+ 1;
5556 strncpy (last
, cp
, len
);
5564 * A function definition is added if it matches:
5565 * <beginning of line><Erlang Atom><whitespace>(
5567 * It is added to the tags database if it doesn't match the
5568 * name of the previous clause header.
5570 * Return the size of the name of the function, or 0 if no function
5574 erlang_func (s
, last
)
5576 char *last
; /* Name of last clause. */
5581 pos
= erlang_atom (s
);
5586 pos
= skip_spaces (s
+ pos
) - s
;
5588 /* Save only the first clause. */
5591 || len
!= (int)strlen (last
)
5592 || !strneq (s
, last
, len
)))
5594 make_tag (s
, len
, TRUE
, s
, pos
, lineno
, linecharno
);
5603 * Handle attributes. Currently, tags are generated for defines
5606 * They are on the form:
5607 * -define(foo, bar).
5608 * -define(Foo(M, N), M+N).
5609 * -record(graph, {vtab = notable, cyclic = true}).
5612 erlang_attribute (s
)
5617 if ((LOOKING_AT (cp
, "-define") || LOOKING_AT (cp
, "-record"))
5620 int len
= erlang_atom (skip_spaces (cp
));
5622 make_tag (cp
, len
, TRUE
, s
, cp
+ len
- s
, lineno
, linecharno
);
5629 * Consume an Erlang atom (or variable).
5630 * Return the number of bytes consumed, or -1 if there was an error.
5638 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
5640 /* The atom is unquoted. */
5643 while (ISALNUM (s
[pos
]) || s
[pos
] == '_');
5645 else if (s
[pos
] == '\'')
5647 for (pos
++; s
[pos
] != '\''; pos
++)
5648 if (s
[pos
] == '\0' /* multiline quoted atoms are ignored */
5649 || (s
[pos
] == '\\' && s
[++pos
] == '\0'))
5658 #ifdef ETAGS_REGEXPS
5660 static char *scan_separators
__P((char *));
5661 static void add_regex
__P((char *, language
*));
5662 static char *substitute
__P((char *, char *, struct re_registers
*));
5665 * Take a string like "/blah/" and turn it into "blah", verifying
5666 * that the first and last characters are the same, and handling
5667 * quoted separator characters. Actually, stops on the occurrence of
5668 * an unquoted separator. Also process \t, \n, etc. and turn into
5669 * appropriate characters. Works in place. Null terminates name string.
5670 * Returns pointer to terminating separator, or NULL for
5671 * unterminated regexps.
5674 scan_separators (name
)
5678 char *copyto
= name
;
5679 bool quoted
= FALSE
;
5681 for (++name
; *name
!= '\0'; ++name
)
5687 case 'a': *copyto
++ = '\007'; break; /* BEL (bell) */
5688 case 'b': *copyto
++ = '\b'; break; /* BS (back space) */
5689 case 'd': *copyto
++ = 0177; break; /* DEL (delete) */
5690 case 'e': *copyto
++ = 033; break; /* ESC (delete) */
5691 case 'f': *copyto
++ = '\f'; break; /* FF (form feed) */
5692 case 'n': *copyto
++ = '\n'; break; /* NL (new line) */
5693 case 'r': *copyto
++ = '\r'; break; /* CR (carriage return) */
5694 case 't': *copyto
++ = '\t'; break; /* TAB (horizontal tab) */
5695 case 'v': *copyto
++ = '\v'; break; /* VT (vertical tab) */
5701 /* Something else is quoted, so preserve the quote. */
5709 else if (*name
== '\\')
5711 else if (*name
== sep
)
5717 name
= NULL
; /* signal unterminated regexp */
5719 /* Terminate copied string. */
5724 /* Look at the argument of --regex or --no-regex and do the right
5725 thing. Same for each line of a regexp file. */
5727 analyse_regex (regex_arg
)
5730 if (regex_arg
== NULL
)
5732 free_regexps (); /* --no-regex: remove existing regexps */
5736 /* A real --regexp option or a line in a regexp file. */
5737 switch (regex_arg
[0])
5739 /* Comments in regexp file or null arg to --regex. */
5745 /* Read a regex file. This is recursive and may result in a
5746 loop, which will stop when the file descriptors are exhausted. */
5750 linebuffer regexbuf
;
5751 char *regexfile
= regex_arg
+ 1;
5753 /* regexfile is a file containing regexps, one per line. */
5754 regexfp
= fopen (regexfile
, "r");
5755 if (regexfp
== NULL
)
5760 linebuffer_init (®exbuf
);
5761 while (readline_internal (®exbuf
, regexfp
) > 0)
5762 analyse_regex (regexbuf
.buffer
);
5763 free (regexbuf
.buffer
);
5768 /* Regexp to be used for a specific language only. */
5772 char *lang_name
= regex_arg
+ 1;
5775 for (cp
= lang_name
; *cp
!= '}'; cp
++)
5778 error ("unterminated language name in regex: %s", regex_arg
);
5782 lang
= get_language_from_langname (lang_name
);
5785 add_regex (cp
, lang
);
5789 /* Regexp to be used for any language. */
5791 add_regex (regex_arg
, NULL
);
5796 /* Separate the regexp pattern, compile it,
5797 and care for optional name and modifiers. */
5799 add_regex (regexp_pattern
, lang
)
5800 char *regexp_pattern
;
5803 static struct re_pattern_buffer zeropattern
;
5804 char sep
, *pat
, *name
, *modifiers
;
5806 struct re_pattern_buffer
*patbuf
;
5809 force_explicit_name
= TRUE
, /* do not use implicit tag names */
5810 ignore_case
= FALSE
, /* case is significant */
5811 multi_line
= FALSE
, /* matches are done one line at a time */
5812 single_line
= FALSE
; /* dot does not match newline */
5815 if (strlen(regexp_pattern
) < 3)
5817 error ("null regexp", (char *)NULL
);
5820 sep
= regexp_pattern
[0];
5821 name
= scan_separators (regexp_pattern
);
5824 error ("%s: unterminated regexp", regexp_pattern
);
5829 error ("null name for regexp \"%s\"", regexp_pattern
);
5832 modifiers
= scan_separators (name
);
5833 if (modifiers
== NULL
) /* no terminating separator --> no name */
5839 modifiers
+= 1; /* skip separator */
5841 /* Parse regex modifiers. */
5842 for (; modifiers
[0] != '\0'; modifiers
++)
5843 switch (modifiers
[0])
5846 if (modifiers
== name
)
5847 error ("forcing explicit tag name but no name, ignoring", NULL
);
5848 force_explicit_name
= TRUE
;
5858 need_filebuf
= TRUE
;
5863 wrongmod
[0] = modifiers
[0];
5865 error ("invalid regexp modifier `%s', ignoring", wrongmod
);
5870 patbuf
= xnew (1, struct re_pattern_buffer
);
5871 *patbuf
= zeropattern
;
5874 static char lc_trans
[CHARS
];
5876 for (i
= 0; i
< CHARS
; i
++)
5877 lc_trans
[i
] = lowcase (i
);
5878 patbuf
->translate
= lc_trans
; /* translation table to fold case */
5882 pat
= concat ("^", regexp_pattern
, ""); /* anchor to beginning of line */
5884 pat
= regexp_pattern
;
5887 re_set_syntax (RE_SYNTAX_EMACS
| RE_DOT_NEWLINE
);
5889 re_set_syntax (RE_SYNTAX_EMACS
);
5891 err
= re_compile_pattern (pat
, strlen (regexp_pattern
), patbuf
);
5896 error ("%s while compiling pattern", err
);
5901 p_head
= xnew (1, regexp
);
5902 p_head
->pattern
= savestr (regexp_pattern
);
5903 p_head
->p_next
= rp
;
5904 p_head
->lang
= lang
;
5905 p_head
->pat
= patbuf
;
5906 p_head
->name
= savestr (name
);
5907 p_head
->error_signaled
= FALSE
;
5908 p_head
->force_explicit_name
= force_explicit_name
;
5909 p_head
->ignore_case
= ignore_case
;
5910 p_head
->multi_line
= multi_line
;
5914 * Do the substitutions indicated by the regular expression and
5918 substitute (in
, out
, regs
)
5920 struct re_registers
*regs
;
5923 int size
, dig
, diglen
;
5926 size
= strlen (out
);
5928 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5929 if (out
[size
- 1] == '\\')
5930 fatal ("pattern error in \"%s\"", out
);
5931 for (t
= etags_strchr (out
, '\\');
5933 t
= etags_strchr (t
+ 2, '\\'))
5937 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5943 /* Allocate space and do the substitutions. */
5945 result
= xnew (size
+ 1, char);
5947 for (t
= result
; *out
!= '\0'; out
++)
5948 if (*out
== '\\' && ISDIGIT (*++out
))
5951 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5952 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
5959 assert (t
<= result
+ size
);
5960 assert (t
- result
== (int)strlen (result
));
5965 /* Deallocate all regexps. */
5970 while (p_head
!= NULL
)
5972 rp
= p_head
->p_next
;
5973 free (p_head
->pattern
);
5974 free (p_head
->name
);
5982 * Reads the whole file as a single string from `filebuf' and looks for
5983 * multi-line regular expressions, creating tags on matches.
5984 * readline already dealt with normal regexps.
5986 * Idea by Ben Wing <ben@666.com> (2002).
5989 regex_tag_multiline ()
5991 char *buffer
= filebuf
.buffer
;
5995 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
5999 if (!rp
->multi_line
)
6000 continue; /* skip normal regexps */
6002 /* Generic initialisations before parsing file from memory. */
6003 lineno
= 1; /* reset global line number */
6004 charno
= 0; /* reset global char number */
6005 linecharno
= 0; /* reset global char number of line start */
6007 /* Only use generic regexps or those for the current language. */
6008 if (rp
->lang
!= NULL
&& rp
->lang
!= curfdp
->lang
)
6011 while (match
>= 0 && match
< filebuf
.len
)
6013 match
= re_search (rp
->pat
, buffer
, filebuf
.len
, charno
,
6014 filebuf
.len
- match
, &rp
->regs
);
6019 if (!rp
->error_signaled
)
6021 error ("regexp stack overflow while matching \"%s\"",
6023 rp
->error_signaled
= TRUE
;
6030 if (match
== rp
->regs
.end
[0])
6032 if (!rp
->error_signaled
)
6034 error ("regexp matches the empty string: \"%s\"",
6036 rp
->error_signaled
= TRUE
;
6038 match
= -3; /* exit from while loop */
6042 /* Match occurred. Construct a tag. */
6043 while (charno
< rp
->regs
.end
[0])
6044 if (buffer
[charno
++] == '\n')
6045 lineno
++, linecharno
= charno
;
6047 if (name
[0] == '\0')
6049 else /* make a named tag */
6050 name
= substitute (buffer
, rp
->name
, &rp
->regs
);
6051 if (rp
->force_explicit_name
)
6052 /* Force explicit tag name, if a name is there. */
6053 pfnote (name
, TRUE
, buffer
+ linecharno
,
6054 charno
- linecharno
+ 1, lineno
, linecharno
);
6056 make_tag (name
, strlen (name
), TRUE
, buffer
+ linecharno
,
6057 charno
- linecharno
+ 1, lineno
, linecharno
);
6064 #endif /* ETAGS_REGEXPS */
6071 register int len
= 0;
6073 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
6075 if (*cp
== '\0' && !intoken (dbp
[len
]))
6084 get_tag (bp
, namepp
)
6088 register char *cp
= bp
;
6092 /* Go till you get to white space or a syntactic break */
6093 for (cp
= bp
+ 1; !notinname (*cp
); cp
++)
6095 make_tag (bp
, cp
- bp
, TRUE
,
6096 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
6100 *namepp
= savenstr (bp
, cp
- bp
);
6104 * Read a line of text from `stream' into `lbp', excluding the
6105 * newline or CR-NL, if any. Return the number of characters read from
6106 * `stream', which is the length of the line including the newline.
6108 * On DOS or Windows we do not count the CR character, if any before the
6109 * NL, in the returned length; this mirrors the behavior of Emacs on those
6110 * platforms (for text files, it translates CR-NL to NL as it reads in the
6113 * If multi-line regular expressions are requested, each line read is
6114 * appended to `filebuf'.
6117 readline_internal (lbp
, stream
)
6119 register FILE *stream
;
6121 char *buffer
= lbp
->buffer
;
6122 register char *p
= lbp
->buffer
;
6123 register char *pend
;
6126 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
6130 register int c
= getc (stream
);
6133 /* We're at the end of linebuffer: expand it. */
6135 xrnew (buffer
, lbp
->size
, char);
6136 p
+= buffer
- lbp
->buffer
;
6137 pend
= buffer
+ lbp
->size
;
6138 lbp
->buffer
= buffer
;
6148 if (p
> buffer
&& p
[-1] == '\r')
6152 /* Assume CRLF->LF translation will be performed by Emacs
6153 when loading this file, so CRs won't appear in the buffer.
6154 It would be cleaner to compensate within Emacs;
6155 however, Emacs does not know how many CRs were deleted
6156 before any given point in the file. */
6171 lbp
->len
= p
- buffer
;
6173 if (need_filebuf
/* we need filebuf for multi-line regexps */
6174 && chars_deleted
> 0) /* not at EOF */
6176 while (filebuf
.size
<= filebuf
.len
+ lbp
->len
+ 1) /* +1 for \n */
6178 /* Expand filebuf. */
6180 xrnew (filebuf
.buffer
, filebuf
.size
, char);
6182 strncpy (filebuf
.buffer
+ filebuf
.len
, lbp
->buffer
, lbp
->len
);
6183 filebuf
.len
+= lbp
->len
;
6184 filebuf
.buffer
[filebuf
.len
++] = '\n';
6185 filebuf
.buffer
[filebuf
.len
] = '\0';
6188 return lbp
->len
+ chars_deleted
;
6192 * Like readline_internal, above, but in addition try to match the
6193 * input line against relevant regular expressions and manage #line
6197 readline (lbp
, stream
)
6203 linecharno
= charno
; /* update global char number of line start */
6204 result
= readline_internal (lbp
, stream
); /* read line */
6205 lineno
+= 1; /* increment global line number */
6206 charno
+= result
; /* increment global char number */
6208 /* Honour #line directives. */
6209 if (!no_line_directive
)
6211 static bool discard_until_line_directive
;
6213 /* Check whether this is a #line directive. */
6214 if (result
> 12 && strneq (lbp
->buffer
, "#line ", 6))
6218 if (DEBUG
) start
= 0; /* shut up the compiler */
6219 if (sscanf (lbp
->buffer
, "#line %d \"%n", &lno
, &start
) == 1)
6221 char *endp
= lbp
->buffer
+ start
;
6224 while ((endp
= etags_strchr (endp
, '"')) != NULL
6225 && endp
[-1] == '\\')
6228 /* Ok, this is a real #line directive. Let's deal with it. */
6230 char *taggedabsname
; /* absolute name of original file */
6231 char *taggedfname
; /* name of original file as given */
6232 char *name
; /* temp var */
6234 discard_until_line_directive
= FALSE
; /* found it */
6235 name
= lbp
->buffer
+ start
;
6237 canonicalize_filename (name
); /* for DOS */
6238 taggedabsname
= absolute_filename (name
, curfdp
->infabsdir
);
6239 if (filename_is_absolute (name
)
6240 || filename_is_absolute (curfdp
->infname
))
6241 taggedfname
= savestr (taggedabsname
);
6243 taggedfname
= relative_filename (taggedabsname
,tagfiledir
);
6245 if (streq (curfdp
->taggedfname
, taggedfname
))
6246 /* The #line directive is only a line number change. We
6247 deal with this afterwards. */
6250 /* The tags following this #line directive should be
6251 attributed to taggedfname. In order to do this, set
6252 curfdp accordingly. */
6254 fdesc
*fdp
; /* file description pointer */
6256 /* Go look for a file description already set up for the
6257 file indicated in the #line directive. If there is
6258 one, use it from now until the next #line
6260 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6261 if (streq (fdp
->infname
, curfdp
->infname
)
6262 && streq (fdp
->taggedfname
, taggedfname
))
6263 /* If we remove the second test above (after the &&)
6264 then all entries pertaining to the same file are
6265 coalesced in the tags file. If we use it, then
6266 entries pertaining to the same file but generated
6267 from different files (via #line directives) will
6268 go into separate sections in the tags file. These
6269 alternatives look equivalent. The first one
6270 destroys some apparently useless information. */
6276 /* Else, if we already tagged the real file, skip all
6277 input lines until the next #line directive. */
6278 if (fdp
== NULL
) /* not found */
6279 for (fdp
= fdhead
; fdp
!= NULL
; fdp
= fdp
->next
)
6280 if (streq (fdp
->infabsname
, taggedabsname
))
6282 discard_until_line_directive
= TRUE
;
6286 /* Else create a new file description and use that from
6287 now on, until the next #line directive. */
6288 if (fdp
== NULL
) /* not found */
6291 fdhead
= xnew (1, fdesc
);
6292 *fdhead
= *curfdp
; /* copy curr. file description */
6294 fdhead
->infname
= savestr (curfdp
->infname
);
6295 fdhead
->infabsname
= savestr (curfdp
->infabsname
);
6296 fdhead
->infabsdir
= savestr (curfdp
->infabsdir
);
6297 fdhead
->taggedfname
= taggedfname
;
6298 fdhead
->usecharno
= FALSE
;
6299 fdhead
->prop
= NULL
;
6300 fdhead
->written
= FALSE
;
6304 free (taggedabsname
);
6306 readline (lbp
, stream
);
6308 } /* if a real #line directive */
6309 } /* if #line is followed by a a number */
6310 } /* if line begins with "#line " */
6312 /* If we are here, no #line directive was found. */
6313 if (discard_until_line_directive
)
6317 /* Do a tail recursion on ourselves, thus discarding the contents
6318 of the line buffer. */
6319 readline (lbp
, stream
);
6323 discard_until_line_directive
= FALSE
;
6326 } /* if #line directives should be considered */
6328 #ifdef ETAGS_REGEXPS
6334 /* Match against relevant regexps. */
6336 for (rp
= p_head
; rp
!= NULL
; rp
= rp
->p_next
)
6338 /* Only use generic regexps or those for the current language.
6339 Also do not use multiline regexps, which is the job of
6340 regex_tag_multiline. */
6341 if ((rp
->lang
!= NULL
&& rp
->lang
!= fdhead
->lang
)
6345 match
= re_match (rp
->pat
, lbp
->buffer
, lbp
->len
, 0, &rp
->regs
);
6350 if (!rp
->error_signaled
)
6352 error ("regexp stack overflow while matching \"%s\"",
6354 rp
->error_signaled
= TRUE
;
6361 /* Empty string matched. */
6362 if (!rp
->error_signaled
)
6364 error ("regexp matches the empty string: \"%s\"", rp
->pattern
);
6365 rp
->error_signaled
= TRUE
;
6369 /* Match occurred. Construct a tag. */
6371 if (name
[0] == '\0')
6373 else /* make a named tag */
6374 name
= substitute (lbp
->buffer
, rp
->name
, &rp
->regs
);
6375 if (rp
->force_explicit_name
)
6376 /* Force explicit tag name, if a name is there. */
6377 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
6379 make_tag (name
, strlen (name
), TRUE
,
6380 lbp
->buffer
, match
, lineno
, linecharno
);
6385 #endif /* ETAGS_REGEXPS */
6390 * Return a pointer to a space of size strlen(cp)+1 allocated
6391 * with xnew where the string CP has been copied.
6397 return savenstr (cp
, strlen (cp
));
6401 * Return a pointer to a space of size LEN+1 allocated with xnew where
6402 * the string CP has been copied for at most the first LEN characters.
6411 dp
= xnew (len
+ 1, char);
6412 strncpy (dp
, cp
, len
);
6418 * Return the ptr in sp at which the character c last
6419 * appears; NULL if not found
6421 * Identical to POSIX strrchr, included for portability.
6424 etags_strrchr (sp
, c
)
6425 register const char *sp
;
6428 register const char *r
;
6440 * Return the ptr in sp at which the character c first
6441 * appears; NULL if not found
6443 * Identical to POSIX strchr, included for portability.
6446 etags_strchr (sp
, c
)
6447 register const char *sp
;
6459 * Compare two strings, ignoring case for alphabetic characters.
6461 * Same as BSD's strcasecmp, included for portability.
6464 etags_strcasecmp (s1
, s2
)
6465 register const char *s1
;
6466 register const char *s2
;
6469 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6470 ? lowcase (*s1
) == lowcase (*s2
)
6474 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6475 ? lowcase (*s1
) - lowcase (*s2
)
6480 * Compare two strings, ignoring case for alphabetic characters.
6481 * Stop after a given number of characters
6483 * Same as BSD's strncasecmp, included for portability.
6486 etags_strncasecmp (s1
, s2
, n
)
6487 register const char *s1
;
6488 register const char *s2
;
6491 while (*s1
!= '\0' && n
-- > 0
6492 && (ISALPHA (*s1
) && ISALPHA (*s2
)
6493 ? lowcase (*s1
) == lowcase (*s2
)
6500 return (ISALPHA (*s1
) && ISALPHA (*s2
)
6501 ? lowcase (*s1
) - lowcase (*s2
)
6505 /* Skip spaces, return new pointer. */
6510 while (iswhite (*cp
))
6515 /* Skip non spaces, return new pointer. */
6517 skip_non_spaces (cp
)
6520 while (*cp
!= '\0' && !iswhite (*cp
))
6525 /* Print error message and exit. */
6531 exit (EXIT_FAILURE
);
6539 exit (EXIT_FAILURE
);
6543 suggest_asking_for_help ()
6545 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
6546 progname
, LONG_OPTIONS
? "--help" : "-h");
6547 exit (EXIT_FAILURE
);
6550 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
6553 const char *s1
, *s2
;
6555 fprintf (stderr
, "%s: ", progname
);
6556 fprintf (stderr
, s1
, s2
);
6557 fprintf (stderr
, "\n");
6560 /* Return a newly-allocated string whose contents
6561 concatenate those of s1, s2, s3. */
6566 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
6567 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
6569 strcpy (result
, s1
);
6570 strcpy (result
+ len1
, s2
);
6571 strcpy (result
+ len1
+ len2
, s3
);
6572 result
[len1
+ len2
+ len3
] = '\0';
6578 /* Does the same work as the system V getcwd, but does not need to
6579 guess the buffer size in advance. */
6585 char *path
= xnew (bufsize
, char);
6587 while (getcwd (path
, bufsize
) == NULL
)
6589 if (errno
!= ERANGE
)
6593 path
= xnew (bufsize
, char);
6596 canonicalize_filename (path
);
6599 #else /* not HAVE_GETCWD */
6602 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
6606 for (p
= path
; *p
!= '\0'; p
++)
6612 return strdup (path
);
6613 #else /* not MSDOS */
6617 linebuffer_init (&path
);
6618 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
6619 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
6624 #endif /* not MSDOS */
6625 #endif /* not HAVE_GETCWD */
6628 /* Return a newly allocated string containing the file name of FILE
6629 relative to the absolute directory DIR (which should end with a slash). */
6631 relative_filename (file
, dir
)
6634 char *fp
, *dp
, *afn
, *res
;
6637 /* Find the common root of file and dir (with a trailing slash). */
6638 afn
= absolute_filename (file
, cwd
);
6641 while (*fp
++ == *dp
++)
6643 fp
--, dp
--; /* back to the first differing char */
6645 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
6648 do /* look at the equal chars until '/' */
6652 /* Build a sequence of "../" strings for the resulting relative file name. */
6654 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
6656 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
6659 strcat (res
, "../");
6661 /* Add the file name relative to the common root of file and dir. */
6662 strcat (res
, fp
+ 1);
6668 /* Return a newly allocated string containing the absolute file name
6669 of FILE given DIR (which should end with a slash). */
6671 absolute_filename (file
, dir
)
6674 char *slashp
, *cp
, *res
;
6676 if (filename_is_absolute (file
))
6677 res
= savestr (file
);
6679 /* We don't support non-absolute file names with a drive
6680 letter, like `d:NAME' (it's too much hassle). */
6681 else if (file
[1] == ':')
6682 fatal ("%s: relative file names with drive letters not supported", file
);
6685 res
= concat (dir
, file
, "");
6687 /* Delete the "/dirname/.." and "/." substrings. */
6688 slashp
= etags_strchr (res
, '/');
6689 while (slashp
!= NULL
&& slashp
[0] != '\0')
6691 if (slashp
[1] == '.')
6693 if (slashp
[2] == '.'
6694 && (slashp
[3] == '/' || slashp
[3] == '\0'))
6699 while (cp
>= res
&& !filename_is_absolute (cp
));
6701 cp
= slashp
; /* the absolute name begins with "/.." */
6703 /* Under MSDOS and NT we get `d:/NAME' as absolute
6704 file name, so the luser could say `d:/../NAME'.
6705 We silently treat this as `d:/NAME'. */
6706 else if (cp
[0] != '/')
6709 strcpy (cp
, slashp
+ 3);
6713 else if (slashp
[2] == '/' || slashp
[2] == '\0')
6715 strcpy (slashp
, slashp
+ 2);
6720 slashp
= etags_strchr (slashp
+ 1, '/');
6724 return savestr ("/");
6729 /* Return a newly allocated string containing the absolute
6730 file name of dir where FILE resides given DIR (which should
6731 end with a slash). */
6733 absolute_dirname (file
, dir
)
6739 canonicalize_filename (file
);
6740 slashp
= etags_strrchr (file
, '/');
6742 return savestr (dir
);
6745 res
= absolute_filename (file
, dir
);
6751 /* Whether the argument string is an absolute file name. The argument
6752 string must have been canonicalized with canonicalize_filename. */
6754 filename_is_absolute (fn
)
6757 return (fn
[0] == '/'
6759 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
6764 /* Translate backslashes into slashes. Works in place. */
6766 canonicalize_filename (fn
)
6770 /* Canonicalize drive letter case. */
6771 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
6772 fn
[0] = upcase (fn
[0]);
6773 /* Convert backslashes to slashes. */
6774 for (; *fn
!= '\0'; fn
++)
6779 fn
= NULL
; /* shut up the compiler */
6784 /* Initialize a linebuffer for use */
6786 linebuffer_init (lbp
)
6789 lbp
->size
= (DEBUG
) ? 3 : 200;
6790 lbp
->buffer
= xnew (lbp
->size
, char);
6791 lbp
->buffer
[0] = '\0';
6795 /* Set the minimum size of a string contained in a linebuffer. */
6797 linebuffer_setlen (lbp
, toksize
)
6801 while (lbp
->size
<= toksize
)
6804 xrnew (lbp
->buffer
, lbp
->size
, char);
6809 /* Like malloc but get fatal error if memory is exhausted. */
6814 PTR result
= (PTR
) malloc (size
);
6816 fatal ("virtual memory exhausted", (char *)NULL
);
6821 xrealloc (ptr
, size
)
6825 PTR result
= (PTR
) realloc (ptr
, size
);
6827 fatal ("virtual memory exhausted", (char *)NULL
);
6833 * c-indentation-style: gnu
6834 * indent-tabs-mode: t
6837 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
6841 /* arch-tag: 8a9b748d-390c-4922-99db-2eeefa921051
6842 (do not change this comment) */
6844 /* etags.c ends here */