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.
28 * Regexp tags by Tom Tromey.
30 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
33 char pot_etags_version
[] = "@(#) pot revision number is 11.53";
44 #include <sys/param.h>
51 #define MAXPATHLEN _MAX_PATH
56 /* On some systems, Emacs defines static as nothing for the sake
57 of unexec. We don't want that here since we don't use unexec. */
67 #include <sys/types.h>
70 #if !defined (S_ISREG) && defined (S_IFREG)
71 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
78 #endif /* ETAGS_REGEXPS */
80 /* Define CTAGS to make the program "ctags" compatible with the usual one.
81 Let it undefined to make the program "etags", which makes emacs-style
82 tag tables and tags typedefs, #defines and struct/union/enum by default. */
90 /* Exit codes for success and failure. */
100 #define C_PLPL 0x00001 /* C++ */
101 #define C_STAR 0x00003 /* C* */
102 #define YACC 0x10000 /* yacc file */
104 #define streq(s,t) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strcmp(s,t))
105 #define strneq(s,t,n) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strncmp(s,t,n))
107 #define lowcase(c) tolower ((unsigned char)c)
109 #define iswhite(arg) (_wht[arg]) /* T if char is white */
110 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
111 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
112 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
115 # define absolutefn(fn) (fn[0] == '/' || (isalpha (fn[0]) && fn[1] == ':'))
117 # define absolutefn(fn) (fn[0] == '/')
122 * xnew -- allocate storage
124 * SYNOPSIS: Type *xnew (int n, Type);
126 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
131 { /* sorting structure */
132 char *name
; /* function or type name */
133 char *file
; /* file name */
134 logical is_func
; /* use pattern or line no */
135 logical been_warned
; /* set if noticed dup */
136 int lno
; /* line number tag is on */
137 long cno
; /* character number line starts on */
138 char *pat
; /* search pattern */
139 struct nd_st
*left
, *right
; /* left and right sons */
142 extern char *getenv ();
145 char *savenstr (), *savestr ();
146 char *etags_strchr (), *etags_strrchr ();
147 char *etags_getcwd ();
148 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
149 long *xmalloc (), *xrealloc ();
151 typedef void Lang_function ();
152 #if FALSE /* many compilers barf on this */
153 Lang_function Asm_labels
;
154 Lang_function default_C_entries
;
155 Lang_function C_entries
;
156 Lang_function Cplusplus_entries
;
157 Lang_function Cstar_entries
;
158 Lang_function Fortran_functions
;
159 Lang_function Yacc_entries
;
160 Lang_function Lisp_functions
;
161 Lang_function Pascal_functions
;
162 Lang_function Perl_functions
;
163 Lang_function Prolog_functions
;
164 Lang_function Scheme_functions
;
165 Lang_function TeX_functions
;
166 Lang_function just_read_file
;
167 #else /* so let's write it this way */
170 void default_C_entries ();
171 void plain_C_entries ();
172 void Cplusplus_entries ();
173 void Cstar_entries ();
174 void Fortran_functions ();
175 void Yacc_entries ();
176 void Lisp_functions ();
177 void Pascal_functions ();
178 void Perl_functions ();
179 void Prolog_functions ();
180 void Scheme_functions ();
181 void TeX_functions ();
182 void just_read_file ();
185 Lang_function
*get_language_from_name ();
186 Lang_function
*get_language_from_interpreter ();
187 Lang_function
*get_language_from_suffix ();
188 int total_size_of_entries ();
190 long readline_internal ();
196 void suggest_asking_for_help ();
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.
231 #define GROW_LINEBUFFER(buf,toksize) \
232 while (buf.size < toksize) \
233 buf.buffer = (char *) xrealloc (buf.buffer, buf.size *= 2)
240 struct linebuffer lb
; /* the current line */
241 struct linebuffer token_name
; /* used by C_entries as a temporary area */
245 struct linebuffer lb
; /* used by C_entries instead of lb */
248 /* boolean "functions" (see init) */
249 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
252 *white
= " \f\t\n\013",
253 /* token ending chars */
254 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
255 /* token starting chars */
256 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
257 /* valid in-token chars */
258 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
260 logical append_to_tagfile
; /* -a: append to tags */
261 /* The following three default to TRUE for etags, but to FALSE for ctags. */
262 logical typedefs
; /* -t: create tags for typedefs */
263 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
264 /* 0 struct/enum/union decls, and C++ */
265 /* member functions. */
266 logical constantypedefs
; /* -d: create tags for C #define and enum */
267 /* constants. Enum consts not implemented. */
268 /* -D: opposite of -d. Default under ctags. */
269 logical update
; /* -u: update tags */
270 logical vgrind_style
; /* -v: create vgrind style index output */
271 logical no_warnings
; /* -w: suppress warnings */
272 logical cxref_style
; /* -x: create cxref style output */
273 logical cplusplus
; /* .[hc] means C++, not C */
274 logical noindentypedefs
; /* -I: ignore indentation in C */
276 struct option longopts
[] =
278 { "append", no_argument
, NULL
, 'a' },
279 { "backward-search", no_argument
, NULL
, 'B' },
280 { "c++", no_argument
, NULL
, 'C' },
281 { "cxref", no_argument
, NULL
, 'x' },
282 { "defines", no_argument
, NULL
, 'd' },
283 { "help", no_argument
, NULL
, 'h' },
284 { "help", no_argument
, NULL
, 'H' },
285 { "ignore-indentation", no_argument
, NULL
, 'I' },
286 { "include", required_argument
, NULL
, 'i' },
287 { "language", required_argument
, NULL
, 'l' },
288 { "no-defines", no_argument
, NULL
, 'D' },
289 { "no-regex", no_argument
, NULL
, 'R' },
290 { "no-warn", no_argument
, NULL
, 'w' },
291 { "output", required_argument
, NULL
, 'o' },
292 { "regex", required_argument
, NULL
, 'r' },
293 { "typedefs", no_argument
, NULL
, 't' },
294 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
295 { "update", no_argument
, NULL
, 'u' },
296 { "version", no_argument
, NULL
, 'V' },
297 { "vgrind", no_argument
, NULL
, 'v' },
302 /* Structure defining a regular expression. Elements are
303 the compiled pattern, and the name string. */
306 struct re_pattern_buffer
*pattern
;
307 struct re_registers regs
;
309 logical error_signaled
;
312 /* Number of regexps found. */
313 int num_patterns
= 0;
315 /* Array of all regexps. */
316 struct pattern
*patterns
= NULL
;
317 #endif /* ETAGS_REGEXPS */
323 /* Non-NULL if language fixed. */
324 Lang_function
*lang_func
= NULL
;
327 char *Asm_suffixes
[] = { "a", /* Unix assembler */
328 "asm", /* Microcontroller assembly */
329 "def", /* BSO/Tasking definition includes */
330 "inc", /* Microcontroller include files */
331 "ins", /* Microcontroller include files */
332 "s", "sa", /* Unix assembler */
333 "src", /* BSO/Tasking C compiler output */
337 /* Note that .c and .h can be considered C++, if the --c++ flag was
338 given. That is why default_C_entries is called here. */
339 char *default_C_suffixes
[] =
342 /* .M is for Objective C++ files. */
343 char *Cplusplus_suffixes
[] =
344 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL
};
346 char *Cstar_suffixes
[] =
347 { "cs", "hs", NULL
};
349 char *Fortran_suffixes
[] =
350 { "F", "f", "f90", "for", NULL
};
352 char *Lisp_suffixes
[] =
353 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL
};
355 char *Pascal_suffixes
[] =
356 { "p", "pas", NULL
};
358 char *Perl_suffixes
[] =
359 { "pl", "pm", NULL
};
360 char *Perl_interpreters
[] =
361 { "perl", "@PERL@", NULL
};
363 char *plain_C_suffixes
[] =
364 { "pc", /* Pro*C file */
365 "m", /* Objective C file */
366 "lm", /* Objective lex file */
369 char *Prolog_suffixes
[] =
372 /* 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 char *TeX_suffixes
[] =
377 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL
};
379 char *Yacc_suffixes
[] =
380 { "y", "ym", NULL
}; /* .ym is Objective yacc file */
382 /* Table of language names and corresponding functions, file suffixes
383 and interpreter names.
384 It is ok for a given function to be listed under more than one
385 name. I just didn't. */
389 Lang_function
*function
;
394 struct lang_entry lang_names
[] =
396 { "asm", Asm_labels
, Asm_suffixes
, NULL
},
397 { "c", default_C_entries
, default_C_suffixes
, NULL
},
398 { "c++", Cplusplus_entries
, Cplusplus_suffixes
, NULL
},
399 { "c*", Cstar_entries
, Cstar_suffixes
, NULL
},
400 { "fortran", Fortran_functions
, Fortran_suffixes
, NULL
},
401 { "lisp", Lisp_functions
, Lisp_suffixes
, NULL
},
402 { "pascal", Pascal_functions
, Pascal_suffixes
, NULL
},
403 { "perl", Perl_functions
, Perl_suffixes
, Perl_interpreters
},
404 { "proc", plain_C_entries
, plain_C_suffixes
, NULL
},
405 { "prolog", Prolog_functions
, Prolog_suffixes
, NULL
},
406 { "scheme", Scheme_functions
, Scheme_suffixes
, NULL
},
407 { "tex", TeX_functions
, TeX_suffixes
, NULL
},
408 { "yacc", Yacc_entries
, Yacc_suffixes
, NULL
},
409 { "auto", NULL
}, /* default guessing scheme */
410 { "none", just_read_file
}, /* regexp matching only */
411 { NULL
, NULL
} /* end of list */
416 print_language_names ()
418 struct lang_entry
*lang
;
421 puts ("\nThese are the currently supported languages, along with the\n\
422 default file name suffixes:");
423 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
425 printf ("\t%s\t", lang
->name
);
426 if (lang
->suffixes
!= NULL
)
427 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
428 printf (" .%s", *ext
);
431 puts ("Where `auto' means use default language for files based on file\n\
432 name suffix, and `none' means only do regexp processing on files.\n\
433 If no language is specified and no matching suffix is found,\n\
434 the first line of the file is read for a sharp-bang (#!) sequence\n\
435 followed by the name of an interpreter. If no such sequence is found,\n\
436 Fortran is tried first; if no tags are found, C is tried next.");
440 # define VERSION "19"
445 printf ("%s for Emacs version %s\n", (CTAGS
) ? "ctags" : "etags", VERSION
);
453 printf ("These are the options accepted by %s. You may use unambiguous\n\
454 abbreviations for the long option names. A - as file name means read\n\
455 names from stdin.\n\n", progname
);
457 puts ("-a, --append\n\
458 Append tag entries to existing tags file.");
461 puts ("-B, --backward-search\n\
462 Write the search commands for the tag entries using '?', the\n\
463 backward-search command instead of '/', the forward-search command.");
466 Treat files whose name suffix defaults to C language as C++ files.");
469 puts ("-d, --defines\n\
470 Create tag entries for constant C #defines, too.");
472 puts ("-D, --no-defines\n\
473 Don't create tag entries for constant C #defines. This makes\n\
474 the tags file smaller.");
478 puts ("-i FILE, --include=FILE\n\
479 Include a note in tag file indicating that, when searching for\n\
480 a tag, one should also consult the tags file FILE after\n\
481 checking the current file.");
482 puts ("-l LANG, --language=LANG\n\
483 Force the following files to be considered as written in the\n\
484 named language up to the next --language=LANG option.");
488 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
489 Make a tag for each line matching pattern REGEXP in the\n\
490 following files. REGEXP is anchored (as if preceded by ^).\n\
491 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
492 named tags can be created with:\n\
493 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
494 puts ("-R, --no-regex\n\
495 Don't create tags from regexps for the following files.");
496 #endif /* ETAGS_REGEXPS */
497 puts ("-o FILE, --output=FILE\n\
498 Write the tags to FILE.");
499 puts ("-I, --ignore-indentation\n\
500 Don't rely on indentation quite as much as normal. Currently,\n\
501 this means not to assume that a closing brace in the first\n\
502 column is the final brace of a function or structure\n\
503 definition in C and C++.");
507 puts ("-t, --typedefs\n\
508 Generate tag entries for C typedefs.");
509 puts ("-T, --typedefs-and-c++\n\
510 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
511 and C++ member functions.");
512 puts ("-u, --update\n\
513 Update the tag entries for the given files, leaving tag\n\
514 entries for other files in place. Currently, this is\n\
515 implemented by deleting the existing entries for the given\n\
516 files and then rewriting the new entries at the end of the\n\
517 tags file. It is often faster to simply rebuild the entire\n\
518 tag file than to use this.");
519 puts ("-v, --vgrind\n\
520 Generates an index of items intended for human consumption,\n\
521 similar to the output of vgrind. The index is sorted, and\n\
522 gives the page number of each item.");
523 puts ("-w, --no-warn\n\
524 Suppress warning messages about entries defined in multiple\n\
526 puts ("-x, --cxref\n\
527 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
528 The output uses line numbers instead of page numbers, but\n\
529 beyond that the differences are cosmetic; try both to see\n\
533 puts ("-V, --version\n\
534 Print the version of the program.\n\
536 Print this help message.");
538 print_language_names ();
551 /* This structure helps us allow mixing of --lang and filenames. */
554 enum argument_type arg_type
;
556 Lang_function
*function
;
559 #ifdef VMS /* VMS specific functions */
563 /* This is a BUG! ANY arbitrary limit is a BUG!
564 Won't someone please fix this? */
565 #define MAX_FILE_SPEC_LEN 255
568 char body
[MAX_FILE_SPEC_LEN
+ 1];
572 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
573 returning in each successive call the next filename matching the input
574 spec. The function expects that each in_spec passed
575 to it will be processed to completion; in particular, up to and
576 including the call following that in which the last matching name
577 is returned, the function ignores the value of in_spec, and will
578 only start processing a new spec with the following call.
579 If an error occurs, on return out_spec contains the value
580 of in_spec when the error occurred.
582 With each successive filename returned in out_spec, the
583 function's return value is one. When there are no more matching
584 names the function returns zero. If on the first call no file
585 matches in_spec, or there is any other error, -1 is returned.
590 #define OUTSIZE MAX_FILE_SPEC_LEN
596 static long context
= 0;
597 static struct dsc$descriptor_s o
;
598 static struct dsc$descriptor_s i
;
599 static logical pass1
= TRUE
;
606 o
.dsc$a_pointer
= (char *) out
;
607 o
.dsc$w_length
= (short)OUTSIZE
;
608 i
.dsc$a_pointer
= in
;
609 i
.dsc$w_length
= (short)strlen(in
);
610 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
611 i
.dsc$b_class
= DSC$K_CLASS_S
;
612 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
613 o
.dsc$b_class
= DSC$K_CLASS_VS
;
615 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
617 out
->body
[out
->curlen
] = EOS
;
620 else if (status
== RMS$_NMF
)
624 strcpy(out
->body
, in
);
627 lib$
find_file_end(&context
);
633 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
634 name of each file specified by the provided arg expanding wildcards.
637 gfnames (arg
, p_error
)
641 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
643 switch (fn_exp (&filename
, arg
))
647 return filename
.body
;
653 return filename
.body
;
657 #ifndef OLD /* Newer versions of VMS do provide `system'. */
661 fprintf (stderr
, "system() function not implemented under VMS\n");
665 #define VERSION_DELIM ';'
666 char *massage_name (s
)
672 if (*s
== VERSION_DELIM
)
690 unsigned int nincluded_files
= 0;
691 char **included_files
= xnew (argc
, char *);
694 int current_arg
= 0, file_count
= 0;
695 struct linebuffer filename_lb
;
701 _fmode
= O_BINARY
; /* all of files are treated as binary files */
706 /* Allocate enough no matter what happens. Overkill, but each one
708 argbuffer
= xnew (argc
, argument
);
711 /* Set syntax for regular expression routines. */
712 re_set_syntax (RE_SYNTAX_EMACS
);
713 #endif /* ETAGS_REGEXPS */
716 * If etags, always find typedefs and structure tags. Why not?
717 * Also default is to find macro constants.
720 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
724 int opt
= getopt_long (argc
, argv
,
725 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
733 /* If getopt returns 0, then it has already processed a
734 long-named option. We should do nothing. */
738 /* This means that a filename has been seen. Record it. */
739 argbuffer
[current_arg
].arg_type
= at_filename
;
740 argbuffer
[current_arg
].what
= optarg
;
745 /* Common options. */
747 append_to_tagfile
= TRUE
;
753 constantypedefs
= TRUE
;
756 constantypedefs
= FALSE
;
758 case 'f': /* for compatibility with old makefiles */
762 fprintf (stderr
, "%s: -%c option may only be given once.\n",
764 suggest_asking_for_help ();
769 case 'S': /* for backward compatibility */
770 noindentypedefs
= TRUE
;
773 argbuffer
[current_arg
].function
= get_language_from_name (optarg
);
774 argbuffer
[current_arg
].arg_type
= at_language
;
779 argbuffer
[current_arg
].arg_type
= at_regexp
;
780 argbuffer
[current_arg
].what
= optarg
;
784 argbuffer
[current_arg
].arg_type
= at_regexp
;
785 argbuffer
[current_arg
].what
= NULL
;
788 #endif /* ETAGS_REGEXPS */
800 typedefs
= typedefs_and_cplusplus
= TRUE
;
805 included_files
[nincluded_files
++] = optarg
;
826 suggest_asking_for_help ();
830 for (; optind
< argc
; ++optind
)
832 argbuffer
[current_arg
].arg_type
= at_filename
;
833 argbuffer
[current_arg
].what
= argv
[optind
];
838 if (nincluded_files
== 0 && file_count
== 0)
840 fprintf (stderr
, "%s: No input files specified.\n", progname
);
841 suggest_asking_for_help ();
846 tagfile
= CTAGS
? "tags" : "TAGS";
848 cwd
= etags_getcwd (); /* the current working directory */
850 if (streq (tagfile
, "-"))
856 tagfiledir
= absolute_dirname (tagfile
, cwd
);
859 init (); /* set up boolean "functions" */
862 initbuffer (&token_name
);
863 initbuffer (&lbs
[0].lb
);
864 initbuffer (&lbs
[1].lb
);
865 initbuffer (&filename_lb
);
869 if (streq (tagfile
, "-"))
872 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
878 * Loop through files finding functions.
880 for (i
= 0; i
< current_arg
; ++i
)
882 switch (argbuffer
[i
].arg_type
)
885 lang_func
= argbuffer
[i
].function
;
889 add_regex (argbuffer
[i
].what
);
894 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
898 error ("Can't find file %s\n", this_file
);
903 this_file
= massage_name (this_file
);
906 this_file
= argbuffer
[i
].what
;
908 /* Input file named "-" means read file names from stdin
910 if (streq (this_file
, "-"))
911 while (readline_internal (&filename_lb
, stdin
) > 0)
912 process_file (filename_lb
.buffer
);
914 process_file (this_file
);
924 while (nincluded_files
-- > 0)
925 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
931 /* If CTAGS, we are here. process_file did not write the tags yet,
932 because we want them ordered. Let's do it now. */
935 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
945 for (i
= 0; i
< current_arg
; ++i
)
947 if (argbuffer
[i
].arg_type
!= at_filename
)
950 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
951 tagfile
, argbuffer
[i
].what
, tagfile
);
952 if (system (cmd
) != GOOD
)
953 fatal ("failed to execute shell command");
955 append_to_tagfile
= TRUE
;
958 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
967 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
975 * Return a Lang_function given the name.
978 get_language_from_name (name
)
981 struct lang_entry
*lang
;
984 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
986 if (streq (name
, lang
->name
))
987 return lang
->function
;
990 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
992 suggest_asking_for_help ();
994 /* This point should never be reached. The function should either
995 return a function pointer or never return. Note that a NULL
996 pointer cannot be considered as an error, as it means that the
997 language has not been explicitely imposed by the user ("auto"). */
998 return NULL
; /* avoid warnings from compiler */
1003 * Return a Lang_function given the interpreter name.
1006 get_language_from_interpreter (interpreter
)
1009 struct lang_entry
*lang
;
1012 if (interpreter
== NULL
)
1014 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1015 if (lang
->interpreters
!= NULL
)
1016 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1017 if (streq (*iname
, interpreter
))
1018 return lang
->function
;
1026 * Return a Lang_function given the file suffix.
1029 get_language_from_suffix (suffix
)
1032 struct lang_entry
*lang
;
1037 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1038 if (lang
->suffixes
!= NULL
)
1039 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1040 if (streq (*ext
, suffix
))
1041 return lang
->function
;
1048 * This routine is called on each file argument.
1054 struct stat stat_buf
;
1057 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1059 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1062 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1064 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1067 inf
= fopen (file
, "r");
1074 find_entries (file
, inf
);
1080 if (absolutefn (file
))
1082 /* file is an absolute filename. Canonicalise it. */
1083 filename
= absolute_filename (file
, cwd
);
1087 /* file is a filename relative to cwd. Make it relative
1088 to the directory of the tags file. */
1089 filename
= relative_filename (file
, tagfiledir
);
1091 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1100 * This routine sets up the boolean pseudo-functions which work
1101 * by setting boolean flags dependent upon the corresponding character
1102 * Every char which is NOT in that string is not a white char. Therefore,
1103 * all of the array "_wht" is set to FALSE, and then the elements
1104 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1105 * of a char is TRUE if it is the string "white", else FALSE.
1113 for (i
= 0; i
< 0177; i
++)
1114 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1115 for (sp
= white
; *sp
; sp
++)
1117 for (sp
= endtk
; *sp
; sp
++)
1119 for (sp
= intk
; *sp
; sp
++)
1121 for (sp
= begtk
; *sp
; sp
++)
1123 _wht
[0] = _wht
['\n'];
1124 _etk
[0] = _etk
['\n'];
1125 _btk
[0] = _btk
['\n'];
1126 _itk
[0] = _itk
['\n'];
1130 * This routine opens the specified file and calls the function
1131 * which finds the function and type definitions.
1134 find_entries (file
, inf
)
1139 Lang_function
*function
;
1140 NODE
*old_last_node
;
1141 extern NODE
*last_node
;
1144 /* Memory leakage here: the memory block pointed by curfile is never
1145 released. The amount of memory leaked here is the sum of the
1146 lengths of the input file names. */
1147 curfile
= savestr (file
);
1149 /* If user specified a language, use it. */
1150 function
= lang_func
;
1151 if (function
!= NULL
)
1158 cp
= etags_strrchr (file
, '.');
1162 function
= get_language_from_suffix (cp
);
1163 if (function
!= NULL
)
1171 /* Look for sharp-bang as the first two characters. */
1172 if (readline_internal (&lb
, inf
) > 2
1173 && lb
.buffer
[0] == '#'
1174 && lb
.buffer
[1] == '!')
1178 /* Set lp to point at the first char after the last slash in the
1179 line or, if no slashes, at the first nonblank. Then set cp to
1180 the first successive blank and terminate the string. */
1181 lp
= etags_strrchr (lb
.buffer
+2, '/');
1185 for (lp
= lb
.buffer
+2; *lp
!= '\0' && isspace (*lp
); lp
++)
1187 for (cp
= lp
; *cp
!= '\0' && !isspace (*cp
); cp
++)
1191 if (strlen (lp
) > 0)
1193 function
= get_language_from_interpreter (lp
);
1194 if (function
!= NULL
)
1205 old_last_node
= last_node
;
1206 Fortran_functions (inf
);
1208 /* No Fortran entries found. Try C. */
1209 if (old_last_node
== last_node
)
1212 default_C_entries (inf
);
1220 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1221 char *name
; /* tag name, or NULL if unnamed */
1222 logical is_func
; /* tag is a function */
1223 char *linestart
; /* start of the line where tag is */
1224 int linelen
; /* length of the line where tag is */
1225 int lno
; /* line number */
1226 long cno
; /* character number */
1230 if (CTAGS
&& name
== NULL
)
1233 np
= xnew (1, NODE
);
1235 /* If ctags mode, change name "main" to M<thisfilename>. */
1236 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1238 register char *fp
= etags_strrchr (curfile
, '/');
1239 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1240 fp
= etags_strrchr (np
->name
, '.');
1241 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1246 np
->been_warned
= FALSE
;
1248 np
->is_func
= is_func
;
1250 /* Our char numbers are 0-base, because of C language tradition?
1251 ctags compatibility? old versions compatibility? I don't know.
1252 Anyway, since emacs's are 1-base we espect etags.el to take care
1253 of the difference. If we wanted to have 1-based numbers, we would
1254 uncomment the +1 below. */
1255 np
->cno
= cno
/* + 1 */ ;
1256 np
->left
= np
->right
= NULL
;
1257 if (CTAGS
&& !cxref_style
)
1259 if (strlen (linestart
) < 50)
1260 np
->pat
= concat (linestart
, "$", "");
1262 np
->pat
= savenstr (linestart
, 50);
1265 np
->pat
= savenstr (linestart
, linelen
);
1267 add_node (np
, &head
);
1272 * recurse on left children, iterate on right children.
1276 register NODE
*node
;
1280 register NODE
*node_right
= node
->right
;
1281 free_tree (node
->left
);
1282 if (node
->name
!= NULL
)
1285 free ((char *) node
);
1292 * Adds a node to the tree of nodes. In etags mode, we don't keep
1293 * it sorted; we just keep a linear list. In ctags mode, maintain
1294 * an ordered tree, with no attempt at balancing.
1296 * add_node is the only function allowed to add nodes, so it can
1299 NODE
*last_node
= NULL
;
1301 add_node (node
, cur_node_p
)
1302 NODE
*node
, **cur_node_p
;
1305 register NODE
*cur_node
= *cur_node_p
;
1307 if (cur_node
== NULL
)
1317 if (last_node
== NULL
)
1318 fatal ("internal error in add_node", 0);
1319 last_node
->right
= node
;
1325 dif
= strcmp (node
->name
, cur_node
->name
);
1328 * If this tag name matches an existing one, then
1329 * do not add the node, but maybe print a warning.
1333 if (streq (node
->file
, cur_node
->file
))
1337 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1338 node
->file
, lineno
, node
->name
);
1339 fprintf (stderr
, "Second entry ignored\n");
1342 else if (!cur_node
->been_warned
&& !no_warnings
)
1346 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1347 node
->file
, cur_node
->file
, node
->name
);
1348 cur_node
->been_warned
= TRUE
;
1353 /* Actually add the node */
1354 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1360 register NODE
*node
;
1367 /* Output subentries that precede this one */
1368 put_entries (node
->left
);
1370 /* Output this entry */
1374 if (node
->name
!= NULL
)
1375 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1376 node
->pat
, node
->name
, node
->lno
, node
->cno
);
1378 fprintf (tagf
, "%s\177%d,%d\n",
1379 node
->pat
, node
->lno
, node
->cno
);
1383 if (node
->name
== NULL
)
1384 error ("internal error: NULL name in ctags mode.", 0);
1389 fprintf (stdout
, "%s %s %d\n",
1390 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1392 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1393 node
->name
, node
->lno
, node
->file
, node
->pat
);
1397 fprintf (tagf
, "%s\t%s\t", node
->name
, node
->file
);
1401 putc (searchar
, tagf
);
1404 for (sp
= node
->pat
; *sp
; sp
++)
1406 if (*sp
== '\\' || *sp
== searchar
)
1410 putc (searchar
, tagf
);
1413 { /* a typedef; text pattern inadequate */
1414 fprintf (tagf
, "%d", node
->lno
);
1420 /* Output subentries that follow this one */
1421 put_entries (node
->right
);
1424 /* Length of a number's decimal representation. */
1432 for (; num
; num
/= 10)
1438 * Return total number of characters that put_entries will output for
1439 * the nodes in the subtree of the specified node. Works only if
1440 * we are not ctags, but called only in that case. This count
1441 * is irrelevant with the new tags.el, but is still supplied for
1442 * backward compatibility.
1445 total_size_of_entries (node
)
1446 register NODE
*node
;
1454 for (; node
; node
= node
->right
)
1456 /* Count left subentries. */
1457 total
+= total_size_of_entries (node
->left
);
1459 /* Count this entry */
1460 total
+= strlen (node
->pat
) + 1;
1461 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1462 if (node
->name
!= NULL
)
1463 total
+= 1 + strlen (node
->name
); /* \001name */
1470 * The C symbol tables.
1474 st_none
, st_C_objprot
, st_C_objimpl
, st_C_objend
, st_C_gnumacro
,
1475 st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
,
1478 /* Feed stuff between (but not including) %[ and %] lines to:
1479 gperf -c -k1,3 -o -p -r -t
1481 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1483 @interface, 0, st_C_objprot
1484 @protocol, 0, st_C_objprot
1485 @implementation,0, st_C_objimpl
1486 @end, 0, st_C_objend
1487 class, C_PLPL, st_C_struct
1488 domain, C_STAR, st_C_struct
1489 union, 0, st_C_struct
1490 struct, 0, st_C_struct
1492 typedef, 0, st_C_typedef
1493 define, 0, st_C_define
1494 long, 0, st_C_typespec
1495 short, 0, st_C_typespec
1496 int, 0, st_C_typespec
1497 char, 0, st_C_typespec
1498 float, 0, st_C_typespec
1499 double, 0, st_C_typespec
1500 signed, 0, st_C_typespec
1501 unsigned, 0, st_C_typespec
1502 auto, 0, st_C_typespec
1503 void, 0, st_C_typespec
1504 extern, 0, st_C_typespec
1505 static, 0, st_C_typespec
1506 const, 0, st_C_typespec
1507 volatile, 0, st_C_typespec
1508 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1509 DEFUN, 0, st_C_gnumacro
1510 SYSCALL, 0, st_C_gnumacro
1511 ENTRY, 0, st_C_gnumacro
1512 PSEUDO, 0, st_C_gnumacro
1513 # These are defined inside C functions, so currently they are not met.
1514 # EXFUN used in glibc, DEFVAR_* in emacs.
1515 #EXFUN, 0, st_C_gnumacro
1516 #DEFVAR_, 0, st_C_gnumacro
1518 and replace lines between %< and %> with its output. */
1520 /* C code produced by gperf version 1.8.1 (K&R C version) */
1521 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1524 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1526 #define MIN_WORD_LENGTH 3
1527 #define MAX_WORD_LENGTH 15
1528 #define MIN_HASH_VALUE 7
1529 #define MAX_HASH_VALUE 63
1532 57 is the maximum key range
1540 static unsigned char hash_table
[] =
1542 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1543 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1544 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1545 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1546 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1547 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1548 63, 63, 63, 63, 17, 63, 63, 63, 4, 14,
1549 4, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1550 8, 63, 63, 0, 23, 63, 63, 63, 63, 63,
1551 63, 63, 63, 63, 63, 63, 63, 28, 63, 28,
1552 10, 31, 27, 18, 63, 6, 63, 63, 26, 1,
1553 11, 2, 29, 63, 29, 16, 26, 13, 15, 63,
1554 63, 63, 63, 63, 63, 63, 63, 63,
1556 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1559 struct C_stab_entry
*
1560 in_word_set (str
, len
)
1565 static struct C_stab_entry wordlist
[] =
1567 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1568 {"SYSCALL", 0, st_C_gnumacro
},
1569 {"",}, {"",}, {"",}, {"",}, {"",},
1570 {"DEFUN", 0, st_C_gnumacro
},
1571 {"",}, {"",}, {"",},
1572 {"domain", C_STAR
, st_C_struct
},
1573 {"",}, {"",}, {"",}, {"",}, {"",},
1574 {"short", 0, st_C_typespec
},
1575 {"union", 0, st_C_struct
},
1576 {"void", 0, st_C_typespec
},
1578 {"PSEUDO", 0, st_C_gnumacro
},
1579 {"double", 0, st_C_typespec
},
1581 {"@end", 0, st_C_objend
},
1582 {"@implementation", 0, st_C_objimpl
},
1583 {"float", 0, st_C_typespec
},
1584 {"int", 0, st_C_typespec
},
1586 {"unsigned", 0, st_C_typespec
},
1587 {"@interface", 0, st_C_objprot
},
1589 {"signed", 0, st_C_typespec
},
1590 {"long", 0, st_C_typespec
},
1591 {"ENTRY", 0, st_C_gnumacro
},
1592 {"define", 0, st_C_define
},
1593 {"const", 0, st_C_typespec
},
1594 {"",}, {"",}, {"",},
1595 {"enum", 0, st_C_enum
},
1596 {"volatile", 0, st_C_typespec
},
1597 {"static", 0, st_C_typespec
},
1598 {"struct", 0, st_C_struct
},
1599 {"",}, {"",}, {"",},
1600 {"@protocol", 0, st_C_objprot
},
1602 {"auto", 0, st_C_typespec
},
1604 {"char", 0, st_C_typespec
},
1605 {"class", C_PLPL
, st_C_struct
},
1606 {"typedef", 0, st_C_typedef
},
1607 {"extern", 0, st_C_typespec
},
1610 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1612 register int key
= hash (str
, len
);
1614 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1616 register char *s
= wordlist
[key
].name
;
1618 if (*s
== *str
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
1619 return &wordlist
[key
];
1627 C_symtype(str
, len
, c_ext
)
1632 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1634 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1640 * C functions are recognized using a simple finite automaton.
1641 * funcdef is its state variable.
1645 fnone
, /* nothing seen */
1646 ftagseen
, /* function-like tag seen */
1647 fstartlist
, /* just after open parenthesis */
1648 finlist
, /* in parameter list */
1649 flistseen
, /* after parameter list */
1650 fignore
/* before open brace */
1655 * typedefs are recognized using a simple finite automaton.
1656 * typeddef is its state variable.
1660 tnone
, /* nothing seen */
1661 ttypedseen
, /* typedef keyword seen */
1662 tinbody
, /* inside typedef body */
1663 tend
, /* just before typedef tag */
1664 tignore
/* junk after typedef tag */
1669 * struct-like structures (enum, struct and union) are recognized
1670 * using another simple finite automaton. `structdef' is its state
1675 snone
, /* nothing seen yet */
1676 skeyseen
, /* struct-like keyword seen */
1677 stagseen
, /* struct-like tag seen */
1678 scolonseen
, /* colon seen after struct-like tag */
1679 sinbody
/* in struct body: recognize member func defs*/
1683 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1684 * struct tag, and structtype is the type of the preceding struct-like
1687 char *structtag
= "<uninited>";
1688 enum sym_type structtype
;
1691 * When objdef is different from onone, objtag is the name of the class.
1693 char *objtag
= "<uninited>";
1696 * Yet another little state machine to deal with preprocessor lines.
1700 dnone
, /* nothing seen */
1701 dsharpseen
, /* '#' seen as first char on line */
1702 ddefineseen
, /* '#' and 'define' seen */
1703 dignorerest
/* ignore rest of line */
1707 * State machine for Objective C protocols and implementations.
1711 onone
, /* nothing seen */
1712 oprotocol
, /* @interface or @protocol seen */
1713 oimplementation
, /* @implementations seen */
1714 otagseen
, /* class name seen */
1715 oparenseen
, /* parenthesis before category seen */
1716 ocatseen
, /* category name seen */
1717 oinbody
, /* in @implementation body */
1718 omethodsign
, /* in @implementation body, after +/- */
1719 omethodtag
, /* after method name */
1720 omethodcolon
, /* after method colon */
1721 omethodparm
, /* after method parameter */
1722 oignore
, /* wait for @end */
1726 * Set this to TRUE, and the next token considered is called a function.
1727 * Used only for GNU emacs's function-defining macros.
1729 logical next_token_is_func
;
1732 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1737 * methodlen is the length of the method name stored in token_name.
1743 * checks to see if the current token is at the start of a
1744 * function, or corresponds to a typedef, or is a struct/union/enum
1747 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1748 * C_EXT is which language we are looking at.
1750 * In the future we will need some way to adjust where the end of
1751 * the token is; for instance, implementing the C++ keyword
1752 * `operator' properly will adjust the end of the token to be after
1753 * whatever follows `operator'.
1761 * next_token_is_func IN OUT
1765 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func
)
1766 register char *str
; /* IN: token pointer */
1767 register int len
; /* IN: token length */
1768 register char c
; /* IN: first char after the token */
1769 int c_ext
; /* IN: C extensions mask */
1770 int cblev
; /* IN: curly brace level */
1771 int parlev
; /* IN: parenthesis level */
1772 logical
*is_func
; /* OUT: function found */
1774 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1777 * Advance the definedef state machine.
1782 /* We're not on a preprocessor line. */
1785 if (toktype
== st_C_define
)
1787 definedef
= ddefineseen
;
1791 definedef
= dignorerest
;
1796 * Make a tag for any macro, unless it is a constant
1797 * and constantypedefs is FALSE.
1799 definedef
= dignorerest
;
1800 *is_func
= (c
== '(');
1801 if (!*is_func
&& !constantypedefs
)
1808 error ("internal error: definedef value.", 0);
1817 if (toktype
== st_C_typedef
)
1820 typdef
= ttypedseen
;
1836 /* Do not return here, so the structdef stuff has a chance. */
1850 * This structdef business is currently only invoked when cblev==0.
1851 * It should be recursively invoked whatever the curly brace level,
1852 * and a stack of states kept, to allow for definitions of structs
1855 * This structdef business is NOT invoked when we are ctags and the
1856 * file is plain C. This is because a struct tag may have the same
1857 * name as another tag, and this loses with ctags.
1859 * This if statement deals with the typdef state machine as
1860 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1861 * return FALSE. All the other code here is for the structdef
1868 if (typdef
== ttypedseen
1869 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1871 structdef
= skeyseen
;
1872 structtype
= toktype
;
1876 if (structdef
== skeyseen
)
1878 /* Save the tag for struct/union/class, for functions that may be
1880 if (structtype
== st_C_struct
)
1881 structtag
= savenstr (str
, len
);
1883 structtag
= "<enum>";
1884 structdef
= stagseen
;
1888 /* Avoid entering funcdef stuff if typdef is going on. */
1889 if (typdef
!= tnone
)
1895 /* Detect GNU macros. */
1896 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
1898 next_token_is_func
= TRUE
;
1901 if (next_token_is_func
)
1903 next_token_is_func
= FALSE
;
1910 * Detecting Objective C constructs.
1921 objdef
= oimplementation
;
1925 case oimplementation
:
1926 /* Save the class tag for functions that may be defined inside. */
1927 objtag
= savenstr (str
, len
);
1931 /* Save the class tag for categories. */
1932 objtag
= savenstr (str
, len
);
1945 objdef
= omethodtag
;
1947 GROW_LINEBUFFER (token_name
, methodlen
+1);
1948 strncpy (token_name
.buffer
, str
, len
);
1949 token_name
.buffer
[methodlen
] = '\0';
1955 objdef
= omethodparm
;
1960 objdef
= omethodtag
;
1962 GROW_LINEBUFFER (token_name
, methodlen
+1);
1963 strncat (token_name
.buffer
, str
, len
);
1968 if (toktype
== st_C_objend
)
1970 /* Memory leakage here: the string pointed by objtag is
1971 never released, because many tests would be needed to
1972 avoid breaking on incorrect input code. The amount of
1973 memory leaked here is the sum of the lenghts of the
1985 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1986 funcdef
= fnone
; /* should be useless */
1989 if (funcdef
== fnone
)
2002 * This routine finds functions, typedefs, #define's and
2003 * struct/union/enum definitions in C syntax and adds them
2017 #define current_lb_is_new (newndx == curndx)
2018 #define switch_line_buffers() (curndx = 1 - curndx)
2020 #define curlb (lbs[curndx].lb)
2021 #define othlb (lbs[1-curndx].lb)
2022 #define newlb (lbs[newndx].lb)
2023 #define curlinepos (lbs[curndx].linepos)
2024 #define othlinepos (lbs[1-curndx].linepos)
2025 #define newlinepos (lbs[newndx].linepos)
2027 #define CNL_SAVE_DEFINEDEF \
2029 curlinepos = charno; \
2031 charno += readline (&curlb, inf); \
2032 lp = curlb.buffer; \
2039 CNL_SAVE_DEFINEDEF; \
2040 if (savetok.valid) \
2043 savetok.valid = FALSE; \
2045 definedef = dnone; \
2048 /* Ideally this macro should never be called wihen tok.valid is FALSE,
2049 but this would mean that the state machines always guess right. */
2050 #define make_tag(isfun) do \
2052 char *name = NULL; \
2053 if (CTAGS || tok.named) \
2054 name = savestr (token_name.buffer); \
2055 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
2056 tok.valid = FALSE; \
2060 C_entries (c_ext
, inf
)
2061 int c_ext
; /* extension of C */
2062 FILE *inf
; /* input file */
2064 register char c
; /* latest char read; '\0' for end of line */
2065 register char *lp
; /* pointer one beyond the character `c' */
2066 int curndx
, newndx
; /* indices for current and new lb */
2067 TOKEN tok
; /* latest token read */
2068 register int tokoff
; /* offset in line of start of current token */
2069 register int toklen
; /* length of current token */
2070 int cblev
; /* current curly brace level */
2071 int parlev
; /* current parenthesis level */
2072 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
2074 TOKEN savetok
; /* token saved during preprocessor handling */
2077 curndx
= newndx
= 0;
2083 funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
2084 definedef
= dnone
; objdef
= onone
;
2085 next_token_is_func
= yacc_rules
= FALSE
;
2086 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2087 tok
.valid
= savetok
.valid
= FALSE
;
2090 cplpl
= c_ext
& C_PLPL
;
2097 /* If we're at the end of the line, the next character is a
2098 '\0'; don't skip it, because it's the thing that tells us
2099 to read the next line. */
2120 /* Newlines inside comments do not end macro definitions in
2135 /* Newlines inside strings do not end macro definitions
2136 in traditional cpp, even though compilers don't
2137 usually accept them. */
2148 /* Hmmm, something went wrong. */
2162 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2167 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2177 else if (/* cplpl && */ *lp
== '/')
2185 if ((c_ext
& YACC
) && *lp
== '%')
2187 /* entering or exiting rules section in yacc file */
2189 definedef
= dnone
; funcdef
= fnone
;
2190 typdef
= tnone
; structdef
= snone
;
2191 next_token_is_func
= FALSE
;
2192 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2194 yacc_rules
= !yacc_rules
;
2200 if (definedef
== dnone
)
2203 logical cpptoken
= TRUE
;
2205 /* Look back on this line. If all blanks, or nonblanks
2206 followed by an end of comment, this is a preprocessor
2208 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2211 if (*cp
== '*' && *(cp
+1) == '/')
2220 definedef
= dsharpseen
;
2221 } /* if (definedef == dnone) */
2227 /* Consider token only if some complicated conditions are satisfied. */
2228 if ((definedef
!= dnone
2229 || (cblev
== 0 && structdef
!= scolonseen
)
2230 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2231 && typdef
!= tignore
2232 && definedef
!= dignorerest
2233 && funcdef
!= finlist
)
2239 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken(*(lp
+ 1)))
2242 * This handles :: in the middle, but not at the
2243 * beginning of an identifier.
2250 logical is_func
= FALSE
;
2253 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2254 c_ext
, cblev
, parlev
, &is_func
))
2256 if (structdef
== sinbody
2257 && definedef
== dnone
2259 /* function defined in C++ class body */
2261 GROW_LINEBUFFER (token_name
,
2262 strlen(structtag
)+2+toklen
+1);
2263 strcpy (token_name
.buffer
, structtag
);
2264 strcat (token_name
.buffer
, "::");
2265 strncat (token_name
.buffer
,
2266 newlb
.buffer
+tokoff
, toklen
);
2269 else if (objdef
== ocatseen
)
2270 /* Objective C category */
2272 GROW_LINEBUFFER (token_name
,
2273 strlen(objtag
)+2+toklen
+1);
2274 strcpy (token_name
.buffer
, objtag
);
2275 strcat (token_name
.buffer
, "(");
2276 strncat (token_name
.buffer
,
2277 newlb
.buffer
+tokoff
, toklen
);
2278 strcat (token_name
.buffer
, ")");
2281 else if (objdef
== omethodtag
2282 || objdef
== omethodparm
)
2283 /* Objective C method */
2289 GROW_LINEBUFFER (token_name
, toklen
+1);
2290 strncpy (token_name
.buffer
,
2291 newlb
.buffer
+tokoff
, toklen
);
2292 token_name
.buffer
[toklen
] = '\0';
2293 if (structdef
== stagseen
2296 && definedef
== dignorerest
)) /* macro */
2301 tok
.lineno
= lineno
;
2302 tok
.linelen
= tokoff
+ toklen
+ 1;
2303 tok
.buffer
= newlb
.buffer
;
2304 tok
.linepos
= newlinepos
;
2307 if (definedef
== dnone
2308 && (funcdef
== ftagseen
2309 || structdef
== stagseen
2311 || objdef
!= onone
))
2313 if (current_lb_is_new
)
2314 switch_line_buffers ();
2321 } /* if (endtoken (c)) */
2322 else if (intoken (c
))
2327 } /* if (midtoken) */
2328 else if (begtoken (c
))
2346 if (structdef
== stagseen
)
2352 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2354 tokoff
= lp
- 1 - newlb
.buffer
;
2359 } /* if (begtoken) */
2360 } /* if must look at token */
2363 /* Detect end of line, colon, comma, semicolon and various braces
2364 after having handled a token.*/
2368 if (definedef
!= dnone
)
2378 objdef
= omethodcolon
;
2380 GROW_LINEBUFFER (token_name
, methodlen
+1);
2381 strcat (token_name
.buffer
, ":");
2384 if (structdef
== stagseen
)
2385 structdef
= scolonseen
;
2402 if (definedef
!= dnone
)
2413 if (funcdef
!= fignore
)
2416 /* The following instruction invalidates the token.
2417 Probably the token should be invalidated in all
2418 other cases where some state machine is reset. */
2421 if (structdef
== stagseen
)
2425 if (definedef
!= dnone
)
2435 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2437 if (structdef
== stagseen
)
2441 if (definedef
!= dnone
)
2443 if (cblev
== 0 && typdef
== tend
)
2449 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2451 if (structdef
== stagseen
)
2455 if (definedef
!= dnone
)
2457 if (objdef
== otagseen
&& parlev
== 0)
2458 objdef
= oparenseen
;
2466 /* Make sure that the next char is not a '*'.
2467 This handles constructs like:
2468 typedef void OperatorFun (int fun); */
2475 } /* switch (typdef) */
2478 funcdef
= fstartlist
;
2487 if (definedef
!= dnone
)
2489 if (objdef
== ocatseen
&& parlev
== 1)
2500 funcdef
= flistseen
;
2503 if (cblev
== 0 && typdef
== tend
)
2509 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2513 if (definedef
!= dnone
)
2515 if (typdef
== ttypedseen
)
2519 case skeyseen
: /* unnamed struct */
2520 structtag
= "_anonymous_";
2521 structdef
= sinbody
;
2524 case scolonseen
: /* named struct */
2525 structdef
= sinbody
;
2550 /* Neutralize `extern "C" {' grot and look inside structs. */
2551 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2558 if (definedef
!= dnone
)
2560 if (funcdef
== fstartlist
)
2561 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2564 if (definedef
!= dnone
)
2566 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2568 cblev
= 0; /* reset curly brace level if first column */
2569 parlev
= 0; /* also reset paren level, just in case... */
2575 if (typdef
== tinbody
)
2577 /* Memory leakage here: the string pointed by structtag is
2578 never released, because I fear to miss something and
2579 break things while freeing the area. The amount of
2580 memory leaked here is the sum of the lenghts of the
2582 if (structdef == sinbody)
2583 free (structtag); */
2586 structtag
= "<error>";
2591 if (objdef
== oinbody
&& cblev
== 0)
2593 objdef
= omethodsign
;
2597 case '=': case '#': case '~': case '&': case '%': case '/':
2598 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2599 if (definedef
!= dnone
)
2601 /* These surely cannot follow a function tag. */
2602 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2606 if (objdef
== otagseen
)
2611 /* If a macro spans multiple lines don't reset its state. */
2619 } /* while not eof */
2623 * Process either a C++ file or a C file depending on the setting
2627 default_C_entries (inf
)
2630 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2633 /* Always do plain ANSI C. */
2635 plain_C_entries (inf
)
2641 /* Always do C++. */
2643 Cplusplus_entries (inf
)
2646 C_entries (C_PLPL
, inf
);
2654 C_entries (C_STAR
, inf
);
2657 /* Always do Yacc. */
2662 C_entries (YACC
, inf
);
2665 /* Fortran parsing */
2673 register int len
= 0;
2675 while (*cp
&& lowcase(*cp
) == lowcase(dbp
[len
]))
2677 if (*cp
== '\0' && !intoken(dbp
[len
]))
2688 while (isspace (*dbp
))
2693 while (isspace (*dbp
))
2695 if (strneq (dbp
, "(*)", 3))
2700 if (!isdigit (*dbp
))
2702 --dbp
; /* force failure */
2707 while (isdigit (*dbp
));
2716 while (isspace (*dbp
))
2721 linecharno
= charno
;
2722 charno
+= readline (&lb
, inf
);
2727 while (isspace (*dbp
))
2736 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2739 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
2740 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2744 Fortran_functions (inf
)
2753 linecharno
= charno
;
2754 charno
+= readline (&lb
, inf
);
2757 dbp
++; /* Ratfor escape to fortran */
2758 while (isspace (*dbp
))
2762 switch (lowcase (*dbp
))
2765 if (tail ("integer"))
2773 if (tail ("logical"))
2777 if (tail ("complex") || tail ("character"))
2781 if (tail ("double"))
2783 while (isspace (*dbp
))
2787 if (tail ("precision"))
2793 while (isspace (*dbp
))
2797 switch (lowcase (*dbp
))
2800 if (tail ("function"))
2804 if (tail ("subroutine"))
2812 if (tail ("program"))
2817 if (tail ("procedure"))
2825 * Bob Weiner, Motorola Inc., 4/3/94
2826 * Unix and microcontroller assembly tag handling
2827 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2841 linecharno
= charno
;
2842 charno
+= readline (&lb
, inf
);
2845 /* If first char is alphabetic or one of [_.$], test for colon
2846 following identifier. */
2847 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2849 /* Read past label. */
2851 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2853 if (*cp
== ':' || isspace (*cp
))
2855 /* Found end of label, so copy it and add it to the table. */
2856 pfnote ((CTAGS
) ? savenstr(lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2857 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2864 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2865 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2868 Perl_functions (inf
)
2879 linecharno
= charno
;
2880 charno
+= readline (&lb
, inf
);
2883 if (*cp
++ == 's' && *cp
++ == 'u' && *cp
++ == 'b' && isspace(*cp
++))
2885 while (*cp
&& isspace(*cp
))
2887 while (*cp
&& ! isspace(*cp
) && *cp
!= '{')
2889 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2890 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2895 /* Added by Mosur Mohan, 4/22/88 */
2896 /* Pascal parsing */
2898 #define GET_NEW_LINE \
2900 linecharno = charno; lineno++; \
2901 charno += 1 + readline (&lb, inf); \
2906 * Locates tags for procedures & functions. Doesn't do any type- or
2907 * var-definitions. It does look for the keyword "extern" or
2908 * "forward" immediately following the procedure statement; if found,
2909 * the tag is skipped.
2912 Pascal_functions (inf
)
2915 struct linebuffer tline
; /* mostly copied from C_entries */
2917 int save_lineno
, save_len
;
2918 char c
, *cp
, *namebuf
;
2920 logical
/* each of these flags is TRUE iff: */
2921 incomment
, /* point is inside a comment */
2922 inquote
, /* point is inside '..' string */
2923 get_tagname
, /* point is after PROCEDURE/FUNCTION
2924 keyword, so next item = potential tag */
2925 found_tag
, /* point is after a potential tag */
2926 inparms
, /* point is within parameter-list */
2927 verify_tag
; /* point has passed the parm-list, so the
2928 next token will determine whether this
2929 is a FORWARD/EXTERN to be ignored, or
2930 whether it is a real tag */
2937 initbuffer (&tline
);
2939 incomment
= inquote
= FALSE
;
2940 found_tag
= FALSE
; /* have a proc name; check if extern */
2941 get_tagname
= FALSE
; /* have found "procedure" keyword */
2942 inparms
= FALSE
; /* found '(' after "proc" */
2943 verify_tag
= FALSE
; /* check if "extern" is ahead */
2945 /* long main loop to get next char */
2949 if (c
== '\0') /* if end of line */
2954 if (!((found_tag
&& verify_tag
) ||
2956 c
= *dbp
++; /* only if don't need *dbp pointing
2957 to the beginning of the name of
2958 the procedure or function */
2962 if (c
== '}') /* within { } comments */
2964 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
2981 inquote
= TRUE
; /* found first quote */
2983 case '{': /* found open { comment */
2987 if (*dbp
== '*') /* found open (* comment */
2992 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2995 case ')': /* end of parms list */
3000 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
3007 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
3009 /* check if this is an "extern" declaration */
3012 if (lowcase (*dbp
== 'e'))
3014 if (tail ("extern")) /* superfluous, really! */
3020 else if (lowcase (*dbp
) == 'f')
3022 if (tail ("forward")) /* check for forward reference */
3028 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
3032 pfnote (namebuf
, TRUE
,
3033 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
3037 if (get_tagname
) /* grab name of proc or fn */
3042 /* save all values for later tagging */
3043 GROW_LINEBUFFER (tline
, strlen (lb
.buffer
) + 1);
3044 strcpy (tline
.buffer
, lb
.buffer
);
3045 save_lineno
= lineno
;
3046 save_lcno
= linecharno
;
3048 /* grab block name */
3049 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
3051 namebuf
= (CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
;
3052 dbp
= cp
; /* set dbp to e-o-token */
3053 save_len
= dbp
- lb
.buffer
+ 1;
3054 get_tagname
= FALSE
;
3058 /* and proceed to check for "extern" */
3060 else if (!incomment
&& !inquote
&& !found_tag
)
3062 /* check for proc/fn keywords */
3063 switch (lowcase (c
))
3066 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3070 if (tail ("unction"))
3075 } /* while not eof */
3077 free (tline
.buffer
);
3081 * lisp tag functions
3082 * look for (def or (DEF, quote or QUOTE
3086 register char *strp
;
3088 return ((strp
[1] == 'd' || strp
[1] == 'D')
3089 && (strp
[2] == 'e' || strp
[2] == 'E')
3090 && (strp
[3] == 'f' || strp
[3] == 'F'));
3095 register char *strp
;
3097 return ((*(++strp
) == 'q' || *strp
== 'Q')
3098 && (*(++strp
) == 'u' || *strp
== 'U')
3099 && (*(++strp
) == 'o' || *strp
== 'O')
3100 && (*(++strp
) == 't' || *strp
== 'T')
3101 && (*(++strp
) == 'e' || *strp
== 'E')
3102 && isspace(*(++strp
)));
3110 if (*dbp
== '\'') /* Skip prefix quote */
3112 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
3115 while (isspace(*dbp
))
3118 for (cp
= dbp
/*+1*/;
3119 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
3125 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3126 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3130 Lisp_functions (inf
)
3139 linecharno
= charno
;
3140 charno
+= readline (&lb
, inf
);
3146 while (!isspace (*dbp
))
3148 while (isspace (*dbp
))
3154 /* Check for (foo::defmumble name-defined ... */
3157 while (*dbp
&& !isspace (*dbp
)
3158 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
3163 while (*dbp
== ':');
3165 if (L_isdef (dbp
- 1))
3167 while (!isspace (*dbp
))
3169 while (isspace (*dbp
))
3180 * Scheme tag functions
3181 * look for (def... xyzzy
3182 * look for (def... (xyzzy
3183 * look for (def ... ((...(xyzzy ....
3184 * look for (set! xyzzy
3190 Scheme_functions (inf
)
3199 linecharno
= charno
;
3200 charno
+= readline (&lb
, inf
);
3202 if (dbp
[0] == '(' &&
3203 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
3204 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3205 (dbp
[3] == 'F' || dbp
[3] == 'f'))
3207 while (!isspace (*dbp
))
3209 /* Skip over open parens and white space */
3210 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
3214 if (dbp
[0] == '(' &&
3215 (dbp
[1] == 'S' || dbp
[1] == 's') &&
3216 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3217 (dbp
[3] == 'T' || dbp
[3] == 't') &&
3218 (dbp
[4] == '!' || dbp
[4] == '!') &&
3221 while (!isspace (*dbp
))
3223 /* Skip over white space */
3224 while (isspace (*dbp
))
3238 /* Go till you get to white space or a syntactic break */
3240 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
3243 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3244 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3247 /* Find tags in TeX and LaTeX input files. */
3249 /* TEX_toktab is a table of TeX control sequences that define tags.
3250 Each TEX_tabent records one such control sequence.
3251 CONVERT THIS TO USE THE Stab TYPE!! */
3258 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
3260 /* Default set of control sequences to put into TEX_toktab.
3261 The value of environment var TEXTAGS is prepended to this. */
3263 char *TEX_defenv
= "\
3264 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3265 :part:appendix:entry:index";
3268 struct TEX_tabent
*TEX_decode_env ();
3270 #if TeX_named_tokens
3274 char TEX_esc
= '\\';
3275 char TEX_opgrp
= '{';
3276 char TEX_clgrp
= '}';
3279 * TeX/LaTeX scanning loop.
3290 /* Select either \ or ! as escape character. */
3293 /* Initialize token table once from environment. */
3295 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
3298 { /* Scan each line in file */
3300 linecharno
= charno
;
3301 charno
+= readline (&lb
, inf
);
3304 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
3310 linecharno
+= dbp
- lasthit
;
3312 i
= TEX_Token (lasthit
);
3316 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3317 #if TeX_named_tokens
3318 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
3320 break; /* We only save a line once */
3326 #define TEX_LESC '\\'
3327 #define TEX_SESC '!'
3330 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3331 chars accordingly. */
3338 while ((c
= getc (inf
)) != EOF
)
3340 /* Skip to next line if we hit the TeX comment char. */
3344 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3363 /* Read environment and prepend it to the default string.
3364 Build token table. */
3366 TEX_decode_env (evarname
, defenv
)
3370 register char *env
, *p
;
3372 struct TEX_tabent
*tab
;
3375 /* Append default string to environment. */
3376 env
= getenv (evarname
);
3380 env
= concat (env
, defenv
, "");
3382 /* Allocate a token table */
3383 for (size
= 1, p
= env
; p
;)
3384 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3386 /* Add 1 to leave room for null terminator. */
3387 tab
= xnew (size
+ 1, struct TEX_tabent
);
3389 /* Unpack environment string into token table. Be careful about */
3390 /* zero-length strings (leading ':', "::" and trailing ':') */
3393 p
= etags_strchr (env
, ':');
3394 if (!p
) /* End of environment string. */
3395 p
= env
+ strlen (env
);
3397 { /* Only non-zero strings. */
3398 tab
[i
].name
= savenstr (env
, p
- env
);
3399 tab
[i
].len
= strlen (tab
[i
].name
);
3406 tab
[i
].name
= NULL
; /* Mark end of table. */
3414 #if TeX_named_tokens
3415 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3416 The name being defined actually starts at (NAME + LEN + 1).
3417 But we seem to include the TeX command in the tag name. */
3419 TEX_getit (name
, len
)
3423 char *p
= name
+ len
;
3428 /* Let tag name extend to next group close (or end of line) */
3429 while (*p
&& *p
!= TEX_clgrp
)
3431 pfnote (savenstr (name
, p
-name
), TRUE
,
3432 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3436 /* If the text at CP matches one of the tag-defining TeX command names,
3437 return the pointer to the first occurrence of that command in TEX_toktab.
3438 Otherwise return -1.
3439 Keep the capital `T' in `Token' for dumb truncating compilers
3440 (this distinguishes it from `TEX_toktab' */
3447 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3448 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3453 /* Support for Prolog. */
3455 /* Whole head (not only functor, but also arguments)
3456 is gotten in compound term. */
3469 if (s
[0] == '\0') /* syntax error. */
3471 else if (insquote
&& s
[0] == '\'' && s
[1] == '\'')
3473 else if (s
[0] == '\'')
3475 insquote
= !insquote
;
3478 else if (!insquote
&& s
[0] == '(')
3483 else if (!insquote
&& s
[0] == ')')
3489 else if (npar
< 0) /* syntax error. */
3492 else if (!insquote
&& s
[0] == '.'
3493 && (isspace (s
[1]) || s
[1] == '\0'))
3495 if (npar
!= 0) /* syntax error. */
3503 pfnote ((CTAGS
) ? savenstr (save_s
, s
-save_s
) : NULL
, TRUE
,
3504 save_s
, s
-save_s
, lineno
, linecharno
);
3507 /* It is assumed that prolog predicate starts from column 0. */
3509 Prolog_functions (inf
)
3512 void skip_comment (), prolog_getit ();
3514 lineno
= linecharno
= charno
= 0;
3518 linecharno
+= charno
;
3519 charno
= readline (&lb
, inf
) + 1; /* 1 for newline. */
3521 if (isspace (dbp
[0])) /* not predicate header. */
3523 else if (dbp
[0] == '%') /* comment. */
3525 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3526 skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3533 skip_comment (plb
, inf
, plineno
, plinecharno
)
3534 struct linebuffer
*plb
;
3536 int *plineno
; /* result */
3537 long *plinecharno
; /* result */
3543 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3544 if (cp
[0] == '*' && cp
[1] == '/')
3547 *plinecharno
+= readline (plb
, inf
) + 1; /* 1 for newline. */
3552 #ifdef ETAGS_REGEXPS
3553 /* Take a string like "/blah/" and turn it into "blah", making sure
3554 that the first and last characters are the same, and handling
3555 quoted separator characters. Actually, stops on the occurence of
3556 an unquoted separator. Also turns "\t" into a Tab character.
3557 Returns pointer to terminating separator. Works in place. Null
3558 terminates name string. */
3560 scan_separators (name
)
3564 char *copyto
= name
;
3565 logical quoted
= FALSE
;
3567 for (++name
; *name
!= '\0'; ++name
)
3573 else if (*name
== sep
)
3577 /* Something else is quoted, so preserve the quote. */
3583 else if (*name
== '\\')
3585 else if (*name
== sep
)
3591 /* Terminate copied string. */
3596 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3597 expression, into a real regular expression by compiling it. */
3599 add_regex (regexp_pattern
)
3600 char *regexp_pattern
;
3604 struct re_pattern_buffer
*patbuf
;
3606 if (regexp_pattern
== NULL
)
3608 /* Remove existing regexps. */
3614 if (regexp_pattern
[0] == '\0')
3616 error ("missing regexp", 0);
3619 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3621 error ("%s: unterminated regexp", regexp_pattern
);
3624 name
= scan_separators (regexp_pattern
);
3625 if (regexp_pattern
[0] == '\0')
3627 error ("null regexp", 0);
3630 (void) scan_separators (name
);
3632 patbuf
= xnew (1, struct re_pattern_buffer
);
3633 patbuf
->translate
= NULL
;
3634 patbuf
->fastmap
= NULL
;
3635 patbuf
->buffer
= NULL
;
3636 patbuf
->allocated
= 0;
3638 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3641 error ("%s while compiling pattern", err
);
3646 if (num_patterns
== 1)
3647 patterns
= xnew (1, struct pattern
);
3649 patterns
= ((struct pattern
*)
3651 (num_patterns
* sizeof (struct pattern
))));
3652 patterns
[num_patterns
- 1].pattern
= patbuf
;
3653 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3654 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
3658 * Do the substitutions indicated by the regular expression and
3662 substitute (in
, out
, regs
)
3664 struct re_registers
*regs
;
3666 char *result
= NULL
, *t
;
3669 /* Pass 1: figure out how much size to allocate. */
3670 for (t
= out
; *t
; ++t
)
3677 fprintf (stderr
, "%s: pattern subtitution ends prematurely\n",
3684 size
+= regs
->end
[dig
] - regs
->start
[dig
];
3689 /* Allocate space and do the substitutions. */
3690 result
= xnew (size
+ 1, char);
3699 /* Using "dig2" satisfies my debugger. Bleah. */
3700 int dig2
= *out
- '0';
3701 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
3702 regs
->end
[dig2
] - regs
->start
[dig2
]);
3703 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
3706 result
[size
++] = *out
;
3709 result
[size
++] = *out
;
3711 result
[size
] = '\0';
3716 #endif /* ETAGS_REGEXPS */
3717 /* Initialize a linebuffer for use */
3719 initbuffer (linebuffer
)
3720 struct linebuffer
*linebuffer
;
3722 linebuffer
->size
= 200;
3723 linebuffer
->buffer
= xnew (200, char);
3727 * Read a line of text from `stream' into `linebuffer'.
3728 * Return the number of characters read from `stream',
3729 * which is the length of the line including the newline, if any.
3732 readline_internal (linebuffer
, stream
)
3733 struct linebuffer
*linebuffer
;
3734 register FILE *stream
;
3736 char *buffer
= linebuffer
->buffer
;
3737 register char *p
= linebuffer
->buffer
;
3738 register char *pend
;
3741 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
3745 register int c
= getc (stream
);
3748 linebuffer
->size
*= 2;
3749 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
3750 p
+= buffer
- linebuffer
->buffer
;
3751 pend
= buffer
+ linebuffer
->size
;
3752 linebuffer
->buffer
= buffer
;
3761 if (p
> buffer
&& p
[-1] == '\r')
3776 return p
- buffer
+ chars_deleted
;
3780 * Like readline_internal, above, but try to match the input
3781 * line against any existing regular expressions.
3784 readline (linebuffer
, stream
)
3785 struct linebuffer
*linebuffer
;
3788 /* Read new line. */
3789 long result
= readline_internal (linebuffer
, stream
);
3790 #ifdef ETAGS_REGEXPS
3793 /* Match against all listed patterns. */
3794 for (i
= 0; i
< num_patterns
; ++i
)
3796 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
3797 (int)result
, 0, &patterns
[i
].regs
);
3802 if (!patterns
[i
].error_signaled
)
3804 error ("error while matching pattern %d", i
);
3805 patterns
[i
].error_signaled
= TRUE
;
3812 /* Match occurred. Construct a tag. */
3813 if (patterns
[i
].name_pattern
[0] != '\0')
3815 /* Make a named tag. */
3816 char *name
= substitute (linebuffer
->buffer
,
3817 patterns
[i
].name_pattern
,
3821 linebuffer
->buffer
, match
, lineno
, linecharno
);
3825 /* Make an unnamed tag. */
3827 linebuffer
->buffer
, match
, lineno
, linecharno
);
3832 #endif /* ETAGS_REGEXPS */
3838 * Read a file, but do no processing. This is used to do regexp
3839 * matching on files that have no language defined.
3842 just_read_file (inf
)
3848 linecharno
= charno
;
3849 charno
+= readline (&lb
, inf
) + 1;
3855 * Return a pointer to a space of size strlen(cp)+1 allocated
3856 * with xnew where the string CP has been copied.
3862 return savenstr (cp
, strlen (cp
));
3866 * Return a pointer to a space of size LEN+1 allocated with xnew where
3867 * the string CP has been copied for at most the first LEN characters.
3876 dp
= xnew (len
+ 1, char);
3877 strncpy (dp
, cp
, len
);
3883 * Return the ptr in sp at which the character c last
3884 * appears; NULL if not found
3886 * Identical to System V strrchr, included for portability.
3889 etags_strrchr (sp
, c
)
3890 register char *sp
, c
;
3905 * Return the ptr in sp at which the character c first
3906 * appears; NULL if not found
3908 * Identical to System V strchr, included for portability.
3911 etags_strchr (sp
, c
)
3912 register char *sp
, c
;
3922 /* Print error message and exit. */
3940 suggest_asking_for_help ()
3942 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
3947 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3952 fprintf (stderr
, "%s: ", progname
);
3953 fprintf (stderr
, s1
, s2
);
3954 fprintf (stderr
, "\n");
3957 /* Return a newly-allocated string whose contents
3958 concatenate those of s1, s2, s3. */
3963 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3964 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
3966 strcpy (result
, s1
);
3967 strcpy (result
+ len1
, s2
);
3968 strcpy (result
+ len1
+ len2
, s3
);
3969 result
[len1
+ len2
+ len3
] = '\0';
3974 /* Does the same work as the system V getcwd, but does not need to
3975 guess the buffer size in advance. */
3980 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
3988 *p
++ = lowcase (*p
);
3990 return strdup (path
);
3991 #else /* not DOS_NT */
3994 char *path
= xnew (bufsize
, char);
3996 while (getcwd (path
, bufsize
) == NULL
)
3998 if (errno
!= ERANGE
)
4001 path
= xnew (bufsize
, char);
4005 #else /* not DOS_NT and not HAVE_GETCWD */
4006 struct linebuffer path
;
4010 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
4011 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
4016 #endif /* not HAVE_GETCWD */
4017 #endif /* not DOS_NT */
4020 /* Return a newly allocated string containing the filename
4021 of FILE relative to the absolute directory DIR (which
4022 should end with a slash). */
4024 relative_filename (file
, dir
)
4027 char *fp
, *dp
, *abs
, *res
;
4029 /* Find the common root of file and dir. */
4030 abs
= absolute_filename (file
, cwd
);
4033 while (*fp
++ == *dp
++)
4042 /* Build a sequence of "../" strings for the resulting relative filename. */
4043 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
4045 dp
= etags_strchr (dp
+ 1, '/'))
4047 res
= concat (res
, "../", "");
4050 /* Add the filename relative to the common root of file and dir. */
4051 res
= concat (res
, fp
+ 1, "");
4057 /* Return a newly allocated string containing the
4058 absolute filename of FILE given CWD (which should
4059 end with a slash). */
4061 absolute_filename (file
, cwd
)
4064 char *slashp
, *cp
, *res
;
4066 if (absolutefn (file
))
4067 res
= concat (file
, "", "");
4069 res
= concat (cwd
, file
, "");
4071 /* Delete the "/dirname/.." and "/." substrings. */
4072 slashp
= etags_strchr (res
, '/');
4073 while (slashp
!= NULL
&& slashp
[0] != '\0')
4075 if (slashp
[1] == '.')
4077 if (slashp
[2] == '.'
4078 && (slashp
[3] == '/' || slashp
[3] == '\0'))
4083 while (cp
>= res
&& *cp
!= '/');
4086 strcpy (cp
, slashp
+ 3);
4088 else /* else (cp == res) */
4090 if (slashp
[3] != '\0')
4091 strcpy (cp
, slashp
+ 4);
4098 else if (slashp
[2] == '/' || slashp
[2] == '\0')
4100 strcpy (slashp
, slashp
+ 2);
4105 slashp
= etags_strchr (slashp
+ 1, '/');
4111 /* Return a newly allocated string containing the absolute
4112 filename of dir where FILE resides given CWD (which should
4113 end with a slash). */
4115 absolute_dirname (file
, cwd
)
4121 slashp
= etags_strrchr (file
, '/');
4126 res
= absolute_filename (file
, cwd
);
4132 /* Like malloc but get fatal error if memory is exhausted. */
4137 long *result
= (long *) malloc (size
);
4139 fatal ("virtual memory exhausted", 0);
4144 xrealloc (ptr
, size
)
4148 long *result
= (long *) realloc (ptr
, size
);
4150 fatal ("virtual memory exhausted");