1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 1987, 1988, 1989, 1992 Free Software Foundation, Inc. and Ken Arnold
4 This file is part of GNU Emacs.
6 GNU Emacs 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 1, or (at your option)
11 GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 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++.
31 #include <sys/types.h>
37 #define alloca __builtin_alloca
42 extern char *alloca ();
46 extern char *malloc (), *realloc ();
47 extern char *getenv ();
48 extern char *strcpy (), *strncpy ();
51 char *etags_index (), *etags_rindex ();
54 /* Define the symbol ETAGS to make the program "etags",
55 which makes emacs-style tag tables by default.
56 Define CTAGS to make the program "ctags" compatible with the usual one.
57 Define neither one to get behavior that depends
58 on the name with which the program is invoked
59 (but we don't normally compile it that way). */
61 #if !defined(ETAGS) && !defined(CTAGS)
62 /* If neither is defined, program can be run as either. */
67 /* On VMS, CTAGS is not useful, so always do ETAGS. */
74 /* Exit codes for success and failure. */
84 * The FILEPOS abstract type, which represents a position in a file,
85 * plus the following accessor functions:
87 * long GET_CHARNO (pos)
88 * returns absolute char number.
89 * long GET_COOKIE (pos)
90 * returns ftell () cookie.
91 * void SET_FILEPOS (pos, fp, charno)
92 * FILE *fp; long charno;
93 * sets `pos' from the current file
94 * position of `fp' and from `charno',
95 * which must be the absolute character
96 * number corresponding to the current
99 * The `pos' parameter is an lvalue expression of type FILEPOS.
100 * Parameters to the accessor functions are evaluated 0 or more times,
101 * and so must have no side effects.
103 * FILEPOS objects can also be assigned and passed to and from
104 * functions in the normal C manner.
106 * Implementation notes: the `+ 0' is to enforce rvalue-ness.
115 #define GET_CHARNO(pos) ((pos).charno + 0)
116 #define GET_COOKIE(pos) ((pos).cookie + 0)
117 #define SET_FILEPOS(pos, fp, cno) \
118 ((void) ((pos).cookie = ftell (fp), (pos).charno = (cno)))
121 /* UNIX real implementation */
122 typedef long FILEPOS
;
123 #define GET_CHARNO(pos) ((pos) + 0)
124 #define GET_COOKIE(pos) GET_CHARNO (pos)
125 #define SET_FILEPOS(pos, fp, cno) ((void) ((pos) = (cno)))
127 /* UNIX debugging implementation */
133 #define GET_CHARNO(pos) ((pos).charno + 0)
134 #define GET_COOKIE(pos) GET_CHARNO (pos)
135 #define SET_FILEPOS(pos, fp, cno) \
136 ((void) ((pos).charno = (cno), \
137 (cno) != ftell (fp) ? (error ("SET_FILEPOS inconsistency"), 0) \
142 #define streq(s, t) (strcmp (s, t) == 0)
143 #define strneq(s, t, n) (strncmp (s, t, n) == 0)
150 #define iswhite(arg) (_wht[arg]) /* T if char is white */
151 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
152 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
153 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
154 #define isgood(arg) (_gd[arg]) /* T if char can be after ')' */
156 #define max(I1,I2) ((I1) > (I2) ? (I1) : (I2))
159 { /* sorting structure */
160 char *name
; /* function or type name */
161 char *file
; /* file name */
162 logical is_func
; /* use pattern or line no */
163 logical rewritten
; /* list name separately */
164 logical been_warned
; /* set if noticed dup */
165 int lno
; /* line number tag is on */
166 long cno
; /* character number line starts on */
167 char *pat
; /* search pattern */
168 struct nd_st
*left
, *right
; /* left and right sons */
172 typedef struct nd_st NODE
;
174 logical gotone
, /* found a func already on line */
175 /* boolean "func" (see init) */
176 header_file
, /* TRUE if .h file, FALSE o.w. */
177 _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177], _gd
[0177];
187 int total_size_of_entries ();
188 logical
consider_token ();
196 void Scheme_funcs ();
201 void find_entries ();
209 void process_file ();
215 * xnew -- allocate storage
218 * Type *xnew (int n, Type);
220 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type)))
225 * Symbol table stuff.
227 * Should probably be implemented with hash table; linked list for now.
232 st_none
, st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
240 struct stab_entry
*next
;
243 typedef struct stab_entry Stab_entry
;
244 typedef Stab_entry
*Stab
;
248 * Stab, Stab_entry, stab_create, stab_search, stab_find -- symbol table
251 * Types: Stab, Stab_entry, enum sym_type
253 * Stab * stab_create ()
255 * Stab_entry * stab_find (stab, sym)
259 * Stab_entry * stab_search (stab, sym)
264 * stab_create creates a Stab, a symbol table object, and returns a
265 * pointer to it. stab_find finds a symbol in a Stab; it returns a
266 * pointer to the Stab_entry if found, otherwise NULL. stab_search
267 * is like stab_find, except that it creates a new Stab_entry,
268 * initialized with type = st_none, if one did not exist already
269 * (it never returns NULL).
271 * A Stab_entry is a structure that contains at least the following
274 * char *name; // must not be modified
275 * enum sym_type type; // should be set
277 * The type field is initially set to st_none; it should be set to
278 * something else by the caller of stab_search. Other possible values
279 * of an enum sym_type can be added.
287 *sp
= NULL
; /* a Stab starts out as a null Stab_entry* */
292 stab_find (stab
, sym
, symlen
)
297 register Stab_entry
*se
;
298 for (se
= *stab
; se
!= NULL
; se
= se
->next
)
300 if (se
->symlen
== symlen
&& strneq (se
->sym
, sym
, symlen
))
308 stab_search (stab
, sym
, symlen
)
313 register Stab_entry
*se
;
314 se
= stab_find (stab
, sym
, symlen
);
319 se
= xnew (1, Stab_entry
);
320 se
->sym
= savenstr (sym
, symlen
);
332 * stab_type -- type of a symbol table entry
335 * enum sym_type stab_type (Stab_entry *se);
338 * May evaluate its argument more than once.
341 #define stab_type(se) ((se)==NULL ? st_none : (se)->type)
357 /* typedefs are recognized using a simple finite automaton.
358 * tydef is its state variable.
362 none
, begin
, middle
, end
368 /* struct tags for C++ are recognized using another simple
369 * finite automaton. `structdef' is its state variable.
370 * This machinery is only invoked for C++; otherwise structdef
371 * should remain snone. However, this machinery can easily be
372 * adapted to find structure tags in normal C code.
376 snone
, /* nothing seen yet */
377 skeyseen
, /* struct-like keyword seen */
378 stagseen
, /* struct-like tag seen */
379 scolonseen
, /* colon seen after struct-like tag */
380 sinbody
/* in a class body: recognize member func defs */
382 STRUCTST structdef
= snone
;
384 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
385 * struct tag, and structkey is the preceding struct-like keyword.
388 Stab_entry
*structkey
;
391 * Yet another little state machine to deal with preprocessor lines.
395 dnone
, /* nothing seen */
396 dsharpseen
, /* '#' seen as first char on line */
397 ddefineseen
, /* '#' and 'define' seen */
398 dignorerest
/* ignore rest of line */
403 * LEVEL_OK_FOR_FUNCDEF allows C++ function definition within class body.
404 * Currently tydef and structdef stuff (typedefs and struct definitions) are
405 * only noticed when level==0, but that may change.
407 * Note that this macro may only be evaluated inside C_entries(). It is
408 * for self-documentation only.
410 #define LEVEL_OK_FOR_FUNCDEF() \
411 (level==0 || (c_ext && level==1 && structdef==sinbody))
415 * set this to TRUE, and the next token considered is called a function.
417 logical next_token_is_func
;
419 /* C extensions. Currently all listed extensions are C++ dialects, so
420 * `c_ext' is used as an abbreviation for `c_ext&C_PLPL'. If a non-C++
421 * dialect is added, this must change.
423 #define C_PLPL 0x1 /* C++ */
424 #define C_STAR 0x3 /* C* */
426 char searchar
= '/'; /* use /.../ searches */
428 LINENO lineno
; /* line number of current line */
429 long charno
; /* current character number */
430 FILEPOS linepos
; /* start of line (C only) */
431 FILEPOS prev_linepos
; /* start of previous line (C only) */
433 long linecharno
; /* charno of start of line; not used by C, but
434 * by every other language.
437 char *curfile
, /* current input file name */
438 *outfile
, /* output file */
439 *white
= " \f\t\n", /* white chars */
440 *endtk
= " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
441 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$", /* token starting chars */
442 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789", /* valid in-token chars */
443 *notgd
= ",;"; /* non-valid after-function chars */
445 int append_to_tagfile
; /* -a: append to tags */
446 int emacs_tags_format
; /* emacs style output (no -e option any more) */
447 /* The following three default to 1 for etags, but to 0 for ctags. */
448 int typedefs
; /* -t: create tags for typedefs */
449 int typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
450 /* 0 struct/enum/union decls, and C++ */
451 /* member functions */
452 int constantypedefs
; /* -d: create tags for C #define and enum */
453 /* constants. Default under etags. Enum */
454 /* constants not implemented. */
455 /* -D: opposite of -d. Default under ctags. */
456 int update
; /* -u: update tags */
457 int vgrind_style
; /* -v: create vgrind style index output */
458 int no_warnings
; /* -w: suppress warnings */
459 int cxref_style
; /* -x: create cxref style output */
460 int cplusplus
; /* .[hc] means C++, not C */
461 int noindentypedefs
; /* -S: ignore indentation in C */
463 /* Name this program was invoked with. */
466 struct option longopts
[] = {
467 { "append", no_argument
, NULL
, 'a' },
468 { "backward-search", no_argument
, NULL
, 'B' },
469 { "c++", no_argument
, NULL
, 'C' },
470 { "cxref", no_argument
, NULL
, 'x' },
471 { "defines", no_argument
, NULL
, 'd' },
472 { "forward-search", no_argument
, NULL
, 'F' },
473 { "help", no_argument
, NULL
, 'H' },
474 { "ignore-indentation", no_argument
, NULL
, 'S' },
475 { "include", required_argument
, NULL
, 'i' },
476 { "no-defines", no_argument
, NULL
, 'D' },
477 { "no-warn", no_argument
, NULL
, 'w' },
478 { "output", required_argument
, NULL
, 'o' },
479 { "typedefs", no_argument
, NULL
, 't' },
480 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
481 { "update", no_argument
, NULL
, 'u' },
482 { "version", no_argument
, NULL
, 'V' },
483 { "vgrind", no_argument
, NULL
, 'v' },
487 FILE *inf
, /* ioptr for current input file */
488 *outf
; /* ioptr for tags file */
490 NODE
*head
; /* the head of the binary tree of tags */
492 int permit_duplicates
= 1; /* Nonzero means allow duplicate tags. */
494 /* A `struct linebuffer' is a structure which holds a line of text.
495 `readline' reads a line from a stream into a linebuffer
496 and works regardless of the length of the line. */
504 struct linebuffer lb
; /* the current line */
505 struct linebuffer lb1
; /* sometimes, a previous line in which a token lies */
506 struct linebuffer filename_lb
; /* used to read in filenames */
521 printf ("for Emacs version 19.0.\n");
529 printf ("These are the options accepted by %s. You may use unambiguous\n\
530 abbreviations for the long option names.\n\n", progname
);
534 Append tag entries to existing tags file.\n\
536 Treat files with `.c' and `.h' extensions as C++ code, not C\n\
537 code. Files with `.C', `.H', `.cxx', `.hxx', or `.cc'\n\
538 extensions are always assumed to be C++ code.\n\
540 Create tag entries for #defines, too.", stdout
);
543 fputs (" This is the default\n\
549 Don't create tag entries for #defines.", stdout
);
552 fputs (" This is the default\n\
557 -o FILE, --output=FILE\n\
558 Write the tags to FILE.\n\
559 -S, --ignore-indentation\n\
560 Don't rely on indentation quite as much as normal. Currently,\n\
561 this means not to assume that a closing brace in the first\n\
562 column is the final brace of a function or structure\n\
565 Generate tag entries for typedefs. This is the default\n\
567 -T, --typedefs-and-c++\n\
568 Generate tag entries for typedefs, struct/enum/union tags, and\n\
569 C++ member functions.");
572 puts ("-i FILE, --include=FILE\n\
573 Include a note in tag file indicating that, when searching for\n\
574 a tag, one should also consult the tags file FILE after\n\
575 checking the current file.");
579 puts ("-B, --backward-search\n\
580 Write the search commands for the tag entries using '?', the\n\
581 backward-search command.\n\
582 -F, --forward-search\n\
583 Write the search commands for the tag entries using '/', the\n\
584 forward-search command.\n\
586 Update the tag entries for the given files, leaving tag\n\
587 entries for other files in place. Currently, this is\n\
588 implemented by deleting the existing entries for the given\n\
589 files and then rewriting the new entries at the end of the\n\
590 tags file. It is often faster to simply rebuild the entire\n\
591 tag file than to use this.\n\
593 Generates an index of items intended for human consumption,\n\
594 similar to the output of vgrind. The index is sorted, and\n\
595 gives the page number of each item.\n\
597 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
598 The output uses line numbers instead of page numbers, but\n\
599 beyond that the differences are cosmetic; try both to see\n\
602 Suppress warning messages about entries defined in multiple\n\
606 puts ("-V, --version\n\
607 Print the version of the program.\n\
609 Print this help message.");
622 unsigned int nincluded_files
= 0;
623 char **included_files
= (char **) alloca (argc
* sizeof (char *));
628 extern char *gfnames ();
629 extern char *massage_name ();
635 emacs_tags_format
= 1;
637 emacs_tags_format
= 0;
641 * If etags, always find typedefs and structure tags. Why not?
642 * Also default is to find macro constants.
644 if (emacs_tags_format
)
645 typedefs
= typedefs_and_cplusplus
= constantypedefs
= 1;
650 opt
= getopt_long (argc
, argv
, "aCdDo:StTi:BFuvxwVH", longopts
, 0);
658 /* If getopt returns '\0', then it has already processed a
659 long-named option. We should do nothing. */
662 /* Common options. */
679 "%s: -o flag may only be given once\n", progname
);
692 typedefs_and_cplusplus
++;
703 if (!emacs_tags_format
)
705 included_files
[nincluded_files
++] = optarg
;
711 if (emacs_tags_format
)
716 if (emacs_tags_format
)
721 if (emacs_tags_format
)
729 if (emacs_tags_format
)
734 if (emacs_tags_format
)
745 fprintf (stderr
, "%s: No input files specified.\n", progname
);
748 fprintf (stderr
, "%s: Try '%s --help' for a complete list of options.\n",
755 outfile
= emacs_tags_format
? "TAGS" : "tags";
758 init (); /* set up boolean "functions" */
762 initbuffer (&filename_lb
);
764 * loop through files finding functions
766 if (emacs_tags_format
)
768 if (streq (outfile
, "-"))
771 outf
= fopen (outfile
, append_to_tagfile
? "a" : "w");
782 while (gfnames (&argc
, &argv
, &got_err
) != NULL
)
786 error ("Can't find file %s\n", this_file
);
791 this_file
= massage_name (this_file
);
794 } /* solely to balance out the ifdef'd parens above */
797 for (; optind
< argc
; optind
++)
799 this_file
= argv
[optind
];
803 /* Input file named "-" means read file names from stdin
805 if (streq (this_file
, "-"))
807 while (!feof (stdin
))
809 (void) readline (&filename_lb
, stdin
);
810 if (strlen (filename_lb
.buffer
) > 0)
811 process_file (filename_lb
.buffer
);
815 process_file (this_file
);
819 if (emacs_tags_format
)
821 while (nincluded_files
-- > 0)
822 fprintf (outf
, "\f\n%s,include\n", *included_files
++);
824 (void) fclose (outf
);
835 /* update cannot be set under VMS, so we may assume that argc
836 and argv have not been munged. */
837 for (i
= optind
; i
< argc
; i
++)
840 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
841 outfile
, argv
[i
], outfile
);
846 outf
= fopen (outfile
, append_to_tagfile
? "a" : "w");
853 (void) fclose (outf
);
856 sprintf (cmd
, "sort %s -o %s", outfile
, outfile
);
864 * This routine is called on each file argument.
870 struct stat stat_buf
;
872 stat (file
, &stat_buf
);
873 if (!(stat_buf
.st_mode
& S_IFREG
)
875 || !(stat_buf
.st_mode
& S_IFLNK
)
879 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
883 if (streq (file
, outfile
) && !streq (outfile
, "-"))
885 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
888 if (emacs_tags_format
)
890 char *cp
= etags_rindex (file
, '/');
897 if (emacs_tags_format
)
899 fprintf (outf
, "\f\n%s,%d\n",
900 file
, total_size_of_entries (head
));
908 * This routine sets up the boolean psuedo-functions which work
909 * by seting boolean flags dependent upon the corresponding character
910 * Every char which is NOT in that string is not a white char. Therefore,
911 * all of the array "_wht" is set to FALSE, and then the elements
912 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
913 * of a char is TRUE if it is the string "white", else FALSE.
921 for (i
= 0; i
< 0177; i
++)
923 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
926 for (sp
= white
; *sp
; sp
++)
928 for (sp
= endtk
; *sp
; sp
++)
930 for (sp
= intk
; *sp
; sp
++)
932 for (sp
= begtk
; *sp
; sp
++)
934 for (sp
= notgd
; *sp
; sp
++)
936 _wht
[0] = _wht
['\n'];
937 _etk
[0] = _etk
['\n'];
938 _btk
[0] = _btk
['\n'];
939 _itk
[0] = _itk
['\n'];
944 * This routine opens the specified file and calls the function
945 * which finds the function and type definitions.
952 void prolog_funcs ();
954 inf
= fopen (file
, "r");
960 curfile
= savestr (file
);
961 cp
= etags_rindex (file
, '.');
963 header_file
= (cp
&& (streq (cp
+ 1, "h")));
965 /* .tex, .aux or .bbl implies LaTeX source code */
966 if (cp
&& (streq (cp
+ 1, "tex") || streq (cp
+ 1, "aux")
967 || streq (cp
+ 1, "bbl")))
970 goto close_and_return
;
972 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
973 if (cp
&& (streq (cp
+ 1, "l")
974 || streq (cp
+ 1, "el")
975 || streq (cp
+ 1, "lsp")
976 || streq (cp
+ 1, "lisp")
977 || streq (cp
+ 1, "cl")
978 || streq (cp
+ 1, "clisp")))
981 goto close_and_return
;
983 /* .scm or .sm or .scheme or ... implies scheme source code */
984 if (cp
&& (streq (cp
+ 1, "sm")
985 || streq (cp
+ 1, "scm")
986 || streq (cp
+ 1, "scheme")
987 || streq (cp
+ 1, "t")
988 || streq (cp
+ 1, "sch")
989 || streq (cp
+ 1, "SM")
990 || streq (cp
+ 1, "SCM")
991 /* The `SCM' or `scm' prefix with a version number */
992 || (cp
[-1] == 'm' && cp
[-2] == 'c' && cp
[-3] == 's'
993 && string_numeric_p (cp
+ 1))
994 || (cp
[-1] == 'M' && cp
[-2] == 'C' && cp
[-3] == 'S'
995 && string_numeric_p (cp
+ 1))))
1001 /* Assume that ".s" or ".a" is assembly code. -wolfgang. */
1002 if (cp
&& (cp
[1] == 's' || cp
[1] == 'a') && cp
[2] == '\0')
1008 /* .C or .H or .cxx or .hxx or .cc: a C++ file */
1009 if (cp
&& (streq (cp
+ 1, "C")
1010 || streq (cp
+ 1, "H")
1011 || streq (cp
+ 1, "cxx")
1012 || streq (cp
+ 1, "hxx")
1013 || streq (cp
+ 1, "cc")))
1015 C_entries (C_PLPL
); /* C++ */
1016 goto close_and_return
;
1018 /* .cs or .hs: a C* file */
1019 if (cp
&& (cp
[1] == 'c' || cp
[1] == 'h') && cp
[2] == 's' && cp
[3] == '\0')
1022 goto close_and_return
;
1024 /* .pl implies prolog source code */
1025 if (cp
&& !strcmp (cp
+ 1, "pl"))
1028 goto close_and_return
;
1030 /* .p or .pas: a Pascal file */
1031 if (cp
&& (streq (cp
+ 1, "p")
1032 || streq (cp
+ 1, "pas")))
1035 goto close_and_return
;
1037 /* if not a .c or .h or .y file, try fortran */
1038 else if (cp
&& ((cp
[1] != 'c'
1041 || (cp
[1] != 0 && cp
[2] != 0)))
1043 if (PF_funcs (inf
) != 0)
1044 goto close_and_return
;
1045 rewind (inf
); /* no fortran tags found, try C */
1047 C_entries (cplusplus
? C_PLPL
: 0);
1050 (void) fclose (inf
);
1053 /* Nonzero if string STR is composed of digits. */
1056 string_numeric_p (str
)
1061 if (*str
< '0' || *str
> '9')
1068 /* Should take a TOKEN* instead!! */
1071 pfnote (name
, is_func
, rewritten
, linestart
, linelen
, lno
, cno
)
1072 char *name
; /* tag name */
1073 logical is_func
; /* function or type name? */
1074 logical rewritten
; /* tag different from text of definition? */
1085 np
= (NODE
*) malloc (sizeof (NODE
));
1088 if (!emacs_tags_format
)
1090 /* It's okay to output early in etags -- it only disrupts the
1091 * character count of the tag entries, which is no longer used
1092 * by tags.el anyway.
1094 error ("too many entries to sort");
1099 np
= xnew (1, NODE
);
1101 /* If ctags mode, change name "main" to M<thisfilename>. */
1102 if (!emacs_tags_format
&& !cxref_style
&& streq (name
, "main"))
1104 fp
= etags_rindex (curfile
, '/');
1105 name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1106 fp
= etags_rindex (name
, '.');
1107 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1111 np
->name
= savestr (name
);
1113 np
->is_func
= is_func
;
1114 np
->rewritten
= rewritten
;
1116 /* UNCOMMENT THE +1 HERE: */
1117 np
->cno
= cno
/* + 1 */ ; /* our char numbers are 0-base; emacs's are 1-base */
1118 np
->left
= np
->right
= 0;
1119 if (emacs_tags_format
)
1121 c
= linestart
[linelen
];
1122 linestart
[linelen
] = 0;
1124 else if (cxref_style
== 0)
1126 sprintf (tem
, strlen (linestart
) < 50 ? "%s$" : "%.50s", linestart
);
1129 np
->pat
= savestr (linestart
);
1130 if (emacs_tags_format
)
1132 linestart
[linelen
] = c
;
1135 add_node (np
, &head
);
1140 * recurse on left children, iterate on right children.
1144 register NODE
*node
;
1148 register NODE
*node_right
= node
->right
;
1149 free_tree (node
->left
);
1152 free ((char *) node
);
1159 * Adds a node to the tree of nodes. In etags mode, we don't keep
1160 * it sorted; we just keep a linear list. In ctags mode, maintain
1161 * an ordered tree, with no attempt at balancing.
1163 * add_node is the only function allowed to add nodes, so it can
1167 add_node (node
, cur_node_p
)
1168 NODE
*node
, **cur_node_p
;
1171 register NODE
*cur_node
= *cur_node_p
;
1172 static NODE
*last_node
= NULL
;/* careful */
1174 if (cur_node
== NULL
)
1181 if (emacs_tags_format
)
1185 fatal ("internal error in add_node");
1186 last_node
->right
= node
;
1192 dif
= strcmp (node
->name
, cur_node
->name
);
1195 * If this tag name matches an existing one, then
1196 * do not add the node, but maybe print a warning.
1200 if (node
->file
== cur_node
->file
)
1204 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1205 node
->file
, lineno
, node
->name
);
1206 fprintf (stderr
, "Second entry ignored\n");
1210 if (!cur_node
->been_warned
&& !no_warnings
)
1213 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1214 node
->file
, cur_node
->file
, node
->name
);
1216 cur_node
->been_warned
= TRUE
;
1220 /* Maybe refuse to add duplicate nodes. */
1221 if (!permit_duplicates
)
1223 if (!strcmp (node
->name
, cur_node
->name
)
1224 && !strcmp (node
->file
, cur_node
->file
))
1228 /* Actually add the node */
1229 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1242 /* Output subentries that precede this one */
1243 put_entries (node
->left
);
1245 /* Output this entry */
1247 if (emacs_tags_format
)
1249 if (node
->rewritten
)
1251 fprintf (outf
, "%s\177%s\001%d,%d\n",
1252 node
->name
, node
->pat
, node
->lno
, node
->cno
);
1256 fprintf (outf
, "%s\177%d,%d\n",
1257 node
->pat
, node
->lno
, node
->cno
);
1260 else if (!cxref_style
)
1262 fprintf (outf
, "%s\t%s\t",
1263 node
->name
, node
->file
);
1267 putc (searchar
, outf
);
1270 for (sp
= node
->pat
; *sp
; sp
++)
1272 if (*sp
== '\\' || *sp
== searchar
)
1276 putc (searchar
, outf
);
1279 { /* a typedef; text pattern inadequate */
1280 fprintf (outf
, "%d", node
->lno
);
1284 else if (vgrind_style
)
1285 fprintf (stdout
, "%s %s %d\n",
1286 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1288 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1289 node
->name
, node
->lno
, node
->file
, node
->pat
);
1291 /* Output subentries that follow this one */
1292 put_entries (node
->right
);
1295 /* Length of a number's decimal representation. */
1303 for (; num
; num
/= 10)
1309 * Return total number of characters that put_entries will output for
1310 * the nodes in the subtree of the specified node. Works only if emacs_tags_format
1311 * is set, but called only in that case. This count is irrelevant with
1312 * the new tags.el, but is still supplied for backward compatibility.
1315 total_size_of_entries (node
)
1324 for (; node
; node
= node
->right
)
1326 /* Count left subentries. */
1327 total
+= total_size_of_entries (node
->left
);
1329 /* Count this entry */
1330 total
+= strlen (node
->pat
) + 1;
1331 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1332 if (node
->rewritten
)
1333 total
+= 1 + strlen (node
->name
); /* \001name */
1340 * The C symbol tables.
1343 Stab
*C_stab
, *C_PLPL_stab
, *C_STAR_stab
;
1347 * Stab *get_C_stab (int c_ext);
1349 #define get_C_stab(c_ext) ((c_ext&C_STAR) ? C_STAR_stab : \
1350 c_ext ? C_PLPL_stab : \
1354 add_keyword (stab
, sym
, type
)
1359 stab_search (stab
, sym
, strlen (sym
))->type
= type
;
1363 C_create_stab (c_ext
)
1368 stab
= stab_create ();
1372 add_keyword (stab
, "class", st_C_struct
);
1374 add_keyword (stab
, "domain", st_C_struct
);
1375 add_keyword (stab
, "union", st_C_struct
);
1376 add_keyword (stab
, "struct", st_C_struct
);
1377 add_keyword (stab
, "enum", st_C_enum
);
1378 add_keyword (stab
, "typedef", st_C_typedef
);
1379 add_keyword (stab
, "define", st_C_define
);
1380 add_keyword (stab
, "long", st_C_typespec
);
1381 add_keyword (stab
, "short", st_C_typespec
);
1382 add_keyword (stab
, "int", st_C_typespec
);
1383 add_keyword (stab
, "char", st_C_typespec
);
1384 add_keyword (stab
, "float", st_C_typespec
);
1385 add_keyword (stab
, "double", st_C_typespec
);
1386 add_keyword (stab
, "signed", st_C_typespec
);
1387 add_keyword (stab
, "unsigned", st_C_typespec
);
1388 add_keyword (stab
, "const", st_C_typespec
);
1389 add_keyword (stab
, "volatile", st_C_typespec
);
1397 C_stab
= C_create_stab (0);
1398 C_PLPL_stab
= C_create_stab (C_PLPL
);
1399 C_STAR_stab
= C_create_stab (C_STAR
| C_PLPL
);
1404 * This routine finds functions and typedefs in C syntax and adds them
1408 #define CNL_SAVE_DEFINEDEF \
1410 prev_linepos = linepos; \
1411 SET_FILEPOS (linepos, inf, charno); \
1413 charno += readline (&lb, inf); \
1419 CNL_SAVE_DEFINEDEF; \
1420 definedef = dnone; \
1425 int c_ext
; /* extension of C? */
1427 register int c
; /* latest char read; '\0' for end of line */
1428 register int tokoff
; /* offset in line of beginning of latest token */
1429 register int toklen
; /* length of latest token */
1430 register char *lp
; /* pointer one beyond the character `c' */
1431 logical incomm
, inquote
, inchar
, midtoken
;
1432 int level
; /* current curly brace level */
1441 gotone
= midtoken
= inquote
= inchar
= incomm
= FALSE
;
1444 next_token_is_func
= 0;
1453 /* If we're at the end of the line, the next character is a
1454 '\0'; don't skip it, because it's the thing that tells us
1455 to read the next line. */
1463 if (c
== '*' && *lp
== '/')
1497 else if (c_ext
&& *lp
== '/')
1504 if (lp
== lb
.buffer
+ 1 && definedef
== dnone
)
1505 definedef
= dsharpseen
;
1509 * The next two are to help the strucdef state machine.
1510 * They break when they are finished, so they don't interfere
1511 * with anything else that is going on.
1514 if (structdef
== stagseen
)
1515 structdef
= scolonseen
;
1517 /* Not a struct definition when semicolon seen in non-sinbody context. */
1519 if (structdef
!= snone
&& structdef
!= sinbody
)
1522 (void) strcpy (structtag
, "<error 1>");
1533 case skeyseen
: /* unnamed struct */
1534 structtag
[0] = '\0';
1537 case scolonseen
: /* named struct */
1538 structdef
= sinbody
;
1544 if (!noindentypedefs
&& lp
== lb
.buffer
+ 1)
1545 level
= 0; /* reset level if first column */
1548 if (level
== 0 && tydef
== middle
)
1555 (void) strcpy (structtag
, "<error 2>");
1559 if (LEVEL_OK_FOR_FUNCDEF () && !inquote
&& !incomm
&& gotone
== FALSE
)
1565 if (c_ext
&& c
== ':' && *lp
== ':' && intoken (*(lp
+ 1)))
1568 * This handles :: in the middle, but not at beginning
1576 /* The following is no longer true,
1577 now that we advance to the next line
1578 at the end of processing the character. */
1580 * We've just finished lexing an identifier.
1581 * Note that if `c' is '\0', `lb' is the NEXT
1582 * line, `lp' points to the beginning of it, and
1583 * old pointers into `lb.buffer' may no longer be
1584 * valid, since `lb.buffer' may have been
1585 * reallocated. In this case (which corresponds
1586 * to an identifier followed immediately by a
1587 * newline), we re-read the line into lb1.
1589 * This would be faster if the previous line's
1590 * buffer were always saved.
1595 logical bingo
, tok_at_end_of_line
;
1596 char *lp_tmp
; /* addressable */
1601 getline (GET_COOKIE (prev_linepos
));
1602 tok_linebuf
= lb1
.buffer
;
1603 tok_at_end_of_line
= TRUE
;
1604 tok
.linestart
= prev_linepos
;
1605 tok
.lineno
= lineno
- 1;
1610 tok_linebuf
= lb
.buffer
;
1611 tok_at_end_of_line
= FALSE
;
1612 tok
.linestart
= linepos
;
1613 tok
.lineno
= lineno
;
1615 tok
.p
= tok_linebuf
+ tokoff
;
1617 tok
.rewritten
= FALSE
;
1619 bingo
= consider_token (c
, &lp_tmp
, &tok
,
1620 &is_func
, c_ext
, level
);
1624 if (GET_CHARNO (tok
.linestart
) != GET_CHARNO (linepos
)
1625 && !tok_at_end_of_line
)
1628 * Resynchronize tok.p to point into the right
1631 getline (GET_COOKIE (tok
.linestart
));
1633 tok
.p
= lb1
.buffer
+ (tok
.p
- tok_linebuf
);
1634 tok_linebuf
= lb1
.buffer
;
1636 if (structdef
== sinbody
&& definedef
== dnone
&& is_func
)
1637 { /* function defined in C++ class body */
1638 sprintf (tokb
, "%s::%.*s",
1639 structtag
[0] == '\0' ? "_anonymous_"
1642 tok
.rewritten
= TRUE
;
1646 sprintf (tokb
, "%.*s", tok
.len
, tok
.p
);
1648 pfnote (tokb
, is_func
, tok
.rewritten
, tok_linebuf
,
1649 tokoff
+ toklen
+ (tok_at_end_of_line
? 0 : 1),
1650 tok
.lineno
, GET_CHARNO (tok
.linestart
));
1651 gotone
= is_func
; /* function */
1656 else if (intoken (c
))
1659 else if (begtoken (c
))
1661 tokoff
= lp
- 1 - lb
.buffer
;
1666 /* Detect end of line, after having handled the last token on the line. */
1672 if (c
== ';' && tydef
== end
) /* clean with typedefs */
1679 * checks to see if the current token is at the start of a
1680 * function, or corresponds to a typedef. It updates the input
1681 * line pointer *LPP so that the '(' will be in it when it returns.
1683 * *IS_FUNC gets TRUE iff the token is a function.
1684 * C_EXT is which language we are looking at.
1686 * In the future we will need some way to adjust where the end of
1687 * the token is; for instance, implementing the C++ keyword
1688 * `operator' properly will adjust the end of the token to be after
1689 * whatever follows `operator'.
1698 consider_token (c
, lpp
, tokp
, is_func
, c_ext
, level
)
1699 reg
char c
; /* IN: first char after the token */
1700 char **lpp
; /* IN OUT: *lpp points to 2nd char after the token */
1701 reg TOKEN
*tokp
; /* IN */
1702 logical
*is_func
; /* OUT */
1706 reg
char *lp
= *lpp
;
1707 logical firsttok
; /* TRUE if have seen first token in ()'s */
1708 Stab_entry
*tokse
= stab_find (get_C_stab (c_ext
), tokp
->p
, tokp
->len
);
1709 enum sym_type toktype
= stab_type (tokse
);
1711 *is_func
= TRUE
; /* a function */
1714 * Advance the definedef state machine. We set `gotone' for good measure;
1720 /* We're not on a preprocessor line. */
1723 if (toktype
== st_C_define
)
1725 definedef
= ddefineseen
;
1730 definedef
= dignorerest
;
1736 * Make a tag for any macro.
1737 * This will flub up if there is a newline immediately following
1740 *is_func
= (c
== '(');
1741 definedef
= dignorerest
;
1743 if (!*is_func
&& !constantypedefs
)
1749 error ("internal error: definedef value");
1753 * Skip whitespace and comments after the token. This loop should
1754 * also skip C++ comments.
1758 /* At whitespace => skip it. */
1763 /* At a comment => skip to end of comment. */
1764 else if (c
== '/' && *lp
== '*')
1766 /* If we find a comment, skip it. */
1767 while (!(c
== '*' && *lp
== '/'))
1776 if (c
== '*' && *lp
== '/')
1778 lp
++; /* lp now points past the '/' */
1779 c
= *lp
++; /* c is now the --whatever-- after the '/' */
1785 /* If we arrived at eof or eol, decide which one it is.
1786 If it's eol, advance to the next line. */
1796 * If you have custom token types, or when configuration files can
1797 * define custom token types, this switch will be larger.
1809 if (tydef
== begin
|| tydef
== end
)
1818 * This structdef business is currently only invoked when level==0.
1819 * It should be recursively invoked whatever the level, and a stack of
1820 * states kept, to allow for definitions of structs within structs.
1822 * This structdef business is NOT invoked when we are ctags and the
1823 * file is plain C. This is because a struct tag may have the same
1824 * name as another tag, and this loses with ctags.
1826 * This if statement deals with the tydef state machine as follows: if
1827 * tydef==begin and token is struct/union/class/enum, goto badone.
1828 * All the other code here is for the structdef state machine.
1834 if (tydef
== begin
|| (typedefs_and_cplusplus
&& level
== 0 && structdef
== snone
))
1836 structdef
= skeyseen
;
1842 if (structdef
== skeyseen
)
1844 /* If next char is '{' or (for C++) ':', found a structure tag. */
1845 if (c
== '{' || (c_ext
&& c
== ':'))
1848 * We should do this slightly differently for straight C:
1849 * instead of defining `tag', as we now do, we should define
1850 * `struct tag'. (Do this only if the find-tag defaulting is
1851 * done on a sophisticated per-mode basis, so that if the user
1852 * says meta-. anywhere in `struct foo', the default comes out
1853 * `struct foo', not `struct' or `foo'.) This will require
1854 * remembering which keyword (struct/union/class/enum) we saw, as a
1855 * Stab_entry* -- this will also make it possible to merge the
1856 * skeyseen and senumseen states, if we want.
1858 if (stab_type (structkey
) == st_C_struct
)
1860 (void) strncpy (structtag
, tokp
->p
, tokp
->len
);
1861 structtag
[tokp
->len
] = '\0'; /* for struct/union/class */
1862 structdef
= stagseen
;
1866 structtag
[0] = '\0'; /* for enum */
1868 *is_func
= FALSE
; /* not a function */
1873 /* Not a definition: reset structdef */
1875 (void) strcpy (structtag
, "<error 3>");
1877 /* Now what? And how does/should this stuff interact with tydef?? */
1878 /* Also maybe reset lp to *lpp for benefit of the function finding code. */
1890 /* Detect GNUmacs's function-defining macros. */
1891 if (definedef
== dnone
)
1893 if (strneq (tokp
->p
, "DEF", 3)
1894 || strneq (tokp
->p
, "ENTRY", 5)
1895 || strneq (tokp
->p
, "SYSCALL", 7)
1896 || strneq (tokp
->p
, "PSEUDO", 6))
1898 next_token_is_func
= TRUE
;
1901 else if (strneq (tokp
->p
, "EXFUN", 5))
1903 next_token_is_func
= FALSE
;
1907 if (next_token_is_func
)
1909 next_token_is_func
= FALSE
;
1915 while ((c
= *lp
++) != ')')
1923 * This line used to confuse ctags:
1925 * This fixes it. A nonwhite char before the first
1926 * token, other than a / (in case of a comment in there)
1927 * makes this not a declaration.
1929 if (begtoken (c
) || c
== '/')
1931 else if (!iswhite (c
) && !firsttok
)
1934 while (iswhite (c
= *lp
++))
1958 long saveftell
= ftell (inf
);
1960 (void) fseek (inf
, atcookie
, 0);
1961 (void) readline (&lb1
, inf
);
1962 (void) fseek (inf
, saveftell
, 0);
1965 /* Fortran parsing */
1981 linecharno
= charno
;
1982 charno
+= readline (&lb
, fi
);
1985 dbp
++; /* Ratfor escape to fortran */
1986 while (isspace (*dbp
))
1993 if (tail ("integer"))
2001 if (tail ("logical"))
2005 if (tail ("complex") || tail ("character"))
2009 if (tail ("double"))
2011 while (isspace (*dbp
))
2015 if (tail ("precision"))
2021 while (isspace (*dbp
))
2028 if (tail ("function"))
2032 if (tail ("subroutine"))
2040 if (tail ("program"))
2045 if (tail ("procedure"))
2057 register int len
= 0;
2059 while (*cp
&& (*cp
& ~' ') == ((*(dbp
+ len
)) & ~' '))
2072 while (isspace (*dbp
))
2077 while (isspace (*dbp
))
2079 if (!isdigit (*dbp
))
2081 --dbp
; /* force failure */
2086 while (isdigit (*dbp
));
2094 char nambuf
[BUFSIZ
];
2096 while (isspace (*dbp
))
2103 for (cp
= dbp
+ 1; *cp
&& (isalpha (*cp
) || isdigit (*cp
)
2104 || (*cp
== '_') || (*cp
== '$')); cp
++)
2108 (void) strcpy (nambuf
, dbp
);
2110 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2114 /* Handle a file of assembler code. */
2130 linecharno
= charno
;
2131 charno
+= readline (&lb
, fi
);
2134 for (i
= 0; ((c
= dbp
[i
]) && !isspace (c
)) && (c
!= ':'); i
++)
2137 if ((i
> 0) && (c
== ':'))
2142 /* Added by Mosur Mohan, 4/22/88 */
2143 /* Pascal parsing */
2145 #define GET_NEW_LINE \
2147 linecharno = charno; lineno++; \
2148 charno += 1 + readline (&lb, inf); \
2152 /* Locates tags for procedures & functions.
2153 * Doesn't do any type- or var-definitions.
2154 * It does look for the keyword "extern" or "forward"
2155 * immediately following the procedure statement;
2156 * if found, the tag is skipped.
2163 struct linebuffer tline
; /* mostly copied from C_entries */
2167 char nambuf
[BUFSIZ
];
2169 logical
/* each of these flags is TRUE iff: */
2170 incomm1
, /* point is inside {..} comment */
2171 incomm2
, /* point is inside (*..*) comment */
2172 inquote
, /* point is inside '..' string */
2173 get_tagname
, /* point is after PROCEDURE/FUNCTION */
2174 /* keyword, so next item = potential tag */
2175 found_tag
, /* point is after a potential tag */
2176 inparms
, /* point is within parameter-list */
2177 verify_tag
; /* point has passed the parm-list, so the */
2178 /* next token will determine whether */
2179 /* this is a FORWARD/EXTERN to be */
2180 /* ignored, or whether it is a real tag */
2186 initbuffer (&tline
);
2188 incomm1
= incomm2
= inquote
= FALSE
;
2189 found_tag
= FALSE
; /* have a proc name; check if extern */
2190 get_tagname
= FALSE
; /* have found "procedure" keyword */
2191 inparms
= FALSE
; /* found '(' after "proc" */
2192 verify_tag
= FALSE
; /* check if "extern" is ahead */
2194 /* long main loop to get next char */
2198 if (c
== 0) /* if end of line */
2203 if (!((found_tag
&& verify_tag
) ||
2205 c
= *dbp
++; /* only if don't need *dbp pointing */
2206 /* to the beginning of the name of */
2207 /* the procedure or function */
2209 if (incomm1
) /* within { - } comments */
2215 else if (incomm2
) /* within (* - *) comments */
2219 while ((c
= *dbp
++) == '*')
2238 inquote
= TRUE
; /* found first quote */
2240 case '{': /* found open-{-comment */
2244 if (*dbp
== '*') /* found open-(*-comment */
2249 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2252 case ')': /* end of parms list */
2257 if ((found_tag
) && (!inparms
)) /* end of proc or fn stmt */
2264 if ((found_tag
) && (verify_tag
) && (*dbp
!= ' '))
2266 /* check if this is an "extern" declaration */
2269 if ((*dbp
== 'e') || (*dbp
== 'E'))
2271 if (tail ("extern")) /* superfluous, really! */
2277 else if ((*dbp
== 'f') || (*dbp
== 'F'))
2279 if (tail ("forward")) /* check for forward reference */
2285 if ((found_tag
) && (verify_tag
)) /* not external proc, so make tag */
2289 pfnote (nambuf
, TRUE
, FALSE
,
2290 tline
.buffer
, cp
- tline
.buffer
+ 1,
2291 save_lineno
, save_lcno
);
2295 if (get_tagname
) /* grab name of proc or fn */
2300 /* save all values for later tagging */
2301 tline
.size
= lb
.size
;
2302 strcpy (tline
.buffer
, lb
.buffer
);
2303 save_lineno
= lineno
;
2304 save_lcno
= linecharno
;
2306 /* grab block name */
2307 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
2311 strcpy (nambuf
, dbp
);
2313 dbp
= cp
; /* restore dbp to e-o-token */
2314 get_tagname
= FALSE
;
2318 /* and proceed to check for "extern" */
2320 if ((!incomm1
) && (!incomm2
) && (!inquote
) &&
2321 (!found_tag
) && (!get_tagname
))
2323 /* check for proc/fn keywords */
2327 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2331 if (tail ("unction"))
2336 } /* while not e-o-f */
2340 * lisp tag functions
2341 * just look for (def or (DEF
2355 linecharno
= charno
;
2356 charno
+= readline (&lb
, fi
);
2362 while (!isspace (*dbp
))
2364 while (isspace (*dbp
))
2370 /* Check for (foo::defmumble name-defined ... */
2371 while (*dbp
&& *dbp
!= ':' && !isspace (*dbp
)
2372 && *dbp
!= '(' && *dbp
!= ')')
2381 while (!isspace (*dbp
))
2383 while (isspace (*dbp
))
2397 return ((dbp
[1] == 'D' || dbp
[1] == 'd') &&
2398 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2399 (dbp
[3] == 'F' || dbp
[3] == 'f'));
2407 char nambuf
[BUFSIZ
];
2411 for (cp
= dbp
+ 1; *cp
&& *cp
!= '(' && *cp
!= ' '; cp
++)
2415 (void) strcpy (nambuf
, dbp
);
2417 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2422 * Scheme tag functions
2423 * look for (def... xyzzy
2424 * look for (def... (xyzzy
2425 * look for (def ... ((...(xyzzy ....
2426 * look for (set! xyzzy
2429 static void get_scheme ();
2442 linecharno
= charno
;
2443 charno
+= readline (&lb
, fi
);
2445 if (dbp
[0] == '(' &&
2446 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
2447 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2448 (dbp
[3] == 'F' || dbp
[3] == 'f'))
2450 while (!isspace (*dbp
))
2452 /* Skip over open parens and white space */
2453 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
2457 if (dbp
[0] == '(' &&
2458 (dbp
[1] == 'S' || dbp
[1] == 's') &&
2459 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2460 (dbp
[3] == 'T' || dbp
[3] == 't') &&
2461 (dbp
[4] == '!' || dbp
[4] == '!') &&
2464 while (!isspace (*dbp
))
2466 /* Skip over white space */
2467 while (isspace (*dbp
))
2479 char nambuf
[BUFSIZ
];
2483 /* Go till you get to white space or a syntactic break */
2484 for (cp
= dbp
+ 1; *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
); cp
++)
2486 /* Null terminate the string there. */
2489 /* Copy the string */
2490 strcpy (nambuf
, dbp
);
2491 /* Unterminate the string */
2493 /* Announce the change */
2494 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2498 /* Find tags in TeX and LaTeX input files. */
2500 /* TEX_toktab is a table of TeX control sequences that define tags.
2501 Each TEX_tabent records one such control sequence.
2502 CONVERT THIS TO USE THE Stab TYPE!! */
2510 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
2512 /* Default set of control sequences to put into TEX_toktab.
2513 The value of environment var TEXTAGS is prepended to this. */
2515 static char *TEX_defenv
=
2516 ":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2519 struct TEX_tabent
*TEX_decode_env ();
2523 static char TEX_esc
= '\\';
2524 static char TEX_opgrp
= '{';
2525 static char TEX_clgrp
= '}';
2528 * TeX/LaTeX scanning loop.
2541 /* Select either \ or ! as escape character. */
2544 /* Initialize token table once from environment. */
2546 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
2549 { /* Scan each line in file */
2551 linecharno
= charno
;
2552 charno
+= readline (&lb
, fi
);
2555 while (dbp
= etags_index (dbp
, TEX_esc
)) /* Look at each escape in line */
2561 linecharno
+= dbp
- lasthit
;
2563 i
= TEX_Token (lasthit
);
2566 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
2567 break; /* We only save a line once */
2573 #define TEX_LESC '\\'
2574 #define TEX_SESC '!'
2577 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
2578 /* chars accordingly. */
2586 while ((c
= getc (f
)) != EOF
)
2588 /* Skip to next line if we hit the TeX comment char. */
2592 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
2611 /* Read environment and prepend it to the default string. */
2612 /* Build token table. */
2615 TEX_decode_env (evarname
, defenv
)
2619 register char *env
, *p
;
2621 struct TEX_tabent
*tab
;
2624 /* Append default string to environment. */
2625 env
= getenv (evarname
);
2629 env
= concat (env
, defenv
, "");
2631 /* Allocate a token table */
2632 for (size
= 1, p
= env
; p
;)
2633 if ((p
= etags_index (p
, ':')) && *(++p
))
2635 /* Add 1 to leave room for null terminator. */
2636 tab
= xnew (size
+ 1, struct TEX_tabent
);
2638 /* Unpack environment string into token table. Be careful about */
2639 /* zero-length strings (leading ':', "::" and trailing ':') */
2642 p
= etags_index (env
, ':');
2643 if (!p
) /* End of environment string. */
2644 p
= env
+ strlen (env
);
2646 { /* Only non-zero strings. */
2647 tab
[i
].name
= savenstr (env
, p
- env
);
2648 tab
[i
].len
= strlen (tab
[i
].name
);
2655 tab
[i
].name
= NULL
; /* Mark end of table. */
2663 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
2664 The name being defined actually starts at (NAME + LEN + 1).
2665 But we seem to include the TeX command in the tag name. */
2668 TEX_getit (name
, len
)
2672 char *p
= name
+ len
;
2673 char nambuf
[BUFSIZ
];
2678 /* Let tag name extend to next group close (or end of line) */
2679 while (*p
&& *p
!= TEX_clgrp
)
2681 (void) strncpy (nambuf
, name
, p
- name
);
2682 nambuf
[p
- name
] = 0;
2684 pfnote (nambuf
, TRUE
, FALSE
, lb
.buffer
, strlen (lb
.buffer
), lineno
, linecharno
);
2688 /* If the text at CP matches one of the tag-defining TeX command names,
2689 return the etags_index of that command in TEX_toktab.
2690 Otherwise return -1. */
2692 /* Keep the capital `T' in `Token' for dumb truncating compilers
2693 (this distinguishes it from `TEX_toktab' */
2700 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
2701 if (strncmp (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
) == 0)
2706 /* Support for Prolog. */
2708 /* whole head (not only functor, but also arguments)
2709 is gotten in compound term. */
2712 prolog_getit (s
, lineno
, linecharno
)
2717 char nambuf
[BUFSIZ
], *save_s
, tmpc
;
2725 if (*s
== '\0') /* syntax error. */
2727 else if (insquote
&& *s
== '\'' && *(s
+ 1) == '\'')
2729 else if (*s
== '\'')
2731 insquote
= !insquote
;
2734 else if (!insquote
&& *s
== '(')
2739 else if (!insquote
&& *s
== ')')
2745 else if (npar
< 0) /* syntax error. */
2748 else if (!insquote
&& *s
== '.' && (isspace (*(s
+ 1)) || *(s
+ 1) == '\0'))
2750 if (npar
!= 0) /* syntax error. */
2760 strcpy (nambuf
, save_s
);
2762 pfnote (nambuf
, TRUE
, save_s
, strlen (nambuf
), lineno
, linecharno
);
2765 /* It is assumed that prolog predicate starts from column 0. */
2771 void skip_comment (), prolog_getit ();
2773 lineno
= linecharno
= charno
= 0;
2777 linecharno
+= charno
;
2778 charno
= readline (&lb
, fi
) + 1; /* 1 for newline. */
2780 if (isspace (dbp
[0])) /* not predicate header. */
2782 else if (dbp
[0] == '%') /* comment. */
2784 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
2785 skip_comment (&lb
, fi
, &lineno
, &linecharno
);
2787 prolog_getit (dbp
, lineno
, linecharno
);
2792 skip_comment (plb
, fi
, plineno
, plinecharno
)
2793 struct linebuffer
*plb
;
2795 int *plineno
; /* result */
2796 long *plinecharno
; /* result */
2798 while (!substr ("*/", plb
->buffer
))
2801 *plinecharno
+= readline (plb
, fi
) + 1;
2802 } /* 1 for newline. */
2805 /* Return TRUE if 'sub' exists somewhere in 's'. */
2812 while (*s
&& (s
= etags_index (s
, *sub
)))
2813 if (prestr (sub
, s
))
2820 /* Return TRUE if 'pre' is prefix of string 's'. */
2829 else if (*pre
== *s
)
2830 return (prestr (pre
+ 1, s
+ 1));
2835 /* Initialize a linebuffer for use */
2838 initbuffer (linebuffer
)
2839 struct linebuffer
*linebuffer
;
2841 linebuffer
->size
= 200;
2842 linebuffer
->buffer
= xnew (200, char);
2846 * Read a line of text from `stream' into `linebuffer'.
2847 * Return the number of characters read from `stream',
2848 * which is the length of the line including the newline, if any.
2851 readline (linebuffer
, stream
)
2852 struct linebuffer
*linebuffer
;
2853 register FILE *stream
;
2855 char *buffer
= linebuffer
->buffer
;
2856 register char *p
= linebuffer
->buffer
;
2857 register char *pend
;
2858 int newline
; /* 1 if ended with newline, 0 if ended with EOF */
2860 pend
= p
+ linebuffer
->size
; /* Separate to avoind 386/IX compiler bug. */
2864 register int c
= getc (stream
);
2867 linebuffer
->size
*= 2;
2868 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2869 p
+= buffer
- linebuffer
->buffer
;
2870 pend
= buffer
+ linebuffer
->size
;
2871 linebuffer
->buffer
= buffer
;
2873 if (c
< 0 || c
== '\n')
2876 newline
= (c
== '\n' ? 1 : 0);
2882 return p
- buffer
+ newline
;
2889 return savenstr (cp
, strlen (cp
));
2899 dp
= xnew (len
+ 1, char);
2900 (void) strncpy (dp
, cp
, len
);
2906 * Return the ptr in sp at which the character c last
2907 * appears; NULL if not found
2909 * Identical to v7 rindex, included for portability.
2913 etags_rindex (sp
, c
)
2914 register char *sp
, c
;
2929 * Return the ptr in sp at which the character c first
2930 * appears; NULL if not found
2932 * Identical to v7 index, included for portability.
2937 register char *sp
, c
;
2947 /* Print error message and exit. */
2958 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
2965 fprintf (stderr
, "%s: ", progname
);
2966 fprintf (stderr
, s1
, s2
);
2967 fprintf (stderr
, "\n");
2970 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
2976 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
2977 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
2979 (void) strcpy (result
, s1
);
2980 (void) strcpy (result
+ len1
, s2
);
2981 (void) strcpy (result
+ len1
+ len2
, s3
);
2982 *(result
+ len1
+ len2
+ len3
) = 0;
2987 /* Like malloc but get fatal error if memory is exhausted. */
2993 char *result
= malloc (size
);
2995 fatal ("virtual memory exhausted", 0);
3000 xrealloc (ptr
, size
)
3004 char *result
= realloc (ptr
, size
);
3006 fatal ("virtual memory exhausted");