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` (pot@cnuce.cnr.it) is the current maintainer.
34 char pot_etags_version
[] = "@(#) pot revision number is 11.53";
45 #include <sys/param.h>
52 #define MAXPATHLEN _MAX_PATH
57 /* On some systems, Emacs defines static as nothing for the sake
58 of unexec. We don't want that here since we don't use unexec. */
68 #include <sys/types.h>
71 #if !defined (S_ISREG) && defined (S_IFREG)
72 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
79 #endif /* ETAGS_REGEXPS */
81 /* Define CTAGS to make the program "ctags" compatible with the usual one.
82 Let it undefined to make the program "etags", which makes emacs-style
83 tag tables and tags typedefs, #defines and struct/union/enum by default. */
91 /* Exit codes for success and failure. */
101 #define C_PLPL 0x00001 /* C++ */
102 #define C_STAR 0x00003 /* C* */
103 #define YACC 0x10000 /* yacc file */
105 #define streq(s,t) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strcmp(s,t))
106 #define strneq(s,t,n) ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strncmp(s,t,n))
108 #define lowcase(c) tolower ((unsigned char)c)
110 #define iswhite(arg) (_wht[arg]) /* T if char is white */
111 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
112 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
113 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
116 # define absolutefn(fn) (fn[0] == '/' || (isalpha (fn[0]) && fn[1] == ':'))
118 # define absolutefn(fn) (fn[0] == '/')
123 * xnew -- allocate storage
125 * SYNOPSIS: Type *xnew (int n, Type);
127 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
132 { /* sorting structure */
133 char *name
; /* function or type name */
134 char *file
; /* file name */
135 logical is_func
; /* use pattern or line no */
136 logical been_warned
; /* set if noticed dup */
137 int lno
; /* line number tag is on */
138 long cno
; /* character number line starts on */
139 char *pat
; /* search pattern */
140 struct nd_st
*left
, *right
; /* left and right sons */
143 extern char *getenv ();
146 char *savenstr (), *savestr ();
147 char *etags_strchr (), *etags_strrchr ();
148 char *etags_getcwd ();
149 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
150 long *xmalloc (), *xrealloc ();
152 typedef void Lang_function ();
153 #if FALSE /* many compilers barf on this */
154 Lang_function Asm_labels
;
155 Lang_function default_C_entries
;
156 Lang_function C_entries
;
157 Lang_function Cplusplus_entries
;
158 Lang_function Cstar_entries
;
159 Lang_function Fortran_functions
;
160 Lang_function Yacc_entries
;
161 Lang_function Lisp_functions
;
162 Lang_function Pascal_functions
;
163 Lang_function Perl_functions
;
164 Lang_function Prolog_functions
;
165 Lang_function Scheme_functions
;
166 Lang_function TeX_functions
;
167 Lang_function just_read_file
;
168 #else /* so let's write it this way */
171 void default_C_entries ();
172 void plain_C_entries ();
173 void Cplusplus_entries ();
174 void Cstar_entries ();
175 void Fortran_functions ();
176 void Yacc_entries ();
177 void Lisp_functions ();
178 void Pascal_functions ();
179 void Perl_functions ();
180 void Prolog_functions ();
181 void Scheme_functions ();
182 void TeX_functions ();
183 void just_read_file ();
186 Lang_function
*get_language_from_name ();
187 Lang_function
*get_language_from_interpreter ();
188 Lang_function
*get_language_from_suffix ();
189 int total_size_of_entries ();
191 long readline_internal ();
197 void suggest_asking_for_help ();
198 void fatal (), pfatal ();
199 void find_entries ();
205 void process_file ();
210 char searchar
= '/'; /* use /.../ searches */
212 int lineno
; /* line number of current line */
213 long charno
; /* current character number */
215 long linecharno
; /* charno of start of line; not used by C,
216 but by every other language. */
218 char *curfile
; /* current input file name */
219 char *tagfile
; /* output file */
220 char *progname
; /* name this program was invoked with */
221 char *cwd
; /* current working directory */
222 char *tagfiledir
; /* directory of tagfile */
224 FILE *tagf
; /* ioptr for tags file */
225 NODE
*head
; /* the head of the binary tree of tags */
228 * A `struct linebuffer' is a structure which holds a line of text.
229 * `readline' reads a line from a stream into a linebuffer and works
230 * regardless of the length of the line.
232 #define GROW_LINEBUFFER(buf,toksize) \
233 while (buf.size < toksize) \
234 buf.buffer = (char *) xrealloc (buf.buffer, buf.size *= 2)
241 struct linebuffer lb
; /* the current line */
242 struct linebuffer token_name
; /* used by C_entries as a temporary area */
246 struct linebuffer lb
; /* used by C_entries instead of lb */
249 /* boolean "functions" (see init) */
250 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
253 *white
= " \f\t\n\013",
254 /* token ending chars */
255 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
256 /* token starting chars */
257 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
258 /* valid in-token chars */
259 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
261 logical append_to_tagfile
; /* -a: append to tags */
262 /* The following three default to TRUE for etags, but to FALSE for ctags. */
263 logical typedefs
; /* -t: create tags for typedefs */
264 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
265 /* 0 struct/enum/union decls, and C++ */
266 /* member functions. */
267 logical constantypedefs
; /* -d: create tags for C #define and enum */
268 /* constants. Enum consts not implemented. */
269 /* -D: opposite of -d. Default under ctags. */
270 logical update
; /* -u: update tags */
271 logical vgrind_style
; /* -v: create vgrind style index output */
272 logical no_warnings
; /* -w: suppress warnings */
273 logical cxref_style
; /* -x: create cxref style output */
274 logical cplusplus
; /* .[hc] means C++, not C */
275 logical noindentypedefs
; /* -I: ignore indentation in C */
277 struct option longopts
[] =
279 { "append", no_argument
, NULL
, 'a' },
280 { "backward-search", no_argument
, NULL
, 'B' },
281 { "c++", no_argument
, NULL
, 'C' },
282 { "cxref", no_argument
, NULL
, 'x' },
283 { "defines", no_argument
, NULL
, 'd' },
284 { "help", no_argument
, NULL
, 'h' },
285 { "help", no_argument
, NULL
, 'H' },
286 { "ignore-indentation", no_argument
, NULL
, 'I' },
287 { "include", required_argument
, NULL
, 'i' },
288 { "language", required_argument
, NULL
, 'l' },
289 { "no-defines", no_argument
, NULL
, 'D' },
290 { "no-regex", no_argument
, NULL
, 'R' },
291 { "no-warn", no_argument
, NULL
, 'w' },
292 { "output", required_argument
, NULL
, 'o' },
293 { "regex", required_argument
, NULL
, 'r' },
294 { "typedefs", no_argument
, NULL
, 't' },
295 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
296 { "update", no_argument
, NULL
, 'u' },
297 { "version", no_argument
, NULL
, 'V' },
298 { "vgrind", no_argument
, NULL
, 'v' },
303 /* Structure defining a regular expression. Elements are
304 the compiled pattern, and the name string. */
307 struct re_pattern_buffer
*pattern
;
308 struct re_registers regs
;
310 logical error_signaled
;
313 /* Number of regexps found. */
314 int num_patterns
= 0;
316 /* Array of all regexps. */
317 struct pattern
*patterns
= NULL
;
318 #endif /* ETAGS_REGEXPS */
324 /* Non-NULL if language fixed. */
325 Lang_function
*lang_func
= NULL
;
328 char *Asm_suffixes
[] = { "a", /* Unix assembler */
329 "asm", /* Microcontroller assembly */
330 "def", /* BSO/Tasking definition includes */
331 "inc", /* Microcontroller include files */
332 "ins", /* Microcontroller include files */
333 "s", "sa", /* Unix assembler */
334 "src", /* BSO/Tasking C compiler output */
338 /* Note that .c and .h can be considered C++, if the --c++ flag was
339 given. That is why default_C_entries is called here. */
340 char *default_C_suffixes
[] =
343 /* .M is for Objective C++ files. */
344 char *Cplusplus_suffixes
[] =
345 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL
};
347 char *Cstar_suffixes
[] =
348 { "cs", "hs", NULL
};
350 char *Fortran_suffixes
[] =
351 { "F", "f", "f90", "for", NULL
};
353 char *Lisp_suffixes
[] =
354 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL
};
356 char *Pascal_suffixes
[] =
357 { "p", "pas", NULL
};
359 char *Perl_suffixes
[] =
360 { "pl", "pm", NULL
};
361 char *Perl_interpreters
[] =
362 { "perl", "@PERL@", NULL
};
364 char *plain_C_suffixes
[] =
365 { "pc", /* Pro*C file */
366 "m", /* Objective C file */
367 "lm", /* Objective lex file */
370 char *Prolog_suffixes
[] =
373 /* Can't do the `SCM' or `scm' prefix with a version number. */
374 char *Scheme_suffixes
[] =
375 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL
};
377 char *TeX_suffixes
[] =
378 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL
};
380 char *Yacc_suffixes
[] =
381 { "y", "ym", NULL
}; /* .ym is Objective yacc file */
383 /* Table of language names and corresponding functions, file suffixes
384 and interpreter names.
385 It is ok for a given function to be listed under more than one
386 name. I just didn't. */
390 Lang_function
*function
;
395 struct lang_entry lang_names
[] =
397 { "asm", Asm_labels
, Asm_suffixes
, NULL
},
398 { "c", default_C_entries
, default_C_suffixes
, NULL
},
399 { "c++", Cplusplus_entries
, Cplusplus_suffixes
, NULL
},
400 { "c*", Cstar_entries
, Cstar_suffixes
, NULL
},
401 { "fortran", Fortran_functions
, Fortran_suffixes
, NULL
},
402 { "lisp", Lisp_functions
, Lisp_suffixes
, NULL
},
403 { "pascal", Pascal_functions
, Pascal_suffixes
, NULL
},
404 { "perl", Perl_functions
, Perl_suffixes
, Perl_interpreters
},
405 { "proc", plain_C_entries
, plain_C_suffixes
, NULL
},
406 { "prolog", Prolog_functions
, Prolog_suffixes
, NULL
},
407 { "scheme", Scheme_functions
, Scheme_suffixes
, NULL
},
408 { "tex", TeX_functions
, TeX_suffixes
, NULL
},
409 { "yacc", Yacc_entries
, Yacc_suffixes
, NULL
},
410 { "auto", NULL
}, /* default guessing scheme */
411 { "none", just_read_file
}, /* regexp matching only */
412 { NULL
, NULL
} /* end of list */
417 print_language_names ()
419 struct lang_entry
*lang
;
422 puts ("\nThese are the currently supported languages, along with the\n\
423 default file name suffixes:");
424 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
426 printf ("\t%s\t", lang
->name
);
427 if (lang
->suffixes
!= NULL
)
428 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
429 printf (" .%s", *ext
);
432 puts ("Where `auto' means use default language for files based on file\n\
433 name suffix, and `none' means only do regexp processing on files.\n\
434 If no language is specified and no matching suffix is found,\n\
435 the first line of the file is read for a sharp-bang (#!) sequence\n\
436 followed by the name of an interpreter. If no such sequence is found,\n\
437 Fortran is tried first; if no tags are found, C is tried next.");
441 # define VERSION "19"
446 printf ("%s for Emacs version %s\n", (CTAGS
) ? "ctags" : "etags", VERSION
);
454 printf ("These are the options accepted by %s. You may use unambiguous\n\
455 abbreviations for the long option names. A - as file name means read\n\
456 names from stdin.\n\n", progname
);
458 puts ("-a, --append\n\
459 Append tag entries to existing tags file.");
462 puts ("-B, --backward-search\n\
463 Write the search commands for the tag entries using '?', the\n\
464 backward-search command instead of '/', the forward-search command.");
467 Treat files whose name suffix defaults to C language as C++ files.");
470 puts ("-d, --defines\n\
471 Create tag entries for constant C #defines, too.");
473 puts ("-D, --no-defines\n\
474 Don't create tag entries for constant C #defines. This makes\n\
475 the tags file smaller.");
479 puts ("-i FILE, --include=FILE\n\
480 Include a note in tag file indicating that, when searching for\n\
481 a tag, one should also consult the tags file FILE after\n\
482 checking the current file.");
483 puts ("-l LANG, --language=LANG\n\
484 Force the following files to be considered as written in the\n\
485 named language up to the next --language=LANG option.");
489 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
490 Make a tag for each line matching pattern REGEXP in the\n\
491 following files. REGEXP is anchored (as if preceded by ^).\n\
492 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
493 named tags can be created with:\n\
494 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
495 puts ("-R, --no-regex\n\
496 Don't create tags from regexps for the following files.");
497 #endif /* ETAGS_REGEXPS */
498 puts ("-o FILE, --output=FILE\n\
499 Write the tags to FILE.");
500 puts ("-I, --ignore-indentation\n\
501 Don't rely on indentation quite as much as normal. Currently,\n\
502 this means not to assume that a closing brace in the first\n\
503 column is the final brace of a function or structure\n\
504 definition in C and C++.");
508 puts ("-t, --typedefs\n\
509 Generate tag entries for C typedefs.");
510 puts ("-T, --typedefs-and-c++\n\
511 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
512 and C++ member functions.");
513 puts ("-u, --update\n\
514 Update the tag entries for the given files, leaving tag\n\
515 entries for other files in place. Currently, this is\n\
516 implemented by deleting the existing entries for the given\n\
517 files and then rewriting the new entries at the end of the\n\
518 tags file. It is often faster to simply rebuild the entire\n\
519 tag file than to use this.");
520 puts ("-v, --vgrind\n\
521 Generates an index of items intended for human consumption,\n\
522 similar to the output of vgrind. The index is sorted, and\n\
523 gives the page number of each item.");
524 puts ("-w, --no-warn\n\
525 Suppress warning messages about entries defined in multiple\n\
527 puts ("-x, --cxref\n\
528 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
529 The output uses line numbers instead of page numbers, but\n\
530 beyond that the differences are cosmetic; try both to see\n\
534 puts ("-V, --version\n\
535 Print the version of the program.\n\
537 Print this help message.");
539 print_language_names ();
552 /* This structure helps us allow mixing of --lang and filenames. */
555 enum argument_type arg_type
;
557 Lang_function
*function
;
560 #ifdef VMS /* VMS specific functions */
564 /* This is a BUG! ANY arbitrary limit is a BUG!
565 Won't someone please fix this? */
566 #define MAX_FILE_SPEC_LEN 255
569 char body
[MAX_FILE_SPEC_LEN
+ 1];
573 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
574 returning in each successive call the next filename matching the input
575 spec. The function expects that each in_spec passed
576 to it will be processed to completion; in particular, up to and
577 including the call following that in which the last matching name
578 is returned, the function ignores the value of in_spec, and will
579 only start processing a new spec with the following call.
580 If an error occurs, on return out_spec contains the value
581 of in_spec when the error occurred.
583 With each successive filename returned in out_spec, the
584 function's return value is one. When there are no more matching
585 names the function returns zero. If on the first call no file
586 matches in_spec, or there is any other error, -1 is returned.
591 #define OUTSIZE MAX_FILE_SPEC_LEN
597 static long context
= 0;
598 static struct dsc$descriptor_s o
;
599 static struct dsc$descriptor_s i
;
600 static logical pass1
= TRUE
;
607 o
.dsc$a_pointer
= (char *) out
;
608 o
.dsc$w_length
= (short)OUTSIZE
;
609 i
.dsc$a_pointer
= in
;
610 i
.dsc$w_length
= (short)strlen(in
);
611 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
612 i
.dsc$b_class
= DSC$K_CLASS_S
;
613 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
614 o
.dsc$b_class
= DSC$K_CLASS_VS
;
616 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
618 out
->body
[out
->curlen
] = EOS
;
621 else if (status
== RMS$_NMF
)
625 strcpy(out
->body
, in
);
628 lib$
find_file_end(&context
);
634 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
635 name of each file specified by the provided arg expanding wildcards.
638 gfnames (arg
, p_error
)
642 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
644 switch (fn_exp (&filename
, arg
))
648 return filename
.body
;
654 return filename
.body
;
658 #ifndef OLD /* Newer versions of VMS do provide `system'. */
662 fprintf (stderr
, "system() function not implemented under VMS\n");
666 #define VERSION_DELIM ';'
667 char *massage_name (s
)
673 if (*s
== VERSION_DELIM
)
691 unsigned int nincluded_files
= 0;
692 char **included_files
= xnew (argc
, char *);
695 int current_arg
= 0, file_count
= 0;
696 struct linebuffer filename_lb
;
702 _fmode
= O_BINARY
; /* all of files are treated as binary files */
707 /* Allocate enough no matter what happens. Overkill, but each one
709 argbuffer
= xnew (argc
, argument
);
712 /* Set syntax for regular expression routines. */
713 re_set_syntax (RE_SYNTAX_EMACS
);
714 #endif /* ETAGS_REGEXPS */
717 * If etags, always find typedefs and structure tags. Why not?
718 * Also default is to find macro constants.
721 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
725 int opt
= getopt_long (argc
, argv
,
726 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
734 /* If getopt returns 0, then it has already processed a
735 long-named option. We should do nothing. */
739 /* This means that a filename has been seen. Record it. */
740 argbuffer
[current_arg
].arg_type
= at_filename
;
741 argbuffer
[current_arg
].what
= optarg
;
746 /* Common options. */
748 append_to_tagfile
= TRUE
;
754 constantypedefs
= TRUE
;
757 constantypedefs
= FALSE
;
759 case 'f': /* for compatibility with old makefiles */
763 fprintf (stderr
, "%s: -%c option may only be given once.\n",
765 suggest_asking_for_help ();
770 case 'S': /* for backward compatibility */
771 noindentypedefs
= TRUE
;
774 argbuffer
[current_arg
].function
= get_language_from_name (optarg
);
775 argbuffer
[current_arg
].arg_type
= at_language
;
780 argbuffer
[current_arg
].arg_type
= at_regexp
;
781 argbuffer
[current_arg
].what
= optarg
;
785 argbuffer
[current_arg
].arg_type
= at_regexp
;
786 argbuffer
[current_arg
].what
= NULL
;
789 #endif /* ETAGS_REGEXPS */
801 typedefs
= typedefs_and_cplusplus
= TRUE
;
806 included_files
[nincluded_files
++] = optarg
;
827 suggest_asking_for_help ();
831 for (; optind
< argc
; ++optind
)
833 argbuffer
[current_arg
].arg_type
= at_filename
;
834 argbuffer
[current_arg
].what
= argv
[optind
];
839 if (nincluded_files
== 0 && file_count
== 0)
841 fprintf (stderr
, "%s: No input files specified.\n", progname
);
842 suggest_asking_for_help ();
847 tagfile
= CTAGS
? "tags" : "TAGS";
849 cwd
= etags_getcwd (); /* the current working directory */
851 if (streq (tagfile
, "-"))
857 tagfiledir
= absolute_dirname (tagfile
, cwd
);
860 init (); /* set up boolean "functions" */
863 initbuffer (&token_name
);
864 initbuffer (&lbs
[0].lb
);
865 initbuffer (&lbs
[1].lb
);
866 initbuffer (&filename_lb
);
870 if (streq (tagfile
, "-"))
873 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
879 * Loop through files finding functions.
881 for (i
= 0; i
< current_arg
; ++i
)
883 switch (argbuffer
[i
].arg_type
)
886 lang_func
= argbuffer
[i
].function
;
890 add_regex (argbuffer
[i
].what
);
895 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
899 error ("Can't find file %s\n", this_file
);
904 this_file
= massage_name (this_file
);
907 this_file
= argbuffer
[i
].what
;
909 /* Input file named "-" means read file names from stdin
911 if (streq (this_file
, "-"))
912 while (readline_internal (&filename_lb
, stdin
) > 0)
913 process_file (filename_lb
.buffer
);
915 process_file (this_file
);
925 while (nincluded_files
-- > 0)
926 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
932 /* If CTAGS, we are here. process_file did not write the tags yet,
933 because we want them ordered. Let's do it now. */
936 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
946 for (i
= 0; i
< current_arg
; ++i
)
948 if (argbuffer
[i
].arg_type
!= at_filename
)
951 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
952 tagfile
, argbuffer
[i
].what
, tagfile
);
953 if (system (cmd
) != GOOD
)
954 fatal ("failed to execute shell command");
956 append_to_tagfile
= TRUE
;
959 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
968 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
976 * Return a Lang_function given the name.
979 get_language_from_name (name
)
982 struct lang_entry
*lang
;
985 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
987 if (streq (name
, lang
->name
))
988 return lang
->function
;
991 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
993 suggest_asking_for_help ();
995 /* This point should never be reached. The function should either
996 return a function pointer or never return. Note that a NULL
997 pointer cannot be considered as an error, as it means that the
998 language has not been explicitely imposed by the user ("auto"). */
999 return NULL
; /* avoid warnings from compiler */
1004 * Return a Lang_function given the interpreter name.
1007 get_language_from_interpreter (interpreter
)
1010 struct lang_entry
*lang
;
1013 if (interpreter
== NULL
)
1015 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1016 if (lang
->interpreters
!= NULL
)
1017 for (iname
= lang
->interpreters
; *iname
!= NULL
; iname
++)
1018 if (streq (*iname
, interpreter
))
1019 return lang
->function
;
1027 * Return a Lang_function given the file suffix.
1030 get_language_from_suffix (suffix
)
1033 struct lang_entry
*lang
;
1038 for (lang
= lang_names
; lang
->name
!= NULL
; lang
++)
1039 if (lang
->suffixes
!= NULL
)
1040 for (ext
= lang
->suffixes
; *ext
!= NULL
; ext
++)
1041 if (streq (*ext
, suffix
))
1042 return lang
->function
;
1049 * This routine is called on each file argument.
1055 struct stat stat_buf
;
1058 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1060 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1063 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1065 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1068 inf
= fopen (file
, "r");
1075 find_entries (file
, inf
);
1081 if (absolutefn (file
))
1083 /* file is an absolute filename. Canonicalise it. */
1084 filename
= absolute_filename (file
, cwd
);
1088 /* file is a filename relative to cwd. Make it relative
1089 to the directory of the tags file. */
1090 filename
= relative_filename (file
, tagfiledir
);
1092 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1101 * This routine sets up the boolean pseudo-functions which work
1102 * by setting boolean flags dependent upon the corresponding character
1103 * Every char which is NOT in that string is not a white char. Therefore,
1104 * all of the array "_wht" is set to FALSE, and then the elements
1105 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1106 * of a char is TRUE if it is the string "white", else FALSE.
1114 for (i
= 0; i
< 0177; i
++)
1115 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1116 for (sp
= white
; *sp
; sp
++)
1118 for (sp
= endtk
; *sp
; sp
++)
1120 for (sp
= intk
; *sp
; sp
++)
1122 for (sp
= begtk
; *sp
; sp
++)
1124 _wht
[0] = _wht
['\n'];
1125 _etk
[0] = _etk
['\n'];
1126 _btk
[0] = _btk
['\n'];
1127 _itk
[0] = _itk
['\n'];
1131 * This routine opens the specified file and calls the function
1132 * which finds the function and type definitions.
1135 find_entries (file
, inf
)
1140 Lang_function
*function
;
1141 NODE
*old_last_node
;
1142 extern NODE
*last_node
;
1145 /* Memory leakage here: the memory block pointed by curfile is never
1146 released. The amount of memory leaked here is the sum of the
1147 lengths of the input file names. */
1148 curfile
= savestr (file
);
1150 /* If user specified a language, use it. */
1151 function
= lang_func
;
1152 if (function
!= NULL
)
1159 cp
= etags_strrchr (file
, '.');
1163 function
= get_language_from_suffix (cp
);
1164 if (function
!= NULL
)
1172 /* Look for sharp-bang as the first two characters. */
1173 if (readline_internal (&lb
, inf
) > 2
1174 && lb
.buffer
[0] == '#'
1175 && lb
.buffer
[1] == '!')
1179 /* Set lp to point at the first char after the last slash in the
1180 line or, if no slashes, at the first nonblank. Then set cp to
1181 the first successive blank and terminate the string. */
1182 lp
= etags_strrchr (lb
.buffer
+2, '/');
1186 for (lp
= lb
.buffer
+2; *lp
!= '\0' && isspace (*lp
); lp
++)
1188 for (cp
= lp
; *cp
!= '\0' && !isspace (*cp
); cp
++)
1192 if (strlen (lp
) > 0)
1194 function
= get_language_from_interpreter (lp
);
1195 if (function
!= NULL
)
1206 old_last_node
= last_node
;
1207 Fortran_functions (inf
);
1209 /* No Fortran entries found. Try C. */
1210 if (old_last_node
== last_node
)
1213 default_C_entries (inf
);
1221 pfnote (name
, is_func
, linestart
, linelen
, lno
, cno
)
1222 char *name
; /* tag name, or NULL if unnamed */
1223 logical is_func
; /* tag is a function */
1224 char *linestart
; /* start of the line where tag is */
1225 int linelen
; /* length of the line where tag is */
1226 int lno
; /* line number */
1227 long cno
; /* character number */
1231 if (CTAGS
&& name
== NULL
)
1234 np
= xnew (1, NODE
);
1236 /* If ctags mode, change name "main" to M<thisfilename>. */
1237 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1239 register char *fp
= etags_strrchr (curfile
, '/');
1240 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1241 fp
= etags_strrchr (np
->name
, '.');
1242 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1247 np
->been_warned
= FALSE
;
1249 np
->is_func
= is_func
;
1251 /* Our char numbers are 0-base, because of C language tradition?
1252 ctags compatibility? old versions compatibility? I don't know.
1253 Anyway, since emacs's are 1-base we expect etags.el to take care
1254 of the difference. If we wanted to have 1-based numbers, we would
1255 uncomment the +1 below. */
1256 np
->cno
= cno
/* + 1 */ ;
1257 np
->left
= np
->right
= NULL
;
1258 if (CTAGS
&& !cxref_style
)
1260 if (strlen (linestart
) < 50)
1261 np
->pat
= concat (linestart
, "$", "");
1263 np
->pat
= savenstr (linestart
, 50);
1266 np
->pat
= savenstr (linestart
, linelen
);
1268 add_node (np
, &head
);
1273 * recurse on left children, iterate on right children.
1277 register NODE
*node
;
1281 register NODE
*node_right
= node
->right
;
1282 free_tree (node
->left
);
1283 if (node
->name
!= NULL
)
1286 free ((char *) node
);
1293 * Adds a node to the tree of nodes. In etags mode, we don't keep
1294 * it sorted; we just keep a linear list. In ctags mode, maintain
1295 * an ordered tree, with no attempt at balancing.
1297 * add_node is the only function allowed to add nodes, so it can
1300 NODE
*last_node
= NULL
;
1302 add_node (node
, cur_node_p
)
1303 NODE
*node
, **cur_node_p
;
1306 register NODE
*cur_node
= *cur_node_p
;
1308 if (cur_node
== NULL
)
1318 if (last_node
== NULL
)
1319 fatal ("internal error in add_node", 0);
1320 last_node
->right
= node
;
1326 dif
= strcmp (node
->name
, cur_node
->name
);
1329 * If this tag name matches an existing one, then
1330 * do not add the node, but maybe print a warning.
1334 if (streq (node
->file
, cur_node
->file
))
1338 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1339 node
->file
, lineno
, node
->name
);
1340 fprintf (stderr
, "Second entry ignored\n");
1343 else if (!cur_node
->been_warned
&& !no_warnings
)
1347 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1348 node
->file
, cur_node
->file
, node
->name
);
1349 cur_node
->been_warned
= TRUE
;
1354 /* Actually add the node */
1355 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1361 register NODE
*node
;
1368 /* Output subentries that precede this one */
1369 put_entries (node
->left
);
1371 /* Output this entry */
1375 if (node
->name
!= NULL
)
1376 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1377 node
->pat
, node
->name
, node
->lno
, node
->cno
);
1379 fprintf (tagf
, "%s\177%d,%d\n",
1380 node
->pat
, node
->lno
, node
->cno
);
1384 if (node
->name
== NULL
)
1385 error ("internal error: NULL name in ctags mode.", 0);
1390 fprintf (stdout
, "%s %s %d\n",
1391 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1393 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1394 node
->name
, node
->lno
, node
->file
, node
->pat
);
1398 fprintf (tagf
, "%s\t%s\t", node
->name
, node
->file
);
1402 putc (searchar
, tagf
);
1405 for (sp
= node
->pat
; *sp
; sp
++)
1407 if (*sp
== '\\' || *sp
== searchar
)
1411 putc (searchar
, tagf
);
1414 { /* a typedef; text pattern inadequate */
1415 fprintf (tagf
, "%d", node
->lno
);
1421 /* Output subentries that follow this one */
1422 put_entries (node
->right
);
1425 /* Length of a number's decimal representation. */
1433 for (; num
; num
/= 10)
1439 * Return total number of characters that put_entries will output for
1440 * the nodes in the subtree of the specified node. Works only if
1441 * we are not ctags, but called only in that case. This count
1442 * is irrelevant with the new tags.el, but is still supplied for
1443 * backward compatibility.
1446 total_size_of_entries (node
)
1447 register NODE
*node
;
1455 for (; node
; node
= node
->right
)
1457 /* Count left subentries. */
1458 total
+= total_size_of_entries (node
->left
);
1460 /* Count this entry */
1461 total
+= strlen (node
->pat
) + 1;
1462 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1463 if (node
->name
!= NULL
)
1464 total
+= 1 + strlen (node
->name
); /* \001name */
1471 * The C symbol tables.
1475 st_none
, st_C_objprot
, st_C_objimpl
, st_C_objend
, st_C_gnumacro
,
1476 st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1479 /* Feed stuff between (but not including) %[ and %] lines to:
1480 gperf -c -k1,3 -o -p -r -t
1482 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1484 @interface, 0, st_C_objprot
1485 @protocol, 0, st_C_objprot
1486 @implementation,0, st_C_objimpl
1487 @end, 0, st_C_objend
1488 class, C_PLPL, st_C_struct
1489 domain, C_STAR, st_C_struct
1490 union, 0, st_C_struct
1491 struct, 0, st_C_struct
1493 typedef, 0, st_C_typedef
1494 define, 0, st_C_define
1495 long, 0, st_C_typespec
1496 short, 0, st_C_typespec
1497 int, 0, st_C_typespec
1498 char, 0, st_C_typespec
1499 float, 0, st_C_typespec
1500 double, 0, st_C_typespec
1501 signed, 0, st_C_typespec
1502 unsigned, 0, st_C_typespec
1503 auto, 0, st_C_typespec
1504 void, 0, st_C_typespec
1505 extern, 0, st_C_typespec
1506 static, 0, st_C_typespec
1507 const, 0, st_C_typespec
1508 volatile, 0, st_C_typespec
1509 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1510 DEFUN, 0, st_C_gnumacro
1511 SYSCALL, 0, st_C_gnumacro
1512 ENTRY, 0, st_C_gnumacro
1513 PSEUDO, 0, st_C_gnumacro
1514 # These are defined inside C functions, so currently they are not met.
1515 # EXFUN used in glibc, DEFVAR_* in emacs.
1516 #EXFUN, 0, st_C_gnumacro
1517 #DEFVAR_, 0, st_C_gnumacro
1519 and replace lines between %< and %> with its output. */
1521 /* C code produced by gperf version 1.8.1 (K&R C version) */
1522 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1525 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1527 #define MIN_WORD_LENGTH 3
1528 #define MAX_WORD_LENGTH 15
1529 #define MIN_HASH_VALUE 7
1530 #define MAX_HASH_VALUE 63
1533 57 is the maximum key range
1541 static unsigned char hash_table
[] =
1543 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1544 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1545 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1546 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1547 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1548 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1549 63, 63, 63, 63, 17, 63, 63, 63, 4, 14,
1550 4, 63, 63, 63, 63, 63, 63, 63, 63, 63,
1551 8, 63, 63, 0, 23, 63, 63, 63, 63, 63,
1552 63, 63, 63, 63, 63, 63, 63, 28, 63, 28,
1553 10, 31, 27, 18, 63, 6, 63, 63, 26, 1,
1554 11, 2, 29, 63, 29, 16, 26, 13, 15, 63,
1555 63, 63, 63, 63, 63, 63, 63, 63,
1557 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1560 struct C_stab_entry
*
1561 in_word_set (str
, len
)
1566 static struct C_stab_entry wordlist
[] =
1568 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1569 {"SYSCALL", 0, st_C_gnumacro
},
1570 {"",}, {"",}, {"",}, {"",}, {"",},
1571 {"DEFUN", 0, st_C_gnumacro
},
1572 {"",}, {"",}, {"",},
1573 {"domain", C_STAR
, st_C_struct
},
1574 {"",}, {"",}, {"",}, {"",}, {"",},
1575 {"short", 0, st_C_typespec
},
1576 {"union", 0, st_C_struct
},
1577 {"void", 0, st_C_typespec
},
1579 {"PSEUDO", 0, st_C_gnumacro
},
1580 {"double", 0, st_C_typespec
},
1582 {"@end", 0, st_C_objend
},
1583 {"@implementation", 0, st_C_objimpl
},
1584 {"float", 0, st_C_typespec
},
1585 {"int", 0, st_C_typespec
},
1587 {"unsigned", 0, st_C_typespec
},
1588 {"@interface", 0, st_C_objprot
},
1590 {"signed", 0, st_C_typespec
},
1591 {"long", 0, st_C_typespec
},
1592 {"ENTRY", 0, st_C_gnumacro
},
1593 {"define", 0, st_C_define
},
1594 {"const", 0, st_C_typespec
},
1595 {"",}, {"",}, {"",},
1596 {"enum", 0, st_C_enum
},
1597 {"volatile", 0, st_C_typespec
},
1598 {"static", 0, st_C_typespec
},
1599 {"struct", 0, st_C_struct
},
1600 {"",}, {"",}, {"",},
1601 {"@protocol", 0, st_C_objprot
},
1603 {"auto", 0, st_C_typespec
},
1605 {"char", 0, st_C_typespec
},
1606 {"class", C_PLPL
, st_C_struct
},
1607 {"typedef", 0, st_C_typedef
},
1608 {"extern", 0, st_C_typespec
},
1611 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1613 register int key
= hash (str
, len
);
1615 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1617 register char *s
= wordlist
[key
].name
;
1619 if (*s
== *str
&& !strncmp (str
+ 1, s
+ 1, len
- 1))
1620 return &wordlist
[key
];
1628 C_symtype(str
, len
, c_ext
)
1633 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1635 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1641 * C functions are recognized using a simple finite automaton.
1642 * funcdef is its state variable.
1646 fnone
, /* nothing seen */
1647 ftagseen
, /* function-like tag seen */
1648 fstartlist
, /* just after open parenthesis */
1649 finlist
, /* in parameter list */
1650 flistseen
, /* after parameter list */
1651 fignore
/* before open brace */
1656 * typedefs are recognized using a simple finite automaton.
1657 * typdef is its state variable.
1661 tnone
, /* nothing seen */
1662 ttypedseen
, /* typedef keyword seen */
1663 tinbody
, /* inside typedef body */
1664 tend
, /* just before typedef tag */
1665 tignore
/* junk after typedef tag */
1670 * struct-like structures (enum, struct and union) are recognized
1671 * using another simple finite automaton. `structdef' is its state
1676 snone
, /* nothing seen yet */
1677 skeyseen
, /* struct-like keyword seen */
1678 stagseen
, /* struct-like tag seen */
1679 scolonseen
, /* colon seen after struct-like tag */
1680 sinbody
/* in struct body: recognize member func defs*/
1684 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1685 * struct tag, and structtype is the type of the preceding struct-like
1688 char *structtag
= "<uninited>";
1689 enum sym_type structtype
;
1692 * When objdef is different from onone, objtag is the name of the class.
1694 char *objtag
= "<uninited>";
1697 * Yet another little state machine to deal with preprocessor lines.
1701 dnone
, /* nothing seen */
1702 dsharpseen
, /* '#' seen as first char on line */
1703 ddefineseen
, /* '#' and 'define' seen */
1704 dignorerest
/* ignore rest of line */
1708 * State machine for Objective C protocols and implementations.
1712 onone
, /* nothing seen */
1713 oprotocol
, /* @interface or @protocol seen */
1714 oimplementation
, /* @implementations seen */
1715 otagseen
, /* class name seen */
1716 oparenseen
, /* parenthesis before category seen */
1717 ocatseen
, /* category name seen */
1718 oinbody
, /* in @implementation body */
1719 omethodsign
, /* in @implementation body, after +/- */
1720 omethodtag
, /* after method name */
1721 omethodcolon
, /* after method colon */
1722 omethodparm
, /* after method parameter */
1723 oignore
/* wait for @end */
1727 * Set this to TRUE, and the next token considered is called a function.
1728 * Used only for GNU emacs's function-defining macros.
1730 logical next_token_is_func
;
1733 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1738 * methodlen is the length of the method name stored in token_name.
1744 * checks to see if the current token is at the start of a
1745 * function, or corresponds to a typedef, or is a struct/union/enum
1748 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1749 * C_EXT is which language we are looking at.
1751 * In the future we will need some way to adjust where the end of
1752 * the token is; for instance, implementing the C++ keyword
1753 * `operator' properly will adjust the end of the token to be after
1754 * whatever follows `operator'.
1762 * next_token_is_func IN OUT
1766 consider_token (str
, len
, c
, c_ext
, cblev
, parlev
, is_func
)
1767 register char *str
; /* IN: token pointer */
1768 register int len
; /* IN: token length */
1769 register char c
; /* IN: first char after the token */
1770 int c_ext
; /* IN: C extensions mask */
1771 int cblev
; /* IN: curly brace level */
1772 int parlev
; /* IN: parenthesis level */
1773 logical
*is_func
; /* OUT: function found */
1775 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1778 * Advance the definedef state machine.
1783 /* We're not on a preprocessor line. */
1786 if (toktype
== st_C_define
)
1788 definedef
= ddefineseen
;
1792 definedef
= dignorerest
;
1797 * Make a tag for any macro, unless it is a constant
1798 * and constantypedefs is FALSE.
1800 definedef
= dignorerest
;
1801 *is_func
= (c
== '(');
1802 if (!*is_func
&& !constantypedefs
)
1809 error ("internal error: definedef value.", 0);
1818 if (toktype
== st_C_typedef
)
1821 typdef
= ttypedseen
;
1837 /* Do not return here, so the structdef stuff has a chance. */
1851 * This structdef business is currently only invoked when cblev==0.
1852 * It should be recursively invoked whatever the curly brace level,
1853 * and a stack of states kept, to allow for definitions of structs
1856 * This structdef business is NOT invoked when we are ctags and the
1857 * file is plain C. This is because a struct tag may have the same
1858 * name as another tag, and this loses with ctags.
1860 * This if statement deals with the typdef state machine as
1861 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1862 * return FALSE. All the other code here is for the structdef
1869 if (typdef
== ttypedseen
1870 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1872 structdef
= skeyseen
;
1873 structtype
= toktype
;
1877 if (structdef
== skeyseen
)
1879 /* Save the tag for struct/union/class, for functions that may be
1881 if (structtype
== st_C_struct
)
1882 structtag
= savenstr (str
, len
);
1884 structtag
= "<enum>";
1885 structdef
= stagseen
;
1889 /* Avoid entering funcdef stuff if typdef is going on. */
1890 if (typdef
!= tnone
)
1896 /* Detect GNU macros. */
1897 if (definedef
== dnone
&& toktype
== st_C_gnumacro
)
1899 next_token_is_func
= TRUE
;
1902 if (next_token_is_func
)
1904 next_token_is_func
= FALSE
;
1911 * Detecting Objective C constructs.
1922 objdef
= oimplementation
;
1926 case oimplementation
:
1927 /* Save the class tag for functions that may be defined inside. */
1928 objtag
= savenstr (str
, len
);
1932 /* Save the class tag for categories. */
1933 objtag
= savenstr (str
, len
);
1946 objdef
= omethodtag
;
1948 GROW_LINEBUFFER (token_name
, methodlen
+1);
1949 strncpy (token_name
.buffer
, str
, len
);
1950 token_name
.buffer
[methodlen
] = '\0';
1956 objdef
= omethodparm
;
1961 objdef
= omethodtag
;
1963 GROW_LINEBUFFER (token_name
, methodlen
+1);
1964 strncat (token_name
.buffer
, str
, len
);
1969 if (toktype
== st_C_objend
)
1971 /* Memory leakage here: the string pointed by objtag is
1972 never released, because many tests would be needed to
1973 avoid breaking on incorrect input code. The amount of
1974 memory leaked here is the sum of the lengths of the
1986 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1987 funcdef
= fnone
; /* should be useless */
1990 if (funcdef
== fnone
)
2003 * This routine finds functions, typedefs, #define's and
2004 * struct/union/enum definitions in C syntax and adds them
2018 #define current_lb_is_new (newndx == curndx)
2019 #define switch_line_buffers() (curndx = 1 - curndx)
2021 #define curlb (lbs[curndx].lb)
2022 #define othlb (lbs[1-curndx].lb)
2023 #define newlb (lbs[newndx].lb)
2024 #define curlinepos (lbs[curndx].linepos)
2025 #define othlinepos (lbs[1-curndx].linepos)
2026 #define newlinepos (lbs[newndx].linepos)
2028 #define CNL_SAVE_DEFINEDEF \
2030 curlinepos = charno; \
2032 charno += readline (&curlb, inf); \
2033 lp = curlb.buffer; \
2040 CNL_SAVE_DEFINEDEF; \
2041 if (savetok.valid) \
2044 savetok.valid = FALSE; \
2046 definedef = dnone; \
2049 /* Ideally this macro should never be called wihen tok.valid is FALSE,
2050 but this would mean that the state machines always guess right. */
2051 #define make_tag(isfun) do \
2053 char *name = NULL; \
2054 if (CTAGS || tok.named) \
2055 name = savestr (token_name.buffer); \
2056 pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
2057 tok.valid = FALSE; \
2061 C_entries (c_ext
, inf
)
2062 int c_ext
; /* extension of C */
2063 FILE *inf
; /* input file */
2065 register char c
; /* latest char read; '\0' for end of line */
2066 register char *lp
; /* pointer one beyond the character `c' */
2067 int curndx
, newndx
; /* indices for current and new lb */
2068 TOKEN tok
; /* latest token read */
2069 register int tokoff
; /* offset in line of start of current token */
2070 register int toklen
; /* length of current token */
2071 int cblev
; /* current curly brace level */
2072 int parlev
; /* current parenthesis level */
2073 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
2075 TOKEN savetok
; /* token saved during preprocessor handling */
2078 curndx
= newndx
= 0;
2084 funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
2085 definedef
= dnone
; objdef
= onone
;
2086 next_token_is_func
= yacc_rules
= FALSE
;
2087 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2088 tok
.valid
= savetok
.valid
= FALSE
;
2091 cplpl
= c_ext
& C_PLPL
;
2098 /* If we're at the end of the line, the next character is a
2099 '\0'; don't skip it, because it's the thing that tells us
2100 to read the next line. */
2121 /* Newlines inside comments do not end macro definitions in
2136 /* Newlines inside strings do not end macro definitions
2137 in traditional cpp, even though compilers don't
2138 usually accept them. */
2149 /* Hmmm, something went wrong. */
2163 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2168 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2178 else if (/* cplpl && */ *lp
== '/')
2186 if ((c_ext
& YACC
) && *lp
== '%')
2188 /* entering or exiting rules section in yacc file */
2190 definedef
= dnone
; funcdef
= fnone
;
2191 typdef
= tnone
; structdef
= snone
;
2192 next_token_is_func
= FALSE
;
2193 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2195 yacc_rules
= !yacc_rules
;
2201 if (definedef
== dnone
)
2204 logical cpptoken
= TRUE
;
2206 /* Look back on this line. If all blanks, or nonblanks
2207 followed by an end of comment, this is a preprocessor
2209 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2212 if (*cp
== '*' && *(cp
+1) == '/')
2221 definedef
= dsharpseen
;
2222 } /* if (definedef == dnone) */
2228 /* Consider token only if some complicated conditions are satisfied. */
2229 if ((definedef
!= dnone
2230 || (cblev
== 0 && structdef
!= scolonseen
)
2231 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2232 && typdef
!= tignore
2233 && definedef
!= dignorerest
2234 && funcdef
!= finlist
)
2240 if (c
== ':' && cplpl
&& *lp
== ':' && begtoken(*(lp
+ 1)))
2243 * This handles :: in the middle, but not at the
2244 * beginning of an identifier.
2251 logical is_func
= FALSE
;
2254 || consider_token (newlb
.buffer
+ tokoff
, toklen
, c
,
2255 c_ext
, cblev
, parlev
, &is_func
))
2257 if (structdef
== sinbody
2258 && definedef
== dnone
2260 /* function defined in C++ class body */
2262 GROW_LINEBUFFER (token_name
,
2263 strlen(structtag
)+2+toklen
+1);
2264 strcpy (token_name
.buffer
, structtag
);
2265 strcat (token_name
.buffer
, "::");
2266 strncat (token_name
.buffer
,
2267 newlb
.buffer
+tokoff
, toklen
);
2270 else if (objdef
== ocatseen
)
2271 /* Objective C category */
2273 GROW_LINEBUFFER (token_name
,
2274 strlen(objtag
)+2+toklen
+1);
2275 strcpy (token_name
.buffer
, objtag
);
2276 strcat (token_name
.buffer
, "(");
2277 strncat (token_name
.buffer
,
2278 newlb
.buffer
+tokoff
, toklen
);
2279 strcat (token_name
.buffer
, ")");
2282 else if (objdef
== omethodtag
2283 || objdef
== omethodparm
)
2284 /* Objective C method */
2290 GROW_LINEBUFFER (token_name
, toklen
+1);
2291 strncpy (token_name
.buffer
,
2292 newlb
.buffer
+tokoff
, toklen
);
2293 token_name
.buffer
[toklen
] = '\0';
2294 if (structdef
== stagseen
2297 && definedef
== dignorerest
)) /* macro */
2302 tok
.lineno
= lineno
;
2303 tok
.linelen
= tokoff
+ toklen
+ 1;
2304 tok
.buffer
= newlb
.buffer
;
2305 tok
.linepos
= newlinepos
;
2308 if (definedef
== dnone
2309 && (funcdef
== ftagseen
2310 || structdef
== stagseen
2312 || objdef
!= onone
))
2314 if (current_lb_is_new
)
2315 switch_line_buffers ();
2322 } /* if (endtoken (c)) */
2323 else if (intoken (c
))
2328 } /* if (midtoken) */
2329 else if (begtoken (c
))
2347 if (structdef
== stagseen
)
2353 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2355 tokoff
= lp
- 1 - newlb
.buffer
;
2360 } /* if (begtoken) */
2361 } /* if must look at token */
2364 /* Detect end of line, colon, comma, semicolon and various braces
2365 after having handled a token.*/
2369 if (definedef
!= dnone
)
2379 objdef
= omethodcolon
;
2381 GROW_LINEBUFFER (token_name
, methodlen
+1);
2382 strcat (token_name
.buffer
, ":");
2385 if (structdef
== stagseen
)
2386 structdef
= scolonseen
;
2403 if (definedef
!= dnone
)
2414 if (funcdef
!= fignore
)
2417 /* The following instruction invalidates the token.
2418 Probably the token should be invalidated in all
2419 other cases where some state machine is reset. */
2422 if (structdef
== stagseen
)
2426 if (definedef
!= dnone
)
2436 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2438 if (structdef
== stagseen
)
2442 if (definedef
!= dnone
)
2444 if (cblev
== 0 && typdef
== tend
)
2450 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2452 if (structdef
== stagseen
)
2456 if (definedef
!= dnone
)
2458 if (objdef
== otagseen
&& parlev
== 0)
2459 objdef
= oparenseen
;
2467 /* Make sure that the next char is not a '*'.
2468 This handles constructs like:
2469 typedef void OperatorFun (int fun); */
2476 } /* switch (typdef) */
2479 funcdef
= fstartlist
;
2488 if (definedef
!= dnone
)
2490 if (objdef
== ocatseen
&& parlev
== 1)
2501 funcdef
= flistseen
;
2504 if (cblev
== 0 && typdef
== tend
)
2510 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2514 if (definedef
!= dnone
)
2516 if (typdef
== ttypedseen
)
2520 case skeyseen
: /* unnamed struct */
2521 structtag
= "_anonymous_";
2522 structdef
= sinbody
;
2525 case scolonseen
: /* named struct */
2526 structdef
= sinbody
;
2551 /* Neutralize `extern "C" {' grot and look inside structs. */
2552 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2559 if (definedef
!= dnone
)
2561 if (funcdef
== fstartlist
)
2562 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2565 if (definedef
!= dnone
)
2567 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2569 cblev
= 0; /* reset curly brace level if first column */
2570 parlev
= 0; /* also reset paren level, just in case... */
2576 if (typdef
== tinbody
)
2578 /* Memory leakage here: the string pointed by structtag is
2579 never released, because I fear to miss something and
2580 break things while freeing the area. The amount of
2581 memory leaked here is the sum of the lengths of the
2583 if (structdef == sinbody)
2584 free (structtag); */
2587 structtag
= "<error>";
2592 if (objdef
== oinbody
&& cblev
== 0)
2594 objdef
= omethodsign
;
2598 case '=': case '#': case '~': case '&': case '%': case '/':
2599 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2600 if (definedef
!= dnone
)
2602 /* These surely cannot follow a function tag. */
2603 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2607 if (objdef
== otagseen
)
2612 /* If a macro spans multiple lines don't reset its state. */
2620 } /* while not eof */
2624 * Process either a C++ file or a C file depending on the setting
2628 default_C_entries (inf
)
2631 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2634 /* Always do plain ANSI C. */
2636 plain_C_entries (inf
)
2642 /* Always do C++. */
2644 Cplusplus_entries (inf
)
2647 C_entries (C_PLPL
, inf
);
2655 C_entries (C_STAR
, inf
);
2658 /* Always do Yacc. */
2663 C_entries (YACC
, inf
);
2666 /* Fortran parsing */
2674 register int len
= 0;
2676 while (*cp
&& lowcase(*cp
) == lowcase(dbp
[len
]))
2678 if (*cp
== '\0' && !intoken(dbp
[len
]))
2689 while (isspace (*dbp
))
2694 while (isspace (*dbp
))
2696 if (strneq (dbp
, "(*)", 3))
2701 if (!isdigit (*dbp
))
2703 --dbp
; /* force failure */
2708 while (isdigit (*dbp
));
2717 while (isspace (*dbp
))
2722 linecharno
= charno
;
2723 charno
+= readline (&lb
, inf
);
2728 while (isspace (*dbp
))
2737 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2740 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
2741 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2745 Fortran_functions (inf
)
2754 linecharno
= charno
;
2755 charno
+= readline (&lb
, inf
);
2758 dbp
++; /* Ratfor escape to fortran */
2759 while (isspace (*dbp
))
2763 switch (lowcase (*dbp
))
2766 if (tail ("integer"))
2774 if (tail ("logical"))
2778 if (tail ("complex") || tail ("character"))
2782 if (tail ("double"))
2784 while (isspace (*dbp
))
2788 if (tail ("precision"))
2794 while (isspace (*dbp
))
2798 switch (lowcase (*dbp
))
2801 if (tail ("function"))
2805 if (tail ("subroutine"))
2813 if (tail ("program"))
2818 if (tail ("procedure"))
2826 * Bob Weiner, Motorola Inc., 4/3/94
2827 * Unix and microcontroller assembly tag handling
2828 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2842 linecharno
= charno
;
2843 charno
+= readline (&lb
, inf
);
2846 /* If first char is alphabetic or one of [_.$], test for colon
2847 following identifier. */
2848 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2850 /* Read past label. */
2852 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2854 if (*cp
== ':' || isspace (*cp
))
2856 /* Found end of label, so copy it and add it to the table. */
2857 pfnote ((CTAGS
) ? savenstr(lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2858 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2865 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2866 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2869 Perl_functions (inf
)
2880 linecharno
= charno
;
2881 charno
+= readline (&lb
, inf
);
2884 if (*cp
++ == 's' && *cp
++ == 'u' && *cp
++ == 'b' && isspace(*cp
++))
2886 while (*cp
&& isspace(*cp
))
2888 while (*cp
&& ! isspace(*cp
) && *cp
!= '{')
2890 pfnote ((CTAGS
) ? savenstr (lb
.buffer
, cp
-lb
.buffer
) : NULL
, TRUE
,
2891 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2896 /* Added by Mosur Mohan, 4/22/88 */
2897 /* Pascal parsing */
2899 #define GET_NEW_LINE \
2901 linecharno = charno; lineno++; \
2902 charno += 1 + readline (&lb, inf); \
2907 * Locates tags for procedures & functions. Doesn't do any type- or
2908 * var-definitions. It does look for the keyword "extern" or
2909 * "forward" immediately following the procedure statement; if found,
2910 * the tag is skipped.
2913 Pascal_functions (inf
)
2916 struct linebuffer tline
; /* mostly copied from C_entries */
2918 int save_lineno
, save_len
;
2919 char c
, *cp
, *namebuf
;
2921 logical
/* each of these flags is TRUE iff: */
2922 incomment
, /* point is inside a comment */
2923 inquote
, /* point is inside '..' string */
2924 get_tagname
, /* point is after PROCEDURE/FUNCTION
2925 keyword, so next item = potential tag */
2926 found_tag
, /* point is after a potential tag */
2927 inparms
, /* point is within parameter-list */
2928 verify_tag
; /* point has passed the parm-list, so the
2929 next token will determine whether this
2930 is a FORWARD/EXTERN to be ignored, or
2931 whether it is a real tag */
2938 initbuffer (&tline
);
2940 incomment
= inquote
= FALSE
;
2941 found_tag
= FALSE
; /* have a proc name; check if extern */
2942 get_tagname
= FALSE
; /* have found "procedure" keyword */
2943 inparms
= FALSE
; /* found '(' after "proc" */
2944 verify_tag
= FALSE
; /* check if "extern" is ahead */
2946 /* long main loop to get next char */
2950 if (c
== '\0') /* if end of line */
2955 if (!((found_tag
&& verify_tag
) ||
2957 c
= *dbp
++; /* only if don't need *dbp pointing
2958 to the beginning of the name of
2959 the procedure or function */
2963 if (c
== '}') /* within { } comments */
2965 else if (c
== '*' && *dbp
== ')') /* within (* *) comments */
2982 inquote
= TRUE
; /* found first quote */
2984 case '{': /* found open { comment */
2988 if (*dbp
== '*') /* found open (* comment */
2993 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2996 case ')': /* end of parms list */
3001 if (found_tag
&& !inparms
) /* end of proc or fn stmt */
3008 if (found_tag
&& verify_tag
&& (*dbp
!= ' '))
3010 /* check if this is an "extern" declaration */
3013 if (lowcase (*dbp
== 'e'))
3015 if (tail ("extern")) /* superfluous, really! */
3021 else if (lowcase (*dbp
) == 'f')
3023 if (tail ("forward")) /* check for forward reference */
3029 if (found_tag
&& verify_tag
) /* not external proc, so make tag */
3033 pfnote (namebuf
, TRUE
,
3034 tline
.buffer
, save_len
, save_lineno
, save_lcno
);
3038 if (get_tagname
) /* grab name of proc or fn */
3043 /* save all values for later tagging */
3044 GROW_LINEBUFFER (tline
, strlen (lb
.buffer
) + 1);
3045 strcpy (tline
.buffer
, lb
.buffer
);
3046 save_lineno
= lineno
;
3047 save_lcno
= linecharno
;
3049 /* grab block name */
3050 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
3052 namebuf
= (CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
;
3053 dbp
= cp
; /* set dbp to e-o-token */
3054 save_len
= dbp
- lb
.buffer
+ 1;
3055 get_tagname
= FALSE
;
3059 /* and proceed to check for "extern" */
3061 else if (!incomment
&& !inquote
&& !found_tag
)
3063 /* check for proc/fn keywords */
3064 switch (lowcase (c
))
3067 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3071 if (tail ("unction"))
3076 } /* while not eof */
3078 free (tline
.buffer
);
3082 * lisp tag functions
3083 * look for (def or (DEF, quote or QUOTE
3087 register char *strp
;
3089 return ((strp
[1] == 'd' || strp
[1] == 'D')
3090 && (strp
[2] == 'e' || strp
[2] == 'E')
3091 && (strp
[3] == 'f' || strp
[3] == 'F'));
3096 register char *strp
;
3098 return ((*(++strp
) == 'q' || *strp
== 'Q')
3099 && (*(++strp
) == 'u' || *strp
== 'U')
3100 && (*(++strp
) == 'o' || *strp
== 'O')
3101 && (*(++strp
) == 't' || *strp
== 'T')
3102 && (*(++strp
) == 'e' || *strp
== 'E')
3103 && isspace(*(++strp
)));
3111 if (*dbp
== '\'') /* Skip prefix quote */
3113 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
3116 while (isspace(*dbp
))
3119 for (cp
= dbp
/*+1*/;
3120 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
3126 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3127 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3131 Lisp_functions (inf
)
3140 linecharno
= charno
;
3141 charno
+= readline (&lb
, inf
);
3147 while (!isspace (*dbp
))
3149 while (isspace (*dbp
))
3155 /* Check for (foo::defmumble name-defined ... */
3158 while (*dbp
&& !isspace (*dbp
)
3159 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
3164 while (*dbp
== ':');
3166 if (L_isdef (dbp
- 1))
3168 while (!isspace (*dbp
))
3170 while (isspace (*dbp
))
3181 * Scheme tag functions
3182 * look for (def... xyzzy
3183 * look for (def... (xyzzy
3184 * look for (def ... ((...(xyzzy ....
3185 * look for (set! xyzzy
3191 Scheme_functions (inf
)
3200 linecharno
= charno
;
3201 charno
+= readline (&lb
, inf
);
3203 if (dbp
[0] == '(' &&
3204 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
3205 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3206 (dbp
[3] == 'F' || dbp
[3] == 'f'))
3208 while (!isspace (*dbp
))
3210 /* Skip over open parens and white space */
3211 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
3215 if (dbp
[0] == '(' &&
3216 (dbp
[1] == 'S' || dbp
[1] == 's') &&
3217 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
3218 (dbp
[3] == 'T' || dbp
[3] == 't') &&
3219 (dbp
[4] == '!' || dbp
[4] == '!') &&
3222 while (!isspace (*dbp
))
3224 /* Skip over white space */
3225 while (isspace (*dbp
))
3239 /* Go till you get to white space or a syntactic break */
3241 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
3244 pfnote ((CTAGS
) ? savenstr (dbp
, cp
-dbp
) : NULL
, TRUE
,
3245 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
3248 /* Find tags in TeX and LaTeX input files. */
3250 /* TEX_toktab is a table of TeX control sequences that define tags.
3251 Each TEX_tabent records one such control sequence.
3252 CONVERT THIS TO USE THE Stab TYPE!! */
3259 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
3261 /* Default set of control sequences to put into TEX_toktab.
3262 The value of environment var TEXTAGS is prepended to this. */
3264 char *TEX_defenv
= "\
3265 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3266 :part:appendix:entry:index";
3269 struct TEX_tabent
*TEX_decode_env ();
3271 #if TeX_named_tokens
3275 char TEX_esc
= '\\';
3276 char TEX_opgrp
= '{';
3277 char TEX_clgrp
= '}';
3280 * TeX/LaTeX scanning loop.
3291 /* Select either \ or ! as escape character. */
3294 /* Initialize token table once from environment. */
3296 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
3299 { /* Scan each line in file */
3301 linecharno
= charno
;
3302 charno
+= readline (&lb
, inf
);
3305 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
3311 linecharno
+= dbp
- lasthit
;
3313 i
= TEX_Token (lasthit
);
3317 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3318 #if TeX_named_tokens
3319 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
3321 break; /* We only save a line once */
3327 #define TEX_LESC '\\'
3328 #define TEX_SESC '!'
3331 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3332 chars accordingly. */
3339 while ((c
= getc (inf
)) != EOF
)
3341 /* Skip to next line if we hit the TeX comment char. */
3345 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3364 /* Read environment and prepend it to the default string.
3365 Build token table. */
3367 TEX_decode_env (evarname
, defenv
)
3371 register char *env
, *p
;
3373 struct TEX_tabent
*tab
;
3376 /* Append default string to environment. */
3377 env
= getenv (evarname
);
3381 env
= concat (env
, defenv
, "");
3383 /* Allocate a token table */
3384 for (size
= 1, p
= env
; p
;)
3385 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3387 /* Add 1 to leave room for null terminator. */
3388 tab
= xnew (size
+ 1, struct TEX_tabent
);
3390 /* Unpack environment string into token table. Be careful about */
3391 /* zero-length strings (leading ':', "::" and trailing ':') */
3394 p
= etags_strchr (env
, ':');
3395 if (!p
) /* End of environment string. */
3396 p
= env
+ strlen (env
);
3398 { /* Only non-zero strings. */
3399 tab
[i
].name
= savenstr (env
, p
- env
);
3400 tab
[i
].len
= strlen (tab
[i
].name
);
3407 tab
[i
].name
= NULL
; /* Mark end of table. */
3415 #if TeX_named_tokens
3416 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3417 The name being defined actually starts at (NAME + LEN + 1).
3418 But we seem to include the TeX command in the tag name. */
3420 TEX_getit (name
, len
)
3424 char *p
= name
+ len
;
3429 /* Let tag name extend to next group close (or end of line) */
3430 while (*p
&& *p
!= TEX_clgrp
)
3432 pfnote (savenstr (name
, p
-name
), TRUE
,
3433 lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
3437 /* If the text at CP matches one of the tag-defining TeX command names,
3438 return the pointer to the first occurrence of that command in TEX_toktab.
3439 Otherwise return -1.
3440 Keep the capital `T' in `Token' for dumb truncating compilers
3441 (this distinguishes it from `TEX_toktab' */
3448 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3449 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3454 /* Support for Prolog. */
3456 /* Whole head (not only functor, but also arguments)
3457 is gotten in compound term. */
3470 if (s
[0] == '\0') /* syntax error. */
3472 else if (insquote
&& s
[0] == '\'' && s
[1] == '\'')
3474 else if (s
[0] == '\'')
3476 insquote
= !insquote
;
3479 else if (!insquote
&& s
[0] == '(')
3484 else if (!insquote
&& s
[0] == ')')
3490 else if (npar
< 0) /* syntax error. */
3493 else if (!insquote
&& s
[0] == '.'
3494 && (isspace (s
[1]) || s
[1] == '\0'))
3496 if (npar
!= 0) /* syntax error. */
3504 pfnote ((CTAGS
) ? savenstr (save_s
, s
-save_s
) : NULL
, TRUE
,
3505 save_s
, s
-save_s
, lineno
, linecharno
);
3508 /* It is assumed that prolog predicate starts from column 0. */
3510 Prolog_functions (inf
)
3513 void skip_comment (), prolog_getit ();
3515 lineno
= linecharno
= charno
= 0;
3519 linecharno
+= charno
;
3520 charno
= readline (&lb
, inf
) + 1; /* 1 for newline. */
3522 if (isspace (dbp
[0])) /* not predicate header. */
3524 else if (dbp
[0] == '%') /* comment. */
3526 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3527 skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3534 skip_comment (plb
, inf
, plineno
, plinecharno
)
3535 struct linebuffer
*plb
;
3537 int *plineno
; /* result */
3538 long *plinecharno
; /* result */
3544 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3545 if (cp
[0] == '*' && cp
[1] == '/')
3548 *plinecharno
+= readline (plb
, inf
) + 1; /* 1 for newline. */
3553 #ifdef ETAGS_REGEXPS
3554 /* Take a string like "/blah/" and turn it into "blah", making sure
3555 that the first and last characters are the same, and handling
3556 quoted separator characters. Actually, stops on the occurrence of
3557 an unquoted separator. Also turns "\t" into a Tab character.
3558 Returns pointer to terminating separator. Works in place. Null
3559 terminates name string. */
3561 scan_separators (name
)
3565 char *copyto
= name
;
3566 logical quoted
= FALSE
;
3568 for (++name
; *name
!= '\0'; ++name
)
3574 else if (*name
== sep
)
3578 /* Something else is quoted, so preserve the quote. */
3584 else if (*name
== '\\')
3586 else if (*name
== sep
)
3592 /* Terminate copied string. */
3597 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3598 expression, into a real regular expression by compiling it. */
3600 add_regex (regexp_pattern
)
3601 char *regexp_pattern
;
3605 struct re_pattern_buffer
*patbuf
;
3607 if (regexp_pattern
== NULL
)
3609 /* Remove existing regexps. */
3615 if (regexp_pattern
[0] == '\0')
3617 error ("missing regexp", 0);
3620 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3622 error ("%s: unterminated regexp", regexp_pattern
);
3625 name
= scan_separators (regexp_pattern
);
3626 if (regexp_pattern
[0] == '\0')
3628 error ("null regexp", 0);
3631 (void) scan_separators (name
);
3633 patbuf
= xnew (1, struct re_pattern_buffer
);
3634 patbuf
->translate
= NULL
;
3635 patbuf
->fastmap
= NULL
;
3636 patbuf
->buffer
= NULL
;
3637 patbuf
->allocated
= 0;
3639 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3642 error ("%s while compiling pattern", err
);
3647 if (num_patterns
== 1)
3648 patterns
= xnew (1, struct pattern
);
3650 patterns
= ((struct pattern
*)
3652 (num_patterns
* sizeof (struct pattern
))));
3653 patterns
[num_patterns
- 1].pattern
= patbuf
;
3654 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3655 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
3659 * Do the substitutions indicated by the regular expression and
3663 substitute (in
, out
, regs
)
3665 struct re_registers
*regs
;
3667 char *result
= NULL
, *t
;
3670 /* Pass 1: figure out how much size to allocate. */
3671 for (t
= out
; *t
; ++t
)
3678 fprintf (stderr
, "%s: pattern substitution ends prematurely\n",
3685 size
+= regs
->end
[dig
] - regs
->start
[dig
];
3690 /* Allocate space and do the substitutions. */
3691 result
= xnew (size
+ 1, char);
3700 /* Using "dig2" satisfies my debugger. Bleah. */
3701 int dig2
= *out
- '0';
3702 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
3703 regs
->end
[dig2
] - regs
->start
[dig2
]);
3704 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
3707 result
[size
++] = *out
;
3710 result
[size
++] = *out
;
3712 result
[size
] = '\0';
3717 #endif /* ETAGS_REGEXPS */
3718 /* Initialize a linebuffer for use */
3720 initbuffer (linebuffer
)
3721 struct linebuffer
*linebuffer
;
3723 linebuffer
->size
= 200;
3724 linebuffer
->buffer
= xnew (200, char);
3728 * Read a line of text from `stream' into `linebuffer'.
3729 * Return the number of characters read from `stream',
3730 * which is the length of the line including the newline, if any.
3733 readline_internal (linebuffer
, stream
)
3734 struct linebuffer
*linebuffer
;
3735 register FILE *stream
;
3737 char *buffer
= linebuffer
->buffer
;
3738 register char *p
= linebuffer
->buffer
;
3739 register char *pend
;
3742 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
3746 register int c
= getc (stream
);
3749 linebuffer
->size
*= 2;
3750 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
3751 p
+= buffer
- linebuffer
->buffer
;
3752 pend
= buffer
+ linebuffer
->size
;
3753 linebuffer
->buffer
= buffer
;
3762 if (p
> buffer
&& p
[-1] == '\r')
3777 return p
- buffer
+ chars_deleted
;
3781 * Like readline_internal, above, but try to match the input
3782 * line against any existing regular expressions.
3785 readline (linebuffer
, stream
)
3786 struct linebuffer
*linebuffer
;
3789 /* Read new line. */
3790 long result
= readline_internal (linebuffer
, stream
);
3791 #ifdef ETAGS_REGEXPS
3794 /* Match against all listed patterns. */
3795 for (i
= 0; i
< num_patterns
; ++i
)
3797 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
3798 (int)result
, 0, &patterns
[i
].regs
);
3803 if (!patterns
[i
].error_signaled
)
3805 error ("error while matching pattern %d", i
);
3806 patterns
[i
].error_signaled
= TRUE
;
3813 /* Match occurred. Construct a tag. */
3814 if (patterns
[i
].name_pattern
[0] != '\0')
3816 /* Make a named tag. */
3817 char *name
= substitute (linebuffer
->buffer
,
3818 patterns
[i
].name_pattern
,
3822 linebuffer
->buffer
, match
, lineno
, linecharno
);
3826 /* Make an unnamed tag. */
3828 linebuffer
->buffer
, match
, lineno
, linecharno
);
3833 #endif /* ETAGS_REGEXPS */
3839 * Read a file, but do no processing. This is used to do regexp
3840 * matching on files that have no language defined.
3843 just_read_file (inf
)
3849 linecharno
= charno
;
3850 charno
+= readline (&lb
, inf
) + 1;
3856 * Return a pointer to a space of size strlen(cp)+1 allocated
3857 * with xnew where the string CP has been copied.
3863 return savenstr (cp
, strlen (cp
));
3867 * Return a pointer to a space of size LEN+1 allocated with xnew where
3868 * the string CP has been copied for at most the first LEN characters.
3877 dp
= xnew (len
+ 1, char);
3878 strncpy (dp
, cp
, len
);
3884 * Return the ptr in sp at which the character c last
3885 * appears; NULL if not found
3887 * Identical to System V strrchr, included for portability.
3890 etags_strrchr (sp
, c
)
3891 register char *sp
, c
;
3906 * Return the ptr in sp at which the character c first
3907 * appears; NULL if not found
3909 * Identical to System V strchr, included for portability.
3912 etags_strchr (sp
, c
)
3913 register char *sp
, c
;
3923 /* Print error message and exit. */
3941 suggest_asking_for_help ()
3943 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
3948 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3953 fprintf (stderr
, "%s: ", progname
);
3954 fprintf (stderr
, s1
, s2
);
3955 fprintf (stderr
, "\n");
3958 /* Return a newly-allocated string whose contents
3959 concatenate those of s1, s2, s3. */
3964 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3965 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
3967 strcpy (result
, s1
);
3968 strcpy (result
+ len1
, s2
);
3969 strcpy (result
+ len1
+ len2
, s3
);
3970 result
[len1
+ len2
+ len3
] = '\0';
3975 /* Does the same work as the system V getcwd, but does not need to
3976 guess the buffer size in advance. */
3981 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
3989 *p
++ = lowcase (*p
);
3991 return strdup (path
);
3992 #else /* not DOS_NT */
3995 char *path
= xnew (bufsize
, char);
3997 while (getcwd (path
, bufsize
) == NULL
)
3999 if (errno
!= ERANGE
)
4002 path
= xnew (bufsize
, char);
4006 #else /* not DOS_NT and not HAVE_GETCWD */
4007 struct linebuffer path
;
4011 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
4012 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
4017 #endif /* not HAVE_GETCWD */
4018 #endif /* not DOS_NT */
4021 /* Return a newly allocated string containing the filename
4022 of FILE relative to the absolute directory DIR (which
4023 should end with a slash). */
4025 relative_filename (file
, dir
)
4028 char *fp
, *dp
, *abs
, *res
;
4030 /* Find the common root of file and dir. */
4031 abs
= absolute_filename (file
, cwd
);
4034 while (*fp
++ == *dp
++)
4043 /* Build a sequence of "../" strings for the resulting relative filename. */
4044 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
4046 dp
= etags_strchr (dp
+ 1, '/'))
4048 res
= concat (res
, "../", "");
4051 /* Add the filename relative to the common root of file and dir. */
4052 res
= concat (res
, fp
+ 1, "");
4058 /* Return a newly allocated string containing the
4059 absolute filename of FILE given CWD (which should
4060 end with a slash). */
4062 absolute_filename (file
, cwd
)
4065 char *slashp
, *cp
, *res
;
4067 if (absolutefn (file
))
4068 res
= concat (file
, "", "");
4070 res
= concat (cwd
, file
, "");
4072 /* Delete the "/dirname/.." and "/." substrings. */
4073 slashp
= etags_strchr (res
, '/');
4074 while (slashp
!= NULL
&& slashp
[0] != '\0')
4076 if (slashp
[1] == '.')
4078 if (slashp
[2] == '.'
4079 && (slashp
[3] == '/' || slashp
[3] == '\0'))
4084 while (cp
>= res
&& *cp
!= '/');
4087 strcpy (cp
, slashp
+ 3);
4089 else /* else (cp == res) */
4091 if (slashp
[3] != '\0')
4092 strcpy (cp
, slashp
+ 4);
4099 else if (slashp
[2] == '/' || slashp
[2] == '\0')
4101 strcpy (slashp
, slashp
+ 2);
4106 slashp
= etags_strchr (slashp
+ 1, '/');
4112 /* Return a newly allocated string containing the absolute
4113 filename of dir where FILE resides given CWD (which should
4114 end with a slash). */
4116 absolute_dirname (file
, cwd
)
4122 slashp
= etags_strrchr (file
, '/');
4127 res
= absolute_filename (file
, cwd
);
4133 /* Like malloc but get fatal error if memory is exhausted. */
4138 long *result
= (long *) malloc (size
);
4140 fatal ("virtual memory exhausted", 0);
4145 xrealloc (ptr
, size
)
4149 long *result
= (long *) realloc (ptr
, size
);
4151 fatal ("virtual memory exhausted");