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.66";
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 */
219 long linecharno
; /* charno of start of line */
221 char *curfile
; /* current input file name */
222 char *tagfile
; /* output file */
223 char *progname
; /* name this program was invoked with */
224 char *cwd
; /* current working directory */
225 char *tagfiledir
; /* directory of tagfile */
227 FILE *tagf
; /* ioptr for tags file */
228 NODE
*head
; /* the head of the binary tree of tags */
231 * A `struct linebuffer' is a structure which holds a line of text.
232 * `readline' reads a line from a stream into a linebuffer and works
233 * regardless of the length of the line.
235 #define GROW_LINEBUFFER(buf,toksize) \
236 while (buf.size < toksize) \
237 buf.buffer = (char *) xrealloc (buf.buffer, buf.size *= 2)
244 struct linebuffer lb
; /* the current line */
245 struct linebuffer token_name
; /* used by C_entries as a temporary area */
249 struct linebuffer lb
; /* used by C_entries instead of lb */
252 /* boolean "functions" (see init) */
253 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
256 *white
= " \f\t\n\013",
257 /* token ending chars */
258 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
259 /* token starting chars */
260 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
261 /* valid in-token chars */
262 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
264 logical append_to_tagfile
; /* -a: append to tags */
265 /* The following three default to TRUE for etags, but to FALSE for ctags. */
266 logical typedefs
; /* -t: create tags for typedefs */
267 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
268 /* 0 struct/enum/union decls, and C++ */
269 /* member functions. */
270 logical constantypedefs
; /* -d: create tags for C #define and enum */
271 /* constants. Enum consts not implemented. */
272 /* -D: opposite of -d. Default under ctags. */
273 logical update
; /* -u: update tags */
274 logical vgrind_style
; /* -v: create vgrind style index output */
275 logical no_warnings
; /* -w: suppress warnings */
276 logical cxref_style
; /* -x: create cxref style output */
277 logical cplusplus
; /* .[hc] means C++, not C */
278 logical noindentypedefs
; /* -I: ignore indentation in C */
280 struct option longopts
[] =
282 { "append", no_argument
, NULL
, 'a' },
283 { "backward-search", no_argument
, NULL
, 'B' },
284 { "c++", no_argument
, NULL
, 'C' },
285 { "cxref", no_argument
, NULL
, 'x' },
286 { "defines", no_argument
, NULL
, 'd' },
287 { "help", no_argument
, NULL
, 'h' },
288 { "help", no_argument
, NULL
, 'H' },
289 { "ignore-indentation", no_argument
, NULL
, 'I' },
290 { "include", required_argument
, NULL
, 'i' },
291 { "language", required_argument
, NULL
, 'l' },
292 { "no-defines", no_argument
, NULL
, 'D' },
293 { "no-regex", no_argument
, NULL
, 'R' },
294 { "no-warn", no_argument
, NULL
, 'w' },
295 { "output", required_argument
, NULL
, 'o' },
296 { "regex", required_argument
, NULL
, 'r' },
297 { "typedefs", no_argument
, NULL
, 't' },
298 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
299 { "update", no_argument
, NULL
, 'u' },
300 { "version", no_argument
, NULL
, 'V' },
301 { "vgrind", no_argument
, NULL
, 'v' },
306 /* Structure defining a regular expression. Elements are
307 the compiled pattern, and the name string. */
310 struct re_pattern_buffer
*pattern
;
311 struct re_registers regs
;
313 logical error_signaled
;
316 /* Number of regexps found. */
317 int num_patterns
= 0;
319 /* Array of all regexps. */
320 struct pattern
*patterns
= NULL
;
321 #endif /* ETAGS_REGEXPS */
327 /* Non-NULL if language fixed. */
328 Lang_function
*lang_func
= NULL
;
331 char *Asm_suffixes
[] = { "a", /* Unix assembler */
332 "asm", /* Microcontroller assembly */
333 "def", /* BSO/Tasking definition includes */
334 "inc", /* Microcontroller include files */
335 "ins", /* Microcontroller include files */
336 "s", "sa", /* Unix assembler */
337 "src", /* BSO/Tasking C compiler output */
341 /* Note that .c and .h can be considered C++, if the --c++ flag was
342 given. That is why default_C_entries is called here. */
343 char *default_C_suffixes
[] =
346 /* .M is for Objective C++ files. */
347 char *Cplusplus_suffixes
[] =
348 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL
};
350 char *Cstar_suffixes
[] =
351 { "cs", "hs", NULL
};
353 char *Erlang_suffixes
[] =
354 { "erl", "hrl", NULL
};
356 char *Fortran_suffixes
[] =
357 { "F", "f", "f90", "for", NULL
};
359 char *Lisp_suffixes
[] =
360 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL
};
362 char *Pascal_suffixes
[] =
363 { "p", "pas", NULL
};
365 char *Perl_suffixes
[] =
366 { "pl", "pm", NULL
};
367 char *Perl_interpreters
[] =
368 { "perl", "@PERL@", NULL
};
370 char *plain_C_suffixes
[] =
371 { "pc", /* Pro*C file */
372 "m", /* Objective C file */
373 "lm", /* Objective lex file */
376 char *Prolog_suffixes
[] =
379 /* Can't do the `SCM' or `scm' prefix with a version number. */
380 char *Scheme_suffixes
[] =
381 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL
};
383 char *TeX_suffixes
[] =
384 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL
};
386 char *Yacc_suffixes
[] =
387 { "y", "ym", NULL
}; /* .ym is Objective yacc file */
389 /* Table of language names and corresponding functions, file suffixes
390 and interpreter names.
391 It is ok for a given function to be listed under more than one
392 name. I just didn't. */
396 Lang_function
*function
;
401 struct lang_entry lang_names
[] =
403 { "asm", Asm_labels
, Asm_suffixes
, NULL
},
404 { "c", default_C_entries
, default_C_suffixes
, NULL
},
405 { "c++", Cplusplus_entries
, Cplusplus_suffixes
, NULL
},
406 { "c*", Cstar_entries
, Cstar_suffixes
, NULL
},
407 { "erlang", Erlang_functions
, Erlang_suffixes
, NULL
},
408 { "fortran", Fortran_functions
, Fortran_suffixes
, NULL
},
409 { "lisp", Lisp_functions
, Lisp_suffixes
, NULL
},
410 { "pascal", Pascal_functions
, Pascal_suffixes
, NULL
},
411 { "perl", Perl_functions
, Perl_suffixes
, Perl_interpreters
},
412 { "proc", plain_C_entries
, plain_C_suffixes
, NULL
},
413 { "prolog", Prolog_functions
, Prolog_suffixes
, NULL
},
414 { "scheme", Scheme_functions
, Scheme_suffixes
, NULL
},
415 { "tex", TeX_functions
, TeX_suffixes
, NULL
},
416 { "yacc", Yacc_entries
, Yacc_suffixes
, NULL
},
417 { "auto", NULL
}, /* default guessing scheme */
418 { "none", just_read_file
}, /* regexp matching only */
419 { NULL
, NULL
} /* end of list */
424 print_language_names ()
426 struct lang_entry
*lang
;
429 puts ("\nThese are the currently supported languages, along with the\n\
430 default file name suffixes:");
431 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
433 printf ("\t%s\t", lang
->name
);
434 if (lang
->suffixes
!= NULL
)
435 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
436 printf (" .%s", *ext
);
439 puts ("Where `auto' means use default language for files based on file\n\
440 name suffix, and `none' means only do regexp processing on files.\n\
441 If no language is specified and no matching suffix is found,\n\
442 the first line of the file is read for a sharp-bang (#!) sequence\n\
443 followed by the name of an interpreter. If no such sequence is found,\n\
444 Fortran is tried first; if no tags are found, C is tried next.");
448 # define VERSION "19"
453 printf ("%s for Emacs version %s\n", (CTAGS
) ? "ctags" : "etags", VERSION
);
461 printf ("These are the options accepted by %s. You may use unambiguous\n\
462 abbreviations for the long option names. A - as file name means read\n\
463 names from stdin.", progname
);
465 printf (" Absolute names are stored in the output file as they\n\
466 are. Relative ones are stored relative to the output file's directory.");
469 puts ("-a, --append\n\
470 Append tag entries to existing tags file.");
473 puts ("-B, --backward-search\n\
474 Write the search commands for the tag entries using '?', the\n\
475 backward-search command instead of '/', the forward-search command.");
478 Treat files whose name suffix defaults to C language as C++ files.");
481 puts ("-d, --defines\n\
482 Create tag entries for constant C #defines, too.");
484 puts ("-D, --no-defines\n\
485 Don't create tag entries for constant C #defines. This makes\n\
486 the tags file smaller.");
490 puts ("-i FILE, --include=FILE\n\
491 Include a note in tag file indicating that, when searching for\n\
492 a tag, one should also consult the tags file FILE after\n\
493 checking the current file.");
494 puts ("-l LANG, --language=LANG\n\
495 Force the following files to be considered as written in the\n\
496 named language up to the next --language=LANG option.");
500 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
501 Make a tag for each line matching pattern REGEXP in the\n\
502 following files. REGEXP is anchored (as if preceded by ^).\n\
503 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
504 named tags can be created with:\n\
505 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
506 puts ("-R, --no-regex\n\
507 Don't create tags from regexps for the following files.");
508 #endif /* ETAGS_REGEXPS */
509 puts ("-o FILE, --output=FILE\n\
510 Write the tags to FILE.");
511 puts ("-I, --ignore-indentation\n\
512 Don't rely on indentation quite as much as normal. Currently,\n\
513 this means not to assume that a closing brace in the first\n\
514 column is the final brace of a function or structure\n\
515 definition in C and C++.");
519 puts ("-t, --typedefs\n\
520 Generate tag entries for C typedefs.");
521 puts ("-T, --typedefs-and-c++\n\
522 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
523 and C++ member functions.");
524 puts ("-u, --update\n\
525 Update the tag entries for the given files, leaving tag\n\
526 entries for other files in place. Currently, this is\n\
527 implemented by deleting the existing entries for the given\n\
528 files and then rewriting the new entries at the end of the\n\
529 tags file. It is often faster to simply rebuild the entire\n\
530 tag file than to use this.");
531 puts ("-v, --vgrind\n\
532 Generates an index of items intended for human consumption,\n\
533 similar to the output of vgrind. The index is sorted, and\n\
534 gives the page number of each item.");
535 puts ("-w, --no-warn\n\
536 Suppress warning messages about entries defined in multiple\n\
538 puts ("-x, --cxref\n\
539 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
540 The output uses line numbers instead of page numbers, but\n\
541 beyond that the differences are cosmetic; try both to see\n\
545 puts ("-V, --version\n\
546 Print the version of the program.\n\
548 Print this help message.");
550 print_language_names ();
563 /* This structure helps us allow mixing of --lang and filenames. */
566 enum argument_type arg_type
;
568 Lang_function
*function
;
571 #ifdef VMS /* VMS specific functions */
575 /* This is a BUG! ANY arbitrary limit is a BUG!
576 Won't someone please fix this? */
577 #define MAX_FILE_SPEC_LEN 255
580 char body
[MAX_FILE_SPEC_LEN
+ 1];
584 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
585 returning in each successive call the next filename matching the input
586 spec. The function expects that each in_spec passed
587 to it will be processed to completion; in particular, up to and
588 including the call following that in which the last matching name
589 is returned, the function ignores the value of in_spec, and will
590 only start processing a new spec with the following call.
591 If an error occurs, on return out_spec contains the value
592 of in_spec when the error occurred.
594 With each successive filename returned in out_spec, the
595 function's return value is one. When there are no more matching
596 names the function returns zero. If on the first call no file
597 matches in_spec, or there is any other error, -1 is returned.
602 #define OUTSIZE MAX_FILE_SPEC_LEN
608 static long context
= 0;
609 static struct dsc$descriptor_s o
;
610 static struct dsc$descriptor_s i
;
611 static logical pass1
= TRUE
;
618 o
.dsc$a_pointer
= (char *) out
;
619 o
.dsc$w_length
= (short)OUTSIZE
;
620 i
.dsc$a_pointer
= in
;
621 i
.dsc$w_length
= (short)strlen(in
);
622 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
623 i
.dsc$b_class
= DSC$K_CLASS_S
;
624 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
625 o
.dsc$b_class
= DSC$K_CLASS_VS
;
627 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
629 out
->body
[out
->curlen
] = EOS
;
632 else if (status
== RMS$_NMF
)
636 strcpy(out
->body
, in
);
639 lib$
find_file_end(&context
);
645 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
646 name of each file specified by the provided arg expanding wildcards.
649 gfnames (arg
, p_error
)
653 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
655 switch (fn_exp (&filename
, arg
))
659 return filename
.body
;
665 return filename
.body
;
669 #ifndef OLD /* Newer versions of VMS do provide `system'. */
673 fprintf (stderr
, "system() function not implemented under VMS\n");
677 #define VERSION_DELIM ';'
678 char *massage_name (s
)
684 if (*s
== VERSION_DELIM
)
702 unsigned int nincluded_files
= 0;
703 char **included_files
= xnew (argc
, char *);
706 int current_arg
= 0, file_count
= 0;
707 struct linebuffer filename_lb
;
713 _fmode
= O_BINARY
; /* all of files are treated as binary files */
718 /* Allocate enough no matter what happens. Overkill, but each one
720 argbuffer
= xnew (argc
, argument
);
723 /* Set syntax for regular expression routines. */
724 re_set_syntax (RE_SYNTAX_EMACS
);
725 #endif /* ETAGS_REGEXPS */
728 * If etags, always find typedefs and structure tags. Why not?
729 * Also default is to find macro constants.
732 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
736 int opt
= getopt_long (argc
, argv
,
737 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
745 /* If getopt returns 0, then it has already processed a
746 long-named option. We should do nothing. */
750 /* This means that a filename has been seen. Record it. */
751 argbuffer
[current_arg
].arg_type
= at_filename
;
752 argbuffer
[current_arg
].what
= optarg
;
757 /* Common options. */
759 append_to_tagfile
= TRUE
;
765 constantypedefs
= TRUE
;
768 constantypedefs
= FALSE
;
770 case 'f': /* for compatibility with old makefiles */
774 fprintf (stderr
, "%s: -%c option may only be given once.\n",
776 suggest_asking_for_help ();
781 case 'S': /* for backward compatibility */
782 noindentypedefs
= TRUE
;
785 argbuffer
[current_arg
].function
= get_language_from_name (optarg
);
786 argbuffer
[current_arg
].arg_type
= at_language
;
791 argbuffer
[current_arg
].arg_type
= at_regexp
;
792 argbuffer
[current_arg
].what
= optarg
;
796 argbuffer
[current_arg
].arg_type
= at_regexp
;
797 argbuffer
[current_arg
].what
= NULL
;
800 #endif /* ETAGS_REGEXPS */
812 typedefs
= typedefs_and_cplusplus
= TRUE
;
817 included_files
[nincluded_files
++] = optarg
;
838 suggest_asking_for_help ();
842 for (; optind
< argc
; ++optind
)
844 argbuffer
[current_arg
].arg_type
= at_filename
;
845 argbuffer
[current_arg
].what
= argv
[optind
];
850 if (nincluded_files
== 0 && file_count
== 0)
852 fprintf (stderr
, "%s: No input files specified.\n", progname
);
853 suggest_asking_for_help ();
857 tagfile
= CTAGS
? "tags" : "TAGS";
858 cwd
= etags_getcwd (); /* the current working directory */
859 if (cwd
[strlen (cwd
) - 1] != '/')
860 cwd
= concat (cwd
, "/", "");
861 if (streq (tagfile
, "-"))
864 tagfiledir
= absolute_dirname (tagfile
, cwd
);
866 init (); /* set up boolean "functions" */
869 initbuffer (&token_name
);
870 initbuffer (&lbs
[0].lb
);
871 initbuffer (&lbs
[1].lb
);
872 initbuffer (&filename_lb
);
876 if (streq (tagfile
, "-"))
880 /* Switch redirected `stdout' to binary mode (setting `_fmode'
881 doesn't take effect until after `stdout' is already open). */
882 if (!isatty (fileno (stdout
)))
883 setmode (fileno (stdout
), O_BINARY
);
887 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
893 * Loop through files finding functions.
895 for (i
= 0; i
< current_arg
; ++i
)
897 switch (argbuffer
[i
].arg_type
)
900 lang_func
= argbuffer
[i
].function
;
904 add_regex (argbuffer
[i
].what
);
909 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
913 error ("Can't find file %s\n", this_file
);
918 this_file
= massage_name (this_file
);
921 this_file
= argbuffer
[i
].what
;
923 /* Input file named "-" means read file names from stdin
925 if (streq (this_file
, "-"))
926 while (readline_internal (&filename_lb
, stdin
) > 0)
927 process_file (filename_lb
.buffer
);
929 process_file (this_file
);
939 while (nincluded_files
-- > 0)
940 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
946 /* If CTAGS, we are here. process_file did not write the tags yet,
947 because we want them ordered. Let's do it now. */
950 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
960 for (i
= 0; i
< current_arg
; ++i
)
962 if (argbuffer
[i
].arg_type
!= at_filename
)
965 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
966 tagfile
, argbuffer
[i
].what
, tagfile
);
967 if (system (cmd
) != GOOD
)
968 fatal ("failed to execute shell command");
970 append_to_tagfile
= TRUE
;
973 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
982 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
990 * Return a Lang_function given the name.
993 get_language_from_name (name
)
996 struct lang_entry
*lang
;
999 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1001 if (streq (name
, lang
->name
))
1002 return lang
->function
;
1005 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
1007 suggest_asking_for_help ();
1009 /* This point should never be reached. The function should either
1010 return a function pointer or never return. Note that a NULL
1011 pointer cannot be considered as an error, as it means that the
1012 language has not been explicitely imposed by the user ("auto"). */
1013 return NULL
; /* avoid warnings from compiler */
1018 * Return a Lang_function given the interpreter name.
1021 get_language_from_interpreter (interpreter
)
1024 struct lang_entry
*lang
;
1027 if (interpreter
== NULL
)
1029 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1030 if (lang
->interpreters
!= NULL
)
1031 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1032 if (streq (*iname
, interpreter
))
1033 return lang
->function
;
1041 * Return a Lang_function given the file suffix.
1044 get_language_from_suffix (suffix
)
1047 struct lang_entry
*lang
;
1052 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1053 if (lang
->suffixes
!= NULL
)
1054 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1055 if (streq (*ext
, suffix
))
1056 return lang
->function
;
1063 * This routine is called on each file argument.
1069 struct stat stat_buf
;
1074 for (p
= file
; *p
!= '\0'; p
++)
1079 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1081 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1084 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1086 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1089 inf
= fopen (file
, "r");
1096 find_entries (file
, inf
);
1102 if (absolutefn (file
))
1104 /* file is an absolute filename. Canonicalise it. */
1105 filename
= absolute_filename (file
, cwd
);
1109 /* file is a filename relative to cwd. Make it relative
1110 to the directory of the tags file. */
1111 filename
= relative_filename (file
, tagfiledir
);
1113 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1122 * This routine sets up the boolean pseudo-functions which work
1123 * by setting boolean flags dependent upon the corresponding character
1124 * Every char which is NOT in that string is not a white char. Therefore,
1125 * all of the array "_wht" is set to FALSE, and then the elements
1126 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1127 * of a char is TRUE if it is the string "white", else FALSE.
1135 for (i
= 0; i
< 0177; i
++)
1136 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1137 for (sp
= white
; *sp
; sp
++)
1139 for (sp
= endtk
; *sp
; sp
++)
1141 for (sp
= intk
; *sp
; sp
++)
1143 for (sp
= begtk
; *sp
; sp
++)
1145 _wht
[0] = _wht
['\n'];
1146 _etk
[0] = _etk
['\n'];
1147 _btk
[0] = _btk
['\n'];
1148 _itk
[0] = _itk
['\n'];
1152 * This routine opens the specified file and calls the function
1153 * which finds the function and type definitions.
1156 find_entries (file
, inf
)
1161 Lang_function
*function
;
1162 NODE
*old_last_node
;
1163 extern NODE
*last_node
;
1166 /* Memory leakage here: the memory block pointed by curfile is never
1167 released. The amount of memory leaked here is the sum of the
1168 lengths of the input file names. */
1169 curfile
= savestr (file
);
1171 /* If user specified a language, use it. */
1172 function
= lang_func
;
1173 if (function
!= NULL
)
1180 cp
= etags_strrchr (file
, '.');
1184 function
= get_language_from_suffix (cp
);
1185 if (function
!= NULL
)
1193 /* Look for sharp-bang as the first two characters. */
1194 if (readline_internal (&lb
, inf
) > 2
1195 && lb
.buffer
[0] == '#'
1196 && lb
.buffer
[1] == '!')
1200 /* Set lp to point at the first char after the last slash in the
1201 line or, if no slashes, at the first nonblank. Then set cp to
1202 the first successive blank and terminate the string. */
1203 lp
= etags_strrchr (lb
.buffer
+2, '/');
1207 for (lp
= lb
.buffer
+2; *lp
!= '\0' && isspace (*lp
); lp
++)
1209 for (cp
= lp
; *cp
!= '\0' && !isspace (*cp
); cp
++)
1213 if (strlen (lp
) > 0)
1215 function
= get_language_from_interpreter (lp
);
1216 if (function
!= NULL
)
1227 old_last_node
= last_node
;
1228 Fortran_functions (inf
);
1230 /* No Fortran entries found. Try C. */
1231 if (old_last_node
== last_node
)
1234 default_C_entries (inf
);
1242 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1243 char *name
; /* tag name, or NULL if unnamed */
1244 logical is_func
; /* tag is a function */
1245 char *linestart
; /* start of the line where tag is */
1246 int linelen
; /* length of the line where tag is */
1247 int lno
; /* line number */
1248 long cno
; /* character number */
1252 if (CTAGS
&& name
== NULL
)
1255 np
= xnew (1, NODE
);
1257 /* If ctags mode, change name "main" to M<thisfilename>. */
1258 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1260 register char *fp
= etags_strrchr (curfile
, '/');
1261 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1262 fp
= etags_strrchr (np
->name
, '.');
1263 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1268 np
->been_warned
= FALSE
;
1270 np
->is_func
= is_func
;
1272 /* Our char numbers are 0-base, because of C language tradition?
1273 ctags compatibility? old versions compatibility? I don't know.
1274 Anyway, since emacs's are 1-base we expect etags.el to take care
1275 of the difference. If we wanted to have 1-based numbers, we would
1276 uncomment the +1 below. */
1277 np
->cno
= cno
/* + 1 */ ;
1278 np
->left
= np
->right
= NULL
;
1279 if (CTAGS
&& !cxref_style
)
1281 if (strlen (linestart
) < 50)
1282 np
->pat
= concat (linestart
, "$", "");
1284 np
->pat
= savenstr (linestart
, 50);
1287 np
->pat
= savenstr (linestart
, linelen
);
1289 add_node (np
, &head
);
1294 * recurse on left children, iterate on right children.
1298 register NODE
*node
;
1302 register NODE
*node_right
= node
->right
;
1303 free_tree (node
->left
);
1304 if (node
->name
!= NULL
)
1307 free ((char *) node
);
1314 * Adds a node to the tree of nodes. In etags mode, we don't keep
1315 * it sorted; we just keep a linear list. In ctags mode, maintain
1316 * an ordered tree, with no attempt at balancing.
1318 * add_node is the only function allowed to add nodes, so it can
1321 NODE
*last_node
= NULL
;
1323 add_node (node
, cur_node_p
)
1324 NODE
*node
, **cur_node_p
;
1327 register NODE
*cur_node
= *cur_node_p
;
1329 if (cur_node
== NULL
)
1339 if (last_node
== NULL
)
1340 fatal ("internal error in add_node", 0);
1341 last_node
->right
= node
;
1347 dif
= strcmp (node
->name
, cur_node
->name
);
1350 * If this tag name matches an existing one, then
1351 * do not add the node, but maybe print a warning.
1355 if (streq (node
->file
, cur_node
->file
))
1359 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1360 node
->file
, lineno
, node
->name
);
1361 fprintf (stderr
, "Second entry ignored\n");
1364 else if (!cur_node
->been_warned
&& !no_warnings
)
1368 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1369 node
->file
, cur_node
->file
, node
->name
);
1370 cur_node
->been_warned
= TRUE
;
1375 /* Actually add the node */
1376 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1382 register NODE
*node
;
1389 /* Output subentries that precede this one */
1390 put_entries (node
->left
);
1392 /* Output this entry */
1396 if (node
->name
!= NULL
)
1397 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1398 node
->pat
, node
->name
, node
->lno
, node
->cno
);
1400 fprintf (tagf
, "%s\177%d,%d\n",
1401 node
->pat
, node
->lno
, node
->cno
);
1405 if (node
->name
== NULL
)
1406 error ("internal error: NULL name in ctags mode.", 0);
1411 fprintf (stdout
, "%s %s %d\n",
1412 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1414 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1415 node
->name
, node
->lno
, node
->file
, node
->pat
);
1419 fprintf (tagf
, "%s\t%s\t", node
->name
, node
->file
);
1423 putc (searchar
, tagf
);
1426 for (sp
= node
->pat
; *sp
; sp
++)
1428 if (*sp
== '\\' || *sp
== searchar
)
1432 putc (searchar
, tagf
);
1435 { /* a typedef; text pattern inadequate */
1436 fprintf (tagf
, "%d", node
->lno
);
1442 /* Output subentries that follow this one */
1443 put_entries (node
->right
);
1446 /* Length of a number's decimal representation. */
1454 for (; num
; num
/= 10)
1460 * Return total number of characters that put_entries will output for
1461 * the nodes in the subtree of the specified node. Works only if
1462 * we are not ctags, but called only in that case. This count
1463 * is irrelevant with the new tags.el, but is still supplied for
1464 * backward compatibility.
1467 total_size_of_entries (node
)
1468 register NODE
*node
;
1476 for (; node
; node
= node
->right
)
1478 /* Count left subentries. */
1479 total
+= total_size_of_entries (node
->left
);
1481 /* Count this entry */
1482 total
+= strlen (node
->pat
) + 1;
1483 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1484 if (node
->name
!= NULL
)
1485 total
+= 1 + strlen (node
->name
); /* \001name */
1492 * The C symbol tables.
1496 st_none
, st_C_objprot
, st_C_objimpl
, st_C_objend
, st_C_gnumacro
,
1497 st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1500 /* Feed stuff between (but not including) %[ and %] lines to:
1501 gperf -c -k 1,3 -o -p -r -t
1503 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1505 @interface, 0, st_C_objprot
1506 @protocol, 0, st_C_objprot
1507 @implementation,0, st_C_objimpl
1508 @end, 0, st_C_objend
1509 class, C_PLPL, st_C_struct
1510 namespace, C_PLPL, st_C_struct
1511 domain, C_STAR, st_C_struct
1512 union, 0, st_C_struct
1513 struct, 0, st_C_struct
1515 typedef, 0, st_C_typedef
1516 define, 0, st_C_define
1517 bool, C_PLPL, st_C_typespec
1518 long, 0, st_C_typespec
1519 short, 0, st_C_typespec
1520 int, 0, st_C_typespec
1521 char, 0, st_C_typespec
1522 float, 0, st_C_typespec
1523 double, 0, st_C_typespec
1524 signed, 0, st_C_typespec
1525 unsigned, 0, st_C_typespec
1526 auto, 0, st_C_typespec
1527 void, 0, st_C_typespec
1528 extern, 0, st_C_typespec
1529 static, 0, st_C_typespec
1530 const, 0, st_C_typespec
1531 volatile, 0, st_C_typespec
1532 explicit, C_PLPL, st_C_typespec
1533 mutable, C_PLPL, st_C_typespec
1534 typename, C_PLPL, st_C_typespec
1535 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1536 DEFUN, 0, st_C_gnumacro
1537 SYSCALL, 0, st_C_gnumacro
1538 ENTRY, 0, st_C_gnumacro
1539 PSEUDO, 0, st_C_gnumacro
1540 # These are defined inside C functions, so currently they are not met.
1541 # EXFUN used in glibc, DEFVAR_* in emacs.
1542 #EXFUN, 0, st_C_gnumacro
1543 #DEFVAR_, 0, st_C_gnumacro
1545 and replace lines between %< and %> with its output. */
1547 /* C code produced by gperf version 2.1 (K&R C version) */
1548 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
1551 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1553 #define MIN_WORD_LENGTH 3
1554 #define MAX_WORD_LENGTH 15
1555 #define MIN_HASH_VALUE 34
1556 #define MAX_HASH_VALUE 121
1559 88 is the maximum key range
1565 register unsigned int len
;
1567 static unsigned char hash_table
[] =
1569 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1570 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1571 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1572 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1573 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1574 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1575 121, 121, 121, 121, 45, 121, 121, 121, 16, 19,
1576 61, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1577 10, 121, 121, 20, 53, 121, 121, 121, 121, 121,
1578 121, 121, 121, 121, 121, 121, 121, 41, 45, 22,
1579 60, 47, 37, 28, 121, 55, 121, 121, 20, 14,
1580 29, 30, 5, 121, 50, 59, 30, 54, 6, 121,
1581 121, 121, 121, 121, 121, 121, 121, 121,
1583 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1586 struct C_stab_entry
*
1587 in_word_set (str
, len
)
1589 register unsigned int len
;
1592 static struct C_stab_entry wordlist
[] =
1594 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1595 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1596 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1597 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1598 {"volatile", 0, st_C_typespec
},
1599 {"PSEUDO", 0, st_C_gnumacro
},
1600 {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1601 {"typedef", 0, st_C_typedef
},
1602 {"typename", C_PLPL
, st_C_typespec
},
1603 {"",}, {"",}, {"",},
1604 {"SYSCALL", 0, st_C_gnumacro
},
1605 {"",}, {"",}, {"",},
1606 {"mutable", C_PLPL
, st_C_typespec
},
1607 {"namespace", C_PLPL
, st_C_struct
},
1608 {"long", 0, st_C_typespec
},
1610 {"const", 0, st_C_typespec
},
1611 {"",}, {"",}, {"",},
1612 {"explicit", C_PLPL
, st_C_typespec
},
1613 {"",}, {"",}, {"",}, {"",},
1614 {"void", 0, st_C_typespec
},
1616 {"char", 0, st_C_typespec
},
1617 {"class", C_PLPL
, st_C_struct
},
1618 {"",}, {"",}, {"",},
1619 {"float", 0, st_C_typespec
},
1621 {"@implementation", 0, st_C_objimpl
},
1622 {"auto", 0, st_C_typespec
},
1624 {"ENTRY", 0, st_C_gnumacro
},
1625 {"@end", 0, st_C_objend
},
1626 {"bool", C_PLPL
, st_C_typespec
},
1627 {"domain", C_STAR
, st_C_struct
},
1629 {"DEFUN", 0, st_C_gnumacro
},
1630 {"extern", 0, st_C_typespec
},
1631 {"@interface", 0, st_C_objprot
},
1632 {"",}, {"",}, {"",},
1633 {"int", 0, st_C_typespec
},
1634 {"",}, {"",}, {"",}, {"",},
1635 {"signed", 0, st_C_typespec
},
1636 {"short", 0, st_C_typespec
},
1637 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1638 {"define", 0, st_C_define
},
1639 {"@protocol", 0, st_C_objprot
},
1640 {"enum", 0, st_C_enum
},
1641 {"static", 0, st_C_typespec
},
1642 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1643 {"union", 0, st_C_struct
},
1644 {"struct", 0, st_C_struct
},
1645 {"",}, {"",}, {"",}, {"",},
1646 {"double", 0, st_C_typespec
},
1647 {"unsigned", 0, st_C_typespec
},
1650 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1652 register int key
= hash (str
, len
);
1654 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1656 register char *s
= wordlist
[key
].name
;
1658 if (*s
== *str
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
1659 return &wordlist
[key
];
1667 C_symtype(str
, len
, c_ext
)
1672 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1674 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1680 * C functions are recognized using a simple finite automaton.
1681 * funcdef is its state variable.
1685 fnone
, /* nothing seen */
1686 ftagseen
, /* function-like tag seen */
1687 fstartlist
, /* just after open parenthesis */
1688 finlist
, /* in parameter list */
1689 flistseen
, /* after parameter list */
1690 fignore
/* before open brace */
1695 * typedefs are recognized using a simple finite automaton.
1696 * typdef is its state variable.
1700 tnone
, /* nothing seen */
1701 ttypedseen
, /* typedef keyword seen */
1702 tinbody
, /* inside typedef body */
1703 tend
, /* just before typedef tag */
1704 tignore
/* junk after typedef tag */
1709 * struct-like structures (enum, struct and union) are recognized
1710 * using another simple finite automaton. `structdef' is its state
1715 snone
, /* nothing seen yet */
1716 skeyseen
, /* struct-like keyword seen */
1717 stagseen
, /* struct-like tag seen */
1718 scolonseen
, /* colon seen after struct-like tag */
1719 sinbody
/* in struct body: recognize member func defs*/
1723 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1724 * struct tag, and structtype is the type of the preceding struct-like
1727 char *structtag
= "<uninited>";
1728 enum sym_type structtype
;
1731 * When objdef is different from onone, objtag is the name of the class.
1733 char *objtag
= "<uninited>";
1736 * Yet another little state machine to deal with preprocessor lines.
1740 dnone
, /* nothing seen */
1741 dsharpseen
, /* '#' seen as first char on line */
1742 ddefineseen
, /* '#' and 'define' seen */
1743 dignorerest
/* ignore rest of line */
1747 * State machine for Objective C protocols and implementations.
1751 onone
, /* nothing seen */
1752 oprotocol
, /* @interface or @protocol seen */
1753 oimplementation
, /* @implementations seen */
1754 otagseen
, /* class name seen */
1755 oparenseen
, /* parenthesis before category seen */
1756 ocatseen
, /* category name seen */
1757 oinbody
, /* in @implementation body */
1758 omethodsign
, /* in @implementation body, after +/- */
1759 omethodtag
, /* after method name */
1760 omethodcolon
, /* after method colon */
1761 omethodparm
, /* after method parameter */
1762 oignore
/* wait for @end */
1766 * Set this to TRUE, and the next token considered is called a function.
1767 * Used only for GNU emacs's function-defining macros.
1769 logical next_token_is_func
;
1772 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1777 * methodlen is the length of the method name stored in token_name.
1783 * checks to see if the current token is at the start of a
1784 * function, or corresponds to a typedef, or is a struct/union/enum
1787 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1788 * C_EXT is which language we are looking at.
1790 * In the future we will need some way to adjust where the end of
1791 * the token is; for instance, implementing the C++ keyword
1792 * `operator' properly will adjust the end of the token to be after
1793 * whatever follows `operator'.
1801 * next_token_is_func IN OUT
1805 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func
)
1806 register char *str
; /* IN: token pointer */
1807 register int len
; /* IN: token length */
1808 register char c
; /* IN: first char after the token */
1809 int c_ext
; /* IN: C extensions mask */
1810 int cblev
; /* IN: curly brace level */
1811 int parlev
; /* IN: parenthesis level */
1812 logical
*is_func
; /* OUT: function found */
1814 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1817 * Advance the definedef state machine.
1822 /* We're not on a preprocessor line. */
1825 if (toktype
== st_C_define
)
1827 definedef
= ddefineseen
;
1831 definedef
= dignorerest
;
1836 * Make a tag for any macro, unless it is a constant
1837 * and constantypedefs is FALSE.
1839 definedef
= dignorerest
;
1840 *is_func
= (c
== '(');
1841 if (!*is_func
&& !constantypedefs
)
1848 error ("internal error: definedef value.", 0);
1857 if (toktype
== st_C_typedef
)
1860 typdef
= ttypedseen
;
1876 /* Do not return here, so the structdef stuff has a chance. */
1890 * This structdef business is currently only invoked when cblev==0.
1891 * It should be recursively invoked whatever the curly brace level,
1892 * and a stack of states kept, to allow for definitions of structs
1895 * This structdef business is NOT invoked when we are ctags and the
1896 * file is plain C. This is because a struct tag may have the same
1897 * name as another tag, and this loses with ctags.
1899 * This if statement deals with the typdef state machine as
1900 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1901 * return FALSE. All the other code here is for the structdef
1908 if (typdef
== ttypedseen
1909 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1911 structdef
= skeyseen
;
1912 structtype
= toktype
;
1916 if (structdef
== skeyseen
)
1918 /* Save the tag for struct/union/class, for functions that may be
1920 if (structtype
== st_C_struct
)
1921 structtag
= savenstr (str
, len
);
1923 structtag
= "<enum>";
1924 structdef
= stagseen
;
1928 /* Avoid entering funcdef stuff if typdef is going on. */
1929 if (typdef
!= tnone
)
1935 /* Detect GNU macros. */
1936 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
1938 next_token_is_func
= TRUE
;
1941 if (next_token_is_func
)
1943 next_token_is_func
= FALSE
;
1950 * Detecting Objective C constructs.
1961 objdef
= oimplementation
;
1965 case oimplementation
:
1966 /* Save the class tag for functions that may be defined inside. */
1967 objtag
= savenstr (str
, len
);
1971 /* Save the class tag for categories. */
1972 objtag
= savenstr (str
, len
);
1985 objdef
= omethodtag
;
1987 GROW_LINEBUFFER (token_name
, methodlen
+1);
1988 strncpy (token_name
.buffer
, str
, len
);
1989 token_name
.buffer
[methodlen
] = '\0';
1995 objdef
= omethodparm
;
2000 objdef
= omethodtag
;
2002 GROW_LINEBUFFER (token_name
, methodlen
+1);
2003 strncat (token_name
.buffer
, str
, len
);
2008 if (toktype
== st_C_objend
)
2010 /* Memory leakage here: the string pointed by objtag is
2011 never released, because many tests would be needed to
2012 avoid breaking on incorrect input code. The amount of
2013 memory leaked here is the sum of the lengths of the
2025 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2026 funcdef
= fnone
; /* should be useless */
2029 if (funcdef
== fnone
)
2042 * This routine finds functions, typedefs, #define's and
2043 * struct/union/enum definitions in C syntax and adds them
2057 #define current_lb_is_new (newndx == curndx)
2058 #define switch_line_buffers() (curndx = 1 - curndx)
2060 #define curlb (lbs[curndx].lb)
2061 #define othlb (lbs[1-curndx].lb)
2062 #define newlb (lbs[newndx].lb)
2063 #define curlinepos (lbs[curndx].linepos)
2064 #define othlinepos (lbs[1-curndx].linepos)
2065 #define newlinepos (lbs[newndx].linepos)
2067 #define CNL_SAVE_DEFINEDEF \
2069 curlinepos = charno; \
2071 linecharno = charno; \
2072 charno += readline (&curlb, inf); \
2073 lp = curlb.buffer; \
2080 CNL_SAVE_DEFINEDEF; \
2081 if (savetok.valid) \
2084 savetok.valid = FALSE; \
2086 definedef = dnone; \
2089 /* Ideally this macro should never be called wihen tok.valid is FALSE,
2090 but this would mean that the state machines always guess right. */
2091 #define make_tag(isfun) do \
2093 char *name = NULL; \
2094 if (CTAGS || tok.named) \
2095 name = savestr (token_name.buffer); \
2096 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
2097 tok.valid = FALSE; \
2101 C_entries (c_ext
, inf
)
2102 int c_ext
; /* extension of C */
2103 FILE *inf
; /* input file */
2105 register char c
; /* latest char read; '\0' for end of line */
2106 register char *lp
; /* pointer one beyond the character `c' */
2107 int curndx
, newndx
; /* indices for current and new lb */
2108 TOKEN tok
; /* latest token read */
2109 register int tokoff
; /* offset in line of start of current token */
2110 register int toklen
; /* length of current token */
2111 int cblev
; /* current curly brace level */
2112 int parlev
; /* current parenthesis level */
2113 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
2115 TOKEN savetok
; /* token saved during preprocessor handling */
2118 curndx
= newndx
= 0;
2124 funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
2125 definedef
= dnone
; objdef
= onone
;
2126 next_token_is_func
= yacc_rules
= FALSE
;
2127 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2128 tok
.valid
= savetok
.valid
= FALSE
;
2131 cplpl
= c_ext
& C_PLPL
;
2138 /* If we're at the end of the line, the next character is a
2139 '\0'; don't skip it, because it's the thing that tells us
2140 to read the next line. */
2161 /* Newlines inside comments do not end macro definitions in
2176 /* Newlines inside strings do not end macro definitions
2177 in traditional cpp, even though compilers don't
2178 usually accept them. */
2189 /* Hmmm, something went wrong. */
2203 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2208 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2218 else if (/* cplpl && */ *lp
== '/')
2226 if ((c_ext
& YACC
) && *lp
== '%')
2228 /* entering or exiting rules section in yacc file */
2230 definedef
= dnone
; funcdef
= fnone
;
2231 typdef
= tnone
; structdef
= snone
;
2232 next_token_is_func
= FALSE
;
2233 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2235 yacc_rules
= !yacc_rules
;
2241 if (definedef
== dnone
)
2244 logical cpptoken
= TRUE
;
2246 /* Look back on this line. If all blanks, or nonblanks
2247 followed by an end of comment, this is a preprocessor
2249 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2252 if (*cp
== '*' && *(cp
+1) == '/')
2261 definedef
= dsharpseen
;
2262 } /* if (definedef == dnone) */
2268 /* Consider token only if some complicated conditions are satisfied. */
2269 if ((definedef
!= dnone
2270 || (cblev
== 0 && structdef
!= scolonseen
)
2271 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2272 && typdef
!= tignore
2273 && definedef
!= dignorerest
2274 && funcdef
!= finlist
)
2280 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken(*(lp
+ 1)))
2283 * This handles :: in the middle, but not at the
2284 * beginning of an identifier.
2291 logical is_func
= FALSE
;
2294 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2295 c_ext
, cblev
, parlev
, &is_func
))
2297 if (structdef
== sinbody
2298 && definedef
== dnone
2300 /* function defined in C++ class body */
2302 GROW_LINEBUFFER (token_name
,
2303 strlen(structtag
)+2+toklen
+1);
2304 strcpy (token_name
.buffer
, structtag
);
2305 strcat (token_name
.buffer
, "::");
2306 strncat (token_name
.buffer
,
2307 newlb
.buffer
+tokoff
, toklen
);
2310 else if (objdef
== ocatseen
)
2311 /* Objective C category */
2313 GROW_LINEBUFFER (token_name
,
2314 strlen(objtag
)+2+toklen
+1);
2315 strcpy (token_name
.buffer
, objtag
);
2316 strcat (token_name
.buffer
, "(");
2317 strncat (token_name
.buffer
,
2318 newlb
.buffer
+tokoff
, toklen
);
2319 strcat (token_name
.buffer
, ")");
2322 else if (objdef
== omethodtag
2323 || objdef
== omethodparm
)
2324 /* Objective C method */
2330 GROW_LINEBUFFER (token_name
, toklen
+1);
2331 strncpy (token_name
.buffer
,
2332 newlb
.buffer
+tokoff
, toklen
);
2333 token_name
.buffer
[toklen
] = '\0';
2334 if (structdef
== stagseen
2337 && definedef
== dignorerest
)) /* macro */
2342 tok
.lineno
= lineno
;
2343 tok
.linelen
= tokoff
+ toklen
+ 1;
2344 tok
.buffer
= newlb
.buffer
;
2345 tok
.linepos
= newlinepos
;
2348 if (definedef
== dnone
2349 && (funcdef
== ftagseen
2350 || structdef
== stagseen
2352 || objdef
!= onone
))
2354 if (current_lb_is_new
)
2355 switch_line_buffers ();
2362 } /* if (endtoken (c)) */
2363 else if (intoken (c
))
2368 } /* if (midtoken) */
2369 else if (begtoken (c
))
2387 if (structdef
== stagseen
)
2393 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2395 tokoff
= lp
- 1 - newlb
.buffer
;
2400 } /* if (begtoken) */
2401 } /* if must look at token */
2404 /* Detect end of line, colon, comma, semicolon and various braces
2405 after having handled a token.*/
2409 if (definedef
!= dnone
)
2419 objdef
= omethodcolon
;
2421 GROW_LINEBUFFER (token_name
, methodlen
+1);
2422 strcat (token_name
.buffer
, ":");
2425 if (structdef
== stagseen
)
2426 structdef
= scolonseen
;
2443 if (definedef
!= dnone
)
2454 if (funcdef
!= fignore
)
2457 /* The following instruction invalidates the token.
2458 Probably the token should be invalidated in all
2459 other cases where some state machine is reset. */
2462 if (structdef
== stagseen
)
2466 if (definedef
!= dnone
)
2476 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2478 if (structdef
== stagseen
)
2482 if (definedef
!= dnone
)
2484 if (cblev
== 0 && typdef
== tend
)
2490 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2492 if (structdef
== stagseen
)
2496 if (definedef
!= dnone
)
2498 if (objdef
== otagseen
&& parlev
== 0)
2499 objdef
= oparenseen
;
2507 /* Make sure that the next char is not a '*'.
2508 This handles constructs like:
2509 typedef void OperatorFun (int fun); */
2516 } /* switch (typdef) */
2519 funcdef
= fstartlist
;
2528 if (definedef
!= dnone
)
2530 if (objdef
== ocatseen
&& parlev
== 1)
2541 funcdef
= flistseen
;
2544 if (cblev
== 0 && typdef
== tend
)
2550 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2554 if (definedef
!= dnone
)
2556 if (typdef
== ttypedseen
)
2560 case skeyseen
: /* unnamed struct */
2561 structtag
= "_anonymous_";
2562 structdef
= sinbody
;
2565 case scolonseen
: /* named struct */
2566 structdef
= sinbody
;
2591 /* Neutralize `extern "C" {' grot and look inside structs. */
2592 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2599 if (definedef
!= dnone
)
2601 if (funcdef
== fstartlist
)
2602 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2605 if (definedef
!= dnone
)
2607 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2609 cblev
= 0; /* reset curly brace level if first column */
2610 parlev
= 0; /* also reset paren level, just in case... */
2616 if (typdef
== tinbody
)
2618 /* Memory leakage here: the string pointed by structtag is
2619 never released, because I fear to miss something and
2620 break things while freeing the area. The amount of
2621 memory leaked here is the sum of the lengths of the
2623 if (structdef == sinbody)
2624 free (structtag); */
2627 structtag
= "<error>";
2632 if (objdef
== oinbody
&& cblev
== 0)
2634 objdef
= omethodsign
;
2638 case '=': case '#': case '~': case '&': case '%': case '/':
2639 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2640 if (definedef
!= dnone
)
2642 /* These surely cannot follow a function tag. */
2643 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2647 if (objdef
== otagseen
)
2652 /* If a macro spans multiple lines don't reset its state. */
2660 } /* while not eof */
2664 * Process either a C++ file or a C file depending on the setting
2668 default_C_entries (inf
)
2671 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2674 /* Always do plain ANSI C. */
2676 plain_C_entries (inf
)
2682 /* Always do C++. */
2684 Cplusplus_entries (inf
)
2687 C_entries (C_PLPL
, inf
);
2695 C_entries (C_STAR
, inf
);
2698 /* Always do Yacc. */
2703 C_entries (YACC
, inf
);
2706 /* Fortran parsing */
2714 register int len
= 0;
2716 while (*cp
&& lowcase(*cp
) == lowcase(dbp
[len
]))
2718 if (*cp
== '\0' && !intoken(dbp
[len
]))
2729 while (isspace (*dbp
))
2734 while (isspace (*dbp
))
2736 if (strneq (dbp
, "(*)", 3))
2741 if (!isdigit (*dbp
))
2743 --dbp
; /* force failure */
2748 while (isdigit (*dbp
));
2757 while (isspace (*dbp
))
2762 linecharno
= charno
;
2763 charno
+= readline (&lb
, inf
);
2768 while (isspace (*dbp
))
2777 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2780 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
2781 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2785 Fortran_functions (inf
)
2794 linecharno
= charno
;
2795 charno
+= readline (&lb
, inf
);
2798 dbp
++; /* Ratfor escape to fortran */
2799 while (isspace (*dbp
))
2803 switch (lowcase (*dbp
))
2806 if (tail ("integer"))
2814 if (tail ("logical"))
2818 if (tail ("complex") || tail ("character"))
2822 if (tail ("double"))
2824 while (isspace (*dbp
))
2828 if (tail ("precision"))
2834 while (isspace (*dbp
))
2838 switch (lowcase (*dbp
))
2841 if (tail ("function"))
2845 if (tail ("subroutine"))
2853 if (tail ("program"))
2858 if (tail ("procedure"))
2866 * Bob Weiner, Motorola Inc., 4/3/94
2867 * Unix and microcontroller assembly tag handling
2868 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2882 linecharno
= charno
;
2883 charno
+= readline (&lb
, inf
);
2886 /* If first char is alphabetic or one of [_.$], test for colon
2887 following identifier. */
2888 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2890 /* Read past label. */
2892 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2894 if (*cp
== ':' || isspace (*cp
))
2896 /* Found end of label, so copy it and add it to the table. */
2897 pfnote ((CTAGS
) ? savenstr(lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2898 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2905 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2906 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2909 Perl_functions (inf
)
2920 linecharno
= charno
;
2921 charno
+= readline (&lb
, inf
);
2924 if (*cp
++ == 's' && *cp
++ == 'u' && *cp
++ == 'b' && isspace(*cp
++))
2926 while (*cp
&& isspace(*cp
))
2928 while (*cp
&& ! isspace(*cp
) && *cp
!= '{')
2930 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2931 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2936 /* Added by Mosur Mohan, 4/22/88 */
2937 /* Pascal parsing */
2940 * Locates tags for procedures & functions. Doesn't do any type- or
2941 * var-definitions. It does look for the keyword "extern" or
2942 * "forward" immediately following the procedure statement; if found,
2943 * the tag is skipped.
2946 Pascal_functions (inf
)
2949 struct linebuffer tline
; /* mostly copied from C_entries */
2951 int save_lineno
, save_len
;
2952 char c
, *cp
, *namebuf
;
2954 logical
/* each of these flags is TRUE iff: */
2955 incomment
, /* point is inside a comment */
2956 inquote
, /* point is inside '..' string */
2957 get_tagname
, /* point is after PROCEDURE/FUNCTION
2958 keyword, so next item = potential tag */
2959 found_tag
, /* point is after a potential tag */
2960 inparms
, /* point is within parameter-list */
2961 verify_tag
; /* point has passed the parm-list, so the
2962 next token will determine whether this
2963 is a FORWARD/EXTERN to be ignored, or
2964 whether it is a real tag */
2971 initbuffer (&tline
);
2973 incomment
= inquote
= FALSE
;
2974 found_tag
= FALSE
; /* have a proc name; check if extern */
2975 get_tagname
= FALSE
; /* have found "procedure" keyword */
2976 inparms
= FALSE
; /* found '(' after "proc" */
2977 verify_tag
= FALSE
; /* check if "extern" is ahead */
2979 /* long main loop to get next char */
2983 if (c
== '\0') /* if end of line */
2986 linecharno
= charno
;
2987 charno
+= readline (&lb
, inf
);
2991 if (!((found_tag
&& verify_tag
) ||
2993 c
= *dbp
++; /* only if don't need *dbp pointing
2994 to the beginning of the name of
2995 the procedure or function */
2999 if (c
== '}') /* within { } comments */
3001 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
3018 inquote
= TRUE
; /* found first quote */
3020 case '{': /* found open { comment */
3024 if (*dbp
== '*') /* found open (* comment */
3029 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
3032 case ')': /* end of parms list */
3037 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
3044 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
3046 /* check if this is an "extern" declaration */
3049 if (lowcase (*dbp
== 'e'))
3051 if (tail ("extern")) /* superfluous, really! */
3057 else if (lowcase (*dbp
) == 'f')
3059 if (tail ("forward")) /* check for forward reference */
3065 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
3069 pfnote (namebuf
, TRUE
,
3070 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
3074 if (get_tagname
) /* grab name of proc or fn */
3079 /* save all values for later tagging */
3080 GROW_LINEBUFFER (tline
, strlen (lb
.buffer
) + 1);
3081 strcpy (tline
.buffer
, lb
.buffer
);
3082 save_lineno
= lineno
;
3083 save_lcno
= linecharno
;
3085 /* grab block name */
3086 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
3088 namebuf
= (CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
;
3089 dbp
= cp
; /* set dbp to e-o-token */
3090 save_len
= dbp
- lb
.buffer
+ 1;
3091 get_tagname
= FALSE
;
3095 /* and proceed to check for "extern" */
3097 else if (!incomment
&& !inquote
&& !found_tag
)
3099 /* check for proc/fn keywords */
3100 switch (lowcase (c
))
3103 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3107 if (tail ("unction"))
3112 } /* while not eof */
3114 free (tline
.buffer
);
3118 * lisp tag functions
3119 * look for (def or (DEF, quote or QUOTE
3123 register char *strp
;
3125 return ((strp
[1] == 'd' || strp
[1] == 'D')
3126 && (strp
[2] == 'e' || strp
[2] == 'E')
3127 && (strp
[3] == 'f' || strp
[3] == 'F'));
3132 register char *strp
;
3134 return ((*(++strp
) == 'q' || *strp
== 'Q')
3135 && (*(++strp
) == 'u' || *strp
== 'U')
3136 && (*(++strp
) == 'o' || *strp
== 'O')
3137 && (*(++strp
) == 't' || *strp
== 'T')
3138 && (*(++strp
) == 'e' || *strp
== 'E')
3139 && isspace(*(++strp
)));
3147 if (*dbp
== '\'') /* Skip prefix quote */
3149 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
3152 while (isspace(*dbp
))
3155 for (cp
= dbp
/*+1*/;
3156 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
3162 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3163 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3167 Lisp_functions (inf
)
3176 linecharno
= charno
;
3177 charno
+= readline (&lb
, inf
);
3183 while (!isspace (*dbp
))
3185 while (isspace (*dbp
))
3191 /* Check for (foo::defmumble name-defined ... */
3194 while (*dbp
&& !isspace (*dbp
)
3195 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
3200 while (*dbp
== ':');
3202 if (L_isdef (dbp
- 1))
3204 while (!isspace (*dbp
))
3206 while (isspace (*dbp
))
3217 * Scheme tag functions
3218 * look for (def... xyzzy
3219 * look for (def... (xyzzy
3220 * look for (def ... ((...(xyzzy ....
3221 * look for (set! xyzzy
3227 Scheme_functions (inf
)
3236 linecharno
= charno
;
3237 charno
+= readline (&lb
, inf
);
3239 if (dbp
[0] == '(' &&
3240 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
3241 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3242 (dbp
[3] == 'F' || dbp
[3] == 'f'))
3244 while (!isspace (*dbp
))
3246 /* Skip over open parens and white space */
3247 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
3251 if (dbp
[0] == '(' &&
3252 (dbp
[1] == 'S' || dbp
[1] == 's') &&
3253 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3254 (dbp
[3] == 'T' || dbp
[3] == 't') &&
3255 (dbp
[4] == '!' || dbp
[4] == '!') &&
3258 while (!isspace (*dbp
))
3260 /* Skip over white space */
3261 while (isspace (*dbp
))
3275 /* Go till you get to white space or a syntactic break */
3277 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
3280 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3281 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3284 /* Find tags in TeX and LaTeX input files. */
3286 /* TEX_toktab is a table of TeX control sequences that define tags.
3287 Each TEX_tabent records one such control sequence.
3288 CONVERT THIS TO USE THE Stab TYPE!! */
3295 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
3297 /* Default set of control sequences to put into TEX_toktab.
3298 The value of environment var TEXTAGS is prepended to this. */
3300 char *TEX_defenv
= "\
3301 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3302 :part:appendix:entry:index";
3305 struct TEX_tabent
*TEX_decode_env ();
3307 #if TeX_named_tokens
3311 char TEX_esc
= '\\';
3312 char TEX_opgrp
= '{';
3313 char TEX_clgrp
= '}';
3316 * TeX/LaTeX scanning loop.
3327 /* Select either \ or ! as escape character. */
3330 /* Initialize token table once from environment. */
3332 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
3335 { /* Scan each line in file */
3337 linecharno
= charno
;
3338 charno
+= readline (&lb
, inf
);
3341 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
3347 linecharno
+= dbp
- lasthit
;
3349 i
= TEX_Token (lasthit
);
3353 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3354 #if TeX_named_tokens
3355 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
3357 break; /* We only save a line once */
3363 #define TEX_LESC '\\'
3364 #define TEX_SESC '!'
3367 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3368 chars accordingly. */
3375 while ((c
= getc (inf
)) != EOF
)
3377 /* Skip to next line if we hit the TeX comment char. */
3381 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3400 /* Read environment and prepend it to the default string.
3401 Build token table. */
3403 TEX_decode_env (evarname
, defenv
)
3407 register char *env
, *p
;
3409 struct TEX_tabent
*tab
;
3412 /* Append default string to environment. */
3413 env
= getenv (evarname
);
3417 env
= concat (env
, defenv
, "");
3419 /* Allocate a token table */
3420 for (size
= 1, p
= env
; p
;)
3421 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3423 /* Add 1 to leave room for null terminator. */
3424 tab
= xnew (size
+ 1, struct TEX_tabent
);
3426 /* Unpack environment string into token table. Be careful about */
3427 /* zero-length strings (leading ':', "::" and trailing ':') */
3430 p
= etags_strchr (env
, ':');
3431 if (!p
) /* End of environment string. */
3432 p
= env
+ strlen (env
);
3434 { /* Only non-zero strings. */
3435 tab
[i
].name
= savenstr (env
, p
- env
);
3436 tab
[i
].len
= strlen (tab
[i
].name
);
3443 tab
[i
].name
= NULL
; /* Mark end of table. */
3451 #if TeX_named_tokens
3452 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3453 The name being defined actually starts at (NAME + LEN + 1).
3454 But we seem to include the TeX command in the tag name. */
3456 TEX_getit (name
, len
)
3460 char *p
= name
+ len
;
3465 /* Let tag name extend to next group close (or end of line) */
3466 while (*p
&& *p
!= TEX_clgrp
)
3468 pfnote (savenstr (name
, p
-name
), TRUE
,
3469 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3473 /* If the text at CP matches one of the tag-defining TeX command names,
3474 return the pointer to the first occurrence of that command in TEX_toktab.
3475 Otherwise return -1.
3476 Keep the capital `T' in `Token' for dumb truncating compilers
3477 (this distinguishes it from `TEX_toktab' */
3484 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3485 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3491 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
3493 * Assumes that the predicate starts at column 0.
3494 * Only the first clause of a predicate is added.
3497 Prolog_functions (inf
)
3501 void prolog_skip_comment ();
3518 linecharno
+= charno
;
3519 charno
= readline (&lb
, inf
);
3521 if (dbp
[0] == '\0') /* Empty line */
3523 else if (isspace (dbp
[0])) /* Not a predicate */
3525 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3526 prolog_skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3527 else if (len
= prolog_pred (dbp
, last
))
3529 /* Predicate. Store the function name so that we only
3530 * generates a tag for the first clause. */
3532 last
= xnew(len
+ 1, char);
3533 else if (len
+ 1 > allocated
)
3534 last
= (char *) xrealloc(last
, len
+ 1);
3535 allocated
= len
+ 1;
3536 strncpy (last
, dbp
, len
);
3544 prolog_skip_comment (plb
, inf
)
3545 struct linebuffer
*plb
;
3552 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3553 if (cp
[0] == '*' && cp
[1] == '/')
3556 linecharno
+= readline (plb
, inf
);
3562 * A predicate definition is added if it matches:
3563 * <beginning of line><Prolog Atom><whitespace>(
3565 * It is added to the tags database if it doesn't match the
3566 * name of the previous clause header.
3568 * Return the size of the name of the predicate, or 0 if no header
3572 prolog_pred (s
, last
)
3574 char *last
; /* Name of last clause. */
3582 pos
= prolog_atom(s
, 0);
3587 pos
+= prolog_white(s
, pos
);
3589 if ((s
[pos
] == '(') || (s
[pos
] == '.'))
3594 /* Save only the first clause. */
3595 if ((last
== NULL
) ||
3596 (len
!= strlen(last
)) ||
3597 (strncmp(s
, last
, len
) != 0))
3599 pfnote ((CTAGS
) ? savenstr (s
, len
) : NULL
, TRUE
,
3600 s
, pos
, lineno
, linecharno
);
3608 * Consume a Prolog atom.
3609 * Return the number of bytes consumed, or -1 if there was an error.
3611 * A prolog atom, in this context, could be one of:
3612 * - An alphanumeric sequence, starting with a lower case letter.
3613 * - A quoted arbitrary string. Single quotes can escape themselves.
3614 * Backslash quotes everything.
3617 prolog_atom (s
, pos
)
3625 if (islower(s
[pos
]) || (s
[pos
] == '_'))
3627 /* The atom is unquoted. */
3629 while (isalnum(s
[pos
]) || (s
[pos
] == '_'))
3633 return pos
- origpos
;
3635 else if (s
[pos
] == '\'')
3646 pos
++; /* A double quote */
3648 else if (s
[pos
] == '\0')
3649 /* Multiline quoted atoms are ignored. */
3651 else if (s
[pos
] == '\\')
3653 if (s
[pos
+1] == '\0')
3660 return pos
- origpos
;
3666 /* Consume whitespace. Return the number of bytes eaten. */
3668 prolog_white (s
, pos
)
3676 while (isspace(s
[pos
]))
3679 return pos
- origpos
;
3683 * Support for Erlang -- Anders Lindgren, Feb 1996.
3685 * Generates tags for functions, defines, and records.
3687 * Assumes that Erlang functions start at column 0.
3690 Erlang_functions (inf
)
3694 void erlang_attribute ();
3711 linecharno
+= charno
;
3712 charno
= readline (&lb
, inf
);
3714 if (dbp
[0] == '\0') /* Empty line */
3716 else if (isspace (dbp
[0])) /* Not function nor attribute */
3718 else if (dbp
[0] == '%') /* comment */
3720 else if (dbp
[0] == '"') /* Sometimes, strings start in column one */
3722 else if (dbp
[0] == '-') /* attribute, e.g. "-define" */
3724 erlang_attribute(dbp
);
3727 else if (len
= erlang_func (dbp
, last
))
3730 * Function. Store the function name so that we only
3731 * generates a tag for the first clause.
3734 last
= xnew(len
+ 1, char);
3735 else if (len
+ 1 > allocated
)
3736 last
= (char *) xrealloc(last
, len
+ 1);
3737 allocated
= len
+ 1;
3738 strncpy (last
, dbp
, len
);
3746 * A function definition is added if it matches:
3747 * <beginning of line><Erlang Atom><whitespace>(
3749 * It is added to the tags database if it doesn't match the
3750 * name of the previous clause header.
3752 * Return the size of the name of the function, or 0 if no function
3756 erlang_func (s
, last
)
3758 char *last
; /* Name of last clause. */
3761 int erlang_white ();
3766 pos
= erlang_atom(s
, 0);
3771 pos
+= erlang_white(s
, pos
);
3773 if (s
[pos
++] == '(')
3775 /* Save only the first clause. */
3776 if ((last
== NULL
) ||
3777 (len
!= strlen(last
)) ||
3778 (strncmp(s
, last
, len
) != 0))
3780 pfnote ((CTAGS
) ? savenstr (s
, len
) : NULL
, TRUE
,
3781 s
, pos
, lineno
, linecharno
);
3790 * Handle attributes. Currently, tags are generated for defines
3793 * They are on the form:
3794 * -define(foo, bar).
3795 * -define(Foo(M, N), M+N).
3796 * -record(graph, {vtab = notable, cyclic = true}).
3799 erlang_attribute (s
)
3803 int erlang_white ();
3808 if ((strncmp(s
, "-define", 7) == 0) ||
3809 (strncmp(s
, "-record", 7) == 0))
3812 pos
+= erlang_white(s
, pos
);
3814 if (s
[pos
++] == '(')
3816 pos
+= erlang_white(s
, pos
);
3818 if (len
= erlang_atom(s
, pos
))
3820 pfnote ((CTAGS
) ? savenstr (& s
[pos
], len
) : NULL
, TRUE
,
3821 s
, pos
+ len
, lineno
, linecharno
);
3830 * Consume an Erlang atom (or variable).
3831 * Return the number of bytes consumed, or -1 if there was an error.
3834 erlang_atom (s
, pos
)
3842 if (isalpha (s
[pos
]) || s
[pos
] == '_')
3844 /* The atom is unquoted. */
3846 while (isalnum (s
[pos
]) || s
[pos
] == '_')
3848 return pos
- origpos
;
3850 else if (s
[pos
] == '\'')
3861 else if (s
[pos
] == '\0')
3862 /* Multiline quoted atoms are ignored. */
3864 else if (s
[pos
] == '\\')
3866 if (s
[pos
+1] == '\0')
3873 return pos
- origpos
;
3879 /* Consume whitespace. Return the number of bytes eaten */
3881 erlang_white (s
, pos
)
3889 while (isspace (s
[pos
]))
3892 return pos
- origpos
;
3895 #ifdef ETAGS_REGEXPS
3896 /* Take a string like "/blah/" and turn it into "blah", making sure
3897 that the first and last characters are the same, and handling
3898 quoted separator characters. Actually, stops on the occurrence of
3899 an unquoted separator. Also turns "\t" into a Tab character.
3900 Returns pointer to terminating separator. Works in place. Null
3901 terminates name string. */
3903 scan_separators (name
)
3907 char *copyto
= name
;
3908 logical quoted
= FALSE
;
3910 for (++name
; *name
!= '\0'; ++name
)
3916 else if (*name
== sep
)
3920 /* Something else is quoted, so preserve the quote. */
3926 else if (*name
== '\\')
3928 else if (*name
== sep
)
3934 /* Terminate copied string. */
3939 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3940 expression, into a real regular expression by compiling it. */
3942 add_regex (regexp_pattern
)
3943 char *regexp_pattern
;
3947 struct re_pattern_buffer
*patbuf
;
3949 if (regexp_pattern
== NULL
)
3951 /* Remove existing regexps. */
3957 if (regexp_pattern
[0] == '\0')
3959 error ("missing regexp", 0);
3962 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3964 error ("%s: unterminated regexp", regexp_pattern
);
3967 name
= scan_separators (regexp_pattern
);
3968 if (regexp_pattern
[0] == '\0')
3970 error ("null regexp", 0);
3973 (void) scan_separators (name
);
3975 patbuf
= xnew (1, struct re_pattern_buffer
);
3976 patbuf
->translate
= NULL
;
3977 patbuf
->fastmap
= NULL
;
3978 patbuf
->buffer
= NULL
;
3979 patbuf
->allocated
= 0;
3981 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3984 error ("%s while compiling pattern", err
);
3989 if (num_patterns
== 1)
3990 patterns
= xnew (1, struct pattern
);
3992 patterns
= ((struct pattern
*)
3994 (num_patterns
* sizeof (struct pattern
))));
3995 patterns
[num_patterns
- 1].pattern
= patbuf
;
3996 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3997 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
4001 * Do the substitutions indicated by the regular expression and
4005 substitute (in
, out
, regs
)
4007 struct re_registers
*regs
;
4009 char *result
= NULL
, *t
;
4012 /* Pass 1: figure out how much size to allocate. */
4013 for (t
= out
; *t
; ++t
)
4020 fprintf (stderr
, "%s: pattern substitution ends prematurely\n",
4027 size
+= regs
->end
[dig
] - regs
->start
[dig
];
4032 /* Allocate space and do the substitutions. */
4033 result
= xnew (size
+ 1, char);
4042 /* Using "dig2" satisfies my debugger. Bleah. */
4043 int dig2
= *out
- '0';
4044 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
4045 regs
->end
[dig2
] - regs
->start
[dig2
]);
4046 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
4049 result
[size
++] = *out
;
4052 result
[size
++] = *out
;
4054 result
[size
] = '\0';
4059 #endif /* ETAGS_REGEXPS */
4060 /* Initialize a linebuffer for use */
4062 initbuffer (linebuffer
)
4063 struct linebuffer
*linebuffer
;
4065 linebuffer
->size
= 200;
4066 linebuffer
->buffer
= xnew (200, char);
4070 * Read a line of text from `stream' into `linebuffer'.
4071 * Return the number of characters read from `stream',
4072 * which is the length of the line including the newline, if any.
4075 readline_internal (linebuffer
, stream
)
4076 struct linebuffer
*linebuffer
;
4077 register FILE *stream
;
4079 char *buffer
= linebuffer
->buffer
;
4080 register char *p
= linebuffer
->buffer
;
4081 register char *pend
;
4084 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
4088 register int c
= getc (stream
);
4091 linebuffer
->size
*= 2;
4092 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
4093 p
+= buffer
- linebuffer
->buffer
;
4094 pend
= buffer
+ linebuffer
->size
;
4095 linebuffer
->buffer
= buffer
;
4105 if (p
> buffer
&& p
[-1] == '\r')
4120 return p
- buffer
+ chars_deleted
;
4124 * Like readline_internal, above, but try to match the input
4125 * line against any existing regular expressions.
4128 readline (linebuffer
, stream
)
4129 struct linebuffer
*linebuffer
;
4132 /* Read new line. */
4133 long result
= readline_internal (linebuffer
, stream
);
4134 #ifdef ETAGS_REGEXPS
4137 /* Match against all listed patterns. */
4138 for (i
= 0; i
< num_patterns
; ++i
)
4140 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
4141 (int)result
, 0, &patterns
[i
].regs
);
4146 if (!patterns
[i
].error_signaled
)
4148 error ("error while matching pattern %d", i
);
4149 patterns
[i
].error_signaled
= TRUE
;
4156 /* Match occurred. Construct a tag. */
4157 if (patterns
[i
].name_pattern
[0] != '\0')
4159 /* Make a named tag. */
4160 char *name
= substitute (linebuffer
->buffer
,
4161 patterns
[i
].name_pattern
,
4165 linebuffer
->buffer
, match
, lineno
, linecharno
);
4169 /* Make an unnamed tag. */
4171 linebuffer
->buffer
, match
, lineno
, linecharno
);
4176 #endif /* ETAGS_REGEXPS */
4182 * Read a file, but do no processing. This is used to do regexp
4183 * matching on files that have no language defined.
4186 just_read_file (inf
)
4195 linecharno
= charno
;
4196 charno
+= readline (&lb
, inf
) + 1;
4202 * Return a pointer to a space of size strlen(cp)+1 allocated
4203 * with xnew where the string CP has been copied.
4209 return savenstr (cp
, strlen (cp
));
4213 * Return a pointer to a space of size LEN+1 allocated with xnew where
4214 * the string CP has been copied for at most the first LEN characters.
4223 dp
= xnew (len
+ 1, char);
4224 strncpy (dp
, cp
, len
);
4230 * Return the ptr in sp at which the character c last
4231 * appears; NULL if not found
4233 * Identical to System V strrchr, included for portability.
4236 etags_strrchr (sp
, c
)
4237 register char *sp
, c
;
4252 * Return the ptr in sp at which the character c first
4253 * appears; NULL if not found
4255 * Identical to System V strchr, included for portability.
4258 etags_strchr (sp
, c
)
4259 register char *sp
, c
;
4269 /* Print error message and exit. */
4287 suggest_asking_for_help ()
4289 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
4294 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
4299 fprintf (stderr
, "%s: ", progname
);
4300 fprintf (stderr
, s1
, s2
);
4301 fprintf (stderr
, "\n");
4304 /* Return a newly-allocated string whose contents
4305 concatenate those of s1, s2, s3. */
4310 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
4311 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
4313 strcpy (result
, s1
);
4314 strcpy (result
+ len1
, s2
);
4315 strcpy (result
+ len1
+ len2
, s3
);
4316 result
[len1
+ len2
+ len3
] = '\0';
4321 /* Does the same work as the system V getcwd, but does not need to
4322 guess the buffer size in advance. */
4327 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
4330 for (p
= path
; *p
!= '\0'; p
++)
4336 return strdup (path
);
4337 #else /* not MSDOS */
4340 char *path
= xnew (bufsize
, char);
4342 while (getcwd (path
, bufsize
) == NULL
)
4344 if (errno
!= ERANGE
)
4347 path
= xnew (bufsize
, char);
4351 #else /* not MSDOS and not HAVE_GETCWD */
4352 struct linebuffer path
;
4356 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
4357 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
4362 #endif /* not HAVE_GETCWD */
4363 #endif /* not MSDOS */
4366 /* Return a newly allocated string containing the filename
4367 of FILE relative to the absolute directory DIR (which
4368 should end with a slash). */
4370 relative_filename (file
, dir
)
4373 char *fp
, *dp
, *abs
, *res
;
4375 /* Find the common root of file and dir. */
4376 abs
= absolute_filename (file
, cwd
);
4379 while (*fp
++ == *dp
++)
4388 /* Build a sequence of "../" strings for the resulting relative filename. */
4389 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
4391 dp
= etags_strchr (dp
+ 1, '/'))
4393 res
= concat (res
, "../", "");
4396 /* Add the filename relative to the common root of file and dir. */
4397 res
= concat (res
, fp
+ 1, "");
4403 /* Return a newly allocated string containing the
4404 absolute filename of FILE given CWD (which should
4405 end with a slash). */
4407 absolute_filename (file
, cwd
)
4410 char *slashp
, *cp
, *res
;
4412 if (absolutefn (file
))
4413 res
= concat (file
, "", "");
4415 /* We don't support non-absolute filenames with a drive
4416 letter, like `d:NAME' (it's too much hassle). */
4417 else if (file
[1] == ':')
4418 fatal ("%s: relative filenames with drive letters not supported", file
);
4421 res
= concat (cwd
, file
, "");
4423 /* Delete the "/dirname/.." and "/." substrings. */
4424 slashp
= etags_strchr (res
, '/');
4425 while (slashp
!= NULL
&& slashp
[0] != '\0')
4427 if (slashp
[1] == '.')
4429 if (slashp
[2] == '.'
4430 && (slashp
[3] == '/' || slashp
[3] == '\0'))
4435 while (cp
>= res
&& !absolutefn (cp
));
4438 strcpy (cp
, slashp
+ 3);
4441 /* Under MSDOS and NT we get `d:/NAME' as absolute
4442 filename, so the luser could say `d:/../NAME'.
4443 We silently treat this as `d:/NAME'. */
4444 else if (cp
[1] == ':')
4445 strcpy (cp
+ 3, slashp
+ 4);
4447 else /* else (cp == res) */
4449 if (slashp
[3] != '\0')
4450 strcpy (cp
, slashp
+ 4);
4457 else if (slashp
[2] == '/' || slashp
[2] == '\0')
4459 strcpy (slashp
, slashp
+ 2);
4464 slashp
= etags_strchr (slashp
+ 1, '/');
4470 /* Return a newly allocated string containing the absolute
4471 filename of dir where FILE resides given CWD (which should
4472 end with a slash). */
4474 absolute_dirname (file
, cwd
)
4482 for (p
= file
; *p
!= '\0'; p
++)
4487 slashp
= etags_strrchr (file
, '/');
4492 res
= absolute_filename (file
, cwd
);
4498 /* Like malloc but get fatal error if memory is exhausted. */
4503 long *result
= (long *) malloc (size
);
4505 fatal ("virtual memory exhausted", 0);
4510 xrealloc (ptr
, size
)
4514 long *result
= (long *) realloc (ptr
, size
);
4516 fatal ("virtual memory exhausted");