1 /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
2 Copyright (C) 1984, 87, 88, 89, 93, 94, 95, 98, 99, 2000, 2001
3 Free Software Foundation, Inc. and Ken Arnold
5 This file is not considered part of GNU Emacs.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 * Ctags originally by Ken Arnold.
24 * Fortran added by Jim Kleckner.
25 * Ed Pelegri-Llopart added C typedefs.
26 * Gnu Emacs TAGS format and modifications by RMS?
27 * 199x Sam Kendall added C++.
28 * 1993 Francesco Potortì reorganised C and C++ based on work by Joe Wells.
29 * 1994 Regexp tags by Tom Tromey.
30 * 2001 Nested classes by Francesco Potortì based on work by Mykola Dzyuba.
32 * Francesco Potortì <pot@gnu.org> has maintained it since 1993.
35 char pot_etags_version
[] = "@(#) pot number is $Revision: 2.79 $";
45 # define NDEBUG /* disable assert */
48 #if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
49 # define P_(proto) proto
56 /* On some systems, Emacs defines static as nothing for the sake
57 of unexec. We don't want that here since we don't use unexec. */
59 # define ETAGS_REGEXPS /* use the regexp features */
60 # define LONG_OPTIONS /* accept long options */
61 #endif /* HAVE_CONFIG_H */
64 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
71 # include <sys/param.h>
73 # ifndef HAVE_CONFIG_H
75 # include <sys/config.h>
87 # define MAXPATHLEN _MAX_PATH
92 # endif /* not HAVE_CONFIG_H */
95 # endif /* undef HAVE_GETCWD */
96 #else /* !WINDOWSNT */
101 extern char *getenv ();
103 #endif /* !WINDOWSNT */
108 # if defined (HAVE_GETCWD) && !WINDOWSNT
109 extern char *getcwd (char *buf
, size_t size
);
111 #endif /* HAVE_UNISTD_H */
120 #include <sys/types.h>
121 #include <sys/stat.h>
123 #if !defined (S_ISREG) && defined (S_IFREG)
124 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
130 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
132 extern int optind
, opterr
;
133 #endif /* LONG_OPTIONS */
137 #endif /* ETAGS_REGEXPS */
139 /* Define CTAGS to make the program "ctags" compatible with the usual one.
140 Leave it undefined to make the program "etags", which makes emacs-style
141 tag tables and tags typedefs, #defines and struct/union/enum by default. */
149 /* Exit codes for success and failure. */
159 #define C_PLPL 0x00001 /* C++ */
160 #define C_STAR 0x00003 /* C* */
161 #define C_JAVA 0x00005 /* JAVA */
162 #define YACC 0x10000 /* yacc file */
163 #define PUREC (!(c_ext & ~YACC)) /* no extensions (apart from possibly yacc) */
165 #define streq(s,t) (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t))
166 #define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
168 #define CHARS 256 /* 2^sizeof(char) */
169 #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
170 #define iswhite(c) (_wht[CHAR(c)]) /* c is white */
171 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name */
172 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token */
173 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token */
174 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens */
176 #define ISALNUM(c) isalnum (CHAR(c))
177 #define ISALPHA(c) isalpha (CHAR(c))
178 #define ISDIGIT(c) isdigit (CHAR(c))
179 #define ISLOWER(c) islower (CHAR(c))
181 #define lowcase(c) tolower (CHAR(c))
182 #define upcase(c) toupper (CHAR(c))
186 * xnew, xrnew -- allocate, reallocate storage
188 * SYNOPSIS: Type *xnew (int n, Type);
189 * void xrnew (OldPointer, int n, Type);
192 # include "chkmalloc.h"
193 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
194 (n) * sizeof (Type)))
195 # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
196 (char *) (op), (n) * sizeof (Type)))
198 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
199 # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
200 (char *) (op), (n) * sizeof (Type)))
205 typedef void Lang_function
P_((FILE *));
210 char *command
; /* Takes one arg and decompresses to stdout */
216 Lang_function
*function
;
222 typedef struct node_st
223 { /* sorting structure */
224 char *name
; /* function or type name */
225 char *file
; /* file name */
226 bool is_func
; /* use pattern or line no */
227 bool been_warned
; /* set if noticed dup */
228 int lno
; /* line number tag is on */
229 long cno
; /* character number line starts on */
230 char *pat
; /* search pattern */
231 struct node_st
*left
, *right
; /* left and right sons */
235 * A `linebuffer' is a structure which holds a line of text.
236 * `readline_internal' reads a line from a stream into a linebuffer
237 * and works regardless of the length of the line.
238 * SIZE is the size of BUFFER, LEN is the length of the string in
239 * BUFFER after readline reads it.
248 /* Many compilers barf on this:
249 Lang_function Ada_funcs;
250 so let's write it this way */
251 static void Ada_funcs
P_((FILE *));
252 static void Asm_labels
P_((FILE *));
253 static void C_entries
P_((int c_ext
, FILE *));
254 static void default_C_entries
P_((FILE *));
255 static void plain_C_entries
P_((FILE *));
256 static void Cjava_entries
P_((FILE *));
257 static void Cobol_paragraphs
P_((FILE *));
258 static void Cplusplus_entries
P_((FILE *));
259 static void Cstar_entries
P_((FILE *));
260 static void Erlang_functions
P_((FILE *));
261 static void Fortran_functions
P_((FILE *));
262 static void Yacc_entries
P_((FILE *));
263 static void Lisp_functions
P_((FILE *));
264 static void Makefile_targets
P_((FILE *));
265 static void Pascal_functions
P_((FILE *));
266 static void Perl_functions
P_((FILE *));
267 static void Postscript_functions
P_((FILE *));
268 static void Prolog_functions
P_((FILE *));
269 static void Python_functions
P_((FILE *));
270 static void Scheme_functions
P_((FILE *));
271 static void TeX_commands
P_((FILE *));
272 static void Texinfo_nodes
P_((FILE *));
273 static void just_read_file
P_((FILE *));
275 static void print_language_names
P_((void));
276 static void print_version
P_((void));
277 static void print_help
P_((void));
278 int main
P_((int, char **));
279 static int number_len
P_((long));
281 static compressor
*get_compressor_from_suffix
P_((char *, char **));
282 static language
*get_language_from_langname
P_((char *));
283 static language
*get_language_from_interpreter
P_((char *));
284 static language
*get_language_from_filename
P_((char *));
285 static int total_size_of_entries
P_((node
*));
286 static long readline
P_((linebuffer
*, FILE *));
287 static long readline_internal
P_((linebuffer
*, FILE *));
288 static void get_tag
P_((char *));
291 static void analyse_regex
P_((char *, bool));
292 static void add_regex
P_((char *, bool, language
*));
293 static void free_patterns
P_((void));
294 #endif /* ETAGS_REGEXPS */
295 static void error
P_((const char *, const char *));
296 static void suggest_asking_for_help
P_((void));
297 void fatal
P_((char *, char *));
298 static void pfatal
P_((char *));
299 static void add_node
P_((node
*, node
**));
301 static void init
P_((void));
302 static void initbuffer
P_((linebuffer
*));
303 static void find_entries
P_((char *, FILE *));
304 static void free_tree
P_((node
*));
305 static void pfnote
P_((char *, bool, char *, int, int, long));
306 static void new_pfnote
P_((char *, int, bool, char *, int, int, long));
307 static void process_file
P_((char *));
308 static void put_entries
P_((node
*));
309 static void takeprec
P_((void));
311 static char *concat
P_((char *, char *, char *));
312 static char *skip_spaces
P_((char *));
313 static char *skip_non_spaces
P_((char *));
314 static char *savenstr
P_((char *, int));
315 static char *savestr
P_((char *));
316 static char *etags_strchr
P_((const char *, int));
317 static char *etags_strrchr
P_((const char *, int));
318 static char *etags_getcwd
P_((void));
319 static char *relative_filename
P_((char *, char *));
320 static char *absolute_filename
P_((char *, char *));
321 static char *absolute_dirname
P_((char *, char *));
322 static bool filename_is_absolute
P_((char *f
));
323 static void canonicalize_filename
P_((char *));
324 static void linebuffer_setlen
P_((linebuffer
*, int));
325 long *xmalloc
P_((unsigned int));
326 long *xrealloc
P_((char *, unsigned int));
329 char searchar
= '/'; /* use /.../ searches */
331 char *tagfile
; /* output file */
332 char *progname
; /* name this program was invoked with */
333 char *cwd
; /* current working directory */
334 char *tagfiledir
; /* directory of tagfile */
335 FILE *tagf
; /* ioptr for tags file */
337 char *curfile
; /* current input file name */
338 language
*curlang
; /* current language */
340 int lineno
; /* line number of current line */
341 long charno
; /* current character number */
342 long linecharno
; /* charno of start of current line */
343 char *dbp
; /* pointer to start of current tag */
345 node
*head
; /* the head of the binary tree of tags */
347 linebuffer lb
; /* the current line */
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_or_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 */
580 print_language_names ()
585 puts ("\nThese are the currently supported languages, along with the\n\
586 default file names and dot suffixes:");
587 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
589 printf (" %-*s", 10, lang
->name
);
590 if (lang
->filenames
!= NULL
)
591 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
592 printf (" %s", *name
);
593 if (lang
->suffixes
!= NULL
)
594 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
595 printf (" .%s", *ext
);
598 puts ("Where `auto' means use default language for files based on file\n\
599 name suffix, and `none' means only do regexp processing on files.\n\
600 If no language is specified and no matching suffix is found,\n\
601 the first line of the file is read for a sharp-bang (#!) sequence\n\
602 followed by the name of an interpreter. If no such sequence is found,\n\
603 Fortran is tried first; if no tags are found, C is tried next.\n\
604 Compressed files are supported using gzip and bzip2.");
608 # define EMACS_NAME "GNU Emacs"
611 # define VERSION "21"
616 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
617 puts ("Copyright (C) 1999 Free Software Foundation, Inc. and Ken Arnold");
618 puts ("This program is distributed under the same terms as Emacs");
626 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
628 These are the options accepted by %s.\n", progname
, progname
);
630 puts ("You may use unambiguous abbreviations for the long option names.");
632 puts ("Long option names do not work with this executable, as it is not\n\
633 linked with GNU getopt.");
634 #endif /* LONG_OPTIONS */
635 puts ("A - as file name means read names from stdin (one per line).");
637 printf (" Absolute names are stored in the output file as they are.\n\
638 Relative ones are stored relative to the output file's directory.");
641 puts ("-a, --append\n\
642 Append tag entries to existing tags file.");
644 puts ("--packages-only\n\
645 For Ada files, only generate tags for packages .");
648 puts ("-B, --backward-search\n\
649 Write the search commands for the tag entries using '?', the\n\
650 backward-search command instead of '/', the forward-search command.");
653 Treat files whose name suffix defaults to C language as C++ files.");
655 puts ("--declarations\n\
656 In C and derived languages, create tags for function declarations,");
658 puts ("\tand create tags for extern variables if --globals is used.");
661 ("\tand create tags for extern variables unless --no-globals is used.");
664 puts ("-d, --defines\n\
665 Create tag entries for C #define constants and enum constants, too.");
667 puts ("-D, --no-defines\n\
668 Don't create tag entries for C #define constants and enum constants.\n\
669 This makes the tags file smaller.");
673 puts ("-i FILE, --include=FILE\n\
674 Include a note in tag file indicating that, when searching for\n\
675 a tag, one should also consult the tags file FILE after\n\
676 checking the current file.");
677 puts ("-l LANG, --language=LANG\n\
678 Force the following files to be considered as written in the\n\
679 named language up to the next --language=LANG option.");
684 Create tag entries for global variables in some languages.");
686 puts ("--no-globals\n\
687 Do not create tag entries for global variables in some\n\
688 languages. This makes the tags file smaller.");
690 Create tag entries for member variables in C and derived languages.");
693 puts ("-r /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
694 Make a tag for each line matching pattern REGEXP in the following\n\
695 files. {LANGUAGE}/REGEXP/ uses REGEXP for LANGUAGE files only.\n\
696 regexfile is a file containing one REGEXP per line.\n\
697 REGEXP is anchored (as if preceded by ^).\n\
698 The form /REGEXP/NAME/ creates a named tag.\n\
699 For example Tcl named tags can be created with:\n\
700 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
701 puts ("-c /REGEXP/, --ignore-case-regex=/REGEXP/ or --ignore-case-regex=@regexfile\n\
702 Like -r, --regex but ignore case when matching expressions.");
703 puts ("-R, --no-regex\n\
704 Don't create tags from regexps for the following files.");
705 #endif /* ETAGS_REGEXPS */
706 puts ("-o FILE, --output=FILE\n\
707 Write the tags to FILE.");
708 puts ("-I, --ignore-indentation\n\
709 Don't rely on indentation quite as much as normal. Currently,\n\
710 this means not to assume that a closing brace in the first\n\
711 column is the final brace of a function or structure\n\
712 definition in C and C++.");
716 puts ("-t, --typedefs\n\
717 Generate tag entries for C and Ada typedefs.");
718 puts ("-T, --typedefs-and-c++\n\
719 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
720 and C++ member functions.");
721 puts ("-u, --update\n\
722 Update the tag entries for the given files, leaving tag\n\
723 entries for other files in place. Currently, this is\n\
724 implemented by deleting the existing entries for the given\n\
725 files and then rewriting the new entries at the end of the\n\
726 tags file. It is often faster to simply rebuild the entire\n\
727 tag file than to use this.");
728 puts ("-v, --vgrind\n\
729 Generates an index of items intended for human consumption,\n\
730 similar to the output of vgrind. The index is sorted, and\n\
731 gives the page number of each item.");
732 puts ("-w, --no-warn\n\
733 Suppress warning messages about entries defined in multiple\n\
735 puts ("-x, --cxref\n\
736 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
737 The output uses line numbers instead of page numbers, but\n\
738 beyond that the differences are cosmetic; try both to see\n\
742 puts ("-V, --version\n\
743 Print the version of the program.\n\
745 Print this help message.");
747 print_language_names ();
750 puts ("Report bugs to bug-gnu-emacs@gnu.org");
764 /* This structure helps us allow mixing of --lang and file names. */
767 enum argument_type arg_type
;
769 language
*lang
; /* language of the regexp */
772 #ifdef VMS /* VMS specific functions */
776 /* This is a BUG! ANY arbitrary limit is a BUG!
777 Won't someone please fix this? */
778 #define MAX_FILE_SPEC_LEN 255
781 char body
[MAX_FILE_SPEC_LEN
+ 1];
785 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
786 returning in each successive call the next file name matching the input
787 spec. The function expects that each in_spec passed
788 to it will be processed to completion; in particular, up to and
789 including the call following that in which the last matching name
790 is returned, the function ignores the value of in_spec, and will
791 only start processing a new spec with the following call.
792 If an error occurs, on return out_spec contains the value
793 of in_spec when the error occurred.
795 With each successive file name returned in out_spec, the
796 function's return value is one. When there are no more matching
797 names the function returns zero. If on the first call no file
798 matches in_spec, or there is any other error, -1 is returned.
803 #define OUTSIZE MAX_FILE_SPEC_LEN
809 static long context
= 0;
810 static struct dsc$descriptor_s o
;
811 static struct dsc$descriptor_s i
;
812 static bool pass1
= TRUE
;
819 o
.dsc$a_pointer
= (char *) out
;
820 o
.dsc$w_length
= (short)OUTSIZE
;
821 i
.dsc$a_pointer
= in
;
822 i
.dsc$w_length
= (short)strlen(in
);
823 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
824 i
.dsc$b_class
= DSC$K_CLASS_S
;
825 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
826 o
.dsc$b_class
= DSC$K_CLASS_VS
;
828 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
830 out
->body
[out
->curlen
] = EOS
;
833 else if (status
== RMS$_NMF
)
837 strcpy(out
->body
, in
);
840 lib$
find_file_end(&context
);
846 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
847 name of each file specified by the provided arg expanding wildcards.
850 gfnames (arg
, p_error
)
854 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
856 switch (fn_exp (&filename
, arg
))
860 return filename
.body
;
866 return filename
.body
;
870 #ifndef OLD /* Newer versions of VMS do provide `system'. */
874 error ("%s", "system() function not implemented under VMS");
878 #define VERSION_DELIM ';'
879 char *massage_name (s
)
885 if (*s
== VERSION_DELIM
)
903 unsigned int nincluded_files
;
904 char **included_files
;
907 int current_arg
, file_count
;
908 linebuffer filename_lb
;
914 _fmode
= O_BINARY
; /* all of files are treated as binary files */
919 included_files
= xnew (argc
, char *);
923 /* Allocate enough no matter what happens. Overkill, but each one
925 argbuffer
= xnew (argc
, argument
);
928 /* Set syntax for regular expression routines. */
929 re_set_syntax (RE_SYNTAX_EMACS
| RE_INTERVALS
);
930 /* Translation table for case-insensitive search. */
931 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
932 lc_trans
[i
] = lowcase (i
);
933 #endif /* ETAGS_REGEXPS */
936 * If etags, always find typedefs and structure tags. Why not?
937 * Also default is to find macro constants, enum constants and
942 typedefs
= typedefs_or_cplusplus
= constantypedefs
= TRUE
;
953 optstring
= "-aCdDf:Il:o:r:c:RStTi:BuvxwVhH";
955 optstring
= "-aCdDf:Il:o:StTi:BuvxwVhH";
956 #endif /* ETAGS_REGEXPS */
959 optstring
= optstring
+ 1;
960 #endif /* LONG_OPTIONS */
962 opt
= getopt_long (argc
, argv
, optstring
, longopts
, 0);
969 /* If getopt returns 0, then it has already processed a
970 long-named option. We should do nothing. */
974 /* This means that a file name has been seen. Record it. */
975 argbuffer
[current_arg
].arg_type
= at_filename
;
976 argbuffer
[current_arg
].what
= optarg
;
981 /* Common options. */
982 case 'a': append_to_tagfile
= TRUE
; break;
983 case 'C': cplusplus
= TRUE
; break;
984 case 'd': constantypedefs
= TRUE
; break;
985 case 'D': constantypedefs
= FALSE
; break;
986 case 'f': /* for compatibility with old makefiles */
990 error ("-o option may only be given once.", (char *)NULL
);
991 suggest_asking_for_help ();
996 case 'S': /* for backward compatibility */
997 noindentypedefs
= TRUE
;
1001 language
*lang
= get_language_from_langname (optarg
);
1004 argbuffer
[current_arg
].lang
= lang
;
1005 argbuffer
[current_arg
].arg_type
= at_language
;
1010 #ifdef ETAGS_REGEXPS
1012 argbuffer
[current_arg
].arg_type
= at_regexp
;
1013 argbuffer
[current_arg
].what
= optarg
;
1017 argbuffer
[current_arg
].arg_type
= at_regexp
;
1018 argbuffer
[current_arg
].what
= NULL
;
1022 argbuffer
[current_arg
].arg_type
= at_icregexp
;
1023 argbuffer
[current_arg
].what
= optarg
;
1026 #endif /* ETAGS_REGEXPS */
1038 typedefs
= typedefs_or_cplusplus
= TRUE
;
1043 included_files
[nincluded_files
++] = optarg
;
1046 /* Ctags options. */
1047 case 'B': searchar
= '?'; break;
1048 case 'u': update
= TRUE
; break;
1049 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1050 case 'x': cxref_style
= TRUE
; break;
1051 case 'w': no_warnings
= TRUE
; break;
1054 suggest_asking_for_help ();
1058 for (; optind
< argc
; ++optind
)
1060 argbuffer
[current_arg
].arg_type
= at_filename
;
1061 argbuffer
[current_arg
].what
= argv
[optind
];
1066 if (nincluded_files
== 0 && file_count
== 0)
1068 error ("no input files specified.", (char *)NULL
);
1069 suggest_asking_for_help ();
1072 if (tagfile
== NULL
)
1073 tagfile
= CTAGS
? "tags" : "TAGS";
1074 cwd
= etags_getcwd (); /* the current working directory */
1075 if (cwd
[strlen (cwd
) - 1] != '/')
1078 cwd
= concat (oldcwd
, "/", "");
1081 if (streq (tagfile
, "-"))
1084 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1086 init (); /* set up boolean "functions" */
1089 initbuffer (&filename_lb
);
1093 if (streq (tagfile
, "-"))
1097 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1098 doesn't take effect until after `stdout' is already open). */
1099 if (!isatty (fileno (stdout
)))
1100 setmode (fileno (stdout
), O_BINARY
);
1104 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1110 * Loop through files finding functions.
1112 for (i
= 0; i
< current_arg
; ++i
)
1114 switch (argbuffer
[i
].arg_type
)
1117 forced_lang
= argbuffer
[i
].lang
;
1119 #ifdef ETAGS_REGEXPS
1121 analyse_regex (argbuffer
[i
].what
, FALSE
);
1124 analyse_regex (argbuffer
[i
].what
, TRUE
);
1129 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1133 error ("can't find file %s\n", this_file
);
1138 this_file
= massage_name (this_file
);
1141 this_file
= argbuffer
[i
].what
;
1143 /* Input file named "-" means read file names from stdin
1144 (one per line) and use them. */
1145 if (streq (this_file
, "-"))
1146 while (readline_internal (&filename_lb
, stdin
) > 0)
1147 process_file (filename_lb
.buffer
);
1149 process_file (this_file
);
1157 #ifdef ETAGS_REGEXPS
1159 #endif /* ETAGS_REGEXPS */
1163 while (nincluded_files
-- > 0)
1164 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1170 /* If CTAGS, we are here. process_file did not write the tags yet,
1171 because we want them ordered. Let's do it now. */
1183 for (i
= 0; i
< current_arg
; ++i
)
1185 if (argbuffer
[i
].arg_type
!= at_filename
)
1188 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1189 tagfile
, argbuffer
[i
].what
, tagfile
);
1190 if (system (cmd
) != GOOD
)
1191 fatal ("failed to execute shell command", (char *)NULL
);
1193 append_to_tagfile
= TRUE
;
1196 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1207 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
1208 exit (system (cmd
));
1216 * Return a compressor given the file name. If EXTPTR is non-zero,
1217 * return a pointer into FILE where the compressor-specific
1218 * extension begins. If no compressor is found, NULL is returned
1219 * and EXTPTR is not significant.
1220 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
1223 get_compressor_from_suffix (file
, extptr
)
1228 char *slash
, *suffix
;
1230 /* This relies on FN to be after canonicalize_filename,
1231 so we don't need to consider backslashes on DOS_NT. */
1232 slash
= etags_strrchr (file
, '/');
1233 suffix
= etags_strrchr (file
, '.');
1234 if (suffix
== NULL
|| suffix
< slash
)
1239 /* Let those poor souls who live with DOS 8+3 file name limits get
1240 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1241 Only the first do loop is run if not MSDOS */
1244 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1245 if (streq (compr
->suffix
, suffix
))
1248 break; /* do it only once: not really a loop */
1251 } while (*suffix
!= '\0');
1258 * Return a language given the name.
1261 get_language_from_langname (name
)
1267 error ("empty language name", (char *)NULL
);
1270 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1271 if (streq (name
, lang
->name
))
1273 error ("unknown language \"%s\"", name
);
1281 * Return a language given the interpreter name.
1284 get_language_from_interpreter (interpreter
)
1290 if (interpreter
== NULL
)
1292 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1293 if (lang
->interpreters
!= NULL
)
1294 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1295 if (streq (*iname
, interpreter
))
1304 * Return a language given the file name.
1307 get_language_from_filename (file
)
1311 char **name
, **ext
, *suffix
;
1313 /* Try whole file name first. */
1314 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1315 if (lang
->filenames
!= NULL
)
1316 for (name
= lang
->filenames
; *name
!= NULL
; name
++)
1317 if (streq (*name
, file
))
1320 /* If not found, try suffix after last dot. */
1321 suffix
= etags_strrchr (file
, '.');
1325 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1326 if (lang
->suffixes
!= NULL
)
1327 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1328 if (streq (*ext
, suffix
))
1336 * This routine is called on each file argument.
1342 struct stat stat_buf
;
1345 char *compressed_name
, *uncompressed_name
;
1346 char *ext
, *real_name
;
1349 canonicalize_filename (file
);
1350 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1352 error ("skipping inclusion of %s in self.", file
);
1355 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1357 compressed_name
= NULL
;
1358 real_name
= uncompressed_name
= savestr (file
);
1362 real_name
= compressed_name
= savestr (file
);
1363 uncompressed_name
= savenstr (file
, ext
- file
);
1366 /* If the canonicalised uncompressed name has already be dealt with,
1367 skip it silently, else add it to the list. */
1369 typedef struct processed_file
1372 struct processed_file
*next
;
1374 static processed_file
*pf_head
= NULL
;
1375 register processed_file
*fnp
;
1377 for (fnp
= pf_head
; fnp
!= NULL
; fnp
= fnp
->next
)
1378 if (streq (uncompressed_name
, fnp
->filename
))
1381 pf_head
= xnew (1, struct processed_file
);
1382 pf_head
->filename
= savestr (uncompressed_name
);
1383 pf_head
->next
= fnp
;
1386 if (stat (real_name
, &stat_buf
) != 0)
1388 /* Reset real_name and try with a different name. */
1390 if (compressed_name
!= NULL
) /* try with the given suffix */
1392 if (stat (uncompressed_name
, &stat_buf
) == 0)
1393 real_name
= uncompressed_name
;
1395 else /* try all possible suffixes */
1397 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1399 compressed_name
= concat (file
, ".", compr
->suffix
);
1400 if (stat (compressed_name
, &stat_buf
) != 0)
1404 char *suf
= compressed_name
+ strlen (file
);
1405 size_t suflen
= strlen (compr
->suffix
) + 1;
1406 for ( ; suf
[1]; suf
++, suflen
--)
1408 memmove (suf
, suf
+ 1, suflen
);
1409 if (stat (compressed_name
, &stat_buf
) == 0)
1411 real_name
= compressed_name
;
1415 if (real_name
!= NULL
)
1418 free (compressed_name
);
1419 compressed_name
= NULL
;
1423 real_name
= compressed_name
;
1428 if (real_name
== NULL
)
1433 } /* try with a different name */
1435 if (!S_ISREG (stat_buf
.st_mode
))
1437 error ("skipping %s: it is not a regular file.", real_name
);
1440 if (real_name
== compressed_name
)
1442 char *cmd
= concat (compr
->command
, " ", real_name
);
1443 inf
= popen (cmd
, "r");
1447 inf
= fopen (real_name
, "r");
1454 find_entries (uncompressed_name
, inf
);
1456 if (real_name
== compressed_name
)
1465 if (filename_is_absolute (uncompressed_name
))
1467 /* file is an absolute file name. Canonicalise it. */
1468 filename
= absolute_filename (uncompressed_name
, cwd
);
1472 /* file is a file name relative to cwd. Make it relative
1473 to the directory of the tags file. */
1474 filename
= relative_filename (uncompressed_name
, tagfiledir
);
1476 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1484 if (compressed_name
) free(compressed_name
);
1485 if (uncompressed_name
) free(uncompressed_name
);
1490 * This routine sets up the boolean pseudo-functions which work
1491 * by setting boolean flags dependent upon the corresponding character.
1492 * Every char which is NOT in that string is not a white char. Therefore,
1493 * all of the array "_wht" is set to FALSE, and then the elements
1494 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1495 * of a char is TRUE if it is the string "white", else FALSE.
1503 for (i
= 0; i
< CHARS
; i
++)
1504 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1505 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1506 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1507 notinname('\0') = notinname('\n');
1508 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1509 begtoken('\0') = begtoken('\n');
1510 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1511 intoken('\0') = intoken('\n');
1512 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1513 endtoken('\0') = endtoken('\n');
1517 * This routine opens the specified file and calls the function
1518 * which finds the function and type definitions.
1520 node
*last_node
= NULL
;
1523 find_entries (file
, inf
)
1529 node
*old_last_node
;
1531 /* Memory leakage here: the string pointed by curfile is
1532 never released, because curfile is copied into np->file
1533 for each node, to be used in CTAGS mode. The amount of
1534 memory leaked here is the sum of the lengths of the
1536 curfile
= savestr (file
);
1538 /* If user specified a language, use it. */
1540 if (lang
!= NULL
&& lang
->function
!= NULL
)
1543 lang
->function (inf
);
1547 /* Try to guess the language given the file name. */
1548 lang
= get_language_from_filename (file
);
1549 if (lang
!= NULL
&& lang
->function
!= NULL
)
1552 lang
->function (inf
);
1556 /* Look for sharp-bang as the first two characters. */
1557 if (readline_internal (&lb
, inf
) > 0
1559 && lb
.buffer
[0] == '#'
1560 && lb
.buffer
[1] == '!')
1564 /* Set lp to point at the first char after the last slash in the
1565 line or, if no slashes, at the first nonblank. Then set cp to
1566 the first successive blank and terminate the string. */
1567 lp
= etags_strrchr (lb
.buffer
+2, '/');
1571 lp
= skip_spaces (lb
.buffer
+ 2);
1572 cp
= skip_non_spaces (lp
);
1575 if (strlen (lp
) > 0)
1577 lang
= get_language_from_interpreter (lp
);
1578 if (lang
!= NULL
&& lang
->function
!= NULL
)
1581 lang
->function (inf
);
1586 /* We rewind here, even if inf may be a pipe. We fail if the
1587 length of the first line is longer than the pipe block size,
1588 which is unlikely. */
1592 old_last_node
= last_node
;
1593 curlang
= get_language_from_langname ("fortran");
1594 Fortran_functions (inf
);
1596 /* No Fortran entries found. Try C. */
1597 if (old_last_node
== last_node
)
1599 /* We do not tag if rewind fails.
1600 Only the file name will be recorded in the tags file. */
1602 curlang
= get_language_from_langname (cplusplus
? "c++" : "c");
1603 default_C_entries (inf
);
1611 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1612 char *name
; /* tag name, or NULL if unnamed */
1613 bool is_func
; /* tag is a function */
1614 char *linestart
; /* start of the line where tag is */
1615 int linelen
; /* length of the line where tag is */
1616 int lno
; /* line number */
1617 long cno
; /* character number */
1621 if (CTAGS
&& name
== NULL
)
1624 np
= xnew (1, node
);
1626 /* If ctags mode, change name "main" to M<thisfilename>. */
1627 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1629 register char *fp
= etags_strrchr (curfile
, '/');
1630 np
->name
= concat ("M", fp
== NULL
? curfile
: fp
+ 1, "");
1631 fp
= etags_strrchr (np
->name
, '.');
1632 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
1637 np
->been_warned
= FALSE
;
1639 np
->is_func
= is_func
;
1641 /* Our char numbers are 0-base, because of C language tradition?
1642 ctags compatibility? old versions compatibility? I don't know.
1643 Anyway, since emacs's are 1-base we expect etags.el to take care
1644 of the difference. If we wanted to have 1-based numbers, we would
1645 uncomment the +1 below. */
1646 np
->cno
= cno
/* + 1 */ ;
1647 np
->left
= np
->right
= NULL
;
1648 if (CTAGS
&& !cxref_style
)
1650 if (strlen (linestart
) < 50)
1651 np
->pat
= concat (linestart
, "$", "");
1653 np
->pat
= savenstr (linestart
, 50);
1656 np
->pat
= savenstr (linestart
, linelen
);
1658 add_node (np
, &head
);
1662 * TAGS format specification
1663 * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
1665 * pfnote should emit the optimized form [unnamed tag] only if:
1666 * 1. name does not contain any of the characters " \t\r\n(),;";
1667 * 2. linestart contains name as either a rightmost, or rightmost but
1668 * one character, substring;
1669 * 3. the character, if any, immediately before name in linestart must
1670 * be one of the characters " \t(),;";
1671 * 4. the character, if any, immediately after name in linestart must
1672 * also be one of the characters " \t(),;".
1674 * The real implementation uses the notinname() macro, which recognises
1675 * characters slightly different form " \t\r\n(),;". See the variable
1678 #define traditional_tag_style TRUE
1680 new_pfnote (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1681 char *name
; /* tag name, or NULL if unnamed */
1682 int namelen
; /* tag length */
1683 bool is_func
; /* tag is a function */
1684 char *linestart
; /* start of the line where tag is */
1685 int linelen
; /* length of the line where tag is */
1686 int lno
; /* line number */
1687 long cno
; /* character number */
1695 for (cp
= name
; !notinname (*cp
); cp
++)
1697 if (*cp
== '\0') /* rule #1 */
1699 cp
= linestart
+ linelen
- namelen
;
1700 if (notinname (linestart
[linelen
-1]))
1701 cp
-= 1; /* rule #4 */
1702 if (cp
>= linestart
/* rule #2 */
1704 || notinname (cp
[-1])) /* rule #3 */
1705 && strneq (name
, cp
, namelen
)) /* rule #2 */
1706 named
= FALSE
; /* use unnamed tag */
1711 name
= savenstr (name
, namelen
);
1714 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
1719 * recurse on left children, iterate on right children.
1727 register node
*node_right
= np
->right
;
1728 free_tree (np
->left
);
1729 if (np
->name
!= NULL
)
1739 * Adds a node to the tree of nodes. In etags mode, we don't keep
1740 * it sorted; we just keep a linear list. In ctags mode, maintain
1741 * an ordered tree, with no attempt at balancing.
1743 * add_node is the only function allowed to add nodes, so it can
1747 add_node (np
, cur_node_p
)
1748 node
*np
, **cur_node_p
;
1751 register node
*cur_node
= *cur_node_p
;
1753 if (cur_node
== NULL
)
1763 if (last_node
== NULL
)
1764 fatal ("internal error in add_node", (char *)NULL
);
1765 last_node
->right
= np
;
1771 dif
= strcmp (np
->name
, cur_node
->name
);
1774 * If this tag name matches an existing one, then
1775 * do not add the node, but maybe print a warning.
1779 if (streq (np
->file
, cur_node
->file
))
1783 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1784 np
->file
, lineno
, np
->name
);
1785 fprintf (stderr
, "Second entry ignored\n");
1788 else if (!cur_node
->been_warned
&& !no_warnings
)
1792 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1793 np
->file
, cur_node
->file
, np
->name
);
1794 cur_node
->been_warned
= TRUE
;
1799 /* Actually add the node */
1800 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1814 /* Output subentries that precede this one */
1815 put_entries (np
->left
);
1817 /* Output this entry */
1821 if (np
->name
!= NULL
)
1822 fprintf (tagf
, "%s\177%s\001%d,%ld\n",
1823 np
->pat
, np
->name
, np
->lno
, np
->cno
);
1825 fprintf (tagf
, "%s\177%d,%ld\n",
1826 np
->pat
, np
->lno
, np
->cno
);
1830 if (np
->name
== NULL
)
1831 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
1836 fprintf (stdout
, "%s %s %d\n",
1837 np
->name
, np
->file
, (np
->lno
+ 63) / 64);
1839 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1840 np
->name
, np
->lno
, np
->file
, np
->pat
);
1844 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->file
);
1848 putc (searchar
, tagf
);
1851 for (sp
= np
->pat
; *sp
; sp
++)
1853 if (*sp
== '\\' || *sp
== searchar
)
1857 putc (searchar
, tagf
);
1860 { /* a typedef; text pattern inadequate */
1861 fprintf (tagf
, "%d", np
->lno
);
1867 /* Output subentries that follow this one */
1868 put_entries (np
->right
);
1871 /* Length of a number's decimal representation. */
1877 while ((num
/= 10) > 0)
1883 * Return total number of characters that put_entries will output for
1884 * the nodes in the subtree of the specified node. Works only if
1885 * we are not ctags, but called only in that case. This count
1886 * is irrelevant with the new tags.el, but is still supplied for
1887 * backward compatibility.
1890 total_size_of_entries (np
)
1898 for (total
= 0; np
!= NULL
; np
= np
->right
)
1900 /* Count left subentries. */
1901 total
+= total_size_of_entries (np
->left
);
1903 /* Count this entry */
1904 total
+= strlen (np
->pat
) + 1;
1905 total
+= number_len ((long) np
->lno
) + 1 + number_len (np
->cno
) + 1;
1906 if (np
->name
!= NULL
)
1907 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
)))
2170 * C functions and variables are recognized using a simple
2171 * finite automaton. fvdef is its state variable.
2175 fvnone
, /* nothing seen */
2176 fdefunkey
, /* Emacs DEFUN keyword seen */
2177 fdefunname
, /* Emacs DEFUN name seen */
2178 foperator
, /* func: operator keyword seen (cplpl) */
2179 fvnameseen
, /* function or variable name seen */
2180 fstartlist
, /* func: just after open parenthesis */
2181 finlist
, /* func: in parameter list */
2182 flistseen
, /* func: after parameter list */
2183 fignore
, /* func: before open brace */
2184 vignore
/* var-like: ignore until ';' */
2187 bool fvextern
; /* func or var: extern keyword seen; */
2190 * typedefs are recognized using a simple finite automaton.
2191 * typdef is its state variable.
2195 tnone
, /* nothing seen */
2196 tkeyseen
, /* typedef keyword seen */
2197 ttypeseen
, /* defined type seen */
2198 tinbody
, /* inside typedef body */
2199 tend
, /* just before typedef tag */
2200 tignore
/* junk after typedef tag */
2204 * struct-like structures (enum, struct and union) are recognized
2205 * using another simple finite automaton. `structdef' is its state
2210 snone
, /* nothing seen yet */
2211 skeyseen
, /* struct-like keyword seen */
2212 stagseen
, /* struct-like tag seen */
2213 scolonseen
, /* colon seen after struct-like tag */
2214 sinbody
/* in struct body: recognize member func defs*/
2218 * When structdef is stagseen, scolonseen, or sinbody, structtype is the
2219 * type of the preceding struct-like keyword.
2221 char *structtag
= "<uninited>";
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 * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
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.
2272 } token
; /* latest token read */
2273 linebuffer token_name
; /* its name */
2275 static bool consider_token
P_((char *, int, int, int, int, int, bool *));
2276 static void make_C_tag
P_((bool));
2280 * checks to see if the current token is at the start of a
2281 * function or variable, or corresponds to a typedef, or
2282 * is a struct/union/enum tag, or #define, or an enum constant.
2284 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2285 * with args. C_EXT is which language we are looking at.
2296 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func_or_var
)
2297 register char *str
; /* IN: token pointer */
2298 register int len
; /* IN: token length */
2299 register int c
; /* IN: first char after the token */
2300 int c_ext
; /* IN: C extensions mask */
2301 int cblev
; /* IN: curly brace level */
2302 int parlev
; /* IN: parenthesis level */
2303 bool *is_func_or_var
; /* OUT: function or variable found */
2305 /* When structdef is stagseen, scolonseen, or snone with cblev > 0,
2306 structtype is the type of the preceding struct-like keyword. */
2307 static enum sym_type structtype
;
2308 static enum sym_type toktype
;
2311 toktype
= C_symtype (str
, len
, c_ext
);
2314 * Advance the definedef state machine.
2319 /* We're not on a preprocessor line. */
2320 if (toktype
== st_C_gnumacro
)
2327 if (toktype
== st_C_define
)
2329 definedef
= ddefineseen
;
2333 definedef
= dignorerest
;
2338 * Make a tag for any macro, unless it is a constant
2339 * and constantypedefs is FALSE.
2341 definedef
= dignorerest
;
2342 *is_func_or_var
= (c
== '(');
2343 if (!*is_func_or_var
&& !constantypedefs
)
2350 error ("internal error: definedef value.", (char *)NULL
);
2359 if (toktype
== st_C_typedef
)
2378 /* Do not return here, so the structdef stuff has a chance. */
2392 * This structdef business is currently only invoked when cblev==0.
2393 * It should be recursively invoked whatever the curly brace level,
2394 * and a stack of states kept, to allow for definitions of structs
2397 * This structdef business is NOT invoked when we are ctags and the
2398 * file is plain C. This is because a struct tag may have the same
2399 * name as another tag, and this loses with ctags.
2403 case st_C_javastruct
:
2404 if (structdef
== stagseen
)
2405 structdef
= scolonseen
;
2409 if (typdef
== tkeyseen
2410 || (typedefs_or_cplusplus
&& cblev
== 0 && structdef
== snone
))
2412 structdef
= skeyseen
;
2413 structtype
= toktype
;
2418 if (structdef
== skeyseen
)
2420 /* Save the tag for struct/union/class, for functions and variables
2421 that may be defined inside. */
2422 if (structtype
== st_C_struct
)
2423 structtag
= savenstr (str
, len
);
2425 structtag
= "<enum>";
2426 structdef
= stagseen
;
2430 if (typdef
!= tnone
)
2433 /* Detect Objective C constructs. */
2443 objdef
= oimplementation
;
2447 case oimplementation
:
2448 /* Save the class tag for functions or variables defined inside. */
2449 objtag
= savenstr (str
, len
);
2453 /* Save the class tag for categories. */
2454 objtag
= savenstr (str
, len
);
2456 *is_func_or_var
= TRUE
;
2460 *is_func_or_var
= TRUE
;
2467 objdef
= omethodtag
;
2468 linebuffer_setlen (&token_name
, len
);
2469 strncpy (token_name
.buffer
, str
, len
);
2470 token_name
.buffer
[len
] = '\0';
2476 objdef
= omethodparm
;
2481 objdef
= omethodtag
;
2482 linebuffer_setlen (&token_name
, token_name
.len
+ len
);
2483 strncat (token_name
.buffer
, str
, len
);
2488 if (toktype
== st_C_objend
)
2490 /* Memory leakage here: the string pointed by objtag is
2491 never released, because many tests would be needed to
2492 avoid breaking on incorrect input code. The amount of
2493 memory leaked here is the sum of the lengths of the
2501 /* A function, variable or enum constant? */
2508 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!= vignore
)
2509 fvdef
= fvnone
; /* should be useless */
2517 *is_func_or_var
= TRUE
;
2520 if ((c_ext
& C_PLPL
) && strneq (str
+len
-10, "::operator", 10))
2523 *is_func_or_var
= TRUE
;
2526 if (constantypedefs
&& structdef
== sinbody
&& structtype
== st_C_enum
)
2531 fvdef
= fdefunname
; /* GNU macro */
2532 *is_func_or_var
= TRUE
;
2535 fvdef
= fvnameseen
; /* function or variable */
2536 *is_func_or_var
= TRUE
;
2547 * C_entries often keeps pointers to tokens or lines which are older than
2548 * the line currently read. By keeping two line buffers, and switching
2549 * them at end of line, it is possible to use those pointers.
2557 #define current_lb_is_new (newndx == curndx)
2558 #define switch_line_buffers() (curndx = 1 - curndx)
2560 #define curlb (lbs[curndx].lb)
2561 #define newlb (lbs[newndx].lb)
2562 #define curlinepos (lbs[curndx].linepos)
2563 #define newlinepos (lbs[newndx].linepos)
2565 #define CNL_SAVE_DEFINEDEF() \
2567 curlinepos = charno; \
2569 linecharno = charno; \
2570 charno += readline (&curlb, inf); \
2571 lp = curlb.buffer; \
2578 CNL_SAVE_DEFINEDEF(); \
2579 if (savetoken.valid) \
2581 token = savetoken; \
2582 savetoken.valid = FALSE; \
2584 definedef = dnone; \
2592 /* This function should never be called when token.valid is FALSE, but
2593 we must protect against invalid input or internal errors. */
2594 if (DEBUG
|| token
.valid
)
2596 if (traditional_tag_style
)
2598 /* This was the original code. Now we call new_pfnote instead,
2599 which uses the new method for naming tags (see new_pfnote). */
2602 if (CTAGS
|| token
.named
)
2604 name
= savestr (token_name
.buffer
);
2606 name
= concat (name
, "##invalid##", "");
2608 pfnote (name
, isfun
,
2609 token
.line
, token
.linelen
, token
.lineno
, token
.linepos
);
2612 new_pfnote (token_name
.buffer
, token_name
.len
, isfun
,
2613 token
.line
, token
.linelen
, token
.lineno
, token
.linepos
);
2614 token
.valid
= FALSE
;
2621 * This routine finds functions, variables, typedefs,
2622 * #define's, enum constants and struct/union/enum definitions in
2623 * C syntax and adds them to the list.
2626 C_entries (c_ext
, inf
)
2627 int c_ext
; /* extension of C */
2628 FILE *inf
; /* input file */
2630 register char c
; /* latest char read; '\0' for end of line */
2631 register char *lp
; /* pointer one beyond the character `c' */
2632 int curndx
, newndx
; /* indices for current and new lb */
2633 register int tokoff
; /* offset in line of start of current token */
2634 register int toklen
; /* length of current token */
2635 char *qualifier
; /* string used to qualify names */
2636 int qlen
; /* length of qualifier */
2637 int cblev
; /* current curly brace level */
2638 int parlev
; /* current parenthesis level */
2639 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
2641 bool yacc_rules
; /* in the rules part of a yacc file */
2642 struct tok savetoken
; /* token saved during preprocessor handling */
2645 initbuffer (&token_name
);
2646 initbuffer (&lbs
[0].lb
);
2647 initbuffer (&lbs
[1].lb
);
2649 tokoff
= toklen
= 0; /* keep compiler quiet */
2650 curndx
= newndx
= 0;
2656 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
2657 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
2659 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2660 token
.valid
= savetoken
.valid
= FALSE
;
2663 cplpl
= (c_ext
& C_PLPL
) == C_PLPL
;
2664 cjava
= (c_ext
& C_JAVA
) == C_JAVA
;
2666 { qualifier
= "."; qlen
= 1; }
2668 { qualifier
= "::"; qlen
= 2; }
2676 /* If we're at the end of the line, the next character is a
2677 '\0'; don't skip it, because it's the thing that tells us
2678 to read the next line. */
2699 /* Newlines inside comments do not end macro definitions in
2701 CNL_SAVE_DEFINEDEF ();
2714 /* Newlines inside strings do not end macro definitions
2715 in traditional cpp, even though compilers don't
2716 usually accept them. */
2717 CNL_SAVE_DEFINEDEF ();
2727 /* Hmmm, something went wrong. */
2755 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2768 else if (/* cplpl && */ *lp
== '/')
2776 if ((c_ext
& YACC
) && *lp
== '%')
2778 /* entering or exiting rules section in yacc file */
2780 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
2781 typdef
= tnone
; structdef
= snone
;
2782 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2784 yacc_rules
= !yacc_rules
;
2790 if (definedef
== dnone
)
2793 bool cpptoken
= TRUE
;
2795 /* Look back on this line. If all blanks, or nonblanks
2796 followed by an end of comment, this is a preprocessor
2798 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2801 if (*cp
== '*' && *(cp
+1) == '/')
2810 definedef
= dsharpseen
;
2811 } /* if (definedef == dnone) */
2817 /* Consider token only if some complicated conditions are satisfied. */
2818 if (typdef
!= tignore
2819 && definedef
!= dignorerest
2821 && (definedef
!= dnone
2822 || (cblev
== 0 && structdef
!= scolonseen
)
2823 || (cblev
== 1 && cplpl
&& structdef
== sinbody
)
2824 || (PUREC
&& structdef
== sinbody
))
2831 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken (lp
[1]))
2834 * This handles :: in the middle, but not at the
2835 * beginning of an identifier. Also, space-separated
2836 * :: is not recognised.
2845 bool funorvar
= FALSE
;
2848 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2849 c_ext
, cblev
, parlev
, &funorvar
))
2851 if (fvdef
== foperator
)
2854 lp
= skip_spaces (lp
-1);
2858 && !iswhite (*lp
) && *lp
!= '(')
2861 toklen
+= lp
- oldlp
;
2863 token
.named
= FALSE
;
2866 && definedef
== dnone
2867 && structdef
== sinbody
)
2868 /* function or var defined in C++ class body */
2870 int len
= strlen (structtag
) + qlen
+ toklen
;
2871 linebuffer_setlen (&token_name
, len
);
2872 strcpy (token_name
.buffer
, structtag
);
2873 strcat (token_name
.buffer
, qualifier
);
2874 strncat (token_name
.buffer
,
2875 newlb
.buffer
+ tokoff
, toklen
);
2878 else if (objdef
== ocatseen
)
2879 /* Objective C category */
2881 int len
= strlen (objtag
) + 2 + toklen
;
2882 linebuffer_setlen (&token_name
, len
);
2883 strcpy (token_name
.buffer
, objtag
);
2884 strcat (token_name
.buffer
, "(");
2885 strncat (token_name
.buffer
,
2886 newlb
.buffer
+ tokoff
, toklen
);
2887 strcat (token_name
.buffer
, ")");
2890 else if (objdef
== omethodtag
2891 || objdef
== omethodparm
)
2892 /* Objective C method */
2896 else if (fvdef
== fdefunname
)
2898 bool defun
= (newlb
.buffer
[tokoff
] == 'F');
2902 /* Rewrite the tag so that emacs lisp DEFUNs
2903 can be found by their elisp name */
2910 linebuffer_setlen (&token_name
, len
);
2911 strncpy (token_name
.buffer
,
2912 newlb
.buffer
+ off
, len
);
2913 token_name
.buffer
[len
] = '\0';
2916 if (token_name
.buffer
[len
] == '_')
2917 token_name
.buffer
[len
] = '-';
2918 token
.named
= defun
;
2922 linebuffer_setlen (&token_name
, toklen
);
2923 strncpy (token_name
.buffer
,
2924 newlb
.buffer
+ tokoff
, toklen
);
2925 token_name
.buffer
[toklen
] = '\0';
2926 /* Name macros and members. */
2927 token
.named
= (structdef
== stagseen
2928 || typdef
== ttypeseen
2931 && definedef
== dignorerest
)
2933 && definedef
== dnone
2934 && structdef
== sinbody
));
2936 token
.lineno
= lineno
;
2937 token
.linelen
= tokoff
+ toklen
+ 1;
2938 token
.line
= newlb
.buffer
;
2939 token
.linepos
= newlinepos
;
2942 if (definedef
== dnone
2943 && (fvdef
== fvnameseen
2944 || fvdef
== foperator
2945 || structdef
== stagseen
2947 || objdef
!= onone
))
2949 if (current_lb_is_new
)
2950 switch_line_buffers ();
2953 make_C_tag (funorvar
);
2957 } /* if (endtoken (c)) */
2958 else if (intoken (c
))
2964 } /* if (midtoken) */
2965 else if (begtoken (c
))
2976 make_C_tag (TRUE
); /* a function */
2983 if (structdef
== stagseen
&& !cjava
)
2989 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2991 tokoff
= lp
- 1 - newlb
.buffer
;
2996 } /* if (begtoken) */
2997 } /* if must look at token */
3000 /* Detect end of line, colon, comma, semicolon and various braces
3001 after having handled a token.*/
3005 if (definedef
!= dnone
)
3011 make_C_tag (TRUE
); /* an Objective C class */
3015 objdef
= omethodcolon
;
3016 linebuffer_setlen (&token_name
, token_name
.len
+ 1);
3017 strcat (token_name
.buffer
, ":");
3020 if (structdef
== stagseen
)
3021 structdef
= scolonseen
;
3028 make_C_tag (FALSE
); /* a yacc function */
3039 if (definedef
!= dnone
)
3046 if ((members
&& cblev
== 1)
3047 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3048 make_C_tag (FALSE
); /* a variable */
3051 token
.valid
= FALSE
;
3054 if ((declarations
&& typdef
== tnone
&& cblev
== 0)
3055 || (members
&& cblev
== 1))
3056 make_C_tag (TRUE
); /* a function declaration */
3062 /* The following instruction invalidates the token.
3063 Probably the token should be invalidated in all other
3064 cases where some state machine is reset prematurely. */
3065 token
.valid
= FALSE
;
3071 make_C_tag (FALSE
); /* a typedef */
3076 if (structdef
== stagseen
)
3080 if (definedef
!= dnone
)
3086 make_C_tag (TRUE
); /* an Objective C method */
3101 case flistseen
: /* a function */
3108 case fvnameseen
: /* a variable */
3109 if ((members
&& structdef
== sinbody
&& cblev
== 1)
3110 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3116 if (structdef
== stagseen
)
3120 if (definedef
!= dnone
)
3122 if (cblev
== 0 && typdef
== tend
)
3125 make_C_tag (FALSE
); /* a typedef */
3136 if ((members
&& cblev
== 1)
3137 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3138 make_C_tag (FALSE
); /* a variable */
3143 if (structdef
== stagseen
)
3147 if (definedef
!= dnone
)
3149 if (objdef
== otagseen
&& parlev
== 0)
3150 objdef
= oparenseen
;
3154 if (typdef
== ttypeseen
3157 && structdef
!= sinbody
)
3159 /* This handles constructs like:
3160 typedef void OperatorFun (int fun); */
3175 if (definedef
!= dnone
)
3177 if (objdef
== ocatseen
&& parlev
== 1)
3179 make_C_tag (TRUE
); /* an Objective C category */
3191 if (cblev
== 0 && (typdef
== tend
))
3194 make_C_tag (FALSE
); /* a typedef */
3197 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3201 if (definedef
!= dnone
)
3203 if (typdef
== ttypeseen
)
3208 make_C_tag (TRUE
); /* a function */
3217 make_C_tag (TRUE
); /* an Objective C class */
3222 make_C_tag (TRUE
); /* an Objective C method */
3226 /* Neutralize `extern "C" {' grot. */
3227 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
3233 case skeyseen
: /* unnamed struct */
3234 structdef
= sinbody
;
3235 structtag
= "_anonymous_";
3238 case scolonseen
: /* named struct */
3239 structdef
= sinbody
;
3240 make_C_tag (FALSE
); /* a struct */
3246 if (definedef
!= dnone
)
3248 if (fvdef
== fstartlist
)
3249 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3252 if (definedef
!= dnone
)
3254 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
3256 cblev
= 0; /* reset curly brace level if first column */
3257 parlev
= 0; /* also reset paren level, just in case... */
3263 if (typdef
== tinbody
)
3265 /* Memory leakage here: the string pointed by structtag is
3266 never released, because I fear to miss something and
3267 break things while freeing the area. The amount of
3268 memory leaked here is the sum of the lengths of the
3270 if (structdef == sinbody)
3271 free (structtag); */
3274 structtag
= "<error>";
3278 if (definedef
!= dnone
)
3288 if ((members
&& cblev
== 1)
3289 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3290 make_C_tag (FALSE
); /* a variable */
3298 if (objdef
== oinbody
&& cblev
== 0)
3300 objdef
= omethodsign
;
3304 case '#': case '~': case '&': case '%': case '/': case '|':
3305 case '^': case '!': case '<': case '>': case '.': case '?': case ']':
3306 if (definedef
!= dnone
)
3308 /* These surely cannot follow a function tag in C. */
3321 if (objdef
== otagseen
)
3323 make_C_tag (TRUE
); /* an Objective C class */
3326 /* If a macro spans multiple lines don't reset its state. */
3328 CNL_SAVE_DEFINEDEF ();
3334 } /* while not eof */
3336 free (token_name
.buffer
);
3337 free (lbs
[0].lb
.buffer
);
3338 free (lbs
[1].lb
.buffer
);
3342 * Process either a C++ file or a C file depending on the setting
3346 default_C_entries (inf
)
3349 C_entries (cplusplus
? C_PLPL
: 0, inf
);
3352 /* Always do plain ANSI C. */
3354 plain_C_entries (inf
)
3360 /* Always do C++. */
3362 Cplusplus_entries (inf
)
3365 C_entries (C_PLPL
, inf
);
3368 /* Always do Java. */
3373 C_entries (C_JAVA
, inf
);
3381 C_entries (C_STAR
, inf
);
3384 /* Always do Yacc. */
3389 C_entries (YACC
, inf
);
3393 /* A useful macro. */
3394 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3395 for (lineno = charno = 0; /* loop initialization */ \
3396 !feof (file_pointer) /* loop test */ \
3397 && (lineno++, /* instructions at start of loop */ \
3398 linecharno = charno, \
3399 charno += readline (&line_buffer, file_pointer), \
3400 char_pointer = lb.buffer, \
3406 * Read a file, but do no processing. This is used to do regexp
3407 * matching on files that have no language defined.
3410 just_read_file (inf
)
3413 register char *dummy
;
3415 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
3420 /* Fortran parsing */
3422 static bool tail
P_((char *));
3423 static void takeprec
P_((void));
3424 static void getit
P_((FILE *));
3430 register int len
= 0;
3432 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
3434 if (*cp
== '\0' && !intoken (dbp
[len
]))
3445 dbp
= skip_spaces (dbp
);
3449 dbp
= skip_spaces (dbp
);
3450 if (strneq (dbp
, "(*)", 3))
3455 if (!ISDIGIT (*dbp
))
3457 --dbp
; /* force failure */
3462 while (ISDIGIT (*dbp
));
3471 dbp
= skip_spaces (dbp
);
3475 linecharno
= charno
;
3476 charno
+= readline (&lb
, inf
);
3481 dbp
= skip_spaces (dbp
);
3483 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
3485 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
3487 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
3488 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3493 Fortran_functions (inf
)
3496 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3499 dbp
++; /* Ratfor escape to fortran */
3500 dbp
= skip_spaces (dbp
);
3503 switch (lowcase (*dbp
))
3506 if (tail ("integer"))
3514 if (tail ("logical"))
3518 if (tail ("complex") || tail ("character"))
3522 if (tail ("double"))
3524 dbp
= skip_spaces (dbp
);
3527 if (tail ("precision"))
3533 dbp
= skip_spaces (dbp
);
3536 switch (lowcase (*dbp
))
3539 if (tail ("function"))
3543 if (tail ("subroutine"))
3551 if (tail ("blockdata") || tail ("block data"))
3553 dbp
= skip_spaces (dbp
);
3554 if (*dbp
== '\0') /* assume un-named */
3555 pfnote (savestr ("blockdata"), TRUE
,
3556 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
3558 getit (inf
); /* look for name */
3568 * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be> (1998)
3571 static void adagetit
P_((FILE *, char *));
3573 /* Once we are positioned after an "interesting" keyword, let's get
3574 the real tag value necessary. */
3576 adagetit (inf
, name_qualifier
)
3578 char *name_qualifier
;
3586 dbp
= skip_spaces (dbp
);
3588 || (dbp
[0] == '-' && dbp
[1] == '-'))
3591 linecharno
= charno
;
3592 charno
+= readline (&lb
, inf
);
3601 /* Skipping body of procedure body or package body or ....
3602 resetting qualifier to body instead of spec. */
3603 name_qualifier
= "/b";
3609 /* Skipping type of task type or protected type ... */
3617 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
3622 dbp
= skip_spaces (dbp
);
3625 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
3633 name
= concat (dbp
, name_qualifier
, "");
3635 pfnote (name
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3646 bool inquote
= FALSE
;
3648 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3650 while (*dbp
!= '\0')
3652 /* Skip a string i.e. "abcd". */
3653 if (inquote
|| (*dbp
== '"'))
3655 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
3660 continue; /* advance char */
3665 break; /* advance line */
3669 /* Skip comments. */
3670 if (dbp
[0] == '-' && dbp
[1] == '-')
3671 break; /* advance line */
3673 /* Skip character enclosed in single quote i.e. 'a'
3674 and skip single quote starting an attribute i.e. 'Image. */
3683 /* Search for beginning of a token. */
3684 if (!begtoken (*dbp
))
3687 continue; /* advance char */
3690 /* We are at the beginning of a token. */
3695 if (!packages_only
&& tail ("function"))
3696 adagetit (inf
, "/f");
3698 break; /* from switch */
3699 continue; /* advance char */
3702 if (!packages_only
&& tail ("procedure"))
3703 adagetit (inf
, "/p");
3704 else if (tail ("package"))
3705 adagetit (inf
, "/s");
3706 else if (tail ("protected")) /* protected type */
3707 adagetit (inf
, "/t");
3709 break; /* from switch */
3710 continue; /* advance char */
3713 if (!packages_only
&& tail ("task"))
3714 adagetit (inf
, "/k");
3715 else if (typedefs
&& !packages_only
&& tail ("type"))
3717 adagetit (inf
, "/t");
3718 while (*dbp
!= '\0')
3722 break; /* from switch */
3723 continue; /* advance char */
3726 /* Look for the end of the token. */
3727 while (!endtoken (*dbp
))
3730 } /* advance char */
3731 } /* advance line */
3736 * Bob Weiner, Motorola Inc., 4/3/94
3737 * Unix and microcontroller assembly tag handling
3738 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
3746 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3748 /* If first char is alphabetic or one of [_.$], test for colon
3749 following identifier. */
3750 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3752 /* Read past label. */
3754 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3756 if (*cp
== ':' || iswhite (*cp
))
3758 /* Found end of label, so copy it and add it to the table. */
3759 pfnote (savenstr(lb
.buffer
, cp
-lb
.buffer
), TRUE
,
3760 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3769 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
3770 * Perl variable names: /^(my|local).../
3771 * Bart Robinson <lomew@cs.utah.edu> (1995)
3772 * Michael Ernst <mernst@alum.mit.edu> (1997)
3775 Perl_functions (inf
)
3780 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3784 && *cp
++ == 'b' && iswhite (*cp
++))
3786 cp
= skip_spaces (cp
);
3791 && !iswhite (*cp
) && *cp
!= '{' && *cp
!= '(')
3793 pfnote (savenstr (sp
, cp
-sp
), TRUE
,
3794 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3797 else if (globals
/* only if tagging global vars is enabled */
3798 && ((cp
= lb
.buffer
,
3807 && (*cp
== '(' || iswhite (*cp
)))
3809 /* After "my" or "local", but before any following paren or space. */
3810 char *varname
= NULL
;
3812 cp
= skip_spaces (cp
);
3813 if (*cp
== '$' || *cp
== '@' || *cp
== '%')
3815 char* varstart
= ++cp
;
3816 while (ISALNUM (*cp
) || *cp
== '_')
3818 varname
= savenstr (varstart
, cp
-varstart
);
3822 /* Should be examining a variable list at this point;
3823 could insist on seeing an open parenthesis. */
3824 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
3828 /* Perhaps I should back cp up one character, so the TAGS table
3829 doesn't mention (and so depend upon) the following char. */
3830 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : varname
,
3831 FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3839 * Look for /^def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
3840 * Eric S. Raymond <esr@thyrsus.com> (1997)
3843 Python_functions (inf
)
3848 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3852 && *cp
++ == 'f' && iswhite (*cp
++))
3854 cp
= skip_spaces (cp
);
3855 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3858 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3866 && *cp
++ == 's' && iswhite (*cp
++))
3868 cp
= skip_spaces (cp
);
3869 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3872 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3878 /* Idea by Corny de Souza
3879 * Cobol tag functions
3880 * We could look for anything that could be a paragraph name.
3881 * i.e. anything that starts in column 8 is one word and ends in a full stop.
3884 Cobol_paragraphs (inf
)
3887 register char *bp
, *ep
;
3889 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
3895 /* If eoln, compiler option or comment ignore whole line. */
3896 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
3899 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
3902 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
3903 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
3910 * Idea by Assar Westerlund <assar@sics.se> (2001)
3913 Makefile_targets (inf
)
3918 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
3920 if (*bp
== '\t' || *bp
== '#')
3922 while (*bp
!= '\0' && *bp
!= '=' && *bp
!= ':')
3925 pfnote (savenstr (lb
.buffer
, bp
- lb
.buffer
), TRUE
,
3926 lb
.buffer
, bp
- lb
.buffer
+ 1, lineno
, linecharno
);
3931 /* Added by Mosur Mohan, 4/22/88 */
3932 /* Pascal parsing */
3935 * Locates tags for procedures & functions. Doesn't do any type- or
3936 * var-definitions. It does look for the keyword "extern" or
3937 * "forward" immediately following the procedure statement; if found,
3938 * the tag is skipped.
3941 Pascal_functions (inf
)
3944 linebuffer tline
; /* mostly copied from C_entries */
3946 int save_lineno
, save_len
;
3947 char c
, *cp
, *namebuf
;
3949 bool /* each of these flags is TRUE iff: */
3950 incomment
, /* point is inside a comment */
3951 inquote
, /* point is inside '..' string */
3952 get_tagname
, /* point is after PROCEDURE/FUNCTION
3953 keyword, so next item = potential tag */
3954 found_tag
, /* point is after a potential tag */
3955 inparms
, /* point is within parameter-list */
3956 verify_tag
; /* point has passed the parm-list, so the
3957 next token will determine whether this
3958 is a FORWARD/EXTERN to be ignored, or
3959 whether it is a real tag */
3961 save_lcno
= save_lineno
= save_len
= 0; /* keep compiler quiet */
3962 namebuf
= NULL
; /* keep compiler quiet */
3967 initbuffer (&tline
);
3969 incomment
= inquote
= FALSE
;
3970 found_tag
= FALSE
; /* have a proc name; check if extern */
3971 get_tagname
= FALSE
; /* have found "procedure" keyword */
3972 inparms
= FALSE
; /* found '(' after "proc" */
3973 verify_tag
= FALSE
; /* check if "extern" is ahead */
3976 while (!feof (inf
)) /* long main loop to get next char */
3979 if (c
== '\0') /* if end of line */
3982 linecharno
= charno
;
3983 charno
+= readline (&lb
, inf
);
3987 if (!((found_tag
&& verify_tag
)
3989 c
= *dbp
++; /* only if don't need *dbp pointing
3990 to the beginning of the name of
3991 the procedure or function */
3995 if (c
== '}') /* within { } comments */
3997 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
4014 inquote
= TRUE
; /* found first quote */
4016 case '{': /* found open { comment */
4020 if (*dbp
== '*') /* found open (* comment */
4025 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
4028 case ')': /* end of parms list */
4033 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
4040 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
4042 /* check if this is an "extern" declaration */
4045 if (lowcase (*dbp
== 'e'))
4047 if (tail ("extern")) /* superfluous, really! */
4053 else if (lowcase (*dbp
) == 'f')
4055 if (tail ("forward")) /* check for forward reference */
4061 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
4065 pfnote (namebuf
, TRUE
,
4066 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
4070 if (get_tagname
) /* grab name of proc or fn */
4075 /* save all values for later tagging */
4076 linebuffer_setlen (&tline
, lb
.len
);
4077 strcpy (tline
.buffer
, lb
.buffer
);
4078 save_lineno
= lineno
;
4079 save_lcno
= linecharno
;
4081 /* grab block name */
4082 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4084 namebuf
= savenstr (dbp
, cp
-dbp
);
4085 dbp
= cp
; /* set dbp to e-o-token */
4086 save_len
= dbp
- lb
.buffer
+ 1;
4087 get_tagname
= FALSE
;
4091 /* and proceed to check for "extern" */
4093 else if (!incomment
&& !inquote
&& !found_tag
)
4095 /* check for proc/fn keywords */
4096 switch (lowcase (c
))
4099 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
4103 if (tail ("unction"))
4108 } /* while not eof */
4110 free (tline
.buffer
);
4115 * Lisp tag functions
4116 * look for (def or (DEF, quote or QUOTE
4119 static int L_isdef
P_((char *));
4120 static int L_isquote
P_((char *));
4121 static void L_getit
P_((void));
4125 register char *strp
;
4127 return ((strp
[1] == 'd' || strp
[1] == 'D')
4128 && (strp
[2] == 'e' || strp
[2] == 'E')
4129 && (strp
[3] == 'f' || strp
[3] == 'F'));
4134 register char *strp
;
4136 return ((*++strp
== 'q' || *strp
== 'Q')
4137 && (*++strp
== 'u' || *strp
== 'U')
4138 && (*++strp
== 'o' || *strp
== 'O')
4139 && (*++strp
== 't' || *strp
== 'T')
4140 && (*++strp
== 'e' || *strp
== 'E')
4141 && iswhite (*++strp
));
4149 if (*dbp
== '\'') /* Skip prefix quote */
4151 else if (*dbp
== '(')
4153 if (L_isquote (dbp
))
4154 dbp
+= 7; /* Skip "(quote " */
4156 dbp
+= 1; /* Skip "(" before name in (defstruct (foo)) */
4157 dbp
= skip_spaces (dbp
);
4160 for (cp
= dbp
/*+1*/;
4161 *cp
!= '\0' && *cp
!= '(' && !iswhite(*cp
) && *cp
!= ')';
4167 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
4168 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4172 Lisp_functions (inf
)
4175 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4181 dbp
= skip_non_spaces (dbp
);
4182 dbp
= skip_spaces (dbp
);
4187 /* Check for (foo::defmumble name-defined ... */
4190 while (*dbp
!= '\0' && !iswhite (*dbp
)
4191 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
4196 while (*dbp
== ':');
4198 if (L_isdef (dbp
- 1))
4200 dbp
= skip_non_spaces (dbp
);
4201 dbp
= skip_spaces (dbp
);
4212 * Postscript tag functions
4213 * Just look for lines where the first character is '/'
4214 * Also look at "defineps" for PSWrap
4215 * Richard Mlynarik <mly@adoc.xerox.com> (1997)
4216 * Ideas by Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
4219 Postscript_functions (inf
)
4222 register char *bp
, *ep
;
4224 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4229 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4232 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
4233 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4235 else if (strneq (bp
, "defineps", 8))
4237 bp
= skip_non_spaces (bp
);
4238 bp
= skip_spaces (bp
);
4246 * Scheme tag functions
4247 * look for (def... xyzzy
4248 * look for (def... (xyzzy
4249 * look for (def ... ((...(xyzzy ....
4250 * look for (set! xyzzy
4254 Scheme_functions (inf
)
4259 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4262 && (bp
[1] == 'D' || bp
[1] == 'd')
4263 && (bp
[2] == 'E' || bp
[2] == 'e')
4264 && (bp
[3] == 'F' || bp
[3] == 'f'))
4266 bp
= skip_non_spaces (bp
);
4267 /* Skip over open parens and white space */
4268 while (iswhite (*bp
) || *bp
== '(')
4273 && (bp
[1] == 'S' || bp
[1] == 's')
4274 && (bp
[2] == 'E' || bp
[2] == 'e')
4275 && (bp
[3] == 'T' || bp
[3] == 't')
4276 && (bp
[4] == '!' || bp
[4] == '!')
4277 && (iswhite (bp
[5])))
4279 bp
= skip_non_spaces (bp
);
4280 bp
= skip_spaces (bp
);
4287 /* Find tags in TeX and LaTeX input files. */
4289 /* TEX_toktab is a table of TeX control sequences that define tags.
4290 Each TEX_tabent records one such control sequence.
4291 CONVERT THIS TO USE THE Stab TYPE!! */
4298 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
4300 /* Default set of control sequences to put into TEX_toktab.
4301 The value of environment var TEXTAGS is prepended to this. */
4303 char *TEX_defenv
= "\
4304 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4305 :part:appendix:entry:index";
4307 static void TEX_mode
P_((FILE *));
4308 static struct TEX_tabent
*TEX_decode_env
P_((char *, char *));
4309 static int TEX_Token
P_((char *));
4311 char TEX_esc
= '\\';
4312 char TEX_opgrp
= '{';
4313 char TEX_clgrp
= '}';
4316 * TeX/LaTeX scanning loop.
4325 /* Select either \ or ! as escape character. */
4328 /* Initialize token table once from environment. */
4330 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
4332 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4335 /* Look at each esc in line. */
4336 while ((cp
= etags_strchr (cp
, TEX_esc
)) != NULL
)
4340 linecharno
+= cp
- lasthit
;
4342 i
= TEX_Token (lasthit
);
4345 /* We seem to include the TeX command in the tag name.
4347 for (p = lasthit + TEX_toktab[i].len;
4348 *p != '\0' && *p != TEX_clgrp;
4351 pfnote (/*savenstr (lasthit, p-lasthit)*/ (char *)NULL
, TRUE
,
4352 lb
.buffer
, lb
.len
, lineno
, linecharno
);
4353 break; /* We only tag a line once */
4359 #define TEX_LESC '\\'
4360 #define TEX_SESC '!'
4363 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
4364 chars accordingly. */
4371 while ((c
= getc (inf
)) != EOF
)
4373 /* Skip to next line if we hit the TeX comment char. */
4377 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
4393 /* If the input file is compressed, inf is a pipe, and rewind may fail.
4394 No attempt is made to correct the situation. */
4398 /* Read environment and prepend it to the default string.
4399 Build token table. */
4400 static struct TEX_tabent
*
4401 TEX_decode_env (evarname
, defenv
)
4405 register char *env
, *p
;
4407 struct TEX_tabent
*tab
;
4410 /* Append default string to environment. */
4411 env
= getenv (evarname
);
4417 env
= concat (oldenv
, defenv
, "");
4420 /* Allocate a token table */
4421 for (size
= 1, p
= env
; p
;)
4422 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
4424 /* Add 1 to leave room for null terminator. */
4425 tab
= xnew (size
+ 1, struct TEX_tabent
);
4427 /* Unpack environment string into token table. Be careful about */
4428 /* zero-length strings (leading ':', "::" and trailing ':') */
4431 p
= etags_strchr (env
, ':');
4432 if (!p
) /* End of environment string. */
4433 p
= env
+ strlen (env
);
4435 { /* Only non-zero strings. */
4436 tab
[i
].name
= savenstr (env
, p
- env
);
4437 tab
[i
].len
= strlen (tab
[i
].name
);
4444 tab
[i
].name
= NULL
; /* Mark end of table. */
4452 /* If the text at CP matches one of the tag-defining TeX command names,
4453 return the pointer to the first occurrence of that command in TEX_toktab.
4454 Otherwise return -1.
4455 Keep the capital `T' in `token' for dumb truncating compilers
4456 (this distinguishes it from `TEX_toktab' */
4463 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
4464 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
4470 /* Texinfo support. Dave Love, Mar. 2000. */
4476 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4482 && *cp
++ == 'e' && iswhite (*cp
++)))
4484 start
= cp
= skip_spaces(cp
);
4485 while (*cp
!= '\0' && *cp
!= ',')
4487 pfnote (savenstr (start
, cp
- start
), TRUE
,
4488 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4495 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
4497 * Assumes that the predicate starts at column 0.
4498 * Only the first clause of a predicate is added.
4500 static int prolog_pred
P_((char *, char *));
4501 static void prolog_skip_comment
P_((linebuffer
*, FILE *));
4502 static int prolog_atom
P_((char *, int));
4505 Prolog_functions (inf
)
4516 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4518 if (cp
[0] == '\0') /* Empty line */
4520 else if (iswhite (cp
[0])) /* Not a predicate */
4522 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
4523 prolog_skip_comment (&lb
, inf
);
4524 else if ((len
= prolog_pred (cp
, last
)) > 0)
4526 /* Predicate. Store the function name so that we only
4527 generate a tag for the first clause. */
4529 last
= xnew(len
+ 1, char);
4530 else if (len
+ 1 > allocated
)
4531 xrnew (last
, len
+ 1, char);
4532 allocated
= len
+ 1;
4533 strncpy (last
, cp
, len
);
4541 prolog_skip_comment (plb
, inf
)
4549 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
4550 if (cp
[0] == '*' && cp
[1] == '/')
4553 linecharno
+= readline (plb
, inf
);
4559 * A predicate definition is added if it matches:
4560 * <beginning of line><Prolog Atom><whitespace>(
4562 * It is added to the tags database if it doesn't match the
4563 * name of the previous clause header.
4565 * Return the size of the name of the predicate, or 0 if no header
4569 prolog_pred (s
, last
)
4571 char *last
; /* Name of last clause. */
4576 pos
= prolog_atom (s
, 0);
4581 pos
= skip_spaces (s
+ pos
) - s
;
4583 if ((s
[pos
] == '(') || (s
[pos
] == '.'))
4588 /* Save only the first clause. */
4590 || len
!= (int)strlen (last
)
4591 || !strneq (s
, last
, len
))
4593 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4601 * Consume a Prolog atom.
4602 * Return the number of bytes consumed, or -1 if there was an error.
4604 * A prolog atom, in this context, could be one of:
4605 * - An alphanumeric sequence, starting with a lower case letter.
4606 * - A quoted arbitrary string. Single quotes can escape themselves.
4607 * Backslash quotes everything.
4610 prolog_atom (s
, pos
)
4618 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
4620 /* The atom is unquoted. */
4622 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
4626 return pos
- origpos
;
4628 else if (s
[pos
] == '\'')
4639 pos
++; /* A double quote */
4641 else if (s
[pos
] == '\0')
4642 /* Multiline quoted atoms are ignored. */
4644 else if (s
[pos
] == '\\')
4646 if (s
[pos
+1] == '\0')
4653 return pos
- origpos
;
4661 * Support for Erlang -- Anders Lindgren, Feb 1996.
4663 * Generates tags for functions, defines, and records.
4665 * Assumes that Erlang functions start at column 0.
4667 static int erlang_func
P_((char *, char *));
4668 static void erlang_attribute
P_((char *));
4669 static int erlang_atom
P_((char *, int));
4672 Erlang_functions (inf
)
4683 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4685 if (cp
[0] == '\0') /* Empty line */
4687 else if (iswhite (cp
[0])) /* Not function nor attribute */
4689 else if (cp
[0] == '%') /* comment */
4691 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
4693 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
4695 erlang_attribute (cp
);
4698 else if ((len
= erlang_func (cp
, last
)) > 0)
4701 * Function. Store the function name so that we only
4702 * generates a tag for the first clause.
4705 last
= xnew (len
+ 1, char);
4706 else if (len
+ 1 > allocated
)
4707 xrnew (last
, len
+ 1, char);
4708 allocated
= len
+ 1;
4709 strncpy (last
, cp
, len
);
4717 * A function definition is added if it matches:
4718 * <beginning of line><Erlang Atom><whitespace>(
4720 * It is added to the tags database if it doesn't match the
4721 * name of the previous clause header.
4723 * Return the size of the name of the function, or 0 if no function
4727 erlang_func (s
, last
)
4729 char *last
; /* Name of last clause. */
4734 pos
= erlang_atom (s
, 0);
4739 pos
= skip_spaces (s
+ pos
) - s
;
4741 /* Save only the first clause. */
4744 || len
!= (int)strlen (last
)
4745 || !strneq (s
, last
, len
)))
4747 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4756 * Handle attributes. Currently, tags are generated for defines
4759 * They are on the form:
4760 * -define(foo, bar).
4761 * -define(Foo(M, N), M+N).
4762 * -record(graph, {vtab = notable, cyclic = true}).
4765 erlang_attribute (s
)
4771 if (strneq (s
, "-define", 7) || strneq (s
, "-record", 7))
4773 pos
= skip_spaces (s
+ 7) - s
;
4774 if (s
[pos
++] == '(')
4776 pos
= skip_spaces (s
+ pos
) - s
;
4777 len
= erlang_atom (s
, pos
);
4779 pfnote (savenstr (& s
[pos
], len
), TRUE
,
4780 s
, pos
+ len
, lineno
, linecharno
);
4788 * Consume an Erlang atom (or variable).
4789 * Return the number of bytes consumed, or -1 if there was an error.
4792 erlang_atom (s
, pos
)
4800 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
4802 /* The atom is unquoted. */
4804 while (ISALNUM (s
[pos
]) || s
[pos
] == '_')
4806 return pos
- origpos
;
4808 else if (s
[pos
] == '\'')
4819 else if (s
[pos
] == '\0')
4820 /* Multiline quoted atoms are ignored. */
4822 else if (s
[pos
] == '\\')
4824 if (s
[pos
+1] == '\0')
4831 return pos
- origpos
;
4838 #ifdef ETAGS_REGEXPS
4840 static char *scan_separators
P_((char *));
4841 static void analyse_regex
P_((char *, bool));
4842 static void add_regex
P_((char *, bool, language
*));
4843 static char *substitute
P_((char *, char *, struct re_registers
*));
4845 /* Take a string like "/blah/" and turn it into "blah", making sure
4846 that the first and last characters are the same, and handling
4847 quoted separator characters. Actually, stops on the occurrence of
4848 an unquoted separator. Also turns "\t" into a Tab character.
4849 Returns pointer to terminating separator. Works in place. Null
4850 terminates name string. */
4852 scan_separators (name
)
4856 char *copyto
= name
;
4857 bool quoted
= FALSE
;
4859 for (++name
; *name
!= '\0'; ++name
)
4865 else if (*name
== sep
)
4869 /* Something else is quoted, so preserve the quote. */
4875 else if (*name
== '\\')
4877 else if (*name
== sep
)
4883 /* Terminate copied string. */
4888 /* Look at the argument of --regex or --no-regex and do the right
4889 thing. Same for each line of a regexp file. */
4891 analyse_regex (regex_arg
, ignore_case
)
4895 if (regex_arg
== NULL
)
4896 free_patterns (); /* --no-regex: remove existing regexps */
4898 /* A real --regexp option or a line in a regexp file. */
4899 switch (regex_arg
[0])
4901 /* Comments in regexp file or null arg to --regex. */
4907 /* Read a regex file. This is recursive and may result in a
4908 loop, which will stop when the file descriptors are exhausted. */
4912 linebuffer regexbuf
;
4913 char *regexfile
= regex_arg
+ 1;
4915 /* regexfile is a file containing regexps, one per line. */
4916 regexfp
= fopen (regexfile
, "r");
4917 if (regexfp
== NULL
)
4922 initbuffer (®exbuf
);
4923 while (readline_internal (®exbuf
, regexfp
) > 0)
4924 analyse_regex (regexbuf
.buffer
, ignore_case
);
4925 free (regexbuf
.buffer
);
4930 /* Regexp to be used for a specific language only. */
4934 char *lang_name
= regex_arg
+ 1;
4937 for (cp
= lang_name
; *cp
!= '}'; cp
++)
4940 error ("unterminated language name in regex: %s", regex_arg
);
4944 lang
= get_language_from_langname (lang_name
);
4947 add_regex (cp
+ 1, ignore_case
, lang
);
4951 /* Regexp to be used for any language. */
4953 add_regex (regex_arg
, ignore_case
, NULL
);
4958 /* Turn a name, which is an ed-style (but Emacs syntax) regular
4959 expression, into a real regular expression by compiling it. */
4961 add_regex (regexp_pattern
, ignore_case
, lang
)
4962 char *regexp_pattern
;
4968 struct re_pattern_buffer
*patbuf
;
4972 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
4974 error ("%s: unterminated regexp", regexp_pattern
);
4977 name
= scan_separators (regexp_pattern
);
4978 if (regexp_pattern
[0] == '\0')
4980 error ("null regexp", (char *)NULL
);
4983 (void) scan_separators (name
);
4985 patbuf
= xnew (1, struct re_pattern_buffer
);
4986 /* Translation table to fold case if appropriate. */
4987 patbuf
->translate
= (ignore_case
) ? lc_trans
: NULL
;
4988 patbuf
->fastmap
= NULL
;
4989 patbuf
->buffer
= NULL
;
4990 patbuf
->allocated
= 0;
4992 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
4995 error ("%s while compiling pattern", err
);
5000 p_head
= xnew (1, pattern
);
5001 p_head
->regex
= savestr (regexp_pattern
);
5002 p_head
->p_next
= pp
;
5003 p_head
->language
= lang
;
5004 p_head
->pattern
= patbuf
;
5005 p_head
->name_pattern
= savestr (name
);
5006 p_head
->error_signaled
= FALSE
;
5010 * Do the substitutions indicated by the regular expression and
5014 substitute (in
, out
, regs
)
5016 struct re_registers
*regs
;
5019 int size
, dig
, diglen
;
5022 size
= strlen (out
);
5024 /* Pass 1: figure out how much to allocate by finding all \N strings. */
5025 if (out
[size
- 1] == '\\')
5026 fatal ("pattern error in \"%s\"", out
);
5027 for (t
= etags_strchr (out
, '\\');
5029 t
= etags_strchr (t
+ 2, '\\'))
5033 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5039 /* Allocate space and do the substitutions. */
5040 result
= xnew (size
+ 1, char);
5042 for (t
= result
; *out
!= '\0'; out
++)
5043 if (*out
== '\\' && ISDIGIT (*++out
))
5046 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
5047 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
5054 assert (t
<= result
+ size
&& t
- result
== (int)strlen (result
));
5059 /* Deallocate all patterns. */
5064 while (p_head
!= NULL
)
5066 pp
= p_head
->p_next
;
5067 free (p_head
->regex
);
5068 free (p_head
->name_pattern
);
5084 /* Go till you get to white space or a syntactic break */
5086 *cp
!= '\0' && *cp
!= '(' && *cp
!= ')' && !iswhite (*cp
);
5089 pfnote (savenstr (bp
, cp
-bp
), TRUE
,
5090 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5093 #endif /* ETAGS_REGEXPS */
5094 /* Initialize a linebuffer for use */
5099 lbp
->size
= (DEBUG
) ? 3 : 200;
5100 lbp
->buffer
= xnew (lbp
->size
, char);
5101 lbp
->buffer
[0] = '\0';
5106 * Read a line of text from `stream' into `lbp', excluding the
5107 * newline or CR-NL, if any. Return the number of characters read from
5108 * `stream', which is the length of the line including the newline.
5110 * On DOS or Windows we do not count the CR character, if any, before the
5111 * NL, in the returned length; this mirrors the behavior of emacs on those
5112 * platforms (for text files, it translates CR-NL to NL as it reads in the
5116 readline_internal (lbp
, stream
)
5118 register FILE *stream
;
5120 char *buffer
= lbp
->buffer
;
5121 register char *p
= lbp
->buffer
;
5122 register char *pend
;
5125 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
5129 register int c
= getc (stream
);
5132 /* We're at the end of linebuffer: expand it. */
5134 xrnew (buffer
, lbp
->size
, char);
5135 p
+= buffer
- lbp
->buffer
;
5136 pend
= buffer
+ lbp
->size
;
5137 lbp
->buffer
= buffer
;
5147 if (p
> buffer
&& p
[-1] == '\r')
5151 /* Assume CRLF->LF translation will be performed by Emacs
5152 when loading this file, so CRs won't appear in the buffer.
5153 It would be cleaner to compensate within Emacs;
5154 however, Emacs does not know how many CRs were deleted
5155 before any given point in the file. */
5170 lbp
->len
= p
- buffer
;
5172 return lbp
->len
+ chars_deleted
;
5176 * Like readline_internal, above, but in addition try to match the
5177 * input line against relevant regular expressions.
5180 readline (lbp
, stream
)
5184 /* Read new line. */
5185 long result
= readline_internal (lbp
, stream
);
5186 #ifdef ETAGS_REGEXPS
5190 /* Match against relevant patterns. */
5192 for (pp
= p_head
; pp
!= NULL
; pp
= pp
->p_next
)
5194 /* Only use generic regexps or those for the current language. */
5195 if (pp
->language
!= NULL
&& pp
->language
!= curlang
)
5198 match
= re_match (pp
->pattern
, lbp
->buffer
, lbp
->len
, 0, &pp
->regs
);
5203 if (!pp
->error_signaled
)
5205 error ("error while matching \"%s\"", pp
->regex
);
5206 pp
->error_signaled
= TRUE
;
5213 /* Match occurred. Construct a tag. */
5214 if (pp
->name_pattern
[0] != '\0')
5216 /* Make a named tag. */
5217 char *name
= substitute (lbp
->buffer
,
5218 pp
->name_pattern
, &pp
->regs
);
5220 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
5224 /* Make an unnamed tag. */
5225 pfnote ((char *)NULL
, TRUE
,
5226 lbp
->buffer
, match
, lineno
, linecharno
);
5231 #endif /* ETAGS_REGEXPS */
5238 * Return a pointer to a space of size strlen(cp)+1 allocated
5239 * with xnew where the string CP has been copied.
5245 return savenstr (cp
, strlen (cp
));
5249 * Return a pointer to a space of size LEN+1 allocated with xnew where
5250 * the string CP has been copied for at most the first LEN characters.
5259 dp
= xnew (len
+ 1, char);
5260 strncpy (dp
, cp
, len
);
5266 * Return the ptr in sp at which the character c last
5267 * appears; NULL if not found
5269 * Identical to POSIX strrchr, included for portability.
5272 etags_strrchr (sp
, c
)
5273 register const char *sp
;
5276 register const char *r
;
5289 * Return the ptr in sp at which the character c first
5290 * appears; NULL if not found
5292 * Identical to POSIX strchr, included for portability.
5295 etags_strchr (sp
, c
)
5296 register const char *sp
;
5307 /* Skip spaces, return new pointer. */
5312 while (iswhite (*cp
))
5317 /* Skip non spaces, return new pointer. */
5319 skip_non_spaces (cp
)
5322 while (*cp
!= '\0' && !iswhite (*cp
))
5327 /* Print error message and exit. */
5345 suggest_asking_for_help ()
5347 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
5358 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
5361 const char *s1
, *s2
;
5363 fprintf (stderr
, "%s: ", progname
);
5364 fprintf (stderr
, s1
, s2
);
5365 fprintf (stderr
, "\n");
5368 /* Return a newly-allocated string whose contents
5369 concatenate those of s1, s2, s3. */
5374 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
5375 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
5377 strcpy (result
, s1
);
5378 strcpy (result
+ len1
, s2
);
5379 strcpy (result
+ len1
+ len2
, s3
);
5380 result
[len1
+ len2
+ len3
] = '\0';
5386 /* Does the same work as the system V getcwd, but does not need to
5387 guess the buffer size in advance. */
5393 char *path
= xnew (bufsize
, char);
5395 while (getcwd (path
, bufsize
) == NULL
)
5397 if (errno
!= ERANGE
)
5401 path
= xnew (bufsize
, char);
5404 canonicalize_filename (path
);
5407 #else /* not HAVE_GETCWD */
5410 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
5414 for (p
= path
; *p
!= '\0'; p
++)
5420 return strdup (path
);
5421 #else /* not MSDOS */
5426 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
5427 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
5432 #endif /* not MSDOS */
5433 #endif /* not HAVE_GETCWD */
5436 /* Return a newly allocated string containing the file name of FILE
5437 relative to the absolute directory DIR (which should end with a slash). */
5439 relative_filename (file
, dir
)
5442 char *fp
, *dp
, *afn
, *res
;
5445 /* Find the common root of file and dir (with a trailing slash). */
5446 afn
= absolute_filename (file
, cwd
);
5449 while (*fp
++ == *dp
++)
5451 fp
--, dp
--; /* back to the first differing char */
5453 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
5456 do /* look at the equal chars until '/' */
5460 /* Build a sequence of "../" strings for the resulting relative file name. */
5462 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
5464 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
5467 strcat (res
, "../");
5469 /* Add the file name relative to the common root of file and dir. */
5470 strcat (res
, fp
+ 1);
5476 /* Return a newly allocated string containing the absolute file name
5477 of FILE given DIR (which should end with a slash). */
5479 absolute_filename (file
, dir
)
5482 char *slashp
, *cp
, *res
;
5484 if (filename_is_absolute (file
))
5485 res
= savestr (file
);
5487 /* We don't support non-absolute file names with a drive
5488 letter, like `d:NAME' (it's too much hassle). */
5489 else if (file
[1] == ':')
5490 fatal ("%s: relative file names with drive letters not supported", file
);
5493 res
= concat (dir
, file
, "");
5495 /* Delete the "/dirname/.." and "/." substrings. */
5496 slashp
= etags_strchr (res
, '/');
5497 while (slashp
!= NULL
&& slashp
[0] != '\0')
5499 if (slashp
[1] == '.')
5501 if (slashp
[2] == '.'
5502 && (slashp
[3] == '/' || slashp
[3] == '\0'))
5507 while (cp
>= res
&& !filename_is_absolute (cp
));
5509 cp
= slashp
; /* the absolute name begins with "/.." */
5511 /* Under MSDOS and NT we get `d:/NAME' as absolute
5512 file name, so the luser could say `d:/../NAME'.
5513 We silently treat this as `d:/NAME'. */
5514 else if (cp
[0] != '/')
5517 strcpy (cp
, slashp
+ 3);
5521 else if (slashp
[2] == '/' || slashp
[2] == '\0')
5523 strcpy (slashp
, slashp
+ 2);
5528 slashp
= etags_strchr (slashp
+ 1, '/');
5532 return savestr ("/");
5537 /* Return a newly allocated string containing the absolute
5538 file name of dir where FILE resides given DIR (which should
5539 end with a slash). */
5541 absolute_dirname (file
, dir
)
5547 canonicalize_filename (file
);
5548 slashp
= etags_strrchr (file
, '/');
5550 return savestr (dir
);
5553 res
= absolute_filename (file
, dir
);
5559 /* Whether the argument string is an absolute file name. The argument
5560 string must have been canonicalized with canonicalize_filename. */
5562 filename_is_absolute (fn
)
5565 return (fn
[0] == '/'
5567 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
5572 /* Translate backslashes into slashes. Works in place. */
5574 canonicalize_filename (fn
)
5578 /* Canonicalize drive letter case. */
5579 if (fn
[0] != '\0' && fn
[1] == ':' && ISLOWER (fn
[0]))
5580 fn
[0] = upcase (fn
[0]);
5581 /* Convert backslashes to slashes. */
5582 for (; *fn
!= '\0'; fn
++)
5587 fn
= NULL
; /* shut up the compiler */
5591 /* Set the minimum size of a string contained in a linebuffer. */
5593 linebuffer_setlen (lbp
, toksize
)
5597 while (lbp
->size
<= toksize
)
5600 xrnew (lbp
->buffer
, lbp
->size
, char);
5605 /* Like malloc but get fatal error if memory is exhausted. */
5610 long *result
= (long *) malloc (size
);
5612 fatal ("virtual memory exhausted", (char *)NULL
);
5617 xrealloc (ptr
, size
)
5621 long *result
= (long *) realloc (ptr
, size
);
5623 fatal ("virtual memory exhausted", (char *)NULL
);