1 /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
2 Copyright (C) 1984, 1987-1989, 1993-1995, 1998-2001
3 Free Software Foundation, Inc. and Ken Arnold
5 This file is not considered part of GNU Emacs.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 * Ctags originally by Ken Arnold.
24 * Fortran added by Jim Kleckner.
25 * Ed Pelegri-Llopart added C typedefs.
26 * Gnu Emacs TAGS format and modifications by RMS?
27 * 1989 Sam Kendall added C++.
28 * 1992 Joseph B. Wells improved C and C++ parsing.
29 * 1993 Francesco Potortì reorganised C and C++.
30 * 1994 Regexp tags by Tom Tromey.
31 * 2001 Nested classes by Francesco Potortì (ideas by Mykola Dzyuba).
33 * Francesco Potortì <pot@gnu.org> has maintained it since 1993.
36 char pot_etags_version
[] = "@(#) pot revision number is 14.39";
46 # define NDEBUG /* disable assert */
51 /* On some systems, Emacs defines static as nothing for the sake
52 of unexec. We don't want that here since we don't use unexec. */
54 # define ETAGS_REGEXPS /* use the regexp features */
55 # define LONG_OPTIONS /* accept long options */
56 # ifndef PTR /* for Xemacs */
59 # ifndef __P /* for Xemacs */
60 # define __P(args) args
63 # if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
64 # define __P(args) args /* use prototypes */
65 # define PTR void * /* for generic pointers */
67 # define __P(args) () /* no prototypes */
68 # define const /* remove const for old compilers' sake */
69 # define PTR long * /* don't use void* */
71 #endif /* !HAVE_CONFIG_H */
74 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
77 /* WIN32_NATIVE is for Xemacs.
78 MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
83 #endif /* WIN32_NATIVE */
89 # include <sys/param.h>
91 # ifndef HAVE_CONFIG_H
93 # include <sys/config.h>
105 # define MAXPATHLEN _MAX_PATH
111 # endif /* undef HAVE_GETCWD */
112 #else /* !WINDOWSNT */
117 extern char *getenv ();
119 #endif /* !WINDOWSNT */
124 # if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
125 extern char *getcwd (char *buf
, size_t size
);
127 #endif /* HAVE_UNISTD_H */
135 #include <sys/types.h>
136 #include <sys/stat.h>
140 # undef assert /* some systems have a buggy assert.h */
141 # define assert(x) ((void) 0)
144 #if !defined (S_ISREG) && defined (S_IFREG)
145 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
151 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
153 extern int optind
, opterr
;
154 #endif /* LONG_OPTIONS */
157 # ifndef HAVE_CONFIG_H /* this is a standalone compilation */
158 # ifdef __CYGWIN__ /* compiling on Cygwin */
160 the regex
.h distributed with Cygwin is
not compatible with etags
, alas
!
161 If you want regular expression support
, you should
delete this notice
and
162 arrange to use the GNU regex
.h
and regex
.c
.
166 #endif /* ETAGS_REGEXPS */
168 /* Define CTAGS to make the program "ctags" compatible with the usual one.
169 Leave it undefined to make the program "etags", which makes emacs-style
170 tag tables and tags typedefs, #defines and struct/union/enum by default. */
178 /* Exit codes for success and failure. */
187 #define streq(s,t) (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t))
188 #define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
190 #define CHARS 256 /* 2^sizeof(char) */
191 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
192 #define iswhite(c) (_wht[CHAR(c)]) /* c is white */
193 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name */
194 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token */
195 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token */
196 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens */
198 #define ISALNUM(c) isalnum (CHAR(c))
199 #define ISALPHA(c) isalpha (CHAR(c))
200 #define ISDIGIT(c) isdigit (CHAR(c))
201 #define ISLOWER(c) islower (CHAR(c))
203 #define lowcase(c) tolower (CHAR(c))
204 #define upcase(c) toupper (CHAR(c))
208 * xnew, xrnew -- allocate, reallocate storage
210 * SYNOPSIS: Type *xnew (int n, Type);
211 * void xrnew (OldPointer, int n, Type);
214 # include "chkmalloc.h"
215 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
216 (n) * sizeof (Type)))
217 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
218 (char *) (op), (n) * sizeof (Type)))
220 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
221 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
222 (char *) (op), (n) * sizeof (Type)))
227 typedef void Lang_function
__P((FILE *));
232 char *command
; /* Takes one arg and decompresses to stdout */
238 Lang_function
*function
;
244 typedef struct node_st
245 { /* sorting structure */
246 char *name
; /* function or type name */
247 char *file
; /* file name */
248 bool is_func
; /* use pattern or line no */
249 bool been_warned
; /* set if noticed dup */
250 int lno
; /* line number tag is on */
251 long cno
; /* character number line starts on */
252 char *pat
; /* search pattern */
253 struct node_st
*left
, *right
; /* left and right sons */
257 * A `linebuffer' is a structure which holds a line of text.
258 * `readline_internal' reads a line from a stream into a linebuffer
259 * and works regardless of the length of the line.
260 * SIZE is the size of BUFFER, LEN is the length of the string in
261 * BUFFER after readline reads it.
270 /* Many compilers barf on this:
271 Lang_function Ada_funcs;
272 so let's write it this way */
273 static void Ada_funcs
__P((FILE *));
274 static void Asm_labels
__P((FILE *));
275 static void C_entries
__P((int c_ext
, FILE *));
276 static void default_C_entries
__P((FILE *));
277 static void plain_C_entries
__P((FILE *));
278 static void Cjava_entries
__P((FILE *));
279 static void Cobol_paragraphs
__P((FILE *));
280 static void Cplusplus_entries
__P((FILE *));
281 static void Cstar_entries
__P((FILE *));
282 static void Erlang_functions
__P((FILE *));
283 static void Fortran_functions
__P((FILE *));
284 static void Yacc_entries
__P((FILE *));
285 static void Lisp_functions
__P((FILE *));
286 static void Makefile_targets
__P((FILE *));
287 static void Pascal_functions
__P((FILE *));
288 static void Perl_functions
__P((FILE *));
289 static void PHP_functions
__P((FILE *));
290 static void Postscript_functions
__P((FILE *));
291 static void Prolog_functions
__P((FILE *));
292 static void Python_functions
__P((FILE *));
293 static void Scheme_functions
__P((FILE *));
294 static void TeX_commands
__P((FILE *));
295 static void Texinfo_nodes
__P((FILE *));
296 static void just_read_file
__P((FILE *));
298 static void print_language_names
__P((void));
299 static void print_version
__P((void));
300 static void print_help
__P((void));
301 int main
__P((int, char **));
303 static compressor
*get_compressor_from_suffix
__P((char *, char **));
304 static language
*get_language_from_langname
__P((const char *));
305 static language
*get_language_from_interpreter
__P((char *));
306 static language
*get_language_from_filename
__P((char *));
307 static long readline
__P((linebuffer
*, FILE *));
308 static long readline_internal
__P((linebuffer
*, FILE *));
309 static bool nocase_tail
__P((char *));
310 static char *get_tag
__P((char *));
313 static void analyse_regex
__P((char *, bool));
314 static void add_regex
__P((char *, bool, language
*));
315 static void free_patterns
__P((void));
316 #endif /* ETAGS_REGEXPS */
317 static void error
__P((const char *, const char *));
318 static void suggest_asking_for_help
__P((void));
319 void fatal
__P((char *, char *));
320 static void pfatal
__P((char *));
321 static void add_node
__P((node
*, node
**));
323 static void init
__P((void));
324 static void initbuffer
__P((linebuffer
*));
325 static void find_entries
__P((char *, FILE *));
326 static void free_tree
__P((node
*));
327 static void pfnote
__P((char *, bool, char *, int, int, long));
328 static void new_pfnote
__P((char *, int, bool, char *, int, int, long));
329 static void process_file
__P((char *));
330 static void put_entries
__P((node
*));
332 static char *concat
__P((char *, char *, char *));
333 static char *skip_spaces
__P((char *));
334 static char *skip_non_spaces
__P((char *));
335 static char *savenstr
__P((char *, int));
336 static char *savestr
__P((char *));
337 static char *etags_strchr
__P((const char *, int));
338 static char *etags_strrchr
__P((const char *, int));
339 static char *etags_getcwd
__P((void));
340 static char *relative_filename
__P((char *, char *));
341 static char *absolute_filename
__P((char *, char *));
342 static char *absolute_dirname
__P((char *, char *));
343 static bool filename_is_absolute
__P((char *f
));
344 static void canonicalize_filename
__P((char *));
345 static void linebuffer_setlen
__P((linebuffer
*, int));
346 static PTR xmalloc
__P((unsigned int));
347 static PTR xrealloc
__P((char *, unsigned int));
350 static char searchar
= '/'; /* use /.../ searches */
352 static char *tagfile
; /* output file */
353 static char *progname
; /* name this program was invoked with */
354 static char *cwd
; /* current working directory */
355 static char *tagfiledir
; /* directory of tagfile */
356 static FILE *tagf
; /* ioptr for tags file */
358 static char *curfile
; /* current input file name */
359 static language
*curlang
; /* current language */
361 static int lineno
; /* line number of current line */
362 static long charno
; /* current character number */
363 static long linecharno
; /* charno of start of current line */
364 static char *dbp
; /* pointer to start of current tag */
365 static bool nocharno
; /* only use line number when making tag */
366 static const int invalidcharno
= -1;
368 static node
*head
; /* the head of the binary tree of tags */
370 static linebuffer lb
; /* the current line */
372 /* boolean "functions" (see init) */
373 static bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
376 *white
= " \f\t\n\r\v",
378 *nonam
= " \f\t\n\r()=,;",
379 /* token ending chars */
380 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
381 /* token starting chars */
382 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
383 /* valid in-token chars */
384 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
386 static bool append_to_tagfile
; /* -a: append to tags */
387 /* The following four default to TRUE for etags, but to FALSE for ctags. */
388 static bool typedefs
; /* -t: create tags for C and Ada typedefs */
389 static bool typedefs_or_cplusplus
; /* -T: create tags for C typedefs, level */
390 /* 0 struct/enum/union decls, and C++ */
391 /* member functions. */
392 static bool constantypedefs
; /* -d: create tags for C #define, enum */
393 /* constants and variables. */
394 /* -D: opposite of -d. Default under ctags. */
395 static bool declarations
; /* --declarations: tag them and extern in C&Co*/
396 static bool globals
; /* create tags for global variables */
397 static bool no_line_directive
; /* ignore #line directives */
398 static bool members
; /* create tags for C member variables */
399 static bool update
; /* -u: update tags */
400 static bool vgrind_style
; /* -v: create vgrind style index output */
401 static bool no_warnings
; /* -w: suppress warnings */
402 static bool cxref_style
; /* -x: create cxref style output */
403 static bool cplusplus
; /* .[hc] means C++, not C */
404 static bool noindentypedefs
; /* -I: ignore indentation in C */
405 static bool packages_only
; /* --packages-only: in Ada, only tag packages*/
408 static struct option longopts
[] =
410 { "packages-only", no_argument
, &packages_only
, TRUE
},
411 { "append", no_argument
, NULL
, 'a' },
412 { "backward-search", no_argument
, NULL
, 'B' },
413 { "c++", no_argument
, NULL
, 'C' },
414 { "cxref", no_argument
, NULL
, 'x' },
415 { "defines", no_argument
, NULL
, 'd' },
416 { "declarations", no_argument
, &declarations
, TRUE
},
417 { "no-defines", no_argument
, NULL
, 'D' },
418 { "globals", no_argument
, &globals
, TRUE
},
419 { "no-globals", no_argument
, &globals
, FALSE
},
420 { "no-line-directive", no_argument
, &no_line_directive
, TRUE
},
421 { "help", no_argument
, NULL
, 'h' },
422 { "help", no_argument
, NULL
, 'H' },
423 { "ignore-indentation", no_argument
, NULL
, 'I' },
424 { "include", required_argument
, NULL
, 'i' },
425 { "language", required_argument
, NULL
, 'l' },
426 { "members", no_argument
, &members
, TRUE
},
427 { "no-members", no_argument
, &members
, FALSE
},
428 { "no-warn", no_argument
, NULL
, 'w' },
429 { "output", required_argument
, NULL
, 'o' },
431 { "regex", required_argument
, NULL
, 'r' },
432 { "no-regex", no_argument
, NULL
, 'R' },
433 { "ignore-case-regex", required_argument
, NULL
, 'c' },
434 #endif /* ETAGS_REGEXPS */
435 { "typedefs", no_argument
, NULL
, 't' },
436 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
437 { "update", no_argument
, NULL
, 'u' },
438 { "version", no_argument
, NULL
, 'V' },
439 { "vgrind", no_argument
, NULL
, 'v' },
442 #endif /* LONG_OPTIONS */
445 /* Structure defining a regular expression. Elements are
446 the compiled pattern, and the name string. */
447 typedef struct pattern
449 struct pattern
*p_next
;
452 struct re_pattern_buffer
*pat
;
453 struct re_registers regs
;
458 /* List of all regexps. */
459 static pattern
*p_head
= NULL
;
461 /* How many characters in the character set. (From regex.c.) */
462 #define CHAR_SET_SIZE 256
463 /* Translation table for case-insensitive matching. */
464 static char lc_trans
[CHAR_SET_SIZE
];
465 #endif /* ETAGS_REGEXPS */
467 static compressor compressors
[] =
469 { "z", "gzip -d -c"},
470 { "Z", "gzip -d -c"},
471 { "gz", "gzip -d -c"},
472 { "GZ", "gzip -d -c"},
473 { "bz2", "bzip2 -d -c" },
481 /* Non-NULL if language fixed. */
482 static language
*forced_lang
= NULL
;
485 static char *Ada_suffixes
[] =
486 { "ads", "adb", "ada", NULL
};
489 static char *Asm_suffixes
[] =
490 { "a", /* Unix assembler */
491 "asm", /* Microcontroller assembly */
492 "def", /* BSO/Tasking definition includes */
493 "inc", /* Microcontroller include files */
494 "ins", /* Microcontroller include files */
495 "s", "sa", /* Unix assembler */
496 "S", /* cpp-processed Unix assembler */
497 "src", /* BSO/Tasking C compiler output */
501 /* Note that .c and .h can be considered C++, if the --c++ flag was
502 given, or if the `class' keyowrd is met inside the file.
503 That is why default_C_entries is called for these. */
504 static char *default_C_suffixes
[] =
507 static char *Cplusplus_suffixes
[] =
508 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
509 "M", /* Objective C++ */
510 "pdb", /* Postscript with C syntax */
513 static char *Cjava_suffixes
[] =
516 static char *Cobol_suffixes
[] =
517 { "COB", "cob", NULL
};
519 static char *Cstar_suffixes
[] =
520 { "cs", "hs", NULL
};
522 static char *Erlang_suffixes
[] =
523 { "erl", "hrl", NULL
};
525 static char *Fortran_suffixes
[] =
526 { "F", "f", "f90", "for", NULL
};
528 static char *Lisp_suffixes
[] =
529 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
531 static char *Makefile_filenames
[] =
532 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
534 static char *Pascal_suffixes
[] =
535 { "p", "pas", NULL
};
537 static char *Perl_suffixes
[] =
538 { "pl", "pm", NULL
};
540 static char *Perl_interpreters
[] =
541 { "perl", "@PERL@", NULL
};
543 static char *PHP_suffixes
[] =
544 { "php", "php3", "php4", NULL
};
546 static char *plain_C_suffixes
[] =
547 { "lm", /* Objective lex file */
548 "m", /* Objective C file */
549 "pc", /* Pro*C file */
552 static char *Postscript_suffixes
[] =
553 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
555 static char *Prolog_suffixes
[] =
558 static char *Python_suffixes
[] =
561 /* Can't do the `SCM' or `scm' prefix with a version number. */
562 static char *Scheme_suffixes
[] =
563 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
565 static char *TeX_suffixes
[] =
566 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
568 static char *Texinfo_suffixes
[] =
569 { "texi", "texinfo", "txi", NULL
};
571 static char *Yacc_suffixes
[] =
572 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
575 * Table of languages.
577 * It is ok for a given function to be listed under more than one
578 * name. I just didn't.
581 static language lang_names
[] =
583 { "ada", Ada_funcs
, NULL
, Ada_suffixes
, NULL
},
584 { "asm", Asm_labels
, NULL
, Asm_suffixes
, NULL
},
585 { "c", default_C_entries
, NULL
, default_C_suffixes
, NULL
},
586 { "c++", Cplusplus_entries
, NULL
, Cplusplus_suffixes
, NULL
},
587 { "c*", Cstar_entries
, NULL
, Cstar_suffixes
, NULL
},
588 { "cobol", Cobol_paragraphs
, NULL
, Cobol_suffixes
, NULL
},
589 { "erlang", Erlang_functions
, NULL
, Erlang_suffixes
, NULL
},
590 { "fortran", Fortran_functions
, NULL
, Fortran_suffixes
, NULL
},
591 { "java", Cjava_entries
, NULL
, Cjava_suffixes
, NULL
},
592 { "lisp", Lisp_functions
, NULL
, Lisp_suffixes
, NULL
},
593 { "makefile", Makefile_targets
, Makefile_filenames
, NULL
, NULL
},
594 { "pascal", Pascal_functions
, NULL
, Pascal_suffixes
, NULL
},
595 { "perl", Perl_functions
, NULL
, Perl_suffixes
, Perl_interpreters
},
596 { "php", PHP_functions
, NULL
, PHP_suffixes
, NULL
},
597 { "postscript", Postscript_functions
, NULL
, Postscript_suffixes
, NULL
},
598 { "proc", plain_C_entries
, NULL
, plain_C_suffixes
, NULL
},
599 { "prolog", Prolog_functions
, NULL
, Prolog_suffixes
, NULL
},
600 { "python", Python_functions
, NULL
, Python_suffixes
, NULL
},
601 { "scheme", Scheme_functions
, NULL
, Scheme_suffixes
, NULL
},
602 { "tex", TeX_commands
, NULL
, TeX_suffixes
, NULL
},
603 { "texinfo", Texinfo_nodes
, NULL
, Texinfo_suffixes
, NULL
},
604 { "yacc", Yacc_entries
, NULL
, Yacc_suffixes
, NULL
},
605 { "auto", NULL
}, /* default guessing scheme */
606 { "none", just_read_file
}, /* regexp matching only */
607 { NULL
, NULL
} /* end of list */
612 print_language_names ()
617 puts ("\nThese are the currently supported languages, along with the\n\
618 default file names and dot suffixes:");
619 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
621 printf (" %-*s", 10, lang
->name
);
622 if (lang
->filenames
!= NULL
)
623 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
624 printf (" %s", *name
);
625 if (lang
->suffixes
!= NULL
)
626 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
627 printf (" .%s", *ext
);
630 puts ("Where `auto' means use default language for files based on file\n\
631 name suffix, and `none' means only do regexp processing on files.\n\
632 If no language is specified and no matching suffix is found,\n\
633 the first line of the file is read for a sharp-bang (#!) sequence\n\
634 followed by the name of an interpreter. If no such sequence is found,\n\
635 Fortran is tried first; if no tags are found, C is tried next.\n\
636 When parsing any C file, a \"class\" keyword switches to C++.\n\
637 Compressed files are supported using gzip and bzip2.");
641 # define EMACS_NAME "GNU Emacs"
644 # define VERSION "21"
649 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
650 puts ("Copyright (C) 1999 Free Software Foundation, Inc. and Ken Arnold");
651 puts ("This program is distributed under the same terms as Emacs");
659 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
661 These are the options accepted by %s.\n", progname
, progname
);
663 puts ("You may use unambiguous abbreviations for the long option names.");
665 puts ("Long option names do not work with this executable, as it is not\n\
666 linked with GNU getopt.");
667 #endif /* LONG_OPTIONS */
668 puts ("A - as file name means read names from stdin (one per line).");
670 printf (" Absolute names are stored in the output file as they are.\n\
671 Relative ones are stored relative to the output file's directory.");
674 puts ("-a, --append\n\
675 Append tag entries to existing tags file.");
677 puts ("--packages-only\n\
678 For Ada files, only generate tags for packages .");
681 puts ("-B, --backward-search\n\
682 Write the search commands for the tag entries using '?', the\n\
683 backward-search command instead of '/', the forward-search command.");
685 /* This option is mostly obsolete, because etags can now automatically
686 detect C++. Retained for backward compatibility and for debugging and
687 experimentation. In principle, we could want to tag as C++ even
688 before any "class" keyword.
690 Treat files whose name suffix defaults to C language as C++ files.");
693 puts ("--declarations\n\
694 In C and derived languages, create tags for function declarations,");
696 puts ("\tand create tags for extern variables if --globals is used.");
699 ("\tand create tags for extern variables unless --no-globals is used.");
702 puts ("-d, --defines\n\
703 Create tag entries for C #define constants and enum constants, too.");
705 puts ("-D, --no-defines\n\
706 Don't create tag entries for C #define constants and enum constants.\n\
707 This makes the tags file smaller.");
711 puts ("-i FILE, --include=FILE\n\
712 Include a note in tag file indicating that, when searching for\n\
713 a tag, one should also consult the tags file FILE after\n\
714 checking the current file.");
715 puts ("-l LANG, --language=LANG\n\
716 Force the following files to be considered as written in the\n\
717 named language up to the next --language=LANG option.");
722 Create tag entries for global variables in some languages.");
724 puts ("--no-globals\n\
725 Do not create tag entries for global variables in some\n\
726 languages. This makes the tags file smaller.");
728 Create tag entries for member variables in C and derived languages.");
731 puts ("-r /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
732 Make a tag for each line matching pattern REGEXP in the following\n\
733 files. {LANGUAGE}/REGEXP/ uses REGEXP for LANGUAGE files only.\n\
734 regexfile is a file containing one REGEXP per line.\n\
735 REGEXP is anchored (as if preceded by ^).\n\
736 The form /REGEXP/NAME/ creates a named tag.\n\
737 For example Tcl named tags can be created with:\n\
738 --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\"");
739 puts ("-c /REGEXP/, --ignore-case-regex=/REGEXP/ or --ignore-case-regex=@regexfile\n\
740 Like -r, --regex but ignore case when matching expressions.");
741 puts ("-R, --no-regex\n\
742 Don't create tags from regexps for the following files.");
743 #endif /* ETAGS_REGEXPS */
744 puts ("-o FILE, --output=FILE\n\
745 Write the tags to FILE.");
746 puts ("-I, --ignore-indentation\n\
747 Don't rely on indentation quite as much as normal. Currently,\n\
748 this means not to assume that a closing brace in the first\n\
749 column is the final brace of a function or structure\n\
750 definition in C and C++.");
754 puts ("-t, --typedefs\n\
755 Generate tag entries for C and Ada typedefs.");
756 puts ("-T, --typedefs-and-c++\n\
757 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
758 and C++ member functions.");
759 puts ("-u, --update\n\
760 Update the tag entries for the given files, leaving tag\n\
761 entries for other files in place. Currently, this is\n\
762 implemented by deleting the existing entries for the given\n\
763 files and then rewriting the new entries at the end of the\n\
764 tags file. It is often faster to simply rebuild the entire\n\
765 tag file than to use this.");
766 puts ("-v, --vgrind\n\
767 Generates an index of items intended for human consumption,\n\
768 similar to the output of vgrind. The index is sorted, and\n\
769 gives the page number of each item.");
770 puts ("-w, --no-warn\n\
771 Suppress warning messages about entries defined in multiple\n\
773 puts ("-x, --cxref\n\
774 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
775 The output uses line numbers instead of page numbers, but\n\
776 beyond that the differences are cosmetic; try both to see\n\
780 puts ("-V, --version\n\
781 Print the version of the program.\n\
783 Print this help message.");
785 print_language_names ();
788 puts ("Report bugs to bug-gnu-emacs@gnu.org");
802 /* This structure helps us allow mixing of --lang and file names. */
805 enum argument_type arg_type
;
807 language
*lang
; /* language of the regexp */
810 #ifdef VMS /* VMS specific functions */
814 /* This is a BUG! ANY arbitrary limit is a BUG!
815 Won't someone please fix this? */
816 #define MAX_FILE_SPEC_LEN 255
819 char body
[MAX_FILE_SPEC_LEN
+ 1];
823 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
824 returning in each successive call the next file name matching the input
825 spec. The function expects that each in_spec passed
826 to it will be processed to completion; in particular, up to and
827 including the call following that in which the last matching name
828 is returned, the function ignores the value of in_spec, and will
829 only start processing a new spec with the following call.
830 If an error occurs, on return out_spec contains the value
831 of in_spec when the error occurred.
833 With each successive file name returned in out_spec, the
834 function's return value is one. When there are no more matching
835 names the function returns zero. If on the first call no file
836 matches in_spec, or there is any other error, -1 is returned.
841 #define OUTSIZE MAX_FILE_SPEC_LEN
847 static long context
= 0;
848 static struct dsc$descriptor_s o
;
849 static struct dsc$descriptor_s i
;
850 static bool pass1
= TRUE
;
857 o
.dsc$a_pointer
= (char *) out
;
858 o
.dsc$w_length
= (short)OUTSIZE
;
859 i
.dsc$a_pointer
= in
;
860 i
.dsc$w_length
= (short)strlen(in
);
861 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
862 i
.dsc$b_class
= DSC$K_CLASS_S
;
863 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
864 o
.dsc$b_class
= DSC$K_CLASS_VS
;
866 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
868 out
->body
[out
->curlen
] = EOS
;
871 else if (status
== RMS$_NMF
)
875 strcpy(out
->body
, in
);
878 lib$
find_file_end(&context
);
884 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
885 name of each file specified by the provided arg expanding wildcards.
888 gfnames (arg
, p_error
)
892 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
894 switch (fn_exp (&filename
, arg
))
898 return filename
.body
;
904 return filename
.body
;
908 #ifndef OLD /* Newer versions of VMS do provide `system'. */
912 error ("%s", "system() function not implemented under VMS");
916 #define VERSION_DELIM ';'
917 char *massage_name (s
)
923 if (*s
== VERSION_DELIM
)
941 unsigned int nincluded_files
;
942 char **included_files
;
945 int current_arg
, file_count
;
946 linebuffer filename_lb
;
952 _fmode
= O_BINARY
; /* all of files are treated as binary files */
957 included_files
= xnew (argc
, char *);
961 /* Allocate enough no matter what happens. Overkill, but each one
963 argbuffer
= xnew (argc
, argument
);
966 /* Set syntax for regular expression routines. */
967 re_set_syntax (RE_SYNTAX_EMACS
| RE_INTERVALS
);
968 /* Translation table for case-insensitive search. */
969 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
970 lc_trans
[i
] = lowcase (i
);
971 #endif /* ETAGS_REGEXPS */
974 * If etags, always find typedefs and structure tags. Why not?
975 * Also default to find macro constants, enum constants and
980 typedefs
= typedefs_or_cplusplus
= constantypedefs
= TRUE
;
982 declarations
= FALSE
;
992 optstring
= "-aCdDf:Il:o:r:c:RStTi:BuvxwVhH";
994 optstring
= "-aCdDf:Il:o:StTi:BuvxwVhH";
995 #endif /* ETAGS_REGEXPS */
998 optstring
= optstring
+ 1;
999 #endif /* LONG_OPTIONS */
1001 opt
= getopt_long (argc
, argv
, optstring
, longopts
, 0);
1008 /* If getopt returns 0, then it has already processed a
1009 long-named option. We should do nothing. */
1013 /* This means that a file name has been seen. Record it. */
1014 argbuffer
[current_arg
].arg_type
= at_filename
;
1015 argbuffer
[current_arg
].what
= optarg
;
1020 /* Common options. */
1021 case 'a': append_to_tagfile
= TRUE
; break;
1022 case 'C': cplusplus
= TRUE
; break;
1023 case 'd': constantypedefs
= TRUE
; break;
1024 case 'D': constantypedefs
= FALSE
; break;
1025 case 'f': /* for compatibility with old makefiles */
1029 error ("-o option may only be given once.", (char *)NULL
);
1030 suggest_asking_for_help ();
1035 case 'S': /* for backward compatibility */
1036 noindentypedefs
= TRUE
;
1040 language
*lang
= get_language_from_langname (optarg
);
1043 argbuffer
[current_arg
].lang
= lang
;
1044 argbuffer
[current_arg
].arg_type
= at_language
;
1050 argbuffer
[current_arg
].arg_type
= at_regexp
;
1051 argbuffer
[current_arg
].what
= optarg
;
1055 argbuffer
[current_arg
].arg_type
= at_regexp
;
1056 argbuffer
[current_arg
].what
= NULL
;
1060 argbuffer
[current_arg
].arg_type
= at_icregexp
;
1061 argbuffer
[current_arg
].what
= optarg
;
1075 typedefs
= typedefs_or_cplusplus
= TRUE
;
1079 included_files
[nincluded_files
++] = optarg
;
1081 /* Ctags options. */
1082 case 'B': searchar
= '?'; break;
1083 case 'u': update
= TRUE
; break;
1084 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1085 case 'x': cxref_style
= TRUE
; break;
1086 case 'w': no_warnings
= TRUE
; break;
1088 suggest_asking_for_help ();
1092 for (; optind
< argc
; ++optind
)
1094 argbuffer
[current_arg
].arg_type
= at_filename
;
1095 argbuffer
[current_arg
].what
= argv
[optind
];
1100 if (nincluded_files
== 0 && file_count
== 0)
1102 error ("no input files specified.", (char *)NULL
);
1103 suggest_asking_for_help ();
1106 if (tagfile
== NULL
)
1107 tagfile
= CTAGS
? "tags" : "TAGS";
1108 cwd
= etags_getcwd (); /* the current working directory */
1109 if (cwd
[strlen (cwd
) - 1] != '/')
1112 cwd
= concat (oldcwd
, "/", "");
1115 if (streq (tagfile
, "-"))
1118 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1120 init (); /* set up boolean "functions" */
1123 initbuffer (&filename_lb
);
1127 if (streq (tagfile
, "-"))
1131 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1132 doesn't take effect until after `stdout' is already open). */
1133 if (!isatty (fileno (stdout
)))
1134 setmode (fileno (stdout
), O_BINARY
);
1138 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1144 * Loop through files finding functions.
1146 for (i
= 0; i
< current_arg
; ++i
)
1148 switch (argbuffer
[i
].arg_type
)
1151 forced_lang
= argbuffer
[i
].lang
;
1153 #ifdef ETAGS_REGEXPS
1155 analyse_regex (argbuffer
[i
].what
, FALSE
);
1158 analyse_regex (argbuffer
[i
].what
, TRUE
);
1163 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1167 error ("can't find file %s\n", this_file
);
1172 this_file
= massage_name (this_file
);
1175 this_file
= argbuffer
[i
].what
;
1177 /* Input file named "-" means read file names from stdin
1178 (one per line) and use them. */
1179 if (streq (this_file
, "-"))
1180 while (readline_internal (&filename_lb
, stdin
) > 0)
1181 process_file (filename_lb
.buffer
);
1183 process_file (this_file
);
1191 #ifdef ETAGS_REGEXPS
1193 #endif /* ETAGS_REGEXPS */
1195 if (!CTAGS
|| cxref_style
)
1201 while (nincluded_files
-- > 0)
1202 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1204 if (fclose (tagf
) == EOF
)
1212 for (i
= 0; i
< current_arg
; ++i
)
1214 if (argbuffer
[i
].arg_type
!= at_filename
)
1217 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1218 tagfile
, argbuffer
[i
].what
, tagfile
);
1219 if (system (cmd
) != GOOD
)
1220 fatal ("failed to execute shell command", (char *)NULL
);
1222 append_to_tagfile
= TRUE
;
1225 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1231 if (fclose (tagf
) == EOF
)
1237 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
1238 exit (system (cmd
));
1246 * Return a compressor given the file name. If EXTPTR is non-zero,
1247 * return a pointer into FILE where the compressor-specific
1248 * extension begins. If no compressor is found, NULL is returned
1249 * and EXTPTR is not significant.
1250 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1253 get_compressor_from_suffix (file
, extptr
)
1258 char *slash
, *suffix
;
1260 /* This relies on FN to be after canonicalize_filename,
1261 so we don't need to consider backslashes on DOS_NT. */
1262 slash
= etags_strrchr (file
, '/');
1263 suffix
= etags_strrchr (file
, '.');
1264 if (suffix
== NULL
|| suffix
< slash
)
1269 /* Let those poor souls who live with DOS 8+3 file name limits get
1270 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1271 Only the first do loop is run if not MSDOS */
1274 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1275 if (streq (compr
->suffix
, suffix
))
1278 break; /* do it only once: not really a loop */
1281 } while (*suffix
!= '\0');
1288 * Return a language given the name.
1291 get_language_from_langname (name
)
1297 error ("empty language name", (char *)NULL
);
1300 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1301 if (streq (name
, lang
->name
))
1303 error ("unknown language \"%s\"", name
);
1311 * Return a language given the interpreter name.
1314 get_language_from_interpreter (interpreter
)
1320 if (interpreter
== NULL
)
1322 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1323 if (lang
->interpreters
!= NULL
)
1324 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1325 if (streq (*iname
, interpreter
))
1334 * Return a language given the file name.
1337 get_language_from_filename (file
)
1341 char **name
, **ext
, *suffix
;
1343 /* Try whole file name first. */
1344 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1345 if (lang
->filenames
!= NULL
)
1346 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1347 if (streq (*name
, file
))
1350 /* If not found, try suffix after last dot. */
1351 suffix
= etags_strrchr (file
, '.');
1355 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1356 if (lang
->suffixes
!= NULL
)
1357 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1358 if (streq (*ext
, suffix
))
1366 * This routine is called on each file argument.
1372 struct stat stat_buf
;
1375 char *compressed_name
, *uncompressed_name
;
1376 char *ext
, *real_name
;
1380 canonicalize_filename (file
);
1381 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1383 error ("skipping inclusion of %s in self.", file
);
1386 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1388 compressed_name
= NULL
;
1389 real_name
= uncompressed_name
= savestr (file
);
1393 real_name
= compressed_name
= savestr (file
);
1394 uncompressed_name
= savenstr (file
, ext
- file
);
1397 /* If the canonicalised uncompressed name has already be dealt with,
1398 skip it silently, else add it to the list. */
1400 typedef struct processed_file
1403 struct processed_file
*next
;
1405 static processed_file
*pf_head
= NULL
;
1406 register processed_file
*fnp
;
1408 for (fnp
= pf_head
; fnp
!= NULL
; fnp
= fnp
->next
)
1409 if (streq (uncompressed_name
, fnp
->filename
))
1412 pf_head
= xnew (1, struct processed_file
);
1413 pf_head
->filename
= savestr (uncompressed_name
);
1414 pf_head
->next
= fnp
;
1417 if (stat (real_name
, &stat_buf
) != 0)
1419 /* Reset real_name and try with a different name. */
1421 if (compressed_name
!= NULL
) /* try with the given suffix */
1423 if (stat (uncompressed_name
, &stat_buf
) == 0)
1424 real_name
= uncompressed_name
;
1426 else /* try all possible suffixes */
1428 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1430 compressed_name
= concat (file
, ".", compr
->suffix
);
1431 if (stat (compressed_name
, &stat_buf
) != 0)
1435 char *suf
= compressed_name
+ strlen (file
);
1436 size_t suflen
= strlen (compr
->suffix
) + 1;
1437 for ( ; suf
[1]; suf
++, suflen
--)
1439 memmove (suf
, suf
+ 1, suflen
);
1440 if (stat (compressed_name
, &stat_buf
) == 0)
1442 real_name
= compressed_name
;
1446 if (real_name
!= NULL
)
1449 free (compressed_name
);
1450 compressed_name
= NULL
;
1454 real_name
= compressed_name
;
1459 if (real_name
== NULL
)
1464 } /* try with a different name */
1466 if (!S_ISREG (stat_buf
.st_mode
))
1468 error ("skipping %s: it is not a regular file.", real_name
);
1471 if (real_name
== compressed_name
)
1473 char *cmd
= concat (compr
->command
, " ", real_name
);
1474 inf
= (FILE *) popen (cmd
, "r");
1478 inf
= fopen (real_name
, "r");
1485 if (filename_is_absolute (uncompressed_name
))
1487 /* file is an absolute file name. Canonicalise it. */
1488 curfile
= absolute_filename (uncompressed_name
, cwd
);
1492 /* file is a file name relative to cwd. Make it relative
1493 to the directory of the tags file. */
1494 curfile
= relative_filename (uncompressed_name
, tagfiledir
);
1496 nocharno
= FALSE
; /* use char position when making tags */
1497 find_entries (uncompressed_name
, inf
);
1499 if (real_name
== compressed_name
)
1500 retval
= pclose (inf
);
1502 retval
= fclose (inf
);
1507 if (compressed_name
) free(compressed_name
);
1508 if (uncompressed_name
) free(uncompressed_name
);
1513 * This routine sets up the boolean pseudo-functions which work
1514 * by setting boolean flags dependent upon the corresponding character.
1515 * Every char which is NOT in that string is not a white char. Therefore,
1516 * all of the array "_wht" is set to FALSE, and then the elements
1517 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1518 * of a char is TRUE if it is the string "white", else FALSE.
1526 for (i
= 0; i
< CHARS
; i
++)
1527 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1528 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1529 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1530 notinname('\0') = notinname('\n');
1531 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1532 begtoken('\0') = begtoken('\n');
1533 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1534 intoken('\0') = intoken('\n');
1535 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1536 endtoken('\0') = endtoken('\n');
1540 * This routine opens the specified file and calls the function
1541 * which finds the function and type definitions.
1543 static node
*last_node
= NULL
;
1546 find_entries (file
, inf
)
1552 node
*old_last_node
;
1554 /* If user specified a language, use it. */
1556 if (lang
!= NULL
&& lang
->function
!= NULL
)
1559 lang
->function (inf
);
1563 /* Try to guess the language given the file name. */
1564 lang
= get_language_from_filename (file
);
1565 if (lang
!= NULL
&& lang
->function
!= NULL
)
1568 lang
->function (inf
);
1572 /* Look for sharp-bang as the first two characters. */
1573 if (readline_internal (&lb
, inf
) > 0
1575 && lb
.buffer
[0] == '#'
1576 && lb
.buffer
[1] == '!')
1580 /* Set lp to point at the first char after the last slash in the
1581 line or, if no slashes, at the first nonblank. Then set cp to
1582 the first successive blank and terminate the string. */
1583 lp
= etags_strrchr (lb
.buffer
+2, '/');
1587 lp
= skip_spaces (lb
.buffer
+ 2);
1588 cp
= skip_non_spaces (lp
);
1591 if (strlen (lp
) > 0)
1593 lang
= get_language_from_interpreter (lp
);
1594 if (lang
!= NULL
&& lang
->function
!= NULL
)
1597 lang
->function (inf
);
1602 /* We rewind here, even if inf may be a pipe. We fail if the
1603 length of the first line is longer than the pipe block size,
1604 which is unlikely. */
1608 old_last_node
= last_node
;
1609 curlang
= get_language_from_langname ("fortran");
1610 Fortran_functions (inf
);
1612 /* No Fortran entries found. Try C. */
1613 if (old_last_node
== last_node
)
1615 /* We do not tag if rewind fails.
1616 Only the file name will be recorded in the tags file. */
1618 curlang
= get_language_from_langname (cplusplus
? "c++" : "c");
1619 default_C_entries (inf
);
1627 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1628 char *name
; /* tag name, or NULL if unnamed */
1629 bool is_func
; /* tag is a function */
1630 char *linestart
; /* start of the line where tag is */
1631 int linelen
; /* length of the line where tag is */
1632 int lno
; /* line number */
1633 long cno
; /* character number */
1637 if (CTAGS
&& name
== NULL
)
1640 np
= xnew (1, node
);
1642 /* If ctags mode, change name "main" to M<thisfilename>. */
1643 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1645 register char *fp
= etags_strrchr (curfile
, '/');
1646 np
->name
= concat ("M", fp
== NULL
? curfile
: fp
+ 1, "");
1647 fp
= etags_strrchr (np
->name
, '.');
1648 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
1653 np
->been_warned
= FALSE
;
1655 np
->is_func
= is_func
;
1658 np
->cno
= invalidcharno
;
1660 /* Our char numbers are 0-base, because of C language tradition?
1661 ctags compatibility? old versions compatibility? I don't know.
1662 Anyway, since emacs's are 1-base we expect etags.el to take care
1663 of the difference. If we wanted to have 1-based numbers, we would
1664 uncomment the +1 below. */
1665 np
->cno
= cno
/* + 1 */ ;
1666 np
->left
= np
->right
= NULL
;
1667 if (CTAGS
&& !cxref_style
)
1669 if (strlen (linestart
) < 50)
1670 np
->pat
= concat (linestart
, "$", "");
1672 np
->pat
= savenstr (linestart
, 50);
1675 np
->pat
= savenstr (linestart
, linelen
);
1677 add_node (np
, &head
);
1681 * TAGS format specification
1682 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1684 * pfnote should emit the optimized form [unnamed tag] only if:
1685 * 1. name does not contain any of the characters " \t\r\n(),;";
1686 * 2. linestart contains name as either a rightmost, or rightmost but
1687 * one character, substring;
1688 * 3. the character, if any, immediately before name in linestart must
1689 * be one of the characters " \t(),;";
1690 * 4. the character, if any, immediately after name in linestart must
1691 * also be one of the characters " \t(),;".
1693 * The real implementation uses the notinname() macro, which recognises
1694 * characters slightly different from " \t\r\n(),;". See the variable
1697 #define traditional_tag_style TRUE
1699 new_pfnote (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1700 char *name
; /* tag name, or NULL if unnamed */
1701 int namelen
; /* tag length */
1702 bool is_func
; /* tag is a function */
1703 char *linestart
; /* start of the line where tag is */
1704 int linelen
; /* length of the line where tag is */
1705 int lno
; /* line number */
1706 long cno
; /* character number */
1714 for (cp
= name
; !notinname (*cp
); cp
++)
1716 if (*cp
== '\0') /* rule #1 */
1718 cp
= linestart
+ linelen
- namelen
;
1719 if (notinname (linestart
[linelen
-1]))
1720 cp
-= 1; /* rule #4 */
1721 if (cp
>= linestart
/* rule #2 */
1723 || notinname (cp
[-1])) /* rule #3 */
1724 && strneq (name
, cp
, namelen
)) /* rule #2 */
1725 named
= FALSE
; /* use unnamed tag */
1730 name
= savenstr (name
, namelen
);
1733 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
1738 * recurse on left children, iterate on right children.
1746 register node
*node_right
= np
->right
;
1747 free_tree (np
->left
);
1748 if (np
->name
!= NULL
)
1758 * Adds a node to the tree of nodes. In etags mode, sort by file
1759 * name. In ctags mode, sort by tag name. Make no attempt at
1762 * add_node is the only function allowed to add nodes, so it can
1766 add_node (np
, cur_node_p
)
1767 node
*np
, **cur_node_p
;
1770 register node
*cur_node
= *cur_node_p
;
1772 if (cur_node
== NULL
)
1782 assert (last_node
!= NULL
);
1783 /* For each file name, tags are in a linked sublist on the right
1784 pointer. The first tags of different files are a linked list
1785 on the left pointer. last_node points to the end of the last
1787 if (last_node
->file
== np
->file
)
1789 /* Let's use the same sublist as the last added node. */
1790 last_node
->right
= np
;
1793 else if (streq (cur_node
->file
, np
->file
))
1795 /* Scanning the list we found the head of a sublist which is
1796 good for us. Let's scan this sublist. */
1797 add_node (np
, &cur_node
->right
);
1800 /* The head of this sublist is not good for us. Let's try the
1802 add_node (np
, &cur_node
->left
);
1807 dif
= strcmp (np
->name
, cur_node
->name
);
1810 * If this tag name matches an existing one, then
1811 * do not add the node, but maybe print a warning.
1815 if (streq (np
->file
, cur_node
->file
))
1819 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1820 np
->file
, lineno
, np
->name
);
1821 fprintf (stderr
, "Second entry ignored\n");
1824 else if (!cur_node
->been_warned
&& !no_warnings
)
1828 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1829 np
->file
, cur_node
->file
, np
->name
);
1830 cur_node
->been_warned
= TRUE
;
1835 /* Actually add the node */
1836 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1842 static int total_size_of_entries
__P((node
*));
1843 static int number_len
__P((long));
1845 /* Length of a number's decimal representation. */
1851 while ((num
/= 10) > 0)
1857 * Return total number of characters that put_entries will output for
1858 * the nodes in the linked list at the right of the specified node.
1859 * This count is irrelevant with etags.el since emacs 19.34 at least,
1860 * but is still supplied for backward compatibility.
1863 total_size_of_entries (np
)
1866 register int total
= 0;
1868 for (; np
!= NULL
; np
= np
->right
)
1870 total
+= strlen (np
->pat
) + 1; /* pat\177 */
1871 if (np
->name
!= NULL
)
1872 total
+= strlen (np
->name
) + 1; /* name\001 */
1873 total
+= number_len ((long) np
->lno
) + 1; /* lno, */
1874 if (np
->cno
!= invalidcharno
) /* cno */
1875 total
+= number_len (np
->cno
);
1876 total
+= 1; /* newline */
1888 static char *file
= NULL
;
1893 /* Output subentries that precede this one */
1895 put_entries (np
->left
);
1897 /* Output this entry */
1901 if (file
!= np
->file
1902 && (file
== NULL
|| !streq (file
, np
->file
)))
1905 fprintf (tagf
, "\f\n%s,%d\n",
1906 file
, total_size_of_entries (np
));
1908 fputs (np
->pat
, tagf
);
1909 fputc ('\177', tagf
);
1910 if (np
->name
!= NULL
)
1912 fputs (np
->name
, tagf
);
1913 fputc ('\001', tagf
);
1915 fprintf (tagf
, "%d,", np
->lno
);
1916 if (np
->cno
== invalidcharno
)
1919 fprintf (tagf
, "%ld\n", np
->cno
);
1924 if (np
->name
== NULL
)
1925 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
1930 fprintf (stdout
, "%s %s %d\n",
1931 np
->name
, np
->file
, (np
->lno
+ 63) / 64);
1933 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1934 np
->name
, np
->lno
, np
->file
, np
->pat
);
1938 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->file
);
1942 putc (searchar
, tagf
);
1945 for (sp
= np
->pat
; *sp
; sp
++)
1947 if (*sp
== '\\' || *sp
== searchar
)
1951 putc (searchar
, tagf
);
1954 { /* a typedef; text pattern inadequate */
1955 fprintf (tagf
, "%d", np
->lno
);
1962 /* Output subentries that follow this one */
1963 put_entries (np
->right
);
1965 put_entries (np
->left
);
1970 #define C_EXT 0x00fff /* C extensions */
1971 #define C_PLAIN 0x00000 /* C */
1972 #define C_PLPL 0x00001 /* C++ */
1973 #define C_STAR 0x00003 /* C* */
1974 #define C_JAVA 0x00005 /* JAVA */
1975 #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
1976 #define YACC 0x10000 /* yacc file */
1979 * The C symbol tables.
1984 st_C_objprot
, st_C_objimpl
, st_C_objend
,
1989 st_C_class
, st_C_template
,
1990 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1993 static unsigned int hash
__P((const char *, unsigned int));
1994 static struct C_stab_entry
* in_word_set
__P((const char *, unsigned int));
1995 static enum sym_type C_symtype
__P((char *, int, int));
1997 /* Feed stuff between (but not including) %[ and %] lines to:
1998 gperf -c -k 1,3 -o -p -r -t
2000 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
2004 while, 0, st_C_ignore
2005 switch, 0, st_C_ignore
2006 return, 0, st_C_ignore
2007 @interface, 0, st_C_objprot
2008 @protocol, 0, st_C_objprot
2009 @implementation,0, st_C_objimpl
2010 @end, 0, st_C_objend
2011 import, C_JAVA, st_C_ignore
2012 package, C_JAVA, st_C_ignore
2013 friend, C_PLPL, st_C_ignore
2014 extends, C_JAVA, st_C_javastruct
2015 implements, C_JAVA, st_C_javastruct
2016 interface, C_JAVA, st_C_struct
2017 class, 0, st_C_class
2018 namespace, C_PLPL, st_C_struct
2019 domain, C_STAR, st_C_struct
2020 union, 0, st_C_struct
2021 struct, 0, st_C_struct
2022 extern, 0, st_C_extern
2024 typedef, 0, st_C_typedef
2025 define, 0, st_C_define
2026 operator, C_PLPL, st_C_operator
2027 template, 0, st_C_template
2028 bool, C_PLPL, st_C_typespec
2029 long, 0, st_C_typespec
2030 short, 0, st_C_typespec
2031 int, 0, st_C_typespec
2032 char, 0, st_C_typespec
2033 float, 0, st_C_typespec
2034 double, 0, st_C_typespec
2035 signed, 0, st_C_typespec
2036 unsigned, 0, st_C_typespec
2037 auto, 0, st_C_typespec
2038 void, 0, st_C_typespec
2039 static, 0, st_C_typespec
2040 const, 0, st_C_typespec
2041 volatile, 0, st_C_typespec
2042 explicit, C_PLPL, st_C_typespec
2043 mutable, C_PLPL, st_C_typespec
2044 typename, C_PLPL, st_C_typespec
2045 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
2046 DEFUN, 0, st_C_gnumacro
2047 SYSCALL, 0, st_C_gnumacro
2048 ENTRY, 0, st_C_gnumacro
2049 PSEUDO, 0, st_C_gnumacro
2050 # These are defined inside C functions, so currently they are not met.
2051 # EXFUN used in glibc, DEFVAR_* in emacs.
2052 #EXFUN, 0, st_C_gnumacro
2053 #DEFVAR_, 0, st_C_gnumacro
2055 and replace lines between %< and %> with its output,
2056 then make in_word_set and C_stab_entry static. */
2058 /* C code produced by gperf version 2.7.1 (19981006 egcs) */
2059 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
2060 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
2062 #define TOTAL_KEYWORDS 47
2063 #define MIN_WORD_LENGTH 2
2064 #define MAX_WORD_LENGTH 15
2065 #define MIN_HASH_VALUE 18
2066 #define MAX_HASH_VALUE 138
2067 /* maximum key range = 121, duplicates = 0 */
2074 register const char *str
;
2075 register unsigned int len
;
2077 static unsigned char asso_values
[] =
2079 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2080 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2081 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2082 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2083 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2084 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2085 139, 139, 139, 139, 63, 139, 139, 139, 33, 44,
2086 62, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2087 42, 139, 139, 12, 32, 139, 139, 139, 139, 139,
2088 139, 139, 139, 139, 139, 139, 139, 34, 59, 37,
2089 24, 58, 33, 3, 139, 16, 139, 139, 42, 60,
2090 18, 11, 39, 139, 23, 57, 4, 63, 6, 20,
2091 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2092 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2093 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2094 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2095 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2096 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2097 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2098 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2099 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2100 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2101 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2102 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2103 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
2104 139, 139, 139, 139, 139, 139
2106 register int hval
= len
;
2112 hval
+= asso_values
[(unsigned char)str
[2]];
2115 hval
+= asso_values
[(unsigned char)str
[0]];
2124 static struct C_stab_entry
*
2125 in_word_set (str
, len
)
2126 register const char *str
;
2127 register unsigned int len
;
2129 static struct C_stab_entry wordlist
[] =
2131 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2132 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2133 {"if", 0, st_C_ignore
},
2134 {""}, {""}, {""}, {""},
2135 {"int", 0, st_C_typespec
},
2137 {"void", 0, st_C_typespec
},
2139 {"interface", C_JAVA
, st_C_struct
},
2141 {"SYSCALL", 0, st_C_gnumacro
},
2143 {"return", 0, st_C_ignore
},
2144 {""}, {""}, {""}, {""}, {""}, {""}, {""},
2145 {"while", 0, st_C_ignore
},
2146 {"auto", 0, st_C_typespec
},
2147 {""}, {""}, {""}, {""}, {""}, {""},
2148 {"float", 0, st_C_typespec
},
2149 {"typedef", 0, st_C_typedef
},
2150 {"typename", C_PLPL
, st_C_typespec
},
2152 {"friend", C_PLPL
, st_C_ignore
},
2153 {"volatile", 0, st_C_typespec
},
2155 {"for", 0, st_C_ignore
},
2156 {"const", 0, st_C_typespec
},
2157 {"import", C_JAVA
, st_C_ignore
},
2159 {"define", 0, st_C_define
},
2160 {"long", 0, st_C_typespec
},
2161 {"implements", C_JAVA
, st_C_javastruct
},
2162 {"signed", 0, st_C_typespec
},
2164 {"extern", 0, st_C_extern
},
2165 {"extends", C_JAVA
, st_C_javastruct
},
2167 {"mutable", C_PLPL
, st_C_typespec
},
2168 {"template", 0, st_C_template
},
2169 {"short", 0, st_C_typespec
},
2170 {"bool", C_PLPL
, st_C_typespec
},
2171 {"char", 0, st_C_typespec
},
2172 {"class", 0, st_C_class
},
2173 {"operator", C_PLPL
, st_C_operator
},
2175 {"switch", 0, st_C_ignore
},
2177 {"ENTRY", 0, st_C_gnumacro
},
2179 {"package", C_JAVA
, st_C_ignore
},
2180 {"union", 0, st_C_struct
},
2181 {"@end", 0, st_C_objend
},
2182 {"struct", 0, st_C_struct
},
2183 {"namespace", C_PLPL
, st_C_struct
},
2185 {"domain", C_STAR
, st_C_struct
},
2186 {"@interface", 0, st_C_objprot
},
2187 {"PSEUDO", 0, st_C_gnumacro
},
2188 {"double", 0, st_C_typespec
},
2190 {"@protocol", 0, st_C_objprot
},
2192 {"static", 0, st_C_typespec
},
2194 {"DEFUN", 0, st_C_gnumacro
},
2195 {""}, {""}, {""}, {""},
2196 {"explicit", C_PLPL
, st_C_typespec
},
2197 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2198 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2200 {"enum", 0, st_C_enum
},
2202 {"unsigned", 0, st_C_typespec
},
2203 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2204 {"@implementation",0, st_C_objimpl
}
2207 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2209 register int key
= hash (str
, len
);
2211 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2213 register const char *s
= wordlist
[key
].name
;
2215 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
2216 return &wordlist
[key
];
2223 static enum sym_type
2224 C_symtype (str
, len
, c_ext
)
2229 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2231 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2238 * C functions and variables are recognized using a simple
2239 * finite automaton. fvdef is its state variable.
2243 fvnone
, /* nothing seen */
2244 fdefunkey
, /* Emacs DEFUN keyword seen */
2245 fdefunname
, /* Emacs DEFUN name seen */
2246 foperator
, /* func: operator keyword seen (cplpl) */
2247 fvnameseen
, /* function or variable name seen */
2248 fstartlist
, /* func: just after open parenthesis */
2249 finlist
, /* func: in parameter list */
2250 flistseen
, /* func: after parameter list */
2251 fignore
, /* func: before open brace */
2252 vignore
/* var-like: ignore until ';' */
2255 static bool fvextern
; /* func or var: extern keyword seen; */
2258 * typedefs are recognized using a simple finite automaton.
2259 * typdef is its state variable.
2263 tnone
, /* nothing seen */
2264 tkeyseen
, /* typedef keyword seen */
2265 ttypeseen
, /* defined type seen */
2266 tinbody
, /* inside typedef body */
2267 tend
, /* just before typedef tag */
2268 tignore
/* junk after typedef tag */
2272 * struct-like structures (enum, struct and union) are recognized
2273 * using another simple finite automaton. `structdef' is its state
2278 snone
, /* nothing seen yet,
2279 or in struct body if cblev > 0 */
2280 skeyseen
, /* struct-like keyword seen */
2281 stagseen
, /* struct-like tag seen */
2282 sintemplate
, /* inside template (ignore) */
2283 scolonseen
/* colon seen after struct-like tag */
2287 * When objdef is different from onone, objtag is the name of the class.
2289 static char *objtag
= "<uninited>";
2292 * Yet another little state machine to deal with preprocessor lines.
2296 dnone
, /* nothing seen */
2297 dsharpseen
, /* '#' seen as first char on line */
2298 ddefineseen
, /* '#' and 'define' seen */
2299 dignorerest
/* ignore rest of line */
2303 * State machine for Objective C protocols and implementations.
2304 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
2308 onone
, /* nothing seen */
2309 oprotocol
, /* @interface or @protocol seen */
2310 oimplementation
, /* @implementations seen */
2311 otagseen
, /* class name seen */
2312 oparenseen
, /* parenthesis before category seen */
2313 ocatseen
, /* category name seen */
2314 oinbody
, /* in @implementation body */
2315 omethodsign
, /* in @implementation body, after +/- */
2316 omethodtag
, /* after method name */
2317 omethodcolon
, /* after method colon */
2318 omethodparm
, /* after method parameter */
2319 oignore
/* wait for @end */
2324 * Use this structure to keep info about the token read, and how it
2325 * should be tagged. Used by the make_C_tag function to build a tag.
2336 } token
; /* latest token read */
2337 static linebuffer token_name
; /* its name */
2340 * Variables and functions for dealing with nested structures.
2341 * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
2343 static void pushclass_above
__P((int, char *, int));
2344 static void popclass_above
__P((int));
2345 static void write_classname
__P((linebuffer
*, char *qualifier
));
2348 char **cname
; /* nested class names */
2349 int *cblev
; /* nested class curly brace level */
2350 int nl
; /* class nesting level (elements used) */
2351 int size
; /* length of the array */
2352 } cstack
; /* stack for nested declaration tags */
2353 /* Current struct nesting depth (namespace, class, struct, union, enum). */
2354 #define nestlev (cstack.nl)
2355 /* After struct keyword or in struct body, not inside an nested function. */
2356 #define instruct (structdef == snone && nestlev > 0 \
2357 && cblev == cstack.cblev[nestlev-1] + 1)
2360 pushclass_above (cblev
, str
, len
)
2367 popclass_above (cblev
);
2369 if (nl
>= cstack
.size
)
2371 int size
= cstack
.size
*= 2;
2372 xrnew (cstack
.cname
, size
, char *);
2373 xrnew (cstack
.cblev
, size
, int);
2375 assert (nl
== 0 || cstack
.cblev
[nl
-1] < cblev
);
2376 cstack
.cname
[nl
] = (str
== NULL
) ? NULL
: savenstr (str
, len
);
2377 cstack
.cblev
[nl
] = cblev
;
2382 popclass_above (cblev
)
2387 for (nl
= cstack
.nl
- 1;
2388 nl
>= 0 && cstack
.cblev
[nl
] >= cblev
;
2391 if (cstack
.cname
[nl
] != NULL
)
2392 free (cstack
.cname
[nl
]);
2398 write_classname (cn
, qualifier
)
2403 int qlen
= strlen (qualifier
);
2405 if (cstack
.nl
== 0 || cstack
.cname
[0] == NULL
)
2409 cn
->buffer
[0] = '\0';
2413 len
= strlen (cstack
.cname
[0]);
2414 linebuffer_setlen (cn
, len
);
2415 strcpy (cn
->buffer
, cstack
.cname
[0]);
2417 for (i
= 1; i
< cstack
.nl
; i
++)
2422 s
= cstack
.cname
[i
];
2427 linebuffer_setlen (cn
, len
);
2428 strncat (cn
->buffer
, qualifier
, qlen
);
2429 strncat (cn
->buffer
, s
, slen
);
2434 static bool consider_token
__P((char *, int, int, int *, int, int, bool *));
2435 static void make_C_tag
__P((bool));
2439 * checks to see if the current token is at the start of a
2440 * function or variable, or corresponds to a typedef, or
2441 * is a struct/union/enum tag, or #define, or an enum constant.
2443 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2444 * with args. C_EXTP points to which language we are looking at.
2455 consider_token (str
, len
, c
, c_extp
, cblev
, parlev
, is_func_or_var
)
2456 register char *str
; /* IN: token pointer */
2457 register int len
; /* IN: token length */
2458 register int c
; /* IN: first char after the token */
2459 int *c_extp
; /* IN, OUT: C extensions mask */
2460 int cblev
; /* IN: curly brace level */
2461 int parlev
; /* IN: parenthesis level */
2462 bool *is_func_or_var
; /* OUT: function or variable found */
2464 /* When structdef is stagseen, scolonseen, or snone with cblev > 0,
2465 structtype is the type of the preceding struct-like keyword, and
2466 structcblev is the curly brace level where it has been seen. */
2467 static enum sym_type structtype
;
2468 static int structcblev
;
2469 static enum sym_type toktype
;
2472 toktype
= C_symtype (str
, len
, *c_extp
);
2475 * Advance the definedef state machine.
2480 /* We're not on a preprocessor line. */
2481 if (toktype
== st_C_gnumacro
)
2488 if (toktype
== st_C_define
)
2490 definedef
= ddefineseen
;
2494 definedef
= dignorerest
;
2499 * Make a tag for any macro, unless it is a constant
2500 * and constantypedefs is FALSE.
2502 definedef
= dignorerest
;
2503 *is_func_or_var
= (c
== '(');
2504 if (!*is_func_or_var
&& !constantypedefs
)
2511 error ("internal error: definedef value.", (char *)NULL
);
2520 if (toktype
== st_C_typedef
)
2542 if (structdef
== snone
&& fvdef
== fvnone
)
2561 * This structdef business is NOT invoked when we are ctags and the
2562 * file is plain C. This is because a struct tag may have the same
2563 * name as another tag, and this loses with ctags.
2567 case st_C_javastruct
:
2568 if (structdef
== stagseen
)
2569 structdef
= scolonseen
;
2574 && (*c_extp
& C_AUTO
) /* automatic detection of C++ language */
2575 && definedef
== dnone
&& structdef
== snone
2576 && typdef
== tnone
&& fvdef
== fvnone
)
2577 *c_extp
= (*c_extp
| C_PLPL
) & ~C_AUTO
;
2578 if (toktype
== st_C_template
)
2585 && (typdef
== tkeyseen
2586 || (typedefs_or_cplusplus
&& structdef
== snone
)))
2588 structdef
= skeyseen
;
2589 structtype
= toktype
;
2590 structcblev
= cblev
;
2595 if (structdef
== skeyseen
)
2597 structdef
= stagseen
;
2601 if (typdef
!= tnone
)
2604 /* Detect Objective C constructs. */
2614 objdef
= oimplementation
;
2618 case oimplementation
:
2619 /* Save the class tag for functions or variables defined inside. */
2620 objtag
= savenstr (str
, len
);
2624 /* Save the class tag for categories. */
2625 objtag
= savenstr (str
, len
);
2627 *is_func_or_var
= TRUE
;
2631 *is_func_or_var
= TRUE
;
2638 objdef
= omethodtag
;
2639 linebuffer_setlen (&token_name
, len
);
2640 strncpy (token_name
.buffer
, str
, len
);
2641 token_name
.buffer
[len
] = '\0';
2647 objdef
= omethodparm
;
2652 objdef
= omethodtag
;
2653 linebuffer_setlen (&token_name
, token_name
.len
+ len
);
2654 strncat (token_name
.buffer
, str
, len
);
2659 if (toktype
== st_C_objend
)
2661 /* Memory leakage here: the string pointed by objtag is
2662 never released, because many tests would be needed to
2663 avoid breaking on incorrect input code. The amount of
2664 memory leaked here is the sum of the lengths of the
2672 /* A function, variable or enum constant? */
2679 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!= vignore
)
2680 fvdef
= fvnone
; /* should be useless */
2688 *is_func_or_var
= TRUE
;
2692 && structdef
== snone
2693 && structtype
== st_C_enum
&& cblev
> structcblev
)
2694 return TRUE
; /* enum constant */
2700 fvdef
= fdefunname
; /* GNU macro */
2701 *is_func_or_var
= TRUE
;
2704 if ((strneq (str
, "asm", 3) && endtoken (str
[3]))
2705 || (strneq (str
, "__asm__", 7) && endtoken (str
[7])))
2710 if ((*c_extp
& C_PLPL
) && strneq (str
+len
-10, "::operator", 10))
2713 *is_func_or_var
= TRUE
;
2716 if (cblev
> 0 && !instruct
)
2718 fvdef
= fvnameseen
; /* function or variable */
2719 *is_func_or_var
= TRUE
;
2730 * C_entries often keeps pointers to tokens or lines which are older than
2731 * the line currently read. By keeping two line buffers, and switching
2732 * them at end of line, it is possible to use those pointers.
2740 #define current_lb_is_new (newndx == curndx)
2741 #define switch_line_buffers() (curndx = 1 - curndx)
2743 #define curlb (lbs[curndx].lb)
2744 #define newlb (lbs[newndx].lb)
2745 #define curlinepos (lbs[curndx].linepos)
2746 #define newlinepos (lbs[newndx].linepos)
2748 #define CNL_SAVE_DEFINEDEF() \
2750 curlinepos = charno; \
2752 linecharno = charno; \
2753 charno += readline (&curlb, inf); \
2754 lp = curlb.buffer; \
2761 CNL_SAVE_DEFINEDEF(); \
2762 if (savetoken.valid) \
2764 token = savetoken; \
2765 savetoken.valid = FALSE; \
2767 definedef = dnone; \
2775 /* This function should never be called when token.valid is FALSE, but
2776 we must protect against invalid input or internal errors. */
2777 if (DEBUG
|| token
.valid
)
2779 if (traditional_tag_style
)
2781 /* This was the original code. Now we call new_pfnote instead,
2782 which uses the new method for naming tags (see new_pfnote). */
2785 if (CTAGS
|| token
.named
)
2786 name
= savestr (token_name
.buffer
);
2787 if (DEBUG
&& !token
.valid
)
2790 name
= concat (name
, "##invalid##", "");
2792 name
= savestr ("##invalid##");
2794 pfnote (name
, isfun
, token
.line
,
2795 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
2798 new_pfnote (token_name
.buffer
, token_name
.len
, isfun
, token
.line
,
2799 token
.offset
+token
.length
+1, token
.lineno
, token
.linepos
);
2800 token
.valid
= FALSE
;
2807 * This routine finds functions, variables, typedefs,
2808 * #define's, enum constants and struct/union/enum definitions in
2809 * C syntax and adds them to the list.
2812 C_entries (c_ext
, inf
)
2813 int c_ext
; /* extension of C */
2814 FILE *inf
; /* input file */
2816 register char c
; /* latest char read; '\0' for end of line */
2817 register char *lp
; /* pointer one beyond the character `c' */
2818 int curndx
, newndx
; /* indices for current and new lb */
2819 register int tokoff
; /* offset in line of start of current token */
2820 register int toklen
; /* length of current token */
2821 char *qualifier
; /* string used to qualify names */
2822 int qlen
; /* length of qualifier */
2823 int cblev
; /* current curly brace level */
2824 int parlev
; /* current parenthesis level */
2825 int typdefcblev
; /* cblev where a typedef struct body begun */
2826 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
2828 bool yacc_rules
; /* in the rules part of a yacc file */
2829 struct tok savetoken
; /* token saved during preprocessor handling */
2832 initbuffer (&token_name
);
2833 initbuffer (&lbs
[0].lb
);
2834 initbuffer (&lbs
[1].lb
);
2835 if (cstack
.size
== 0)
2837 cstack
.size
= (DEBUG
) ? 1 : 4;
2839 cstack
.cname
= xnew (cstack
.size
, char *);
2840 cstack
.cblev
= xnew (cstack
.size
, int);
2843 tokoff
= toklen
= typdefcblev
= 0; /* keep compiler quiet */
2844 curndx
= newndx
= 0;
2850 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
2851 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
2853 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2854 token
.valid
= savetoken
.valid
= FALSE
;
2857 cplpl
= (c_ext
& C_PLPL
) == C_PLPL
;
2858 cjava
= (c_ext
& C_JAVA
) == C_JAVA
;
2860 { qualifier
= "."; qlen
= 1; }
2862 { qualifier
= "::"; qlen
= 2; }
2870 /* If we're at the end of the line, the next character is a
2871 '\0'; don't skip it, because it's the thing that tells us
2872 to read the next line. */
2893 /* Newlines inside comments do not end macro definitions in
2895 CNL_SAVE_DEFINEDEF ();
2908 /* Newlines inside strings do not end macro definitions
2909 in traditional cpp, even though compilers don't
2910 usually accept them. */
2911 CNL_SAVE_DEFINEDEF ();
2921 /* Hmmm, something went wrong. */
2950 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2963 else if (/* cplpl && */ *lp
== '/')
2971 if ((c_ext
& YACC
) && *lp
== '%')
2973 /* Entering or exiting rules section in yacc file. */
2975 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
2976 typdef
= tnone
; structdef
= snone
;
2977 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2979 yacc_rules
= !yacc_rules
;
2985 if (definedef
== dnone
)
2988 bool cpptoken
= TRUE
;
2990 /* Look back on this line. If all blanks, or nonblanks
2991 followed by an end of comment, this is a preprocessor
2993 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2996 if (*cp
== '*' && *(cp
+1) == '/')
3005 definedef
= dsharpseen
;
3006 } /* if (definedef == dnone) */
3012 /* Consider token only if some involved conditions are satisfied. */
3013 if (typdef
!= tignore
3014 && definedef
!= dignorerest
3016 && structdef
!= sintemplate
3017 && (definedef
!= dnone
3018 || structdef
!= scolonseen
))
3024 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken (lp
[1]))
3027 * This handles :: in the middle, but not at the
3028 * beginning of an identifier. Also, space-separated
3029 * :: is not recognised.
3034 goto still_in_token
;
3038 bool funorvar
= FALSE
;
3041 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
3042 &c_ext
, cblev
, parlev
, &funorvar
))
3044 if (fvdef
== foperator
)
3047 lp
= skip_spaces (lp
-1);
3051 && !iswhite (*lp
) && *lp
!= '(')
3054 toklen
+= lp
- oldlp
;
3056 token
.named
= FALSE
;
3057 if ((c_ext
& C_EXT
) /* not pure C */
3058 && nestlev
> 0 && definedef
== dnone
)
3059 /* in struct body */
3061 write_classname (&token_name
, qualifier
);
3062 linebuffer_setlen (&token_name
,
3063 token_name
.len
+qlen
+toklen
);
3064 strcat (token_name
.buffer
, qualifier
);
3065 strncat (token_name
.buffer
,
3066 newlb
.buffer
+ tokoff
, toklen
);
3069 else if (objdef
== ocatseen
)
3070 /* Objective C category */
3072 int len
= strlen (objtag
) + 2 + toklen
;
3073 linebuffer_setlen (&token_name
, len
);
3074 strcpy (token_name
.buffer
, objtag
);
3075 strcat (token_name
.buffer
, "(");
3076 strncat (token_name
.buffer
,
3077 newlb
.buffer
+ tokoff
, toklen
);
3078 strcat (token_name
.buffer
, ")");
3081 else if (objdef
== omethodtag
3082 || objdef
== omethodparm
)
3083 /* Objective C method */
3087 else if (fvdef
== fdefunname
)
3088 /* GNU DEFUN and similar macros */
3090 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
3094 /* Rewrite the tag so that emacs lisp DEFUNs
3095 can be found by their elisp name */
3102 linebuffer_setlen (&token_name
, len
);
3103 strncpy (token_name
.buffer
,
3104 newlb
.buffer
+ off
, len
);
3105 token_name
.buffer
[len
] = '\0';
3108 if (token_name
.buffer
[len
] == '_')
3109 token_name
.buffer
[len
] = '-';
3110 token
.named
= defun
;
3114 linebuffer_setlen (&token_name
, toklen
);
3115 strncpy (token_name
.buffer
,
3116 newlb
.buffer
+ tokoff
, toklen
);
3117 token_name
.buffer
[toklen
] = '\0';
3118 /* Name macros and members. */
3119 token
.named
= (structdef
== stagseen
3120 || typdef
== ttypeseen
3123 && definedef
== dignorerest
)
3125 && definedef
== dnone
3126 && structdef
== snone
3129 token
.lineno
= lineno
;
3130 token
.offset
= tokoff
;
3131 token
.length
= toklen
;
3132 token
.line
= newlb
.buffer
;
3133 token
.linepos
= newlinepos
;
3136 if (definedef
== dnone
3137 && (fvdef
== fvnameseen
3138 || fvdef
== foperator
3139 || structdef
== stagseen
3141 || typdef
== ttypeseen
3142 || objdef
!= onone
))
3144 if (current_lb_is_new
)
3145 switch_line_buffers ();
3147 else if (definedef
!= dnone
3148 || fvdef
== fdefunname
3150 make_C_tag (funorvar
);
3154 } /* if (endtoken (c)) */
3155 else if (intoken (c
))
3161 } /* if (midtoken) */
3162 else if (begtoken (c
))
3173 make_C_tag (TRUE
); /* a function */
3180 if (structdef
== stagseen
&& !cjava
)
3182 popclass_above (cblev
);
3189 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
3191 tokoff
= lp
- 1 - newlb
.buffer
;
3196 } /* if (begtoken) */
3197 } /* if must look at token */
3200 /* Detect end of line, colon, comma, semicolon and various braces
3201 after having handled a token.*/
3205 if (yacc_rules
&& token
.offset
== 0 && token
.valid
)
3207 make_C_tag (FALSE
); /* a yacc function */
3210 if (definedef
!= dnone
)
3216 make_C_tag (TRUE
); /* an Objective C class */
3220 objdef
= omethodcolon
;
3221 linebuffer_setlen (&token_name
, token_name
.len
+ 1);
3222 strcat (token_name
.buffer
, ":");
3225 if (structdef
== stagseen
)
3226 structdef
= scolonseen
;
3229 if (definedef
!= dnone
)
3235 make_C_tag (FALSE
); /* a typedef */
3245 if (typdef
== tignore
)
3249 if ((globals
&& cblev
== 0 && (!fvextern
|| declarations
))
3250 || (members
&& instruct
))
3251 make_C_tag (FALSE
); /* a variable */
3254 token
.valid
= FALSE
;
3257 if ((declarations
&& typdef
== tnone
&& !instruct
)
3258 || (members
&& typdef
!= tignore
&& instruct
))
3259 make_C_tag (TRUE
); /* a function declaration */
3265 && structdef
== stagseen
&& (c_ext
& C_PLPL
))
3266 make_C_tag (FALSE
); /* forward declaration */
3268 /* The following instruction invalidates the token.
3269 Probably the token should be invalidated in all other
3270 cases where some state machine is reset prematurely. */
3271 token
.valid
= FALSE
;
3272 } /* switch (fvdef) */
3278 if (structdef
== stagseen
)
3282 if (definedef
!= dnone
)
3288 make_C_tag (TRUE
); /* an Objective C method */
3304 case fvnameseen
: /* a variable */
3305 if ((globals
&& cblev
== 0 && (!fvextern
|| declarations
))
3306 || (members
&& instruct
))
3309 case flistseen
: /* a function */
3310 if ((declarations
&& typdef
== tnone
&& !instruct
)
3311 || (members
&& typdef
!= tignore
&& instruct
))
3313 make_C_tag (TRUE
); /* a function declaration */
3316 else if (!declarations
)
3318 token
.valid
= FALSE
;
3323 if (structdef
== stagseen
)
3327 if (definedef
!= dnone
)
3329 if (structdef
== stagseen
)
3336 make_C_tag (FALSE
); /* a typedef */
3348 if ((members
&& cblev
== 1)
3349 || (globals
&& cblev
== 0
3350 && (!fvextern
|| declarations
)))
3351 make_C_tag (FALSE
); /* a variable */
3360 if (definedef
!= dnone
)
3362 if (objdef
== otagseen
&& parlev
== 0)
3363 objdef
= oparenseen
;
3367 if (typdef
== ttypeseen
3371 /* This handles constructs like:
3372 typedef void OperatorFun (int fun); */
3389 if (definedef
!= dnone
)
3391 if (objdef
== ocatseen
&& parlev
== 1)
3393 make_C_tag (TRUE
); /* an Objective C category */
3407 || typdef
== ttypeseen
))
3410 make_C_tag (FALSE
); /* a typedef */
3413 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3417 if (definedef
!= dnone
)
3419 if (typdef
== ttypeseen
)
3421 /* Whenever typdef is set to tinbody (currently only
3422 here), typdefcblev should be set to cblev. */
3424 typdefcblev
= cblev
;
3429 make_C_tag (TRUE
); /* a function */
3438 make_C_tag (TRUE
); /* an Objective C class */
3443 make_C_tag (TRUE
); /* an Objective C method */
3447 /* Neutralize `extern "C" {' grot. */
3448 if (cblev
== 0 && structdef
== snone
&& nestlev
== 0
3455 case skeyseen
: /* unnamed struct */
3456 pushclass_above (cblev
, NULL
, 0);
3459 case stagseen
: /* named struct or enum */
3460 case scolonseen
: /* a class */
3461 pushclass_above (cblev
, token
.line
+token
.offset
, token
.length
);
3463 make_C_tag (FALSE
); /* a struct or enum */
3469 if (definedef
!= dnone
)
3471 if (fvdef
== fstartlist
)
3472 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3475 if (definedef
!= dnone
)
3477 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
3479 cblev
= 0; /* reset curly brace level if first column */
3480 parlev
= 0; /* also reset paren level, just in case... */
3484 popclass_above (cblev
);
3486 /* Only if typdef == tinbody is typdefcblev significant. */
3487 if (typdef
== tinbody
&& cblev
<= typdefcblev
)
3489 assert (cblev
== typdefcblev
);
3494 if (definedef
!= dnone
)
3504 if ((members
&& cblev
== 1)
3505 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3506 make_C_tag (FALSE
); /* a variable */
3513 if (cplpl
&& structdef
== stagseen
)
3515 structdef
= sintemplate
;
3520 if (structdef
== sintemplate
)
3522 structdef
= stagseen
;
3528 if (objdef
== oinbody
&& cblev
== 0)
3530 objdef
= omethodsign
;
3535 case '#': case '~': case '&': case '%': case '/': case '|':
3536 case '^': case '!': case '.': case '?': case ']':
3537 if (definedef
!= dnone
)
3539 /* These surely cannot follow a function tag in C. */
3552 if (objdef
== otagseen
)
3554 make_C_tag (TRUE
); /* an Objective C class */
3557 /* If a macro spans multiple lines don't reset its state. */
3559 CNL_SAVE_DEFINEDEF ();
3565 } /* while not eof */
3567 free (token_name
.buffer
);
3568 free (lbs
[0].lb
.buffer
);
3569 free (lbs
[1].lb
.buffer
);
3573 * Process either a C++ file or a C file depending on the setting
3577 default_C_entries (inf
)
3580 C_entries (cplusplus
? C_PLPL
: C_AUTO
, inf
);
3583 /* Always do plain C. */
3585 plain_C_entries (inf
)
3591 /* Always do C++. */
3593 Cplusplus_entries (inf
)
3596 C_entries (C_PLPL
, inf
);
3599 /* Always do Java. */
3604 C_entries (C_JAVA
, inf
);
3612 C_entries (C_STAR
, inf
);
3615 /* Always do Yacc. */
3620 C_entries (YACC
, inf
);
3624 /* Useful macros. */
3625 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3626 for (lineno = charno = 0; /* loop initialization */ \
3627 !feof (file_pointer) /* loop test */ \
3628 && (lineno++, /* instructions at start of loop */ \
3629 linecharno = charno, \
3630 charno += readline (&line_buffer, file_pointer), \
3631 char_pointer = lb.buffer, \
3634 #define LOOKING_AT(cp, keyword) /* keyword is a constant string */ \
3635 (strneq ((cp), keyword, sizeof(keyword)-1) /* cp points at keyword */ \
3636 && notinname ((cp)[sizeof(keyword)-1]) /* end of keyword */ \
3637 && ((cp) = skip_spaces((cp)+sizeof(keyword)-1))) /* skip spaces */
3640 * Read a file, but do no processing. This is used to do regexp
3641 * matching on files that have no language defined.
3644 just_read_file (inf
)
3647 register char *dummy
;
3649 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
3654 /* Fortran parsing */
3656 static void F_takeprec
__P((void));
3657 static void F_getit
__P((FILE *));
3662 dbp
= skip_spaces (dbp
);
3666 dbp
= skip_spaces (dbp
);
3667 if (strneq (dbp
, "(*)", 3))
3672 if (!ISDIGIT (*dbp
))
3674 --dbp
; /* force failure */
3679 while (ISDIGIT (*dbp
));
3688 dbp
= skip_spaces (dbp
);
3692 linecharno
= charno
;
3693 charno
+= readline (&lb
, inf
);
3698 dbp
= skip_spaces (dbp
);
3700 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
3702 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
3704 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
3705 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3710 Fortran_functions (inf
)
3713 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3716 dbp
++; /* Ratfor escape to fortran */
3717 dbp
= skip_spaces (dbp
);
3720 switch (lowcase (*dbp
))
3723 if (nocase_tail ("integer"))
3727 if (nocase_tail ("real"))
3731 if (nocase_tail ("logical"))
3735 if (nocase_tail ("complex") || nocase_tail ("character"))
3739 if (nocase_tail ("double"))
3741 dbp
= skip_spaces (dbp
);
3744 if (nocase_tail ("precision"))
3750 dbp
= skip_spaces (dbp
);
3753 switch (lowcase (*dbp
))
3756 if (nocase_tail ("function"))
3760 if (nocase_tail ("subroutine"))
3764 if (nocase_tail ("entry"))
3768 if (nocase_tail ("blockdata") || nocase_tail ("block data"))
3770 dbp
= skip_spaces (dbp
);
3771 if (*dbp
== '\0') /* assume un-named */
3772 pfnote (savestr ("blockdata"), TRUE
,
3773 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
3775 F_getit (inf
); /* look for name */
3786 * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be> (1998)
3789 static void Ada_getit
__P((FILE *, char *));
3791 /* Once we are positioned after an "interesting" keyword, let's get
3792 the real tag value necessary. */
3794 Ada_getit (inf
, name_qualifier
)
3796 char *name_qualifier
;
3804 dbp
= skip_spaces (dbp
);
3806 || (dbp
[0] == '-' && dbp
[1] == '-'))
3809 linecharno
= charno
;
3810 charno
+= readline (&lb
, inf
);
3813 switch (lowcase(*dbp
))
3816 if (nocase_tail ("body"))
3818 /* Skipping body of procedure body or package body or ....
3819 resetting qualifier to body instead of spec. */
3820 name_qualifier
= "/b";
3825 /* Skipping type of task type or protected type ... */
3826 if (nocase_tail ("type"))
3833 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
3838 dbp
= skip_spaces (dbp
);
3841 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
3849 name
= concat (dbp
, name_qualifier
, "");
3851 pfnote (name
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3862 bool inquote
= FALSE
;
3864 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3866 while (*dbp
!= '\0')
3868 /* Skip a string i.e. "abcd". */
3869 if (inquote
|| (*dbp
== '"'))
3871 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
3876 continue; /* advance char */
3881 break; /* advance line */
3885 /* Skip comments. */
3886 if (dbp
[0] == '-' && dbp
[1] == '-')
3887 break; /* advance line */
3889 /* Skip character enclosed in single quote i.e. 'a'
3890 and skip single quote starting an attribute i.e. 'Image. */
3899 /* Search for beginning of a token. */
3900 if (!begtoken (*dbp
))
3903 continue; /* advance char */
3906 /* We are at the beginning of a token. */
3907 switch (lowcase(*dbp
))
3910 if (!packages_only
&& nocase_tail ("function"))
3911 Ada_getit (inf
, "/f");
3913 break; /* from switch */
3914 continue; /* advance char */
3916 if (!packages_only
&& nocase_tail ("procedure"))
3917 Ada_getit (inf
, "/p");
3918 else if (nocase_tail ("package"))
3919 Ada_getit (inf
, "/s");
3920 else if (nocase_tail ("protected")) /* protected type */
3921 Ada_getit (inf
, "/t");
3923 break; /* from switch */
3924 continue; /* advance char */
3926 if (!packages_only
&& nocase_tail ("task"))
3927 Ada_getit (inf
, "/k");
3928 else if (typedefs
&& !packages_only
&& nocase_tail ("type"))
3930 Ada_getit (inf
, "/t");
3931 while (*dbp
!= '\0')
3935 break; /* from switch */
3936 continue; /* advance char */
3939 /* Look for the end of the token. */
3940 while (!endtoken (*dbp
))
3943 } /* advance char */
3944 } /* advance line */
3949 * Unix and microcontroller assembly tag handling
3950 * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
3951 * Idea by Bob Weiner, Motorola Inc. (1994)
3959 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3961 /* If first char is alphabetic or one of [_.$], test for colon
3962 following identifier. */
3963 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3965 /* Read past label. */
3967 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3969 if (*cp
== ':' || iswhite (*cp
))
3971 /* Found end of label, so copy it and add it to the table. */
3972 pfnote (savenstr(lb
.buffer
, cp
-lb
.buffer
), TRUE
,
3973 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3982 * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
3983 * Perl variable names: /^(my|local).../
3984 * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
3985 * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
3986 * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
3989 Perl_functions (inf
)
3992 char *package
= savestr ("main"); /* current package name */
3995 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3999 if (LOOKING_AT (cp
, "package"))
4002 package
= get_tag (cp
);
4003 if (package
== NULL
) /* can't parse package name */
4004 package
= savestr ("");
4006 package
= savestr(package
); /* make a copy */
4008 else if (LOOKING_AT (cp
, "sub"))
4010 char *name
, *fullname
, *pos
;
4013 while (!notinname (*cp
))
4017 name
= savenstr (sp
, cp
-sp
);
4018 if ((pos
= etags_strchr (name
, ':')) != NULL
&& pos
[1] == ':')
4021 fullname
= concat (package
, "::", name
);
4022 pfnote (fullname
, TRUE
,
4023 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4024 if (name
!= fullname
)
4027 else if (globals
/* only if tagging global vars is enabled */
4028 && (LOOKING_AT (cp
, "my") || LOOKING_AT (cp
, "local")))
4030 /* After "my" or "local", but before any following paren or space. */
4031 char *varname
= NULL
;
4033 if (*cp
== '$' || *cp
== '@' || *cp
== '%')
4035 char* varstart
= ++cp
;
4036 while (ISALNUM (*cp
) || *cp
== '_')
4038 varname
= savenstr (varstart
, cp
-varstart
);
4042 /* Should be examining a variable list at this point;
4043 could insist on seeing an open parenthesis. */
4044 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
4048 /* Perhaps I should back cp up one character, so the TAGS table
4049 doesn't mention (and so depend upon) the following char. */
4050 pfnote (varname
, FALSE
,
4051 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4059 * Look for /^def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
4060 * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
4063 Python_functions (inf
)
4068 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4069 if (LOOKING_AT (cp
, "def") || LOOKING_AT (cp
, "class"))
4071 while (!notinname (*cp
) && *cp
!= ':')
4074 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4082 * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
4083 * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
4084 * - /^[ \t]*define\(\"[^\"]+/
4085 * Only with --members:
4086 * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
4087 * Idea by Diez B. Roggisch (2001)
4094 bool search_identifier
= FALSE
;
4096 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4098 cp
= skip_spaces (cp
);
4099 if (search_identifier
4102 while (!notinname (*cp
))
4105 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4106 search_identifier
= FALSE
;
4108 else if (LOOKING_AT (cp
, "function"))
4111 cp
= skip_spaces (cp
+1);
4114 while (!notinname (*cp
))
4117 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4120 search_identifier
= TRUE
;
4122 else if (LOOKING_AT (cp
, "class"))
4126 while (*cp
!= '\0' && !iswhite (*cp
))
4128 pfnote (NULL
, FALSE
,
4129 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4132 search_identifier
= TRUE
;
4134 else if (strneq (cp
, "define", 6)
4135 && (cp
= skip_spaces (cp
+6))
4137 && (*cp
== '"' || *cp
== '\''))
4140 while (*cp
!= quote
&& *cp
!= '\0')
4142 pfnote (NULL
, FALSE
,
4143 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4146 && LOOKING_AT (cp
, "var")
4149 while (!notinname(*cp
))
4151 pfnote (NULL
, FALSE
,
4152 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4159 * Cobol tag functions
4160 * We could look for anything that could be a paragraph name.
4161 * i.e. anything that starts in column 8 is one word and ends in a full stop.
4162 * Idea by Corny de Souza (1993)
4165 Cobol_paragraphs (inf
)
4168 register char *bp
, *ep
;
4170 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4176 /* If eoln, compiler option or comment ignore whole line. */
4177 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
4180 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
4183 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
4184 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4191 * Idea by Assar Westerlund <assar@sics.se> (2001)
4194 Makefile_targets (inf
)
4199 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4201 if (*bp
== '\t' || *bp
== '#')
4203 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
4206 pfnote (savenstr (lb
.buffer
, bp
- lb
.buffer
), TRUE
,
4207 lb
.buffer
, bp
- lb
.buffer
+ 1, lineno
, linecharno
);
4214 * Original code by Mosur K. Mohan (1989)
4216 * Locates tags for procedures & functions. Doesn't do any type- or
4217 * var-definitions. It does look for the keyword "extern" or
4218 * "forward" immediately following the procedure statement; if found,
4219 * the tag is skipped.
4222 Pascal_functions (inf
)
4225 linebuffer tline
; /* mostly copied from C_entries */
4227 int save_lineno
, save_len
;
4228 char c
, *cp
, *namebuf
;
4230 bool /* each of these flags is TRUE iff: */
4231 incomment
, /* point is inside a comment */
4232 inquote
, /* point is inside '..' string */
4233 get_tagname
, /* point is after PROCEDURE/FUNCTION
4234 keyword, so next item = potential tag */
4235 found_tag
, /* point is after a potential tag */
4236 inparms
, /* point is within parameter-list */
4237 verify_tag
; /* point has passed the parm-list, so the
4238 next token will determine whether this
4239 is a FORWARD/EXTERN to be ignored, or
4240 whether it is a real tag */
4242 save_lcno
= save_lineno
= save_len
= 0; /* keep compiler quiet */
4243 namebuf
= NULL
; /* keep compiler quiet */
4248 initbuffer (&tline
);
4250 incomment
= inquote
= FALSE
;
4251 found_tag
= FALSE
; /* have a proc name; check if extern */
4252 get_tagname
= FALSE
; /* have found "procedure" keyword */
4253 inparms
= FALSE
; /* found '(' after "proc" */
4254 verify_tag
= FALSE
; /* check if "extern" is ahead */
4257 while (!feof (inf
)) /* long main loop to get next char */
4260 if (c
== '\0') /* if end of line */
4263 linecharno
= charno
;
4264 charno
+= readline (&lb
, inf
);
4268 if (!((found_tag
&& verify_tag
)
4270 c
= *dbp
++; /* only if don't need *dbp pointing
4271 to the beginning of the name of
4272 the procedure or function */
4276 if (c
== '}') /* within { } comments */
4278 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4295 inquote
= TRUE
; /* found first quote */
4297 case '{': /* found open { comment */
4301 if (*dbp
== '*') /* found open (* comment */
4306 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4309 case ')': /* end of parms list */
4314 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4321 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4323 /* check if this is an "extern" declaration */
4326 if (lowcase (*dbp
== 'e'))
4328 if (nocase_tail ("extern")) /* superfluous, really! */
4334 else if (lowcase (*dbp
) == 'f')
4336 if (nocase_tail ("forward")) /* check for forward reference */
4342 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4346 pfnote (namebuf
, TRUE
,
4347 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
4351 if (get_tagname
) /* grab name of proc or fn */
4356 /* save all values for later tagging */
4357 linebuffer_setlen (&tline
, lb
.len
);
4358 strcpy (tline
.buffer
, lb
.buffer
);
4359 save_lineno
= lineno
;
4360 save_lcno
= linecharno
;
4362 /* grab block name */
4363 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4365 namebuf
= savenstr (dbp
, cp
-dbp
);
4366 dbp
= cp
; /* set dbp to e-o-token */
4367 save_len
= dbp
- lb
.buffer
+ 1;
4368 get_tagname
= FALSE
;
4372 /* and proceed to check for "extern" */
4374 else if (!incomment
&& !inquote
&& !found_tag
)
4376 /* check for proc/fn keywords */
4377 switch (lowcase (c
))
4380 if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
4384 if (nocase_tail ("unction"))
4389 } /* while not eof */
4391 free (tline
.buffer
);
4396 * Lisp tag functions
4397 * look for (def or (DEF, quote or QUOTE
4400 static void L_getit
__P((void));
4405 if (*dbp
== '\'') /* Skip prefix quote */
4407 else if (*dbp
== '(')
4410 /* Try to skip "(quote " */
4411 if (!LOOKING_AT (dbp
, "quote") && !LOOKING_AT (dbp
, "QUOTE"))
4412 /* Ok, then skip "(" before name in (defstruct (foo)) */
4413 dbp
= skip_spaces (dbp
);
4419 Lisp_functions (inf
)
4422 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4427 if (strneq (dbp
+1, "def", 3) || strneq (dbp
+1, "DEF", 3))
4429 dbp
= skip_non_spaces (dbp
);
4430 dbp
= skip_spaces (dbp
);
4435 /* Check for (foo::defmumble name-defined ... */
4438 while (!notinname (*dbp
) && *dbp
!= ':');
4443 while (*dbp
== ':');
4445 if (strneq (dbp
, "def", 3) || strneq (dbp
, "DEF", 3))
4447 dbp
= skip_non_spaces (dbp
);
4448 dbp
= skip_spaces (dbp
);
4458 * Postscript tag functions
4459 * Just look for lines where the first character is '/'
4460 * Also look at "defineps" for PSWrap
4462 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4463 * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4466 Postscript_functions (inf
)
4469 register char *bp
, *ep
;
4471 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4476 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4479 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
4480 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4482 else if (LOOKING_AT (bp
, "defineps"))
4489 * Scheme tag functions
4490 * look for (def... xyzzy
4492 * (def ... ((...(xyzzy ....
4494 * Original code by Ken Haase (1985?)
4498 Scheme_functions (inf
)
4503 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4505 if (strneq (bp
, "(def", 4) || strneq (bp
, "(DEF", 4))
4507 bp
= skip_non_spaces (bp
+4);
4508 /* Skip over open parens and white space */
4509 while (notinname (*bp
))
4513 if (LOOKING_AT (bp
, "(SET!") || LOOKING_AT (bp
, "(set!"))
4519 /* Find tags in TeX and LaTeX input files. */
4521 /* TEX_toktab is a table of TeX control sequences that define tags.
4522 Each TEX_tabent records one such control sequence.
4523 CONVERT THIS TO USE THE Stab TYPE!! */
4530 static struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
4532 /* Default set of control sequences to put into TEX_toktab.
4533 The value of environment var TEXTAGS is prepended to this. */
4535 static char *TEX_defenv
= "\
4536 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4537 :part:appendix:entry:index";
4539 static void TEX_mode
__P((FILE *));
4540 static struct TEX_tabent
*TEX_decode_env
__P((char *, char *));
4541 static int TEX_Token
__P((char *));
4543 static char TEX_esc
= '\\';
4544 static char TEX_opgrp
= '{';
4545 static char TEX_clgrp
= '}';
4548 * TeX/LaTeX scanning loop.
4557 /* Select either \ or ! as escape character. */
4560 /* Initialize token table once from environment. */
4562 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
4564 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4567 /* Look at each esc in line. */
4568 while ((cp
= etags_strchr (cp
, TEX_esc
)) != NULL
)
4572 linecharno
+= cp
- lasthit
;
4574 i
= TEX_Token (lasthit
);
4577 /* We seem to include the TeX command in the tag name.
4579 for (p = lasthit + TEX_toktab[i].len;
4580 *p != '\0' && *p != TEX_clgrp;
4583 pfnote (/*savenstr (lasthit, p-lasthit)*/ (char *)NULL
, TRUE
,
4584 lb
.buffer
, lb
.len
, lineno
, linecharno
);
4585 break; /* We only tag a line once */
4591 #define TEX_LESC '\\'
4592 #define TEX_SESC '!'
4595 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
4596 chars accordingly. */
4603 while ((c
= getc (inf
)) != EOF
)
4605 /* Skip to next line if we hit the TeX comment char. */
4609 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
4625 /* If the input file is compressed, inf is a pipe, and rewind may fail.
4626 No attempt is made to correct the situation. */
4630 /* Read environment and prepend it to the default string.
4631 Build token table. */
4632 static struct TEX_tabent
*
4633 TEX_decode_env (evarname
, defenv
)
4637 register char *env
, *p
;
4639 struct TEX_tabent
*tab
;
4642 /* Append default string to environment. */
4643 env
= getenv (evarname
);
4649 env
= concat (oldenv
, defenv
, "");
4652 /* Allocate a token table */
4653 for (size
= 1, p
= env
; p
;)
4654 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
4656 /* Add 1 to leave room for null terminator. */
4657 tab
= xnew (size
+ 1, struct TEX_tabent
);
4659 /* Unpack environment string into token table. Be careful about */
4660 /* zero-length strings (leading ':', "::" and trailing ':') */
4663 p
= etags_strchr (env
, ':');
4664 if (!p
) /* End of environment string. */
4665 p
= env
+ strlen (env
);
4667 { /* Only non-zero strings. */
4668 tab
[i
].name
= savenstr (env
, p
- env
);
4669 tab
[i
].len
= strlen (tab
[i
].name
);
4676 tab
[i
].name
= NULL
; /* Mark end of table. */
4684 /* If the text at CP matches one of the tag-defining TeX command names,
4685 return the pointer to the first occurrence of that command in TEX_toktab.
4686 Otherwise return -1.
4687 Keep the capital `T' in `token' for dumb truncating compilers
4688 (this distinguishes it from `TEX_toktab' */
4695 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
4696 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
4702 /* Texinfo support. Dave Love, Mar. 2000. */
4708 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4709 if (LOOKING_AT (cp
, "@node"))
4712 while (*cp
!= '\0' && *cp
!= ',')
4714 pfnote (savenstr (start
, cp
- start
), TRUE
,
4715 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4723 * Assumes that the predicate or rule starts at column 0.
4724 * Only the first clause of a predicate or rule is added.
4725 * Original code by Sunichirou Sugou (1989)
4726 * Rewritten by Anders Lindgren (1996)
4728 static int prolog_pr
__P((char *, char *));
4729 static void prolog_skip_comment
__P((linebuffer
*, FILE *));
4730 static int prolog_atom
__P((char *, int));
4733 Prolog_functions (inf
)
4744 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4746 if (cp
[0] == '\0') /* Empty line */
4748 else if (iswhite (cp
[0])) /* Not a predicate */
4750 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
4751 prolog_skip_comment (&lb
, inf
);
4752 else if ((len
= prolog_pr (cp
, last
)) > 0)
4754 /* Predicate or rule. Store the function name so that we
4755 only generate a tag for the first clause. */
4757 last
= xnew(len
+ 1, char);
4758 else if (len
+ 1 > allocated
)
4759 xrnew (last
, len
+ 1, char);
4760 allocated
= len
+ 1;
4761 strncpy (last
, cp
, len
);
4769 prolog_skip_comment (plb
, inf
)
4777 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
4778 if (cp
[0] == '*' && cp
[1] == '/')
4781 linecharno
+= readline (plb
, inf
);
4787 * A predicate or rule definition is added if it matches:
4788 * <beginning of line><Prolog Atom><whitespace>(
4789 * or <beginning of line><Prolog Atom><whitespace>:-
4791 * It is added to the tags database if it doesn't match the
4792 * name of the previous clause header.
4794 * Return the size of the name of the predicate or rule, or 0 if no
4800 char *last
; /* Name of last clause. */
4805 pos
= prolog_atom (s
, 0);
4810 pos
= skip_spaces (s
+ pos
) - s
;
4813 || (s
[pos
] == '(' && (pos
+= 1))
4814 || (s
[pos
] == ':' && s
[pos
+ 1] == '-' && (pos
+= 2)))
4815 && (last
== NULL
/* save only the first clause */
4816 || len
!= strlen (last
)
4817 || !strneq (s
, last
, len
)))
4819 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4827 * Consume a Prolog atom.
4828 * Return the number of bytes consumed, or -1 if there was an error.
4830 * A prolog atom, in this context, could be one of:
4831 * - An alphanumeric sequence, starting with a lower case letter.
4832 * - A quoted arbitrary string. Single quotes can escape themselves.
4833 * Backslash quotes everything.
4836 prolog_atom (s
, pos
)
4844 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
4846 /* The atom is unquoted. */
4848 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
4852 return pos
- origpos
;
4854 else if (s
[pos
] == '\'')
4865 pos
++; /* A double quote */
4867 else if (s
[pos
] == '\0')
4868 /* Multiline quoted atoms are ignored. */
4870 else if (s
[pos
] == '\\')
4872 if (s
[pos
+1] == '\0')
4879 return pos
- origpos
;
4887 * Support for Erlang
4889 * Generates tags for functions, defines, and records.
4890 * Assumes that Erlang functions start at column 0.
4891 * Original code by Anders Lindgren (1996)
4893 static int erlang_func
__P((char *, char *));
4894 static void erlang_attribute
__P((char *));
4895 static int erlang_atom
__P((char *, int));
4898 Erlang_functions (inf
)
4909 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4911 if (cp
[0] == '\0') /* Empty line */
4913 else if (iswhite (cp
[0])) /* Not function nor attribute */
4915 else if (cp
[0] == '%') /* comment */
4917 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
4919 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
4921 erlang_attribute (cp
);
4924 else if ((len
= erlang_func (cp
, last
)) > 0)
4927 * Function. Store the function name so that we only
4928 * generates a tag for the first clause.
4931 last
= xnew (len
+ 1, char);
4932 else if (len
+ 1 > allocated
)
4933 xrnew (last
, len
+ 1, char);
4934 allocated
= len
+ 1;
4935 strncpy (last
, cp
, len
);
4943 * A function definition is added if it matches:
4944 * <beginning of line><Erlang Atom><whitespace>(
4946 * It is added to the tags database if it doesn't match the
4947 * name of the previous clause header.
4949 * Return the size of the name of the function, or 0 if no function
4953 erlang_func (s
, last
)
4955 char *last
; /* Name of last clause. */
4960 pos
= erlang_atom (s
, 0);
4965 pos
= skip_spaces (s
+ pos
) - s
;
4967 /* Save only the first clause. */
4970 || len
!= (int)strlen (last
)
4971 || !strneq (s
, last
, len
)))
4973 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4982 * Handle attributes. Currently, tags are generated for defines
4985 * They are on the form:
4986 * -define(foo, bar).
4987 * -define(Foo(M, N), M+N).
4988 * -record(graph, {vtab = notable, cyclic = true}).
4991 erlang_attribute (s
)
4997 if (LOOKING_AT (s
, "-define") || LOOKING_AT (s
, "-record"))
4999 if (s
[pos
++] == '(')
5001 pos
= skip_spaces (s
+ pos
) - s
;
5002 len
= erlang_atom (s
, pos
);
5004 pfnote (savenstr (& s
[pos
], len
), TRUE
,
5005 s
, pos
+ len
, lineno
, linecharno
);
5013 * Consume an Erlang atom (or variable).
5014 * Return the number of bytes consumed, or -1 if there was an error.
5017 erlang_atom (s
, pos
)
5025 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
5027 /* The atom is unquoted. */
5029 while (ISALNUM (s
[pos
]) || s
[pos
] == '_')
5031 return pos
- origpos
;
5033 else if (s
[pos
] == '\'')
5044 else if (s
[pos
] == '\0')
5045 /* Multiline quoted atoms are ignored. */
5047 else if (s
[pos
] == '\\')
5049 if (s
[pos
+1] == '\0')
5056 return pos
- origpos
;
5063 #ifdef ETAGS_REGEXPS
5065 static char *scan_separators
__P((char *));
5066 static void analyse_regex
__P((char *, bool));
5067 static void add_regex
__P((char *, bool, language
*));
5068 static char *substitute
__P((char *, char *, struct re_registers
*));
5070 /* Take a string like "/blah/" and turn it into "blah", making sure
5071 that the first and last characters are the same, and handling
5072 quoted separator characters. Actually, stops on the occurrence of
5073 an unquoted separator. Also turns "\t" into a Tab character.
5074 Returns pointer to terminating separator. Works in place. Null
5075 terminates name string. */
5077 scan_separators (name
)
5081 char *copyto
= name
;
5082 bool quoted
= FALSE
;
5084 for (++name
; *name
!= '\0'; ++name
)
5090 else if (*name
== sep
)
5094 /* Something else is quoted, so preserve the quote. */
5100 else if (*name
== '\\')
5102 else if (*name
== sep
)
5108 /* Terminate copied string. */
5113 /* Look at the argument of --regex or --no-regex and do the right
5114 thing. Same for each line of a regexp file. */
5116 analyse_regex (regex_arg
, ignore_case
)
5120 if (regex_arg
== NULL
)
5122 free_patterns (); /* --no-regex: remove existing regexps */
5126 /* A real --regexp option or a line in a regexp file. */
5127 switch (regex_arg
[0])
5129 /* Comments in regexp file or null arg to --regex. */
5135 /* Read a regex file. This is recursive and may result in a
5136 loop, which will stop when the file descriptors are exhausted. */
5140 linebuffer regexbuf
;
5141 char *regexfile
= regex_arg
+ 1;
5143 /* regexfile is a file containing regexps, one per line. */
5144 regexfp
= fopen (regexfile
, "r");
5145 if (regexfp
== NULL
)
5150 initbuffer (®exbuf
);
5151 while (readline_internal (®exbuf
, regexfp
) > 0)
5152 analyse_regex (regexbuf
.buffer
, ignore_case
);
5153 free (regexbuf
.buffer
);
5158 /* Regexp to be used for a specific language only. */
5162 char *lang_name
= regex_arg
+ 1;
5165 for (cp
= lang_name
; *cp
!= '}'; cp
++)
5168 error ("unterminated language name in regex: %s", regex_arg
);
5172 lang
= get_language_from_langname (lang_name
);
5175 add_regex (cp
+ 1, ignore_case
, lang
);
5179 /* Regexp to be used for any language. */
5181 add_regex (regex_arg
, ignore_case
, NULL
);
5186 /* Turn a name, which is an ed-style (but Emacs syntax) regular
5187 expression, into a real regular expression by compiling it. */
5189 add_regex (regexp_pattern
, ignore_case
, lang
)
5190 char *regexp_pattern
;
5194 static struct re_pattern_buffer zeropattern
;
5197 struct re_pattern_buffer
*patbuf
;
5201 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
5203 error ("%s: unterminated regexp", regexp_pattern
);
5206 name
= scan_separators (regexp_pattern
);
5207 if (regexp_pattern
[0] == '\0')
5209 error ("null regexp", (char *)NULL
);
5212 (void) scan_separators (name
);
5214 patbuf
= xnew (1, struct re_pattern_buffer
);
5215 *patbuf
= zeropattern
;
5217 patbuf
->translate
= lc_trans
; /* translation table to fold case */
5219 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
5222 error ("%s while compiling pattern", err
);
5227 p_head
= xnew (1, pattern
);
5228 p_head
->regex
= savestr (regexp_pattern
);
5229 p_head
->p_next
= pp
;
5230 p_head
->lang
= lang
;
5231 p_head
->pat
= patbuf
;
5232 p_head
->name_pattern
= savestr (name
);
5233 p_head
->error_signaled
= FALSE
;
5237 * Do the substitutions indicated by the regular expression and
5241 substitute (in
, out
, regs
)
5243 struct re_registers
*regs
;
5246 int size
, dig
, diglen
;
5249 size
= strlen (out
);
5251 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5252 if (out
[size
- 1] == '\\')
5253 fatal ("pattern error in \"%s\"", out
);
5254 for (t
= etags_strchr (out
, '\\');
5256 t
= etags_strchr (t
+ 2, '\\'))
5260 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5266 /* Allocate space and do the substitutions. */
5267 result
= xnew (size
+ 1, char);
5269 for (t
= result
; *out
!= '\0'; out
++)
5270 if (*out
== '\\' && ISDIGIT (*++out
))
5273 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5274 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
5281 assert (t
<= result
+ size
&& t
- result
== (int)strlen (result
));
5286 /* Deallocate all patterns. */
5291 while (p_head
!= NULL
)
5293 pp
= p_head
->p_next
;
5294 free (p_head
->regex
);
5295 free (p_head
->name_pattern
);
5301 #endif /* ETAGS_REGEXPS */
5308 register int len
= 0;
5310 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
5312 if (*cp
== '\0' && !intoken (dbp
[len
]))
5324 register char *cp
, *name
;
5328 /* Go till you get to white space or a syntactic break */
5329 for (cp
= bp
+ 1; !notinname (*cp
); cp
++)
5331 name
= savenstr (bp
, cp
-bp
);
5333 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5337 /* Initialize a linebuffer for use */
5342 lbp
->size
= (DEBUG
) ? 3 : 200;
5343 lbp
->buffer
= xnew (lbp
->size
, char);
5344 lbp
->buffer
[0] = '\0';
5349 * Read a line of text from `stream' into `lbp', excluding the
5350 * newline or CR-NL, if any. Return the number of characters read from
5351 * `stream', which is the length of the line including the newline.
5353 * On DOS or Windows we do not count the CR character, if any, before the
5354 * NL, in the returned length; this mirrors the behavior of emacs on those
5355 * platforms (for text files, it translates CR-NL to NL as it reads in the
5359 readline_internal (lbp
, stream
)
5361 register FILE *stream
;
5363 char *buffer
= lbp
->buffer
;
5364 register char *p
= lbp
->buffer
;
5365 register char *pend
;
5368 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
5372 register int c
= getc (stream
);
5375 /* We're at the end of linebuffer: expand it. */
5377 xrnew (buffer
, lbp
->size
, char);
5378 p
+= buffer
- lbp
->buffer
;
5379 pend
= buffer
+ lbp
->size
;
5380 lbp
->buffer
= buffer
;
5390 if (p
> buffer
&& p
[-1] == '\r')
5394 /* Assume CRLF->LF translation will be performed by Emacs
5395 when loading this file, so CRs won't appear in the buffer.
5396 It would be cleaner to compensate within Emacs;
5397 however, Emacs does not know how many CRs were deleted
5398 before any given point in the file. */
5413 lbp
->len
= p
- buffer
;
5415 return lbp
->len
+ chars_deleted
;
5419 * Like readline_internal, above, but in addition try to match the
5420 * input line against relevant regular expressions.
5423 readline (lbp
, stream
)
5427 /* Read new line. */
5428 long result
= readline_internal (lbp
, stream
);
5430 if (!no_line_directive
5431 && result
> 12 && strneq (lbp
->buffer
, "#line ", 6))
5435 if (sscanf (lbp
->buffer
, "#line %d \"%n", &lno
, &start
) == 1)
5437 char *endp
= lbp
->buffer
+ start
;
5439 while ((endp
= etags_strchr (endp
, '"')) != NULL
5440 && endp
[-1] == '\\')
5444 int len
= endp
- (lbp
->buffer
+ start
);
5446 if (!strneq (curfile
, lbp
->buffer
+ start
, len
))
5447 curfile
= savenstr (lbp
->buffer
+ start
, len
);
5449 nocharno
= TRUE
; /* do not use char position for tags */
5450 return readline (lbp
, stream
);
5454 #ifdef ETAGS_REGEXPS
5459 /* Match against relevant patterns. */
5461 for (pp
= p_head
; pp
!= NULL
; pp
= pp
->p_next
)
5463 /* Only use generic regexps or those for the current language. */
5464 if (pp
->lang
!= NULL
&& pp
->lang
!= curlang
)
5467 match
= re_match (pp
->pat
, lbp
->buffer
, lbp
->len
, 0, &pp
->regs
);
5472 if (!pp
->error_signaled
)
5474 error ("error while matching \"%s\"", pp
->regex
);
5475 pp
->error_signaled
= TRUE
;
5482 /* Match occurred. Construct a tag. */
5483 if (pp
->name_pattern
[0] != '\0')
5485 /* Make a named tag. */
5486 char *name
= substitute (lbp
->buffer
,
5487 pp
->name_pattern
, &pp
->regs
);
5489 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
5493 /* Make an unnamed tag. */
5494 pfnote ((char *)NULL
, TRUE
,
5495 lbp
->buffer
, match
, lineno
, linecharno
);
5501 #endif /* ETAGS_REGEXPS */
5508 * Return a pointer to a space of size strlen(cp)+1 allocated
5509 * with xnew where the string CP has been copied.
5515 return savenstr (cp
, strlen (cp
));
5519 * Return a pointer to a space of size LEN+1 allocated with xnew where
5520 * the string CP has been copied for at most the first LEN characters.
5529 dp
= xnew (len
+ 1, char);
5530 strncpy (dp
, cp
, len
);
5536 * Return the ptr in sp at which the character c last
5537 * appears; NULL if not found
5539 * Identical to POSIX strrchr, included for portability.
5542 etags_strrchr (sp
, c
)
5543 register const char *sp
;
5546 register const char *r
;
5559 * Return the ptr in sp at which the character c first
5560 * appears; NULL if not found
5562 * Identical to POSIX strchr, included for portability.
5565 etags_strchr (sp
, c
)
5566 register const char *sp
;
5577 /* Skip spaces, return new pointer. */
5582 while (iswhite (*cp
))
5587 /* Skip non spaces, return new pointer. */
5589 skip_non_spaces (cp
)
5592 while (*cp
!= '\0' && !iswhite (*cp
))
5597 /* Print error message and exit. */
5615 suggest_asking_for_help ()
5617 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
5628 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
5631 const char *s1
, *s2
;
5633 fprintf (stderr
, "%s: ", progname
);
5634 fprintf (stderr
, s1
, s2
);
5635 fprintf (stderr
, "\n");
5638 /* Return a newly-allocated string whose contents
5639 concatenate those of s1, s2, s3. */
5644 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
5645 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
5647 strcpy (result
, s1
);
5648 strcpy (result
+ len1
, s2
);
5649 strcpy (result
+ len1
+ len2
, s3
);
5650 result
[len1
+ len2
+ len3
] = '\0';
5656 /* Does the same work as the system V getcwd, but does not need to
5657 guess the buffer size in advance. */
5663 char *path
= xnew (bufsize
, char);
5665 while (getcwd (path
, bufsize
) == NULL
)
5667 if (errno
!= ERANGE
)
5671 path
= xnew (bufsize
, char);
5674 canonicalize_filename (path
);
5677 #else /* not HAVE_GETCWD */
5680 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
5684 for (p
= path
; *p
!= '\0'; p
++)
5690 return strdup (path
);
5691 #else /* not MSDOS */
5696 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
5697 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
5702 #endif /* not MSDOS */
5703 #endif /* not HAVE_GETCWD */
5706 /* Return a newly allocated string containing the file name of FILE
5707 relative to the absolute directory DIR (which should end with a slash). */
5709 relative_filename (file
, dir
)
5712 char *fp
, *dp
, *afn
, *res
;
5715 /* Find the common root of file and dir (with a trailing slash). */
5716 afn
= absolute_filename (file
, cwd
);
5719 while (*fp
++ == *dp
++)
5721 fp
--, dp
--; /* back to the first differing char */
5723 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
5726 do /* look at the equal chars until '/' */
5730 /* Build a sequence of "../" strings for the resulting relative file name. */
5732 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
5734 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
5737 strcat (res
, "../");
5739 /* Add the file name relative to the common root of file and dir. */
5740 strcat (res
, fp
+ 1);
5746 /* Return a newly allocated string containing the absolute file name
5747 of FILE given DIR (which should end with a slash). */
5749 absolute_filename (file
, dir
)
5752 char *slashp
, *cp
, *res
;
5754 if (filename_is_absolute (file
))
5755 res
= savestr (file
);
5757 /* We don't support non-absolute file names with a drive
5758 letter, like `d:NAME' (it's too much hassle). */
5759 else if (file
[1] == ':')
5760 fatal ("%s: relative file names with drive letters not supported", file
);
5763 res
= concat (dir
, file
, "");
5765 /* Delete the "/dirname/.." and "/." substrings. */
5766 slashp
= etags_strchr (res
, '/');
5767 while (slashp
!= NULL
&& slashp
[0] != '\0')
5769 if (slashp
[1] == '.')
5771 if (slashp
[2] == '.'
5772 && (slashp
[3] == '/' || slashp
[3] == '\0'))
5777 while (cp
>= res
&& !filename_is_absolute (cp
));
5779 cp
= slashp
; /* the absolute name begins with "/.." */
5781 /* Under MSDOS and NT we get `d:/NAME' as absolute
5782 file name, so the luser could say `d:/../NAME'.
5783 We silently treat this as `d:/NAME'. */
5784 else if (cp
[0] != '/')
5787 strcpy (cp
, slashp
+ 3);
5791 else if (slashp
[2] == '/' || slashp
[2] == '\0')
5793 strcpy (slashp
, slashp
+ 2);
5798 slashp
= etags_strchr (slashp
+ 1, '/');
5802 return savestr ("/");
5807 /* Return a newly allocated string containing the absolute
5808 file name of dir where FILE resides given DIR (which should
5809 end with a slash). */
5811 absolute_dirname (file
, dir
)
5817 canonicalize_filename (file
);
5818 slashp
= etags_strrchr (file
, '/');
5820 return savestr (dir
);
5823 res
= absolute_filename (file
, dir
);
5829 /* Whether the argument string is an absolute file name. The argument
5830 string must have been canonicalized with canonicalize_filename. */
5832 filename_is_absolute (fn
)
5835 return (fn
[0] == '/'
5837 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
5842 /* Translate backslashes into slashes. Works in place. */
5844 canonicalize_filename (fn
)
5848 /* Canonicalize drive letter case. */
5849 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
5850 fn
[0] = upcase (fn
[0]);
5851 /* Convert backslashes to slashes. */
5852 for (; *fn
!= '\0'; fn
++)
5857 fn
= NULL
; /* shut up the compiler */
5861 /* Set the minimum size of a string contained in a linebuffer. */
5863 linebuffer_setlen (lbp
, toksize
)
5867 while (lbp
->size
<= toksize
)
5870 xrnew (lbp
->buffer
, lbp
->size
, char);
5875 /* Like malloc but get fatal error if memory is exhausted. */
5880 PTR result
= (PTR
) malloc (size
);
5882 fatal ("virtual memory exhausted", (char *)NULL
);
5887 xrealloc (ptr
, size
)
5891 PTR result
= (PTR
) realloc (ptr
, size
);
5893 fatal ("virtual memory exhausted", (char *)NULL
);
5899 * c-indentation-style: gnu
5900 * indent-tabs-mode: t
5902 * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer")