(print): Handle internal display-local object.
[emacs.git] / lib-src / etags.c
blobb50bbe48d635d7e76fbb28b0a630470c507622a9
1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984,87,88,89,93,94 Free Software Foundation, Inc. and Ken Arnold
3 This file is not considered part of GNU Emacs.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 * Authors:
21 * Ctags originally by Ken Arnold.
22 * Fortran added by Jim Kleckner.
23 * Ed Pelegri-Llopart added C typedefs.
24 * Gnu Emacs TAGS format and modifications by RMS?
25 * Sam Kendall added C++.
26 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
27 #ifdef ETAGS_REGEXPS
28 * Regexp tags by Tom Tromey.
29 #endif
31 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
34 char pot_etags_version[] = "@(#) pot revision number is 11.16";
36 #ifdef MSDOS
37 #include <fcntl.h>
38 #include <sys/param.h>
39 #endif /* MSDOS */
41 #ifdef WINDOWSNT
42 #include <stdlib.h>
43 #include <fcntl.h>
44 #include <string.h>
45 #define MAXPATHLEN _MAX_PATH
46 #endif
48 #ifdef HAVE_CONFIG_H
49 #include <config.h>
50 /* On some systems, Emacs defines static as nothing for the sake
51 of unexec. We don't want that here since we don't use unexec. */
52 #undef static
53 #endif
55 #include <stdio.h>
56 #include <ctype.h>
57 #include <errno.h>
58 #ifndef errno
59 extern int errno;
60 #endif
61 #include <sys/types.h>
62 #include <sys/stat.h>
64 #if !defined (S_ISREG) && defined (S_IFREG)
65 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
66 #endif
68 #include <getopt.h>
70 #ifdef ETAGS_REGEXPS
71 #include <regex.h>
72 #endif /* ETAGS_REGEXPS */
74 #define TRUE 1
75 #define FALSE 0
77 /* Define CTAGS to make the program "ctags" compatible with the usual one.
78 Let it undefined to make the program "etags", which makes emacs-style
79 tag tables and tags typedefs, #defines and struct/union/enum by default. */
80 #ifdef CTAGS
81 # undef CTAGS
82 # define CTAGS TRUE
83 #else
84 # define CTAGS FALSE
85 #endif
87 /* Exit codes for success and failure. */
88 #ifdef VMS
89 #define GOOD 1
90 #define BAD 0
91 #else
92 #define GOOD 0
93 #define BAD 1
94 #endif
96 /* C extensions. */
97 #define C_PLPL 0x00001 /* C++ */
98 #define C_STAR 0x00003 /* C* */
99 #define YACC 0x10000 /* yacc file */
101 #define streq(s,t) (strcmp (s, t) == 0)
102 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
104 #define iswhite(arg) (_wht[arg]) /* T if char is white */
105 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
106 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
107 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
110 * xnew -- allocate storage
112 * SYNOPSIS: Type *xnew (int n, Type);
114 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
116 typedef int logical;
118 typedef struct nd_st
119 { /* sorting structure */
120 char *name; /* function or type name */
121 char *file; /* file name */
122 logical is_func; /* use pattern or line no */
123 logical named; /* list name separately */
124 logical been_warned; /* set if noticed dup */
125 int lno; /* line number tag is on */
126 long cno; /* character number line starts on */
127 char *pat; /* search pattern */
128 struct nd_st *left, *right; /* left and right sons */
129 } NODE;
131 extern char *getenv ();
133 char *concat ();
134 char *savenstr (), *savestr ();
135 char *etags_strchr (), *etags_strrchr ();
136 char *etags_getcwd ();
137 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
138 char *xmalloc (), *xrealloc ();
140 typedef void Lang_function ();
141 #if FALSE /* many compilers barf on this */
142 Lang_function Asm_labels;
143 Lang_function default_C_entries;
144 Lang_function C_entries;
145 Lang_function Cplusplus_entries;
146 Lang_function Cstar_entries;
147 Lang_function Fortran_functions;
148 Lang_function Yacc_entries;
149 Lang_function Lisp_functions;
150 Lang_function Pascal_functions;
151 Lang_function Prolog_functions;
152 Lang_function Scheme_functions;
153 Lang_function TeX_functions;
154 Lang_function just_read_file;
155 #else /* so let's write it this way */
156 void Asm_labels ();
157 void default_C_entries ();
158 void C_entries ();
159 void Cplusplus_entries ();
160 void Cstar_entries ();
161 void Fortran_functions ();
162 void Yacc_entries ();
163 void Lisp_functions ();
164 void Pascal_functions ();
165 void Prolog_functions ();
166 void Scheme_functions ();
167 void TeX_functions ();
168 void just_read_file ();
169 #endif
171 logical get_language ();
172 int total_size_of_entries ();
173 long readline ();
174 long readline_internal ();
175 #ifdef ETAGS_REGEXPS
176 void add_regex ();
177 #endif
178 void add_node ();
179 void error ();
180 void fatal (), pfatal ();
181 void find_entries ();
182 void free_tree ();
183 void getit ();
184 void init ();
185 void initbuffer ();
186 void pfnote ();
187 void process_file ();
188 void put_entries ();
189 void takeprec ();
192 char searchar = '/'; /* use /.../ searches */
194 int lineno; /* line number of current line */
195 long charno; /* current character number */
197 long linecharno; /* charno of start of line; not used by C,
198 but by every other language. */
200 char *curfile; /* current input file name */
201 char *tagfile; /* output file */
202 char *progname; /* name this program was invoked with */
203 char *cwd; /* current working directory */
204 char *tagfiledir; /* directory of tagfile */
206 FILE *tagf; /* ioptr for tags file */
207 NODE *head; /* the head of the binary tree of tags */
210 * A `struct linebuffer' is a structure which holds a line of text.
211 * `readline' reads a line from a stream into a linebuffer and works
212 * regardless of the length of the line.
214 struct linebuffer
216 long size;
217 char *buffer;
220 struct linebuffer lb; /* the current line */
221 struct
223 long linepos;
224 struct linebuffer lb; /* used by C_entries instead of lb */
225 } lbs[2];
227 /* boolean "functions" (see init) */
228 logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
229 char
230 *white = " \f\t\n\013", /* white chars */
231 *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
232 /* token starting chars */
233 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
234 /* valid in-token chars */
235 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
237 logical append_to_tagfile; /* -a: append to tags */
238 /* The following three default to TRUE for etags, but to FALSE for ctags. */
239 logical typedefs; /* -t: create tags for typedefs */
240 logical typedefs_and_cplusplus; /* -T: create tags for typedefs, level */
241 /* 0 struct/enum/union decls, and C++ */
242 /* member functions. */
243 logical constantypedefs; /* -d: create tags for C #define and enum */
244 /* constants. Enum consts not implemented. */
245 /* -D: opposite of -d. Default under ctags. */
246 logical update; /* -u: update tags */
247 logical vgrind_style; /* -v: create vgrind style index output */
248 logical no_warnings; /* -w: suppress warnings */
249 logical cxref_style; /* -x: create cxref style output */
250 logical cplusplus; /* .[hc] means C++, not C */
251 logical noindentypedefs; /* -I: ignore indentation in C */
252 #define permit_duplicates TRUE /* allow duplicate tags */
254 struct option longopts[] =
256 { "append", no_argument, NULL, 'a' },
257 { "backward-search", no_argument, NULL, 'B' },
258 { "c++", no_argument, NULL, 'C' },
259 { "cxref", no_argument, NULL, 'x' },
260 { "defines", no_argument, NULL, 'd' },
261 { "help", no_argument, NULL, 'h' },
262 { "help", no_argument, NULL, 'H' },
263 { "ignore-indentation", no_argument, NULL, 'I' },
264 { "include", required_argument, NULL, 'i' },
265 { "language", required_argument, NULL, 'l' },
266 { "no-defines", no_argument, NULL, 'D' },
267 { "no-regex", no_argument, NULL, 'R' },
268 { "no-warn", no_argument, NULL, 'w' },
269 { "output", required_argument, NULL, 'o' },
270 { "regex", required_argument, NULL, 'r' },
271 { "typedefs", no_argument, NULL, 't' },
272 { "typedefs-and-c++", no_argument, NULL, 'T' },
273 { "update", no_argument, NULL, 'u' },
274 { "version", no_argument, NULL, 'V' },
275 { "vgrind", no_argument, NULL, 'v' },
276 { 0 }
279 #ifdef ETAGS_REGEXPS
280 /* Structure defining a regular expression. Elements are
281 the compiled pattern, and the name string. */
282 struct pattern
284 struct re_pattern_buffer *pattern;
285 struct re_registers regs;
286 char *name_pattern;
287 logical error_signaled;
290 /* Number of regexps found. */
291 int num_patterns = 0;
293 /* Array of all regexps. */
294 struct pattern *patterns = NULL;
295 #endif /* ETAGS_REGEXPS */
297 /* Language stuff. */
298 struct lang_entry
300 char *extension;
301 Lang_function *function;
304 /* Table of language names and corresponding functions. */
305 /* It is ok for a given function to be listed under more than one
306 name. I just didn't. */
307 /* "auto" language reverts to default behavior. */
308 struct lang_entry lang_names[] =
310 { "asm", Asm_labels },
311 { "c", default_C_entries },
312 { "c++", Cplusplus_entries },
313 { "c*", Cstar_entries },
314 { "fortran", Fortran_functions },
315 { "lisp", Lisp_functions },
316 { "none", just_read_file },
317 { "pascal", Pascal_functions },
318 { "scheme" , Scheme_functions },
319 { "tex", TeX_functions },
320 { "auto", NULL },
321 { NULL, NULL }
324 /* Table of file extensions and corresponding language functions. */
325 struct lang_entry lang_extensions[] =
327 /* Assume that ".s" or ".a" is assembly code. -wolfgang.
328 Or even ".sa". */
329 { "a", Asm_labels }, /* Unix assembler */
330 { "asm", Asm_labels }, /* Microcontroller assembly */
331 { "def", Asm_labels }, /* BSO/Tasking definition includes */
332 { "inc", Asm_labels }, /* Microcontroller include files */
333 { "ins", Asm_labels }, /* Microcontroller include files */
334 { "s", Asm_labels },
335 { "sa", Asm_labels }, /* Unix assembler */
336 { "src", Asm_labels }, /* BSO/Tasking C compiler output */
338 /* .aux, .bbl, .clo, .cls, .dtx or .tex implies LaTeX source code. */
339 { "aux", TeX_functions },
340 { "bbl", TeX_functions },
341 { "clo", TeX_functions },
342 { "cls", TeX_functions },
343 { "dtx", TeX_functions },
344 { "sty", TeX_functions },
345 { "tex", TeX_functions },
347 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
348 { "cl", Lisp_functions },
349 { "clisp", Lisp_functions },
350 { "el", Lisp_functions },
351 { "l", Lisp_functions },
352 { "lisp", Lisp_functions },
353 { "lsp", Lisp_functions },
355 /* .scm or .sm or .scheme implies scheme source code */
356 { "SCM", Scheme_functions },
357 { "SM", Scheme_functions },
358 { "oak", Scheme_functions },
359 { "sch", Scheme_functions },
360 { "scheme", Scheme_functions },
361 { "scm", Scheme_functions },
362 { "sm", Scheme_functions },
363 { "t", Scheme_functions },
364 /* FIXME Can't do the `SCM' or `scm' prefix with a version number */
366 /* Note that ".c" and ".h" can be considered C++, if the --c++
367 flag was given. That is why default_C_entries is called here. */
368 { "c", default_C_entries },
369 { "h", default_C_entries },
371 /* .C or .H or .cpp or .cxx or .hxx or .hh or .cc or .cpp: a C++ file */
372 { "C", Cplusplus_entries },
373 { "H", Cplusplus_entries },
374 { "cc", Cplusplus_entries },
375 { "cpp", Cplusplus_entries },
376 { "cxx", Cplusplus_entries },
377 { "hh", Cplusplus_entries },
378 { "hxx", Cplusplus_entries },
380 /* .y: a yacc file */
381 { "y", Yacc_entries },
383 /* .cs or .hs: a C* file */
384 { "cs", Cstar_entries },
385 { "hs", Cstar_entries },
387 /* .f and .for are FORTRAN. */
388 { "F", Fortran_functions },
389 { "f", Fortran_functions },
390 { "for", Fortran_functions },
392 /* .pl implies prolog source code */
393 { "pl", Prolog_functions },
395 /* .p or .pas: a Pascal file */
396 { "p", Pascal_functions },
397 { "pas", Pascal_functions },
399 { NULL, NULL }
402 /* Non-NULL if language fixed. */
403 Lang_function *lang_func = NULL;
406 void
407 print_language_names ()
409 struct lang_entry *name, *ext;
411 puts ("\nThese are the currently supported languages, along with the\n\
412 default extensions for files:");
413 for (name = lang_names; name->extension; ++name)
415 printf ("\t%s\t", name->extension);
416 for (ext = lang_extensions; ext->extension; ++ext)
417 if (name->function == ext->function)
418 printf (" .%s", ext->extension);
419 puts ("");
421 puts ("Where `auto' means use default language for files based on filename\n\
422 extension, and `none' means only do regexp processing on files.\n\
423 If no language is specified and no extension is found for some file,\n\
424 Fortran is tried first; if no tags are found, C is tried next.");
427 void
428 print_version ()
430 #ifdef VERSION
431 printf ("%s for Emacs version %s.\n", (CTAGS) ? "CTAGS" : "ETAGS", VERSION);
432 #else
433 printf ("%s for Emacs version 19.\n", (CTAGS) ? "CTAGS" : "ETAGS");
434 #endif
436 exit (GOOD);
439 void
440 print_help ()
442 printf ("These are the options accepted by %s. You may use unambiguous\n\
443 abbreviations for the long option names. A - as file name means read\n\
444 names from stdin.\n\n", progname);
446 puts ("-a, --append\n\
447 Append tag entries to existing tags file.");
449 if (CTAGS)
450 puts ("-B, --backward-search\n\
451 Write the search commands for the tag entries using '?', the\n\
452 backward-search command instead of '/', the forward-search command.");
454 puts ("-C, --c++\n\
455 Treat files whose extension defaults to C language as C++ files.");
457 if (CTAGS)
458 puts ("-d, --defines\n\
459 Create tag entries for constant C #defines, too.");
460 else
461 puts ("-D, --no-defines\n\
462 Don't create tag entries for constant C #defines. This makes\n\
463 the tags file smaller.");
465 if (!CTAGS)
467 puts ("-i FILE, --include=FILE\n\
468 Include a note in tag file indicating that, when searching for\n\
469 a tag, one should also consult the tags file FILE after\n\
470 checking the current file.");
471 puts ("-l LANG, --language=LANG\n\
472 Force the following files to be considered as written in the\n\
473 named language up to the next --language=LANG option.");
476 #ifdef ETAGS_REGEXPS
477 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
478 Make a tag for each line matching pattern REGEXP in the\n\
479 following files. REGEXP is anchored (as if preceded by ^).\n\
480 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
481 named tags can be created with:\n\
482 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
483 puts ("-R, --no-regex\n\
484 Don't create tags from regexps for the following files.");
485 #endif /* ETAGS_REGEXPS */
486 puts ("-o FILE, --output=FILE\n\
487 Write the tags to FILE.");
488 puts ("-I, --ignore-indentation\n\
489 Don't rely on indentation quite as much as normal. Currently,\n\
490 this means not to assume that a closing brace in the first\n\
491 column is the final brace of a function or structure\n\
492 definition in C and C++.");
494 if (CTAGS)
496 puts ("-t, --typedefs\n\
497 Generate tag entries for C typedefs.");
498 puts ("-T, --typedefs-and-c++\n\
499 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
500 and C++ member functions.");
501 puts ("-u, --update\n\
502 Update the tag entries for the given files, leaving tag\n\
503 entries for other files in place. Currently, this is\n\
504 implemented by deleting the existing entries for the given\n\
505 files and then rewriting the new entries at the end of the\n\
506 tags file. It is often faster to simply rebuild the entire\n\
507 tag file than to use this.");
508 puts ("-v, --vgrind\n\
509 Generates an index of items intended for human consumption,\n\
510 similar to the output of vgrind. The index is sorted, and\n\
511 gives the page number of each item.");
512 puts ("-w, --no-warn\n\
513 Suppress warning messages about entries defined in multiple\n\
514 files.");
515 puts ("-x, --cxref\n\
516 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
517 The output uses line numbers instead of page numbers, but\n\
518 beyond that the differences are cosmetic; try both to see\n\
519 which you like.");
522 puts ("-V, --version\n\
523 Print the version of the program.\n\
524 -h, --help\n\
525 Print this help message.");
527 print_language_names ();
529 exit (GOOD);
533 enum argument_type
535 at_language,
536 at_regexp,
537 at_filename
540 /* This structure helps us allow mixing of --lang and filenames. */
541 typedef struct
543 enum argument_type arg_type;
544 char *what;
545 Lang_function *function;
546 } ARGUMENT;
548 #ifdef VMS /* VMS specific functions */
550 #define EOS '\0'
552 /* This is a BUG! ANY arbitrary limit is a BUG!
553 Won't someone please fix this? */
554 #define MAX_FILE_SPEC_LEN 255
555 typedef struct {
556 short curlen;
557 char body[MAX_FILE_SPEC_LEN + 1];
558 } vspec;
561 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
562 returning in each successive call the next filename matching the input
563 spec. The function expects that each in_spec passed
564 to it will be processed to completion; in particular, up to and
565 including the call following that in which the last matching name
566 is returned, the function ignores the value of in_spec, and will
567 only start processing a new spec with the following call.
568 If an error occurs, on return out_spec contains the value
569 of in_spec when the error occurred.
571 With each successive filename returned in out_spec, the
572 function's return value is one. When there are no more matching
573 names the function returns zero. If on the first call no file
574 matches in_spec, or there is any other error, -1 is returned.
577 #include <rmsdef.h>
578 #include <descrip.h>
579 #define OUTSIZE MAX_FILE_SPEC_LEN
580 short
581 fn_exp (out, in)
582 vspec *out;
583 char *in;
585 static long context = 0;
586 static struct dsc$descriptor_s o;
587 static struct dsc$descriptor_s i;
588 static logical pass1 = TRUE;
589 long status;
590 short retval;
592 if (pass1)
594 pass1 = FALSE;
595 o.dsc$a_pointer = (char *) out;
596 o.dsc$w_length = (short)OUTSIZE;
597 i.dsc$a_pointer = in;
598 i.dsc$w_length = (short)strlen(in);
599 i.dsc$b_dtype = DSC$K_DTYPE_T;
600 i.dsc$b_class = DSC$K_CLASS_S;
601 o.dsc$b_dtype = DSC$K_DTYPE_VT;
602 o.dsc$b_class = DSC$K_CLASS_VS;
604 if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL)
606 out->body[out->curlen] = EOS;
607 return 1;
609 else if (status == RMS$_NMF)
610 retval = 0;
611 else
613 strcpy(out->body, in);
614 retval = -1;
616 lib$find_file_end(&context);
617 pass1 = TRUE;
618 return retval;
622 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
623 name of each file specified by the provided arg expanding wildcards.
625 char *
626 gfnames (arg, p_error)
627 char *arg;
628 logical *p_error;
630 static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
632 switch (fn_exp (&filename, arg))
634 case 1:
635 *p_error = FALSE;
636 return filename.body;
637 case 0:
638 *p_error = FALSE;
639 return NULL;
640 default:
641 *p_error = TRUE;
642 return filename.body;
646 #ifndef OLD /* Newer versions of VMS do provide `system'. */
647 system (cmd)
648 char *cmd;
650 fprintf (stderr, "system() function not implemented under VMS\n");
652 #endif
654 #define VERSION_DELIM ';'
655 char *massage_name (s)
656 char *s;
658 char *start = s;
660 for ( ; *s; s++)
661 if (*s == VERSION_DELIM)
663 *s = EOS;
664 break;
666 else
667 *s = tolower(*s);
668 return start;
670 #endif /* VMS */
673 void
674 main (argc, argv)
675 int argc;
676 char *argv[];
678 int i;
679 unsigned int nincluded_files = 0;
680 char **included_files = xnew (argc, char *);
681 char *this_file;
682 ARGUMENT *argbuffer;
683 int current_arg = 0, file_count = 0;
684 struct linebuffer filename_lb;
685 #ifdef VMS
686 logical got_err;
687 #endif
689 #ifdef DOS_NT
690 _fmode = O_BINARY; /* all of files are treated as binary files */
691 #endif /* DOS_NT */
693 progname = argv[0];
695 /* Allocate enough no matter what happens. Overkill, but each one
696 is small. */
697 argbuffer = xnew (argc, ARGUMENT);
699 #ifdef ETAGS_REGEXPS
700 /* Set syntax for regular expression routines. */
701 re_set_syntax (RE_SYNTAX_EMACS);
702 #endif /* ETAGS_REGEXPS */
705 * If etags, always find typedefs and structure tags. Why not?
706 * Also default is to find macro constants.
708 if (!CTAGS)
709 typedefs = typedefs_and_cplusplus = constantypedefs = TRUE;
711 while (1)
713 int opt = getopt_long (argc, argv,
714 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts, 0);
716 if (opt == EOF)
717 break;
719 switch (opt)
721 case 0:
722 /* If getopt returns 0, then it has already processed a
723 long-named option. We should do nothing. */
724 break;
726 case 1:
727 /* This means that a filename has been seen. Record it. */
728 argbuffer[current_arg].arg_type = at_filename;
729 argbuffer[current_arg].what = optarg;
730 ++current_arg;
731 ++file_count;
732 break;
734 /* Common options. */
735 case 'a':
736 append_to_tagfile = TRUE;
737 break;
738 case 'C':
739 cplusplus = TRUE;
740 break;
741 case 'd':
742 constantypedefs = TRUE;
743 break;
744 case 'D':
745 constantypedefs = FALSE;
746 break;
747 case 'f': /* for compatibility with old makefiles */
748 case 'o':
749 if (tagfile)
751 fprintf (stderr, "%s: -%c option may only be given once.\n",
752 progname, opt);
753 goto usage;
755 tagfile = optarg;
756 break;
757 case 'I':
758 case 'S': /* for backward compatibility */
759 noindentypedefs = TRUE;
760 break;
761 case 'l':
762 if (!get_language (optarg, &argbuffer[current_arg].function))
764 fprintf (stderr, "%s: language \"%s\" not recognized.\n",
765 progname, optarg);
766 goto usage;
768 argbuffer[current_arg].arg_type = at_language;
769 ++current_arg;
770 break;
771 #ifdef ETAGS_REGEXPS
772 case 'r':
773 argbuffer[current_arg].arg_type = at_regexp;
774 argbuffer[current_arg].what = optarg;
775 ++current_arg;
776 break;
777 case 'R':
778 argbuffer[current_arg].arg_type = at_regexp;
779 argbuffer[current_arg].what = NULL;
780 ++current_arg;
781 break;
782 #endif /* ETAGS_REGEXPS */
783 case 'V':
784 print_version ();
785 break;
786 case 'h':
787 case 'H':
788 print_help ();
789 break;
790 case 't':
791 typedefs = TRUE;
792 break;
793 case 'T':
794 typedefs = typedefs_and_cplusplus = TRUE;
795 break;
796 #if (!CTAGS)
797 /* Etags options */
798 case 'i':
799 included_files[nincluded_files++] = optarg;
800 break;
801 #else /* CTAGS */
802 /* Ctags options. */
803 case 'B':
804 searchar = '?';
805 break;
806 case 'u':
807 update = TRUE;
808 break;
809 case 'v':
810 vgrind_style = TRUE;
811 /*FALLTHRU*/
812 case 'x':
813 cxref_style = TRUE;
814 break;
815 case 'w':
816 no_warnings = TRUE;
817 break;
818 #endif /* CTAGS */
819 default:
820 goto usage;
824 for (; optind < argc; ++optind)
826 argbuffer[current_arg].arg_type = at_filename;
827 argbuffer[current_arg].what = argv[optind];
828 ++current_arg;
829 ++file_count;
832 if (nincluded_files == 0 && file_count == 0)
834 fprintf (stderr, "%s: No input files specified.\n", progname);
836 usage:
837 fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
838 progname);
839 exit (BAD);
842 if (tagfile == NULL)
844 tagfile = CTAGS ? "tags" : "TAGS";
846 cwd = etags_getcwd (); /* the current working directory */
847 strcat (cwd, "/");
848 if (streq (tagfile, "-"))
850 tagfiledir = cwd;
852 else
854 tagfiledir = absolute_dirname (tagfile, cwd);
857 init (); /* set up boolean "functions" */
859 initbuffer (&lb);
860 initbuffer (&lbs[0].lb);
861 initbuffer (&lbs[1].lb);
862 initbuffer (&filename_lb);
864 if (!CTAGS)
866 if (streq (tagfile, "-"))
867 tagf = stdout;
868 else
869 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
870 if (tagf == NULL)
871 pfatal (tagfile);
875 * Loop through files finding functions.
877 for (i = 0; i < current_arg; ++i)
879 switch (argbuffer[i].arg_type)
881 case at_language:
882 lang_func = argbuffer[i].function;
883 break;
884 #ifdef ETAGS_REGEXPS
885 case at_regexp:
886 add_regex (argbuffer[i].what);
887 break;
888 #endif
889 case at_filename:
890 #ifdef VMS
891 while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL)
893 if (got_err)
895 error ("Can't find file %s\n", this_file);
896 argc--, argv++;
898 else
900 this_file = massage_name (this_file);
902 #else
903 this_file = argbuffer[i].what;
904 #endif
905 /* Input file named "-" means read file names from stdin
906 and use them. */
907 if (streq (this_file, "-"))
909 while (!feof (stdin))
911 (void) readline_internal (&filename_lb, stdin);
912 if (strlen (filename_lb.buffer) > 0)
913 process_file (filename_lb.buffer);
916 else
917 process_file (this_file);
918 #ifdef VMS
920 #endif
921 break;
925 if (!CTAGS)
927 while (nincluded_files-- > 0)
928 fprintf (tagf, "\f\n%s,include\n", *included_files++);
930 fclose (tagf);
931 exit (GOOD);
934 /* If CTAGS, we are here. process_file did not write the tags yet,
935 because we want them ordered. Let's do it now. */
936 if (cxref_style)
938 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
939 if (tagf == NULL)
940 pfatal (tagfile);
941 put_entries (head);
942 exit (GOOD);
945 if (update)
947 char cmd[BUFSIZ];
948 for (i = 0; i < current_arg; ++i)
950 if (argbuffer[i].arg_type != at_filename)
951 continue;
952 sprintf (cmd,
953 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
954 tagfile, argbuffer[i].what, tagfile);
955 if (system (cmd) != GOOD)
956 fatal ("failed to execute shell command");
958 append_to_tagfile = TRUE;
961 tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
962 if (tagf == NULL)
963 pfatal (tagfile);
964 put_entries (head);
965 fclose (tagf);
967 if (update)
969 char cmd[BUFSIZ];
970 sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
971 exit (system (cmd));
973 exit (GOOD);
978 * Set the language, given the name.
980 logical
981 get_language (language, func)
982 char *language;
983 Lang_function **func;
985 struct lang_entry *lang;
987 for (lang = lang_names; lang->extension; ++lang)
989 if (streq (language, lang->extension))
991 *func = lang->function;
992 return TRUE;
996 return FALSE;
1001 * This routine is called on each file argument.
1003 void
1004 process_file (file)
1005 char *file;
1007 struct stat stat_buf;
1008 FILE *inf;
1010 if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
1012 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
1013 return;
1015 if (streq (file, tagfile) && !streq (tagfile, "-"))
1017 fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
1018 return;
1020 inf = fopen (file, "r");
1021 if (inf == NULL)
1023 perror (file);
1024 return;
1027 find_entries (file, inf);
1029 if (!CTAGS)
1031 char *filename;
1033 if (file[0] == '/')
1035 /* file is an absolute filename. Canonicalise it. */
1036 filename = absolute_filename (file, cwd);
1038 else
1040 /* file is a filename relative to cwd. Make it relative
1041 to the directory of the tags file. */
1042 filename = relative_filename (file, tagfiledir);
1044 fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
1045 put_entries (head);
1046 free_tree (head);
1047 head = NULL;
1052 * This routine sets up the boolean pseudo-functions which work
1053 * by setting boolean flags dependent upon the corresponding character
1054 * Every char which is NOT in that string is not a white char. Therefore,
1055 * all of the array "_wht" is set to FALSE, and then the elements
1056 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1057 * of a char is TRUE if it is the string "white", else FALSE.
1059 void
1060 init ()
1062 register char *sp;
1063 register int i;
1065 for (i = 0; i < 0177; i++)
1066 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
1067 for (sp = white; *sp; sp++)
1068 _wht[*sp] = TRUE;
1069 for (sp = endtk; *sp; sp++)
1070 _etk[*sp] = TRUE;
1071 for (sp = intk; *sp; sp++)
1072 _itk[*sp] = TRUE;
1073 for (sp = begtk; *sp; sp++)
1074 _btk[*sp] = TRUE;
1075 _wht[0] = _wht['\n'];
1076 _etk[0] = _etk['\n'];
1077 _btk[0] = _btk['\n'];
1078 _itk[0] = _itk['\n'];
1082 * This routine opens the specified file and calls the function
1083 * which finds the function and type definitions.
1085 void
1086 find_entries (file, inf)
1087 char *file;
1088 FILE *inf;
1090 char *cp;
1091 struct lang_entry *lang;
1092 NODE *old_last_node;
1093 extern NODE *last_node;
1095 curfile = savestr (file);
1096 cp = etags_strrchr (file, '.');
1098 /* If user specified a language, use it. */
1099 if (lang_func != NULL)
1101 lang_func (inf);
1102 fclose (inf);
1103 return;
1106 if (cp)
1108 ++cp;
1109 for (lang = lang_extensions; lang->extension; ++lang)
1111 if (streq (cp, lang->extension))
1113 lang->function (inf);
1114 fclose (inf);
1115 return;
1120 /* Try Fortran. */
1121 old_last_node = last_node;
1122 Fortran_functions (inf);
1124 /* No Fortran entries found. Try C. */
1125 if (old_last_node == last_node)
1126 default_C_entries (inf);
1127 fclose (inf);
1130 /* Record a tag. */
1131 /* Should take a TOKEN* instead!! */
1132 void
1133 pfnote (name, is_func, named, linestart, linelen, lno, cno)
1134 char *name; /* tag name */
1135 logical is_func; /* tag is a function */
1136 logical named; /* tag different from text of definition */
1137 char *linestart; /* start of the line where tag is */
1138 int linelen; /* length of the line where tag is */
1139 int lno; /* line number */
1140 long cno; /* character number */
1142 register NODE *np = xnew (1, NODE);
1143 register char *fp;
1145 /* If ctags mode, change name "main" to M<thisfilename>. */
1146 if (CTAGS && !cxref_style && streq (name, "main"))
1148 fp = etags_strrchr (curfile, '/');
1149 np->name = concat ("M", fp == 0 ? curfile : fp + 1, "");
1150 fp = etags_strrchr (np->name, '.');
1151 if (fp && fp[1] != '\0' && fp[2] == '\0')
1152 fp[0] = 0;
1153 np->named = TRUE;
1155 else
1157 np->name = name;
1158 np->named = named;
1160 np->file = curfile;
1161 np->is_func = is_func;
1162 np->lno = lno;
1163 /* Our char numbers are 0-base, because of C language tradition?
1164 ctags compatibility? old versions compatibility? I don't know.
1165 Anyway, since emacs's are 1-base we espect etags.el to take care
1166 of the difference. If we wanted to have 1-based numbers, we would
1167 uncomment the +1 below. */
1168 np->cno = cno /* + 1 */ ;
1169 np->left = np->right = NULL;
1170 np->pat = savenstr (linestart, ((CTAGS && !cxref_style) ? 50 : linelen));
1172 add_node (np, &head);
1176 * free_tree ()
1177 * recurse on left children, iterate on right children.
1179 void
1180 free_tree (node)
1181 register NODE *node;
1183 while (node)
1185 register NODE *node_right = node->right;
1186 free_tree (node->left);
1187 if (node->named)
1188 free (node->name);
1189 free (node->pat);
1190 free ((char *) node);
1191 node = node_right;
1196 * add_node ()
1197 * Adds a node to the tree of nodes. In etags mode, we don't keep
1198 * it sorted; we just keep a linear list. In ctags mode, maintain
1199 * an ordered tree, with no attempt at balancing.
1201 * add_node is the only function allowed to add nodes, so it can
1202 * maintain state.
1204 NODE *last_node = NULL;
1205 void
1206 add_node (node, cur_node_p)
1207 NODE *node, **cur_node_p;
1209 register int dif;
1210 register NODE *cur_node = *cur_node_p;
1212 if (cur_node == NULL)
1214 *cur_node_p = node;
1215 last_node = node;
1216 return;
1219 if (!CTAGS)
1221 /* Etags Mode */
1222 if (last_node == NULL)
1223 fatal ("internal error in add_node", 0);
1224 last_node->right = node;
1225 last_node = node;
1227 else
1229 /* Ctags Mode */
1230 dif = strcmp (node->name, cur_node->name);
1233 * If this tag name matches an existing one, then
1234 * do not add the node, but maybe print a warning.
1236 if (!dif)
1238 if (node->file == cur_node->file)
1240 if (!no_warnings)
1242 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
1243 node->file, lineno, node->name);
1244 fprintf (stderr, "Second entry ignored\n");
1246 return;
1248 if (!cur_node->been_warned && !no_warnings)
1250 fprintf (stderr,
1251 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1252 node->file, cur_node->file, node->name);
1254 cur_node->been_warned = TRUE;
1255 return;
1258 /* Maybe refuse to add duplicate nodes. */
1259 if (!permit_duplicates)
1261 if (streq (node->name, cur_node->name)
1262 && streq (node->file, cur_node->file))
1263 return;
1266 /* Actually add the node */
1267 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
1271 void
1272 put_entries (node)
1273 register NODE *node;
1275 register char *sp;
1277 if (node == NULL)
1278 return;
1280 /* Output subentries that precede this one */
1281 put_entries (node->left);
1283 /* Output this entry */
1285 if (!CTAGS)
1287 if (node->named)
1289 fprintf (tagf, "%s\177%s\001%d,%d\n",
1290 node->pat, node->name,
1291 node->lno, node->cno);
1293 else
1295 fprintf (tagf, "%s\177%d,%d\n",
1296 node->pat,
1297 node->lno, node->cno);
1300 else if (!cxref_style)
1302 fprintf (tagf, "%s\t%s\t",
1303 node->name, node->file);
1305 if (node->is_func)
1306 { /* a function */
1307 putc (searchar, tagf);
1308 putc ('^', tagf);
1310 for (sp = node->pat; *sp; sp++)
1312 if (*sp == '\\' || *sp == searchar)
1313 putc ('\\', tagf);
1314 putc (*sp, tagf);
1316 putc (searchar, tagf);
1318 else
1319 { /* a typedef; text pattern inadequate */
1320 fprintf (tagf, "%d", node->lno);
1322 putc ('\n', tagf);
1324 else if (vgrind_style)
1325 fprintf (stdout, "%s %s %d\n",
1326 node->name, node->file, (node->lno + 63) / 64);
1327 else
1328 fprintf (stdout, "%-16s %3d %-16s %s\n",
1329 node->name, node->lno, node->file, node->pat);
1331 /* Output subentries that follow this one */
1332 put_entries (node->right);
1335 /* Length of a number's decimal representation. */
1337 number_len (num)
1338 long num;
1340 int len = 0;
1341 if (!num)
1342 return 1;
1343 for (; num; num /= 10)
1344 ++len;
1345 return len;
1349 * Return total number of characters that put_entries will output for
1350 * the nodes in the subtree of the specified node. Works only if
1351 * we are not ctags, but called only in that case. This count
1352 * is irrelevant with the new tags.el, but is still supplied for
1353 * backward compatibility.
1356 total_size_of_entries (node)
1357 register NODE *node;
1359 register int total;
1361 if (node == NULL)
1362 return 0;
1364 total = 0;
1365 for (; node; node = node->right)
1367 /* Count left subentries. */
1368 total += total_size_of_entries (node->left);
1370 /* Count this entry */
1371 total += strlen (node->pat) + 1;
1372 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1373 if (node->named)
1374 total += 1 + strlen (node->name); /* \001name */
1377 return total;
1381 * The C symbol tables.
1383 enum sym_type
1385 st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
1388 /* Feed stuff between (but not including) %[ and %] lines to:
1389 gperf -c -k1,3 -o -p -r -t
1391 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1393 class, C_PLPL, st_C_struct
1394 domain, C_STAR, st_C_struct
1395 union, 0, st_C_struct
1396 struct, 0, st_C_struct
1397 enum, 0, st_C_enum
1398 typedef, 0, st_C_typedef
1399 define, 0, st_C_define
1400 long, 0, st_C_typespec
1401 short, 0, st_C_typespec
1402 int, 0, st_C_typespec
1403 char, 0, st_C_typespec
1404 float, 0, st_C_typespec
1405 double, 0, st_C_typespec
1406 signed, 0, st_C_typespec
1407 unsigned, 0, st_C_typespec
1408 auto, 0, st_C_typespec
1409 void, 0, st_C_typespec
1410 extern, 0, st_C_typespec
1411 static, 0, st_C_typespec
1412 const, 0, st_C_typespec
1413 volatile, 0, st_C_typespec
1415 and replace lines between %< and %> with its output. */
1416 /*%<*/
1417 /* C code produced by gperf version 1.8.1 (K&R C version) */
1418 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1421 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
1423 #define MIN_WORD_LENGTH 3
1424 #define MAX_WORD_LENGTH 8
1425 #define MIN_HASH_VALUE 10
1426 #define MAX_HASH_VALUE 62
1428 21 keywords
1429 53 is the maximum key range
1432 static int
1433 hash (str, len)
1434 register char *str;
1435 register int len;
1437 static unsigned char hash_table[] =
1439 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1440 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1441 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1442 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1443 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1444 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1445 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1446 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1447 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1448 62, 62, 62, 62, 62, 62, 62, 2, 62, 7,
1449 6, 9, 15, 30, 62, 24, 62, 62, 1, 24,
1450 7, 27, 13, 62, 19, 26, 18, 27, 1, 62,
1451 62, 62, 62, 62, 62, 62, 62, 62,
1453 return len + hash_table[str[2]] + hash_table[str[0]];
1456 struct C_stab_entry *
1457 in_word_set (str, len)
1458 register char *str;
1459 register int len;
1462 static struct C_stab_entry wordlist[] =
1464 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1465 {"",},
1466 {"volatile", 0, st_C_typespec},
1467 {"",},
1468 {"long", 0, st_C_typespec},
1469 {"char", 0, st_C_typespec},
1470 {"class", C_PLPL, st_C_struct},
1471 {"",}, {"",}, {"",}, {"",},
1472 {"const", 0, st_C_typespec},
1473 {"",}, {"",}, {"",}, {"",},
1474 {"auto", 0, st_C_typespec},
1475 {"",}, {"",},
1476 {"define", 0, st_C_define},
1477 {"",},
1478 {"void", 0, st_C_typespec},
1479 {"",}, {"",}, {"",},
1480 {"extern", 0, st_C_typespec},
1481 {"static", 0, st_C_typespec},
1482 {"",},
1483 {"domain", C_STAR, st_C_struct},
1484 {"",},
1485 {"typedef", 0, st_C_typedef},
1486 {"double", 0, st_C_typespec},
1487 {"enum", 0, st_C_enum},
1488 {"",}, {"",}, {"",}, {"",},
1489 {"int", 0, st_C_typespec},
1490 {"",},
1491 {"float", 0, st_C_typespec},
1492 {"",}, {"",}, {"",},
1493 {"struct", 0, st_C_struct},
1494 {"",}, {"",}, {"",}, {"",},
1495 {"union", 0, st_C_struct},
1496 {"",},
1497 {"short", 0, st_C_typespec},
1498 {"",}, {"",},
1499 {"unsigned", 0, st_C_typespec},
1500 {"signed", 0, st_C_typespec},
1503 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
1505 register int key = hash (str, len);
1507 if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
1509 register char *s = wordlist[key].name;
1511 if (*s == *str && strneq (str + 1, s + 1, len - 1))
1512 return &wordlist[key];
1515 return 0;
1517 /*%>*/
1519 enum sym_type
1520 C_symtype(str, len, c_ext)
1521 char *str;
1522 int len;
1523 int c_ext;
1525 register struct C_stab_entry *se = in_word_set(str, len);
1527 if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
1528 return st_none;
1529 return se->type;
1533 * C functions are recognized using a simple finite automaton.
1534 * funcdef is its state variable.
1536 typedef enum
1538 fnone, /* nothing seen */
1539 ftagseen, /* function-like tag seen */
1540 fstartlist, /* just after open parenthesis */
1541 finlist, /* in parameter list */
1542 flistseen, /* after parameter list */
1543 fignore /* before open brace */
1544 } FUNCST;
1545 FUNCST funcdef;
1549 * typedefs are recognized using a simple finite automaton.
1550 * typeddef is its state variable.
1552 typedef enum
1554 tnone, /* nothing seen */
1555 ttypedseen, /* typedef keyword seen */
1556 tinbody, /* inside typedef body */
1557 tend, /* just before typedef tag */
1558 tignore /* junk after typedef tag */
1559 } TYPEDST;
1560 TYPEDST typdef;
1564 * struct-like structures (enum, struct and union) are recognized
1565 * using another simple finite automaton. `structdef' is its state
1566 * variable.
1568 typedef enum
1570 snone, /* nothing seen yet */
1571 skeyseen, /* struct-like keyword seen */
1572 stagseen, /* struct-like tag seen */
1573 scolonseen, /* colon seen after struct-like tag */
1574 sinbody /* in struct body: recognize member func defs*/
1575 } STRUCTST;
1576 STRUCTST structdef;
1579 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1580 * struct tag, and structtype is the type of the preceding struct-like
1581 * keyword.
1583 char *structtag = "<uninited>";
1584 enum sym_type structtype;
1587 * Yet another little state machine to deal with preprocessor lines.
1589 typedef enum
1591 dnone, /* nothing seen */
1592 dsharpseen, /* '#' seen as first char on line */
1593 ddefineseen, /* '#' and 'define' seen */
1594 dignorerest /* ignore rest of line */
1595 } DEFINEST;
1596 DEFINEST definedef;
1599 * Set this to TRUE, and the next token considered is called a function.
1600 * Used only for GNU emacs's function-defining macros.
1602 logical next_token_is_func;
1605 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1607 logical yacc_rules;
1610 * consider_token ()
1611 * checks to see if the current token is at the start of a
1612 * function, or corresponds to a typedef, or is a struct/union/enum
1613 * tag.
1615 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1616 * C_EXT is which language we are looking at.
1618 * In the future we will need some way to adjust where the end of
1619 * the token is; for instance, implementing the C++ keyword
1620 * `operator' properly will adjust the end of the token to be after
1621 * whatever follows `operator'.
1623 * Globals
1624 * funcdef IN OUT
1625 * structdef IN OUT
1626 * definedef IN OUT
1627 * typdef IN OUT
1628 * next_token_is_func IN OUT
1631 logical
1632 consider_token (str, len, c, c_ext, cblev, is_func)
1633 register char *str; /* IN: token pointer */
1634 register int len; /* IN: token length */
1635 register char c; /* IN: first char after the token */
1636 int c_ext; /* IN: C extensions mask */
1637 int cblev; /* IN: curly brace level */
1638 logical *is_func; /* OUT: function found */
1640 enum sym_type toktype = C_symtype (str, len, c_ext);
1643 * Advance the definedef state machine.
1645 switch (definedef)
1647 case dnone:
1648 /* We're not on a preprocessor line. */
1649 break;
1650 case dsharpseen:
1651 if (toktype == st_C_define)
1653 definedef = ddefineseen;
1655 else
1657 definedef = dignorerest;
1659 return FALSE;
1660 case ddefineseen:
1662 * Make a tag for any macro, unless it is a constant
1663 * and constantypedefs is FALSE.
1665 definedef = dignorerest;
1666 *is_func = (c == '(');
1667 if (!*is_func && !constantypedefs)
1668 return FALSE;
1669 else
1670 return TRUE;
1671 case dignorerest:
1672 return FALSE;
1673 default:
1674 error ("internal error: definedef value.", 0);
1678 * Now typedefs
1680 switch (typdef)
1682 case tnone:
1683 if (toktype == st_C_typedef)
1685 if (typedefs)
1686 typdef = ttypedseen;
1687 funcdef = fnone;
1688 return FALSE;
1690 break;
1691 case ttypedseen:
1692 switch (toktype)
1694 case st_none:
1695 case st_C_typespec:
1696 typdef = tend;
1697 break;
1698 case st_C_struct:
1699 case st_C_enum:
1700 break;
1702 /* Do not return here, so the structdef stuff has a chance. */
1703 break;
1704 case tend:
1705 switch (toktype)
1707 case st_C_typespec:
1708 case st_C_struct:
1709 case st_C_enum:
1710 return FALSE;
1712 return TRUE;
1716 * This structdef business is currently only invoked when cblev==0.
1717 * It should be recursively invoked whatever the curly brace level,
1718 * and a stack of states kept, to allow for definitions of structs
1719 * within structs.
1721 * This structdef business is NOT invoked when we are ctags and the
1722 * file is plain C. This is because a struct tag may have the same
1723 * name as another tag, and this loses with ctags.
1725 * This if statement deals with the typdef state machine as
1726 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1727 * return FALSE. All the other code here is for the structdef
1728 * state machine.
1730 switch (toktype)
1732 case st_C_struct:
1733 case st_C_enum:
1734 if (typdef == ttypedseen
1735 || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
1737 structdef = skeyseen;
1738 structtype = toktype;
1740 return FALSE;
1742 if (structdef == skeyseen)
1744 /* Save the tag for struct/union/class, for functions that may be
1745 defined inside. */
1746 if (structtype == st_C_struct)
1747 structtag = savenstr (str, len);
1748 else
1749 structtag = "<enum>";
1750 structdef = stagseen;
1751 return TRUE;
1754 /* Avoid entering funcdef stuff if typdef is going on. */
1755 if (typdef != tnone)
1757 definedef = dnone;
1758 return FALSE;
1761 /* Detect GNU macros. */
1762 if (definedef == dnone)
1763 if (strneq (str, "DEFUN", 5) /* Used in emacs */
1764 #if FALSE
1765 These are defined inside C functions, so currently they
1766 are not met anyway.
1767 || strneq (str, "EXFUN", 5) /* Used in glibc */
1768 || strneq (str, "DEFVAR_", 7) /* Used in emacs */
1769 #endif
1770 || strneq (str, "SYSCALL", 7) /* Used in glibc (mach) */
1771 || strneq (str, "ENTRY", 5) /* Used in glibc */
1772 || strneq (str, "PSEUDO", 6)) /* Used in glibc */
1775 next_token_is_func = TRUE;
1776 return FALSE;
1778 if (next_token_is_func)
1780 next_token_is_func = FALSE;
1781 funcdef = fignore;
1782 *is_func = TRUE;
1783 return TRUE;
1786 /* A function? */
1787 switch (toktype)
1789 case st_C_typespec:
1790 if (funcdef != finlist && funcdef != fignore)
1791 funcdef = fnone; /* should be useless */
1792 return FALSE;
1793 default:
1794 if (funcdef == fnone)
1796 funcdef = ftagseen;
1797 *is_func = TRUE;
1798 return TRUE;
1802 return FALSE;
1806 * C_entries ()
1807 * This routine finds functions, typedefs, #define's and
1808 * struct/union/enum definitions in C syntax and adds them
1809 * to the list.
1811 typedef struct
1813 char *str;
1814 logical named;
1815 int linelen;
1816 int lineno;
1817 } TOKEN;
1819 #define current_lb_is_new (newndx == curndx)
1820 #define switch_line_buffers() (curndx = 1 - curndx)
1822 #define curlb (lbs[curndx].lb)
1823 #define othlb (lbs[1-curndx].lb)
1824 #define newlb (lbs[newndx].lb)
1825 #define curlinepos (lbs[curndx].linepos)
1826 #define othlinepos (lbs[1-curndx].linepos)
1827 #define newlinepos (lbs[newndx].linepos)
1829 #define CNL_SAVE_DEFINEDEF \
1830 do { \
1831 curlinepos = charno; \
1832 lineno++; \
1833 charno += readline (&curlb, inf); \
1834 lp = curlb.buffer; \
1835 quotednl = FALSE; \
1836 newndx = curndx; \
1837 } while (0)
1839 #define CNL \
1840 do { \
1841 CNL_SAVE_DEFINEDEF; \
1842 if (token_saved) \
1844 tok = savetok; \
1845 token_saved = FALSE; \
1847 definedef = dnone; \
1848 } while (0)
1850 #define make_tag_from_new_lb(isfun) pfnote (tok.str, isfun, tok.named, \
1851 newlb.buffer, tok.linelen, tok.lineno, newlinepos)
1852 #define make_tag_from_oth_lb(isfun) pfnote (tok.str, isfun, tok.named, \
1853 othlb.buffer, tok.linelen, tok.lineno, othlinepos)
1855 void
1856 C_entries (c_ext, inf)
1857 int c_ext; /* extension of C */
1858 FILE *inf; /* input file */
1860 register char c; /* latest char read; '\0' for end of line */
1861 register char *lp; /* pointer one beyond the character `c' */
1862 int curndx, newndx; /* indices for current and new lb */
1863 TOKEN tok; /* latest token read */
1864 register int tokoff; /* offset in line of start of current token */
1865 register int toklen; /* length of current token */
1866 int cblev; /* current curly brace level */
1867 int parlev; /* current parenthesis level */
1868 logical incomm, inquote, inchar, quotednl, midtoken;
1869 logical cplpl;
1870 logical token_saved; /* token saved */
1871 TOKEN savetok; /* token saved during preprocessor handling */
1873 curndx = newndx = 0;
1874 lineno = 0;
1875 charno = 0;
1876 lp = curlb.buffer;
1877 *lp = 0;
1879 definedef = dnone; funcdef = fnone; typdef = tnone; structdef = snone;
1880 next_token_is_func = yacc_rules = token_saved = FALSE;
1881 midtoken = inquote = inchar = incomm = quotednl = FALSE;
1882 cblev = 0;
1883 parlev = 0;
1884 cplpl = c_ext & C_PLPL;
1886 while (!feof (inf))
1888 c = *lp++;
1889 if (c == '\\')
1891 /* If we're at the end of the line, the next character is a
1892 '\0'; don't skip it, because it's the thing that tells us
1893 to read the next line. */
1894 if (*lp == '\0')
1896 quotednl = TRUE;
1897 continue;
1899 lp++;
1900 c = ' ';
1902 else if (incomm)
1904 switch (c)
1906 case '*':
1907 if (*lp == '/')
1909 c = *lp++;
1910 incomm = FALSE;
1912 break;
1913 case '\0':
1914 /* Newlines inside comments do not end macro definitions in
1915 traditional cpp. */
1916 CNL_SAVE_DEFINEDEF;
1917 break;
1919 continue;
1921 else if (inquote)
1923 switch (c)
1925 case '"':
1926 inquote = FALSE;
1927 break;
1928 case '\0':
1929 /* Newlines inside strings do not end macro definitions
1930 in traditional cpp, even though compilers don't
1931 usually accept them. */
1932 CNL_SAVE_DEFINEDEF;
1933 break;
1935 continue;
1937 else if (inchar)
1939 switch (c)
1941 case '\0':
1942 /* Hmmm, something went wrong. */
1943 CNL;
1944 /* FALLTHRU */
1945 case '\'':
1946 inchar = FALSE;
1947 break;
1949 continue;
1951 else
1952 switch (c)
1954 case '"':
1955 inquote = TRUE;
1956 if (funcdef != finlist && funcdef != fignore)
1957 funcdef = fnone;
1958 continue;
1959 case '\'':
1960 inchar = TRUE;
1961 if (funcdef != finlist && funcdef != fignore)
1962 funcdef = fnone;
1963 continue;
1964 case '/':
1965 if (*lp == '*')
1967 lp++;
1968 incomm = TRUE;
1969 continue;
1971 else if (cplpl && *lp == '/')
1973 c = 0;
1974 break;
1976 else
1977 break;
1978 case '%':
1979 if ((c_ext & YACC) && *lp == '%')
1981 /* entering or exiting rules section in yacc file */
1982 lp++;
1983 definedef = dnone; funcdef = fnone;
1984 typdef = tnone; structdef = snone;
1985 next_token_is_func = FALSE;
1986 midtoken = inquote = inchar = incomm = quotednl = FALSE;
1987 cblev = 0;
1988 yacc_rules = !yacc_rules;
1989 continue;
1991 else
1992 break;
1993 case '#':
1994 if (definedef == dnone)
1996 char *cp;
1997 logical cpptoken = TRUE;
1999 /* Look back on this line. If all blanks, or nonblanks
2000 followed by an end of comment, this is a preprocessor
2001 token. */
2002 for (cp = newlb.buffer; cp < lp-1; cp++)
2003 if (!iswhite (*cp))
2005 if (*cp == '*' && *(cp+1) == '/')
2007 cp++;
2008 cpptoken = TRUE;
2010 else
2011 cpptoken = FALSE;
2013 if (cpptoken)
2014 definedef = dsharpseen;
2015 } /* if (definedef == dnone) */
2017 continue;
2018 } /* switch (c) */
2021 /* Consider token only if some complicated conditions are satisfied. */
2022 if ((definedef != dnone
2023 || (cblev == 0 && structdef != scolonseen)
2024 || (cblev == 1 && cplpl && structdef == sinbody))
2025 && typdef != tignore
2026 && definedef != dignorerest
2027 && funcdef != finlist)
2029 if (midtoken)
2031 if (endtoken (c))
2033 if (cplpl && c == ':' && *lp == ':' && begtoken(*(lp + 1)))
2036 * This handles :: in the middle, but not at the
2037 * beginning of an identifier.
2039 lp += 2;
2040 toklen += 3;
2042 else
2044 logical is_func = FALSE;
2046 if (yacc_rules
2047 || consider_token (newlb.buffer + tokoff, toklen,
2048 c, c_ext, cblev, &is_func))
2050 if (structdef == sinbody
2051 && definedef == dnone
2052 && is_func)
2053 /* function defined in C++ class body */
2055 char *cp = newlb.buffer + tokoff + toklen;
2056 char c = *cp;
2057 *cp = '\0';
2058 tok.str = concat (structtag, "::",
2059 newlb.buffer + tokoff);
2060 *cp = c;
2061 tok.named = TRUE;
2063 else
2065 tok.str = savenstr (newlb.buffer+tokoff, toklen);
2066 if (structdef == stagseen
2067 || typdef == tend
2068 || (is_func
2069 && definedef == dignorerest)) /* macro */
2070 tok.named = TRUE;
2071 else
2072 tok.named = FALSE;
2074 tok.lineno = lineno;
2075 tok.linelen = tokoff + toklen + 1;
2077 if (definedef == dnone
2078 && (funcdef == ftagseen
2079 || structdef == stagseen
2080 || typdef == tend))
2082 if (current_lb_is_new)
2083 switch_line_buffers ();
2085 else
2086 make_tag_from_new_lb (is_func);
2088 midtoken = FALSE;
2090 } /* if (endtoken (c)) */
2091 else if (intoken (c))
2093 toklen++;
2094 continue;
2096 } /* if (midtoken) */
2097 else if (begtoken (c))
2099 switch (definedef)
2101 case dnone:
2102 switch (funcdef)
2104 case fstartlist:
2105 funcdef = finlist;
2106 continue;
2107 case flistseen:
2108 make_tag_from_oth_lb (TRUE);
2109 funcdef = fignore;
2110 break;
2111 case ftagseen:
2112 funcdef = fnone;
2113 break;
2115 if (structdef == stagseen)
2116 structdef = snone;
2117 break;
2118 case dsharpseen:
2119 /* Take a quick peek ahead for a define directive,
2120 so we can avoid saving the token when not absolutely
2121 necessary. [This is a speed hack.] */
2122 if (c == 'd' && strneq (lp, "efine", 5)
2123 && iswhite (*(lp + 5)))
2125 savetok = tok;
2126 token_saved = TRUE;
2127 definedef = ddefineseen;
2128 lp += 6;
2130 else
2131 definedef = dignorerest;
2132 continue;
2134 if (!yacc_rules || lp == newlb.buffer + 1)
2136 tokoff = lp - 1 - newlb.buffer;
2137 toklen = 1;
2138 midtoken = TRUE;
2140 continue;
2142 } /* if must look at token */
2145 /* Detect end of line, colon, comma, semicolon and various braces
2146 after having handled a token.*/
2147 switch (c)
2149 case ':':
2150 if (definedef != dnone)
2151 break;
2152 if (structdef == stagseen)
2153 structdef = scolonseen;
2154 else
2155 switch (funcdef)
2157 case ftagseen:
2158 if (yacc_rules)
2160 make_tag_from_oth_lb (FALSE);
2161 funcdef = fignore;
2163 break;
2164 case fstartlist:
2165 funcdef = fnone;
2166 break;
2168 break;
2169 case ';':
2170 if (definedef != dnone)
2171 break;
2172 if (cblev == 0)
2173 switch (typdef)
2175 case tend:
2176 make_tag_from_oth_lb (FALSE);
2177 /* FALLTHRU */
2178 default:
2179 typdef = tnone;
2181 if (funcdef != fignore)
2182 funcdef = fnone;
2183 if (structdef == stagseen)
2184 structdef = snone;
2185 break;
2186 case ',':
2187 if (definedef != dnone)
2188 break;
2189 if (funcdef != finlist && funcdef != fignore)
2190 funcdef = fnone;
2191 if (structdef == stagseen)
2192 structdef = snone;
2193 break;
2194 case '[':
2195 if (definedef != dnone)
2196 break;
2197 if (cblev == 0 && typdef == tend)
2199 typdef = tignore;
2200 make_tag_from_oth_lb (FALSE);
2201 break;
2203 if (funcdef != finlist && funcdef != fignore)
2204 funcdef = fnone;
2205 if (structdef == stagseen)
2206 structdef = snone;
2207 break;
2208 case '(':
2209 if (definedef != dnone)
2210 break;
2211 switch (funcdef)
2213 case fnone:
2214 switch (typdef)
2216 case ttypedseen:
2217 case tend:
2218 /* Make sure that the next char is not a '*'.
2219 This handles constructs like:
2220 typedef void OperatorFun (int fun); */
2221 if (*lp != '*')
2223 typdef = tignore;
2224 make_tag_from_oth_lb (FALSE);
2226 break;
2227 } /* switch (typdef) */
2228 break;
2229 case ftagseen:
2230 funcdef = fstartlist;
2231 break;
2232 case flistseen:
2233 funcdef = finlist;
2234 break;
2236 parlev++;
2237 break;
2238 case ')':
2239 if (definedef != dnone)
2240 break;
2241 if (--parlev == 0)
2243 switch (funcdef)
2245 case fstartlist:
2246 case finlist:
2247 funcdef = flistseen;
2248 break;
2250 if (cblev == 0 && typdef == tend)
2252 typdef = tignore;
2253 make_tag_from_oth_lb (FALSE);
2256 else if (parlev < 0) /* can happen due to ill-conceived #if's. */
2257 parlev = 0;
2258 break;
2259 case '{':
2260 if (definedef != dnone)
2261 break;
2262 if (typdef == ttypedseen)
2263 typdef = tinbody;
2264 switch (structdef)
2266 case skeyseen: /* unnamed struct */
2267 structtag = "_anonymous_";
2268 structdef = sinbody;
2269 break;
2270 case stagseen:
2271 case scolonseen: /* named struct */
2272 structdef = sinbody;
2273 make_tag_from_oth_lb (FALSE);
2274 break;
2276 switch (funcdef)
2278 case flistseen:
2279 make_tag_from_oth_lb (TRUE);
2280 /* FALLTHRU */
2281 case fignore:
2282 funcdef = fnone;
2283 break;
2284 case fnone:
2285 /* Neutralize `extern "C" {' grot and look inside structs. */
2286 if (cblev == 0 && structdef == snone && typdef == tnone)
2287 cblev = -1;
2289 cblev++;
2290 break;
2291 case '*':
2292 if (definedef != dnone)
2293 break;
2294 if (funcdef == fstartlist)
2295 funcdef = fnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
2296 break;
2297 case '}':
2298 if (definedef != dnone)
2299 break;
2300 if (!noindentypedefs && lp == newlb.buffer + 1)
2302 cblev = 0; /* reset curly brace level if first column */
2303 parlev = 0; /* also reset paren level, just in case... */
2305 else if (cblev > 0)
2306 cblev--;
2307 if (cblev == 0)
2309 if (typdef == tinbody)
2310 typdef = tend;
2311 #if FALSE /* too risky */
2312 if (structdef == sinbody)
2313 free (structtag);
2314 #endif
2315 structdef = snone;
2316 structtag = "<error>";
2318 break;
2319 case '=':
2320 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
2321 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2322 if (definedef != dnone)
2323 break;
2324 /* These surely cannot follow a function tag. */
2325 if (funcdef != finlist && funcdef != fignore)
2326 funcdef = fnone;
2327 break;
2328 case '\0':
2329 /* If a macro spans multiple lines don't reset its state. */
2330 if (quotednl)
2331 CNL_SAVE_DEFINEDEF;
2332 else
2333 CNL;
2334 break;
2335 } /* switch (c) */
2337 } /* while not eof */
2341 * Process either a C++ file or a C file depending on the setting
2342 * of a global flag.
2344 void
2345 default_C_entries (inf)
2346 FILE *inf;
2348 C_entries (cplusplus ? C_PLPL : 0, inf);
2351 /* Always do C++. */
2352 void
2353 Cplusplus_entries (inf)
2354 FILE *inf;
2356 C_entries (C_PLPL, inf);
2359 /* Always do C*. */
2360 void
2361 Cstar_entries (inf)
2362 FILE *inf;
2364 C_entries (C_STAR, inf);
2367 /* Always do Yacc. */
2368 void
2369 Yacc_entries (inf)
2370 FILE *inf;
2372 C_entries (YACC, inf);
2375 /* Fortran parsing */
2377 char *dbp;
2379 logical
2380 tail (cp)
2381 char *cp;
2383 register int len = 0;
2385 while (*cp && (*cp | ' ') == (dbp[len] | ' '))
2386 cp++, len++;
2387 if (*cp == 0)
2389 dbp += len;
2390 return TRUE;
2392 return FALSE;
2395 void
2396 takeprec ()
2398 while (isspace (*dbp))
2399 dbp++;
2400 if (*dbp != '*')
2401 return;
2402 dbp++;
2403 while (isspace (*dbp))
2404 dbp++;
2405 if (tail ("(*)"))
2406 return;
2407 if (!isdigit (*dbp))
2409 --dbp; /* force failure */
2410 return;
2413 dbp++;
2414 while (isdigit (*dbp));
2417 void
2418 getit (inf)
2419 FILE *inf;
2421 register char *cp;
2423 while (isspace (*dbp))
2424 dbp++;
2425 if (*dbp == '\0')
2427 lineno++;
2428 linecharno = charno;
2429 charno += readline (&lb, inf);
2430 dbp = lb.buffer;
2431 if (dbp[5] != '&')
2432 return;
2433 dbp += 6;
2434 while (isspace (*dbp))
2435 dbp++;
2437 if (!isalpha (*dbp)
2438 && *dbp != '_'
2439 && *dbp != '$')
2440 return;
2441 for (cp = dbp + 1;
2442 (*cp
2443 && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
2444 cp++)
2445 continue;
2446 pfnote (savenstr (dbp, cp-dbp), TRUE, FALSE, lb.buffer,
2447 cp - lb.buffer + 1, lineno, linecharno);
2450 void
2451 Fortran_functions (inf)
2452 FILE *inf;
2454 lineno = 0;
2455 charno = 0;
2457 while (!feof (inf))
2459 lineno++;
2460 linecharno = charno;
2461 charno += readline (&lb, inf);
2462 dbp = lb.buffer;
2463 if (*dbp == '%')
2464 dbp++; /* Ratfor escape to fortran */
2465 while (isspace (*dbp))
2466 dbp++;
2467 if (*dbp == 0)
2468 continue;
2469 switch (*dbp | ' ')
2471 case 'i':
2472 if (tail ("integer"))
2473 takeprec ();
2474 break;
2475 case 'r':
2476 if (tail ("real"))
2477 takeprec ();
2478 break;
2479 case 'l':
2480 if (tail ("logical"))
2481 takeprec ();
2482 break;
2483 case 'c':
2484 if (tail ("complex") || tail ("character"))
2485 takeprec ();
2486 break;
2487 case 'd':
2488 if (tail ("double"))
2490 while (isspace (*dbp))
2491 dbp++;
2492 if (*dbp == 0)
2493 continue;
2494 if (tail ("precision"))
2495 break;
2496 continue;
2498 break;
2500 while (isspace (*dbp))
2501 dbp++;
2502 if (*dbp == 0)
2503 continue;
2504 switch (*dbp | ' ')
2506 case 'f':
2507 if (tail ("function"))
2508 getit (inf);
2509 continue;
2510 case 's':
2511 if (tail ("subroutine"))
2512 getit (inf);
2513 continue;
2514 case 'e':
2515 if (tail ("entry"))
2516 getit (inf);
2517 continue;
2518 case 'p':
2519 if (tail ("program"))
2521 getit (inf);
2522 continue;
2524 if (tail ("procedure"))
2525 getit (inf);
2526 continue;
2532 * Bob Weiner, Motorola Inc., 4/3/94
2533 * Unix and microcontroller assembly tag handling
2534 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2536 void
2537 Asm_labels (inf)
2538 FILE *inf;
2540 register char *cp;
2542 lineno = 0;
2543 charno = 0;
2545 while (!feof (inf))
2547 lineno++;
2548 linecharno = charno;
2549 charno += readline (&lb, inf);
2550 cp = lb.buffer;
2552 /* If first char is alphabetic or one of [_.$], test for colon
2553 following identifier. */
2554 if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2556 /* Read past label. */
2557 cp++;
2558 while (isalnum (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
2559 cp++;
2560 if (*cp == ':' || isspace (*cp))
2562 /* Found end of label, so copy it and add it to the table. */
2563 pfnote (savenstr (lb.buffer, cp-lb.buffer), TRUE, FALSE,
2564 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2570 /* Added by Mosur Mohan, 4/22/88 */
2571 /* Pascal parsing */
2573 #define GET_NEW_LINE \
2575 linecharno = charno; lineno++; \
2576 charno += 1 + readline (&lb, inf); \
2577 dbp = lb.buffer; \
2581 * Locates tags for procedures & functions. Doesn't do any type- or
2582 * var-definitions. It does look for the keyword "extern" or
2583 * "forward" immediately following the procedure statement; if found,
2584 * the tag is skipped.
2586 void
2587 Pascal_functions (inf)
2588 FILE *inf;
2590 struct linebuffer tline; /* mostly copied from C_entries */
2591 long save_lcno;
2592 int save_lineno;
2593 char c, *cp;
2594 char *nambuf;
2596 logical /* each of these flags is TRUE iff: */
2597 incomment, /* point is inside a comment */
2598 inquote, /* point is inside '..' string */
2599 get_tagname, /* point is after PROCEDURE/FUNCTION */
2600 /* keyword, so next item = potential tag */
2601 found_tag, /* point is after a potential tag */
2602 inparms, /* point is within parameter-list */
2603 verify_tag; /* point has passed the parm-list, so the */
2604 /* next token will determine whether */
2605 /* this is a FORWARD/EXTERN to be */
2606 /* ignored, or whether it is a real tag */
2608 lineno = 0;
2609 charno = 0;
2610 dbp = lb.buffer;
2611 *dbp = 0;
2612 initbuffer (&tline);
2614 incomment = inquote = FALSE;
2615 found_tag = FALSE; /* have a proc name; check if extern */
2616 get_tagname = FALSE; /* have found "procedure" keyword */
2617 inparms = FALSE; /* found '(' after "proc" */
2618 verify_tag = FALSE; /* check if "extern" is ahead */
2620 /* long main loop to get next char */
2621 while (!feof (inf))
2623 c = *dbp++;
2624 if (c == '\0') /* if end of line */
2626 GET_NEW_LINE;
2627 if (*dbp == '\0')
2628 continue;
2629 if (!((found_tag && verify_tag) ||
2630 get_tagname))
2631 c = *dbp++; /* only if don't need *dbp pointing */
2632 /* to the beginning of the name of */
2633 /* the procedure or function */
2635 if (incomment)
2637 if (c == '}') /* within { - } comments */
2638 incomment = FALSE;
2639 else if (c == '*' && dbp[1] == ')') /* within (* - *) comments */
2641 dbp++;
2642 incomment = FALSE;
2644 continue;
2646 else if (inquote)
2648 if (c == '\'')
2649 inquote = FALSE;
2650 continue;
2652 else
2653 switch (c)
2655 case '\'':
2656 inquote = TRUE; /* found first quote */
2657 continue;
2658 case '{': /* found open-{-comment */
2659 incomment = TRUE;
2660 continue;
2661 case '(':
2662 if (*dbp == '*') /* found open-(*-comment */
2664 incomment = TRUE;
2665 dbp++;
2667 else if (found_tag) /* found '(' after tag, i.e., parm-list */
2668 inparms = TRUE;
2669 continue;
2670 case ')': /* end of parms list */
2671 if (inparms)
2672 inparms = FALSE;
2673 continue;
2674 case ';':
2675 if ((found_tag) && (!inparms)) /* end of proc or fn stmt */
2677 verify_tag = TRUE;
2678 break;
2680 continue;
2682 if ((found_tag) && (verify_tag) && (*dbp != ' '))
2684 /* check if this is an "extern" declaration */
2685 if (*dbp == 0)
2686 continue;
2687 if ((*dbp == 'e') || (*dbp == 'E'))
2689 if (tail ("extern")) /* superfluous, really! */
2691 found_tag = FALSE;
2692 verify_tag = FALSE;
2695 else if ((*dbp == 'f') || (*dbp == 'F'))
2697 if (tail ("forward")) /* check for forward reference */
2699 found_tag = FALSE;
2700 verify_tag = FALSE;
2703 if ((found_tag) && (verify_tag)) /* not external proc, so make tag */
2705 found_tag = FALSE;
2706 verify_tag = FALSE;
2707 pfnote (nambuf, TRUE, FALSE, tline.buffer,
2708 cp - tline.buffer + 1, save_lineno, save_lcno);
2709 continue;
2712 if (get_tagname) /* grab name of proc or fn */
2714 if (*dbp == 0)
2715 continue;
2717 /* save all values for later tagging */
2718 tline.size = lb.size;
2719 strcpy (tline.buffer, lb.buffer);
2720 save_lineno = lineno;
2721 save_lcno = linecharno;
2723 /* grab block name */
2724 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
2725 continue;
2726 nambuf = savenstr (dbp, cp-dbp);
2727 dbp = cp; /* restore dbp to e-o-token */
2728 get_tagname = FALSE;
2729 found_tag = TRUE;
2730 continue;
2732 /* and proceed to check for "extern" */
2734 else if (!incomment && !inquote && !found_tag)
2736 /* check for proc/fn keywords */
2737 switch (c | ' ')
2739 case 'p':
2740 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2741 get_tagname = TRUE;
2742 continue;
2743 case 'f':
2744 if (tail ("unction"))
2745 get_tagname = TRUE;
2746 continue;
2749 } /* while not eof */
2753 * lisp tag functions
2754 * look for (def or (DEF, quote or QUOTE
2757 L_isdef (strp)
2758 register char *strp;
2760 return ((strp[1] == 'd' || strp[1] == 'D')
2761 && (strp[2] == 'e' || strp[2] == 'E')
2762 && (strp[3] == 'f' || strp[3] == 'F'));
2766 L_isquote (strp)
2767 register char *strp;
2769 return ((*(++strp) == 'q' || *strp == 'Q')
2770 && (*(++strp) == 'u' || *strp == 'U')
2771 && (*(++strp) == 'o' || *strp == 'O')
2772 && (*(++strp) == 't' || *strp == 'T')
2773 && (*(++strp) == 'e' || *strp == 'E')
2774 && isspace(*(++strp)));
2777 void
2778 L_getit ()
2780 register char *cp;
2782 if (*dbp == '\'') /* Skip prefix quote */
2783 dbp++;
2784 else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
2786 dbp += 7;
2787 while (isspace(*dbp))
2788 dbp++;
2790 for (cp = dbp /*+1*/;
2791 *cp && *cp != '(' && *cp != ' ' && *cp != ')';
2792 cp++)
2793 continue;
2794 if (cp == dbp)
2795 return;
2797 pfnote (savenstr (dbp, cp-dbp), TRUE, FALSE, lb.buffer,
2798 cp - lb.buffer + 1, lineno, linecharno);
2801 void
2802 Lisp_functions (inf)
2803 FILE *inf;
2805 lineno = 0;
2806 charno = 0;
2808 while (!feof (inf))
2810 lineno++;
2811 linecharno = charno;
2812 charno += readline (&lb, inf);
2813 dbp = lb.buffer;
2814 if (dbp[0] == '(')
2816 if (L_isdef (dbp))
2818 while (!isspace (*dbp))
2819 dbp++;
2820 while (isspace (*dbp))
2821 dbp++;
2822 L_getit ();
2824 else
2826 /* Check for (foo::defmumble name-defined ... */
2828 dbp++;
2829 while (*dbp && !isspace (*dbp)
2830 && *dbp != ':' && *dbp != '(' && *dbp != ')');
2831 if (*dbp == ':')
2834 dbp++;
2835 while (*dbp == ':');
2837 if (L_isdef (dbp - 1))
2839 while (!isspace (*dbp))
2840 dbp++;
2841 while (isspace (*dbp))
2842 dbp++;
2843 L_getit ();
2852 * Scheme tag functions
2853 * look for (def... xyzzy
2854 * look for (def... (xyzzy
2855 * look for (def ... ((...(xyzzy ....
2856 * look for (set! xyzzy
2859 void get_scheme ();
2861 void
2862 Scheme_functions (inf)
2863 FILE *inf;
2865 lineno = 0;
2866 charno = 0;
2868 while (!feof (inf))
2870 lineno++;
2871 linecharno = charno;
2872 charno += readline (&lb, inf);
2873 dbp = lb.buffer;
2874 if (dbp[0] == '(' &&
2875 (dbp[1] == 'D' || dbp[1] == 'd') &&
2876 (dbp[2] == 'E' || dbp[2] == 'e') &&
2877 (dbp[3] == 'F' || dbp[3] == 'f'))
2879 while (!isspace (*dbp))
2880 dbp++;
2881 /* Skip over open parens and white space */
2882 while (*dbp && (isspace (*dbp) || *dbp == '('))
2883 dbp++;
2884 get_scheme ();
2886 if (dbp[0] == '(' &&
2887 (dbp[1] == 'S' || dbp[1] == 's') &&
2888 (dbp[2] == 'E' || dbp[2] == 'e') &&
2889 (dbp[3] == 'T' || dbp[3] == 't') &&
2890 (dbp[4] == '!' || dbp[4] == '!') &&
2891 (isspace (dbp[5])))
2893 while (!isspace (*dbp))
2894 dbp++;
2895 /* Skip over white space */
2896 while (isspace (*dbp))
2897 dbp++;
2898 get_scheme ();
2903 void
2904 get_scheme ()
2906 register char *cp;
2908 if (*dbp == 0)
2909 return;
2910 /* Go till you get to white space or a syntactic break */
2911 for (cp = dbp + 1;
2912 *cp && *cp != '(' && *cp != ')' && !isspace (*cp);
2913 cp++)
2914 continue;
2915 pfnote (savenstr (dbp, cp-dbp), TRUE, FALSE,
2916 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
2919 /* Find tags in TeX and LaTeX input files. */
2921 /* TEX_toktab is a table of TeX control sequences that define tags.
2922 Each TEX_tabent records one such control sequence.
2923 CONVERT THIS TO USE THE Stab TYPE!! */
2924 struct TEX_tabent
2926 char *name;
2927 int len;
2930 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
2932 /* Default set of control sequences to put into TEX_toktab.
2933 The value of environment var TEXTAGS is prepended to this. */
2935 char *TEX_defenv = "\
2936 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2938 void TEX_mode ();
2939 struct TEX_tabent *TEX_decode_env ();
2940 void TEX_getit ();
2941 int TEX_Token ();
2943 char TEX_esc = '\\';
2944 char TEX_opgrp = '{';
2945 char TEX_clgrp = '}';
2948 * TeX/LaTeX scanning loop.
2950 void
2951 TeX_functions (inf)
2952 FILE *inf;
2954 char *lasthit;
2956 lineno = 0;
2957 charno = 0;
2959 /* Select either \ or ! as escape character. */
2960 TEX_mode (inf);
2962 /* Initialize token table once from environment. */
2963 if (!TEX_toktab)
2964 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
2966 while (!feof (inf))
2967 { /* Scan each line in file */
2968 lineno++;
2969 linecharno = charno;
2970 charno += readline (&lb, inf);
2971 dbp = lb.buffer;
2972 lasthit = dbp;
2973 while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
2975 register int i;
2977 if (!*(++dbp))
2978 break;
2979 linecharno += dbp - lasthit;
2980 lasthit = dbp;
2981 i = TEX_Token (lasthit);
2982 if (0 <= i)
2984 TEX_getit (lasthit, TEX_toktab[i].len);
2985 break; /* We only save a line once */
2991 #define TEX_LESC '\\'
2992 #define TEX_SESC '!'
2993 #define TEX_cmt '%'
2995 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
2996 chars accordingly. */
2997 void
2998 TEX_mode (inf)
2999 FILE *inf;
3001 int c;
3003 while ((c = getc (inf)) != EOF)
3005 /* Skip to next line if we hit the TeX comment char. */
3006 if (c == TEX_cmt)
3007 while (c != '\n')
3008 c = getc (inf);
3009 else if (c == TEX_LESC || c == TEX_SESC )
3010 break;
3013 if (c == TEX_LESC)
3015 TEX_esc = TEX_LESC;
3016 TEX_opgrp = '{';
3017 TEX_clgrp = '}';
3019 else
3021 TEX_esc = TEX_SESC;
3022 TEX_opgrp = '<';
3023 TEX_clgrp = '>';
3025 rewind (inf);
3028 /* Read environment and prepend it to the default string.
3029 Build token table. */
3030 struct TEX_tabent *
3031 TEX_decode_env (evarname, defenv)
3032 char *evarname;
3033 char *defenv;
3035 register char *env, *p;
3037 struct TEX_tabent *tab;
3038 int size, i;
3040 /* Append default string to environment. */
3041 env = getenv (evarname);
3042 if (!env)
3043 env = defenv;
3044 else
3045 env = concat (env, defenv, "");
3047 /* Allocate a token table */
3048 for (size = 1, p = env; p;)
3049 if ((p = etags_strchr (p, ':')) && *(++p))
3050 size++;
3051 /* Add 1 to leave room for null terminator. */
3052 tab = xnew (size + 1, struct TEX_tabent);
3054 /* Unpack environment string into token table. Be careful about */
3055 /* zero-length strings (leading ':', "::" and trailing ':') */
3056 for (i = 0; *env;)
3058 p = etags_strchr (env, ':');
3059 if (!p) /* End of environment string. */
3060 p = env + strlen (env);
3061 if (p - env > 0)
3062 { /* Only non-zero strings. */
3063 tab[i].name = savenstr (env, p - env);
3064 tab[i].len = strlen (tab[i].name);
3065 i++;
3067 if (*p)
3068 env = p + 1;
3069 else
3071 tab[i].name = NULL; /* Mark end of table. */
3072 tab[i].len = 0;
3073 break;
3076 return tab;
3079 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3080 The name being defined actually starts at (NAME + LEN + 1).
3081 But we seem to include the TeX command in the tag name. */
3082 void
3083 TEX_getit (name, len)
3084 char *name;
3085 int len;
3087 char *p = name + len;
3089 if (*name == 0)
3090 return;
3092 /* Let tag name extend to next group close (or end of line) */
3093 while (*p && *p != TEX_clgrp)
3094 p++;
3095 pfnote (savenstr (name, p-name), TRUE, FALSE, lb.buffer,
3096 strlen (lb.buffer), lineno, linecharno);
3099 /* If the text at CP matches one of the tag-defining TeX command names,
3100 return the pointer to the first occurrence of that command in TEX_toktab.
3101 Otherwise return -1.
3102 Keep the capital `T' in `Token' for dumb truncating compilers
3103 (this distinguishes it from `TEX_toktab' */
3105 TEX_Token (cp)
3106 char *cp;
3108 int i;
3110 for (i = 0; TEX_toktab[i].len > 0; i++)
3111 if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
3112 return i;
3113 return -1;
3116 /* Support for Prolog. */
3118 /* Whole head (not only functor, but also arguments)
3119 is gotten in compound term. */
3120 void
3121 prolog_getit (s)
3122 char *s;
3124 char *save_s;
3125 int insquote, npar;
3127 save_s = s;
3128 insquote = FALSE;
3129 npar = 0;
3130 while (1)
3132 if (s[0] == '\0') /* syntax error. */
3133 return;
3134 else if (insquote && s[0] == '\'' && s[1] == '\'')
3135 s += 2;
3136 else if (s[0] == '\'')
3138 insquote = !insquote;
3139 s++;
3141 else if (!insquote && s[0] == '(')
3143 npar++;
3144 s++;
3146 else if (!insquote && s[0] == ')')
3148 npar--;
3149 s++;
3150 if (npar == 0)
3151 break;
3152 else if (npar < 0) /* syntax error. */
3153 return;
3155 else if (!insquote && s[0] == '.'
3156 && (isspace (s[1]) || s[1] == '\0'))
3157 { /* fullstop. */
3158 if (npar != 0) /* syntax error. */
3159 return;
3160 s++;
3161 break;
3163 else
3164 s++;
3166 pfnote (savenstr (save_s, s-save_s), TRUE, FALSE,
3167 save_s, s-save_s, lineno, linecharno);
3170 /* It is assumed that prolog predicate starts from column 0. */
3171 void
3172 Prolog_functions (inf)
3173 FILE *inf;
3175 void skip_comment (), prolog_getit ();
3177 lineno = linecharno = charno = 0;
3178 while (!feof (inf))
3180 lineno++;
3181 linecharno += charno;
3182 charno = readline (&lb, inf) + 1; /* 1 for newline. */
3183 dbp = lb.buffer;
3184 if (isspace (dbp[0])) /* not predicate header. */
3185 continue;
3186 else if (dbp[0] == '%') /* comment. */
3187 continue;
3188 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
3189 skip_comment (&lb, inf, &lineno, &linecharno);
3190 else /* found. */
3191 prolog_getit (dbp);
3195 void
3196 skip_comment (plb, inf, plineno, plinecharno)
3197 struct linebuffer *plb;
3198 FILE *inf;
3199 int *plineno; /* result */
3200 long *plinecharno; /* result */
3202 char *cp;
3206 for (cp = plb->buffer; *cp != '\0'; cp++)
3207 if (cp[0] == '*' && cp[1] == '/')
3208 return;
3209 (*plineno)++;
3210 *plinecharno += readline (plb, inf) + 1; /* 1 for newline. */
3212 while (!feof(inf));
3215 #ifdef ETAGS_REGEXPS
3216 /* Take a string like "/blah/" and turn it into "blah", making sure
3217 that the first and last characters are the same, and handling
3218 quoted separator characters. Actually, stops on the occurence of
3219 an unquoted separator. Also turns "\t" into a Tab character.
3220 Returns pointer to terminating separator. Works in place. Null
3221 terminates name string. */
3222 char *
3223 scan_separators (name)
3224 char *name;
3226 char sep = name[0];
3227 char *copyto = name;
3228 logical quoted = FALSE;
3230 for (++name; *name != '\0'; ++name)
3232 if (quoted)
3234 if (*name == 't')
3235 *copyto++ = '\t';
3236 else if (*name == sep)
3237 *copyto++ = sep;
3238 else
3240 /* Something else is quoted, so preserve the quote. */
3241 *copyto++ = '\\';
3242 *copyto++ = *name;
3244 quoted = FALSE;
3246 else if (*name == '\\')
3247 quoted = TRUE;
3248 else if (*name == sep)
3249 break;
3250 else
3251 *copyto++ = *name;
3254 /* Terminate copied string. */
3255 *copyto = '\0';
3256 return name;
3259 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3260 expression, into a real regular expression by compiling it. */
3261 void
3262 add_regex (regexp_pattern)
3263 char *regexp_pattern;
3265 char *name;
3266 const char *err;
3267 struct re_pattern_buffer *patbuf;
3269 if (regexp_pattern == NULL)
3271 /* Remove existing regexps. */
3272 num_patterns = 0;
3273 patterns = NULL;
3274 return;
3277 if (regexp_pattern[0] == '\0')
3279 error ("missing regexp", 0);
3280 return;
3282 if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
3284 error ("%s: unterminated regexp", regexp_pattern);
3285 return;
3287 name = scan_separators (regexp_pattern);
3288 if (regexp_pattern[0] == '\0')
3290 error ("null regexp", 0);
3291 return;
3293 (void) scan_separators (name);
3295 patbuf = xnew (1, struct re_pattern_buffer);
3296 patbuf->translate = NULL;
3297 patbuf->fastmap = NULL;
3298 patbuf->buffer = NULL;
3299 patbuf->allocated = 0;
3301 err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
3302 if (err != NULL)
3304 error ("%s while compiling pattern", err);
3305 return;
3308 num_patterns += 1;
3309 if (num_patterns == 1)
3310 patterns = xnew (1, struct pattern);
3311 else
3312 patterns = ((struct pattern *)
3313 xrealloc (patterns,
3314 (num_patterns * sizeof (struct pattern))));
3315 patterns[num_patterns - 1].pattern = patbuf;
3316 patterns[num_patterns - 1].name_pattern = savestr (name);
3317 patterns[num_patterns - 1].error_signaled = FALSE;
3321 * Do the subtitutions indicated by the regular expression and
3322 * arguments.
3324 char *
3325 substitute (in, out, regs)
3326 char *in, *out;
3327 struct re_registers *regs;
3329 char *result = NULL, *t;
3330 int size = 0;
3332 /* Pass 1: figure out how much size to allocate. */
3333 for (t = out; *t; ++t)
3335 if (*t == '\\')
3337 ++t;
3338 if (!*t)
3340 fprintf (stderr, "%s: pattern subtitution ends prematurely\n",
3341 progname);
3342 return NULL;
3344 if (isdigit (*t))
3346 int dig = *t - '0';
3347 size += regs->end[dig] - regs->start[dig];
3352 /* Allocate space and do the substitutions. */
3353 result = xnew (size + 1, char);
3354 size = 0;
3355 for (; *out; ++out)
3357 if (*out == '\\')
3359 ++out;
3360 if (isdigit (*out))
3362 /* Using "dig2" satisfies my debugger. Bleah. */
3363 int dig2 = *out - '0';
3364 strncpy (result + size, in + regs->start[dig2],
3365 regs->end[dig2] - regs->start[dig2]);
3366 size += regs->end[dig2] - regs->start[dig2];
3368 else
3370 switch (*out)
3372 case '\t':
3373 result[size++] = '\t';
3374 break;
3375 case '\\':
3376 *out = '\\';
3377 break;
3378 default:
3379 result[size++] = *out;
3380 break;
3384 else
3385 result[size++] = *out;
3387 result[size] = '\0';
3389 return result;
3392 #endif /* ETAGS_REGEXPS */
3393 /* Initialize a linebuffer for use */
3394 void
3395 initbuffer (linebuffer)
3396 struct linebuffer *linebuffer;
3398 linebuffer->size = 200;
3399 linebuffer->buffer = xnew (200, char);
3403 * Read a line of text from `stream' into `linebuffer'.
3404 * Return the number of characters read from `stream',
3405 * which is the length of the line including the newline, if any.
3407 long
3408 readline_internal (linebuffer, stream)
3409 struct linebuffer *linebuffer;
3410 register FILE *stream;
3412 char *buffer = linebuffer->buffer;
3413 register char *p = linebuffer->buffer;
3414 register char *pend;
3415 int chars_deleted;
3417 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
3419 while (1)
3421 register int c = getc (stream);
3422 if (p == pend)
3424 linebuffer->size *= 2;
3425 buffer = (char *) xrealloc (buffer, linebuffer->size);
3426 p += buffer - linebuffer->buffer;
3427 pend = buffer + linebuffer->size;
3428 linebuffer->buffer = buffer;
3430 if (c == EOF)
3432 chars_deleted = 0;
3433 break;
3435 if (c == '\n')
3437 if (p[-1] == '\r' && p > buffer)
3439 *--p = '\0';
3440 chars_deleted = 2;
3442 else
3444 *p = '\0';
3445 chars_deleted = 1;
3447 break;
3449 *p++ = c;
3452 return p - buffer + chars_deleted;
3456 * Like readline_internal, above, but try to match the input
3457 * line against any existing regular expressions.
3459 long
3460 readline (linebuffer, stream)
3461 struct linebuffer *linebuffer;
3462 FILE *stream;
3464 /* Read new line. */
3465 int i;
3466 long result = readline_internal (linebuffer, stream);
3468 #ifdef ETAGS_REGEXPS
3469 /* Match against all listed patterns. */
3470 for (i = 0; i < num_patterns; ++i)
3472 int match = re_match (patterns[i].pattern, linebuffer->buffer,
3473 (int)result, 0, &patterns[i].regs);
3474 switch (match)
3476 case -2:
3477 /* Some error. */
3478 if (!patterns[i].error_signaled)
3480 error ("error while matching pattern %d", i);
3481 patterns[i].error_signaled = TRUE;
3483 break;
3484 case -1:
3485 /* No match. */
3486 break;
3487 default:
3488 /* Match occurred. Construct a tag. */
3489 if (patterns[i].name_pattern[0] != '\0')
3491 /* Make a named tag. */
3492 char *name = substitute (linebuffer->buffer,
3493 patterns[i].name_pattern,
3494 &patterns[i].regs);
3495 if (name != NULL)
3496 pfnote (name, TRUE, TRUE, linebuffer->buffer,
3497 match, lineno, linecharno);
3499 else
3501 /* Make an unnamed tag. */
3502 pfnote (NULL, TRUE, FALSE, linebuffer->buffer,
3503 match, lineno, linecharno);
3505 break;
3508 #endif /* ETAGS_REGEXPS */
3510 return result;
3514 * Read a file, but do no processing. This is used to do regexp
3515 * matching on files that have no language defined.
3517 void
3518 just_read_file (inf)
3519 FILE *inf;
3521 while (!feof (inf))
3523 ++lineno;
3524 linecharno = charno;
3525 charno += readline (&lb, inf) + 1;
3531 * Return a pointer to a space of size strlen(cp)+1 allocated
3532 * with xnew where the string CP has been copied.
3534 char *
3535 savestr (cp)
3536 char *cp;
3538 return savenstr (cp, strlen (cp));
3542 * Return a pointer to a space of size LEN+1 allocated with xnew where
3543 * the string CP has been copied for at most the first LEN characters.
3545 char *
3546 savenstr (cp, len)
3547 char *cp;
3548 int len;
3550 register char *dp;
3552 dp = xnew (len + 1, char);
3553 strncpy (dp, cp, len);
3554 dp[len] = '\0';
3555 return dp;
3559 * Return the ptr in sp at which the character c last
3560 * appears; NULL if not found
3562 * Identical to System V strrchr, included for portability.
3564 char *
3565 etags_strrchr (sp, c)
3566 register char *sp, c;
3568 register char *r;
3570 r = NULL;
3573 if (*sp == c)
3574 r = sp;
3575 } while (*sp++);
3576 return r;
3581 * Return the ptr in sp at which the character c first
3582 * appears; NULL if not found
3584 * Identical to System V strchr, included for portability.
3586 char *
3587 etags_strchr (sp, c)
3588 register char *sp, c;
3592 if (*sp == c)
3593 return sp;
3594 } while (*sp++);
3595 return NULL;
3598 /* Print error message and exit. */
3599 void
3600 fatal (s1, s2)
3601 char *s1, *s2;
3603 error (s1, s2);
3604 exit (BAD);
3607 void
3608 pfatal (s1)
3609 char *s1;
3611 perror (s1);
3612 exit (BAD);
3615 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3616 void
3617 error (s1, s2)
3618 char *s1, *s2;
3620 fprintf (stderr, "%s: ", progname);
3621 fprintf (stderr, s1, s2);
3622 fprintf (stderr, "\n");
3625 /* Return a newly-allocated string whose contents
3626 concatenate those of s1, s2, s3. */
3627 char *
3628 concat (s1, s2, s3)
3629 char *s1, *s2, *s3;
3631 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3632 char *result = xnew (len1 + len2 + len3 + 1, char);
3634 strcpy (result, s1);
3635 strcpy (result + len1, s2);
3636 strcpy (result + len1 + len2, s3);
3637 result[len1 + len2 + len3] = '\0';
3639 return result;
3642 /* Does the same work as the system V getcwd, but does not need to
3643 guess buffer size in advance. */
3644 char *
3645 etags_getcwd ()
3646 #ifdef DOS_NT
3648 char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
3650 getwd (path);
3651 p = path;
3652 while (*p)
3653 if (*p == '\\')
3654 *p++ = '/';
3655 else
3656 *p++ = tolower (*p);
3658 return strdup (path);
3660 #elif HAVE_GETCWD /* not DOS_NT */
3662 int bufsize = 200;
3663 char *path = xnew (bufsize, char);
3665 while (getcwd (path, bufsize) == NULL)
3667 if (errno != ERANGE)
3668 pfatal ("pwd");
3669 bufsize *= 2;
3670 path = xnew (bufsize, char);
3673 return path;
3675 #else /* not DOS_NT and not HAVE_GETCWD */
3677 struct linebuffer path;
3678 FILE *pipe;
3680 initbuffer (&path);
3681 pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
3682 if (pipe == NULL || readline_internal (&path, pipe) == 0)
3683 pfatal ("pwd");
3684 pclose (pipe);
3686 return path.buffer;
3688 #endif /* not DOS_NT and not HAVE_GETCWD */
3690 /* Return a newly allocated string containing the filename
3691 of FILE relative to the absolute directory DIR (which
3692 should end with a slash). */
3693 char *
3694 relative_filename (file, dir)
3695 char *file, *dir;
3697 char *fp, *dp, *res;
3699 /* Find the common root of file and dir. */
3700 fp = absolute_filename (file, cwd);
3701 dp = dir;
3702 while (*fp++ == *dp++)
3703 continue;
3706 fp--;
3707 dp--;
3709 while (*fp != '/');
3711 /* Build a sequence of "../" strings for the resulting relative filename. */
3712 for (dp = etags_strchr (dp + 1, '/'), res = "";
3713 dp != NULL;
3714 dp = etags_strchr (dp + 1, '/'))
3716 res = concat (res, "../", "");
3719 /* Add the filename relative to the common root of file and dir. */
3720 res = concat (res, fp + 1, "");
3722 return res; /* temporary stub */
3725 /* Return a newly allocated string containing the
3726 absolute filename of FILE given CWD (which should
3727 end with a slash). */
3728 char *
3729 absolute_filename (file, cwd)
3730 char *file, *cwd;
3732 char *slashp, *cp, *res;
3734 if (file[0] == '/')
3735 res = concat (file, "", "");
3736 else
3737 res = concat (cwd, file, "");
3739 /* Delete the "/dirname/.." and "/." substrings. */
3740 slashp = etags_strchr (res, '/');
3741 while (slashp != NULL && slashp[0] != '\0')
3743 if (slashp[1] == '.')
3745 if (slashp[2] == '.'
3746 && (slashp[3] == '/' || slashp[3] == '\0'))
3748 cp = slashp;
3750 cp--;
3751 while (cp >= res && *cp != '/');
3752 if (*cp == '/')
3754 strcpy (cp, slashp + 3);
3756 else /* else (cp == res) */
3758 if (slashp[3] != '\0')
3759 strcpy (cp, slashp + 4);
3760 else
3761 return ".";
3763 slashp = cp;
3764 continue;
3766 else if (slashp[2] == '/' || slashp[2] == '\0')
3768 strcpy (slashp, slashp + 2);
3769 continue;
3773 slashp = etags_strchr (slashp + 1, '/');
3776 return res;
3779 /* Return a newly allocated string containing the absolute
3780 filename of dir where FILE resides given CWD (which should
3781 end with a slash). */
3782 char *
3783 absolute_dirname (file, cwd)
3784 char *file, *cwd;
3786 char *slashp, *res;
3787 char save;
3789 slashp = etags_strrchr (file, '/');
3790 if (slashp == NULL)
3791 return cwd;
3792 save = slashp[1];
3793 slashp[1] = '\0';
3794 res = absolute_filename (file, cwd);
3795 slashp[1] = save;
3797 return res;
3800 /* Like malloc but get fatal error if memory is exhausted. */
3801 char *
3802 xmalloc (size)
3803 unsigned int size;
3805 char *result = (char *) malloc (size);
3806 if (result == NULL)
3807 fatal ("virtual memory exhausted", 0);
3808 return result;
3811 char *
3812 xrealloc (ptr, size)
3813 char *ptr;
3814 unsigned int size;
3816 char *result = (char *) realloc (ptr, size);
3817 if (result == NULL)
3818 fatal ("virtual memory exhausted");
3819 return result;