1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 87, 88, 89, 93, 94, 95, 98, 99, 2000
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 * Sam Kendall added C++.
28 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
29 * Regexp tags by Tom Tromey.
31 * Francesco Potorti` (pot@gnu.org) is the current maintainer.
34 char pot_etags_version
[] = "@(#) pot revision number is 13.47";
43 #if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
44 # define P_(proto) proto
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 #endif /* HAVE_CONFIG_H */
59 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
66 # include <sys/param.h>
68 # ifndef HAVE_CONFIG_H
70 # include <sys/config.h>
82 # define MAXPATHLEN _MAX_PATH
87 # endif /* not HAVE_CONFIG_H */
90 # endif /* undef HAVE_GETCWD */
91 #else /* !WINDOWSNT */
96 extern char *getenv ();
98 #endif /* !WINDOWSNT */
103 # if defined (HAVE_GETCWD) && !WINDOWSNT
104 extern char *getcwd (char *buf
, size_t size
);
106 #endif /* HAVE_UNISTD_H */
114 #include <sys/types.h>
115 #include <sys/stat.h>
117 #if !defined (S_ISREG) && defined (S_IFREG)
118 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
124 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
126 extern int optind
, opterr
;
127 #endif /* LONG_OPTIONS */
131 #endif /* ETAGS_REGEXPS */
133 /* Define CTAGS to make the program "ctags" compatible with the usual one.
134 Leave it undefined to make the program "etags", which makes emacs-style
135 tag tables and tags typedefs, #defines and struct/union/enum by default. */
143 /* Exit codes for success and failure. */
153 #define C_PLPL 0x00001 /* C++ */
154 #define C_STAR 0x00003 /* C* */
155 #define C_JAVA 0x00005 /* JAVA */
156 #define YACC 0x10000 /* yacc file */
158 #define streq(s,t) ((DEBUG && (s) == NULL && (t) == NULL \
159 && (abort (), 1)) || !strcmp (s, t))
160 #define strneq(s,t,n) ((DEBUG && (s) == NULL && (t) == NULL \
161 && (abort (), 1)) || !strncmp (s, t, n))
163 #define CHARS 256 /* 2^sizeof(char) */
164 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
165 #define iswhite(c) (_wht[CHAR(c)]) /* c is white */
166 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name */
167 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token */
168 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token */
169 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens */
171 #define ISALNUM(c) isalnum (CHAR(c))
172 #define ISALPHA(c) isalpha (CHAR(c))
173 #define ISDIGIT(c) isdigit (CHAR(c))
174 #define ISLOWER(c) islower (CHAR(c))
176 #define lowcase(c) tolower (CHAR(c))
177 #define upcase(c) toupper (CHAR(c))
181 * xnew, xrnew -- allocate, reallocate storage
183 * SYNOPSIS: Type *xnew (int n, Type);
184 * Type *xrnew (OldPointer, int n, Type);
187 # include "chkmalloc.h"
188 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
189 (n) * sizeof (Type)))
190 # define xrnew(op,n,Type) ((Type *) trace_realloc (__FILE__, __LINE__, \
191 (op), (n) * sizeof (Type)))
193 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
194 # define xrnew(op,n,Type) ((Type *) xrealloc ((op), (n) * sizeof (Type)))
199 typedef void Lang_function
P_((FILE *));
204 char *command
; /* Takes one arg and decompresses to stdout */
210 Lang_function
*function
;
216 typedef struct node_st
217 { /* sorting structure */
218 char *name
; /* function or type name */
219 char *file
; /* file name */
220 bool is_func
; /* use pattern or line no */
221 bool been_warned
; /* set if noticed dup */
222 int lno
; /* line number tag is on */
223 long cno
; /* character number line starts on */
224 char *pat
; /* search pattern */
225 struct node_st
*left
, *right
; /* left and right sons */
229 * A `linebuffer' is a structure which holds a line of text.
230 * `readline_internal' reads a line from a stream into a linebuffer
231 * and works regardless of the length of the line.
232 * SIZE is the size of BUFFER, LEN is the length of the string in
233 * BUFFER after readline reads it.
242 /* Many compilers barf on this:
243 Lang_function Ada_funcs;
244 so let's write it this way */
245 static void Ada_funcs
P_((FILE *));
246 static void Asm_labels
P_((FILE *));
247 static void C_entries
P_((int c_ext
, FILE *));
248 static void default_C_entries
P_((FILE *));
249 static void plain_C_entries
P_((FILE *));
250 static void Cjava_entries
P_((FILE *));
251 static void Cobol_paragraphs
P_((FILE *));
252 static void Cplusplus_entries
P_((FILE *));
253 static void Cstar_entries
P_((FILE *));
254 static void Erlang_functions
P_((FILE *));
255 static void Fortran_functions
P_((FILE *));
256 static void Yacc_entries
P_((FILE *));
257 static void Lisp_functions
P_((FILE *));
258 static void Makefile_targets
P_((FILE *));
259 static void Pascal_functions
P_((FILE *));
260 static void Perl_functions
P_((FILE *));
261 static void Postscript_functions
P_((FILE *));
262 static void Prolog_functions
P_((FILE *));
263 static void Python_functions
P_((FILE *));
264 static void Scheme_functions
P_((FILE *));
265 static void TeX_commands
P_((FILE *));
266 static void Texinfo_nodes
P_((FILE *));
267 static void just_read_file
P_((FILE *));
269 static void print_language_names
P_((void));
270 static void print_version
P_((void));
271 static void print_help
P_((void));
272 int main
P_((int, char **));
273 static int number_len
P_((long));
275 static compressor
*get_compressor_from_suffix
P_((char *, char **));
276 static language
*get_language_from_langname
P_((char *));
277 static language
*get_language_from_interpreter
P_((char *));
278 static language
*get_language_from_filename
P_((char *));
279 static int total_size_of_entries
P_((node
*));
280 static long readline
P_((linebuffer
*, FILE *));
281 static long readline_internal
P_((linebuffer
*, FILE *));
282 static void get_tag
P_((char *));
285 static void analyse_regex
P_((char *, bool));
286 static void add_regex
P_((char *, bool, language
*));
287 static void free_patterns
P_((void));
288 #endif /* ETAGS_REGEXPS */
289 static void error
P_((const char *, const char *));
290 static void suggest_asking_for_help
P_((void));
291 static void fatal
P_((char *, char *));
292 static void pfatal
P_((char *));
293 static void add_node
P_((node
*, node
**));
295 static void init
P_((void));
296 static void initbuffer
P_((linebuffer
*));
297 static void find_entries
P_((char *, FILE *));
298 static void free_tree
P_((node
*));
299 static void pfnote
P_((char *, bool, char *, int, int, long));
300 static void new_pfnote
P_((char *, int, bool, char *, int, int, long));
301 static void process_file
P_((char *));
302 static void put_entries
P_((node
*));
303 static void takeprec
P_((void));
305 static char *concat
P_((char *, char *, char *));
306 static char *skip_spaces
P_((char *));
307 static char *skip_non_spaces
P_((char *));
308 static char *savenstr
P_((char *, int));
309 static char *savestr
P_((char *));
310 static char *etags_strchr
P_((const char *, int));
311 static char *etags_strrchr
P_((const char *, int));
312 static char *etags_getcwd
P_((void));
313 static char *relative_filename
P_((char *, char *));
314 static char *absolute_filename
P_((char *, char *));
315 static char *absolute_dirname
P_((char *, char *));
316 static bool filename_is_absolute
P_((char *f
));
317 static void canonicalize_filename
P_((char *));
318 static void grow_linebuffer
P_((linebuffer
*, int));
319 long *xmalloc
P_((unsigned int));
320 long *xrealloc
P_((char *, unsigned int));
323 char searchar
= '/'; /* use /.../ searches */
325 char *tagfile
; /* output file */
326 char *progname
; /* name this program was invoked with */
327 char *cwd
; /* current working directory */
328 char *tagfiledir
; /* directory of tagfile */
329 FILE *tagf
; /* ioptr for tags file */
331 char *curfile
; /* current input file name */
332 language
*curlang
; /* current language */
334 int lineno
; /* line number of current line */
335 long charno
; /* current character number */
336 long linecharno
; /* charno of start of current line */
337 char *dbp
; /* pointer to start of current tag */
339 node
*head
; /* the head of the binary tree of tags */
341 linebuffer lb
; /* the current line */
342 linebuffer token_name
; /* used by C_entries as a temporary area */
346 linebuffer lb
; /* used by C_entries instead of lb */
349 /* boolean "functions" (see init) */
350 bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
353 *white
= " \f\t\n\r\v",
355 *nonam
= " \f\t\n\r(=,[;",
356 /* token ending chars */
357 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
358 /* token starting chars */
359 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
360 /* valid in-token chars */
361 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
363 bool append_to_tagfile
; /* -a: append to tags */
364 /* The following four default to TRUE for etags, but to FALSE for ctags. */
365 bool typedefs
; /* -t: create tags for C and Ada typedefs */
366 bool typedefs_and_cplusplus
; /* -T: create tags for C typedefs, level */
367 /* 0 struct/enum/union decls, and C++ */
368 /* member functions. */
369 bool constantypedefs
; /* -d: create tags for C #define, enum */
370 /* constants and variables. */
371 /* -D: opposite of -d. Default under ctags. */
372 bool declarations
; /* --declarations: tag them and extern in C&Co*/
373 bool globals
; /* create tags for global variables */
374 bool members
; /* create tags for C member variables */
375 bool update
; /* -u: update tags */
376 bool vgrind_style
; /* -v: create vgrind style index output */
377 bool no_warnings
; /* -w: suppress warnings */
378 bool cxref_style
; /* -x: create cxref style output */
379 bool cplusplus
; /* .[hc] means C++, not C */
380 bool noindentypedefs
; /* -I: ignore indentation in C */
381 bool packages_only
; /* --packages-only: in Ada, only tag packages*/
384 struct option longopts
[] =
386 { "packages-only", no_argument
, &packages_only
, TRUE
},
387 { "append", no_argument
, NULL
, 'a' },
388 { "backward-search", no_argument
, NULL
, 'B' },
389 { "c++", no_argument
, NULL
, 'C' },
390 { "cxref", no_argument
, NULL
, 'x' },
391 { "defines", no_argument
, NULL
, 'd' },
392 { "declarations", no_argument
, &declarations
, TRUE
},
393 { "no-defines", no_argument
, NULL
, 'D' },
394 { "globals", no_argument
, &globals
, TRUE
},
395 { "no-globals", no_argument
, &globals
, FALSE
},
396 { "help", no_argument
, NULL
, 'h' },
397 { "help", no_argument
, NULL
, 'H' },
398 { "ignore-indentation", no_argument
, NULL
, 'I' },
399 { "include", required_argument
, NULL
, 'i' },
400 { "language", required_argument
, NULL
, 'l' },
401 { "members", no_argument
, &members
, TRUE
},
402 { "no-members", no_argument
, &members
, FALSE
},
403 { "no-warn", no_argument
, NULL
, 'w' },
404 { "output", required_argument
, NULL
, 'o' },
406 { "regex", required_argument
, NULL
, 'r' },
407 { "no-regex", no_argument
, NULL
, 'R' },
408 { "ignore-case-regex", required_argument
, NULL
, 'c' },
409 #endif /* ETAGS_REGEXPS */
410 { "typedefs", no_argument
, NULL
, 't' },
411 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
412 { "update", no_argument
, NULL
, 'u' },
413 { "version", no_argument
, NULL
, 'V' },
414 { "vgrind", no_argument
, NULL
, 'v' },
417 #endif /* LONG_OPTIONS */
420 /* Structure defining a regular expression. Elements are
421 the compiled pattern, and the name string. */
422 typedef struct pattern
424 struct pattern
*p_next
;
427 struct re_pattern_buffer
*pattern
;
428 struct re_registers regs
;
433 /* List of all regexps. */
434 pattern
*p_head
= NULL
;
436 /* How many characters in the character set. (From regex.c.) */
437 #define CHAR_SET_SIZE 256
438 /* Translation table for case-insensitive matching. */
439 char lc_trans
[CHAR_SET_SIZE
];
440 #endif /* ETAGS_REGEXPS */
442 compressor compressors
[] =
444 { "z", "gzip -d -c"},
445 { "Z", "gzip -d -c"},
446 { "gz", "gzip -d -c"},
447 { "GZ", "gzip -d -c"},
448 { "bz2", "bzip2 -d -c" },
456 /* Non-NULL if language fixed. */
457 language
*forced_lang
= NULL
;
460 char *Ada_suffixes
[] =
461 { "ads", "adb", "ada", NULL
};
464 char *Asm_suffixes
[] = { "a", /* Unix assembler */
465 "asm", /* Microcontroller assembly */
466 "def", /* BSO/Tasking definition includes */
467 "inc", /* Microcontroller include files */
468 "ins", /* Microcontroller include files */
469 "s", "sa", /* Unix assembler */
470 "S", /* cpp-processed Unix assembler */
471 "src", /* BSO/Tasking C compiler output */
475 /* Note that .c and .h can be considered C++, if the --c++ flag was
476 given. That is why default_C_entries is called here. */
477 char *default_C_suffixes
[] =
480 char *Cplusplus_suffixes
[] =
481 { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
482 "M", /* Objective C++ */
483 "pdb", /* Postscript with C syntax */
486 char *Cjava_suffixes
[] =
489 char *Cobol_suffixes
[] =
490 { "COB", "cob", NULL
};
492 char *Cstar_suffixes
[] =
493 { "cs", "hs", NULL
};
495 char *Erlang_suffixes
[] =
496 { "erl", "hrl", NULL
};
498 char *Fortran_suffixes
[] =
499 { "F", "f", "f90", "for", NULL
};
501 char *Lisp_suffixes
[] =
502 { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL
};
504 char *Makefile_filenames
[] =
505 { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL
};
507 char *Pascal_suffixes
[] =
508 { "p", "pas", NULL
};
510 char *Perl_suffixes
[] =
511 { "pl", "pm", NULL
};
512 char *Perl_interpreters
[] =
513 { "perl", "@PERL@", NULL
};
515 char *plain_C_suffixes
[] =
516 { "lm", /* Objective lex file */
517 "m", /* Objective C file */
518 "pc", /* Pro*C file */
521 char *Postscript_suffixes
[] =
522 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
524 char *Prolog_suffixes
[] =
527 char *Python_suffixes
[] =
530 /* Can't do the `SCM' or `scm' prefix with a version number. */
531 char *Scheme_suffixes
[] =
532 { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL
};
534 char *TeX_suffixes
[] =
535 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
537 char *Texinfo_suffixes
[] =
538 { "texi", "texinfo", "txi", NULL
};
540 char *Yacc_suffixes
[] =
541 { "y", "y++", "ym", "yxx", "yy", NULL
}; /* .ym is Objective yacc file */
544 * Table of languages.
546 * It is ok for a given function to be listed under more than one
547 * name. I just didn't.
550 language lang_names
[] =
552 { "ada", Ada_funcs
, NULL
, Ada_suffixes
, NULL
},
553 { "asm", Asm_labels
, NULL
, Asm_suffixes
, NULL
},
554 { "c", default_C_entries
, NULL
, default_C_suffixes
, NULL
},
555 { "c++", Cplusplus_entries
, NULL
, Cplusplus_suffixes
, NULL
},
556 { "c*", Cstar_entries
, NULL
, Cstar_suffixes
, NULL
},
557 { "cobol", Cobol_paragraphs
, NULL
, Cobol_suffixes
, NULL
},
558 { "erlang", Erlang_functions
, NULL
, Erlang_suffixes
, NULL
},
559 { "fortran", Fortran_functions
, NULL
, Fortran_suffixes
, NULL
},
560 { "java", Cjava_entries
, NULL
, Cjava_suffixes
, NULL
},
561 { "lisp", Lisp_functions
, NULL
, Lisp_suffixes
, NULL
},
562 { "makefile", Makefile_targets
, Makefile_filenames
, NULL
, NULL
},
563 { "pascal", Pascal_functions
, NULL
, Pascal_suffixes
, NULL
},
564 { "perl", Perl_functions
, NULL
, Perl_suffixes
, Perl_interpreters
},
565 { "postscript", Postscript_functions
, NULL
, Postscript_suffixes
, NULL
},
566 { "proc", plain_C_entries
, NULL
, plain_C_suffixes
, NULL
},
567 { "prolog", Prolog_functions
, NULL
, Prolog_suffixes
, NULL
},
568 { "python", Python_functions
, NULL
, Python_suffixes
, NULL
},
569 { "scheme", Scheme_functions
, NULL
, Scheme_suffixes
, NULL
},
570 { "tex", TeX_commands
, NULL
, TeX_suffixes
, NULL
},
571 { "texinfo", Texinfo_nodes
, NULL
, Texinfo_suffixes
, NULL
},
572 { "yacc", Yacc_entries
, NULL
, Yacc_suffixes
, NULL
},
573 { "auto", NULL
}, /* default guessing scheme */
574 { "none", just_read_file
}, /* regexp matching only */
575 { NULL
, NULL
} /* end of list */
579 print_language_names ()
584 puts ("\nThese are the currently supported languages, along with the\n\
585 default file name suffixes:");
586 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
588 printf ("\t%s\t", lang
->name
);
589 if (lang
->suffixes
!= NULL
)
590 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
591 printf (" .%s", *ext
);
594 puts ("Where `auto' means use default language for files based on file\n\
595 name suffix, and `none' means only do regexp processing on files.\n\
596 If no language is specified and no matching suffix is found,\n\
597 the first line of the file is read for a sharp-bang (#!) sequence\n\
598 followed by the name of an interpreter. If no such sequence is found,\n\
599 Fortran is tried first; if no tags are found, C is tried next.\n\
600 Compressed files are supported using gzip and bzip2.");
604 # define EMACS_NAME "GNU Emacs"
607 # define VERSION "21"
612 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
613 puts ("Copyright (C) 1999 Free Software Foundation, Inc. and Ken Arnold");
614 puts ("This program is distributed under the same terms as Emacs");
622 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
624 These are the options accepted by %s.\n", progname
, progname
);
626 puts ("You may use unambiguous abbreviations for the long option names.");
628 puts ("Long option names do not work with this executable, as it is not\n\
629 linked with GNU getopt.");
630 #endif /* LONG_OPTIONS */
631 puts ("A - as file name means read names from stdin (one per line).");
633 printf (" Absolute names are stored in the output file as they are.\n\
634 Relative ones are stored relative to the output file's directory.");
637 puts ("-a, --append\n\
638 Append tag entries to existing tags file.");
640 puts ("--packages-only\n\
641 For Ada files, only generate tags for packages .");
644 puts ("-B, --backward-search\n\
645 Write the search commands for the tag entries using '?', the\n\
646 backward-search command instead of '/', the forward-search command.");
649 Treat files whose name suffix defaults to C language as C++ files.");
651 puts ("--declarations\n\
652 In C and derived languages, create tags for function declarations,");
654 puts ("\tand create tags for extern variables if --globals is used.");
657 ("\tand create tags for extern variables unless --no-globals is used.");
660 puts ("-d, --defines\n\
661 Create tag entries for C #define constants and enum constants, too.");
663 puts ("-D, --no-defines\n\
664 Don't create tag entries for C #define constants and enum constants.\n\
665 This makes the tags file smaller.");
669 puts ("-i FILE, --include=FILE\n\
670 Include a note in tag file indicating that, when searching for\n\
671 a tag, one should also consult the tags file FILE after\n\
672 checking the current file.");
673 puts ("-l LANG, --language=LANG\n\
674 Force the following files to be considered as written in the\n\
675 named language up to the next --language=LANG option.");
680 Create tag entries for global variables in some languages.");
682 puts ("--no-globals\n\
683 Do not create tag entries for global variables in some\n\
684 languages. This makes the tags file smaller.");
686 Create tag entries for member variables in C and derived languages.");
689 puts ("-r /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
690 Make a tag for each line matching pattern REGEXP in the following\n\
691 files. {LANGUAGE}/REGEXP/ uses REGEXP for LANGUAGE files only.\n\
692 regexfile is a file containing one REGEXP per line.\n\
693 REGEXP is anchored (as if preceded by ^).\n\
694 The form /REGEXP/NAME/ creates a named tag.\n\
695 For example Tcl named tags can be created with:\n\
696 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
697 puts ("-c /REGEXP/, --ignore-case-regex=/REGEXP/ or --ignore-case-regex=@regexfile\n\
698 Like -r, --regex but ignore case when matching expressions.");
699 puts ("-R, --no-regex\n\
700 Don't create tags from regexps for the following files.");
701 #endif /* ETAGS_REGEXPS */
702 puts ("-o FILE, --output=FILE\n\
703 Write the tags to FILE.");
704 puts ("-I, --ignore-indentation\n\
705 Don't rely on indentation quite as much as normal. Currently,\n\
706 this means not to assume that a closing brace in the first\n\
707 column is the final brace of a function or structure\n\
708 definition in C and C++.");
712 puts ("-t, --typedefs\n\
713 Generate tag entries for C and Ada typedefs.");
714 puts ("-T, --typedefs-and-c++\n\
715 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
716 and C++ member functions.");
717 puts ("-u, --update\n\
718 Update the tag entries for the given files, leaving tag\n\
719 entries for other files in place. Currently, this is\n\
720 implemented by deleting the existing entries for the given\n\
721 files and then rewriting the new entries at the end of the\n\
722 tags file. It is often faster to simply rebuild the entire\n\
723 tag file than to use this.");
724 puts ("-v, --vgrind\n\
725 Generates an index of items intended for human consumption,\n\
726 similar to the output of vgrind. The index is sorted, and\n\
727 gives the page number of each item.");
728 puts ("-w, --no-warn\n\
729 Suppress warning messages about entries defined in multiple\n\
731 puts ("-x, --cxref\n\
732 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
733 The output uses line numbers instead of page numbers, but\n\
734 beyond that the differences are cosmetic; try both to see\n\
738 puts ("-V, --version\n\
739 Print the version of the program.\n\
741 Print this help message.");
743 print_language_names ();
746 puts ("Report bugs to bug-gnu-emacs@gnu.org");
760 /* This structure helps us allow mixing of --lang and file names. */
763 enum argument_type arg_type
;
765 language
*lang
; /* language of the regexp */
768 #ifdef VMS /* VMS specific functions */
772 /* This is a BUG! ANY arbitrary limit is a BUG!
773 Won't someone please fix this? */
774 #define MAX_FILE_SPEC_LEN 255
777 char body
[MAX_FILE_SPEC_LEN
+ 1];
781 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
782 returning in each successive call the next file name matching the input
783 spec. The function expects that each in_spec passed
784 to it will be processed to completion; in particular, up to and
785 including the call following that in which the last matching name
786 is returned, the function ignores the value of in_spec, and will
787 only start processing a new spec with the following call.
788 If an error occurs, on return out_spec contains the value
789 of in_spec when the error occurred.
791 With each successive file name returned in out_spec, the
792 function's return value is one. When there are no more matching
793 names the function returns zero. If on the first call no file
794 matches in_spec, or there is any other error, -1 is returned.
799 #define OUTSIZE MAX_FILE_SPEC_LEN
805 static long context
= 0;
806 static struct dsc$descriptor_s o
;
807 static struct dsc$descriptor_s i
;
808 static bool pass1
= TRUE
;
815 o
.dsc$a_pointer
= (char *) out
;
816 o
.dsc$w_length
= (short)OUTSIZE
;
817 i
.dsc$a_pointer
= in
;
818 i
.dsc$w_length
= (short)strlen(in
);
819 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
820 i
.dsc$b_class
= DSC$K_CLASS_S
;
821 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
822 o
.dsc$b_class
= DSC$K_CLASS_VS
;
824 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
826 out
->body
[out
->curlen
] = EOS
;
829 else if (status
== RMS$_NMF
)
833 strcpy(out
->body
, in
);
836 lib$
find_file_end(&context
);
842 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
843 name of each file specified by the provided arg expanding wildcards.
846 gfnames (arg
, p_error
)
850 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
852 switch (fn_exp (&filename
, arg
))
856 return filename
.body
;
862 return filename
.body
;
866 #ifndef OLD /* Newer versions of VMS do provide `system'. */
870 error ("%s", "system() function not implemented under VMS");
874 #define VERSION_DELIM ';'
875 char *massage_name (s
)
881 if (*s
== VERSION_DELIM
)
899 unsigned int nincluded_files
;
900 char **included_files
;
903 int current_arg
, file_count
;
904 linebuffer filename_lb
;
910 _fmode
= O_BINARY
; /* all of files are treated as binary files */
915 included_files
= xnew (argc
, char *);
919 /* Allocate enough no matter what happens. Overkill, but each one
921 argbuffer
= xnew (argc
, argument
);
924 /* Set syntax for regular expression routines. */
925 re_set_syntax (RE_SYNTAX_EMACS
| RE_INTERVALS
);
926 /* Translation table for case-insensitive search. */
927 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
928 lc_trans
[i
] = lowcase (i
);
929 #endif /* ETAGS_REGEXPS */
932 * If etags, always find typedefs and structure tags. Why not?
933 * Also default is to find macro constants, enum constants and
938 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
949 optstring
= "-aCdDf:Il:o:r:c:RStTi:BuvxwVhH";
951 optstring
= "-aCdDf:Il:o:StTi:BuvxwVhH";
952 #endif /* ETAGS_REGEXPS */
955 optstring
= optstring
+ 1;
956 #endif /* LONG_OPTIONS */
958 opt
= getopt_long (argc
, argv
, optstring
, longopts
, 0);
965 /* If getopt returns 0, then it has already processed a
966 long-named option. We should do nothing. */
970 /* This means that a file name has been seen. Record it. */
971 argbuffer
[current_arg
].arg_type
= at_filename
;
972 argbuffer
[current_arg
].what
= optarg
;
977 /* Common options. */
978 case 'a': append_to_tagfile
= TRUE
; break;
979 case 'C': cplusplus
= TRUE
; break;
980 case 'd': constantypedefs
= TRUE
; break;
981 case 'D': constantypedefs
= FALSE
; break;
982 case 'f': /* for compatibility with old makefiles */
986 error ("-o option may only be given once.", (char *)NULL
);
987 suggest_asking_for_help ();
992 case 'S': /* for backward compatibility */
993 noindentypedefs
= TRUE
;
997 language
*lang
= get_language_from_langname (optarg
);
1000 argbuffer
[current_arg
].lang
= lang
;
1001 argbuffer
[current_arg
].arg_type
= at_language
;
1006 #ifdef ETAGS_REGEXPS
1008 argbuffer
[current_arg
].arg_type
= at_regexp
;
1009 argbuffer
[current_arg
].what
= optarg
;
1013 argbuffer
[current_arg
].arg_type
= at_regexp
;
1014 argbuffer
[current_arg
].what
= NULL
;
1018 argbuffer
[current_arg
].arg_type
= at_icregexp
;
1019 argbuffer
[current_arg
].what
= optarg
;
1022 #endif /* ETAGS_REGEXPS */
1034 typedefs
= typedefs_and_cplusplus
= TRUE
;
1039 included_files
[nincluded_files
++] = optarg
;
1042 /* Ctags options. */
1043 case 'B': searchar
= '?'; break;
1044 case 'u': update
= TRUE
; break;
1045 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1046 case 'x': cxref_style
= TRUE
; break;
1047 case 'w': no_warnings
= TRUE
; break;
1050 suggest_asking_for_help ();
1054 for (; optind
< argc
; ++optind
)
1056 argbuffer
[current_arg
].arg_type
= at_filename
;
1057 argbuffer
[current_arg
].what
= argv
[optind
];
1062 if (nincluded_files
== 0 && file_count
== 0)
1064 error ("no input files specified.", (char *)NULL
);
1065 suggest_asking_for_help ();
1068 if (tagfile
== NULL
)
1069 tagfile
= CTAGS
? "tags" : "TAGS";
1070 cwd
= etags_getcwd (); /* the current working directory */
1071 if (cwd
[strlen (cwd
) - 1] != '/')
1074 cwd
= concat (oldcwd
, "/", "");
1077 if (streq (tagfile
, "-"))
1080 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1082 init (); /* set up boolean "functions" */
1085 initbuffer (&token_name
);
1086 initbuffer (&lbs
[0].lb
);
1087 initbuffer (&lbs
[1].lb
);
1088 initbuffer (&filename_lb
);
1092 if (streq (tagfile
, "-"))
1096 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1097 doesn't take effect until after `stdout' is already open). */
1098 if (!isatty (fileno (stdout
)))
1099 setmode (fileno (stdout
), O_BINARY
);
1103 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1109 * Loop through files finding functions.
1111 for (i
= 0; i
< current_arg
; ++i
)
1113 switch (argbuffer
[i
].arg_type
)
1116 forced_lang
= argbuffer
[i
].lang
;
1118 #ifdef ETAGS_REGEXPS
1120 analyse_regex (argbuffer
[i
].what
, FALSE
);
1123 analyse_regex (argbuffer
[i
].what
, TRUE
);
1128 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1132 error ("can't find file %s\n", this_file
);
1137 this_file
= massage_name (this_file
);
1140 this_file
= argbuffer
[i
].what
;
1142 /* Input file named "-" means read file names from stdin
1143 (one per line) and use them. */
1144 if (streq (this_file
, "-"))
1145 while (readline_internal (&filename_lb
, stdin
) > 0)
1146 process_file (filename_lb
.buffer
);
1148 process_file (this_file
);
1156 #ifdef ETAGS_REGEXPS
1158 #endif /* ETAGS_REGEXPS */
1162 while (nincluded_files
-- > 0)
1163 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1169 /* If CTAGS, we are here. process_file did not write the tags yet,
1170 because we want them ordered. Let's do it now. */
1182 for (i
= 0; i
< current_arg
; ++i
)
1184 if (argbuffer
[i
].arg_type
!= at_filename
)
1187 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1188 tagfile
, argbuffer
[i
].what
, tagfile
);
1189 if (system (cmd
) != GOOD
)
1190 fatal ("failed to execute shell command", (char *)NULL
);
1192 append_to_tagfile
= TRUE
;
1195 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1206 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
1207 exit (system (cmd
));
1215 * Return a compressor given the file name. If EXTPTR is non-zero,
1216 * return a pointer into FILE where the compressor-specific
1217 * extension begins. If no compressor is found, NULL is returned
1218 * and EXTPTR is not significant.
1219 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca>
1222 get_compressor_from_suffix (file
, extptr
)
1227 char *slash
, *suffix
;
1229 /* This relies on FN to be after canonicalize_filename,
1230 so we don't need to consider backslashes on DOS_NT. */
1231 slash
= etags_strrchr (file
, '/');
1232 suffix
= etags_strrchr (file
, '.');
1233 if (suffix
== NULL
|| suffix
< slash
)
1238 /* Let those poor souls who live with DOS 8+3 file name limits get
1239 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1240 Only the first do loop is run if not MSDOS */
1243 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1244 if (streq (compr
->suffix
, suffix
))
1247 break; /* do it only once: not really a loop */
1250 } while (*suffix
!= '\0');
1257 * Return a language given the name.
1260 get_language_from_langname (name
)
1266 error ("empty language name", (char *)NULL
);
1269 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1270 if (streq (name
, lang
->name
))
1272 error ("unknown language \"%s\"", name
);
1280 * Return a language given the interpreter name.
1283 get_language_from_interpreter (interpreter
)
1289 if (interpreter
== NULL
)
1291 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1292 if (lang
->interpreters
!= NULL
)
1293 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1294 if (streq (*iname
, interpreter
))
1303 * Return a language given the file name.
1306 get_language_from_filename (file
)
1310 char **name
, **ext
, *suffix
;
1312 /* Try whole file name first. */
1313 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1314 if (lang
->filenames
!= NULL
)
1315 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1316 if (streq (*name
, file
))
1319 /* If not found, try suffix after last dot. */
1320 suffix
= etags_strrchr (file
, '.');
1324 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1325 if (lang
->suffixes
!= NULL
)
1326 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1327 if (streq (*ext
, suffix
))
1335 * This routine is called on each file argument.
1341 struct stat stat_buf
;
1344 char *compressed_name
, *uncompressed_name
;
1345 char *ext
, *real_name
;
1348 canonicalize_filename (file
);
1349 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1351 error ("skipping inclusion of %s in self.", file
);
1354 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1356 compressed_name
= NULL
;
1357 real_name
= uncompressed_name
= savestr (file
);
1361 real_name
= compressed_name
= savestr (file
);
1362 uncompressed_name
= savenstr (file
, ext
- file
);
1365 /* If the canonicalised uncompressed name has already be dealt with,
1366 skip it silently, else add it to the list. */
1368 typedef struct processed_file
1371 struct processed_file
*next
;
1373 static processed_file
*pf_head
= NULL
;
1374 register processed_file
*fnp
;
1376 for (fnp
= pf_head
; fnp
!= NULL
; fnp
= fnp
->next
)
1377 if (streq (uncompressed_name
, fnp
->filename
))
1380 pf_head
= xnew (1, struct processed_file
);
1381 pf_head
->filename
= savestr (uncompressed_name
);
1382 pf_head
->next
= fnp
;
1385 if (stat (real_name
, &stat_buf
) != 0)
1387 /* Reset real_name and try with a different name. */
1389 if (compressed_name
!= NULL
) /* try with the given suffix */
1391 if (stat (uncompressed_name
, &stat_buf
) == 0)
1392 real_name
= uncompressed_name
;
1394 else /* try all possible suffixes */
1396 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1398 compressed_name
= concat (file
, ".", compr
->suffix
);
1399 if (stat (compressed_name
, &stat_buf
) != 0)
1403 char *suf
= compressed_name
+ strlen (file
);
1404 size_t suflen
= strlen (compr
->suffix
) + 1;
1405 for ( ; suf
[1]; suf
++, suflen
--)
1407 memmove (suf
, suf
+ 1, suflen
);
1408 if (stat (compressed_name
, &stat_buf
) == 0)
1410 real_name
= compressed_name
;
1414 if (real_name
!= NULL
)
1417 free (compressed_name
);
1418 compressed_name
= NULL
;
1422 real_name
= compressed_name
;
1427 if (real_name
== NULL
)
1432 } /* try with a different name */
1434 if (!S_ISREG (stat_buf
.st_mode
))
1436 error ("skipping %s: it is not a regular file.", real_name
);
1439 if (real_name
== compressed_name
)
1441 char *cmd
= concat (compr
->command
, " ", real_name
);
1442 inf
= popen (cmd
, "r");
1446 inf
= fopen (real_name
, "r");
1453 find_entries (uncompressed_name
, inf
);
1455 if (real_name
== compressed_name
)
1464 if (filename_is_absolute (uncompressed_name
))
1466 /* file is an absolute file name. Canonicalise it. */
1467 filename
= absolute_filename (uncompressed_name
, cwd
);
1471 /* file is a file name relative to cwd. Make it relative
1472 to the directory of the tags file. */
1473 filename
= relative_filename (uncompressed_name
, tagfiledir
);
1475 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1483 if (compressed_name
) free(compressed_name
);
1484 if (uncompressed_name
) free(uncompressed_name
);
1489 * This routine sets up the boolean pseudo-functions which work
1490 * by setting boolean flags dependent upon the corresponding character.
1491 * Every char which is NOT in that string is not a white char. Therefore,
1492 * all of the array "_wht" is set to FALSE, and then the elements
1493 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1494 * of a char is TRUE if it is the string "white", else FALSE.
1502 for (i
= 0; i
< CHARS
; i
++)
1503 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1504 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1505 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1506 notinname('\0') = notinname('\n');
1507 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1508 begtoken('\0') = begtoken('\n');
1509 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1510 intoken('\0') = intoken('\n');
1511 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1512 endtoken('\0') = endtoken('\n');
1516 * This routine opens the specified file and calls the function
1517 * which finds the function and type definitions.
1519 node
*last_node
= NULL
;
1522 find_entries (file
, inf
)
1528 node
*old_last_node
;
1530 /* Memory leakage here: the string pointed by curfile is
1531 never released, because curfile is copied into np->file
1532 for each node, to be used in CTAGS mode. The amount of
1533 memory leaked here is the sum of the lengths of the
1535 curfile
= savestr (file
);
1537 /* If user specified a language, use it. */
1539 if (lang
!= NULL
&& lang
->function
!= NULL
)
1542 lang
->function (inf
);
1546 /* Try to guess the language given the file name. */
1547 lang
= get_language_from_filename (file
);
1548 if (lang
!= NULL
&& lang
->function
!= NULL
)
1551 lang
->function (inf
);
1555 /* Look for sharp-bang as the first two characters. */
1556 if (readline_internal (&lb
, inf
) > 0
1558 && lb
.buffer
[0] == '#'
1559 && lb
.buffer
[1] == '!')
1563 /* Set lp to point at the first char after the last slash in the
1564 line or, if no slashes, at the first nonblank. Then set cp to
1565 the first successive blank and terminate the string. */
1566 lp
= etags_strrchr (lb
.buffer
+2, '/');
1570 lp
= skip_spaces (lb
.buffer
+ 2);
1571 cp
= skip_non_spaces (lp
);
1574 if (strlen (lp
) > 0)
1576 lang
= get_language_from_interpreter (lp
);
1577 if (lang
!= NULL
&& lang
->function
!= NULL
)
1580 lang
->function (inf
);
1585 /* We rewind here, even if inf may be a pipe. We fail if the
1586 length of the first line is longer than the pipe block size,
1587 which is unlikely. */
1591 old_last_node
= last_node
;
1592 curlang
= get_language_from_langname ("fortran");
1593 Fortran_functions (inf
);
1595 /* No Fortran entries found. Try C. */
1596 if (old_last_node
== last_node
)
1598 /* We do not tag if rewind fails.
1599 Only the file name will be recorded in the tags file. */
1601 curlang
= get_language_from_langname (cplusplus
? "c++" : "c");
1602 default_C_entries (inf
);
1609 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1610 char *name
; /* tag name, or NULL if unnamed */
1611 bool is_func
; /* tag is a function */
1612 char *linestart
; /* start of the line where tag is */
1613 int linelen
; /* length of the line where tag is */
1614 int lno
; /* line number */
1615 long cno
; /* character number */
1619 if (CTAGS
&& name
== NULL
)
1622 np
= xnew (1, node
);
1624 /* If ctags mode, change name "main" to M<thisfilename>. */
1625 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1627 register char *fp
= etags_strrchr (curfile
, '/');
1628 np
->name
= concat ("M", fp
== NULL
? curfile
: fp
+ 1, "");
1629 fp
= etags_strrchr (np
->name
, '.');
1630 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
1635 np
->been_warned
= FALSE
;
1637 np
->is_func
= is_func
;
1639 /* Our char numbers are 0-base, because of C language tradition?
1640 ctags compatibility? old versions compatibility? I don't know.
1641 Anyway, since emacs's are 1-base we expect etags.el to take care
1642 of the difference. If we wanted to have 1-based numbers, we would
1643 uncomment the +1 below. */
1644 np
->cno
= cno
/* + 1 */ ;
1645 np
->left
= np
->right
= NULL
;
1646 if (CTAGS
&& !cxref_style
)
1648 if (strlen (linestart
) < 50)
1649 np
->pat
= concat (linestart
, "$", "");
1651 np
->pat
= savenstr (linestart
, 50);
1654 np
->pat
= savenstr (linestart
, linelen
);
1656 add_node (np
, &head
);
1659 /* Date: Wed, 22 Jan 1997 02:56:31 -0500 [last amended 18 Sep 1997]
1660 * From: Sam Kendall <kendall@mv.mv.com>
1661 * Subject: Proposal for firming up the TAGS format specification
1662 * To: F.Potorti@cnuce.cnr.it
1664 * pfnote should emit the optimized form [unnamed tag] only if:
1665 * 1. name does not contain any of the characters " \t\r\n(),;";
1666 * 2. linestart contains name as either a rightmost, or rightmost but
1667 * one character, substring;
1668 * 3. the character, if any, immediately before name in linestart must
1669 * be one of the characters " \t(),;";
1670 * 4. the character, if any, immediately after name in linestart must
1671 * also be one of the characters " \t(),;".
1673 * The real implementation uses the notinname() macro, which recognises
1674 * characters slightly different form " \t\r\n(),;". See the variable
1677 #define traditional_tag_style TRUE
1679 new_pfnote (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1680 char *name
; /* tag name, or NULL if unnamed */
1681 int namelen
; /* tag length */
1682 bool is_func
; /* tag is a function */
1683 char *linestart
; /* start of the line where tag is */
1684 int linelen
; /* length of the line where tag is */
1685 int lno
; /* line number */
1686 long cno
; /* character number */
1694 for (cp
= name
; !notinname (*cp
); cp
++)
1696 if (*cp
== '\0') /* rule #1 */
1698 cp
= linestart
+ linelen
- namelen
;
1699 if (notinname (linestart
[linelen
-1]))
1700 cp
-= 1; /* rule #4 */
1701 if (cp
>= linestart
/* rule #2 */
1703 || notinname (cp
[-1])) /* rule #3 */
1704 && strneq (name
, cp
, namelen
)) /* rule #2 */
1705 named
= FALSE
; /* use unnamed tag */
1710 name
= savenstr (name
, namelen
);
1713 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
1718 * recurse on left children, iterate on right children.
1726 register node
*node_right
= np
->right
;
1727 free_tree (np
->left
);
1728 if (np
->name
!= NULL
)
1738 * Adds a node to the tree of nodes. In etags mode, we don't keep
1739 * it sorted; we just keep a linear list. In ctags mode, maintain
1740 * an ordered tree, with no attempt at balancing.
1742 * add_node is the only function allowed to add nodes, so it can
1746 add_node (np
, cur_node_p
)
1747 node
*np
, **cur_node_p
;
1750 register node
*cur_node
= *cur_node_p
;
1752 if (cur_node
== NULL
)
1762 if (last_node
== NULL
)
1763 fatal ("internal error in add_node", (char *)NULL
);
1764 last_node
->right
= np
;
1770 dif
= strcmp (np
->name
, cur_node
->name
);
1773 * If this tag name matches an existing one, then
1774 * do not add the node, but maybe print a warning.
1778 if (streq (np
->file
, cur_node
->file
))
1782 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1783 np
->file
, lineno
, np
->name
);
1784 fprintf (stderr
, "Second entry ignored\n");
1787 else if (!cur_node
->been_warned
&& !no_warnings
)
1791 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1792 np
->file
, cur_node
->file
, np
->name
);
1793 cur_node
->been_warned
= TRUE
;
1798 /* Actually add the node */
1799 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1812 /* Output subentries that precede this one */
1813 put_entries (np
->left
);
1815 /* Output this entry */
1819 if (np
->name
!= NULL
)
1820 fprintf (tagf
, "%s\177%s\001%d,%ld\n",
1821 np
->pat
, np
->name
, np
->lno
, np
->cno
);
1823 fprintf (tagf
, "%s\177%d,%ld\n",
1824 np
->pat
, np
->lno
, np
->cno
);
1828 if (np
->name
== NULL
)
1829 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
1834 fprintf (stdout
, "%s %s %d\n",
1835 np
->name
, np
->file
, (np
->lno
+ 63) / 64);
1837 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1838 np
->name
, np
->lno
, np
->file
, np
->pat
);
1842 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->file
);
1846 putc (searchar
, tagf
);
1849 for (sp
= np
->pat
; *sp
; sp
++)
1851 if (*sp
== '\\' || *sp
== searchar
)
1855 putc (searchar
, tagf
);
1858 { /* a typedef; text pattern inadequate */
1859 fprintf (tagf
, "%d", np
->lno
);
1865 /* Output subentries that follow this one */
1866 put_entries (np
->right
);
1869 /* Length of a number's decimal representation. */
1875 while ((num
/= 10) > 0)
1881 * Return total number of characters that put_entries will output for
1882 * the nodes in the subtree of the specified node. Works only if
1883 * we are not ctags, but called only in that case. This count
1884 * is irrelevant with the new tags.el, but is still supplied for
1885 * backward compatibility.
1888 total_size_of_entries (np
)
1896 for (total
= 0; np
!= NULL
; np
= np
->right
)
1898 /* Count left subentries. */
1899 total
+= total_size_of_entries (np
->left
);
1901 /* Count this entry */
1902 total
+= strlen (np
->pat
) + 1;
1903 total
+= number_len ((long) np
->lno
) + 1 + number_len (np
->cno
) + 1;
1904 if (np
->name
!= NULL
)
1905 total
+= 1 + strlen (np
->name
); /* \001name */
1912 * The C symbol tables.
1917 st_C_objprot
, st_C_objimpl
, st_C_objend
,
1922 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1925 static unsigned int hash
P_((const char *, unsigned int));
1926 static struct C_stab_entry
* in_word_set
P_((const char *, unsigned int));
1927 static enum sym_type C_symtype
P_((char *, int, int));
1929 /* Feed stuff between (but not including) %[ and %] lines to:
1930 gperf -c -k 1,3 -o -p -r -t
1932 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1936 while, 0, st_C_ignore
1937 switch, 0, st_C_ignore
1938 return, 0, st_C_ignore
1939 @interface, 0, st_C_objprot
1940 @protocol, 0, st_C_objprot
1941 @implementation,0, st_C_objimpl
1942 @end, 0, st_C_objend
1943 import, C_JAVA, st_C_ignore
1944 package, C_JAVA, st_C_ignore
1945 friend, C_PLPL, st_C_ignore
1946 extends, C_JAVA, st_C_javastruct
1947 implements, C_JAVA, st_C_javastruct
1948 interface, C_JAVA, st_C_struct
1949 class, C_PLPL, st_C_struct
1950 namespace, C_PLPL, st_C_struct
1951 domain, C_STAR, st_C_struct
1952 union, 0, st_C_struct
1953 struct, 0, st_C_struct
1954 extern, 0, st_C_extern
1956 typedef, 0, st_C_typedef
1957 define, 0, st_C_define
1958 operator, C_PLPL, st_C_operator
1959 bool, C_PLPL, st_C_typespec
1960 long, 0, st_C_typespec
1961 short, 0, st_C_typespec
1962 int, 0, st_C_typespec
1963 char, 0, st_C_typespec
1964 float, 0, st_C_typespec
1965 double, 0, st_C_typespec
1966 signed, 0, st_C_typespec
1967 unsigned, 0, st_C_typespec
1968 auto, 0, st_C_typespec
1969 void, 0, st_C_typespec
1970 static, 0, st_C_typespec
1971 const, 0, st_C_typespec
1972 volatile, 0, st_C_typespec
1973 explicit, C_PLPL, st_C_typespec
1974 mutable, C_PLPL, st_C_typespec
1975 typename, C_PLPL, st_C_typespec
1976 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1977 DEFUN, 0, st_C_gnumacro
1978 SYSCALL, 0, st_C_gnumacro
1979 ENTRY, 0, st_C_gnumacro
1980 PSEUDO, 0, st_C_gnumacro
1981 # These are defined inside C functions, so currently they are not met.
1982 # EXFUN used in glibc, DEFVAR_* in emacs.
1983 #EXFUN, 0, st_C_gnumacro
1984 #DEFVAR_, 0, st_C_gnumacro
1986 and replace lines between %< and %> with its output. */
1988 /* C code produced by gperf version 2.7.1 (19981006 egcs) */
1989 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
1990 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1992 #define TOTAL_KEYWORDS 46
1993 #define MIN_WORD_LENGTH 2
1994 #define MAX_WORD_LENGTH 15
1995 #define MIN_HASH_VALUE 13
1996 #define MAX_HASH_VALUE 123
1997 /* maximum key range = 111, duplicates = 0 */
2004 register const char *str
;
2005 register unsigned int len
;
2007 static unsigned char asso_values
[] =
2009 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2010 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2011 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2012 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2013 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2014 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2015 124, 124, 124, 124, 3, 124, 124, 124, 43, 6,
2016 11, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2017 11, 124, 124, 58, 7, 124, 124, 124, 124, 124,
2018 124, 124, 124, 124, 124, 124, 124, 57, 7, 42,
2019 4, 14, 52, 0, 124, 53, 124, 124, 29, 11,
2020 6, 35, 32, 124, 29, 34, 59, 58, 51, 24,
2021 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2022 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2023 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2024 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2025 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2026 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2027 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2028 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2029 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2030 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2031 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2032 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2033 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2034 124, 124, 124, 124, 124, 124
2036 register int hval
= len
;
2042 hval
+= asso_values
[(unsigned char)str
[2]];
2045 hval
+= asso_values
[(unsigned char)str
[0]];
2054 static struct C_stab_entry
*
2055 in_word_set (str
, len
)
2056 register const char *str
;
2057 register unsigned int len
;
2059 static struct C_stab_entry wordlist
[] =
2061 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2062 {""}, {""}, {""}, {""},
2063 {"@end", 0, st_C_objend
},
2064 {""}, {""}, {""}, {""},
2065 {"ENTRY", 0, st_C_gnumacro
},
2066 {"@interface", 0, st_C_objprot
},
2068 {"domain", C_STAR
, st_C_struct
},
2070 {"PSEUDO", 0, st_C_gnumacro
},
2072 {"namespace", C_PLPL
, st_C_struct
},
2074 {"@implementation",0, st_C_objimpl
},
2075 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2076 {"long", 0, st_C_typespec
},
2077 {"signed", 0, st_C_typespec
},
2078 {"@protocol", 0, st_C_objprot
},
2079 {""}, {""}, {""}, {""},
2080 {"bool", C_PLPL
, st_C_typespec
},
2081 {""}, {""}, {""}, {""}, {""}, {""},
2082 {"const", 0, st_C_typespec
},
2083 {"explicit", C_PLPL
, st_C_typespec
},
2084 {"if", 0, st_C_ignore
},
2086 {"operator", C_PLPL
, st_C_operator
},
2088 {"DEFUN", 0, st_C_gnumacro
},
2090 {"define", 0, st_C_define
},
2091 {""}, {""}, {""}, {""}, {""},
2092 {"double", 0, st_C_typespec
},
2093 {"struct", 0, st_C_struct
},
2094 {""}, {""}, {""}, {""},
2095 {"short", 0, st_C_typespec
},
2097 {"enum", 0, st_C_enum
},
2098 {"mutable", C_PLPL
, st_C_typespec
},
2100 {"extern", 0, st_C_extern
},
2101 {"extends", C_JAVA
, st_C_javastruct
},
2102 {"package", C_JAVA
, st_C_ignore
},
2103 {"while", 0, st_C_ignore
},
2105 {"for", 0, st_C_ignore
},
2107 {"volatile", 0, st_C_typespec
},
2109 {"import", C_JAVA
, st_C_ignore
},
2110 {"float", 0, st_C_typespec
},
2111 {"switch", 0, st_C_ignore
},
2112 {"return", 0, st_C_ignore
},
2113 {"implements", C_JAVA
, st_C_javastruct
},
2115 {"static", 0, st_C_typespec
},
2116 {"typedef", 0, st_C_typedef
},
2117 {"typename", C_PLPL
, st_C_typespec
},
2118 {"unsigned", 0, st_C_typespec
},
2120 {"char", 0, st_C_typespec
},
2121 {"class", C_PLPL
, st_C_struct
},
2123 {"void", 0, st_C_typespec
},
2125 {"friend", C_PLPL
, st_C_ignore
},
2127 {"int", 0, st_C_typespec
},
2128 {"union", 0, st_C_struct
},
2130 {"auto", 0, st_C_typespec
},
2131 {"interface", C_JAVA
, st_C_struct
},
2133 {"SYSCALL", 0, st_C_gnumacro
}
2136 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2138 register int key
= hash (str
, len
);
2140 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2142 register const char *s
= wordlist
[key
].name
;
2144 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
2145 return &wordlist
[key
];
2152 static enum sym_type
2153 C_symtype (str
, len
, c_ext
)
2158 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2160 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2166 * C functions and variables are recognized using a simple
2167 * finite automaton. fvdef is its state variable.
2171 fvnone
, /* nothing seen */
2172 foperator
, /* func: operator keyword seen (cplpl) */
2173 fvnameseen
, /* function or variable name seen */
2174 fstartlist
, /* func: just after open parenthesis */
2175 finlist
, /* func: in parameter list */
2176 flistseen
, /* func: after parameter list */
2177 fignore
, /* func: before open brace */
2178 vignore
/* var-like: ignore until ';' */
2181 bool fvextern
; /* func or var: extern keyword seen; */
2184 * typedefs are recognized using a simple finite automaton.
2185 * typdef is its state variable.
2189 tnone
, /* nothing seen */
2190 tkeyseen
, /* typedef keyword seen */
2191 ttypeseen
, /* defined type seen */
2192 tinbody
, /* inside typedef body */
2193 tend
, /* just before typedef tag */
2194 tignore
/* junk after typedef tag */
2199 * struct-like structures (enum, struct and union) are recognized
2200 * using another simple finite automaton. `structdef' is its state
2205 snone
, /* nothing seen yet */
2206 skeyseen
, /* struct-like keyword seen */
2207 stagseen
, /* struct-like tag seen */
2208 scolonseen
, /* colon seen after struct-like tag */
2209 sinbody
/* in struct body: recognize member func defs*/
2213 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
2214 * struct tag, and structtype is the type of the preceding struct-like
2217 char *structtag
= "<uninited>";
2218 enum sym_type structtype
;
2221 * When objdef is different from onone, objtag is the name of the class.
2223 char *objtag
= "<uninited>";
2226 * Yet another little state machine to deal with preprocessor lines.
2230 dnone
, /* nothing seen */
2231 dsharpseen
, /* '#' seen as first char on line */
2232 ddefineseen
, /* '#' and 'define' seen */
2233 dignorerest
/* ignore rest of line */
2237 * State machine for Objective C protocols and implementations.
2238 * Tom R.Hageman <tom@basil.icce.rug.nl>
2242 onone
, /* nothing seen */
2243 oprotocol
, /* @interface or @protocol seen */
2244 oimplementation
, /* @implementations seen */
2245 otagseen
, /* class name seen */
2246 oparenseen
, /* parenthesis before category seen */
2247 ocatseen
, /* category name seen */
2248 oinbody
, /* in @implementation body */
2249 omethodsign
, /* in @implementation body, after +/- */
2250 omethodtag
, /* after method name */
2251 omethodcolon
, /* after method colon */
2252 omethodparm
, /* after method parameter */
2253 oignore
/* wait for @end */
2258 * Use this structure to keep info about the token read, and how it
2259 * should be tagged. Used by the make_C_tag function to build a tag.
2272 token tok
; /* latest token read */
2275 * Set this to TRUE, and the next token considered is called a function.
2276 * Used only for GNU emacs's function-defining macros.
2278 bool next_token_is_func
;
2281 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
2286 * methodlen is the length of the method name stored in token_name.
2290 static bool consider_token
P_((char *, int, int, int, int, int, bool *));
2291 static void make_C_tag
P_((bool));
2295 * checks to see if the current token is at the start of a
2296 * function or variable, or corresponds to a typedef, or
2297 * is a struct/union/enum tag, or #define, or an enum constant.
2299 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2300 * with args. C_EXT is which language we are looking at.
2308 * next_token_is_func IN OUT
2312 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func_or_var
)
2313 register char *str
; /* IN: token pointer */
2314 register int len
; /* IN: token length */
2315 register int c
; /* IN: first char after the token */
2316 int c_ext
; /* IN: C extensions mask */
2317 int cblev
; /* IN: curly brace level */
2318 int parlev
; /* IN: parenthesis level */
2319 bool *is_func_or_var
; /* OUT: function or variable found */
2321 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
2324 * Advance the definedef state machine.
2329 /* We're not on a preprocessor line. */
2332 if (toktype
== st_C_define
)
2334 definedef
= ddefineseen
;
2338 definedef
= dignorerest
;
2343 * Make a tag for any macro, unless it is a constant
2344 * and constantypedefs is FALSE.
2346 definedef
= dignorerest
;
2347 *is_func_or_var
= (c
== '(');
2348 if (!*is_func_or_var
&& !constantypedefs
)
2355 error ("internal error: definedef value.", (char *)NULL
);
2364 if (toktype
== st_C_typedef
)
2383 /* Do not return here, so the structdef stuff has a chance. */
2397 * This structdef business is currently only invoked when cblev==0.
2398 * It should be recursively invoked whatever the curly brace level,
2399 * and a stack of states kept, to allow for definitions of structs
2402 * This structdef business is NOT invoked when we are ctags and the
2403 * file is plain C. This is because a struct tag may have the same
2404 * name as another tag, and this loses with ctags.
2408 case st_C_javastruct
:
2409 if (structdef
== stagseen
)
2410 structdef
= scolonseen
;
2414 if (typdef
== tkeyseen
2415 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
2417 structdef
= skeyseen
;
2418 structtype
= toktype
;
2423 if (structdef
== skeyseen
)
2425 /* Save the tag for struct/union/class, for functions and variables
2426 that may be defined inside. */
2427 if (structtype
== st_C_struct
)
2428 structtag
= savenstr (str
, len
);
2430 structtag
= "<enum>";
2431 structdef
= stagseen
;
2435 if (typdef
!= tnone
)
2438 /* Detect GNU macros.
2440 Writers of emacs code are recommended to put the
2441 first two args of a DEFUN on the same line.
2443 The DEFUN macro, used in emacs C source code, has a first arg
2444 that is a string (the lisp function name), and a second arg that
2445 is a C function name. Since etags skips strings, the second arg
2446 is tagged. This is unfortunate, as it would be better to tag the
2447 first arg. The simplest way to deal with this problem would be
2448 to name the tag with a name built from the function name, by
2449 removing the initial 'F' character and substituting '-' for '_'.
2450 Anyway, this assumes that the conventions of naming lisp
2451 functions will never change. Currently, this method is not
2453 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
2455 next_token_is_func
= TRUE
;
2458 if (next_token_is_func
)
2460 next_token_is_func
= FALSE
;
2462 *is_func_or_var
= TRUE
;
2466 /* Detect Objective C constructs. */
2476 objdef
= oimplementation
;
2480 case oimplementation
:
2481 /* Save the class tag for functions or variables defined inside. */
2482 objtag
= savenstr (str
, len
);
2486 /* Save the class tag for categories. */
2487 objtag
= savenstr (str
, len
);
2489 *is_func_or_var
= TRUE
;
2493 *is_func_or_var
= TRUE
;
2500 objdef
= omethodtag
;
2502 grow_linebuffer (&token_name
, methodlen
+ 1);
2503 strncpy (token_name
.buffer
, str
, len
);
2504 token_name
.buffer
[methodlen
] = '\0';
2505 token_name
.len
= methodlen
;
2511 objdef
= omethodparm
;
2516 objdef
= omethodtag
;
2518 grow_linebuffer (&token_name
, methodlen
+ 1);
2519 strncat (token_name
.buffer
, str
, len
);
2520 token_name
.len
= methodlen
;
2525 if (toktype
== st_C_objend
)
2527 /* Memory leakage here: the string pointed by objtag is
2528 never released, because many tests would be needed to
2529 avoid breaking on incorrect input code. The amount of
2530 memory leaked here is the sum of the lengths of the
2538 /* A function, variable or enum constant? */
2545 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!= vignore
)
2546 fvdef
= fvnone
; /* should be useless */
2554 *is_func_or_var
= TRUE
;
2557 if ((c_ext
& C_PLPL
) && strneq (str
+len
-10, "::operator", 10))
2560 *is_func_or_var
= TRUE
;
2563 if (constantypedefs
&& structdef
== sinbody
&& structtype
== st_C_enum
)
2565 if (fvdef
== fvnone
)
2567 fvdef
= fvnameseen
; /* function or variable */
2568 *is_func_or_var
= TRUE
;
2579 * This routine finds functions, variables, typedefs,
2580 * #define's, enum constants and struct/union/enum definitions in
2581 * C syntax and adds them to the list.
2583 #define current_lb_is_new (newndx == curndx)
2584 #define switch_line_buffers() (curndx = 1 - curndx)
2586 #define curlb (lbs[curndx].lb)
2587 #define othlb (lbs[1-curndx].lb)
2588 #define newlb (lbs[newndx].lb)
2589 #define curlinepos (lbs[curndx].linepos)
2590 #define othlinepos (lbs[1-curndx].linepos)
2591 #define newlinepos (lbs[newndx].linepos)
2593 #define CNL_SAVE_DEFINEDEF() \
2595 curlinepos = charno; \
2597 linecharno = charno; \
2598 charno += readline (&curlb, inf); \
2599 lp = curlb.buffer; \
2606 CNL_SAVE_DEFINEDEF(); \
2607 if (savetok.valid) \
2610 savetok.valid = FALSE; \
2612 definedef = dnone; \
2620 /* This function should never be called when tok.valid is FALSE, but
2621 we must protect against invalid input or internal errors. */
2624 if (traditional_tag_style
)
2626 /* This was the original code. Now we call new_pfnote instead,
2627 which uses the new method for naming tags (see new_pfnote). */
2630 if (CTAGS
|| tok
.named
)
2631 name
= savestr (token_name
.buffer
);
2632 pfnote (name
, isfun
,
2633 tok
.buffer
, tok
.linelen
, tok
.lineno
, tok
.linepos
);
2636 new_pfnote (token_name
.buffer
, token_name
.len
, isfun
,
2637 tok
.buffer
, tok
.linelen
, tok
.lineno
, tok
.linepos
);
2646 C_entries (c_ext
, inf
)
2647 int c_ext
; /* extension of C */
2648 FILE *inf
; /* input file */
2650 register char c
; /* latest char read; '\0' for end of line */
2651 register char *lp
; /* pointer one beyond the character `c' */
2652 int curndx
, newndx
; /* indices for current and new lb */
2653 register int tokoff
; /* offset in line of start of current token */
2654 register int toklen
; /* length of current token */
2655 char *qualifier
; /* string used to qualify names */
2656 int qlen
; /* length of qualifier */
2657 int cblev
; /* current curly brace level */
2658 int parlev
; /* current parenthesis level */
2659 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
2660 bool purec
, cplpl
, cjava
;
2661 token savetok
; /* token saved during preprocessor handling */
2664 tokoff
= toklen
= 0; /* keep compiler quiet */
2665 curndx
= newndx
= 0;
2671 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
2672 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
2673 next_token_is_func
= yacc_rules
= FALSE
;
2674 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2675 tok
.valid
= savetok
.valid
= FALSE
;
2678 purec
= !(c_ext
& ~YACC
); /* no extensions (apart from possibly yacc) */
2679 cplpl
= (c_ext
& C_PLPL
) == C_PLPL
;
2680 cjava
= (c_ext
& C_JAVA
) == C_JAVA
;
2682 { qualifier
= "."; qlen
= 1; }
2684 { qualifier
= "::"; qlen
= 2; }
2691 /* If we're at the end of the line, the next character is a
2692 '\0'; don't skip it, because it's the thing that tells us
2693 to read the next line. */
2714 /* Newlines inside comments do not end macro definitions in
2716 CNL_SAVE_DEFINEDEF ();
2729 /* Newlines inside strings do not end macro definitions
2730 in traditional cpp, even though compilers don't
2731 usually accept them. */
2732 CNL_SAVE_DEFINEDEF ();
2742 /* Hmmm, something went wrong. */
2756 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2764 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2777 else if (/* cplpl && */ *lp
== '/')
2785 if ((c_ext
& YACC
) && *lp
== '%')
2787 /* entering or exiting rules section in yacc file */
2789 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
2790 typdef
= tnone
; structdef
= snone
;
2791 next_token_is_func
= FALSE
;
2792 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2794 yacc_rules
= !yacc_rules
;
2800 if (definedef
== dnone
)
2803 bool cpptoken
= TRUE
;
2805 /* Look back on this line. If all blanks, or nonblanks
2806 followed by an end of comment, this is a preprocessor
2808 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2811 if (*cp
== '*' && *(cp
+1) == '/')
2820 definedef
= dsharpseen
;
2821 } /* if (definedef == dnone) */
2827 /* Consider token only if some complicated conditions are satisfied. */
2828 if ((definedef
!= dnone
2829 || (cblev
== 0 && structdef
!= scolonseen
)
2830 || (cblev
== 1 && cplpl
&& structdef
== sinbody
)
2831 || (structdef
== sinbody
&& purec
))
2832 && typdef
!= tignore
2833 && definedef
!= dignorerest
2834 && fvdef
!= finlist
)
2840 bool funorvar
= FALSE
;
2842 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken (lp
[1]))
2845 * This handles :: in the middle, but not at the
2846 * beginning of an identifier. Also, space-separated
2847 * :: is not recognised.
2857 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2858 c_ext
, cblev
, parlev
, &funorvar
))
2860 if (fvdef
== foperator
)
2863 lp
= skip_spaces (lp
-1);
2867 && !iswhite (*lp
) && *lp
!= '(')
2870 toklen
+= lp
- oldlp
;
2875 && definedef
== dnone
2876 && structdef
== sinbody
)
2877 /* function or var defined in C++ class body */
2879 int len
= strlen (structtag
) + qlen
+ toklen
;
2880 grow_linebuffer (&token_name
, len
+ 1);
2881 strcpy (token_name
.buffer
, structtag
);
2882 strcat (token_name
.buffer
, qualifier
);
2883 strncat (token_name
.buffer
,
2884 newlb
.buffer
+ tokoff
, toklen
);
2885 token_name
.len
= len
;
2888 else if (objdef
== ocatseen
)
2889 /* Objective C category */
2891 int len
= strlen (objtag
) + 2 + toklen
;
2892 grow_linebuffer (&token_name
, len
+ 1);
2893 strcpy (token_name
.buffer
, objtag
);
2894 strcat (token_name
.buffer
, "(");
2895 strncat (token_name
.buffer
,
2896 newlb
.buffer
+ tokoff
, toklen
);
2897 strcat (token_name
.buffer
, ")");
2898 token_name
.len
= len
;
2901 else if (objdef
== omethodtag
2902 || objdef
== omethodparm
)
2903 /* Objective C method */
2909 grow_linebuffer (&token_name
, toklen
+ 1);
2910 strncpy (token_name
.buffer
,
2911 newlb
.buffer
+ tokoff
, toklen
);
2912 token_name
.buffer
[toklen
] = '\0';
2913 token_name
.len
= toklen
;
2914 /* Name macros and members. */
2915 tok
.named
= (structdef
== stagseen
2916 || typdef
== ttypeseen
2919 && definedef
== dignorerest
)
2921 && definedef
== dnone
2922 && structdef
== sinbody
));
2924 tok
.lineno
= lineno
;
2925 tok
.linelen
= tokoff
+ toklen
+ 1;
2926 tok
.buffer
= newlb
.buffer
;
2927 tok
.linepos
= newlinepos
;
2930 if (definedef
== dnone
2931 && (fvdef
== fvnameseen
2932 || fvdef
== foperator
2933 || structdef
== stagseen
2935 || objdef
!= onone
))
2937 if (current_lb_is_new
)
2938 switch_line_buffers ();
2941 make_C_tag (funorvar
);
2945 } /* if (endtoken (c)) */
2946 else if (intoken (c
))
2952 } /* if (midtoken) */
2953 else if (begtoken (c
))
2964 make_C_tag (TRUE
); /* a function */
2971 if (structdef
== stagseen
&& !cjava
)
2977 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2979 tokoff
= lp
- 1 - newlb
.buffer
;
2984 } /* if (begtoken) */
2985 } /* if must look at token */
2988 /* Detect end of line, colon, comma, semicolon and various braces
2989 after having handled a token.*/
2993 if (definedef
!= dnone
)
2999 make_C_tag (TRUE
); /* an Objective C class */
3003 objdef
= omethodcolon
;
3005 grow_linebuffer (&token_name
, methodlen
+ 1);
3006 strcat (token_name
.buffer
, ":");
3007 token_name
.len
= methodlen
;
3010 if (structdef
== stagseen
)
3011 structdef
= scolonseen
;
3018 make_C_tag (FALSE
); /* a yacc function */
3029 if (definedef
!= dnone
)
3035 make_C_tag (FALSE
); /* a typedef */
3045 if ((members
&& cblev
== 1)
3046 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3047 make_C_tag (FALSE
); /* a variable */
3053 if (declarations
&& (cblev
== 0 || cblev
== 1))
3054 make_C_tag (TRUE
); /* a function declaration */
3059 /* The following instruction invalidates the token.
3060 Probably the token should be invalidated in all
3061 other cases where some state machine is reset. */
3064 if (structdef
== stagseen
)
3068 if (definedef
!= dnone
)
3074 make_C_tag (TRUE
); /* an Objective C method */
3086 if ((members
&& cblev
== 1)
3087 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3088 make_C_tag (FALSE
); /* a variable */
3093 if (structdef
== stagseen
)
3097 if (definedef
!= dnone
)
3099 if (cblev
== 0 && typdef
== tend
)
3102 make_C_tag (FALSE
); /* a typedef */
3113 if ((members
&& cblev
== 1)
3114 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3115 make_C_tag (FALSE
); /* a variable */
3120 if (structdef
== stagseen
)
3124 if (definedef
!= dnone
)
3126 if (objdef
== otagseen
&& parlev
== 0)
3127 objdef
= oparenseen
;
3131 if (typdef
== ttypeseen
3134 && structdef
!= sinbody
)
3136 /* This handles constructs like:
3137 typedef void OperatorFun (int fun); */
3152 if (definedef
!= dnone
)
3154 if (objdef
== ocatseen
&& parlev
== 1)
3156 make_C_tag (TRUE
); /* an Objective C category */
3168 if (cblev
== 0 && (typdef
== tend
))
3171 make_C_tag (FALSE
); /* a typedef */
3174 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3178 if (definedef
!= dnone
)
3180 if (typdef
== ttypeseen
)
3184 case skeyseen
: /* unnamed struct */
3185 structdef
= sinbody
;
3186 structtag
= "_anonymous_";
3189 case scolonseen
: /* named struct */
3190 structdef
= sinbody
;
3191 make_C_tag (FALSE
); /* a struct */
3197 make_C_tag (TRUE
); /* a function */
3206 make_C_tag (TRUE
); /* an Objective C class */
3211 make_C_tag (TRUE
); /* an Objective C method */
3215 /* Neutralize `extern "C" {' grot. */
3216 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
3223 if (definedef
!= dnone
)
3225 if (fvdef
== fstartlist
)
3226 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3229 if (definedef
!= dnone
)
3231 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
3233 cblev
= 0; /* reset curly brace level if first column */
3234 parlev
= 0; /* also reset paren level, just in case... */
3240 if (typdef
== tinbody
)
3242 /* Memory leakage here: the string pointed by structtag is
3243 never released, because I fear to miss something and
3244 break things while freeing the area. The amount of
3245 memory leaked here is the sum of the lengths of the
3247 if (structdef == sinbody)
3248 free (structtag); */
3251 structtag
= "<error>";
3255 if (definedef
!= dnone
)
3265 if ((members
&& cblev
== 1)
3266 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3267 make_C_tag (FALSE
); /* a variable */
3275 if (objdef
== oinbody
&& cblev
== 0)
3277 objdef
= omethodsign
;
3281 case '#': case '~': case '&': case '%': case '/': case '|':
3282 case '^': case '!': case '<': case '>': case '.': case '?': case ']':
3283 if (definedef
!= dnone
)
3285 /* These surely cannot follow a function tag in C. */
3298 if (objdef
== otagseen
)
3300 make_C_tag (TRUE
); /* an Objective C class */
3303 /* If a macro spans multiple lines don't reset its state. */
3305 CNL_SAVE_DEFINEDEF ();
3311 } /* while not eof */
3315 * Process either a C++ file or a C file depending on the setting
3319 default_C_entries (inf
)
3322 C_entries (cplusplus
? C_PLPL
: 0, inf
);
3325 /* Always do plain ANSI C. */
3327 plain_C_entries (inf
)
3333 /* Always do C++. */
3335 Cplusplus_entries (inf
)
3338 C_entries (C_PLPL
, inf
);
3341 /* Always do Java. */
3346 C_entries (C_JAVA
, inf
);
3354 C_entries (C_STAR
, inf
);
3357 /* Always do Yacc. */
3362 C_entries (YACC
, inf
);
3365 /* A useful macro. */
3366 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3367 for (lineno = charno = 0; /* loop initialization */ \
3368 !feof (file_pointer) /* loop test */ \
3369 && (lineno++, /* instructions at start of loop */ \
3370 linecharno = charno, \
3371 charno += readline (&line_buffer, file_pointer), \
3372 char_pointer = lb.buffer, \
3378 * Read a file, but do no processing. This is used to do regexp
3379 * matching on files that have no language defined.
3382 just_read_file (inf
)
3385 register char *dummy
;
3387 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
3391 /* Fortran parsing */
3393 static bool tail
P_((char *));
3394 static void takeprec
P_((void));
3395 static void getit
P_((FILE *));
3401 register int len
= 0;
3403 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
3405 if (*cp
== '\0' && !intoken (dbp
[len
]))
3416 dbp
= skip_spaces (dbp
);
3420 dbp
= skip_spaces (dbp
);
3421 if (strneq (dbp
, "(*)", 3))
3426 if (!ISDIGIT (*dbp
))
3428 --dbp
; /* force failure */
3433 while (ISDIGIT (*dbp
));
3442 dbp
= skip_spaces (dbp
);
3446 linecharno
= charno
;
3447 charno
+= readline (&lb
, inf
);
3452 dbp
= skip_spaces (dbp
);
3454 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
3456 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
3458 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
3459 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3464 Fortran_functions (inf
)
3467 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3470 dbp
++; /* Ratfor escape to fortran */
3471 dbp
= skip_spaces (dbp
);
3474 switch (lowcase (*dbp
))
3477 if (tail ("integer"))
3485 if (tail ("logical"))
3489 if (tail ("complex") || tail ("character"))
3493 if (tail ("double"))
3495 dbp
= skip_spaces (dbp
);
3498 if (tail ("precision"))
3504 dbp
= skip_spaces (dbp
);
3507 switch (lowcase (*dbp
))
3510 if (tail ("function"))
3514 if (tail ("subroutine"))
3522 if (tail ("blockdata") || tail ("block data"))
3524 dbp
= skip_spaces (dbp
);
3525 if (*dbp
== '\0') /* assume un-named */
3526 pfnote (savestr ("blockdata"), TRUE
,
3527 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
3529 getit (inf
); /* look for name */
3537 * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be>, 1998-04-24
3541 static void adagetit
P_((FILE *, char *));
3543 /* Once we are positioned after an "interesting" keyword, let's get
3544 the real tag value necessary. */
3546 adagetit (inf
, name_qualifier
)
3548 char *name_qualifier
;
3556 dbp
= skip_spaces (dbp
);
3558 || (dbp
[0] == '-' && dbp
[1] == '-'))
3561 linecharno
= charno
;
3562 charno
+= readline (&lb
, inf
);
3571 /* Skipping body of procedure body or package body or ....
3572 resetting qualifier to body instead of spec. */
3573 name_qualifier
= "/b";
3579 /* Skipping type of task type or protected type ... */
3587 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
3592 dbp
= skip_spaces (dbp
);
3595 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
3603 name
= concat (dbp
, name_qualifier
, "");
3605 pfnote (name
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3616 bool inquote
= FALSE
;
3618 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3620 while (*dbp
!= '\0')
3622 /* Skip a string i.e. "abcd". */
3623 if (inquote
|| (*dbp
== '"'))
3625 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
3630 continue; /* advance char */
3635 break; /* advance line */
3639 /* Skip comments. */
3640 if (dbp
[0] == '-' && dbp
[1] == '-')
3641 break; /* advance line */
3643 /* Skip character enclosed in single quote i.e. 'a'
3644 and skip single quote starting an attribute i.e. 'Image. */
3653 /* Search for beginning of a token. */
3654 if (!begtoken (*dbp
))
3657 continue; /* advance char */
3660 /* We are at the beginning of a token. */
3665 if (!packages_only
&& tail ("function"))
3666 adagetit (inf
, "/f");
3668 break; /* from switch */
3669 continue; /* advance char */
3672 if (!packages_only
&& tail ("procedure"))
3673 adagetit (inf
, "/p");
3674 else if (tail ("package"))
3675 adagetit (inf
, "/s");
3676 else if (tail ("protected")) /* protected type */
3677 adagetit (inf
, "/t");
3679 break; /* from switch */
3680 continue; /* advance char */
3683 if (!packages_only
&& tail ("task"))
3684 adagetit (inf
, "/k");
3685 else if (typedefs
&& !packages_only
&& tail ("type"))
3687 adagetit (inf
, "/t");
3688 while (*dbp
!= '\0')
3692 break; /* from switch */
3693 continue; /* advance char */
3696 /* Look for the end of the token. */
3697 while (!endtoken (*dbp
))
3700 } /* advance char */
3701 } /* advance line */
3705 * Bob Weiner, Motorola Inc., 4/3/94
3706 * Unix and microcontroller assembly tag handling
3707 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
3715 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3717 /* If first char is alphabetic or one of [_.$], test for colon
3718 following identifier. */
3719 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3721 /* Read past label. */
3723 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3725 if (*cp
== ':' || iswhite (*cp
))
3727 /* Found end of label, so copy it and add it to the table. */
3728 pfnote (savenstr(lb
.buffer
, cp
-lb
.buffer
), TRUE
,
3729 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3736 * Perl support by Bart Robinson <lomew@cs.utah.edu>
3737 * enhanced by Michael Ernst <mernst@alum.mit.edu>
3738 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
3739 * Perl variable names: /^(my|local).../
3742 Perl_functions (inf
)
3747 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3751 && *cp
++ == 'b' && iswhite (*cp
++))
3753 cp
= skip_spaces (cp
);
3758 && !iswhite (*cp
) && *cp
!= '{' && *cp
!= '(')
3760 pfnote (savenstr (sp
, cp
-sp
), TRUE
,
3761 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3764 else if (globals
/* only if tagging global vars is enabled */
3765 && ((cp
= lb
.buffer
,
3774 && (*cp
== '(' || iswhite (*cp
)))
3776 /* After "my" or "local", but before any following paren or space. */
3777 char *varname
= NULL
;
3779 cp
= skip_spaces (cp
);
3780 if (*cp
== '$' || *cp
== '@' || *cp
== '%')
3782 char* varstart
= ++cp
;
3783 while (ISALNUM (*cp
) || *cp
== '_')
3785 varname
= savenstr (varstart
, cp
-varstart
);
3789 /* Should be examining a variable list at this point;
3790 could insist on seeing an open parenthesis. */
3791 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
3795 /* Perhaps I should back cp up one character, so the TAGS table
3796 doesn't mention (and so depend upon) the following char. */
3797 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : varname
,
3798 FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3804 * Python support by Eric S. Raymond <esr@thyrsus.com>
3805 * Look for /^def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
3808 Python_functions (inf
)
3813 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3817 && *cp
++ == 'f' && iswhite (*cp
++))
3819 cp
= skip_spaces (cp
);
3820 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3823 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3831 && *cp
++ == 's' && iswhite (*cp
++))
3833 cp
= skip_spaces (cp
);
3834 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3837 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3842 /* Idea by Corny de Souza
3843 * Cobol tag functions
3844 * We could look for anything that could be a paragraph name.
3845 * i.e. anything that starts in column 8 is one word and ends in a full stop.
3848 Cobol_paragraphs (inf
)
3851 register char *bp
, *ep
;
3853 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
3859 /* If eoln, compiler option or comment ignore whole line. */
3860 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
3863 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
3866 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
3867 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
3875 Makefile_targets (inf
)
3880 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
3882 if (*bp
== '\t' || *bp
== '#')
3884 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
3887 pfnote (savenstr (lb
.buffer
, bp
- lb
.buffer
), TRUE
,
3888 lb
.buffer
, bp
- lb
.buffer
+ 1, lineno
, linecharno
);
3892 /* Added by Mosur Mohan, 4/22/88 */
3893 /* Pascal parsing */
3896 * Locates tags for procedures & functions. Doesn't do any type- or
3897 * var-definitions. It does look for the keyword "extern" or
3898 * "forward" immediately following the procedure statement; if found,
3899 * the tag is skipped.
3902 Pascal_functions (inf
)
3905 linebuffer tline
; /* mostly copied from C_entries */
3907 int save_lineno
, save_len
;
3908 char c
, *cp
, *namebuf
;
3910 bool /* each of these flags is TRUE iff: */
3911 incomment
, /* point is inside a comment */
3912 inquote
, /* point is inside '..' string */
3913 get_tagname
, /* point is after PROCEDURE/FUNCTION
3914 keyword, so next item = potential tag */
3915 found_tag
, /* point is after a potential tag */
3916 inparms
, /* point is within parameter-list */
3917 verify_tag
; /* point has passed the parm-list, so the
3918 next token will determine whether this
3919 is a FORWARD/EXTERN to be ignored, or
3920 whether it is a real tag */
3922 save_lcno
= save_lineno
= save_len
= 0; /* keep compiler quiet */
3923 namebuf
= NULL
; /* keep compiler quiet */
3928 initbuffer (&tline
);
3930 incomment
= inquote
= FALSE
;
3931 found_tag
= FALSE
; /* have a proc name; check if extern */
3932 get_tagname
= FALSE
; /* have found "procedure" keyword */
3933 inparms
= FALSE
; /* found '(' after "proc" */
3934 verify_tag
= FALSE
; /* check if "extern" is ahead */
3937 while (!feof (inf
)) /* long main loop to get next char */
3940 if (c
== '\0') /* if end of line */
3943 linecharno
= charno
;
3944 charno
+= readline (&lb
, inf
);
3948 if (!((found_tag
&& verify_tag
)
3950 c
= *dbp
++; /* only if don't need *dbp pointing
3951 to the beginning of the name of
3952 the procedure or function */
3956 if (c
== '}') /* within { } comments */
3958 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
3975 inquote
= TRUE
; /* found first quote */
3977 case '{': /* found open { comment */
3981 if (*dbp
== '*') /* found open (* comment */
3986 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
3989 case ')': /* end of parms list */
3994 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4001 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4003 /* check if this is an "extern" declaration */
4006 if (lowcase (*dbp
== 'e'))
4008 if (tail ("extern")) /* superfluous, really! */
4014 else if (lowcase (*dbp
) == 'f')
4016 if (tail ("forward")) /* check for forward reference */
4022 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4026 pfnote (namebuf
, TRUE
,
4027 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
4031 if (get_tagname
) /* grab name of proc or fn */
4036 /* save all values for later tagging */
4037 grow_linebuffer (&tline
, lb
.len
+ 1);
4038 strcpy (tline
.buffer
, lb
.buffer
);
4039 save_lineno
= lineno
;
4040 save_lcno
= linecharno
;
4042 /* grab block name */
4043 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4045 namebuf
= savenstr (dbp
, cp
-dbp
);
4046 dbp
= cp
; /* set dbp to e-o-token */
4047 save_len
= dbp
- lb
.buffer
+ 1;
4048 get_tagname
= FALSE
;
4052 /* and proceed to check for "extern" */
4054 else if (!incomment
&& !inquote
&& !found_tag
)
4056 /* check for proc/fn keywords */
4057 switch (lowcase (c
))
4060 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
4064 if (tail ("unction"))
4069 } /* while not eof */
4071 free (tline
.buffer
);
4075 * lisp tag functions
4076 * look for (def or (DEF, quote or QUOTE
4079 static int L_isdef
P_((char *));
4080 static int L_isquote
P_((char *));
4081 static void L_getit
P_((void));
4085 register char *strp
;
4087 return ((strp
[1] == 'd' || strp
[1] == 'D')
4088 && (strp
[2] == 'e' || strp
[2] == 'E')
4089 && (strp
[3] == 'f' || strp
[3] == 'F'));
4094 register char *strp
;
4096 return ((*++strp
== 'q' || *strp
== 'Q')
4097 && (*++strp
== 'u' || *strp
== 'U')
4098 && (*++strp
== 'o' || *strp
== 'O')
4099 && (*++strp
== 't' || *strp
== 'T')
4100 && (*++strp
== 'e' || *strp
== 'E')
4101 && iswhite (*++strp
));
4109 if (*dbp
== '\'') /* Skip prefix quote */
4111 else if (*dbp
== '(')
4113 if (L_isquote (dbp
))
4114 dbp
+= 7; /* Skip "(quote " */
4116 dbp
+= 1; /* Skip "(" before name in (defstruct (foo)) */
4117 dbp
= skip_spaces (dbp
);
4120 for (cp
= dbp
/*+1*/;
4121 *cp
!= '\0' && *cp
!= '(' && !iswhite(*cp
) && *cp
!= ')';
4127 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
4128 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4132 Lisp_functions (inf
)
4135 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4141 dbp
= skip_non_spaces (dbp
);
4142 dbp
= skip_spaces (dbp
);
4147 /* Check for (foo::defmumble name-defined ... */
4150 while (*dbp
!= '\0' && !iswhite (*dbp
)
4151 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
4156 while (*dbp
== ':');
4158 if (L_isdef (dbp
- 1))
4160 dbp
= skip_non_spaces (dbp
);
4161 dbp
= skip_spaces (dbp
);
4171 * Postscript tag functions
4172 * Just look for lines where the first character is '/'
4173 * Richard Mlynarik <mly@adoc.xerox.com>
4174 * Also look at "defineps" for PSWrap
4175 * suggested by Masatake YAMATO <masata-y@is.aist-nara.ac.jp>
4178 Postscript_functions (inf
)
4181 register char *bp
, *ep
;
4183 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4188 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4191 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
4192 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4194 else if (strneq (bp
, "defineps", 8))
4196 bp
= skip_non_spaces (bp
);
4197 bp
= skip_spaces (bp
);
4205 * Scheme tag functions
4206 * look for (def... xyzzy
4207 * look for (def... (xyzzy
4208 * look for (def ... ((...(xyzzy ....
4209 * look for (set! xyzzy
4213 Scheme_functions (inf
)
4218 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4221 && (bp
[1] == 'D' || bp
[1] == 'd')
4222 && (bp
[2] == 'E' || bp
[2] == 'e')
4223 && (bp
[3] == 'F' || bp
[3] == 'f'))
4225 bp
= skip_non_spaces (bp
);
4226 /* Skip over open parens and white space */
4227 while (iswhite (*bp
) || *bp
== '(')
4232 && (bp
[1] == 'S' || bp
[1] == 's')
4233 && (bp
[2] == 'E' || bp
[2] == 'e')
4234 && (bp
[3] == 'T' || bp
[3] == 't')
4235 && (bp
[4] == '!' || bp
[4] == '!')
4236 && (iswhite (bp
[5])))
4238 bp
= skip_non_spaces (bp
);
4239 bp
= skip_spaces (bp
);
4245 /* Find tags in TeX and LaTeX input files. */
4247 /* TEX_toktab is a table of TeX control sequences that define tags.
4248 Each TEX_tabent records one such control sequence.
4249 CONVERT THIS TO USE THE Stab TYPE!! */
4256 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
4258 /* Default set of control sequences to put into TEX_toktab.
4259 The value of environment var TEXTAGS is prepended to this. */
4261 char *TEX_defenv
= "\
4262 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4263 :part:appendix:entry:index";
4265 static void TEX_mode
P_((FILE *));
4266 static struct TEX_tabent
*TEX_decode_env
P_((char *, char *));
4267 static int TEX_Token
P_((char *));
4269 char TEX_esc
= '\\';
4270 char TEX_opgrp
= '{';
4271 char TEX_clgrp
= '}';
4274 * TeX/LaTeX scanning loop.
4283 /* Select either \ or ! as escape character. */
4286 /* Initialize token table once from environment. */
4288 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
4290 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4293 /* Look at each esc in line. */
4294 while ((cp
= etags_strchr (cp
, TEX_esc
)) != NULL
)
4298 linecharno
+= cp
- lasthit
;
4300 i
= TEX_Token (lasthit
);
4303 /* We seem to include the TeX command in the tag name.
4305 for (p = lasthit + TEX_toktab[i].len;
4306 *p != '\0' && *p != TEX_clgrp;
4309 pfnote (/*savenstr (lasthit, p-lasthit)*/ (char *)NULL
, TRUE
,
4310 lb
.buffer
, lb
.len
, lineno
, linecharno
);
4311 break; /* We only tag a line once */
4317 #define TEX_LESC '\\'
4318 #define TEX_SESC '!'
4321 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
4322 chars accordingly. */
4329 while ((c
= getc (inf
)) != EOF
)
4331 /* Skip to next line if we hit the TeX comment char. */
4335 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
4351 /* If the input file is compressed, inf is a pipe, and rewind may fail.
4352 No attempt is made to correct the situation. */
4356 /* Read environment and prepend it to the default string.
4357 Build token table. */
4358 static struct TEX_tabent
*
4359 TEX_decode_env (evarname
, defenv
)
4363 register char *env
, *p
;
4365 struct TEX_tabent
*tab
;
4368 /* Append default string to environment. */
4369 env
= getenv (evarname
);
4375 env
= concat (oldenv
, defenv
, "");
4378 /* Allocate a token table */
4379 for (size
= 1, p
= env
; p
;)
4380 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
4382 /* Add 1 to leave room for null terminator. */
4383 tab
= xnew (size
+ 1, struct TEX_tabent
);
4385 /* Unpack environment string into token table. Be careful about */
4386 /* zero-length strings (leading ':', "::" and trailing ':') */
4389 p
= etags_strchr (env
, ':');
4390 if (!p
) /* End of environment string. */
4391 p
= env
+ strlen (env
);
4393 { /* Only non-zero strings. */
4394 tab
[i
].name
= savenstr (env
, p
- env
);
4395 tab
[i
].len
= strlen (tab
[i
].name
);
4402 tab
[i
].name
= NULL
; /* Mark end of table. */
4410 /* If the text at CP matches one of the tag-defining TeX command names,
4411 return the pointer to the first occurrence of that command in TEX_toktab.
4412 Otherwise return -1.
4413 Keep the capital `T' in `token' for dumb truncating compilers
4414 (this distinguishes it from `TEX_toktab' */
4421 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
4422 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
4427 /* Texinfo support. Dave Love, Mar. 2000. */
4433 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4439 && *cp
++ == 'e' && iswhite (*cp
++)))
4441 start
= cp
= skip_spaces(cp
);
4442 while (*cp
!= '\0' && *cp
!= ',')
4444 pfnote (savenstr (start
, cp
- start
), TRUE
,
4445 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4451 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
4453 * Assumes that the predicate starts at column 0.
4454 * Only the first clause of a predicate is added.
4456 static int prolog_pred
P_((char *, char *));
4457 static void prolog_skip_comment
P_((linebuffer
*, FILE *));
4458 static int prolog_atom
P_((char *, int));
4461 Prolog_functions (inf
)
4472 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4474 if (cp
[0] == '\0') /* Empty line */
4476 else if (iswhite (cp
[0])) /* Not a predicate */
4478 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
4479 prolog_skip_comment (&lb
, inf
);
4480 else if ((len
= prolog_pred (cp
, last
)) > 0)
4482 /* Predicate. Store the function name so that we only
4483 generate a tag for the first clause. */
4485 last
= xnew(len
+ 1, char);
4486 else if (len
+ 1 > allocated
)
4487 last
= xrnew (last
, len
+ 1, char);
4488 allocated
= len
+ 1;
4489 strncpy (last
, cp
, len
);
4497 prolog_skip_comment (plb
, inf
)
4505 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
4506 if (cp
[0] == '*' && cp
[1] == '/')
4509 linecharno
+= readline (plb
, inf
);
4515 * A predicate definition is added if it matches:
4516 * <beginning of line><Prolog Atom><whitespace>(
4518 * It is added to the tags database if it doesn't match the
4519 * name of the previous clause header.
4521 * Return the size of the name of the predicate, or 0 if no header
4525 prolog_pred (s
, last
)
4527 char *last
; /* Name of last clause. */
4532 pos
= prolog_atom (s
, 0);
4537 pos
= skip_spaces (s
+ pos
) - s
;
4539 if ((s
[pos
] == '(') || (s
[pos
] == '.'))
4544 /* Save only the first clause. */
4546 || len
!= (int)strlen (last
)
4547 || !strneq (s
, last
, len
))
4549 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4557 * Consume a Prolog atom.
4558 * Return the number of bytes consumed, or -1 if there was an error.
4560 * A prolog atom, in this context, could be one of:
4561 * - An alphanumeric sequence, starting with a lower case letter.
4562 * - A quoted arbitrary string. Single quotes can escape themselves.
4563 * Backslash quotes everything.
4566 prolog_atom (s
, pos
)
4574 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
4576 /* The atom is unquoted. */
4578 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
4582 return pos
- origpos
;
4584 else if (s
[pos
] == '\'')
4595 pos
++; /* A double quote */
4597 else if (s
[pos
] == '\0')
4598 /* Multiline quoted atoms are ignored. */
4600 else if (s
[pos
] == '\\')
4602 if (s
[pos
+1] == '\0')
4609 return pos
- origpos
;
4616 * Support for Erlang -- Anders Lindgren, Feb 1996.
4618 * Generates tags for functions, defines, and records.
4620 * Assumes that Erlang functions start at column 0.
4622 static int erlang_func
P_((char *, char *));
4623 static void erlang_attribute
P_((char *));
4624 static int erlang_atom
P_((char *, int));
4627 Erlang_functions (inf
)
4638 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4640 if (cp
[0] == '\0') /* Empty line */
4642 else if (iswhite (cp
[0])) /* Not function nor attribute */
4644 else if (cp
[0] == '%') /* comment */
4646 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
4648 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
4650 erlang_attribute (cp
);
4653 else if ((len
= erlang_func (cp
, last
)) > 0)
4656 * Function. Store the function name so that we only
4657 * generates a tag for the first clause.
4660 last
= xnew (len
+ 1, char);
4661 else if (len
+ 1 > allocated
)
4662 last
= xrnew (last
, len
+ 1, char);
4663 allocated
= len
+ 1;
4664 strncpy (last
, cp
, len
);
4672 * A function definition is added if it matches:
4673 * <beginning of line><Erlang Atom><whitespace>(
4675 * It is added to the tags database if it doesn't match the
4676 * name of the previous clause header.
4678 * Return the size of the name of the function, or 0 if no function
4682 erlang_func (s
, last
)
4684 char *last
; /* Name of last clause. */
4689 pos
= erlang_atom (s
, 0);
4694 pos
= skip_spaces (s
+ pos
) - s
;
4696 /* Save only the first clause. */
4699 || len
!= (int)strlen (last
)
4700 || !strneq (s
, last
, len
)))
4702 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4711 * Handle attributes. Currently, tags are generated for defines
4714 * They are on the form:
4715 * -define(foo, bar).
4716 * -define(Foo(M, N), M+N).
4717 * -record(graph, {vtab = notable, cyclic = true}).
4720 erlang_attribute (s
)
4726 if (strneq (s
, "-define", 7) || strneq (s
, "-record", 7))
4728 pos
= skip_spaces (s
+ 7) - s
;
4729 if (s
[pos
++] == '(')
4731 pos
= skip_spaces (s
+ pos
) - s
;
4732 len
= erlang_atom (s
, pos
);
4734 pfnote (savenstr (& s
[pos
], len
), TRUE
,
4735 s
, pos
+ len
, lineno
, linecharno
);
4743 * Consume an Erlang atom (or variable).
4744 * Return the number of bytes consumed, or -1 if there was an error.
4747 erlang_atom (s
, pos
)
4755 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
4757 /* The atom is unquoted. */
4759 while (ISALNUM (s
[pos
]) || s
[pos
] == '_')
4761 return pos
- origpos
;
4763 else if (s
[pos
] == '\'')
4774 else if (s
[pos
] == '\0')
4775 /* Multiline quoted atoms are ignored. */
4777 else if (s
[pos
] == '\\')
4779 if (s
[pos
+1] == '\0')
4786 return pos
- origpos
;
4792 #ifdef ETAGS_REGEXPS
4794 static char *scan_separators
P_((char *));
4795 static void analyse_regex
P_((char *, bool));
4796 static void add_regex
P_((char *, bool, language
*));
4797 static char *substitute
P_((char *, char *, struct re_registers
*));
4799 /* Take a string like "/blah/" and turn it into "blah", making sure
4800 that the first and last characters are the same, and handling
4801 quoted separator characters. Actually, stops on the occurrence of
4802 an unquoted separator. Also turns "\t" into a Tab character.
4803 Returns pointer to terminating separator. Works in place. Null
4804 terminates name string. */
4806 scan_separators (name
)
4810 char *copyto
= name
;
4811 bool quoted
= FALSE
;
4813 for (++name
; *name
!= '\0'; ++name
)
4819 else if (*name
== sep
)
4823 /* Something else is quoted, so preserve the quote. */
4829 else if (*name
== '\\')
4831 else if (*name
== sep
)
4837 /* Terminate copied string. */
4842 /* Look at the argument of --regex or --no-regex and do the right
4843 thing. Same for each line of a regexp file. */
4845 analyse_regex (regex_arg
, ignore_case
)
4849 if (regex_arg
== NULL
)
4850 free_patterns (); /* --no-regex: remove existing regexps */
4852 /* A real --regexp option or a line in a regexp file. */
4853 switch (regex_arg
[0])
4855 /* Comments in regexp file or null arg to --regex. */
4861 /* Read a regex file. This is recursive and may result in a
4862 loop, which will stop when the file descriptors are exhausted. */
4866 linebuffer regexbuf
;
4867 char *regexfile
= regex_arg
+ 1;
4869 /* regexfile is a file containing regexps, one per line. */
4870 regexfp
= fopen (regexfile
, "r");
4871 if (regexfp
== NULL
)
4876 initbuffer (®exbuf
);
4877 while (readline_internal (®exbuf
, regexfp
) > 0)
4878 analyse_regex (regexbuf
.buffer
, ignore_case
);
4879 free (regexbuf
.buffer
);
4884 /* Regexp to be used for a specific language only. */
4888 char *lang_name
= regex_arg
+ 1;
4891 for (cp
= lang_name
; *cp
!= '}'; cp
++)
4894 error ("unterminated language name in regex: %s", regex_arg
);
4898 lang
= get_language_from_langname (lang_name
);
4901 add_regex (cp
+ 1, ignore_case
, lang
);
4905 /* Regexp to be used for any language. */
4907 add_regex (regex_arg
, ignore_case
, NULL
);
4912 /* Turn a name, which is an ed-style (but Emacs syntax) regular
4913 expression, into a real regular expression by compiling it. */
4915 add_regex (regexp_pattern
, ignore_case
, lang
)
4916 char *regexp_pattern
;
4922 struct re_pattern_buffer
*patbuf
;
4926 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
4928 error ("%s: unterminated regexp", regexp_pattern
);
4931 name
= scan_separators (regexp_pattern
);
4932 if (regexp_pattern
[0] == '\0')
4934 error ("null regexp", (char *)NULL
);
4937 (void) scan_separators (name
);
4939 patbuf
= xnew (1, struct re_pattern_buffer
);
4940 /* Translation table to fold case if appropriate. */
4941 patbuf
->translate
= (ignore_case
) ? lc_trans
: NULL
;
4942 patbuf
->fastmap
= NULL
;
4943 patbuf
->buffer
= NULL
;
4944 patbuf
->allocated
= 0;
4946 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
4949 error ("%s while compiling pattern", err
);
4954 p_head
= xnew (1, pattern
);
4955 p_head
->regex
= savestr (regexp_pattern
);
4956 p_head
->p_next
= pp
;
4957 p_head
->language
= lang
;
4958 p_head
->pattern
= patbuf
;
4959 p_head
->name_pattern
= savestr (name
);
4960 p_head
->error_signaled
= FALSE
;
4964 * Do the substitutions indicated by the regular expression and
4968 substitute (in
, out
, regs
)
4970 struct re_registers
*regs
;
4973 int size
, dig
, diglen
;
4976 size
= strlen (out
);
4978 /* Pass 1: figure out how much to allocate by finding all \N strings. */
4979 if (out
[size
- 1] == '\\')
4980 fatal ("pattern error in \"%s\"", out
);
4981 for (t
= etags_strchr (out
, '\\');
4983 t
= etags_strchr (t
+ 2, '\\'))
4987 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
4993 /* Allocate space and do the substitutions. */
4994 result
= xnew (size
+ 1, char);
4996 for (t
= result
; *out
!= '\0'; out
++)
4997 if (*out
== '\\' && ISDIGIT (*++out
))
4999 /* Using "dig2" satisfies my debugger. Bleah. */
5001 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5002 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
5009 if (DEBUG
&& (t
> result
+ size
|| t
- result
!= (int)strlen (result
)))
5015 /* Deallocate all patterns. */
5020 while (p_head
!= NULL
)
5022 pp
= p_head
->p_next
;
5023 free (p_head
->regex
);
5024 free (p_head
->name_pattern
);
5039 /* Go till you get to white space or a syntactic break */
5041 *cp
!= '\0' && *cp
!= '(' && *cp
!= ')' && !iswhite (*cp
);
5044 pfnote (savenstr (bp
, cp
-bp
), TRUE
,
5045 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5048 #endif /* ETAGS_REGEXPS */
5049 /* Initialize a linebuffer for use */
5055 lbp
->buffer
= xnew (200, char);
5059 * Read a line of text from `stream' into `lbp', excluding the
5060 * newline or CR-NL, if any. Return the number of characters read from
5061 * `stream', which is the length of the line including the newline.
5063 * On DOS or Windows we do not count the CR character, if any, before the
5064 * NL, in the returned length; this mirrors the behavior of emacs on those
5065 * platforms (for text files, it translates CR-NL to NL as it reads in the
5069 readline_internal (lbp
, stream
)
5071 register FILE *stream
;
5073 char *buffer
= lbp
->buffer
;
5074 register char *p
= lbp
->buffer
;
5075 register char *pend
;
5078 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
5082 register int c
= getc (stream
);
5085 /* We're at the end of linebuffer: expand it. */
5087 buffer
= xrnew (buffer
, lbp
->size
, char);
5088 p
+= buffer
- lbp
->buffer
;
5089 pend
= buffer
+ lbp
->size
;
5090 lbp
->buffer
= buffer
;
5100 if (p
> buffer
&& p
[-1] == '\r')
5104 /* Assume CRLF->LF translation will be performed by Emacs
5105 when loading this file, so CRs won't appear in the buffer.
5106 It would be cleaner to compensate within Emacs;
5107 however, Emacs does not know how many CRs were deleted
5108 before any given point in the file. */
5123 lbp
->len
= p
- buffer
;
5125 return lbp
->len
+ chars_deleted
;
5129 * Like readline_internal, above, but in addition try to match the
5130 * input line against relevant regular expressions.
5133 readline (lbp
, stream
)
5137 /* Read new line. */
5138 long result
= readline_internal (lbp
, stream
);
5139 #ifdef ETAGS_REGEXPS
5143 /* Match against relevant patterns. */
5145 for (pp
= p_head
; pp
!= NULL
; pp
= pp
->p_next
)
5147 /* Only use generic regexps or those for the current language. */
5148 if (pp
->language
!= NULL
&& pp
->language
!= curlang
)
5151 match
= re_match (pp
->pattern
, lbp
->buffer
, lbp
->len
, 0, &pp
->regs
);
5156 if (!pp
->error_signaled
)
5158 error ("error while matching \"%s\"", pp
->regex
);
5159 pp
->error_signaled
= TRUE
;
5166 /* Match occurred. Construct a tag. */
5167 if (pp
->name_pattern
[0] != '\0')
5169 /* Make a named tag. */
5170 char *name
= substitute (lbp
->buffer
,
5171 pp
->name_pattern
, &pp
->regs
);
5173 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
5177 /* Make an unnamed tag. */
5178 pfnote ((char *)NULL
, TRUE
,
5179 lbp
->buffer
, match
, lineno
, linecharno
);
5184 #endif /* ETAGS_REGEXPS */
5190 * Return a pointer to a space of size strlen(cp)+1 allocated
5191 * with xnew where the string CP has been copied.
5197 return savenstr (cp
, strlen (cp
));
5201 * Return a pointer to a space of size LEN+1 allocated with xnew where
5202 * the string CP has been copied for at most the first LEN characters.
5211 dp
= xnew (len
+ 1, char);
5212 strncpy (dp
, cp
, len
);
5218 * Return the ptr in sp at which the character c last
5219 * appears; NULL if not found
5221 * Identical to POSIX strrchr, included for portability.
5224 etags_strrchr (sp
, c
)
5225 register const char *sp
;
5228 register const char *r
;
5241 * Return the ptr in sp at which the character c first
5242 * appears; NULL if not found
5244 * Identical to POSIX strchr, included for portability.
5247 etags_strchr (sp
, c
)
5248 register const char *sp
;
5259 /* Skip spaces, return new pointer. */
5264 while (iswhite (*cp
))
5269 /* Skip non spaces, return new pointer. */
5271 skip_non_spaces (cp
)
5274 while (*cp
!= '\0' && !iswhite (*cp
))
5279 /* Print error message and exit. */
5297 suggest_asking_for_help ()
5299 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
5310 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
5313 const char *s1
, *s2
;
5315 fprintf (stderr
, "%s: ", progname
);
5316 fprintf (stderr
, s1
, s2
);
5317 fprintf (stderr
, "\n");
5320 /* Return a newly-allocated string whose contents
5321 concatenate those of s1, s2, s3. */
5326 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
5327 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
5329 strcpy (result
, s1
);
5330 strcpy (result
+ len1
, s2
);
5331 strcpy (result
+ len1
+ len2
, s3
);
5332 result
[len1
+ len2
+ len3
] = '\0';
5337 /* Does the same work as the system V getcwd, but does not need to
5338 guess the buffer size in advance. */
5344 char *path
= xnew (bufsize
, char);
5346 while (getcwd (path
, bufsize
) == NULL
)
5348 if (errno
!= ERANGE
)
5352 path
= xnew (bufsize
, char);
5355 canonicalize_filename (path
);
5358 #else /* not HAVE_GETCWD */
5360 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
5364 for (p
= path
; *p
!= '\0'; p
++)
5370 return strdup (path
);
5371 #else /* not MSDOS */
5376 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
5377 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
5382 #endif /* not MSDOS */
5383 #endif /* not HAVE_GETCWD */
5386 /* Return a newly allocated string containing the file name of FILE
5387 relative to the absolute directory DIR (which should end with a slash). */
5389 relative_filename (file
, dir
)
5392 char *fp
, *dp
, *afn
, *res
;
5395 /* Find the common root of file and dir (with a trailing slash). */
5396 afn
= absolute_filename (file
, cwd
);
5399 while (*fp
++ == *dp
++)
5401 fp
--, dp
--; /* back to the first differing char */
5403 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
5406 do /* look at the equal chars until '/' */
5410 /* Build a sequence of "../" strings for the resulting relative file name. */
5412 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
5414 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
5417 strcat (res
, "../");
5419 /* Add the file name relative to the common root of file and dir. */
5420 strcat (res
, fp
+ 1);
5426 /* Return a newly allocated string containing the absolute file name
5427 of FILE given DIR (which should end with a slash). */
5429 absolute_filename (file
, dir
)
5432 char *slashp
, *cp
, *res
;
5434 if (filename_is_absolute (file
))
5435 res
= savestr (file
);
5437 /* We don't support non-absolute file names with a drive
5438 letter, like `d:NAME' (it's too much hassle). */
5439 else if (file
[1] == ':')
5440 fatal ("%s: relative file names with drive letters not supported", file
);
5443 res
= concat (dir
, file
, "");
5445 /* Delete the "/dirname/.." and "/." substrings. */
5446 slashp
= etags_strchr (res
, '/');
5447 while (slashp
!= NULL
&& slashp
[0] != '\0')
5449 if (slashp
[1] == '.')
5451 if (slashp
[2] == '.'
5452 && (slashp
[3] == '/' || slashp
[3] == '\0'))
5457 while (cp
>= res
&& !filename_is_absolute (cp
));
5459 cp
= slashp
; /* the absolute name begins with "/.." */
5461 /* Under MSDOS and NT we get `d:/NAME' as absolute
5462 file name, so the luser could say `d:/../NAME'.
5463 We silently treat this as `d:/NAME'. */
5464 else if (cp
[0] != '/')
5467 strcpy (cp
, slashp
+ 3);
5471 else if (slashp
[2] == '/' || slashp
[2] == '\0')
5473 strcpy (slashp
, slashp
+ 2);
5478 slashp
= etags_strchr (slashp
+ 1, '/');
5482 return savestr ("/");
5487 /* Return a newly allocated string containing the absolute
5488 file name of dir where FILE resides given DIR (which should
5489 end with a slash). */
5491 absolute_dirname (file
, dir
)
5497 canonicalize_filename (file
);
5498 slashp
= etags_strrchr (file
, '/');
5500 return savestr (dir
);
5503 res
= absolute_filename (file
, dir
);
5509 /* Whether the argument string is an absolute file name. The argument
5510 string must have been canonicalized with canonicalize_filename. */
5512 filename_is_absolute (fn
)
5515 return (fn
[0] == '/'
5517 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
5522 /* Translate backslashes into slashes. Works in place. */
5524 canonicalize_filename (fn
)
5528 /* Canonicalize drive letter case. */
5529 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
5530 fn
[0] = upcase (fn
[0]);
5531 /* Convert backslashes to slashes. */
5532 for (; *fn
!= '\0'; fn
++)
5537 fn
= NULL
; /* shut up the compiler */
5541 /* Increase the size of a linebuffer. */
5543 grow_linebuffer (lbp
, toksize
)
5547 while (lbp
->size
< toksize
)
5549 lbp
->buffer
= xrnew (lbp
->buffer
, lbp
->size
, char);
5552 /* Like malloc but get fatal error if memory is exhausted. */
5557 long *result
= (long *) malloc (size
);
5559 fatal ("virtual memory exhausted", (char *)NULL
);
5564 xrealloc (ptr
, size
)
5568 long *result
= (long *) realloc (ptr
, size
);
5570 fatal ("virtual memory exhausted", (char *)NULL
);