1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 87, 88, 89, 93, 94, 95, 98, 99, 2000
3 Free Software Foundation, Inc. and Ken Arnold
5 This file is not considered part of GNU Emacs.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 * Ctags originally by Ken Arnold.
24 * Fortran added by Jim Kleckner.
25 * Ed Pelegri-Llopart added C typedefs.
26 * Gnu Emacs TAGS format and modifications by RMS?
27 * Sam Kendall added C++.
28 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
29 * Regexp tags by Tom Tromey.
31 * Francesco Potorti` (pot@gnu.org) is the current maintainer.
34 char pot_etags_version
[] = "@(#) pot revision number is 13.44";
43 #if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
44 # define P_(proto) proto
51 /* On some systems, Emacs defines static as nothing for the sake
52 of unexec. We don't want that here since we don't use unexec. */
54 # define ETAGS_REGEXPS /* use the regexp features */
55 # define LONG_OPTIONS /* accept long options */
56 #endif /* HAVE_CONFIG_H */
59 # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
66 # include <sys/param.h>
68 # ifndef HAVE_CONFIG_H
70 # include <sys/config.h>
82 # define MAXPATHLEN _MAX_PATH
87 # endif /* not HAVE_CONFIG_H */
90 # endif /* undef HAVE_GETCWD */
91 #else /* !WINDOWSNT */
96 extern char *getenv ();
98 #endif /* !WINDOWSNT */
103 # if defined (HAVE_GETCWD) && !WINDOWSNT
104 extern char *getcwd (char *buf
, size_t size
);
106 #endif /* HAVE_UNISTD_H */
114 #include <sys/types.h>
115 #include <sys/stat.h>
117 #if !defined (S_ISREG) && defined (S_IFREG)
118 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
124 # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
126 extern int optind
, opterr
;
127 #endif /* LONG_OPTIONS */
131 #endif /* ETAGS_REGEXPS */
133 /* Define CTAGS to make the program "ctags" compatible with the usual one.
134 Leave it undefined to make the program "etags", which makes emacs-style
135 tag tables and tags typedefs, #defines and struct/union/enum by default. */
143 /* Exit codes for success and failure. */
153 #define C_PLPL 0x00001 /* C++ */
154 #define C_STAR 0x00003 /* C* */
155 #define C_JAVA 0x00005 /* JAVA */
156 #define YACC 0x10000 /* yacc file */
158 #define streq(s,t) ((DEBUG && (s) == NULL && (t) == NULL \
159 && (abort (), 1)) || !strcmp (s, t))
160 #define strneq(s,t,n) ((DEBUG && (s) == NULL && (t) == NULL \
161 && (abort (), 1)) || !strncmp (s, t, n))
163 #define lowcase(c) tolower ((unsigned char)(c))
164 #define UPCASE(c) toupper ((unsigned char)(c))
166 #define CHARS 256 /* 2^sizeof(char) */
167 #define CHAR(x) ((unsigned int)x & (CHARS - 1))
168 #define iswhite(c) (_wht[CHAR(c)]) /* c is white */
169 #define notinname(c) (_nin[CHAR(c)]) /* c is not in a name */
170 #define begtoken(c) (_btk[CHAR(c)]) /* c can start token */
171 #define intoken(c) (_itk[CHAR(c)]) /* c can be in token */
172 #define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens */
174 #define ISALNUM(c) isalnum ((unsigned char) (c))
175 #define ISALPHA(c) isalpha ((unsigned char) (c))
176 #define ISDIGIT(c) isdigit ((unsigned char) (c))
177 #define ISLOWER(c) islower ((unsigned char) (c))
181 * xnew, xrnew -- allocate, reallocate storage
183 * SYNOPSIS: Type *xnew (int n, Type);
184 * Type *xrnew (OldPointer, int n, Type);
187 # include "chkmalloc.h"
188 # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
189 (n) * sizeof (Type)))
190 # define xrnew(op,n,Type) ((Type *) trace_realloc (__FILE__, __LINE__, \
191 (op), (n) * sizeof (Type)))
193 # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
194 # define xrnew(op,n,Type) ((Type *) xrealloc ((op), (n) * sizeof (Type)))
199 typedef void Lang_function
P_((FILE *));
204 char *command
; /* Takes one arg and decompresses to stdout */
210 Lang_function
*function
;
215 typedef struct node_st
216 { /* sorting structure */
217 char *name
; /* function or type name */
218 char *file
; /* file name */
219 bool is_func
; /* use pattern or line no */
220 bool been_warned
; /* set if noticed dup */
221 int lno
; /* line number tag is on */
222 long cno
; /* character number line starts on */
223 char *pat
; /* search pattern */
224 struct node_st
*left
, *right
; /* left and right sons */
228 * A `linebuffer' is a structure which holds a line of text.
229 * `readline_internal' reads a line from a stream into a linebuffer
230 * and works regardless of the length of the line.
231 * SIZE is the size of BUFFER, LEN is the length of the string in
232 * BUFFER after readline reads it.
241 /* Many compilers barf on this:
242 Lang_function Ada_funcs;
243 so let's write it this way */
244 static void Ada_funcs
P_((FILE *));
245 static void Asm_labels
P_((FILE *));
246 static void C_entries
P_((int c_ext
, FILE *));
247 static void default_C_entries
P_((FILE *));
248 static void plain_C_entries
P_((FILE *));
249 static void Cjava_entries
P_((FILE *));
250 static void Cobol_paragraphs
P_((FILE *));
251 static void Cplusplus_entries
P_((FILE *));
252 static void Cstar_entries
P_((FILE *));
253 static void Erlang_functions
P_((FILE *));
254 static void Fortran_functions
P_((FILE *));
255 static void Yacc_entries
P_((FILE *));
256 static void Lisp_functions
P_((FILE *));
257 static void Pascal_functions
P_((FILE *));
258 static void Perl_functions
P_((FILE *));
259 static void Postscript_functions
P_((FILE *));
260 static void Prolog_functions
P_((FILE *));
261 static void Python_functions
P_((FILE *));
262 static void Scheme_functions
P_((FILE *));
263 static void TeX_functions
P_((FILE *));
264 static void Texinfo_functions
P_ ((FILE *));
265 static void just_read_file
P_((FILE *));
267 static void print_language_names
P_((void));
268 static void print_version
P_((void));
269 static void print_help
P_((void));
270 int main
P_((int, char **));
271 static int number_len
P_((long));
273 static compressor
*get_compressor_from_suffix
P_((char *, char **));
274 static language
*get_language_from_name
P_((char *));
275 static language
*get_language_from_interpreter
P_((char *));
276 static language
*get_language_from_suffix
P_((char *));
277 static int total_size_of_entries
P_((node
*));
278 static long readline
P_((linebuffer
*, FILE *));
279 static long readline_internal
P_((linebuffer
*, FILE *));
280 static void get_tag
P_((char *));
283 static void analyse_regex
P_((char *, bool));
284 static void add_regex
P_((char *, bool, language
*));
285 static void free_patterns
P_((void));
286 #endif /* ETAGS_REGEXPS */
287 static void error
P_((const char *, const char *));
288 static void suggest_asking_for_help
P_((void));
289 static void fatal
P_((char *, char *));
290 static void pfatal
P_((char *));
291 static void add_node
P_((node
*, node
**));
293 static void init
P_((void));
294 static void initbuffer
P_((linebuffer
*));
295 static void find_entries
P_((char *, FILE *));
296 static void free_tree
P_((node
*));
297 static void pfnote
P_((char *, bool, char *, int, int, long));
298 static void new_pfnote
P_((char *, int, bool, char *, int, int, long));
299 static void process_file
P_((char *));
300 static void put_entries
P_((node
*));
301 static void takeprec
P_((void));
303 static char *concat
P_((char *, char *, char *));
304 static char *skip_spaces
P_((char *));
305 static char *skip_non_spaces
P_((char *));
306 static char *savenstr
P_((char *, int));
307 static char *savestr
P_((char *));
308 static char *etags_strchr
P_((const char *, int));
309 static char *etags_strrchr
P_((const char *, int));
310 static char *etags_getcwd
P_((void));
311 static char *relative_filename
P_((char *, char *));
312 static char *absolute_filename
P_((char *, char *));
313 static char *absolute_dirname
P_((char *, char *));
314 static bool filename_is_absolute
P_((char *f
));
315 static void canonicalize_filename
P_((char *));
316 static void grow_linebuffer
P_((linebuffer
*, int));
317 long *xmalloc
P_((unsigned int));
318 long *xrealloc
P_((char *, unsigned int));
321 char searchar
= '/'; /* use /.../ searches */
323 char *tagfile
; /* output file */
324 char *progname
; /* name this program was invoked with */
325 char *cwd
; /* current working directory */
326 char *tagfiledir
; /* directory of tagfile */
327 FILE *tagf
; /* ioptr for tags file */
329 char *curfile
; /* current input file name */
330 language
*curlang
; /* current language */
332 int lineno
; /* line number of current line */
333 long charno
; /* current character number */
334 long linecharno
; /* charno of start of current line */
335 char *dbp
; /* pointer to start of current tag */
337 node
*head
; /* the head of the binary tree of tags */
339 linebuffer lb
; /* the current line */
340 linebuffer token_name
; /* used by C_entries as a temporary area */
344 linebuffer lb
; /* used by C_entries instead of lb */
347 /* boolean "functions" (see init) */
348 bool _wht
[CHARS
], _nin
[CHARS
], _itk
[CHARS
], _btk
[CHARS
], _etk
[CHARS
];
351 *white
= " \f\t\n\r\v",
353 *nonam
= " \f\t\n\r(=,[;",
354 /* token ending chars */
355 *endtk
= " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
356 /* token starting chars */
357 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
358 /* valid in-token chars */
359 *midtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
361 bool append_to_tagfile
; /* -a: append to tags */
362 /* The following four default to TRUE for etags, but to FALSE for ctags. */
363 bool typedefs
; /* -t: create tags for C and Ada typedefs */
364 bool typedefs_and_cplusplus
; /* -T: create tags for C typedefs, level */
365 /* 0 struct/enum/union decls, and C++ */
366 /* member functions. */
367 bool constantypedefs
; /* -d: create tags for C #define, enum */
368 /* constants and variables. */
369 /* -D: opposite of -d. Default under ctags. */
370 bool declarations
; /* --declarations: tag them and extern in C&Co*/
371 bool globals
; /* create tags for global variables */
372 bool members
; /* create tags for C member variables */
373 bool update
; /* -u: update tags */
374 bool vgrind_style
; /* -v: create vgrind style index output */
375 bool no_warnings
; /* -w: suppress warnings */
376 bool cxref_style
; /* -x: create cxref style output */
377 bool cplusplus
; /* .[hc] means C++, not C */
378 bool noindentypedefs
; /* -I: ignore indentation in C */
379 bool packages_only
; /* --packages-only: in Ada, only tag packages*/
382 struct option longopts
[] =
384 { "packages-only", no_argument
, &packages_only
, TRUE
},
385 { "append", no_argument
, NULL
, 'a' },
386 { "backward-search", no_argument
, NULL
, 'B' },
387 { "c++", no_argument
, NULL
, 'C' },
388 { "cxref", no_argument
, NULL
, 'x' },
389 { "defines", no_argument
, NULL
, 'd' },
390 { "declarations", no_argument
, &declarations
, TRUE
},
391 { "no-defines", no_argument
, NULL
, 'D' },
392 { "globals", no_argument
, &globals
, TRUE
},
393 { "no-globals", no_argument
, &globals
, FALSE
},
394 { "help", no_argument
, NULL
, 'h' },
395 { "help", no_argument
, NULL
, 'H' },
396 { "ignore-indentation", no_argument
, NULL
, 'I' },
397 { "include", required_argument
, NULL
, 'i' },
398 { "language", required_argument
, NULL
, 'l' },
399 { "members", no_argument
, &members
, TRUE
},
400 { "no-members", no_argument
, &members
, FALSE
},
401 { "no-warn", no_argument
, NULL
, 'w' },
402 { "output", required_argument
, NULL
, 'o' },
404 { "regex", required_argument
, NULL
, 'r' },
405 { "no-regex", no_argument
, NULL
, 'R' },
406 { "ignore-case-regex", required_argument
, NULL
, 'c' },
407 #endif /* ETAGS_REGEXPS */
408 { "typedefs", no_argument
, NULL
, 't' },
409 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
410 { "update", no_argument
, NULL
, 'u' },
411 { "version", no_argument
, NULL
, 'V' },
412 { "vgrind", no_argument
, NULL
, 'v' },
415 #endif /* LONG_OPTIONS */
418 /* Structure defining a regular expression. Elements are
419 the compiled pattern, and the name string. */
420 typedef struct pattern
422 struct pattern
*p_next
;
425 struct re_pattern_buffer
*pattern
;
426 struct re_registers regs
;
431 /* List of all regexps. */
432 pattern
*p_head
= NULL
;
434 /* How many characters in the character set. (From regex.c.) */
435 #define CHAR_SET_SIZE 256
436 /* Translation table for case-insensitive matching. */
437 char lc_trans
[CHAR_SET_SIZE
];
438 #endif /* ETAGS_REGEXPS */
440 compressor compressors
[] =
442 { "z", "gzip -d -c"},
443 { "Z", "gzip -d -c"},
444 { "gz", "gzip -d -c"},
445 { "GZ", "gzip -d -c"},
446 { "bz2", "bzip2 -d -c" },
454 /* Non-NULL if language fixed. */
455 language
*forced_lang
= NULL
;
458 char *Ada_suffixes
[] =
459 { "ads", "adb", "ada", NULL
};
462 char *Asm_suffixes
[] = { "a", /* Unix assembler */
463 "asm", /* Microcontroller assembly */
464 "def", /* BSO/Tasking definition includes */
465 "inc", /* Microcontroller include files */
466 "ins", /* Microcontroller include files */
467 "s", "sa", /* Unix assembler */
468 "S", /* cpp-processed Unix assembler */
469 "src", /* BSO/Tasking C compiler output */
473 /* Note that .c and .h can be considered C++, if the --c++ flag was
474 given. That is why default_C_entries is called here. */
475 char *default_C_suffixes
[] =
478 char *Cplusplus_suffixes
[] =
479 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx",
480 "M", /* Objective C++ */
481 "pdb", /* Postscript with C syntax */
484 char *Cjava_suffixes
[] =
487 char *Cobol_suffixes
[] =
488 { "COB", "cob", NULL
};
490 char *Cstar_suffixes
[] =
491 { "cs", "hs", NULL
};
493 char *Erlang_suffixes
[] =
494 { "erl", "hrl", NULL
};
496 char *Fortran_suffixes
[] =
497 { "F", "f", "f90", "for", NULL
};
499 char *Lisp_suffixes
[] =
500 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", "LSP", NULL
};
502 char *Pascal_suffixes
[] =
503 { "p", "pas", NULL
};
505 char *Perl_suffixes
[] =
506 { "pl", "pm", NULL
};
507 char *Perl_interpreters
[] =
508 { "perl", "@PERL@", NULL
};
510 char *plain_C_suffixes
[] =
511 { "pc", /* Pro*C file */
512 "m", /* Objective C file */
513 "lm", /* Objective lex file */
516 char *Postscript_suffixes
[] =
517 { "ps", "psw", NULL
}; /* .psw is for PSWrap */
519 char *Prolog_suffixes
[] =
522 char *Python_suffixes
[] =
525 /* Can't do the `SCM' or `scm' prefix with a version number. */
526 char *Scheme_suffixes
[] =
527 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "ss", "t", NULL
};
529 char *TeX_suffixes
[] =
530 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL
};
532 char *Texinfo_suffixes
[] =
533 { "texi", "txi", "texinfo", NULL
};
535 char *Yacc_suffixes
[] =
536 { "y", "ym", "yy", "yxx", "y++", NULL
}; /* .ym is Objective yacc file */
539 * Table of languages.
541 * It is ok for a given function to be listed under more than one
542 * name. I just didn't.
545 language lang_names
[] =
547 { "ada", Ada_funcs
, Ada_suffixes
, NULL
},
548 { "asm", Asm_labels
, Asm_suffixes
, NULL
},
549 { "c", default_C_entries
, default_C_suffixes
, NULL
},
550 { "c++", Cplusplus_entries
, Cplusplus_suffixes
, NULL
},
551 { "c*", Cstar_entries
, Cstar_suffixes
, NULL
},
552 { "cobol", Cobol_paragraphs
, Cobol_suffixes
, NULL
},
553 { "erlang", Erlang_functions
, Erlang_suffixes
, NULL
},
554 { "fortran", Fortran_functions
, Fortran_suffixes
, NULL
},
555 { "java", Cjava_entries
, Cjava_suffixes
, NULL
},
556 { "lisp", Lisp_functions
, Lisp_suffixes
, NULL
},
557 { "pascal", Pascal_functions
, Pascal_suffixes
, NULL
},
558 { "perl", Perl_functions
, Perl_suffixes
, Perl_interpreters
},
559 { "postscript", Postscript_functions
, Postscript_suffixes
, NULL
},
560 { "proc", plain_C_entries
, plain_C_suffixes
, NULL
},
561 { "prolog", Prolog_functions
, Prolog_suffixes
, NULL
},
562 { "python", Python_functions
, Python_suffixes
, NULL
},
563 { "scheme", Scheme_functions
, Scheme_suffixes
, NULL
},
564 { "tex", TeX_functions
, TeX_suffixes
, NULL
},
565 { "texinfo", Texinfo_functions
, Texinfo_suffixes
, NULL
},
566 { "yacc", Yacc_entries
, Yacc_suffixes
, NULL
},
567 { "auto", NULL
}, /* default guessing scheme */
568 { "none", just_read_file
}, /* regexp matching only */
569 { NULL
, NULL
} /* end of list */
573 print_language_names ()
578 puts ("\nThese are the currently supported languages, along with the\n\
579 default file name suffixes:");
580 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
582 printf ("\t%s\t", lang
->name
);
583 if (lang
->suffixes
!= NULL
)
584 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
585 printf (" .%s", *ext
);
588 puts ("Where `auto' means use default language for files based on file\n\
589 name suffix, and `none' means only do regexp processing on files.\n\
590 If no language is specified and no matching suffix is found,\n\
591 the first line of the file is read for a sharp-bang (#!) sequence\n\
592 followed by the name of an interpreter. If no such sequence is found,\n\
593 Fortran is tried first; if no tags are found, C is tried next.\n\
594 Compressed files are supported using gzip and bzip2.");
598 # define EMACS_NAME "GNU Emacs"
601 # define VERSION "21"
606 printf ("%s (%s %s)\n", (CTAGS
) ? "ctags" : "etags", EMACS_NAME
, VERSION
);
607 puts ("Copyright (C) 1999 Free Software Foundation, Inc. and Ken Arnold");
608 puts ("This program is distributed under the same terms as Emacs");
616 printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
618 These are the options accepted by %s.\n", progname
, progname
);
620 puts ("You may use unambiguous abbreviations for the long option names.");
622 puts ("Long option names do not work with this executable, as it is not\n\
623 linked with GNU getopt.");
624 #endif /* LONG_OPTIONS */
625 puts ("A - as file name means read names from stdin (one per line).");
627 printf (" Absolute names are stored in the output file as they are.\n\
628 Relative ones are stored relative to the output file's directory.");
631 puts ("-a, --append\n\
632 Append tag entries to existing tags file.");
634 puts ("--packages-only\n\
635 For Ada files, only generate tags for packages .");
638 puts ("-B, --backward-search\n\
639 Write the search commands for the tag entries using '?', the\n\
640 backward-search command instead of '/', the forward-search command.");
643 Treat files whose name suffix defaults to C language as C++ files.");
645 puts ("--declarations\n\
646 In C and derived languages, create tags for function declarations,");
648 puts ("\tand create tags for extern variables if --globals is used.");
651 ("\tand create tags for extern variables unless --no-globals is used.");
654 puts ("-d, --defines\n\
655 Create tag entries for C #define constants and enum constants, too.");
657 puts ("-D, --no-defines\n\
658 Don't create tag entries for C #define constants and enum constants.\n\
659 This makes the tags file smaller.");
663 puts ("-i FILE, --include=FILE\n\
664 Include a note in tag file indicating that, when searching for\n\
665 a tag, one should also consult the tags file FILE after\n\
666 checking the current file.");
667 puts ("-l LANG, --language=LANG\n\
668 Force the following files to be considered as written in the\n\
669 named language up to the next --language=LANG option.");
674 Create tag entries for global variables in some languages.");
676 puts ("--no-globals\n\
677 Do not create tag entries for global variables in some\n\
678 languages. This makes the tags file smaller.");
680 Create tag entries for member variables in C and derived languages.");
683 puts ("-r /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
684 Make a tag for each line matching pattern REGEXP in the following\n\
685 files. {LANGUAGE}/REGEXP/ uses REGEXP for LANGUAGE files only.\n\
686 regexfile is a file containing one REGEXP per line.\n\
687 REGEXP is anchored (as if preceded by ^).\n\
688 The form /REGEXP/NAME/ creates a named tag.\n\
689 For example Tcl named tags can be created with:\n\
690 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
691 puts ("-c /REGEXP/, --ignore-case-regex=/REGEXP/ or --ignore-case-regex=@regexfile\n\
692 Like -r, --regex but ignore case when matching expressions.");
693 puts ("-R, --no-regex\n\
694 Don't create tags from regexps for the following files.");
695 #endif /* ETAGS_REGEXPS */
696 puts ("-o FILE, --output=FILE\n\
697 Write the tags to FILE.");
698 puts ("-I, --ignore-indentation\n\
699 Don't rely on indentation quite as much as normal. Currently,\n\
700 this means not to assume that a closing brace in the first\n\
701 column is the final brace of a function or structure\n\
702 definition in C and C++.");
706 puts ("-t, --typedefs\n\
707 Generate tag entries for C and Ada typedefs.");
708 puts ("-T, --typedefs-and-c++\n\
709 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
710 and C++ member functions.");
711 puts ("-u, --update\n\
712 Update the tag entries for the given files, leaving tag\n\
713 entries for other files in place. Currently, this is\n\
714 implemented by deleting the existing entries for the given\n\
715 files and then rewriting the new entries at the end of the\n\
716 tags file. It is often faster to simply rebuild the entire\n\
717 tag file than to use this.");
718 puts ("-v, --vgrind\n\
719 Generates an index of items intended for human consumption,\n\
720 similar to the output of vgrind. The index is sorted, and\n\
721 gives the page number of each item.");
722 puts ("-w, --no-warn\n\
723 Suppress warning messages about entries defined in multiple\n\
725 puts ("-x, --cxref\n\
726 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
727 The output uses line numbers instead of page numbers, but\n\
728 beyond that the differences are cosmetic; try both to see\n\
732 puts ("-V, --version\n\
733 Print the version of the program.\n\
735 Print this help message.");
737 print_language_names ();
740 puts ("Report bugs to bug-gnu-emacs@gnu.org");
754 /* This structure helps us allow mixing of --lang and file names. */
757 enum argument_type arg_type
;
759 language
*lang
; /* language of the regexp */
762 #ifdef VMS /* VMS specific functions */
766 /* This is a BUG! ANY arbitrary limit is a BUG!
767 Won't someone please fix this? */
768 #define MAX_FILE_SPEC_LEN 255
771 char body
[MAX_FILE_SPEC_LEN
+ 1];
775 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
776 returning in each successive call the next file name matching the input
777 spec. The function expects that each in_spec passed
778 to it will be processed to completion; in particular, up to and
779 including the call following that in which the last matching name
780 is returned, the function ignores the value of in_spec, and will
781 only start processing a new spec with the following call.
782 If an error occurs, on return out_spec contains the value
783 of in_spec when the error occurred.
785 With each successive file name returned in out_spec, the
786 function's return value is one. When there are no more matching
787 names the function returns zero. If on the first call no file
788 matches in_spec, or there is any other error, -1 is returned.
793 #define OUTSIZE MAX_FILE_SPEC_LEN
799 static long context
= 0;
800 static struct dsc$descriptor_s o
;
801 static struct dsc$descriptor_s i
;
802 static bool pass1
= TRUE
;
809 o
.dsc$a_pointer
= (char *) out
;
810 o
.dsc$w_length
= (short)OUTSIZE
;
811 i
.dsc$a_pointer
= in
;
812 i
.dsc$w_length
= (short)strlen(in
);
813 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
814 i
.dsc$b_class
= DSC$K_CLASS_S
;
815 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
816 o
.dsc$b_class
= DSC$K_CLASS_VS
;
818 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
820 out
->body
[out
->curlen
] = EOS
;
823 else if (status
== RMS$_NMF
)
827 strcpy(out
->body
, in
);
830 lib$
find_file_end(&context
);
836 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
837 name of each file specified by the provided arg expanding wildcards.
840 gfnames (arg
, p_error
)
844 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
846 switch (fn_exp (&filename
, arg
))
850 return filename
.body
;
856 return filename
.body
;
860 #ifndef OLD /* Newer versions of VMS do provide `system'. */
864 error ("%s", "system() function not implemented under VMS");
868 #define VERSION_DELIM ';'
869 char *massage_name (s
)
875 if (*s
== VERSION_DELIM
)
893 unsigned int nincluded_files
;
894 char **included_files
;
897 int current_arg
, file_count
;
898 linebuffer filename_lb
;
904 _fmode
= O_BINARY
; /* all of files are treated as binary files */
909 included_files
= xnew (argc
, char *);
913 /* Allocate enough no matter what happens. Overkill, but each one
915 argbuffer
= xnew (argc
, argument
);
918 /* Set syntax for regular expression routines. */
919 re_set_syntax (RE_SYNTAX_EMACS
| RE_INTERVALS
);
920 /* Translation table for case-insensitive search. */
921 for (i
= 0; i
< CHAR_SET_SIZE
; i
++)
922 lc_trans
[i
] = lowcase (i
);
923 #endif /* ETAGS_REGEXPS */
926 * If etags, always find typedefs and structure tags. Why not?
927 * Also default is to find macro constants, enum constants and
932 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
943 optstring
= "-aCdDf:Il:o:r:c:RStTi:BuvxwVhH";
945 optstring
= "-aCdDf:Il:o:StTi:BuvxwVhH";
946 #endif /* ETAGS_REGEXPS */
949 optstring
= optstring
+ 1;
950 #endif /* LONG_OPTIONS */
952 opt
= getopt_long (argc
, argv
, optstring
, longopts
, 0);
959 /* If getopt returns 0, then it has already processed a
960 long-named option. We should do nothing. */
964 /* This means that a file name has been seen. Record it. */
965 argbuffer
[current_arg
].arg_type
= at_filename
;
966 argbuffer
[current_arg
].what
= optarg
;
971 /* Common options. */
972 case 'a': append_to_tagfile
= TRUE
; break;
973 case 'C': cplusplus
= TRUE
; break;
974 case 'd': constantypedefs
= TRUE
; break;
975 case 'D': constantypedefs
= FALSE
; break;
976 case 'f': /* for compatibility with old makefiles */
980 error ("-o option may only be given once.", (char *)NULL
);
981 suggest_asking_for_help ();
986 case 'S': /* for backward compatibility */
987 noindentypedefs
= TRUE
;
991 language
*lang
= get_language_from_name (optarg
);
994 argbuffer
[current_arg
].lang
= lang
;
995 argbuffer
[current_arg
].arg_type
= at_language
;
1000 #ifdef ETAGS_REGEXPS
1002 argbuffer
[current_arg
].arg_type
= at_regexp
;
1003 argbuffer
[current_arg
].what
= optarg
;
1007 argbuffer
[current_arg
].arg_type
= at_regexp
;
1008 argbuffer
[current_arg
].what
= NULL
;
1012 argbuffer
[current_arg
].arg_type
= at_icregexp
;
1013 argbuffer
[current_arg
].what
= optarg
;
1016 #endif /* ETAGS_REGEXPS */
1028 typedefs
= typedefs_and_cplusplus
= TRUE
;
1033 included_files
[nincluded_files
++] = optarg
;
1036 /* Ctags options. */
1037 case 'B': searchar
= '?'; break;
1038 case 'u': update
= TRUE
; break;
1039 case 'v': vgrind_style
= TRUE
; /*FALLTHRU*/
1040 case 'x': cxref_style
= TRUE
; break;
1041 case 'w': no_warnings
= TRUE
; break;
1044 suggest_asking_for_help ();
1048 for (; optind
< argc
; ++optind
)
1050 argbuffer
[current_arg
].arg_type
= at_filename
;
1051 argbuffer
[current_arg
].what
= argv
[optind
];
1056 if (nincluded_files
== 0 && file_count
== 0)
1058 error ("no input files specified.", (char *)NULL
);
1059 suggest_asking_for_help ();
1062 if (tagfile
== NULL
)
1063 tagfile
= CTAGS
? "tags" : "TAGS";
1064 cwd
= etags_getcwd (); /* the current working directory */
1065 if (cwd
[strlen (cwd
) - 1] != '/')
1068 cwd
= concat (oldcwd
, "/", "");
1071 if (streq (tagfile
, "-"))
1074 tagfiledir
= absolute_dirname (tagfile
, cwd
);
1076 init (); /* set up boolean "functions" */
1079 initbuffer (&token_name
);
1080 initbuffer (&lbs
[0].lb
);
1081 initbuffer (&lbs
[1].lb
);
1082 initbuffer (&filename_lb
);
1086 if (streq (tagfile
, "-"))
1090 /* Switch redirected `stdout' to binary mode (setting `_fmode'
1091 doesn't take effect until after `stdout' is already open). */
1092 if (!isatty (fileno (stdout
)))
1093 setmode (fileno (stdout
), O_BINARY
);
1097 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1103 * Loop through files finding functions.
1105 for (i
= 0; i
< current_arg
; ++i
)
1107 switch (argbuffer
[i
].arg_type
)
1110 forced_lang
= argbuffer
[i
].lang
;
1112 #ifdef ETAGS_REGEXPS
1114 analyse_regex (argbuffer
[i
].what
, FALSE
);
1117 analyse_regex (argbuffer
[i
].what
, TRUE
);
1122 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
1126 error ("can't find file %s\n", this_file
);
1131 this_file
= massage_name (this_file
);
1134 this_file
= argbuffer
[i
].what
;
1136 /* Input file named "-" means read file names from stdin
1137 (one per line) and use them. */
1138 if (streq (this_file
, "-"))
1139 while (readline_internal (&filename_lb
, stdin
) > 0)
1140 process_file (filename_lb
.buffer
);
1142 process_file (this_file
);
1150 #ifdef ETAGS_REGEXPS
1152 #endif /* ETAGS_REGEXPS */
1156 while (nincluded_files
-- > 0)
1157 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
1163 /* If CTAGS, we are here. process_file did not write the tags yet,
1164 because we want them ordered. Let's do it now. */
1176 for (i
= 0; i
< current_arg
; ++i
)
1178 if (argbuffer
[i
].arg_type
!= at_filename
)
1181 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
1182 tagfile
, argbuffer
[i
].what
, tagfile
);
1183 if (system (cmd
) != GOOD
)
1184 fatal ("failed to execute shell command", (char *)NULL
);
1186 append_to_tagfile
= TRUE
;
1189 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
1200 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
1201 exit (system (cmd
));
1209 * Return a compressor given the file name. If EXTPTR is non-zero,
1210 * return a pointer into FILE where the compressor-specific
1211 * extension begins. If no compressor is found, NULL is returned
1212 * and EXTPTR is not significant.
1213 * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca>
1216 get_compressor_from_suffix (file
, extptr
)
1221 char *slash
, *suffix
;
1223 /* This relies on FN to be after canonicalize_filename,
1224 so we don't need to consider backslashes on DOS_NT. */
1225 slash
= etags_strrchr (file
, '/');
1226 suffix
= etags_strrchr (file
, '.');
1227 if (suffix
== NULL
|| suffix
< slash
)
1232 /* Let those poor souls who live with DOS 8+3 file name limits get
1233 some solace by treating foo.cgz as if it were foo.c.gz, etc.
1234 Only the first do loop is run if not MSDOS */
1237 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1238 if (streq (compr
->suffix
, suffix
))
1241 break; /* do it only once: not really a loop */
1244 } while (*suffix
!= '\0');
1251 * Return a language given the name.
1254 get_language_from_name (name
)
1260 error ("empty language name", (char *)NULL
);
1263 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1264 if (streq (name
, lang
->name
))
1266 error ("unknown language \"%s\"", name
);
1274 * Return a language given the interpreter name.
1277 get_language_from_interpreter (interpreter
)
1283 if (interpreter
== NULL
)
1285 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1286 if (lang
->interpreters
!= NULL
)
1287 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1288 if (streq (*iname
, interpreter
))
1297 * Return a language given the file name.
1300 get_language_from_suffix (file
)
1304 char **ext
, *suffix
;
1306 suffix
= etags_strrchr (file
, '.');
1310 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1311 if (lang
->suffixes
!= NULL
)
1312 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1313 if (streq (*ext
, suffix
))
1321 * This routine is called on each file argument.
1327 struct stat stat_buf
;
1330 char *compressed_name
, *uncompressed_name
;
1331 char *ext
, *real_name
;
1334 canonicalize_filename (file
);
1335 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1337 error ("skipping inclusion of %s in self.", file
);
1340 if ((compr
= get_compressor_from_suffix (file
, &ext
)) == NULL
)
1342 compressed_name
= NULL
;
1343 real_name
= uncompressed_name
= savestr (file
);
1347 real_name
= compressed_name
= savestr (file
);
1348 uncompressed_name
= savenstr (file
, ext
- file
);
1351 /* If the canonicalised uncompressed name has already be dealt with,
1352 skip it silently, else add it to the list. */
1354 typedef struct processed_file
1357 struct processed_file
*next
;
1359 static processed_file
*pf_head
= NULL
;
1360 register processed_file
*fnp
;
1362 for (fnp
= pf_head
; fnp
!= NULL
; fnp
= fnp
->next
)
1363 if (streq (uncompressed_name
, fnp
->filename
))
1366 pf_head
= xnew (1, struct processed_file
);
1367 pf_head
->filename
= savestr (uncompressed_name
);
1368 pf_head
->next
= fnp
;
1371 if (stat (real_name
, &stat_buf
) != 0)
1373 /* Reset real_name and try with a different name. */
1375 if (compressed_name
!= NULL
) /* try with the given suffix */
1377 if (stat (uncompressed_name
, &stat_buf
) == 0)
1378 real_name
= uncompressed_name
;
1380 else /* try all possible suffixes */
1382 for (compr
= compressors
; compr
->suffix
!= NULL
; compr
++)
1384 compressed_name
= concat (file
, ".", compr
->suffix
);
1385 if (stat (compressed_name
, &stat_buf
) != 0)
1389 char *suf
= compressed_name
+ strlen (file
);
1390 size_t suflen
= strlen (compr
->suffix
) + 1;
1391 for ( ; suf
[1]; suf
++, suflen
--)
1393 memmove (suf
, suf
+ 1, suflen
);
1394 if (stat (compressed_name
, &stat_buf
) == 0)
1396 real_name
= compressed_name
;
1400 if (real_name
!= NULL
)
1403 free (compressed_name
);
1404 compressed_name
= NULL
;
1408 real_name
= compressed_name
;
1413 if (real_name
== NULL
)
1418 } /* try with a different name */
1420 if (!S_ISREG (stat_buf
.st_mode
))
1422 error ("skipping %s: it is not a regular file.", real_name
);
1425 if (real_name
== compressed_name
)
1427 char *cmd
= concat (compr
->command
, " ", real_name
);
1428 inf
= popen (cmd
, "r");
1432 inf
= fopen (real_name
, "r");
1439 find_entries (uncompressed_name
, inf
);
1441 if (real_name
== compressed_name
)
1450 if (filename_is_absolute (uncompressed_name
))
1452 /* file is an absolute file name. Canonicalise it. */
1453 filename
= absolute_filename (uncompressed_name
, cwd
);
1457 /* file is a file name relative to cwd. Make it relative
1458 to the directory of the tags file. */
1459 filename
= relative_filename (uncompressed_name
, tagfiledir
);
1461 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1469 if (compressed_name
) free(compressed_name
);
1470 if (uncompressed_name
) free(uncompressed_name
);
1475 * This routine sets up the boolean pseudo-functions which work
1476 * by setting boolean flags dependent upon the corresponding character.
1477 * Every char which is NOT in that string is not a white char. Therefore,
1478 * all of the array "_wht" is set to FALSE, and then the elements
1479 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1480 * of a char is TRUE if it is the string "white", else FALSE.
1488 for (i
= 0; i
< CHARS
; i
++)
1489 iswhite(i
) = notinname(i
) = begtoken(i
) = intoken(i
) = endtoken(i
) = FALSE
;
1490 for (sp
= white
; *sp
!= '\0'; sp
++) iswhite (*sp
) = TRUE
;
1491 for (sp
= nonam
; *sp
!= '\0'; sp
++) notinname (*sp
) = TRUE
;
1492 notinname('\0') = notinname('\n');
1493 for (sp
= begtk
; *sp
!= '\0'; sp
++) begtoken (*sp
) = TRUE
;
1494 begtoken('\0') = begtoken('\n');
1495 for (sp
= midtk
; *sp
!= '\0'; sp
++) intoken (*sp
) = TRUE
;
1496 intoken('\0') = intoken('\n');
1497 for (sp
= endtk
; *sp
!= '\0'; sp
++) endtoken (*sp
) = TRUE
;
1498 endtoken('\0') = endtoken('\n');
1502 * This routine opens the specified file and calls the function
1503 * which finds the function and type definitions.
1505 node
*last_node
= NULL
;
1508 find_entries (file
, inf
)
1514 node
*old_last_node
;
1516 /* Memory leakage here: the string pointed by curfile is
1517 never released, because curfile is copied into np->file
1518 for each node, to be used in CTAGS mode. The amount of
1519 memory leaked here is the sum of the lengths of the
1521 curfile
= savestr (file
);
1523 /* If user specified a language, use it. */
1525 if (lang
!= NULL
&& lang
->function
!= NULL
)
1528 lang
->function (inf
);
1532 /* Try to guess the language given the file name. */
1533 lang
= get_language_from_suffix (file
);
1534 if (lang
!= NULL
&& lang
->function
!= NULL
)
1537 lang
->function (inf
);
1541 /* Look for sharp-bang as the first two characters. */
1542 if (readline_internal (&lb
, inf
) > 0
1544 && lb
.buffer
[0] == '#'
1545 && lb
.buffer
[1] == '!')
1549 /* Set lp to point at the first char after the last slash in the
1550 line or, if no slashes, at the first nonblank. Then set cp to
1551 the first successive blank and terminate the string. */
1552 lp
= etags_strrchr (lb
.buffer
+2, '/');
1556 lp
= skip_spaces (lb
.buffer
+ 2);
1557 cp
= skip_non_spaces (lp
);
1560 if (strlen (lp
) > 0)
1562 lang
= get_language_from_interpreter (lp
);
1563 if (lang
!= NULL
&& lang
->function
!= NULL
)
1566 lang
->function (inf
);
1571 /* We rewind here, even if inf may be a pipe. We fail if the
1572 length of the first line is longer than the pipe block size,
1573 which is unlikely. */
1577 old_last_node
= last_node
;
1578 curlang
= get_language_from_name ("fortran");
1579 Fortran_functions (inf
);
1581 /* No Fortran entries found. Try C. */
1582 if (old_last_node
== last_node
)
1584 /* We do not tag if rewind fails.
1585 Only the file name will be recorded in the tags file. */
1587 curlang
= get_language_from_name (cplusplus
? "c++" : "c");
1588 default_C_entries (inf
);
1595 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1596 char *name
; /* tag name, or NULL if unnamed */
1597 bool is_func
; /* tag is a function */
1598 char *linestart
; /* start of the line where tag is */
1599 int linelen
; /* length of the line where tag is */
1600 int lno
; /* line number */
1601 long cno
; /* character number */
1605 if (CTAGS
&& name
== NULL
)
1608 np
= xnew (1, node
);
1610 /* If ctags mode, change name "main" to M<thisfilename>. */
1611 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1613 register char *fp
= etags_strrchr (curfile
, '/');
1614 np
->name
= concat ("M", fp
== NULL
? curfile
: fp
+ 1, "");
1615 fp
= etags_strrchr (np
->name
, '.');
1616 if (fp
!= NULL
&& fp
[1] != '\0' && fp
[2] == '\0')
1621 np
->been_warned
= FALSE
;
1623 np
->is_func
= is_func
;
1625 /* Our char numbers are 0-base, because of C language tradition?
1626 ctags compatibility? old versions compatibility? I don't know.
1627 Anyway, since emacs's are 1-base we expect etags.el to take care
1628 of the difference. If we wanted to have 1-based numbers, we would
1629 uncomment the +1 below. */
1630 np
->cno
= cno
/* + 1 */ ;
1631 np
->left
= np
->right
= NULL
;
1632 if (CTAGS
&& !cxref_style
)
1634 if (strlen (linestart
) < 50)
1635 np
->pat
= concat (linestart
, "$", "");
1637 np
->pat
= savenstr (linestart
, 50);
1640 np
->pat
= savenstr (linestart
, linelen
);
1642 add_node (np
, &head
);
1645 /* Date: Wed, 22 Jan 1997 02:56:31 -0500 [last amended 18 Sep 1997]
1646 * From: Sam Kendall <kendall@mv.mv.com>
1647 * Subject: Proposal for firming up the TAGS format specification
1648 * To: F.Potorti@cnuce.cnr.it
1650 * pfnote should emit the optimized form [unnamed tag] only if:
1651 * 1. name does not contain any of the characters " \t\r\n(),;";
1652 * 2. linestart contains name as either a rightmost, or rightmost but
1653 * one character, substring;
1654 * 3. the character, if any, immediately before name in linestart must
1655 * be one of the characters " \t(),;";
1656 * 4. the character, if any, immediately after name in linestart must
1657 * also be one of the characters " \t(),;".
1659 * The real implementation uses the notinname() macro, which recognises
1660 * characters slightly different form " \t\r\n(),;". See the variable
1663 #define traditional_tag_style TRUE
1665 new_pfnote (name
, namelen
, is_func
, linestart
, linelen
, lno
, cno
)
1666 char *name
; /* tag name, or NULL if unnamed */
1667 int namelen
; /* tag length */
1668 bool is_func
; /* tag is a function */
1669 char *linestart
; /* start of the line where tag is */
1670 int linelen
; /* length of the line where tag is */
1671 int lno
; /* line number */
1672 long cno
; /* character number */
1680 for (cp
= name
; !notinname (*cp
); cp
++)
1682 if (*cp
== '\0') /* rule #1 */
1684 cp
= linestart
+ linelen
- namelen
;
1685 if (notinname (linestart
[linelen
-1]))
1686 cp
-= 1; /* rule #4 */
1687 if (cp
>= linestart
/* rule #2 */
1689 || notinname (cp
[-1])) /* rule #3 */
1690 && strneq (name
, cp
, namelen
)) /* rule #2 */
1691 named
= FALSE
; /* use unnamed tag */
1696 name
= savenstr (name
, namelen
);
1699 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
);
1704 * recurse on left children, iterate on right children.
1712 register node
*node_right
= np
->right
;
1713 free_tree (np
->left
);
1714 if (np
->name
!= NULL
)
1724 * Adds a node to the tree of nodes. In etags mode, we don't keep
1725 * it sorted; we just keep a linear list. In ctags mode, maintain
1726 * an ordered tree, with no attempt at balancing.
1728 * add_node is the only function allowed to add nodes, so it can
1732 add_node (np
, cur_node_p
)
1733 node
*np
, **cur_node_p
;
1736 register node
*cur_node
= *cur_node_p
;
1738 if (cur_node
== NULL
)
1748 if (last_node
== NULL
)
1749 fatal ("internal error in add_node", (char *)NULL
);
1750 last_node
->right
= np
;
1756 dif
= strcmp (np
->name
, cur_node
->name
);
1759 * If this tag name matches an existing one, then
1760 * do not add the node, but maybe print a warning.
1764 if (streq (np
->file
, cur_node
->file
))
1768 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1769 np
->file
, lineno
, np
->name
);
1770 fprintf (stderr
, "Second entry ignored\n");
1773 else if (!cur_node
->been_warned
&& !no_warnings
)
1777 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1778 np
->file
, cur_node
->file
, np
->name
);
1779 cur_node
->been_warned
= TRUE
;
1784 /* Actually add the node */
1785 add_node (np
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1798 /* Output subentries that precede this one */
1799 put_entries (np
->left
);
1801 /* Output this entry */
1805 if (np
->name
!= NULL
)
1806 fprintf (tagf
, "%s\177%s\001%d,%ld\n",
1807 np
->pat
, np
->name
, np
->lno
, np
->cno
);
1809 fprintf (tagf
, "%s\177%d,%ld\n",
1810 np
->pat
, np
->lno
, np
->cno
);
1814 if (np
->name
== NULL
)
1815 error ("internal error: NULL name in ctags mode.", (char *)NULL
);
1820 fprintf (stdout
, "%s %s %d\n",
1821 np
->name
, np
->file
, (np
->lno
+ 63) / 64);
1823 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1824 np
->name
, np
->lno
, np
->file
, np
->pat
);
1828 fprintf (tagf
, "%s\t%s\t", np
->name
, np
->file
);
1832 putc (searchar
, tagf
);
1835 for (sp
= np
->pat
; *sp
; sp
++)
1837 if (*sp
== '\\' || *sp
== searchar
)
1841 putc (searchar
, tagf
);
1844 { /* a typedef; text pattern inadequate */
1845 fprintf (tagf
, "%d", np
->lno
);
1851 /* Output subentries that follow this one */
1852 put_entries (np
->right
);
1855 /* Length of a number's decimal representation. */
1861 while ((num
/= 10) > 0)
1867 * Return total number of characters that put_entries will output for
1868 * the nodes in the subtree of the specified node. Works only if
1869 * we are not ctags, but called only in that case. This count
1870 * is irrelevant with the new tags.el, but is still supplied for
1871 * backward compatibility.
1874 total_size_of_entries (np
)
1882 for (total
= 0; np
!= NULL
; np
= np
->right
)
1884 /* Count left subentries. */
1885 total
+= total_size_of_entries (np
->left
);
1887 /* Count this entry */
1888 total
+= strlen (np
->pat
) + 1;
1889 total
+= number_len ((long) np
->lno
) + 1 + number_len (np
->cno
) + 1;
1890 if (np
->name
!= NULL
)
1891 total
+= 1 + strlen (np
->name
); /* \001name */
1898 * The C symbol tables.
1903 st_C_objprot
, st_C_objimpl
, st_C_objend
,
1908 st_C_struct
, st_C_extern
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1911 static unsigned int hash
P_((const char *, unsigned int));
1912 static struct C_stab_entry
* in_word_set
P_((const char *, unsigned int));
1913 static enum sym_type C_symtype
P_((char *, int, int));
1915 /* Feed stuff between (but not including) %[ and %] lines to:
1916 gperf -c -k 1,3 -o -p -r -t
1918 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1922 while, 0, st_C_ignore
1923 switch, 0, st_C_ignore
1924 return, 0, st_C_ignore
1925 @interface, 0, st_C_objprot
1926 @protocol, 0, st_C_objprot
1927 @implementation,0, st_C_objimpl
1928 @end, 0, st_C_objend
1929 import, C_JAVA, st_C_ignore
1930 package, C_JAVA, st_C_ignore
1931 friend, C_PLPL, st_C_ignore
1932 extends, C_JAVA, st_C_javastruct
1933 implements, C_JAVA, st_C_javastruct
1934 interface, C_JAVA, st_C_struct
1935 class, C_PLPL, st_C_struct
1936 namespace, C_PLPL, st_C_struct
1937 domain, C_STAR, st_C_struct
1938 union, 0, st_C_struct
1939 struct, 0, st_C_struct
1940 extern, 0, st_C_extern
1942 typedef, 0, st_C_typedef
1943 define, 0, st_C_define
1944 operator, C_PLPL, st_C_operator
1945 bool, C_PLPL, st_C_typespec
1946 long, 0, st_C_typespec
1947 short, 0, st_C_typespec
1948 int, 0, st_C_typespec
1949 char, 0, st_C_typespec
1950 float, 0, st_C_typespec
1951 double, 0, st_C_typespec
1952 signed, 0, st_C_typespec
1953 unsigned, 0, st_C_typespec
1954 auto, 0, st_C_typespec
1955 void, 0, st_C_typespec
1956 static, 0, st_C_typespec
1957 const, 0, st_C_typespec
1958 volatile, 0, st_C_typespec
1959 explicit, C_PLPL, st_C_typespec
1960 mutable, C_PLPL, st_C_typespec
1961 typename, C_PLPL, st_C_typespec
1962 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1963 DEFUN, 0, st_C_gnumacro
1964 SYSCALL, 0, st_C_gnumacro
1965 ENTRY, 0, st_C_gnumacro
1966 PSEUDO, 0, st_C_gnumacro
1967 # These are defined inside C functions, so currently they are not met.
1968 # EXFUN used in glibc, DEFVAR_* in emacs.
1969 #EXFUN, 0, st_C_gnumacro
1970 #DEFVAR_, 0, st_C_gnumacro
1972 and replace lines between %< and %> with its output. */
1974 /* C code produced by gperf version 2.7.1 (19981006 egcs) */
1975 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
1976 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1978 #define TOTAL_KEYWORDS 46
1979 #define MIN_WORD_LENGTH 2
1980 #define MAX_WORD_LENGTH 15
1981 #define MIN_HASH_VALUE 13
1982 #define MAX_HASH_VALUE 123
1983 /* maximum key range = 111, duplicates = 0 */
1990 register const char *str
;
1991 register unsigned int len
;
1993 static unsigned char asso_values
[] =
1995 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
1996 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
1997 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
1998 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
1999 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2000 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2001 124, 124, 124, 124, 3, 124, 124, 124, 43, 6,
2002 11, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2003 11, 124, 124, 58, 7, 124, 124, 124, 124, 124,
2004 124, 124, 124, 124, 124, 124, 124, 57, 7, 42,
2005 4, 14, 52, 0, 124, 53, 124, 124, 29, 11,
2006 6, 35, 32, 124, 29, 34, 59, 58, 51, 24,
2007 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2008 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2009 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2010 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2011 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2012 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2013 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2014 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2015 124, 124, 124, 124, 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, 124, 124, 124, 124, 124, 124,
2019 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
2020 124, 124, 124, 124, 124, 124
2022 register int hval
= len
;
2028 hval
+= asso_values
[(unsigned char)str
[2]];
2031 hval
+= asso_values
[(unsigned char)str
[0]];
2040 static struct C_stab_entry
*
2041 in_word_set (str
, len
)
2042 register const char *str
;
2043 register unsigned int len
;
2045 static struct C_stab_entry wordlist
[] =
2047 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2048 {""}, {""}, {""}, {""},
2049 {"@end", 0, st_C_objend
},
2050 {""}, {""}, {""}, {""},
2051 {"ENTRY", 0, st_C_gnumacro
},
2052 {"@interface", 0, st_C_objprot
},
2054 {"domain", C_STAR
, st_C_struct
},
2056 {"PSEUDO", 0, st_C_gnumacro
},
2058 {"namespace", C_PLPL
, st_C_struct
},
2060 {"@implementation",0, st_C_objimpl
},
2061 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
2062 {"long", 0, st_C_typespec
},
2063 {"signed", 0, st_C_typespec
},
2064 {"@protocol", 0, st_C_objprot
},
2065 {""}, {""}, {""}, {""},
2066 {"bool", C_PLPL
, st_C_typespec
},
2067 {""}, {""}, {""}, {""}, {""}, {""},
2068 {"const", 0, st_C_typespec
},
2069 {"explicit", C_PLPL
, st_C_typespec
},
2070 {"if", 0, st_C_ignore
},
2072 {"operator", C_PLPL
, st_C_operator
},
2074 {"DEFUN", 0, st_C_gnumacro
},
2076 {"define", 0, st_C_define
},
2077 {""}, {""}, {""}, {""}, {""},
2078 {"double", 0, st_C_typespec
},
2079 {"struct", 0, st_C_struct
},
2080 {""}, {""}, {""}, {""},
2081 {"short", 0, st_C_typespec
},
2083 {"enum", 0, st_C_enum
},
2084 {"mutable", C_PLPL
, st_C_typespec
},
2086 {"extern", 0, st_C_extern
},
2087 {"extends", C_JAVA
, st_C_javastruct
},
2088 {"package", C_JAVA
, st_C_ignore
},
2089 {"while", 0, st_C_ignore
},
2091 {"for", 0, st_C_ignore
},
2093 {"volatile", 0, st_C_typespec
},
2095 {"import", C_JAVA
, st_C_ignore
},
2096 {"float", 0, st_C_typespec
},
2097 {"switch", 0, st_C_ignore
},
2098 {"return", 0, st_C_ignore
},
2099 {"implements", C_JAVA
, st_C_javastruct
},
2101 {"static", 0, st_C_typespec
},
2102 {"typedef", 0, st_C_typedef
},
2103 {"typename", C_PLPL
, st_C_typespec
},
2104 {"unsigned", 0, st_C_typespec
},
2106 {"char", 0, st_C_typespec
},
2107 {"class", C_PLPL
, st_C_struct
},
2109 {"void", 0, st_C_typespec
},
2111 {"friend", C_PLPL
, st_C_ignore
},
2113 {"int", 0, st_C_typespec
},
2114 {"union", 0, st_C_struct
},
2116 {"auto", 0, st_C_typespec
},
2117 {"interface", C_JAVA
, st_C_struct
},
2119 {"SYSCALL", 0, st_C_gnumacro
}
2122 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
2124 register int key
= hash (str
, len
);
2126 if (key
<= MAX_HASH_VALUE
&& key
>= 0)
2128 register const char *s
= wordlist
[key
].name
;
2130 if (*str
== *s
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
2131 return &wordlist
[key
];
2138 static enum sym_type
2139 C_symtype (str
, len
, c_ext
)
2144 register struct C_stab_entry
*se
= in_word_set (str
, len
);
2146 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
2152 * C functions and variables are recognized using a simple
2153 * finite automaton. fvdef is its state variable.
2157 fvnone
, /* nothing seen */
2158 foperator
, /* func: operator keyword seen (cplpl) */
2159 fvnameseen
, /* function or variable name seen */
2160 fstartlist
, /* func: just after open parenthesis */
2161 finlist
, /* func: in parameter list */
2162 flistseen
, /* func: after parameter list */
2163 fignore
, /* func: before open brace */
2164 vignore
/* var-like: ignore until ';' */
2167 bool fvextern
; /* func or var: extern keyword seen; */
2170 * typedefs are recognized using a simple finite automaton.
2171 * typdef is its state variable.
2175 tnone
, /* nothing seen */
2176 tkeyseen
, /* typedef keyword seen */
2177 ttypeseen
, /* defined type seen */
2178 tinbody
, /* inside typedef body */
2179 tend
, /* just before typedef tag */
2180 tignore
/* junk after typedef tag */
2185 * struct-like structures (enum, struct and union) are recognized
2186 * using another simple finite automaton. `structdef' is its state
2191 snone
, /* nothing seen yet */
2192 skeyseen
, /* struct-like keyword seen */
2193 stagseen
, /* struct-like tag seen */
2194 scolonseen
, /* colon seen after struct-like tag */
2195 sinbody
/* in struct body: recognize member func defs*/
2199 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
2200 * struct tag, and structtype is the type of the preceding struct-like
2203 char *structtag
= "<uninited>";
2204 enum sym_type structtype
;
2207 * When objdef is different from onone, objtag is the name of the class.
2209 char *objtag
= "<uninited>";
2212 * Yet another little state machine to deal with preprocessor lines.
2216 dnone
, /* nothing seen */
2217 dsharpseen
, /* '#' seen as first char on line */
2218 ddefineseen
, /* '#' and 'define' seen */
2219 dignorerest
/* ignore rest of line */
2223 * State machine for Objective C protocols and implementations.
2224 * Tom R.Hageman <tom@basil.icce.rug.nl>
2228 onone
, /* nothing seen */
2229 oprotocol
, /* @interface or @protocol seen */
2230 oimplementation
, /* @implementations seen */
2231 otagseen
, /* class name seen */
2232 oparenseen
, /* parenthesis before category seen */
2233 ocatseen
, /* category name seen */
2234 oinbody
, /* in @implementation body */
2235 omethodsign
, /* in @implementation body, after +/- */
2236 omethodtag
, /* after method name */
2237 omethodcolon
, /* after method colon */
2238 omethodparm
, /* after method parameter */
2239 oignore
/* wait for @end */
2244 * Use this structure to keep info about the token read, and how it
2245 * should be tagged. Used by the make_C_tag function to build a tag.
2258 token tok
; /* latest token read */
2261 * Set this to TRUE, and the next token considered is called a function.
2262 * Used only for GNU emacs's function-defining macros.
2264 bool next_token_is_func
;
2267 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
2272 * methodlen is the length of the method name stored in token_name.
2276 static bool consider_token
P_((char *, int, int, int, int, int, bool *));
2277 static void make_C_tag
P_((bool));
2281 * checks to see if the current token is at the start of a
2282 * function or variable, or corresponds to a typedef, or
2283 * is a struct/union/enum tag, or #define, or an enum constant.
2285 * *IS_FUNC gets TRUE iff the token is a function or #define macro
2286 * with args. C_EXT is which language we are looking at.
2294 * next_token_is_func IN OUT
2298 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func_or_var
)
2299 register char *str
; /* IN: token pointer */
2300 register int len
; /* IN: token length */
2301 register int c
; /* IN: first char after the token */
2302 int c_ext
; /* IN: C extensions mask */
2303 int cblev
; /* IN: curly brace level */
2304 int parlev
; /* IN: parenthesis level */
2305 bool *is_func_or_var
; /* OUT: function or variable found */
2307 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
2310 * Advance the definedef state machine.
2315 /* We're not on a preprocessor line. */
2318 if (toktype
== st_C_define
)
2320 definedef
= ddefineseen
;
2324 definedef
= dignorerest
;
2329 * Make a tag for any macro, unless it is a constant
2330 * and constantypedefs is FALSE.
2332 definedef
= dignorerest
;
2333 *is_func_or_var
= (c
== '(');
2334 if (!*is_func_or_var
&& !constantypedefs
)
2341 error ("internal error: definedef value.", (char *)NULL
);
2350 if (toktype
== st_C_typedef
)
2369 /* Do not return here, so the structdef stuff has a chance. */
2383 * This structdef business is currently only invoked when cblev==0.
2384 * It should be recursively invoked whatever the curly brace level,
2385 * and a stack of states kept, to allow for definitions of structs
2388 * This structdef business is NOT invoked when we are ctags and the
2389 * file is plain C. This is because a struct tag may have the same
2390 * name as another tag, and this loses with ctags.
2394 case st_C_javastruct
:
2395 if (structdef
== stagseen
)
2396 structdef
= scolonseen
;
2400 if (typdef
== tkeyseen
2401 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
2403 structdef
= skeyseen
;
2404 structtype
= toktype
;
2409 if (structdef
== skeyseen
)
2411 /* Save the tag for struct/union/class, for functions and variables
2412 that may be defined inside. */
2413 if (structtype
== st_C_struct
)
2414 structtag
= savenstr (str
, len
);
2416 structtag
= "<enum>";
2417 structdef
= stagseen
;
2421 if (typdef
!= tnone
)
2424 /* Detect GNU macros.
2426 Writers of emacs code are recommended to put the
2427 first two args of a DEFUN on the same line.
2429 The DEFUN macro, used in emacs C source code, has a first arg
2430 that is a string (the lisp function name), and a second arg that
2431 is a C function name. Since etags skips strings, the second arg
2432 is tagged. This is unfortunate, as it would be better to tag the
2433 first arg. The simplest way to deal with this problem would be
2434 to name the tag with a name built from the function name, by
2435 removing the initial 'F' character and substituting '-' for '_'.
2436 Anyway, this assumes that the conventions of naming lisp
2437 functions will never change. Currently, this method is not
2439 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
2441 next_token_is_func
= TRUE
;
2444 if (next_token_is_func
)
2446 next_token_is_func
= FALSE
;
2448 *is_func_or_var
= TRUE
;
2452 /* Detect Objective C constructs. */
2462 objdef
= oimplementation
;
2466 case oimplementation
:
2467 /* Save the class tag for functions or variables defined inside. */
2468 objtag
= savenstr (str
, len
);
2472 /* Save the class tag for categories. */
2473 objtag
= savenstr (str
, len
);
2475 *is_func_or_var
= TRUE
;
2479 *is_func_or_var
= TRUE
;
2486 objdef
= omethodtag
;
2488 grow_linebuffer (&token_name
, methodlen
+ 1);
2489 strncpy (token_name
.buffer
, str
, len
);
2490 token_name
.buffer
[methodlen
] = '\0';
2491 token_name
.len
= methodlen
;
2497 objdef
= omethodparm
;
2502 objdef
= omethodtag
;
2504 grow_linebuffer (&token_name
, methodlen
+ 1);
2505 strncat (token_name
.buffer
, str
, len
);
2506 token_name
.len
= methodlen
;
2511 if (toktype
== st_C_objend
)
2513 /* Memory leakage here: the string pointed by objtag is
2514 never released, because many tests would be needed to
2515 avoid breaking on incorrect input code. The amount of
2516 memory leaked here is the sum of the lengths of the
2524 /* A function, variable or enum constant? */
2531 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!= vignore
)
2532 fvdef
= fvnone
; /* should be useless */
2540 *is_func_or_var
= TRUE
;
2543 if ((c_ext
& C_PLPL
) && strneq (str
+len
-10, "::operator", 10))
2546 *is_func_or_var
= TRUE
;
2549 if (constantypedefs
&& structdef
== sinbody
&& structtype
== st_C_enum
)
2551 if (fvdef
== fvnone
)
2553 fvdef
= fvnameseen
; /* function or variable */
2554 *is_func_or_var
= TRUE
;
2565 * This routine finds functions, variables, typedefs,
2566 * #define's, enum constants and struct/union/enum definitions in
2567 * C syntax and adds them to the list.
2569 #define current_lb_is_new (newndx == curndx)
2570 #define switch_line_buffers() (curndx = 1 - curndx)
2572 #define curlb (lbs[curndx].lb)
2573 #define othlb (lbs[1-curndx].lb)
2574 #define newlb (lbs[newndx].lb)
2575 #define curlinepos (lbs[curndx].linepos)
2576 #define othlinepos (lbs[1-curndx].linepos)
2577 #define newlinepos (lbs[newndx].linepos)
2579 #define CNL_SAVE_DEFINEDEF() \
2581 curlinepos = charno; \
2583 linecharno = charno; \
2584 charno += readline (&curlb, inf); \
2585 lp = curlb.buffer; \
2592 CNL_SAVE_DEFINEDEF(); \
2593 if (savetok.valid) \
2596 savetok.valid = FALSE; \
2598 definedef = dnone; \
2606 /* This function should never be called when tok.valid is FALSE, but
2607 we must protect against invalid input or internal errors. */
2610 if (traditional_tag_style
)
2612 /* This was the original code. Now we call new_pfnote instead,
2613 which uses the new method for naming tags (see new_pfnote). */
2616 if (CTAGS
|| tok
.named
)
2617 name
= savestr (token_name
.buffer
);
2618 pfnote (name
, isfun
,
2619 tok
.buffer
, tok
.linelen
, tok
.lineno
, tok
.linepos
);
2622 new_pfnote (token_name
.buffer
, token_name
.len
, isfun
,
2623 tok
.buffer
, tok
.linelen
, tok
.lineno
, tok
.linepos
);
2632 C_entries (c_ext
, inf
)
2633 int c_ext
; /* extension of C */
2634 FILE *inf
; /* input file */
2636 register char c
; /* latest char read; '\0' for end of line */
2637 register char *lp
; /* pointer one beyond the character `c' */
2638 int curndx
, newndx
; /* indices for current and new lb */
2639 register int tokoff
; /* offset in line of start of current token */
2640 register int toklen
; /* length of current token */
2641 char *qualifier
; /* string used to qualify names */
2642 int qlen
; /* length of qualifier */
2643 int cblev
; /* current curly brace level */
2644 int parlev
; /* current parenthesis level */
2645 bool incomm
, inquote
, inchar
, quotednl
, midtoken
;
2646 bool purec
, cplpl
, cjava
;
2647 token savetok
; /* token saved during preprocessor handling */
2650 tokoff
= toklen
= 0; /* keep compiler quiet */
2651 curndx
= newndx
= 0;
2657 fvdef
= fvnone
; fvextern
= FALSE
; typdef
= tnone
;
2658 structdef
= snone
; definedef
= dnone
; objdef
= onone
;
2659 next_token_is_func
= yacc_rules
= FALSE
;
2660 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2661 tok
.valid
= savetok
.valid
= FALSE
;
2664 purec
= !(c_ext
& ~YACC
); /* no extensions (apart from possibly yacc) */
2665 cplpl
= (c_ext
& C_PLPL
) == C_PLPL
;
2666 cjava
= (c_ext
& C_JAVA
) == C_JAVA
;
2668 { qualifier
= "."; qlen
= 1; }
2670 { qualifier
= "::"; qlen
= 2; }
2677 /* If we're at the end of the line, the next character is a
2678 '\0'; don't skip it, because it's the thing that tells us
2679 to read the next line. */
2700 /* Newlines inside comments do not end macro definitions in
2702 CNL_SAVE_DEFINEDEF ();
2715 /* Newlines inside strings do not end macro definitions
2716 in traditional cpp, even though compilers don't
2717 usually accept them. */
2718 CNL_SAVE_DEFINEDEF ();
2728 /* Hmmm, something went wrong. */
2742 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2750 if (fvdef
!= finlist
&& fvdef
!= fignore
&& fvdef
!=vignore
)
2763 else if (/* cplpl && */ *lp
== '/')
2771 if ((c_ext
& YACC
) && *lp
== '%')
2773 /* entering or exiting rules section in yacc file */
2775 definedef
= dnone
; fvdef
= fvnone
; fvextern
= FALSE
;
2776 typdef
= tnone
; structdef
= snone
;
2777 next_token_is_func
= FALSE
;
2778 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2780 yacc_rules
= !yacc_rules
;
2786 if (definedef
== dnone
)
2789 bool cpptoken
= TRUE
;
2791 /* Look back on this line. If all blanks, or nonblanks
2792 followed by an end of comment, this is a preprocessor
2794 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2797 if (*cp
== '*' && *(cp
+1) == '/')
2806 definedef
= dsharpseen
;
2807 } /* if (definedef == dnone) */
2813 /* Consider token only if some complicated conditions are satisfied. */
2814 if ((definedef
!= dnone
2815 || (cblev
== 0 && structdef
!= scolonseen
)
2816 || (cblev
== 1 && cplpl
&& structdef
== sinbody
)
2817 || (structdef
== sinbody
&& purec
))
2818 && typdef
!= tignore
2819 && definedef
!= dignorerest
2820 && fvdef
!= finlist
)
2826 bool funorvar
= FALSE
;
2828 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken (lp
[1]))
2831 * This handles :: in the middle, but not at the
2832 * beginning of an identifier. Also, space-separated
2833 * :: is not recognised.
2843 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2844 c_ext
, cblev
, parlev
, &funorvar
))
2846 if (fvdef
== foperator
)
2849 lp
= skip_spaces (lp
-1);
2853 && !iswhite (*lp
) && *lp
!= '(')
2856 toklen
+= lp
- oldlp
;
2861 && definedef
== dnone
2862 && structdef
== sinbody
)
2863 /* function or var defined in C++ class body */
2865 int len
= strlen (structtag
) + qlen
+ toklen
;
2866 grow_linebuffer (&token_name
, len
+ 1);
2867 strcpy (token_name
.buffer
, structtag
);
2868 strcat (token_name
.buffer
, qualifier
);
2869 strncat (token_name
.buffer
,
2870 newlb
.buffer
+ tokoff
, toklen
);
2871 token_name
.len
= len
;
2874 else if (objdef
== ocatseen
)
2875 /* Objective C category */
2877 int len
= strlen (objtag
) + 2 + toklen
;
2878 grow_linebuffer (&token_name
, len
+ 1);
2879 strcpy (token_name
.buffer
, objtag
);
2880 strcat (token_name
.buffer
, "(");
2881 strncat (token_name
.buffer
,
2882 newlb
.buffer
+ tokoff
, toklen
);
2883 strcat (token_name
.buffer
, ")");
2884 token_name
.len
= len
;
2887 else if (objdef
== omethodtag
2888 || objdef
== omethodparm
)
2889 /* Objective C method */
2895 grow_linebuffer (&token_name
, toklen
+ 1);
2896 strncpy (token_name
.buffer
,
2897 newlb
.buffer
+ tokoff
, toklen
);
2898 token_name
.buffer
[toklen
] = '\0';
2899 token_name
.len
= toklen
;
2900 /* Name macros and members. */
2901 tok
.named
= (structdef
== stagseen
2902 || typdef
== ttypeseen
2905 && definedef
== dignorerest
)
2907 && definedef
== dnone
2908 && structdef
== sinbody
));
2910 tok
.lineno
= lineno
;
2911 tok
.linelen
= tokoff
+ toklen
+ 1;
2912 tok
.buffer
= newlb
.buffer
;
2913 tok
.linepos
= newlinepos
;
2916 if (definedef
== dnone
2917 && (fvdef
== fvnameseen
2918 || fvdef
== foperator
2919 || structdef
== stagseen
2921 || objdef
!= onone
))
2923 if (current_lb_is_new
)
2924 switch_line_buffers ();
2927 make_C_tag (funorvar
);
2931 } /* if (endtoken (c)) */
2932 else if (intoken (c
))
2938 } /* if (midtoken) */
2939 else if (begtoken (c
))
2950 make_C_tag (TRUE
); /* a function */
2957 if (structdef
== stagseen
&& !cjava
)
2963 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2965 tokoff
= lp
- 1 - newlb
.buffer
;
2970 } /* if (begtoken) */
2971 } /* if must look at token */
2974 /* Detect end of line, colon, comma, semicolon and various braces
2975 after having handled a token.*/
2979 if (definedef
!= dnone
)
2985 make_C_tag (TRUE
); /* an Objective C class */
2989 objdef
= omethodcolon
;
2991 grow_linebuffer (&token_name
, methodlen
+ 1);
2992 strcat (token_name
.buffer
, ":");
2993 token_name
.len
= methodlen
;
2996 if (structdef
== stagseen
)
2997 structdef
= scolonseen
;
3004 make_C_tag (FALSE
); /* a yacc function */
3015 if (definedef
!= dnone
)
3021 make_C_tag (FALSE
); /* a typedef */
3031 if ((members
&& cblev
== 1)
3032 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3033 make_C_tag (FALSE
); /* a variable */
3039 if (declarations
&& (cblev
== 0 || cblev
== 1))
3040 make_C_tag (TRUE
); /* a function declaration */
3045 /* The following instruction invalidates the token.
3046 Probably the token should be invalidated in all
3047 other cases where some state machine is reset. */
3050 if (structdef
== stagseen
)
3054 if (definedef
!= dnone
)
3060 make_C_tag (TRUE
); /* an Objective C method */
3072 if ((members
&& cblev
== 1)
3073 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3074 make_C_tag (FALSE
); /* a variable */
3079 if (structdef
== stagseen
)
3083 if (definedef
!= dnone
)
3085 if (cblev
== 0 && typdef
== tend
)
3088 make_C_tag (FALSE
); /* a typedef */
3099 if ((members
&& cblev
== 1)
3100 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3101 make_C_tag (FALSE
); /* a variable */
3106 if (structdef
== stagseen
)
3110 if (definedef
!= dnone
)
3112 if (objdef
== otagseen
&& parlev
== 0)
3113 objdef
= oparenseen
;
3117 if (typdef
== ttypeseen
3120 && structdef
!= sinbody
)
3122 /* This handles constructs like:
3123 typedef void OperatorFun (int fun); */
3138 if (definedef
!= dnone
)
3140 if (objdef
== ocatseen
&& parlev
== 1)
3142 make_C_tag (TRUE
); /* an Objective C category */
3154 if (cblev
== 0 && (typdef
== tend
))
3157 make_C_tag (FALSE
); /* a typedef */
3160 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
3164 if (definedef
!= dnone
)
3166 if (typdef
== ttypeseen
)
3170 case skeyseen
: /* unnamed struct */
3171 structdef
= sinbody
;
3172 structtag
= "_anonymous_";
3175 case scolonseen
: /* named struct */
3176 structdef
= sinbody
;
3177 make_C_tag (FALSE
); /* a struct */
3183 make_C_tag (TRUE
); /* a function */
3192 make_C_tag (TRUE
); /* an Objective C class */
3197 make_C_tag (TRUE
); /* an Objective C method */
3201 /* Neutralize `extern "C" {' grot. */
3202 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
3209 if (definedef
!= dnone
)
3211 if (fvdef
== fstartlist
)
3212 fvdef
= fvnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
3215 if (definedef
!= dnone
)
3217 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
3219 cblev
= 0; /* reset curly brace level if first column */
3220 parlev
= 0; /* also reset paren level, just in case... */
3226 if (typdef
== tinbody
)
3228 /* Memory leakage here: the string pointed by structtag is
3229 never released, because I fear to miss something and
3230 break things while freeing the area. The amount of
3231 memory leaked here is the sum of the lengths of the
3233 if (structdef == sinbody)
3234 free (structtag); */
3237 structtag
= "<error>";
3241 if (definedef
!= dnone
)
3251 if ((members
&& cblev
== 1)
3252 || (globals
&& cblev
== 0 && (!fvextern
|| declarations
)))
3253 make_C_tag (FALSE
); /* a variable */
3261 if (objdef
== oinbody
&& cblev
== 0)
3263 objdef
= omethodsign
;
3267 case '#': case '~': case '&': case '%': case '/': case '|':
3268 case '^': case '!': case '<': case '>': case '.': case '?': case ']':
3269 if (definedef
!= dnone
)
3271 /* These surely cannot follow a function tag in C. */
3284 if (objdef
== otagseen
)
3286 make_C_tag (TRUE
); /* an Objective C class */
3289 /* If a macro spans multiple lines don't reset its state. */
3291 CNL_SAVE_DEFINEDEF ();
3297 } /* while not eof */
3301 * Process either a C++ file or a C file depending on the setting
3305 default_C_entries (inf
)
3308 C_entries (cplusplus
? C_PLPL
: 0, inf
);
3311 /* Always do plain ANSI C. */
3313 plain_C_entries (inf
)
3319 /* Always do C++. */
3321 Cplusplus_entries (inf
)
3324 C_entries (C_PLPL
, inf
);
3327 /* Always do Java. */
3332 C_entries (C_JAVA
, inf
);
3340 C_entries (C_STAR
, inf
);
3343 /* Always do Yacc. */
3348 C_entries (YACC
, inf
);
3351 /* A useful macro. */
3352 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
3353 for (lineno = charno = 0; /* loop initialization */ \
3354 !feof (file_pointer) /* loop test */ \
3355 && (lineno++, /* instructions at start of loop */ \
3356 linecharno = charno, \
3357 charno += readline (&line_buffer, file_pointer), \
3358 char_pointer = lb.buffer, \
3364 * Read a file, but do no processing. This is used to do regexp
3365 * matching on files that have no language defined.
3368 just_read_file (inf
)
3371 register char *dummy
;
3373 LOOP_ON_INPUT_LINES (inf
, lb
, dummy
)
3377 /* Fortran parsing */
3379 static bool tail
P_((char *));
3380 static void takeprec
P_((void));
3381 static void getit
P_((FILE *));
3387 register int len
= 0;
3389 while (*cp
!= '\0' && lowcase (*cp
) == lowcase (dbp
[len
]))
3391 if (*cp
== '\0' && !intoken (dbp
[len
]))
3402 dbp
= skip_spaces (dbp
);
3406 dbp
= skip_spaces (dbp
);
3407 if (strneq (dbp
, "(*)", 3))
3412 if (!ISDIGIT (*dbp
))
3414 --dbp
; /* force failure */
3419 while (ISDIGIT (*dbp
));
3428 dbp
= skip_spaces (dbp
);
3432 linecharno
= charno
;
3433 charno
+= readline (&lb
, inf
);
3438 dbp
= skip_spaces (dbp
);
3440 if (!ISALPHA (*dbp
) && *dbp
!= '_' && *dbp
!= '$')
3442 for (cp
= dbp
+ 1; *cp
!= '\0' && intoken (*cp
); cp
++)
3444 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
3445 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3450 Fortran_functions (inf
)
3453 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3456 dbp
++; /* Ratfor escape to fortran */
3457 dbp
= skip_spaces (dbp
);
3460 switch (lowcase (*dbp
))
3463 if (tail ("integer"))
3471 if (tail ("logical"))
3475 if (tail ("complex") || tail ("character"))
3479 if (tail ("double"))
3481 dbp
= skip_spaces (dbp
);
3484 if (tail ("precision"))
3490 dbp
= skip_spaces (dbp
);
3493 switch (lowcase (*dbp
))
3496 if (tail ("function"))
3500 if (tail ("subroutine"))
3508 if (tail ("blockdata") || tail ("block data"))
3510 dbp
= skip_spaces (dbp
);
3511 if (*dbp
== '\0') /* assume un-named */
3512 pfnote (savestr ("blockdata"), TRUE
,
3513 lb
.buffer
, dbp
- lb
.buffer
, lineno
, linecharno
);
3515 getit (inf
); /* look for name */
3523 * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be>, 1998-04-24
3527 static void adagetit
P_((FILE *, char *));
3529 /* Once we are positioned after an "interesting" keyword, let's get
3530 the real tag value necessary. */
3532 adagetit (inf
, name_qualifier
)
3534 char *name_qualifier
;
3542 dbp
= skip_spaces (dbp
);
3544 || (dbp
[0] == '-' && dbp
[1] == '-'))
3547 linecharno
= charno
;
3548 charno
+= readline (&lb
, inf
);
3557 /* Skipping body of procedure body or package body or ....
3558 resetting qualifier to body instead of spec. */
3559 name_qualifier
= "/b";
3565 /* Skipping type of task type or protected type ... */
3573 for (cp
= dbp
; *cp
!= '\0' && *cp
!= '"'; cp
++)
3578 dbp
= skip_spaces (dbp
);
3581 && (ISALPHA (*cp
) || ISDIGIT (*cp
) || *cp
== '_' || *cp
== '.'));
3589 name
= concat (dbp
, name_qualifier
, "");
3591 pfnote (name
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3602 bool inquote
= FALSE
;
3604 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
3606 while (*dbp
!= '\0')
3608 /* Skip a string i.e. "abcd". */
3609 if (inquote
|| (*dbp
== '"'))
3611 dbp
= etags_strchr ((inquote
) ? dbp
: dbp
+1, '"');
3616 continue; /* advance char */
3621 break; /* advance line */
3625 /* Skip comments. */
3626 if (dbp
[0] == '-' && dbp
[1] == '-')
3627 break; /* advance line */
3629 /* Skip character enclosed in single quote i.e. 'a'
3630 and skip single quote starting an attribute i.e. 'Image. */
3639 /* Search for beginning of a token. */
3640 if (!begtoken (*dbp
))
3643 continue; /* advance char */
3646 /* We are at the beginning of a token. */
3651 if (!packages_only
&& tail ("function"))
3652 adagetit (inf
, "/f");
3654 break; /* from switch */
3655 continue; /* advance char */
3658 if (!packages_only
&& tail ("procedure"))
3659 adagetit (inf
, "/p");
3660 else if (tail ("package"))
3661 adagetit (inf
, "/s");
3662 else if (tail ("protected")) /* protected type */
3663 adagetit (inf
, "/t");
3665 break; /* from switch */
3666 continue; /* advance char */
3669 if (!packages_only
&& tail ("task"))
3670 adagetit (inf
, "/k");
3671 else if (typedefs
&& !packages_only
&& tail ("type"))
3673 adagetit (inf
, "/t");
3674 while (*dbp
!= '\0')
3678 break; /* from switch */
3679 continue; /* advance char */
3682 /* Look for the end of the token. */
3683 while (!endtoken (*dbp
))
3686 } /* advance char */
3687 } /* advance line */
3691 * Bob Weiner, Motorola Inc., 4/3/94
3692 * Unix and microcontroller assembly tag handling
3693 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
3701 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3703 /* If first char is alphabetic or one of [_.$], test for colon
3704 following identifier. */
3705 if (ISALPHA (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3707 /* Read past label. */
3709 while (ISALNUM (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
3711 if (*cp
== ':' || iswhite (*cp
))
3713 /* Found end of label, so copy it and add it to the table. */
3714 pfnote (savenstr(lb
.buffer
, cp
-lb
.buffer
), TRUE
,
3715 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3722 * Perl support by Bart Robinson <lomew@cs.utah.edu>
3723 * enhanced by Michael Ernst <mernst@alum.mit.edu>
3724 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
3725 * Perl variable names: /^(my|local).../
3728 Perl_functions (inf
)
3733 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3737 && *cp
++ == 'b' && iswhite (*cp
++))
3739 cp
= skip_spaces (cp
);
3744 && !iswhite (*cp
) && *cp
!= '{' && *cp
!= '(')
3746 pfnote (savenstr (sp
, cp
-sp
), TRUE
,
3747 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3750 else if (globals
/* only if tagging global vars is enabled */
3751 && ((cp
= lb
.buffer
,
3760 && (*cp
== '(' || iswhite (*cp
)))
3762 /* After "my" or "local", but before any following paren or space. */
3763 char *varname
= NULL
;
3765 cp
= skip_spaces (cp
);
3766 if (*cp
== '$' || *cp
== '@' || *cp
== '%')
3768 char* varstart
= ++cp
;
3769 while (ISALNUM (*cp
) || *cp
== '_')
3771 varname
= savenstr (varstart
, cp
-varstart
);
3775 /* Should be examining a variable list at this point;
3776 could insist on seeing an open parenthesis. */
3777 while (*cp
!= '\0' && *cp
!= ';' && *cp
!= '=' && *cp
!= ')')
3781 /* Perhaps I should back cp up one character, so the TAGS table
3782 doesn't mention (and so depend upon) the following char. */
3783 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : varname
,
3784 FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3790 * Python support by Eric S. Raymond <esr@thyrsus.com>
3791 * Look for /^def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
3794 Python_functions (inf
)
3799 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
3803 && *cp
++ == 'f' && iswhite (*cp
++))
3805 cp
= skip_spaces (cp
);
3806 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3809 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3817 && *cp
++ == 's' && iswhite (*cp
++))
3819 cp
= skip_spaces (cp
);
3820 while (*cp
!= '\0' && !iswhite (*cp
) && *cp
!= '(' && *cp
!= ':')
3823 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3828 /* Idea by Corny de Souza
3829 * Cobol tag functions
3830 * We could look for anything that could be a paragraph name.
3831 * i.e. anything that starts in column 8 is one word and ends in a full stop.
3834 Cobol_paragraphs (inf
)
3837 register char *bp
, *ep
;
3839 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
3845 /* If eoln, compiler option or comment ignore whole line. */
3846 if (bp
[-1] != ' ' || !ISALNUM (bp
[0]))
3849 for (ep
= bp
; ISALNUM (*ep
) || *ep
== '-'; ep
++)
3852 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
3853 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
3857 /* Added by Mosur Mohan, 4/22/88 */
3858 /* Pascal parsing */
3861 * Locates tags for procedures & functions. Doesn't do any type- or
3862 * var-definitions. It does look for the keyword "extern" or
3863 * "forward" immediately following the procedure statement; if found,
3864 * the tag is skipped.
3867 Pascal_functions (inf
)
3870 linebuffer tline
; /* mostly copied from C_entries */
3872 int save_lineno
, save_len
;
3873 char c
, *cp
, *namebuf
;
3875 bool /* each of these flags is TRUE iff: */
3876 incomment
, /* point is inside a comment */
3877 inquote
, /* point is inside '..' string */
3878 get_tagname
, /* point is after PROCEDURE/FUNCTION
3879 keyword, so next item = potential tag */
3880 found_tag
, /* point is after a potential tag */
3881 inparms
, /* point is within parameter-list */
3882 verify_tag
; /* point has passed the parm-list, so the
3883 next token will determine whether this
3884 is a FORWARD/EXTERN to be ignored, or
3885 whether it is a real tag */
3887 save_lcno
= save_lineno
= save_len
= 0; /* keep compiler quiet */
3888 namebuf
= NULL
; /* keep compiler quiet */
3893 initbuffer (&tline
);
3895 incomment
= inquote
= FALSE
;
3896 found_tag
= FALSE
; /* have a proc name; check if extern */
3897 get_tagname
= FALSE
; /* have found "procedure" keyword */
3898 inparms
= FALSE
; /* found '(' after "proc" */
3899 verify_tag
= FALSE
; /* check if "extern" is ahead */
3902 while (!feof (inf
)) /* long main loop to get next char */
3905 if (c
== '\0') /* if end of line */
3908 linecharno
= charno
;
3909 charno
+= readline (&lb
, inf
);
3913 if (!((found_tag
&& verify_tag
)
3915 c
= *dbp
++; /* only if don't need *dbp pointing
3916 to the beginning of the name of
3917 the procedure or function */
3921 if (c
== '}') /* within { } comments */
3923 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
3940 inquote
= TRUE
; /* found first quote */
3942 case '{': /* found open { comment */
3946 if (*dbp
== '*') /* found open (* comment */
3951 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
3954 case ')': /* end of parms list */
3959 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
3966 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
3968 /* check if this is an "extern" declaration */
3971 if (lowcase (*dbp
== 'e'))
3973 if (tail ("extern")) /* superfluous, really! */
3979 else if (lowcase (*dbp
) == 'f')
3981 if (tail ("forward")) /* check for forward reference */
3987 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
3991 pfnote (namebuf
, TRUE
,
3992 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
3996 if (get_tagname
) /* grab name of proc or fn */
4001 /* save all values for later tagging */
4002 grow_linebuffer (&tline
, lb
.len
+ 1);
4003 strcpy (tline
.buffer
, lb
.buffer
);
4004 save_lineno
= lineno
;
4005 save_lcno
= linecharno
;
4007 /* grab block name */
4008 for (cp
= dbp
+ 1; *cp
!= '\0' && !endtoken (*cp
); cp
++)
4010 namebuf
= savenstr (dbp
, cp
-dbp
);
4011 dbp
= cp
; /* set dbp to e-o-token */
4012 save_len
= dbp
- lb
.buffer
+ 1;
4013 get_tagname
= FALSE
;
4017 /* and proceed to check for "extern" */
4019 else if (!incomment
&& !inquote
&& !found_tag
)
4021 /* check for proc/fn keywords */
4022 switch (lowcase (c
))
4025 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
4029 if (tail ("unction"))
4034 } /* while not eof */
4036 free (tline
.buffer
);
4040 * lisp tag functions
4041 * look for (def or (DEF, quote or QUOTE
4044 static int L_isdef
P_((char *));
4045 static int L_isquote
P_((char *));
4046 static void L_getit
P_((void));
4050 register char *strp
;
4052 return ((strp
[1] == 'd' || strp
[1] == 'D')
4053 && (strp
[2] == 'e' || strp
[2] == 'E')
4054 && (strp
[3] == 'f' || strp
[3] == 'F'));
4059 register char *strp
;
4061 return ((*++strp
== 'q' || *strp
== 'Q')
4062 && (*++strp
== 'u' || *strp
== 'U')
4063 && (*++strp
== 'o' || *strp
== 'O')
4064 && (*++strp
== 't' || *strp
== 'T')
4065 && (*++strp
== 'e' || *strp
== 'E')
4066 && iswhite (*++strp
));
4074 if (*dbp
== '\'') /* Skip prefix quote */
4076 else if (*dbp
== '(')
4078 if (L_isquote (dbp
))
4079 dbp
+= 7; /* Skip "(quote " */
4081 dbp
+= 1; /* Skip "(" before name in (defstruct (foo)) */
4082 dbp
= skip_spaces (dbp
);
4085 for (cp
= dbp
/*+1*/;
4086 *cp
!= '\0' && *cp
!= '(' && !iswhite(*cp
) && *cp
!= ')';
4092 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
,
4093 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4097 Lisp_functions (inf
)
4100 LOOP_ON_INPUT_LINES (inf
, lb
, dbp
)
4106 dbp
= skip_non_spaces (dbp
);
4107 dbp
= skip_spaces (dbp
);
4112 /* Check for (foo::defmumble name-defined ... */
4115 while (*dbp
!= '\0' && !iswhite (*dbp
)
4116 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
4121 while (*dbp
== ':');
4123 if (L_isdef (dbp
- 1))
4125 dbp
= skip_non_spaces (dbp
);
4126 dbp
= skip_spaces (dbp
);
4136 * Postscript tag functions
4137 * Just look for lines where the first character is '/'
4138 * Richard Mlynarik <mly@adoc.xerox.com>
4139 * Also look at "defineps" for PSWrap
4140 * suggested by Masatake YAMATO <masata-y@is.aist-nara.ac.jp>
4143 Postscript_functions (inf
)
4146 register char *bp
, *ep
;
4148 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4153 *ep
!= '\0' && *ep
!= ' ' && *ep
!= '{';
4156 pfnote (savenstr (bp
, ep
-bp
), TRUE
,
4157 lb
.buffer
, ep
- lb
.buffer
+ 1, lineno
, linecharno
);
4159 else if (strneq (bp
, "defineps", 8))
4161 bp
= skip_non_spaces (bp
);
4162 bp
= skip_spaces (bp
);
4170 * Scheme tag functions
4171 * look for (def... xyzzy
4172 * look for (def... (xyzzy
4173 * look for (def ... ((...(xyzzy ....
4174 * look for (set! xyzzy
4178 Scheme_functions (inf
)
4183 LOOP_ON_INPUT_LINES (inf
, lb
, bp
)
4186 && (bp
[1] == 'D' || bp
[1] == 'd')
4187 && (bp
[2] == 'E' || bp
[2] == 'e')
4188 && (bp
[3] == 'F' || bp
[3] == 'f'))
4190 bp
= skip_non_spaces (bp
);
4191 /* Skip over open parens and white space */
4192 while (iswhite (*bp
) || *bp
== '(')
4197 && (bp
[1] == 'S' || bp
[1] == 's')
4198 && (bp
[2] == 'E' || bp
[2] == 'e')
4199 && (bp
[3] == 'T' || bp
[3] == 't')
4200 && (bp
[4] == '!' || bp
[4] == '!')
4201 && (iswhite (bp
[5])))
4203 bp
= skip_non_spaces (bp
);
4204 bp
= skip_spaces (bp
);
4210 /* Find tags in TeX and LaTeX input files. */
4212 /* TEX_toktab is a table of TeX control sequences that define tags.
4213 Each TEX_tabent records one such control sequence.
4214 CONVERT THIS TO USE THE Stab TYPE!! */
4221 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
4223 /* Default set of control sequences to put into TEX_toktab.
4224 The value of environment var TEXTAGS is prepended to this. */
4226 char *TEX_defenv
= "\
4227 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
4228 :part:appendix:entry:index";
4230 static void TEX_mode
P_((FILE *));
4231 static struct TEX_tabent
*TEX_decode_env
P_((char *, char *));
4232 static int TEX_Token
P_((char *));
4234 char TEX_esc
= '\\';
4235 char TEX_opgrp
= '{';
4236 char TEX_clgrp
= '}';
4239 * TeX/LaTeX scanning loop.
4248 /* Select either \ or ! as escape character. */
4251 /* Initialize token table once from environment. */
4253 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
4255 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4258 /* Look at each esc in line. */
4259 while ((cp
= etags_strchr (cp
, TEX_esc
)) != NULL
)
4263 linecharno
+= cp
- lasthit
;
4265 i
= TEX_Token (lasthit
);
4268 /* We seem to include the TeX command in the tag name.
4270 for (p = lasthit + TEX_toktab[i].len;
4271 *p != '\0' && *p != TEX_clgrp;
4274 pfnote (/*savenstr (lasthit, p-lasthit)*/ (char *)NULL
, TRUE
,
4275 lb
.buffer
, lb
.len
, lineno
, linecharno
);
4276 break; /* We only tag a line once */
4282 #define TEX_LESC '\\'
4283 #define TEX_SESC '!'
4286 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
4287 chars accordingly. */
4294 while ((c
= getc (inf
)) != EOF
)
4296 /* Skip to next line if we hit the TeX comment char. */
4300 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
4316 /* If the input file is compressed, inf is a pipe, and rewind may fail.
4317 No attempt is made to correct the situation. */
4321 /* Read environment and prepend it to the default string.
4322 Build token table. */
4323 static struct TEX_tabent
*
4324 TEX_decode_env (evarname
, defenv
)
4328 register char *env
, *p
;
4330 struct TEX_tabent
*tab
;
4333 /* Append default string to environment. */
4334 env
= getenv (evarname
);
4340 env
= concat (oldenv
, defenv
, "");
4343 /* Allocate a token table */
4344 for (size
= 1, p
= env
; p
;)
4345 if ((p
= etags_strchr (p
, ':')) && *++p
!= '\0')
4347 /* Add 1 to leave room for null terminator. */
4348 tab
= xnew (size
+ 1, struct TEX_tabent
);
4350 /* Unpack environment string into token table. Be careful about */
4351 /* zero-length strings (leading ':', "::" and trailing ':') */
4354 p
= etags_strchr (env
, ':');
4355 if (!p
) /* End of environment string. */
4356 p
= env
+ strlen (env
);
4358 { /* Only non-zero strings. */
4359 tab
[i
].name
= savenstr (env
, p
- env
);
4360 tab
[i
].len
= strlen (tab
[i
].name
);
4367 tab
[i
].name
= NULL
; /* Mark end of table. */
4375 /* If the text at CP matches one of the tag-defining TeX command names,
4376 return the pointer to the first occurrence of that command in TEX_toktab.
4377 Otherwise return -1.
4378 Keep the capital `T' in `token' for dumb truncating compilers
4379 (this distinguishes it from `TEX_toktab' */
4386 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
4387 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
4392 /* Texinfo support. Dave Love, Mar. 2000. */
4394 Texinfo_functions (inf
)
4398 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4400 if ((*cp
++ == '@' && *cp
++ == 'n' && *cp
++ == 'o' && *cp
++ == 'd'
4401 && *cp
++ == 'e' && iswhite (*cp
++)))
4403 while (iswhite (*cp
))
4406 while (*cp
!= '\0' && *cp
!= ',')
4408 pfnote (savenstr (start
, cp
- start
), TRUE
,
4409 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
4415 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
4417 * Assumes that the predicate starts at column 0.
4418 * Only the first clause of a predicate is added.
4420 static int prolog_pred
P_((char *, char *));
4421 static void prolog_skip_comment
P_((linebuffer
*, FILE *));
4422 static int prolog_atom
P_((char *, int));
4425 Prolog_functions (inf
)
4436 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4438 if (cp
[0] == '\0') /* Empty line */
4440 else if (iswhite (cp
[0])) /* Not a predicate */
4442 else if (cp
[0] == '/' && cp
[1] == '*') /* comment. */
4443 prolog_skip_comment (&lb
, inf
);
4444 else if ((len
= prolog_pred (cp
, last
)) > 0)
4446 /* Predicate. Store the function name so that we only
4447 generate a tag for the first clause. */
4449 last
= xnew(len
+ 1, char);
4450 else if (len
+ 1 > allocated
)
4451 last
= xrnew (last
, len
+ 1, char);
4452 allocated
= len
+ 1;
4453 strncpy (last
, cp
, len
);
4461 prolog_skip_comment (plb
, inf
)
4469 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
4470 if (cp
[0] == '*' && cp
[1] == '/')
4473 linecharno
+= readline (plb
, inf
);
4479 * A predicate definition is added if it matches:
4480 * <beginning of line><Prolog Atom><whitespace>(
4482 * It is added to the tags database if it doesn't match the
4483 * name of the previous clause header.
4485 * Return the size of the name of the predicate, or 0 if no header
4489 prolog_pred (s
, last
)
4491 char *last
; /* Name of last clause. */
4496 pos
= prolog_atom (s
, 0);
4501 pos
= skip_spaces (s
+ pos
) - s
;
4503 if ((s
[pos
] == '(') || (s
[pos
] == '.'))
4508 /* Save only the first clause. */
4510 || len
!= (int)strlen (last
)
4511 || !strneq (s
, last
, len
))
4513 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4521 * Consume a Prolog atom.
4522 * Return the number of bytes consumed, or -1 if there was an error.
4524 * A prolog atom, in this context, could be one of:
4525 * - An alphanumeric sequence, starting with a lower case letter.
4526 * - A quoted arbitrary string. Single quotes can escape themselves.
4527 * Backslash quotes everything.
4530 prolog_atom (s
, pos
)
4538 if (ISLOWER(s
[pos
]) || (s
[pos
] == '_'))
4540 /* The atom is unquoted. */
4542 while (ISALNUM(s
[pos
]) || (s
[pos
] == '_'))
4546 return pos
- origpos
;
4548 else if (s
[pos
] == '\'')
4559 pos
++; /* A double quote */
4561 else if (s
[pos
] == '\0')
4562 /* Multiline quoted atoms are ignored. */
4564 else if (s
[pos
] == '\\')
4566 if (s
[pos
+1] == '\0')
4573 return pos
- origpos
;
4580 * Support for Erlang -- Anders Lindgren, Feb 1996.
4582 * Generates tags for functions, defines, and records.
4584 * Assumes that Erlang functions start at column 0.
4586 static int erlang_func
P_((char *, char *));
4587 static void erlang_attribute
P_((char *));
4588 static int erlang_atom
P_((char *, int));
4591 Erlang_functions (inf
)
4602 LOOP_ON_INPUT_LINES (inf
, lb
, cp
)
4604 if (cp
[0] == '\0') /* Empty line */
4606 else if (iswhite (cp
[0])) /* Not function nor attribute */
4608 else if (cp
[0] == '%') /* comment */
4610 else if (cp
[0] == '"') /* Sometimes, strings start in column one */
4612 else if (cp
[0] == '-') /* attribute, e.g. "-define" */
4614 erlang_attribute (cp
);
4617 else if ((len
= erlang_func (cp
, last
)) > 0)
4620 * Function. Store the function name so that we only
4621 * generates a tag for the first clause.
4624 last
= xnew (len
+ 1, char);
4625 else if (len
+ 1 > allocated
)
4626 last
= xrnew (last
, len
+ 1, char);
4627 allocated
= len
+ 1;
4628 strncpy (last
, cp
, len
);
4636 * A function definition is added if it matches:
4637 * <beginning of line><Erlang Atom><whitespace>(
4639 * It is added to the tags database if it doesn't match the
4640 * name of the previous clause header.
4642 * Return the size of the name of the function, or 0 if no function
4646 erlang_func (s
, last
)
4648 char *last
; /* Name of last clause. */
4653 pos
= erlang_atom (s
, 0);
4658 pos
= skip_spaces (s
+ pos
) - s
;
4660 /* Save only the first clause. */
4663 || len
!= (int)strlen (last
)
4664 || !strneq (s
, last
, len
)))
4666 pfnote (savenstr (s
, len
), TRUE
, s
, pos
, lineno
, linecharno
);
4675 * Handle attributes. Currently, tags are generated for defines
4678 * They are on the form:
4679 * -define(foo, bar).
4680 * -define(Foo(M, N), M+N).
4681 * -record(graph, {vtab = notable, cyclic = true}).
4684 erlang_attribute (s
)
4690 if (strneq (s
, "-define", 7) || strneq (s
, "-record", 7))
4692 pos
= skip_spaces (s
+ 7) - s
;
4693 if (s
[pos
++] == '(')
4695 pos
= skip_spaces (s
+ pos
) - s
;
4696 len
= erlang_atom (s
, pos
);
4698 pfnote (savenstr (& s
[pos
], len
), TRUE
,
4699 s
, pos
+ len
, lineno
, linecharno
);
4707 * Consume an Erlang atom (or variable).
4708 * Return the number of bytes consumed, or -1 if there was an error.
4711 erlang_atom (s
, pos
)
4719 if (ISALPHA (s
[pos
]) || s
[pos
] == '_')
4721 /* The atom is unquoted. */
4723 while (ISALNUM (s
[pos
]) || s
[pos
] == '_')
4725 return pos
- origpos
;
4727 else if (s
[pos
] == '\'')
4738 else if (s
[pos
] == '\0')
4739 /* Multiline quoted atoms are ignored. */
4741 else if (s
[pos
] == '\\')
4743 if (s
[pos
+1] == '\0')
4750 return pos
- origpos
;
4756 #ifdef ETAGS_REGEXPS
4758 static char *scan_separators
P_((char *));
4759 static void analyse_regex
P_((char *, bool));
4760 static void add_regex
P_((char *, bool, language
*));
4761 static char *substitute
P_((char *, char *, struct re_registers
*));
4763 /* Take a string like "/blah/" and turn it into "blah", making sure
4764 that the first and last characters are the same, and handling
4765 quoted separator characters. Actually, stops on the occurrence of
4766 an unquoted separator. Also turns "\t" into a Tab character.
4767 Returns pointer to terminating separator. Works in place. Null
4768 terminates name string. */
4770 scan_separators (name
)
4774 char *copyto
= name
;
4775 bool quoted
= FALSE
;
4777 for (++name
; *name
!= '\0'; ++name
)
4783 else if (*name
== sep
)
4787 /* Something else is quoted, so preserve the quote. */
4793 else if (*name
== '\\')
4795 else if (*name
== sep
)
4801 /* Terminate copied string. */
4806 /* Look at the argument of --regex or --no-regex and do the right
4807 thing. Same for each line of a regexp file. */
4809 analyse_regex (regex_arg
, ignore_case
)
4813 if (regex_arg
== NULL
)
4814 free_patterns (); /* --no-regex: remove existing regexps */
4816 /* A real --regexp option or a line in a regexp file. */
4817 switch (regex_arg
[0])
4819 /* Comments in regexp file or null arg to --regex. */
4825 /* Read a regex file. This is recursive and may result in a
4826 loop, which will stop when the file descriptors are exhausted. */
4830 linebuffer regexbuf
;
4831 char *regexfile
= regex_arg
+ 1;
4833 /* regexfile is a file containing regexps, one per line. */
4834 regexfp
= fopen (regexfile
, "r");
4835 if (regexfp
== NULL
)
4840 initbuffer (®exbuf
);
4841 while (readline_internal (®exbuf
, regexfp
) > 0)
4842 analyse_regex (regexbuf
.buffer
, ignore_case
);
4843 free (regexbuf
.buffer
);
4848 /* Regexp to be used for a specific language only. */
4852 char *lang_name
= regex_arg
+ 1;
4855 for (cp
= lang_name
; *cp
!= '}'; cp
++)
4858 error ("unterminated language name in regex: %s", regex_arg
);
4862 lang
= get_language_from_name (lang_name
);
4865 add_regex (cp
+ 1, ignore_case
, lang
);
4869 /* Regexp to be used for any language. */
4871 add_regex (regex_arg
, ignore_case
, NULL
);
4876 /* Turn a name, which is an ed-style (but Emacs syntax) regular
4877 expression, into a real regular expression by compiling it. */
4879 add_regex (regexp_pattern
, ignore_case
, lang
)
4880 char *regexp_pattern
;
4886 struct re_pattern_buffer
*patbuf
;
4890 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
4892 error ("%s: unterminated regexp", regexp_pattern
);
4895 name
= scan_separators (regexp_pattern
);
4896 if (regexp_pattern
[0] == '\0')
4898 error ("null regexp", (char *)NULL
);
4901 (void) scan_separators (name
);
4903 patbuf
= xnew (1, struct re_pattern_buffer
);
4904 /* Translation table to fold case if appropriate. */
4905 patbuf
->translate
= (ignore_case
) ? lc_trans
: NULL
;
4906 patbuf
->fastmap
= NULL
;
4907 patbuf
->buffer
= NULL
;
4908 patbuf
->allocated
= 0;
4910 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
4913 error ("%s while compiling pattern", err
);
4918 p_head
= xnew (1, pattern
);
4919 p_head
->regex
= savestr (regexp_pattern
);
4920 p_head
->p_next
= pp
;
4921 p_head
->language
= lang
;
4922 p_head
->pattern
= patbuf
;
4923 p_head
->name_pattern
= savestr (name
);
4924 p_head
->error_signaled
= FALSE
;
4928 * Do the substitutions indicated by the regular expression and
4932 substitute (in
, out
, regs
)
4934 struct re_registers
*regs
;
4937 int size
, dig
, diglen
;
4940 size
= strlen (out
);
4942 /* Pass 1: figure out how much to allocate by finding all \N strings. */
4943 if (out
[size
- 1] == '\\')
4944 fatal ("pattern error in \"%s\"", out
);
4945 for (t
= etags_strchr (out
, '\\');
4947 t
= etags_strchr (t
+ 2, '\\'))
4951 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
4957 /* Allocate space and do the substitutions. */
4958 result
= xnew (size
+ 1, char);
4960 for (t
= result
; *out
!= '\0'; out
++)
4961 if (*out
== '\\' && ISDIGIT (*++out
))
4963 /* Using "dig2" satisfies my debugger. Bleah. */
4965 diglen
= regs
->end
[dig
] - regs
->start
[dig
];
4966 strncpy (t
, in
+ regs
->start
[dig
], diglen
);
4973 if (DEBUG
&& (t
> result
+ size
|| t
- result
!= (int)strlen (result
)))
4979 /* Deallocate all patterns. */
4984 while (p_head
!= NULL
)
4986 pp
= p_head
->p_next
;
4987 free (p_head
->regex
);
4988 free (p_head
->name_pattern
);
5003 /* Go till you get to white space or a syntactic break */
5005 *cp
!= '\0' && *cp
!= '(' && *cp
!= ')' && !iswhite (*cp
);
5008 pfnote (savenstr (bp
, cp
-bp
), TRUE
,
5009 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
5012 #endif /* ETAGS_REGEXPS */
5013 /* Initialize a linebuffer for use */
5019 lbp
->buffer
= xnew (200, char);
5023 * Read a line of text from `stream' into `lbp', excluding the
5024 * newline or CR-NL, if any. Return the number of characters read from
5025 * `stream', which is the length of the line including the newline.
5027 * On DOS or Windows we do not count the CR character, if any, before the
5028 * NL, in the returned length; this mirrors the behavior of emacs on those
5029 * platforms (for text files, it translates CR-NL to NL as it reads in the
5033 readline_internal (lbp
, stream
)
5035 register FILE *stream
;
5037 char *buffer
= lbp
->buffer
;
5038 register char *p
= lbp
->buffer
;
5039 register char *pend
;
5042 pend
= p
+ lbp
->size
; /* Separate to avoid 386/IX compiler bug. */
5046 register int c
= getc (stream
);
5049 /* We're at the end of linebuffer: expand it. */
5051 buffer
= xrnew (buffer
, lbp
->size
, char);
5052 p
+= buffer
- lbp
->buffer
;
5053 pend
= buffer
+ lbp
->size
;
5054 lbp
->buffer
= buffer
;
5064 if (p
> buffer
&& p
[-1] == '\r')
5068 /* Assume CRLF->LF translation will be performed by Emacs
5069 when loading this file, so CRs won't appear in the buffer.
5070 It would be cleaner to compensate within Emacs;
5071 however, Emacs does not know how many CRs were deleted
5072 before any given point in the file. */
5087 lbp
->len
= p
- buffer
;
5089 return lbp
->len
+ chars_deleted
;
5093 * Like readline_internal, above, but in addition try to match the
5094 * input line against relevant regular expressions.
5097 readline (lbp
, stream
)
5101 /* Read new line. */
5102 long result
= readline_internal (lbp
, stream
);
5103 #ifdef ETAGS_REGEXPS
5107 /* Match against relevant patterns. */
5109 for (pp
= p_head
; pp
!= NULL
; pp
= pp
->p_next
)
5111 /* Only use generic regexps or those for the current language. */
5112 if (pp
->language
!= NULL
&& pp
->language
!= curlang
)
5115 match
= re_match (pp
->pattern
, lbp
->buffer
, lbp
->len
, 0, &pp
->regs
);
5120 if (!pp
->error_signaled
)
5122 error ("error while matching \"%s\"", pp
->regex
);
5123 pp
->error_signaled
= TRUE
;
5130 /* Match occurred. Construct a tag. */
5131 if (pp
->name_pattern
[0] != '\0')
5133 /* Make a named tag. */
5134 char *name
= substitute (lbp
->buffer
,
5135 pp
->name_pattern
, &pp
->regs
);
5137 pfnote (name
, TRUE
, lbp
->buffer
, match
, lineno
, linecharno
);
5141 /* Make an unnamed tag. */
5142 pfnote ((char *)NULL
, TRUE
,
5143 lbp
->buffer
, match
, lineno
, linecharno
);
5148 #endif /* ETAGS_REGEXPS */
5154 * Return a pointer to a space of size strlen(cp)+1 allocated
5155 * with xnew where the string CP has been copied.
5161 return savenstr (cp
, strlen (cp
));
5165 * Return a pointer to a space of size LEN+1 allocated with xnew where
5166 * the string CP has been copied for at most the first LEN characters.
5175 dp
= xnew (len
+ 1, char);
5176 strncpy (dp
, cp
, len
);
5182 * Return the ptr in sp at which the character c last
5183 * appears; NULL if not found
5185 * Identical to POSIX strrchr, included for portability.
5188 etags_strrchr (sp
, c
)
5189 register const char *sp
;
5192 register const char *r
;
5205 * Return the ptr in sp at which the character c first
5206 * appears; NULL if not found
5208 * Identical to POSIX strchr, included for portability.
5211 etags_strchr (sp
, c
)
5212 register const char *sp
;
5223 /* Skip spaces, return new pointer. */
5228 while (iswhite (*cp
))
5233 /* Skip non spaces, return new pointer. */
5235 skip_non_spaces (cp
)
5238 while (*cp
!= '\0' && !iswhite (*cp
))
5243 /* Print error message and exit. */
5261 suggest_asking_for_help ()
5263 fprintf (stderr
, "\tTry `%s %s' for a complete list of options.\n",
5274 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
5277 const char *s1
, *s2
;
5279 fprintf (stderr
, "%s: ", progname
);
5280 fprintf (stderr
, s1
, s2
);
5281 fprintf (stderr
, "\n");
5284 /* Return a newly-allocated string whose contents
5285 concatenate those of s1, s2, s3. */
5290 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
5291 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
5293 strcpy (result
, s1
);
5294 strcpy (result
+ len1
, s2
);
5295 strcpy (result
+ len1
+ len2
, s3
);
5296 result
[len1
+ len2
+ len3
] = '\0';
5301 /* Does the same work as the system V getcwd, but does not need to
5302 guess the buffer size in advance. */
5308 char *path
= xnew (bufsize
, char);
5310 while (getcwd (path
, bufsize
) == NULL
)
5312 if (errno
!= ERANGE
)
5316 path
= xnew (bufsize
, char);
5319 canonicalize_filename (path
);
5322 #else /* not HAVE_GETCWD */
5324 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
5328 for (p
= path
; *p
!= '\0'; p
++)
5334 return strdup (path
);
5335 #else /* not MSDOS */
5340 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
5341 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
5346 #endif /* not MSDOS */
5347 #endif /* not HAVE_GETCWD */
5350 /* Return a newly allocated string containing the file name of FILE
5351 relative to the absolute directory DIR (which should end with a slash). */
5353 relative_filename (file
, dir
)
5356 char *fp
, *dp
, *afn
, *res
;
5359 /* Find the common root of file and dir (with a trailing slash). */
5360 afn
= absolute_filename (file
, cwd
);
5363 while (*fp
++ == *dp
++)
5365 fp
--, dp
--; /* back to the first differing char */
5367 if (fp
== afn
&& afn
[0] != '/') /* cannot build a relative name */
5370 do /* look at the equal chars until '/' */
5374 /* Build a sequence of "../" strings for the resulting relative file name. */
5376 while ((dp
= etags_strchr (dp
+ 1, '/')) != NULL
)
5378 res
= xnew (3*i
+ strlen (fp
+ 1) + 1, char);
5381 strcat (res
, "../");
5383 /* Add the file name relative to the common root of file and dir. */
5384 strcat (res
, fp
+ 1);
5390 /* Return a newly allocated string containing the absolute file name
5391 of FILE given DIR (which should end with a slash). */
5393 absolute_filename (file
, dir
)
5396 char *slashp
, *cp
, *res
;
5398 if (filename_is_absolute (file
))
5399 res
= savestr (file
);
5401 /* We don't support non-absolute file names with a drive
5402 letter, like `d:NAME' (it's too much hassle). */
5403 else if (file
[1] == ':')
5404 fatal ("%s: relative file names with drive letters not supported", file
);
5407 res
= concat (dir
, file
, "");
5409 /* Delete the "/dirname/.." and "/." substrings. */
5410 slashp
= etags_strchr (res
, '/');
5411 while (slashp
!= NULL
&& slashp
[0] != '\0')
5413 if (slashp
[1] == '.')
5415 if (slashp
[2] == '.'
5416 && (slashp
[3] == '/' || slashp
[3] == '\0'))
5421 while (cp
>= res
&& !filename_is_absolute (cp
));
5423 cp
= slashp
; /* the absolute name begins with "/.." */
5425 /* Under MSDOS and NT we get `d:/NAME' as absolute
5426 file name, so the luser could say `d:/../NAME'.
5427 We silently treat this as `d:/NAME'. */
5428 else if (cp
[0] != '/')
5431 strcpy (cp
, slashp
+ 3);
5435 else if (slashp
[2] == '/' || slashp
[2] == '\0')
5437 strcpy (slashp
, slashp
+ 2);
5442 slashp
= etags_strchr (slashp
+ 1, '/');
5446 return savestr ("/");
5451 /* Return a newly allocated string containing the absolute
5452 file name of dir where FILE resides given DIR (which should
5453 end with a slash). */
5455 absolute_dirname (file
, dir
)
5461 canonicalize_filename (file
);
5462 slashp
= etags_strrchr (file
, '/');
5464 return savestr (dir
);
5467 res
= absolute_filename (file
, dir
);
5473 /* Whether the argument string is an absolute file name. The argument
5474 string must have been canonicalized with canonicalize_filename. */
5476 filename_is_absolute (fn
)
5479 return (fn
[0] == '/'
5481 || (ISALPHA(fn
[0]) && fn
[1] == ':' && fn
[2] == '/')
5486 /* Translate backslashes into slashes. Works in place. */
5488 canonicalize_filename (fn
)
5492 /* Canonicalize drive letter case. */
5493 if (fn
[0] && fn
[1] == ':' && ISLOWER (fn
[0]))
5494 fn
[0] = UPCASE (fn
[0]);
5495 /* Convert backslashes to slashes. */
5496 for (; *fn
!= '\0'; fn
++)
5501 fn
= NULL
; /* shut up the compiler */
5505 /* Increase the size of a linebuffer. */
5507 grow_linebuffer (lbp
, toksize
)
5511 while (lbp
->size
< toksize
)
5513 lbp
->buffer
= xrnew (lbp
->buffer
, lbp
->size
, char);
5516 /* Like malloc but get fatal error if memory is exhausted. */
5521 long *result
= (long *) malloc (size
);
5523 fatal ("virtual memory exhausted", (char *)NULL
);
5528 xrealloc (ptr
, size
)
5532 long *result
= (long *) realloc (ptr
, size
);
5534 fatal ("virtual memory exhausted", (char *)NULL
);