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
5 This file is not considered part of GNU Emacs.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 * Ctags originally by Ken Arnold.
24 * Fortran added by Jim Kleckner.
25 * Ed Pelegri-Llopart added C typedefs.
26 * Gnu Emacs TAGS format and modifications by RMS?
27 * Sam Kendall added C++.
28 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
29 * Regexp tags by Tom Tromey.
31 * Francesco Potorti` (F.Potorti@cnuce.cnr.it) is the current maintainer.
34 char pot_etags_version
[] = "@(#) pot revision number is 11.59";
46 # include <sys/param.h>
54 # define MAXPATHLEN _MAX_PATH
59 /* On some systems, Emacs defines static as nothing for the sake
60 of unexec. We don't want that here since we don't use unexec. */
70 #include <sys/types.h>
73 #if !defined (S_ISREG) && defined (S_IFREG)
74 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
81 #endif /* ETAGS_REGEXPS */
83 /* Define CTAGS to make the program "ctags" compatible with the usual one.
84 Let it undefined to make the program "etags", which makes emacs-style
85 tag tables and tags typedefs, #defines and struct/union/enum by default. */
93 /* Exit codes for success and failure. */
103 #define C_PLPL 0x00001 /* C++ */
104 #define C_STAR 0x00003 /* C* */
105 #define YACC 0x10000 /* yacc file */
107 #define streq(s,t) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strcmp(s,t))
108 #define strneq(s,t,n) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strncmp(s,t,n))
110 #define lowcase(c) tolower ((unsigned char)c)
112 #define iswhite(arg) (_wht[arg]) /* T if char is white */
113 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
114 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
115 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
118 # define absolutefn(fn) (fn[0] == '/' \
119 || (fn[1] == ':' && fn[2] == '/'))
121 # define absolutefn(fn) (fn[0] == '/')
126 * xnew -- allocate storage
128 * SYNOPSIS: Type *xnew (int n, Type);
130 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
135 { /* sorting structure */
136 char *name
; /* function or type name */
137 char *file
; /* file name */
138 logical is_func
; /* use pattern or line no */
139 logical been_warned
; /* set if noticed dup */
140 int lno
; /* line number tag is on */
141 long cno
; /* character number line starts on */
142 char *pat
; /* search pattern */
143 struct nd_st
*left
, *right
; /* left and right sons */
146 extern char *getenv ();
149 char *savenstr (), *savestr ();
150 char *etags_strchr (), *etags_strrchr ();
151 char *etags_getcwd ();
152 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
153 long *xmalloc (), *xrealloc ();
155 typedef void Lang_function ();
156 #if FALSE /* many compilers barf on this */
157 Lang_function Asm_labels
;
158 Lang_function default_C_entries
;
159 Lang_function C_entries
;
160 Lang_function Cplusplus_entries
;
161 Lang_function Cstar_entries
;
162 Lang_function Erlang_functions
;
163 Lang_function Fortran_functions
;
164 Lang_function Yacc_entries
;
165 Lang_function Lisp_functions
;
166 Lang_function Pascal_functions
;
167 Lang_function Perl_functions
;
168 Lang_function Prolog_functions
;
169 Lang_function Scheme_functions
;
170 Lang_function TeX_functions
;
171 Lang_function just_read_file
;
172 #else /* so let's write it this way */
175 void default_C_entries ();
176 void plain_C_entries ();
177 void Cplusplus_entries ();
178 void Cstar_entries ();
179 void Erlang_functions ();
180 void Fortran_functions ();
181 void Yacc_entries ();
182 void Lisp_functions ();
183 void Pascal_functions ();
184 void Perl_functions ();
185 void Prolog_functions ();
186 void Scheme_functions ();
187 void TeX_functions ();
188 void just_read_file ();
191 Lang_function
*get_language_from_name ();
192 Lang_function
*get_language_from_interpreter ();
193 Lang_function
*get_language_from_suffix ();
194 int total_size_of_entries ();
196 long readline_internal ();
202 void suggest_asking_for_help ();
203 void fatal (), pfatal ();
204 void find_entries ();
210 void process_file ();
215 char searchar
= '/'; /* use /.../ searches */
217 int lineno
; /* line number of current line */
218 long charno
; /* current character number */
220 long linecharno
; /* charno of start of line; not used by C,
221 but by every other language. */
223 char *curfile
; /* current input file name */
224 char *tagfile
; /* output file */
225 char *progname
; /* name this program was invoked with */
226 char *cwd
; /* current working directory */
227 char *tagfiledir
; /* directory of tagfile */
229 FILE *tagf
; /* ioptr for tags file */
230 NODE
*head
; /* the head of the binary tree of tags */
233 * A `struct linebuffer' is a structure which holds a line of text.
234 * `readline' reads a line from a stream into a linebuffer and works
235 * regardless of the length of the line.
237 #define GROW_LINEBUFFER(buf,toksize) \
238 while (buf.size < toksize) \
239 buf.buffer = (char *) xrealloc (buf.buffer, buf.size *= 2)
246 struct linebuffer lb
; /* the current line */
247 struct linebuffer token_name
; /* used by C_entries as a temporary area */
251 struct linebuffer lb
; /* used by C_entries instead of lb */
254 /* boolean "functions" (see init) */
255 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
258 *white
= " \f\t\n\013",
259 /* token ending chars */
260 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
261 /* token starting chars */
262 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
263 /* valid in-token chars */
264 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
266 logical append_to_tagfile
; /* -a: append to tags */
267 /* The following three default to TRUE for etags, but to FALSE for ctags. */
268 logical typedefs
; /* -t: create tags for typedefs */
269 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
270 /* 0 struct/enum/union decls, and C++ */
271 /* member functions. */
272 logical constantypedefs
; /* -d: create tags for C #define and enum */
273 /* constants. Enum consts not implemented. */
274 /* -D: opposite of -d. Default under ctags. */
275 logical update
; /* -u: update tags */
276 logical vgrind_style
; /* -v: create vgrind style index output */
277 logical no_warnings
; /* -w: suppress warnings */
278 logical cxref_style
; /* -x: create cxref style output */
279 logical cplusplus
; /* .[hc] means C++, not C */
280 logical noindentypedefs
; /* -I: ignore indentation in C */
282 struct option longopts
[] =
284 { "append", no_argument
, NULL
, 'a' },
285 { "backward-search", no_argument
, NULL
, 'B' },
286 { "c++", no_argument
, NULL
, 'C' },
287 { "cxref", no_argument
, NULL
, 'x' },
288 { "defines", no_argument
, NULL
, 'd' },
289 { "help", no_argument
, NULL
, 'h' },
290 { "help", no_argument
, NULL
, 'H' },
291 { "ignore-indentation", no_argument
, NULL
, 'I' },
292 { "include", required_argument
, NULL
, 'i' },
293 { "language", required_argument
, NULL
, 'l' },
294 { "no-defines", no_argument
, NULL
, 'D' },
295 { "no-regex", no_argument
, NULL
, 'R' },
296 { "no-warn", no_argument
, NULL
, 'w' },
297 { "output", required_argument
, NULL
, 'o' },
298 { "regex", required_argument
, NULL
, 'r' },
299 { "typedefs", no_argument
, NULL
, 't' },
300 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
301 { "update", no_argument
, NULL
, 'u' },
302 { "version", no_argument
, NULL
, 'V' },
303 { "vgrind", no_argument
, NULL
, 'v' },
308 /* Structure defining a regular expression. Elements are
309 the compiled pattern, and the name string. */
312 struct re_pattern_buffer
*pattern
;
313 struct re_registers regs
;
315 logical error_signaled
;
318 /* Number of regexps found. */
319 int num_patterns
= 0;
321 /* Array of all regexps. */
322 struct pattern
*patterns
= NULL
;
323 #endif /* ETAGS_REGEXPS */
329 /* Non-NULL if language fixed. */
330 Lang_function
*lang_func
= NULL
;
333 char *Asm_suffixes
[] = { "a", /* Unix assembler */
334 "asm", /* Microcontroller assembly */
335 "def", /* BSO/Tasking definition includes */
336 "inc", /* Microcontroller include files */
337 "ins", /* Microcontroller include files */
338 "s", "sa", /* Unix assembler */
339 "src", /* BSO/Tasking C compiler output */
343 /* Note that .c and .h can be considered C++, if the --c++ flag was
344 given. That is why default_C_entries is called here. */
345 char *default_C_suffixes
[] =
348 /* .M is for Objective C++ files. */
349 char *Cplusplus_suffixes
[] =
350 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL
};
352 char *Cstar_suffixes
[] =
353 { "cs", "hs", NULL
};
355 char *Erlang_suffixes
[] =
356 { "erl", "hrl", NULL
};
358 char *Fortran_suffixes
[] =
359 { "F", "f", "f90", "for", NULL
};
361 char *Lisp_suffixes
[] =
362 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL
};
364 char *Pascal_suffixes
[] =
365 { "p", "pas", NULL
};
367 char *Perl_suffixes
[] =
368 { "pl", "pm", NULL
};
369 char *Perl_interpreters
[] =
370 { "perl", "@PERL@", NULL
};
372 char *plain_C_suffixes
[] =
373 { "pc", /* Pro*C file */
374 "m", /* Objective C file */
375 "lm", /* Objective lex file */
378 char *Prolog_suffixes
[] =
381 /* Can't do the `SCM' or `scm' prefix with a version number. */
382 char *Scheme_suffixes
[] =
383 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL
};
385 char *TeX_suffixes
[] =
386 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL
};
388 char *Yacc_suffixes
[] =
389 { "y", "ym", NULL
}; /* .ym is Objective yacc file */
391 /* Table of language names and corresponding functions, file suffixes
392 and interpreter names.
393 It is ok for a given function to be listed under more than one
394 name. I just didn't. */
398 Lang_function
*function
;
403 struct lang_entry lang_names
[] =
405 { "asm", Asm_labels
, Asm_suffixes
, NULL
},
406 { "c", default_C_entries
, default_C_suffixes
, NULL
},
407 { "c++", Cplusplus_entries
, Cplusplus_suffixes
, NULL
},
408 { "c*", Cstar_entries
, Cstar_suffixes
, NULL
},
409 { "erlang", Erlang_functions
, Erlang_suffixes
, NULL
},
410 { "fortran", Fortran_functions
, Fortran_suffixes
, NULL
},
411 { "lisp", Lisp_functions
, Lisp_suffixes
, NULL
},
412 { "pascal", Pascal_functions
, Pascal_suffixes
, NULL
},
413 { "perl", Perl_functions
, Perl_suffixes
, Perl_interpreters
},
414 { "proc", plain_C_entries
, plain_C_suffixes
, NULL
},
415 { "prolog", Prolog_functions
, Prolog_suffixes
, NULL
},
416 { "scheme", Scheme_functions
, Scheme_suffixes
, NULL
},
417 { "tex", TeX_functions
, TeX_suffixes
, NULL
},
418 { "yacc", Yacc_entries
, Yacc_suffixes
, NULL
},
419 { "auto", NULL
}, /* default guessing scheme */
420 { "none", just_read_file
}, /* regexp matching only */
421 { NULL
, NULL
} /* end of list */
426 print_language_names ()
428 struct lang_entry
*lang
;
431 puts ("\nThese are the currently supported languages, along with the\n\
432 default file name suffixes:");
433 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
435 printf ("\t%s\t", lang
->name
);
436 if (lang
->suffixes
!= NULL
)
437 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
438 printf (" .%s", *ext
);
441 puts ("Where `auto' means use default language for files based on file\n\
442 name suffix, and `none' means only do regexp processing on files.\n\
443 If no language is specified and no matching suffix is found,\n\
444 the first line of the file is read for a sharp-bang (#!) sequence\n\
445 followed by the name of an interpreter. If no such sequence is found,\n\
446 Fortran is tried first; if no tags are found, C is tried next.");
450 # define VERSION "19"
455 printf ("%s for Emacs version %s\n", (CTAGS
) ? "ctags" : "etags", VERSION
);
463 printf ("These are the options accepted by %s. You may use unambiguous\n\
464 abbreviations for the long option names. A - as file name means read\n\
465 names from stdin.", progname
);
467 printf (" Absolute names are stored in the output file as they\n\
468 are. Relative ones are stored relative to the output file's directory.");
471 puts ("-a, --append\n\
472 Append tag entries to existing tags file.");
475 puts ("-B, --backward-search\n\
476 Write the search commands for the tag entries using '?', the\n\
477 backward-search command instead of '/', the forward-search command.");
480 Treat files whose name suffix defaults to C language as C++ files.");
483 puts ("-d, --defines\n\
484 Create tag entries for constant C #defines, too.");
486 puts ("-D, --no-defines\n\
487 Don't create tag entries for constant C #defines. This makes\n\
488 the tags file smaller.");
492 puts ("-i FILE, --include=FILE\n\
493 Include a note in tag file indicating that, when searching for\n\
494 a tag, one should also consult the tags file FILE after\n\
495 checking the current file.");
496 puts ("-l LANG, --language=LANG\n\
497 Force the following files to be considered as written in the\n\
498 named language up to the next --language=LANG option.");
502 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
503 Make a tag for each line matching pattern REGEXP in the\n\
504 following files. REGEXP is anchored (as if preceded by ^).\n\
505 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
506 named tags can be created with:\n\
507 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
508 puts ("-R, --no-regex\n\
509 Don't create tags from regexps for the following files.");
510 #endif /* ETAGS_REGEXPS */
511 puts ("-o FILE, --output=FILE\n\
512 Write the tags to FILE.");
513 puts ("-I, --ignore-indentation\n\
514 Don't rely on indentation quite as much as normal. Currently,\n\
515 this means not to assume that a closing brace in the first\n\
516 column is the final brace of a function or structure\n\
517 definition in C and C++.");
521 puts ("-t, --typedefs\n\
522 Generate tag entries for C typedefs.");
523 puts ("-T, --typedefs-and-c++\n\
524 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
525 and C++ member functions.");
526 puts ("-u, --update\n\
527 Update the tag entries for the given files, leaving tag\n\
528 entries for other files in place. Currently, this is\n\
529 implemented by deleting the existing entries for the given\n\
530 files and then rewriting the new entries at the end of the\n\
531 tags file. It is often faster to simply rebuild the entire\n\
532 tag file than to use this.");
533 puts ("-v, --vgrind\n\
534 Generates an index of items intended for human consumption,\n\
535 similar to the output of vgrind. The index is sorted, and\n\
536 gives the page number of each item.");
537 puts ("-w, --no-warn\n\
538 Suppress warning messages about entries defined in multiple\n\
540 puts ("-x, --cxref\n\
541 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
542 The output uses line numbers instead of page numbers, but\n\
543 beyond that the differences are cosmetic; try both to see\n\
547 puts ("-V, --version\n\
548 Print the version of the program.\n\
550 Print this help message.");
552 print_language_names ();
565 /* This structure helps us allow mixing of --lang and filenames. */
568 enum argument_type arg_type
;
570 Lang_function
*function
;
573 #ifdef VMS /* VMS specific functions */
577 /* This is a BUG! ANY arbitrary limit is a BUG!
578 Won't someone please fix this? */
579 #define MAX_FILE_SPEC_LEN 255
582 char body
[MAX_FILE_SPEC_LEN
+ 1];
586 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
587 returning in each successive call the next filename matching the input
588 spec. The function expects that each in_spec passed
589 to it will be processed to completion; in particular, up to and
590 including the call following that in which the last matching name
591 is returned, the function ignores the value of in_spec, and will
592 only start processing a new spec with the following call.
593 If an error occurs, on return out_spec contains the value
594 of in_spec when the error occurred.
596 With each successive filename returned in out_spec, the
597 function's return value is one. When there are no more matching
598 names the function returns zero. If on the first call no file
599 matches in_spec, or there is any other error, -1 is returned.
604 #define OUTSIZE MAX_FILE_SPEC_LEN
610 static long context
= 0;
611 static struct dsc$descriptor_s o
;
612 static struct dsc$descriptor_s i
;
613 static logical pass1
= TRUE
;
620 o
.dsc$a_pointer
= (char *) out
;
621 o
.dsc$w_length
= (short)OUTSIZE
;
622 i
.dsc$a_pointer
= in
;
623 i
.dsc$w_length
= (short)strlen(in
);
624 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
625 i
.dsc$b_class
= DSC$K_CLASS_S
;
626 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
627 o
.dsc$b_class
= DSC$K_CLASS_VS
;
629 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
631 out
->body
[out
->curlen
] = EOS
;
634 else if (status
== RMS$_NMF
)
638 strcpy(out
->body
, in
);
641 lib$
find_file_end(&context
);
647 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
648 name of each file specified by the provided arg expanding wildcards.
651 gfnames (arg
, p_error
)
655 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
657 switch (fn_exp (&filename
, arg
))
661 return filename
.body
;
667 return filename
.body
;
671 #ifndef OLD /* Newer versions of VMS do provide `system'. */
675 fprintf (stderr
, "system() function not implemented under VMS\n");
679 #define VERSION_DELIM ';'
680 char *massage_name (s
)
686 if (*s
== VERSION_DELIM
)
704 unsigned int nincluded_files
= 0;
705 char **included_files
= xnew (argc
, char *);
708 int current_arg
= 0, file_count
= 0;
709 struct linebuffer filename_lb
;
715 _fmode
= O_BINARY
; /* all of files are treated as binary files */
720 /* Allocate enough no matter what happens. Overkill, but each one
722 argbuffer
= xnew (argc
, argument
);
725 /* Set syntax for regular expression routines. */
726 re_set_syntax (RE_SYNTAX_EMACS
);
727 #endif /* ETAGS_REGEXPS */
730 * If etags, always find typedefs and structure tags. Why not?
731 * Also default is to find macro constants.
734 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
738 int opt
= getopt_long (argc
, argv
,
739 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
747 /* If getopt returns 0, then it has already processed a
748 long-named option. We should do nothing. */
752 /* This means that a filename has been seen. Record it. */
753 argbuffer
[current_arg
].arg_type
= at_filename
;
754 argbuffer
[current_arg
].what
= optarg
;
759 /* Common options. */
761 append_to_tagfile
= TRUE
;
767 constantypedefs
= TRUE
;
770 constantypedefs
= FALSE
;
772 case 'f': /* for compatibility with old makefiles */
776 fprintf (stderr
, "%s: -%c option may only be given once.\n",
778 suggest_asking_for_help ();
783 case 'S': /* for backward compatibility */
784 noindentypedefs
= TRUE
;
787 argbuffer
[current_arg
].function
= get_language_from_name (optarg
);
788 argbuffer
[current_arg
].arg_type
= at_language
;
793 argbuffer
[current_arg
].arg_type
= at_regexp
;
794 argbuffer
[current_arg
].what
= optarg
;
798 argbuffer
[current_arg
].arg_type
= at_regexp
;
799 argbuffer
[current_arg
].what
= NULL
;
802 #endif /* ETAGS_REGEXPS */
814 typedefs
= typedefs_and_cplusplus
= TRUE
;
819 included_files
[nincluded_files
++] = optarg
;
840 suggest_asking_for_help ();
844 for (; optind
< argc
; ++optind
)
846 argbuffer
[current_arg
].arg_type
= at_filename
;
847 argbuffer
[current_arg
].what
= argv
[optind
];
852 if (nincluded_files
== 0 && file_count
== 0)
854 fprintf (stderr
, "%s: No input files specified.\n", progname
);
855 suggest_asking_for_help ();
859 tagfile
= CTAGS
? "tags" : "TAGS";
860 cwd
= etags_getcwd (); /* the current working directory */
861 if (cwd
[strlen(cwd
)-1] != '/')
863 if (streq (tagfile
, "-"))
866 tagfiledir
= absolute_dirname (tagfile
, cwd
);
868 init (); /* set up boolean "functions" */
871 initbuffer (&token_name
);
872 initbuffer (&lbs
[0].lb
);
873 initbuffer (&lbs
[1].lb
);
874 initbuffer (&filename_lb
);
878 if (streq (tagfile
, "-"))
882 /* Switch redirected `stdout' to binary mode (setting `_fmode'
883 doesn't take effect until after `stdout' is already open), */
884 if (!isatty (fileno (stdout
)))
885 setmode (fileno (stdout
), O_BINARY
);
889 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
895 * Loop through files finding functions.
897 for (i
= 0; i
< current_arg
; ++i
)
899 switch (argbuffer
[i
].arg_type
)
902 lang_func
= argbuffer
[i
].function
;
906 add_regex (argbuffer
[i
].what
);
911 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
915 error ("Can't find file %s\n", this_file
);
920 this_file
= massage_name (this_file
);
923 this_file
= argbuffer
[i
].what
;
925 /* Input file named "-" means read file names from stdin
927 if (streq (this_file
, "-"))
928 while (readline_internal (&filename_lb
, stdin
) > 0)
929 process_file (filename_lb
.buffer
);
931 process_file (this_file
);
941 while (nincluded_files
-- > 0)
942 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
948 /* If CTAGS, we are here. process_file did not write the tags yet,
949 because we want them ordered. Let's do it now. */
952 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
962 for (i
= 0; i
< current_arg
; ++i
)
964 if (argbuffer
[i
].arg_type
!= at_filename
)
967 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
968 tagfile
, argbuffer
[i
].what
, tagfile
);
969 if (system (cmd
) != GOOD
)
970 fatal ("failed to execute shell command");
972 append_to_tagfile
= TRUE
;
975 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
984 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
992 * Return a Lang_function given the name.
995 get_language_from_name (name
)
998 struct lang_entry
*lang
;
1001 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1003 if (streq (name
, lang
->name
))
1004 return lang
->function
;
1007 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
1009 suggest_asking_for_help ();
1011 /* This point should never be reached. The function should either
1012 return a function pointer or never return. Note that a NULL
1013 pointer cannot be considered as an error, as it means that the
1014 language has not been explicitely imposed by the user ("auto"). */
1015 return NULL
; /* avoid warnings from compiler */
1020 * Return a Lang_function given the interpreter name.
1023 get_language_from_interpreter (interpreter
)
1026 struct lang_entry
*lang
;
1029 if (interpreter
== NULL
)
1031 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1032 if (lang
->interpreters
!= NULL
)
1033 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1034 if (streq (*iname
, interpreter
))
1035 return lang
->function
;
1043 * Return a Lang_function given the file suffix.
1046 get_language_from_suffix (suffix
)
1049 struct lang_entry
*lang
;
1054 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1055 if (lang
->suffixes
!= NULL
)
1056 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1057 if (streq (*ext
, suffix
))
1058 return lang
->function
;
1065 * This routine is called on each file argument.
1071 struct stat stat_buf
;
1074 /* The rest of the program can't grok `\\'-style slashes. */
1085 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1087 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1090 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1092 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1095 inf
= fopen (file
, "r");
1102 find_entries (file
, inf
);
1108 if (absolutefn (file
))
1110 /* file is an absolute filename. Canonicalise it. */
1111 filename
= absolute_filename (file
, cwd
);
1115 /* file is a filename relative to cwd. Make it relative
1116 to the directory of the tags file. */
1117 filename
= relative_filename (file
, tagfiledir
);
1119 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1128 * This routine sets up the boolean pseudo-functions which work
1129 * by setting boolean flags dependent upon the corresponding character
1130 * Every char which is NOT in that string is not a white char. Therefore,
1131 * all of the array "_wht" is set to FALSE, and then the elements
1132 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1133 * of a char is TRUE if it is the string "white", else FALSE.
1141 for (i
= 0; i
< 0177; i
++)
1142 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1143 for (sp
= white
; *sp
; sp
++)
1145 for (sp
= endtk
; *sp
; sp
++)
1147 for (sp
= intk
; *sp
; sp
++)
1149 for (sp
= begtk
; *sp
; sp
++)
1151 _wht
[0] = _wht
['\n'];
1152 _etk
[0] = _etk
['\n'];
1153 _btk
[0] = _btk
['\n'];
1154 _itk
[0] = _itk
['\n'];
1158 * This routine opens the specified file and calls the function
1159 * which finds the function and type definitions.
1162 find_entries (file
, inf
)
1167 Lang_function
*function
;
1168 NODE
*old_last_node
;
1169 extern NODE
*last_node
;
1172 /* Memory leakage here: the memory block pointed by curfile is never
1173 released. The amount of memory leaked here is the sum of the
1174 lengths of the input file names. */
1175 curfile
= savestr (file
);
1177 /* If user specified a language, use it. */
1178 function
= lang_func
;
1179 if (function
!= NULL
)
1186 cp
= etags_strrchr (file
, '.');
1190 function
= get_language_from_suffix (cp
);
1191 if (function
!= NULL
)
1199 /* Look for sharp-bang as the first two characters. */
1200 if (readline_internal (&lb
, inf
) > 2
1201 && lb
.buffer
[0] == '#'
1202 && lb
.buffer
[1] == '!')
1206 /* Set lp to point at the first char after the last slash in the
1207 line or, if no slashes, at the first nonblank. Then set cp to
1208 the first successive blank and terminate the string. */
1209 lp
= etags_strrchr (lb
.buffer
+2, '/');
1213 for (lp
= lb
.buffer
+2; *lp
!= '\0' && isspace (*lp
); lp
++)
1215 for (cp
= lp
; *cp
!= '\0' && !isspace (*cp
); cp
++)
1219 if (strlen (lp
) > 0)
1221 function
= get_language_from_interpreter (lp
);
1222 if (function
!= NULL
)
1233 old_last_node
= last_node
;
1234 Fortran_functions (inf
);
1236 /* No Fortran entries found. Try C. */
1237 if (old_last_node
== last_node
)
1240 default_C_entries (inf
);
1248 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1249 char *name
; /* tag name, or NULL if unnamed */
1250 logical is_func
; /* tag is a function */
1251 char *linestart
; /* start of the line where tag is */
1252 int linelen
; /* length of the line where tag is */
1253 int lno
; /* line number */
1254 long cno
; /* character number */
1258 if (CTAGS
&& name
== NULL
)
1261 np
= xnew (1, NODE
);
1263 /* If ctags mode, change name "main" to M<thisfilename>. */
1264 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1266 register char *fp
= etags_strrchr (curfile
, '/');
1267 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1268 fp
= etags_strrchr (np
->name
, '.');
1269 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1274 np
->been_warned
= FALSE
;
1276 np
->is_func
= is_func
;
1278 /* Our char numbers are 0-base, because of C language tradition?
1279 ctags compatibility? old versions compatibility? I don't know.
1280 Anyway, since emacs's are 1-base we expect etags.el to take care
1281 of the difference. If we wanted to have 1-based numbers, we would
1282 uncomment the +1 below. */
1283 np
->cno
= cno
/* + 1 */ ;
1284 np
->left
= np
->right
= NULL
;
1285 if (CTAGS
&& !cxref_style
)
1287 if (strlen (linestart
) < 50)
1288 np
->pat
= concat (linestart
, "$", "");
1290 np
->pat
= savenstr (linestart
, 50);
1293 np
->pat
= savenstr (linestart
, linelen
);
1295 add_node (np
, &head
);
1300 * recurse on left children, iterate on right children.
1304 register NODE
*node
;
1308 register NODE
*node_right
= node
->right
;
1309 free_tree (node
->left
);
1310 if (node
->name
!= NULL
)
1313 free ((char *) node
);
1320 * Adds a node to the tree of nodes. In etags mode, we don't keep
1321 * it sorted; we just keep a linear list. In ctags mode, maintain
1322 * an ordered tree, with no attempt at balancing.
1324 * add_node is the only function allowed to add nodes, so it can
1327 NODE
*last_node
= NULL
;
1329 add_node (node
, cur_node_p
)
1330 NODE
*node
, **cur_node_p
;
1333 register NODE
*cur_node
= *cur_node_p
;
1335 if (cur_node
== NULL
)
1345 if (last_node
== NULL
)
1346 fatal ("internal error in add_node", 0);
1347 last_node
->right
= node
;
1353 dif
= strcmp (node
->name
, cur_node
->name
);
1356 * If this tag name matches an existing one, then
1357 * do not add the node, but maybe print a warning.
1361 if (streq (node
->file
, cur_node
->file
))
1365 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1366 node
->file
, lineno
, node
->name
);
1367 fprintf (stderr
, "Second entry ignored\n");
1370 else if (!cur_node
->been_warned
&& !no_warnings
)
1374 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1375 node
->file
, cur_node
->file
, node
->name
);
1376 cur_node
->been_warned
= TRUE
;
1381 /* Actually add the node */
1382 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1388 register NODE
*node
;
1395 /* Output subentries that precede this one */
1396 put_entries (node
->left
);
1398 /* Output this entry */
1402 if (node
->name
!= NULL
)
1403 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1404 node
->pat
, node
->name
, node
->lno
, node
->cno
);
1406 fprintf (tagf
, "%s\177%d,%d\n",
1407 node
->pat
, node
->lno
, node
->cno
);
1411 if (node
->name
== NULL
)
1412 error ("internal error: NULL name in ctags mode.", 0);
1417 fprintf (stdout
, "%s %s %d\n",
1418 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1420 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1421 node
->name
, node
->lno
, node
->file
, node
->pat
);
1425 fprintf (tagf
, "%s\t%s\t", node
->name
, node
->file
);
1429 putc (searchar
, tagf
);
1432 for (sp
= node
->pat
; *sp
; sp
++)
1434 if (*sp
== '\\' || *sp
== searchar
)
1438 putc (searchar
, tagf
);
1441 { /* a typedef; text pattern inadequate */
1442 fprintf (tagf
, "%d", node
->lno
);
1448 /* Output subentries that follow this one */
1449 put_entries (node
->right
);
1452 /* Length of a number's decimal representation. */
1460 for (; num
; num
/= 10)
1466 * Return total number of characters that put_entries will output for
1467 * the nodes in the subtree of the specified node. Works only if
1468 * we are not ctags, but called only in that case. This count
1469 * is irrelevant with the new tags.el, but is still supplied for
1470 * backward compatibility.
1473 total_size_of_entries (node
)
1474 register NODE
*node
;
1482 for (; node
; node
= node
->right
)
1484 /* Count left subentries. */
1485 total
+= total_size_of_entries (node
->left
);
1487 /* Count this entry */
1488 total
+= strlen (node
->pat
) + 1;
1489 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1490 if (node
->name
!= NULL
)
1491 total
+= 1 + strlen (node
->name
); /* \001name */
1498 * The C symbol tables.
1502 st_none
, st_C_objprot
, st_C_objimpl
, st_C_objend
, st_C_gnumacro
,
1503 st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1506 /* Feed stuff between (but not including) %[ and %] lines to:
1507 gperf -c -k1,3 -o -p -r -t
1509 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1511 @interface, 0, st_C_objprot
1512 @protocol, 0, st_C_objprot
1513 @implementation,0, st_C_objimpl
1514 @end, 0, st_C_objend
1515 class, C_PLPL, st_C_struct
1516 domain, C_STAR, st_C_struct
1517 union, 0, st_C_struct
1518 struct, 0, st_C_struct
1520 typedef, 0, st_C_typedef
1521 define, 0, st_C_define
1522 long, 0, st_C_typespec
1523 short, 0, st_C_typespec
1524 int, 0, st_C_typespec
1525 char, 0, st_C_typespec
1526 float, 0, st_C_typespec
1527 double, 0, st_C_typespec
1528 signed, 0, st_C_typespec
1529 unsigned, 0, st_C_typespec
1530 auto, 0, st_C_typespec
1531 void, 0, st_C_typespec
1532 extern, 0, st_C_typespec
1533 static, 0, st_C_typespec
1534 const, 0, st_C_typespec
1535 volatile, 0, st_C_typespec
1536 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1537 DEFUN, 0, st_C_gnumacro
1538 SYSCALL, 0, st_C_gnumacro
1539 ENTRY, 0, st_C_gnumacro
1540 PSEUDO, 0, st_C_gnumacro
1541 # These are defined inside C functions, so currently they are not met.
1542 # EXFUN used in glibc, DEFVAR_* in emacs.
1543 #EXFUN, 0, st_C_gnumacro
1544 #DEFVAR_, 0, st_C_gnumacro
1546 and replace lines between %< and %> with its output. */
1548 /* C code produced by gperf version 1.8.1 (K&R C version) */
1549 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1552 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1554 #define MIN_WORD_LENGTH 3
1555 #define MAX_WORD_LENGTH 15
1556 #define MIN_HASH_VALUE 7
1557 #define MAX_HASH_VALUE 63
1560 57 is the maximum key range
1568 static unsigned char hash_table
[] =
1570 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1571 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1572 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1573 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1574 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1575 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1576 63, 63, 63, 63, 17, 63, 63, 63, 4, 14,
1577 4, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1578 8, 63, 63, 0, 23, 63, 63, 63, 63, 63,
1579 63, 63, 63, 63, 63, 63, 63, 28, 63, 28,
1580 10, 31, 27, 18, 63, 6, 63, 63, 26, 1,
1581 11, 2, 29, 63, 29, 16, 26, 13, 15, 63,
1582 63, 63, 63, 63, 63, 63, 63, 63,
1584 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1587 struct C_stab_entry
*
1588 in_word_set (str
, len
)
1593 static struct C_stab_entry wordlist
[] =
1595 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1596 {"SYSCALL", 0, st_C_gnumacro
},
1597 {"",}, {"",}, {"",}, {"",}, {"",},
1598 {"DEFUN", 0, st_C_gnumacro
},
1599 {"",}, {"",}, {"",},
1600 {"domain", C_STAR
, st_C_struct
},
1601 {"",}, {"",}, {"",}, {"",}, {"",},
1602 {"short", 0, st_C_typespec
},
1603 {"union", 0, st_C_struct
},
1604 {"void", 0, st_C_typespec
},
1606 {"PSEUDO", 0, st_C_gnumacro
},
1607 {"double", 0, st_C_typespec
},
1609 {"@end", 0, st_C_objend
},
1610 {"@implementation", 0, st_C_objimpl
},
1611 {"float", 0, st_C_typespec
},
1612 {"int", 0, st_C_typespec
},
1614 {"unsigned", 0, st_C_typespec
},
1615 {"@interface", 0, st_C_objprot
},
1617 {"signed", 0, st_C_typespec
},
1618 {"long", 0, st_C_typespec
},
1619 {"ENTRY", 0, st_C_gnumacro
},
1620 {"define", 0, st_C_define
},
1621 {"const", 0, st_C_typespec
},
1622 {"",}, {"",}, {"",},
1623 {"enum", 0, st_C_enum
},
1624 {"volatile", 0, st_C_typespec
},
1625 {"static", 0, st_C_typespec
},
1626 {"struct", 0, st_C_struct
},
1627 {"",}, {"",}, {"",},
1628 {"@protocol", 0, st_C_objprot
},
1630 {"auto", 0, st_C_typespec
},
1632 {"char", 0, st_C_typespec
},
1633 {"class", C_PLPL
, st_C_struct
},
1634 {"typedef", 0, st_C_typedef
},
1635 {"extern", 0, st_C_typespec
},
1638 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1640 register int key
= hash (str
, len
);
1642 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1644 register char *s
= wordlist
[key
].name
;
1646 if (*s
== *str
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
1647 return &wordlist
[key
];
1655 C_symtype(str
, len
, c_ext
)
1660 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1662 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1668 * C functions are recognized using a simple finite automaton.
1669 * funcdef is its state variable.
1673 fnone
, /* nothing seen */
1674 ftagseen
, /* function-like tag seen */
1675 fstartlist
, /* just after open parenthesis */
1676 finlist
, /* in parameter list */
1677 flistseen
, /* after parameter list */
1678 fignore
/* before open brace */
1683 * typedefs are recognized using a simple finite automaton.
1684 * typdef is its state variable.
1688 tnone
, /* nothing seen */
1689 ttypedseen
, /* typedef keyword seen */
1690 tinbody
, /* inside typedef body */
1691 tend
, /* just before typedef tag */
1692 tignore
/* junk after typedef tag */
1697 * struct-like structures (enum, struct and union) are recognized
1698 * using another simple finite automaton. `structdef' is its state
1703 snone
, /* nothing seen yet */
1704 skeyseen
, /* struct-like keyword seen */
1705 stagseen
, /* struct-like tag seen */
1706 scolonseen
, /* colon seen after struct-like tag */
1707 sinbody
/* in struct body: recognize member func defs*/
1711 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1712 * struct tag, and structtype is the type of the preceding struct-like
1715 char *structtag
= "<uninited>";
1716 enum sym_type structtype
;
1719 * When objdef is different from onone, objtag is the name of the class.
1721 char *objtag
= "<uninited>";
1724 * Yet another little state machine to deal with preprocessor lines.
1728 dnone
, /* nothing seen */
1729 dsharpseen
, /* '#' seen as first char on line */
1730 ddefineseen
, /* '#' and 'define' seen */
1731 dignorerest
/* ignore rest of line */
1735 * State machine for Objective C protocols and implementations.
1739 onone
, /* nothing seen */
1740 oprotocol
, /* @interface or @protocol seen */
1741 oimplementation
, /* @implementations seen */
1742 otagseen
, /* class name seen */
1743 oparenseen
, /* parenthesis before category seen */
1744 ocatseen
, /* category name seen */
1745 oinbody
, /* in @implementation body */
1746 omethodsign
, /* in @implementation body, after +/- */
1747 omethodtag
, /* after method name */
1748 omethodcolon
, /* after method colon */
1749 omethodparm
, /* after method parameter */
1750 oignore
/* wait for @end */
1754 * Set this to TRUE, and the next token considered is called a function.
1755 * Used only for GNU emacs's function-defining macros.
1757 logical next_token_is_func
;
1760 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1765 * methodlen is the length of the method name stored in token_name.
1771 * checks to see if the current token is at the start of a
1772 * function, or corresponds to a typedef, or is a struct/union/enum
1775 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1776 * C_EXT is which language we are looking at.
1778 * In the future we will need some way to adjust where the end of
1779 * the token is; for instance, implementing the C++ keyword
1780 * `operator' properly will adjust the end of the token to be after
1781 * whatever follows `operator'.
1789 * next_token_is_func IN OUT
1793 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func
)
1794 register char *str
; /* IN: token pointer */
1795 register int len
; /* IN: token length */
1796 register char c
; /* IN: first char after the token */
1797 int c_ext
; /* IN: C extensions mask */
1798 int cblev
; /* IN: curly brace level */
1799 int parlev
; /* IN: parenthesis level */
1800 logical
*is_func
; /* OUT: function found */
1802 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1805 * Advance the definedef state machine.
1810 /* We're not on a preprocessor line. */
1813 if (toktype
== st_C_define
)
1815 definedef
= ddefineseen
;
1819 definedef
= dignorerest
;
1824 * Make a tag for any macro, unless it is a constant
1825 * and constantypedefs is FALSE.
1827 definedef
= dignorerest
;
1828 *is_func
= (c
== '(');
1829 if (!*is_func
&& !constantypedefs
)
1836 error ("internal error: definedef value.", 0);
1845 if (toktype
== st_C_typedef
)
1848 typdef
= ttypedseen
;
1864 /* Do not return here, so the structdef stuff has a chance. */
1878 * This structdef business is currently only invoked when cblev==0.
1879 * It should be recursively invoked whatever the curly brace level,
1880 * and a stack of states kept, to allow for definitions of structs
1883 * This structdef business is NOT invoked when we are ctags and the
1884 * file is plain C. This is because a struct tag may have the same
1885 * name as another tag, and this loses with ctags.
1887 * This if statement deals with the typdef state machine as
1888 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1889 * return FALSE. All the other code here is for the structdef
1896 if (typdef
== ttypedseen
1897 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1899 structdef
= skeyseen
;
1900 structtype
= toktype
;
1904 if (structdef
== skeyseen
)
1906 /* Save the tag for struct/union/class, for functions that may be
1908 if (structtype
== st_C_struct
)
1909 structtag
= savenstr (str
, len
);
1911 structtag
= "<enum>";
1912 structdef
= stagseen
;
1916 /* Avoid entering funcdef stuff if typdef is going on. */
1917 if (typdef
!= tnone
)
1923 /* Detect GNU macros. */
1924 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
1926 next_token_is_func
= TRUE
;
1929 if (next_token_is_func
)
1931 next_token_is_func
= FALSE
;
1938 * Detecting Objective C constructs.
1949 objdef
= oimplementation
;
1953 case oimplementation
:
1954 /* Save the class tag for functions that may be defined inside. */
1955 objtag
= savenstr (str
, len
);
1959 /* Save the class tag for categories. */
1960 objtag
= savenstr (str
, len
);
1973 objdef
= omethodtag
;
1975 GROW_LINEBUFFER (token_name
, methodlen
+1);
1976 strncpy (token_name
.buffer
, str
, len
);
1977 token_name
.buffer
[methodlen
] = '\0';
1983 objdef
= omethodparm
;
1988 objdef
= omethodtag
;
1990 GROW_LINEBUFFER (token_name
, methodlen
+1);
1991 strncat (token_name
.buffer
, str
, len
);
1996 if (toktype
== st_C_objend
)
1998 /* Memory leakage here: the string pointed by objtag is
1999 never released, because many tests would be needed to
2000 avoid breaking on incorrect input code. The amount of
2001 memory leaked here is the sum of the lengths of the
2013 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2014 funcdef
= fnone
; /* should be useless */
2017 if (funcdef
== fnone
)
2030 * This routine finds functions, typedefs, #define's and
2031 * struct/union/enum definitions in C syntax and adds them
2045 #define current_lb_is_new (newndx == curndx)
2046 #define switch_line_buffers() (curndx = 1 - curndx)
2048 #define curlb (lbs[curndx].lb)
2049 #define othlb (lbs[1-curndx].lb)
2050 #define newlb (lbs[newndx].lb)
2051 #define curlinepos (lbs[curndx].linepos)
2052 #define othlinepos (lbs[1-curndx].linepos)
2053 #define newlinepos (lbs[newndx].linepos)
2055 #define CNL_SAVE_DEFINEDEF \
2057 curlinepos = charno; \
2059 charno += readline (&curlb, inf); \
2060 lp = curlb.buffer; \
2067 CNL_SAVE_DEFINEDEF; \
2068 if (savetok.valid) \
2071 savetok.valid = FALSE; \
2073 definedef = dnone; \
2076 /* Ideally this macro should never be called wihen tok.valid is FALSE,
2077 but this would mean that the state machines always guess right. */
2078 #define make_tag(isfun) do \
2080 char *name = NULL; \
2081 if (CTAGS || tok.named) \
2082 name = savestr (token_name.buffer); \
2083 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
2084 tok.valid = FALSE; \
2088 C_entries (c_ext
, inf
)
2089 int c_ext
; /* extension of C */
2090 FILE *inf
; /* input file */
2092 register char c
; /* latest char read; '\0' for end of line */
2093 register char *lp
; /* pointer one beyond the character `c' */
2094 int curndx
, newndx
; /* indices for current and new lb */
2095 TOKEN tok
; /* latest token read */
2096 register int tokoff
; /* offset in line of start of current token */
2097 register int toklen
; /* length of current token */
2098 int cblev
; /* current curly brace level */
2099 int parlev
; /* current parenthesis level */
2100 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
2102 TOKEN savetok
; /* token saved during preprocessor handling */
2105 curndx
= newndx
= 0;
2111 funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
2112 definedef
= dnone
; objdef
= onone
;
2113 next_token_is_func
= yacc_rules
= FALSE
;
2114 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2115 tok
.valid
= savetok
.valid
= FALSE
;
2118 cplpl
= c_ext
& C_PLPL
;
2125 /* If we're at the end of the line, the next character is a
2126 '\0'; don't skip it, because it's the thing that tells us
2127 to read the next line. */
2148 /* Newlines inside comments do not end macro definitions in
2163 /* Newlines inside strings do not end macro definitions
2164 in traditional cpp, even though compilers don't
2165 usually accept them. */
2176 /* Hmmm, something went wrong. */
2190 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2195 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2205 else if (/* cplpl && */ *lp
== '/')
2213 if ((c_ext
& YACC
) && *lp
== '%')
2215 /* entering or exiting rules section in yacc file */
2217 definedef
= dnone
; funcdef
= fnone
;
2218 typdef
= tnone
; structdef
= snone
;
2219 next_token_is_func
= FALSE
;
2220 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2222 yacc_rules
= !yacc_rules
;
2228 if (definedef
== dnone
)
2231 logical cpptoken
= TRUE
;
2233 /* Look back on this line. If all blanks, or nonblanks
2234 followed by an end of comment, this is a preprocessor
2236 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2239 if (*cp
== '*' && *(cp
+1) == '/')
2248 definedef
= dsharpseen
;
2249 } /* if (definedef == dnone) */
2255 /* Consider token only if some complicated conditions are satisfied. */
2256 if ((definedef
!= dnone
2257 || (cblev
== 0 && structdef
!= scolonseen
)
2258 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2259 && typdef
!= tignore
2260 && definedef
!= dignorerest
2261 && funcdef
!= finlist
)
2267 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken(*(lp
+ 1)))
2270 * This handles :: in the middle, but not at the
2271 * beginning of an identifier.
2278 logical is_func
= FALSE
;
2281 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2282 c_ext
, cblev
, parlev
, &is_func
))
2284 if (structdef
== sinbody
2285 && definedef
== dnone
2287 /* function defined in C++ class body */
2289 GROW_LINEBUFFER (token_name
,
2290 strlen(structtag
)+2+toklen
+1);
2291 strcpy (token_name
.buffer
, structtag
);
2292 strcat (token_name
.buffer
, "::");
2293 strncat (token_name
.buffer
,
2294 newlb
.buffer
+tokoff
, toklen
);
2297 else if (objdef
== ocatseen
)
2298 /* Objective C category */
2300 GROW_LINEBUFFER (token_name
,
2301 strlen(objtag
)+2+toklen
+1);
2302 strcpy (token_name
.buffer
, objtag
);
2303 strcat (token_name
.buffer
, "(");
2304 strncat (token_name
.buffer
,
2305 newlb
.buffer
+tokoff
, toklen
);
2306 strcat (token_name
.buffer
, ")");
2309 else if (objdef
== omethodtag
2310 || objdef
== omethodparm
)
2311 /* Objective C method */
2317 GROW_LINEBUFFER (token_name
, toklen
+1);
2318 strncpy (token_name
.buffer
,
2319 newlb
.buffer
+tokoff
, toklen
);
2320 token_name
.buffer
[toklen
] = '\0';
2321 if (structdef
== stagseen
2324 && definedef
== dignorerest
)) /* macro */
2329 tok
.lineno
= lineno
;
2330 tok
.linelen
= tokoff
+ toklen
+ 1;
2331 tok
.buffer
= newlb
.buffer
;
2332 tok
.linepos
= newlinepos
;
2335 if (definedef
== dnone
2336 && (funcdef
== ftagseen
2337 || structdef
== stagseen
2339 || objdef
!= onone
))
2341 if (current_lb_is_new
)
2342 switch_line_buffers ();
2349 } /* if (endtoken (c)) */
2350 else if (intoken (c
))
2355 } /* if (midtoken) */
2356 else if (begtoken (c
))
2374 if (structdef
== stagseen
)
2380 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2382 tokoff
= lp
- 1 - newlb
.buffer
;
2387 } /* if (begtoken) */
2388 } /* if must look at token */
2391 /* Detect end of line, colon, comma, semicolon and various braces
2392 after having handled a token.*/
2396 if (definedef
!= dnone
)
2406 objdef
= omethodcolon
;
2408 GROW_LINEBUFFER (token_name
, methodlen
+1);
2409 strcat (token_name
.buffer
, ":");
2412 if (structdef
== stagseen
)
2413 structdef
= scolonseen
;
2430 if (definedef
!= dnone
)
2441 if (funcdef
!= fignore
)
2444 /* The following instruction invalidates the token.
2445 Probably the token should be invalidated in all
2446 other cases where some state machine is reset. */
2449 if (structdef
== stagseen
)
2453 if (definedef
!= dnone
)
2463 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2465 if (structdef
== stagseen
)
2469 if (definedef
!= dnone
)
2471 if (cblev
== 0 && typdef
== tend
)
2477 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2479 if (structdef
== stagseen
)
2483 if (definedef
!= dnone
)
2485 if (objdef
== otagseen
&& parlev
== 0)
2486 objdef
= oparenseen
;
2494 /* Make sure that the next char is not a '*'.
2495 This handles constructs like:
2496 typedef void OperatorFun (int fun); */
2503 } /* switch (typdef) */
2506 funcdef
= fstartlist
;
2515 if (definedef
!= dnone
)
2517 if (objdef
== ocatseen
&& parlev
== 1)
2528 funcdef
= flistseen
;
2531 if (cblev
== 0 && typdef
== tend
)
2537 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2541 if (definedef
!= dnone
)
2543 if (typdef
== ttypedseen
)
2547 case skeyseen
: /* unnamed struct */
2548 structtag
= "_anonymous_";
2549 structdef
= sinbody
;
2552 case scolonseen
: /* named struct */
2553 structdef
= sinbody
;
2578 /* Neutralize `extern "C" {' grot and look inside structs. */
2579 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2586 if (definedef
!= dnone
)
2588 if (funcdef
== fstartlist
)
2589 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2592 if (definedef
!= dnone
)
2594 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2596 cblev
= 0; /* reset curly brace level if first column */
2597 parlev
= 0; /* also reset paren level, just in case... */
2603 if (typdef
== tinbody
)
2605 /* Memory leakage here: the string pointed by structtag is
2606 never released, because I fear to miss something and
2607 break things while freeing the area. The amount of
2608 memory leaked here is the sum of the lengths of the
2610 if (structdef == sinbody)
2611 free (structtag); */
2614 structtag
= "<error>";
2619 if (objdef
== oinbody
&& cblev
== 0)
2621 objdef
= omethodsign
;
2625 case '=': case '#': case '~': case '&': case '%': case '/':
2626 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2627 if (definedef
!= dnone
)
2629 /* These surely cannot follow a function tag. */
2630 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2634 if (objdef
== otagseen
)
2639 /* If a macro spans multiple lines don't reset its state. */
2647 } /* while not eof */
2651 * Process either a C++ file or a C file depending on the setting
2655 default_C_entries (inf
)
2658 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2661 /* Always do plain ANSI C. */
2663 plain_C_entries (inf
)
2669 /* Always do C++. */
2671 Cplusplus_entries (inf
)
2674 C_entries (C_PLPL
, inf
);
2682 C_entries (C_STAR
, inf
);
2685 /* Always do Yacc. */
2690 C_entries (YACC
, inf
);
2693 /* Fortran parsing */
2701 register int len
= 0;
2703 while (*cp
&& lowcase(*cp
) == lowcase(dbp
[len
]))
2705 if (*cp
== '\0' && !intoken(dbp
[len
]))
2716 while (isspace (*dbp
))
2721 while (isspace (*dbp
))
2723 if (strneq (dbp
, "(*)", 3))
2728 if (!isdigit (*dbp
))
2730 --dbp
; /* force failure */
2735 while (isdigit (*dbp
));
2744 while (isspace (*dbp
))
2749 linecharno
= charno
;
2750 charno
+= readline (&lb
, inf
);
2755 while (isspace (*dbp
))
2764 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2767 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
2768 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2772 Fortran_functions (inf
)
2781 linecharno
= charno
;
2782 charno
+= readline (&lb
, inf
);
2785 dbp
++; /* Ratfor escape to fortran */
2786 while (isspace (*dbp
))
2790 switch (lowcase (*dbp
))
2793 if (tail ("integer"))
2801 if (tail ("logical"))
2805 if (tail ("complex") || tail ("character"))
2809 if (tail ("double"))
2811 while (isspace (*dbp
))
2815 if (tail ("precision"))
2821 while (isspace (*dbp
))
2825 switch (lowcase (*dbp
))
2828 if (tail ("function"))
2832 if (tail ("subroutine"))
2840 if (tail ("program"))
2845 if (tail ("procedure"))
2853 * Bob Weiner, Motorola Inc., 4/3/94
2854 * Unix and microcontroller assembly tag handling
2855 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2869 linecharno
= charno
;
2870 charno
+= readline (&lb
, inf
);
2873 /* If first char is alphabetic or one of [_.$], test for colon
2874 following identifier. */
2875 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2877 /* Read past label. */
2879 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2881 if (*cp
== ':' || isspace (*cp
))
2883 /* Found end of label, so copy it and add it to the table. */
2884 pfnote ((CTAGS
) ? savenstr(lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2885 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2892 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2893 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2896 Perl_functions (inf
)
2907 linecharno
= charno
;
2908 charno
+= readline (&lb
, inf
);
2911 if (*cp
++ == 's' && *cp
++ == 'u' && *cp
++ == 'b' && isspace(*cp
++))
2913 while (*cp
&& isspace(*cp
))
2915 while (*cp
&& ! isspace(*cp
) && *cp
!= '{')
2917 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2918 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2923 /* Added by Mosur Mohan, 4/22/88 */
2924 /* Pascal parsing */
2926 #define GET_NEW_LINE \
2928 linecharno = charno; lineno++; \
2929 charno += 1 + readline (&lb, inf); \
2934 * Locates tags for procedures & functions. Doesn't do any type- or
2935 * var-definitions. It does look for the keyword "extern" or
2936 * "forward" immediately following the procedure statement; if found,
2937 * the tag is skipped.
2940 Pascal_functions (inf
)
2943 struct linebuffer tline
; /* mostly copied from C_entries */
2945 int save_lineno
, save_len
;
2946 char c
, *cp
, *namebuf
;
2948 logical
/* each of these flags is TRUE iff: */
2949 incomment
, /* point is inside a comment */
2950 inquote
, /* point is inside '..' string */
2951 get_tagname
, /* point is after PROCEDURE/FUNCTION
2952 keyword, so next item = potential tag */
2953 found_tag
, /* point is after a potential tag */
2954 inparms
, /* point is within parameter-list */
2955 verify_tag
; /* point has passed the parm-list, so the
2956 next token will determine whether this
2957 is a FORWARD/EXTERN to be ignored, or
2958 whether it is a real tag */
2965 initbuffer (&tline
);
2967 incomment
= inquote
= FALSE
;
2968 found_tag
= FALSE
; /* have a proc name; check if extern */
2969 get_tagname
= FALSE
; /* have found "procedure" keyword */
2970 inparms
= FALSE
; /* found '(' after "proc" */
2971 verify_tag
= FALSE
; /* check if "extern" is ahead */
2973 /* long main loop to get next char */
2977 if (c
== '\0') /* if end of line */
2982 if (!((found_tag
&& verify_tag
) ||
2984 c
= *dbp
++; /* only if don't need *dbp pointing
2985 to the beginning of the name of
2986 the procedure or function */
2990 if (c
== '}') /* within { } comments */
2992 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
3009 inquote
= TRUE
; /* found first quote */
3011 case '{': /* found open { comment */
3015 if (*dbp
== '*') /* found open (* comment */
3020 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
3023 case ')': /* end of parms list */
3028 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
3035 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
3037 /* check if this is an "extern" declaration */
3040 if (lowcase (*dbp
== 'e'))
3042 if (tail ("extern")) /* superfluous, really! */
3048 else if (lowcase (*dbp
) == 'f')
3050 if (tail ("forward")) /* check for forward reference */
3056 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
3060 pfnote (namebuf
, TRUE
,
3061 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
3065 if (get_tagname
) /* grab name of proc or fn */
3070 /* save all values for later tagging */
3071 GROW_LINEBUFFER (tline
, strlen (lb
.buffer
) + 1);
3072 strcpy (tline
.buffer
, lb
.buffer
);
3073 save_lineno
= lineno
;
3074 save_lcno
= linecharno
;
3076 /* grab block name */
3077 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
3079 namebuf
= (CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
;
3080 dbp
= cp
; /* set dbp to e-o-token */
3081 save_len
= dbp
- lb
.buffer
+ 1;
3082 get_tagname
= FALSE
;
3086 /* and proceed to check for "extern" */
3088 else if (!incomment
&& !inquote
&& !found_tag
)
3090 /* check for proc/fn keywords */
3091 switch (lowcase (c
))
3094 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3098 if (tail ("unction"))
3103 } /* while not eof */
3105 free (tline
.buffer
);
3109 * lisp tag functions
3110 * look for (def or (DEF, quote or QUOTE
3114 register char *strp
;
3116 return ((strp
[1] == 'd' || strp
[1] == 'D')
3117 && (strp
[2] == 'e' || strp
[2] == 'E')
3118 && (strp
[3] == 'f' || strp
[3] == 'F'));
3123 register char *strp
;
3125 return ((*(++strp
) == 'q' || *strp
== 'Q')
3126 && (*(++strp
) == 'u' || *strp
== 'U')
3127 && (*(++strp
) == 'o' || *strp
== 'O')
3128 && (*(++strp
) == 't' || *strp
== 'T')
3129 && (*(++strp
) == 'e' || *strp
== 'E')
3130 && isspace(*(++strp
)));
3138 if (*dbp
== '\'') /* Skip prefix quote */
3140 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
3143 while (isspace(*dbp
))
3146 for (cp
= dbp
/*+1*/;
3147 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
3153 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3154 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3158 Lisp_functions (inf
)
3167 linecharno
= charno
;
3168 charno
+= readline (&lb
, inf
);
3174 while (!isspace (*dbp
))
3176 while (isspace (*dbp
))
3182 /* Check for (foo::defmumble name-defined ... */
3185 while (*dbp
&& !isspace (*dbp
)
3186 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
3191 while (*dbp
== ':');
3193 if (L_isdef (dbp
- 1))
3195 while (!isspace (*dbp
))
3197 while (isspace (*dbp
))
3208 * Scheme tag functions
3209 * look for (def... xyzzy
3210 * look for (def... (xyzzy
3211 * look for (def ... ((...(xyzzy ....
3212 * look for (set! xyzzy
3218 Scheme_functions (inf
)
3227 linecharno
= charno
;
3228 charno
+= readline (&lb
, inf
);
3230 if (dbp
[0] == '(' &&
3231 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
3232 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3233 (dbp
[3] == 'F' || dbp
[3] == 'f'))
3235 while (!isspace (*dbp
))
3237 /* Skip over open parens and white space */
3238 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
3242 if (dbp
[0] == '(' &&
3243 (dbp
[1] == 'S' || dbp
[1] == 's') &&
3244 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3245 (dbp
[3] == 'T' || dbp
[3] == 't') &&
3246 (dbp
[4] == '!' || dbp
[4] == '!') &&
3249 while (!isspace (*dbp
))
3251 /* Skip over white space */
3252 while (isspace (*dbp
))
3266 /* Go till you get to white space or a syntactic break */
3268 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
3271 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3272 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3275 /* Find tags in TeX and LaTeX input files. */
3277 /* TEX_toktab is a table of TeX control sequences that define tags.
3278 Each TEX_tabent records one such control sequence.
3279 CONVERT THIS TO USE THE Stab TYPE!! */
3286 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
3288 /* Default set of control sequences to put into TEX_toktab.
3289 The value of environment var TEXTAGS is prepended to this. */
3291 char *TEX_defenv
= "\
3292 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3293 :part:appendix:entry:index";
3296 struct TEX_tabent
*TEX_decode_env ();
3298 #if TeX_named_tokens
3302 char TEX_esc
= '\\';
3303 char TEX_opgrp
= '{';
3304 char TEX_clgrp
= '}';
3307 * TeX/LaTeX scanning loop.
3318 /* Select either \ or ! as escape character. */
3321 /* Initialize token table once from environment. */
3323 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
3326 { /* Scan each line in file */
3328 linecharno
= charno
;
3329 charno
+= readline (&lb
, inf
);
3332 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
3338 linecharno
+= dbp
- lasthit
;
3340 i
= TEX_Token (lasthit
);
3344 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3345 #if TeX_named_tokens
3346 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
3348 break; /* We only save a line once */
3354 #define TEX_LESC '\\'
3355 #define TEX_SESC '!'
3358 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3359 chars accordingly. */
3366 while ((c
= getc (inf
)) != EOF
)
3368 /* Skip to next line if we hit the TeX comment char. */
3372 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3391 /* Read environment and prepend it to the default string.
3392 Build token table. */
3394 TEX_decode_env (evarname
, defenv
)
3398 register char *env
, *p
;
3400 struct TEX_tabent
*tab
;
3403 /* Append default string to environment. */
3404 env
= getenv (evarname
);
3408 env
= concat (env
, defenv
, "");
3410 /* Allocate a token table */
3411 for (size
= 1, p
= env
; p
;)
3412 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3414 /* Add 1 to leave room for null terminator. */
3415 tab
= xnew (size
+ 1, struct TEX_tabent
);
3417 /* Unpack environment string into token table. Be careful about */
3418 /* zero-length strings (leading ':', "::" and trailing ':') */
3421 p
= etags_strchr (env
, ':');
3422 if (!p
) /* End of environment string. */
3423 p
= env
+ strlen (env
);
3425 { /* Only non-zero strings. */
3426 tab
[i
].name
= savenstr (env
, p
- env
);
3427 tab
[i
].len
= strlen (tab
[i
].name
);
3434 tab
[i
].name
= NULL
; /* Mark end of table. */
3442 #if TeX_named_tokens
3443 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3444 The name being defined actually starts at (NAME + LEN + 1).
3445 But we seem to include the TeX command in the tag name. */
3447 TEX_getit (name
, len
)
3451 char *p
= name
+ len
;
3456 /* Let tag name extend to next group close (or end of line) */
3457 while (*p
&& *p
!= TEX_clgrp
)
3459 pfnote (savenstr (name
, p
-name
), TRUE
,
3460 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3464 /* If the text at CP matches one of the tag-defining TeX command names,
3465 return the pointer to the first occurrence of that command in TEX_toktab.
3466 Otherwise return -1.
3467 Keep the capital `T' in `Token' for dumb truncating compilers
3468 (this distinguishes it from `TEX_toktab' */
3475 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3476 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3482 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
3484 * Assumes that the predicate starts at column 0.
3485 * Only the first clause of a predicate is added.
3488 Prolog_functions (inf
)
3492 void prolog_skip_comment ();
3509 linecharno
+= charno
;
3510 charno
= readline (&lb
, inf
);
3512 if (dbp
[0] == '\0') /* Empty line */
3514 else if (isspace (dbp
[0])) /* Not a predicate */
3516 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3517 prolog_skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3518 else if (len
= prolog_pred (dbp
, last
))
3520 /* Predicate. Store the function name so that we only
3521 * generates a tag for the first clause. */
3523 last
= xnew(len
+ 1, char);
3524 else if (len
+ 1 > allocated
)
3525 last
= (char *) xrealloc(last
, len
+ 1);
3526 allocated
= len
+ 1;
3527 strncpy (last
, dbp
, len
);
3535 prolog_skip_comment (plb
, inf
)
3536 struct linebuffer
*plb
;
3543 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3544 if (cp
[0] == '*' && cp
[1] == '/')
3547 linecharno
+= readline (plb
, inf
);
3553 * A predicate definition is added if it matches:
3554 * <beginning of line><Prolog Atom><whitespace>(
3556 * It is added to the tags database if it doesn't match the
3557 * name of the previous clause header.
3559 * Return the size of the name of the predicate, or 0 if no header
3563 prolog_pred (s
, last
)
3565 char *last
; /* Name of last clause. */
3573 pos
= prolog_atom(s
, 0);
3578 pos
+= prolog_white(s
, pos
);
3580 if ((s
[pos
] == '(') || (s
[pos
] == '.'))
3585 /* Save only the first clause. */
3586 if ((last
== NULL
) ||
3587 (len
!= strlen(last
)) ||
3588 (strncmp(s
, last
, len
) != 0))
3590 pfnote ((CTAGS
) ? savenstr (s
, len
) : NULL
, TRUE
,
3591 s
, pos
, lineno
, linecharno
);
3599 * Consume a Prolog atom.
3600 * Return the number of bytes consumed, or -1 if there was an error.
3602 * A prolog atom, in this context, could be one of:
3603 * - An alphanumeric sequence, starting with a lower case letter.
3604 * - A quoted arbitrary string. Single quotes can escape themselves.
3605 * Backslash quotes everything.
3608 prolog_atom (s
, pos
)
3616 if (islower(s
[pos
]) || (s
[pos
] == '_'))
3618 /* The atom is unquoted. */
3620 while (isalnum(s
[pos
]) || (s
[pos
] == '_'))
3624 return pos
- origpos
;
3626 else if (s
[pos
] == '\'')
3637 pos
++; /* A double quote */
3639 else if (s
[pos
] == '\0')
3640 /* Multiline quoted atoms are ignored. */
3642 else if (s
[pos
] == '\\')
3644 if (s
[pos
+1] == '\0')
3651 return pos
- origpos
;
3657 /* Consume whitespace. Return the number of bytes eaten. */
3659 prolog_white (s
, pos
)
3667 while (isspace(s
[pos
]))
3670 return pos
- origpos
;
3674 * Support for Erlang -- Anders Lindgren, Feb 1996.
3676 * Generates tags for functions, defines, and records.
3678 * Assumes that Erlang functions start at column 0.
3681 Erlang_functions (inf
)
3685 void erlang_attribute ();
3702 linecharno
+= charno
;
3703 charno
= readline (&lb
, inf
);
3705 if (dbp
[0] == '\0') /* Empty line */
3707 else if (isspace (dbp
[0])) /* Not function nor attribute */
3709 else if (dbp
[0] == '%') /* comment */
3711 else if (dbp
[0] == '"') /* Sometimes, strings start in column one */
3713 else if (dbp
[0] == '-') /* attribute, e.g. "-define" */
3715 erlang_attribute(dbp
);
3718 else if (len
= erlang_func (dbp
, last
))
3721 * Function. Store the function name so that we only
3722 * generates a tag for the first clause.
3725 last
= xnew(len
+ 1, char);
3726 else if (len
+ 1 > allocated
)
3727 last
= (char *) xrealloc(last
, len
+ 1);
3728 allocated
= len
+ 1;
3729 strncpy (last
, dbp
, len
);
3737 * A function definition is added if it matches:
3738 * <beginning of line><Erlang Atom><whitespace>(
3740 * It is added to the tags database if it doesn't match the
3741 * name of the previous clause header.
3743 * Return the size of the name of the function, or 0 if no function
3747 erlang_func (s
, last
)
3749 char *last
; /* Name of last clause. */
3752 int erlang_white ();
3757 pos
= erlang_atom(s
, 0);
3762 pos
+= erlang_white(s
, pos
);
3764 if (s
[pos
++] == '(')
3766 /* Save only the first clause. */
3767 if ((last
== NULL
) ||
3768 (len
!= strlen(last
)) ||
3769 (strncmp(s
, last
, len
) != 0))
3771 pfnote ((CTAGS
) ? savenstr (s
, len
) : NULL
, TRUE
,
3772 s
, pos
, lineno
, linecharno
);
3781 * Handle attributes. Currently, tags are generated for defines
3784 * They are on the form:
3785 * -define(foo, bar).
3786 * -define(Foo(M, N), M+N).
3787 * -record(graph, {vtab = notable, cyclic = true}).
3790 erlang_attribute (s
)
3794 int erlang_white ();
3799 if ((strncmp(s
, "-define", 7) == 0) ||
3800 (strncmp(s
, "-record", 7) == 0))
3803 pos
+= erlang_white(s
, pos
);
3805 if (s
[pos
++] == '(')
3807 pos
+= erlang_white(s
, pos
);
3809 if (len
= erlang_atom(s
, pos
))
3811 pfnote ((CTAGS
) ? savenstr (& s
[pos
], len
) : NULL
, TRUE
,
3812 s
, pos
+ len
, lineno
, linecharno
);
3821 * Consume an Erlang atom (or variable).
3822 * Return the number of bytes consumed, or -1 if there was an error.
3825 erlang_atom (s
, pos
)
3833 if (isalpha (s
[pos
]) || s
[pos
] == '_')
3835 /* The atom is unquoted. */
3837 while (isalnum (s
[pos
]) || s
[pos
] == '_')
3839 return pos
- origpos
;
3841 else if (s
[pos
] == '\'')
3852 else if (s
[pos
] == '\0')
3853 /* Multiline quoted atoms are ignored. */
3855 else if (s
[pos
] == '\\')
3857 if (s
[pos
+1] == '\0')
3864 return pos
- origpos
;
3870 /* Consume whitespace. Return the number of bytes eaten */
3872 erlang_white (s
, pos
)
3880 while (isspace (s
[pos
]))
3883 return pos
- origpos
;
3886 #ifdef ETAGS_REGEXPS
3887 /* Take a string like "/blah/" and turn it into "blah", making sure
3888 that the first and last characters are the same, and handling
3889 quoted separator characters. Actually, stops on the occurrence of
3890 an unquoted separator. Also turns "\t" into a Tab character.
3891 Returns pointer to terminating separator. Works in place. Null
3892 terminates name string. */
3894 scan_separators (name
)
3898 char *copyto
= name
;
3899 logical quoted
= FALSE
;
3901 for (++name
; *name
!= '\0'; ++name
)
3907 else if (*name
== sep
)
3911 /* Something else is quoted, so preserve the quote. */
3917 else if (*name
== '\\')
3919 else if (*name
== sep
)
3925 /* Terminate copied string. */
3930 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3931 expression, into a real regular expression by compiling it. */
3933 add_regex (regexp_pattern
)
3934 char *regexp_pattern
;
3938 struct re_pattern_buffer
*patbuf
;
3940 if (regexp_pattern
== NULL
)
3942 /* Remove existing regexps. */
3948 if (regexp_pattern
[0] == '\0')
3950 error ("missing regexp", 0);
3953 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3955 error ("%s: unterminated regexp", regexp_pattern
);
3958 name
= scan_separators (regexp_pattern
);
3959 if (regexp_pattern
[0] == '\0')
3961 error ("null regexp", 0);
3964 (void) scan_separators (name
);
3966 patbuf
= xnew (1, struct re_pattern_buffer
);
3967 patbuf
->translate
= NULL
;
3968 patbuf
->fastmap
= NULL
;
3969 patbuf
->buffer
= NULL
;
3970 patbuf
->allocated
= 0;
3972 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3975 error ("%s while compiling pattern", err
);
3980 if (num_patterns
== 1)
3981 patterns
= xnew (1, struct pattern
);
3983 patterns
= ((struct pattern
*)
3985 (num_patterns
* sizeof (struct pattern
))));
3986 patterns
[num_patterns
- 1].pattern
= patbuf
;
3987 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3988 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
3992 * Do the substitutions indicated by the regular expression and
3996 substitute (in
, out
, regs
)
3998 struct re_registers
*regs
;
4000 char *result
= NULL
, *t
;
4003 /* Pass 1: figure out how much size to allocate. */
4004 for (t
= out
; *t
; ++t
)
4011 fprintf (stderr
, "%s: pattern substitution ends prematurely\n",
4018 size
+= regs
->end
[dig
] - regs
->start
[dig
];
4023 /* Allocate space and do the substitutions. */
4024 result
= xnew (size
+ 1, char);
4033 /* Using "dig2" satisfies my debugger. Bleah. */
4034 int dig2
= *out
- '0';
4035 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
4036 regs
->end
[dig2
] - regs
->start
[dig2
]);
4037 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
4040 result
[size
++] = *out
;
4043 result
[size
++] = *out
;
4045 result
[size
] = '\0';
4050 #endif /* ETAGS_REGEXPS */
4051 /* Initialize a linebuffer for use */
4053 initbuffer (linebuffer
)
4054 struct linebuffer
*linebuffer
;
4056 linebuffer
->size
= 200;
4057 linebuffer
->buffer
= xnew (200, char);
4061 * Read a line of text from `stream' into `linebuffer'.
4062 * Return the number of characters read from `stream',
4063 * which is the length of the line including the newline, if any.
4066 readline_internal (linebuffer
, stream
)
4067 struct linebuffer
*linebuffer
;
4068 register FILE *stream
;
4070 char *buffer
= linebuffer
->buffer
;
4071 register char *p
= linebuffer
->buffer
;
4072 register char *pend
;
4075 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
4079 register int c
= getc (stream
);
4082 linebuffer
->size
*= 2;
4083 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
4084 p
+= buffer
- linebuffer
->buffer
;
4085 pend
= buffer
+ linebuffer
->size
;
4086 linebuffer
->buffer
= buffer
;
4096 if (p
> buffer
&& p
[-1] == '\r')
4111 return p
- buffer
+ chars_deleted
;
4115 * Like readline_internal, above, but try to match the input
4116 * line against any existing regular expressions.
4119 readline (linebuffer
, stream
)
4120 struct linebuffer
*linebuffer
;
4123 /* Read new line. */
4124 long result
= readline_internal (linebuffer
, stream
);
4125 #ifdef ETAGS_REGEXPS
4128 /* Match against all listed patterns. */
4129 for (i
= 0; i
< num_patterns
; ++i
)
4131 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
4132 (int)result
, 0, &patterns
[i
].regs
);
4137 if (!patterns
[i
].error_signaled
)
4139 error ("error while matching pattern %d", i
);
4140 patterns
[i
].error_signaled
= TRUE
;
4147 /* Match occurred. Construct a tag. */
4148 if (patterns
[i
].name_pattern
[0] != '\0')
4150 /* Make a named tag. */
4151 char *name
= substitute (linebuffer
->buffer
,
4152 patterns
[i
].name_pattern
,
4156 linebuffer
->buffer
, match
, lineno
, linecharno
);
4160 /* Make an unnamed tag. */
4162 linebuffer
->buffer
, match
, lineno
, linecharno
);
4167 #endif /* ETAGS_REGEXPS */
4173 * Read a file, but do no processing. This is used to do regexp
4174 * matching on files that have no language defined.
4177 just_read_file (inf
)
4186 linecharno
= charno
;
4187 charno
+= readline (&lb
, inf
) + 1;
4193 * Return a pointer to a space of size strlen(cp)+1 allocated
4194 * with xnew where the string CP has been copied.
4200 return savenstr (cp
, strlen (cp
));
4204 * Return a pointer to a space of size LEN+1 allocated with xnew where
4205 * the string CP has been copied for at most the first LEN characters.
4214 dp
= xnew (len
+ 1, char);
4215 strncpy (dp
, cp
, len
);
4221 * Return the ptr in sp at which the character c last
4222 * appears; NULL if not found
4224 * Identical to System V strrchr, included for portability.
4227 etags_strrchr (sp
, c
)
4228 register char *sp
, c
;
4243 * Return the ptr in sp at which the character c first
4244 * appears; NULL if not found
4246 * Identical to System V strchr, included for portability.
4249 etags_strchr (sp
, c
)
4250 register char *sp
, c
;
4260 /* Print error message and exit. */
4278 suggest_asking_for_help ()
4280 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
4285 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
4290 fprintf (stderr
, "%s: ", progname
);
4291 fprintf (stderr
, s1
, s2
);
4292 fprintf (stderr
, "\n");
4295 /* Return a newly-allocated string whose contents
4296 concatenate those of s1, s2, s3. */
4301 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
4302 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
4304 strcpy (result
, s1
);
4305 strcpy (result
+ len1
, s2
);
4306 strcpy (result
+ len1
+ len2
, s3
);
4307 result
[len1
+ len2
+ len3
] = '\0';
4312 /* Does the same work as the system V getcwd, but does not need to
4313 guess the buffer size in advance. */
4318 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
4327 *p
++ = lowcase (*p
);
4330 return strdup (path
);
4331 #else /* not DOS_NT */
4334 char *path
= xnew (bufsize
, char);
4336 while (getcwd (path
, bufsize
) == NULL
)
4338 if (errno
!= ERANGE
)
4341 path
= xnew (bufsize
, char);
4345 #else /* not DOS_NT and not HAVE_GETCWD */
4346 struct linebuffer path
;
4350 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
4351 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
4356 #endif /* not HAVE_GETCWD */
4357 #endif /* not DOS_NT */
4360 /* Return a newly allocated string containing the filename
4361 of FILE relative to the absolute directory DIR (which
4362 should end with a slash). */
4364 relative_filename (file
, dir
)
4367 char *fp
, *dp
, *abs
, *res
;
4369 /* Find the common root of file and dir. */
4370 abs
= absolute_filename (file
, cwd
);
4373 while (*fp
++ == *dp
++)
4382 /* Build a sequence of "../" strings for the resulting relative filename. */
4383 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
4385 dp
= etags_strchr (dp
+ 1, '/'))
4387 res
= concat (res
, "../", "");
4390 /* Add the filename relative to the common root of file and dir. */
4391 res
= concat (res
, fp
+ 1, "");
4397 /* Return a newly allocated string containing the
4398 absolute filename of FILE given CWD (which should
4399 end with a slash). */
4401 absolute_filename (file
, cwd
)
4404 char *slashp
, *cp
, *res
;
4406 if (absolutefn (file
))
4407 res
= concat (file
, "", "");
4409 /* We don't support non-absolute filenames with a drive
4410 letter, like `d:NAME' (it's too much hassle). */
4411 else if (file
[1] == ':')
4412 fatal ("%s: relative filenames with drive letters not supported", file
);
4415 res
= concat (cwd
, file
, "");
4417 /* Delete the "/dirname/.." and "/." substrings. */
4418 slashp
= etags_strchr (res
, '/');
4419 while (slashp
!= NULL
&& slashp
[0] != '\0')
4421 if (slashp
[1] == '.')
4423 if (slashp
[2] == '.'
4424 && (slashp
[3] == '/' || slashp
[3] == '\0'))
4429 while (cp
>= res
&& !absolutefn (cp
));
4432 strcpy (cp
, slashp
+ 3);
4435 /* Under MSDOS and NT we get `d:/NAME' as absolute
4436 filename, so the luser could say `d:/../NAME'.
4437 We silently treat this as `d:/NAME'. */
4438 else if (cp
[1] == ':')
4439 strcpy (cp
+ 3, slashp
+ 4);
4441 else /* else (cp == res) */
4443 if (slashp
[3] != '\0')
4444 strcpy (cp
, slashp
+ 4);
4451 else if (slashp
[2] == '/' || slashp
[2] == '\0')
4453 strcpy (slashp
, slashp
+ 2);
4458 slashp
= etags_strchr (slashp
+ 1, '/');
4464 /* Return a newly allocated string containing the absolute
4465 filename of dir where FILE resides given CWD (which should
4466 end with a slash). */
4468 absolute_dirname (file
, cwd
)
4484 slashp
= etags_strrchr (file
, '/');
4489 res
= absolute_filename (file
, cwd
);
4495 /* Like malloc but get fatal error if memory is exhausted. */
4500 long *result
= (long *) malloc (size
);
4502 fatal ("virtual memory exhausted", 0);
4507 xrealloc (ptr
, size
)
4511 long *result
= (long *) realloc (ptr
, size
);
4513 fatal ("virtual memory exhausted");