1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 1987, 1988, 1989, 1993 Free Software Foundation, Inc. and Ken Arnold
4 This file is not considered part of GNU Emacs.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 * Ctags originally by Ken Arnold.
23 * FORTRAN added by Jim Kleckner.
24 * Ed Pelegri-Llopart added C typedefs.
25 * Gnu Emacs TAGS format and modifications by RMS?
26 * Sam Kendall added C++.
28 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
31 char pot_etags_version
[] = "@(#) pot revision number is 10.18";
38 #include <../src/config.h>
43 #include <sys/types.h>
46 #if !defined (S_ISREG) && defined (S_IFREG)
47 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
52 extern char *getenv ();
53 extern char *getcwd ();
56 /* Define CTAGS to make the program "ctags" compatible with the usual one.
57 Let it undefined to make the program "etags", which makes emacs-style
58 tag tables and tags typedefs, #defines and struct/union/enum by default. */
66 /* Exit codes for success and failure. */
76 * The FILEPOS abstract type, which represents a position in a file,
77 * plus the following accessor functions:
79 * long GET_CHARNO (pos)
80 * returns absolute char number.
81 * void SET_FILEPOS (pos, fp, charno)
82 * FILE *fp; long charno;
83 * sets `pos' from the current file
84 * position of `fp' and from `charno',
85 * which must be the absolute character
86 * number corresponding to the current
89 * The `pos' parameter is an lvalue expression of type FILEPOS.
90 * Parameters to the accessor functions are evaluated 0 or more times,
91 * and so must have no side effects.
93 * FILEPOS objects can also be assigned and passed to and from
94 * functions in the normal C manner.
96 * Implementation notes: the `+ 0' is to enforce rvalue-ness.
100 /* real implementation */
101 typedef long FILEPOS
;
102 #define GET_CHARNO(pos) ((pos) + 0)
103 #define SET_FILEPOS(pos, fp, cno) ((void) ((pos) = (cno)))
105 /* debugging implementation */
111 #define GET_CHARNO(pos) ((pos).charno + 0)
112 #define SET_FILEPOS(pos, fp, cno) \
113 ((void) ((pos).charno = (cno), \
114 (cno) != ftell (fp) ? (error ("SET_FILEPOS inconsistency"), 0) \
118 #define streq(s, t) (strcmp (s, t) == 0)
119 #define strneq(s, t, n) (strncmp (s, t, n) == 0)
125 #define iswhite(arg) (_wht[arg]) /* T if char is white */
126 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
127 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
128 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
130 #define max(I1,I2) ((I1) > (I2) ? (I1) : (I2))
133 { /* sorting structure */
134 char *name
; /* function or type name */
135 char *file
; /* file name */
136 logical is_func
; /* use pattern or line no */
137 logical named
; /* list name separately */
138 logical been_warned
; /* set if noticed dup */
139 int lno
; /* line number tag is on */
140 long cno
; /* character number line starts on */
141 char *pat
; /* search pattern */
142 struct nd_st
*left
, *right
; /* left and right sons */
145 typedef struct nd_st NODE
;
147 logical header_file
; /* TRUE if .h file, FALSE o.w. */
148 /* boolean "functions" (see init) */
149 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
151 char cwd
[BUFSIZ
]; /* current working directory */
152 char *outfiledir
; /* directory of tagfile */
155 char *savenstr (), *savestr ();
156 char *etags_index (), *etags_rindex ();
157 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
158 char *xmalloc (), *xrealloc ();
159 int L_isdef (), L_isquote ();
161 int total_size_of_entries ();
162 logical
consider_token ();
170 void Scheme_funcs ();
175 logical
find_entries ();
182 void process_file ();
188 * xnew -- allocate storage
191 * Type *xnew (int n, Type);
193 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
196 * Symbol table types.
200 st_none
, st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
217 #define C_PLPL 0x00001 /* C++ */
218 #define C_STAR 0x00003 /* C* */
219 #define YACC 0x10000 /* yacc file */
221 char searchar
= '/'; /* use /.../ searches */
223 LINENO lineno
; /* line number of current line */
224 long charno
; /* current character number */
226 long linecharno
; /* charno of start of line; not used by C, but
227 * by every other language.
230 char *curfile
, /* current input file name */
231 *outfile
, /* output file */
232 *white
= " \f\t\n", /* white chars */
233 *endtk
= " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
234 /* token starting chars */
235 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
236 /* valid in-token chars */
237 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
239 int append_to_tagfile
; /* -a: append to tags */
240 /* The following three default to 1 for etags, but to 0 for ctags. */
241 int typedefs
; /* -t: create tags for typedefs */
242 int typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
243 /* 0 struct/enum/union decls, and C++ */
244 /* member functions. */
245 int constantypedefs
; /* -d: create tags for C #define and enum */
246 /* constants. Enum consts not implemented. */
247 /* -D: opposite of -d. Default under ctags. */
248 int update
; /* -u: update tags */
249 int vgrind_style
; /* -v: create vgrind style index output */
250 int no_warnings
; /* -w: suppress warnings */
251 int cxref_style
; /* -x: create cxref style output */
252 int cplusplus
; /* .[hc] means C++, not C */
253 int noindentypedefs
; /* -S: ignore indentation in C */
255 /* Name this program was invoked with. */
258 struct option longopts
[] = {
259 { "append", no_argument
, NULL
, 'a' },
260 { "backward-search", no_argument
, NULL
, 'B' },
261 { "c++", no_argument
, NULL
, 'C' },
262 { "cxref", no_argument
, NULL
, 'x' },
263 { "defines", no_argument
, NULL
, 'd' },
264 { "forward-search", no_argument
, NULL
, 'F' },
265 { "help", no_argument
, NULL
, 'H' },
266 { "ignore-indentation", no_argument
, NULL
, 'S' },
267 { "include", required_argument
, NULL
, 'i' },
268 { "no-defines", no_argument
, NULL
, 'D' },
269 { "no-warn", no_argument
, NULL
, 'w' },
270 { "output", required_argument
, NULL
, 'o' },
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' },
279 FILE *inf
, /* ioptr for current input file */
280 *outf
; /* ioptr for tags file */
282 NODE
*head
; /* the head of the binary tree of tags */
284 int permit_duplicates
= 1; /* Nonzero means allow duplicate tags. */
286 /* A `struct linebuffer' is a structure which holds a line of text.
287 `readline' reads a line from a stream into a linebuffer
288 and works regardless of the length of the line. */
296 struct linebuffer lb
; /* the current line */
297 struct linebuffer filename_lb
; /* used to read in filenames */
301 struct linebuffer lb
; /* used by C_entries instead of lb */
308 printf ("%s for Emacs version %g.\n", (CTAGS
) ? "CTAGS" : "ETAGS", VERSION
);
310 printf ("%s for Emacs version 19.\n", (CTAGS
) ? "CTAGS" : "ETAGS");
319 printf ("These are the options accepted by %s. You may use unambiguous\n\
320 abbreviations for the long option names. A - as file name means read file\n\
321 names from stdin.\n\n", progname
);
323 puts ("-a, --append\n\
324 Append tag entries to existing tags file.");
327 puts ("-B, --backward-search\n\
328 Write the search commands for the tag entries using '?', the\n\
329 backward-search command.");
332 Treat files with `.c' and `.h' extensions as C++ code, not C\n\
333 code. Files with `.C', `.H', `.cxx', `.hxx', or `.cc'\n\
334 extensions are always assumed to be C++ code.");
337 puts ("-d, --defines\n\
338 Create tag entries for C #defines, too.");
340 puts ("-D, --no-defines\n\
341 Don't create tag entries for C #defines. This makes the tags\n\
345 puts ("-F, --forward-search\n\
346 Write the search commands for the tag entries using '/', the\n\
347 forward-search command.");
350 puts ("-i FILE, --include=FILE\n\
351 Include a note in tag file indicating that, when searching for\n\
352 a tag, one should also consult the tags file FILE after\n\
353 checking the current file.");
355 puts ("-o FILE, --output=FILE\n\
356 Write the tags to FILE.");
357 puts ("-S, --ignore-indentation\n\
358 Don't rely on indentation quite as much as normal. Currently,\n\
359 this means not to assume that a closing brace in the first\n\
360 column is the final brace of a function or structure\n\
361 definition in C and C++.");
365 puts ("-t, --typedefs\n\
366 Generate tag entries for C typedefs.");
367 puts ("-T, --typedefs-and-c++\n\
368 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
369 and C++ member functions.");
374 puts ("-u, --update\n\
375 Update the tag entries for the given files, leaving tag\n\
376 entries for other files in place. Currently, this is\n\
377 implemented by deleting the existing entries for the given\n\
378 files and then rewriting the new entries at the end of the\n\
379 tags file. It is often faster to simply rebuild the entire\n\
380 tag file than to use this.");
381 puts ("-v, --vgrind\n\
382 Generates an index of items intended for human consumption,\n\
383 similar to the output of vgrind. The index is sorted, and\n\
384 gives the page number of each item.");
385 puts ("-x, --cxref\n\
386 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
387 The output uses line numbers instead of page numbers, but\n\
388 beyond that the differences are cosmetic; try both to see\n\
390 puts ("-w, --no-warn\n\
391 Suppress warning messages about entries defined in multiple\n\
395 puts ("-V, --version\n\
396 Print the version of the program.\n\
398 Print this help message.");
411 unsigned int nincluded_files
= 0;
412 char **included_files
= xnew (argc
, char *);
417 extern char *gfnames ();
418 extern char *massage_name ();
422 _fmode
= O_BINARY
; /* all of files are treated as binary files */
428 * If etags, always find typedefs and structure tags. Why not?
429 * Also default is to find macro constants.
432 typedefs
= typedefs_and_cplusplus
= constantypedefs
= 1;
437 opt
= getopt_long (argc
, argv
, "aCdDf:o:StTi:BFuvxwVH", longopts
, 0);
445 /* If getopt returns '\0', then it has already processed a
446 long-named option. We should do nothing. */
449 /* Common options. */
462 case 'f': /* for compatibility with old makefiles */
467 "%s: -%c flag may only be given once\n", progname
, opt
);
486 included_files
[nincluded_files
++] = optarg
;
492 if (!CTAGS
) goto usage
;
496 if (!CTAGS
) goto usage
;
500 if (!CTAGS
) goto usage
;
504 typedefs_and_cplusplus
++;
505 if (!CTAGS
) goto usage
;
509 if (!CTAGS
) goto usage
;
516 if (!CTAGS
) goto usage
;
520 if (!CTAGS
) goto usage
;
528 if (optind
== argc
&& nincluded_files
== 0)
530 fprintf (stderr
, "%s: No input files specified.\n", progname
);
533 fprintf (stderr
, "%s: Try `%s --help' for a complete list of options.\n",
540 outfile
= CTAGS
? "tags" : "TAGS";
542 getcwd (cwd
, BUFSIZ
); /* the current working directory */
544 if (streq (outfile
, "-"))
550 outfiledir
= absolute_dirname (outfile
, cwd
);
553 init (); /* set up boolean "functions" */
556 initbuffer (&lbs
[0].lb
);
557 initbuffer (&lbs
[1].lb
);
558 initbuffer (&filename_lb
);
560 * loop through files finding functions
564 if (streq (outfile
, "-"))
567 outf
= fopen (outfile
, append_to_tagfile
? "a" : "w");
578 while (gfnames (&argc
, &argv
, &got_err
) != NULL
)
582 error ("Can't find file %s\n", this_file
);
587 this_file
= massage_name (this_file
);
590 } /* solely to balance out the ifdef'd parens above */
593 for (; optind
< argc
; optind
++)
595 this_file
= argv
[optind
];
597 /* Input file named "-" means read file names from stdin and use them. */
598 if (streq (this_file
, "-"))
600 while (!feof (stdin
))
602 (void) readline (&filename_lb
, stdin
);
603 if (strlen (filename_lb
.buffer
) > 0)
604 process_file (filename_lb
.buffer
);
608 process_file (this_file
);
613 while (nincluded_files
-- > 0)
614 fprintf (outf
, "\f\n%s,include\n", *included_files
++);
616 (void) fclose (outf
);
627 /* update cannot be set under VMS, so we may assume that argc
628 and argv have not been munged. */
629 for (i
= optind
; i
< argc
; i
++)
632 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
633 outfile
, argv
[i
], outfile
);
638 outf
= fopen (outfile
, append_to_tagfile
? "a" : "w");
645 (void) fclose (outf
);
648 sprintf (cmd
, "sort %s -o %s", outfile
, outfile
);
656 * This routine is called on each file argument.
662 struct stat stat_buf
;
664 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
666 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
669 if (streq (file
, outfile
) && !streq (outfile
, "-"))
671 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
674 if (!find_entries (file
))
684 /* file is an absolute filename. Canonicalise it. */
685 filename
= absolute_filename (file
, cwd
);
689 /* file is a filename relative to cwd. Make it relative
690 to the directory of the tags file. */
691 filename
= relative_filename (file
, outfiledir
);
693 fprintf (outf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
701 * This routine sets up the boolean pseudo-functions which work
702 * by setting boolean flags dependent upon the corresponding character
703 * Every char which is NOT in that string is not a white char. Therefore,
704 * all of the array "_wht" is set to FALSE, and then the elements
705 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
706 * of a char is TRUE if it is the string "white", else FALSE.
714 for (i
= 0; i
< 0177; i
++)
715 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
716 for (sp
= white
; *sp
; sp
++)
718 for (sp
= endtk
; *sp
; sp
++)
720 for (sp
= intk
; *sp
; sp
++)
722 for (sp
= begtk
; *sp
; sp
++)
724 _wht
[0] = _wht
['\n'];
725 _etk
[0] = _etk
['\n'];
726 _btk
[0] = _btk
['\n'];
727 _itk
[0] = _itk
['\n'];
731 * This routine opens the specified file and calls the function
732 * which finds the function and type definitions.
739 void prolog_funcs ();
741 inf
= fopen (file
, "r");
747 curfile
= savestr (file
);
748 cp
= etags_rindex (file
, '.');
750 header_file
= (cp
&& (streq (cp
+ 1, "h")));
752 /* .tex, .aux or .bbl implies LaTeX source code */
753 if (cp
&& (streq (cp
+ 1, "tex") || streq (cp
+ 1, "aux")
754 || streq (cp
+ 1, "bbl")))
757 goto close_and_return
;
759 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
760 if (cp
&& (streq (cp
+ 1, "l")
761 || streq (cp
+ 1, "el")
762 || streq (cp
+ 1, "lsp")
763 || streq (cp
+ 1, "lisp")
764 || streq (cp
+ 1, "cl")
765 || streq (cp
+ 1, "clisp")))
768 goto close_and_return
;
770 /* .scm or .sm or .scheme or ... implies scheme source code */
771 if (cp
&& (streq (cp
+ 1, "sm")
772 || streq (cp
+ 1, "scm")
773 || streq (cp
+ 1, "scheme")
774 || streq (cp
+ 1, "t")
775 || streq (cp
+ 1, "sch")
776 || streq (cp
+ 1, "ss")
777 || streq (cp
+ 1, "SM")
778 || streq (cp
+ 1, "SCM")
779 /* The `SCM' or `scm' prefix with a version number */
780 || (cp
[-1] == 'm' && cp
[-2] == 'c' && cp
[-3] == 's'
781 && string_numeric_p (cp
+ 1))
782 || (cp
[-1] == 'M' && cp
[-2] == 'C' && cp
[-3] == 'S'
783 && string_numeric_p (cp
+ 1))))
786 goto close_and_return
;
788 /* Assume that ".s" or ".a" is assembly code. -wolfgang.
790 if (cp
&& (streq (cp
+ 1, "s")
791 || streq (cp
+ 1, "a")
792 || streq (cp
+ 1, "sa")))
795 goto close_and_return
;
797 /* .C or .H or .cxx or .hxx or .cc: a C++ file */
798 if (cp
&& (streq (cp
+ 1, "C")
799 || streq (cp
+ 1, "H")
800 || streq (cp
+ 1, "cxx")
801 || streq (cp
+ 1, "hxx")
802 || streq (cp
+ 1, "cc")))
804 C_entries (C_PLPL
); /* C++ */
805 goto close_and_return
;
807 /* .cs or .hs: a C* file */
808 if (cp
&& (streq (cp
+ 1, "cs")
809 || streq (cp
+ 1, "hs")))
812 goto close_and_return
;
814 /* .y: a yacc file */
815 if (cp
&& (streq (cp
+ 1, "y")))
818 goto close_and_return
;
820 /* .pl implies prolog source code */
821 if (cp
&& streq (cp
+ 1, "pl"))
824 goto close_and_return
;
826 /* .p or .pas: a Pascal file */
827 if (cp
&& (streq (cp
+ 1, "p")
828 || streq (cp
+ 1, "pas")))
831 goto close_and_return
;
833 /* If .f or .for, assume it is fortran or nothing. */
834 if (cp
&& (streq (cp
+ 1, "f")
835 || streq (cp
+ 1, "for")))
837 (void) PF_funcs (inf
);
838 goto close_and_return
;
840 /* if not a .c or .h or .y file, try fortran */
841 if (cp
&& ((cp
[1] != 'c'
844 || (cp
[1] != 0 && cp
[2] != 0)))
846 if (PF_funcs (inf
) != 0)
847 goto close_and_return
;
848 rewind (inf
); /* no fortran tags found, try C */
850 C_entries (cplusplus
? C_PLPL
: 0);
857 /* Nonzero if string STR is composed of digits. */
860 string_numeric_p (str
)
865 if (*str
< '0' || *str
> '9')
872 /* Should take a TOKEN* instead!! */
874 pfnote (name
, is_func
, named
, linestart
, linelen
, lno
, cno
)
875 char *name
; /* tag name */
876 logical is_func
; /* function or type name? */
877 logical named
; /* tag different from text of definition? */
893 /* It's okay to output early in etags -- it only disrupts the
894 * character count of the tag entries, which is no longer used
897 error ("too many entries to sort", 0);
904 /* If ctags mode, change name "main" to M<thisfilename>. */
905 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
907 fp
= etags_rindex (curfile
, '/');
908 name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
909 fp
= etags_rindex (name
, '.');
910 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
914 np
->name
= savestr (name
);
916 np
->is_func
= is_func
;
919 /* UNCOMMENT THE +1 HERE: */
920 np
->cno
= cno
/* + 1 */ ; /* our char numbers are 0-base; emacs's are 1-base */
921 np
->left
= np
->right
= 0;
924 c
= linestart
[linelen
];
925 linestart
[linelen
] = 0;
927 else if (cxref_style
== 0)
929 sprintf (tem
, strlen (linestart
) < 50 ? "%s$" : "%.50s", linestart
);
932 np
->pat
= savestr (linestart
);
935 linestart
[linelen
] = c
;
938 add_node (np
, &head
);
943 * recurse on left children, iterate on right children.
951 register NODE
*node_right
= node
->right
;
952 free_tree (node
->left
);
955 free ((char *) node
);
962 * Adds a node to the tree of nodes. In etags mode, we don't keep
963 * it sorted; we just keep a linear list. In ctags mode, maintain
964 * an ordered tree, with no attempt at balancing.
966 * add_node is the only function allowed to add nodes, so it can
969 /* Must avoid static vars within functions since some systems
970 #define static as nothing. */
971 static NODE
*last_node
= NULL
;
974 add_node (node
, cur_node_p
)
975 NODE
*node
, **cur_node_p
;
978 register NODE
*cur_node
= *cur_node_p
;
980 if (cur_node
== NULL
)
990 if (last_node
== NULL
)
991 fatal ("internal error in add_node", 0);
992 last_node
->right
= node
;
998 dif
= strcmp (node
->name
, cur_node
->name
);
1001 * If this tag name matches an existing one, then
1002 * do not add the node, but maybe print a warning.
1006 if (node
->file
== cur_node
->file
)
1010 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1011 node
->file
, lineno
, node
->name
);
1012 fprintf (stderr
, "Second entry ignored\n");
1016 if (!cur_node
->been_warned
&& !no_warnings
)
1019 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1020 node
->file
, cur_node
->file
, node
->name
);
1022 cur_node
->been_warned
= TRUE
;
1026 /* Maybe refuse to add duplicate nodes. */
1027 if (!permit_duplicates
)
1029 if (streq (node
->name
, cur_node
->name
)
1030 && streq (node
->file
, cur_node
->file
))
1034 /* Actually add the node */
1035 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1041 register NODE
*node
;
1048 /* Output subentries that precede this one */
1049 put_entries (node
->left
);
1051 /* Output this entry */
1057 fprintf (outf
, "%s\177%s\001%d,%d\n",
1058 node
->pat
, node
->name
,
1059 node
->lno
, node
->cno
);
1063 fprintf (outf
, "%s\177%d,%d\n",
1065 node
->lno
, node
->cno
);
1068 else if (!cxref_style
)
1070 fprintf (outf
, "%s\t%s\t",
1071 node
->name
, node
->file
);
1075 putc (searchar
, outf
);
1078 for (sp
= node
->pat
; *sp
; sp
++)
1080 if (*sp
== '\\' || *sp
== searchar
)
1084 putc (searchar
, outf
);
1087 { /* a typedef; text pattern inadequate */
1088 fprintf (outf
, "%d", node
->lno
);
1092 else if (vgrind_style
)
1093 fprintf (stdout
, "%s %s %d\n",
1094 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1096 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1097 node
->name
, node
->lno
, node
->file
, node
->pat
);
1099 /* Output subentries that follow this one */
1100 put_entries (node
->right
);
1103 /* Length of a number's decimal representation. */
1111 for (; num
; num
/= 10)
1117 * Return total number of characters that put_entries will output for
1118 * the nodes in the subtree of the specified node. Works only if
1119 * we are not ctags, but called only in that case. This count
1120 * is irrelevant with the new tags.el, but is still supplied for
1121 * backward compatibility.
1124 total_size_of_entries (node
)
1125 register NODE
*node
;
1133 for (; node
; node
= node
->right
)
1135 /* Count left subentries. */
1136 total
+= total_size_of_entries (node
->left
);
1138 /* Count this entry */
1139 total
+= strlen (node
->pat
) + 1;
1140 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1142 total
+= 1 + strlen (node
->name
); /* \001name */
1149 * The C symbol tables.
1152 /* Feed stuff between (but not including) %[ and %] lines to:
1153 gperf -c -k1,3 -o -p -r -t
1155 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1157 class, C_PLPL, st_C_struct
1158 domain, C_STAR, st_C_struct
1159 union, 0, st_C_struct
1160 struct, 0, st_C_struct
1162 typedef, 0, st_C_typedef
1163 define, 0, st_C_define
1164 long, 0, st_C_typespec
1165 short, 0, st_C_typespec
1166 int, 0, st_C_typespec
1167 char, 0, st_C_typespec
1168 float, 0, st_C_typespec
1169 double, 0, st_C_typespec
1170 signed, 0, st_C_typespec
1171 unsigned, 0, st_C_typespec
1172 auto, 0, st_C_typespec
1173 void, 0, st_C_typespec
1174 extern, 0, st_C_typespec
1175 static, 0, st_C_typespec
1176 const, 0, st_C_typespec
1177 volatile, 0, st_C_typespec
1179 and replace lines between %< and %> with its output. */
1181 /* C code produced by gperf version 1.8.1 (K&R C version) */
1182 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1185 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1187 #define MIN_WORD_LENGTH 3
1188 #define MAX_WORD_LENGTH 8
1189 #define MIN_HASH_VALUE 10
1190 #define MAX_HASH_VALUE 62
1193 53 is the maximum key range
1201 static unsigned char hash_table
[] =
1203 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1204 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1205 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1206 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1207 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1208 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1209 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1210 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1211 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1212 62, 62, 62, 62, 62, 62, 62, 2, 62, 7,
1213 6, 9, 15, 30, 62, 24, 62, 62, 1, 24,
1214 7, 27, 13, 62, 19, 26, 18, 27, 1, 62,
1215 62, 62, 62, 62, 62, 62, 62, 62,
1217 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1220 struct C_stab_entry
*
1221 in_word_set (str
, len
)
1226 static struct C_stab_entry wordlist
[] =
1228 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1230 {"volatile", 0, st_C_typespec
},
1232 {"long", 0, st_C_typespec
},
1233 {"char", 0, st_C_typespec
},
1234 {"class", C_PLPL
, st_C_struct
},
1235 {"",}, {"",}, {"",}, {"",},
1236 {"const", 0, st_C_typespec
},
1237 {"",}, {"",}, {"",}, {"",},
1238 {"auto", 0, st_C_typespec
},
1240 {"define", 0, st_C_define
},
1242 {"void", 0, st_C_typespec
},
1243 {"",}, {"",}, {"",},
1244 {"extern", 0, st_C_typespec
},
1245 {"static", 0, st_C_typespec
},
1247 {"domain", C_STAR
, st_C_struct
},
1249 {"typedef", 0, st_C_typedef
},
1250 {"double", 0, st_C_typespec
},
1251 {"enum", 0, st_C_enum
},
1252 {"",}, {"",}, {"",}, {"",},
1253 {"int", 0, st_C_typespec
},
1255 {"float", 0, st_C_typespec
},
1256 {"",}, {"",}, {"",},
1257 {"struct", 0, st_C_struct
},
1258 {"",}, {"",}, {"",}, {"",},
1259 {"union", 0, st_C_struct
},
1261 {"short", 0, st_C_typespec
},
1263 {"unsigned", 0, st_C_typespec
},
1264 {"signed", 0, st_C_typespec
},
1267 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1269 register int key
= hash (str
, len
);
1271 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1273 register char *s
= wordlist
[key
].name
;
1275 if (*s
== *str
&& strneq (str
+ 1, s
+ 1, len
- 1))
1276 return &wordlist
[key
];
1284 C_symtype(str
, len
, c_ext
)
1289 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1291 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1297 * C functions are recognized using a simple finite automaton.
1298 * funcdef is its state variable.
1302 fnone
, /* nothing seen */
1303 ftagseen
, /* function-like tag seen */
1304 fstartlist
, /* just after open parenthesis */
1305 finlist
, /* in parameter list */
1306 flistseen
, /* after parameter list */
1307 fignore
/* before open brace */
1313 * typedefs are recognized using a simple finite automaton.
1314 * typeddef is its state variable.
1318 tnone
, /* nothing seen */
1319 ttypedseen
, /* typedef keyword seen */
1320 tinbody
, /* inside typedef body */
1321 tend
, /* just before typedef tag */
1322 tignore
/* junk after typedef tag */
1328 * struct-like structures (enum, struct and union) are recognized
1329 * using another simple finite automaton. `structdef' is its state
1334 snone
, /* nothing seen yet */
1335 skeyseen
, /* struct-like keyword seen */
1336 stagseen
, /* struct-like tag seen */
1337 scolonseen
, /* colon seen after struct-like tag */
1338 sinbody
/* in struct body: recognize member func defs*/
1343 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1344 * struct tag, and structtype is the type of the preceding struct-like
1347 char structtag
[BUFSIZ
];
1348 enum sym_type structtype
;
1351 * Yet another little state machine to deal with preprocessor lines.
1355 dnone
, /* nothing seen */
1356 dsharpseen
, /* '#' seen as first char on line */
1357 ddefineseen
, /* '#' and 'define' seen */
1358 dignorerest
/* ignore rest of line */
1363 * Set this to TRUE, and the next token considered is called a function.
1364 * Used only for GNUmacs's function-defining macros.
1366 logical next_token_is_func
;
1369 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1375 * This routine finds functions, typedefs, #define's and
1376 * struct/union/enum definitions in C syntax and adds them
1380 #define curlb (lbs[curndx].lb)
1381 #define othlb (lbs[1-curndx].lb)
1382 #define newlb (lbs[newndx].lb)
1383 #define curlinepos (lbs[curndx].linepos)
1384 #define othlinepos (lbs[1-curndx].linepos)
1385 #define newlinepos (lbs[newndx].linepos)
1387 /* Save and restore token state. This is used when preprocessor defines
1388 are handled, to avoid disturbing active function/typedef/struct states. */
1389 #define TOKEN_SAVED_P (savetok.lineno > 0)
1390 #define SAVE_TOKEN (savetok = tok, savetok.p = (char *) tokoff, \
1391 savetok.len = toklen, strcpy(savenameb, nameb))
1392 #define RESTORE_TOKEN (tok = savetok, tokoff = (int) tok.p, \
1393 toklen = tok.len, strcpy(nameb, savenameb), \
1396 #define CNL_SAVE_DEFINEDEF \
1398 SET_FILEPOS (curlinepos, inf, charno); \
1400 charno += readline (&curlb, inf); \
1401 lp = curlb.buffer; \
1408 CNL_SAVE_DEFINEDEF; \
1409 if (TOKEN_SAVED_P) \
1411 definedef = dnone; \
1414 #define MAKE_TAG_FROM_NEW_LB(isfun) pfnote (nameb, isfun, tok.named, \
1415 newlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (newlinepos))
1416 #define MAKE_TAG_FROM_OTH_LB(isfun) pfnote (nameb, isfun, tok.named, \
1417 othlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (othlinepos))
1421 int c_ext
; /* extension of C? */
1423 register char c
; /* latest char read; '\0' for end of line */
1424 register char *lp
; /* pointer one beyond the character `c' */
1425 int curndx
, newndx
; /* indices for current and new lb */
1426 TOKEN tok
; /* latest token read for funcdef & structdef */
1427 char nameb
[BUFSIZ
]; /* latest token name for funcdef & structdef */
1428 register int tokoff
; /* offset in line of start of latest token */
1429 register int toklen
; /* length of latest token */
1430 int cblev
; /* current curly brace level */
1431 int parlev
; /* current parenthesis level */
1432 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
1434 TOKEN savetok
; /* saved token during preprocessor handling */
1435 char savenameb
[BUFSIZ
]; /* ouch! */
1438 curndx
= newndx
= 0;
1444 definedef
= dnone
; funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
1445 next_token_is_func
= yacc_rules
= FALSE
;
1446 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1449 cplpl
= c_ext
& C_PLPL
;
1456 /* If we're at the end of the line, the next character is a
1457 '\0'; don't skip it, because it's the thing that tells us
1458 to read the next line. */
1479 /* Newlines inside comments do not end macro definitions in
1494 /* Newlines inside strings do not end macro definitions
1495 in traditional cpp, even though compilers don't
1496 usually accept them. */
1507 /* Hmmm, something went wrong. */
1521 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1526 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1536 else if (cplpl
&& *lp
== '/')
1544 if ((c_ext
& YACC
) && *lp
== '%')
1546 /* entering or exiting rules section in yacc file */
1548 definedef
= dnone
; funcdef
= fnone
;
1549 typdef
= tnone
; structdef
= snone
;
1550 next_token_is_func
= FALSE
;
1551 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1553 yacc_rules
= !yacc_rules
;
1559 if (lp
== newlb
.buffer
+ 1 && definedef
== dnone
)
1560 definedef
= dsharpseen
;
1565 /* Consider token only if some complicated conditions are satisfied. */
1566 if (((cblev
== 0 && structdef
!= scolonseen
)
1567 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
1568 && typdef
!= tignore
1569 && definedef
!= dignorerest
1570 && (funcdef
!= finlist
1571 || definedef
!= dnone
))
1577 if (cplpl
&& c
== ':' && *lp
== ':' && begtoken(*(lp
+ 1)))
1580 * This handles :: in the middle, but not at beginning
1588 logical is_func
= FALSE
;
1590 tok
.lineno
= lineno
;
1591 tok
.p
= newlb
.buffer
+ tokoff
;
1595 || consider_token (c
, &tok
, c_ext
, cblev
, &is_func
))
1597 if (structdef
== sinbody
1598 && definedef
== dnone
1600 /* function defined in C++ class body */
1603 sprintf (nameb
, "%s::%.*s",
1604 ((structtag
[0] == '\0')
1605 ? "_anonymous_" : structtag
),
1610 sprintf (nameb
, "%.*s", tok
.len
, tok
.p
);
1613 if (structdef
== stagseen
1617 if (definedef
== dnone
1618 && (funcdef
== ftagseen
1619 || structdef
== stagseen
1622 if (newndx
== curndx
)
1623 curndx
= 1 - curndx
; /* switch line buffers */
1626 MAKE_TAG_FROM_NEW_LB (is_func
);
1630 } /* if (endtoken (c)) */
1631 else if (intoken (c
))
1636 } /* if (midtoken) */
1637 else if (begtoken (c
))
1648 MAKE_TAG_FROM_OTH_LB (TRUE
);
1655 if (structdef
== stagseen
)
1659 /* Take a quick peek ahead for define directive,
1660 so we can avoid saving the token when not absolutely
1661 necessary. [This is a speed hack.] */
1662 if (c
== 'd' && strneq(lp
, "efine", 5)
1663 && iswhite(*(lp
+ 5)))
1666 definedef
= ddefineseen
;
1670 definedef
= dignorerest
;
1673 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
1675 tokoff
= lp
- 1 - newlb
.buffer
;
1681 } /* if must look at token */
1684 /* Detect end of line, colon, comma, semicolon and various braces
1685 after having handled a token.*/
1689 if (definedef
!= dnone
)
1691 if (structdef
== stagseen
)
1692 structdef
= scolonseen
;
1699 MAKE_TAG_FROM_OTH_LB (FALSE
);
1709 if (definedef
!= dnone
)
1715 MAKE_TAG_FROM_OTH_LB (FALSE
);
1720 if (funcdef
!= fignore
)
1722 if (structdef
== stagseen
)
1726 if (definedef
!= dnone
)
1728 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1730 if (structdef
== stagseen
)
1734 if (definedef
!= dnone
)
1736 if (cblev
== 0 && typdef
== tend
)
1739 MAKE_TAG_FROM_OTH_LB (FALSE
);
1742 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1744 if (structdef
== stagseen
)
1748 if (definedef
!= dnone
)
1753 funcdef
= fstartlist
;
1762 if (definedef
!= dnone
)
1770 funcdef
= flistseen
;
1773 if (cblev
== 0 && typdef
== tend
)
1776 MAKE_TAG_FROM_OTH_LB (FALSE
);
1779 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
1783 if (definedef
!= dnone
)
1785 if (typdef
== ttypedseen
)
1789 case skeyseen
: /* unnamed struct */
1790 structtag
[0] = '\0';
1791 structdef
= sinbody
;
1794 case scolonseen
: /* named struct */
1795 structdef
= sinbody
;
1796 MAKE_TAG_FROM_OTH_LB (FALSE
);
1802 MAKE_TAG_FROM_OTH_LB (TRUE
);
1808 /* Neutralize `extern "C" {' grot.
1809 if (cblev == 0 && structdef == snone && typdef == tnone)
1815 if (definedef
!= dnone
)
1817 if (funcdef
== fstartlist
)
1818 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
1821 if (definedef
!= dnone
)
1823 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
1825 cblev
= 0; /* reset curly brace level if first column */
1826 parlev
= 0; /* also reset paren level, just in case... */
1832 if (typdef
== tinbody
)
1835 strcpy (structtag
, "<error 2>");
1839 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
1840 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
1841 if (definedef
!= dnone
)
1843 /* These surely cannot follow a function tag. */
1844 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1848 /* If a macro spans multiple lines don't reset its state. */
1856 } /* while not eof */
1861 * checks to see if the current token is at the start of a
1862 * function, or corresponds to a typedef, or is a struct/union/enum
1865 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1866 * C_EXT is which language we are looking at.
1868 * In the future we will need some way to adjust where the end of
1869 * the token is; for instance, implementing the C++ keyword
1870 * `operator' properly will adjust the end of the token to be after
1871 * whatever follows `operator'.
1878 * next_token_is_func IN OUT
1882 consider_token (c
, tokp
, c_ext
, cblev
, is_func
)
1883 register char c
; /* IN: first char after the token */
1884 register TOKEN
*tokp
; /* IN: token pointer */
1885 int c_ext
; /* IN: C extensions mask */
1886 int cblev
; /* IN: curly brace level */
1887 logical
*is_func
; /* OUT */
1889 enum sym_type toktype
= C_symtype(tokp
->p
, tokp
->len
, c_ext
);
1892 * Advance the definedef state machine.
1897 /* We're not on a preprocessor line. */
1900 if (toktype
== st_C_define
)
1902 definedef
= ddefineseen
;
1906 definedef
= dignorerest
;
1911 * Make a tag for any macro.
1913 definedef
= dignorerest
;
1914 *is_func
= (c
== '(');
1915 if (!*is_func
&& !constantypedefs
)
1922 error ("internal error: definedef value.", 0);
1931 if (toktype
== st_C_typedef
)
1934 typdef
= ttypedseen
;
1950 /* Do not return here, so the structdef stuff has a chance. */
1964 * This structdef business is currently only invoked when cblev==0.
1965 * It should be recursively invoked whatever the curly brace level,
1966 * and a stack of states kept, to allow for definitions of structs
1969 * This structdef business is NOT invoked when we are ctags and the
1970 * file is plain C. This is because a struct tag may have the same
1971 * name as another tag, and this loses with ctags.
1973 * This if statement deals with the typdef state machine as
1974 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1975 * return (FALSE). All the other code here is for the structdef
1982 if (typdef
== ttypedseen
1983 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1985 structdef
= skeyseen
;
1986 structtype
= toktype
;
1990 if (structdef
== skeyseen
)
1992 if (structtype
== st_C_struct
)
1994 strncpy (structtag
, tokp
->p
, tokp
->len
);
1995 structtag
[tokp
->len
] = '\0'; /* for struct/union/class */
1999 structtag
[0] = '\0'; /* for enum (why is it treated differently?) */
2001 structdef
= stagseen
;
2005 /* Avoid entering funcdef stuff if typdef is going on. */
2006 if (typdef
!= tnone
)
2012 /* Detect GNUmacs's function-defining macros. */
2013 if (definedef
== dnone
)
2015 if (strneq (tokp
->p
, "DEF", 3)
2016 || strneq (tokp
->p
, "ENTRY", 5)
2017 || strneq (tokp
->p
, "SYSCALL", 7)
2018 || strneq (tokp
->p
, "PSEUDO", 6))
2020 next_token_is_func
= TRUE
;
2023 if (strneq (tokp
->p
, "EXFUN", 5))
2025 next_token_is_func
= FALSE
;
2029 if (next_token_is_func
)
2031 next_token_is_func
= FALSE
;
2033 *is_func
= TRUE
; /* to force search string in ctags */
2041 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2042 funcdef
= fnone
; /* should be useless */
2045 if (funcdef
== fnone
)
2056 /* Fortran parsing */
2072 linecharno
= charno
;
2073 charno
+= readline (&lb
, fi
);
2076 dbp
++; /* Ratfor escape to fortran */
2077 while (isspace (*dbp
))
2084 if (tail ("integer"))
2092 if (tail ("logical"))
2096 if (tail ("complex") || tail ("character"))
2100 if (tail ("double"))
2102 while (isspace (*dbp
))
2106 if (tail ("precision"))
2112 while (isspace (*dbp
))
2119 if (tail ("function"))
2123 if (tail ("subroutine"))
2131 if (tail ("program"))
2136 if (tail ("procedure"))
2148 register int len
= 0;
2150 while (*cp
&& (*cp
| ' ') == (dbp
[len
] | ' '))
2163 while (isspace (*dbp
))
2168 while (isspace (*dbp
))
2170 if (!isdigit (*dbp
))
2172 --dbp
; /* force failure */
2177 while (isdigit (*dbp
));
2186 char nambuf
[BUFSIZ
];
2188 while (isspace (*dbp
))
2193 linecharno
= charno
;
2194 charno
+= readline (&lb
, fi
);
2199 while (isspace (*dbp
))
2208 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2213 strcpy (nambuf
, dbp
);
2215 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
,
2216 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2220 /* Handle a file of assembler code. */
2236 linecharno
= charno
;
2237 charno
+= readline (&lb
, fi
);
2240 for (i
= 0; ((c
= dbp
[i
]) && !isspace (c
)) && (c
!= ':'); i
++)
2243 if ((i
> 0) && (c
== ':'))
2248 /* Added by Mosur Mohan, 4/22/88 */
2249 /* Pascal parsing */
2251 #define GET_NEW_LINE \
2253 linecharno = charno; lineno++; \
2254 charno += 1 + readline (&lb, inf); \
2258 /* Locates tags for procedures & functions.
2259 * Doesn't do any type- or var-definitions.
2260 * It does look for the keyword "extern" or "forward"
2261 * immediately following the procedure statement;
2262 * if found, the tag is skipped.
2269 struct linebuffer tline
; /* mostly copied from C_entries */
2273 char nambuf
[BUFSIZ
];
2275 logical
/* each of these flags is TRUE iff: */
2276 incomm1
, /* point is inside {..} comment */
2277 incomm2
, /* point is inside (*..*) comment */
2278 inquote
, /* point is inside '..' string */
2279 get_tagname
, /* point is after PROCEDURE/FUNCTION */
2280 /* keyword, so next item = potential tag */
2281 found_tag
, /* point is after a potential tag */
2282 inparms
, /* point is within parameter-list */
2283 verify_tag
; /* point has passed the parm-list, so the */
2284 /* next token will determine whether */
2285 /* this is a FORWARD/EXTERN to be */
2286 /* ignored, or whether it is a real tag */
2292 initbuffer (&tline
);
2294 incomm1
= incomm2
= inquote
= FALSE
;
2295 found_tag
= FALSE
; /* have a proc name; check if extern */
2296 get_tagname
= FALSE
; /* have found "procedure" keyword */
2297 inparms
= FALSE
; /* found '(' after "proc" */
2298 verify_tag
= FALSE
; /* check if "extern" is ahead */
2300 /* long main loop to get next char */
2304 if (c
== 0) /* if end of line */
2309 if (!((found_tag
&& verify_tag
) ||
2311 c
= *dbp
++; /* only if don't need *dbp pointing */
2312 /* to the beginning of the name of */
2313 /* the procedure or function */
2315 if (incomm1
) /* within { - } comments */
2321 else if (incomm2
) /* within (* - *) comments */
2325 while ((c
= *dbp
++) == '*')
2344 inquote
= TRUE
; /* found first quote */
2346 case '{': /* found open-{-comment */
2350 if (*dbp
== '*') /* found open-(*-comment */
2355 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2358 case ')': /* end of parms list */
2363 if ((found_tag
) && (!inparms
)) /* end of proc or fn stmt */
2370 if ((found_tag
) && (verify_tag
) && (*dbp
!= ' '))
2372 /* check if this is an "extern" declaration */
2375 if ((*dbp
== 'e') || (*dbp
== 'E'))
2377 if (tail ("extern")) /* superfluous, really! */
2383 else if ((*dbp
== 'f') || (*dbp
== 'F'))
2385 if (tail ("forward")) /* check for forward reference */
2391 if ((found_tag
) && (verify_tag
)) /* not external proc, so make tag */
2395 pfnote (nambuf
, TRUE
, FALSE
,
2396 tline
.buffer
, cp
- tline
.buffer
+ 1,
2397 save_lineno
, save_lcno
);
2401 if (get_tagname
) /* grab name of proc or fn */
2406 /* save all values for later tagging */
2407 tline
.size
= lb
.size
;
2408 strcpy (tline
.buffer
, lb
.buffer
);
2409 save_lineno
= lineno
;
2410 save_lcno
= linecharno
;
2412 /* grab block name */
2413 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
2417 strcpy (nambuf
, dbp
);
2419 dbp
= cp
; /* restore dbp to e-o-token */
2420 get_tagname
= FALSE
;
2424 /* and proceed to check for "extern" */
2426 if ((!incomm1
) && (!incomm2
) && (!inquote
) &&
2427 (!found_tag
) && (!get_tagname
))
2429 /* check for proc/fn keywords */
2433 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2437 if (tail ("unction"))
2442 } /* while not e-o-f */
2446 * lisp tag functions
2447 * just look for (def or (DEF
2461 linecharno
= charno
;
2462 charno
+= readline (&lb
, fi
);
2468 while (!isspace (*dbp
))
2470 while (isspace (*dbp
))
2476 /* Check for (foo::defmumble name-defined ... */
2479 while (*dbp
&& !isspace (*dbp
)
2480 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
2485 while (*dbp
== ':');
2487 if (L_isdef (dbp
- 1))
2489 while (!isspace (*dbp
))
2491 while (isspace (*dbp
))
2505 return ((dbp
[1] == 'd' || dbp
[1] == 'D')
2506 && (dbp
[2] == 'e' || dbp
[2] == 'E')
2507 && (dbp
[3] == 'f' || dbp
[3] == 'F'));
2514 return ((*(++dbp
) == 'q' || *dbp
== 'Q')
2515 && (*(++dbp
) == 'u' || *dbp
== 'U')
2516 && (*(++dbp
) == 'o' || *dbp
== 'O')
2517 && (*(++dbp
) == 't' || *dbp
== 'T')
2518 && (*(++dbp
) == 'e' || *dbp
== 'E')
2519 && isspace(*(++dbp
)));
2527 char nambuf
[BUFSIZ
];
2529 if (*dbp
== '\'') /* Skip prefix quote */
2531 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
2534 while (isspace(*dbp
))
2537 for (cp
= dbp
/*+1*/; *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')'; cp
++)
2544 strcpy (nambuf
, dbp
);
2546 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
,
2547 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2552 * Scheme tag functions
2553 * look for (def... xyzzy
2554 * look for (def... (xyzzy
2555 * look for (def ... ((...(xyzzy ....
2556 * look for (set! xyzzy
2559 static void get_scheme ();
2572 linecharno
= charno
;
2573 charno
+= readline (&lb
, fi
);
2575 if (dbp
[0] == '(' &&
2576 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
2577 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2578 (dbp
[3] == 'F' || dbp
[3] == 'f'))
2580 while (!isspace (*dbp
))
2582 /* Skip over open parens and white space */
2583 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
2587 if (dbp
[0] == '(' &&
2588 (dbp
[1] == 'S' || dbp
[1] == 's') &&
2589 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2590 (dbp
[3] == 'T' || dbp
[3] == 't') &&
2591 (dbp
[4] == '!' || dbp
[4] == '!') &&
2594 while (!isspace (*dbp
))
2596 /* Skip over white space */
2597 while (isspace (*dbp
))
2609 char nambuf
[BUFSIZ
];
2613 /* Go till you get to white space or a syntactic break */
2614 for (cp
= dbp
+ 1; *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
); cp
++)
2616 /* Null terminate the string there. */
2619 /* Copy the string */
2620 strcpy (nambuf
, dbp
);
2621 /* Unterminate the string */
2623 /* Announce the change */
2624 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2628 /* Find tags in TeX and LaTeX input files. */
2630 /* TEX_toktab is a table of TeX control sequences that define tags.
2631 Each TEX_tabent records one such control sequence.
2632 CONVERT THIS TO USE THE Stab TYPE!! */
2640 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
2642 /* Default set of control sequences to put into TEX_toktab.
2643 The value of environment var TEXTAGS is prepended to this. */
2645 static char *TEX_defenv
=
2646 ":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2649 struct TEX_tabent
*TEX_decode_env ();
2653 static char TEX_esc
= '\\';
2654 static char TEX_opgrp
= '{';
2655 static char TEX_clgrp
= '}';
2658 * TeX/LaTeX scanning loop.
2671 /* Select either \ or ! as escape character. */
2674 /* Initialize token table once from environment. */
2676 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
2679 { /* Scan each line in file */
2681 linecharno
= charno
;
2682 charno
+= readline (&lb
, fi
);
2685 while (dbp
= etags_index (dbp
, TEX_esc
)) /* Look at each escape in line */
2691 linecharno
+= dbp
- lasthit
;
2693 i
= TEX_Token (lasthit
);
2696 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
2697 break; /* We only save a line once */
2703 #define TEX_LESC '\\'
2704 #define TEX_SESC '!'
2707 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
2708 /* chars accordingly. */
2716 while ((c
= getc (f
)) != EOF
)
2718 /* Skip to next line if we hit the TeX comment char. */
2722 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
2741 /* Read environment and prepend it to the default string. */
2742 /* Build token table. */
2745 TEX_decode_env (evarname
, defenv
)
2749 register char *env
, *p
;
2751 struct TEX_tabent
*tab
;
2754 /* Append default string to environment. */
2755 env
= getenv (evarname
);
2759 env
= concat (env
, defenv
, "");
2761 /* Allocate a token table */
2762 for (size
= 1, p
= env
; p
;)
2763 if ((p
= etags_index (p
, ':')) && *(++p
))
2765 /* Add 1 to leave room for null terminator. */
2766 tab
= xnew (size
+ 1, struct TEX_tabent
);
2768 /* Unpack environment string into token table. Be careful about */
2769 /* zero-length strings (leading ':', "::" and trailing ':') */
2772 p
= etags_index (env
, ':');
2773 if (!p
) /* End of environment string. */
2774 p
= env
+ strlen (env
);
2776 { /* Only non-zero strings. */
2777 tab
[i
].name
= savenstr (env
, p
- env
);
2778 tab
[i
].len
= strlen (tab
[i
].name
);
2785 tab
[i
].name
= NULL
; /* Mark end of table. */
2793 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
2794 The name being defined actually starts at (NAME + LEN + 1).
2795 But we seem to include the TeX command in the tag name. */
2798 TEX_getit (name
, len
)
2802 char *p
= name
+ len
;
2803 char nambuf
[BUFSIZ
];
2808 /* Let tag name extend to next group close (or end of line) */
2809 while (*p
&& *p
!= TEX_clgrp
)
2811 strncpy (nambuf
, name
, p
- name
);
2812 nambuf
[p
- name
] = 0;
2814 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
2818 /* If the text at CP matches one of the tag-defining TeX command names,
2819 return the etags_index of that command in TEX_toktab.
2820 Otherwise return -1. */
2822 /* Keep the capital `T' in `Token' for dumb truncating compilers
2823 (this distinguishes it from `TEX_toktab' */
2830 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
2831 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
2836 /* Support for Prolog. */
2838 /* whole head (not only functor, but also arguments)
2839 is gotten in compound term. */
2842 prolog_getit (s
, lineno
, linecharno
)
2847 char nambuf
[BUFSIZ
], *save_s
, tmpc
;
2855 if (*s
== '\0') /* syntax error. */
2857 else if (insquote
&& *s
== '\'' && *(s
+ 1) == '\'')
2859 else if (*s
== '\'')
2861 insquote
= !insquote
;
2864 else if (!insquote
&& *s
== '(')
2869 else if (!insquote
&& *s
== ')')
2875 else if (npar
< 0) /* syntax error. */
2878 else if (!insquote
&& *s
== '.' && (isspace (*(s
+ 1)) || *(s
+ 1) == '\0'))
2880 if (npar
!= 0) /* syntax error. */
2890 strcpy (nambuf
, save_s
);
2892 pfnote (nambuf
, TRUE
, FALSE
, save_s
, strlen (nambuf
), lineno
, linecharno
);
2895 /* It is assumed that prolog predicate starts from column 0. */
2901 void skip_comment (), prolog_getit ();
2903 lineno
= linecharno
= charno
= 0;
2907 linecharno
+= charno
;
2908 charno
= readline (&lb
, fi
) + 1; /* 1 for newline. */
2910 if (isspace (dbp
[0])) /* not predicate header. */
2912 else if (dbp
[0] == '%') /* comment. */
2914 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
2915 skip_comment (&lb
, fi
, &lineno
, &linecharno
);
2917 prolog_getit (dbp
, lineno
, linecharno
);
2922 skip_comment (plb
, fi
, plineno
, plinecharno
)
2923 struct linebuffer
*plb
;
2925 int *plineno
; /* result */
2926 long *plinecharno
; /* result */
2928 while (!substr ("*/", plb
->buffer
))
2931 *plinecharno
+= readline (plb
, fi
) + 1;
2932 } /* 1 for newline. */
2935 /* Return TRUE if 'sub' exists somewhere in 's'. */
2942 while (*s
&& (s
= etags_index (s
, *sub
)))
2943 if (prestr (sub
, s
))
2950 /* Return TRUE if 'pre' is prefix of string 's'. */
2959 else if (*pre
== *s
)
2960 return (prestr (pre
+ 1, s
+ 1));
2965 /* Initialize a linebuffer for use */
2968 initbuffer (linebuffer
)
2969 struct linebuffer
*linebuffer
;
2971 linebuffer
->size
= 200;
2972 linebuffer
->buffer
= xnew (200, char);
2976 * Read a line of text from `stream' into `linebuffer'.
2977 * Return the number of characters read from `stream',
2978 * which is the length of the line including the newline, if any.
2981 readline (linebuffer
, stream
)
2982 struct linebuffer
*linebuffer
;
2983 register FILE *stream
;
2985 char *buffer
= linebuffer
->buffer
;
2986 register char *p
= linebuffer
->buffer
;
2987 register char *pend
;
2988 int newline
; /* 1 if ended with newline, 0 if ended with EOF */
2990 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
2994 register int c
= getc (stream
);
2997 linebuffer
->size
*= 2;
2998 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2999 p
+= buffer
- linebuffer
->buffer
;
3000 pend
= buffer
+ linebuffer
->size
;
3001 linebuffer
->buffer
= buffer
;
3003 if (c
== EOF
|| c
== '\n')
3006 newline
= (c
== '\n') ? 1 : 0;
3012 return p
- buffer
+ newline
;
3019 return savenstr (cp
, strlen (cp
));
3029 dp
= xnew (len
+ 1, char);
3030 strncpy (dp
, cp
, len
);
3036 * Return the ptr in sp at which the character c last
3037 * appears; NULL if not found
3039 * Identical to v7 rindex, included for portability.
3043 etags_rindex (sp
, c
)
3044 register char *sp
, c
;
3059 * Return the ptr in sp at which the character c first
3060 * appears; NULL if not found
3062 * Identical to v7 index, included for portability.
3067 register char *sp
, c
;
3077 /* Print error message and exit. */
3088 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3095 fprintf (stderr
, "%s: ", progname
);
3096 fprintf (stderr
, s1
, s2
);
3097 fprintf (stderr
, "\n");
3100 /* Return a newly-allocated string whose contents
3101 concatenate those of s1, s2, s3. */
3107 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3108 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
3110 strcpy (result
, s1
);
3111 strcpy (result
+ len1
, s2
);
3112 strcpy (result
+ len1
+ len2
, s3
);
3113 result
[len1
+ len2
+ len3
] = '\0';
3118 /* Return a newly allocated string containing the filename of FILE relative
3119 to the absolute directory DIR (which should end with a slash). */
3122 relative_filename (file
, dir
)
3125 char *fp
, *dp
, *res
;
3127 /* Find the common root of file and dir. */
3128 fp
= absolute_filename (file
, cwd
);
3130 while (*fp
++ == *dp
++)
3139 /* Build a sequence of "../" strings for the resulting relative filename. */
3140 for (dp
= etags_index (dp
+ 1, '/'), res
= "";
3142 dp
= etags_index (dp
+ 1, '/'))
3144 res
= concat (res
, "../", "");
3147 /* Add the filename relative to the common root of file and dir. */
3148 res
= concat (res
, fp
+ 1, "");
3150 return res
; /* temporary stub */
3153 /* Return a newly allocated string containing the
3154 absolute filename of FILE given CWD (which should end with a slash). */
3156 absolute_filename (file
, cwd
)
3159 char *slashp
, *cp
, *res
;
3162 res
= concat (file
, "", "");
3164 res
= concat (cwd
, file
, "");
3166 /* Delete the "/dirname/.." and "/." substrings. */
3167 slashp
= etags_index (res
, '/');
3168 while (slashp
!= NULL
&& slashp
[0] != '\0')
3170 if (slashp
[1] == '.')
3172 if (slashp
[2] == '.'
3173 && (slashp
[3] == '/' || slashp
[3] == '\0'))
3178 while (cp
>= res
&& *cp
!= '/');
3181 strcpy (cp
, slashp
+ 3);
3183 else /* else (cp == res) */
3185 if (slashp
[3] != NULL
)
3186 strcpy (cp
, slashp
+ 4);
3192 else if (slashp
[2] == '/' || slashp
[2] == '\0')
3194 strcpy (slashp
, slashp
+ 2);
3199 slashp
= etags_index (slashp
+ 1, '/');
3206 /* Return a newly allocated string containing the absolute filename
3207 of dir where FILE resides given CWD (which should end with a slash). */
3209 absolute_dirname (file
, cwd
)
3215 slashp
= etags_rindex (file
, '/');
3220 res
= absolute_filename (file
, cwd
);
3226 /* Like malloc but get fatal error if memory is exhausted. */
3232 char *result
= (char *) malloc (size
);
3234 fatal ("virtual memory exhausted", 0);
3239 xrealloc (ptr
, size
)
3243 char *result
= (char *) realloc (ptr
, size
);
3245 fatal ("virtual memory exhausted");