Mercurial > hg > xemacs-beta
annotate lib-src/etags.c @ 5713:8b5bdc8aebfd
Added tag r21-5-33 for changeset bee2e2568828
| author | Stephen J. Turnbull <stephen@xemacs.org> |
|---|---|
| date | Sat, 05 Jan 2013 02:11:23 +0900 |
| parents | 061f4f90f874 |
| children |
| rev | line source |
|---|---|
| 458 | 1 /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*- |
| 3972 | 2 |
| 3 Copyright (C) 1984 The Regents of the University of California | |
| 4 | |
| 5 Redistribution and use in source and binary forms, with or without | |
| 6 modification, are permitted provided that the following conditions are | |
| 7 met: | |
| 8 1. Redistributions of source code must retain the above copyright | |
| 9 notice, this list of conditions and the following disclaimer. | |
| 10 2. Redistributions in binary form must reproduce the above copyright | |
| 11 notice, this list of conditions and the following disclaimer in the | |
| 12 documentation and/or other materials provided with the | |
| 13 distribution. | |
| 14 3. Neither the name of the University nor the names of its | |
| 15 contributors may be used to endorse or promote products derived | |
| 16 from this software without specific prior written permission. | |
| 17 | |
| 18 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' | |
| 19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 20 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 21 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS | |
| 22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
| 25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 26 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | |
| 27 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN | |
| 28 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | |
| 30 | |
| 31 Copyright (C) 1984, 1987, 1988, 1989, 1993, 1994, 1995, 1998, 1999, | |
| 32 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 | |
| 33 Free Software Foundation, Inc. | |
| 34 | |
| 35 This file is not considered part of GNU Emacs. | |
| 36 | |
|
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
37 This program is free software: you can redistribute it and/or modify it |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
38 under the terms of the GNU General Public License as published by the |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
39 Free Software Foundation, either version 3 of the License, or (at your |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
40 option) any later version. |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
41 |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
42 This program is distributed in the hope that it will be useful, but WITHOUT |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
43 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
44 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
45 for more details. |
| 3972 | 46 |
| 47 You should have received a copy of the GNU General Public License | |
|
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
4768
diff
changeset
|
48 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 3972 | 49 |
| 50 | |
| 51 /* NB To comply with the above BSD license, copyright information is | |
| 52 reproduced in etc/ETAGS.README. That file should be updated when the | |
| 53 above notices are. | |
| 54 | |
| 55 To the best of our knowledge, this code was originally based on the | |
| 56 ctags.c distributed with BSD4.2, which was copyrighted by the | |
| 57 University of California, as described above. */ | |
| 58 | |
| 428 | 59 |
| 60 /* | |
| 61 * Authors: | |
| 3972 | 62 * 1983 Ctags originally by Ken Arnold. |
| 63 * 1984 Fortran added by Jim Kleckner. | |
| 64 * 1984 Ed Pelegri-Llopart added C typedefs. | |
| 65 * 1985 Emacs TAGS format by Richard Stallman. | |
| 458 | 66 * 1989 Sam Kendall added C++. |
| 2225 | 67 * 1992 Joseph B. Wells improved C and C++ parsing. |
| 68 * 1993 Francesco Potort́ reorganised C and C++. | |
| 69 * 1994 Line-by-line regexp tags by Tom Tromey. | |
| 70 * 2001 Nested classes by Francesco Potort́ (concept by Mykola Dzyuba). | |
| 71 * 2002 #line directives by Francesco Potort́. | |
| 428 | 72 * |
| 2225 | 73 * Francesco Potort́ <pot@gnu.org> has maintained and improved it since 1993. |
| 428 | 74 */ |
| 75 | |
| 2325 | 76 /* |
| 77 * If you want to add support for a new language, start by looking at the LUA | |
| 78 * language, which is the simplest. Alternatively, consider shipping a | |
| 79 * configuration file containing regexp definitions for etags. | |
| 80 */ | |
| 81 | |
| 3993 | 82 char pot_etags_version[] = "@(#) pot revision number is 17.33"; |
| 428 | 83 |
| 84 #define TRUE 1 | |
| 85 #define FALSE 0 | |
| 86 | |
| 458 | 87 #ifdef DEBUG |
| 88 # undef DEBUG | |
| 89 # define DEBUG TRUE | |
| 90 #else | |
| 91 # define DEBUG FALSE | |
| 92 # define NDEBUG /* disable assert */ | |
| 428 | 93 #endif |
| 94 | |
| 95 #ifdef HAVE_CONFIG_H | |
| 96 # include <config.h> | |
| 97 /* On some systems, Emacs defines static as nothing for the sake | |
| 98 of unexec. We don't want that here since we don't use unexec. */ | |
| 99 # undef static | |
| 3524 | 100 # ifndef PTR /* for XEmacs */ |
| 2225 | 101 # define PTR void * |
| 715 | 102 # endif |
| 3524 | 103 # ifndef __P /* for XEmacs */ |
| 2225 | 104 # define __P(args) args |
| 709 | 105 # endif |
| 2325 | 106 #else /* no config.h */ |
| 709 | 107 # if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C)) |
| 108 # define __P(args) args /* use prototypes */ | |
| 109 # define PTR void * /* for generic pointers */ | |
| 2325 | 110 # else /* not standard C */ |
| 709 | 111 # define __P(args) () /* no prototypes */ |
| 112 # define const /* remove const for old compilers' sake */ | |
| 113 # define PTR long * /* don't use void* */ | |
| 531 | 114 # endif |
| 115 #endif /* !HAVE_CONFIG_H */ | |
| 428 | 116 |
| 432 | 117 #ifndef _GNU_SOURCE |
| 118 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */ | |
| 119 #endif | |
| 120 | |
| 3524 | 121 /* WIN32_NATIVE is for XEmacs. |
| 458 | 122 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */ |
| 442 | 123 #ifdef WIN32_NATIVE |
| 458 | 124 # undef MSDOS |
| 125 # undef WINDOWSNT | |
| 126 # define WINDOWSNT | |
| 127 #endif /* WIN32_NATIVE */ | |
| 128 | |
| 129 #ifdef MSDOS | |
| 130 # undef MSDOS | |
| 131 # define MSDOS TRUE | |
| 132 # include <fcntl.h> | |
| 133 # include <sys/param.h> | |
| 134 # include <io.h> | |
| 135 # ifndef HAVE_CONFIG_H | |
| 136 # define DOS_NT | |
| 137 # include <sys/config.h> | |
| 138 # endif | |
| 139 #else | |
| 140 # define MSDOS FALSE | |
| 141 #endif /* MSDOS */ | |
| 142 | |
| 143 #ifdef WINDOWSNT | |
| 428 | 144 # include <stdlib.h> |
| 145 # include <fcntl.h> | |
| 146 # include <string.h> | |
| 442 | 147 # include <direct.h> |
| 428 | 148 # include <io.h> |
| 149 # define MAXPATHLEN _MAX_PATH | |
| 458 | 150 # undef HAVE_NTGUI |
| 151 # undef DOS_NT | |
| 152 # define DOS_NT | |
| 428 | 153 # ifndef HAVE_GETCWD |
| 154 # define HAVE_GETCWD | |
| 155 # endif /* undef HAVE_GETCWD */ | |
| 2325 | 156 #else /* not WINDOWSNT */ |
| 442 | 157 # ifdef STDC_HEADERS |
| 158 # include <stdlib.h> | |
| 159 # include <string.h> | |
| 2325 | 160 # else /* no standard C headers */ |
| 3972 | 161 extern char *getenv (); |
| 162 extern char *strcpy (); | |
| 163 extern char *strncpy (); | |
| 164 extern char *strcat (); | |
| 165 extern char *strncat (); | |
| 166 extern unsigned long strlen (); | |
| 167 extern PTR malloc (); | |
| 168 extern PTR realloc (); | |
| 2325 | 169 # ifdef VMS |
| 170 # define EXIT_SUCCESS 1 | |
| 171 # define EXIT_FAILURE 0 | |
| 172 # else /* no VMS */ | |
| 173 # define EXIT_SUCCESS 0 | |
| 174 # define EXIT_FAILURE 1 | |
| 175 # endif | |
| 442 | 176 # endif |
| 458 | 177 #endif /* !WINDOWSNT */ |
| 428 | 178 |
| 179 #ifdef HAVE_UNISTD_H | |
| 180 # include <unistd.h> | |
| 181 #else | |
| 458 | 182 # if defined (HAVE_GETCWD) && !defined (WINDOWSNT) |
| 442 | 183 extern char *getcwd (char *buf, size_t size); |
| 428 | 184 # endif |
| 185 #endif /* HAVE_UNISTD_H */ | |
| 186 | |
| 187 #include <stdio.h> | |
| 188 #include <ctype.h> | |
| 189 #include <errno.h> | |
| 442 | 190 #ifndef errno |
| 191 extern int errno; | |
| 192 #endif | |
| 428 | 193 #include <sys/types.h> |
| 194 #include <sys/stat.h> | |
| 195 | |
| 458 | 196 #include <assert.h> |
| 197 #ifdef NDEBUG | |
| 198 # undef assert /* some systems have a buggy assert.h */ | |
| 199 # define assert(x) ((void) 0) | |
| 200 #endif | |
| 201 | |
| 428 | 202 #if !defined (S_ISREG) && defined (S_IFREG) |
| 203 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) | |
| 204 #endif | |
| 205 | |
| 3517 | 206 #ifdef NO_LONG_OPTIONS /* define this if you don't have GNU getopt */ |
| 207 # define NO_LONG_OPTIONS TRUE | |
| 428 | 208 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr) |
| 209 extern char *optarg; | |
| 210 extern int optind, opterr; | |
| 3517 | 211 #else |
| 212 # define NO_LONG_OPTIONS FALSE | |
| 213 # include <getopt.h> | |
| 214 #endif /* NO_LONG_OPTIONS */ | |
| 215 | |
| 216 #ifndef HAVE_CONFIG_H /* this is a standalone compilation */ | |
| 217 # ifdef __CYGWIN__ /* compiling on Cygwin */ | |
| 709 | 218 !!! NOTICE !!! |
| 219 the regex.h distributed with Cygwin is not compatible with etags, alas! | |
| 220 If you want regular expression support, you should delete this notice and | |
| 221 arrange to use the GNU regex.h and regex.c. | |
| 222 # endif | |
| 3517 | 223 #endif |
| 224 #include <regex.h> | |
| 428 | 225 |
| 226 /* Define CTAGS to make the program "ctags" compatible with the usual one. | |
| 227 Leave it undefined to make the program "etags", which makes emacs-style | |
| 228 tag tables and tags typedefs, #defines and struct/union/enum by default. */ | |
| 229 #ifdef CTAGS | |
| 230 # undef CTAGS | |
| 231 # define CTAGS TRUE | |
| 232 #else | |
| 233 # define CTAGS FALSE | |
| 234 #endif | |
| 235 | |
| 458 | 236 #define streq(s,t) (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t)) |
| 2225 | 237 #define strcaseeq(s,t) (assert((s)!=NULL && (t)!=NULL), !etags_strcasecmp (s, t)) |
| 458 | 238 #define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n)) |
| 2225 | 239 #define strncaseeq(s,t,n) (assert((s)!=NULL && (t)!=NULL), !etags_strncasecmp (s, t, n)) |
| 428 | 240 |
| 241 #define CHARS 256 /* 2^sizeof(char) */ | |
| 458 | 242 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1)) |
| 2225 | 243 #define iswhite(c) (_wht[CHAR(c)]) /* c is white (see white) */ |
| 244 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name (see nonam) */ | |
| 245 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token (see begtk) */ | |
| 246 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token (see midtk) */ | |
| 247 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens (see endtk) */ | |
| 428 | 248 |
| 458 | 249 #define ISALNUM(c) isalnum (CHAR(c)) |
| 250 #define ISALPHA(c) isalpha (CHAR(c)) | |
| 251 #define ISDIGIT(c) isdigit (CHAR(c)) | |
| 252 #define ISLOWER(c) islower (CHAR(c)) | |
| 253 | |
| 254 #define lowcase(c) tolower (CHAR(c)) | |
| 255 #define upcase(c) toupper (CHAR(c)) | |
| 256 | |
| 428 | 257 |
| 258 /* | |
| 259 * xnew, xrnew -- allocate, reallocate storage | |
| 260 * | |
| 261 * SYNOPSIS: Type *xnew (int n, Type); | |
| 458 | 262 * void xrnew (OldPointer, int n, Type); |
| 428 | 263 */ |
| 458 | 264 #if DEBUG |
| 428 | 265 # include "chkmalloc.h" |
| 266 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \ | |
| 267 (n) * sizeof (Type))) | |
| 458 | 268 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \ |
| 269 (char *) (op), (n) * sizeof (Type))) | |
| 428 | 270 #else |
| 271 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type))) | |
| 458 | 272 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \ |
| 273 (char *) (op), (n) * sizeof (Type))) | |
| 428 | 274 #endif |
| 275 | |
| 709 | 276 #define bool int |
| 277 | |
| 278 typedef void Lang_function __P((FILE *)); | |
| 428 | 279 |
| 280 typedef struct | |
| 281 { | |
| 2225 | 282 char *suffix; /* file name suffix for this compressor */ |
| 283 char *command; /* takes one arg and decompresses to stdout */ | |
| 428 | 284 } compressor; |
| 285 | |
| 286 typedef struct | |
| 287 { | |
| 2225 | 288 char *name; /* language name */ |
| 289 char *help; /* detailed help for the language */ | |
| 290 Lang_function *function; /* parse function */ | |
| 291 char **suffixes; /* name suffixes of this language's files */ | |
| 292 char **filenames; /* names of this language's files */ | |
| 293 char **interpreters; /* interpreters for this language */ | |
| 294 bool metasource; /* source used to generate other sources */ | |
| 428 | 295 } language; |
| 296 | |
| 2225 | 297 typedef struct fdesc |
| 298 { | |
| 299 struct fdesc *next; /* for the linked list */ | |
| 300 char *infname; /* uncompressed input file name */ | |
| 301 char *infabsname; /* absolute uncompressed input file name */ | |
| 302 char *infabsdir; /* absolute dir of input file */ | |
| 303 char *taggedfname; /* file name to write in tagfile */ | |
| 304 language *lang; /* language of file */ | |
| 305 char *prop; /* file properties to write in tagfile */ | |
| 306 bool usecharno; /* etags tags shall contain char number */ | |
| 307 bool written; /* entry written in the tags file */ | |
| 308 } fdesc; | |
| 309 | |
| 428 | 310 typedef struct node_st |
| 2225 | 311 { /* sorting structure */ |
| 312 struct node_st *left, *right; /* left and right sons */ | |
| 313 fdesc *fdp; /* description of file to whom tag belongs */ | |
| 314 char *name; /* tag name */ | |
| 315 char *regex; /* search regexp */ | |
| 316 bool valid; /* write this tag on the tag file */ | |
| 317 bool is_func; /* function tag: use regexp in CTAGS mode */ | |
| 318 bool been_warned; /* warning already given for duplicated tag */ | |
| 319 int lno; /* line number tag is on */ | |
| 428 | 320 long cno; /* character number line starts on */ |
| 321 } node; | |
| 322 | |
| 323 /* | |
| 324 * A `linebuffer' is a structure which holds a line of text. | |
| 325 * `readline_internal' reads a line from a stream into a linebuffer | |
| 326 * and works regardless of the length of the line. | |
| 327 * SIZE is the size of BUFFER, LEN is the length of the string in | |
| 328 * BUFFER after readline reads it. | |
| 329 */ | |
| 330 typedef struct | |
| 331 { | |
| 332 long size; | |
| 333 int len; | |
| 334 char *buffer; | |
| 335 } linebuffer; | |
| 336 | |
| 2225 | 337 /* Used to support mixing of --lang and file names. */ |
| 338 typedef struct | |
| 339 { | |
| 340 enum { | |
| 341 at_language, /* a language specification */ | |
| 342 at_regexp, /* a regular expression */ | |
| 343 at_filename, /* a file name */ | |
| 344 at_stdin, /* read from stdin here */ | |
| 345 at_end /* stop parsing the list */ | |
| 346 } arg_type; /* argument type */ | |
| 347 language *lang; /* language associated with the argument */ | |
| 348 char *what; /* the argument itself */ | |
| 349 } argument; | |
| 350 | |
| 351 /* Structure defining a regular expression. */ | |
| 352 typedef struct regexp | |
| 353 { | |
| 354 struct regexp *p_next; /* pointer to next in list */ | |
| 355 language *lang; /* if set, use only for this language */ | |
| 356 char *pattern; /* the regexp pattern */ | |
| 357 char *name; /* tag name */ | |
| 358 struct re_pattern_buffer *pat; /* the compiled pattern */ | |
| 359 struct re_registers regs; /* re registers */ | |
| 360 bool error_signaled; /* already signaled for this regexp */ | |
| 361 bool force_explicit_name; /* do not allow implict tag name */ | |
| 362 bool ignore_case; /* ignore case when matching */ | |
| 363 bool multi_line; /* do a multi-line match on the whole file */ | |
| 364 } regexp; | |
| 365 | |
| 366 | |
| 428 | 367 /* Many compilers barf on this: |
| 368 Lang_function Ada_funcs; | |
| 369 so let's write it this way */ | |
| 709 | 370 static void Ada_funcs __P((FILE *)); |
| 371 static void Asm_labels __P((FILE *)); | |
| 372 static void C_entries __P((int c_ext, FILE *)); | |
| 373 static void default_C_entries __P((FILE *)); | |
| 374 static void plain_C_entries __P((FILE *)); | |
| 375 static void Cjava_entries __P((FILE *)); | |
| 376 static void Cobol_paragraphs __P((FILE *)); | |
| 377 static void Cplusplus_entries __P((FILE *)); | |
| 378 static void Cstar_entries __P((FILE *)); | |
| 379 static void Erlang_functions __P((FILE *)); | |
| 2554 | 380 static void Forth_words __P((FILE *)); |
| 709 | 381 static void Fortran_functions __P((FILE *)); |
| 2225 | 382 static void HTML_labels __P((FILE *)); |
| 709 | 383 static void Lisp_functions __P((FILE *)); |
| 2325 | 384 static void Lua_functions __P((FILE *)); |
| 709 | 385 static void Makefile_targets __P((FILE *)); |
| 386 static void Pascal_functions __P((FILE *)); | |
| 387 static void Perl_functions __P((FILE *)); | |
| 388 static void PHP_functions __P((FILE *)); | |
| 2225 | 389 static void PS_functions __P((FILE *)); |
| 709 | 390 static void Prolog_functions __P((FILE *)); |
| 391 static void Python_functions __P((FILE *)); | |
| 392 static void Scheme_functions __P((FILE *)); | |
| 393 static void TeX_commands __P((FILE *)); | |
| 394 static void Texinfo_nodes __P((FILE *)); | |
| 2225 | 395 static void Yacc_entries __P((FILE *)); |
| 709 | 396 static void just_read_file __P((FILE *)); |
| 397 | |
| 398 static void print_language_names __P((void)); | |
| 399 static void print_version __P((void)); | |
| 2225 | 400 static void print_help __P((argument *)); |
| 709 | 401 int main __P((int, char **)); |
| 402 | |
| 403 static compressor *get_compressor_from_suffix __P((char *, char **)); | |
| 404 static language *get_language_from_langname __P((const char *)); | |
| 405 static language *get_language_from_interpreter __P((char *)); | |
| 2225 | 406 static language *get_language_from_filename __P((char *, bool)); |
| 407 static void readline __P((linebuffer *, FILE *)); | |
| 709 | 408 static long readline_internal __P((linebuffer *, FILE *)); |
| 2225 | 409 static bool nocase_tail __P((char *)); |
| 410 static void get_tag __P((char *, char **)); | |
| 428 | 411 |
| 2225 | 412 static void analyse_regex __P((char *)); |
| 413 static void free_regexps __P((void)); | |
| 414 static void regex_tag_multiline __P((void)); | |
| 709 | 415 static void error __P((const char *, const char *)); |
| 416 static void suggest_asking_for_help __P((void)); | |
| 417 void fatal __P((char *, char *)); | |
| 418 static void pfatal __P((char *)); | |
| 419 static void add_node __P((node *, node **)); | |
| 420 | |
| 421 static void init __P((void)); | |
| 2225 | 422 static void process_file_name __P((char *, language *)); |
| 423 static void process_file __P((FILE *, char *, language *)); | |
| 424 static void find_entries __P((FILE *)); | |
| 709 | 425 static void free_tree __P((node *)); |
| 2225 | 426 static void free_fdesc __P((fdesc *)); |
| 709 | 427 static void pfnote __P((char *, bool, char *, int, int, long)); |
| 2225 | 428 static void make_tag __P((char *, int, bool, char *, int, int, long)); |
| 429 static void invalidate_nodes __P((fdesc *, node **)); | |
| 709 | 430 static void put_entries __P((node *)); |
| 431 | |
| 432 static char *concat __P((char *, char *, char *)); | |
| 433 static char *skip_spaces __P((char *)); | |
| 434 static char *skip_non_spaces __P((char *)); | |
| 435 static char *savenstr __P((char *, int)); | |
| 436 static char *savestr __P((char *)); | |
| 437 static char *etags_strchr __P((const char *, int)); | |
| 438 static char *etags_strrchr __P((const char *, int)); | |
| 2225 | 439 static int etags_strcasecmp __P((const char *, const char *)); |
| 440 static int etags_strncasecmp __P((const char *, const char *, int)); | |
| 709 | 441 static char *etags_getcwd __P((void)); |
| 442 static char *relative_filename __P((char *, char *)); | |
| 443 static char *absolute_filename __P((char *, char *)); | |
| 444 static char *absolute_dirname __P((char *, char *)); | |
| 445 static bool filename_is_absolute __P((char *f)); | |
| 446 static void canonicalize_filename __P((char *)); | |
| 2225 | 447 static void linebuffer_init __P((linebuffer *)); |
| 709 | 448 static void linebuffer_setlen __P((linebuffer *, int)); |
| 2225 | 449 static PTR xmalloc __P((unsigned int)); |
| 450 static PTR xrealloc __P((char *, unsigned int)); | |
| 428 | 451 |
| 452 | |
| 2225 | 453 static char searchar = '/'; /* use /.../ searches */ |
| 454 | |
| 455 static char *tagfile; /* output file */ | |
| 456 static char *progname; /* name this program was invoked with */ | |
| 457 static char *cwd; /* current working directory */ | |
| 458 static char *tagfiledir; /* directory of tagfile */ | |
| 459 static FILE *tagf; /* ioptr for tags file */ | |
| 460 | |
| 461 static fdesc *fdhead; /* head of file description list */ | |
| 462 static fdesc *curfdp; /* current file description */ | |
| 463 static int lineno; /* line number of current line */ | |
| 464 static long charno; /* current character number */ | |
| 465 static long linecharno; /* charno of start of current line */ | |
| 466 static char *dbp; /* pointer to start of current tag */ | |
| 467 | |
| 468 static const int invalidcharno = -1; | |
| 469 | |
| 470 static node *nodehead; /* the head of the binary tree of tags */ | |
| 471 static node *last_node; /* the last node created */ | |
| 472 | |
| 473 static linebuffer lb; /* the current line */ | |
| 474 static linebuffer filebuf; /* a buffer containing the whole file */ | |
| 475 static linebuffer token_name; /* a buffer containing a tag name */ | |
| 428 | 476 |
| 477 /* boolean "functions" (see init) */ | |
| 2225 | 478 static bool _wht[CHARS], _nin[CHARS], _itk[CHARS], _btk[CHARS], _etk[CHARS]; |
| 479 static char | |
| 428 | 480 /* white chars */ |
| 442 | 481 *white = " \f\t\n\r\v", |
| 428 | 482 /* not in a name */ |
| 2225 | 483 *nonam = " \f\t\n\r()=,;", /* look at make_tag before modifying! */ |
| 428 | 484 /* token ending chars */ |
| 485 *endtk = " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?", | |
| 486 /* token starting chars */ | |
| 487 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@", | |
| 488 /* valid in-token chars */ | |
| 489 *midtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789"; | |
| 490 | |
| 2225 | 491 static bool append_to_tagfile; /* -a: append to tags */ |
| 3972 | 492 /* The next five default to TRUE for etags, but to FALSE for ctags. */ |
| 2225 | 493 static bool typedefs; /* -t: create tags for C and Ada typedefs */ |
| 494 static bool typedefs_or_cplusplus; /* -T: create tags for C typedefs, level */ | |
| 428 | 495 /* 0 struct/enum/union decls, and C++ */ |
| 496 /* member functions. */ | |
| 2225 | 497 static bool constantypedefs; /* -d: create tags for C #define, enum */ |
| 428 | 498 /* constants and variables. */ |
| 499 /* -D: opposite of -d. Default under ctags. */ | |
| 2225 | 500 static bool globals; /* create tags for global variables */ |
| 3972 | 501 static bool members; /* create tags for C member variables */ |
| 2225 | 502 static bool declarations; /* --declarations: tag them and extern in C&Co*/ |
| 503 static bool no_line_directive; /* ignore #line directives (undocumented) */ | |
| 3876 | 504 static bool no_duplicates; /* no duplicate tags for ctags (undocumented) */ |
| 2225 | 505 static bool update; /* -u: update tags */ |
| 506 static bool vgrind_style; /* -v: create vgrind style index output */ | |
| 3876 | 507 static bool no_warnings; /* -w: suppress warnings (undocumented) */ |
| 2225 | 508 static bool cxref_style; /* -x: create cxref style output */ |
| 509 static bool cplusplus; /* .[hc] means C++, not C */ | |
| 510 static bool ignoreindent; /* -I: ignore indentation in C */ | |
| 511 static bool packages_only; /* --packages-only: in Ada, only tag packages*/ | |
| 512 | |
| 3517 | 513 /* STDIN is defined in LynxOS system headers */ |
| 514 #ifdef STDIN | |
| 515 # undef STDIN | |
| 516 #endif | |
| 517 | |
| 2225 | 518 #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */ |
| 519 static bool parsing_stdin; /* --parse-stdin used */ | |
| 520 | |
| 521 static regexp *p_head; /* list of all regexps */ | |
| 522 static bool need_filebuf; /* some regexes are multi-line */ | |
| 3517 | 523 |
| 2225 | 524 static struct option longopts[] = |
| 428 | 525 { |
| 3876 | 526 { "append", no_argument, NULL, 'a' }, |
| 527 { "packages-only", no_argument, &packages_only, TRUE }, | |
| 528 { "c++", no_argument, NULL, 'C' }, | |
| 529 { "declarations", no_argument, &declarations, TRUE }, | |
| 530 { "no-line-directive", no_argument, &no_line_directive, TRUE }, | |
| 531 { "no-duplicates", no_argument, &no_duplicates, TRUE }, | |
| 532 { "help", no_argument, NULL, 'h' }, | |
| 533 { "help", no_argument, NULL, 'H' }, | |
| 534 { "ignore-indentation", no_argument, NULL, 'I' }, | |
| 535 { "language", required_argument, NULL, 'l' }, | |
| 536 { "members", no_argument, &members, TRUE }, | |
| 537 { "no-members", no_argument, &members, FALSE }, | |
| 538 { "output", required_argument, NULL, 'o' }, | |
| 539 { "regex", required_argument, NULL, 'r' }, | |
| 540 { "no-regex", no_argument, NULL, 'R' }, | |
| 541 { "ignore-case-regex", required_argument, NULL, 'c' }, | |
| 2225 | 542 { "parse-stdin", required_argument, NULL, STDIN }, |
| 3876 | 543 { "version", no_argument, NULL, 'V' }, |
| 2225 | 544 |
| 3090 | 545 #if CTAGS /* Ctags options */ |
| 3876 | 546 { "backward-search", no_argument, NULL, 'B' }, |
| 547 { "cxref", no_argument, NULL, 'x' }, | |
| 548 { "defines", no_argument, NULL, 'd' }, | |
| 549 { "globals", no_argument, &globals, TRUE }, | |
| 550 { "typedefs", no_argument, NULL, 't' }, | |
| 551 { "typedefs-and-c++", no_argument, NULL, 'T' }, | |
| 552 { "update", no_argument, NULL, 'u' }, | |
| 553 { "vgrind", no_argument, NULL, 'v' }, | |
| 554 { "no-warn", no_argument, NULL, 'w' }, | |
| 2225 | 555 |
| 3090 | 556 #else /* Etags options */ |
| 3876 | 557 { "no-defines", no_argument, NULL, 'D' }, |
| 558 { "no-globals", no_argument, &globals, FALSE }, | |
| 559 { "include", required_argument, NULL, 'i' }, | |
| 2225 | 560 #endif |
| 428 | 561 { NULL } |
| 562 }; | |
| 563 | |
| 2225 | 564 static compressor compressors[] = |
| 428 | 565 { |
| 566 { "z", "gzip -d -c"}, | |
| 567 { "Z", "gzip -d -c"}, | |
| 568 { "gz", "gzip -d -c"}, | |
| 569 { "GZ", "gzip -d -c"}, | |
| 570 { "bz2", "bzip2 -d -c" }, | |
| 571 { NULL } | |
| 572 }; | |
| 573 | |
| 574 /* | |
| 575 * Language stuff. | |
| 576 */ | |
| 577 | |
| 578 /* Ada code */ | |
| 2225 | 579 static char *Ada_suffixes [] = |
| 428 | 580 { "ads", "adb", "ada", NULL }; |
| 2225 | 581 static char Ada_help [] = |
| 582 "In Ada code, functions, procedures, packages, tasks and types are\n\ | |
| 583 tags. Use the `--packages-only' option to create tags for\n\ | |
| 584 packages only.\n\ | |
| 585 Ada tag names have suffixes indicating the type of entity:\n\ | |
| 586 Entity type: Qualifier:\n\ | |
| 587 ------------ ----------\n\ | |
| 588 function /f\n\ | |
| 589 procedure /p\n\ | |
| 590 package spec /s\n\ | |
| 591 package body /b\n\ | |
| 592 type /t\n\ | |
| 593 task /k\n\ | |
| 594 Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\ | |
| 595 body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\ | |
| 596 will just search for any tag `bidule'."; | |
| 428 | 597 |
| 598 /* Assembly code */ | |
| 2225 | 599 static char *Asm_suffixes [] = |
| 600 { "a", /* Unix assembler */ | |
| 601 "asm", /* Microcontroller assembly */ | |
| 602 "def", /* BSO/Tasking definition includes */ | |
| 603 "inc", /* Microcontroller include files */ | |
| 604 "ins", /* Microcontroller include files */ | |
| 605 "s", "sa", /* Unix assembler */ | |
| 606 "S", /* cpp-processed Unix assembler */ | |
| 607 "src", /* BSO/Tasking C compiler output */ | |
| 608 NULL | |
| 609 }; | |
| 610 static char Asm_help [] = | |
| 611 "In assembler code, labels appearing at the beginning of a line,\n\ | |
| 612 followed by a colon, are tags."; | |
| 613 | |
| 428 | 614 |
| 615 /* Note that .c and .h can be considered C++, if the --c++ flag was | |
| 2225 | 616 given, or if the `class' or `template' keyowrds are met inside the file. |
| 458 | 617 That is why default_C_entries is called for these. */ |
| 2225 | 618 static char *default_C_suffixes [] = |
| 428 | 619 { "c", "h", NULL }; |
| 2225 | 620 static char default_C_help [] = |
| 621 "In C code, any C function or typedef is a tag, and so are\n\ | |
| 622 definitions of `struct', `union' and `enum'. `#define' macro\n\ | |
| 623 definitions and `enum' constants are tags unless you specify\n\ | |
| 624 `--no-defines'. Global variables are tags unless you specify\n\ | |
| 3972 | 625 `--no-globals' and so are struct members unless you specify\n\ |
| 626 `--no-members'. Use of `--no-globals', `--no-defines' and\n\ | |
| 627 `--no-members' can make the tags table file much smaller.\n\ | |
| 2225 | 628 You can tag function declarations and external variables by\n\ |
| 3972 | 629 using `--declarations'."; |
| 2225 | 630 |
| 631 static char *Cplusplus_suffixes [] = | |
| 458 | 632 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx", |
| 428 | 633 "M", /* Objective C++ */ |
| 634 "pdb", /* Postscript with C syntax */ | |
| 635 NULL }; | |
| 2225 | 636 static char Cplusplus_help [] = |
| 637 "In C++ code, all the tag constructs of C code are tagged. (Use\n\ | |
| 638 --help --lang=c --lang=c++ for full help.)\n\ | |
| 3972 | 639 In addition to C tags, member functions are also recognized. Member\n\ |
| 640 variables are recognized unless you use the `--no-members' option.\n\ | |
| 2225 | 641 Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\ |
| 642 and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\ | |
| 643 `operator+'."; | |
| 644 | |
| 645 static char *Cjava_suffixes [] = | |
| 428 | 646 { "java", NULL }; |
| 2225 | 647 static char Cjava_help [] = |
| 648 "In Java code, all the tags constructs of C and C++ code are\n\ | |
| 649 tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)"; | |
| 650 | |
| 651 | |
| 652 static char *Cobol_suffixes [] = | |
| 428 | 653 { "COB", "cob", NULL }; |
| 2225 | 654 static char Cobol_help [] = |
| 655 "In Cobol code, tags are paragraph names; that is, any word\n\ | |
| 656 starting in column 8 and followed by a period."; | |
| 657 | |
| 658 static char *Cstar_suffixes [] = | |
| 428 | 659 { "cs", "hs", NULL }; |
| 660 | |
| 2225 | 661 static char *Erlang_suffixes [] = |
| 428 | 662 { "erl", "hrl", NULL }; |
| 2225 | 663 static char Erlang_help [] = |
| 664 "In Erlang code, the tags are the functions, records and macros\n\ | |
| 665 defined in the file."; | |
| 666 | |
| 2554 | 667 char *Forth_suffixes [] = |
| 668 { "fth", "tok", NULL }; | |
| 669 static char Forth_help [] = | |
| 670 "In Forth code, tags are words defined by `:',\n\ | |
| 671 constant, code, create, defer, value, variable, buffer:, field."; | |
| 672 | |
| 2225 | 673 static char *Fortran_suffixes [] = |
| 428 | 674 { "F", "f", "f90", "for", NULL }; |
| 2225 | 675 static char Fortran_help [] = |
| 676 "In Fortran code, functions, subroutines and block data are tags."; | |
| 677 | |
| 678 static char *HTML_suffixes [] = | |
| 679 { "htm", "html", "shtml", NULL }; | |
| 680 static char HTML_help [] = | |
| 681 "In HTML input files, the tags are the `title' and the `h1', `h2',\n\ | |
| 682 `h3' headers. Also, tags are `name=' in anchors and all\n\ | |
| 683 occurrences of `id='."; | |
| 684 | |
| 685 static char *Lisp_suffixes [] = | |
| 458 | 686 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL }; |
| 2225 | 687 static char Lisp_help [] = |
| 688 "In Lisp code, any function defined with `defun', any variable\n\ | |
| 689 defined with `defvar' or `defconst', and in general the first\n\ | |
| 690 argument of any expression that starts with `(def' in column zero\n\ | |
| 691 is a tag."; | |
| 692 | |
| 2325 | 693 static char *Lua_suffixes [] = |
| 694 { "lua", "LUA", NULL }; | |
| 695 static char Lua_help [] = | |
| 696 "In Lua scripts, all functions are tags."; | |
| 697 | |
| 2225 | 698 static char *Makefile_filenames [] = |
| 458 | 699 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL}; |
| 2225 | 700 static char Makefile_help [] = |
| 701 "In makefiles, targets are tags; additionally, variables are tags\n\ | |
| 702 unless you specify `--no-globals'."; | |
| 703 | |
| 704 static char *Objc_suffixes [] = | |
| 458 | 705 { "lm", /* Objective lex file */ |
| 428 | 706 "m", /* Objective C file */ |
| 707 NULL }; | |
| 2225 | 708 static char Objc_help [] = |
| 709 "In Objective C code, tags include Objective C definitions for classes,\n\ | |
| 710 class categories, methods and protocols. Tags for variables and\n\ | |
| 3517 | 711 functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.\n\ |
| 712 (Use --help --lang=c --lang=objc --lang=java for full help.)"; | |
| 2225 | 713 |
| 714 static char *Pascal_suffixes [] = | |
| 715 { "p", "pas", NULL }; | |
| 716 static char Pascal_help [] = | |
| 717 "In Pascal code, the tags are the functions and procedures defined\n\ | |
| 718 in the file."; | |
| 3517 | 719 /* " // this is for working around an Emacs highlighting bug... */ |
| 2225 | 720 |
| 721 static char *Perl_suffixes [] = | |
| 722 { "pl", "pm", NULL }; | |
| 723 static char *Perl_interpreters [] = | |
| 724 { "perl", "@PERL@", NULL }; | |
| 725 static char Perl_help [] = | |
| 726 "In Perl code, the tags are the packages, subroutines and variables\n\ | |
| 727 defined by the `package', `sub', `my' and `local' keywords. Use\n\ | |
| 728 `--globals' if you want to tag global variables. Tags for\n\ | |
| 729 subroutines are named `PACKAGE::SUB'. The name for subroutines\n\ | |
| 730 defined in the default package is `main::SUB'."; | |
| 731 | |
| 732 static char *PHP_suffixes [] = | |
| 733 { "php", "php3", "php4", NULL }; | |
| 734 static char PHP_help [] = | |
| 3972 | 735 "In PHP code, tags are functions, classes and defines. Unless you use\n\ |
| 736 the `--no-members' option, vars are tags too."; | |
| 2225 | 737 |
| 738 static char *plain_C_suffixes [] = | |
| 739 { "pc", /* Pro*C file */ | |
| 740 NULL }; | |
| 741 | |
| 742 static char *PS_suffixes [] = | |
| 428 | 743 { "ps", "psw", NULL }; /* .psw is for PSWrap */ |
| 2225 | 744 static char PS_help [] = |
| 745 "In PostScript code, the tags are the functions."; | |
| 746 | |
| 747 static char *Prolog_suffixes [] = | |
| 428 | 748 { "prolog", NULL }; |
| 2225 | 749 static char Prolog_help [] = |
| 750 "In Prolog code, tags are predicates and rules at the beginning of\n\ | |
| 751 line."; | |
| 752 | |
| 753 static char *Python_suffixes [] = | |
| 428 | 754 { "py", NULL }; |
| 2225 | 755 static char Python_help [] = |
| 756 "In Python code, `def' or `class' at the beginning of a line\n\ | |
| 757 generate a tag."; | |
| 428 | 758 |
| 759 /* Can't do the `SCM' or `scm' prefix with a version number. */ | |
| 2225 | 760 static char *Scheme_suffixes [] = |
| 458 | 761 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL }; |
| 2225 | 762 static char Scheme_help [] = |
| 763 "In Scheme code, tags include anything defined with `def' or with a\n\ | |
| 764 construct whose name starts with `def'. They also include\n\ | |
| 765 variables set with `set!' at top level in the file."; | |
| 766 | |
| 767 static char *TeX_suffixes [] = | |
| 458 | 768 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL }; |
| 2225 | 769 static char TeX_help [] = |
| 770 "In LaTeX text, the argument of any of the commands `\\chapter',\n\ | |
| 771 `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\ | |
| 772 `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\ | |
| 773 `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\ | |
| 774 `\\newenvironment' or `\\renewenvironment' is a tag.\n\ | |
| 775 \n\ | |
| 776 Other commands can be specified by setting the environment variable\n\ | |
| 777 `TEXTAGS' to a colon-separated list like, for example,\n\ | |
| 778 TEXTAGS=\"mycommand:myothercommand\"."; | |
| 779 | |
| 780 | |
| 781 static char *Texinfo_suffixes [] = | |
| 458 | 782 { "texi", "texinfo", "txi", NULL }; |
| 2225 | 783 static char Texinfo_help [] = |
| 784 "for texinfo files, lines starting with @node are tagged."; | |
| 785 | |
| 786 static char *Yacc_suffixes [] = | |
| 458 | 787 { "y", "y++", "ym", "yxx", "yy", NULL }; /* .ym is Objective yacc file */ |
| 2225 | 788 static char Yacc_help [] = |
| 789 "In Bison or Yacc input files, each rule defines as a tag the\n\ | |
| 790 nonterminal it constructs. The portions of the file that contain\n\ | |
| 791 C code are parsed as C code (use --help --lang=c --lang=yacc\n\ | |
| 792 for full help)."; | |
| 793 | |
| 794 static char auto_help [] = | |
| 795 "`auto' is not a real language, it indicates to use\n\ | |
| 796 a default language for files base on file name suffix and file contents."; | |
| 797 | |
| 798 static char none_help [] = | |
| 799 "`none' is not a real language, it indicates to only do\n\ | |
| 800 regexp processing on files."; | |
| 801 | |
| 802 static char no_lang_help [] = | |
| 803 "No detailed help available for this language."; | |
| 804 | |
| 428 | 805 |
| 806 /* | |
| 807 * Table of languages. | |
| 808 * | |
| 809 * It is ok for a given function to be listed under more than one | |
| 810 * name. I just didn't. | |
| 811 */ | |
| 812 | |
| 2225 | 813 static language lang_names [] = |
| 428 | 814 { |
| 2225 | 815 { "ada", Ada_help, Ada_funcs, Ada_suffixes }, |
| 816 { "asm", Asm_help, Asm_labels, Asm_suffixes }, | |
| 817 { "c", default_C_help, default_C_entries, default_C_suffixes }, | |
| 818 { "c++", Cplusplus_help, Cplusplus_entries, Cplusplus_suffixes }, | |
| 819 { "c*", no_lang_help, Cstar_entries, Cstar_suffixes }, | |
| 820 { "cobol", Cobol_help, Cobol_paragraphs, Cobol_suffixes }, | |
| 821 { "erlang", Erlang_help, Erlang_functions, Erlang_suffixes }, | |
| 2554 | 822 { "forth", Forth_help, Forth_words, Forth_suffixes }, |
| 2225 | 823 { "fortran", Fortran_help, Fortran_functions, Fortran_suffixes }, |
| 824 { "html", HTML_help, HTML_labels, HTML_suffixes }, | |
| 825 { "java", Cjava_help, Cjava_entries, Cjava_suffixes }, | |
| 826 { "lisp", Lisp_help, Lisp_functions, Lisp_suffixes }, | |
| 2325 | 827 { "lua", Lua_help, Lua_functions, Lua_suffixes }, |
| 2225 | 828 { "makefile", Makefile_help,Makefile_targets,NULL,Makefile_filenames}, |
| 829 { "objc", Objc_help, plain_C_entries, Objc_suffixes }, | |
| 830 { "pascal", Pascal_help, Pascal_functions, Pascal_suffixes }, | |
| 831 { "perl",Perl_help,Perl_functions,Perl_suffixes,NULL,Perl_interpreters}, | |
| 832 { "php", PHP_help, PHP_functions, PHP_suffixes }, | |
| 833 { "postscript",PS_help, PS_functions, PS_suffixes }, | |
| 834 { "proc", no_lang_help, plain_C_entries, plain_C_suffixes }, | |
| 835 { "prolog", Prolog_help, Prolog_functions, Prolog_suffixes }, | |
| 836 { "python", Python_help, Python_functions, Python_suffixes }, | |
| 837 { "scheme", Scheme_help, Scheme_functions, Scheme_suffixes }, | |
| 838 { "tex", TeX_help, TeX_commands, TeX_suffixes }, | |
| 839 { "texinfo", Texinfo_help, Texinfo_nodes, Texinfo_suffixes }, | |
| 840 { "yacc", Yacc_help,Yacc_entries,Yacc_suffixes,NULL,NULL,TRUE}, | |
| 841 { "auto", auto_help }, /* default guessing scheme */ | |
| 842 { "none", none_help, just_read_file }, /* regexp matching only */ | |
| 843 { NULL } /* end of list */ | |
| 428 | 844 }; |
| 458 | 845 |
| 428 | 846 |
| 847 static void | |
| 848 print_language_names () | |
| 849 { | |
| 850 language *lang; | |
| 458 | 851 char **name, **ext; |
| 428 | 852 |
| 853 puts ("\nThese are the currently supported languages, along with the\n\ | |
| 458 | 854 default file names and dot suffixes:"); |
| 428 | 855 for (lang = lang_names; lang->name != NULL; lang++) |
| 856 { | |
| 458 | 857 printf (" %-*s", 10, lang->name); |
| 858 if (lang->filenames != NULL) | |
| 859 for (name = lang->filenames; *name != NULL; name++) | |
| 860 printf (" %s", *name); | |
| 428 | 861 if (lang->suffixes != NULL) |
| 862 for (ext = lang->suffixes; *ext != NULL; ext++) | |
| 863 printf (" .%s", *ext); | |
| 864 puts (""); | |
| 865 } | |
| 2225 | 866 puts ("where `auto' means use default language for files based on file\n\ |
| 428 | 867 name suffix, and `none' means only do regexp processing on files.\n\ |
| 868 If no language is specified and no matching suffix is found,\n\ | |
| 869 the first line of the file is read for a sharp-bang (#!) sequence\n\ | |
| 870 followed by the name of an interpreter. If no such sequence is found,\n\ | |
| 871 Fortran is tried first; if no tags are found, C is tried next.\n\ | |
| 2225 | 872 When parsing any C file, a \"class\" or \"template\" keyword\n\ |
| 873 switches to C++."); | |
| 874 puts ("Compressed files are supported using gzip and bzip2.\n\ | |
| 875 \n\ | |
| 876 For detailed help on a given language use, for example,\n\ | |
| 877 etags --help --lang=ada."); | |
| 428 | 878 } |
| 879 | |
| 442 | 880 #ifndef EMACS_NAME |
| 2225 | 881 # define EMACS_NAME "standalone" |
| 428 | 882 #endif |
| 3972 | 883 #ifndef VERSION |
| 3993 | 884 # define VERSION "17.33" |
| 428 | 885 #endif |
| 886 static void | |
| 887 print_version () | |
| 888 { | |
| 3972 | 889 printf ("%s (%s %s)\n", (CTAGS) ? "ctags" : "etags", EMACS_NAME, VERSION); |
| 890 puts ("Copyright (C) 2007 Free Software Foundation, Inc."); | |
| 891 puts ("This program is distributed under the terms in ETAGS.README"); | |
| 428 | 892 |
| 2225 | 893 exit (EXIT_SUCCESS); |
| 428 | 894 } |
| 895 | |
| 3993 | 896 #ifndef PRINT_UNDOCUMENTED_OPTIONS_HELP |
| 897 # define PRINT_UNDOCUMENTED_OPTIONS_HELP FALSE | |
| 898 #endif | |
| 899 | |
| 428 | 900 static void |
| 2225 | 901 print_help (argbuffer) |
| 902 argument *argbuffer; | |
| 428 | 903 { |
| 2225 | 904 bool help_for_lang = FALSE; |
| 905 | |
| 906 for (; argbuffer->arg_type != at_end; argbuffer++) | |
| 907 if (argbuffer->arg_type == at_language) | |
| 908 { | |
| 909 if (help_for_lang) | |
| 910 puts (""); | |
| 911 puts (argbuffer->lang->help); | |
| 912 help_for_lang = TRUE; | |
| 913 } | |
| 914 | |
| 915 if (help_for_lang) | |
| 916 exit (EXIT_SUCCESS); | |
| 917 | |
| 428 | 918 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\ |
| 919 \n\ | |
| 920 These are the options accepted by %s.\n", progname, progname); | |
| 3517 | 921 if (NO_LONG_OPTIONS) |
| 922 puts ("WARNING: long option names do not work with this executable,\n\ | |
| 923 as it is not linked with GNU getopt."); | |
| 2325 | 924 else |
| 3517 | 925 puts ("You may use unambiguous abbreviations for the long option names."); |
| 2225 | 926 puts (" A - as file name means read names from stdin (one per line).\n\ |
| 927 Absolute names are stored in the output file as they are.\n\ | |
| 928 Relative ones are stored relative to the output file's directory.\n"); | |
| 929 | |
| 3090 | 930 puts ("-a, --append\n\ |
| 428 | 931 Append tag entries to existing tags file."); |
| 932 | |
| 933 puts ("--packages-only\n\ | |
| 2225 | 934 For Ada files, only generate tags for packages."); |
| 428 | 935 |
| 936 if (CTAGS) | |
| 937 puts ("-B, --backward-search\n\ | |
| 938 Write the search commands for the tag entries using '?', the\n\ | |
| 939 backward-search command instead of '/', the forward-search command."); | |
| 940 | |
| 458 | 941 /* This option is mostly obsolete, because etags can now automatically |
| 942 detect C++. Retained for backward compatibility and for debugging and | |
| 943 experimentation. In principle, we could want to tag as C++ even | |
| 2225 | 944 before any "class" or "template" keyword. |
| 428 | 945 puts ("-C, --c++\n\ |
| 946 Treat files whose name suffix defaults to C language as C++ files."); | |
| 458 | 947 */ |
| 428 | 948 |
| 949 puts ("--declarations\n\ | |
| 950 In C and derived languages, create tags for function declarations,"); | |
| 951 if (CTAGS) | |
| 952 puts ("\tand create tags for extern variables if --globals is used."); | |
| 953 else | |
| 954 puts | |
| 955 ("\tand create tags for extern variables unless --no-globals is used."); | |
| 956 | |
| 957 if (CTAGS) | |
| 958 puts ("-d, --defines\n\ | |
| 959 Create tag entries for C #define constants and enum constants, too."); | |
| 960 else | |
| 961 puts ("-D, --no-defines\n\ | |
| 962 Don't create tag entries for C #define constants and enum constants.\n\ | |
| 963 This makes the tags file smaller."); | |
| 964 | |
| 965 if (!CTAGS) | |
| 2225 | 966 puts ("-i FILE, --include=FILE\n\ |
| 428 | 967 Include a note in tag file indicating that, when searching for\n\ |
| 968 a tag, one should also consult the tags file FILE after\n\ | |
| 969 checking the current file."); | |
| 2225 | 970 |
| 971 puts ("-l LANG, --language=LANG\n\ | |
| 428 | 972 Force the following files to be considered as written in the\n\ |
| 973 named language up to the next --language=LANG option."); | |
| 974 | |
| 975 if (CTAGS) | |
| 976 puts ("--globals\n\ | |
| 977 Create tag entries for global variables in some languages."); | |
| 978 else | |
| 979 puts ("--no-globals\n\ | |
| 980 Do not create tag entries for global variables in some\n\ | |
| 981 languages. This makes the tags file smaller."); | |
| 3993 | 982 |
| 983 if (PRINT_UNDOCUMENTED_OPTIONS_HELP) | |
| 984 puts ("--no-line-directive\n\ | |
| 985 Ignore #line preprocessor directives in C and derived languages."); | |
| 986 | |
| 3972 | 987 if (CTAGS) |
| 988 puts ("--members\n\ | |
| 2225 | 989 Create tag entries for members of structures in some languages."); |
| 3972 | 990 else |
| 991 puts ("--no-members\n\ | |
| 992 Do not create tag entries for members of structures\n\ | |
| 993 in some languages."); | |
| 428 | 994 |
| 2225 | 995 puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\ |
| 996 Make a tag for each line matching a regular expression pattern\n\ | |
| 997 in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\ | |
| 998 files only. REGEXFILE is a file containing one REGEXP per line.\n\ | |
| 999 REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\ | |
| 1000 optional. The TAGREGEXP pattern is anchored (as if preceded by ^)."); | |
| 1001 puts (" If TAGNAME/ is present, the tags created are named.\n\ | |
| 428 | 1002 For example Tcl named tags can be created with:\n\ |
| 2225 | 1003 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\ |
| 1004 MODS are optional one-letter modifiers: `i' means to ignore case,\n\ | |
| 1005 `m' means to allow multi-line matches, `s' implies `m' and\n\ | |
| 1006 causes dot to match any character, including newline."); | |
| 3993 | 1007 |
| 428 | 1008 puts ("-R, --no-regex\n\ |
| 1009 Don't create tags from regexps for the following files."); | |
| 3993 | 1010 |
| 2225 | 1011 puts ("-I, --ignore-indentation\n\ |
| 1012 In C and C++ do not assume that a closing brace in the first\n\ | |
| 1013 column is the final brace of a function or structure definition."); | |
| 3993 | 1014 |
| 428 | 1015 puts ("-o FILE, --output=FILE\n\ |
| 1016 Write the tags to FILE."); | |
| 3993 | 1017 |
| 2225 | 1018 puts ("--parse-stdin=NAME\n\ |
| 1019 Read from standard input and record tags as belonging to file NAME."); | |
| 428 | 1020 |
| 1021 if (CTAGS) | |
| 1022 { | |
| 1023 puts ("-t, --typedefs\n\ | |
| 1024 Generate tag entries for C and Ada typedefs."); | |
| 1025 puts ("-T, --typedefs-and-c++\n\ | |
| 1026 Generate tag entries for C typedefs, C struct/enum/union tags,\n\ | |
| 1027 and C++ member functions."); | |
| 2225 | 1028 } |
| 1029 | |
| 1030 if (CTAGS) | |
| 1031 puts ("-u, --update\n\ | |
| 428 | 1032 Update the tag entries for the given files, leaving tag\n\ |
| 1033 entries for other files in place. Currently, this is\n\ | |
| 1034 implemented by deleting the existing entries for the given\n\ | |
| 1035 files and then rewriting the new entries at the end of the\n\ | |
| 1036 tags file. It is often faster to simply rebuild the entire\n\ | |
| 1037 tag file than to use this."); | |
| 2225 | 1038 |
| 1039 if (CTAGS) | |
| 1040 { | |
| 428 | 1041 puts ("-v, --vgrind\n\ |
| 3090 | 1042 Print on the standard output an index of items intended for\n\ |
| 1043 human consumption, similar to the output of vgrind. The index\n\ | |
| 1044 is sorted, and gives the page number of each item."); | |
| 3993 | 1045 |
| 1046 if (PRINT_UNDOCUMENTED_OPTIONS_HELP) | |
| 1047 puts ("-w, --no-duplicates\n\ | |
| 3876 | 1048 Do not create duplicate tag entries, for compatibility with\n\ |
| 1049 traditional ctags."); | |
| 3993 | 1050 |
| 1051 if (PRINT_UNDOCUMENTED_OPTIONS_HELP) | |
| 1052 puts ("-w, --no-warn\n\ | |
| 3876 | 1053 Suppress warning messages about duplicate tag entries."); |
| 3993 | 1054 |
| 428 | 1055 puts ("-x, --cxref\n\ |
| 1056 Like --vgrind, but in the style of cxref, rather than vgrind.\n\ | |
| 1057 The output uses line numbers instead of page numbers, but\n\ | |
| 1058 beyond that the differences are cosmetic; try both to see\n\ | |
| 1059 which you like."); | |
| 1060 } | |
| 1061 | |
| 1062 puts ("-V, --version\n\ | |
| 1063 Print the version of the program.\n\ | |
| 1064 -h, --help\n\ | |
| 2225 | 1065 Print this help message.\n\ |
| 1066 Followed by one or more `--language' options prints detailed\n\ | |
| 1067 help about tag generation for the specified languages."); | |
| 428 | 1068 |
| 1069 print_language_names (); | |
| 1070 | |
| 1071 puts (""); | |
| 1072 puts ("Report bugs to bug-gnu-emacs@gnu.org"); | |
| 1073 | |
| 2225 | 1074 exit (EXIT_SUCCESS); |
| 428 | 1075 } |
| 1076 | |
| 1077 | |
| 1078 #ifdef VMS /* VMS specific functions */ | |
| 1079 | |
| 1080 #define EOS '\0' | |
| 1081 | |
| 1082 /* This is a BUG! ANY arbitrary limit is a BUG! | |
| 1083 Won't someone please fix this? */ | |
| 1084 #define MAX_FILE_SPEC_LEN 255 | |
| 1085 typedef struct { | |
| 1086 short curlen; | |
| 1087 char body[MAX_FILE_SPEC_LEN + 1]; | |
| 1088 } vspec; | |
| 1089 | |
| 1090 /* | |
| 1091 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names | |
| 1092 returning in each successive call the next file name matching the input | |
| 1093 spec. The function expects that each in_spec passed | |
| 1094 to it will be processed to completion; in particular, up to and | |
| 1095 including the call following that in which the last matching name | |
| 1096 is returned, the function ignores the value of in_spec, and will | |
| 1097 only start processing a new spec with the following call. | |
| 1098 If an error occurs, on return out_spec contains the value | |
| 1099 of in_spec when the error occurred. | |
| 1100 | |
| 1101 With each successive file name returned in out_spec, the | |
| 1102 function's return value is one. When there are no more matching | |
| 1103 names the function returns zero. If on the first call no file | |
| 1104 matches in_spec, or there is any other error, -1 is returned. | |
| 1105 */ | |
| 1106 | |
| 1107 #include <rmsdef.h> | |
| 1108 #include <descrip.h> | |
| 1109 #define OUTSIZE MAX_FILE_SPEC_LEN | |
| 442 | 1110 static short |
| 428 | 1111 fn_exp (out, in) |
| 1112 vspec *out; | |
| 1113 char *in; | |
| 1114 { | |
| 1115 static long context = 0; | |
| 1116 static struct dsc$descriptor_s o; | |
| 1117 static struct dsc$descriptor_s i; | |
| 1118 static bool pass1 = TRUE; | |
| 1119 long status; | |
| 1120 short retval; | |
| 1121 | |
| 1122 if (pass1) | |
| 1123 { | |
| 1124 pass1 = FALSE; | |
| 1125 o.dsc$a_pointer = (char *) out; | |
| 1126 o.dsc$w_length = (short)OUTSIZE; | |
| 1127 i.dsc$a_pointer = in; | |
| 1128 i.dsc$w_length = (short)strlen(in); | |
| 1129 i.dsc$b_dtype = DSC$K_DTYPE_T; | |
| 1130 i.dsc$b_class = DSC$K_CLASS_S; | |
| 1131 o.dsc$b_dtype = DSC$K_DTYPE_VT; | |
| 1132 o.dsc$b_class = DSC$K_CLASS_VS; | |
| 1133 } | |
| 1134 if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL) | |
| 1135 { | |
| 1136 out->body[out->curlen] = EOS; | |
| 1137 return 1; | |
| 1138 } | |
| 1139 else if (status == RMS$_NMF) | |
| 1140 retval = 0; | |
| 1141 else | |
| 1142 { | |
| 1143 strcpy(out->body, in); | |
| 1144 retval = -1; | |
| 1145 } | |
| 1146 lib$find_file_end(&context); | |
| 1147 pass1 = TRUE; | |
| 1148 return retval; | |
| 1149 } | |
| 1150 | |
| 1151 /* | |
| 1152 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the | |
| 1153 name of each file specified by the provided arg expanding wildcards. | |
| 1154 */ | |
| 442 | 1155 static char * |
| 428 | 1156 gfnames (arg, p_error) |
| 1157 char *arg; | |
| 1158 bool *p_error; | |
| 1159 { | |
| 1160 static vspec filename = {MAX_FILE_SPEC_LEN, "\0"}; | |
| 1161 | |
| 1162 switch (fn_exp (&filename, arg)) | |
| 1163 { | |
| 1164 case 1: | |
| 1165 *p_error = FALSE; | |
| 1166 return filename.body; | |
| 1167 case 0: | |
| 1168 *p_error = FALSE; | |
| 1169 return NULL; | |
| 1170 default: | |
| 1171 *p_error = TRUE; | |
| 1172 return filename.body; | |
| 1173 } | |
| 1174 } | |
| 1175 | |
| 1176 #ifndef OLD /* Newer versions of VMS do provide `system'. */ | |
| 1177 system (cmd) | |
| 1178 char *cmd; | |
| 1179 { | |
| 1180 error ("%s", "system() function not implemented under VMS"); | |
| 1181 } | |
| 1182 #endif | |
| 1183 | |
| 1184 #define VERSION_DELIM ';' | |
| 1185 char *massage_name (s) | |
| 1186 char *s; | |
| 1187 { | |
| 1188 char *start = s; | |
| 1189 | |
| 1190 for ( ; *s; s++) | |
| 1191 if (*s == VERSION_DELIM) | |
| 1192 { | |
| 1193 *s = EOS; | |
| 1194 break; | |
| 1195 } | |
| 1196 else | |
| 1197 *s = lowcase (*s); | |
| 1198 return start; | |
| 1199 } | |
| 1200 #endif /* VMS */ | |
| 1201 | |
| 1202 | |
| 1203 int | |
| 1204 main (argc, argv) | |
| 1205 int argc; | |
| 1206 char *argv[]; | |
| 1207 { | |
| 1208 int i; | |
| 1209 unsigned int nincluded_files; | |
| 1210 char **included_files; | |
| 1211 argument *argbuffer; | |
| 1212 int current_arg, file_count; | |
| 1213 linebuffer filename_lb; | |
| 2225 | 1214 bool help_asked = FALSE; |
| 428 | 1215 #ifdef VMS |
| 1216 bool got_err; | |
| 1217 #endif | |
| 2225 | 1218 char *optstring; |
| 1219 int opt; | |
| 1220 | |
| 428 | 1221 |
| 458 | 1222 #ifdef DOS_NT |
| 428 | 1223 _fmode = O_BINARY; /* all of files are treated as binary files */ |
| 458 | 1224 #endif /* DOS_NT */ |
| 428 | 1225 |
| 1226 progname = argv[0]; | |
| 1227 nincluded_files = 0; | |
| 1228 included_files = xnew (argc, char *); | |
| 1229 current_arg = 0; | |
| 1230 file_count = 0; | |
| 1231 | |
| 1232 /* Allocate enough no matter what happens. Overkill, but each one | |
| 1233 is small. */ | |
| 1234 argbuffer = xnew (argc, argument); | |
| 1235 | |
| 1236 /* | |
| 1237 * If etags, always find typedefs and structure tags. Why not? | |
| 3972 | 1238 * Also default to find macro constants, enum constants, struct |
| 1239 * members and global variables. | |
| 428 | 1240 */ |
| 1241 if (!CTAGS) | |
| 1242 { | |
| 458 | 1243 typedefs = typedefs_or_cplusplus = constantypedefs = TRUE; |
| 3972 | 1244 globals = members = TRUE; |
| 428 | 1245 } |
| 1246 | |
| 2554 | 1247 /* When the optstring begins with a '-' getopt_long does not rearrange the |
| 1248 non-options arguments to be at the end, but leaves them alone. */ | |
| 3517 | 1249 optstring = concat (NO_LONG_OPTIONS ? "" : "-", |
| 1250 "ac:Cf:Il:o:r:RSVhH", | |
| 3090 | 1251 (CTAGS) ? "BxdtTuvw" : "Di:"); |
| 1252 | |
| 1253 while ((opt = getopt_long (argc, argv, optstring, longopts, NULL)) != EOF) | |
| 2225 | 1254 switch (opt) |
| 1255 { | |
| 1256 case 0: | |
| 1257 /* If getopt returns 0, then it has already processed a | |
| 1258 long-named option. We should do nothing. */ | |
| 428 | 1259 break; |
| 1260 | |
| 2225 | 1261 case 1: |
| 1262 /* This means that a file name has been seen. Record it. */ | |
| 1263 argbuffer[current_arg].arg_type = at_filename; | |
| 1264 argbuffer[current_arg].what = optarg; | |
| 1265 ++current_arg; | |
| 1266 ++file_count; | |
| 1267 break; | |
| 1268 | |
| 1269 case STDIN: | |
| 1270 /* Parse standard input. Idea by Vivek <vivek@etla.org>. */ | |
| 1271 argbuffer[current_arg].arg_type = at_stdin; | |
| 1272 argbuffer[current_arg].what = optarg; | |
| 1273 ++current_arg; | |
| 1274 ++file_count; | |
| 1275 if (parsing_stdin) | |
| 1276 fatal ("cannot parse standard input more than once", (char *)NULL); | |
| 1277 parsing_stdin = TRUE; | |
| 1278 break; | |
| 1279 | |
| 1280 /* Common options. */ | |
| 3090 | 1281 case 'a': append_to_tagfile = TRUE; break; |
| 2225 | 1282 case 'C': cplusplus = TRUE; break; |
| 1283 case 'f': /* for compatibility with old makefiles */ | |
| 1284 case 'o': | |
| 1285 if (tagfile) | |
| 428 | 1286 { |
| 2225 | 1287 error ("-o option may only be given once.", (char *)NULL); |
| 1288 suggest_asking_for_help (); | |
| 1289 /* NOTREACHED */ | |
| 428 | 1290 } |
| 2225 | 1291 tagfile = optarg; |
| 1292 break; | |
| 1293 case 'I': | |
| 1294 case 'S': /* for backward compatibility */ | |
| 1295 ignoreindent = TRUE; | |
| 1296 break; | |
| 1297 case 'l': | |
| 1298 { | |
| 1299 language *lang = get_language_from_langname (optarg); | |
| 1300 if (lang != NULL) | |
| 1301 { | |
| 1302 argbuffer[current_arg].lang = lang; | |
| 1303 argbuffer[current_arg].arg_type = at_language; | |
| 1304 ++current_arg; | |
| 1305 } | |
| 428 | 1306 } |
| 2225 | 1307 break; |
| 1308 case 'c': | |
| 1309 /* Backward compatibility: support obsolete --ignore-case-regexp. */ | |
| 1310 optarg = concat (optarg, "i", ""); /* memory leak here */ | |
| 1311 /* FALLTHRU */ | |
| 1312 case 'r': | |
| 1313 argbuffer[current_arg].arg_type = at_regexp; | |
| 1314 argbuffer[current_arg].what = optarg; | |
| 1315 ++current_arg; | |
| 1316 break; | |
| 1317 case 'R': | |
| 1318 argbuffer[current_arg].arg_type = at_regexp; | |
| 1319 argbuffer[current_arg].what = NULL; | |
| 1320 ++current_arg; | |
| 1321 break; | |
| 1322 case 'V': | |
| 1323 print_version (); | |
| 1324 break; | |
| 1325 case 'h': | |
| 1326 case 'H': | |
| 1327 help_asked = TRUE; | |
| 1328 break; | |
| 1329 | |
| 1330 /* Etags options */ | |
| 1331 case 'D': constantypedefs = FALSE; break; | |
| 1332 case 'i': included_files[nincluded_files++] = optarg; break; | |
| 1333 | |
| 1334 /* Ctags options. */ | |
| 1335 case 'B': searchar = '?'; break; | |
| 1336 case 'd': constantypedefs = TRUE; break; | |
| 1337 case 't': typedefs = TRUE; break; | |
| 1338 case 'T': typedefs = typedefs_or_cplusplus = TRUE; break; | |
| 1339 case 'u': update = TRUE; break; | |
| 1340 case 'v': vgrind_style = TRUE; /*FALLTHRU*/ | |
| 1341 case 'x': cxref_style = TRUE; break; | |
| 1342 case 'w': no_warnings = TRUE; break; | |
| 1343 default: | |
| 1344 suggest_asking_for_help (); | |
| 1345 /* NOTREACHED */ | |
| 1346 } | |
| 1347 | |
| 2554 | 1348 /* No more options. Store the rest of arguments. */ |
| 2225 | 1349 for (; optind < argc; optind++) |
| 428 | 1350 { |
| 1351 argbuffer[current_arg].arg_type = at_filename; | |
| 1352 argbuffer[current_arg].what = argv[optind]; | |
| 1353 ++current_arg; | |
| 1354 ++file_count; | |
| 1355 } | |
| 1356 | |
| 2225 | 1357 argbuffer[current_arg].arg_type = at_end; |
| 1358 | |
| 1359 if (help_asked) | |
| 1360 print_help (argbuffer); | |
| 1361 /* NOTREACHED */ | |
| 1362 | |
| 428 | 1363 if (nincluded_files == 0 && file_count == 0) |
| 1364 { | |
| 1365 error ("no input files specified.", (char *)NULL); | |
| 1366 suggest_asking_for_help (); | |
| 2225 | 1367 /* NOTREACHED */ |
| 428 | 1368 } |
| 1369 | |
| 1370 if (tagfile == NULL) | |
| 1371 tagfile = CTAGS ? "tags" : "TAGS"; | |
| 1372 cwd = etags_getcwd (); /* the current working directory */ | |
| 1373 if (cwd[strlen (cwd) - 1] != '/') | |
| 1374 { | |
| 1375 char *oldcwd = cwd; | |
| 1376 cwd = concat (oldcwd, "/", ""); | |
| 1377 free (oldcwd); | |
| 1378 } | |
| 2325 | 1379 /* Relative file names are made relative to the current directory. */ |
| 1380 if (streq (tagfile, "-") | |
| 1381 || strneq (tagfile, "/dev/", 5)) | |
| 428 | 1382 tagfiledir = cwd; |
| 1383 else | |
| 1384 tagfiledir = absolute_dirname (tagfile, cwd); | |
| 1385 | |
| 1386 init (); /* set up boolean "functions" */ | |
| 1387 | |
| 2225 | 1388 linebuffer_init (&lb); |
| 1389 linebuffer_init (&filename_lb); | |
| 1390 linebuffer_init (&filebuf); | |
| 1391 linebuffer_init (&token_name); | |
| 428 | 1392 |
| 1393 if (!CTAGS) | |
| 1394 { | |
| 1395 if (streq (tagfile, "-")) | |
| 1396 { | |
| 1397 tagf = stdout; | |
| 458 | 1398 #ifdef DOS_NT |
| 428 | 1399 /* Switch redirected `stdout' to binary mode (setting `_fmode' |
| 1400 doesn't take effect until after `stdout' is already open). */ | |
| 1401 if (!isatty (fileno (stdout))) | |
| 1402 setmode (fileno (stdout), O_BINARY); | |
| 458 | 1403 #endif /* DOS_NT */ |
| 428 | 1404 } |
| 1405 else | |
| 1406 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w"); | |
| 1407 if (tagf == NULL) | |
| 1408 pfatal (tagfile); | |
| 1409 } | |
| 1410 | |
| 1411 /* | |
| 1412 * Loop through files finding functions. | |
| 1413 */ | |
| 2225 | 1414 for (i = 0; i < current_arg; i++) |
| 428 | 1415 { |
| 2225 | 1416 static language *lang; /* non-NULL if language is forced */ |
| 1417 char *this_file; | |
| 1418 | |
| 428 | 1419 switch (argbuffer[i].arg_type) |
| 1420 { | |
| 1421 case at_language: | |
| 2225 | 1422 lang = argbuffer[i].lang; |
| 428 | 1423 break; |
| 1424 case at_regexp: | |
| 2225 | 1425 analyse_regex (argbuffer[i].what); |
| 428 | 1426 break; |
| 1427 case at_filename: | |
| 1428 #ifdef VMS | |
| 1429 while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL) | |
| 1430 { | |
| 1431 if (got_err) | |
| 1432 { | |
| 1433 error ("can't find file %s\n", this_file); | |
| 1434 argc--, argv++; | |
| 1435 } | |
| 1436 else | |
| 1437 { | |
| 1438 this_file = massage_name (this_file); | |
| 1439 } | |
| 1440 #else | |
| 1441 this_file = argbuffer[i].what; | |
| 1442 #endif | |
| 1443 /* Input file named "-" means read file names from stdin | |
| 1444 (one per line) and use them. */ | |
| 1445 if (streq (this_file, "-")) | |
| 2225 | 1446 { |
| 1447 if (parsing_stdin) | |
| 1448 fatal ("cannot parse standard input AND read file names from it", | |
| 1449 (char *)NULL); | |
| 1450 while (readline_internal (&filename_lb, stdin) > 0) | |
| 1451 process_file_name (filename_lb.buffer, lang); | |
| 1452 } | |
| 428 | 1453 else |
| 2225 | 1454 process_file_name (this_file, lang); |
| 428 | 1455 #ifdef VMS |
| 1456 } | |
| 1457 #endif | |
| 1458 break; | |
| 2225 | 1459 case at_stdin: |
| 1460 this_file = argbuffer[i].what; | |
| 1461 process_file (stdin, this_file, lang); | |
| 1462 break; | |
| 428 | 1463 } |
| 1464 } | |
| 1465 | |
| 2225 | 1466 free_regexps (); |
| 1467 free (lb.buffer); | |
| 1468 free (filebuf.buffer); | |
| 1469 free (token_name.buffer); | |
| 1470 | |
| 1471 if (!CTAGS || cxref_style) | |
| 428 | 1472 { |
| 3090 | 1473 /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */ |
| 1474 put_entries (nodehead); | |
| 2225 | 1475 free_tree (nodehead); |
| 1476 nodehead = NULL; | |
| 1477 if (!CTAGS) | |
| 1478 { | |
| 1479 fdesc *fdp; | |
| 1480 | |
| 1481 /* Output file entries that have no tags. */ | |
| 1482 for (fdp = fdhead; fdp != NULL; fdp = fdp->next) | |
| 1483 if (!fdp->written) | |
| 1484 fprintf (tagf, "\f\n%s,0\n", fdp->taggedfname); | |
| 1485 | |
| 1486 while (nincluded_files-- > 0) | |
| 1487 fprintf (tagf, "\f\n%s,include\n", *included_files++); | |
| 3090 | 1488 |
| 1489 if (fclose (tagf) == EOF) | |
| 1490 pfatal (tagfile); | |
| 2225 | 1491 } |
| 1492 | |
| 1493 exit (EXIT_SUCCESS); | |
| 428 | 1494 } |
| 1495 | |
| 1496 if (update) | |
| 1497 { | |
| 1498 char cmd[BUFSIZ]; | |
| 1499 for (i = 0; i < current_arg; ++i) | |
| 1500 { | |
| 2225 | 1501 switch (argbuffer[i].arg_type) |
| 1502 { | |
| 1503 case at_filename: | |
| 1504 case at_stdin: | |
| 1505 break; | |
| 1506 default: | |
| 1507 continue; /* the for loop */ | |
| 1508 } | |
| 428 | 1509 sprintf (cmd, |
| 1510 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS", | |
| 1511 tagfile, argbuffer[i].what, tagfile); | |
| 2225 | 1512 if (system (cmd) != EXIT_SUCCESS) |
| 428 | 1513 fatal ("failed to execute shell command", (char *)NULL); |
| 1514 } | |
| 1515 append_to_tagfile = TRUE; | |
| 1516 } | |
| 1517 | |
| 1518 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w"); | |
| 1519 if (tagf == NULL) | |
| 1520 pfatal (tagfile); | |
| 2225 | 1521 put_entries (nodehead); /* write all the tags (CTAGS) */ |
| 1522 free_tree (nodehead); | |
| 1523 nodehead = NULL; | |
| 1524 if (fclose (tagf) == EOF) | |
| 1525 pfatal (tagfile); | |
| 428 | 1526 |
| 3090 | 1527 if (CTAGS) |
| 1528 if (append_to_tagfile || update) | |
| 1529 { | |
| 3876 | 1530 char cmd[2*BUFSIZ+20]; |
| 1531 /* Maybe these should be used: | |
| 1532 setenv ("LC_COLLATE", "C", 1); | |
| 1533 setenv ("LC_ALL", "C", 1); */ | |
| 1534 sprintf (cmd, "sort -u -o %.*s %.*s", BUFSIZ, tagfile, BUFSIZ, tagfile); | |
| 3090 | 1535 exit (system (cmd)); |
| 1536 } | |
| 2225 | 1537 return EXIT_SUCCESS; |
| 428 | 1538 } |
| 1539 | |
| 1540 | |
| 1541 /* | |
| 1542 * Return a compressor given the file name. If EXTPTR is non-zero, | |
| 1543 * return a pointer into FILE where the compressor-specific | |
| 1544 * extension begins. If no compressor is found, NULL is returned | |
| 1545 * and EXTPTR is not significant. | |
| 458 | 1546 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998) |
| 428 | 1547 */ |
| 442 | 1548 static compressor * |
| 428 | 1549 get_compressor_from_suffix (file, extptr) |
| 1550 char *file; | |
| 1551 char **extptr; | |
| 1552 { | |
| 1553 compressor *compr; | |
| 1554 char *slash, *suffix; | |
| 1555 | |
| 1556 /* This relies on FN to be after canonicalize_filename, | |
| 458 | 1557 so we don't need to consider backslashes on DOS_NT. */ |
| 428 | 1558 slash = etags_strrchr (file, '/'); |
| 1559 suffix = etags_strrchr (file, '.'); | |
| 1560 if (suffix == NULL || suffix < slash) | |
| 1561 return NULL; | |
| 1562 if (extptr != NULL) | |
| 1563 *extptr = suffix; | |
| 1564 suffix += 1; | |
| 1565 /* Let those poor souls who live with DOS 8+3 file name limits get | |
| 1566 some solace by treating foo.cgz as if it were foo.c.gz, etc. | |
| 458 | 1567 Only the first do loop is run if not MSDOS */ |
| 428 | 1568 do |
| 1569 { | |
| 1570 for (compr = compressors; compr->suffix != NULL; compr++) | |
| 1571 if (streq (compr->suffix, suffix)) | |
| 1572 return compr; | |
| 458 | 1573 if (!MSDOS) |
| 442 | 1574 break; /* do it only once: not really a loop */ |
| 428 | 1575 if (extptr != NULL) |
| 1576 *extptr = ++suffix; | |
| 1577 } while (*suffix != '\0'); | |
| 1578 return NULL; | |
| 1579 } | |
| 1580 | |
| 1581 | |
| 1582 | |
| 1583 /* | |
| 1584 * Return a language given the name. | |
| 1585 */ | |
| 442 | 1586 static language * |
| 458 | 1587 get_language_from_langname (name) |
| 709 | 1588 const char *name; |
| 428 | 1589 { |
| 1590 language *lang; | |
| 1591 | |
| 1592 if (name == NULL) | |
| 1593 error ("empty language name", (char *)NULL); | |
| 1594 else | |
| 1595 { | |
| 1596 for (lang = lang_names; lang->name != NULL; lang++) | |
| 1597 if (streq (name, lang->name)) | |
| 1598 return lang; | |
| 1599 error ("unknown language \"%s\"", name); | |
| 1600 } | |
| 1601 | |
| 1602 return NULL; | |
| 1603 } | |
| 1604 | |
| 1605 | |
| 1606 /* | |
| 1607 * Return a language given the interpreter name. | |
| 1608 */ | |
| 442 | 1609 static language * |
| 428 | 1610 get_language_from_interpreter (interpreter) |
| 1611 char *interpreter; | |
| 1612 { | |
| 1613 language *lang; | |
| 1614 char **iname; | |
| 1615 | |
| 1616 if (interpreter == NULL) | |
| 1617 return NULL; | |
| 1618 for (lang = lang_names; lang->name != NULL; lang++) | |
| 1619 if (lang->interpreters != NULL) | |
| 1620 for (iname = lang->interpreters; *iname != NULL; iname++) | |
| 1621 if (streq (*iname, interpreter)) | |
| 1622 return lang; | |
| 1623 | |
| 1624 return NULL; | |
| 1625 } | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 /* | |
| 1630 * Return a language given the file name. | |
| 1631 */ | |
| 442 | 1632 static language * |
| 2225 | 1633 get_language_from_filename (file, case_sensitive) |
| 428 | 1634 char *file; |
| 2225 | 1635 bool case_sensitive; |
| 428 | 1636 { |
| 1637 language *lang; | |
| 458 | 1638 char **name, **ext, *suffix; |
| 1639 | |
| 1640 /* Try whole file name first. */ | |
| 1641 for (lang = lang_names; lang->name != NULL; lang++) | |
| 1642 if (lang->filenames != NULL) | |
| 1643 for (name = lang->filenames; *name != NULL; name++) | |
| 2225 | 1644 if ((case_sensitive) |
| 1645 ? streq (*name, file) | |
| 1646 : strcaseeq (*name, file)) | |
| 458 | 1647 return lang; |
| 1648 | |
| 1649 /* If not found, try suffix after last dot. */ | |
| 428 | 1650 suffix = etags_strrchr (file, '.'); |
| 1651 if (suffix == NULL) | |
| 1652 return NULL; | |
| 1653 suffix += 1; | |
| 1654 for (lang = lang_names; lang->name != NULL; lang++) | |
| 1655 if (lang->suffixes != NULL) | |
| 1656 for (ext = lang->suffixes; *ext != NULL; ext++) | |
| 2225 | 1657 if ((case_sensitive) |
| 1658 ? streq (*ext, suffix) | |
| 1659 : strcaseeq (*ext, suffix)) | |
| 428 | 1660 return lang; |
| 1661 return NULL; | |
| 1662 } | |
| 1663 | |
| 2225 | 1664 |
| 428 | 1665 /* |
| 1666 * This routine is called on each file argument. | |
| 1667 */ | |
| 442 | 1668 static void |
| 2225 | 1669 process_file_name (file, lang) |
| 428 | 1670 char *file; |
| 2225 | 1671 language *lang; |
| 428 | 1672 { |
| 1673 struct stat stat_buf; | |
| 1674 FILE *inf; | |
| 2225 | 1675 fdesc *fdp; |
| 428 | 1676 compressor *compr; |
| 1677 char *compressed_name, *uncompressed_name; | |
| 1678 char *ext, *real_name; | |
| 2225 | 1679 int retval; |
| 428 | 1680 |
| 1681 canonicalize_filename (file); | |
| 1682 if (streq (file, tagfile) && !streq (tagfile, "-")) | |
| 1683 { | |
| 1684 error ("skipping inclusion of %s in self.", file); | |
| 1685 return; | |
| 1686 } | |
| 1687 if ((compr = get_compressor_from_suffix (file, &ext)) == NULL) | |
| 1688 { | |
| 1689 compressed_name = NULL; | |
| 1690 real_name = uncompressed_name = savestr (file); | |
| 1691 } | |
| 1692 else | |
| 1693 { | |
| 1694 real_name = compressed_name = savestr (file); | |
| 1695 uncompressed_name = savenstr (file, ext - file); | |
| 1696 } | |
| 1697 | |
| 2225 | 1698 /* If the canonicalized uncompressed name |
| 1699 has already been dealt with, skip it silently. */ | |
| 1700 for (fdp = fdhead; fdp != NULL; fdp = fdp->next) | |
| 428 | 1701 { |
| 2225 | 1702 assert (fdp->infname != NULL); |
| 1703 if (streq (uncompressed_name, fdp->infname)) | |
| 1704 goto cleanup; | |
| 1705 } | |
| 428 | 1706 |
| 1707 if (stat (real_name, &stat_buf) != 0) | |
| 1708 { | |
| 1709 /* Reset real_name and try with a different name. */ | |
| 1710 real_name = NULL; | |
| 1711 if (compressed_name != NULL) /* try with the given suffix */ | |
| 1712 { | |
| 1713 if (stat (uncompressed_name, &stat_buf) == 0) | |
| 1714 real_name = uncompressed_name; | |
| 1715 } | |
| 1716 else /* try all possible suffixes */ | |
| 1717 { | |
| 1718 for (compr = compressors; compr->suffix != NULL; compr++) | |
| 1719 { | |
| 1720 compressed_name = concat (file, ".", compr->suffix); | |
| 1721 if (stat (compressed_name, &stat_buf) != 0) | |
| 1722 { | |
| 458 | 1723 if (MSDOS) |
| 1724 { | |
| 1725 char *suf = compressed_name + strlen (file); | |
| 1726 size_t suflen = strlen (compr->suffix) + 1; | |
| 1727 for ( ; suf[1]; suf++, suflen--) | |
| 1728 { | |
| 1729 memmove (suf, suf + 1, suflen); | |
| 1730 if (stat (compressed_name, &stat_buf) == 0) | |
| 1731 { | |
| 1732 real_name = compressed_name; | |
| 1733 break; | |
| 1734 } | |
| 1735 } | |
| 1736 if (real_name != NULL) | |
| 1737 break; | |
| 1738 } /* MSDOS */ | |
| 428 | 1739 free (compressed_name); |
| 1740 compressed_name = NULL; | |
| 1741 } | |
| 1742 else | |
| 1743 { | |
| 1744 real_name = compressed_name; | |
| 1745 break; | |
| 1746 } | |
| 1747 } | |
| 1748 } | |
| 1749 if (real_name == NULL) | |
| 1750 { | |
| 1751 perror (file); | |
| 2225 | 1752 goto cleanup; |
| 428 | 1753 } |
| 1754 } /* try with a different name */ | |
| 1755 | |
| 1756 if (!S_ISREG (stat_buf.st_mode)) | |
| 1757 { | |
| 1758 error ("skipping %s: it is not a regular file.", real_name); | |
| 2225 | 1759 goto cleanup; |
| 428 | 1760 } |
| 1761 if (real_name == compressed_name) | |
| 1762 { | |
| 1763 char *cmd = concat (compr->command, " ", real_name); | |
| 458 | 1764 inf = (FILE *) popen (cmd, "r"); |
| 428 | 1765 free (cmd); |
| 1766 } | |
| 1767 else | |
| 1768 inf = fopen (real_name, "r"); | |
| 1769 if (inf == NULL) | |
| 1770 { | |
| 1771 perror (real_name); | |
| 2225 | 1772 goto cleanup; |
| 428 | 1773 } |
| 1774 | |
| 2225 | 1775 process_file (inf, uncompressed_name, lang); |
| 428 | 1776 |
| 1777 if (real_name == compressed_name) | |
| 2225 | 1778 retval = pclose (inf); |
| 428 | 1779 else |
| 2225 | 1780 retval = fclose (inf); |
| 1781 if (retval < 0) | |
| 1782 pfatal (file); | |
| 1783 | |
| 1784 cleanup: | |
| 1785 if (compressed_name) free (compressed_name); | |
| 1786 if (uncompressed_name) free (uncompressed_name); | |
| 1787 last_node = NULL; | |
| 1788 curfdp = NULL; | |
| 1789 return; | |
| 1790 } | |
| 1791 | |
| 1792 static void | |
| 1793 process_file (fh, fn, lang) | |
| 1794 FILE *fh; | |
| 1795 char *fn; | |
| 1796 language *lang; | |
| 1797 { | |
| 1798 static const fdesc emptyfdesc; | |
| 1799 fdesc *fdp; | |
| 1800 | |
| 1801 /* Create a new input file description entry. */ | |
| 1802 fdp = xnew (1, fdesc); | |
| 1803 *fdp = emptyfdesc; | |
| 1804 fdp->next = fdhead; | |
| 1805 fdp->infname = savestr (fn); | |
| 1806 fdp->lang = lang; | |
| 1807 fdp->infabsname = absolute_filename (fn, cwd); | |
| 1808 fdp->infabsdir = absolute_dirname (fn, cwd); | |
| 1809 if (filename_is_absolute (fn)) | |
| 428 | 1810 { |
| 2225 | 1811 /* An absolute file name. Canonicalize it. */ |
| 1812 fdp->taggedfname = absolute_filename (fn, NULL); | |
| 1813 } | |
| 1814 else | |
| 1815 { | |
| 1816 /* A file name relative to cwd. Make it relative | |
| 1817 to the directory of the tags file. */ | |
| 1818 fdp->taggedfname = relative_filename (fn, tagfiledir); | |
| 1819 } | |
| 1820 fdp->usecharno = TRUE; /* use char position when making tags */ | |
| 1821 fdp->prop = NULL; | |
| 1822 fdp->written = FALSE; /* not written on tags file yet */ | |
| 1823 | |
| 1824 fdhead = fdp; | |
| 1825 curfdp = fdhead; /* the current file description */ | |
| 1826 | |
| 1827 find_entries (fh); | |
| 1828 | |
| 1829 /* If not Ctags, and if this is not metasource and if it contained no #line | |
| 1830 directives, we can write the tags and free all nodes pointing to | |
| 1831 curfdp. */ | |
| 1832 if (!CTAGS | |
| 1833 && curfdp->usecharno /* no #line directives in this file */ | |
| 1834 && !curfdp->lang->metasource) | |
| 1835 { | |
| 1836 node *np, *prev; | |
| 1837 | |
| 1838 /* Look for the head of the sublist relative to this file. See add_node | |
| 1839 for the structure of the node tree. */ | |
| 1840 prev = NULL; | |
| 1841 for (np = nodehead; np != NULL; prev = np, np = np->left) | |
| 1842 if (np->fdp == curfdp) | |
| 1843 break; | |
| 1844 | |
| 1845 /* If we generated tags for this file, write and delete them. */ | |
| 1846 if (np != NULL) | |
| 428 | 1847 { |
| 2225 | 1848 /* This is the head of the last sublist, if any. The following |
| 1849 instructions depend on this being true. */ | |
| 1850 assert (np->left == NULL); | |
| 1851 | |
| 1852 assert (fdhead == curfdp); | |
| 1853 assert (last_node->fdp == curfdp); | |
| 1854 put_entries (np); /* write tags for file curfdp->taggedfname */ | |
| 1855 free_tree (np); /* remove the written nodes */ | |
| 1856 if (prev == NULL) | |
| 1857 nodehead = NULL; /* no nodes left */ | |
| 1858 else | |
| 1859 prev->left = NULL; /* delete the pointer to the sublist */ | |
| 428 | 1860 } |
| 1861 } | |
| 1862 } | |
| 1863 | |
| 1864 /* | |
| 1865 * This routine sets up the boolean pseudo-functions which work | |
| 1866 * by setting boolean flags dependent upon the corresponding character. | |
| 1867 * Every char which is NOT in that string is not a white char. Therefore, | |
| 1868 * all of the array "_wht" is set to FALSE, and then the elements | |
| 1869 * subscripted by the chars in "white" are set to TRUE. Thus "_wht" | |
| 1870 * of a char is TRUE if it is the string "white", else FALSE. | |
| 1871 */ | |
| 442 | 1872 static void |
| 428 | 1873 init () |
| 1874 { | |
| 1875 register char *sp; | |
| 1876 register int i; | |
| 1877 | |
| 1878 for (i = 0; i < CHARS; i++) | |
| 1879 iswhite(i) = notinname(i) = begtoken(i) = intoken(i) = endtoken(i) = FALSE; | |
| 1880 for (sp = white; *sp != '\0'; sp++) iswhite (*sp) = TRUE; | |
| 1881 for (sp = nonam; *sp != '\0'; sp++) notinname (*sp) = TRUE; | |
| 442 | 1882 notinname('\0') = notinname('\n'); |
| 428 | 1883 for (sp = begtk; *sp != '\0'; sp++) begtoken (*sp) = TRUE; |
| 442 | 1884 begtoken('\0') = begtoken('\n'); |
| 428 | 1885 for (sp = midtk; *sp != '\0'; sp++) intoken (*sp) = TRUE; |
| 442 | 1886 intoken('\0') = intoken('\n'); |
| 428 | 1887 for (sp = endtk; *sp != '\0'; sp++) endtoken (*sp) = TRUE; |
| 1888 endtoken('\0') = endtoken('\n'); | |
| 1889 } | |
| 1890 | |
| 1891 /* | |
| 1892 * This routine opens the specified file and calls the function | |
| 1893 * which finds the function and type definitions. | |
| 1894 */ | |
| 442 | 1895 static void |
| 2225 | 1896 find_entries (inf) |
| 428 | 1897 FILE *inf; |
| 1898 { | |
| 1899 char *cp; | |
| 2225 | 1900 language *lang = curfdp->lang; |
| 1901 Lang_function *parser = NULL; | |
| 428 | 1902 |
| 1903 /* If user specified a language, use it. */ | |
| 1904 if (lang != NULL && lang->function != NULL) | |
| 1905 { | |
| 2225 | 1906 parser = lang->function; |
| 428 | 1907 } |
| 1908 | |
| 2225 | 1909 /* Else try to guess the language given the file name. */ |
| 1910 if (parser == NULL) | |
| 428 | 1911 { |
| 2225 | 1912 lang = get_language_from_filename (curfdp->infname, TRUE); |
| 1913 if (lang != NULL && lang->function != NULL) | |
| 1914 { | |
| 1915 curfdp->lang = lang; | |
| 1916 parser = lang->function; | |
| 1917 } | |
| 428 | 1918 } |
| 1919 | |
| 2225 | 1920 /* Else look for sharp-bang as the first two characters. */ |
| 1921 if (parser == NULL | |
| 1922 && readline_internal (&lb, inf) > 0 | |
| 428 | 1923 && lb.len >= 2 |
| 1924 && lb.buffer[0] == '#' | |
| 1925 && lb.buffer[1] == '!') | |
| 1926 { | |
| 1927 char *lp; | |
| 1928 | |
| 1929 /* Set lp to point at the first char after the last slash in the | |
| 1930 line or, if no slashes, at the first nonblank. Then set cp to | |
| 1931 the first successive blank and terminate the string. */ | |
| 1932 lp = etags_strrchr (lb.buffer+2, '/'); | |
| 1933 if (lp != NULL) | |
| 1934 lp += 1; | |
| 1935 else | |
| 1936 lp = skip_spaces (lb.buffer + 2); | |
| 1937 cp = skip_non_spaces (lp); | |
| 1938 *cp = '\0'; | |
| 1939 | |
| 1940 if (strlen (lp) > 0) | |
| 1941 { | |
| 1942 lang = get_language_from_interpreter (lp); | |
| 1943 if (lang != NULL && lang->function != NULL) | |
| 1944 { | |
| 2225 | 1945 curfdp->lang = lang; |
| 1946 parser = lang->function; | |
| 428 | 1947 } |
| 1948 } | |
| 1949 } | |
| 2225 | 1950 |
| 428 | 1951 /* We rewind here, even if inf may be a pipe. We fail if the |
| 1952 length of the first line is longer than the pipe block size, | |
| 1953 which is unlikely. */ | |
| 1954 rewind (inf); | |
| 1955 | |
| 2225 | 1956 /* Else try to guess the language given the case insensitive file name. */ |
| 1957 if (parser == NULL) | |
| 1958 { | |
| 1959 lang = get_language_from_filename (curfdp->infname, FALSE); | |
| 1960 if (lang != NULL && lang->function != NULL) | |
| 1961 { | |
| 1962 curfdp->lang = lang; | |
| 1963 parser = lang->function; | |
| 1964 } | |
| 1965 } | |
| 1966 | |
| 1967 /* Else try Fortran or C. */ | |
| 1968 if (parser == NULL) | |
| 428 | 1969 { |
| 2225 | 1970 node *old_last_node = last_node; |
| 1971 | |
| 1972 curfdp->lang = get_language_from_langname ("fortran"); | |
| 1973 find_entries (inf); | |
| 1974 | |
| 1975 if (old_last_node == last_node) | |
| 1976 /* No Fortran entries found. Try C. */ | |
| 1977 { | |
| 1978 /* We do not tag if rewind fails. | |
| 1979 Only the file name will be recorded in the tags file. */ | |
| 1980 rewind (inf); | |
| 1981 curfdp->lang = get_language_from_langname (cplusplus ? "c++" : "c"); | |
| 1982 find_entries (inf); | |
| 1983 } | |
| 1984 return; | |
| 428 | 1985 } |
| 2225 | 1986 |
| 1987 if (!no_line_directive | |
| 1988 && curfdp->lang != NULL && curfdp->lang->metasource) | |
| 1989 /* It may be that this is a bingo.y file, and we already parsed a bingo.c | |
| 1990 file, or anyway we parsed a file that is automatically generated from | |
| 1991 this one. If this is the case, the bingo.c file contained #line | |
| 1992 directives that generated tags pointing to this file. Let's delete | |
| 1993 them all before parsing this file, which is the real source. */ | |
| 1994 { | |
| 1995 fdesc **fdpp = &fdhead; | |
| 1996 while (*fdpp != NULL) | |
| 1997 if (*fdpp != curfdp | |
| 1998 && streq ((*fdpp)->taggedfname, curfdp->taggedfname)) | |
| 1999 /* We found one of those! We must delete both the file description | |
| 2000 and all tags referring to it. */ | |
| 2001 { | |
| 2002 fdesc *badfdp = *fdpp; | |
| 2003 | |
| 2004 /* Delete the tags referring to badfdp->taggedfname | |
| 2005 that were obtained from badfdp->infname. */ | |
| 2006 invalidate_nodes (badfdp, &nodehead); | |
| 2007 | |
| 2008 *fdpp = badfdp->next; /* remove the bad description from the list */ | |
| 2009 free_fdesc (badfdp); | |
| 2010 } | |
| 2011 else | |
| 2012 fdpp = &(*fdpp)->next; /* advance the list pointer */ | |
| 2013 } | |
| 2014 | |
| 2015 assert (parser != NULL); | |
| 2016 | |
| 2017 /* Generic initialisations before reading from file. */ | |
| 2018 linebuffer_setlen (&filebuf, 0); /* reset the file buffer */ | |
| 2019 | |
| 2020 /* Generic initialisations before parsing file with readline. */ | |
| 2021 lineno = 0; /* reset global line number */ | |
| 2022 charno = 0; /* reset global char number */ | |
| 2023 linecharno = 0; /* reset global char number of line start */ | |
| 2024 | |
| 2025 parser (inf); | |
| 2026 | |
| 2027 regex_tag_multiline (); | |
| 428 | 2028 } |
| 458 | 2029 |
| 428 | 2030 |
| 2225 | 2031 /* |
| 2032 * Check whether an implicitly named tag should be created, | |
| 2033 * then call `pfnote'. | |
| 2034 * NAME is a string that is internally copied by this function. | |
| 2035 * | |
| 2036 * TAGS format specification | |
| 2037 * Idea by Sam Kendall <kendall@mv.mv.com> (1997) | |
| 2038 * The following is explained in some more detail in etc/ETAGS.EBNF. | |
| 2039 * | |
| 2040 * make_tag creates tags with "implicit tag names" (unnamed tags) | |
| 2041 * if the following are all true, assuming NONAM=" \f\t\n\r()=,;": | |
| 2042 * 1. NAME does not contain any of the characters in NONAM; | |
| 2043 * 2. LINESTART contains name as either a rightmost, or rightmost but | |
| 2044 * one character, substring; | |
| 2045 * 3. the character, if any, immediately before NAME in LINESTART must | |
| 2046 * be a character in NONAM; | |
| 2047 * 4. the character, if any, immediately after NAME in LINESTART must | |
| 2048 * also be a character in NONAM. | |
| 2049 * | |
| 2050 * The implementation uses the notinname() macro, which recognises the | |
| 2051 * characters stored in the string `nonam'. | |
| 2052 * etags.el needs to use the same characters that are in NONAM. | |
| 2053 */ | |
| 2054 static void | |
| 2055 make_tag (name, namelen, is_func, linestart, linelen, lno, cno) | |
| 2056 char *name; /* tag name, or NULL if unnamed */ | |
| 2057 int namelen; /* tag length */ | |
| 2058 bool is_func; /* tag is a function */ | |
| 2059 char *linestart; /* start of the line where tag is */ | |
| 2060 int linelen; /* length of the line where tag is */ | |
| 2061 int lno; /* line number */ | |
| 2062 long cno; /* character number */ | |
| 2063 { | |
| 2064 bool named = (name != NULL && namelen > 0); | |
| 2065 | |
| 2066 if (!CTAGS && named) /* maybe set named to false */ | |
| 2067 /* Let's try to make an implicit tag name, that is, create an unnamed tag | |
| 2068 such that etags.el can guess a name from it. */ | |
| 2069 { | |
| 2070 int i; | |
| 2071 register char *cp = name; | |
| 2072 | |
| 2073 for (i = 0; i < namelen; i++) | |
| 2074 if (notinname (*cp++)) | |
| 2075 break; | |
| 2076 if (i == namelen) /* rule #1 */ | |
| 2077 { | |
| 2078 cp = linestart + linelen - namelen; | |
| 2079 if (notinname (linestart[linelen-1])) | |
| 2080 cp -= 1; /* rule #4 */ | |
| 2081 if (cp >= linestart /* rule #2 */ | |
| 2082 && (cp == linestart | |
| 2083 || notinname (cp[-1])) /* rule #3 */ | |
| 2084 && strneq (name, cp, namelen)) /* rule #2 */ | |
| 2085 named = FALSE; /* use implicit tag name */ | |
| 2086 } | |
| 2087 } | |
| 2088 | |
| 2089 if (named) | |
| 2090 name = savenstr (name, namelen); | |
| 2091 else | |
| 2092 name = NULL; | |
| 2093 pfnote (name, is_func, linestart, linelen, lno, cno); | |
| 2094 } | |
| 2095 | |
| 428 | 2096 /* Record a tag. */ |
| 442 | 2097 static void |
| 428 | 2098 pfnote (name, is_func, linestart, linelen, lno, cno) |
| 2099 char *name; /* tag name, or NULL if unnamed */ | |
| 2100 bool is_func; /* tag is a function */ | |
| 2101 char *linestart; /* start of the line where tag is */ | |
| 2102 int linelen; /* length of the line where tag is */ | |
| 2103 int lno; /* line number */ | |
| 2104 long cno; /* character number */ | |
| 2105 { | |
| 2106 register node *np; | |
| 2107 | |
| 2225 | 2108 assert (name == NULL || name[0] != '\0'); |
| 428 | 2109 if (CTAGS && name == NULL) |
| 2110 return; | |
| 2111 | |
| 2112 np = xnew (1, node); | |
| 2113 | |
| 2114 /* If ctags mode, change name "main" to M<thisfilename>. */ | |
| 2115 if (CTAGS && !cxref_style && streq (name, "main")) | |
| 2116 { | |
| 2225 | 2117 register char *fp = etags_strrchr (curfdp->taggedfname, '/'); |
| 2118 np->name = concat ("M", fp == NULL ? curfdp->taggedfname : fp + 1, ""); | |
| 428 | 2119 fp = etags_strrchr (np->name, '.'); |
| 2120 if (fp != NULL && fp[1] != '\0' && fp[2] == '\0') | |
| 2121 fp[0] = '\0'; | |
| 2122 } | |
| 2123 else | |
| 2124 np->name = name; | |
| 2225 | 2125 np->valid = TRUE; |
| 428 | 2126 np->been_warned = FALSE; |
| 2225 | 2127 np->fdp = curfdp; |
| 428 | 2128 np->is_func = is_func; |
| 2129 np->lno = lno; | |
| 2225 | 2130 if (np->fdp->usecharno) |
| 2131 /* Our char numbers are 0-base, because of C language tradition? | |
| 2132 ctags compatibility? old versions compatibility? I don't know. | |
| 2133 Anyway, since emacs's are 1-base we expect etags.el to take care | |
| 2134 of the difference. If we wanted to have 1-based numbers, we would | |
| 2135 uncomment the +1 below. */ | |
| 2136 np->cno = cno /* + 1 */ ; | |
| 2137 else | |
| 2138 np->cno = invalidcharno; | |
| 428 | 2139 np->left = np->right = NULL; |
| 2140 if (CTAGS && !cxref_style) | |
| 2141 { | |
| 2142 if (strlen (linestart) < 50) | |
| 2225 | 2143 np->regex = concat (linestart, "$", ""); |
| 428 | 2144 else |
| 2225 | 2145 np->regex = savenstr (linestart, 50); |
| 428 | 2146 } |
| 2147 else | |
| 2225 | 2148 np->regex = savenstr (linestart, linelen); |
| 2149 | |
| 2150 add_node (np, &nodehead); | |
| 428 | 2151 } |
| 2152 | |
| 2153 /* | |
| 2154 * free_tree () | |
| 2155 * recurse on left children, iterate on right children. | |
| 2156 */ | |
| 442 | 2157 static void |
| 428 | 2158 free_tree (np) |
| 2159 register node *np; | |
| 2160 { | |
| 2161 while (np) | |
| 2162 { | |
| 2163 register node *node_right = np->right; | |
| 2164 free_tree (np->left); | |
| 2165 if (np->name != NULL) | |
| 2166 free (np->name); | |
| 2225 | 2167 free (np->regex); |
| 428 | 2168 free (np); |
| 2169 np = node_right; | |
| 2170 } | |
| 2171 } | |
| 2172 | |
| 2173 /* | |
| 2225 | 2174 * free_fdesc () |
| 2175 * delete a file description | |
| 2176 */ | |
| 2177 static void | |
| 2178 free_fdesc (fdp) | |
| 2179 register fdesc *fdp; | |
| 2180 { | |
| 2181 if (fdp->infname != NULL) free (fdp->infname); | |
| 2182 if (fdp->infabsname != NULL) free (fdp->infabsname); | |
| 2183 if (fdp->infabsdir != NULL) free (fdp->infabsdir); | |
| 2184 if (fdp->taggedfname != NULL) free (fdp->taggedfname); | |
| 2185 if (fdp->prop != NULL) free (fdp->prop); | |
| 2186 free (fdp); | |
| 2187 } | |
| 2188 | |
| 2189 /* | |
| 428 | 2190 * add_node () |
| 2225 | 2191 * Adds a node to the tree of nodes. In etags mode, sort by file |
| 2192 * name. In ctags mode, sort by tag name. Make no attempt at | |
| 2193 * balancing. | |
| 428 | 2194 * |
| 2195 * add_node is the only function allowed to add nodes, so it can | |
| 2196 * maintain state. | |
| 2197 */ | |
| 442 | 2198 static void |
| 428 | 2199 add_node (np, cur_node_p) |
| 2200 node *np, **cur_node_p; | |
| 2201 { | |
| 2202 register int dif; | |
| 2203 register node *cur_node = *cur_node_p; | |
| 2204 | |
| 2205 if (cur_node == NULL) | |
| 2206 { | |
| 2207 *cur_node_p = np; | |
| 2208 last_node = np; | |
| 2209 return; | |
| 2210 } | |
| 2211 | |
| 2212 if (!CTAGS) | |
| 2225 | 2213 /* Etags Mode */ |
| 428 | 2214 { |
| 2225 | 2215 /* For each file name, tags are in a linked sublist on the right |
| 2216 pointer. The first tags of different files are a linked list | |
| 2217 on the left pointer. last_node points to the end of the last | |
| 2218 used sublist. */ | |
| 2219 if (last_node != NULL && last_node->fdp == np->fdp) | |
| 2220 { | |
| 2221 /* Let's use the same sublist as the last added node. */ | |
| 2222 assert (last_node->right == NULL); | |
| 2223 last_node->right = np; | |
| 2224 last_node = np; | |
| 2225 } | |
| 2226 else if (cur_node->fdp == np->fdp) | |
| 2227 { | |
| 2228 /* Scanning the list we found the head of a sublist which is | |
| 2229 good for us. Let's scan this sublist. */ | |
| 2230 add_node (np, &cur_node->right); | |
| 2231 } | |
| 2232 else | |
| 2233 /* The head of this sublist is not good for us. Let's try the | |
| 2234 next one. */ | |
| 2235 add_node (np, &cur_node->left); | |
| 2236 } /* if ETAGS mode */ | |
| 2237 | |
| 428 | 2238 else |
| 2239 { | |
| 2240 /* Ctags Mode */ | |
| 2241 dif = strcmp (np->name, cur_node->name); | |
| 2242 | |
| 2243 /* | |
| 2244 * If this tag name matches an existing one, then | |
| 2245 * do not add the node, but maybe print a warning. | |
| 2246 */ | |
| 3876 | 2247 if (no_duplicates && !dif) |
| 428 | 2248 { |
| 2225 | 2249 if (np->fdp == cur_node->fdp) |
| 428 | 2250 { |
| 2251 if (!no_warnings) | |
| 2252 { | |
| 2253 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n", | |
| 2225 | 2254 np->fdp->infname, lineno, np->name); |
| 428 | 2255 fprintf (stderr, "Second entry ignored\n"); |
| 2256 } | |
| 2257 } | |
| 2258 else if (!cur_node->been_warned && !no_warnings) | |
| 2259 { | |
| 2260 fprintf | |
| 2261 (stderr, | |
| 2262 "Duplicate entry in files %s and %s: %s (Warning only)\n", | |
| 2225 | 2263 np->fdp->infname, cur_node->fdp->infname, np->name); |
| 428 | 2264 cur_node->been_warned = TRUE; |
| 2265 } | |
| 2266 return; | |
| 2267 } | |
| 2268 | |
| 2269 /* Actually add the node */ | |
| 2270 add_node (np, dif < 0 ? &cur_node->left : &cur_node->right); | |
| 2225 | 2271 } /* if CTAGS mode */ |
| 2272 } | |
| 2273 | |
| 2274 /* | |
| 2275 * invalidate_nodes () | |
| 2276 * Scan the node tree and invalidate all nodes pointing to the | |
| 2277 * given file description (CTAGS case) or free them (ETAGS case). | |
| 2278 */ | |
| 2279 static void | |
| 2280 invalidate_nodes (badfdp, npp) | |
| 2281 fdesc *badfdp; | |
| 2282 node **npp; | |
| 2283 { | |
| 2284 node *np = *npp; | |
| 2285 | |
| 2286 if (np == NULL) | |
| 2287 return; | |
| 2288 | |
| 2289 if (CTAGS) | |
| 2290 { | |
| 2291 if (np->left != NULL) | |
| 2292 invalidate_nodes (badfdp, &np->left); | |
| 2293 if (np->fdp == badfdp) | |
| 2294 np->valid = FALSE; | |
| 2295 if (np->right != NULL) | |
| 2296 invalidate_nodes (badfdp, &np->right); | |
| 2297 } | |
| 2298 else | |
| 2299 { | |
| 2300 assert (np->fdp != NULL); | |
| 2301 if (np->fdp == badfdp) | |
| 2302 { | |
| 2303 *npp = np->left; /* detach the sublist from the list */ | |
| 2304 np->left = NULL; /* isolate it */ | |
| 2305 free_tree (np); /* free it */ | |
| 2306 invalidate_nodes (badfdp, npp); | |
| 2307 } | |
| 2308 else | |
| 2309 invalidate_nodes (badfdp, &np->left); | |
| 428 | 2310 } |
| 2311 } | |
| 458 | 2312 |
| 428 | 2313 |
| 2225 | 2314 static int total_size_of_entries __P((node *)); |
| 2315 static int number_len __P((long)); | |
| 2316 | |
| 2317 /* Length of a non-negative number's decimal representation. */ | |
| 428 | 2318 static int |
| 2319 number_len (num) | |
| 2320 long num; | |
| 2321 { | |
| 2322 int len = 1; | |
| 2323 while ((num /= 10) > 0) | |
| 2324 len += 1; | |
| 2325 return len; | |
| 2326 } | |
| 2327 | |
| 2328 /* | |
| 2329 * Return total number of characters that put_entries will output for | |
| 2225 | 2330 * the nodes in the linked list at the right of the specified node. |
| 2331 * This count is irrelevant with etags.el since emacs 19.34 at least, | |
| 2332 * but is still supplied for backward compatibility. | |
| 428 | 2333 */ |
| 442 | 2334 static int |
| 428 | 2335 total_size_of_entries (np) |
| 2336 register node *np; | |
| 2337 { | |
| 2225 | 2338 register int total = 0; |
| 2339 | |
| 2340 for (; np != NULL; np = np->right) | |
| 2341 if (np->valid) | |
| 2342 { | |
| 2343 total += strlen (np->regex) + 1; /* pat\177 */ | |
| 2344 if (np->name != NULL) | |
| 2345 total += strlen (np->name) + 1; /* name\001 */ | |
| 2346 total += number_len ((long) np->lno) + 1; /* lno, */ | |
| 2347 if (np->cno != invalidcharno) /* cno */ | |
| 2348 total += number_len (np->cno); | |
| 2349 total += 1; /* newline */ | |
| 2350 } | |
| 2351 | |
| 2352 return total; | |
| 2353 } | |
| 2354 | |
| 2355 static void | |
| 2356 put_entries (np) | |
| 2357 register node *np; | |
| 2358 { | |
| 2359 register char *sp; | |
| 2360 static fdesc *fdp = NULL; | |
| 428 | 2361 |
| 2362 if (np == NULL) | |
| 2225 | 2363 return; |
| 2364 | |
| 2365 /* Output subentries that precede this one */ | |
| 2366 if (CTAGS) | |
| 2367 put_entries (np->left); | |
| 2368 | |
| 2369 /* Output this entry */ | |
| 2370 if (np->valid) | |
| 428 | 2371 { |
| 2225 | 2372 if (!CTAGS) |
| 2373 { | |
| 2374 /* Etags mode */ | |
| 2375 if (fdp != np->fdp) | |
| 2376 { | |
| 2377 fdp = np->fdp; | |
| 2378 fprintf (tagf, "\f\n%s,%d\n", | |
| 2379 fdp->taggedfname, total_size_of_entries (np)); | |
| 2380 fdp->written = TRUE; | |
| 2381 } | |
| 2382 fputs (np->regex, tagf); | |
| 2383 fputc ('\177', tagf); | |
| 2384 if (np->name != NULL) | |
| 2385 { | |
| 2386 fputs (np->name, tagf); | |
| 2387 fputc ('\001', tagf); | |
| 2388 } | |
| 2389 fprintf (tagf, "%d,", np->lno); | |
| 2390 if (np->cno != invalidcharno) | |
| 2391 fprintf (tagf, "%ld", np->cno); | |
| 2392 fputs ("\n", tagf); | |
| 2393 } | |
| 2394 else | |
| 2395 { | |
| 2396 /* Ctags mode */ | |
| 2397 if (np->name == NULL) | |
| 2398 error ("internal error: NULL name in ctags mode.", (char *)NULL); | |
| 2399 | |
| 2400 if (cxref_style) | |
| 2401 { | |
| 2402 if (vgrind_style) | |
| 2403 fprintf (stdout, "%s %s %d\n", | |
| 2404 np->name, np->fdp->taggedfname, (np->lno + 63) / 64); | |
| 2405 else | |
| 2406 fprintf (stdout, "%-16s %3d %-16s %s\n", | |
| 2407 np->name, np->lno, np->fdp->taggedfname, np->regex); | |
| 2408 } | |
| 2409 else | |
| 2410 { | |
| 2411 fprintf (tagf, "%s\t%s\t", np->name, np->fdp->taggedfname); | |
| 2412 | |
| 2413 if (np->is_func) | |
| 2414 { /* function or #define macro with args */ | |
| 2415 putc (searchar, tagf); | |
| 2416 putc ('^', tagf); | |
| 2417 | |
| 2418 for (sp = np->regex; *sp; sp++) | |
| 2419 { | |
| 2420 if (*sp == '\\' || *sp == searchar) | |
| 2421 putc ('\\', tagf); | |
| 2422 putc (*sp, tagf); | |
| 2423 } | |
| 2424 putc (searchar, tagf); | |
| 2425 } | |
| 2426 else | |
| 2427 { /* anything else; text pattern inadequate */ | |
| 2428 fprintf (tagf, "%d", np->lno); | |
| 2429 } | |
| 2430 putc ('\n', tagf); | |
| 2431 } | |
| 2432 } | |
| 2433 } /* if this node contains a valid tag */ | |
| 2434 | |
| 2435 /* Output subentries that follow this one */ | |
| 2436 put_entries (np->right); | |
| 2437 if (!CTAGS) | |
| 2438 put_entries (np->left); | |
| 428 | 2439 } |
| 458 | 2440 |
| 428 | 2441 |
| 458 | 2442 /* C extensions. */ |
| 2443 #define C_EXT 0x00fff /* C extensions */ | |
| 2444 #define C_PLAIN 0x00000 /* C */ | |
| 2445 #define C_PLPL 0x00001 /* C++ */ | |
| 2446 #define C_STAR 0x00003 /* C* */ | |
| 2447 #define C_JAVA 0x00005 /* JAVA */ | |
| 2448 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */ | |
| 2449 #define YACC 0x10000 /* yacc file */ | |
| 2450 | |
| 428 | 2451 /* |
| 2452 * The C symbol tables. | |
| 2453 */ | |
| 2454 enum sym_type | |
| 2455 { | |
| 2456 st_none, | |
| 2457 st_C_objprot, st_C_objimpl, st_C_objend, | |
| 2458 st_C_gnumacro, | |
| 2325 | 2459 st_C_ignore, st_C_attribute, |
| 428 | 2460 st_C_javastruct, |
| 2461 st_C_operator, | |
| 531 | 2462 st_C_class, st_C_template, |
| 2325 | 2463 st_C_struct, st_C_extern, st_C_enum, st_C_define, st_C_typedef |
| 428 | 2464 }; |
| 2465 | |
| 709 | 2466 static unsigned int hash __P((const char *, unsigned int)); |
| 2467 static struct C_stab_entry * in_word_set __P((const char *, unsigned int)); | |
| 2468 static enum sym_type C_symtype __P((char *, int, int)); | |
| 442 | 2469 |
| 428 | 2470 /* Feed stuff between (but not including) %[ and %] lines to: |
| 2325 | 2471 gperf -m 5 |
| 428 | 2472 %[ |
| 2325 | 2473 %compare-strncmp |
| 2474 %enum | |
| 2475 %struct-type | |
| 428 | 2476 struct C_stab_entry { char *name; int c_ext; enum sym_type type; } |
| 2477 %% | |
| 2325 | 2478 if, 0, st_C_ignore |
| 2479 for, 0, st_C_ignore | |
| 2480 while, 0, st_C_ignore | |
| 2481 switch, 0, st_C_ignore | |
| 2482 return, 0, st_C_ignore | |
| 2483 __attribute__, 0, st_C_attribute | |
| 2484 @interface, 0, st_C_objprot | |
| 2485 @protocol, 0, st_C_objprot | |
| 2486 @implementation,0, st_C_objimpl | |
| 2487 @end, 0, st_C_objend | |
| 3876 | 2488 import, (C_JAVA & ~C_PLPL), st_C_ignore |
| 2489 package, (C_JAVA & ~C_PLPL), st_C_ignore | |
| 2325 | 2490 friend, C_PLPL, st_C_ignore |
| 3876 | 2491 extends, (C_JAVA & ~C_PLPL), st_C_javastruct |
| 2492 implements, (C_JAVA & ~C_PLPL), st_C_javastruct | |
| 2493 interface, (C_JAVA & ~C_PLPL), st_C_struct | |
| 2325 | 2494 class, 0, st_C_class |
| 2495 namespace, C_PLPL, st_C_struct | |
| 2496 domain, C_STAR, st_C_struct | |
| 2497 union, 0, st_C_struct | |
| 2498 struct, 0, st_C_struct | |
| 2499 extern, 0, st_C_extern | |
| 2500 enum, 0, st_C_enum | |
| 2501 typedef, 0, st_C_typedef | |
| 2502 define, 0, st_C_define | |
| 3517 | 2503 undef, 0, st_C_define |
| 2325 | 2504 operator, C_PLPL, st_C_operator |
| 2505 template, 0, st_C_template | |
| 428 | 2506 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach). |
| 2325 | 2507 DEFUN, 0, st_C_gnumacro |
| 2508 SYSCALL, 0, st_C_gnumacro | |
| 2509 ENTRY, 0, st_C_gnumacro | |
| 2510 PSEUDO, 0, st_C_gnumacro | |
| 428 | 2511 # These are defined inside C functions, so currently they are not met. |
| 2512 # EXFUN used in glibc, DEFVAR_* in emacs. | |
| 2325 | 2513 #EXFUN, 0, st_C_gnumacro |
| 2514 #DEFVAR_, 0, st_C_gnumacro | |
| 428 | 2515 %] |
| 2325 | 2516 and replace lines between %< and %> with its output, then: |
| 2517 - remove the #if characterset check | |
| 2518 - make in_word_set static and not inline. */ | |
| 428 | 2519 /*%<*/ |
| 2325 | 2520 /* C code produced by gperf version 3.0.1 */ |
| 2521 /* Command-line: gperf -m 5 */ | |
| 3517 | 2522 /* Computed positions: -k'2-3' */ |
| 2325 | 2523 |
| 428 | 2524 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }; |
| 3517 | 2525 /* maximum key range = 33, duplicates = 0 */ |
| 428 | 2526 |
| 2527 #ifdef __GNUC__ | |
| 2528 __inline | |
| 2325 | 2529 #else |
| 2530 #ifdef __cplusplus | |
| 2531 inline | |
| 2532 #endif | |
| 428 | 2533 #endif |
| 2534 static unsigned int | |
| 2535 hash (str, len) | |
| 2536 register const char *str; | |
| 2537 register unsigned int len; | |
| 2538 { | |
| 2539 static unsigned char asso_values[] = | |
| 2540 { | |
| 3517 | 2541 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, |
| 2542 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2543 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2544 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2545 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2546 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2547 35, 35, 35, 35, 35, 35, 35, 35, 35, 15, | |
| 2548 14, 35, 35, 35, 35, 35, 35, 35, 14, 35, | |
| 2549 35, 35, 35, 12, 13, 35, 35, 35, 35, 12, | |
| 2550 35, 35, 35, 35, 35, 1, 35, 16, 35, 6, | |
| 2551 23, 0, 0, 35, 22, 0, 35, 35, 5, 0, | |
| 2552 0, 15, 1, 35, 6, 35, 8, 19, 35, 16, | |
| 2553 4, 5, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2554 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2555 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2556 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2557 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2558 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2559 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2560 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2561 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2562 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2563 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2564 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2565 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, | |
| 2566 35, 35, 35, 35, 35, 35 | |
| 428 | 2567 }; |
| 3517 | 2568 register int hval = len; |
| 2569 | |
| 2570 switch (hval) | |
| 2571 { | |
| 2572 default: | |
| 2573 hval += asso_values[(unsigned char)str[2]]; | |
| 2574 /*FALLTHROUGH*/ | |
| 2575 case 2: | |
| 2576 hval += asso_values[(unsigned char)str[1]]; | |
| 2577 break; | |
| 2578 } | |
| 2579 return hval; | |
| 428 | 2580 } |
| 2581 | |
| 2582 static struct C_stab_entry * | |
| 2583 in_word_set (str, len) | |
| 2584 register const char *str; | |
| 2585 register unsigned int len; | |
| 2586 { | |
| 2325 | 2587 enum |
| 2588 { | |
| 3517 | 2589 TOTAL_KEYWORDS = 32, |
| 2325 | 2590 MIN_WORD_LENGTH = 2, |
| 2591 MAX_WORD_LENGTH = 15, | |
| 3517 | 2592 MIN_HASH_VALUE = 2, |
| 2593 MAX_HASH_VALUE = 34 | |
| 2325 | 2594 }; |
| 2595 | |
| 428 | 2596 static struct C_stab_entry wordlist[] = |
| 2597 { | |
| 3517 | 2598 {""}, {""}, |
| 2325 | 2599 {"if", 0, st_C_ignore}, |
| 3517 | 2600 {""}, |
| 2325 | 2601 {"@end", 0, st_C_objend}, |
| 3517 | 2602 {"union", 0, st_C_struct}, |
| 2603 {"define", 0, st_C_define}, | |
| 3876 | 2604 {"import", (C_JAVA & ~C_PLPL), st_C_ignore}, |
| 3517 | 2605 {"template", 0, st_C_template}, |
| 2606 {"operator", C_PLPL, st_C_operator}, | |
| 2607 {"@interface", 0, st_C_objprot}, | |
| 3876 | 2608 {"implements", (C_JAVA & ~C_PLPL), st_C_javastruct}, |
| 3517 | 2609 {"friend", C_PLPL, st_C_ignore}, |
| 2610 {"typedef", 0, st_C_typedef}, | |
| 2611 {"return", 0, st_C_ignore}, | |
| 2612 {"@implementation",0, st_C_objimpl}, | |
| 2613 {"@protocol", 0, st_C_objprot}, | |
| 3876 | 2614 {"interface", (C_JAVA & ~C_PLPL), st_C_struct}, |
| 2325 | 2615 {"extern", 0, st_C_extern}, |
| 3876 | 2616 {"extends", (C_JAVA & ~C_PLPL), st_C_javastruct}, |
| 3517 | 2617 {"struct", 0, st_C_struct}, |
| 2618 {"domain", C_STAR, st_C_struct}, | |
| 2619 {"switch", 0, st_C_ignore}, | |
| 2620 {"enum", 0, st_C_enum}, | |
| 2325 | 2621 {"for", 0, st_C_ignore}, |
| 3517 | 2622 {"namespace", C_PLPL, st_C_struct}, |
| 2623 {"class", 0, st_C_class}, | |
| 2624 {"while", 0, st_C_ignore}, | |
| 2625 {"undef", 0, st_C_define}, | |
| 3876 | 2626 {"package", (C_JAVA & ~C_PLPL), st_C_ignore}, |
| 2325 | 2627 {"__attribute__", 0, st_C_attribute}, |
| 2628 {"SYSCALL", 0, st_C_gnumacro}, | |
| 3517 | 2629 {"ENTRY", 0, st_C_gnumacro}, |
| 2325 | 2630 {"PSEUDO", 0, st_C_gnumacro}, |
| 2631 {"DEFUN", 0, st_C_gnumacro} | |
| 428 | 2632 }; |
| 2633 | |
| 2634 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) | |
| 2635 { | |
| 2636 register int key = hash (str, len); | |
| 2637 | |
| 2638 if (key <= MAX_HASH_VALUE && key >= 0) | |
| 2639 { | |
| 2640 register const char *s = wordlist[key].name; | |
| 2641 | |
| 2325 | 2642 if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0') |
| 428 | 2643 return &wordlist[key]; |
| 2644 } | |
| 2645 } | |
| 2646 return 0; | |
| 2647 } | |
| 2648 /*%>*/ | |
| 2649 | |
| 2650 static enum sym_type | |
| 2651 C_symtype (str, len, c_ext) | |
| 2652 char *str; | |
| 2653 int len; | |
| 2654 int c_ext; | |
| 2655 { | |
| 2656 register struct C_stab_entry *se = in_word_set (str, len); | |
| 2657 | |
| 2658 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext))) | |
| 2659 return st_none; | |
| 2660 return se->type; | |
| 2661 } | |
| 458 | 2662 |
| 428 | 2663 |
| 458 | 2664 /* |
| 2325 | 2665 * Ignoring __attribute__ ((list)) |
| 2666 */ | |
| 2667 static bool inattribute; /* looking at an __attribute__ construct */ | |
| 2668 | |
| 2669 /* | |
| 458 | 2670 * C functions and variables are recognized using a simple |
| 2671 * finite automaton. fvdef is its state variable. | |
| 2672 */ | |
| 2225 | 2673 static enum |
| 428 | 2674 { |
| 2675 fvnone, /* nothing seen */ | |
| 458 | 2676 fdefunkey, /* Emacs DEFUN keyword seen */ |
| 2677 fdefunname, /* Emacs DEFUN name seen */ | |
| 428 | 2678 foperator, /* func: operator keyword seen (cplpl) */ |
| 2679 fvnameseen, /* function or variable name seen */ | |
| 2680 fstartlist, /* func: just after open parenthesis */ | |
| 2681 finlist, /* func: in parameter list */ | |
| 2682 flistseen, /* func: after parameter list */ | |
| 2683 fignore, /* func: before open brace */ | |
| 2684 vignore /* var-like: ignore until ';' */ | |
| 2685 } fvdef; | |
| 2686 | |
| 2225 | 2687 static bool fvextern; /* func or var: extern keyword seen; */ |
| 428 | 2688 |
| 458 | 2689 /* |
| 2690 * typedefs are recognized using a simple finite automaton. | |
| 2691 * typdef is its state variable. | |
| 2692 */ | |
| 2225 | 2693 static enum |
| 428 | 2694 { |
| 2695 tnone, /* nothing seen */ | |
| 2696 tkeyseen, /* typedef keyword seen */ | |
| 2697 ttypeseen, /* defined type seen */ | |
| 2698 tinbody, /* inside typedef body */ | |
| 2699 tend, /* just before typedef tag */ | |
| 2700 tignore /* junk after typedef tag */ | |
| 2701 } typdef; | |
| 2702 | |
| 458 | 2703 /* |
| 2704 * struct-like structures (enum, struct and union) are recognized | |
| 2705 * using another simple finite automaton. `structdef' is its state | |
| 2706 * variable. | |
| 2707 */ | |
| 2225 | 2708 static enum |
| 428 | 2709 { |
| 458 | 2710 snone, /* nothing seen yet, |
| 2325 | 2711 or in struct body if bracelev > 0 */ |
| 428 | 2712 skeyseen, /* struct-like keyword seen */ |
| 2713 stagseen, /* struct-like tag seen */ | |
| 458 | 2714 scolonseen /* colon seen after struct-like tag */ |
| 428 | 2715 } structdef; |
| 2716 | |
| 2717 /* | |
| 2718 * When objdef is different from onone, objtag is the name of the class. | |
| 2719 */ | |
| 2225 | 2720 static char *objtag = "<uninited>"; |
| 428 | 2721 |
| 2722 /* | |
| 2723 * Yet another little state machine to deal with preprocessor lines. | |
| 2724 */ | |
| 2225 | 2725 static enum |
| 428 | 2726 { |
| 2727 dnone, /* nothing seen */ | |
| 2728 dsharpseen, /* '#' seen as first char on line */ | |
| 2729 ddefineseen, /* '#' and 'define' seen */ | |
| 2730 dignorerest /* ignore rest of line */ | |
| 2731 } definedef; | |
| 2732 | |
| 2733 /* | |
| 2734 * State machine for Objective C protocols and implementations. | |
| 458 | 2735 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995) |
| 428 | 2736 */ |
| 2225 | 2737 static enum |
| 428 | 2738 { |
| 2739 onone, /* nothing seen */ | |
| 2740 oprotocol, /* @interface or @protocol seen */ | |
| 2741 oimplementation, /* @implementations seen */ | |
| 2742 otagseen, /* class name seen */ | |
| 2743 oparenseen, /* parenthesis before category seen */ | |
| 2744 ocatseen, /* category name seen */ | |
| 2745 oinbody, /* in @implementation body */ | |
| 2746 omethodsign, /* in @implementation body, after +/- */ | |
| 2747 omethodtag, /* after method name */ | |
| 2748 omethodcolon, /* after method colon */ | |
| 2749 omethodparm, /* after method parameter */ | |
| 2750 oignore /* wait for @end */ | |
| 2751 } objdef; | |
| 2752 | |
| 2753 | |
| 2754 /* | |
| 2755 * Use this structure to keep info about the token read, and how it | |
| 2756 * should be tagged. Used by the make_C_tag function to build a tag. | |
| 2757 */ | |
| 2225 | 2758 static struct tok |
| 428 | 2759 { |
| 2225 | 2760 char *line; /* string containing the token */ |
| 2761 int offset; /* where the token starts in LINE */ | |
| 2762 int length; /* token length */ | |
| 2763 /* | |
| 2764 The previous members can be used to pass strings around for generic | |
| 2765 purposes. The following ones specifically refer to creating tags. In this | |
| 2766 case the token contained here is the pattern that will be used to create a | |
| 2767 tag. | |
| 2768 */ | |
| 2769 bool valid; /* do not create a tag; the token should be | |
| 2770 invalidated whenever a state machine is | |
| 2771 reset prematurely */ | |
| 2772 bool named; /* create a named tag */ | |
| 2773 int lineno; /* source line number of tag */ | |
| 2774 long linepos; /* source char number of tag */ | |
| 458 | 2775 } token; /* latest token read */ |
| 428 | 2776 |
| 2777 /* | |
| 458 | 2778 * Variables and functions for dealing with nested structures. |
| 2779 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001) | |
| 428 | 2780 */ |
| 709 | 2781 static void pushclass_above __P((int, char *, int)); |
| 2782 static void popclass_above __P((int)); | |
| 2783 static void write_classname __P((linebuffer *, char *qualifier)); | |
| 458 | 2784 |
| 2225 | 2785 static struct { |
| 458 | 2786 char **cname; /* nested class names */ |
| 2325 | 2787 int *bracelev; /* nested class brace level */ |
| 458 | 2788 int nl; /* class nesting level (elements used) */ |
| 2789 int size; /* length of the array */ | |
| 2790 } cstack; /* stack for nested declaration tags */ | |
| 2791 /* Current struct nesting depth (namespace, class, struct, union, enum). */ | |
| 2792 #define nestlev (cstack.nl) | |
| 2225 | 2793 /* After struct keyword or in struct body, not inside a nested function. */ |
| 458 | 2794 #define instruct (structdef == snone && nestlev > 0 \ |
| 2325 | 2795 && bracelev == cstack.bracelev[nestlev-1] + 1) |
| 458 | 2796 |
| 2797 static void | |
| 2325 | 2798 pushclass_above (bracelev, str, len) |
| 2799 int bracelev; | |
| 458 | 2800 char *str; |
| 2801 int len; | |
| 2802 { | |
| 2803 int nl; | |
| 2804 | |
| 2325 | 2805 popclass_above (bracelev); |
| 458 | 2806 nl = cstack.nl; |
| 2807 if (nl >= cstack.size) | |
| 2808 { | |
| 2809 int size = cstack.size *= 2; | |
| 2810 xrnew (cstack.cname, size, char *); | |
| 2325 | 2811 xrnew (cstack.bracelev, size, int); |
| 458 | 2812 } |
| 2325 | 2813 assert (nl == 0 || cstack.bracelev[nl-1] < bracelev); |
| 458 | 2814 cstack.cname[nl] = (str == NULL) ? NULL : savenstr (str, len); |
| 2325 | 2815 cstack.bracelev[nl] = bracelev; |
| 458 | 2816 cstack.nl = nl + 1; |
| 2817 } | |
| 2818 | |
| 2819 static void | |
| 2325 | 2820 popclass_above (bracelev) |
| 2821 int bracelev; | |
| 458 | 2822 { |
| 2823 int nl; | |
| 2824 | |
| 2825 for (nl = cstack.nl - 1; | |
| 2325 | 2826 nl >= 0 && cstack.bracelev[nl] >= bracelev; |
| 458 | 2827 nl--) |
| 2828 { | |
| 2829 if (cstack.cname[nl] != NULL) | |
| 2830 free (cstack.cname[nl]); | |
| 2831 cstack.nl = nl; | |
| 2832 } | |
| 2833 } | |
| 2834 | |
| 2835 static void | |
| 2836 write_classname (cn, qualifier) | |
| 2837 linebuffer *cn; | |
| 2838 char *qualifier; | |
| 2839 { | |
| 2840 int i, len; | |
| 2841 int qlen = strlen (qualifier); | |
| 2842 | |
| 2843 if (cstack.nl == 0 || cstack.cname[0] == NULL) | |
| 2844 { | |
| 2845 len = 0; | |
| 2846 cn->len = 0; | |
| 2847 cn->buffer[0] = '\0'; | |
| 2848 } | |
| 2849 else | |
| 2850 { | |
| 2851 len = strlen (cstack.cname[0]); | |
| 2852 linebuffer_setlen (cn, len); | |
| 2853 strcpy (cn->buffer, cstack.cname[0]); | |
| 2854 } | |
| 2855 for (i = 1; i < cstack.nl; i++) | |
| 2856 { | |
| 2857 char *s; | |
| 2858 int slen; | |
| 2859 | |
| 2860 s = cstack.cname[i]; | |
| 2861 if (s == NULL) | |
| 2862 continue; | |
| 2863 slen = strlen (s); | |
| 2864 len += slen + qlen; | |
| 2865 linebuffer_setlen (cn, len); | |
| 2866 strncat (cn->buffer, qualifier, qlen); | |
| 2867 strncat (cn->buffer, s, slen); | |
| 2868 } | |
| 2869 } | |
| 2870 | |
| 2871 | |
| 709 | 2872 static bool consider_token __P((char *, int, int, int *, int, int, bool *)); |
| 2873 static void make_C_tag __P((bool)); | |
| 442 | 2874 |
| 428 | 2875 /* |
| 2876 * consider_token () | |
| 2877 * checks to see if the current token is at the start of a | |
| 2878 * function or variable, or corresponds to a typedef, or | |
| 2879 * is a struct/union/enum tag, or #define, or an enum constant. | |
| 2880 * | |
| 2881 * *IS_FUNC gets TRUE iff the token is a function or #define macro | |
| 458 | 2882 * with args. C_EXTP points to which language we are looking at. |
| 428 | 2883 * |
| 2884 * Globals | |
| 2885 * fvdef IN OUT | |
| 2886 * structdef IN OUT | |
| 2887 * definedef IN OUT | |
| 2888 * typdef IN OUT | |
| 2889 * objdef IN OUT | |
| 2890 */ | |
| 2891 | |
| 2892 static bool | |
| 2325 | 2893 consider_token (str, len, c, c_extp, bracelev, parlev, is_func_or_var) |
| 428 | 2894 register char *str; /* IN: token pointer */ |
| 2895 register int len; /* IN: token length */ | |
| 442 | 2896 register int c; /* IN: first char after the token */ |
| 458 | 2897 int *c_extp; /* IN, OUT: C extensions mask */ |
| 2325 | 2898 int bracelev; /* IN: brace level */ |
| 428 | 2899 int parlev; /* IN: parenthesis level */ |
| 2900 bool *is_func_or_var; /* OUT: function or variable found */ | |
| 2901 { | |
| 2325 | 2902 /* When structdef is stagseen, scolonseen, or snone with bracelev > 0, |
| 458 | 2903 structtype is the type of the preceding struct-like keyword, and |
| 2325 | 2904 structbracelev is the brace level where it has been seen. */ |
| 458 | 2905 static enum sym_type structtype; |
| 2325 | 2906 static int structbracelev; |
| 458 | 2907 static enum sym_type toktype; |
| 2908 | |
| 2909 | |
| 2910 toktype = C_symtype (str, len, *c_extp); | |
| 428 | 2911 |
| 2912 /* | |
| 2325 | 2913 * Skip __attribute__ |
| 428 | 2914 */ |
| 2325 | 2915 if (toktype == st_C_attribute) |
| 428 | 2916 { |
| 2325 | 2917 inattribute = TRUE; |
| 428 | 2918 return FALSE; |
| 2325 | 2919 } |
| 2920 | |
| 2921 /* | |
| 2922 * Advance the definedef state machine. | |
| 2923 */ | |
| 2924 switch (definedef) | |
| 2925 { | |
| 2926 case dnone: | |
| 2927 /* We're not on a preprocessor line. */ | |
| 2928 if (toktype == st_C_gnumacro) | |
| 2929 { | |
| 2930 fvdef = fdefunkey; | |
| 2931 return FALSE; | |
| 2932 } | |
| 2933 break; | |
| 2934 case dsharpseen: | |
| 2935 if (toktype == st_C_define) | |
| 2936 { | |
| 2937 definedef = ddefineseen; | |
| 2938 } | |
| 2939 else | |
| 2940 { | |
| 2941 definedef = dignorerest; | |
| 2942 } | |
| 2943 return FALSE; | |
| 2944 case ddefineseen: | |
| 2945 /* | |
| 2946 * Make a tag for any macro, unless it is a constant | |
| 2947 * and constantypedefs is FALSE. | |
| 2948 */ | |
| 2949 definedef = dignorerest; | |
| 2950 *is_func_or_var = (c == '('); | |
| 2951 if (!*is_func_or_var && !constantypedefs) | |
| 2952 return FALSE; | |
| 2953 else | |
| 2954 return TRUE; | |
| 2955 case dignorerest: | |
| 2956 return FALSE; | |
| 2957 default: | |
| 2958 error ("internal error: definedef value.", (char *)NULL); | |
| 2959 } | |
| 2960 | |
| 2961 /* | |
| 2962 * Now typedefs | |
| 2963 */ | |
| 2964 switch (typdef) | |
| 2965 { | |
| 2966 case tnone: | |
| 2967 if (toktype == st_C_typedef) | |
| 2968 { | |
| 2969 if (typedefs) | |
| 2970 typdef = tkeyseen; | |
| 2971 fvextern = FALSE; | |
| 2972 fvdef = fvnone; | |
| 2973 return FALSE; | |
| 2974 } | |
| 2975 break; | |
| 2976 case tkeyseen: | |
| 2977 switch (toktype) | |
| 2978 { | |
| 2979 case st_none: | |
| 2980 case st_C_class: | |
| 2981 case st_C_struct: | |
| 2982 case st_C_enum: | |
| 2983 typdef = ttypeseen; | |
| 2984 } | |
| 2985 break; | |
| 2986 case ttypeseen: | |
| 2987 if (structdef == snone && fvdef == fvnone) | |
| 2988 { | |
| 2989 fvdef = fvnameseen; | |
| 2990 return TRUE; | |
| 2991 } | |
| 2992 break; | |
| 2993 case tend: | |
| 2994 switch (toktype) | |
| 2995 { | |
| 2996 case st_C_class: | |
| 2997 case st_C_struct: | |
| 2998 case st_C_enum: | |
| 2999 return FALSE; | |
| 3000 } | |
| 3001 return TRUE; | |
| 3002 } | |
| 3003 | |
| 3004 /* | |
| 3005 * This structdef business is NOT invoked when we are ctags and the | |
| 3006 * file is plain C. This is because a struct tag may have the same | |
| 3007 * name as another tag, and this loses with ctags. | |
| 3008 */ | |
| 3009 switch (toktype) | |
| 3010 { | |
| 3011 case st_C_javastruct: | |
| 3012 if (structdef == stagseen) | |
| 3013 structdef = scolonseen; | |
| 3014 return FALSE; | |
| 3015 case st_C_template: | |
| 3016 case st_C_class: | |
| 3017 if ((*c_extp & C_AUTO) /* automatic detection of C++ language */ | |
| 3018 && bracelev == 0 | |
| 3019 && definedef == dnone && structdef == snone | |
| 3020 && typdef == tnone && fvdef == fvnone) | |
| 3021 *c_extp = (*c_extp | C_PLPL) & ~C_AUTO; | |
| 3022 if (toktype == st_C_template) | |
| 3023 break; | |
| 3024 /* FALLTHRU */ | |
| 3025 case st_C_struct: | |
| 3026 case st_C_enum: | |
| 3027 if (parlev == 0 | |
| 3028 && fvdef != vignore | |
| 3029 && (typdef == tkeyseen | |
| 3030 || (typedefs_or_cplusplus && structdef == snone))) | |
| 3031 { | |
| 3032 structdef = skeyseen; | |
| 3033 structtype = toktype; | |
| 3034 structbracelev = bracelev; | |
| 3035 if (fvdef == fvnameseen) | |
| 3036 fvdef = fvnone; | |
| 3037 } | |
| 3038 return FALSE; | |
| 3039 } | |
| 3040 | |
| 3041 if (structdef == skeyseen) | |
| 3042 { | |
| 3043 structdef = stagseen; | |
| 3044 return TRUE; | |
| 3045 } | |
| 3046 | |
| 3047 if (typdef != tnone) | |
| 3048 definedef = dnone; | |
| 3049 | |
| 3050 /* Detect Objective C constructs. */ | |
| 3051 switch (objdef) | |
| 3052 { | |
| 3053 case onone: | |
| 3054 switch (toktype) | |
| 3055 { | |
| 3056 case st_C_objprot: | |
| 3057 objdef = oprotocol; | |
| 3058 return FALSE; | |
| 3059 case st_C_objimpl: | |
| 3060 objdef = oimplementation; | |
| 3061 return FALSE; | |
| 3062 } | |
| 3063 break; | |
| 3064 case oimplementation: | |
| 3065 /* Save the class tag for functions or variables defined inside. */ | |
| 3066 objtag = savenstr (str, len); | |
| 3067 objdef = oinbody; | |
| 3068 return FALSE; | |
| 3069 case oprotocol: | |
| 3070 /* Save the class tag for categories. */ | |
| 3071 objtag = savenstr (str, len); | |
| 3072 objdef = otagseen; | |
| 3073 *is_func_or_var = TRUE; | |
| 3074 return TRUE; | |
| 3075 case oparenseen: | |
| 3076 objdef = ocatseen; | |
| 3077 *is_func_or_var = TRUE; | |
| 3078 return TRUE; | |
| 3079 case oinbody: | |
| 3080 break; | |
| 3081 case omethodsign: | |
| 3082 if (parlev == 0) | |
| 3083 { | |
| 3084 fvdef = fvnone; | |
| 3085 objdef = omethodtag; | |
| 3086 linebuffer_setlen (&token_name, len); | |
| 3087 strncpy (token_name.buffer, str, len); | |
| 3088 token_name.buffer[len] = '\0'; | |
| 3089 return TRUE; | |
| 3090 } | |
| 3091 return FALSE; | |
| 3092 case omethodcolon: | |
| 3093 if (parlev == 0) | |
| 3094 objdef = omethodparm; | |
| 3095 return FALSE; | |
| 3096 case omethodparm: | |
| 3097 if (parlev == 0) | |
| 3098 { | |
| 3099 fvdef = fvnone; | |
| 3100 objdef = omethodtag; | |
| 3101 linebuffer_setlen (&token_name, token_name.len + len); | |
| 3102 strncat (token_name.buffer, str, len); | |
| 3103 return TRUE; | |
| 3104 } | |
| 3105 return FALSE; | |
| 3106 case oignore: | |
| 3107 if (toktype == st_C_objend) | |
| 3108 { | |
| 3109 /* Memory leakage here: the string pointed by objtag is | |
| 3110 never released, because many tests would be needed to | |
| 3111 avoid breaking on incorrect input code. The amount of | |
| 3112 memory leaked here is the sum of the lengths of the | |
| 3113 class tags. | |
| 3114 free (objtag); */ | |
| 3115 objdef = onone; | |
| 3116 } | |
| 3117 return FALSE; | |
| 3118 } | |
| 3119 | |
| 3120 /* A function, variable or enum constant? */ | |
| 3121 switch (toktype) | |
| 3122 { | |
| 3123 case st_C_extern: | |
| 3124 fvextern = TRUE; | |
| 3125 switch (fvdef) | |
| 3126 { | |
| 3127 case finlist: | |
| 3128 case flistseen: | |
| 3129 case fignore: | |
| 3130 case vignore: | |
| 3131 break; | |
| 3132 default: | |
| 3133 fvdef = fvnone; | |
| 3134 } | |
| 3135 return FALSE; | |
| 3136 case st_C_ignore: | |
| 3137 fvextern = FALSE; | |
| 3138 fvdef = vignore; | |
| 3139 return FALSE; | |
| 3140 case st_C_operator: | |
| 3141 fvdef = foperator; | |
| 3142 *is_func_or_var = TRUE; | |
| 3143 return TRUE; | |
| 3144 case st_none: | |
| 3145 if (constantypedefs | |
| 3146 && structdef == snone | |
| 3147 && structtype == st_C_enum && bracelev > structbracelev) | |
| 3148 return TRUE; /* enum constant */ | |
| 3149 switch (fvdef) | |
| 3150 { | |
| 3151 case fdefunkey: | |
| 3152 if (bracelev > 0) | |
| 3153 break; | |
| 3154 fvdef = fdefunname; /* GNU macro */ | |
| 3155 *is_func_or_var = TRUE; | |
| 3156 return TRUE; | |
| 3157 case fvnone: | |
| 3158 switch (typdef) | |
| 3159 { | |
| 3160 case ttypeseen: | |
| 3161 return FALSE; | |
| 3162 case tnone: | |
| 3163 if ((strneq (str, "asm", 3) && endtoken (str[3])) | |
| 3164 || (strneq (str, "__asm__", 7) && endtoken (str[7]))) | |
| 3165 { | |
| 3166 fvdef = vignore; | |
| 3167 return FALSE; | |
| 3168 } | |
| 3169 break; | |
| 3170 } | |
| 3171 /* FALLTHRU */ | |
| 3172 case fvnameseen: | |
| 2225 | 3173 if (len >= 10 && strneq (str+len-10, "::operator", 10)) |
| 458 | 3174 { |
| 2225 | 3175 if (*c_extp & C_AUTO) /* automatic detection of C++ */ |
| 3176 *c_extp = (*c_extp | C_PLPL) & ~C_AUTO; | |
| 458 | 3177 fvdef = foperator; |
| 3178 *is_func_or_var = TRUE; | |
| 3179 return TRUE; | |
| 3180 } | |
| 2325 | 3181 if (bracelev > 0 && !instruct) |
| 458 | 3182 break; |
| 428 | 3183 fvdef = fvnameseen; /* function or variable */ |
| 3184 *is_func_or_var = TRUE; | |
| 3185 return TRUE; | |
| 3186 } | |
| 3187 break; | |
| 3188 } | |
| 3189 | |
| 3190 return FALSE; | |
| 3191 } | |
| 3192 | |
| 458 | 3193 |
| 428 | 3194 /* |
| 458 | 3195 * C_entries often keeps pointers to tokens or lines which are older than |
| 3196 * the line currently read. By keeping two line buffers, and switching | |
| 3197 * them at end of line, it is possible to use those pointers. | |
| 428 | 3198 */ |
| 2225 | 3199 static struct |
| 458 | 3200 { |
| 3201 long linepos; | |
| 3202 linebuffer lb; | |
| 3203 } lbs[2]; | |
| 3204 | |
| 428 | 3205 #define current_lb_is_new (newndx == curndx) |
| 3206 #define switch_line_buffers() (curndx = 1 - curndx) | |
| 3207 | |
| 3208 #define curlb (lbs[curndx].lb) | |
| 3209 #define newlb (lbs[newndx].lb) | |
| 3210 #define curlinepos (lbs[curndx].linepos) | |
| 3211 #define newlinepos (lbs[newndx].linepos) | |
| 3212 | |
| 2225 | 3213 #define plainc ((c_ext & C_EXT) == C_PLAIN) |
| 3214 #define cplpl (c_ext & C_PLPL) | |
| 3215 #define cjava ((c_ext & C_JAVA) == C_JAVA) | |
| 3216 | |
| 428 | 3217 #define CNL_SAVE_DEFINEDEF() \ |
| 3218 do { \ | |
| 3219 curlinepos = charno; \ | |
| 2225 | 3220 readline (&curlb, inf); \ |
| 428 | 3221 lp = curlb.buffer; \ |
| 3222 quotednl = FALSE; \ | |
| 3223 newndx = curndx; \ | |
| 3224 } while (0) | |
| 3225 | |
| 3226 #define CNL() \ | |
| 3227 do { \ | |
| 3228 CNL_SAVE_DEFINEDEF(); \ | |
| 458 | 3229 if (savetoken.valid) \ |
| 428 | 3230 { \ |
| 458 | 3231 token = savetoken; \ |
| 3232 savetoken.valid = FALSE; \ | |
| 428 | 3233 } \ |
| 3234 definedef = dnone; \ | |
| 3235 } while (0) | |
| 3236 | |
| 3237 | |
| 3238 static void | |
| 3239 make_C_tag (isfun) | |
| 3240 bool isfun; | |
| 3241 { | |
| 3876 | 3242 /* This function is never called when token.valid is FALSE, but |
| 428 | 3243 we must protect against invalid input or internal errors. */ |
| 2225 | 3244 if (!DEBUG && !token.valid) |
| 3245 return; | |
| 3246 | |
| 3247 if (token.valid) | |
| 3248 make_tag (token_name.buffer, token_name.len, isfun, token.line, | |
| 3249 token.offset+token.length+1, token.lineno, token.linepos); | |
| 3250 else /* this case is optimised away if !DEBUG */ | |
| 3251 make_tag (concat ("INVALID TOKEN:-->", token_name.buffer, ""), | |
| 3252 token_name.len + 17, isfun, token.line, | |
| 3253 token.offset+token.length+1, token.lineno, token.linepos); | |
| 3254 | |
| 3255 token.valid = FALSE; | |
| 428 | 3256 } |
| 3257 | |
| 3258 | |
| 458 | 3259 /* |
| 3260 * C_entries () | |
| 3261 * This routine finds functions, variables, typedefs, | |
| 3262 * #define's, enum constants and struct/union/enum definitions in | |
| 3263 * C syntax and adds them to the list. | |
| 3264 */ | |
| 442 | 3265 static void |
| 428 | 3266 C_entries (c_ext, inf) |
| 3267 int c_ext; /* extension of C */ | |
| 3268 FILE *inf; /* input file */ | |
| 3269 { | |
| 3270 register char c; /* latest char read; '\0' for end of line */ | |
| 3271 register char *lp; /* pointer one beyond the character `c' */ | |
| 3272 int curndx, newndx; /* indices for current and new lb */ | |
| 3273 register int tokoff; /* offset in line of start of current token */ | |
| 3274 register int toklen; /* length of current token */ | |
| 3275 char *qualifier; /* string used to qualify names */ | |
| 3276 int qlen; /* length of qualifier */ | |
| 2325 | 3277 int bracelev; /* current brace level */ |
| 3278 int bracketlev; /* current bracket level */ | |
| 428 | 3279 int parlev; /* current parenthesis level */ |
| 2325 | 3280 int attrparlev; /* __attribute__ parenthesis level */ |
| 3281 int templatelev; /* current template level */ | |
| 3282 int typdefbracelev; /* bracelev where a typedef struct body begun */ | |
| 428 | 3283 bool incomm, inquote, inchar, quotednl, midtoken; |
| 458 | 3284 bool yacc_rules; /* in the rules part of a yacc file */ |
| 3876 | 3285 struct tok savetoken = {0}; /* token saved during preprocessor handling */ |
| 458 | 3286 |
| 3287 | |
| 2225 | 3288 linebuffer_init (&lbs[0].lb); |
| 3289 linebuffer_init (&lbs[1].lb); | |
| 458 | 3290 if (cstack.size == 0) |
| 3291 { | |
| 3292 cstack.size = (DEBUG) ? 1 : 4; | |
| 3293 cstack.nl = 0; | |
| 3294 cstack.cname = xnew (cstack.size, char *); | |
| 2325 | 3295 cstack.bracelev = xnew (cstack.size, int); |
| 458 | 3296 } |
| 428 | 3297 |
| 2325 | 3298 tokoff = toklen = typdefbracelev = 0; /* keep compiler quiet */ |
| 428 | 3299 curndx = newndx = 0; |
| 3300 lp = curlb.buffer; | |
| 3301 *lp = 0; | |
| 3302 | |
| 3303 fvdef = fvnone; fvextern = FALSE; typdef = tnone; | |
| 3304 structdef = snone; definedef = dnone; objdef = onone; | |
| 458 | 3305 yacc_rules = FALSE; |
| 428 | 3306 midtoken = inquote = inchar = incomm = quotednl = FALSE; |
| 458 | 3307 token.valid = savetoken.valid = FALSE; |
| 2325 | 3308 bracelev = bracketlev = parlev = attrparlev = templatelev = 0; |
| 428 | 3309 if (cjava) |
| 3310 { qualifier = "."; qlen = 1; } | |
| 3311 else | |
| 3312 { qualifier = "::"; qlen = 2; } | |
| 3313 | |
| 458 | 3314 |
| 428 | 3315 while (!feof (inf)) |
| 3316 { | |
| 3317 c = *lp++; | |
| 3318 if (c == '\\') | |
| 3319 { | |
| 2325 | 3320 /* If we are at the end of the line, the next character is a |
| 3321 '\0'; do not skip it, because it is what tells us | |
| 428 | 3322 to read the next line. */ |
| 3323 if (*lp == '\0') | |
| 3324 { | |
| 3325 quotednl = TRUE; | |
| 3326 continue; | |
| 3327 } | |
| 3328 lp++; | |
| 3329 c = ' '; | |
| 3330 } | |
| 3331 else if (incomm) | |
| 3332 { | |
| 3333 switch (c) | |
| 3334 { | |
| 3335 case '*': | |
| 3336 if (*lp == '/') | |
| 3337 { | |
| 3338 c = *lp++; | |
| 3339 incomm = FALSE; | |
| 3340 } | |
| 3341 break; | |
| 3342 case '\0': | |
| 3343 /* Newlines inside comments do not end macro definitions in | |
| 3344 traditional cpp. */ | |
| 3345 CNL_SAVE_DEFINEDEF (); | |
| 3346 break; | |
| 3347 } | |
| 3348 continue; | |
| 3349 } | |
| 3350 else if (inquote) | |
| 3351 { | |
| 3352 switch (c) | |
| 3353 { | |
| 3354 case '"': | |
| 3355 inquote = FALSE; | |
| 3356 break; | |
| 3357 case '\0': | |
| 3358 /* Newlines inside strings do not end macro definitions | |
| 3359 in traditional cpp, even though compilers don't | |
| 3360 usually accept them. */ | |
| 3361 CNL_SAVE_DEFINEDEF (); | |
| 3362 break; | |
| 3363 } | |
| 3364 continue; | |
| 3365 } | |
| 3366 else if (inchar) | |
| 3367 { | |
| 3368 switch (c) | |
| 3369 { | |
| 3370 case '\0': | |
| 3371 /* Hmmm, something went wrong. */ | |
| 3372 CNL (); | |
| 3373 /* FALLTHRU */ | |
| 3374 case '\'': | |
| 3375 inchar = FALSE; | |
| 3376 break; | |
| 3377 } | |
| 3378 continue; | |
| 3379 } | |
| 2325 | 3380 else if (bracketlev > 0) |
| 3381 { | |
| 3382 switch (c) | |
| 3383 { | |
| 3384 case ']': | |
| 3385 if (--bracketlev > 0) | |
| 428 | 3386 continue; |
| 2325 | 3387 break; |
| 3388 case '\0': | |
| 3389 CNL_SAVE_DEFINEDEF (); | |
| 3390 break; | |
| 3391 } | |
| 3392 continue; | |
| 3393 } | |
| 3394 else switch (c) | |
| 3395 { | |
| 3396 case '"': | |
| 3397 inquote = TRUE; | |
| 3398 if (inattribute) | |
| 3399 break; | |
| 3400 switch (fvdef) | |
| 3401 { | |
| 3402 case fdefunkey: | |
| 3403 case fstartlist: | |
| 3404 case finlist: | |
| 3405 case fignore: | |
| 3406 case vignore: | |
| 428 | 3407 break; |
| 2325 | 3408 default: |
| 3409 fvextern = FALSE; | |
| 3410 fvdef = fvnone; | |
| 3411 } | |
| 3412 continue; | |
| 3413 case '\'': | |
| 3414 inchar = TRUE; | |
| 3415 if (inattribute) | |
| 3416 break; | |
| 3417 if (fvdef != finlist && fvdef != fignore && fvdef !=vignore) | |
| 3418 { | |
| 3419 fvextern = FALSE; | |
| 3420 fvdef = fvnone; | |
| 3421 } | |
| 3422 continue; | |
| 3423 case '/': | |
| 3424 if (*lp == '*') | |
| 3425 { | |
| 3972 | 3426 incomm = TRUE; |
| 2325 | 3427 lp++; |
| 3972 | 3428 c = ' '; |
| 2325 | 3429 } |
| 3430 else if (/* cplpl && */ *lp == '/') | |
| 3431 { | |
| 3432 c = '\0'; | |
| 3433 } | |
| 3972 | 3434 break; |
| 2325 | 3435 case '%': |
| 3436 if ((c_ext & YACC) && *lp == '%') | |
| 3437 { | |
| 3438 /* Entering or exiting rules section in yacc file. */ | |
| 3439 lp++; | |
| 3440 definedef = dnone; fvdef = fvnone; fvextern = FALSE; | |
| 3441 typdef = tnone; structdef = snone; | |
| 3442 midtoken = inquote = inchar = incomm = quotednl = FALSE; | |
| 3443 bracelev = 0; | |
| 3444 yacc_rules = !yacc_rules; | |
| 3445 continue; | |
| 3446 } | |
| 3447 else | |
| 3448 break; | |
| 3449 case '#': | |
| 3450 if (definedef == dnone) | |
| 3451 { | |
| 3452 char *cp; | |
| 3453 bool cpptoken = TRUE; | |
| 3454 | |
| 3455 /* Look back on this line. If all blanks, or nonblanks | |
| 3456 followed by an end of comment, this is a preprocessor | |
| 3457 token. */ | |
| 3458 for (cp = newlb.buffer; cp < lp-1; cp++) | |
| 3459 if (!iswhite (*cp)) | |
| 3460 { | |
| 3461 if (*cp == '*' && *(cp+1) == '/') | |
| 3462 { | |
| 3463 cp++; | |
| 3464 cpptoken = TRUE; | |
| 3465 } | |
| 3466 else | |
| 3467 cpptoken = FALSE; | |
| 3468 } | |
| 3469 if (cpptoken) | |
| 3470 definedef = dsharpseen; | |
| 3471 } /* if (definedef == dnone) */ | |
| 3472 continue; | |
| 3473 case '[': | |
| 3474 bracketlev++; | |
| 428 | 3475 continue; |
| 2325 | 3476 } /* switch (c) */ |
| 428 | 3477 |
| 3478 | |
| 458 | 3479 /* Consider token only if some involved conditions are satisfied. */ |
| 3480 if (typdef != tignore | |
| 428 | 3481 && definedef != dignorerest |
| 458 | 3482 && fvdef != finlist |
| 2325 | 3483 && templatelev == 0 |
| 458 | 3484 && (definedef != dnone |
| 2325 | 3485 || structdef != scolonseen) |
| 3486 && !inattribute) | |
| 428 | 3487 { |
| 3488 if (midtoken) | |
| 3489 { | |
| 3490 if (endtoken (c)) | |
| 3491 { | |
| 2225 | 3492 if (c == ':' && *lp == ':' && begtoken (lp[1])) |
| 3493 /* This handles :: in the middle, | |
| 3494 but not at the beginning of an identifier. | |
| 3495 Also, space-separated :: is not recognised. */ | |
| 428 | 3496 { |
| 2225 | 3497 if (c_ext & C_AUTO) /* automatic detection of C++ */ |
| 3498 c_ext = (c_ext | C_PLPL) & ~C_AUTO; | |
| 428 | 3499 lp += 2; |
| 3500 toklen += 2; | |
| 3501 c = lp[-1]; | |
| 458 | 3502 goto still_in_token; |
| 428 | 3503 } |
| 3504 else | |
| 3505 { | |
| 458 | 3506 bool funorvar = FALSE; |
| 3507 | |
| 428 | 3508 if (yacc_rules |
| 3509 || consider_token (newlb.buffer + tokoff, toklen, c, | |
| 2325 | 3510 &c_ext, bracelev, parlev, |
| 3511 &funorvar)) | |
| 428 | 3512 { |
| 3513 if (fvdef == foperator) | |
| 3514 { | |
| 3515 char *oldlp = lp; | |
| 3516 lp = skip_spaces (lp-1); | |
| 3517 if (*lp != '\0') | |
| 3518 lp += 1; | |
| 3519 while (*lp != '\0' | |
| 442 | 3520 && !iswhite (*lp) && *lp != '(') |
| 428 | 3521 lp += 1; |
| 3522 c = *lp++; | |
| 3523 toklen += lp - oldlp; | |
| 3524 } | |
| 458 | 3525 token.named = FALSE; |
| 2225 | 3526 if (!plainc |
| 458 | 3527 && nestlev > 0 && definedef == dnone) |
| 3528 /* in struct body */ | |
| 428 | 3529 { |
| 458 | 3530 write_classname (&token_name, qualifier); |
| 3531 linebuffer_setlen (&token_name, | |
| 3532 token_name.len+qlen+toklen); | |
| 428 | 3533 strcat (token_name.buffer, qualifier); |
| 3534 strncat (token_name.buffer, | |
| 3535 newlb.buffer + tokoff, toklen); | |
| 458 | 3536 token.named = TRUE; |
| 428 | 3537 } |
| 3538 else if (objdef == ocatseen) | |
| 3539 /* Objective C category */ | |
| 3540 { | |
| 3541 int len = strlen (objtag) + 2 + toklen; | |
| 458 | 3542 linebuffer_setlen (&token_name, len); |
| 428 | 3543 strcpy (token_name.buffer, objtag); |
| 3544 strcat (token_name.buffer, "("); | |
| 3545 strncat (token_name.buffer, | |
| 3546 newlb.buffer + tokoff, toklen); | |
| 3547 strcat (token_name.buffer, ")"); | |
| 458 | 3548 token.named = TRUE; |
| 428 | 3549 } |
| 3550 else if (objdef == omethodtag | |
| 3551 || objdef == omethodparm) | |
| 3552 /* Objective C method */ | |
| 3553 { | |
| 458 | 3554 token.named = TRUE; |
| 3555 } | |
| 3556 else if (fvdef == fdefunname) | |
| 3557 /* GNU DEFUN and similar macros */ | |
| 3558 { | |
| 3559 bool defun = (newlb.buffer[tokoff] == 'F'); | |
| 3560 int off = tokoff; | |
| 3561 int len = toklen; | |
| 3562 | |
| 3563 /* Rewrite the tag so that emacs lisp DEFUNs | |
| 3564 can be found by their elisp name */ | |
| 3565 if (defun) | |
| 3566 { | |
| 3567 off += 1; | |
| 3568 len -= 1; | |
| 3569 } | |
| 3570 linebuffer_setlen (&token_name, len); | |
| 3571 strncpy (token_name.buffer, | |
| 3572 newlb.buffer + off, len); | |
| 3573 token_name.buffer[len] = '\0'; | |
| 3574 if (defun) | |
| 3575 while (--len >= 0) | |
| 3576 if (token_name.buffer[len] == '_') | |
| 3577 token_name.buffer[len] = '-'; | |
| 3578 token.named = defun; | |
| 428 | 3579 } |
| 3580 else | |
| 3581 { | |
| 458 | 3582 linebuffer_setlen (&token_name, toklen); |
| 428 | 3583 strncpy (token_name.buffer, |
| 3584 newlb.buffer + tokoff, toklen); | |
| 3585 token_name.buffer[toklen] = '\0'; | |
| 3586 /* Name macros and members. */ | |
| 458 | 3587 token.named = (structdef == stagseen |
| 3588 || typdef == ttypeseen | |
| 3589 || typdef == tend | |
| 3590 || (funorvar | |
| 3591 && definedef == dignorerest) | |
| 3592 || (funorvar | |
| 3593 && definedef == dnone | |
| 3594 && structdef == snone | |
| 2325 | 3595 && bracelev > 0)); |
| 428 | 3596 } |
| 458 | 3597 token.lineno = lineno; |
| 3598 token.offset = tokoff; | |
| 3599 token.length = toklen; | |
| 3600 token.line = newlb.buffer; | |
| 3601 token.linepos = newlinepos; | |
| 3602 token.valid = TRUE; | |
| 428 | 3603 |
| 3604 if (definedef == dnone | |
| 3605 && (fvdef == fvnameseen | |
| 3606 || fvdef == foperator | |
| 3607 || structdef == stagseen | |
| 3608 || typdef == tend | |
| 458 | 3609 || typdef == ttypeseen |
| 428 | 3610 || objdef != onone)) |
| 3611 { | |
| 3612 if (current_lb_is_new) | |
| 3613 switch_line_buffers (); | |
| 3614 } | |
| 458 | 3615 else if (definedef != dnone |
| 3616 || fvdef == fdefunname | |
| 3617 || instruct) | |
| 428 | 3618 make_C_tag (funorvar); |
| 3619 } | |
| 2325 | 3620 else /* not yacc and consider_token failed */ |
| 3621 { | |
| 3622 if (inattribute && fvdef == fignore) | |
| 3623 { | |
| 3624 /* We have just met __attribute__ after a | |
| 3625 function parameter list: do not tag the | |
| 3626 function again. */ | |
| 3627 fvdef = fvnone; | |
| 3628 } | |
| 3629 } | |
| 428 | 3630 midtoken = FALSE; |
| 3631 } | |
| 3632 } /* if (endtoken (c)) */ | |
| 3633 else if (intoken (c)) | |
| 458 | 3634 still_in_token: |
| 428 | 3635 { |
| 3636 toklen++; | |
| 3637 continue; | |
| 3638 } | |
| 3639 } /* if (midtoken) */ | |
| 3640 else if (begtoken (c)) | |
| 3641 { | |
| 3642 switch (definedef) | |
| 3643 { | |
| 3644 case dnone: | |
| 3645 switch (fvdef) | |
| 3646 { | |
| 3647 case fstartlist: | |
| 2325 | 3648 /* This prevents tagging fb in |
| 3649 void (__attribute__((noreturn)) *fb) (void); | |
| 3650 Fixing this is not easy and not very important. */ | |
| 428 | 3651 fvdef = finlist; |
| 3652 continue; | |
| 3653 case flistseen: | |
| 2225 | 3654 if (plainc || declarations) |
| 3655 { | |
| 3656 make_C_tag (TRUE); /* a function */ | |
| 3657 fvdef = fignore; | |
| 3658 } | |
| 428 | 3659 break; |
| 3660 } | |
| 3661 if (structdef == stagseen && !cjava) | |
| 458 | 3662 { |
| 2325 | 3663 popclass_above (bracelev); |
| 458 | 3664 structdef = snone; |
| 3665 } | |
| 428 | 3666 break; |
| 3667 case dsharpseen: | |
| 458 | 3668 savetoken = token; |
| 2225 | 3669 break; |
| 428 | 3670 } |
| 3671 if (!yacc_rules || lp == newlb.buffer + 1) | |
| 3672 { | |
| 3673 tokoff = lp - 1 - newlb.buffer; | |
| 3674 toklen = 1; | |
| 3675 midtoken = TRUE; | |
| 3676 } | |
| 3677 continue; | |
| 3678 } /* if (begtoken) */ | |
| 3679 } /* if must look at token */ | |
| 3680 | |
| 3681 | |
| 3682 /* Detect end of line, colon, comma, semicolon and various braces | |
| 3683 after having handled a token.*/ | |
| 3684 switch (c) | |
| 3685 { | |
| 3686 case ':': | |
| 2325 | 3687 if (inattribute) |
| 3688 break; | |
| 458 | 3689 if (yacc_rules && token.offset == 0 && token.valid) |
| 3690 { | |
| 3691 make_C_tag (FALSE); /* a yacc function */ | |
| 3692 break; | |
| 3693 } | |
| 428 | 3694 if (definedef != dnone) |
| 3695 break; | |
| 3696 switch (objdef) | |
| 3697 { | |
| 3698 case otagseen: | |
| 3699 objdef = oignore; | |
| 3700 make_C_tag (TRUE); /* an Objective C class */ | |
| 3701 break; | |
| 3702 case omethodtag: | |
| 3703 case omethodparm: | |
| 3704 objdef = omethodcolon; | |
| 458 | 3705 linebuffer_setlen (&token_name, token_name.len + 1); |
| 428 | 3706 strcat (token_name.buffer, ":"); |
| 3707 break; | |
| 3708 } | |
| 3709 if (structdef == stagseen) | |
| 2225 | 3710 { |
| 3711 structdef = scolonseen; | |
| 3712 break; | |
| 3713 } | |
| 3714 /* Should be useless, but may be work as a safety net. */ | |
| 3715 if (cplpl && fvdef == flistseen) | |
| 3716 { | |
| 3717 make_C_tag (TRUE); /* a function */ | |
| 3718 fvdef = fignore; | |
| 3719 break; | |
| 3720 } | |
| 428 | 3721 break; |
| 3722 case ';': | |
| 2325 | 3723 if (definedef != dnone || inattribute) |
| 428 | 3724 break; |
| 458 | 3725 switch (typdef) |
| 428 | 3726 { |
| 458 | 3727 case tend: |
| 3728 case ttypeseen: | |
| 3729 make_C_tag (FALSE); /* a typedef */ | |
| 3730 typdef = tnone; | |
| 3731 fvdef = fvnone; | |
| 428 | 3732 break; |
| 458 | 3733 case tnone: |
| 3734 case tinbody: | |
| 3735 case tignore: | |
| 3736 switch (fvdef) | |
| 3737 { | |
| 3738 case fignore: | |
| 2225 | 3739 if (typdef == tignore || cplpl) |
| 458 | 3740 fvdef = fvnone; |
| 3741 break; | |
| 3742 case fvnameseen: | |
| 2325 | 3743 if ((globals && bracelev == 0 && (!fvextern || declarations)) |
| 458 | 3744 || (members && instruct)) |
| 3745 make_C_tag (FALSE); /* a variable */ | |
| 3746 fvextern = FALSE; | |
| 3747 fvdef = fvnone; | |
| 3748 token.valid = FALSE; | |
| 3749 break; | |
| 3750 case flistseen: | |
| 2325 | 3751 if ((declarations |
| 3752 && (cplpl || !instruct) | |
| 3753 && (typdef == tnone || (typdef != tignore && instruct))) | |
| 3754 || (members | |
| 3755 && plainc && instruct)) | |
| 3756 make_C_tag (TRUE); /* a function */ | |
| 458 | 3757 /* FALLTHRU */ |
| 3758 default: | |
| 3759 fvextern = FALSE; | |
| 3760 fvdef = fvnone; | |
| 3761 if (declarations | |
| 2225 | 3762 && cplpl && structdef == stagseen) |
| 458 | 3763 make_C_tag (FALSE); /* forward declaration */ |
| 3764 else | |
| 3765 token.valid = FALSE; | |
| 3766 } /* switch (fvdef) */ | |
| 428 | 3767 /* FALLTHRU */ |
| 3768 default: | |
| 458 | 3769 if (!instruct) |
| 3770 typdef = tnone; | |
| 428 | 3771 } |
| 3772 if (structdef == stagseen) | |
| 3773 structdef = snone; | |
| 3774 break; | |
| 3775 case ',': | |
| 2325 | 3776 if (definedef != dnone || inattribute) |
| 428 | 3777 break; |
| 3778 switch (objdef) | |
| 3779 { | |
| 3780 case omethodtag: | |
| 3781 case omethodparm: | |
| 3782 make_C_tag (TRUE); /* an Objective C method */ | |
| 3783 objdef = oinbody; | |
| 3784 break; | |
| 3785 } | |
| 3786 switch (fvdef) | |
| 3787 { | |
| 458 | 3788 case fdefunkey: |
| 428 | 3789 case foperator: |
| 458 | 3790 case fstartlist: |
| 428 | 3791 case finlist: |
| 3792 case fignore: | |
| 3793 case vignore: | |
| 3794 break; | |
| 458 | 3795 case fdefunname: |
| 3796 fvdef = fignore; | |
| 3797 break; | |
| 2325 | 3798 case fvnameseen: |
| 3799 if (parlev == 0 | |
| 3800 && ((globals | |
| 3801 && bracelev == 0 | |
| 3802 && templatelev == 0 | |
| 3803 && (!fvextern || declarations)) | |
| 3804 || (members && instruct))) | |
| 3805 make_C_tag (FALSE); /* a variable */ | |
| 458 | 3806 break; |
| 2325 | 3807 case flistseen: |
| 458 | 3808 if ((declarations && typdef == tnone && !instruct) |
| 3809 || (members && typdef != tignore && instruct)) | |
| 3810 { | |
| 2325 | 3811 make_C_tag (TRUE); /* a function */ |
| 458 | 3812 fvdef = fvnameseen; |
| 3813 } | |
| 3814 else if (!declarations) | |
| 3815 fvdef = fvnone; | |
| 3816 token.valid = FALSE; | |
| 428 | 3817 break; |
| 3818 default: | |
| 3819 fvdef = fvnone; | |
| 3820 } | |
| 3821 if (structdef == stagseen) | |
| 3822 structdef = snone; | |
| 3823 break; | |
| 2325 | 3824 case ']': |
| 3825 if (definedef != dnone || inattribute) | |
| 428 | 3826 break; |
| 458 | 3827 if (structdef == stagseen) |
| 3828 structdef = snone; | |
| 3829 switch (typdef) | |
| 428 | 3830 { |
| 458 | 3831 case ttypeseen: |
| 3832 case tend: | |
| 428 | 3833 typdef = tignore; |
| 3834 make_C_tag (FALSE); /* a typedef */ | |
| 3835 break; | |
| 458 | 3836 case tnone: |
| 3837 case tinbody: | |
| 3838 switch (fvdef) | |
| 3839 { | |
| 3840 case foperator: | |
| 3841 case finlist: | |
| 3842 case fignore: | |
| 3843 case vignore: | |
| 3844 break; | |
| 3845 case fvnameseen: | |
| 2325 | 3846 if ((members && bracelev == 1) |
| 3847 || (globals && bracelev == 0 | |
| 458 | 3848 && (!fvextern || declarations))) |
| 3849 make_C_tag (FALSE); /* a variable */ | |
| 3850 /* FALLTHRU */ | |
| 3851 default: | |
| 3852 fvdef = fvnone; | |
| 3853 } | |
| 428 | 3854 break; |
| 3855 } | |
| 3856 break; | |
| 3857 case '(': | |
| 2325 | 3858 if (inattribute) |
| 3859 { | |
| 3860 attrparlev++; | |
| 3861 break; | |
| 3862 } | |
| 428 | 3863 if (definedef != dnone) |
| 3864 break; | |
| 3865 if (objdef == otagseen && parlev == 0) | |
| 3866 objdef = oparenseen; | |
| 3867 switch (fvdef) | |
| 3868 { | |
| 3869 case fvnameseen: | |
| 3870 if (typdef == ttypeseen | |
| 3871 && *lp != '*' | |
| 458 | 3872 && !instruct) |
| 428 | 3873 { |
| 3874 /* This handles constructs like: | |
| 3875 typedef void OperatorFun (int fun); */ | |
| 3876 make_C_tag (FALSE); | |
| 3877 typdef = tignore; | |
| 458 | 3878 fvdef = fignore; |
| 3879 break; | |
| 428 | 3880 } |
| 3881 /* FALLTHRU */ | |
| 3882 case foperator: | |
| 3883 fvdef = fstartlist; | |
| 3884 break; | |
| 3885 case flistseen: | |
| 3886 fvdef = finlist; | |
| 3887 break; | |
| 3888 } | |
| 3889 parlev++; | |
| 3890 break; | |
| 3891 case ')': | |
| 2325 | 3892 if (inattribute) |
| 3893 { | |
| 3894 if (--attrparlev == 0) | |
| 3895 inattribute = FALSE; | |
| 3896 break; | |
| 3897 } | |
| 428 | 3898 if (definedef != dnone) |
| 3899 break; | |
| 3900 if (objdef == ocatseen && parlev == 1) | |
| 3901 { | |
| 3902 make_C_tag (TRUE); /* an Objective C category */ | |
| 3903 objdef = oignore; | |
| 3904 } | |
| 3905 if (--parlev == 0) | |
| 3906 { | |
| 3907 switch (fvdef) | |
| 3908 { | |
| 3909 case fstartlist: | |
| 3910 case finlist: | |
| 3911 fvdef = flistseen; | |
| 3912 break; | |
| 3913 } | |
| 458 | 3914 if (!instruct |
| 3915 && (typdef == tend | |
| 3916 || typdef == ttypeseen)) | |
| 428 | 3917 { |
| 3918 typdef = tignore; | |
| 3919 make_C_tag (FALSE); /* a typedef */ | |
| 3920 } | |
| 3921 } | |
| 3922 else if (parlev < 0) /* can happen due to ill-conceived #if's. */ | |
| 3923 parlev = 0; | |
| 3924 break; | |
| 3925 case '{': | |
| 3926 if (definedef != dnone) | |
| 3927 break; | |
| 3928 if (typdef == ttypeseen) | |
| 3929 { | |
| 531 | 3930 /* Whenever typdef is set to tinbody (currently only |
| 2325 | 3931 here), typdefbracelev should be set to bracelev. */ |
| 531 | 3932 typdef = tinbody; |
| 2325 | 3933 typdefbracelev = bracelev; |
| 428 | 3934 } |
| 3935 switch (fvdef) | |
| 3936 { | |
| 3937 case flistseen: | |
| 458 | 3938 make_C_tag (TRUE); /* a function */ |
| 428 | 3939 /* FALLTHRU */ |
| 3940 case fignore: | |
| 3941 fvdef = fvnone; | |
| 3942 break; | |
| 3943 case fvnone: | |
| 3944 switch (objdef) | |
| 3945 { | |
| 3946 case otagseen: | |
| 3947 make_C_tag (TRUE); /* an Objective C class */ | |
| 3948 objdef = oignore; | |
| 3949 break; | |
| 3950 case omethodtag: | |
| 3951 case omethodparm: | |
| 3952 make_C_tag (TRUE); /* an Objective C method */ | |
| 3953 objdef = oinbody; | |
| 3954 break; | |
| 3955 default: | |
| 3956 /* Neutralize `extern "C" {' grot. */ | |
| 2325 | 3957 if (bracelev == 0 && structdef == snone && nestlev == 0 |
| 458 | 3958 && typdef == tnone) |
| 2325 | 3959 bracelev = -1; |
| 428 | 3960 } |
| 2225 | 3961 break; |
| 428 | 3962 } |
| 458 | 3963 switch (structdef) |
| 3964 { | |
| 3965 case skeyseen: /* unnamed struct */ | |
| 2325 | 3966 pushclass_above (bracelev, NULL, 0); |
| 458 | 3967 structdef = snone; |
| 3968 break; | |
| 3969 case stagseen: /* named struct or enum */ | |
| 3970 case scolonseen: /* a class */ | |
| 2325 | 3971 pushclass_above (bracelev,token.line+token.offset, token.length); |
| 458 | 3972 structdef = snone; |
| 3973 make_C_tag (FALSE); /* a struct or enum */ | |
| 3974 break; | |
| 3975 } | |
| 2325 | 3976 bracelev++; |
| 428 | 3977 break; |
| 3978 case '*': | |
| 3979 if (definedef != dnone) | |
| 3980 break; | |
| 3981 if (fvdef == fstartlist) | |
| 2225 | 3982 { |
| 3983 fvdef = fvnone; /* avoid tagging `foo' in `foo (*bar()) ()' */ | |
| 3984 token.valid = FALSE; | |
| 3985 } | |
| 428 | 3986 break; |
| 3987 case '}': | |
| 3988 if (definedef != dnone) | |
| 3989 break; | |
| 2225 | 3990 if (!ignoreindent && lp == newlb.buffer + 1) |
| 428 | 3991 { |
| 2325 | 3992 if (bracelev != 0) |
| 2225 | 3993 token.valid = FALSE; |
| 2325 | 3994 bracelev = 0; /* reset brace level if first column */ |
| 428 | 3995 parlev = 0; /* also reset paren level, just in case... */ |
| 3996 } | |
| 2325 | 3997 else if (bracelev > 0) |
| 3998 bracelev--; | |
| 2225 | 3999 else |
| 4000 token.valid = FALSE; /* something gone amiss, token unreliable */ | |
| 2325 | 4001 popclass_above (bracelev); |
| 458 | 4002 structdef = snone; |
| 2325 | 4003 /* Only if typdef == tinbody is typdefbracelev significant. */ |
| 4004 if (typdef == tinbody && bracelev <= typdefbracelev) | |
| 428 | 4005 { |
| 2325 | 4006 assert (bracelev == typdefbracelev); |
| 458 | 4007 typdef = tend; |
| 428 | 4008 } |
| 4009 break; | |
| 4010 case '=': | |
| 4011 if (definedef != dnone) | |
| 4012 break; | |
| 4013 switch (fvdef) | |
| 4014 { | |
| 4015 case foperator: | |
| 4016 case finlist: | |
| 4017 case fignore: | |
| 4018 case vignore: | |
| 4019 break; | |
| 4020 case fvnameseen: | |
| 2325 | 4021 if ((members && bracelev == 1) |
| 4022 || (globals && bracelev == 0 && (!fvextern || declarations))) | |
| 428 | 4023 make_C_tag (FALSE); /* a variable */ |
| 4024 /* FALLTHRU */ | |
| 4025 default: | |
| 4026 fvdef = vignore; | |
| 4027 } | |
| 4028 break; | |
| 458 | 4029 case '<': |
| 2325 | 4030 if (cplpl |
| 4031 && (structdef == stagseen || fvdef == fvnameseen)) | |
| 458 | 4032 { |
| 2325 | 4033 templatelev++; |
| 458 | 4034 break; |
| 4035 } | |
| 4036 goto resetfvdef; | |
| 4037 case '>': | |
| 2325 | 4038 if (templatelev > 0) |
| 458 | 4039 { |
| 2325 | 4040 templatelev--; |
| 458 | 4041 break; |
| 4042 } | |
| 4043 goto resetfvdef; | |
| 428 | 4044 case '+': |
| 4045 case '-': | |
| 2325 | 4046 if (objdef == oinbody && bracelev == 0) |
| 428 | 4047 { |
| 4048 objdef = omethodsign; | |
| 4049 break; | |
| 4050 } | |
| 4051 /* FALLTHRU */ | |
| 458 | 4052 resetfvdef: |
| 2325 | 4053 case '#': case '~': case '&': case '%': case '/': |
| 4054 case '|': case '^': case '!': case '.': case '?': | |
| 428 | 4055 if (definedef != dnone) |
| 4056 break; | |
| 4057 /* These surely cannot follow a function tag in C. */ | |
| 4058 switch (fvdef) | |
| 4059 { | |
| 4060 case foperator: | |
| 4061 case finlist: | |
| 4062 case fignore: | |
| 4063 case vignore: | |
| 4064 break; | |
| 4065 default: | |
| 4066 fvdef = fvnone; | |
| 4067 } | |
| 4068 break; | |
| 4069 case '\0': | |
| 4070 if (objdef == otagseen) | |
| 4071 { | |
| 4072 make_C_tag (TRUE); /* an Objective C class */ | |
| 4073 objdef = oignore; | |
| 4074 } | |
| 4075 /* If a macro spans multiple lines don't reset its state. */ | |
| 4076 if (quotednl) | |
| 4077 CNL_SAVE_DEFINEDEF (); | |
| 4078 else | |
| 4079 CNL (); | |
| 4080 break; | |
| 4081 } /* switch (c) */ | |
| 4082 | |
| 4083 } /* while not eof */ | |
| 458 | 4084 |
| 4085 free (lbs[0].lb.buffer); | |
| 4086 free (lbs[1].lb.buffer); | |
| 428 | 4087 } |
| 4088 | |
| 4089 /* | |
| 4090 * Process either a C++ file or a C file depending on the setting | |
| 4091 * of a global flag. | |
| 4092 */ | |
| 442 | 4093 static void |
| 428 | 4094 default_C_entries (inf) |
| 4095 FILE *inf; | |
| 4096 { | |
| 458 | 4097 C_entries (cplusplus ? C_PLPL : C_AUTO, inf); |
| 428 | 4098 } |
| 4099 | |
| 458 | 4100 /* Always do plain C. */ |
| 442 | 4101 static void |
| 428 | 4102 plain_C_entries (inf) |
| 4103 FILE *inf; | |
| 4104 { | |
| 4105 C_entries (0, inf); | |
| 4106 } | |
| 4107 | |
| 4108 /* Always do C++. */ | |
| 442 | 4109 static void |
| 428 | 4110 Cplusplus_entries (inf) |
| 4111 FILE *inf; | |
| 4112 { | |
| 4113 C_entries (C_PLPL, inf); | |
| 4114 } | |
| 4115 | |
| 4116 /* Always do Java. */ | |
| 442 | 4117 static void |
| 428 | 4118 Cjava_entries (inf) |
| 4119 FILE *inf; | |
| 4120 { | |
| 4121 C_entries (C_JAVA, inf); | |
| 4122 } | |
| 4123 | |
| 4124 /* Always do C*. */ | |
| 442 | 4125 static void |
| 428 | 4126 Cstar_entries (inf) |
| 4127 FILE *inf; | |
| 4128 { | |
| 4129 C_entries (C_STAR, inf); | |
| 4130 } | |
| 4131 | |
| 4132 /* Always do Yacc. */ | |
| 442 | 4133 static void |
| 428 | 4134 Yacc_entries (inf) |
| 4135 FILE *inf; | |
| 4136 { | |
| 4137 C_entries (YACC, inf); | |
| 4138 } | |
| 458 | 4139 |
| 428 | 4140 |
| 709 | 4141 /* Useful macros. */ |
| 428 | 4142 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \ |
| 2225 | 4143 for (; /* loop initialization */ \ |
| 428 | 4144 !feof (file_pointer) /* loop test */ \ |
| 2225 | 4145 && /* instructions at start of loop */ \ |
| 4146 (readline (&line_buffer, file_pointer), \ | |
| 4147 char_pointer = line_buffer.buffer, \ | |
| 428 | 4148 TRUE); \ |
| 4149 ) | |
| 2554 | 4150 |
| 4151 #define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \ | |
| 4152 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \ | |
| 4153 && strneq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \ | |
| 4154 && notinname ((cp)[sizeof(kw)-1]) /* end of kw */ \ | |
| 4155 && ((cp) = skip_spaces((cp)+sizeof(kw)-1))) /* skip spaces */ | |
| 4156 | |
| 4157 /* Similar to LOOKING_AT but does not use notinname, does not skip */ | |
| 4158 #define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \ | |
| 4159 ((assert("" kw), TRUE) /* syntax error if not a literal string */ \ | |
| 4160 && strncaseeq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \ | |
| 4161 && ((cp) += sizeof(kw)-1)) /* skip spaces */ | |
| 428 | 4162 |
| 4163 /* | |
| 4164 * Read a file, but do no processing. This is used to do regexp | |
| 4165 * matching on files that have no language defined. | |
| 4166 */ | |
| 442 | 4167 static void |
| 428 | 4168 just_read_file (inf) |
| 4169 FILE *inf; | |
| 4170 { | |
| 4171 register char *dummy; | |
| 4172 | |
| 4173 LOOP_ON_INPUT_LINES (inf, lb, dummy) | |
| 4174 continue; | |
| 4175 } | |
| 458 | 4176 |
| 428 | 4177 |
| 4178 /* Fortran parsing */ | |
| 4179 | |
| 2225 | 4180 static void F_takeprec __P((void)); |
| 4181 static void F_getit __P((FILE *)); | |
| 428 | 4182 |
| 442 | 4183 static void |
| 2225 | 4184 F_takeprec () |
| 428 | 4185 { |
| 4186 dbp = skip_spaces (dbp); | |
| 4187 if (*dbp != '*') | |
| 4188 return; | |
| 4189 dbp++; | |
| 4190 dbp = skip_spaces (dbp); | |
| 4191 if (strneq (dbp, "(*)", 3)) | |
| 4192 { | |
| 4193 dbp += 3; | |
| 4194 return; | |
| 4195 } | |
| 458 | 4196 if (!ISDIGIT (*dbp)) |
| 428 | 4197 { |
| 4198 --dbp; /* force failure */ | |
| 4199 return; | |
| 4200 } | |
| 4201 do | |
| 4202 dbp++; | |
| 458 | 4203 while (ISDIGIT (*dbp)); |
| 428 | 4204 } |
| 4205 | |
| 4206 static void | |
| 2225 | 4207 F_getit (inf) |
| 428 | 4208 FILE *inf; |
| 4209 { | |
| 4210 register char *cp; | |
| 4211 | |
| 4212 dbp = skip_spaces (dbp); | |
| 4213 if (*dbp == '\0') | |
| 4214 { | |
| 2225 | 4215 readline (&lb, inf); |
| 428 | 4216 dbp = lb.buffer; |
| 4217 if (dbp[5] != '&') | |
| 4218 return; | |
| 4219 dbp += 6; | |
| 4220 dbp = skip_spaces (dbp); | |
| 4221 } | |
| 458 | 4222 if (!ISALPHA (*dbp) && *dbp != '_' && *dbp != '$') |
| 428 | 4223 return; |
| 4224 for (cp = dbp + 1; *cp != '\0' && intoken (*cp); cp++) | |
| 4225 continue; | |
| 2225 | 4226 make_tag (dbp, cp-dbp, TRUE, |
| 4227 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 428 | 4228 } |
| 4229 | |
| 4230 | |
| 442 | 4231 static void |
| 428 | 4232 Fortran_functions (inf) |
| 4233 FILE *inf; | |
| 4234 { | |
| 4235 LOOP_ON_INPUT_LINES (inf, lb, dbp) | |
| 4236 { | |
| 4237 if (*dbp == '%') | |
| 4238 dbp++; /* Ratfor escape to fortran */ | |
| 4239 dbp = skip_spaces (dbp); | |
| 4240 if (*dbp == '\0') | |
| 4241 continue; | |
| 4242 switch (lowcase (*dbp)) | |
| 4243 { | |
| 4244 case 'i': | |
| 2225 | 4245 if (nocase_tail ("integer")) |
| 4246 F_takeprec (); | |
| 428 | 4247 break; |
| 4248 case 'r': | |
| 2225 | 4249 if (nocase_tail ("real")) |
| 4250 F_takeprec (); | |
| 428 | 4251 break; |
| 4252 case 'l': | |
| 2225 | 4253 if (nocase_tail ("logical")) |
| 4254 F_takeprec (); | |
| 428 | 4255 break; |
| 4256 case 'c': | |
| 2225 | 4257 if (nocase_tail ("complex") || nocase_tail ("character")) |
| 4258 F_takeprec (); | |
| 428 | 4259 break; |
| 4260 case 'd': | |
| 2225 | 4261 if (nocase_tail ("double")) |
| 428 | 4262 { |
| 4263 dbp = skip_spaces (dbp); | |
| 4264 if (*dbp == '\0') | |
| 4265 continue; | |
| 2225 | 4266 if (nocase_tail ("precision")) |
| 428 | 4267 break; |
| 4268 continue; | |
| 4269 } | |
| 4270 break; | |
| 4271 } | |
| 4272 dbp = skip_spaces (dbp); | |
| 4273 if (*dbp == '\0') | |
| 4274 continue; | |
| 4275 switch (lowcase (*dbp)) | |
| 4276 { | |
| 4277 case 'f': | |
| 2225 | 4278 if (nocase_tail ("function")) |
| 4279 F_getit (inf); | |
| 428 | 4280 continue; |
| 4281 case 's': | |
| 2225 | 4282 if (nocase_tail ("subroutine")) |
| 4283 F_getit (inf); | |
| 428 | 4284 continue; |
| 4285 case 'e': | |
| 2225 | 4286 if (nocase_tail ("entry")) |
| 4287 F_getit (inf); | |
| 428 | 4288 continue; |
| 4289 case 'b': | |
| 2225 | 4290 if (nocase_tail ("blockdata") || nocase_tail ("block data")) |
| 428 | 4291 { |
| 4292 dbp = skip_spaces (dbp); | |
| 4293 if (*dbp == '\0') /* assume un-named */ | |
| 2225 | 4294 make_tag ("blockdata", 9, TRUE, |
| 4295 lb.buffer, dbp - lb.buffer, lineno, linecharno); | |
| 428 | 4296 else |
| 2225 | 4297 F_getit (inf); /* look for name */ |
| 428 | 4298 } |
| 4299 continue; | |
| 4300 } | |
| 4301 } | |
| 4302 } | |
| 458 | 4303 |
| 428 | 4304 |
| 4305 /* | |
| 4306 * Ada parsing | |
| 2225 | 4307 * Original code by |
| 4308 * Philippe Waroquiers (1998) | |
| 428 | 4309 */ |
| 442 | 4310 |
| 2225 | 4311 static void Ada_getit __P((FILE *, char *)); |
| 442 | 4312 |
| 428 | 4313 /* Once we are positioned after an "interesting" keyword, let's get |
| 4314 the real tag value necessary. */ | |
| 4315 static void | |
| 2225 | 4316 Ada_getit (inf, name_qualifier) |
| 428 | 4317 FILE *inf; |
| 4318 char *name_qualifier; | |
| 4319 { | |
| 4320 register char *cp; | |
| 4321 char *name; | |
| 4322 char c; | |
| 4323 | |
| 4324 while (!feof (inf)) | |
| 4325 { | |
| 4326 dbp = skip_spaces (dbp); | |
| 4327 if (*dbp == '\0' | |
| 4328 || (dbp[0] == '-' && dbp[1] == '-')) | |
| 4329 { | |
| 2225 | 4330 readline (&lb, inf); |
| 428 | 4331 dbp = lb.buffer; |
| 4332 } | |
| 2225 | 4333 switch (lowcase(*dbp)) |
| 428 | 4334 { |
| 4335 case 'b': | |
| 2225 | 4336 if (nocase_tail ("body")) |
| 428 | 4337 { |
| 4338 /* Skipping body of procedure body or package body or .... | |
| 4339 resetting qualifier to body instead of spec. */ | |
| 4340 name_qualifier = "/b"; | |
| 4341 continue; | |
| 4342 } | |
| 4343 break; | |
| 4344 case 't': | |
| 4345 /* Skipping type of task type or protected type ... */ | |
| 2225 | 4346 if (nocase_tail ("type")) |
| 428 | 4347 continue; |
| 4348 break; | |
| 4349 } | |
| 4350 if (*dbp == '"') | |
| 4351 { | |
| 4352 dbp += 1; | |
| 4353 for (cp = dbp; *cp != '\0' && *cp != '"'; cp++) | |
| 4354 continue; | |
| 4355 } | |
| 4356 else | |
| 4357 { | |
| 4358 dbp = skip_spaces (dbp); | |
| 4359 for (cp = dbp; | |
| 4360 (*cp != '\0' | |
| 458 | 4361 && (ISALPHA (*cp) || ISDIGIT (*cp) || *cp == '_' || *cp == '.')); |
| 428 | 4362 cp++) |
| 4363 continue; | |
| 4364 if (cp == dbp) | |
| 4365 return; | |
| 4366 } | |
| 4367 c = *cp; | |
| 4368 *cp = '\0'; | |
| 4369 name = concat (dbp, name_qualifier, ""); | |
| 4370 *cp = c; | |
| 2225 | 4371 make_tag (name, strlen (name), TRUE, |
| 4372 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 4373 free (name); | |
| 428 | 4374 if (c == '"') |
| 4375 dbp = cp + 1; | |
| 4376 return; | |
| 4377 } | |
| 4378 } | |
| 4379 | |
| 442 | 4380 static void |
| 428 | 4381 Ada_funcs (inf) |
| 4382 FILE *inf; | |
| 4383 { | |
| 4384 bool inquote = FALSE; | |
| 2225 | 4385 bool skip_till_semicolumn = FALSE; |
| 428 | 4386 |
| 4387 LOOP_ON_INPUT_LINES (inf, lb, dbp) | |
| 4388 { | |
| 4389 while (*dbp != '\0') | |
| 4390 { | |
| 4391 /* Skip a string i.e. "abcd". */ | |
| 4392 if (inquote || (*dbp == '"')) | |
| 4393 { | |
| 4394 dbp = etags_strchr ((inquote) ? dbp : dbp+1, '"'); | |
| 4395 if (dbp != NULL) | |
| 4396 { | |
| 4397 inquote = FALSE; | |
| 4398 dbp += 1; | |
| 4399 continue; /* advance char */ | |
| 4400 } | |
| 4401 else | |
| 4402 { | |
| 4403 inquote = TRUE; | |
| 4404 break; /* advance line */ | |
| 4405 } | |
| 4406 } | |
| 4407 | |
| 4408 /* Skip comments. */ | |
| 4409 if (dbp[0] == '-' && dbp[1] == '-') | |
| 4410 break; /* advance line */ | |
| 4411 | |
| 4412 /* Skip character enclosed in single quote i.e. 'a' | |
| 4413 and skip single quote starting an attribute i.e. 'Image. */ | |
| 4414 if (*dbp == '\'') | |
| 4415 { | |
| 4416 dbp++ ; | |
| 4417 if (*dbp != '\0') | |
| 4418 dbp++; | |
| 4419 continue; | |
| 4420 } | |
| 4421 | |
| 2225 | 4422 if (skip_till_semicolumn) |
| 4423 { | |
| 4424 if (*dbp == ';') | |
| 4425 skip_till_semicolumn = FALSE; | |
| 4426 dbp++; | |
| 4427 continue; /* advance char */ | |
| 4428 } | |
| 4429 | |
| 428 | 4430 /* Search for beginning of a token. */ |
| 4431 if (!begtoken (*dbp)) | |
| 4432 { | |
| 4433 dbp++; | |
| 4434 continue; /* advance char */ | |
| 4435 } | |
| 4436 | |
| 4437 /* We are at the beginning of a token. */ | |
| 2225 | 4438 switch (lowcase(*dbp)) |
| 428 | 4439 { |
| 4440 case 'f': | |
| 2225 | 4441 if (!packages_only && nocase_tail ("function")) |
| 4442 Ada_getit (inf, "/f"); | |
| 428 | 4443 else |
| 4444 break; /* from switch */ | |
| 4445 continue; /* advance char */ | |
| 4446 case 'p': | |
| 2225 | 4447 if (!packages_only && nocase_tail ("procedure")) |
| 4448 Ada_getit (inf, "/p"); | |
| 4449 else if (nocase_tail ("package")) | |
| 4450 Ada_getit (inf, "/s"); | |
| 4451 else if (nocase_tail ("protected")) /* protected type */ | |
| 4452 Ada_getit (inf, "/t"); | |
| 428 | 4453 else |
| 4454 break; /* from switch */ | |
| 4455 continue; /* advance char */ | |
| 2225 | 4456 |
| 4457 case 'u': | |
| 4458 if (typedefs && !packages_only && nocase_tail ("use")) | |
| 4459 { | |
| 4460 /* when tagging types, avoid tagging use type Pack.Typename; | |
| 4461 for this, we will skip everything till a ; */ | |
| 4462 skip_till_semicolumn = TRUE; | |
| 4463 continue; /* advance char */ | |
| 4464 } | |
| 4465 | |
| 428 | 4466 case 't': |
| 2225 | 4467 if (!packages_only && nocase_tail ("task")) |
| 4468 Ada_getit (inf, "/k"); | |
| 4469 else if (typedefs && !packages_only && nocase_tail ("type")) | |
| 428 | 4470 { |
| 2225 | 4471 Ada_getit (inf, "/t"); |
| 428 | 4472 while (*dbp != '\0') |
| 4473 dbp += 1; | |
| 4474 } | |
| 4475 else | |
| 4476 break; /* from switch */ | |
| 4477 continue; /* advance char */ | |
| 4478 } | |
| 4479 | |
| 4480 /* Look for the end of the token. */ | |
| 4481 while (!endtoken (*dbp)) | |
| 4482 dbp++; | |
| 4483 | |
| 4484 } /* advance char */ | |
| 4485 } /* advance line */ | |
| 4486 } | |
| 458 | 4487 |
| 428 | 4488 |
| 4489 /* | |
| 4490 * Unix and microcontroller assembly tag handling | |
| 2225 | 4491 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/ |
| 4492 * Idea by Bob Weiner, Motorola Inc. (1994) | |
| 428 | 4493 */ |
| 442 | 4494 static void |
| 428 | 4495 Asm_labels (inf) |
| 4496 FILE *inf; | |
| 4497 { | |
| 4498 register char *cp; | |
| 4499 | |
| 4500 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 4501 { | |
| 4502 /* If first char is alphabetic or one of [_.$], test for colon | |
| 4503 following identifier. */ | |
| 458 | 4504 if (ISALPHA (*cp) || *cp == '_' || *cp == '.' || *cp == '$') |
| 428 | 4505 { |
| 4506 /* Read past label. */ | |
| 4507 cp++; | |
| 458 | 4508 while (ISALNUM (*cp) || *cp == '_' || *cp == '.' || *cp == '$') |
| 428 | 4509 cp++; |
| 442 | 4510 if (*cp == ':' || iswhite (*cp)) |
| 2225 | 4511 /* Found end of label, so copy it and add it to the table. */ |
| 4512 make_tag (lb.buffer, cp - lb.buffer, TRUE, | |
| 428 | 4513 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); |
| 4514 } | |
| 4515 } | |
| 4516 } | |
| 458 | 4517 |
| 428 | 4518 |
| 4519 /* | |
| 458 | 4520 * Perl support |
| 2225 | 4521 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/ |
| 428 | 4522 * Perl variable names: /^(my|local).../ |
| 2225 | 4523 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995) |
| 4524 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997) | |
| 4525 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001) | |
| 428 | 4526 */ |
| 442 | 4527 static void |
| 428 | 4528 Perl_functions (inf) |
| 4529 FILE *inf; | |
| 4530 { | |
| 2225 | 4531 char *package = savestr ("main"); /* current package name */ |
| 428 | 4532 register char *cp; |
| 4533 | |
| 4534 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 4535 { | |
| 2225 | 4536 skip_spaces(cp); |
| 4537 | |
| 4538 if (LOOKING_AT (cp, "package")) | |
| 4539 { | |
| 4540 free (package); | |
| 4541 get_tag (cp, &package); | |
| 4542 } | |
| 4543 else if (LOOKING_AT (cp, "sub")) | |
| 428 | 4544 { |
| 2225 | 4545 char *pos; |
| 4546 char *sp = cp; | |
| 4547 | |
| 4548 while (!notinname (*cp)) | |
| 4549 cp++; | |
| 4550 if (cp == sp) | |
| 4551 continue; /* nothing found */ | |
| 4552 if ((pos = etags_strchr (sp, ':')) != NULL | |
| 4553 && pos < cp && pos[1] == ':') | |
| 4554 /* The name is already qualified. */ | |
| 4555 make_tag (sp, cp - sp, TRUE, | |
| 4556 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 4557 else | |
| 4558 /* Qualify it. */ | |
| 4559 { | |
| 4560 char savechar, *name; | |
| 4561 | |
| 4562 savechar = *cp; | |
| 4563 *cp = '\0'; | |
| 4564 name = concat (package, "::", sp); | |
| 4565 *cp = savechar; | |
| 4566 make_tag (name, strlen(name), TRUE, | |
| 4567 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 4568 free (name); | |
| 4569 } | |
| 428 | 4570 } |
| 2225 | 4571 else if (globals) /* only if we are tagging global vars */ |
| 428 | 4572 { |
| 2225 | 4573 /* Skip a qualifier, if any. */ |
| 4574 bool qual = LOOKING_AT (cp, "my") || LOOKING_AT (cp, "local"); | |
| 428 | 4575 /* After "my" or "local", but before any following paren or space. */ |
| 2225 | 4576 char *varstart = cp; |
| 4577 | |
| 4578 if (qual /* should this be removed? If yes, how? */ | |
| 4579 && (*cp == '$' || *cp == '@' || *cp == '%')) | |
| 428 | 4580 { |
| 2225 | 4581 varstart += 1; |
| 4582 do | |
| 428 | 4583 cp++; |
| 2225 | 4584 while (ISALNUM (*cp) || *cp == '_'); |
| 428 | 4585 } |
| 2225 | 4586 else if (qual) |
| 428 | 4587 { |
| 4588 /* Should be examining a variable list at this point; | |
| 4589 could insist on seeing an open parenthesis. */ | |
| 4590 while (*cp != '\0' && *cp != ';' && *cp != '=' && *cp != ')') | |
| 4591 cp++; | |
| 4592 } | |
| 2225 | 4593 else |
| 4594 continue; | |
| 4595 | |
| 4596 make_tag (varstart, cp - varstart, FALSE, | |
| 4597 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 428 | 4598 } |
| 4599 } | |
| 3517 | 4600 free (package); |
| 428 | 4601 } |
| 458 | 4602 |
| 709 | 4603 |
| 428 | 4604 /* |
| 458 | 4605 * Python support |
| 2225 | 4606 * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/ |
| 4607 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997) | |
| 4608 * More ideas by seb bacon <seb@jamkit.com> (2002) | |
| 428 | 4609 */ |
| 442 | 4610 static void |
| 428 | 4611 Python_functions (inf) |
| 4612 FILE *inf; | |
| 4613 { | |
| 4614 register char *cp; | |
| 4615 | |
| 4616 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 2225 | 4617 { |
| 4618 cp = skip_spaces (cp); | |
| 4619 if (LOOKING_AT (cp, "def") || LOOKING_AT (cp, "class")) | |
| 4620 { | |
| 4621 char *name = cp; | |
| 4622 while (!notinname (*cp) && *cp != ':') | |
| 4623 cp++; | |
| 4624 make_tag (name, cp - name, TRUE, | |
| 4625 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 4626 } | |
| 4627 } | |
| 709 | 4628 } |
| 4629 | |
| 4630 | |
| 4631 /* | |
| 4632 * PHP support | |
| 4633 * Look for: | |
| 4634 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/ | |
| 4635 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/ | |
| 4636 * - /^[ \t]*define\(\"[^\"]+/ | |
| 4637 * Only with --members: | |
| 4638 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/ | |
| 2225 | 4639 * Idea by Diez B. Roggisch (2001) |
| 709 | 4640 */ |
| 4641 static void | |
| 4642 PHP_functions (inf) | |
| 4643 FILE *inf; | |
| 4644 { | |
| 2225 | 4645 register char *cp, *name; |
| 709 | 4646 bool search_identifier = FALSE; |
| 4647 | |
| 4648 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 428 | 4649 { |
| 709 | 4650 cp = skip_spaces (cp); |
| 2225 | 4651 name = cp; |
| 709 | 4652 if (search_identifier |
| 4653 && *cp != '\0') | |
| 428 | 4654 { |
| 2225 | 4655 while (!notinname (*cp)) |
| 428 | 4656 cp++; |
| 2225 | 4657 make_tag (name, cp - name, TRUE, |
| 4658 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 709 | 4659 search_identifier = FALSE; |
| 428 | 4660 } |
| 709 | 4661 else if (LOOKING_AT (cp, "function")) |
| 4662 { | |
| 4663 if(*cp == '&') | |
| 4664 cp = skip_spaces (cp+1); | |
| 4665 if(*cp != '\0') | |
| 4666 { | |
| 2225 | 4667 name = cp; |
| 4668 while (!notinname (*cp)) | |
| 709 | 4669 cp++; |
| 2225 | 4670 make_tag (name, cp - name, TRUE, |
| 4671 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 709 | 4672 } |
| 4673 else | |
| 4674 search_identifier = TRUE; | |
| 4675 } | |
| 4676 else if (LOOKING_AT (cp, "class")) | |
| 428 | 4677 { |
| 709 | 4678 if (*cp != '\0') |
| 4679 { | |
| 2225 | 4680 name = cp; |
| 709 | 4681 while (*cp != '\0' && !iswhite (*cp)) |
| 4682 cp++; | |
| 2225 | 4683 make_tag (name, cp - name, FALSE, |
| 4684 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 709 | 4685 } |
| 4686 else | |
| 4687 search_identifier = TRUE; | |
| 4688 } | |
| 4689 else if (strneq (cp, "define", 6) | |
| 4690 && (cp = skip_spaces (cp+6)) | |
| 4691 && *cp++ == '(' | |
| 4692 && (*cp == '"' || *cp == '\'')) | |
| 4693 { | |
| 4694 char quote = *cp++; | |
| 2225 | 4695 name = cp; |
| 709 | 4696 while (*cp != quote && *cp != '\0') |
| 428 | 4697 cp++; |
| 2225 | 4698 make_tag (name, cp - name, FALSE, |
| 4699 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 709 | 4700 } |
| 4701 else if (members | |
| 4702 && LOOKING_AT (cp, "var") | |
| 4703 && *cp == '$') | |
| 4704 { | |
| 2225 | 4705 name = cp; |
| 4706 while (!notinname(*cp)) | |
| 709 | 4707 cp++; |
| 2225 | 4708 make_tag (name, cp - name, FALSE, |
| 4709 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 428 | 4710 } |
| 4711 } | |
| 4712 } | |
| 458 | 4713 |
| 428 | 4714 |
| 2225 | 4715 /* |
| 428 | 4716 * Cobol tag functions |
| 4717 * We could look for anything that could be a paragraph name. | |
| 4718 * i.e. anything that starts in column 8 is one word and ends in a full stop. | |
| 2225 | 4719 * Idea by Corny de Souza (1993) |
| 428 | 4720 */ |
| 442 | 4721 static void |
| 428 | 4722 Cobol_paragraphs (inf) |
| 4723 FILE *inf; | |
| 4724 { | |
| 4725 register char *bp, *ep; | |
| 4726 | |
| 4727 LOOP_ON_INPUT_LINES (inf, lb, bp) | |
| 4728 { | |
| 4729 if (lb.len < 9) | |
| 4730 continue; | |
| 4731 bp += 8; | |
| 4732 | |
| 4733 /* If eoln, compiler option or comment ignore whole line. */ | |
| 458 | 4734 if (bp[-1] != ' ' || !ISALNUM (bp[0])) |
| 428 | 4735 continue; |
| 4736 | |
| 458 | 4737 for (ep = bp; ISALNUM (*ep) || *ep == '-'; ep++) |
| 428 | 4738 continue; |
| 4739 if (*ep++ == '.') | |
| 2225 | 4740 make_tag (bp, ep - bp, TRUE, |
| 4741 lb.buffer, ep - lb.buffer + 1, lineno, linecharno); | |
| 428 | 4742 } |
| 4743 } | |
| 458 | 4744 |
| 4745 | |
| 4746 /* | |
| 4747 * Makefile support | |
| 2225 | 4748 * Ideas by Assar Westerlund <assar@sics.se> (2001) |
| 458 | 4749 */ |
| 4750 static void | |
| 4751 Makefile_targets (inf) | |
| 4752 FILE *inf; | |
| 4753 { | |
| 4754 register char *bp; | |
| 4755 | |
| 4756 LOOP_ON_INPUT_LINES (inf, lb, bp) | |
| 4757 { | |
| 4758 if (*bp == '\t' || *bp == '#') | |
| 4759 continue; | |
| 4760 while (*bp != '\0' && *bp != '=' && *bp != ':') | |
| 4761 bp++; | |
| 2225 | 4762 if (*bp == ':' || (globals && *bp == '=')) |
| 3876 | 4763 { |
| 4764 /* We should detect if there is more than one tag, but we do not. | |
| 4765 We just skip initial and final spaces. */ | |
| 4766 char * namestart = skip_spaces (lb.buffer); | |
| 4767 while (--bp > namestart) | |
| 4768 if (!notinname (*bp)) | |
| 4769 break; | |
| 4770 make_tag (namestart, bp - namestart + 1, TRUE, | |
| 4771 lb.buffer, bp - lb.buffer + 2, lineno, linecharno); | |
| 4772 } | |
| 458 | 4773 } |
| 4774 } | |
| 4775 | |
| 428 | 4776 |
| 4777 /* | |
| 2225 | 4778 * Pascal parsing |
| 4779 * Original code by Mosur K. Mohan (1989) | |
| 4780 * | |
| 428 | 4781 * Locates tags for procedures & functions. Doesn't do any type- or |
| 4782 * var-definitions. It does look for the keyword "extern" or | |
| 4783 * "forward" immediately following the procedure statement; if found, | |
| 4784 * the tag is skipped. | |
| 4785 */ | |
| 442 | 4786 static void |
| 428 | 4787 Pascal_functions (inf) |
| 4788 FILE *inf; | |
| 4789 { | |
| 4790 linebuffer tline; /* mostly copied from C_entries */ | |
| 4791 long save_lcno; | |
| 2225 | 4792 int save_lineno, namelen, taglen; |
| 4793 char c, *name; | |
| 428 | 4794 |
| 4795 bool /* each of these flags is TRUE iff: */ | |
| 4796 incomment, /* point is inside a comment */ | |
| 4797 inquote, /* point is inside '..' string */ | |
| 4798 get_tagname, /* point is after PROCEDURE/FUNCTION | |
| 4799 keyword, so next item = potential tag */ | |
| 4800 found_tag, /* point is after a potential tag */ | |
| 4801 inparms, /* point is within parameter-list */ | |
| 4802 verify_tag; /* point has passed the parm-list, so the | |
| 4803 next token will determine whether this | |
| 4804 is a FORWARD/EXTERN to be ignored, or | |
| 4805 whether it is a real tag */ | |
| 4806 | |
| 2225 | 4807 save_lcno = save_lineno = namelen = taglen = 0; /* keep compiler quiet */ |
| 4808 name = NULL; /* keep compiler quiet */ | |
| 428 | 4809 dbp = lb.buffer; |
| 4810 *dbp = '\0'; | |
| 2225 | 4811 linebuffer_init (&tline); |
| 428 | 4812 |
| 4813 incomment = inquote = FALSE; | |
| 4814 found_tag = FALSE; /* have a proc name; check if extern */ | |
| 2225 | 4815 get_tagname = FALSE; /* found "procedure" keyword */ |
| 428 | 4816 inparms = FALSE; /* found '(' after "proc" */ |
| 4817 verify_tag = FALSE; /* check if "extern" is ahead */ | |
| 4818 | |
| 4819 | |
| 4820 while (!feof (inf)) /* long main loop to get next char */ | |
| 4821 { | |
| 4822 c = *dbp++; | |
| 4823 if (c == '\0') /* if end of line */ | |
| 4824 { | |
| 2225 | 4825 readline (&lb, inf); |
| 428 | 4826 dbp = lb.buffer; |
| 4827 if (*dbp == '\0') | |
| 4828 continue; | |
| 4829 if (!((found_tag && verify_tag) | |
| 4830 || get_tagname)) | |
| 4831 c = *dbp++; /* only if don't need *dbp pointing | |
| 4832 to the beginning of the name of | |
| 4833 the procedure or function */ | |
| 4834 } | |
| 4835 if (incomment) | |
| 4836 { | |
| 4837 if (c == '}') /* within { } comments */ | |
| 4838 incomment = FALSE; | |
| 4839 else if (c == '*' && *dbp == ')') /* within (* *) comments */ | |
| 4840 { | |
| 4841 dbp++; | |
| 4842 incomment = FALSE; | |
| 4843 } | |
| 4844 continue; | |
| 4845 } | |
| 4846 else if (inquote) | |
| 4847 { | |
| 4848 if (c == '\'') | |
| 4849 inquote = FALSE; | |
| 4850 continue; | |
| 4851 } | |
| 4852 else | |
| 4853 switch (c) | |
| 4854 { | |
| 4855 case '\'': | |
| 4856 inquote = TRUE; /* found first quote */ | |
| 4857 continue; | |
| 4858 case '{': /* found open { comment */ | |
| 4859 incomment = TRUE; | |
| 4860 continue; | |
| 4861 case '(': | |
| 4862 if (*dbp == '*') /* found open (* comment */ | |
| 4863 { | |
| 4864 incomment = TRUE; | |
| 4865 dbp++; | |
| 4866 } | |
| 4867 else if (found_tag) /* found '(' after tag, i.e., parm-list */ | |
| 4868 inparms = TRUE; | |
| 4869 continue; | |
| 4870 case ')': /* end of parms list */ | |
| 4871 if (inparms) | |
| 4872 inparms = FALSE; | |
| 4873 continue; | |
| 4874 case ';': | |
| 4875 if (found_tag && !inparms) /* end of proc or fn stmt */ | |
| 4876 { | |
| 4877 verify_tag = TRUE; | |
| 4878 break; | |
| 4879 } | |
| 4880 continue; | |
| 4881 } | |
| 4882 if (found_tag && verify_tag && (*dbp != ' ')) | |
| 4883 { | |
| 2225 | 4884 /* Check if this is an "extern" declaration. */ |
| 428 | 4885 if (*dbp == '\0') |
| 4886 continue; | |
| 4887 if (lowcase (*dbp == 'e')) | |
| 4888 { | |
| 2225 | 4889 if (nocase_tail ("extern")) /* superfluous, really! */ |
| 428 | 4890 { |
| 4891 found_tag = FALSE; | |
| 4892 verify_tag = FALSE; | |
| 4893 } | |
| 4894 } | |
| 4895 else if (lowcase (*dbp) == 'f') | |
| 4896 { | |
| 2225 | 4897 if (nocase_tail ("forward")) /* check for forward reference */ |
| 428 | 4898 { |
| 4899 found_tag = FALSE; | |
| 4900 verify_tag = FALSE; | |
| 4901 } | |
| 4902 } | |
| 4903 if (found_tag && verify_tag) /* not external proc, so make tag */ | |
| 4904 { | |
| 4905 found_tag = FALSE; | |
| 4906 verify_tag = FALSE; | |
| 2225 | 4907 make_tag (name, namelen, TRUE, |
| 4908 tline.buffer, taglen, save_lineno, save_lcno); | |
| 428 | 4909 continue; |
| 4910 } | |
| 4911 } | |
| 4912 if (get_tagname) /* grab name of proc or fn */ | |
| 4913 { | |
| 2225 | 4914 char *cp; |
| 4915 | |
| 428 | 4916 if (*dbp == '\0') |
| 4917 continue; | |
| 4918 | |
| 2225 | 4919 /* Find block name. */ |
| 4920 for (cp = dbp + 1; *cp != '\0' && !endtoken (*cp); cp++) | |
| 4921 continue; | |
| 4922 | |
| 4923 /* Save all values for later tagging. */ | |
| 458 | 4924 linebuffer_setlen (&tline, lb.len); |
| 428 | 4925 strcpy (tline.buffer, lb.buffer); |
| 4926 save_lineno = lineno; | |
| 4927 save_lcno = linecharno; | |
| 2225 | 4928 name = tline.buffer + (dbp - lb.buffer); |
| 4929 namelen = cp - dbp; | |
| 4930 taglen = cp - lb.buffer + 1; | |
| 4931 | |
| 428 | 4932 dbp = cp; /* set dbp to e-o-token */ |
| 4933 get_tagname = FALSE; | |
| 4934 found_tag = TRUE; | |
| 4935 continue; | |
| 4936 | |
| 2225 | 4937 /* And proceed to check for "extern". */ |
| 428 | 4938 } |
| 4939 else if (!incomment && !inquote && !found_tag) | |
| 4940 { | |
| 2225 | 4941 /* Check for proc/fn keywords. */ |
| 428 | 4942 switch (lowcase (c)) |
| 4943 { | |
| 4944 case 'p': | |
| 2225 | 4945 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */ |
| 428 | 4946 get_tagname = TRUE; |
| 4947 continue; | |
| 4948 case 'f': | |
| 2225 | 4949 if (nocase_tail ("unction")) |
| 428 | 4950 get_tagname = TRUE; |
| 4951 continue; | |
| 4952 } | |
| 4953 } | |
| 2225 | 4954 } /* while not eof */ |
| 428 | 4955 |
| 4956 free (tline.buffer); | |
| 4957 } | |
| 458 | 4958 |
| 428 | 4959 |
| 4960 /* | |
| 458 | 4961 * Lisp tag functions |
| 428 | 4962 * look for (def or (DEF, quote or QUOTE |
| 4963 */ | |
| 442 | 4964 |
| 709 | 4965 static void L_getit __P((void)); |
| 442 | 4966 |
| 428 | 4967 static void |
| 4968 L_getit () | |
| 4969 { | |
| 4970 if (*dbp == '\'') /* Skip prefix quote */ | |
| 4971 dbp++; | |
| 4972 else if (*dbp == '(') | |
| 4973 { | |
| 2225 | 4974 dbp++; |
| 4975 /* Try to skip "(quote " */ | |
| 4976 if (!LOOKING_AT (dbp, "quote") && !LOOKING_AT (dbp, "QUOTE")) | |
| 4977 /* Ok, then skip "(" before name in (defstruct (foo)) */ | |
| 4978 dbp = skip_spaces (dbp); | |
| 428 | 4979 } |
| 2225 | 4980 get_tag (dbp, NULL); |
| 428 | 4981 } |
| 4982 | |
| 442 | 4983 static void |
| 428 | 4984 Lisp_functions (inf) |
| 4985 FILE *inf; | |
| 4986 { | |
| 4987 LOOP_ON_INPUT_LINES (inf, lb, dbp) | |
| 4988 { | |
| 2225 | 4989 if (dbp[0] != '(') |
| 4990 continue; | |
| 4991 | |
| 4992 if (strneq (dbp+1, "def", 3) || strneq (dbp+1, "DEF", 3)) | |
| 428 | 4993 { |
| 2225 | 4994 dbp = skip_non_spaces (dbp); |
| 4995 dbp = skip_spaces (dbp); | |
| 4996 L_getit (); | |
| 4997 } | |
| 4998 else | |
| 4999 { | |
| 5000 /* Check for (foo::defmumble name-defined ... */ | |
| 5001 do | |
| 5002 dbp++; | |
| 5003 while (!notinname (*dbp) && *dbp != ':'); | |
| 5004 if (*dbp == ':') | |
| 428 | 5005 { |
| 5006 do | |
| 5007 dbp++; | |
| 2225 | 5008 while (*dbp == ':'); |
| 5009 | |
| 5010 if (strneq (dbp, "def", 3) || strneq (dbp, "DEF", 3)) | |
| 428 | 5011 { |
| 2225 | 5012 dbp = skip_non_spaces (dbp); |
| 5013 dbp = skip_spaces (dbp); | |
| 5014 L_getit (); | |
| 428 | 5015 } |
| 5016 } | |
| 5017 } | |
| 5018 } | |
| 5019 } | |
| 458 | 5020 |
| 428 | 5021 |
| 5022 /* | |
| 2325 | 5023 * Lua script language parsing |
| 5024 * Original code by David A. Capello <dacap@users.sourceforge.net> (2004) | |
| 5025 * | |
| 5026 * "function" and "local function" are tags if they start at column 1. | |
| 5027 */ | |
| 5028 static void | |
| 5029 Lua_functions (inf) | |
| 5030 FILE *inf; | |
| 5031 { | |
| 5032 register char *bp; | |
| 5033 | |
| 5034 LOOP_ON_INPUT_LINES (inf, lb, bp) | |
| 5035 { | |
| 5036 if (bp[0] != 'f' && bp[0] != 'l') | |
| 5037 continue; | |
| 5038 | |
| 3876 | 5039 (void)LOOKING_AT (bp, "local"); /* skip possible "local" */ |
| 2325 | 5040 |
| 5041 if (LOOKING_AT (bp, "function")) | |
| 5042 get_tag (bp, NULL); | |
| 5043 } | |
| 5044 } | |
| 5045 | |
| 5046 | |
| 5047 /* | |
| 2554 | 5048 * Postscript tags |
| 428 | 5049 * Just look for lines where the first character is '/' |
| 5050 * Also look at "defineps" for PSWrap | |
| 2225 | 5051 * Ideas by: |
| 5052 * Richard Mlynarik <mly@adoc.xerox.com> (1997) | |
| 5053 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999) | |
| 428 | 5054 */ |
| 442 | 5055 static void |
| 2225 | 5056 PS_functions (inf) |
| 428 | 5057 FILE *inf; |
| 5058 { | |
| 5059 register char *bp, *ep; | |
| 5060 | |
| 5061 LOOP_ON_INPUT_LINES (inf, lb, bp) | |
| 5062 { | |
| 5063 if (bp[0] == '/') | |
| 5064 { | |
| 5065 for (ep = bp+1; | |
| 5066 *ep != '\0' && *ep != ' ' && *ep != '{'; | |
| 5067 ep++) | |
| 5068 continue; | |
| 2225 | 5069 make_tag (bp, ep - bp, TRUE, |
| 5070 lb.buffer, ep - lb.buffer + 1, lineno, linecharno); | |
| 428 | 5071 } |
| 2225 | 5072 else if (LOOKING_AT (bp, "defineps")) |
| 5073 get_tag (bp, NULL); | |
| 428 | 5074 } |
| 5075 } | |
| 5076 | |
| 5077 | |
| 5078 /* | |
| 2554 | 5079 * Forth tags |
| 5080 * Ignore anything after \ followed by space or in ( ) | |
| 5081 * Look for words defined by : | |
| 5082 * Look for constant, code, create, defer, value, and variable | |
| 5083 * OBP extensions: Look for buffer:, field, | |
| 5084 * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004) | |
| 5085 */ | |
| 5086 static void | |
| 5087 Forth_words (inf) | |
| 5088 FILE *inf; | |
| 5089 { | |
| 5090 register char *bp; | |
| 5091 | |
| 5092 LOOP_ON_INPUT_LINES (inf, lb, bp) | |
| 5093 while ((bp = skip_spaces (bp))[0] != '\0') | |
| 5094 if (bp[0] == '\\' && iswhite(bp[1])) | |
| 5095 break; /* read next line */ | |
| 5096 else if (bp[0] == '(' && iswhite(bp[1])) | |
| 5097 do /* skip to ) or eol */ | |
| 5098 bp++; | |
| 5099 while (*bp != ')' && *bp != '\0'); | |
| 5100 else if ((bp[0] == ':' && iswhite(bp[1]) && bp++) | |
| 5101 || LOOKING_AT_NOCASE (bp, "constant") | |
| 5102 || LOOKING_AT_NOCASE (bp, "code") | |
| 5103 || LOOKING_AT_NOCASE (bp, "create") | |
| 5104 || LOOKING_AT_NOCASE (bp, "defer") | |
| 5105 || LOOKING_AT_NOCASE (bp, "value") | |
| 5106 || LOOKING_AT_NOCASE (bp, "variable") | |
| 5107 || LOOKING_AT_NOCASE (bp, "buffer:") | |
| 5108 || LOOKING_AT_NOCASE (bp, "field")) | |
| 5109 get_tag (skip_spaces (bp), NULL); /* Yay! A definition! */ | |
| 5110 else | |
| 5111 bp = skip_non_spaces (bp); | |
| 5112 } | |
| 5113 | |
| 5114 | |
| 5115 /* | |
| 428 | 5116 * Scheme tag functions |
| 5117 * look for (def... xyzzy | |
| 2225 | 5118 * (def... (xyzzy |
| 5119 * (def ... ((...(xyzzy .... | |
| 5120 * (set! xyzzy | |
| 5121 * Original code by Ken Haase (1985?) | |
| 428 | 5122 */ |
| 442 | 5123 static void |
| 428 | 5124 Scheme_functions (inf) |
| 5125 FILE *inf; | |
| 5126 { | |
| 5127 register char *bp; | |
| 5128 | |
| 5129 LOOP_ON_INPUT_LINES (inf, lb, bp) | |
| 5130 { | |
| 2225 | 5131 if (strneq (bp, "(def", 4) || strneq (bp, "(DEF", 4)) |
| 428 | 5132 { |
| 2225 | 5133 bp = skip_non_spaces (bp+4); |
| 428 | 5134 /* Skip over open parens and white space */ |
| 2225 | 5135 while (notinname (*bp)) |
| 428 | 5136 bp++; |
| 2225 | 5137 get_tag (bp, NULL); |
| 428 | 5138 } |
| 709 | 5139 if (LOOKING_AT (bp, "(SET!") || LOOKING_AT (bp, "(set!")) |
| 2225 | 5140 get_tag (bp, NULL); |
| 428 | 5141 } |
| 5142 } | |
| 458 | 5143 |
| 428 | 5144 |
| 5145 /* Find tags in TeX and LaTeX input files. */ | |
| 5146 | |
| 5147 /* TEX_toktab is a table of TeX control sequences that define tags. | |
| 2225 | 5148 * Each entry records one such control sequence. |
| 5149 * | |
| 5150 * Original code from who knows whom. | |
| 5151 * Ideas by: | |
| 5152 * Stefan Monnier (2002) | |
| 5153 */ | |
| 5154 | |
| 5155 static linebuffer *TEX_toktab = NULL; /* Table with tag tokens */ | |
| 428 | 5156 |
| 5157 /* Default set of control sequences to put into TEX_toktab. | |
| 5158 The value of environment var TEXTAGS is prepended to this. */ | |
| 2225 | 5159 static char *TEX_defenv = "\ |
| 428 | 5160 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\ |
| 2225 | 5161 :part:appendix:entry:index:def\ |
| 5162 :newcommand:renewcommand:newenvironment:renewenvironment"; | |
| 428 | 5163 |
| 709 | 5164 static void TEX_mode __P((FILE *)); |
| 2225 | 5165 static void TEX_decode_env __P((char *, char *)); |
| 5166 | |
| 5167 static char TEX_esc = '\\'; | |
| 5168 static char TEX_opgrp = '{'; | |
| 5169 static char TEX_clgrp = '}'; | |
| 428 | 5170 |
| 5171 /* | |
| 5172 * TeX/LaTeX scanning loop. | |
| 5173 */ | |
| 442 | 5174 static void |
| 458 | 5175 TeX_commands (inf) |
| 428 | 5176 FILE *inf; |
| 5177 { | |
| 2225 | 5178 char *cp; |
| 5179 linebuffer *key; | |
| 428 | 5180 |
| 5181 /* Select either \ or ! as escape character. */ | |
| 5182 TEX_mode (inf); | |
| 5183 | |
| 5184 /* Initialize token table once from environment. */ | |
| 2225 | 5185 if (TEX_toktab == NULL) |
| 5186 TEX_decode_env ("TEXTAGS", TEX_defenv); | |
| 428 | 5187 |
| 5188 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 5189 { | |
| 2225 | 5190 /* Look at each TEX keyword in line. */ |
| 5191 for (;;) | |
| 428 | 5192 { |
| 2225 | 5193 /* Look for a TEX escape. */ |
| 5194 while (*cp++ != TEX_esc) | |
| 5195 if (cp[-1] == '\0' || cp[-1] == '%') | |
| 5196 goto tex_next_line; | |
| 5197 | |
| 5198 for (key = TEX_toktab; key->buffer != NULL; key++) | |
| 5199 if (strneq (cp, key->buffer, key->len)) | |
| 5200 { | |
| 5201 register char *p; | |
| 5202 int namelen, linelen; | |
| 5203 bool opgrp = FALSE; | |
| 5204 | |
| 5205 cp = skip_spaces (cp + key->len); | |
| 5206 if (*cp == TEX_opgrp) | |
| 5207 { | |
| 5208 opgrp = TRUE; | |
| 5209 cp++; | |
| 5210 } | |
| 5211 for (p = cp; | |
| 5212 (!iswhite (*p) && *p != '#' && | |
| 5213 *p != TEX_opgrp && *p != TEX_clgrp); | |
| 5214 p++) | |
| 5215 continue; | |
| 5216 namelen = p - cp; | |
| 5217 linelen = lb.len; | |
| 5218 if (!opgrp || *p == TEX_clgrp) | |
| 5219 { | |
| 5220 while (*p != '\0' && *p != TEX_opgrp && *p != TEX_clgrp) | |
| 3876 | 5221 p++; |
| 2225 | 5222 linelen = p - lb.buffer + 1; |
| 5223 } | |
| 5224 make_tag (cp, namelen, TRUE, | |
| 5225 lb.buffer, linelen, lineno, linecharno); | |
| 5226 goto tex_next_line; /* We only tag a line once */ | |
| 5227 } | |
| 428 | 5228 } |
| 2225 | 5229 tex_next_line: |
| 5230 ; | |
| 428 | 5231 } |
| 5232 } | |
| 5233 | |
| 5234 #define TEX_LESC '\\' | |
| 5235 #define TEX_SESC '!' | |
| 5236 | |
| 5237 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping | |
| 5238 chars accordingly. */ | |
| 442 | 5239 static void |
| 428 | 5240 TEX_mode (inf) |
| 5241 FILE *inf; | |
| 5242 { | |
| 5243 int c; | |
| 5244 | |
| 5245 while ((c = getc (inf)) != EOF) | |
| 5246 { | |
| 5247 /* Skip to next line if we hit the TeX comment char. */ | |
| 2225 | 5248 if (c == '%') |
| 3876 | 5249 while (c != '\n' && c != EOF) |
| 428 | 5250 c = getc (inf); |
| 5251 else if (c == TEX_LESC || c == TEX_SESC ) | |
| 5252 break; | |
| 5253 } | |
| 5254 | |
| 5255 if (c == TEX_LESC) | |
| 5256 { | |
| 5257 TEX_esc = TEX_LESC; | |
| 5258 TEX_opgrp = '{'; | |
| 5259 TEX_clgrp = '}'; | |
| 5260 } | |
| 5261 else | |
| 5262 { | |
| 5263 TEX_esc = TEX_SESC; | |
| 5264 TEX_opgrp = '<'; | |
| 5265 TEX_clgrp = '>'; | |
| 5266 } | |
| 5267 /* If the input file is compressed, inf is a pipe, and rewind may fail. | |
| 5268 No attempt is made to correct the situation. */ | |
| 5269 rewind (inf); | |
| 5270 } | |
| 5271 | |
| 5272 /* Read environment and prepend it to the default string. | |
| 5273 Build token table. */ | |
| 2225 | 5274 static void |
| 428 | 5275 TEX_decode_env (evarname, defenv) |
| 5276 char *evarname; | |
| 5277 char *defenv; | |
| 5278 { | |
| 5279 register char *env, *p; | |
| 2225 | 5280 int i, len; |
| 428 | 5281 |
| 5282 /* Append default string to environment. */ | |
| 5283 env = getenv (evarname); | |
| 5284 if (!env) | |
| 5285 env = defenv; | |
| 5286 else | |
| 5287 { | |
| 5288 char *oldenv = env; | |
| 5289 env = concat (oldenv, defenv, ""); | |
| 5290 } | |
| 5291 | |
| 5292 /* Allocate a token table */ | |
| 2225 | 5293 for (len = 1, p = env; p;) |
| 428 | 5294 if ((p = etags_strchr (p, ':')) && *++p != '\0') |
| 2225 | 5295 len++; |
| 5296 TEX_toktab = xnew (len, linebuffer); | |
| 428 | 5297 |
| 5298 /* Unpack environment string into token table. Be careful about */ | |
| 5299 /* zero-length strings (leading ':', "::" and trailing ':') */ | |
| 2225 | 5300 for (i = 0; *env != '\0';) |
| 428 | 5301 { |
| 5302 p = etags_strchr (env, ':'); | |
| 5303 if (!p) /* End of environment string. */ | |
| 5304 p = env + strlen (env); | |
| 5305 if (p - env > 0) | |
| 5306 { /* Only non-zero strings. */ | |
| 2225 | 5307 TEX_toktab[i].buffer = savenstr (env, p - env); |
| 5308 TEX_toktab[i].len = p - env; | |
| 428 | 5309 i++; |
| 5310 } | |
| 5311 if (*p) | |
| 5312 env = p + 1; | |
| 5313 else | |
| 5314 { | |
| 2225 | 5315 TEX_toktab[i].buffer = NULL; /* Mark end of table. */ |
| 5316 TEX_toktab[i].len = 0; | |
| 428 | 5317 break; |
| 5318 } | |
| 5319 } | |
| 5320 } | |
| 458 | 5321 |
| 5322 | |
| 5323 /* Texinfo support. Dave Love, Mar. 2000. */ | |
| 5324 static void | |
| 5325 Texinfo_nodes (inf) | |
| 5326 FILE * inf; | |
| 5327 { | |
| 5328 char *cp, *start; | |
| 5329 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 709 | 5330 if (LOOKING_AT (cp, "@node")) |
| 5331 { | |
| 5332 start = cp; | |
| 5333 while (*cp != '\0' && *cp != ',') | |
| 5334 cp++; | |
| 2225 | 5335 make_tag (start, cp - start, TRUE, |
| 5336 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 5337 } | |
| 5338 } | |
| 5339 | |
| 5340 | |
| 5341 /* | |
| 5342 * HTML support. | |
| 5343 * Contents of <title>, <h1>, <h2>, <h3> are tags. | |
| 5344 * Contents of <a name=xxx> are tags with name xxx. | |
| 5345 * | |
| 5346 * Francesco Potort́, 2002. | |
| 5347 */ | |
| 5348 static void | |
| 5349 HTML_labels (inf) | |
| 5350 FILE * inf; | |
| 5351 { | |
| 5352 bool getnext = FALSE; /* next text outside of HTML tags is a tag */ | |
| 5353 bool skiptag = FALSE; /* skip to the end of the current HTML tag */ | |
| 5354 bool intag = FALSE; /* inside an html tag, looking for ID= */ | |
| 5355 bool inanchor = FALSE; /* when INTAG, is an anchor, look for NAME= */ | |
| 5356 char *end; | |
| 5357 | |
| 5358 | |
| 5359 linebuffer_setlen (&token_name, 0); /* no name in buffer */ | |
| 5360 | |
| 5361 LOOP_ON_INPUT_LINES (inf, lb, dbp) | |
| 5362 for (;;) /* loop on the same line */ | |
| 5363 { | |
| 5364 if (skiptag) /* skip HTML tag */ | |
| 5365 { | |
| 5366 while (*dbp != '\0' && *dbp != '>') | |
| 5367 dbp++; | |
| 5368 if (*dbp == '>') | |
| 5369 { | |
| 5370 dbp += 1; | |
| 5371 skiptag = FALSE; | |
| 5372 continue; /* look on the same line */ | |
| 5373 } | |
| 5374 break; /* go to next line */ | |
| 5375 } | |
| 5376 | |
| 5377 else if (intag) /* look for "name=" or "id=" */ | |
| 5378 { | |
| 5379 while (*dbp != '\0' && *dbp != '>' | |
| 5380 && lowcase (*dbp) != 'n' && lowcase (*dbp) != 'i') | |
| 5381 dbp++; | |
| 5382 if (*dbp == '\0') | |
| 5383 break; /* go to next line */ | |
| 5384 if (*dbp == '>') | |
| 5385 { | |
| 5386 dbp += 1; | |
| 5387 intag = FALSE; | |
| 5388 continue; /* look on the same line */ | |
| 5389 } | |
| 5390 if ((inanchor && LOOKING_AT_NOCASE (dbp, "name=")) | |
| 5391 || LOOKING_AT_NOCASE (dbp, "id=")) | |
| 5392 { | |
| 5393 bool quoted = (dbp[0] == '"'); | |
| 5394 | |
| 5395 if (quoted) | |
| 5396 for (end = ++dbp; *end != '\0' && *end != '"'; end++) | |
| 5397 continue; | |
| 5398 else | |
| 5399 for (end = dbp; *end != '\0' && intoken (*end); end++) | |
| 5400 continue; | |
| 5401 linebuffer_setlen (&token_name, end - dbp); | |
| 5402 strncpy (token_name.buffer, dbp, end - dbp); | |
| 5403 token_name.buffer[end - dbp] = '\0'; | |
| 5404 | |
| 5405 dbp = end; | |
| 5406 intag = FALSE; /* we found what we looked for */ | |
| 5407 skiptag = TRUE; /* skip to the end of the tag */ | |
| 5408 getnext = TRUE; /* then grab the text */ | |
| 5409 continue; /* look on the same line */ | |
| 5410 } | |
| 5411 dbp += 1; | |
| 5412 } | |
| 5413 | |
| 5414 else if (getnext) /* grab next tokens and tag them */ | |
| 5415 { | |
| 5416 dbp = skip_spaces (dbp); | |
| 5417 if (*dbp == '\0') | |
| 5418 break; /* go to next line */ | |
| 5419 if (*dbp == '<') | |
| 5420 { | |
| 5421 intag = TRUE; | |
| 5422 inanchor = (lowcase (dbp[1]) == 'a' && !intoken (dbp[2])); | |
| 5423 continue; /* look on the same line */ | |
| 5424 } | |
| 5425 | |
| 5426 for (end = dbp + 1; *end != '\0' && *end != '<'; end++) | |
| 5427 continue; | |
| 5428 make_tag (token_name.buffer, token_name.len, TRUE, | |
| 5429 dbp, end - dbp, lineno, linecharno); | |
| 5430 linebuffer_setlen (&token_name, 0); /* no name in buffer */ | |
| 5431 getnext = FALSE; | |
| 5432 break; /* go to next line */ | |
| 5433 } | |
| 5434 | |
| 5435 else /* look for an interesting HTML tag */ | |
| 5436 { | |
| 5437 while (*dbp != '\0' && *dbp != '<') | |
| 5438 dbp++; | |
| 5439 if (*dbp == '\0') | |
| 5440 break; /* go to next line */ | |
| 5441 intag = TRUE; | |
| 5442 if (lowcase (dbp[1]) == 'a' && !intoken (dbp[2])) | |
| 5443 { | |
| 5444 inanchor = TRUE; | |
| 5445 continue; /* look on the same line */ | |
| 5446 } | |
| 5447 else if (LOOKING_AT_NOCASE (dbp, "<title>") | |
| 5448 || LOOKING_AT_NOCASE (dbp, "<h1>") | |
| 5449 || LOOKING_AT_NOCASE (dbp, "<h2>") | |
| 5450 || LOOKING_AT_NOCASE (dbp, "<h3>")) | |
| 5451 { | |
| 5452 intag = FALSE; | |
| 5453 getnext = TRUE; | |
| 5454 continue; /* look on the same line */ | |
| 5455 } | |
| 5456 dbp += 1; | |
| 5457 } | |
| 709 | 5458 } |
| 458 | 5459 } |
| 5460 | |
| 428 | 5461 |
| 5462 /* | |
| 2225 | 5463 * Prolog support |
| 428 | 5464 * |
| 709 | 5465 * Assumes that the predicate or rule starts at column 0. |
| 5466 * Only the first clause of a predicate or rule is added. | |
| 2225 | 5467 * Original code by Sunichirou Sugou (1989) |
| 5468 * Rewritten by Anders Lindgren (1996) | |
| 428 | 5469 */ |
| 709 | 5470 static int prolog_pr __P((char *, char *)); |
| 5471 static void prolog_skip_comment __P((linebuffer *, FILE *)); | |
| 5472 static int prolog_atom __P((char *, int)); | |
| 442 | 5473 |
| 5474 static void | |
| 428 | 5475 Prolog_functions (inf) |
| 5476 FILE *inf; | |
| 5477 { | |
| 5478 char *cp, *last; | |
| 5479 int len; | |
| 5480 int allocated; | |
| 5481 | |
| 5482 allocated = 0; | |
| 5483 len = 0; | |
| 5484 last = NULL; | |
| 5485 | |
| 5486 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 5487 { | |
| 5488 if (cp[0] == '\0') /* Empty line */ | |
| 5489 continue; | |
| 442 | 5490 else if (iswhite (cp[0])) /* Not a predicate */ |
| 428 | 5491 continue; |
| 5492 else if (cp[0] == '/' && cp[1] == '*') /* comment. */ | |
| 5493 prolog_skip_comment (&lb, inf); | |
| 709 | 5494 else if ((len = prolog_pr (cp, last)) > 0) |
| 428 | 5495 { |
| 709 | 5496 /* Predicate or rule. Store the function name so that we |
| 5497 only generate a tag for the first clause. */ | |
| 428 | 5498 if (last == NULL) |
| 5499 last = xnew(len + 1, char); | |
| 5500 else if (len + 1 > allocated) | |
| 458 | 5501 xrnew (last, len + 1, char); |
| 428 | 5502 allocated = len + 1; |
| 5503 strncpy (last, cp, len); | |
| 5504 last[len] = '\0'; | |
| 5505 } | |
| 5506 } | |
| 3517 | 5507 if (last != NULL) |
| 5508 free (last); | |
| 428 | 5509 } |
| 5510 | |
| 5511 | |
| 442 | 5512 static void |
| 428 | 5513 prolog_skip_comment (plb, inf) |
| 5514 linebuffer *plb; | |
| 5515 FILE *inf; | |
| 5516 { | |
| 5517 char *cp; | |
| 5518 | |
| 5519 do | |
| 5520 { | |
| 5521 for (cp = plb->buffer; *cp != '\0'; cp++) | |
| 5522 if (cp[0] == '*' && cp[1] == '/') | |
| 5523 return; | |
| 2225 | 5524 readline (plb, inf); |
| 428 | 5525 } |
| 5526 while (!feof(inf)); | |
| 5527 } | |
| 5528 | |
| 5529 /* | |
| 709 | 5530 * A predicate or rule definition is added if it matches: |
| 428 | 5531 * <beginning of line><Prolog Atom><whitespace>( |
| 709 | 5532 * or <beginning of line><Prolog Atom><whitespace>:- |
| 428 | 5533 * |
| 5534 * It is added to the tags database if it doesn't match the | |
| 5535 * name of the previous clause header. | |
| 5536 * | |
| 709 | 5537 * Return the size of the name of the predicate or rule, or 0 if no |
| 5538 * header was found. | |
| 428 | 5539 */ |
| 442 | 5540 static int |
| 709 | 5541 prolog_pr (s, last) |
| 428 | 5542 char *s; |
| 5543 char *last; /* Name of last clause. */ | |
| 5544 { | |
| 5545 int pos; | |
| 5546 int len; | |
| 5547 | |
| 5548 pos = prolog_atom (s, 0); | |
| 5549 if (pos < 1) | |
| 5550 return 0; | |
| 5551 | |
| 5552 len = pos; | |
| 5553 pos = skip_spaces (s + pos) - s; | |
| 5554 | |
| 709 | 5555 if ((s[pos] == '.' |
| 5556 || (s[pos] == '(' && (pos += 1)) | |
| 5557 || (s[pos] == ':' && s[pos + 1] == '-' && (pos += 2))) | |
| 5558 && (last == NULL /* save only the first clause */ | |
| 2325 | 5559 || len != (int)strlen (last) |
| 709 | 5560 || !strneq (s, last, len))) |
| 428 | 5561 { |
| 2225 | 5562 make_tag (s, len, TRUE, s, pos, lineno, linecharno); |
| 428 | 5563 return len; |
| 5564 } | |
| 709 | 5565 else |
| 5566 return 0; | |
| 428 | 5567 } |
| 5568 | |
| 5569 /* | |
| 5570 * Consume a Prolog atom. | |
| 5571 * Return the number of bytes consumed, or -1 if there was an error. | |
| 5572 * | |
| 5573 * A prolog atom, in this context, could be one of: | |
| 5574 * - An alphanumeric sequence, starting with a lower case letter. | |
| 5575 * - A quoted arbitrary string. Single quotes can escape themselves. | |
| 5576 * Backslash quotes everything. | |
| 5577 */ | |
| 442 | 5578 static int |
| 428 | 5579 prolog_atom (s, pos) |
| 5580 char *s; | |
| 5581 int pos; | |
| 5582 { | |
| 5583 int origpos; | |
| 5584 | |
| 5585 origpos = pos; | |
| 5586 | |
| 458 | 5587 if (ISLOWER(s[pos]) || (s[pos] == '_')) |
| 428 | 5588 { |
| 5589 /* The atom is unquoted. */ | |
| 5590 pos++; | |
| 458 | 5591 while (ISALNUM(s[pos]) || (s[pos] == '_')) |
| 428 | 5592 { |
| 5593 pos++; | |
| 5594 } | |
| 5595 return pos - origpos; | |
| 5596 } | |
| 5597 else if (s[pos] == '\'') | |
| 5598 { | |
| 5599 pos++; | |
| 5600 | |
| 2225 | 5601 for (;;) |
| 428 | 5602 { |
| 5603 if (s[pos] == '\'') | |
| 5604 { | |
| 5605 pos++; | |
| 5606 if (s[pos] != '\'') | |
| 5607 break; | |
| 5608 pos++; /* A double quote */ | |
| 5609 } | |
| 5610 else if (s[pos] == '\0') | |
| 5611 /* Multiline quoted atoms are ignored. */ | |
| 5612 return -1; | |
| 5613 else if (s[pos] == '\\') | |
| 5614 { | |
| 5615 if (s[pos+1] == '\0') | |
| 5616 return -1; | |
| 5617 pos += 2; | |
| 5618 } | |
| 5619 else | |
| 5620 pos++; | |
| 5621 } | |
| 5622 return pos - origpos; | |
| 5623 } | |
| 5624 else | |
| 5625 return -1; | |
| 5626 } | |
| 458 | 5627 |
| 428 | 5628 |
| 5629 /* | |
| 2225 | 5630 * Support for Erlang |
| 428 | 5631 * |
| 5632 * Generates tags for functions, defines, and records. | |
| 5633 * Assumes that Erlang functions start at column 0. | |
| 2225 | 5634 * Original code by Anders Lindgren (1996) |
| 428 | 5635 */ |
| 709 | 5636 static int erlang_func __P((char *, char *)); |
| 5637 static void erlang_attribute __P((char *)); | |
| 2225 | 5638 static int erlang_atom __P((char *)); |
| 442 | 5639 |
| 5640 static void | |
| 428 | 5641 Erlang_functions (inf) |
| 5642 FILE *inf; | |
| 5643 { | |
| 5644 char *cp, *last; | |
| 5645 int len; | |
| 5646 int allocated; | |
| 5647 | |
| 5648 allocated = 0; | |
| 5649 len = 0; | |
| 5650 last = NULL; | |
| 5651 | |
| 5652 LOOP_ON_INPUT_LINES (inf, lb, cp) | |
| 5653 { | |
| 5654 if (cp[0] == '\0') /* Empty line */ | |
| 5655 continue; | |
| 442 | 5656 else if (iswhite (cp[0])) /* Not function nor attribute */ |
| 428 | 5657 continue; |
| 5658 else if (cp[0] == '%') /* comment */ | |
| 5659 continue; | |
| 5660 else if (cp[0] == '"') /* Sometimes, strings start in column one */ | |
| 5661 continue; | |
| 5662 else if (cp[0] == '-') /* attribute, e.g. "-define" */ | |
| 5663 { | |
| 5664 erlang_attribute (cp); | |
| 3517 | 5665 if (last != NULL) |
| 5666 { | |
| 5667 free (last); | |
| 5668 last = NULL; | |
| 5669 } | |
| 428 | 5670 } |
| 5671 else if ((len = erlang_func (cp, last)) > 0) | |
| 5672 { | |
| 5673 /* | |
| 5674 * Function. Store the function name so that we only | |
| 5675 * generates a tag for the first clause. | |
| 5676 */ | |
| 5677 if (last == NULL) | |
| 5678 last = xnew (len + 1, char); | |
| 5679 else if (len + 1 > allocated) | |
| 458 | 5680 xrnew (last, len + 1, char); |
| 428 | 5681 allocated = len + 1; |
| 5682 strncpy (last, cp, len); | |
| 5683 last[len] = '\0'; | |
| 5684 } | |
| 5685 } | |
| 3517 | 5686 if (last != NULL) |
| 5687 free (last); | |
| 428 | 5688 } |
| 5689 | |
| 5690 | |
| 5691 /* | |
| 5692 * A function definition is added if it matches: | |
| 5693 * <beginning of line><Erlang Atom><whitespace>( | |
| 5694 * | |
| 5695 * It is added to the tags database if it doesn't match the | |
| 5696 * name of the previous clause header. | |
| 5697 * | |
| 5698 * Return the size of the name of the function, or 0 if no function | |
| 5699 * was found. | |
| 5700 */ | |
| 442 | 5701 static int |
| 428 | 5702 erlang_func (s, last) |
| 5703 char *s; | |
| 5704 char *last; /* Name of last clause. */ | |
| 5705 { | |
| 5706 int pos; | |
| 5707 int len; | |
| 5708 | |
| 2225 | 5709 pos = erlang_atom (s); |
| 428 | 5710 if (pos < 1) |
| 5711 return 0; | |
| 5712 | |
| 5713 len = pos; | |
| 5714 pos = skip_spaces (s + pos) - s; | |
| 5715 | |
| 5716 /* Save only the first clause. */ | |
| 5717 if (s[pos++] == '(' | |
| 5718 && (last == NULL | |
| 5719 || len != (int)strlen (last) | |
| 5720 || !strneq (s, last, len))) | |
| 5721 { | |
| 2225 | 5722 make_tag (s, len, TRUE, s, pos, lineno, linecharno); |
| 428 | 5723 return len; |
| 5724 } | |
| 5725 | |
| 5726 return 0; | |
| 5727 } | |
| 5728 | |
| 5729 | |
| 5730 /* | |
| 5731 * Handle attributes. Currently, tags are generated for defines | |
| 5732 * and records. | |
| 5733 * | |
| 5734 * They are on the form: | |
| 5735 * -define(foo, bar). | |
| 5736 * -define(Foo(M, N), M+N). | |
| 5737 * -record(graph, {vtab = notable, cyclic = true}). | |
| 5738 */ | |
| 442 | 5739 static void |
| 428 | 5740 erlang_attribute (s) |
| 5741 char *s; | |
| 5742 { | |
| 2225 | 5743 char *cp = s; |
| 5744 | |
| 5745 if ((LOOKING_AT (cp, "-define") || LOOKING_AT (cp, "-record")) | |
| 5746 && *cp++ == '(') | |
| 428 | 5747 { |
| 2225 | 5748 int len = erlang_atom (skip_spaces (cp)); |
| 5749 if (len > 0) | |
| 5750 make_tag (cp, len, TRUE, s, cp + len - s, lineno, linecharno); | |
| 428 | 5751 } |
| 5752 return; | |
| 5753 } | |
| 5754 | |
| 5755 | |
| 5756 /* | |
| 5757 * Consume an Erlang atom (or variable). | |
| 5758 * Return the number of bytes consumed, or -1 if there was an error. | |
| 5759 */ | |
| 442 | 5760 static int |
| 2225 | 5761 erlang_atom (s) |
| 428 | 5762 char *s; |
| 5763 { | |
| 2225 | 5764 int pos = 0; |
| 428 | 5765 |
| 458 | 5766 if (ISALPHA (s[pos]) || s[pos] == '_') |
| 428 | 5767 { |
| 5768 /* The atom is unquoted. */ | |
| 2225 | 5769 do |
| 428 | 5770 pos++; |
| 2225 | 5771 while (ISALNUM (s[pos]) || s[pos] == '_'); |
| 428 | 5772 } |
| 5773 else if (s[pos] == '\'') | |
| 5774 { | |
| 2225 | 5775 for (pos++; s[pos] != '\''; pos++) |
| 5776 if (s[pos] == '\0' /* multiline quoted atoms are ignored */ | |
| 5777 || (s[pos] == '\\' && s[++pos] == '\0')) | |
| 5778 return 0; | |
| 428 | 5779 pos++; |
| 5780 } | |
| 2225 | 5781 |
| 5782 return pos; | |
| 428 | 5783 } |
| 458 | 5784 |
| 428 | 5785 |
| 709 | 5786 static char *scan_separators __P((char *)); |
| 2225 | 5787 static void add_regex __P((char *, language *)); |
| 709 | 5788 static char *substitute __P((char *, char *, struct re_registers *)); |
| 442 | 5789 |
| 2225 | 5790 /* |
| 5791 * Take a string like "/blah/" and turn it into "blah", verifying | |
| 5792 * that the first and last characters are the same, and handling | |
| 5793 * quoted separator characters. Actually, stops on the occurrence of | |
| 5794 * an unquoted separator. Also process \t, \n, etc. and turn into | |
| 5795 * appropriate characters. Works in place. Null terminates name string. | |
| 5796 * Returns pointer to terminating separator, or NULL for | |
| 5797 * unterminated regexps. | |
| 5798 */ | |
| 428 | 5799 static char * |
| 5800 scan_separators (name) | |
| 5801 char *name; | |
| 5802 { | |
| 5803 char sep = name[0]; | |
| 5804 char *copyto = name; | |
| 5805 bool quoted = FALSE; | |
| 5806 | |
| 5807 for (++name; *name != '\0'; ++name) | |
| 5808 { | |
| 5809 if (quoted) | |
| 5810 { | |
| 2225 | 5811 switch (*name) |
| 428 | 5812 { |
| 2225 | 5813 case 'a': *copyto++ = '\007'; break; /* BEL (bell) */ |
| 5814 case 'b': *copyto++ = '\b'; break; /* BS (back space) */ | |
| 5815 case 'd': *copyto++ = 0177; break; /* DEL (delete) */ | |
| 5816 case 'e': *copyto++ = 033; break; /* ESC (delete) */ | |
| 5817 case 'f': *copyto++ = '\f'; break; /* FF (form feed) */ | |
| 5818 case 'n': *copyto++ = '\n'; break; /* NL (new line) */ | |
| 5819 case 'r': *copyto++ = '\r'; break; /* CR (carriage return) */ | |
| 5820 case 't': *copyto++ = '\t'; break; /* TAB (horizontal tab) */ | |
| 5821 case 'v': *copyto++ = '\v'; break; /* VT (vertical tab) */ | |
| 5822 default: | |
| 5823 if (*name == sep) | |
| 5824 *copyto++ = sep; | |
| 5825 else | |
| 5826 { | |
| 5827 /* Something else is quoted, so preserve the quote. */ | |
| 5828 *copyto++ = '\\'; | |
| 5829 *copyto++ = *name; | |
| 5830 } | |
| 5831 break; | |
| 428 | 5832 } |
| 5833 quoted = FALSE; | |
| 5834 } | |
| 5835 else if (*name == '\\') | |
| 5836 quoted = TRUE; | |
| 5837 else if (*name == sep) | |
| 5838 break; | |
| 5839 else | |
| 5840 *copyto++ = *name; | |
| 5841 } | |
| 2225 | 5842 if (*name != sep) |
| 5843 name = NULL; /* signal unterminated regexp */ | |
| 428 | 5844 |
| 5845 /* Terminate copied string. */ | |
| 5846 *copyto = '\0'; | |
| 5847 return name; | |
| 5848 } | |
| 5849 | |
| 5850 /* Look at the argument of --regex or --no-regex and do the right | |
| 5851 thing. Same for each line of a regexp file. */ | |
| 442 | 5852 static void |
| 2225 | 5853 analyse_regex (regex_arg) |
| 428 | 5854 char *regex_arg; |
| 5855 { | |
| 5856 if (regex_arg == NULL) | |
| 709 | 5857 { |
| 2225 | 5858 free_regexps (); /* --no-regex: remove existing regexps */ |
| 709 | 5859 return; |
| 5860 } | |
| 428 | 5861 |
| 5862 /* A real --regexp option or a line in a regexp file. */ | |
| 5863 switch (regex_arg[0]) | |
| 5864 { | |
| 5865 /* Comments in regexp file or null arg to --regex. */ | |
| 5866 case '\0': | |
| 5867 case ' ': | |
| 5868 case '\t': | |
| 5869 break; | |
| 5870 | |
| 5871 /* Read a regex file. This is recursive and may result in a | |
| 5872 loop, which will stop when the file descriptors are exhausted. */ | |
| 5873 case '@': | |
| 5874 { | |
| 5875 FILE *regexfp; | |
| 5876 linebuffer regexbuf; | |
| 5877 char *regexfile = regex_arg + 1; | |
| 5878 | |
| 5879 /* regexfile is a file containing regexps, one per line. */ | |
| 5880 regexfp = fopen (regexfile, "r"); | |
| 5881 if (regexfp == NULL) | |
| 5882 { | |
| 5883 pfatal (regexfile); | |
| 5884 return; | |
| 5885 } | |
| 2225 | 5886 linebuffer_init (®exbuf); |
| 428 | 5887 while (readline_internal (®exbuf, regexfp) > 0) |
| 2225 | 5888 analyse_regex (regexbuf.buffer); |
| 428 | 5889 free (regexbuf.buffer); |
| 5890 fclose (regexfp); | |
| 5891 } | |
| 5892 break; | |
| 5893 | |
| 5894 /* Regexp to be used for a specific language only. */ | |
| 5895 case '{': | |
| 5896 { | |
| 5897 language *lang; | |
| 5898 char *lang_name = regex_arg + 1; | |
| 5899 char *cp; | |
| 5900 | |
| 5901 for (cp = lang_name; *cp != '}'; cp++) | |
| 5902 if (*cp == '\0') | |
| 5903 { | |
| 5904 error ("unterminated language name in regex: %s", regex_arg); | |
| 5905 return; | |
| 5906 } | |
| 2225 | 5907 *cp++ = '\0'; |
| 458 | 5908 lang = get_language_from_langname (lang_name); |
| 428 | 5909 if (lang == NULL) |
| 5910 return; | |
| 2225 | 5911 add_regex (cp, lang); |
| 428 | 5912 } |
| 5913 break; | |
| 5914 | |
| 5915 /* Regexp to be used for any language. */ | |
| 5916 default: | |
| 2225 | 5917 add_regex (regex_arg, NULL); |
| 428 | 5918 break; |
| 5919 } | |
| 5920 } | |
| 5921 | |
| 2225 | 5922 /* Separate the regexp pattern, compile it, |
| 5923 and care for optional name and modifiers. */ | |
| 442 | 5924 static void |
| 2225 | 5925 add_regex (regexp_pattern, lang) |
| 428 | 5926 char *regexp_pattern; |
| 5927 language *lang; | |
| 5928 { | |
| 531 | 5929 static struct re_pattern_buffer zeropattern; |
| 2225 | 5930 char sep, *pat, *name, *modifiers; |
| 428 | 5931 const char *err; |
| 5932 struct re_pattern_buffer *patbuf; | |
| 2225 | 5933 regexp *rp; |
| 5934 bool | |
| 5935 force_explicit_name = TRUE, /* do not use implicit tag names */ | |
| 5936 ignore_case = FALSE, /* case is significant */ | |
| 5937 multi_line = FALSE, /* matches are done one line at a time */ | |
| 5938 single_line = FALSE; /* dot does not match newline */ | |
| 5939 | |
| 5940 | |
| 5941 if (strlen(regexp_pattern) < 3) | |
| 5942 { | |
| 5943 error ("null regexp", (char *)NULL); | |
| 5944 return; | |
| 5945 } | |
| 5946 sep = regexp_pattern[0]; | |
| 5947 name = scan_separators (regexp_pattern); | |
| 5948 if (name == NULL) | |
| 428 | 5949 { |
| 5950 error ("%s: unterminated regexp", regexp_pattern); | |
| 5951 return; | |
| 5952 } | |
| 2225 | 5953 if (name[1] == sep) |
| 428 | 5954 { |
| 2225 | 5955 error ("null name for regexp \"%s\"", regexp_pattern); |
| 428 | 5956 return; |
| 5957 } | |
| 2225 | 5958 modifiers = scan_separators (name); |
| 5959 if (modifiers == NULL) /* no terminating separator --> no name */ | |
| 5960 { | |
| 5961 modifiers = name; | |
| 5962 name = ""; | |
| 5963 } | |
| 5964 else | |
| 5965 modifiers += 1; /* skip separator */ | |
| 5966 | |
| 5967 /* Parse regex modifiers. */ | |
| 5968 for (; modifiers[0] != '\0'; modifiers++) | |
| 5969 switch (modifiers[0]) | |
| 5970 { | |
| 5971 case 'N': | |
| 5972 if (modifiers == name) | |
| 5973 error ("forcing explicit tag name but no name, ignoring", NULL); | |
| 5974 force_explicit_name = TRUE; | |
| 5975 break; | |
| 5976 case 'i': | |
| 5977 ignore_case = TRUE; | |
| 5978 break; | |
| 5979 case 's': | |
| 5980 single_line = TRUE; | |
| 5981 /* FALLTHRU */ | |
| 5982 case 'm': | |
| 5983 multi_line = TRUE; | |
| 5984 need_filebuf = TRUE; | |
| 5985 break; | |
| 5986 default: | |
| 5987 { | |
| 5988 char wrongmod [2]; | |
| 5989 wrongmod[0] = modifiers[0]; | |
| 5990 wrongmod[1] = '\0'; | |
| 5991 error ("invalid regexp modifier `%s', ignoring", wrongmod); | |
| 5992 } | |
| 5993 break; | |
| 5994 } | |
| 428 | 5995 |
| 5996 patbuf = xnew (1, struct re_pattern_buffer); | |
| 531 | 5997 *patbuf = zeropattern; |
| 5998 if (ignore_case) | |
| 2225 | 5999 { |
| 6000 static char lc_trans[CHARS]; | |
| 6001 int i; | |
| 6002 for (i = 0; i < CHARS; i++) | |
| 6003 lc_trans[i] = lowcase (i); | |
| 6004 patbuf->translate = lc_trans; /* translation table to fold case */ | |
| 6005 } | |
| 6006 | |
| 6007 if (multi_line) | |
| 6008 pat = concat ("^", regexp_pattern, ""); /* anchor to beginning of line */ | |
| 6009 else | |
| 6010 pat = regexp_pattern; | |
| 6011 | |
| 6012 if (single_line) | |
| 6013 re_set_syntax (RE_SYNTAX_EMACS | RE_DOT_NEWLINE); | |
| 6014 else | |
| 6015 re_set_syntax (RE_SYNTAX_EMACS); | |
| 6016 | |
| 6017 err = re_compile_pattern (pat, strlen (regexp_pattern), patbuf); | |
| 6018 if (multi_line) | |
| 6019 free (pat); | |
| 428 | 6020 if (err != NULL) |
| 6021 { | |
| 6022 error ("%s while compiling pattern", err); | |
| 6023 return; | |
| 6024 } | |
| 6025 | |
| 2225 | 6026 rp = p_head; |
| 6027 p_head = xnew (1, regexp); | |
| 6028 p_head->pattern = savestr (regexp_pattern); | |
| 6029 p_head->p_next = rp; | |
| 709 | 6030 p_head->lang = lang; |
| 6031 p_head->pat = patbuf; | |
| 2225 | 6032 p_head->name = savestr (name); |
| 428 | 6033 p_head->error_signaled = FALSE; |
| 2225 | 6034 p_head->force_explicit_name = force_explicit_name; |
| 6035 p_head->ignore_case = ignore_case; | |
| 6036 p_head->multi_line = multi_line; | |
| 428 | 6037 } |
| 6038 | |
| 6039 /* | |
| 6040 * Do the substitutions indicated by the regular expression and | |
| 6041 * arguments. | |
| 6042 */ | |
| 6043 static char * | |
| 6044 substitute (in, out, regs) | |
| 6045 char *in, *out; | |
| 6046 struct re_registers *regs; | |
| 6047 { | |
| 6048 char *result, *t; | |
| 6049 int size, dig, diglen; | |
| 6050 | |
| 6051 result = NULL; | |
| 6052 size = strlen (out); | |
| 6053 | |
| 6054 /* Pass 1: figure out how much to allocate by finding all \N strings. */ | |
| 6055 if (out[size - 1] == '\\') | |
| 6056 fatal ("pattern error in \"%s\"", out); | |
| 6057 for (t = etags_strchr (out, '\\'); | |
| 6058 t != NULL; | |
| 6059 t = etags_strchr (t + 2, '\\')) | |
| 458 | 6060 if (ISDIGIT (t[1])) |
| 428 | 6061 { |
| 6062 dig = t[1] - '0'; | |
| 6063 diglen = regs->end[dig] - regs->start[dig]; | |
| 6064 size += diglen - 2; | |
| 6065 } | |
| 6066 else | |
| 6067 size -= 1; | |
| 6068 | |
| 6069 /* Allocate space and do the substitutions. */ | |
| 2225 | 6070 assert (size >= 0); |
| 428 | 6071 result = xnew (size + 1, char); |
| 6072 | |
| 6073 for (t = result; *out != '\0'; out++) | |
| 458 | 6074 if (*out == '\\' && ISDIGIT (*++out)) |
| 428 | 6075 { |
| 6076 dig = *out - '0'; | |
| 6077 diglen = regs->end[dig] - regs->start[dig]; | |
| 6078 strncpy (t, in + regs->start[dig], diglen); | |
| 6079 t += diglen; | |
| 6080 } | |
| 6081 else | |
| 6082 *t++ = *out; | |
| 6083 *t = '\0'; | |
| 6084 | |
| 2225 | 6085 assert (t <= result + size); |
| 6086 assert (t - result == (int)strlen (result)); | |
| 428 | 6087 |
| 6088 return result; | |
| 6089 } | |
| 6090 | |
| 2225 | 6091 /* Deallocate all regexps. */ |
| 442 | 6092 static void |
| 2225 | 6093 free_regexps () |
| 428 | 6094 { |
| 2225 | 6095 regexp *rp; |
| 428 | 6096 while (p_head != NULL) |
| 6097 { | |
| 2225 | 6098 rp = p_head->p_next; |
| 6099 free (p_head->pattern); | |
| 6100 free (p_head->name); | |
| 428 | 6101 free (p_head); |
| 2225 | 6102 p_head = rp; |
| 428 | 6103 } |
| 6104 return; | |
| 6105 } | |
| 2225 | 6106 |
| 6107 /* | |
| 6108 * Reads the whole file as a single string from `filebuf' and looks for | |
| 6109 * multi-line regular expressions, creating tags on matches. | |
| 6110 * readline already dealt with normal regexps. | |
| 6111 * | |
| 6112 * Idea by Ben Wing <ben@666.com> (2002). | |
| 6113 */ | |
| 6114 static void | |
| 6115 regex_tag_multiline () | |
| 6116 { | |
| 6117 char *buffer = filebuf.buffer; | |
| 6118 regexp *rp; | |
| 6119 char *name; | |
| 6120 | |
| 6121 for (rp = p_head; rp != NULL; rp = rp->p_next) | |
| 6122 { | |
| 6123 int match = 0; | |
| 6124 | |
| 6125 if (!rp->multi_line) | |
| 6126 continue; /* skip normal regexps */ | |
| 6127 | |
| 6128 /* Generic initialisations before parsing file from memory. */ | |
| 6129 lineno = 1; /* reset global line number */ | |
| 6130 charno = 0; /* reset global char number */ | |
| 6131 linecharno = 0; /* reset global char number of line start */ | |
| 6132 | |
| 6133 /* Only use generic regexps or those for the current language. */ | |
| 6134 if (rp->lang != NULL && rp->lang != curfdp->lang) | |
| 6135 continue; | |
| 6136 | |
| 6137 while (match >= 0 && match < filebuf.len) | |
| 6138 { | |
| 6139 match = re_search (rp->pat, buffer, filebuf.len, charno, | |
| 6140 filebuf.len - match, &rp->regs); | |
| 6141 switch (match) | |
| 6142 { | |
| 6143 case -2: | |
| 6144 /* Some error. */ | |
| 6145 if (!rp->error_signaled) | |
| 6146 { | |
| 6147 error ("regexp stack overflow while matching \"%s\"", | |
| 6148 rp->pattern); | |
| 6149 rp->error_signaled = TRUE; | |
| 6150 } | |
| 6151 break; | |
| 6152 case -1: | |
| 6153 /* No match. */ | |
| 6154 break; | |
| 6155 default: | |
| 6156 if (match == rp->regs.end[0]) | |
| 6157 { | |
| 6158 if (!rp->error_signaled) | |
| 6159 { | |
| 6160 error ("regexp matches the empty string: \"%s\"", | |
| 6161 rp->pattern); | |
| 6162 rp->error_signaled = TRUE; | |
| 6163 } | |
| 6164 match = -3; /* exit from while loop */ | |
| 6165 break; | |
| 6166 } | |
| 6167 | |
| 6168 /* Match occurred. Construct a tag. */ | |
| 6169 while (charno < rp->regs.end[0]) | |
| 6170 if (buffer[charno++] == '\n') | |
| 6171 lineno++, linecharno = charno; | |
| 6172 name = rp->name; | |
| 6173 if (name[0] == '\0') | |
| 6174 name = NULL; | |
| 6175 else /* make a named tag */ | |
| 6176 name = substitute (buffer, rp->name, &rp->regs); | |
| 6177 if (rp->force_explicit_name) | |
| 6178 /* Force explicit tag name, if a name is there. */ | |
| 6179 pfnote (name, TRUE, buffer + linecharno, | |
| 6180 charno - linecharno + 1, lineno, linecharno); | |
| 6181 else | |
| 6182 make_tag (name, strlen (name), TRUE, buffer + linecharno, | |
| 6183 charno - linecharno + 1, lineno, linecharno); | |
| 6184 break; | |
| 6185 } | |
| 6186 } | |
| 6187 } | |
| 6188 } | |
| 6189 | |
| 428 | 6190 |
| 2225 | 6191 static bool |
| 6192 nocase_tail (cp) | |
| 6193 char *cp; | |
| 428 | 6194 { |
| 2225 | 6195 register int len = 0; |
| 6196 | |
| 6197 while (*cp != '\0' && lowcase (*cp) == lowcase (dbp[len])) | |
| 6198 cp++, len++; | |
| 6199 if (*cp == '\0' && !intoken (dbp[len])) | |
| 6200 { | |
| 6201 dbp += len; | |
| 6202 return TRUE; | |
| 6203 } | |
| 6204 return FALSE; | |
| 428 | 6205 } |
| 6206 | |
| 442 | 6207 static void |
| 2225 | 6208 get_tag (bp, namepp) |
| 6209 register char *bp; | |
| 6210 char **namepp; | |
| 428 | 6211 { |
| 2225 | 6212 register char *cp = bp; |
| 6213 | |
| 6214 if (*bp != '\0') | |
| 6215 { | |
| 6216 /* Go till you get to white space or a syntactic break */ | |
| 6217 for (cp = bp + 1; !notinname (*cp); cp++) | |
| 6218 continue; | |
| 6219 make_tag (bp, cp - bp, TRUE, | |
| 6220 lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
| 6221 } | |
| 6222 | |
| 6223 if (namepp != NULL) | |
| 6224 *namepp = savenstr (bp, cp - bp); | |
| 428 | 6225 } |
| 6226 | |
| 6227 /* | |
| 6228 * Read a line of text from `stream' into `lbp', excluding the | |
| 6229 * newline or CR-NL, if any. Return the number of characters read from | |
| 6230 * `stream', which is the length of the line including the newline. | |
| 6231 * | |
| 2225 | 6232 * On DOS or Windows we do not count the CR character, if any before the |
| 6233 * NL, in the returned length; this mirrors the behavior of Emacs on those | |
| 428 | 6234 * platforms (for text files, it translates CR-NL to NL as it reads in the |
| 6235 * file). | |
| 2225 | 6236 * |
| 6237 * If multi-line regular expressions are requested, each line read is | |
| 6238 * appended to `filebuf'. | |
| 428 | 6239 */ |
| 442 | 6240 static long |
| 428 | 6241 readline_internal (lbp, stream) |
| 6242 linebuffer *lbp; | |
| 6243 register FILE *stream; | |
| 6244 { | |
| 6245 char *buffer = lbp->buffer; | |
| 6246 register char *p = lbp->buffer; | |
| 6247 register char *pend; | |
| 6248 int chars_deleted; | |
| 6249 | |
| 6250 pend = p + lbp->size; /* Separate to avoid 386/IX compiler bug. */ | |
| 6251 | |
| 2225 | 6252 for (;;) |
| 428 | 6253 { |
| 6254 register int c = getc (stream); | |
| 6255 if (p == pend) | |
| 6256 { | |
| 6257 /* We're at the end of linebuffer: expand it. */ | |
| 6258 lbp->size *= 2; | |
| 458 | 6259 xrnew (buffer, lbp->size, char); |
| 428 | 6260 p += buffer - lbp->buffer; |
| 6261 pend = buffer + lbp->size; | |
| 6262 lbp->buffer = buffer; | |
| 6263 } | |
| 6264 if (c == EOF) | |
| 6265 { | |
| 6266 *p = '\0'; | |
| 6267 chars_deleted = 0; | |
| 6268 break; | |
| 6269 } | |
| 6270 if (c == '\n') | |
| 6271 { | |
| 6272 if (p > buffer && p[-1] == '\r') | |
| 6273 { | |
| 6274 p -= 1; | |
| 458 | 6275 #ifdef DOS_NT |
| 428 | 6276 /* Assume CRLF->LF translation will be performed by Emacs |
| 6277 when loading this file, so CRs won't appear in the buffer. | |
| 6278 It would be cleaner to compensate within Emacs; | |
| 6279 however, Emacs does not know how many CRs were deleted | |
| 6280 before any given point in the file. */ | |
| 6281 chars_deleted = 1; | |
| 6282 #else | |
| 6283 chars_deleted = 2; | |
| 6284 #endif | |
| 6285 } | |
| 6286 else | |
| 6287 { | |
| 6288 chars_deleted = 1; | |
| 6289 } | |
| 6290 *p = '\0'; | |
| 6291 break; | |
| 6292 } | |
| 6293 *p++ = c; | |
| 6294 } | |
| 6295 lbp->len = p - buffer; | |
| 6296 | |
| 2225 | 6297 if (need_filebuf /* we need filebuf for multi-line regexps */ |
| 6298 && chars_deleted > 0) /* not at EOF */ | |
| 6299 { | |
| 6300 while (filebuf.size <= filebuf.len + lbp->len + 1) /* +1 for \n */ | |
| 6301 { | |
| 6302 /* Expand filebuf. */ | |
| 6303 filebuf.size *= 2; | |
| 6304 xrnew (filebuf.buffer, filebuf.size, char); | |
| 6305 } | |
| 6306 strncpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len); | |
| 6307 filebuf.len += lbp->len; | |
| 6308 filebuf.buffer[filebuf.len++] = '\n'; | |
| 6309 filebuf.buffer[filebuf.len] = '\0'; | |
| 6310 } | |
| 6311 | |
| 428 | 6312 return lbp->len + chars_deleted; |
| 6313 } | |
| 6314 | |
| 6315 /* | |
| 6316 * Like readline_internal, above, but in addition try to match the | |
| 2225 | 6317 * input line against relevant regular expressions and manage #line |
| 6318 * directives. | |
| 428 | 6319 */ |
| 2225 | 6320 static void |
| 428 | 6321 readline (lbp, stream) |
| 6322 linebuffer *lbp; | |
| 6323 FILE *stream; | |
| 6324 { | |
| 2225 | 6325 long result; |
| 6326 | |
| 6327 linecharno = charno; /* update global char number of line start */ | |
| 6328 result = readline_internal (lbp, stream); /* read line */ | |
| 6329 lineno += 1; /* increment global line number */ | |
| 6330 charno += result; /* increment global char number */ | |
| 6331 | |
| 6332 /* Honour #line directives. */ | |
| 6333 if (!no_line_directive) | |
| 6334 { | |
| 6335 static bool discard_until_line_directive; | |
| 6336 | |
| 6337 /* Check whether this is a #line directive. */ | |
| 6338 if (result > 12 && strneq (lbp->buffer, "#line ", 6)) | |
| 6339 { | |
| 3876 | 6340 unsigned int lno; |
| 6341 int start = 0; | |
| 6342 | |
| 6343 if (sscanf (lbp->buffer, "#line %u \"%n", &lno, &start) >= 1 | |
| 6344 && start > 0) /* double quote character found */ | |
| 2225 | 6345 { |
| 6346 char *endp = lbp->buffer + start; | |
| 6347 | |
| 6348 while ((endp = etags_strchr (endp, '"')) != NULL | |
| 6349 && endp[-1] == '\\') | |
| 6350 endp++; | |
| 6351 if (endp != NULL) | |
| 6352 /* Ok, this is a real #line directive. Let's deal with it. */ | |
| 6353 { | |
| 6354 char *taggedabsname; /* absolute name of original file */ | |
| 6355 char *taggedfname; /* name of original file as given */ | |
| 6356 char *name; /* temp var */ | |
| 6357 | |
| 6358 discard_until_line_directive = FALSE; /* found it */ | |
| 6359 name = lbp->buffer + start; | |
| 6360 *endp = '\0'; | |
| 6361 canonicalize_filename (name); /* for DOS */ | |
| 3876 | 6362 taggedabsname = absolute_filename (name, tagfiledir); |
| 2225 | 6363 if (filename_is_absolute (name) |
| 6364 || filename_is_absolute (curfdp->infname)) | |
| 6365 taggedfname = savestr (taggedabsname); | |
| 6366 else | |
| 6367 taggedfname = relative_filename (taggedabsname,tagfiledir); | |
| 6368 | |
| 6369 if (streq (curfdp->taggedfname, taggedfname)) | |
| 6370 /* The #line directive is only a line number change. We | |
| 6371 deal with this afterwards. */ | |
| 6372 free (taggedfname); | |
| 6373 else | |
| 6374 /* The tags following this #line directive should be | |
| 6375 attributed to taggedfname. In order to do this, set | |
| 6376 curfdp accordingly. */ | |
| 6377 { | |
| 6378 fdesc *fdp; /* file description pointer */ | |
| 6379 | |
| 6380 /* Go look for a file description already set up for the | |
| 6381 file indicated in the #line directive. If there is | |
| 6382 one, use it from now until the next #line | |
| 6383 directive. */ | |
| 6384 for (fdp = fdhead; fdp != NULL; fdp = fdp->next) | |
| 6385 if (streq (fdp->infname, curfdp->infname) | |
| 6386 && streq (fdp->taggedfname, taggedfname)) | |
| 6387 /* If we remove the second test above (after the &&) | |
| 6388 then all entries pertaining to the same file are | |
| 6389 coalesced in the tags file. If we use it, then | |
| 6390 entries pertaining to the same file but generated | |
| 6391 from different files (via #line directives) will | |
| 6392 go into separate sections in the tags file. These | |
| 6393 alternatives look equivalent. The first one | |
| 6394 destroys some apparently useless information. */ | |
| 6395 { | |
| 6396 curfdp = fdp; | |
| 6397 free (taggedfname); | |
| 6398 break; | |
| 6399 } | |
| 6400 /* Else, if we already tagged the real file, skip all | |
| 6401 input lines until the next #line directive. */ | |
| 6402 if (fdp == NULL) /* not found */ | |
| 6403 for (fdp = fdhead; fdp != NULL; fdp = fdp->next) | |
| 6404 if (streq (fdp->infabsname, taggedabsname)) | |
| 6405 { | |
| 6406 discard_until_line_directive = TRUE; | |
| 6407 free (taggedfname); | |
| 6408 break; | |
| 6409 } | |
| 6410 /* Else create a new file description and use that from | |
| 6411 now on, until the next #line directive. */ | |
| 6412 if (fdp == NULL) /* not found */ | |
| 6413 { | |
| 6414 fdp = fdhead; | |
| 6415 fdhead = xnew (1, fdesc); | |
| 6416 *fdhead = *curfdp; /* copy curr. file description */ | |
| 6417 fdhead->next = fdp; | |
| 6418 fdhead->infname = savestr (curfdp->infname); | |
| 6419 fdhead->infabsname = savestr (curfdp->infabsname); | |
| 6420 fdhead->infabsdir = savestr (curfdp->infabsdir); | |
| 6421 fdhead->taggedfname = taggedfname; | |
| 6422 fdhead->usecharno = FALSE; | |
| 6423 fdhead->prop = NULL; | |
| 6424 fdhead->written = FALSE; | |
| 6425 curfdp = fdhead; | |
| 6426 } | |
| 6427 } | |
| 6428 free (taggedabsname); | |
| 6429 lineno = lno - 1; | |
| 6430 readline (lbp, stream); | |
| 6431 return; | |
| 6432 } /* if a real #line directive */ | |
| 6433 } /* if #line is followed by a a number */ | |
| 6434 } /* if line begins with "#line " */ | |
| 6435 | |
| 6436 /* If we are here, no #line directive was found. */ | |
| 6437 if (discard_until_line_directive) | |
| 6438 { | |
| 6439 if (result > 0) | |
| 6440 { | |
| 6441 /* Do a tail recursion on ourselves, thus discarding the contents | |
| 6442 of the line buffer. */ | |
| 6443 readline (lbp, stream); | |
| 6444 return; | |
| 6445 } | |
| 6446 /* End of file. */ | |
| 6447 discard_until_line_directive = FALSE; | |
| 6448 return; | |
| 6449 } | |
| 6450 } /* if #line directives should be considered */ | |
| 6451 | |
| 6452 { | |
| 6453 int match; | |
| 6454 regexp *rp; | |
| 6455 char *name; | |
| 6456 | |
| 6457 /* Match against relevant regexps. */ | |
| 6458 if (lbp->len > 0) | |
| 6459 for (rp = p_head; rp != NULL; rp = rp->p_next) | |
| 6460 { | |
| 6461 /* Only use generic regexps or those for the current language. | |
| 6462 Also do not use multiline regexps, which is the job of | |
| 6463 regex_tag_multiline. */ | |
| 6464 if ((rp->lang != NULL && rp->lang != fdhead->lang) | |
| 6465 || rp->multi_line) | |
| 6466 continue; | |
| 6467 | |
| 6468 match = re_match (rp->pat, lbp->buffer, lbp->len, 0, &rp->regs); | |
| 6469 switch (match) | |
| 6470 { | |
| 6471 case -2: | |
| 6472 /* Some error. */ | |
| 6473 if (!rp->error_signaled) | |
| 6474 { | |
| 6475 error ("regexp stack overflow while matching \"%s\"", | |
| 6476 rp->pattern); | |
| 6477 rp->error_signaled = TRUE; | |
| 6478 } | |
| 6479 break; | |
| 6480 case -1: | |
| 6481 /* No match. */ | |
| 6482 break; | |
| 6483 case 0: | |
| 6484 /* Empty string matched. */ | |
| 6485 if (!rp->error_signaled) | |
| 6486 { | |
| 6487 error ("regexp matches the empty string: \"%s\"", rp->pattern); | |
| 6488 rp->error_signaled = TRUE; | |
| 6489 } | |
| 6490 break; | |
| 6491 default: | |
| 6492 /* Match occurred. Construct a tag. */ | |
| 6493 name = rp->name; | |
| 6494 if (name[0] == '\0') | |
| 6495 name = NULL; | |
| 6496 else /* make a named tag */ | |
| 6497 name = substitute (lbp->buffer, rp->name, &rp->regs); | |
| 6498 if (rp->force_explicit_name) | |
| 6499 /* Force explicit tag name, if a name is there. */ | |
| 6500 pfnote (name, TRUE, lbp->buffer, match, lineno, linecharno); | |
| 6501 else | |
| 6502 make_tag (name, strlen (name), TRUE, | |
| 6503 lbp->buffer, match, lineno, linecharno); | |
| 6504 break; | |
| 6505 } | |
| 6506 } | |
| 6507 } | |
| 428 | 6508 } |
| 458 | 6509 |
| 428 | 6510 |
| 6511 /* | |
| 6512 * Return a pointer to a space of size strlen(cp)+1 allocated | |
| 6513 * with xnew where the string CP has been copied. | |
| 6514 */ | |
| 442 | 6515 static char * |
| 428 | 6516 savestr (cp) |
| 6517 char *cp; | |
| 6518 { | |
| 6519 return savenstr (cp, strlen (cp)); | |
| 6520 } | |
| 6521 | |
| 6522 /* | |
| 6523 * Return a pointer to a space of size LEN+1 allocated with xnew where | |
| 6524 * the string CP has been copied for at most the first LEN characters. | |
| 6525 */ | |
| 442 | 6526 static char * |
| 428 | 6527 savenstr (cp, len) |
| 6528 char *cp; | |
| 6529 int len; | |
| 6530 { | |
| 6531 register char *dp; | |
| 6532 | |
| 6533 dp = xnew (len + 1, char); | |
| 6534 strncpy (dp, cp, len); | |
| 6535 dp[len] = '\0'; | |
| 6536 return dp; | |
| 6537 } | |
| 6538 | |
| 6539 /* | |
| 6540 * Return the ptr in sp at which the character c last | |
| 6541 * appears; NULL if not found | |
| 442 | 6542 * |
| 6543 * Identical to POSIX strrchr, included for portability. | |
| 428 | 6544 */ |
| 442 | 6545 static char * |
| 428 | 6546 etags_strrchr (sp, c) |
| 442 | 6547 register const char *sp; |
| 6548 register int c; | |
| 428 | 6549 { |
| 438 | 6550 register const char *r; |
| 428 | 6551 |
| 6552 r = NULL; | |
| 6553 do | |
| 6554 { | |
| 6555 if (*sp == c) | |
| 6556 r = sp; | |
| 6557 } while (*sp++); | |
| 442 | 6558 return (char *)r; |
| 428 | 6559 } |
| 6560 | |
| 6561 /* | |
| 6562 * Return the ptr in sp at which the character c first | |
| 6563 * appears; NULL if not found | |
| 442 | 6564 * |
| 6565 * Identical to POSIX strchr, included for portability. | |
| 428 | 6566 */ |
| 442 | 6567 static char * |
| 428 | 6568 etags_strchr (sp, c) |
| 442 | 6569 register const char *sp; |
| 6570 register int c; | |
| 428 | 6571 { |
| 6572 do | |
| 6573 { | |
| 6574 if (*sp == c) | |
| 442 | 6575 return (char *)sp; |
| 428 | 6576 } while (*sp++); |
| 6577 return NULL; | |
| 6578 } | |
| 6579 | |
| 2225 | 6580 /* |
| 6581 * Compare two strings, ignoring case for alphabetic characters. | |
| 6582 * | |
| 6583 * Same as BSD's strcasecmp, included for portability. | |
| 6584 */ | |
| 6585 static int | |
| 6586 etags_strcasecmp (s1, s2) | |
| 6587 register const char *s1; | |
| 6588 register const char *s2; | |
| 6589 { | |
| 6590 while (*s1 != '\0' | |
| 6591 && (ISALPHA (*s1) && ISALPHA (*s2) | |
| 6592 ? lowcase (*s1) == lowcase (*s2) | |
| 6593 : *s1 == *s2)) | |
| 6594 s1++, s2++; | |
| 6595 | |
| 6596 return (ISALPHA (*s1) && ISALPHA (*s2) | |
| 6597 ? lowcase (*s1) - lowcase (*s2) | |
| 6598 : *s1 - *s2); | |
| 6599 } | |
| 6600 | |
| 6601 /* | |
| 6602 * Compare two strings, ignoring case for alphabetic characters. | |
| 6603 * Stop after a given number of characters | |
| 6604 * | |
| 6605 * Same as BSD's strncasecmp, included for portability. | |
| 6606 */ | |
| 6607 static int | |
| 6608 etags_strncasecmp (s1, s2, n) | |
| 6609 register const char *s1; | |
| 6610 register const char *s2; | |
| 6611 register int n; | |
| 6612 { | |
| 6613 while (*s1 != '\0' && n-- > 0 | |
| 6614 && (ISALPHA (*s1) && ISALPHA (*s2) | |
| 6615 ? lowcase (*s1) == lowcase (*s2) | |
| 6616 : *s1 == *s2)) | |
| 6617 s1++, s2++; | |
| 6618 | |
| 6619 if (n < 0) | |
| 6620 return 0; | |
| 6621 else | |
| 6622 return (ISALPHA (*s1) && ISALPHA (*s2) | |
| 6623 ? lowcase (*s1) - lowcase (*s2) | |
| 6624 : *s1 - *s2); | |
| 6625 } | |
| 6626 | |
| 2554 | 6627 /* Skip spaces (end of string is not space), return new pointer. */ |
| 442 | 6628 static char * |
| 428 | 6629 skip_spaces (cp) |
| 6630 char *cp; | |
| 6631 { | |
| 442 | 6632 while (iswhite (*cp)) |
| 428 | 6633 cp++; |
| 6634 return cp; | |
| 6635 } | |
| 6636 | |
| 2554 | 6637 /* Skip non spaces, except end of string, return new pointer. */ |
| 442 | 6638 static char * |
| 428 | 6639 skip_non_spaces (cp) |
| 6640 char *cp; | |
| 6641 { | |
| 442 | 6642 while (*cp != '\0' && !iswhite (*cp)) |
| 428 | 6643 cp++; |
| 6644 return cp; | |
| 6645 } | |
| 6646 | |
| 6647 /* Print error message and exit. */ | |
| 458 | 6648 void |
| 428 | 6649 fatal (s1, s2) |
| 6650 char *s1, *s2; | |
| 6651 { | |
| 6652 error (s1, s2); | |
| 2225 | 6653 exit (EXIT_FAILURE); |
| 428 | 6654 } |
| 6655 | |
| 442 | 6656 static void |
| 428 | 6657 pfatal (s1) |
| 6658 char *s1; | |
| 6659 { | |
| 6660 perror (s1); | |
| 2225 | 6661 exit (EXIT_FAILURE); |
| 428 | 6662 } |
| 6663 | |
| 442 | 6664 static void |
| 428 | 6665 suggest_asking_for_help () |
| 6666 { | |
| 2325 | 6667 fprintf (stderr, "\tTry `%s %s' for a complete list of options.\n", |
| 3517 | 6668 progname, NO_LONG_OPTIONS ? "-h" : "--help"); |
| 2225 | 6669 exit (EXIT_FAILURE); |
| 428 | 6670 } |
| 6671 | |
| 6672 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
| 442 | 6673 static void |
| 428 | 6674 error (s1, s2) |
| 6675 const char *s1, *s2; | |
| 6676 { | |
| 6677 fprintf (stderr, "%s: ", progname); | |
| 6678 fprintf (stderr, s1, s2); | |
| 6679 fprintf (stderr, "\n"); | |
| 6680 } | |
| 6681 | |
| 6682 /* Return a newly-allocated string whose contents | |
| 6683 concatenate those of s1, s2, s3. */ | |
| 442 | 6684 static char * |
| 428 | 6685 concat (s1, s2, s3) |
| 6686 char *s1, *s2, *s3; | |
| 6687 { | |
| 6688 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
| 6689 char *result = xnew (len1 + len2 + len3 + 1, char); | |
| 6690 | |
| 6691 strcpy (result, s1); | |
| 6692 strcpy (result + len1, s2); | |
| 6693 strcpy (result + len1 + len2, s3); | |
| 6694 result[len1 + len2 + len3] = '\0'; | |
| 6695 | |
| 6696 return result; | |
| 6697 } | |
| 458 | 6698 |
| 428 | 6699 |
| 6700 /* Does the same work as the system V getcwd, but does not need to | |
| 6701 guess the buffer size in advance. */ | |
| 442 | 6702 static char * |
| 428 | 6703 etags_getcwd () |
| 6704 { | |
| 6705 #ifdef HAVE_GETCWD | |
| 6706 int bufsize = 200; | |
| 6707 char *path = xnew (bufsize, char); | |
| 6708 | |
| 6709 while (getcwd (path, bufsize) == NULL) | |
| 6710 { | |
| 6711 if (errno != ERANGE) | |
| 6712 pfatal ("getcwd"); | |
| 6713 bufsize *= 2; | |
| 6714 free (path); | |
| 6715 path = xnew (bufsize, char); | |
| 6716 } | |
| 6717 | |
| 6718 canonicalize_filename (path); | |
| 6719 return path; | |
| 6720 | |
| 6721 #else /* not HAVE_GETCWD */ | |
| 458 | 6722 #if MSDOS |
| 6723 | |
| 6724 char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */ | |
| 6725 | |
| 6726 getwd (path); | |
| 6727 | |
| 6728 for (p = path; *p != '\0'; p++) | |
| 6729 if (*p == '\\') | |
| 6730 *p = '/'; | |
| 6731 else | |
| 6732 *p = lowcase (*p); | |
| 6733 | |
| 6734 return strdup (path); | |
| 6735 #else /* not MSDOS */ | |
| 428 | 6736 linebuffer path; |
| 6737 FILE *pipe; | |
| 6738 | |
| 2225 | 6739 linebuffer_init (&path); |
| 428 | 6740 pipe = (FILE *) popen ("pwd 2>/dev/null", "r"); |
| 6741 if (pipe == NULL || readline_internal (&path, pipe) == 0) | |
| 6742 pfatal ("pwd"); | |
| 6743 pclose (pipe); | |
| 6744 | |
| 6745 return path.buffer; | |
| 458 | 6746 #endif /* not MSDOS */ |
| 428 | 6747 #endif /* not HAVE_GETCWD */ |
| 6748 } | |
| 6749 | |
| 6750 /* Return a newly allocated string containing the file name of FILE | |
| 6751 relative to the absolute directory DIR (which should end with a slash). */ | |
| 442 | 6752 static char * |
| 428 | 6753 relative_filename (file, dir) |
| 6754 char *file, *dir; | |
| 6755 { | |
| 6756 char *fp, *dp, *afn, *res; | |
| 6757 int i; | |
| 6758 | |
| 6759 /* Find the common root of file and dir (with a trailing slash). */ | |
| 6760 afn = absolute_filename (file, cwd); | |
| 6761 fp = afn; | |
| 6762 dp = dir; | |
| 6763 while (*fp++ == *dp++) | |
| 6764 continue; | |
| 6765 fp--, dp--; /* back to the first differing char */ | |
| 458 | 6766 #ifdef DOS_NT |
| 428 | 6767 if (fp == afn && afn[0] != '/') /* cannot build a relative name */ |
| 6768 return afn; | |
| 6769 #endif | |
| 6770 do /* look at the equal chars until '/' */ | |
| 6771 fp--, dp--; | |
| 6772 while (*fp != '/'); | |
| 6773 | |
| 6774 /* Build a sequence of "../" strings for the resulting relative file name. */ | |
| 6775 i = 0; | |
| 6776 while ((dp = etags_strchr (dp + 1, '/')) != NULL) | |
| 6777 i += 1; | |
| 6778 res = xnew (3*i + strlen (fp + 1) + 1, char); | |
| 6779 res[0] = '\0'; | |
| 6780 while (i-- > 0) | |
| 6781 strcat (res, "../"); | |
| 6782 | |
| 6783 /* Add the file name relative to the common root of file and dir. */ | |
| 6784 strcat (res, fp + 1); | |
| 6785 free (afn); | |
| 6786 | |
| 6787 return res; | |
| 6788 } | |
| 6789 | |
| 6790 /* Return a newly allocated string containing the absolute file name | |
| 6791 of FILE given DIR (which should end with a slash). */ | |
| 442 | 6792 static char * |
| 428 | 6793 absolute_filename (file, dir) |
| 6794 char *file, *dir; | |
| 6795 { | |
| 6796 char *slashp, *cp, *res; | |
| 6797 | |
| 6798 if (filename_is_absolute (file)) | |
| 6799 res = savestr (file); | |
| 458 | 6800 #ifdef DOS_NT |
| 428 | 6801 /* We don't support non-absolute file names with a drive |
| 6802 letter, like `d:NAME' (it's too much hassle). */ | |
| 6803 else if (file[1] == ':') | |
| 6804 fatal ("%s: relative file names with drive letters not supported", file); | |
| 6805 #endif | |
| 6806 else | |
| 6807 res = concat (dir, file, ""); | |
| 6808 | |
| 6809 /* Delete the "/dirname/.." and "/." substrings. */ | |
| 6810 slashp = etags_strchr (res, '/'); | |
| 6811 while (slashp != NULL && slashp[0] != '\0') | |
| 6812 { | |
| 6813 if (slashp[1] == '.') | |
| 6814 { | |
| 6815 if (slashp[2] == '.' | |
| 6816 && (slashp[3] == '/' || slashp[3] == '\0')) | |
| 6817 { | |
| 6818 cp = slashp; | |
| 6819 do | |
| 6820 cp--; | |
| 6821 while (cp >= res && !filename_is_absolute (cp)); | |
| 6822 if (cp < res) | |
| 6823 cp = slashp; /* the absolute name begins with "/.." */ | |
| 458 | 6824 #ifdef DOS_NT |
| 6825 /* Under MSDOS and NT we get `d:/NAME' as absolute | |
| 428 | 6826 file name, so the luser could say `d:/../NAME'. |
| 6827 We silently treat this as `d:/NAME'. */ | |
| 6828 else if (cp[0] != '/') | |
| 6829 cp = slashp; | |
| 6830 #endif | |
|
4768
0f5bee973a7b
Etags: use memmove instead of strcpy to move characters within a string. See
Jerry James <james@xemacs.org>
parents:
3993
diff
changeset
|
6831 memmove (cp, slashp + 3, strlen (slashp + 3) + 1); |
| 428 | 6832 slashp = cp; |
| 6833 continue; | |
| 6834 } | |
| 6835 else if (slashp[2] == '/' || slashp[2] == '\0') | |
| 6836 { | |
|
4768
0f5bee973a7b
Etags: use memmove instead of strcpy to move characters within a string. See
Jerry James <james@xemacs.org>
parents:
3993
diff
changeset
|
6837 memmove (slashp, slashp + 2, strlen (slashp + 2) + 1); |
| 428 | 6838 continue; |
| 6839 } | |
| 6840 } | |
| 6841 | |
| 6842 slashp = etags_strchr (slashp + 1, '/'); | |
| 6843 } | |
| 6844 | |
| 3517 | 6845 if (res[0] == '\0') /* just a safety net: should never happen */ |
| 6846 { | |
| 6847 free (res); | |
| 6848 return savestr ("/"); | |
| 6849 } | |
| 428 | 6850 else |
| 6851 return res; | |
| 6852 } | |
| 6853 | |
| 6854 /* Return a newly allocated string containing the absolute | |
| 6855 file name of dir where FILE resides given DIR (which should | |
| 6856 end with a slash). */ | |
| 442 | 6857 static char * |
| 428 | 6858 absolute_dirname (file, dir) |
| 6859 char *file, *dir; | |
| 6860 { | |
| 6861 char *slashp, *res; | |
| 6862 char save; | |
| 6863 | |
| 6864 canonicalize_filename (file); | |
| 6865 slashp = etags_strrchr (file, '/'); | |
| 6866 if (slashp == NULL) | |
| 6867 return savestr (dir); | |
| 6868 save = slashp[1]; | |
| 6869 slashp[1] = '\0'; | |
| 6870 res = absolute_filename (file, dir); | |
| 6871 slashp[1] = save; | |
| 6872 | |
| 6873 return res; | |
| 6874 } | |
| 6875 | |
| 6876 /* Whether the argument string is an absolute file name. The argument | |
| 6877 string must have been canonicalized with canonicalize_filename. */ | |
| 442 | 6878 static bool |
| 428 | 6879 filename_is_absolute (fn) |
| 6880 char *fn; | |
| 6881 { | |
| 6882 return (fn[0] == '/' | |
| 458 | 6883 #ifdef DOS_NT |
| 6884 || (ISALPHA(fn[0]) && fn[1] == ':' && fn[2] == '/') | |
| 428 | 6885 #endif |
| 6886 ); | |
| 6887 } | |
| 6888 | |
| 6889 /* Translate backslashes into slashes. Works in place. */ | |
| 442 | 6890 static void |
| 428 | 6891 canonicalize_filename (fn) |
| 6892 register char *fn; | |
| 6893 { | |
| 458 | 6894 #ifdef DOS_NT |
| 428 | 6895 /* Canonicalize drive letter case. */ |
| 458 | 6896 if (fn[0] != '\0' && fn[1] == ':' && ISLOWER (fn[0])) |
| 6897 fn[0] = upcase (fn[0]); | |
| 428 | 6898 /* Convert backslashes to slashes. */ |
| 6899 for (; *fn != '\0'; fn++) | |
| 6900 if (*fn == '\\') | |
| 6901 *fn = '/'; | |
| 6902 #else | |
| 6903 /* No action. */ | |
| 6904 fn = NULL; /* shut up the compiler */ | |
| 6905 #endif | |
| 6906 } | |
| 6907 | |
| 2225 | 6908 |
| 6909 /* Initialize a linebuffer for use */ | |
| 6910 static void | |
| 6911 linebuffer_init (lbp) | |
| 6912 linebuffer *lbp; | |
| 6913 { | |
| 6914 lbp->size = (DEBUG) ? 3 : 200; | |
| 6915 lbp->buffer = xnew (lbp->size, char); | |
| 6916 lbp->buffer[0] = '\0'; | |
| 6917 lbp->len = 0; | |
| 6918 } | |
| 6919 | |
| 458 | 6920 /* Set the minimum size of a string contained in a linebuffer. */ |
| 442 | 6921 static void |
| 458 | 6922 linebuffer_setlen (lbp, toksize) |
| 428 | 6923 linebuffer *lbp; |
| 6924 int toksize; | |
| 6925 { | |
| 458 | 6926 while (lbp->size <= toksize) |
| 6927 { | |
| 6928 lbp->size *= 2; | |
| 6929 xrnew (lbp->buffer, lbp->size, char); | |
| 6930 } | |
| 6931 lbp->len = toksize; | |
| 428 | 6932 } |
| 6933 | |
| 2225 | 6934 /* Like malloc but get fatal error if memory is exhausted. */ |
| 6935 static PTR | |
| 428 | 6936 xmalloc (size) |
| 6937 unsigned int size; | |
| 6938 { | |
| 709 | 6939 PTR result = (PTR) malloc (size); |
| 428 | 6940 if (result == NULL) |
| 6941 fatal ("virtual memory exhausted", (char *)NULL); | |
| 6942 return result; | |
| 6943 } | |
| 6944 | |
| 2225 | 6945 static PTR |
| 428 | 6946 xrealloc (ptr, size) |
| 6947 char *ptr; | |
| 6948 unsigned int size; | |
| 6949 { | |
| 709 | 6950 PTR result = (PTR) realloc (ptr, size); |
| 428 | 6951 if (result == NULL) |
| 6952 fatal ("virtual memory exhausted", (char *)NULL); | |
| 6953 return result; | |
| 6954 } | |
| 709 | 6955 |
| 6956 /* | |
| 6957 * Local Variables: | |
| 6958 * indent-tabs-mode: t | |
| 6959 * tab-width: 8 | |
| 2225 | 6960 * fill-column: 79 |
| 6961 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp") | |
| 3972 | 6962 * c-file-style: "gnu" |
| 709 | 6963 * End: |
| 6964 */ | |
| 2225 | 6965 |
| 6966 /* arch-tag: 8a9b748d-390c-4922-99db-2eeefa921051 | |
| 6967 (do not change this comment) */ | |
| 6968 | |
| 6969 /* etags.c ends here */ |
