(streq, strneq): Use == NULL rather than !.
[bpt/emacs.git] / lib-src / etags.c
1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 87, 88, 89, 93, 94, 95
3 Free Software Foundation, Inc. and Ken Arnold
4
5 This file is not considered part of GNU Emacs.
6
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.
11
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.
16
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. */
20
21 /*
22 * Authors:
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.
30 *
31 * Francesco Potorti` (F.Potorti@cnuce.cnr.it) is the current maintainer.
32 */
33
34 char pot_etags_version[] = "@(#) pot revision number is 11.80";
35
36 #define TRUE 1
37 #define FALSE 0
38
39 #ifndef DEBUG
40 # define DEBUG FALSE
41 #endif
42
43 #ifdef MSDOS
44 # include <string.h>
45 # include <fcntl.h>
46 # include <sys/param.h>
47 #endif /* MSDOS */
48
49 #ifdef WINDOWSNT
50 # include <stdlib.h>
51 # include <fcntl.h>
52 # include <string.h>
53 # include <io.h>
54 # define MAXPATHLEN _MAX_PATH
55 #endif
56
57 #if !defined (MSDOS) && !defined (WINDOWSNT) && defined (STDC_HEADERS)
58 #include <stdlib.h>
59 #include <string.h>
60 #endif
61
62 #ifdef HAVE_CONFIG_H
63 # include <config.h>
64 /* On some systems, Emacs defines static as nothing for the sake
65 of unexec. We don't want that here since we don't use unexec. */
66 # undef static
67 #endif
68
69 #include <stdio.h>
70 #include <ctype.h>
71 #include <errno.h>
72 #ifndef errno
73 extern int errno;
74 #endif
75 #include <sys/types.h>
76 #include <sys/stat.h>
77
78 #if !defined (S_ISREG) && defined (S_IFREG)
79 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
80 #endif
81
82 #include <getopt.h>
83
84 #ifdef ETAGS_REGEXPS
85 # include <regex.h>
86 #endif /* ETAGS_REGEXPS */
87
88 /* Define CTAGS to make the program "ctags" compatible with the usual one.
89 Let it undefined to make the program "etags", which makes emacs-style
90 tag tables and tags typedefs, #defines and struct/union/enum by default. */
91 #ifdef CTAGS
92 # undef CTAGS
93 # define CTAGS TRUE
94 #else
95 # define CTAGS FALSE
96 #endif
97
98 /* Exit codes for success and failure. */
99 #ifdef VMS
100 # define GOOD 1
101 # define BAD 0
102 #else
103 # define GOOD 0
104 # define BAD 1
105 #endif
106
107 /* C extensions. */
108 #define C_PLPL 0x00001 /* C++ */
109 #define C_STAR 0x00003 /* C* */
110 #define YACC 0x10000 /* yacc file */
111
112 #define streq(s,t) ((DEBUG && (s) == NULL && (t) == NULL \
113 && (abort (), 1)) || !strcmp (s, t))
114 #define strneq(s,t,n) ((DEBUG && (s) == NULL && (t) == NULL \
115 && (abort (), 1)) || !strncmp (s, t, n))
116
117 #define lowcase(c) tolower ((char)c)
118
119 #define iswhite(arg) (_wht[arg]) /* T if char is white */
120 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
121 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
122 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
123
124 #ifdef DOS_NT
125 # define absolutefn(fn) (fn[0] == '/' \
126 || (fn[1] == ':' && fn[2] == '/'))
127 #else
128 # define absolutefn(fn) (fn[0] == '/')
129 #endif
130
131
132 /*
133 * xnew -- allocate storage
134 *
135 * SYNOPSIS: Type *xnew (int n, Type);
136 */
137 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
138
139 typedef int logical;
140
141 typedef struct nd_st
142 { /* sorting structure */
143 char *name; /* function or type name */
144 char *file; /* file name */
145 logical is_func; /* use pattern or line no */
146 logical been_warned; /* set if noticed dup */
147 int lno; /* line number tag is on */
148 long cno; /* character number line starts on */
149 char *pat; /* search pattern */
150 struct nd_st *left, *right; /* left and right sons */
151 } NODE;
152
153 extern char *getenv ();
154
155 char *concat ();
156 char *savenstr (), *savestr ();
157 char *etags_strchr (), *etags_strrchr ();
158 char *etags_getcwd ();
159 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
160 void grow_linebuffer ();
161 long *xmalloc (), *xrealloc ();
162
163 typedef void Lang_function ();
164 #if FALSE /* many compilers barf on this */
165 Lang_function Asm_labels;
166 Lang_function default_C_entries;
167 Lang_function C_entries;
168 Lang_function Cplusplus_entries;
169 Lang_function Cstar_entries;
170 Lang_function Erlang_functions;
171 Lang_function Fortran_functions;
172 Lang_function Yacc_entries;
173 Lang_function Lisp_functions;
174 Lang_function Pascal_functions;
175 Lang_function Perl_functions;
176 Lang_function Prolog_functions;
177 Lang_function Scheme_functions;
178 Lang_function TeX_functions;
179 Lang_function just_read_file;
180 #else /* so let's write it this way */
181 void Asm_labels ();
182 void C_entries ();
183 void default_C_entries ();
184 void plain_C_entries ();
185 void Cplusplus_entries ();
186 void Cstar_entries ();
187 void Erlang_functions ();
188 void Fortran_functions ();
189 void Yacc_entries ();
190 void Lisp_functions ();
191 void Pascal_functions ();
192 void Perl_functions ();
193 void Prolog_functions ();
194 void Scheme_functions ();
195 void TeX_functions ();
196 void just_read_file ();
197 #endif
198
199 Lang_function *get_language_from_name ();
200 Lang_function *get_language_from_interpreter ();
201 Lang_function *get_language_from_suffix ();
202 int total_size_of_entries ();
203 long readline ();
204 long readline_internal ();
205 #ifdef ETAGS_REGEXPS
206 void add_regex ();
207 #endif
208 void add_node ();
209 void error ();
210 void suggest_asking_for_help ();
211 void fatal (), pfatal ();
212 void find_entries ();
213 void free_tree ();
214 void getit ();
215 void init ();
216 void initbuffer ();
217 void pfnote ();
218 void process_file ();
219 void put_entries ();
220 void takeprec ();
221
222 \f
223 char searchar = '/'; /* use /.../ searches */
224
225 int lineno; /* line number of current line */
226 long charno; /* current character number */
227 long linecharno; /* charno of start of line */
228
229 char *curfile; /* current input file name */
230 char *tagfile; /* output file */
231 char *progname; /* name this program was invoked with */
232 char *cwd; /* current working directory */
233 char *tagfiledir; /* directory of tagfile */
234
235 FILE *tagf; /* ioptr for tags file */
236 NODE *head; /* the head of the binary tree of tags */
237
238 /*
239 * A `struct linebuffer' is a structure which holds a line of text.
240 * `readline' reads a line from a stream into a linebuffer and works
241 * regardless of the length of the line.
242 */
243 struct linebuffer
244 {
245 long size;
246 char *buffer;
247 };
248
249 struct linebuffer lb; /* the current line */
250 struct linebuffer token_name; /* used by C_entries as a temporary area */
251 struct
252 {
253 long linepos;
254 struct linebuffer lb; /* used by C_entries instead of lb */
255 } lbs[2];
256
257 /* boolean "functions" (see init) */
258 logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
259 char
260 /* white chars */
261 *white = " \f\t\n\013",
262 /* token ending chars */
263 *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
264 /* token starting chars */
265 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
266 /* valid in-token chars */
267 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
268
269 logical append_to_tagfile; /* -a: append to tags */
270 /* The following three default to TRUE for etags, but to FALSE for ctags. */
271 logical typedefs; /* -t: create tags for typedefs */
272 logical typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
273 /* 0 struct/enum/union decls, and C++ */
274 /* member functions. */
275 logical constantypedefs; /* -d: create tags for C #define and enum */
276 /* constants. */
277 /* -D: opposite of -d. Default under ctags. */
278 logical update; /* -u: update tags */
279 logical vgrind_style; /* -v: create vgrind style index output */
280 logical no_warnings; /* -w: suppress warnings */
281 logical cxref_style; /* -x: create cxref style output */
282 logical cplusplus; /* .[hc] means C++, not C */
283 logical noindentypedefs; /* -I: ignore indentation in C */
284
285 struct option longopts[] =
286 {
287 { "append", no_argument, NULL, 'a' },
288 { "backward-search", no_argument, NULL, 'B' },
289 { "c++", no_argument, NULL, 'C' },
290 { "cxref", no_argument, NULL, 'x' },
291 { "defines", no_argument, NULL, 'd' },
292 { "help", no_argument, NULL, 'h' },
293 { "help", no_argument, NULL, 'H' },
294 { "ignore-indentation", no_argument, NULL, 'I' },
295 { "include", required_argument, NULL, 'i' },
296 { "language", required_argument, NULL, 'l' },
297 { "no-defines", no_argument, NULL, 'D' },
298 { "no-regex", no_argument, NULL, 'R' },
299 { "no-warn", no_argument, NULL, 'w' },
300 { "output", required_argument, NULL, 'o' },
301 { "regex", required_argument, NULL, 'r' },
302 { "typedefs", no_argument, NULL, 't' },
303 { "typedefs-and-c++", no_argument, NULL, 'T' },
304 { "update", no_argument, NULL, 'u' },
305 { "version", no_argument, NULL, 'V' },
306 { "vgrind", no_argument, NULL, 'v' },
307 { 0 }
308 };
309
310 #ifdef ETAGS_REGEXPS
311 /* Structure defining a regular expression. Elements are
312 the compiled pattern, and the name string. */
313 struct pattern
314 {
315 struct re_pattern_buffer *pattern;
316 struct re_registers regs;
317 char *name_pattern;
318 logical error_signaled;
319 };
320
321 /* Number of regexps found. */
322 int num_patterns = 0;
323
324 /* Array of all regexps. */
325 struct pattern *patterns = NULL;
326 #endif /* ETAGS_REGEXPS */
327
328 /*
329 * Language stuff.
330 */
331
332 /* Non-NULL if language fixed. */
333 Lang_function *lang_func = NULL;
334
335 /* Assembly code */
336 char *Asm_suffixes [] = { "a", /* Unix assembler */
337 "asm", /* Microcontroller assembly */
338 "def", /* BSO/Tasking definition includes */
339 "inc", /* Microcontroller include files */
340 "ins", /* Microcontroller include files */
341 "s", "sa", /* Unix assembler */
342 "src", /* BSO/Tasking C compiler output */
343 NULL
344 };
345
346 /* Note that .c and .h can be considered C++, if the --c++ flag was
347 given. That is why default_C_entries is called here. */
348 char *default_C_suffixes [] =
349 { "c", "h", NULL };
350
351 /* .M is for Objective C++ files. */
352 char *Cplusplus_suffixes [] =
353 { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL};
354
355 char *Cstar_suffixes [] =
356 { "cs", "hs", NULL };
357
358 char *Erlang_suffixes [] =
359 { "erl", "hrl", NULL };
360
361 char *Fortran_suffixes [] =
362 { "F", "f", "f90", "for", NULL };
363
364 char *Lisp_suffixes [] =
365 { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL };
366
367 char *Pascal_suffixes [] =
368 { "p", "pas", NULL };
369
370 char *Perl_suffixes [] =
371 { "pl", "pm", NULL };
372 char *Perl_interpreters [] =
373 { "perl", "@PERL@", NULL };
374
375 char *plain_C_suffixes [] =
376 { "pc", /* Pro*C file */
377 "m", /* Objective C file */
378 "lm", /* Objective lex file */
379 NULL };
380
381 char *Prolog_suffixes [] =
382 { "prolog", NULL };
383
384 /* Can't do the `SCM' or `scm' prefix with a version number. */
385 char *Scheme_suffixes [] =
386 { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL };
387
388 char *TeX_suffixes [] =
389 { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL };
390
391 char *Yacc_suffixes [] =
392 { "y", "ym", NULL }; /* .ym is Objective yacc file */
393
394 /* Table of language names and corresponding functions, file suffixes
395 and interpreter names.
396 It is ok for a given function to be listed under more than one
397 name. I just didn't. */
398 struct lang_entry
399 {
400 char *name;
401 Lang_function *function;
402 char **suffixes;
403 char **interpreters;
404 };
405
406 struct lang_entry lang_names [] =
407 {
408 { "asm", Asm_labels, Asm_suffixes, NULL },
409 { "c", default_C_entries, default_C_suffixes, NULL },
410 { "c++", Cplusplus_entries, Cplusplus_suffixes, NULL },
411 { "c*", Cstar_entries, Cstar_suffixes, NULL },
412 { "erlang", Erlang_functions, Erlang_suffixes, NULL },
413 { "fortran", Fortran_functions, Fortran_suffixes, NULL },
414 { "lisp", Lisp_functions, Lisp_suffixes, NULL },
415 { "pascal", Pascal_functions, Pascal_suffixes, NULL },
416 { "perl", Perl_functions, Perl_suffixes, Perl_interpreters },
417 { "proc", plain_C_entries, plain_C_suffixes, NULL },
418 { "prolog", Prolog_functions, Prolog_suffixes, NULL },
419 { "scheme", Scheme_functions, Scheme_suffixes, NULL },
420 { "tex", TeX_functions, TeX_suffixes, NULL },
421 { "yacc", Yacc_entries, Yacc_suffixes, NULL },
422 { "auto", NULL }, /* default guessing scheme */
423 { "none", just_read_file }, /* regexp matching only */
424 { NULL, NULL } /* end of list */
425 };
426
427 \f
428 void
429 print_language_names ()
430 {
431 struct lang_entry *lang;
432 char **ext;
433
434 puts ("\nThese are the currently supported languages, along with the\n\
435 default file name suffixes:");
436 for (lang = lang_names; lang->name != NULL; lang++)
437 {
438 printf ("\t%s\t", lang->name);
439 if (lang->suffixes != NULL)
440 for (ext = lang->suffixes; *ext != NULL; ext++)
441 printf (" .%s", *ext);
442 puts ("");
443 }
444 puts ("Where `auto' means use default language for files based on file\n\
445 name suffix, and `none' means only do regexp processing on files.\n\
446 If no language is specified and no matching suffix is found,\n\
447 the first line of the file is read for a sharp-bang (#!) sequence\n\
448 followed by the name of an interpreter. If no such sequence is found,\n\
449 Fortran is tried first; if no tags are found, C is tried next.");
450 }
451
452 #ifndef VERSION
453 # define VERSION "19"
454 #endif
455 void
456 print_version ()
457 {
458 printf ("%s (GNU Emacs %s)\n", (CTAGS) ? "ctags" : "etags", VERSION);
459 puts ("Copyright (C) 1996 Free Software Foundation, Inc. and Ken Arnold");
460 puts ("This program is distributed under the same terms as Emacs");
461
462 exit (GOOD);
463 }
464
465 void
466 print_help ()
467 {
468 printf ("These are the options accepted by %s. You may use unambiguous\n\
469 abbreviations for the long option names. A - as file name means read\n\
470 names from stdin.", progname);
471 if (!CTAGS)
472 printf (" Absolute names are stored in the output file as they\n\
473 are. Relative ones are stored relative to the output file's directory.");
474 puts ("\n");
475
476 puts ("-a, --append\n\
477 Append tag entries to existing tags file.");
478
479 if (CTAGS)
480 puts ("-B, --backward-search\n\
481 Write the search commands for the tag entries using '?', the\n\
482 backward-search command instead of '/', the forward-search command.");
483
484 puts ("-C, --c++\n\
485 Treat files whose name suffix defaults to C language as C++ files.");
486
487 if (CTAGS)
488 puts ("-d, --defines\n\
489 Create tag entries for C #define constants and enum constants, too.");
490 else
491 puts ("-D, --no-defines\n\
492 Don't create tag entries for C #define constants and enum constants.\n\
493 This makes the tags file smaller.");
494
495 if (!CTAGS)
496 {
497 puts ("-i FILE, --include=FILE\n\
498 Include a note in tag file indicating that, when searching for\n\
499 a tag, one should also consult the tags file FILE after\n\
500 checking the current file.");
501 puts ("-l LANG, --language=LANG\n\
502 Force the following files to be considered as written in the\n\
503 named language up to the next --language=LANG option.");
504 }
505
506 #ifdef ETAGS_REGEXPS
507 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
508 Make a tag for each line matching pattern REGEXP in the\n\
509 following files. REGEXP is anchored (as if preceded by ^).\n\
510 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
511 named tags can be created with:\n\
512 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
513 puts ("-R, --no-regex\n\
514 Don't create tags from regexps for the following files.");
515 #endif /* ETAGS_REGEXPS */
516 puts ("-o FILE, --output=FILE\n\
517 Write the tags to FILE.");
518 puts ("-I, --ignore-indentation\n\
519 Don't rely on indentation quite as much as normal. Currently,\n\
520 this means not to assume that a closing brace in the first\n\
521 column is the final brace of a function or structure\n\
522 definition in C and C++.");
523
524 if (CTAGS)
525 {
526 puts ("-t, --typedefs\n\
527 Generate tag entries for C typedefs.");
528 puts ("-T, --typedefs-and-c++\n\
529 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
530 and C++ member functions.");
531 puts ("-u, --update\n\
532 Update the tag entries for the given files, leaving tag\n\
533 entries for other files in place. Currently, this is\n\
534 implemented by deleting the existing entries for the given\n\
535 files and then rewriting the new entries at the end of the\n\
536 tags file. It is often faster to simply rebuild the entire\n\
537 tag file than to use this.");
538 puts ("-v, --vgrind\n\
539 Generates an index of items intended for human consumption,\n\
540 similar to the output of vgrind. The index is sorted, and\n\
541 gives the page number of each item.");
542 puts ("-w, --no-warn\n\
543 Suppress warning messages about entries defined in multiple\n\
544 files.");
545 puts ("-x, --cxref\n\
546 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
547 The output uses line numbers instead of page numbers, but\n\
548 beyond that the differences are cosmetic; try both to see\n\
549 which you like.");
550 }
551
552 puts ("-V, --version\n\
553 Print the version of the program.\n\
554 -h, --help\n\
555 Print this help message.");
556
557 print_language_names ();
558
559 puts ("");
560 puts ("Report bugs to bug-gnu-emacs@prep.ai.mit.edu");
561
562 exit (GOOD);
563 }
564
565 \f
566 enum argument_type
567 {
568 at_language,
569 at_regexp,
570 at_filename
571 };
572
573 /* This structure helps us allow mixing of --lang and filenames. */
574 typedef struct
575 {
576 enum argument_type arg_type;
577 char *what;
578 Lang_function *function;
579 } argument;
580
581 #ifdef VMS /* VMS specific functions */
582
583 #define EOS '\0'
584
585 /* This is a BUG! ANY arbitrary limit is a BUG!
586 Won't someone please fix this? */
587 #define MAX_FILE_SPEC_LEN 255
588 typedef struct {
589 short curlen;
590 char body[MAX_FILE_SPEC_LEN + 1];
591 } vspec;
592
593 /*
594 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
595 returning in each successive call the next filename matching the input
596 spec. The function expects that each in_spec passed
597 to it will be processed to completion; in particular, up to and
598 including the call following that in which the last matching name
599 is returned, the function ignores the value of in_spec, and will
600 only start processing a new spec with the following call.
601 If an error occurs, on return out_spec contains the value
602 of in_spec when the error occurred.
603
604 With each successive filename returned in out_spec, the
605 function's return value is one. When there are no more matching
606 names the function returns zero. If on the first call no file
607 matches in_spec, or there is any other error, -1 is returned.
608 */
609
610 #include <rmsdef.h>
611 #include <descrip.h>
612 #define OUTSIZE MAX_FILE_SPEC_LEN
613 short
614 fn_exp (out, in)
615 vspec *out;
616 char *in;
617 {
618 static long context = 0;
619 static struct dsc$descriptor_s o;
620 static struct dsc$descriptor_s i;
621 static logical pass1 = TRUE;
622 long status;
623 short retval;
624
625 if (pass1)
626 {
627 pass1 = FALSE;
628 o.dsc$a_pointer = (char *) out;
629 o.dsc$w_length = (short)OUTSIZE;
630 i.dsc$a_pointer = in;
631 i.dsc$w_length = (short)strlen(in);
632 i.dsc$b_dtype = DSC$K_DTYPE_T;
633 i.dsc$b_class = DSC$K_CLASS_S;
634 o.dsc$b_dtype = DSC$K_DTYPE_VT;
635 o.dsc$b_class = DSC$K_CLASS_VS;
636 }
637 if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL)
638 {
639 out->body[out->curlen] = EOS;
640 return 1;
641 }
642 else if (status == RMS$_NMF)
643 retval = 0;
644 else
645 {
646 strcpy(out->body, in);
647 retval = -1;
648 }
649 lib$find_file_end(&context);
650 pass1 = TRUE;
651 return retval;
652 }
653
654 /*
655 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
656 name of each file specified by the provided arg expanding wildcards.
657 */
658 char *
659 gfnames (arg, p_error)
660 char *arg;
661 logical *p_error;
662 {
663 static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
664
665 switch (fn_exp (&filename, arg))
666 {
667 case 1:
668 *p_error = FALSE;
669 return filename.body;
670 case 0:
671 *p_error = FALSE;
672 return NULL;
673 default:
674 *p_error = TRUE;
675 return filename.body;
676 }
677 }
678
679 #ifndef OLD /* Newer versions of VMS do provide `system'. */
680 system (cmd)
681 char *cmd;
682 {
683 fprintf (stderr, "system() function not implemented under VMS\n");
684 }
685 #endif
686
687 #define VERSION_DELIM ';'
688 char *massage_name (s)
689 char *s;
690 {
691 char *start = s;
692
693 for ( ; *s; s++)
694 if (*s == VERSION_DELIM)
695 {
696 *s = EOS;
697 break;
698 }
699 else
700 *s = lowcase (*s);
701 return start;
702 }
703 #endif /* VMS */
704
705 \f
706 int
707 main (argc, argv)
708 int argc;
709 char *argv[];
710 {
711 int i;
712 unsigned int nincluded_files = 0;
713 char **included_files = xnew (argc, char *);
714 char *this_file;
715 argument *argbuffer;
716 int current_arg = 0, file_count = 0;
717 struct linebuffer filename_lb;
718 #ifdef VMS
719 logical got_err;
720 #endif
721
722 #ifdef DOS_NT
723 _fmode = O_BINARY; /* all of files are treated as binary files */
724 #endif /* DOS_NT */
725
726 progname = argv[0];
727
728 /* Allocate enough no matter what happens. Overkill, but each one
729 is small. */
730 argbuffer = xnew (argc, argument);
731
732 #ifdef ETAGS_REGEXPS
733 /* Set syntax for regular expression routines. */
734 re_set_syntax (RE_SYNTAX_EMACS);
735 #endif /* ETAGS_REGEXPS */
736
737 /*
738 * If etags, always find typedefs and structure tags. Why not?
739 * Also default is to find macro constants and enum constants.
740 */
741 if (!CTAGS)
742 typedefs = typedefs_and_cplusplus = constantypedefs = TRUE;
743
744 while (1)
745 {
746 int opt = getopt_long (argc, argv,
747 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts, 0);
748
749 if (opt == EOF)
750 break;
751
752 switch (opt)
753 {
754 case 0:
755 /* If getopt returns 0, then it has already processed a
756 long-named option. We should do nothing. */
757 break;
758
759 case 1:
760 /* This means that a filename has been seen. Record it. */
761 argbuffer[current_arg].arg_type = at_filename;
762 argbuffer[current_arg].what = optarg;
763 ++current_arg;
764 ++file_count;
765 break;
766
767 /* Common options. */
768 case 'a':
769 append_to_tagfile = TRUE;
770 break;
771 case 'C':
772 cplusplus = TRUE;
773 break;
774 case 'd':
775 constantypedefs = TRUE;
776 break;
777 case 'D':
778 constantypedefs = FALSE;
779 break;
780 case 'f': /* for compatibility with old makefiles */
781 case 'o':
782 if (tagfile)
783 {
784 fprintf (stderr, "%s: -%c option may only be given once.\n",
785 progname, opt);
786 suggest_asking_for_help ();
787 }
788 tagfile = optarg;
789 break;
790 case 'I':
791 case 'S': /* for backward compatibility */
792 noindentypedefs = TRUE;
793 break;
794 case 'l':
795 argbuffer[current_arg].function = get_language_from_name (optarg);
796 argbuffer[current_arg].arg_type = at_language;
797 ++current_arg;
798 break;
799 #ifdef ETAGS_REGEXPS
800 case 'r':
801 argbuffer[current_arg].arg_type = at_regexp;
802 argbuffer[current_arg].what = optarg;
803 ++current_arg;
804 break;
805 case 'R':
806 argbuffer[current_arg].arg_type = at_regexp;
807 argbuffer[current_arg].what = NULL;
808 ++current_arg;
809 break;
810 #endif /* ETAGS_REGEXPS */
811 case 'V':
812 print_version ();
813 break;
814 case 'h':
815 case 'H':
816 print_help ();
817 break;
818 case 't':
819 typedefs = TRUE;
820 break;
821 case 'T':
822 typedefs = typedefs_and_cplusplus = TRUE;
823 break;
824 #if (!CTAGS)
825 /* Etags options */
826 case 'i':
827 included_files[nincluded_files++] = optarg;
828 break;
829 #else /* CTAGS */
830 /* Ctags options. */
831 case 'B':
832 searchar = '?';
833 break;
834 case 'u':
835 update = TRUE;
836 break;
837 case 'v':
838 vgrind_style = TRUE;
839 /*FALLTHRU*/
840 case 'x':
841 cxref_style = TRUE;
842 break;
843 case 'w':
844 no_warnings = TRUE;
845 break;
846 #endif /* CTAGS */
847 default:
848 suggest_asking_for_help ();
849 }
850 }
851
852 for (; optind < argc; ++optind)
853 {
854 argbuffer[current_arg].arg_type = at_filename;
855 argbuffer[current_arg].what = argv[optind];
856 ++current_arg;
857 ++file_count;
858 }
859
860 if (nincluded_files == 0 && file_count == 0)
861 {
862 fprintf (stderr, "%s: No input files specified.\n", progname);
863 suggest_asking_for_help ();
864 }
865
866 if (tagfile == NULL)
867 tagfile = CTAGS ? "tags" : "TAGS";
868 cwd = etags_getcwd (); /* the current working directory */
869 if (cwd[strlen (cwd) - 1] != '/')
870 cwd = concat (cwd, "/", "");
871 if (streq (tagfile, "-"))
872 tagfiledir = cwd;
873 else
874 tagfiledir = absolute_dirname (tagfile, cwd);
875
876 init (); /* set up boolean "functions" */
877
878 initbuffer (&lb);
879 initbuffer (&token_name);
880 initbuffer (&lbs[0].lb);
881 initbuffer (&lbs[1].lb);
882 initbuffer (&filename_lb);
883
884 if (!CTAGS)
885 {
886 if (streq (tagfile, "-"))
887 {
888 tagf = stdout;
889 #ifdef DOS_NT
890 /* Switch redirected `stdout' to binary mode (setting `_fmode'
891 doesn't take effect until after `stdout' is already open). */
892 if (!isatty (fileno (stdout)))
893 setmode (fileno (stdout), O_BINARY);
894 #endif /* DOS_NT */
895 }
896 else
897 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
898 if (tagf == NULL)
899 pfatal (tagfile);
900 }
901
902 /*
903 * Loop through files finding functions.
904 */
905 for (i = 0; i < current_arg; ++i)
906 {
907 switch (argbuffer[i].arg_type)
908 {
909 case at_language:
910 lang_func = argbuffer[i].function;
911 break;
912 #ifdef ETAGS_REGEXPS
913 case at_regexp:
914 add_regex (argbuffer[i].what);
915 break;
916 #endif
917 case at_filename:
918 #ifdef VMS
919 while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL)
920 {
921 if (got_err)
922 {
923 error ("Can't find file %s\n", this_file);
924 argc--, argv++;
925 }
926 else
927 {
928 this_file = massage_name (this_file);
929 }
930 #else
931 this_file = argbuffer[i].what;
932 #endif
933 /* Input file named "-" means read file names from stdin
934 and use them. */
935 if (streq (this_file, "-"))
936 while (readline_internal (&filename_lb, stdin) > 0)
937 process_file (filename_lb.buffer);
938 else
939 process_file (this_file);
940 #ifdef VMS
941 }
942 #endif
943 break;
944 }
945 }
946
947 if (!CTAGS)
948 {
949 while (nincluded_files-- > 0)
950 fprintf (tagf, "\f\n%s,include\n", *included_files++);
951
952 fclose (tagf);
953 exit (GOOD);
954 }
955
956 /* If CTAGS, we are here. process_file did not write the tags yet,
957 because we want them ordered. Let's do it now. */
958 if (cxref_style)
959 {
960 put_entries (head);
961 exit (GOOD);
962 }
963
964 if (update)
965 {
966 char cmd[BUFSIZ];
967 for (i = 0; i < current_arg; ++i)
968 {
969 if (argbuffer[i].arg_type != at_filename)
970 continue;
971 sprintf (cmd,
972 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
973 tagfile, argbuffer[i].what, tagfile);
974 if (system (cmd) != GOOD)
975 fatal ("failed to execute shell command", (char *)NULL);
976 }
977 append_to_tagfile = TRUE;
978 }
979
980 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
981 if (tagf == NULL)
982 pfatal (tagfile);
983 put_entries (head);
984 fclose (tagf);
985
986 if (update)
987 {
988 char cmd[BUFSIZ];
989 sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
990 exit (system (cmd));
991 }
992 return GOOD;
993 }
994
995
996 /*
997 * Return a Lang_function given the name.
998 */
999 Lang_function *
1000 get_language_from_name (name)
1001 char *name;
1002 {
1003 struct lang_entry *lang;
1004
1005 if (name != NULL)
1006 for (lang = lang_names; lang->name != NULL; lang++)
1007 {
1008 if (streq (name, lang->name))
1009 return lang->function;
1010 }
1011
1012 fprintf (stderr, "%s: language \"%s\" not recognized.\n",
1013 progname, optarg);
1014 suggest_asking_for_help ();
1015
1016 /* This point should never be reached. The function should either
1017 return a function pointer or never return. Note that a NULL
1018 pointer cannot be considered as an error, as it means that the
1019 language has not been explicitely imposed by the user ("auto"). */
1020 return NULL; /* avoid warnings from compiler */
1021 }
1022
1023
1024 /*
1025 * Return a Lang_function given the interpreter name.
1026 */
1027 Lang_function *
1028 get_language_from_interpreter (interpreter)
1029 char *interpreter;
1030 {
1031 struct lang_entry *lang;
1032 char **iname;
1033
1034 if (interpreter == NULL)
1035 return NULL;
1036 for (lang = lang_names; lang->name != NULL; lang++)
1037 if (lang->interpreters != NULL)
1038 for (iname = lang->interpreters; *iname != NULL; iname++)
1039 if (streq (*iname, interpreter))
1040 return lang->function;
1041
1042 return NULL;
1043 }
1044
1045
1046
1047 /*
1048 * Return a Lang_function given the file suffix.
1049 */
1050 Lang_function *
1051 get_language_from_suffix (suffix)
1052 char *suffix;
1053 {
1054 struct lang_entry *lang;
1055 char **ext;
1056
1057 if (suffix == NULL)
1058 return NULL;
1059 for (lang = lang_names; lang->name != NULL; lang++)
1060 if (lang->suffixes != NULL)
1061 for (ext = lang->suffixes; *ext != NULL; ext++)
1062 if (streq (*ext, suffix))
1063 return lang->function;
1064
1065 return NULL;
1066 }
1067
1068
1069 /*
1070 * This routine is called on each file argument.
1071 */
1072 void
1073 process_file (file)
1074 char *file;
1075 {
1076 struct stat stat_buf;
1077 FILE *inf;
1078 #ifdef DOS_NT
1079 char *p;
1080
1081 for (p = file; *p != '\0'; p++)
1082 if (*p == '\\')
1083 *p = '/';
1084 #endif
1085
1086 if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
1087 {
1088 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
1089 return;
1090 }
1091 if (streq (file, tagfile) && !streq (tagfile, "-"))
1092 {
1093 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
1094 return;
1095 }
1096 inf = fopen (file, "r");
1097 if (inf == NULL)
1098 {
1099 perror (file);
1100 return;
1101 }
1102
1103 find_entries (file, inf);
1104
1105 if (!CTAGS)
1106 {
1107 char *filename;
1108
1109 if (absolutefn (file))
1110 {
1111 /* file is an absolute filename. Canonicalise it. */
1112 filename = absolute_filename (file, cwd);
1113 }
1114 else
1115 {
1116 /* file is a filename relative to cwd. Make it relative
1117 to the directory of the tags file. */
1118 filename = relative_filename (file, tagfiledir);
1119 }
1120 fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
1121 free (filename);
1122 put_entries (head);
1123 free_tree (head);
1124 head = NULL;
1125 }
1126 }
1127
1128 /*
1129 * This routine sets up the boolean pseudo-functions which work
1130 * by setting boolean flags dependent upon the corresponding character
1131 * Every char which is NOT in that string is not a white char. Therefore,
1132 * all of the array "_wht" is set to FALSE, and then the elements
1133 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1134 * of a char is TRUE if it is the string "white", else FALSE.
1135 */
1136 void
1137 init ()
1138 {
1139 register char *sp;
1140 register int i;
1141
1142 for (i = 0; i < 0177; i++)
1143 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
1144 for (sp = white; *sp; sp++)
1145 _wht[*sp] = TRUE;
1146 for (sp = endtk; *sp; sp++)
1147 _etk[*sp] = TRUE;
1148 for (sp = intk; *sp; sp++)
1149 _itk[*sp] = TRUE;
1150 for (sp = begtk; *sp; sp++)
1151 _btk[*sp] = TRUE;
1152 _wht[0] = _wht['\n'];
1153 _etk[0] = _etk['\n'];
1154 _btk[0] = _btk['\n'];
1155 _itk[0] = _itk['\n'];
1156 }
1157
1158 /*
1159 * This routine opens the specified file and calls the function
1160 * which finds the function and type definitions.
1161 */
1162 void
1163 find_entries (file, inf)
1164 char *file;
1165 FILE *inf;
1166 {
1167 char *cp;
1168 Lang_function *function;
1169 NODE *old_last_node;
1170 extern NODE *last_node;
1171
1172
1173 /* Memory leakage here: the memory block pointed by curfile is never
1174 released. The amount of memory leaked here is the sum of the
1175 lengths of the input file names. */
1176 curfile = savestr (file);
1177
1178 /* If user specified a language, use it. */
1179 function = lang_func;
1180 if (function != NULL)
1181 {
1182 function (inf);
1183 fclose (inf);
1184 return;
1185 }
1186
1187 cp = etags_strrchr (file, '.');
1188 if (cp != NULL)
1189 {
1190 cp += 1;
1191 function = get_language_from_suffix (cp);
1192 if (function != NULL)
1193 {
1194 function (inf);
1195 fclose (inf);
1196 return;
1197 }
1198 }
1199
1200 /* Look for sharp-bang as the first two characters. */
1201 if (readline_internal (&lb, inf) > 2
1202 && lb.buffer[0] == '#'
1203 && lb.buffer[1] == '!')
1204 {
1205 char *lp;
1206
1207 /* Set lp to point at the first char after the last slash in the
1208 line or, if no slashes, at the first nonblank. Then set cp to
1209 the first successive blank and terminate the string. */
1210 lp = etags_strrchr (lb.buffer+2, '/');
1211 if (lp != NULL)
1212 lp += 1;
1213 else
1214 for (lp = lb.buffer+2; *lp != '\0' && isspace (*lp); lp++)
1215 continue;
1216 for (cp = lp; *cp != '\0' && !isspace (*cp); cp++)
1217 continue;
1218 *cp = '\0';
1219
1220 if (strlen (lp) > 0)
1221 {
1222 function = get_language_from_interpreter (lp);
1223 if (function != NULL)
1224 {
1225 function (inf);
1226 fclose (inf);
1227 return;
1228 }
1229 }
1230 }
1231 rewind (inf);
1232
1233 /* Try Fortran. */
1234 old_last_node = last_node;
1235 Fortran_functions (inf);
1236
1237 /* No Fortran entries found. Try C. */
1238 if (old_last_node == last_node)
1239 {
1240 rewind (inf);
1241 default_C_entries (inf);
1242 }
1243 fclose (inf);
1244 return;
1245 }
1246 \f
1247 /* Record a tag. */
1248 void
1249 pfnote (name, is_func, linestart, linelen, lno, cno)
1250 char *name; /* tag name, or NULL if unnamed */
1251 logical is_func; /* tag is a function */
1252 char *linestart; /* start of the line where tag is */
1253 int linelen; /* length of the line where tag is */
1254 int lno; /* line number */
1255 long cno; /* character number */
1256 {
1257 register NODE *np;
1258
1259 if (CTAGS && name == NULL)
1260 return;
1261
1262 np = xnew (1, NODE);
1263
1264 /* If ctags mode, change name "main" to M<thisfilename>. */
1265 if (CTAGS && !cxref_style && streq (name, "main"))
1266 {
1267 register char *fp = etags_strrchr (curfile, '/');
1268 np->name = concat ("M", fp == 0 ? curfile : fp + 1, "");
1269 fp = etags_strrchr (np->name, '.');
1270 if (fp && fp[1] != '\0' && fp[2] == '\0')
1271 fp[0] = 0;
1272 }
1273 else
1274 np->name = name;
1275 np->been_warned = FALSE;
1276 np->file = curfile;
1277 np->is_func = is_func;
1278 np->lno = lno;
1279 /* Our char numbers are 0-base, because of C language tradition?
1280 ctags compatibility? old versions compatibility? I don't know.
1281 Anyway, since emacs's are 1-base we expect etags.el to take care
1282 of the difference. If we wanted to have 1-based numbers, we would
1283 uncomment the +1 below. */
1284 np->cno = cno /* + 1 */ ;
1285 np->left = np->right = NULL;
1286 if (CTAGS && !cxref_style)
1287 {
1288 if (strlen (linestart) < 50)
1289 np->pat = concat (linestart, "$", "");
1290 else
1291 np->pat = savenstr (linestart, 50);
1292 }
1293 else
1294 np->pat = savenstr (linestart, linelen);
1295
1296 add_node (np, &head);
1297 }
1298
1299 /*
1300 * free_tree ()
1301 * recurse on left children, iterate on right children.
1302 */
1303 void
1304 free_tree (node)
1305 register NODE *node;
1306 {
1307 while (node)
1308 {
1309 register NODE *node_right = node->right;
1310 free_tree (node->left);
1311 if (node->name != NULL)
1312 free (node->name);
1313 free (node->pat);
1314 free ((char *) node);
1315 node = node_right;
1316 }
1317 }
1318
1319 /*
1320 * add_node ()
1321 * Adds a node to the tree of nodes. In etags mode, we don't keep
1322 * it sorted; we just keep a linear list. In ctags mode, maintain
1323 * an ordered tree, with no attempt at balancing.
1324 *
1325 * add_node is the only function allowed to add nodes, so it can
1326 * maintain state.
1327 */
1328 NODE *last_node = NULL;
1329 void
1330 add_node (node, cur_node_p)
1331 NODE *node, **cur_node_p;
1332 {
1333 register int dif;
1334 register NODE *cur_node = *cur_node_p;
1335
1336 if (cur_node == NULL)
1337 {
1338 *cur_node_p = node;
1339 last_node = node;
1340 return;
1341 }
1342
1343 if (!CTAGS)
1344 {
1345 /* Etags Mode */
1346 if (last_node == NULL)
1347 fatal ("internal error in add_node", (char *)NULL);
1348 last_node->right = node;
1349 last_node = node;
1350 }
1351 else
1352 {
1353 /* Ctags Mode */
1354 dif = strcmp (node->name, cur_node->name);
1355
1356 /*
1357 * If this tag name matches an existing one, then
1358 * do not add the node, but maybe print a warning.
1359 */
1360 if (!dif)
1361 {
1362 if (streq (node->file, cur_node->file))
1363 {
1364 if (!no_warnings)
1365 {
1366 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1367 node->file, lineno, node->name);
1368 fprintf (stderr, "Second entry ignored\n");
1369 }
1370 }
1371 else if (!cur_node->been_warned && !no_warnings)
1372 {
1373 fprintf
1374 (stderr,
1375 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1376 node->file, cur_node->file, node->name);
1377 cur_node->been_warned = TRUE;
1378 }
1379 return;
1380 }
1381
1382 /* Actually add the node */
1383 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1384 }
1385 }
1386 \f
1387 void
1388 put_entries (node)
1389 register NODE *node;
1390 {
1391 register char *sp;
1392
1393 if (node == NULL)
1394 return;
1395
1396 /* Output subentries that precede this one */
1397 put_entries (node->left);
1398
1399 /* Output this entry */
1400
1401 if (!CTAGS)
1402 {
1403 if (node->name != NULL)
1404 fprintf (tagf, "%s\177%s\001%d,%d\n",
1405 node->pat, node->name, node->lno, node->cno);
1406 else
1407 fprintf (tagf, "%s\177%d,%d\n",
1408 node->pat, node->lno, node->cno);
1409 }
1410 else
1411 {
1412 if (node->name == NULL)
1413 error ("internal error: NULL name in ctags mode.", (char *)NULL);
1414
1415 if (cxref_style)
1416 {
1417 if (vgrind_style)
1418 fprintf (stdout, "%s %s %d\n",
1419 node->name, node->file, (node->lno + 63) / 64);
1420 else
1421 fprintf (stdout, "%-16s %3d %-16s %s\n",
1422 node->name, node->lno, node->file, node->pat);
1423 }
1424 else
1425 {
1426 fprintf (tagf, "%s\t%s\t", node->name, node->file);
1427
1428 if (node->is_func)
1429 { /* a function */
1430 putc (searchar, tagf);
1431 putc ('^', tagf);
1432
1433 for (sp = node->pat; *sp; sp++)
1434 {
1435 if (*sp == '\\' || *sp == searchar)
1436 putc ('\\', tagf);
1437 putc (*sp, tagf);
1438 }
1439 putc (searchar, tagf);
1440 }
1441 else
1442 { /* a typedef; text pattern inadequate */
1443 fprintf (tagf, "%d", node->lno);
1444 }
1445 putc ('\n', tagf);
1446 }
1447 }
1448
1449 /* Output subentries that follow this one */
1450 put_entries (node->right);
1451 }
1452
1453 /* Length of a number's decimal representation. */
1454 int
1455 number_len (num)
1456 long num;
1457 {
1458 int len = 0;
1459 if (!num)
1460 return 1;
1461 for (; num; num /= 10)
1462 ++len;
1463 return len;
1464 }
1465
1466 /*
1467 * Return total number of characters that put_entries will output for
1468 * the nodes in the subtree of the specified node. Works only if
1469 * we are not ctags, but called only in that case. This count
1470 * is irrelevant with the new tags.el, but is still supplied for
1471 * backward compatibility.
1472 */
1473 int
1474 total_size_of_entries (node)
1475 register NODE *node;
1476 {
1477 register int total;
1478
1479 if (node == NULL)
1480 return 0;
1481
1482 total = 0;
1483 for (; node; node = node->right)
1484 {
1485 /* Count left subentries. */
1486 total += total_size_of_entries (node->left);
1487
1488 /* Count this entry */
1489 total += strlen (node->pat) + 1;
1490 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1491 if (node->name != NULL)
1492 total += 1 + strlen (node->name); /* \001name */
1493 }
1494
1495 return total;
1496 }
1497 \f
1498 /*
1499 * The C symbol tables.
1500 */
1501 enum sym_type
1502 {
1503 st_none, st_C_objprot, st_C_objimpl, st_C_objend, st_C_gnumacro,
1504 st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
1505 };
1506
1507 /* Feed stuff between (but not including) %[ and %] lines to:
1508 gperf -c -k 1,3 -o -p -r -t
1509 %[
1510 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1511 %%
1512 @interface, 0, st_C_objprot
1513 @protocol, 0, st_C_objprot
1514 @implementation,0, st_C_objimpl
1515 @end, 0, st_C_objend
1516 class, C_PLPL, st_C_struct
1517 namespace, C_PLPL, st_C_struct
1518 domain, C_STAR, st_C_struct
1519 union, 0, st_C_struct
1520 struct, 0, st_C_struct
1521 enum, 0, st_C_enum
1522 typedef, 0, st_C_typedef
1523 define, 0, st_C_define
1524 bool, C_PLPL, st_C_typespec
1525 long, 0, st_C_typespec
1526 short, 0, st_C_typespec
1527 int, 0, st_C_typespec
1528 char, 0, st_C_typespec
1529 float, 0, st_C_typespec
1530 double, 0, st_C_typespec
1531 signed, 0, st_C_typespec
1532 unsigned, 0, st_C_typespec
1533 auto, 0, st_C_typespec
1534 void, 0, st_C_typespec
1535 extern, 0, st_C_typespec
1536 static, 0, st_C_typespec
1537 const, 0, st_C_typespec
1538 volatile, 0, st_C_typespec
1539 explicit, C_PLPL, st_C_typespec
1540 mutable, C_PLPL, st_C_typespec
1541 typename, C_PLPL, st_C_typespec
1542 # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
1543 DEFUN, 0, st_C_gnumacro
1544 SYSCALL, 0, st_C_gnumacro
1545 ENTRY, 0, st_C_gnumacro
1546 PSEUDO, 0, st_C_gnumacro
1547 # These are defined inside C functions, so currently they are not met.
1548 # EXFUN used in glibc, DEFVAR_* in emacs.
1549 #EXFUN, 0, st_C_gnumacro
1550 #DEFVAR_, 0, st_C_gnumacro
1551 %]
1552 and replace lines between %< and %> with its output. */
1553 /*%<*/
1554 /* C code produced by gperf version 2.1 (K&R C version) */
1555 /* Command-line: gperf -c -k 1,3 -o -p -r -t */
1556
1557
1558 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
1559
1560 #define MIN_WORD_LENGTH 3
1561 #define MAX_WORD_LENGTH 15
1562 #define MIN_HASH_VALUE 34
1563 #define MAX_HASH_VALUE 121
1564 /*
1565 34 keywords
1566 88 is the maximum key range
1567 */
1568
1569 static int
1570 hash (str, len)
1571 register char *str;
1572 register unsigned int len;
1573 {
1574 static unsigned char hash_table[] =
1575 {
1576 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1577 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1578 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1579 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1580 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1581 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1582 121, 121, 121, 121, 45, 121, 121, 121, 16, 19,
1583 61, 121, 121, 121, 121, 121, 121, 121, 121, 121,
1584 10, 121, 121, 20, 53, 121, 121, 121, 121, 121,
1585 121, 121, 121, 121, 121, 121, 121, 41, 45, 22,
1586 60, 47, 37, 28, 121, 55, 121, 121, 20, 14,
1587 29, 30, 5, 121, 50, 59, 30, 54, 6, 121,
1588 121, 121, 121, 121, 121, 121, 121, 121,
1589 };
1590 return len + hash_table[str[2]] + hash_table[str[0]];
1591 }
1592
1593 struct C_stab_entry *
1594 in_word_set (str, len)
1595 register char *str;
1596 register unsigned int len;
1597 {
1598
1599 static struct C_stab_entry wordlist[] =
1600 {
1601 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1602 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1603 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1604 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1605 {"volatile", 0, st_C_typespec},
1606 {"PSEUDO", 0, st_C_gnumacro},
1607 {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1608 {"typedef", 0, st_C_typedef},
1609 {"typename", C_PLPL, st_C_typespec},
1610 {"",}, {"",}, {"",},
1611 {"SYSCALL", 0, st_C_gnumacro},
1612 {"",}, {"",}, {"",},
1613 {"mutable", C_PLPL, st_C_typespec},
1614 {"namespace", C_PLPL, st_C_struct},
1615 {"long", 0, st_C_typespec},
1616 {"",}, {"",},
1617 {"const", 0, st_C_typespec},
1618 {"",}, {"",}, {"",},
1619 {"explicit", C_PLPL, st_C_typespec},
1620 {"",}, {"",}, {"",}, {"",},
1621 {"void", 0, st_C_typespec},
1622 {"",},
1623 {"char", 0, st_C_typespec},
1624 {"class", C_PLPL, st_C_struct},
1625 {"",}, {"",}, {"",},
1626 {"float", 0, st_C_typespec},
1627 {"",},
1628 {"@implementation", 0, st_C_objimpl},
1629 {"auto", 0, st_C_typespec},
1630 {"",},
1631 {"ENTRY", 0, st_C_gnumacro},
1632 {"@end", 0, st_C_objend},
1633 {"bool", C_PLPL, st_C_typespec},
1634 {"domain", C_STAR, st_C_struct},
1635 {"",},
1636 {"DEFUN", 0, st_C_gnumacro},
1637 {"extern", 0, st_C_typespec},
1638 {"@interface", 0, st_C_objprot},
1639 {"",}, {"",}, {"",},
1640 {"int", 0, st_C_typespec},
1641 {"",}, {"",}, {"",}, {"",},
1642 {"signed", 0, st_C_typespec},
1643 {"short", 0, st_C_typespec},
1644 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1645 {"define", 0, st_C_define},
1646 {"@protocol", 0, st_C_objprot},
1647 {"enum", 0, st_C_enum},
1648 {"static", 0, st_C_typespec},
1649 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1650 {"union", 0, st_C_struct},
1651 {"struct", 0, st_C_struct},
1652 {"",}, {"",}, {"",}, {"",},
1653 {"double", 0, st_C_typespec},
1654 {"unsigned", 0, st_C_typespec},
1655 };
1656
1657 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
1658 {
1659 register int key = hash (str, len);
1660
1661 if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
1662 {
1663 register char *s = wordlist[key].name;
1664
1665 if (*s == *str && !strncmp (str + 1, s + 1, len - 1))
1666 return &wordlist[key];
1667 }
1668 }
1669 return 0;
1670 }
1671 /*%>*/
1672
1673 enum sym_type
1674 C_symtype (str, len, c_ext)
1675 char *str;
1676 int len;
1677 int c_ext;
1678 {
1679 register struct C_stab_entry *se = in_word_set (str, len);
1680
1681 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
1682 return st_none;
1683 return se->type;
1684 }
1685 \f
1686 /*
1687 * C functions are recognized using a simple finite automaton.
1688 * funcdef is its state variable.
1689 */
1690 enum
1691 {
1692 fnone, /* nothing seen */
1693 ftagseen, /* function-like tag seen */
1694 fstartlist, /* just after open parenthesis */
1695 finlist, /* in parameter list */
1696 flistseen, /* after parameter list */
1697 fignore /* before open brace */
1698 } funcdef;
1699
1700
1701 /*
1702 * typedefs are recognized using a simple finite automaton.
1703 * typdef is its state variable.
1704 */
1705 enum
1706 {
1707 tnone, /* nothing seen */
1708 ttypedseen, /* typedef keyword seen */
1709 tinbody, /* inside typedef body */
1710 tend, /* just before typedef tag */
1711 tignore /* junk after typedef tag */
1712 } typdef;
1713
1714
1715 /*
1716 * struct-like structures (enum, struct and union) are recognized
1717 * using another simple finite automaton. `structdef' is its state
1718 * variable.
1719 */
1720 enum
1721 {
1722 snone, /* nothing seen yet */
1723 skeyseen, /* struct-like keyword seen */
1724 stagseen, /* struct-like tag seen */
1725 scolonseen, /* colon seen after struct-like tag */
1726 sinbody /* in struct body: recognize member func defs*/
1727 } structdef;
1728
1729 /*
1730 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1731 * struct tag, and structtype is the type of the preceding struct-like
1732 * keyword.
1733 */
1734 char *structtag = "<uninited>";
1735 enum sym_type structtype;
1736
1737 /*
1738 * When objdef is different from onone, objtag is the name of the class.
1739 */
1740 char *objtag = "<uninited>";
1741
1742 /*
1743 * Yet another little state machine to deal with preprocessor lines.
1744 */
1745 enum
1746 {
1747 dnone, /* nothing seen */
1748 dsharpseen, /* '#' seen as first char on line */
1749 ddefineseen, /* '#' and 'define' seen */
1750 dignorerest /* ignore rest of line */
1751 } definedef;
1752
1753 /*
1754 * State machine for Objective C protocols and implementations.
1755 */
1756 enum
1757 {
1758 onone, /* nothing seen */
1759 oprotocol, /* @interface or @protocol seen */
1760 oimplementation, /* @implementations seen */
1761 otagseen, /* class name seen */
1762 oparenseen, /* parenthesis before category seen */
1763 ocatseen, /* category name seen */
1764 oinbody, /* in @implementation body */
1765 omethodsign, /* in @implementation body, after +/- */
1766 omethodtag, /* after method name */
1767 omethodcolon, /* after method colon */
1768 omethodparm, /* after method parameter */
1769 oignore /* wait for @end */
1770 } objdef;
1771
1772 /*
1773 * Set this to TRUE, and the next token considered is called a function.
1774 * Used only for GNU emacs's function-defining macros.
1775 */
1776 logical next_token_is_func;
1777
1778 /*
1779 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1780 */
1781 logical yacc_rules;
1782
1783 /*
1784 * methodlen is the length of the method name stored in token_name.
1785 */
1786 int methodlen;
1787
1788 /*
1789 * consider_token ()
1790 * checks to see if the current token is at the start of a
1791 * function, or corresponds to a typedef, or is a struct/union/enum
1792 * tag, or #define, or an enum constant.
1793 *
1794 * *IS_FUNC gets TRUE iff the token is a function or #define macro
1795 * with args. C_EXT is which language we are looking at.
1796 *
1797 * In the future we will need some way to adjust where the end of
1798 * the token is; for instance, implementing the C++ keyword
1799 * `operator' properly will adjust the end of the token to be after
1800 * whatever follows `operator'.
1801 *
1802 * Globals
1803 * funcdef IN OUT
1804 * structdef IN OUT
1805 * definedef IN OUT
1806 * typdef IN OUT
1807 * objdef IN OUT
1808 * next_token_is_func IN OUT
1809 */
1810
1811 logical
1812 consider_token (str, len, c, c_ext, cblev, parlev, is_func)
1813 register char *str; /* IN: token pointer */
1814 register int len; /* IN: token length */
1815 register char c; /* IN: first char after the token */
1816 int c_ext; /* IN: C extensions mask */
1817 int cblev; /* IN: curly brace level */
1818 int parlev; /* IN: parenthesis level */
1819 logical *is_func; /* OUT: function found */
1820 {
1821 enum sym_type toktype = C_symtype (str, len, c_ext);
1822
1823 /*
1824 * Advance the definedef state machine.
1825 */
1826 switch (definedef)
1827 {
1828 case dnone:
1829 /* We're not on a preprocessor line. */
1830 break;
1831 case dsharpseen:
1832 if (toktype == st_C_define)
1833 {
1834 definedef = ddefineseen;
1835 }
1836 else
1837 {
1838 definedef = dignorerest;
1839 }
1840 return FALSE;
1841 case ddefineseen:
1842 /*
1843 * Make a tag for any macro, unless it is a constant
1844 * and constantypedefs is FALSE.
1845 */
1846 definedef = dignorerest;
1847 *is_func = (c == '(');
1848 if (!*is_func && !constantypedefs)
1849 return FALSE;
1850 else
1851 return TRUE;
1852 case dignorerest:
1853 return FALSE;
1854 default:
1855 error ("internal error: definedef value.", (char *)NULL);
1856 }
1857
1858 /*
1859 * Now typedefs
1860 */
1861 switch (typdef)
1862 {
1863 case tnone:
1864 if (toktype == st_C_typedef)
1865 {
1866 if (typedefs)
1867 typdef = ttypedseen;
1868 funcdef = fnone;
1869 return FALSE;
1870 }
1871 break;
1872 case ttypedseen:
1873 switch (toktype)
1874 {
1875 case st_none:
1876 case st_C_typespec:
1877 typdef = tend;
1878 break;
1879 case st_C_struct:
1880 case st_C_enum:
1881 break;
1882 }
1883 /* Do not return here, so the structdef stuff has a chance. */
1884 break;
1885 case tend:
1886 switch (toktype)
1887 {
1888 case st_C_typespec:
1889 case st_C_struct:
1890 case st_C_enum:
1891 return FALSE;
1892 }
1893 return TRUE;
1894 }
1895
1896 /*
1897 * This structdef business is currently only invoked when cblev==0.
1898 * It should be recursively invoked whatever the curly brace level,
1899 * and a stack of states kept, to allow for definitions of structs
1900 * within structs.
1901 *
1902 * This structdef business is NOT invoked when we are ctags and the
1903 * file is plain C. This is because a struct tag may have the same
1904 * name as another tag, and this loses with ctags.
1905 */
1906 switch (toktype)
1907 {
1908 case st_C_struct:
1909 case st_C_enum:
1910 if (typdef == ttypedseen
1911 || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
1912 {
1913 structdef = skeyseen;
1914 structtype = toktype;
1915 }
1916 return FALSE;
1917 }
1918
1919 if (structdef == skeyseen)
1920 {
1921 /* Save the tag for struct/union/class, for functions that may be
1922 defined inside. */
1923 if (structtype == st_C_struct)
1924 structtag = savenstr (str, len);
1925 else
1926 structtag = "<enum>";
1927 structdef = stagseen;
1928 return TRUE;
1929 }
1930
1931 /* Avoid entering funcdef stuff if typdef is going on. */
1932 if (typdef != tnone)
1933 {
1934 definedef = dnone;
1935 return FALSE;
1936 }
1937
1938 /* Detect GNU macros.
1939
1940 DEFUN note for writers of emacs C code:
1941 The DEFUN macro, used in emacs C source code, has a first arg
1942 that is a string (the lisp function name), and a second arg that
1943 is a C function name. Since etags skips strings, the second arg
1944 is tagged. This is unfortunate, as it would be better to tag the
1945 first arg. The simplest way to deal with this problem would be
1946 to name the tag with a name built from the function name, by
1947 removing the initial 'F' character and substituting '-' for '_'.
1948 Anyway, this assumes that the conventions of naming lisp
1949 functions will never change. Currently, this method is not
1950 implemented, so writers of emacs code are recommended to put the
1951 first two args of a DEFUN on the same line. */
1952 if (definedef == dnone && toktype == st_C_gnumacro)
1953 {
1954 next_token_is_func = TRUE;
1955 return FALSE;
1956 }
1957 if (next_token_is_func)
1958 {
1959 next_token_is_func = FALSE;
1960 funcdef = fignore;
1961 *is_func = TRUE;
1962 return TRUE;
1963 }
1964
1965 /* Detect Objective C constructs. */
1966 switch (objdef)
1967 {
1968 case onone:
1969 switch (toktype)
1970 {
1971 case st_C_objprot:
1972 objdef = oprotocol;
1973 return FALSE;
1974 case st_C_objimpl:
1975 objdef = oimplementation;
1976 return FALSE;
1977 }
1978 break;
1979 case oimplementation:
1980 /* Save the class tag for functions that may be defined inside. */
1981 objtag = savenstr (str, len);
1982 objdef = oinbody;
1983 return FALSE;
1984 case oprotocol:
1985 /* Save the class tag for categories. */
1986 objtag = savenstr (str, len);
1987 objdef = otagseen;
1988 *is_func = TRUE;
1989 return TRUE;
1990 case oparenseen:
1991 objdef = ocatseen;
1992 *is_func = TRUE;
1993 return TRUE;
1994 case oinbody:
1995 break;
1996 case omethodsign:
1997 if (parlev == 0)
1998 {
1999 objdef = omethodtag;
2000 methodlen = len;
2001 grow_linebuffer (&token_name, methodlen+1);
2002 strncpy (token_name.buffer, str, len);
2003 token_name.buffer[methodlen] = '\0';
2004 return TRUE;
2005 }
2006 return FALSE;
2007 case omethodcolon:
2008 if (parlev == 0)
2009 objdef = omethodparm;
2010 return FALSE;
2011 case omethodparm:
2012 if (parlev == 0)
2013 {
2014 objdef = omethodtag;
2015 methodlen += len;
2016 grow_linebuffer (&token_name, methodlen+1);
2017 strncat (token_name.buffer, str, len);
2018 return TRUE;
2019 }
2020 return FALSE;
2021 case oignore:
2022 if (toktype == st_C_objend)
2023 {
2024 /* Memory leakage here: the string pointed by objtag is
2025 never released, because many tests would be needed to
2026 avoid breaking on incorrect input code. The amount of
2027 memory leaked here is the sum of the lengths of the
2028 class tags.
2029 free (objtag); */
2030 objdef = onone;
2031 }
2032 return FALSE;
2033 }
2034
2035 /* A function or enum constant? */
2036 switch (toktype)
2037 {
2038 case st_C_typespec:
2039 if (funcdef != finlist && funcdef != fignore)
2040 funcdef = fnone; /* should be useless */
2041 return FALSE;
2042 case st_none:
2043 if (constantypedefs && structdef == sinbody && structtype == st_C_enum)
2044 return TRUE;
2045 if (funcdef == fnone)
2046 {
2047 funcdef = ftagseen;
2048 *is_func = TRUE;
2049 return TRUE;
2050 }
2051 }
2052
2053 return FALSE;
2054 }
2055
2056 /*
2057 * C_entries ()
2058 * This routine finds functions, typedefs, #define's, enum
2059 * constants and struct/union/enum definitions in C syntax
2060 * and adds them to the list.
2061 */
2062 typedef struct
2063 {
2064 logical valid;
2065 char *str;
2066 logical named;
2067 int linelen;
2068 int lineno;
2069 long linepos;
2070 char *buffer;
2071 } TOKEN;
2072
2073 #define current_lb_is_new (newndx == curndx)
2074 #define switch_line_buffers() (curndx = 1 - curndx)
2075
2076 #define curlb (lbs[curndx].lb)
2077 #define othlb (lbs[1-curndx].lb)
2078 #define newlb (lbs[newndx].lb)
2079 #define curlinepos (lbs[curndx].linepos)
2080 #define othlinepos (lbs[1-curndx].linepos)
2081 #define newlinepos (lbs[newndx].linepos)
2082
2083 #define CNL_SAVE_DEFINEDEF \
2084 do { \
2085 curlinepos = charno; \
2086 lineno++; \
2087 linecharno = charno; \
2088 charno += readline (&curlb, inf); \
2089 lp = curlb.buffer; \
2090 quotednl = FALSE; \
2091 newndx = curndx; \
2092 } while (0)
2093
2094 #define CNL \
2095 do { \
2096 CNL_SAVE_DEFINEDEF; \
2097 if (savetok.valid) \
2098 { \
2099 tok = savetok; \
2100 savetok.valid = FALSE; \
2101 } \
2102 definedef = dnone; \
2103 } while (0)
2104
2105
2106 void
2107 make_C_tag (isfun, tokp)
2108 logical isfun;
2109 TOKEN *tokp;
2110 {
2111 char *name = NULL;
2112
2113 /* This function should never be called when tok.valid is FALSE, but
2114 we must protect against invalid input or internal errors. */
2115 if (tokp->valid)
2116 {
2117 if (CTAGS || tokp->named)
2118 name = savestr (token_name.buffer);
2119 pfnote (name, isfun,
2120 tokp->buffer, tokp->linelen, tokp->lineno, tokp->linepos);
2121 tokp->valid = FALSE;
2122 }
2123 else if (DEBUG)
2124 abort ();
2125 }
2126
2127
2128 void
2129 C_entries (c_ext, inf)
2130 int c_ext; /* extension of C */
2131 FILE *inf; /* input file */
2132 {
2133 register char c; /* latest char read; '\0' for end of line */
2134 register char *lp; /* pointer one beyond the character `c' */
2135 int curndx, newndx; /* indices for current and new lb */
2136 TOKEN tok; /* latest token read */
2137 register int tokoff; /* offset in line of start of current token */
2138 register int toklen; /* length of current token */
2139 int cblev; /* current curly brace level */
2140 int parlev; /* current parenthesis level */
2141 logical incomm, inquote, inchar, quotednl, midtoken;
2142 logical cplpl;
2143 TOKEN savetok; /* token saved during preprocessor handling */
2144
2145
2146 curndx = newndx = 0;
2147 lineno = 0;
2148 charno = 0;
2149 lp = curlb.buffer;
2150 *lp = 0;
2151
2152 funcdef = fnone; typdef = tnone; structdef = snone;
2153 definedef = dnone; objdef = onone;
2154 next_token_is_func = yacc_rules = FALSE;
2155 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2156 tok.valid = savetok.valid = FALSE;
2157 cblev = 0;
2158 parlev = 0;
2159 cplpl = c_ext & C_PLPL;
2160
2161 while (!feof (inf))
2162 {
2163 c = *lp++;
2164 if (c == '\\')
2165 {
2166 /* If we're at the end of the line, the next character is a
2167 '\0'; don't skip it, because it's the thing that tells us
2168 to read the next line. */
2169 if (*lp == '\0')
2170 {
2171 quotednl = TRUE;
2172 continue;
2173 }
2174 lp++;
2175 c = ' ';
2176 }
2177 else if (incomm)
2178 {
2179 switch (c)
2180 {
2181 case '*':
2182 if (*lp == '/')
2183 {
2184 c = *lp++;
2185 incomm = FALSE;
2186 }
2187 break;
2188 case '\0':
2189 /* Newlines inside comments do not end macro definitions in
2190 traditional cpp. */
2191 CNL_SAVE_DEFINEDEF;
2192 break;
2193 }
2194 continue;
2195 }
2196 else if (inquote)
2197 {
2198 switch (c)
2199 {
2200 case '"':
2201 inquote = FALSE;
2202 break;
2203 case '\0':
2204 /* Newlines inside strings do not end macro definitions
2205 in traditional cpp, even though compilers don't
2206 usually accept them. */
2207 CNL_SAVE_DEFINEDEF;
2208 break;
2209 }
2210 continue;
2211 }
2212 else if (inchar)
2213 {
2214 switch (c)
2215 {
2216 case '\0':
2217 /* Hmmm, something went wrong. */
2218 CNL;
2219 /* FALLTHRU */
2220 case '\'':
2221 inchar = FALSE;
2222 break;
2223 }
2224 continue;
2225 }
2226 else
2227 switch (c)
2228 {
2229 case '"':
2230 inquote = TRUE;
2231 if (funcdef != finlist && funcdef != fignore)
2232 funcdef = fnone;
2233 continue;
2234 case '\'':
2235 inchar = TRUE;
2236 if (funcdef != finlist && funcdef != fignore)
2237 funcdef = fnone;
2238 continue;
2239 case '/':
2240 if (*lp == '*')
2241 {
2242 lp++;
2243 incomm = TRUE;
2244 continue;
2245 }
2246 else if (/* cplpl && */ *lp == '/')
2247 {
2248 c = '\0';
2249 break;
2250 }
2251 else
2252 break;
2253 case '%':
2254 if ((c_ext & YACC) && *lp == '%')
2255 {
2256 /* entering or exiting rules section in yacc file */
2257 lp++;
2258 definedef = dnone; funcdef = fnone;
2259 typdef = tnone; structdef = snone;
2260 next_token_is_func = FALSE;
2261 midtoken = inquote = inchar = incomm = quotednl = FALSE;
2262 cblev = 0;
2263 yacc_rules = !yacc_rules;
2264 continue;
2265 }
2266 else
2267 break;
2268 case '#':
2269 if (definedef == dnone)
2270 {
2271 char *cp;
2272 logical cpptoken = TRUE;
2273
2274 /* Look back on this line. If all blanks, or nonblanks
2275 followed by an end of comment, this is a preprocessor
2276 token. */
2277 for (cp = newlb.buffer; cp < lp-1; cp++)
2278 if (!iswhite (*cp))
2279 {
2280 if (*cp == '*' && *(cp+1) == '/')
2281 {
2282 cp++;
2283 cpptoken = TRUE;
2284 }
2285 else
2286 cpptoken = FALSE;
2287 }
2288 if (cpptoken)
2289 definedef = dsharpseen;
2290 } /* if (definedef == dnone) */
2291
2292 continue;
2293 } /* switch (c) */
2294
2295
2296 /* Consider token only if some complicated conditions are satisfied. */
2297 if ((definedef != dnone
2298 || (cblev == 0 && structdef != scolonseen)
2299 || (cblev == 1 && cplpl && structdef == sinbody)
2300 || (structdef == sinbody && structtype == st_C_enum))
2301 && typdef != tignore
2302 && definedef != dignorerest
2303 && funcdef != finlist)
2304 {
2305 if (midtoken)
2306 {
2307 if (endtoken (c))
2308 {
2309 if (c == ':' && cplpl && *lp == ':' && begtoken(*(lp + 1)))
2310 {
2311 /*
2312 * This handles :: in the middle, but not at the
2313 * beginning of an identifier.
2314 */
2315 lp += 2;
2316 toklen += 3;
2317 }
2318 else
2319 {
2320 logical is_func = FALSE;
2321
2322 if (yacc_rules
2323 || consider_token (newlb.buffer + tokoff, toklen, c,
2324 c_ext, cblev, parlev, &is_func))
2325 {
2326 if (structdef == sinbody
2327 && definedef == dnone
2328 && is_func)
2329 /* function defined in C++ class body */
2330 {
2331 grow_linebuffer (&token_name,
2332 strlen(structtag)+2+toklen+1);
2333 strcpy (token_name.buffer, structtag);
2334 strcat (token_name.buffer, "::");
2335 strncat (token_name.buffer,
2336 newlb.buffer+tokoff, toklen);
2337 tok.named = TRUE;
2338 }
2339 else if (objdef == ocatseen)
2340 /* Objective C category */
2341 {
2342 grow_linebuffer (&token_name,
2343 strlen(objtag)+2+toklen+1);
2344 strcpy (token_name.buffer, objtag);
2345 strcat (token_name.buffer, "(");
2346 strncat (token_name.buffer,
2347 newlb.buffer+tokoff, toklen);
2348 strcat (token_name.buffer, ")");
2349 tok.named = TRUE;
2350 }
2351 else if (objdef == omethodtag
2352 || objdef == omethodparm)
2353 /* Objective C method */
2354 {
2355 tok.named = TRUE;
2356 }
2357 else
2358 {
2359 grow_linebuffer (&token_name, toklen+1);
2360 strncpy (token_name.buffer,
2361 newlb.buffer+tokoff, toklen);
2362 token_name.buffer[toklen] = '\0';
2363 if (structdef == stagseen
2364 || typdef == tend
2365 || (is_func
2366 && definedef == dignorerest)) /* macro */
2367 tok.named = TRUE;
2368 else
2369 tok.named = FALSE;
2370 }
2371 tok.lineno = lineno;
2372 tok.linelen = tokoff + toklen + 1;
2373 tok.buffer = newlb.buffer;
2374 tok.linepos = newlinepos;
2375 tok.valid = TRUE;
2376
2377 if (definedef == dnone
2378 && (funcdef == ftagseen
2379 || structdef == stagseen
2380 || typdef == tend
2381 || objdef != onone))
2382 {
2383 if (current_lb_is_new)
2384 switch_line_buffers ();
2385 }
2386 else
2387 make_C_tag (is_func, &tok);
2388 }
2389 midtoken = FALSE;
2390 }
2391 } /* if (endtoken (c)) */
2392 else if (intoken (c))
2393 {
2394 toklen++;
2395 continue;
2396 }
2397 } /* if (midtoken) */
2398 else if (begtoken (c))
2399 {
2400 switch (definedef)
2401 {
2402 case dnone:
2403 switch (funcdef)
2404 {
2405 case fstartlist:
2406 funcdef = finlist;
2407 continue;
2408 case flistseen:
2409 make_C_tag (TRUE, &tok);
2410 funcdef = fignore;
2411 break;
2412 case ftagseen:
2413 funcdef = fnone;
2414 break;
2415 }
2416 if (structdef == stagseen)
2417 structdef = snone;
2418 break;
2419 case dsharpseen:
2420 savetok = tok;
2421 }
2422 if (!yacc_rules || lp == newlb.buffer + 1)
2423 {
2424 tokoff = lp - 1 - newlb.buffer;
2425 toklen = 1;
2426 midtoken = TRUE;
2427 }
2428 continue;
2429 } /* if (begtoken) */
2430 } /* if must look at token */
2431
2432
2433 /* Detect end of line, colon, comma, semicolon and various braces
2434 after having handled a token.*/
2435 switch (c)
2436 {
2437 case ':':
2438 if (definedef != dnone)
2439 break;
2440 switch (objdef)
2441 {
2442 case otagseen:
2443 objdef = oignore;
2444 make_C_tag (TRUE, &tok);
2445 break;
2446 case omethodtag:
2447 case omethodparm:
2448 objdef = omethodcolon;
2449 methodlen += 1;
2450 grow_linebuffer (&token_name, methodlen+1);
2451 strcat (token_name.buffer, ":");
2452 break;
2453 }
2454 if (structdef == stagseen)
2455 structdef = scolonseen;
2456 else
2457 switch (funcdef)
2458 {
2459 case ftagseen:
2460 if (yacc_rules)
2461 {
2462 make_C_tag (FALSE, &tok);
2463 funcdef = fignore;
2464 }
2465 break;
2466 case fstartlist:
2467 funcdef = fnone;
2468 break;
2469 }
2470 break;
2471 case ';':
2472 if (definedef != dnone)
2473 break;
2474 if (cblev == 0)
2475 switch (typdef)
2476 {
2477 case tend:
2478 make_C_tag (FALSE, &tok);
2479 /* FALLTHRU */
2480 default:
2481 typdef = tnone;
2482 }
2483 if (funcdef != fignore)
2484 {
2485 funcdef = fnone;
2486 /* The following instruction invalidates the token.
2487 Probably the token should be invalidated in all
2488 other cases where some state machine is reset. */
2489 tok.valid = FALSE;
2490 }
2491 if (structdef == stagseen)
2492 structdef = snone;
2493 break;
2494 case ',':
2495 if (definedef != dnone)
2496 break;
2497 switch (objdef)
2498 {
2499 case omethodtag:
2500 case omethodparm:
2501 make_C_tag (TRUE, &tok);
2502 objdef = oinbody;
2503 break;
2504 }
2505 if (funcdef != finlist && funcdef != fignore)
2506 funcdef = fnone;
2507 if (structdef == stagseen)
2508 structdef = snone;
2509 break;
2510 case '[':
2511 if (definedef != dnone)
2512 break;
2513 if (cblev == 0 && typdef == tend)
2514 {
2515 typdef = tignore;
2516 make_C_tag (FALSE, &tok);
2517 break;
2518 }
2519 if (funcdef != finlist && funcdef != fignore)
2520 funcdef = fnone;
2521 if (structdef == stagseen)
2522 structdef = snone;
2523 break;
2524 case '(':
2525 if (definedef != dnone)
2526 break;
2527 if (objdef == otagseen && parlev == 0)
2528 objdef = oparenseen;
2529 switch (funcdef)
2530 {
2531 case fnone:
2532 switch (typdef)
2533 {
2534 case ttypedseen:
2535 case tend:
2536 /* Make sure that the next char is not a '*'.
2537 This handles constructs like:
2538 typedef void OperatorFun (int fun); */
2539 if (tok.valid && *lp != '*')
2540 {
2541 typdef = tignore;
2542 make_C_tag (FALSE, &tok);
2543 }
2544 break;
2545 } /* switch (typdef) */
2546 break;
2547 case ftagseen:
2548 funcdef = fstartlist;
2549 break;
2550 case flistseen:
2551 funcdef = finlist;
2552 break;
2553 }
2554 parlev++;
2555 break;
2556 case ')':
2557 if (definedef != dnone)
2558 break;
2559 if (objdef == ocatseen && parlev == 1)
2560 {
2561 make_C_tag (TRUE, &tok);
2562 objdef = oignore;
2563 }
2564 if (--parlev == 0)
2565 {
2566 switch (funcdef)
2567 {
2568 case fstartlist:
2569 case finlist:
2570 funcdef = flistseen;
2571 break;
2572 }
2573 if (cblev == 0 && typdef == tend)
2574 {
2575 typdef = tignore;
2576 make_C_tag (FALSE, &tok);
2577 }
2578 }
2579 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
2580 parlev = 0;
2581 break;
2582 case '{':
2583 if (definedef != dnone)
2584 break;
2585 if (typdef == ttypedseen)
2586 typdef = tinbody;
2587 switch (structdef)
2588 {
2589 case skeyseen: /* unnamed struct */
2590 structdef = sinbody;
2591 structtag = "_anonymous_";
2592 break;
2593 case stagseen:
2594 case scolonseen: /* named struct */
2595 structdef = sinbody;
2596 make_C_tag (FALSE, &tok);
2597 break;
2598 }
2599 switch (funcdef)
2600 {
2601 case flistseen:
2602 make_C_tag (TRUE, &tok);
2603 /* FALLTHRU */
2604 case fignore:
2605 funcdef = fnone;
2606 break;
2607 case fnone:
2608 switch (objdef)
2609 {
2610 case otagseen:
2611 make_C_tag (TRUE, &tok);
2612 objdef = oignore;
2613 break;
2614 case omethodtag:
2615 case omethodparm:
2616 make_C_tag (TRUE, &tok);
2617 objdef = oinbody;
2618 break;
2619 default:
2620 /* Neutralize `extern "C" {' grot. */
2621 if (cblev == 0 && structdef == snone && typdef == tnone)
2622 cblev = -1;
2623 }
2624 }
2625 cblev++;
2626 break;
2627 case '*':
2628 if (definedef != dnone)
2629 break;
2630 if (funcdef == fstartlist)
2631 funcdef = fnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
2632 break;
2633 case '}':
2634 if (definedef != dnone)
2635 break;
2636 if (!noindentypedefs && lp == newlb.buffer + 1)
2637 {
2638 cblev = 0; /* reset curly brace level if first column */
2639 parlev = 0; /* also reset paren level, just in case... */
2640 }
2641 else if (cblev > 0)
2642 cblev--;
2643 if (cblev == 0)
2644 {
2645 if (typdef == tinbody)
2646 typdef = tend;
2647 /* Memory leakage here: the string pointed by structtag is
2648 never released, because I fear to miss something and
2649 break things while freeing the area. The amount of
2650 memory leaked here is the sum of the lengths of the
2651 struct tags.
2652 if (structdef == sinbody)
2653 free (structtag); */
2654
2655 structdef = snone;
2656 structtag = "<error>";
2657 }
2658 break;
2659 case '+':
2660 case '-':
2661 if (objdef == oinbody && cblev == 0)
2662 {
2663 objdef = omethodsign;
2664 break;
2665 }
2666 /* FALLTHRU */
2667 case '=': case '#': case '~': case '&': case '%': case '/':
2668 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2669 if (definedef != dnone)
2670 break;
2671 /* These surely cannot follow a function tag. */
2672 if (funcdef != finlist && funcdef != fignore)
2673 funcdef = fnone;
2674 break;
2675 case '\0':
2676 if (objdef == otagseen)
2677 {
2678 make_C_tag (TRUE, &tok);
2679 objdef = oignore;
2680 }
2681 /* If a macro spans multiple lines don't reset its state. */
2682 if (quotednl)
2683 CNL_SAVE_DEFINEDEF;
2684 else
2685 CNL;
2686 break;
2687 } /* switch (c) */
2688
2689 } /* while not eof */
2690 }
2691
2692 /*
2693 * Process either a C++ file or a C file depending on the setting
2694 * of a global flag.
2695 */
2696 void
2697 default_C_entries (inf)
2698 FILE *inf;
2699 {
2700 C_entries (cplusplus ? C_PLPL : 0, inf);
2701 }
2702
2703 /* Always do plain ANSI C. */
2704 void
2705 plain_C_entries (inf)
2706 FILE *inf;
2707 {
2708 C_entries (0, inf);
2709 }
2710
2711 /* Always do C++. */
2712 void
2713 Cplusplus_entries (inf)
2714 FILE *inf;
2715 {
2716 C_entries (C_PLPL, inf);
2717 }
2718
2719 /* Always do C*. */
2720 void
2721 Cstar_entries (inf)
2722 FILE *inf;
2723 {
2724 C_entries (C_STAR, inf);
2725 }
2726
2727 /* Always do Yacc. */
2728 void
2729 Yacc_entries (inf)
2730 FILE *inf;
2731 {
2732 C_entries (YACC, inf);
2733 }
2734 \f
2735 /* Fortran parsing */
2736
2737 char *dbp;
2738
2739 logical
2740 tail (cp)
2741 char *cp;
2742 {
2743 register int len = 0;
2744
2745 while (*cp && lowcase(*cp) == lowcase(dbp[len]))
2746 cp++, len++;
2747 if (*cp == '\0' && !intoken(dbp[len]))
2748 {
2749 dbp += len;
2750 return TRUE;
2751 }
2752 return FALSE;
2753 }
2754
2755 void
2756 takeprec ()
2757 {
2758 while (isspace (*dbp))
2759 dbp++;
2760 if (*dbp != '*')
2761 return;
2762 dbp++;
2763 while (isspace (*dbp))
2764 dbp++;
2765 if (strneq (dbp, "(*)", 3))
2766 {
2767 dbp += 3;
2768 return;
2769 }
2770 if (!isdigit (*dbp))
2771 {
2772 --dbp; /* force failure */
2773 return;
2774 }
2775 do
2776 dbp++;
2777 while (isdigit (*dbp));
2778 }
2779
2780 void
2781 getit (inf)
2782 FILE *inf;
2783 {
2784 register char *cp;
2785
2786 while (isspace (*dbp))
2787 dbp++;
2788 if (*dbp == '\0')
2789 {
2790 lineno++;
2791 linecharno = charno;
2792 charno += readline (&lb, inf);
2793 dbp = lb.buffer;
2794 if (dbp[5] != '&')
2795 return;
2796 dbp += 6;
2797 while (isspace (*dbp))
2798 dbp++;
2799 }
2800 if (!isalpha (*dbp)
2801 && *dbp != '_'
2802 && *dbp != '$')
2803 return;
2804 for (cp = dbp + 1;
2805 (*cp
2806 && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
2807 cp++)
2808 continue;
2809 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
2810 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2811 }
2812
2813 void
2814 Fortran_functions (inf)
2815 FILE *inf;
2816 {
2817 lineno = 0;
2818 charno = 0;
2819
2820 while (!feof (inf))
2821 {
2822 lineno++;
2823 linecharno = charno;
2824 charno += readline (&lb, inf);
2825 dbp = lb.buffer;
2826 if (*dbp == '%')
2827 dbp++; /* Ratfor escape to fortran */
2828 while (isspace (*dbp))
2829 dbp++;
2830 if (*dbp == '\0')
2831 continue;
2832 switch (lowcase (*dbp))
2833 {
2834 case 'i':
2835 if (tail ("integer"))
2836 takeprec ();
2837 break;
2838 case 'r':
2839 if (tail ("real"))
2840 takeprec ();
2841 break;
2842 case 'l':
2843 if (tail ("logical"))
2844 takeprec ();
2845 break;
2846 case 'c':
2847 if (tail ("complex") || tail ("character"))
2848 takeprec ();
2849 break;
2850 case 'd':
2851 if (tail ("double"))
2852 {
2853 while (isspace (*dbp))
2854 dbp++;
2855 if (*dbp == '\0')
2856 continue;
2857 if (tail ("precision"))
2858 break;
2859 continue;
2860 }
2861 break;
2862 }
2863 while (isspace (*dbp))
2864 dbp++;
2865 if (*dbp == '\0')
2866 continue;
2867 switch (lowcase (*dbp))
2868 {
2869 case 'f':
2870 if (tail ("function"))
2871 getit (inf);
2872 continue;
2873 case 's':
2874 if (tail ("subroutine"))
2875 getit (inf);
2876 continue;
2877 case 'e':
2878 if (tail ("entry"))
2879 getit (inf);
2880 continue;
2881 case 'p':
2882 if (tail ("program"))
2883 {
2884 getit (inf);
2885 continue;
2886 }
2887 if (tail ("procedure"))
2888 getit (inf);
2889 continue;
2890 }
2891 }
2892 }
2893 \f
2894 /*
2895 * Bob Weiner, Motorola Inc., 4/3/94
2896 * Unix and microcontroller assembly tag handling
2897 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2898 */
2899 void
2900 Asm_labels (inf)
2901 FILE *inf;
2902 {
2903 register char *cp;
2904
2905 lineno = 0;
2906 charno = 0;
2907
2908 while (!feof (inf))
2909 {
2910 lineno++;
2911 linecharno = charno;
2912 charno += readline (&lb, inf);
2913 cp = lb.buffer;
2914
2915 /* If first char is alphabetic or one of [_.$], test for colon
2916 following identifier. */
2917 if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2918 {
2919 /* Read past label. */
2920 cp++;
2921 while (isalnum (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2922 cp++;
2923 if (*cp == ':' || isspace (*cp))
2924 {
2925 /* Found end of label, so copy it and add it to the table. */
2926 pfnote ((CTAGS) ? savenstr(lb.buffer, cp-lb.buffer) : NULL, TRUE,
2927 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2928 }
2929 }
2930 }
2931 }
2932 \f
2933 /*
2934 * Perl support by Bart Robinson <lomew@cs.utah.edu>
2935 * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
2936 */
2937 void
2938 Perl_functions (inf)
2939 FILE *inf;
2940 {
2941 register char *cp;
2942
2943 lineno = 0;
2944 charno = 0;
2945
2946 while (!feof (inf))
2947 {
2948 lineno++;
2949 linecharno = charno;
2950 charno += readline (&lb, inf);
2951 cp = lb.buffer;
2952
2953 if (*cp++ == 's' && *cp++ == 'u' && *cp++ == 'b' && isspace(*cp++))
2954 {
2955 while (*cp && isspace(*cp))
2956 cp++;
2957 while (*cp && ! isspace(*cp) && *cp != '{')
2958 cp++;
2959 pfnote ((CTAGS) ? savenstr (lb.buffer, cp-lb.buffer) : NULL, TRUE,
2960 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2961 }
2962 }
2963 }
2964 \f
2965 /* Added by Mosur Mohan, 4/22/88 */
2966 /* Pascal parsing */
2967
2968 /*
2969 * Locates tags for procedures & functions. Doesn't do any type- or
2970 * var-definitions. It does look for the keyword "extern" or
2971 * "forward" immediately following the procedure statement; if found,
2972 * the tag is skipped.
2973 */
2974 void
2975 Pascal_functions (inf)
2976 FILE *inf;
2977 {
2978 struct linebuffer tline; /* mostly copied from C_entries */
2979 long save_lcno;
2980 int save_lineno, save_len;
2981 char c, *cp, *namebuf;
2982
2983 logical /* each of these flags is TRUE iff: */
2984 incomment, /* point is inside a comment */
2985 inquote, /* point is inside '..' string */
2986 get_tagname, /* point is after PROCEDURE/FUNCTION
2987 keyword, so next item = potential tag */
2988 found_tag, /* point is after a potential tag */
2989 inparms, /* point is within parameter-list */
2990 verify_tag; /* point has passed the parm-list, so the
2991 next token will determine whether this
2992 is a FORWARD/EXTERN to be ignored, or
2993 whether it is a real tag */
2994
2995 lineno = 0;
2996 charno = 0;
2997 dbp = lb.buffer;
2998 *dbp = '\0';
2999 save_len = 0;
3000 initbuffer (&tline);
3001
3002 incomment = inquote = FALSE;
3003 found_tag = FALSE; /* have a proc name; check if extern */
3004 get_tagname = FALSE; /* have found "procedure" keyword */
3005 inparms = FALSE; /* found '(' after "proc" */
3006 verify_tag = FALSE; /* check if "extern" is ahead */
3007
3008 /* long main loop to get next char */
3009 while (!feof (inf))
3010 {
3011 c = *dbp++;
3012 if (c == '\0') /* if end of line */
3013 {
3014 lineno++;
3015 linecharno = charno;
3016 charno += readline (&lb, inf);
3017 dbp = lb.buffer;
3018 if (*dbp == '\0')
3019 continue;
3020 if (!((found_tag && verify_tag) ||
3021 get_tagname))
3022 c = *dbp++; /* only if don't need *dbp pointing
3023 to the beginning of the name of
3024 the procedure or function */
3025 }
3026 if (incomment)
3027 {
3028 if (c == '}') /* within { } comments */
3029 incomment = FALSE;
3030 else if (c == '*' && *dbp == ')') /* within (* *) comments */
3031 {
3032 dbp++;
3033 incomment = FALSE;
3034 }
3035 continue;
3036 }
3037 else if (inquote)
3038 {
3039 if (c == '\'')
3040 inquote = FALSE;
3041 continue;
3042 }
3043 else
3044 switch (c)
3045 {
3046 case '\'':
3047 inquote = TRUE; /* found first quote */
3048 continue;
3049 case '{': /* found open { comment */
3050 incomment = TRUE;
3051 continue;
3052 case '(':
3053 if (*dbp == '*') /* found open (* comment */
3054 {
3055 incomment = TRUE;
3056 dbp++;
3057 }
3058 else if (found_tag) /* found '(' after tag, i.e., parm-list */
3059 inparms = TRUE;
3060 continue;
3061 case ')': /* end of parms list */
3062 if (inparms)
3063 inparms = FALSE;
3064 continue;
3065 case ';':
3066 if (found_tag && !inparms) /* end of proc or fn stmt */
3067 {
3068 verify_tag = TRUE;
3069 break;
3070 }
3071 continue;
3072 }
3073 if (found_tag && verify_tag && (*dbp != ' '))
3074 {
3075 /* check if this is an "extern" declaration */
3076 if (*dbp == '\0')
3077 continue;
3078 if (lowcase (*dbp == 'e'))
3079 {
3080 if (tail ("extern")) /* superfluous, really! */
3081 {
3082 found_tag = FALSE;
3083 verify_tag = FALSE;
3084 }
3085 }
3086 else if (lowcase (*dbp) == 'f')
3087 {
3088 if (tail ("forward")) /* check for forward reference */
3089 {
3090 found_tag = FALSE;
3091 verify_tag = FALSE;
3092 }
3093 }
3094 if (found_tag && verify_tag) /* not external proc, so make tag */
3095 {
3096 found_tag = FALSE;
3097 verify_tag = FALSE;
3098 pfnote (namebuf, TRUE,
3099 tline.buffer, save_len, save_lineno, save_lcno);
3100 continue;
3101 }
3102 }
3103 if (get_tagname) /* grab name of proc or fn */
3104 {
3105 if (*dbp == '\0')
3106 continue;
3107
3108 /* save all values for later tagging */
3109 grow_linebuffer (&tline, strlen (lb.buffer) + 1);
3110 strcpy (tline.buffer, lb.buffer);
3111 save_lineno = lineno;
3112 save_lcno = linecharno;
3113
3114 /* grab block name */
3115 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
3116 continue;
3117 namebuf = (CTAGS) ? savenstr (dbp, cp-dbp) : NULL;
3118 dbp = cp; /* set dbp to e-o-token */
3119 save_len = dbp - lb.buffer + 1;
3120 get_tagname = FALSE;
3121 found_tag = TRUE;
3122 continue;
3123
3124 /* and proceed to check for "extern" */
3125 }
3126 else if (!incomment && !inquote && !found_tag)
3127 {
3128 /* check for proc/fn keywords */
3129 switch (lowcase (c))
3130 {
3131 case 'p':
3132 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
3133 get_tagname = TRUE;
3134 continue;
3135 case 'f':
3136 if (tail ("unction"))
3137 get_tagname = TRUE;
3138 continue;
3139 }
3140 }
3141 } /* while not eof */
3142
3143 free (tline.buffer);
3144 }
3145 \f
3146 /*
3147 * lisp tag functions
3148 * look for (def or (DEF, quote or QUOTE
3149 */
3150 int
3151 L_isdef (strp)
3152 register char *strp;
3153 {
3154 return ((strp[1] == 'd' || strp[1] == 'D')
3155 && (strp[2] == 'e' || strp[2] == 'E')
3156 && (strp[3] == 'f' || strp[3] == 'F'));
3157 }
3158
3159 int
3160 L_isquote (strp)
3161 register char *strp;
3162 {
3163 return ((*(++strp) == 'q' || *strp == 'Q')
3164 && (*(++strp) == 'u' || *strp == 'U')
3165 && (*(++strp) == 'o' || *strp == 'O')
3166 && (*(++strp) == 't' || *strp == 'T')
3167 && (*(++strp) == 'e' || *strp == 'E')
3168 && isspace(*(++strp)));
3169 }
3170
3171 void
3172 L_getit ()
3173 {
3174 register char *cp;
3175
3176 if (*dbp == '\'') /* Skip prefix quote */
3177 dbp++;
3178 else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
3179 {
3180 dbp += 7;
3181 while (isspace(*dbp))
3182 dbp++;
3183 }
3184 for (cp = dbp /*+1*/;
3185 *cp && *cp != '(' && *cp != ' ' && *cp != ')';
3186 cp++)
3187 continue;
3188 if (cp == dbp)
3189 return;
3190
3191 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3192 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3193 }
3194
3195 void
3196 Lisp_functions (inf)
3197 FILE *inf;
3198 {
3199 lineno = 0;
3200 charno = 0;
3201
3202 while (!feof (inf))
3203 {
3204 lineno++;
3205 linecharno = charno;
3206 charno += readline (&lb, inf);
3207 dbp = lb.buffer;
3208 if (dbp[0] == '(')
3209 {
3210 if (L_isdef (dbp))
3211 {
3212 while (!isspace (*dbp))
3213 dbp++;
3214 while (isspace (*dbp))
3215 dbp++;
3216 L_getit ();
3217 }
3218 else
3219 {
3220 /* Check for (foo::defmumble name-defined ... */
3221 do
3222 dbp++;
3223 while (*dbp && !isspace (*dbp)
3224 && *dbp != ':' && *dbp != '(' && *dbp != ')');
3225 if (*dbp == ':')
3226 {
3227 do
3228 dbp++;
3229 while (*dbp == ':');
3230
3231 if (L_isdef (dbp - 1))
3232 {
3233 while (!isspace (*dbp))
3234 dbp++;
3235 while (isspace (*dbp))
3236 dbp++;
3237 L_getit ();
3238 }
3239 }
3240 }
3241 }
3242 }
3243 }
3244 \f
3245 /*
3246 * Scheme tag functions
3247 * look for (def... xyzzy
3248 * look for (def... (xyzzy
3249 * look for (def ... ((...(xyzzy ....
3250 * look for (set! xyzzy
3251 */
3252
3253 void get_scheme ();
3254
3255 void
3256 Scheme_functions (inf)
3257 FILE *inf;
3258 {
3259 lineno = 0;
3260 charno = 0;
3261
3262 while (!feof (inf))
3263 {
3264 lineno++;
3265 linecharno = charno;
3266 charno += readline (&lb, inf);
3267 dbp = lb.buffer;
3268 if (dbp[0] == '(' &&
3269 (dbp[1] == 'D' || dbp[1] == 'd') &&
3270 (dbp[2] == 'E' || dbp[2] == 'e') &&
3271 (dbp[3] == 'F' || dbp[3] == 'f'))
3272 {
3273 while (!isspace (*dbp))
3274 dbp++;
3275 /* Skip over open parens and white space */
3276 while (*dbp && (isspace (*dbp) || *dbp == '('))
3277 dbp++;
3278 get_scheme ();
3279 }
3280 if (dbp[0] == '(' &&
3281 (dbp[1] == 'S' || dbp[1] == 's') &&
3282 (dbp[2] == 'E' || dbp[2] == 'e') &&
3283 (dbp[3] == 'T' || dbp[3] == 't') &&
3284 (dbp[4] == '!' || dbp[4] == '!') &&
3285 (isspace (dbp[5])))
3286 {
3287 while (!isspace (*dbp))
3288 dbp++;
3289 /* Skip over white space */
3290 while (isspace (*dbp))
3291 dbp++;
3292 get_scheme ();
3293 }
3294 }
3295 }
3296
3297 void
3298 get_scheme ()
3299 {
3300 register char *cp;
3301
3302 if (*dbp == '\0')
3303 return;
3304 /* Go till you get to white space or a syntactic break */
3305 for (cp = dbp + 1;
3306 *cp && *cp != '(' && *cp != ')' && !isspace (*cp);
3307 cp++)
3308 continue;
3309 pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
3310 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
3311 }
3312 \f
3313 /* Find tags in TeX and LaTeX input files. */
3314
3315 /* TEX_toktab is a table of TeX control sequences that define tags.
3316 Each TEX_tabent records one such control sequence.
3317 CONVERT THIS TO USE THE Stab TYPE!! */
3318 struct TEX_tabent
3319 {
3320 char *name;
3321 int len;
3322 };
3323
3324 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
3325
3326 /* Default set of control sequences to put into TEX_toktab.
3327 The value of environment var TEXTAGS is prepended to this. */
3328
3329 char *TEX_defenv = "\
3330 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
3331 :part:appendix:entry:index";
3332
3333 void TEX_mode ();
3334 struct TEX_tabent *TEX_decode_env ();
3335 int TEX_Token ();
3336 #if TeX_named_tokens
3337 void TEX_getit ();
3338 #endif
3339
3340 char TEX_esc = '\\';
3341 char TEX_opgrp = '{';
3342 char TEX_clgrp = '}';
3343
3344 /*
3345 * TeX/LaTeX scanning loop.
3346 */
3347 void
3348 TeX_functions (inf)
3349 FILE *inf;
3350 {
3351 char *lasthit;
3352
3353 lineno = 0;
3354 charno = 0;
3355
3356 /* Select either \ or ! as escape character. */
3357 TEX_mode (inf);
3358
3359 /* Initialize token table once from environment. */
3360 if (!TEX_toktab)
3361 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
3362
3363 while (!feof (inf))
3364 { /* Scan each line in file */
3365 lineno++;
3366 linecharno = charno;
3367 charno += readline (&lb, inf);
3368 dbp = lb.buffer;
3369 lasthit = dbp;
3370 while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
3371 {
3372 register int i;
3373
3374 if (!*(++dbp))
3375 break;
3376 linecharno += dbp - lasthit;
3377 lasthit = dbp;
3378 i = TEX_Token (lasthit);
3379 if (0 <= i)
3380 {
3381 pfnote ((char *)NULL, TRUE,
3382 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3383 #if TeX_named_tokens
3384 TEX_getit (lasthit, TEX_toktab[i].len);
3385 #endif
3386 break; /* We only save a line once */
3387 }
3388 }
3389 }
3390 }
3391
3392 #define TEX_LESC '\\'
3393 #define TEX_SESC '!'
3394 #define TEX_cmt '%'
3395
3396 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3397 chars accordingly. */
3398 void
3399 TEX_mode (inf)
3400 FILE *inf;
3401 {
3402 int c;
3403
3404 while ((c = getc (inf)) != EOF)
3405 {
3406 /* Skip to next line if we hit the TeX comment char. */
3407 if (c == TEX_cmt)
3408 while (c != '\n')
3409 c = getc (inf);
3410 else if (c == TEX_LESC || c == TEX_SESC )
3411 break;
3412 }
3413
3414 if (c == TEX_LESC)
3415 {
3416 TEX_esc = TEX_LESC;
3417 TEX_opgrp = '{';
3418 TEX_clgrp = '}';
3419 }
3420 else
3421 {
3422 TEX_esc = TEX_SESC;
3423 TEX_opgrp = '<';
3424 TEX_clgrp = '>';
3425 }
3426 rewind (inf);
3427 }
3428
3429 /* Read environment and prepend it to the default string.
3430 Build token table. */
3431 struct TEX_tabent *
3432 TEX_decode_env (evarname, defenv)
3433 char *evarname;
3434 char *defenv;
3435 {
3436 register char *env, *p;
3437
3438 struct TEX_tabent *tab;
3439 int size, i;
3440
3441 /* Append default string to environment. */
3442 env = getenv (evarname);
3443 if (!env)
3444 env = defenv;
3445 else
3446 env = concat (env, defenv, "");
3447
3448 /* Allocate a token table */
3449 for (size = 1, p = env; p;)
3450 if ((p = etags_strchr (p, ':')) && *(++p))
3451 size++;
3452 /* Add 1 to leave room for null terminator. */
3453 tab = xnew (size + 1, struct TEX_tabent);
3454
3455 /* Unpack environment string into token table. Be careful about */
3456 /* zero-length strings (leading ':', "::" and trailing ':') */
3457 for (i = 0; *env;)
3458 {
3459 p = etags_strchr (env, ':');
3460 if (!p) /* End of environment string. */
3461 p = env + strlen (env);
3462 if (p - env > 0)
3463 { /* Only non-zero strings. */
3464 tab[i].name = savenstr (env, p - env);
3465 tab[i].len = strlen (tab[i].name);
3466 i++;
3467 }
3468 if (*p)
3469 env = p + 1;
3470 else
3471 {
3472 tab[i].name = NULL; /* Mark end of table. */
3473 tab[i].len = 0;
3474 break;
3475 }
3476 }
3477 return tab;
3478 }
3479
3480 #if TeX_named_tokens
3481 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3482 The name being defined actually starts at (NAME + LEN + 1).
3483 But we seem to include the TeX command in the tag name. */
3484 void
3485 TEX_getit (name, len)
3486 char *name;
3487 int len;
3488 {
3489 char *p = name + len;
3490
3491 if (*name == '\0')
3492 return;
3493
3494 /* Let tag name extend to next group close (or end of line) */
3495 while (*p && *p != TEX_clgrp)
3496 p++;
3497 pfnote (savenstr (name, p-name), TRUE,
3498 lb.buffer, strlen (lb.buffer), lineno, linecharno);
3499 }
3500 #endif
3501
3502 /* If the text at CP matches one of the tag-defining TeX command names,
3503 return the pointer to the first occurrence of that command in TEX_toktab.
3504 Otherwise return -1.
3505 Keep the capital `T' in `Token' for dumb truncating compilers
3506 (this distinguishes it from `TEX_toktab' */
3507 int
3508 TEX_Token (cp)
3509 char *cp;
3510 {
3511 int i;
3512
3513 for (i = 0; TEX_toktab[i].len > 0; i++)
3514 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
3515 return i;
3516 return -1;
3517 }
3518 \f
3519 /*
3520 * Prolog support (rewritten) by Anders Lindgren, Mar. 96
3521 *
3522 * Assumes that the predicate starts at column 0.
3523 * Only the first clause of a predicate is added.
3524 */
3525 void
3526 Prolog_functions (inf)
3527 FILE *inf;
3528 {
3529 int prolog_pred ();
3530 void prolog_skip_comment ();
3531
3532 char * last;
3533 int len;
3534 int allocated;
3535
3536 allocated = 0;
3537 len = 0;
3538 last = NULL;
3539
3540 lineno = 0;
3541 linecharno = 0;
3542 charno = 0;
3543
3544 while (!feof (inf))
3545 {
3546 lineno++;
3547 linecharno += charno;
3548 charno = readline (&lb, inf);
3549 dbp = lb.buffer;
3550 if (dbp[0] == '\0') /* Empty line */
3551 continue;
3552 else if (isspace (dbp[0])) /* Not a predicate */
3553 continue;
3554 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
3555 prolog_skip_comment (&lb, inf);
3556 else if (len = prolog_pred (dbp, last))
3557 {
3558 /* Predicate. Store the function name so that we only
3559 generate a tag for the first clause. */
3560 if (last == NULL)
3561 last = xnew(len + 1, char);
3562 else if (len + 1 > allocated)
3563 last = (char *) xrealloc(last, len + 1);
3564 allocated = len + 1;
3565 strncpy (last, dbp, len);
3566 last[len] = '\0';
3567 }
3568 }
3569 }
3570
3571
3572 void
3573 prolog_skip_comment (plb, inf)
3574 struct linebuffer *plb;
3575 FILE *inf;
3576 {
3577 char *cp;
3578
3579 do
3580 {
3581 for (cp = plb->buffer; *cp != '\0'; cp++)
3582 if (cp[0] == '*' && cp[1] == '/')
3583 return;
3584 lineno++;
3585 linecharno += readline (plb, inf);
3586 }
3587 while (!feof(inf));
3588 }
3589
3590 /*
3591 * A predicate definition is added if it matches:
3592 * <beginning of line><Prolog Atom><whitespace>(
3593 *
3594 * It is added to the tags database if it doesn't match the
3595 * name of the previous clause header.
3596 *
3597 * Return the size of the name of the predicate, or 0 if no header
3598 * was found.
3599 */
3600 int
3601 prolog_pred (s, last)
3602 char *s;
3603 char *last; /* Name of last clause. */
3604 {
3605 int prolog_atom();
3606 int prolog_white();
3607
3608 int pos;
3609 int len;
3610
3611 pos = prolog_atom(s, 0);
3612 if (pos < 1)
3613 return 0;
3614
3615 len = pos;
3616 pos += prolog_white(s, pos);
3617
3618 if ((s[pos] == '(') || (s[pos] == '.'))
3619 {
3620 if (s[pos] == '(')
3621 pos++;
3622
3623 /* Save only the first clause. */
3624 if ((last == NULL) ||
3625 (len != strlen(last)) ||
3626 (strncmp(s, last, len) != 0))
3627 {
3628 pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
3629 s, pos, lineno, linecharno);
3630 return len;
3631 }
3632 }
3633 return 0;
3634 }
3635
3636 /*
3637 * Consume a Prolog atom.
3638 * Return the number of bytes consumed, or -1 if there was an error.
3639 *
3640 * A prolog atom, in this context, could be one of:
3641 * - An alphanumeric sequence, starting with a lower case letter.
3642 * - A quoted arbitrary string. Single quotes can escape themselves.
3643 * Backslash quotes everything.
3644 */
3645 int
3646 prolog_atom (s, pos)
3647 char *s;
3648 int pos;
3649 {
3650 int origpos;
3651
3652 origpos = pos;
3653
3654 if (islower(s[pos]) || (s[pos] == '_'))
3655 {
3656 /* The atom is unquoted. */
3657 pos++;
3658 while (isalnum(s[pos]) || (s[pos] == '_'))
3659 {
3660 pos++;
3661 }
3662 return pos - origpos;
3663 }
3664 else if (s[pos] == '\'')
3665 {
3666 pos++;
3667
3668 while (1)
3669 {
3670 if (s[pos] == '\'')
3671 {
3672 pos++;
3673 if (s[pos] != '\'')
3674 break;
3675 pos++; /* A double quote */
3676 }
3677 else if (s[pos] == '\0')
3678 /* Multiline quoted atoms are ignored. */
3679 return -1;
3680 else if (s[pos] == '\\')
3681 {
3682 if (s[pos+1] == '\0')
3683 return -1;
3684 pos += 2;
3685 }
3686 else
3687 pos++;
3688 }
3689 return pos - origpos;
3690 }
3691 else
3692 return -1;
3693 }
3694
3695 /* Consume whitespace. Return the number of bytes eaten. */
3696 int
3697 prolog_white (s, pos)
3698 char *s;
3699 int pos;
3700 {
3701 int origpos;
3702
3703 origpos = pos;
3704
3705 while (isspace(s[pos]))
3706 pos++;
3707
3708 return pos - origpos;
3709 }
3710 \f
3711 /*
3712 * Support for Erlang -- Anders Lindgren, Feb 1996.
3713 *
3714 * Generates tags for functions, defines, and records.
3715 *
3716 * Assumes that Erlang functions start at column 0.
3717 */
3718 void
3719 Erlang_functions (inf)
3720 FILE *inf;
3721 {
3722 int erlang_func ();
3723 void erlang_attribute ();
3724
3725 char * last;
3726 int len;
3727 int allocated;
3728
3729 allocated = 0;
3730 len = 0;
3731 last = NULL;
3732
3733 lineno = 0;
3734 linecharno = 0;
3735 charno = 0;
3736
3737 while (!feof (inf))
3738 {
3739 lineno++;
3740 linecharno += charno;
3741 charno = readline (&lb, inf);
3742 dbp = lb.buffer;
3743 if (dbp[0] == '\0') /* Empty line */
3744 continue;
3745 else if (isspace (dbp[0])) /* Not function nor attribute */
3746 continue;
3747 else if (dbp[0] == '%') /* comment */
3748 continue;
3749 else if (dbp[0] == '"') /* Sometimes, strings start in column one */
3750 continue;
3751 else if (dbp[0] == '-') /* attribute, e.g. "-define" */
3752 {
3753 erlang_attribute(dbp);
3754 last = NULL;
3755 }
3756 else if (len = erlang_func (dbp, last))
3757 {
3758 /*
3759 * Function. Store the function name so that we only
3760 * generates a tag for the first clause.
3761 */
3762 if (last == NULL)
3763 last = xnew(len + 1, char);
3764 else if (len + 1 > allocated)
3765 last = (char *) xrealloc(last, len + 1);
3766 allocated = len + 1;
3767 strncpy (last, dbp, len);
3768 last[len] = '\0';
3769 }
3770 }
3771 }
3772
3773
3774 /*
3775 * A function definition is added if it matches:
3776 * <beginning of line><Erlang Atom><whitespace>(
3777 *
3778 * It is added to the tags database if it doesn't match the
3779 * name of the previous clause header.
3780 *
3781 * Return the size of the name of the function, or 0 if no function
3782 * was found.
3783 */
3784 int
3785 erlang_func (s, last)
3786 char *s;
3787 char *last; /* Name of last clause. */
3788 {
3789 int erlang_atom ();
3790 int erlang_white ();
3791
3792 int pos;
3793 int len;
3794
3795 pos = erlang_atom(s, 0);
3796 if (pos < 1)
3797 return 0;
3798
3799 len = pos;
3800 pos += erlang_white(s, pos);
3801
3802 if (s[pos++] == '(')
3803 {
3804 /* Save only the first clause. */
3805 if ((last == NULL) ||
3806 (len != strlen(last)) ||
3807 (strncmp(s, last, len) != 0))
3808 {
3809 pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
3810 s, pos, lineno, linecharno);
3811 return len;
3812 }
3813 }
3814 return 0;
3815 }
3816
3817
3818 /*
3819 * Handle attributes. Currently, tags are generated for defines
3820 * and records.
3821 *
3822 * They are on the form:
3823 * -define(foo, bar).
3824 * -define(Foo(M, N), M+N).
3825 * -record(graph, {vtab = notable, cyclic = true}).
3826 */
3827 void
3828 erlang_attribute (s)
3829 char *s;
3830 {
3831 int erlang_atom ();
3832 int erlang_white ();
3833
3834 int pos;
3835 int len;
3836
3837 if ((strncmp(s, "-define", 7) == 0) ||
3838 (strncmp(s, "-record", 7) == 0))
3839 {
3840 pos = 7;
3841 pos += erlang_white(s, pos);
3842
3843 if (s[pos++] == '(')
3844 {
3845 pos += erlang_white(s, pos);
3846
3847 if (len = erlang_atom(s, pos))
3848 {
3849 pfnote ((CTAGS) ? savenstr (& s[pos], len) : NULL, TRUE,
3850 s, pos + len, lineno, linecharno);
3851 }
3852 }
3853 }
3854 return;
3855 }
3856
3857
3858 /*
3859 * Consume an Erlang atom (or variable).
3860 * Return the number of bytes consumed, or -1 if there was an error.
3861 */
3862 int
3863 erlang_atom (s, pos)
3864 char *s;
3865 int pos;
3866 {
3867 int origpos;
3868
3869 origpos = pos;
3870
3871 if (isalpha (s[pos]) || s[pos] == '_')
3872 {
3873 /* The atom is unquoted. */
3874 pos++;
3875 while (isalnum (s[pos]) || s[pos] == '_')
3876 pos++;
3877 return pos - origpos;
3878 }
3879 else if (s[pos] == '\'')
3880 {
3881 pos++;
3882
3883 while (1)
3884 {
3885 if (s[pos] == '\'')
3886 {
3887 pos++;
3888 break;
3889 }
3890 else if (s[pos] == '\0')
3891 /* Multiline quoted atoms are ignored. */
3892 return -1;
3893 else if (s[pos] == '\\')
3894 {
3895 if (s[pos+1] == '\0')
3896 return -1;
3897 pos += 2;
3898 }
3899 else
3900 pos++;
3901 }
3902 return pos - origpos;
3903 }
3904 else
3905 return -1;
3906 }
3907
3908 /* Consume whitespace. Return the number of bytes eaten */
3909 int
3910 erlang_white (s, pos)
3911 char *s;
3912 int pos;
3913 {
3914 int origpos;
3915
3916 origpos = pos;
3917
3918 while (isspace (s[pos]))
3919 pos++;
3920
3921 return pos - origpos;
3922 }
3923 \f
3924 #ifdef ETAGS_REGEXPS
3925 /* Take a string like "/blah/" and turn it into "blah", making sure
3926 that the first and last characters are the same, and handling
3927 quoted separator characters. Actually, stops on the occurrence of
3928 an unquoted separator. Also turns "\t" into a Tab character.
3929 Returns pointer to terminating separator. Works in place. Null
3930 terminates name string. */
3931 char *
3932 scan_separators (name)
3933 char *name;
3934 {
3935 char sep = name[0];
3936 char *copyto = name;
3937 logical quoted = FALSE;
3938
3939 for (++name; *name != '\0'; ++name)
3940 {
3941 if (quoted)
3942 {
3943 if (*name == 't')
3944 *copyto++ = '\t';
3945 else if (*name == sep)
3946 *copyto++ = sep;
3947 else
3948 {
3949 /* Something else is quoted, so preserve the quote. */
3950 *copyto++ = '\\';
3951 *copyto++ = *name;
3952 }
3953 quoted = FALSE;
3954 }
3955 else if (*name == '\\')
3956 quoted = TRUE;
3957 else if (*name == sep)
3958 break;
3959 else
3960 *copyto++ = *name;
3961 }
3962
3963 /* Terminate copied string. */
3964 *copyto = '\0';
3965 return name;
3966 }
3967
3968 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3969 expression, into a real regular expression by compiling it. */
3970 void
3971 add_regex (regexp_pattern)
3972 char *regexp_pattern;
3973 {
3974 char *name;
3975 const char *err;
3976 struct re_pattern_buffer *patbuf;
3977
3978 if (regexp_pattern == NULL)
3979 {
3980 /* Remove existing regexps. */
3981 num_patterns = 0;
3982 patterns = NULL;
3983 return;
3984 }
3985
3986 if (regexp_pattern[0] == '\0')
3987 {
3988 error ("missing regexp", (char *)NULL);
3989 return;
3990 }
3991 if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
3992 {
3993 error ("%s: unterminated regexp", regexp_pattern);
3994 return;
3995 }
3996 name = scan_separators (regexp_pattern);
3997 if (regexp_pattern[0] == '\0')
3998 {
3999 error ("null regexp", (char *)NULL);
4000 return;
4001 }
4002 (void) scan_separators (name);
4003
4004 patbuf = xnew (1, struct re_pattern_buffer);
4005 patbuf->translate = NULL;
4006 patbuf->fastmap = NULL;
4007 patbuf->buffer = NULL;
4008 patbuf->allocated = 0;
4009
4010 err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
4011 if (err != NULL)
4012 {
4013 error ("%s while compiling pattern", err);
4014 return;
4015 }
4016
4017 num_patterns += 1;
4018 if (num_patterns == 1)
4019 patterns = xnew (1, struct pattern);
4020 else
4021 patterns = ((struct pattern *)
4022 xrealloc (patterns,
4023 (num_patterns * sizeof (struct pattern))));
4024 patterns[num_patterns - 1].pattern = patbuf;
4025 patterns[num_patterns - 1].name_pattern = savestr (name);
4026 patterns[num_patterns - 1].error_signaled = FALSE;
4027 }
4028
4029 /*
4030 * Do the substitutions indicated by the regular expression and
4031 * arguments.
4032 */
4033 char *
4034 substitute (in, out, regs)
4035 char *in, *out;
4036 struct re_registers *regs;
4037 {
4038 char *result = NULL, *t;
4039 int size = 0;
4040
4041 /* Pass 1: figure out how much size to allocate. */
4042 for (t = out; *t; ++t)
4043 {
4044 if (*t == '\\')
4045 {
4046 ++t;
4047 if (!*t)
4048 {
4049 fprintf (stderr, "%s: pattern substitution ends prematurely\n",
4050 progname);
4051 return NULL;
4052 }
4053 if (isdigit (*t))
4054 {
4055 int dig = *t - '0';
4056 size += regs->end[dig] - regs->start[dig];
4057 }
4058 }
4059 }
4060
4061 /* Allocate space and do the substitutions. */
4062 result = xnew (size + 1, char);
4063 size = 0;
4064 for (; *out; ++out)
4065 {
4066 if (*out == '\\')
4067 {
4068 ++out;
4069 if (isdigit (*out))
4070 {
4071 /* Using "dig2" satisfies my debugger. Bleah. */
4072 int dig2 = *out - '0';
4073 strncpy (result + size, in + regs->start[dig2],
4074 regs->end[dig2] - regs->start[dig2]);
4075 size += regs->end[dig2] - regs->start[dig2];
4076 }
4077 else
4078 result[size++] = *out;
4079 }
4080 else
4081 result[size++] = *out;
4082 }
4083 result[size] = '\0';
4084
4085 return result;
4086 }
4087 \f
4088 #endif /* ETAGS_REGEXPS */
4089 /* Initialize a linebuffer for use */
4090 void
4091 initbuffer (linebuffer)
4092 struct linebuffer *linebuffer;
4093 {
4094 linebuffer->size = 200;
4095 linebuffer->buffer = xnew (200, char);
4096 }
4097
4098 /*
4099 * Read a line of text from `stream' into `linebuffer'.
4100 * Return the number of characters read from `stream',
4101 * which is the length of the line including the newline, if any.
4102 */
4103 long
4104 readline_internal (linebuffer, stream)
4105 struct linebuffer *linebuffer;
4106 register FILE *stream;
4107 {
4108 char *buffer = linebuffer->buffer;
4109 register char *p = linebuffer->buffer;
4110 register char *pend;
4111 int chars_deleted;
4112
4113 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
4114
4115 while (1)
4116 {
4117 register int c = getc (stream);
4118 if (p == pend)
4119 {
4120 linebuffer->size *= 2;
4121 buffer = (char *) xrealloc (buffer, linebuffer->size);
4122 p += buffer - linebuffer->buffer;
4123 pend = buffer + linebuffer->size;
4124 linebuffer->buffer = buffer;
4125 }
4126 if (c == EOF)
4127 {
4128 *p = '\0';
4129 chars_deleted = 0;
4130 break;
4131 }
4132 if (c == '\n')
4133 {
4134 if (p > buffer && p[-1] == '\r')
4135 {
4136 *--p = '\0';
4137 #ifdef DOS_NT
4138 /* Assume CRLF->LF translation will be performed by Emacs
4139 when loading this file, so CRs won't appear in the buffer.
4140 It would be cleaner to compensate within Emacs;
4141 however, Emacs does not know how many CRs were deleted
4142 before any given point in the file. */
4143 chars_deleted = 1;
4144 #else
4145 chars_deleted = 2;
4146 #endif
4147 }
4148 else
4149 {
4150 *p = '\0';
4151 chars_deleted = 1;
4152 }
4153 break;
4154 }
4155 *p++ = c;
4156 }
4157
4158 return p - buffer + chars_deleted;
4159 }
4160
4161 /*
4162 * Like readline_internal, above, but try to match the input
4163 * line against any existing regular expressions.
4164 */
4165 long
4166 readline (linebuffer, stream)
4167 struct linebuffer *linebuffer;
4168 FILE *stream;
4169 {
4170 /* Read new line. */
4171 long result = readline_internal (linebuffer, stream);
4172 #ifdef ETAGS_REGEXPS
4173 int i;
4174
4175 /* Match against all listed patterns. */
4176 for (i = 0; i < num_patterns; ++i)
4177 {
4178 int match = re_match (patterns[i].pattern, linebuffer->buffer,
4179 (int)result, 0, &patterns[i].regs);
4180 switch (match)
4181 {
4182 case -2:
4183 /* Some error. */
4184 if (!patterns[i].error_signaled)
4185 {
4186 error ("error while matching pattern %d", i);
4187 patterns[i].error_signaled = TRUE;
4188 }
4189 break;
4190 case -1:
4191 /* No match. */
4192 break;
4193 default:
4194 /* Match occurred. Construct a tag. */
4195 if (patterns[i].name_pattern[0] != '\0')
4196 {
4197 /* Make a named tag. */
4198 char *name = substitute (linebuffer->buffer,
4199 patterns[i].name_pattern,
4200 &patterns[i].regs);
4201 if (name != NULL)
4202 pfnote (name, TRUE,
4203 linebuffer->buffer, match, lineno, linecharno);
4204 }
4205 else
4206 {
4207 /* Make an unnamed tag. */
4208 pfnote ((char *)NULL, TRUE,
4209 linebuffer->buffer, match, lineno, linecharno);
4210 }
4211 break;
4212 }
4213 }
4214 #endif /* ETAGS_REGEXPS */
4215
4216 return result;
4217 }
4218
4219 /*
4220 * Read a file, but do no processing. This is used to do regexp
4221 * matching on files that have no language defined.
4222 */
4223 void
4224 just_read_file (inf)
4225 FILE *inf;
4226 {
4227 lineno = 0;
4228 charno = 0;
4229
4230 while (!feof (inf))
4231 {
4232 ++lineno;
4233 linecharno = charno;
4234 charno += readline (&lb, inf) + 1;
4235 }
4236 }
4237
4238 \f
4239 /*
4240 * Return a pointer to a space of size strlen(cp)+1 allocated
4241 * with xnew where the string CP has been copied.
4242 */
4243 char *
4244 savestr (cp)
4245 char *cp;
4246 {
4247 return savenstr (cp, strlen (cp));
4248 }
4249
4250 /*
4251 * Return a pointer to a space of size LEN+1 allocated with xnew where
4252 * the string CP has been copied for at most the first LEN characters.
4253 */
4254 char *
4255 savenstr (cp, len)
4256 char *cp;
4257 int len;
4258 {
4259 register char *dp;
4260
4261 dp = xnew (len + 1, char);
4262 strncpy (dp, cp, len);
4263 dp[len] = '\0';
4264 return dp;
4265 }
4266
4267 /*
4268 * Return the ptr in sp at which the character c last
4269 * appears; NULL if not found
4270 *
4271 * Identical to System V strrchr, included for portability.
4272 */
4273 char *
4274 etags_strrchr (sp, c)
4275 register char *sp, c;
4276 {
4277 register char *r;
4278
4279 r = NULL;
4280 do
4281 {
4282 if (*sp == c)
4283 r = sp;
4284 } while (*sp++);
4285 return r;
4286 }
4287
4288
4289 /*
4290 * Return the ptr in sp at which the character c first
4291 * appears; NULL if not found
4292 *
4293 * Identical to System V strchr, included for portability.
4294 */
4295 char *
4296 etags_strchr (sp, c)
4297 register char *sp, c;
4298 {
4299 do
4300 {
4301 if (*sp == c)
4302 return sp;
4303 } while (*sp++);
4304 return NULL;
4305 }
4306
4307 /* Print error message and exit. */
4308 void
4309 fatal (s1, s2)
4310 char *s1, *s2;
4311 {
4312 error (s1, s2);
4313 exit (BAD);
4314 }
4315
4316 void
4317 pfatal (s1)
4318 char *s1;
4319 {
4320 perror (s1);
4321 exit (BAD);
4322 }
4323
4324 void
4325 suggest_asking_for_help ()
4326 {
4327 fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
4328 progname);
4329 exit (BAD);
4330 }
4331
4332 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
4333 void
4334 error (s1, s2)
4335 char *s1, *s2;
4336 {
4337 fprintf (stderr, "%s: ", progname);
4338 fprintf (stderr, s1, s2);
4339 fprintf (stderr, "\n");
4340 }
4341
4342 /* Return a newly-allocated string whose contents
4343 concatenate those of s1, s2, s3. */
4344 char *
4345 concat (s1, s2, s3)
4346 char *s1, *s2, *s3;
4347 {
4348 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
4349 char *result = xnew (len1 + len2 + len3 + 1, char);
4350
4351 strcpy (result, s1);
4352 strcpy (result + len1, s2);
4353 strcpy (result + len1 + len2, s3);
4354 result[len1 + len2 + len3] = '\0';
4355
4356 return result;
4357 }
4358 \f
4359 /* Does the same work as the system V getcwd, but does not need to
4360 guess the buffer size in advance. */
4361 char *
4362 etags_getcwd ()
4363 {
4364 #ifdef HAVE_GETCWD
4365 int bufsize = 200;
4366 char *path = xnew (bufsize, char);
4367
4368 while (getcwd (path, bufsize) == NULL)
4369 {
4370 if (errno != ERANGE)
4371 pfatal ("getcwd");
4372 bufsize *= 2;
4373 path = xnew (bufsize, char);
4374 }
4375
4376 #if WINDOWSNT
4377 {
4378 /* Convert backslashes to slashes. */
4379 char *p;
4380 for (p = path; *p != '\0'; p++)
4381 if (*p == '\\')
4382 *p = '/';
4383 }
4384 #endif
4385
4386 return path;
4387
4388 #else /* not HAVE_GETCWD */
4389 #ifdef MSDOS
4390 char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
4391
4392 getwd (path);
4393
4394 for (p = path; *p != '\0'; p++)
4395 if (*p == '\\')
4396 *p = '/';
4397 else
4398 *p = lowcase (*p);
4399
4400 return strdup (path);
4401 #else /* not MSDOS */
4402 struct linebuffer path;
4403 FILE *pipe;
4404
4405 initbuffer (&path);
4406 pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
4407 if (pipe == NULL || readline_internal (&path, pipe) == 0)
4408 pfatal ("pwd");
4409 pclose (pipe);
4410
4411 return path.buffer;
4412 #endif /* not MSDOS */
4413 #endif /* not HAVE_GETCWD */
4414 }
4415
4416 /* Return a newly allocated string containing the filename
4417 of FILE relative to the absolute directory DIR (which
4418 should end with a slash). */
4419 char *
4420 relative_filename (file, dir)
4421 char *file, *dir;
4422 {
4423 char *fp, *dp, *abs, *res;
4424
4425 /* Find the common root of file and dir (with a trailing slash). */
4426 abs = absolute_filename (file, cwd);
4427 fp = abs;
4428 dp = dir;
4429 while (*fp++ == *dp++)
4430 continue;
4431 fp--, dp--; /* back to the first differing char */
4432 do /* look at the equal chars until / */
4433 fp--, dp--;
4434 while (*fp != '/');
4435
4436 /* Build a sequence of "../" strings for the resulting relative filename. */
4437 for (dp = etags_strchr (dp + 1, '/'), res = "";
4438 dp != NULL;
4439 dp = etags_strchr (dp + 1, '/'))
4440 {
4441 res = concat (res, "../", "");
4442 }
4443
4444 /* Add the filename relative to the common root of file and dir. */
4445 res = concat (res, fp + 1, "");
4446 free (abs);
4447
4448 return res;
4449 }
4450
4451 /* Return a newly allocated string containing the
4452 absolute filename of FILE given CWD (which should
4453 end with a slash). */
4454 char *
4455 absolute_filename (file, cwd)
4456 char *file, *cwd;
4457 {
4458 char *slashp, *cp, *res;
4459
4460 if (absolutefn (file))
4461 res = concat (file, "", "");
4462 #ifdef DOS_NT
4463 /* We don't support non-absolute filenames with a drive
4464 letter, like `d:NAME' (it's too much hassle). */
4465 else if (file[1] == ':')
4466 fatal ("%s: relative filenames with drive letters not supported", file);
4467 #endif
4468 else
4469 res = concat (cwd, file, "");
4470
4471 /* Delete the "/dirname/.." and "/." substrings. */
4472 slashp = etags_strchr (res, '/');
4473 while (slashp != NULL && slashp[0] != '\0')
4474 {
4475 if (slashp[1] == '.')
4476 {
4477 if (slashp[2] == '.'
4478 && (slashp[3] == '/' || slashp[3] == '\0'))
4479 {
4480 cp = slashp;
4481 do
4482 cp--;
4483 while (cp >= res && !absolutefn (cp));
4484 if (*cp == '/')
4485 {
4486 strcpy (cp, slashp + 3);
4487 }
4488 #ifdef DOS_NT
4489 /* Under MSDOS and NT we get `d:/NAME' as absolute
4490 filename, so the luser could say `d:/../NAME'.
4491 We silently treat this as `d:/NAME'. */
4492 else if (cp[1] == ':')
4493 strcpy (cp + 3, slashp + 4);
4494 #endif
4495 else /* else (cp == res) */
4496 {
4497 if (slashp[3] != '\0')
4498 strcpy (cp, slashp + 4);
4499 else
4500 return ".";
4501 }
4502 slashp = cp;
4503 continue;
4504 }
4505 else if (slashp[2] == '/' || slashp[2] == '\0')
4506 {
4507 strcpy (slashp, slashp + 2);
4508 continue;
4509 }
4510 }
4511
4512 slashp = etags_strchr (slashp + 1, '/');
4513 }
4514
4515 return res;
4516 }
4517
4518 /* Return a newly allocated string containing the absolute
4519 filename of dir where FILE resides given CWD (which should
4520 end with a slash). */
4521 char *
4522 absolute_dirname (file, cwd)
4523 char *file, *cwd;
4524 {
4525 char *slashp, *res;
4526 char save;
4527 #ifdef DOS_NT
4528 char *p;
4529
4530 for (p = file; *p != '\0'; p++)
4531 if (*p == '\\')
4532 *p = '/';
4533 #endif
4534
4535 slashp = etags_strrchr (file, '/');
4536 if (slashp == NULL)
4537 return cwd;
4538 save = slashp[1];
4539 slashp[1] = '\0';
4540 res = absolute_filename (file, cwd);
4541 slashp[1] = save;
4542
4543 return res;
4544 }
4545
4546 /* Increase the size of a linebuffer. */
4547 void
4548 grow_linebuffer (bufp, toksize)
4549 struct linebuffer *bufp;
4550 int toksize;
4551 {
4552 while (bufp->size < toksize)
4553 bufp->size *= 2;
4554 bufp->buffer = (char *) xrealloc (bufp->buffer, bufp->size);
4555 }
4556
4557 /* Like malloc but get fatal error if memory is exhausted. */
4558 long *
4559 xmalloc (size)
4560 unsigned int size;
4561 {
4562 long *result = (long *) malloc (size);
4563 if (result == NULL)
4564 fatal ("virtual memory exhausted", (char *)NULL);
4565 return result;
4566 }
4567
4568 long *
4569 xrealloc (ptr, size)
4570 char *ptr;
4571 unsigned int size;
4572 {
4573 long *result = (long *) realloc (ptr, size);
4574 if (result == NULL)
4575 fatal ("virtual memory exhausted", (char *)NULL);
4576 return result;
4577 }