1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 87, 88, 89, 93, 94, 95
3 Free Software Foundation, Inc. and Ken Arnold
4 This file is not considered part of GNU Emacs.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 * Ctags originally by Ken Arnold.
23 * Fortran added by Jim Kleckner.
24 * Ed Pelegri-Llopart added C typedefs.
25 * Gnu Emacs TAGS format and modifications by RMS?
26 * Sam Kendall added C++.
27 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
29 * Regexp tags by Tom Tromey.
32 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
35 char pot_etags_version
[] = "@(#) pot revision number is 11.45";
45 #include <sys/param.h>
52 #define MAXPATHLEN _MAX_PATH
57 /* On some systems, Emacs defines static as nothing for the sake
58 of unexec. We don't want that here since we don't use unexec. */
68 #include <sys/types.h>
71 #if !defined (S_ISREG) && defined (S_IFREG)
72 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
79 #endif /* ETAGS_REGEXPS */
81 /* Define CTAGS to make the program "ctags" compatible with the usual one.
82 Let it undefined to make the program "etags", which makes emacs-style
83 tag tables and tags typedefs, #defines and struct/union/enum by default. */
91 /* Exit codes for success and failure. */
101 #define C_PLPL 0x00001 /* C++ */
102 #define C_STAR 0x00003 /* C* */
103 #define YACC 0x10000 /* yacc file */
105 #define streq(s,t) (strcmp (s, t) == 0)
106 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
108 #define lowcase(c) tolower ((unsigned char)c)
110 #define iswhite(arg) (_wht[arg]) /* T if char is white */
111 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
112 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
113 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
116 # define absolutefn(fn) (fn[0] == '/' || (isalpha (fn[0]) && fn[1] == ':'))
118 # define absolutefn(fn) (fn[0] == '/')
123 * xnew -- allocate storage
125 * SYNOPSIS: Type *xnew (int n, Type);
127 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
132 { /* sorting structure */
133 char *name
; /* function or type name */
134 char *file
; /* file name */
135 logical is_func
; /* use pattern or line no */
136 logical been_warned
; /* set if noticed dup */
137 int lno
; /* line number tag is on */
138 long cno
; /* character number line starts on */
139 char *pat
; /* search pattern */
140 struct nd_st
*left
, *right
; /* left and right sons */
143 extern char *getenv ();
146 char *savenstr (), *savestr ();
147 char *etags_strchr (), *etags_strrchr ();
148 char *etags_getcwd ();
149 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
150 long *xmalloc (), *xrealloc ();
152 typedef void Lang_function ();
153 #if FALSE /* many compilers barf on this */
154 Lang_function Asm_labels
;
155 Lang_function default_C_entries
;
156 Lang_function C_entries
;
157 Lang_function Cplusplus_entries
;
158 Lang_function Cstar_entries
;
159 Lang_function Fortran_functions
;
160 Lang_function Yacc_entries
;
161 Lang_function Lisp_functions
;
162 Lang_function Pascal_functions
;
163 Lang_function Perl_functions
;
164 Lang_function Prolog_functions
;
165 Lang_function Scheme_functions
;
166 Lang_function TeX_functions
;
167 Lang_function just_read_file
;
168 #else /* so let's write it this way */
171 void default_C_entries ();
172 void plain_C_entries ();
173 void Cplusplus_entries ();
174 void Cstar_entries ();
175 void Fortran_functions ();
176 void Yacc_entries ();
177 void Lisp_functions ();
178 void Pascal_functions ();
179 void Perl_functions ();
180 void Prolog_functions ();
181 void Scheme_functions ();
182 void TeX_functions ();
183 void just_read_file ();
186 Lang_function
*get_language_from_name ();
187 Lang_function
*get_language_from_interpreter ();
188 Lang_function
*get_language_from_suffix ();
189 int total_size_of_entries ();
191 long readline_internal ();
197 void fatal (), pfatal ();
198 void find_entries ();
204 void process_file ();
209 char searchar
= '/'; /* use /.../ searches */
211 int lineno
; /* line number of current line */
212 long charno
; /* current character number */
214 long linecharno
; /* charno of start of line; not used by C,
215 but by every other language. */
217 char *curfile
; /* current input file name */
218 char *tagfile
; /* output file */
219 char *progname
; /* name this program was invoked with */
220 char *cwd
; /* current working directory */
221 char *tagfiledir
; /* directory of tagfile */
223 FILE *tagf
; /* ioptr for tags file */
224 NODE
*head
; /* the head of the binary tree of tags */
227 * A `struct linebuffer' is a structure which holds a line of text.
228 * `readline' reads a line from a stream into a linebuffer and works
229 * regardless of the length of the line.
237 struct linebuffer lb
; /* the current line */
238 struct linebuffer token_name
; /* used by C_entries as temporary area */
242 struct linebuffer lb
; /* used by C_entries instead of lb */
245 /* boolean "functions" (see init) */
246 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
248 *white
= " \f\t\n\013", /* white chars */
249 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
250 /* token starting chars */
251 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
252 /* valid in-token chars */
253 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
255 logical append_to_tagfile
; /* -a: append to tags */
256 /* The following three default to TRUE for etags, but to FALSE for ctags. */
257 logical typedefs
; /* -t: create tags for typedefs */
258 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
259 /* 0 struct/enum/union decls, and C++ */
260 /* member functions. */
261 logical constantypedefs
; /* -d: create tags for C #define and enum */
262 /* constants. Enum consts not implemented. */
263 /* -D: opposite of -d. Default under ctags. */
264 logical update
; /* -u: update tags */
265 logical vgrind_style
; /* -v: create vgrind style index output */
266 logical no_warnings
; /* -w: suppress warnings */
267 logical cxref_style
; /* -x: create cxref style output */
268 logical cplusplus
; /* .[hc] means C++, not C */
269 logical noindentypedefs
; /* -I: ignore indentation in C */
271 struct option longopts
[] =
273 { "append", no_argument
, NULL
, 'a' },
274 { "backward-search", no_argument
, NULL
, 'B' },
275 { "c++", no_argument
, NULL
, 'C' },
276 { "cxref", no_argument
, NULL
, 'x' },
277 { "defines", no_argument
, NULL
, 'd' },
278 { "help", no_argument
, NULL
, 'h' },
279 { "help", no_argument
, NULL
, 'H' },
280 { "ignore-indentation", no_argument
, NULL
, 'I' },
281 { "include", required_argument
, NULL
, 'i' },
282 { "language", required_argument
, NULL
, 'l' },
283 { "no-defines", no_argument
, NULL
, 'D' },
284 { "no-regex", no_argument
, NULL
, 'R' },
285 { "no-warn", no_argument
, NULL
, 'w' },
286 { "output", required_argument
, NULL
, 'o' },
287 { "regex", required_argument
, NULL
, 'r' },
288 { "typedefs", no_argument
, NULL
, 't' },
289 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
290 { "update", no_argument
, NULL
, 'u' },
291 { "version", no_argument
, NULL
, 'V' },
292 { "vgrind", no_argument
, NULL
, 'v' },
297 /* Structure defining a regular expression. Elements are
298 the compiled pattern, and the name string. */
301 struct re_pattern_buffer
*pattern
;
302 struct re_registers regs
;
304 logical error_signaled
;
307 /* Number of regexps found. */
308 int num_patterns
= 0;
310 /* Array of all regexps. */
311 struct pattern
*patterns
= NULL
;
312 #endif /* ETAGS_REGEXPS */
318 /* Non-NULL if language fixed. */
319 Lang_function
*lang_func
= NULL
;
322 char *Asm_suffixes
[] = { "a", /* Unix assembler */
323 "asm", /* Microcontroller assembly */
324 "def", /* BSO/Tasking definition includes */
325 "inc", /* Microcontroller include files */
326 "ins", /* Microcontroller include files */
327 "s", "sa", /* Unix assembler */
328 "src", /* BSO/Tasking C compiler output */
332 /* Note that .c and .h can be considered C++, if the --c++ flag was
333 given. That is why default_C_entries is called here. */
334 char *default_C_suffixes
[] =
338 char *Cplusplus_suffixes
[] =
339 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", NULL
};
342 char *Cstar_suffixes
[] =
343 { "cs", "hs", NULL
};
346 char *Fortran_suffixes
[] =
347 { "F", "f", "f90", "for", NULL
};
349 /* Lisp source code */
350 char *Lisp_suffixes
[] =
351 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL
};
354 char *Pascal_suffixes
[] =
355 { "p", "pas", NULL
};
358 char *Perl_suffixes
[] =
359 { "pl", "pm", NULL
};
360 char *Perl_interpreters
[] =
364 char *plain_C_suffixes
[] =
367 /* Prolog source code */
368 char *Prolog_suffixes
[] =
371 /* Scheme source code */
372 /* FIXME Can't do the `SCM' or `scm' prefix with a version number */
373 char *Scheme_suffixes
[] =
374 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL
};
376 /* TeX/LaTeX source code */
377 char *TeX_suffixes
[] =
378 { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL
};
381 char *Yacc_suffixes
[] =
384 /* Table of language names and corresponding functions, file suffixes
385 and interpreter names.
386 It is ok for a given function to be listed under more than one
387 name. I just didn't. */
391 Lang_function
*function
;
396 struct lang_entry lang_names
[] =
398 { "asm", Asm_labels
, Asm_suffixes
},
399 { "c", default_C_entries
, default_C_suffixes
},
400 { "c++", Cplusplus_entries
, Cplusplus_suffixes
},
401 { "c*", Cstar_entries
, Cstar_suffixes
},
402 { "fortran", Fortran_functions
, Fortran_suffixes
},
403 { "lisp", Lisp_functions
, Lisp_suffixes
},
404 { "pascal", Pascal_functions
, Pascal_suffixes
},
405 { "perl", Perl_functions
, Perl_suffixes
, Perl_interpreters
},
406 { "proc", plain_C_entries
, plain_C_suffixes
},
407 { "prolog", Prolog_functions
, Prolog_suffixes
},
408 { "scheme" , Scheme_functions
, Scheme_suffixes
},
409 { "tex", TeX_functions
, TeX_suffixes
},
410 { "yacc", Yacc_entries
, Yacc_suffixes
},
411 { "auto", NULL
}, /* default guessing scheme */
412 { "none", just_read_file
}, /* regexp matching only */
413 { NULL
, NULL
} /* end of list */
418 print_language_names ()
420 struct lang_entry
*lang
;
423 puts ("\nThese are the currently supported languages, along with the\n\
424 default file name suffixes:");
425 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
427 printf ("\t%s\t", lang
->name
);
428 if (lang
->suffixes
!= NULL
)
429 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
430 printf (" .%s", *ext
);
433 puts ("Where `auto' means use default language for files based on file\n\
434 name suffix, and `none' means only do regexp processing on files.\n\
435 If no language is specified and no matching suffix is found,\n\
436 the first line of the file is read for a sharp-bang (#!) sequence\n\
437 followed by the name of an interpreter. If no such sequence is found,\n\
438 Fortran is tried first; if no tags are found, C is tried next.");
442 # define VERSION "19"
447 printf ("%s for Emacs version %s\n", (CTAGS
) ? "ctags" : "etags", VERSION
);
455 printf ("These are the options accepted by %s. You may use unambiguous\n\
456 abbreviations for the long option names. A - as file name means read\n\
457 names from stdin.\n\n", progname
);
459 puts ("-a, --append\n\
460 Append tag entries to existing tags file.");
463 puts ("-B, --backward-search\n\
464 Write the search commands for the tag entries using '?', the\n\
465 backward-search command instead of '/', the forward-search command.");
468 Treat files whose name suffix defaults to C language as C++ files.");
471 puts ("-d, --defines\n\
472 Create tag entries for constant C #defines, too.");
474 puts ("-D, --no-defines\n\
475 Don't create tag entries for constant C #defines. This makes\n\
476 the tags file smaller.");
480 puts ("-i FILE, --include=FILE\n\
481 Include a note in tag file indicating that, when searching for\n\
482 a tag, one should also consult the tags file FILE after\n\
483 checking the current file.");
484 puts ("-l LANG, --language=LANG\n\
485 Force the following files to be considered as written in the\n\
486 named language up to the next --language=LANG option.");
490 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
491 Make a tag for each line matching pattern REGEXP in the\n\
492 following files. REGEXP is anchored (as if preceded by ^).\n\
493 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
494 named tags can be created with:\n\
495 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
496 puts ("-R, --no-regex\n\
497 Don't create tags from regexps for the following files.");
498 #endif /* ETAGS_REGEXPS */
499 puts ("-o FILE, --output=FILE\n\
500 Write the tags to FILE.");
501 puts ("-I, --ignore-indentation\n\
502 Don't rely on indentation quite as much as normal. Currently,\n\
503 this means not to assume that a closing brace in the first\n\
504 column is the final brace of a function or structure\n\
505 definition in C and C++.");
509 puts ("-t, --typedefs\n\
510 Generate tag entries for C typedefs.");
511 puts ("-T, --typedefs-and-c++\n\
512 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
513 and C++ member functions.");
514 puts ("-u, --update\n\
515 Update the tag entries for the given files, leaving tag\n\
516 entries for other files in place. Currently, this is\n\
517 implemented by deleting the existing entries for the given\n\
518 files and then rewriting the new entries at the end of the\n\
519 tags file. It is often faster to simply rebuild the entire\n\
520 tag file than to use this.");
521 puts ("-v, --vgrind\n\
522 Generates an index of items intended for human consumption,\n\
523 similar to the output of vgrind. The index is sorted, and\n\
524 gives the page number of each item.");
525 puts ("-w, --no-warn\n\
526 Suppress warning messages about entries defined in multiple\n\
528 puts ("-x, --cxref\n\
529 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
530 The output uses line numbers instead of page numbers, but\n\
531 beyond that the differences are cosmetic; try both to see\n\
535 puts ("-V, --version\n\
536 Print the version of the program.\n\
538 Print this help message.");
540 print_language_names ();
553 /* This structure helps us allow mixing of --lang and filenames. */
556 enum argument_type arg_type
;
558 Lang_function
*function
;
561 #ifdef VMS /* VMS specific functions */
565 /* This is a BUG! ANY arbitrary limit is a BUG!
566 Won't someone please fix this? */
567 #define MAX_FILE_SPEC_LEN 255
570 char body
[MAX_FILE_SPEC_LEN
+ 1];
574 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
575 returning in each successive call the next filename matching the input
576 spec. The function expects that each in_spec passed
577 to it will be processed to completion; in particular, up to and
578 including the call following that in which the last matching name
579 is returned, the function ignores the value of in_spec, and will
580 only start processing a new spec with the following call.
581 If an error occurs, on return out_spec contains the value
582 of in_spec when the error occurred.
584 With each successive filename returned in out_spec, the
585 function's return value is one. When there are no more matching
586 names the function returns zero. If on the first call no file
587 matches in_spec, or there is any other error, -1 is returned.
592 #define OUTSIZE MAX_FILE_SPEC_LEN
598 static long context
= 0;
599 static struct dsc$descriptor_s o
;
600 static struct dsc$descriptor_s i
;
601 static logical pass1
= TRUE
;
608 o
.dsc$a_pointer
= (char *) out
;
609 o
.dsc$w_length
= (short)OUTSIZE
;
610 i
.dsc$a_pointer
= in
;
611 i
.dsc$w_length
= (short)strlen(in
);
612 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
613 i
.dsc$b_class
= DSC$K_CLASS_S
;
614 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
615 o
.dsc$b_class
= DSC$K_CLASS_VS
;
617 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
619 out
->body
[out
->curlen
] = EOS
;
622 else if (status
== RMS$_NMF
)
626 strcpy(out
->body
, in
);
629 lib$
find_file_end(&context
);
635 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
636 name of each file specified by the provided arg expanding wildcards.
639 gfnames (arg
, p_error
)
643 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
645 switch (fn_exp (&filename
, arg
))
649 return filename
.body
;
655 return filename
.body
;
659 #ifndef OLD /* Newer versions of VMS do provide `system'. */
663 fprintf (stderr
, "system() function not implemented under VMS\n");
667 #define VERSION_DELIM ';'
668 char *massage_name (s
)
674 if (*s
== VERSION_DELIM
)
692 unsigned int nincluded_files
= 0;
693 char **included_files
= xnew (argc
, char *);
696 int current_arg
= 0, file_count
= 0;
697 struct linebuffer filename_lb
;
703 _fmode
= O_BINARY
; /* all of files are treated as binary files */
708 /* Allocate enough no matter what happens. Overkill, but each one
710 argbuffer
= xnew (argc
, argument
);
713 /* Set syntax for regular expression routines. */
714 re_set_syntax (RE_SYNTAX_EMACS
);
715 #endif /* ETAGS_REGEXPS */
718 * If etags, always find typedefs and structure tags. Why not?
719 * Also default is to find macro constants.
722 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
726 int opt
= getopt_long (argc
, argv
,
727 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
735 /* If getopt returns 0, then it has already processed a
736 long-named option. We should do nothing. */
740 /* This means that a filename has been seen. Record it. */
741 argbuffer
[current_arg
].arg_type
= at_filename
;
742 argbuffer
[current_arg
].what
= optarg
;
747 /* Common options. */
749 append_to_tagfile
= TRUE
;
755 constantypedefs
= TRUE
;
758 constantypedefs
= FALSE
;
760 case 'f': /* for compatibility with old makefiles */
764 fprintf (stderr
, "%s: -%c option may only be given once.\n",
771 case 'S': /* for backward compatibility */
772 noindentypedefs
= TRUE
;
775 argbuffer
[current_arg
].function
= get_language_from_name (optarg
);
776 if (argbuffer
[current_arg
].function
== NULL
)
778 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
782 argbuffer
[current_arg
].arg_type
= at_language
;
787 argbuffer
[current_arg
].arg_type
= at_regexp
;
788 argbuffer
[current_arg
].what
= optarg
;
792 argbuffer
[current_arg
].arg_type
= at_regexp
;
793 argbuffer
[current_arg
].what
= NULL
;
796 #endif /* ETAGS_REGEXPS */
808 typedefs
= typedefs_and_cplusplus
= TRUE
;
813 included_files
[nincluded_files
++] = optarg
;
838 for (; optind
< argc
; ++optind
)
840 argbuffer
[current_arg
].arg_type
= at_filename
;
841 argbuffer
[current_arg
].what
= argv
[optind
];
846 if (nincluded_files
== 0 && file_count
== 0)
848 fprintf (stderr
, "%s: No input files specified.\n", progname
);
851 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
858 tagfile
= CTAGS
? "tags" : "TAGS";
860 cwd
= etags_getcwd (); /* the current working directory */
862 if (streq (tagfile
, "-"))
868 tagfiledir
= absolute_dirname (tagfile
, cwd
);
871 init (); /* set up boolean "functions" */
874 initbuffer (&token_name
);
875 initbuffer (&lbs
[0].lb
);
876 initbuffer (&lbs
[1].lb
);
877 initbuffer (&filename_lb
);
881 if (streq (tagfile
, "-"))
884 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
890 * Loop through files finding functions.
892 for (i
= 0; i
< current_arg
; ++i
)
894 switch (argbuffer
[i
].arg_type
)
897 lang_func
= argbuffer
[i
].function
;
901 add_regex (argbuffer
[i
].what
);
906 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
910 error ("Can't find file %s\n", this_file
);
915 this_file
= massage_name (this_file
);
918 this_file
= argbuffer
[i
].what
;
920 /* Input file named "-" means read file names from stdin
922 if (streq (this_file
, "-"))
923 while (readline_internal (&filename_lb
, stdin
) > 0)
924 process_file (filename_lb
.buffer
);
926 process_file (this_file
);
936 while (nincluded_files
-- > 0)
937 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
943 /* If CTAGS, we are here. process_file did not write the tags yet,
944 because we want them ordered. Let's do it now. */
947 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
957 for (i
= 0; i
< current_arg
; ++i
)
959 if (argbuffer
[i
].arg_type
!= at_filename
)
962 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
963 tagfile
, argbuffer
[i
].what
, tagfile
);
964 if (system (cmd
) != GOOD
)
965 fatal ("failed to execute shell command");
967 append_to_tagfile
= TRUE
;
970 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
979 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
987 * Return a Lang_function given the name.
990 get_language_from_name (name
)
993 struct lang_entry
*lang
;
997 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
999 if (streq (name
, lang
->name
))
1000 return lang
->function
;
1008 * Return a Lang_function given the interpreter name.
1011 get_language_from_interpreter (interpreter
)
1014 struct lang_entry
*lang
;
1017 if (interpreter
== NULL
)
1019 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1020 if (lang
->interpreters
!= NULL
)
1021 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1022 if (streq (*iname
, interpreter
))
1023 return lang
->function
;
1031 * Return a Lang_function given the file suffix.
1034 get_language_from_suffix (suffix
)
1037 struct lang_entry
*lang
;
1042 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1043 if (lang
->suffixes
!= NULL
)
1044 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1045 if (streq (*ext
, suffix
))
1046 return lang
->function
;
1053 * This routine is called on each file argument.
1059 struct stat stat_buf
;
1062 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1064 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1067 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1069 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1072 inf
= fopen (file
, "r");
1079 find_entries (file
, inf
);
1085 if (absolutefn (file
))
1087 /* file is an absolute filename. Canonicalise it. */
1088 filename
= absolute_filename (file
, cwd
);
1092 /* file is a filename relative to cwd. Make it relative
1093 to the directory of the tags file. */
1094 filename
= relative_filename (file
, tagfiledir
);
1096 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1105 * This routine sets up the boolean pseudo-functions which work
1106 * by setting boolean flags dependent upon the corresponding character
1107 * Every char which is NOT in that string is not a white char. Therefore,
1108 * all of the array "_wht" is set to FALSE, and then the elements
1109 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1110 * of a char is TRUE if it is the string "white", else FALSE.
1118 for (i
= 0; i
< 0177; i
++)
1119 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1120 for (sp
= white
; *sp
; sp
++)
1122 for (sp
= endtk
; *sp
; sp
++)
1124 for (sp
= intk
; *sp
; sp
++)
1126 for (sp
= begtk
; *sp
; sp
++)
1128 _wht
[0] = _wht
['\n'];
1129 _etk
[0] = _etk
['\n'];
1130 _btk
[0] = _btk
['\n'];
1131 _itk
[0] = _itk
['\n'];
1135 * This routine opens the specified file and calls the function
1136 * which finds the function and type definitions.
1139 find_entries (file
, inf
)
1144 Lang_function
*function
;
1145 NODE
*old_last_node
;
1146 extern NODE
*last_node
;
1148 /* Memory leakage here: the memory block pointed by curfile is never
1149 released. The amount of memory leaked here is the sum of the
1150 lengths of the input file names. */
1151 curfile
= savestr (file
);
1153 /* If user specified a language, use it. */
1154 function
= lang_func
;
1155 if (function
!= NULL
)
1162 cp
= etags_strrchr (file
, '.');
1166 function
= get_language_from_suffix (cp
);
1167 if (function
!= NULL
)
1175 /* Look for sharp-bang as the first two characters. */
1176 if (readline_internal (&lb
, inf
) > 2
1177 && lb
.buffer
[0] == '#'
1178 && lb
.buffer
[1] == '!')
1182 /* Set lp to point at the first char after the last slash in the
1183 line or, if no slashes, at the first nonblank. Then set cp to
1184 the first successive blank and terminate the string. */
1185 lp
= etags_strrchr (lb
.buffer
+2, '/');
1189 for (lp
= lb
.buffer
+2; *lp
!= '\0' && isspace (*lp
); lp
++)
1191 for (cp
= lp
; *cp
!= '\0' && !isspace (*cp
); cp
++)
1195 if (strlen (lp
) > 0)
1197 function
= get_language_from_interpreter (lp
);
1198 if (function
!= NULL
)
1209 old_last_node
= last_node
;
1210 Fortran_functions (inf
);
1212 /* No Fortran entries found. Try C. */
1213 if (old_last_node
== last_node
)
1216 default_C_entries (inf
);
1224 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1225 char *name
; /* tag name, if different from definition */
1226 logical is_func
; /* tag is a function */
1227 char *linestart
; /* start of the line where tag is */
1228 int linelen
; /* length of the line where tag is */
1229 int lno
; /* line number */
1230 long cno
; /* character number */
1232 register NODE
*np
= xnew (1, NODE
);
1234 /* If ctags mode, change name "main" to M<thisfilename>. */
1235 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1237 register char *fp
= etags_strrchr (curfile
, '/');
1238 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1239 fp
= etags_strrchr (np
->name
, '.');
1240 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1245 np
->been_warned
= FALSE
;
1247 np
->is_func
= is_func
;
1249 /* Our char numbers are 0-base, because of C language tradition?
1250 ctags compatibility? old versions compatibility? I don't know.
1251 Anyway, since emacs's are 1-base we espect etags.el to take care
1252 of the difference. If we wanted to have 1-based numbers, we would
1253 uncomment the +1 below. */
1254 np
->cno
= cno
/* + 1 */ ;
1255 np
->left
= np
->right
= NULL
;
1256 np
->pat
= savenstr (linestart
, ((CTAGS
&& !cxref_style
) ? 50 : linelen
));
1258 add_node (np
, &head
);
1263 * recurse on left children, iterate on right children.
1267 register NODE
*node
;
1271 register NODE
*node_right
= node
->right
;
1272 free_tree (node
->left
);
1273 if (node
->name
!= NULL
)
1276 free ((char *) node
);
1283 * Adds a node to the tree of nodes. In etags mode, we don't keep
1284 * it sorted; we just keep a linear list. In ctags mode, maintain
1285 * an ordered tree, with no attempt at balancing.
1287 * add_node is the only function allowed to add nodes, so it can
1290 NODE
*last_node
= NULL
;
1292 add_node (node
, cur_node_p
)
1293 NODE
*node
, **cur_node_p
;
1296 register NODE
*cur_node
= *cur_node_p
;
1298 if (cur_node
== NULL
)
1308 if (last_node
== NULL
)
1309 fatal ("internal error in add_node", 0);
1310 last_node
->right
= node
;
1316 dif
= strcmp (node
->name
, cur_node
->name
);
1319 * If this tag name matches an existing one, then
1320 * do not add the node, but maybe print a warning.
1324 if (streq (node
->file
, cur_node
->file
))
1328 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1329 node
->file
, lineno
, node
->name
);
1330 fprintf (stderr
, "Second entry ignored\n");
1333 else if (!cur_node
->been_warned
&& !no_warnings
)
1337 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1338 node
->file
, cur_node
->file
, node
->name
);
1339 cur_node
->been_warned
= TRUE
;
1344 /* Actually add the node */
1345 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1351 register NODE
*node
;
1358 /* Output subentries that precede this one */
1359 put_entries (node
->left
);
1361 /* Output this entry */
1365 if (node
->name
!= NULL
)
1366 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1367 node
->pat
, node
->name
, node
->lno
, node
->cno
);
1369 fprintf (tagf
, "%s\177%d,%d\n",
1370 node
->pat
, node
->lno
, node
->cno
);
1372 else if (!cxref_style
)
1374 fprintf (tagf
, "%s\t%s\t",
1375 node
->name
, node
->file
);
1379 putc (searchar
, tagf
);
1382 for (sp
= node
->pat
; *sp
; sp
++)
1384 if (*sp
== '\\' || *sp
== searchar
)
1388 putc (searchar
, tagf
);
1391 { /* a typedef; text pattern inadequate */
1392 fprintf (tagf
, "%d", node
->lno
);
1396 else if (vgrind_style
)
1397 fprintf (stdout
, "%s %s %d\n",
1398 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1400 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1401 node
->name
, node
->lno
, node
->file
, node
->pat
);
1403 /* Output subentries that follow this one */
1404 put_entries (node
->right
);
1407 /* Length of a number's decimal representation. */
1415 for (; num
; num
/= 10)
1421 * Return total number of characters that put_entries will output for
1422 * the nodes in the subtree of the specified node. Works only if
1423 * we are not ctags, but called only in that case. This count
1424 * is irrelevant with the new tags.el, but is still supplied for
1425 * backward compatibility.
1428 total_size_of_entries (node
)
1429 register NODE
*node
;
1437 for (; node
; node
= node
->right
)
1439 /* Count left subentries. */
1440 total
+= total_size_of_entries (node
->left
);
1442 /* Count this entry */
1443 total
+= strlen (node
->pat
) + 1;
1444 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1445 if (node
->name
!= NULL
)
1446 total
+= 1 + strlen (node
->name
); /* \001name */
1453 * The C symbol tables.
1457 st_none
, st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1460 /* Feed stuff between (but not including) %[ and %] lines to:
1461 gperf -c -k1,3 -o -p -r -t
1463 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1465 class, C_PLPL, st_C_struct
1466 domain, C_STAR, st_C_struct
1467 union, 0, st_C_struct
1468 struct, 0, st_C_struct
1470 typedef, 0, st_C_typedef
1471 define, 0, st_C_define
1472 long, 0, st_C_typespec
1473 short, 0, st_C_typespec
1474 int, 0, st_C_typespec
1475 char, 0, st_C_typespec
1476 float, 0, st_C_typespec
1477 double, 0, st_C_typespec
1478 signed, 0, st_C_typespec
1479 unsigned, 0, st_C_typespec
1480 auto, 0, st_C_typespec
1481 void, 0, st_C_typespec
1482 extern, 0, st_C_typespec
1483 static, 0, st_C_typespec
1484 const, 0, st_C_typespec
1485 volatile, 0, st_C_typespec
1487 and replace lines between %< and %> with its output. */
1489 /* C code produced by gperf version 1.8.1 (K&R C version) */
1490 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1493 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1495 #define MIN_WORD_LENGTH 3
1496 #define MAX_WORD_LENGTH 8
1497 #define MIN_HASH_VALUE 10
1498 #define MAX_HASH_VALUE 62
1501 53 is the maximum key range
1509 static unsigned char hash_table
[] =
1511 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1512 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1513 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1514 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1515 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1516 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1517 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1518 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1519 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1520 62, 62, 62, 62, 62, 62, 62, 2, 62, 7,
1521 6, 9, 15, 30, 62, 24, 62, 62, 1, 24,
1522 7, 27, 13, 62, 19, 26, 18, 27, 1, 62,
1523 62, 62, 62, 62, 62, 62, 62, 62,
1525 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1528 struct C_stab_entry
*
1529 in_word_set (str
, len
)
1534 static struct C_stab_entry wordlist
[] =
1536 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1538 {"volatile", 0, st_C_typespec
},
1540 {"long", 0, st_C_typespec
},
1541 {"char", 0, st_C_typespec
},
1542 {"class", C_PLPL
, st_C_struct
},
1543 {"",}, {"",}, {"",}, {"",},
1544 {"const", 0, st_C_typespec
},
1545 {"",}, {"",}, {"",}, {"",},
1546 {"auto", 0, st_C_typespec
},
1548 {"define", 0, st_C_define
},
1550 {"void", 0, st_C_typespec
},
1551 {"",}, {"",}, {"",},
1552 {"extern", 0, st_C_typespec
},
1553 {"static", 0, st_C_typespec
},
1555 {"domain", C_STAR
, st_C_struct
},
1557 {"typedef", 0, st_C_typedef
},
1558 {"double", 0, st_C_typespec
},
1559 {"enum", 0, st_C_enum
},
1560 {"",}, {"",}, {"",}, {"",},
1561 {"int", 0, st_C_typespec
},
1563 {"float", 0, st_C_typespec
},
1564 {"",}, {"",}, {"",},
1565 {"struct", 0, st_C_struct
},
1566 {"",}, {"",}, {"",}, {"",},
1567 {"union", 0, st_C_struct
},
1569 {"short", 0, st_C_typespec
},
1571 {"unsigned", 0, st_C_typespec
},
1572 {"signed", 0, st_C_typespec
},
1575 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1577 register int key
= hash (str
, len
);
1579 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1581 register char *s
= wordlist
[key
].name
;
1583 if (*s
== *str
&& strneq (str
+ 1, s
+ 1, len
- 1))
1584 return &wordlist
[key
];
1592 C_symtype(str
, len
, c_ext
)
1597 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1599 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1605 * C functions are recognized using a simple finite automaton.
1606 * funcdef is its state variable.
1610 fnone
, /* nothing seen */
1611 ftagseen
, /* function-like tag seen */
1612 fstartlist
, /* just after open parenthesis */
1613 finlist
, /* in parameter list */
1614 flistseen
, /* after parameter list */
1615 fignore
/* before open brace */
1621 * typedefs are recognized using a simple finite automaton.
1622 * typeddef is its state variable.
1626 tnone
, /* nothing seen */
1627 ttypedseen
, /* typedef keyword seen */
1628 tinbody
, /* inside typedef body */
1629 tend
, /* just before typedef tag */
1630 tignore
/* junk after typedef tag */
1636 * struct-like structures (enum, struct and union) are recognized
1637 * using another simple finite automaton. `structdef' is its state
1642 snone
, /* nothing seen yet */
1643 skeyseen
, /* struct-like keyword seen */
1644 stagseen
, /* struct-like tag seen */
1645 scolonseen
, /* colon seen after struct-like tag */
1646 sinbody
/* in struct body: recognize member func defs*/
1651 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1652 * struct tag, and structtype is the type of the preceding struct-like
1655 char *structtag
= "<uninited>";
1656 enum sym_type structtype
;
1659 * Yet another little state machine to deal with preprocessor lines.
1663 dnone
, /* nothing seen */
1664 dsharpseen
, /* '#' seen as first char on line */
1665 ddefineseen
, /* '#' and 'define' seen */
1666 dignorerest
/* ignore rest of line */
1671 * Set this to TRUE, and the next token considered is called a function.
1672 * Used only for GNU emacs's function-defining macros.
1674 logical next_token_is_func
;
1677 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1683 * checks to see if the current token is at the start of a
1684 * function, or corresponds to a typedef, or is a struct/union/enum
1687 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1688 * C_EXT is which language we are looking at.
1690 * In the future we will need some way to adjust where the end of
1691 * the token is; for instance, implementing the C++ keyword
1692 * `operator' properly will adjust the end of the token to be after
1693 * whatever follows `operator'.
1700 * next_token_is_func IN OUT
1704 consider_token (str
, len
, c
, c_ext
, cblev
, is_func
)
1705 register char *str
; /* IN: token pointer */
1706 register int len
; /* IN: token length */
1707 register char c
; /* IN: first char after the token */
1708 int c_ext
; /* IN: C extensions mask */
1709 int cblev
; /* IN: curly brace level */
1710 logical
*is_func
; /* OUT: function found */
1712 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1715 * Advance the definedef state machine.
1720 /* We're not on a preprocessor line. */
1723 if (toktype
== st_C_define
)
1725 definedef
= ddefineseen
;
1729 definedef
= dignorerest
;
1734 * Make a tag for any macro, unless it is a constant
1735 * and constantypedefs is FALSE.
1737 definedef
= dignorerest
;
1738 *is_func
= (c
== '(');
1739 if (!*is_func
&& !constantypedefs
)
1746 error ("internal error: definedef value.", 0);
1755 if (toktype
== st_C_typedef
)
1758 typdef
= ttypedseen
;
1774 /* Do not return here, so the structdef stuff has a chance. */
1788 * This structdef business is currently only invoked when cblev==0.
1789 * It should be recursively invoked whatever the curly brace level,
1790 * and a stack of states kept, to allow for definitions of structs
1793 * This structdef business is NOT invoked when we are ctags and the
1794 * file is plain C. This is because a struct tag may have the same
1795 * name as another tag, and this loses with ctags.
1797 * This if statement deals with the typdef state machine as
1798 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1799 * return FALSE. All the other code here is for the structdef
1806 if (typdef
== ttypedseen
1807 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1809 structdef
= skeyseen
;
1810 structtype
= toktype
;
1814 if (structdef
== skeyseen
)
1816 /* Save the tag for struct/union/class, for functions that may be
1818 if (structtype
== st_C_struct
)
1819 structtag
= savenstr (str
, len
);
1821 structtag
= "<enum>";
1822 structdef
= stagseen
;
1826 /* Avoid entering funcdef stuff if typdef is going on. */
1827 if (typdef
!= tnone
)
1833 /* Detect GNU macros. */
1834 if (definedef
== dnone
)
1835 if (strneq (str
, "DEFUN", len
) /* Used in emacs */
1837 These are defined inside C functions
, so currently they
1839 || strneq (str
, "EXFUN", len
) /* Used in glibc */
1840 || strneq (str
, "DEFVAR_", 7) /* Used in emacs */
1842 || strneq (str
, "SYSCALL", len
) /* Used in glibc (mach) */
1843 || strneq (str
, "ENTRY", len
) /* Used in glibc */
1844 || strneq (str
, "PSEUDO", len
)) /* Used in glibc */
1847 next_token_is_func
= TRUE
;
1850 if (next_token_is_func
)
1852 next_token_is_func
= FALSE
;
1862 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1863 funcdef
= fnone
; /* should be useless */
1866 if (funcdef
== fnone
)
1879 * This routine finds functions, typedefs, #define's and
1880 * struct/union/enum definitions in C syntax and adds them
1894 #define current_lb_is_new (newndx == curndx)
1895 #define switch_line_buffers() (curndx = 1 - curndx)
1897 #define curlb (lbs[curndx].lb)
1898 #define othlb (lbs[1-curndx].lb)
1899 #define newlb (lbs[newndx].lb)
1900 #define curlinepos (lbs[curndx].linepos)
1901 #define othlinepos (lbs[1-curndx].linepos)
1902 #define newlinepos (lbs[newndx].linepos)
1904 #define CNL_SAVE_DEFINEDEF \
1906 curlinepos = charno; \
1908 charno += readline (&curlb, inf); \
1909 lp = curlb.buffer; \
1916 CNL_SAVE_DEFINEDEF; \
1917 if (savetok.valid) \
1920 savetok.valid = FALSE; \
1922 definedef = dnone; \
1925 #define make_tag(isfun) do \
1929 char *name = NULL; \
1931 name = savestr (token_name.buffer); \
1932 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
1934 else if (DEBUG) abort (); \
1935 tok.valid = FALSE; \
1939 C_entries (c_ext
, inf
)
1940 int c_ext
; /* extension of C */
1941 FILE *inf
; /* input file */
1943 register char c
; /* latest char read; '\0' for end of line */
1944 register char *lp
; /* pointer one beyond the character `c' */
1945 int curndx
, newndx
; /* indices for current and new lb */
1946 TOKEN tok
; /* latest token read */
1947 register int tokoff
; /* offset in line of start of current token */
1948 register int toklen
; /* length of current token */
1949 int cblev
; /* current curly brace level */
1950 int parlev
; /* current parenthesis level */
1951 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
1953 TOKEN savetok
; /* token saved during preprocessor handling */
1956 curndx
= newndx
= 0;
1962 definedef
= dnone
; funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
1963 next_token_is_func
= yacc_rules
= FALSE
;
1964 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1965 tok
.valid
= savetok
.valid
= FALSE
;
1968 cplpl
= c_ext
& C_PLPL
;
1975 /* If we're at the end of the line, the next character is a
1976 '\0'; don't skip it, because it's the thing that tells us
1977 to read the next line. */
1998 /* Newlines inside comments do not end macro definitions in
2013 /* Newlines inside strings do not end macro definitions
2014 in traditional cpp, even though compilers don't
2015 usually accept them. */
2026 /* Hmmm, something went wrong. */
2040 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2045 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2055 else if (cplpl
&& *lp
== '/')
2063 if ((c_ext
& YACC
) && *lp
== '%')
2065 /* entering or exiting rules section in yacc file */
2067 definedef
= dnone
; funcdef
= fnone
;
2068 typdef
= tnone
; structdef
= snone
;
2069 next_token_is_func
= FALSE
;
2070 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2072 yacc_rules
= !yacc_rules
;
2078 if (definedef
== dnone
)
2081 logical cpptoken
= TRUE
;
2083 /* Look back on this line. If all blanks, or nonblanks
2084 followed by an end of comment, this is a preprocessor
2086 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2089 if (*cp
== '*' && *(cp
+1) == '/')
2098 definedef
= dsharpseen
;
2099 } /* if (definedef == dnone) */
2105 /* Consider token only if some complicated conditions are satisfied. */
2106 if ((definedef
!= dnone
2107 || (cblev
== 0 && structdef
!= scolonseen
)
2108 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2109 && typdef
!= tignore
2110 && definedef
!= dignorerest
2111 && funcdef
!= finlist
)
2117 if (cplpl
&& c
== ':' && *lp
== ':' && begtoken(*(lp
+ 1)))
2120 * This handles :: in the middle, but not at the
2121 * beginning of an identifier.
2128 logical is_func
= FALSE
;
2131 || consider_token (newlb
.buffer
+ tokoff
, toklen
,
2132 c
, c_ext
, cblev
, &is_func
))
2134 if (structdef
== sinbody
2135 && definedef
== dnone
2137 /* function defined in C++ class body */
2139 int strsize
= strlen(structtag
) + 2 + toklen
+ 1;
2140 while (token_name
.size
< strsize
)
2142 token_name
.size
*= 2;
2144 = (char *) xrealloc (token_name
.buffer
,
2147 strcpy (token_name
.buffer
, structtag
);
2148 strcat (token_name
.buffer
, "::");
2149 strncat (token_name
.buffer
,
2150 newlb
.buffer
+tokoff
, toklen
);
2155 while (token_name
.size
< toklen
+ 1)
2157 token_name
.size
*= 2;
2159 = (char *) xrealloc (token_name
.buffer
,
2162 strncpy (token_name
.buffer
,
2163 newlb
.buffer
+tokoff
, toklen
);
2164 token_name
.buffer
[toklen
] = '\0';
2165 if (structdef
== stagseen
2168 && definedef
== dignorerest
)) /* macro */
2173 tok
.lineno
= lineno
;
2174 tok
.linelen
= tokoff
+ toklen
+ 1;
2175 tok
.buffer
= newlb
.buffer
;
2176 tok
.linepos
= newlinepos
;
2179 if (definedef
== dnone
2180 && (funcdef
== ftagseen
2181 || structdef
== stagseen
2184 if (current_lb_is_new
)
2185 switch_line_buffers ();
2192 } /* if (endtoken (c)) */
2193 else if (intoken (c
))
2198 } /* if (midtoken) */
2199 else if (begtoken (c
))
2217 if (structdef
== stagseen
)
2223 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2225 tokoff
= lp
- 1 - newlb
.buffer
;
2230 } /* if (begtoken) */
2231 } /* if must look at token */
2234 /* Detect end of line, colon, comma, semicolon and various braces
2235 after having handled a token.*/
2239 if (definedef
!= dnone
)
2241 if (structdef
== stagseen
)
2242 structdef
= scolonseen
;
2259 if (definedef
!= dnone
)
2270 if (funcdef
!= fignore
)
2273 /* The following instruction invalidates the token.
2274 Probably the token should be invalidated in all
2275 other cases where some state machine is reset. */
2278 if (structdef
== stagseen
)
2282 if (definedef
!= dnone
)
2284 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2286 if (structdef
== stagseen
)
2290 if (definedef
!= dnone
)
2292 if (cblev
== 0 && typdef
== tend
)
2298 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2300 if (structdef
== stagseen
)
2304 if (definedef
!= dnone
)
2313 /* Make sure that the next char is not a '*'.
2314 This handles constructs like:
2315 typedef void OperatorFun (int fun); */
2322 } /* switch (typdef) */
2325 funcdef
= fstartlist
;
2334 if (definedef
!= dnone
)
2342 funcdef
= flistseen
;
2345 if (cblev
== 0 && typdef
== tend
)
2351 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2355 if (definedef
!= dnone
)
2357 if (typdef
== ttypedseen
)
2361 case skeyseen
: /* unnamed struct */
2362 structtag
= "_anonymous_";
2363 structdef
= sinbody
;
2366 case scolonseen
: /* named struct */
2367 structdef
= sinbody
;
2380 /* Neutralize `extern "C" {' grot and look inside structs. */
2381 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2387 if (definedef
!= dnone
)
2389 if (funcdef
== fstartlist
)
2390 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2393 if (definedef
!= dnone
)
2395 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2397 cblev
= 0; /* reset curly brace level if first column */
2398 parlev
= 0; /* also reset paren level, just in case... */
2404 if (typdef
== tinbody
)
2406 /* Memory leakage here: the string pointed by structtag is
2407 never released, because I fear to miss something and
2408 break things while freeing the area. The amount of
2409 memory leaked here is the sum of the lenghts of the
2411 if (structdef == sinbody)
2412 free (structtag); */
2415 structtag
= "<error>";
2419 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
2420 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2421 if (definedef
!= dnone
)
2423 /* These surely cannot follow a function tag. */
2424 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2428 /* If a macro spans multiple lines don't reset its state. */
2436 } /* while not eof */
2440 * Process either a C++ file or a C file depending on the setting
2444 default_C_entries (inf
)
2447 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2450 /* Always do plain ANSI C. */
2452 plain_C_entries (inf
)
2458 /* Always do C++. */
2460 Cplusplus_entries (inf
)
2463 C_entries (C_PLPL
, inf
);
2471 C_entries (C_STAR
, inf
);
2474 /* Always do Yacc. */
2479 C_entries (YACC
, inf
);
2482 /* Fortran parsing */
2490 register int len
= 0;
2492 while (*cp
&& lowcase(*cp
) == lowcase(dbp
[len
]))
2494 if (*cp
== '\0' && !intoken(dbp
[len
]))
2505 while (isspace (*dbp
))
2510 while (isspace (*dbp
))
2512 if (strneq (dbp
, "(*)", 3))
2517 if (!isdigit (*dbp
))
2519 --dbp
; /* force failure */
2524 while (isdigit (*dbp
));
2533 while (isspace (*dbp
))
2538 linecharno
= charno
;
2539 charno
+= readline (&lb
, inf
);
2544 while (isspace (*dbp
))
2553 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2556 pfnote (NULL
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2560 Fortran_functions (inf
)
2569 linecharno
= charno
;
2570 charno
+= readline (&lb
, inf
);
2573 dbp
++; /* Ratfor escape to fortran */
2574 while (isspace (*dbp
))
2578 switch (lowcase (*dbp
))
2581 if (tail ("integer"))
2589 if (tail ("logical"))
2593 if (tail ("complex") || tail ("character"))
2597 if (tail ("double"))
2599 while (isspace (*dbp
))
2603 if (tail ("precision"))
2609 while (isspace (*dbp
))
2613 switch (lowcase (*dbp
))
2616 if (tail ("function"))
2620 if (tail ("subroutine"))
2628 if (tail ("program"))
2633 if (tail ("procedure"))
2641 * Bob Weiner, Motorola Inc., 4/3/94
2642 * Unix and microcontroller assembly tag handling
2643 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2657 linecharno
= charno
;
2658 charno
+= readline (&lb
, inf
);
2661 /* If first char is alphabetic or one of [_.$], test for colon
2662 following identifier. */
2663 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2665 /* Read past label. */
2667 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2669 if (*cp
== ':' || isspace (*cp
))
2671 /* Found end of label, so copy it and add it to the table. */
2673 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2680 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2681 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2684 Perl_functions (inf
)
2695 linecharno
= charno
;
2696 charno
+= readline (&lb
, inf
);
2699 if (*cp
++ == 's' && *cp
++ == 'u' && *cp
++ == 'b' && isspace(*cp
++))
2701 while (*cp
&& isspace(*cp
))
2703 while (*cp
&& ! isspace(*cp
) && *cp
!= '{')
2706 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2711 /* Added by Mosur Mohan, 4/22/88 */
2712 /* Pascal parsing */
2714 #define GET_NEW_LINE \
2716 linecharno = charno; lineno++; \
2717 charno += 1 + readline (&lb, inf); \
2722 * Locates tags for procedures & functions. Doesn't do any type- or
2723 * var-definitions. It does look for the keyword "extern" or
2724 * "forward" immediately following the procedure statement; if found,
2725 * the tag is skipped.
2728 Pascal_functions (inf
)
2731 struct linebuffer tline
; /* mostly copied from C_entries */
2733 int save_lineno
, save_len
;
2736 logical
/* each of these flags is TRUE iff: */
2737 incomment
, /* point is inside a comment */
2738 inquote
, /* point is inside '..' string */
2739 get_tagname
, /* point is after PROCEDURE/FUNCTION
2740 keyword, so next item = potential tag */
2741 found_tag
, /* point is after a potential tag */
2742 inparms
, /* point is within parameter-list */
2743 verify_tag
; /* point has passed the parm-list, so the
2744 next token will determine whether this
2745 is a FORWARD/EXTERN to be ignored, or
2746 whether it is a real tag */
2753 initbuffer (&tline
);
2755 incomment
= inquote
= FALSE
;
2756 found_tag
= FALSE
; /* have a proc name; check if extern */
2757 get_tagname
= FALSE
; /* have found "procedure" keyword */
2758 inparms
= FALSE
; /* found '(' after "proc" */
2759 verify_tag
= FALSE
; /* check if "extern" is ahead */
2761 /* long main loop to get next char */
2765 if (c
== '\0') /* if end of line */
2770 if (!((found_tag
&& verify_tag
) ||
2772 c
= *dbp
++; /* only if don't need *dbp pointing
2773 to the beginning of the name of
2774 the procedure or function */
2778 if (c
== '}') /* within { } comments */
2780 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
2797 inquote
= TRUE
; /* found first quote */
2799 case '{': /* found open { comment */
2803 if (*dbp
== '*') /* found open (* comment */
2808 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2811 case ')': /* end of parms list */
2816 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
2823 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
2825 /* check if this is an "extern" declaration */
2828 if (lowcase (*dbp
== 'e'))
2830 if (tail ("extern")) /* superfluous, really! */
2836 else if (lowcase (*dbp
) == 'f')
2838 if (tail ("forward")) /* check for forward reference */
2844 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
2849 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
2853 if (get_tagname
) /* grab name of proc or fn */
2860 /* save all values for later tagging */
2861 size
= strlen (lb
.buffer
) + 1;
2862 while (size
> tline
.size
)
2865 tline
.buffer
= (char *) xrealloc (tline
.buffer
, tline
.size
);
2867 strcpy (tline
.buffer
, lb
.buffer
);
2868 save_lineno
= lineno
;
2869 save_lcno
= linecharno
;
2871 /* grab block name */
2872 for (dbp
++; *dbp
&& (!endtoken (*dbp
)); dbp
++)
2874 save_len
= dbp
- lb
.buffer
+ 1;
2875 get_tagname
= FALSE
;
2879 /* and proceed to check for "extern" */
2881 else if (!incomment
&& !inquote
&& !found_tag
)
2883 /* check for proc/fn keywords */
2884 switch (lowcase (c
))
2887 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2891 if (tail ("unction"))
2896 } /* while not eof */
2898 free (tline
.buffer
);
2902 * lisp tag functions
2903 * look for (def or (DEF, quote or QUOTE
2907 register char *strp
;
2909 return ((strp
[1] == 'd' || strp
[1] == 'D')
2910 && (strp
[2] == 'e' || strp
[2] == 'E')
2911 && (strp
[3] == 'f' || strp
[3] == 'F'));
2916 register char *strp
;
2918 return ((*(++strp
) == 'q' || *strp
== 'Q')
2919 && (*(++strp
) == 'u' || *strp
== 'U')
2920 && (*(++strp
) == 'o' || *strp
== 'O')
2921 && (*(++strp
) == 't' || *strp
== 'T')
2922 && (*(++strp
) == 'e' || *strp
== 'E')
2923 && isspace(*(++strp
)));
2931 if (*dbp
== '\'') /* Skip prefix quote */
2933 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
2936 while (isspace(*dbp
))
2939 for (cp
= dbp
/*+1*/;
2940 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
2946 pfnote (NULL
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2950 Lisp_functions (inf
)
2959 linecharno
= charno
;
2960 charno
+= readline (&lb
, inf
);
2966 while (!isspace (*dbp
))
2968 while (isspace (*dbp
))
2974 /* Check for (foo::defmumble name-defined ... */
2977 while (*dbp
&& !isspace (*dbp
)
2978 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
2983 while (*dbp
== ':');
2985 if (L_isdef (dbp
- 1))
2987 while (!isspace (*dbp
))
2989 while (isspace (*dbp
))
3000 * Scheme tag functions
3001 * look for (def... xyzzy
3002 * look for (def... (xyzzy
3003 * look for (def ... ((...(xyzzy ....
3004 * look for (set! xyzzy
3010 Scheme_functions (inf
)
3019 linecharno
= charno
;
3020 charno
+= readline (&lb
, inf
);
3022 if (dbp
[0] == '(' &&
3023 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
3024 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3025 (dbp
[3] == 'F' || dbp
[3] == 'f'))
3027 while (!isspace (*dbp
))
3029 /* Skip over open parens and white space */
3030 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
3034 if (dbp
[0] == '(' &&
3035 (dbp
[1] == 'S' || dbp
[1] == 's') &&
3036 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3037 (dbp
[3] == 'T' || dbp
[3] == 't') &&
3038 (dbp
[4] == '!' || dbp
[4] == '!') &&
3041 while (!isspace (*dbp
))
3043 /* Skip over white space */
3044 while (isspace (*dbp
))
3058 /* Go till you get to white space or a syntactic break */
3060 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
3063 pfnote (NULL
, TRUE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3066 /* Find tags in TeX and LaTeX input files. */
3068 /* TEX_toktab is a table of TeX control sequences that define tags.
3069 Each TEX_tabent records one such control sequence.
3070 CONVERT THIS TO USE THE Stab TYPE!! */
3077 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
3079 /* Default set of control sequences to put into TEX_toktab.
3080 The value of environment var TEXTAGS is prepended to this. */
3082 char *TEX_defenv
= "\
3083 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3084 :part:appendix:entry:index";
3087 struct TEX_tabent
*TEX_decode_env ();
3089 #if TeX_named_tokens
3093 char TEX_esc
= '\\';
3094 char TEX_opgrp
= '{';
3095 char TEX_clgrp
= '}';
3098 * TeX/LaTeX scanning loop.
3109 /* Select either \ or ! as escape character. */
3112 /* Initialize token table once from environment. */
3114 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
3117 { /* Scan each line in file */
3119 linecharno
= charno
;
3120 charno
+= readline (&lb
, inf
);
3123 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
3129 linecharno
+= dbp
- lasthit
;
3131 i
= TEX_Token (lasthit
);
3135 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3136 #if TeX_named_tokens
3137 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
3139 break; /* We only save a line once */
3145 #define TEX_LESC '\\'
3146 #define TEX_SESC '!'
3149 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3150 chars accordingly. */
3157 while ((c
= getc (inf
)) != EOF
)
3159 /* Skip to next line if we hit the TeX comment char. */
3163 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3182 /* Read environment and prepend it to the default string.
3183 Build token table. */
3185 TEX_decode_env (evarname
, defenv
)
3189 register char *env
, *p
;
3191 struct TEX_tabent
*tab
;
3194 /* Append default string to environment. */
3195 env
= getenv (evarname
);
3199 env
= concat (env
, defenv
, "");
3201 /* Allocate a token table */
3202 for (size
= 1, p
= env
; p
;)
3203 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3205 /* Add 1 to leave room for null terminator. */
3206 tab
= xnew (size
+ 1, struct TEX_tabent
);
3208 /* Unpack environment string into token table. Be careful about */
3209 /* zero-length strings (leading ':', "::" and trailing ':') */
3212 p
= etags_strchr (env
, ':');
3213 if (!p
) /* End of environment string. */
3214 p
= env
+ strlen (env
);
3216 { /* Only non-zero strings. */
3217 tab
[i
].name
= savenstr (env
, p
- env
);
3218 tab
[i
].len
= strlen (tab
[i
].name
);
3225 tab
[i
].name
= NULL
; /* Mark end of table. */
3233 #if TeX_named_tokens
3234 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3235 The name being defined actually starts at (NAME + LEN + 1).
3236 But we seem to include the TeX command in the tag name. */
3238 TEX_getit (name
, len
)
3242 char *p
= name
+ len
;
3247 /* Let tag name extend to next group close (or end of line) */
3248 while (*p
&& *p
!= TEX_clgrp
)
3250 pfnote (savenstr (name
, p
-name
), TRUE
,
3251 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3255 /* If the text at CP matches one of the tag-defining TeX command names,
3256 return the pointer to the first occurrence of that command in TEX_toktab.
3257 Otherwise return -1.
3258 Keep the capital `T' in `Token' for dumb truncating compilers
3259 (this distinguishes it from `TEX_toktab' */
3266 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3267 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3272 /* Support for Prolog. */
3274 /* Whole head (not only functor, but also arguments)
3275 is gotten in compound term. */
3288 if (s
[0] == '\0') /* syntax error. */
3290 else if (insquote
&& s
[0] == '\'' && s
[1] == '\'')
3292 else if (s
[0] == '\'')
3294 insquote
= !insquote
;
3297 else if (!insquote
&& s
[0] == '(')
3302 else if (!insquote
&& s
[0] == ')')
3308 else if (npar
< 0) /* syntax error. */
3311 else if (!insquote
&& s
[0] == '.'
3312 && (isspace (s
[1]) || s
[1] == '\0'))
3314 if (npar
!= 0) /* syntax error. */
3322 pfnote (NULL
, TRUE
, save_s
, s
-save_s
, lineno
, linecharno
);
3325 /* It is assumed that prolog predicate starts from column 0. */
3327 Prolog_functions (inf
)
3330 void skip_comment (), prolog_getit ();
3332 lineno
= linecharno
= charno
= 0;
3336 linecharno
+= charno
;
3337 charno
= readline (&lb
, inf
) + 1; /* 1 for newline. */
3339 if (isspace (dbp
[0])) /* not predicate header. */
3341 else if (dbp
[0] == '%') /* comment. */
3343 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3344 skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3351 skip_comment (plb
, inf
, plineno
, plinecharno
)
3352 struct linebuffer
*plb
;
3354 int *plineno
; /* result */
3355 long *plinecharno
; /* result */
3361 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3362 if (cp
[0] == '*' && cp
[1] == '/')
3365 *plinecharno
+= readline (plb
, inf
) + 1; /* 1 for newline. */
3370 #ifdef ETAGS_REGEXPS
3371 /* Take a string like "/blah/" and turn it into "blah", making sure
3372 that the first and last characters are the same, and handling
3373 quoted separator characters. Actually, stops on the occurence of
3374 an unquoted separator. Also turns "\t" into a Tab character.
3375 Returns pointer to terminating separator. Works in place. Null
3376 terminates name string. */
3378 scan_separators (name
)
3382 char *copyto
= name
;
3383 logical quoted
= FALSE
;
3385 for (++name
; *name
!= '\0'; ++name
)
3391 else if (*name
== sep
)
3395 /* Something else is quoted, so preserve the quote. */
3401 else if (*name
== '\\')
3403 else if (*name
== sep
)
3409 /* Terminate copied string. */
3414 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3415 expression, into a real regular expression by compiling it. */
3417 add_regex (regexp_pattern
)
3418 char *regexp_pattern
;
3422 struct re_pattern_buffer
*patbuf
;
3424 if (regexp_pattern
== NULL
)
3426 /* Remove existing regexps. */
3432 if (regexp_pattern
[0] == '\0')
3434 error ("missing regexp", 0);
3437 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3439 error ("%s: unterminated regexp", regexp_pattern
);
3442 name
= scan_separators (regexp_pattern
);
3443 if (regexp_pattern
[0] == '\0')
3445 error ("null regexp", 0);
3448 (void) scan_separators (name
);
3450 patbuf
= xnew (1, struct re_pattern_buffer
);
3451 patbuf
->translate
= NULL
;
3452 patbuf
->fastmap
= NULL
;
3453 patbuf
->buffer
= NULL
;
3454 patbuf
->allocated
= 0;
3456 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3459 error ("%s while compiling pattern", err
);
3464 if (num_patterns
== 1)
3465 patterns
= xnew (1, struct pattern
);
3467 patterns
= ((struct pattern
*)
3469 (num_patterns
* sizeof (struct pattern
))));
3470 patterns
[num_patterns
- 1].pattern
= patbuf
;
3471 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3472 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
3476 * Do the substitutions indicated by the regular expression and
3480 substitute (in
, out
, regs
)
3482 struct re_registers
*regs
;
3484 char *result
= NULL
, *t
;
3487 /* Pass 1: figure out how much size to allocate. */
3488 for (t
= out
; *t
; ++t
)
3495 fprintf (stderr
, "%s: pattern subtitution ends prematurely\n",
3502 size
+= regs
->end
[dig
] - regs
->start
[dig
];
3507 /* Allocate space and do the substitutions. */
3508 result
= xnew (size
+ 1, char);
3517 /* Using "dig2" satisfies my debugger. Bleah. */
3518 int dig2
= *out
- '0';
3519 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
3520 regs
->end
[dig2
] - regs
->start
[dig2
]);
3521 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
3524 result
[size
++] = *out
;
3527 result
[size
++] = *out
;
3529 result
[size
] = '\0';
3534 #endif /* ETAGS_REGEXPS */
3535 /* Initialize a linebuffer for use */
3537 initbuffer (linebuffer
)
3538 struct linebuffer
*linebuffer
;
3540 linebuffer
->size
= 200;
3541 linebuffer
->buffer
= xnew (200, char);
3545 * Read a line of text from `stream' into `linebuffer'.
3546 * Return the number of characters read from `stream',
3547 * which is the length of the line including the newline, if any.
3550 readline_internal (linebuffer
, stream
)
3551 struct linebuffer
*linebuffer
;
3552 register FILE *stream
;
3554 char *buffer
= linebuffer
->buffer
;
3555 register char *p
= linebuffer
->buffer
;
3556 register char *pend
;
3559 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
3563 register int c
= getc (stream
);
3566 linebuffer
->size
*= 2;
3567 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
3568 p
+= buffer
- linebuffer
->buffer
;
3569 pend
= buffer
+ linebuffer
->size
;
3570 linebuffer
->buffer
= buffer
;
3579 if (p
> buffer
&& p
[-1] == '\r')
3594 return p
- buffer
+ chars_deleted
;
3598 * Like readline_internal, above, but try to match the input
3599 * line against any existing regular expressions.
3602 readline (linebuffer
, stream
)
3603 struct linebuffer
*linebuffer
;
3606 /* Read new line. */
3608 long result
= readline_internal (linebuffer
, stream
);
3610 #ifdef ETAGS_REGEXPS
3611 /* Match against all listed patterns. */
3612 for (i
= 0; i
< num_patterns
; ++i
)
3614 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
3615 (int)result
, 0, &patterns
[i
].regs
);
3620 if (!patterns
[i
].error_signaled
)
3622 error ("error while matching pattern %d", i
);
3623 patterns
[i
].error_signaled
= TRUE
;
3630 /* Match occurred. Construct a tag. */
3631 if (patterns
[i
].name_pattern
[0] != '\0')
3633 /* Make a named tag. */
3634 char *name
= substitute (linebuffer
->buffer
,
3635 patterns
[i
].name_pattern
,
3639 linebuffer
->buffer
, match
, lineno
, linecharno
);
3643 /* Make an unnamed tag. */
3645 linebuffer
->buffer
, match
, lineno
, linecharno
);
3650 #endif /* ETAGS_REGEXPS */
3656 * Read a file, but do no processing. This is used to do regexp
3657 * matching on files that have no language defined.
3660 just_read_file (inf
)
3666 linecharno
= charno
;
3667 charno
+= readline (&lb
, inf
) + 1;
3673 * Return a pointer to a space of size strlen(cp)+1 allocated
3674 * with xnew where the string CP has been copied.
3680 return savenstr (cp
, strlen (cp
));
3684 * Return a pointer to a space of size LEN+1 allocated with xnew where
3685 * the string CP has been copied for at most the first LEN characters.
3694 dp
= xnew (len
+ 1, char);
3695 strncpy (dp
, cp
, len
);
3701 * Return the ptr in sp at which the character c last
3702 * appears; NULL if not found
3704 * Identical to System V strrchr, included for portability.
3707 etags_strrchr (sp
, c
)
3708 register char *sp
, c
;
3723 * Return the ptr in sp at which the character c first
3724 * appears; NULL if not found
3726 * Identical to System V strchr, included for portability.
3729 etags_strchr (sp
, c
)
3730 register char *sp
, c
;
3740 /* Print error message and exit. */
3757 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3762 fprintf (stderr
, "%s: ", progname
);
3763 fprintf (stderr
, s1
, s2
);
3764 fprintf (stderr
, "\n");
3767 /* Return a newly-allocated string whose contents
3768 concatenate those of s1, s2, s3. */
3773 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3774 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
3776 strcpy (result
, s1
);
3777 strcpy (result
+ len1
, s2
);
3778 strcpy (result
+ len1
+ len2
, s3
);
3779 result
[len1
+ len2
+ len3
] = '\0';
3784 /* Does the same work as the system V getcwd, but does not need to
3785 guess the buffer size in advance. */
3790 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
3798 *p
++ = lowcase (*p
);
3800 return strdup (path
);
3801 #else /* not DOS_NT */
3804 char *path
= xnew (bufsize
, char);
3806 while (getcwd (path
, bufsize
) == NULL
)
3808 if (errno
!= ERANGE
)
3811 path
= xnew (bufsize
, char);
3815 #else /* not DOS_NT and not HAVE_GETCWD */
3816 struct linebuffer path
;
3820 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
3821 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
3826 #endif /* not HAVE_GETCWD */
3827 #endif /* not DOS_NT */
3830 /* Return a newly allocated string containing the filename
3831 of FILE relative to the absolute directory DIR (which
3832 should end with a slash). */
3834 relative_filename (file
, dir
)
3837 char *fp
, *dp
, *abs
, *res
;
3839 /* Find the common root of file and dir. */
3840 abs
= absolute_filename (file
, cwd
);
3843 while (*fp
++ == *dp
++)
3852 /* Build a sequence of "../" strings for the resulting relative filename. */
3853 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
3855 dp
= etags_strchr (dp
+ 1, '/'))
3857 res
= concat (res
, "../", "");
3860 /* Add the filename relative to the common root of file and dir. */
3861 res
= concat (res
, fp
+ 1, "");
3867 /* Return a newly allocated string containing the
3868 absolute filename of FILE given CWD (which should
3869 end with a slash). */
3871 absolute_filename (file
, cwd
)
3874 char *slashp
, *cp
, *res
;
3876 if (absolutefn (file
))
3877 res
= concat (file
, "", "");
3879 res
= concat (cwd
, file
, "");
3881 /* Delete the "/dirname/.." and "/." substrings. */
3882 slashp
= etags_strchr (res
, '/');
3883 while (slashp
!= NULL
&& slashp
[0] != '\0')
3885 if (slashp
[1] == '.')
3887 if (slashp
[2] == '.'
3888 && (slashp
[3] == '/' || slashp
[3] == '\0'))
3893 while (cp
>= res
&& *cp
!= '/');
3896 strcpy (cp
, slashp
+ 3);
3898 else /* else (cp == res) */
3900 if (slashp
[3] != '\0')
3901 strcpy (cp
, slashp
+ 4);
3908 else if (slashp
[2] == '/' || slashp
[2] == '\0')
3910 strcpy (slashp
, slashp
+ 2);
3915 slashp
= etags_strchr (slashp
+ 1, '/');
3921 /* Return a newly allocated string containing the absolute
3922 filename of dir where FILE resides given CWD (which should
3923 end with a slash). */
3925 absolute_dirname (file
, cwd
)
3931 slashp
= etags_strrchr (file
, '/');
3936 res
= absolute_filename (file
, cwd
);
3942 /* Like malloc but get fatal error if memory is exhausted. */
3947 long *result
= (long *) malloc (size
);
3949 fatal ("virtual memory exhausted", 0);
3954 xrealloc (ptr
, size
)
3958 long *result
= (long *) realloc (ptr
, size
);
3960 fatal ("virtual memory exhausted");