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.48";
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 names and dot suffixes:");
586 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
588 printf (" %-*s", 10, lang
->name
);
589 if (lang
->filenames
!= NULL
)
590 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
591 printf (" %s", *name
);
592 if (lang
->suffixes
!= NULL
)
593 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
594 printf (" .%s", *ext
);
597 puts ("Where `auto' means use default language for files based on file\n\
598 name suffix, and `none' means only do regexp processing on files.\n\
599 If no language is specified and no matching suffix is found,\n\
600 the first line of the file is read for a sharp-bang (#!) sequence\n\
601 followed by the name of an interpreter. If no such sequence is found,\n\
602 Fortran is tried first; if no tags are found, C is tried next.\n\
603 Compressed files are supported using gzip and bzip2.");
607 # define EMACS_NAME "GNU Emacs"
610 # define VERSION "21"
615 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
616 puts ("Copyright (C) 1999 Free Software Foundation, Inc. and Ken Arnold");
617 puts ("This program is distributed under the same terms as Emacs");
625 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
627 These are the options accepted by %s.\n", progname
, progname
);
629 puts ("You may use unambiguous abbreviations for the long option names.");
631 puts ("Long option names do not work with this executable, as it is not\n\
632 linked with GNU getopt.");
633 #endif /* LONG_OPTIONS */
634 puts ("A - as file name means read names from stdin (one per line).");
636 printf (" Absolute names are stored in the output file as they are.\n\
637 Relative ones are stored relative to the output file's directory.");
640 puts ("-a, --append\n\
641 Append tag entries to existing tags file.");
643 puts ("--packages-only\n\
644 For Ada files, only generate tags for packages .");
647 puts ("-B, --backward-search\n\
648 Write the search commands for the tag entries using '?', the\n\
649 backward-search command instead of '/', the forward-search command.");
652 Treat files whose name suffix defaults to C language as C++ files.");
654 puts ("--declarations\n\
655 In C and derived languages, create tags for function declarations,");
657 puts ("\tand create tags for extern variables if --globals is used.");
660 ("\tand create tags for extern variables unless --no-globals is used.");
663 puts ("-d, --defines\n\
664 Create tag entries for C #define constants and enum constants, too.");
666 puts ("-D, --no-defines\n\
667 Don't create tag entries for C #define constants and enum constants.\n\
668 This makes the tags file smaller.");
672 puts ("-i FILE, --include=FILE\n\
673 Include a note in tag file indicating that, when searching for\n\
674 a tag, one should also consult the tags file FILE after\n\
675 checking the current file.");
676 puts ("-l LANG, --language=LANG\n\
677 Force the following files to be considered as written in the\n\
678 named language up to the next --language=LANG option.");
683 Create tag entries for global variables in some languages.");
685 puts ("--no-globals\n\
686 Do not create tag entries for global variables in some\n\
687 languages. This makes the tags file smaller.");
689 Create tag entries for member variables in C and derived languages.");
692 puts ("-r /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
693 Make a tag for each line matching pattern REGEXP in the following\n\
694 files. {LANGUAGE}/REGEXP/ uses REGEXP for LANGUAGE files only.\n\
695 regexfile is a file containing one REGEXP per line.\n\
696 REGEXP is anchored (as if preceded by ^).\n\
697 The form /REGEXP/NAME/ creates a named tag.\n\
698 For example Tcl named tags can be created with:\n\
699 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
700 puts ("-c /REGEXP/, --ignore-case-regex=/REGEXP/ or --ignore-case-regex=@regexfile\n\
701 Like -r, --regex but ignore case when matching expressions.");
702 puts ("-R, --no-regex\n\
703 Don't create tags from regexps for the following files.");
704 #endif /* ETAGS_REGEXPS */
705 puts ("-o FILE, --output=FILE\n\
706 Write the tags to FILE.");
707 puts ("-I, --ignore-indentation\n\
708 Don't rely on indentation quite as much as normal. Currently,\n\
709 this means not to assume that a closing brace in the first\n\
710 column is the final brace of a function or structure\n\
711 definition in C and C++.");
715 puts ("-t, --typedefs\n\
716 Generate tag entries for C and Ada typedefs.");
717 puts ("-T, --typedefs-and-c++\n\
718 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
719 and C++ member functions.");
720 puts ("-u, --update\n\
721 Update the tag entries for the given files, leaving tag\n\
722 entries for other files in place. Currently, this is\n\
723 implemented by deleting the existing entries for the given\n\
724 files and then rewriting the new entries at the end of the\n\
725 tags file. It is often faster to simply rebuild the entire\n\
726 tag file than to use this.");
727 puts ("-v, --vgrind\n\
728 Generates an index of items intended for human consumption,\n\
729 similar to the output of vgrind. The index is sorted, and\n\
730 gives the page number of each item.");
731 puts ("-w, --no-warn\n\
732 Suppress warning messages about entries defined in multiple\n\
734 puts ("-x, --cxref\n\
735 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
736 The output uses line numbers instead of page numbers, but\n\
737 beyond that the differences are cosmetic; try both to see\n\
741 puts ("-V, --version\n\
742 Print the version of the program.\n\
744 Print this help message.");
746 print_language_names ();
749 puts ("Report bugs to bug-gnu-emacs@gnu.org");
763 /* This structure helps us allow mixing of --lang and file names. */
766 enum argument_type arg_type
;
768 language
*lang
; /* language of the regexp */
771 #ifdef VMS /* VMS specific functions */
775 /* This is a BUG! ANY arbitrary limit is a BUG!
776 Won't someone please fix this? */
777 #define MAX_FILE_SPEC_LEN 255
780 char body
[MAX_FILE_SPEC_LEN
+ 1];
784 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
785 returning in each successive call the next file name matching the input
786 spec. The function expects that each in_spec passed
787 to it will be processed to completion; in particular, up to and
788 including the call following that in which the last matching name
789 is returned, the function ignores the value of in_spec, and will
790 only start processing a new spec with the following call.
791 If an error occurs, on return out_spec contains the value
792 of in_spec when the error occurred.
794 With each successive file name returned in out_spec, the
795 function's return value is one. When there are no more matching
796 names the function returns zero. If on the first call no file
797 matches in_spec, or there is any other error, -1 is returned.
802 #define OUTSIZE MAX_FILE_SPEC_LEN
808 static long context
= 0;
809 static struct dsc$descriptor_s o
;
810 static struct dsc$descriptor_s i
;
811 static bool pass1
= TRUE
;
818 o
.dsc$a_pointer
= (char *) out
;
819 o
.dsc$w_length
= (short)OUTSIZE
;
820 i
.dsc$a_pointer
= in
;
821 i
.dsc$w_length
= (short)strlen(in
);
822 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
823 i
.dsc$b_class
= DSC$K_CLASS_S
;
824 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
825 o
.dsc$b_class
= DSC$K_CLASS_VS
;
827 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
829 out
->body
[out
->curlen
] = EOS
;
832 else if (status
== RMS$_NMF
)
836 strcpy(out
->body
, in
);
839 lib$
find_file_end(&context
);
845 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
846 name of each file specified by the provided arg expanding wildcards.
849 gfnames (arg
, p_error
)
853 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
855 switch (fn_exp (&filename
, arg
))
859 return filename
.body
;
865 return filename
.body
;
869 #ifndef OLD /* Newer versions of VMS do provide `system'. */
873 error ("%s", "system() function not implemented under VMS");
877 #define VERSION_DELIM ';'
878 char *massage_name (s
)
884 if (*s
== VERSION_DELIM
)
902 unsigned int nincluded_files
;
903 char **included_files
;
906 int current_arg
, file_count
;
907 linebuffer filename_lb
;
913 _fmode
= O_BINARY
; /* all of files are treated as binary files */
918 included_files
= xnew (argc
, char *);
922 /* Allocate enough no matter what happens. Overkill, but each one
924 argbuffer
= xnew (argc
, argument
);
927 /* Set syntax for regular expression routines. */
928 re_set_syntax (RE_SYNTAX_EMACS
| RE_INTERVALS
);
929 /* Translation table for case-insensitive search. */
930 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
931 lc_trans
[i
] = lowcase (i
);
932 #endif /* ETAGS_REGEXPS */
935 * If etags, always find typedefs and structure tags. Why not?
936 * Also default is to find macro constants, enum constants and
941 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
952 optstring
= "-aCdDf:Il:o:r:c:RStTi:BuvxwVhH";
954 optstring
= "-aCdDf:Il:o:StTi:BuvxwVhH";
955 #endif /* ETAGS_REGEXPS */
958 optstring
= optstring
+ 1;
959 #endif /* LONG_OPTIONS */
961 opt
= getopt_long (argc
, argv
, optstring
, longopts
, 0);
968 /* If getopt returns 0, then it has already processed a
969 long-named option. We should do nothing. */
973 /* This means that a file name has been seen. Record it. */
974 argbuffer
[current_arg
].arg_type
= at_filename
;
975 argbuffer
[current_arg
].what
= optarg
;
980 /* Common options. */
981 case 'a': append_to_tagfile
= TRUE
; break;
982 case 'C': cplusplus
= TRUE
; break;
983 case 'd': constantypedefs
= TRUE
; break;
984 case 'D': constantypedefs
= FALSE
; break;
985 case 'f': /* for compatibility with old makefiles */
989 error ("-o option may only be given once.", (char *)NULL
);
990 suggest_asking_for_help ();
995 case 'S': /* for backward compatibility */
996 noindentypedefs
= TRUE
;
1000 language
*lang
= get_language_from_langname (optarg
);
1003 argbuffer
[current_arg
].lang
= lang
;
1004 argbuffer
[current_arg
].arg_type
= at_language
;
1009 #ifdef ETAGS_REGEXPS
1011 argbuffer
[current_arg
].arg_type
= at_regexp
;
1012 argbuffer
[current_arg
].what
= optarg
;
1016 argbuffer
[current_arg
].arg_type
= at_regexp
;
1017 argbuffer
[current_arg
].what
= NULL
;
1021 argbuffer
[current_arg
].arg_type
= at_icregexp
;
1022 argbuffer
[current_arg
].what
= optarg
;
1025 #endif /* ETAGS_REGEXPS */
1037 typedefs
= typedefs_and_cplusplus
= TRUE
;
1042 included_files
[nincluded_files
++] = optarg
;
1045 /* Ctags options. */
1046 case 'B': searchar
= '?'; break;
1047 case 'u': update
= TRUE
; break;
1048 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1049 case 'x': cxref_style
= TRUE
; break;
1050 case 'w': no_warnings
= TRUE
; break;
1053 suggest_asking_for_help ();
1057 for (; optind
< argc
; ++optind
)
1059 argbuffer
[current_arg
].arg_type
= at_filename
;
1060 argbuffer
[current_arg
].what
= argv
[optind
];
1065 if (nincluded_files
== 0 && file_count
== 0)
1067 error ("no input files specified.", (char *)NULL
);
1068 suggest_asking_for_help ();
1071 if (tagfile
== NULL
)
1072 tagfile
= CTAGS
? "tags" : "TAGS";
1073 cwd
= etags_getcwd (); /* the current working directory */
1074 if (cwd
[strlen (cwd
) - 1] != '/')
1077 cwd
= concat (oldcwd
, "/", "");
1080 if (streq (tagfile
, "-"))
1083 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1085 init (); /* set up boolean "functions" */
1088 initbuffer (&token_name
);
1089 initbuffer (&lbs
[0].lb
);
1090 initbuffer (&lbs
[1].lb
);
1091 initbuffer (&filename_lb
);
1095 if (streq (tagfile
, "-"))
1099 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1100 doesn't take effect until after `stdout' is already open). */
1101 if (!isatty (fileno (stdout
)))
1102 setmode (fileno (stdout
), O_BINARY
);
1106 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1112 * Loop through files finding functions.
1114 for (i
= 0; i
< current_arg
; ++i
)
1116 switch (argbuffer
[i
].arg_type
)
1119 forced_lang
= argbuffer
[i
].lang
;
1121 #ifdef ETAGS_REGEXPS
1123 analyse_regex (argbuffer
[i
].what
, FALSE
);
1126 analyse_regex (argbuffer
[i
].what
, TRUE
);
1131 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1135 error ("can't find file %s\n", this_file
);
1140 this_file
= massage_name (this_file
);
1143 this_file
= argbuffer
[i
].what
;
1145 /* Input file named "-" means read file names from stdin
1146 (one per line) and use them. */
1147 if (streq (this_file
, "-"))
1148 while (readline_internal (&filename_lb
, stdin
) > 0)
1149 process_file (filename_lb
.buffer
);
1151 process_file (this_file
);
1159 #ifdef ETAGS_REGEXPS
1161 #endif /* ETAGS_REGEXPS */
1165 while (nincluded_files
-- > 0)
1166 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1172 /* If CTAGS, we are here. process_file did not write the tags yet,
1173 because we want them ordered. Let's do it now. */
1185 for (i
= 0; i
< current_arg
; ++i
)
1187 if (argbuffer
[i
].arg_type
!= at_filename
)
1190 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1191 tagfile
, argbuffer
[i
].what
, tagfile
);
1192 if (system (cmd
) != GOOD
)
1193 fatal ("failed to execute shell command", (char *)NULL
);
1195 append_to_tagfile
= TRUE
;
1198 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1209 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
1210 exit (system (cmd
));
1218 * Return a compressor given the file name. If EXTPTR is non-zero,
1219 * return a pointer into FILE where the compressor-specific
1220 * extension begins. If no compressor is found, NULL is returned
1221 * and EXTPTR is not significant.
1222 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca>
1225 get_compressor_from_suffix (file
, extptr
)
1230 char *slash
, *suffix
;
1232 /* This relies on FN to be after canonicalize_filename,
1233 so we don't need to consider backslashes on DOS_NT. */
1234 slash
= etags_strrchr (file
, '/');
1235 suffix
= etags_strrchr (file
, '.');
1236 if (suffix
== NULL
|| suffix
< slash
)
1241 /* Let those poor souls who live with DOS 8+3 file name limits get
1242 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1243 Only the first do loop is run if not MSDOS */
1246 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1247 if (streq (compr
->suffix
, suffix
))
1250 break; /* do it only once: not really a loop */
1253 } while (*suffix
!= '\0');
1260 * Return a language given the name.
1263 get_language_from_langname (name
)
1269 error ("empty language name", (char *)NULL
);
1272 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1273 if (streq (name
, lang
->name
))
1275 error ("unknown language \"%s\"", name
);
1283 * Return a language given the interpreter name.
1286 get_language_from_interpreter (interpreter
)
1292 if (interpreter
== NULL
)
1294 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1295 if (lang
->interpreters
!= NULL
)
1296 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1297 if (streq (*iname
, interpreter
))
1306 * Return a language given the file name.
1309 get_language_from_filename (file
)
1313 char **name
, **ext
, *suffix
;
1315 /* Try whole file name first. */
1316 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1317 if (lang
->filenames
!= NULL
)
1318 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1319 if (streq (*name
, file
))
1322 /* If not found, try suffix after last dot. */
1323 suffix
= etags_strrchr (file
, '.');
1327 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1328 if (lang
->suffixes
!= NULL
)
1329 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1330 if (streq (*ext
, suffix
))
1338 * This routine is called on each file argument.
1344 struct stat stat_buf
;
1347 char *compressed_name
, *uncompressed_name
;
1348 char *ext
, *real_name
;
1351 canonicalize_filename (file
);
1352 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1354 error ("skipping inclusion of %s in self.", file
);
1357 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1359 compressed_name
= NULL
;
1360 real_name
= uncompressed_name
= savestr (file
);
1364 real_name
= compressed_name
= savestr (file
);
1365 uncompressed_name
= savenstr (file
, ext
- file
);
1368 /* If the canonicalised uncompressed name has already be dealt with,
1369 skip it silently, else add it to the list. */
1371 typedef struct processed_file
1374 struct processed_file
*next
;
1376 static processed_file
*pf_head
= NULL
;
1377 register processed_file
*fnp
;
1379 for (fnp
= pf_head
; fnp
!= NULL
; fnp
= fnp
->next
)
1380 if (streq (uncompressed_name
, fnp
->filename
))
1383 pf_head
= xnew (1, struct processed_file
);
1384 pf_head
->filename
= savestr (uncompressed_name
);
1385 pf_head
->next
= fnp
;
1388 if (stat (real_name
, &stat_buf
) != 0)
1390 /* Reset real_name and try with a different name. */
1392 if (compressed_name
!= NULL
) /* try with the given suffix */
1394 if (stat (uncompressed_name
, &stat_buf
) == 0)
1395 real_name
= uncompressed_name
;
1397 else /* try all possible suffixes */
1399 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1401 compressed_name
= concat (file
, ".", compr
->suffix
);
1402 if (stat (compressed_name
, &stat_buf
) != 0)
1406 char *suf
= compressed_name
+ strlen (file
);
1407 size_t suflen
= strlen (compr
->suffix
) + 1;
1408 for ( ; suf
[1]; suf
++, suflen
--)
1410 memmove (suf
, suf
+ 1, suflen
);
1411 if (stat (compressed_name
, &stat_buf
) == 0)
1413 real_name
= compressed_name
;
1417 if (real_name
!= NULL
)
1420 free (compressed_name
);
1421 compressed_name
= NULL
;
1425 real_name
= compressed_name
;
1430 if (real_name
== NULL
)
1435 } /* try with a different name */
1437 if (!S_ISREG (stat_buf
.st_mode
))
1439 error ("skipping %s: it is not a regular file.", real_name
);
1442 if (real_name
== compressed_name
)
1444 char *cmd
= concat (compr
->command
, " ", real_name
);
1445 inf
= popen (cmd
, "r");
1449 inf
= fopen (real_name
, "r");
1456 find_entries (uncompressed_name
, inf
);
1458 if (real_name
== compressed_name
)
1467 if (filename_is_absolute (uncompressed_name
))
1469 /* file is an absolute file name. Canonicalise it. */
1470 filename
= absolute_filename (uncompressed_name
, cwd
);
1474 /* file is a file name relative to cwd. Make it relative
1475 to the directory of the tags file. */
1476 filename
= relative_filename (uncompressed_name
, tagfiledir
);
1478 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1486 if (compressed_name
) free(compressed_name
);
1487 if (uncompressed_name
) free(uncompressed_name
);
1492 * This routine sets up the boolean pseudo-functions which work
1493 * by setting boolean flags dependent upon the corresponding character.
1494 * Every char which is NOT in that string is not a white char. Therefore,
1495 * all of the array "_wht" is set to FALSE, and then the elements
1496 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1497 * of a char is TRUE if it is the string "white", else FALSE.
1505 for (i
= 0; i
< CHARS
; i
++)
1506 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1507 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1508 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1509 notinname('\0') = notinname('\n');
1510 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1511 begtoken('\0') = begtoken('\n');
1512 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1513 intoken('\0') = intoken('\n');
1514 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1515 endtoken('\0') = endtoken('\n');
1519 * This routine opens the specified file and calls the function
1520 * which finds the function and type definitions.
1522 node
*last_node
= NULL
;
1525 find_entries (file
, inf
)
1531 node
*old_last_node
;
1533 /* Memory leakage here: the string pointed by curfile is
1534 never released, because curfile is copied into np->file
1535 for each node, to be used in CTAGS mode. The amount of
1536 memory leaked here is the sum of the lengths of the
1538 curfile
= savestr (file
);
1540 /* If user specified a language, use it. */
1542 if (lang
!= NULL
&& lang
->function
!= NULL
)
1545 lang
->function (inf
);
1549 /* Try to guess the language given the file name. */
1550 lang
= get_language_from_filename (file
);
1551 if (lang
!= NULL
&& lang
->function
!= NULL
)
1554 lang
->function (inf
);
1558 /* Look for sharp-bang as the first two characters. */
1559 if (readline_internal (&lb
, inf
) > 0
1561 && lb
.buffer
[0] == '#'
1562 && lb
.buffer
[1] == '!')
1566 /* Set lp to point at the first char after the last slash in the
1567 line or, if no slashes, at the first nonblank. Then set cp to
1568 the first successive blank and terminate the string. */
1569 lp
= etags_strrchr (lb
.buffer
+2, '/');
1573 lp
= skip_spaces (lb
.buffer
+ 2);
1574 cp
= skip_non_spaces (lp
);
1577 if (strlen (lp
) > 0)
1579 lang
= get_language_from_interpreter (lp
);
1580 if (lang
!= NULL
&& lang
->function
!= NULL
)
1583 lang
->function (inf
);
1588 /* We rewind here, even if inf may be a pipe. We fail if the
1589 length of the first line is longer than the pipe block size,
1590 which is unlikely. */
1594 old_last_node
= last_node
;
1595 curlang
= get_language_from_langname ("fortran");
1596 Fortran_functions (inf
);
1598 /* No Fortran entries found. Try C. */
1599 if (old_last_node
== last_node
)
1601 /* We do not tag if rewind fails.
1602 Only the file name will be recorded in the tags file. */
1604 curlang
= get_language_from_langname (cplusplus
? "c++" : "c");
1605 default_C_entries (inf
);
1612 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1613 char *name
; /* tag name, or NULL if unnamed */
1614 bool is_func
; /* tag is a function */
1615 char *linestart
; /* start of the line where tag is */
1616 int linelen
; /* length of the line where tag is */
1617 int lno
; /* line number */
1618 long cno
; /* character number */
1622 if (CTAGS
&& name
== NULL
)
1625 np
= xnew (1, node
);
1627 /* If ctags mode, change name "main" to M<thisfilename>. */
1628 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1630 register char *fp
= etags_strrchr (curfile
, '/');
1631 np
->name
= concat ("M", fp
== NULL
? curfile
: fp
+ 1, "");
1632 fp
= etags_strrchr (np
->name
, '.');
1633 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
1638 np
->been_warned
= FALSE
;
1640 np
->is_func
= is_func
;
1642 /* Our char numbers are 0-base, because of C language tradition?
1643 ctags compatibility? old versions compatibility? I don't know.
1644 Anyway, since emacs's are 1-base we expect etags.el to take care
1645 of the difference. If we wanted to have 1-based numbers, we would
1646 uncomment the +1 below. */
1647 np
->cno
= cno
/* + 1 */ ;
1648 np
->left
= np
->right
= NULL
;
1649 if (CTAGS
&& !cxref_style
)
1651 if (strlen (linestart
) < 50)
1652 np
->pat
= concat (linestart
, "$", "");
1654 np
->pat
= savenstr (linestart
, 50);
1657 np
->pat
= savenstr (linestart
, linelen
);
1659 add_node (np
, &head
);
1662 /* Date: Wed, 22 Jan 1997 02:56:31 -0500 [last amended 18 Sep 1997]
1663 * From: Sam Kendall <kendall@mv.mv.com>
1664 * Subject: Proposal for firming up the TAGS format specification
1665 * To: F.Potorti@cnuce.cnr.it
1667 * pfnote should emit the optimized form [unnamed tag] only if:
1668 * 1. name does not contain any of the characters " \t\r\n(),;";
1669 * 2. linestart contains name as either a rightmost, or rightmost but
1670 * one character, substring;
1671 * 3. the character, if any, immediately before name in linestart must
1672 * be one of the characters " \t(),;";
1673 * 4. the character, if any, immediately after name in linestart must
1674 * also be one of the characters " \t(),;".
1676 * The real implementation uses the notinname() macro, which recognises
1677 * characters slightly different form " \t\r\n(),;". See the variable
1680 #define traditional_tag_style TRUE
1682 new_pfnote (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1683 char *name
; /* tag name, or NULL if unnamed */
1684 int namelen
; /* tag length */
1685 bool is_func
; /* tag is a function */
1686 char *linestart
; /* start of the line where tag is */
1687 int linelen
; /* length of the line where tag is */
1688 int lno
; /* line number */
1689 long cno
; /* character number */
1697 for (cp
= name
; !notinname (*cp
); cp
++)
1699 if (*cp
== '\0') /* rule #1 */
1701 cp
= linestart
+ linelen
- namelen
;
1702 if (notinname (linestart
[linelen
-1]))
1703 cp
-= 1; /* rule #4 */
1704 if (cp
>= linestart
/* rule #2 */
1706 || notinname (cp
[-1])) /* rule #3 */
1707 && strneq (name
, cp
, namelen
)) /* rule #2 */
1708 named
= FALSE
; /* use unnamed tag */
1713 name
= savenstr (name
, namelen
);
1716 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
1721 * recurse on left children, iterate on right children.
1729 register node
*node_right
= np
->right
;
1730 free_tree (np
->left
);
1731 if (np
->name
!= NULL
)
1741 * Adds a node to the tree of nodes. In etags mode, we don't keep
1742 * it sorted; we just keep a linear list. In ctags mode, maintain
1743 * an ordered tree, with no attempt at balancing.
1745 * add_node is the only function allowed to add nodes, so it can
1749 add_node (np
, cur_node_p
)
1750 node
*np
, **cur_node_p
;
1753 register node
*cur_node
= *cur_node_p
;
1755 if (cur_node
== NULL
)
1765 if (last_node
== NULL
)
1766 fatal ("internal error in add_node", (char *)NULL
);
1767 last_node
->right
= np
;
1773 dif
= strcmp (np
->name
, cur_node
->name
);
1776 * If this tag name matches an existing one, then
1777 * do not add the node, but maybe print a warning.
1781 if (streq (np
->file
, cur_node
->file
))
1785 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1786 np
->file
, lineno
, np
->name
);
1787 fprintf (stderr
, "Second entry ignored\n");
1790 else if (!cur_node
->been_warned
&& !no_warnings
)
1794 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1795 np
->file
, cur_node
->file
, np
->name
);
1796 cur_node
->been_warned
= TRUE
;
1801 /* Actually add the node */
1802 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1815 /* Output subentries that precede this one */
1816 put_entries (np
->left
);
1818 /* Output this entry */
1822 if (np
->name
!= NULL
)
1823 fprintf (tagf
, "%s\177%s\001%d,%ld\n",
1824 np
->pat
, np
->name
, np
->lno
, np
->cno
);
1826 fprintf (tagf
, "%s\177%d,%ld\n",
1827 np
->pat
, np
->lno
, np
->cno
);
1831 if (np
->name
== NULL
)
1832 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
1837 fprintf (stdout
, "%s %s %d\n",
1838 np
->name
, np
->file
, (np
->lno
+ 63) / 64);
1840 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1841 np
->name
, np
->lno
, np
->file
, np
->pat
);
1845 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->file
);
1849 putc (searchar
, tagf
);
1852 for (sp
= np
->pat
; *sp
; sp
++)
1854 if (*sp
== '\\' || *sp
== searchar
)
1858 putc (searchar
, tagf
);
1861 { /* a typedef; text pattern inadequate */
1862 fprintf (tagf
, "%d", np
->lno
);
1868 /* Output subentries that follow this one */
1869 put_entries (np
->right
);
1872 /* Length of a number's decimal representation. */
1878 while ((num
/= 10) > 0)
1884 * Return total number of characters that put_entries will output for
1885 * the nodes in the subtree of the specified node. Works only if
1886 * we are not ctags, but called only in that case. This count
1887 * is irrelevant with the new tags.el, but is still supplied for
1888 * backward compatibility.
1891 total_size_of_entries (np
)
1899 for (total
= 0; np
!= NULL
; np
= np
->right
)
1901 /* Count left subentries. */
1902 total
+= total_size_of_entries (np
->left
);
1904 /* Count this entry */
1905 total
+= strlen (np
->pat
) + 1;
1906 total
+= number_len ((long) np
->lno
) + 1 + number_len (np
->cno
) + 1;
1907 if (np
->name
!= NULL
)
1908 total
+= 1 + strlen (np
->name
); /* \001name */
1915 * The C symbol tables.
1920 st_C_objprot
, st_C_objimpl
, st_C_objend
,
1925 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1928 static unsigned int hash
P_((const char *, unsigned int));
1929 static struct C_stab_entry
* in_word_set
P_((const char *, unsigned int));
1930 static enum sym_type C_symtype
P_((char *, int, int));
1932 /* Feed stuff between (but not including) %[ and %] lines to:
1933 gperf -c -k 1,3 -o -p -r -t
1935 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1939 while, 0, st_C_ignore
1940 switch, 0, st_C_ignore
1941 return, 0, st_C_ignore
1942 @interface, 0, st_C_objprot
1943 @protocol, 0, st_C_objprot
1944 @implementation,0, st_C_objimpl
1945 @end, 0, st_C_objend
1946 import, C_JAVA, st_C_ignore
1947 package, C_JAVA, st_C_ignore
1948 friend, C_PLPL, st_C_ignore
1949 extends, C_JAVA, st_C_javastruct
1950 implements, C_JAVA, st_C_javastruct
1951 interface, C_JAVA, st_C_struct
1952 class, C_PLPL, st_C_struct
1953 namespace, C_PLPL, st_C_struct
1954 domain, C_STAR, st_C_struct
1955 union, 0, st_C_struct
1956 struct, 0, st_C_struct
1957 extern, 0, st_C_extern
1959 typedef, 0, st_C_typedef
1960 define, 0, st_C_define
1961 operator, C_PLPL, st_C_operator
1962 bool, C_PLPL, st_C_typespec
1963 long, 0, st_C_typespec
1964 short, 0, st_C_typespec
1965 int, 0, st_C_typespec
1966 char, 0, st_C_typespec
1967 float, 0, st_C_typespec
1968 double, 0, st_C_typespec
1969 signed, 0, st_C_typespec
1970 unsigned, 0, st_C_typespec
1971 auto, 0, st_C_typespec
1972 void, 0, st_C_typespec
1973 static, 0, st_C_typespec
1974 const, 0, st_C_typespec
1975 volatile, 0, st_C_typespec
1976 explicit, C_PLPL, st_C_typespec
1977 mutable, C_PLPL, st_C_typespec
1978 typename, C_PLPL, st_C_typespec
1979 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1980 DEFUN, 0, st_C_gnumacro
1981 SYSCALL, 0, st_C_gnumacro
1982 ENTRY, 0, st_C_gnumacro
1983 PSEUDO, 0, st_C_gnumacro
1984 # These are defined inside C functions, so currently they are not met.
1985 # EXFUN used in glibc, DEFVAR_* in emacs.
1986 #EXFUN, 0, st_C_gnumacro
1987 #DEFVAR_, 0, st_C_gnumacro
1989 and replace lines between %< and %> with its output. */
1991 /* C code produced by gperf version 2.7.1 (19981006 egcs) */
1992 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
1993 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1995 #define TOTAL_KEYWORDS 46
1996 #define MIN_WORD_LENGTH 2
1997 #define MAX_WORD_LENGTH 15
1998 #define MIN_HASH_VALUE 13
1999 #define MAX_HASH_VALUE 123
2000 /* maximum key range = 111, duplicates = 0 */
2007 register const char *str
;
2008 register unsigned int len
;
2010 static unsigned char asso_values
[] =
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, 124, 124, 124, 124, 124, 124,
2016 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2017 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2018 124, 124, 124, 124, 3, 124, 124, 124, 43, 6,
2019 11, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2020 11, 124, 124, 58, 7, 124, 124, 124, 124, 124,
2021 124, 124, 124, 124, 124, 124, 124, 57, 7, 42,
2022 4, 14, 52, 0, 124, 53, 124, 124, 29, 11,
2023 6, 35, 32, 124, 29, 34, 59, 58, 51, 24,
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, 124, 124, 124, 124,
2035 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2036 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2037 124, 124, 124, 124, 124, 124
2039 register int hval
= len
;
2045 hval
+= asso_values
[(unsigned char)str
[2]];
2048 hval
+= asso_values
[(unsigned char)str
[0]];
2057 static struct C_stab_entry
*
2058 in_word_set (str
, len
)
2059 register const char *str
;
2060 register unsigned int len
;
2062 static struct C_stab_entry wordlist
[] =
2064 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2065 {""}, {""}, {""}, {""},
2066 {"@end", 0, st_C_objend
},
2067 {""}, {""}, {""}, {""},
2068 {"ENTRY", 0, st_C_gnumacro
},
2069 {"@interface", 0, st_C_objprot
},
2071 {"domain", C_STAR
, st_C_struct
},
2073 {"PSEUDO", 0, st_C_gnumacro
},
2075 {"namespace", C_PLPL
, st_C_struct
},
2077 {"@implementation",0, st_C_objimpl
},
2078 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2079 {"long", 0, st_C_typespec
},
2080 {"signed", 0, st_C_typespec
},
2081 {"@protocol", 0, st_C_objprot
},
2082 {""}, {""}, {""}, {""},
2083 {"bool", C_PLPL
, st_C_typespec
},
2084 {""}, {""}, {""}, {""}, {""}, {""},
2085 {"const", 0, st_C_typespec
},
2086 {"explicit", C_PLPL
, st_C_typespec
},
2087 {"if", 0, st_C_ignore
},
2089 {"operator", C_PLPL
, st_C_operator
},
2091 {"DEFUN", 0, st_C_gnumacro
},
2093 {"define", 0, st_C_define
},
2094 {""}, {""}, {""}, {""}, {""},
2095 {"double", 0, st_C_typespec
},
2096 {"struct", 0, st_C_struct
},
2097 {""}, {""}, {""}, {""},
2098 {"short", 0, st_C_typespec
},
2100 {"enum", 0, st_C_enum
},
2101 {"mutable", C_PLPL
, st_C_typespec
},
2103 {"extern", 0, st_C_extern
},
2104 {"extends", C_JAVA
, st_C_javastruct
},
2105 {"package", C_JAVA
, st_C_ignore
},
2106 {"while", 0, st_C_ignore
},
2108 {"for", 0, st_C_ignore
},
2110 {"volatile", 0, st_C_typespec
},
2112 {"import", C_JAVA
, st_C_ignore
},
2113 {"float", 0, st_C_typespec
},
2114 {"switch", 0, st_C_ignore
},
2115 {"return", 0, st_C_ignore
},
2116 {"implements", C_JAVA
, st_C_javastruct
},
2118 {"static", 0, st_C_typespec
},
2119 {"typedef", 0, st_C_typedef
},
2120 {"typename", C_PLPL
, st_C_typespec
},
2121 {"unsigned", 0, st_C_typespec
},
2123 {"char", 0, st_C_typespec
},
2124 {"class", C_PLPL
, st_C_struct
},
2126 {"void", 0, st_C_typespec
},
2128 {"friend", C_PLPL
, st_C_ignore
},
2130 {"int", 0, st_C_typespec
},
2131 {"union", 0, st_C_struct
},
2133 {"auto", 0, st_C_typespec
},
2134 {"interface", C_JAVA
, st_C_struct
},
2136 {"SYSCALL", 0, st_C_gnumacro
}
2139 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2141 register int key
= hash (str
, len
);
2143 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2145 register const char *s
= wordlist
[key
].name
;
2147 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
2148 return &wordlist
[key
];
2155 static enum sym_type
2156 C_symtype (str
, len
, c_ext
)
2161 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2163 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2169 * C functions and variables are recognized using a simple
2170 * finite automaton. fvdef is its state variable.
2174 fvnone
, /* nothing seen */
2175 foperator
, /* func: operator keyword seen (cplpl) */
2176 fvnameseen
, /* function or variable name seen */
2177 fstartlist
, /* func: just after open parenthesis */
2178 finlist
, /* func: in parameter list */
2179 flistseen
, /* func: after parameter list */
2180 fignore
, /* func: before open brace */
2181 vignore
/* var-like: ignore until ';' */
2184 bool fvextern
; /* func or var: extern keyword seen; */
2187 * typedefs are recognized using a simple finite automaton.
2188 * typdef is its state variable.
2192 tnone
, /* nothing seen */
2193 tkeyseen
, /* typedef keyword seen */
2194 ttypeseen
, /* defined type seen */
2195 tinbody
, /* inside typedef body */
2196 tend
, /* just before typedef tag */
2197 tignore
/* junk after typedef tag */
2202 * struct-like structures (enum, struct and union) are recognized
2203 * using another simple finite automaton. `structdef' is its state
2208 snone
, /* nothing seen yet */
2209 skeyseen
, /* struct-like keyword seen */
2210 stagseen
, /* struct-like tag seen */
2211 scolonseen
, /* colon seen after struct-like tag */
2212 sinbody
/* in struct body: recognize member func defs*/
2216 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
2217 * struct tag, and structtype is the type of the preceding struct-like
2220 char *structtag
= "<uninited>";
2221 enum sym_type structtype
;
2224 * When objdef is different from onone, objtag is the name of the class.
2226 char *objtag
= "<uninited>";
2229 * Yet another little state machine to deal with preprocessor lines.
2233 dnone
, /* nothing seen */
2234 dsharpseen
, /* '#' seen as first char on line */
2235 ddefineseen
, /* '#' and 'define' seen */
2236 dignorerest
/* ignore rest of line */
2240 * State machine for Objective C protocols and implementations.
2241 * Tom R.Hageman <tom@basil.icce.rug.nl>
2245 onone
, /* nothing seen */
2246 oprotocol
, /* @interface or @protocol seen */
2247 oimplementation
, /* @implementations seen */
2248 otagseen
, /* class name seen */
2249 oparenseen
, /* parenthesis before category seen */
2250 ocatseen
, /* category name seen */
2251 oinbody
, /* in @implementation body */
2252 omethodsign
, /* in @implementation body, after +/- */
2253 omethodtag
, /* after method name */
2254 omethodcolon
, /* after method colon */
2255 omethodparm
, /* after method parameter */
2256 oignore
/* wait for @end */
2261 * Use this structure to keep info about the token read, and how it
2262 * should be tagged. Used by the make_C_tag function to build a tag.
2275 token tok
; /* latest token read */
2278 * Set this to TRUE, and the next token considered is called a function.
2279 * Used only for GNU emacs's function-defining macros.
2281 bool next_token_is_func
;
2284 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
2289 * methodlen is the length of the method name stored in token_name.
2293 static bool consider_token
P_((char *, int, int, int, int, int, bool *));
2294 static void make_C_tag
P_((bool));
2298 * checks to see if the current token is at the start of a
2299 * function or variable, or corresponds to a typedef, or
2300 * is a struct/union/enum tag, or #define, or an enum constant.
2302 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2303 * with args. C_EXT is which language we are looking at.
2311 * next_token_is_func IN OUT
2315 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func_or_var
)
2316 register char *str
; /* IN: token pointer */
2317 register int len
; /* IN: token length */
2318 register int c
; /* IN: first char after the token */
2319 int c_ext
; /* IN: C extensions mask */
2320 int cblev
; /* IN: curly brace level */
2321 int parlev
; /* IN: parenthesis level */
2322 bool *is_func_or_var
; /* OUT: function or variable found */
2324 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
2327 * Advance the definedef state machine.
2332 /* We're not on a preprocessor line. */
2335 if (toktype
== st_C_define
)
2337 definedef
= ddefineseen
;
2341 definedef
= dignorerest
;
2346 * Make a tag for any macro, unless it is a constant
2347 * and constantypedefs is FALSE.
2349 definedef
= dignorerest
;
2350 *is_func_or_var
= (c
== '(');
2351 if (!*is_func_or_var
&& !constantypedefs
)
2358 error ("internal error: definedef value.", (char *)NULL
);
2367 if (toktype
== st_C_typedef
)
2386 /* Do not return here, so the structdef stuff has a chance. */
2400 * This structdef business is currently only invoked when cblev==0.
2401 * It should be recursively invoked whatever the curly brace level,
2402 * and a stack of states kept, to allow for definitions of structs
2405 * This structdef business is NOT invoked when we are ctags and the
2406 * file is plain C. This is because a struct tag may have the same
2407 * name as another tag, and this loses with ctags.
2411 case st_C_javastruct
:
2412 if (structdef
== stagseen
)
2413 structdef
= scolonseen
;
2417 if (typdef
== tkeyseen
2418 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
2420 structdef
= skeyseen
;
2421 structtype
= toktype
;
2426 if (structdef
== skeyseen
)
2428 /* Save the tag for struct/union/class, for functions and variables
2429 that may be defined inside. */
2430 if (structtype
== st_C_struct
)
2431 structtag
= savenstr (str
, len
);
2433 structtag
= "<enum>";
2434 structdef
= stagseen
;
2438 if (typdef
!= tnone
)
2441 /* Detect GNU macros.
2443 Writers of emacs code are recommended to put the
2444 first two args of a DEFUN on the same line.
2446 The DEFUN macro, used in emacs C source code, has a first arg
2447 that is a string (the lisp function name), and a second arg that
2448 is a C function name. Since etags skips strings, the second arg
2449 is tagged. This is unfortunate, as it would be better to tag the
2450 first arg. The simplest way to deal with this problem would be
2451 to name the tag with a name built from the function name, by
2452 removing the initial 'F' character and substituting '-' for '_'.
2453 Anyway, this assumes that the conventions of naming lisp
2454 functions will never change. Currently, this method is not
2456 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
2458 next_token_is_func
= TRUE
;
2461 if (next_token_is_func
)
2463 next_token_is_func
= FALSE
;
2465 *is_func_or_var
= TRUE
;
2469 /* Detect Objective C constructs. */
2479 objdef
= oimplementation
;
2483 case oimplementation
:
2484 /* Save the class tag for functions or variables defined inside. */
2485 objtag
= savenstr (str
, len
);
2489 /* Save the class tag for categories. */
2490 objtag
= savenstr (str
, len
);
2492 *is_func_or_var
= TRUE
;
2496 *is_func_or_var
= TRUE
;
2503 objdef
= omethodtag
;
2505 grow_linebuffer (&token_name
, methodlen
+ 1);
2506 strncpy (token_name
.buffer
, str
, len
);
2507 token_name
.buffer
[methodlen
] = '\0';
2508 token_name
.len
= methodlen
;
2514 objdef
= omethodparm
;
2519 objdef
= omethodtag
;
2521 grow_linebuffer (&token_name
, methodlen
+ 1);
2522 strncat (token_name
.buffer
, str
, len
);
2523 token_name
.len
= methodlen
;
2528 if (toktype
== st_C_objend
)
2530 /* Memory leakage here: the string pointed by objtag is
2531 never released, because many tests would be needed to
2532 avoid breaking on incorrect input code. The amount of
2533 memory leaked here is the sum of the lengths of the
2541 /* A function, variable or enum constant? */
2548 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!= vignore
)
2549 fvdef
= fvnone
; /* should be useless */
2557 *is_func_or_var
= TRUE
;
2560 if ((c_ext
& C_PLPL
) && strneq (str
+len
-10, "::operator", 10))
2563 *is_func_or_var
= TRUE
;
2566 if (constantypedefs
&& structdef
== sinbody
&& structtype
== st_C_enum
)
2568 if (fvdef
== fvnone
)
2570 fvdef
= fvnameseen
; /* function or variable */
2571 *is_func_or_var
= TRUE
;
2582 * This routine finds functions, variables, typedefs,
2583 * #define's, enum constants and struct/union/enum definitions in
2584 * C syntax and adds them to the list.
2586 #define current_lb_is_new (newndx == curndx)
2587 #define switch_line_buffers() (curndx = 1 - curndx)
2589 #define curlb (lbs[curndx].lb)
2590 #define othlb (lbs[1-curndx].lb)
2591 #define newlb (lbs[newndx].lb)
2592 #define curlinepos (lbs[curndx].linepos)
2593 #define othlinepos (lbs[1-curndx].linepos)
2594 #define newlinepos (lbs[newndx].linepos)
2596 #define CNL_SAVE_DEFINEDEF() \
2598 curlinepos = charno; \
2600 linecharno = charno; \
2601 charno += readline (&curlb, inf); \
2602 lp = curlb.buffer; \
2609 CNL_SAVE_DEFINEDEF(); \
2610 if (savetok.valid) \
2613 savetok.valid = FALSE; \
2615 definedef = dnone; \
2623 /* This function should never be called when tok.valid is FALSE, but
2624 we must protect against invalid input or internal errors. */
2627 if (traditional_tag_style
)
2629 /* This was the original code. Now we call new_pfnote instead,
2630 which uses the new method for naming tags (see new_pfnote). */
2633 if (CTAGS
|| tok
.named
)
2634 name
= savestr (token_name
.buffer
);
2635 pfnote (name
, isfun
,
2636 tok
.buffer
, tok
.linelen
, tok
.lineno
, tok
.linepos
);
2639 new_pfnote (token_name
.buffer
, token_name
.len
, isfun
,
2640 tok
.buffer
, tok
.linelen
, tok
.lineno
, tok
.linepos
);
2649 C_entries (c_ext
, inf
)
2650 int c_ext
; /* extension of C */
2651 FILE *inf
; /* input file */
2653 register char c
; /* latest char read; '\0' for end of line */
2654 register char *lp
; /* pointer one beyond the character `c' */
2655 int curndx
, newndx
; /* indices for current and new lb */
2656 register int tokoff
; /* offset in line of start of current token */
2657 register int toklen
; /* length of current token */
2658 char *qualifier
; /* string used to qualify names */
2659 int qlen
; /* length of qualifier */
2660 int cblev
; /* current curly brace level */
2661 int parlev
; /* current parenthesis level */
2662 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
2663 bool purec
, cplpl
, cjava
;
2664 token savetok
; /* token saved during preprocessor handling */
2667 tokoff
= toklen
= 0; /* keep compiler quiet */
2668 curndx
= newndx
= 0;
2674 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
2675 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
2676 next_token_is_func
= yacc_rules
= FALSE
;
2677 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2678 tok
.valid
= savetok
.valid
= FALSE
;
2681 purec
= !(c_ext
& ~YACC
); /* no extensions (apart from possibly yacc) */
2682 cplpl
= (c_ext
& C_PLPL
) == C_PLPL
;
2683 cjava
= (c_ext
& C_JAVA
) == C_JAVA
;
2685 { qualifier
= "."; qlen
= 1; }
2687 { qualifier
= "::"; qlen
= 2; }
2694 /* If we're at the end of the line, the next character is a
2695 '\0'; don't skip it, because it's the thing that tells us
2696 to read the next line. */
2717 /* Newlines inside comments do not end macro definitions in
2719 CNL_SAVE_DEFINEDEF ();
2732 /* Newlines inside strings do not end macro definitions
2733 in traditional cpp, even though compilers don't
2734 usually accept them. */
2735 CNL_SAVE_DEFINEDEF ();
2745 /* Hmmm, something went wrong. */
2759 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2767 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2780 else if (/* cplpl && */ *lp
== '/')
2788 if ((c_ext
& YACC
) && *lp
== '%')
2790 /* entering or exiting rules section in yacc file */
2792 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
2793 typdef
= tnone
; structdef
= snone
;
2794 next_token_is_func
= FALSE
;
2795 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2797 yacc_rules
= !yacc_rules
;
2803 if (definedef
== dnone
)
2806 bool cpptoken
= TRUE
;
2808 /* Look back on this line. If all blanks, or nonblanks
2809 followed by an end of comment, this is a preprocessor
2811 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2814 if (*cp
== '*' && *(cp
+1) == '/')
2823 definedef
= dsharpseen
;
2824 } /* if (definedef == dnone) */
2830 /* Consider token only if some complicated conditions are satisfied. */
2831 if ((definedef
!= dnone
2832 || (cblev
== 0 && structdef
!= scolonseen
)
2833 || (cblev
== 1 && cplpl
&& structdef
== sinbody
)
2834 || (structdef
== sinbody
&& purec
))
2835 && typdef
!= tignore
2836 && definedef
!= dignorerest
2837 && fvdef
!= finlist
)
2843 bool funorvar
= FALSE
;
2845 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken (lp
[1]))
2848 * This handles :: in the middle, but not at the
2849 * beginning of an identifier. Also, space-separated
2850 * :: is not recognised.
2860 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2861 c_ext
, cblev
, parlev
, &funorvar
))
2863 if (fvdef
== foperator
)
2866 lp
= skip_spaces (lp
-1);
2870 && !iswhite (*lp
) && *lp
!= '(')
2873 toklen
+= lp
- oldlp
;
2878 && definedef
== dnone
2879 && structdef
== sinbody
)
2880 /* function or var defined in C++ class body */
2882 int len
= strlen (structtag
) + qlen
+ toklen
;
2883 grow_linebuffer (&token_name
, len
+ 1);
2884 strcpy (token_name
.buffer
, structtag
);
2885 strcat (token_name
.buffer
, qualifier
);
2886 strncat (token_name
.buffer
,
2887 newlb
.buffer
+ tokoff
, toklen
);
2888 token_name
.len
= len
;
2891 else if (objdef
== ocatseen
)
2892 /* Objective C category */
2894 int len
= strlen (objtag
) + 2 + toklen
;
2895 grow_linebuffer (&token_name
, len
+ 1);
2896 strcpy (token_name
.buffer
, objtag
);
2897 strcat (token_name
.buffer
, "(");
2898 strncat (token_name
.buffer
,
2899 newlb
.buffer
+ tokoff
, toklen
);
2900 strcat (token_name
.buffer
, ")");
2901 token_name
.len
= len
;
2904 else if (objdef
== omethodtag
2905 || objdef
== omethodparm
)
2906 /* Objective C method */
2912 grow_linebuffer (&token_name
, toklen
+ 1);
2913 strncpy (token_name
.buffer
,
2914 newlb
.buffer
+ tokoff
, toklen
);
2915 token_name
.buffer
[toklen
] = '\0';
2916 token_name
.len
= toklen
;
2917 /* Name macros and members. */
2918 tok
.named
= (structdef
== stagseen
2919 || typdef
== ttypeseen
2922 && definedef
== dignorerest
)
2924 && definedef
== dnone
2925 && structdef
== sinbody
));
2927 tok
.lineno
= lineno
;
2928 tok
.linelen
= tokoff
+ toklen
+ 1;
2929 tok
.buffer
= newlb
.buffer
;
2930 tok
.linepos
= newlinepos
;
2933 if (definedef
== dnone
2934 && (fvdef
== fvnameseen
2935 || fvdef
== foperator
2936 || structdef
== stagseen
2938 || objdef
!= onone
))
2940 if (current_lb_is_new
)
2941 switch_line_buffers ();
2944 make_C_tag (funorvar
);
2948 } /* if (endtoken (c)) */
2949 else if (intoken (c
))
2955 } /* if (midtoken) */
2956 else if (begtoken (c
))
2967 make_C_tag (TRUE
); /* a function */
2974 if (structdef
== stagseen
&& !cjava
)
2980 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2982 tokoff
= lp
- 1 - newlb
.buffer
;
2987 } /* if (begtoken) */
2988 } /* if must look at token */
2991 /* Detect end of line, colon, comma, semicolon and various braces
2992 after having handled a token.*/
2996 if (definedef
!= dnone
)
3002 make_C_tag (TRUE
); /* an Objective C class */
3006 objdef
= omethodcolon
;
3008 grow_linebuffer (&token_name
, methodlen
+ 1);
3009 strcat (token_name
.buffer
, ":");
3010 token_name
.len
= methodlen
;
3013 if (structdef
== stagseen
)
3014 structdef
= scolonseen
;
3021 make_C_tag (FALSE
); /* a yacc function */
3032 if (definedef
!= dnone
)
3038 make_C_tag (FALSE
); /* a typedef */
3048 if ((members
&& cblev
== 1)
3049 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3050 make_C_tag (FALSE
); /* a variable */
3056 if (declarations
&& (cblev
== 0 || cblev
== 1))
3057 make_C_tag (TRUE
); /* a function declaration */
3062 /* The following instruction invalidates the token.
3063 Probably the token should be invalidated in all
3064 other cases where some state machine is reset. */
3067 if (structdef
== stagseen
)
3071 if (definedef
!= dnone
)
3077 make_C_tag (TRUE
); /* an Objective C method */
3089 if ((members
&& cblev
== 1)
3090 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3091 make_C_tag (FALSE
); /* a variable */
3096 if (structdef
== stagseen
)
3100 if (definedef
!= dnone
)
3102 if (cblev
== 0 && typdef
== tend
)
3105 make_C_tag (FALSE
); /* a typedef */
3116 if ((members
&& cblev
== 1)
3117 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3118 make_C_tag (FALSE
); /* a variable */
3123 if (structdef
== stagseen
)
3127 if (definedef
!= dnone
)
3129 if (objdef
== otagseen
&& parlev
== 0)
3130 objdef
= oparenseen
;
3134 if (typdef
== ttypeseen
3137 && structdef
!= sinbody
)
3139 /* This handles constructs like:
3140 typedef void OperatorFun (int fun); */
3155 if (definedef
!= dnone
)
3157 if (objdef
== ocatseen
&& parlev
== 1)
3159 make_C_tag (TRUE
); /* an Objective C category */
3171 if (cblev
== 0 && (typdef
== tend
))
3174 make_C_tag (FALSE
); /* a typedef */
3177 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3181 if (definedef
!= dnone
)
3183 if (typdef
== ttypeseen
)
3187 case skeyseen
: /* unnamed struct */
3188 structdef
= sinbody
;
3189 structtag
= "_anonymous_";
3192 case scolonseen
: /* named struct */
3193 structdef
= sinbody
;
3194 make_C_tag (FALSE
); /* a struct */
3200 make_C_tag (TRUE
); /* a function */
3209 make_C_tag (TRUE
); /* an Objective C class */
3214 make_C_tag (TRUE
); /* an Objective C method */
3218 /* Neutralize `extern "C" {' grot. */
3219 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
3226 if (definedef
!= dnone
)
3228 if (fvdef
== fstartlist
)
3229 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3232 if (definedef
!= dnone
)
3234 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
3236 cblev
= 0; /* reset curly brace level if first column */
3237 parlev
= 0; /* also reset paren level, just in case... */
3243 if (typdef
== tinbody
)
3245 /* Memory leakage here: the string pointed by structtag is
3246 never released, because I fear to miss something and
3247 break things while freeing the area. The amount of
3248 memory leaked here is the sum of the lengths of the
3250 if (structdef == sinbody)
3251 free (structtag); */
3254 structtag
= "<error>";
3258 if (definedef
!= dnone
)
3268 if ((members
&& cblev
== 1)
3269 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3270 make_C_tag (FALSE
); /* a variable */
3278 if (objdef
== oinbody
&& cblev
== 0)
3280 objdef
= omethodsign
;
3284 case '#': case '~': case '&': case '%': case '/': case '|':
3285 case '^': case '!': case '<': case '>': case '.': case '?': case ']':
3286 if (definedef
!= dnone
)
3288 /* These surely cannot follow a function tag in C. */
3301 if (objdef
== otagseen
)
3303 make_C_tag (TRUE
); /* an Objective C class */
3306 /* If a macro spans multiple lines don't reset its state. */
3308 CNL_SAVE_DEFINEDEF ();
3314 } /* while not eof */
3318 * Process either a C++ file or a C file depending on the setting
3322 default_C_entries (inf
)
3325 C_entries (cplusplus
? C_PLPL
: 0, inf
);
3328 /* Always do plain ANSI C. */
3330 plain_C_entries (inf
)
3336 /* Always do C++. */
3338 Cplusplus_entries (inf
)
3341 C_entries (C_PLPL
, inf
);
3344 /* Always do Java. */
3349 C_entries (C_JAVA
, inf
);
3357 C_entries (C_STAR
, inf
);
3360 /* Always do Yacc. */
3365 C_entries (YACC
, inf
);
3368 /* A useful macro. */
3369 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3370 for (lineno = charno = 0; /* loop initialization */ \
3371 !feof (file_pointer) /* loop test */ \
3372 && (lineno++, /* instructions at start of loop */ \
3373 linecharno = charno, \
3374 charno += readline (&line_buffer, file_pointer), \
3375 char_pointer = lb.buffer, \
3381 * Read a file, but do no processing. This is used to do regexp
3382 * matching on files that have no language defined.
3385 just_read_file (inf
)
3388 register char *dummy
;
3390 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
3394 /* Fortran parsing */
3396 static bool tail
P_((char *));
3397 static void takeprec
P_((void));
3398 static void getit
P_((FILE *));
3404 register int len
= 0;
3406 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
3408 if (*cp
== '\0' && !intoken (dbp
[len
]))
3419 dbp
= skip_spaces (dbp
);
3423 dbp
= skip_spaces (dbp
);
3424 if (strneq (dbp
, "(*)", 3))
3429 if (!ISDIGIT (*dbp
))
3431 --dbp
; /* force failure */
3436 while (ISDIGIT (*dbp
));
3445 dbp
= skip_spaces (dbp
);
3449 linecharno
= charno
;
3450 charno
+= readline (&lb
, inf
);
3455 dbp
= skip_spaces (dbp
);
3457 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
3459 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
3461 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
3462 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3467 Fortran_functions (inf
)
3470 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3473 dbp
++; /* Ratfor escape to fortran */
3474 dbp
= skip_spaces (dbp
);
3477 switch (lowcase (*dbp
))
3480 if (tail ("integer"))
3488 if (tail ("logical"))
3492 if (tail ("complex") || tail ("character"))
3496 if (tail ("double"))
3498 dbp
= skip_spaces (dbp
);
3501 if (tail ("precision"))
3507 dbp
= skip_spaces (dbp
);
3510 switch (lowcase (*dbp
))
3513 if (tail ("function"))
3517 if (tail ("subroutine"))
3525 if (tail ("blockdata") || tail ("block data"))
3527 dbp
= skip_spaces (dbp
);
3528 if (*dbp
== '\0') /* assume un-named */
3529 pfnote (savestr ("blockdata"), TRUE
,
3530 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
3532 getit (inf
); /* look for name */
3540 * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be>, 1998-04-24
3544 static void adagetit
P_((FILE *, char *));
3546 /* Once we are positioned after an "interesting" keyword, let's get
3547 the real tag value necessary. */
3549 adagetit (inf
, name_qualifier
)
3551 char *name_qualifier
;
3559 dbp
= skip_spaces (dbp
);
3561 || (dbp
[0] == '-' && dbp
[1] == '-'))
3564 linecharno
= charno
;
3565 charno
+= readline (&lb
, inf
);
3574 /* Skipping body of procedure body or package body or ....
3575 resetting qualifier to body instead of spec. */
3576 name_qualifier
= "/b";
3582 /* Skipping type of task type or protected type ... */
3590 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
3595 dbp
= skip_spaces (dbp
);
3598 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
3606 name
= concat (dbp
, name_qualifier
, "");
3608 pfnote (name
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3619 bool inquote
= FALSE
;
3621 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3623 while (*dbp
!= '\0')
3625 /* Skip a string i.e. "abcd". */
3626 if (inquote
|| (*dbp
== '"'))
3628 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
3633 continue; /* advance char */
3638 break; /* advance line */
3642 /* Skip comments. */
3643 if (dbp
[0] == '-' && dbp
[1] == '-')
3644 break; /* advance line */
3646 /* Skip character enclosed in single quote i.e. 'a'
3647 and skip single quote starting an attribute i.e. 'Image. */
3656 /* Search for beginning of a token. */
3657 if (!begtoken (*dbp
))
3660 continue; /* advance char */
3663 /* We are at the beginning of a token. */
3668 if (!packages_only
&& tail ("function"))
3669 adagetit (inf
, "/f");
3671 break; /* from switch */
3672 continue; /* advance char */
3675 if (!packages_only
&& tail ("procedure"))
3676 adagetit (inf
, "/p");
3677 else if (tail ("package"))
3678 adagetit (inf
, "/s");
3679 else if (tail ("protected")) /* protected type */
3680 adagetit (inf
, "/t");
3682 break; /* from switch */
3683 continue; /* advance char */
3686 if (!packages_only
&& tail ("task"))
3687 adagetit (inf
, "/k");
3688 else if (typedefs
&& !packages_only
&& tail ("type"))
3690 adagetit (inf
, "/t");
3691 while (*dbp
!= '\0')
3695 break; /* from switch */
3696 continue; /* advance char */
3699 /* Look for the end of the token. */
3700 while (!endtoken (*dbp
))
3703 } /* advance char */
3704 } /* advance line */
3708 * Bob Weiner, Motorola Inc., 4/3/94
3709 * Unix and microcontroller assembly tag handling
3710 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
3718 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3720 /* If first char is alphabetic or one of [_.$], test for colon
3721 following identifier. */
3722 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3724 /* Read past label. */
3726 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3728 if (*cp
== ':' || iswhite (*cp
))
3730 /* Found end of label, so copy it and add it to the table. */
3731 pfnote (savenstr(lb
.buffer
, cp
-lb
.buffer
), TRUE
,
3732 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3739 * Perl support by Bart Robinson <lomew@cs.utah.edu>
3740 * enhanced by Michael Ernst <mernst@alum.mit.edu>
3741 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
3742 * Perl variable names: /^(my|local).../
3745 Perl_functions (inf
)
3750 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3754 && *cp
++ == 'b' && iswhite (*cp
++))
3756 cp
= skip_spaces (cp
);
3761 && !iswhite (*cp
) && *cp
!= '{' && *cp
!= '(')
3763 pfnote (savenstr (sp
, cp
-sp
), TRUE
,
3764 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3767 else if (globals
/* only if tagging global vars is enabled */
3768 && ((cp
= lb
.buffer
,
3777 && (*cp
== '(' || iswhite (*cp
)))
3779 /* After "my" or "local", but before any following paren or space. */
3780 char *varname
= NULL
;
3782 cp
= skip_spaces (cp
);
3783 if (*cp
== '$' || *cp
== '@' || *cp
== '%')
3785 char* varstart
= ++cp
;
3786 while (ISALNUM (*cp
) || *cp
== '_')
3788 varname
= savenstr (varstart
, cp
-varstart
);
3792 /* Should be examining a variable list at this point;
3793 could insist on seeing an open parenthesis. */
3794 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
3798 /* Perhaps I should back cp up one character, so the TAGS table
3799 doesn't mention (and so depend upon) the following char. */
3800 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : varname
,
3801 FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3807 * Python support by Eric S. Raymond <esr@thyrsus.com>
3808 * Look for /^def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
3811 Python_functions (inf
)
3816 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3820 && *cp
++ == 'f' && iswhite (*cp
++))
3822 cp
= skip_spaces (cp
);
3823 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3826 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3834 && *cp
++ == 's' && iswhite (*cp
++))
3836 cp
= skip_spaces (cp
);
3837 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3840 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3845 /* Idea by Corny de Souza
3846 * Cobol tag functions
3847 * We could look for anything that could be a paragraph name.
3848 * i.e. anything that starts in column 8 is one word and ends in a full stop.
3851 Cobol_paragraphs (inf
)
3854 register char *bp
, *ep
;
3856 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
3862 /* If eoln, compiler option or comment ignore whole line. */
3863 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
3866 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
3869 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
3870 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
3878 Makefile_targets (inf
)
3883 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
3885 if (*bp
== '\t' || *bp
== '#')
3887 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
3890 pfnote (savenstr (lb
.buffer
, bp
- lb
.buffer
), TRUE
,
3891 lb
.buffer
, bp
- lb
.buffer
+ 1, lineno
, linecharno
);
3895 /* Added by Mosur Mohan, 4/22/88 */
3896 /* Pascal parsing */
3899 * Locates tags for procedures & functions. Doesn't do any type- or
3900 * var-definitions. It does look for the keyword "extern" or
3901 * "forward" immediately following the procedure statement; if found,
3902 * the tag is skipped.
3905 Pascal_functions (inf
)
3908 linebuffer tline
; /* mostly copied from C_entries */
3910 int save_lineno
, save_len
;
3911 char c
, *cp
, *namebuf
;
3913 bool /* each of these flags is TRUE iff: */
3914 incomment
, /* point is inside a comment */
3915 inquote
, /* point is inside '..' string */
3916 get_tagname
, /* point is after PROCEDURE/FUNCTION
3917 keyword, so next item = potential tag */
3918 found_tag
, /* point is after a potential tag */
3919 inparms
, /* point is within parameter-list */
3920 verify_tag
; /* point has passed the parm-list, so the
3921 next token will determine whether this
3922 is a FORWARD/EXTERN to be ignored, or
3923 whether it is a real tag */
3925 save_lcno
= save_lineno
= save_len
= 0; /* keep compiler quiet */
3926 namebuf
= NULL
; /* keep compiler quiet */
3931 initbuffer (&tline
);
3933 incomment
= inquote
= FALSE
;
3934 found_tag
= FALSE
; /* have a proc name; check if extern */
3935 get_tagname
= FALSE
; /* have found "procedure" keyword */
3936 inparms
= FALSE
; /* found '(' after "proc" */
3937 verify_tag
= FALSE
; /* check if "extern" is ahead */
3940 while (!feof (inf
)) /* long main loop to get next char */
3943 if (c
== '\0') /* if end of line */
3946 linecharno
= charno
;
3947 charno
+= readline (&lb
, inf
);
3951 if (!((found_tag
&& verify_tag
)
3953 c
= *dbp
++; /* only if don't need *dbp pointing
3954 to the beginning of the name of
3955 the procedure or function */
3959 if (c
== '}') /* within { } comments */
3961 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
3978 inquote
= TRUE
; /* found first quote */
3980 case '{': /* found open { comment */
3984 if (*dbp
== '*') /* found open (* comment */
3989 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
3992 case ')': /* end of parms list */
3997 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4004 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4006 /* check if this is an "extern" declaration */
4009 if (lowcase (*dbp
== 'e'))
4011 if (tail ("extern")) /* superfluous, really! */
4017 else if (lowcase (*dbp
) == 'f')
4019 if (tail ("forward")) /* check for forward reference */
4025 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4029 pfnote (namebuf
, TRUE
,
4030 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
4034 if (get_tagname
) /* grab name of proc or fn */
4039 /* save all values for later tagging */
4040 grow_linebuffer (&tline
, lb
.len
+ 1);
4041 strcpy (tline
.buffer
, lb
.buffer
);
4042 save_lineno
= lineno
;
4043 save_lcno
= linecharno
;
4045 /* grab block name */
4046 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4048 namebuf
= savenstr (dbp
, cp
-dbp
);
4049 dbp
= cp
; /* set dbp to e-o-token */
4050 save_len
= dbp
- lb
.buffer
+ 1;
4051 get_tagname
= FALSE
;
4055 /* and proceed to check for "extern" */
4057 else if (!incomment
&& !inquote
&& !found_tag
)
4059 /* check for proc/fn keywords */
4060 switch (lowcase (c
))
4063 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
4067 if (tail ("unction"))
4072 } /* while not eof */
4074 free (tline
.buffer
);
4078 * lisp tag functions
4079 * look for (def or (DEF, quote or QUOTE
4082 static int L_isdef
P_((char *));
4083 static int L_isquote
P_((char *));
4084 static void L_getit
P_((void));
4088 register char *strp
;
4090 return ((strp
[1] == 'd' || strp
[1] == 'D')
4091 && (strp
[2] == 'e' || strp
[2] == 'E')
4092 && (strp
[3] == 'f' || strp
[3] == 'F'));
4097 register char *strp
;
4099 return ((*++strp
== 'q' || *strp
== 'Q')
4100 && (*++strp
== 'u' || *strp
== 'U')
4101 && (*++strp
== 'o' || *strp
== 'O')
4102 && (*++strp
== 't' || *strp
== 'T')
4103 && (*++strp
== 'e' || *strp
== 'E')
4104 && iswhite (*++strp
));
4112 if (*dbp
== '\'') /* Skip prefix quote */
4114 else if (*dbp
== '(')
4116 if (L_isquote (dbp
))
4117 dbp
+= 7; /* Skip "(quote " */
4119 dbp
+= 1; /* Skip "(" before name in (defstruct (foo)) */
4120 dbp
= skip_spaces (dbp
);
4123 for (cp
= dbp
/*+1*/;
4124 *cp
!= '\0' && *cp
!= '(' && !iswhite(*cp
) && *cp
!= ')';
4130 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
4131 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4135 Lisp_functions (inf
)
4138 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4144 dbp
= skip_non_spaces (dbp
);
4145 dbp
= skip_spaces (dbp
);
4150 /* Check for (foo::defmumble name-defined ... */
4153 while (*dbp
!= '\0' && !iswhite (*dbp
)
4154 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
4159 while (*dbp
== ':');
4161 if (L_isdef (dbp
- 1))
4163 dbp
= skip_non_spaces (dbp
);
4164 dbp
= skip_spaces (dbp
);
4174 * Postscript tag functions
4175 * Just look for lines where the first character is '/'
4176 * Richard Mlynarik <mly@adoc.xerox.com>
4177 * Also look at "defineps" for PSWrap
4178 * suggested by Masatake YAMATO <masata-y@is.aist-nara.ac.jp>
4181 Postscript_functions (inf
)
4184 register char *bp
, *ep
;
4186 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4191 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4194 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
4195 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4197 else if (strneq (bp
, "defineps", 8))
4199 bp
= skip_non_spaces (bp
);
4200 bp
= skip_spaces (bp
);
4208 * Scheme tag functions
4209 * look for (def... xyzzy
4210 * look for (def... (xyzzy
4211 * look for (def ... ((...(xyzzy ....
4212 * look for (set! xyzzy
4216 Scheme_functions (inf
)
4221 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4224 && (bp
[1] == 'D' || bp
[1] == 'd')
4225 && (bp
[2] == 'E' || bp
[2] == 'e')
4226 && (bp
[3] == 'F' || bp
[3] == 'f'))
4228 bp
= skip_non_spaces (bp
);
4229 /* Skip over open parens and white space */
4230 while (iswhite (*bp
) || *bp
== '(')
4235 && (bp
[1] == 'S' || bp
[1] == 's')
4236 && (bp
[2] == 'E' || bp
[2] == 'e')
4237 && (bp
[3] == 'T' || bp
[3] == 't')
4238 && (bp
[4] == '!' || bp
[4] == '!')
4239 && (iswhite (bp
[5])))
4241 bp
= skip_non_spaces (bp
);
4242 bp
= skip_spaces (bp
);
4248 /* Find tags in TeX and LaTeX input files. */
4250 /* TEX_toktab is a table of TeX control sequences that define tags.
4251 Each TEX_tabent records one such control sequence.
4252 CONVERT THIS TO USE THE Stab TYPE!! */
4259 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
4261 /* Default set of control sequences to put into TEX_toktab.
4262 The value of environment var TEXTAGS is prepended to this. */
4264 char *TEX_defenv
= "\
4265 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4266 :part:appendix:entry:index";
4268 static void TEX_mode
P_((FILE *));
4269 static struct TEX_tabent
*TEX_decode_env
P_((char *, char *));
4270 static int TEX_Token
P_((char *));
4272 char TEX_esc
= '\\';
4273 char TEX_opgrp
= '{';
4274 char TEX_clgrp
= '}';
4277 * TeX/LaTeX scanning loop.
4286 /* Select either \ or ! as escape character. */
4289 /* Initialize token table once from environment. */
4291 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
4293 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4296 /* Look at each esc in line. */
4297 while ((cp
= etags_strchr (cp
, TEX_esc
)) != NULL
)
4301 linecharno
+= cp
- lasthit
;
4303 i
= TEX_Token (lasthit
);
4306 /* We seem to include the TeX command in the tag name.
4308 for (p = lasthit + TEX_toktab[i].len;
4309 *p != '\0' && *p != TEX_clgrp;
4312 pfnote (/*savenstr (lasthit, p-lasthit)*/ (char *)NULL
, TRUE
,
4313 lb
.buffer
, lb
.len
, lineno
, linecharno
);
4314 break; /* We only tag a line once */
4320 #define TEX_LESC '\\'
4321 #define TEX_SESC '!'
4324 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
4325 chars accordingly. */
4332 while ((c
= getc (inf
)) != EOF
)
4334 /* Skip to next line if we hit the TeX comment char. */
4338 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
4354 /* If the input file is compressed, inf is a pipe, and rewind may fail.
4355 No attempt is made to correct the situation. */
4359 /* Read environment and prepend it to the default string.
4360 Build token table. */
4361 static struct TEX_tabent
*
4362 TEX_decode_env (evarname
, defenv
)
4366 register char *env
, *p
;
4368 struct TEX_tabent
*tab
;
4371 /* Append default string to environment. */
4372 env
= getenv (evarname
);
4378 env
= concat (oldenv
, defenv
, "");
4381 /* Allocate a token table */
4382 for (size
= 1, p
= env
; p
;)
4383 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
4385 /* Add 1 to leave room for null terminator. */
4386 tab
= xnew (size
+ 1, struct TEX_tabent
);
4388 /* Unpack environment string into token table. Be careful about */
4389 /* zero-length strings (leading ':', "::" and trailing ':') */
4392 p
= etags_strchr (env
, ':');
4393 if (!p
) /* End of environment string. */
4394 p
= env
+ strlen (env
);
4396 { /* Only non-zero strings. */
4397 tab
[i
].name
= savenstr (env
, p
- env
);
4398 tab
[i
].len
= strlen (tab
[i
].name
);
4405 tab
[i
].name
= NULL
; /* Mark end of table. */
4413 /* If the text at CP matches one of the tag-defining TeX command names,
4414 return the pointer to the first occurrence of that command in TEX_toktab.
4415 Otherwise return -1.
4416 Keep the capital `T' in `token' for dumb truncating compilers
4417 (this distinguishes it from `TEX_toktab' */
4424 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
4425 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
4430 /* Texinfo support. Dave Love, Mar. 2000. */
4436 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4442 && *cp
++ == 'e' && iswhite (*cp
++)))
4444 start
= cp
= skip_spaces(cp
);
4445 while (*cp
!= '\0' && *cp
!= ',')
4447 pfnote (savenstr (start
, cp
- start
), TRUE
,
4448 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4454 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
4456 * Assumes that the predicate starts at column 0.
4457 * Only the first clause of a predicate is added.
4459 static int prolog_pred
P_((char *, char *));
4460 static void prolog_skip_comment
P_((linebuffer
*, FILE *));
4461 static int prolog_atom
P_((char *, int));
4464 Prolog_functions (inf
)
4475 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4477 if (cp
[0] == '\0') /* Empty line */
4479 else if (iswhite (cp
[0])) /* Not a predicate */
4481 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
4482 prolog_skip_comment (&lb
, inf
);
4483 else if ((len
= prolog_pred (cp
, last
)) > 0)
4485 /* Predicate. Store the function name so that we only
4486 generate a tag for the first clause. */
4488 last
= xnew(len
+ 1, char);
4489 else if (len
+ 1 > allocated
)
4490 last
= xrnew (last
, len
+ 1, char);
4491 allocated
= len
+ 1;
4492 strncpy (last
, cp
, len
);
4500 prolog_skip_comment (plb
, inf
)
4508 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
4509 if (cp
[0] == '*' && cp
[1] == '/')
4512 linecharno
+= readline (plb
, inf
);
4518 * A predicate definition is added if it matches:
4519 * <beginning of line><Prolog Atom><whitespace>(
4521 * It is added to the tags database if it doesn't match the
4522 * name of the previous clause header.
4524 * Return the size of the name of the predicate, or 0 if no header
4528 prolog_pred (s
, last
)
4530 char *last
; /* Name of last clause. */
4535 pos
= prolog_atom (s
, 0);
4540 pos
= skip_spaces (s
+ pos
) - s
;
4542 if ((s
[pos
] == '(') || (s
[pos
] == '.'))
4547 /* Save only the first clause. */
4549 || len
!= (int)strlen (last
)
4550 || !strneq (s
, last
, len
))
4552 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4560 * Consume a Prolog atom.
4561 * Return the number of bytes consumed, or -1 if there was an error.
4563 * A prolog atom, in this context, could be one of:
4564 * - An alphanumeric sequence, starting with a lower case letter.
4565 * - A quoted arbitrary string. Single quotes can escape themselves.
4566 * Backslash quotes everything.
4569 prolog_atom (s
, pos
)
4577 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
4579 /* The atom is unquoted. */
4581 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
4585 return pos
- origpos
;
4587 else if (s
[pos
] == '\'')
4598 pos
++; /* A double quote */
4600 else if (s
[pos
] == '\0')
4601 /* Multiline quoted atoms are ignored. */
4603 else if (s
[pos
] == '\\')
4605 if (s
[pos
+1] == '\0')
4612 return pos
- origpos
;
4619 * Support for Erlang -- Anders Lindgren, Feb 1996.
4621 * Generates tags for functions, defines, and records.
4623 * Assumes that Erlang functions start at column 0.
4625 static int erlang_func
P_((char *, char *));
4626 static void erlang_attribute
P_((char *));
4627 static int erlang_atom
P_((char *, int));
4630 Erlang_functions (inf
)
4641 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4643 if (cp
[0] == '\0') /* Empty line */
4645 else if (iswhite (cp
[0])) /* Not function nor attribute */
4647 else if (cp
[0] == '%') /* comment */
4649 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
4651 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
4653 erlang_attribute (cp
);
4656 else if ((len
= erlang_func (cp
, last
)) > 0)
4659 * Function. Store the function name so that we only
4660 * generates a tag for the first clause.
4663 last
= xnew (len
+ 1, char);
4664 else if (len
+ 1 > allocated
)
4665 last
= xrnew (last
, len
+ 1, char);
4666 allocated
= len
+ 1;
4667 strncpy (last
, cp
, len
);
4675 * A function definition is added if it matches:
4676 * <beginning of line><Erlang Atom><whitespace>(
4678 * It is added to the tags database if it doesn't match the
4679 * name of the previous clause header.
4681 * Return the size of the name of the function, or 0 if no function
4685 erlang_func (s
, last
)
4687 char *last
; /* Name of last clause. */
4692 pos
= erlang_atom (s
, 0);
4697 pos
= skip_spaces (s
+ pos
) - s
;
4699 /* Save only the first clause. */
4702 || len
!= (int)strlen (last
)
4703 || !strneq (s
, last
, len
)))
4705 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4714 * Handle attributes. Currently, tags are generated for defines
4717 * They are on the form:
4718 * -define(foo, bar).
4719 * -define(Foo(M, N), M+N).
4720 * -record(graph, {vtab = notable, cyclic = true}).
4723 erlang_attribute (s
)
4729 if (strneq (s
, "-define", 7) || strneq (s
, "-record", 7))
4731 pos
= skip_spaces (s
+ 7) - s
;
4732 if (s
[pos
++] == '(')
4734 pos
= skip_spaces (s
+ pos
) - s
;
4735 len
= erlang_atom (s
, pos
);
4737 pfnote (savenstr (& s
[pos
], len
), TRUE
,
4738 s
, pos
+ len
, lineno
, linecharno
);
4746 * Consume an Erlang atom (or variable).
4747 * Return the number of bytes consumed, or -1 if there was an error.
4750 erlang_atom (s
, pos
)
4758 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
4760 /* The atom is unquoted. */
4762 while (ISALNUM (s
[pos
]) || s
[pos
] == '_')
4764 return pos
- origpos
;
4766 else if (s
[pos
] == '\'')
4777 else if (s
[pos
] == '\0')
4778 /* Multiline quoted atoms are ignored. */
4780 else if (s
[pos
] == '\\')
4782 if (s
[pos
+1] == '\0')
4789 return pos
- origpos
;
4795 #ifdef ETAGS_REGEXPS
4797 static char *scan_separators
P_((char *));
4798 static void analyse_regex
P_((char *, bool));
4799 static void add_regex
P_((char *, bool, language
*));
4800 static char *substitute
P_((char *, char *, struct re_registers
*));
4802 /* Take a string like "/blah/" and turn it into "blah", making sure
4803 that the first and last characters are the same, and handling
4804 quoted separator characters. Actually, stops on the occurrence of
4805 an unquoted separator. Also turns "\t" into a Tab character.
4806 Returns pointer to terminating separator. Works in place. Null
4807 terminates name string. */
4809 scan_separators (name
)
4813 char *copyto
= name
;
4814 bool quoted
= FALSE
;
4816 for (++name
; *name
!= '\0'; ++name
)
4822 else if (*name
== sep
)
4826 /* Something else is quoted, so preserve the quote. */
4832 else if (*name
== '\\')
4834 else if (*name
== sep
)
4840 /* Terminate copied string. */
4845 /* Look at the argument of --regex or --no-regex and do the right
4846 thing. Same for each line of a regexp file. */
4848 analyse_regex (regex_arg
, ignore_case
)
4852 if (regex_arg
== NULL
)
4853 free_patterns (); /* --no-regex: remove existing regexps */
4855 /* A real --regexp option or a line in a regexp file. */
4856 switch (regex_arg
[0])
4858 /* Comments in regexp file or null arg to --regex. */
4864 /* Read a regex file. This is recursive and may result in a
4865 loop, which will stop when the file descriptors are exhausted. */
4869 linebuffer regexbuf
;
4870 char *regexfile
= regex_arg
+ 1;
4872 /* regexfile is a file containing regexps, one per line. */
4873 regexfp
= fopen (regexfile
, "r");
4874 if (regexfp
== NULL
)
4879 initbuffer (®exbuf
);
4880 while (readline_internal (®exbuf
, regexfp
) > 0)
4881 analyse_regex (regexbuf
.buffer
, ignore_case
);
4882 free (regexbuf
.buffer
);
4887 /* Regexp to be used for a specific language only. */
4891 char *lang_name
= regex_arg
+ 1;
4894 for (cp
= lang_name
; *cp
!= '}'; cp
++)
4897 error ("unterminated language name in regex: %s", regex_arg
);
4901 lang
= get_language_from_langname (lang_name
);
4904 add_regex (cp
+ 1, ignore_case
, lang
);
4908 /* Regexp to be used for any language. */
4910 add_regex (regex_arg
, ignore_case
, NULL
);
4915 /* Turn a name, which is an ed-style (but Emacs syntax) regular
4916 expression, into a real regular expression by compiling it. */
4918 add_regex (regexp_pattern
, ignore_case
, lang
)
4919 char *regexp_pattern
;
4925 struct re_pattern_buffer
*patbuf
;
4929 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
4931 error ("%s: unterminated regexp", regexp_pattern
);
4934 name
= scan_separators (regexp_pattern
);
4935 if (regexp_pattern
[0] == '\0')
4937 error ("null regexp", (char *)NULL
);
4940 (void) scan_separators (name
);
4942 patbuf
= xnew (1, struct re_pattern_buffer
);
4943 /* Translation table to fold case if appropriate. */
4944 patbuf
->translate
= (ignore_case
) ? lc_trans
: NULL
;
4945 patbuf
->fastmap
= NULL
;
4946 patbuf
->buffer
= NULL
;
4947 patbuf
->allocated
= 0;
4949 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
4952 error ("%s while compiling pattern", err
);
4957 p_head
= xnew (1, pattern
);
4958 p_head
->regex
= savestr (regexp_pattern
);
4959 p_head
->p_next
= pp
;
4960 p_head
->language
= lang
;
4961 p_head
->pattern
= patbuf
;
4962 p_head
->name_pattern
= savestr (name
);
4963 p_head
->error_signaled
= FALSE
;
4967 * Do the substitutions indicated by the regular expression and
4971 substitute (in
, out
, regs
)
4973 struct re_registers
*regs
;
4976 int size
, dig
, diglen
;
4979 size
= strlen (out
);
4981 /* Pass 1: figure out how much to allocate by finding all \N strings. */
4982 if (out
[size
- 1] == '\\')
4983 fatal ("pattern error in \"%s\"", out
);
4984 for (t
= etags_strchr (out
, '\\');
4986 t
= etags_strchr (t
+ 2, '\\'))
4990 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
4996 /* Allocate space and do the substitutions. */
4997 result
= xnew (size
+ 1, char);
4999 for (t
= result
; *out
!= '\0'; out
++)
5000 if (*out
== '\\' && ISDIGIT (*++out
))
5002 /* Using "dig2" satisfies my debugger. Bleah. */
5004 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5005 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
5012 if (DEBUG
&& (t
> result
+ size
|| t
- result
!= (int)strlen (result
)))
5018 /* Deallocate all patterns. */
5023 while (p_head
!= NULL
)
5025 pp
= p_head
->p_next
;
5026 free (p_head
->regex
);
5027 free (p_head
->name_pattern
);
5042 /* Go till you get to white space or a syntactic break */
5044 *cp
!= '\0' && *cp
!= '(' && *cp
!= ')' && !iswhite (*cp
);
5047 pfnote (savenstr (bp
, cp
-bp
), TRUE
,
5048 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5051 #endif /* ETAGS_REGEXPS */
5052 /* Initialize a linebuffer for use */
5058 lbp
->buffer
= xnew (200, char);
5062 * Read a line of text from `stream' into `lbp', excluding the
5063 * newline or CR-NL, if any. Return the number of characters read from
5064 * `stream', which is the length of the line including the newline.
5066 * On DOS or Windows we do not count the CR character, if any, before the
5067 * NL, in the returned length; this mirrors the behavior of emacs on those
5068 * platforms (for text files, it translates CR-NL to NL as it reads in the
5072 readline_internal (lbp
, stream
)
5074 register FILE *stream
;
5076 char *buffer
= lbp
->buffer
;
5077 register char *p
= lbp
->buffer
;
5078 register char *pend
;
5081 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
5085 register int c
= getc (stream
);
5088 /* We're at the end of linebuffer: expand it. */
5090 buffer
= xrnew (buffer
, lbp
->size
, char);
5091 p
+= buffer
- lbp
->buffer
;
5092 pend
= buffer
+ lbp
->size
;
5093 lbp
->buffer
= buffer
;
5103 if (p
> buffer
&& p
[-1] == '\r')
5107 /* Assume CRLF->LF translation will be performed by Emacs
5108 when loading this file, so CRs won't appear in the buffer.
5109 It would be cleaner to compensate within Emacs;
5110 however, Emacs does not know how many CRs were deleted
5111 before any given point in the file. */
5126 lbp
->len
= p
- buffer
;
5128 return lbp
->len
+ chars_deleted
;
5132 * Like readline_internal, above, but in addition try to match the
5133 * input line against relevant regular expressions.
5136 readline (lbp
, stream
)
5140 /* Read new line. */
5141 long result
= readline_internal (lbp
, stream
);
5142 #ifdef ETAGS_REGEXPS
5146 /* Match against relevant patterns. */
5148 for (pp
= p_head
; pp
!= NULL
; pp
= pp
->p_next
)
5150 /* Only use generic regexps or those for the current language. */
5151 if (pp
->language
!= NULL
&& pp
->language
!= curlang
)
5154 match
= re_match (pp
->pattern
, lbp
->buffer
, lbp
->len
, 0, &pp
->regs
);
5159 if (!pp
->error_signaled
)
5161 error ("error while matching \"%s\"", pp
->regex
);
5162 pp
->error_signaled
= TRUE
;
5169 /* Match occurred. Construct a tag. */
5170 if (pp
->name_pattern
[0] != '\0')
5172 /* Make a named tag. */
5173 char *name
= substitute (lbp
->buffer
,
5174 pp
->name_pattern
, &pp
->regs
);
5176 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
5180 /* Make an unnamed tag. */
5181 pfnote ((char *)NULL
, TRUE
,
5182 lbp
->buffer
, match
, lineno
, linecharno
);
5187 #endif /* ETAGS_REGEXPS */
5193 * Return a pointer to a space of size strlen(cp)+1 allocated
5194 * with xnew where the string CP has been copied.
5200 return savenstr (cp
, strlen (cp
));
5204 * Return a pointer to a space of size LEN+1 allocated with xnew where
5205 * the string CP has been copied for at most the first LEN characters.
5214 dp
= xnew (len
+ 1, char);
5215 strncpy (dp
, cp
, len
);
5221 * Return the ptr in sp at which the character c last
5222 * appears; NULL if not found
5224 * Identical to POSIX strrchr, included for portability.
5227 etags_strrchr (sp
, c
)
5228 register const char *sp
;
5231 register const char *r
;
5244 * Return the ptr in sp at which the character c first
5245 * appears; NULL if not found
5247 * Identical to POSIX strchr, included for portability.
5250 etags_strchr (sp
, c
)
5251 register const char *sp
;
5262 /* Skip spaces, return new pointer. */
5267 while (iswhite (*cp
))
5272 /* Skip non spaces, return new pointer. */
5274 skip_non_spaces (cp
)
5277 while (*cp
!= '\0' && !iswhite (*cp
))
5282 /* Print error message and exit. */
5300 suggest_asking_for_help ()
5302 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
5313 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
5316 const char *s1
, *s2
;
5318 fprintf (stderr
, "%s: ", progname
);
5319 fprintf (stderr
, s1
, s2
);
5320 fprintf (stderr
, "\n");
5323 /* Return a newly-allocated string whose contents
5324 concatenate those of s1, s2, s3. */
5329 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
5330 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
5332 strcpy (result
, s1
);
5333 strcpy (result
+ len1
, s2
);
5334 strcpy (result
+ len1
+ len2
, s3
);
5335 result
[len1
+ len2
+ len3
] = '\0';
5340 /* Does the same work as the system V getcwd, but does not need to
5341 guess the buffer size in advance. */
5347 char *path
= xnew (bufsize
, char);
5349 while (getcwd (path
, bufsize
) == NULL
)
5351 if (errno
!= ERANGE
)
5355 path
= xnew (bufsize
, char);
5358 canonicalize_filename (path
);
5361 #else /* not HAVE_GETCWD */
5363 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
5367 for (p
= path
; *p
!= '\0'; p
++)
5373 return strdup (path
);
5374 #else /* not MSDOS */
5379 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
5380 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
5385 #endif /* not MSDOS */
5386 #endif /* not HAVE_GETCWD */
5389 /* Return a newly allocated string containing the file name of FILE
5390 relative to the absolute directory DIR (which should end with a slash). */
5392 relative_filename (file
, dir
)
5395 char *fp
, *dp
, *afn
, *res
;
5398 /* Find the common root of file and dir (with a trailing slash). */
5399 afn
= absolute_filename (file
, cwd
);
5402 while (*fp
++ == *dp
++)
5404 fp
--, dp
--; /* back to the first differing char */
5406 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
5409 do /* look at the equal chars until '/' */
5413 /* Build a sequence of "../" strings for the resulting relative file name. */
5415 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
5417 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
5420 strcat (res
, "../");
5422 /* Add the file name relative to the common root of file and dir. */
5423 strcat (res
, fp
+ 1);
5429 /* Return a newly allocated string containing the absolute file name
5430 of FILE given DIR (which should end with a slash). */
5432 absolute_filename (file
, dir
)
5435 char *slashp
, *cp
, *res
;
5437 if (filename_is_absolute (file
))
5438 res
= savestr (file
);
5440 /* We don't support non-absolute file names with a drive
5441 letter, like `d:NAME' (it's too much hassle). */
5442 else if (file
[1] == ':')
5443 fatal ("%s: relative file names with drive letters not supported", file
);
5446 res
= concat (dir
, file
, "");
5448 /* Delete the "/dirname/.." and "/." substrings. */
5449 slashp
= etags_strchr (res
, '/');
5450 while (slashp
!= NULL
&& slashp
[0] != '\0')
5452 if (slashp
[1] == '.')
5454 if (slashp
[2] == '.'
5455 && (slashp
[3] == '/' || slashp
[3] == '\0'))
5460 while (cp
>= res
&& !filename_is_absolute (cp
));
5462 cp
= slashp
; /* the absolute name begins with "/.." */
5464 /* Under MSDOS and NT we get `d:/NAME' as absolute
5465 file name, so the luser could say `d:/../NAME'.
5466 We silently treat this as `d:/NAME'. */
5467 else if (cp
[0] != '/')
5470 strcpy (cp
, slashp
+ 3);
5474 else if (slashp
[2] == '/' || slashp
[2] == '\0')
5476 strcpy (slashp
, slashp
+ 2);
5481 slashp
= etags_strchr (slashp
+ 1, '/');
5485 return savestr ("/");
5490 /* Return a newly allocated string containing the absolute
5491 file name of dir where FILE resides given DIR (which should
5492 end with a slash). */
5494 absolute_dirname (file
, dir
)
5500 canonicalize_filename (file
);
5501 slashp
= etags_strrchr (file
, '/');
5503 return savestr (dir
);
5506 res
= absolute_filename (file
, dir
);
5512 /* Whether the argument string is an absolute file name. The argument
5513 string must have been canonicalized with canonicalize_filename. */
5515 filename_is_absolute (fn
)
5518 return (fn
[0] == '/'
5520 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
5525 /* Translate backslashes into slashes. Works in place. */
5527 canonicalize_filename (fn
)
5531 /* Canonicalize drive letter case. */
5532 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
5533 fn
[0] = upcase (fn
[0]);
5534 /* Convert backslashes to slashes. */
5535 for (; *fn
!= '\0'; fn
++)
5540 fn
= NULL
; /* shut up the compiler */
5544 /* Increase the size of a linebuffer. */
5546 grow_linebuffer (lbp
, toksize
)
5550 while (lbp
->size
< toksize
)
5552 lbp
->buffer
= xrnew (lbp
->buffer
, lbp
->size
, char);
5555 /* Like malloc but get fatal error if memory is exhausted. */
5560 long *result
= (long *) malloc (size
);
5562 fatal ("virtual memory exhausted", (char *)NULL
);
5567 xrealloc (ptr
, size
)
5571 long *result
= (long *) realloc (ptr
, size
);
5573 fatal ("virtual memory exhausted", (char *)NULL
);