Mercurial > emacs
annotate lib-src/etags.c @ 1803:5752a95053f2
* xterm.c (x_window_to_scrollbar): Search frames'
condemned_scrollbars list as well; input might arrive during
redisplay.
(x_scrollbar_report_motion): Don't forget to BLOCK_INPUT.
(XTjudge_scrollbars): Clear the condemned scrollbar list before
traversing it, so we don't try to process an event on a scrollbar
we've killed.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sat, 16 Jan 1993 05:58:15 +0000 |
parents | 00beac79bcc7 |
children | 1045deef809f |
rev | line source |
---|---|
240 | 1 /* Tags file maker to go with GNU Emacs |
621 | 2 Copyright (C) 1984, 1987, 1988, 1989, 1992 Free Software Foundation, Inc. and Ken Arnold |
240 | 3 |
4 This file is part of GNU Emacs. | |
5 | |
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) | |
9 any later version. | |
10 | |
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. | |
15 | |
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. */ | |
19 | |
20 /* | |
21 * Authors: | |
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++. | |
27 */ | |
28 | |
29 #include <stdio.h> | |
30 #include <ctype.h> | |
31 #include <sys/types.h> | |
32 #include <sys/stat.h> | |
33 | |
621 | 34 #include "getopt.h" |
35 | |
401 | 36 #ifdef __GNUC__ |
37 #define alloca __builtin_alloca | |
38 #else | |
39 #ifdef sparc | |
40 #include <alloca.h> | |
41 #else | |
42 extern char *alloca (); | |
43 #endif | |
44 #endif | |
45 | |
240 | 46 extern char *malloc (), *realloc (); |
47 extern char *getenv (); | |
48 extern char *strcpy (), *strncpy (); | |
49 extern int strcmp (); | |
50 | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
51 char *etags_index (), *etags_rindex (); |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
52 char *savenstr (); |
240 | 53 |
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). */ | |
60 | |
61 #if !defined(ETAGS) && !defined(CTAGS) | |
62 /* If neither is defined, program can be run as either. */ | |
63 #define ETAGS | |
64 #define CTAGS | |
65 #endif | |
66 | |
67 /* On VMS, CTAGS is not useful, so always do ETAGS. */ | |
68 #ifdef VMS | |
69 #ifndef ETAGS | |
70 #define ETAGS | |
71 #endif | |
72 #endif | |
73 | |
74 /* Exit codes for success and failure. */ | |
75 #ifdef VMS | |
76 #define GOOD (1) | |
77 #define BAD (0) | |
78 #else | |
79 #define GOOD (0) | |
80 #define BAD (1) | |
81 #endif | |
82 | |
83 /* | |
84 * The FILEPOS abstract type, which represents a position in a file, | |
85 * plus the following accessor functions: | |
86 * | |
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 | |
97 * position of `fp'. | |
98 * | |
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. | |
102 * | |
103 * FILEPOS objects can also be assigned and passed to and from | |
104 * functions in the normal C manner. | |
105 * | |
106 * Implementation notes: the `+ 0' is to enforce rvalue-ness. | |
107 */ | |
108 #ifdef VMS | |
109 typedef struct | |
110 { | |
111 long cookie; | |
112 long charno; | |
113 } FILEPOS; | |
114 | |
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))) | |
119 #else | |
120 #ifndef DEBUG | |
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))) | |
126 #else | |
127 /* UNIX debugging implementation */ | |
128 typedef struct | |
129 { | |
130 long charno; | |
131 } FILEPOS; | |
132 | |
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) \ | |
138 : 0)) | |
139 #endif | |
140 #endif | |
141 | |
142 #define streq(s, t) (strcmp (s, t) == 0) | |
143 #define strneq(s, t, n) (strncmp (s, t, n) == 0) | |
144 #define reg register | |
145 #define logical char | |
146 | |
147 #define TRUE 1 | |
148 #define FALSE 0 | |
149 | |
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 ')' */ | |
155 | |
156 #define max(I1,I2) ((I1) > (I2) ? (I1) : (I2)) | |
157 | |
158 struct nd_st | |
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 */ | |
169 }; | |
170 | |
171 long ftell (); | |
172 typedef struct nd_st NODE; | |
173 | |
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]; | |
178 | |
179 | |
180 char *concat (); | |
181 char *savenstr (); | |
182 char *savestr (); | |
183 char *xmalloc (); | |
184 char *xrealloc (); | |
185 int L_isdef (); | |
186 int PF_funcs (); | |
187 int total_size_of_entries (); | |
188 logical consider_token (); | |
189 logical tail (); | |
190 long readline (); | |
191 void Asm_funcs (); | |
192 void C_entries (); | |
193 void L_funcs (); | |
194 void L_getit (); | |
195 void PAS_funcs (); | |
196 void Scheme_funcs (); | |
197 void TEX_funcs (); | |
198 void add_node (); | |
199 void error (); | |
200 void fatal (); | |
201 void find_entries (); | |
202 void free_tree (); | |
203 void getit (); | |
204 void getline (); | |
205 void init (); | |
206 void initbuffer (); | |
207 void initbuffer (); | |
208 void pfnote (); | |
209 void process_file (); | |
210 void put_entries (); | |
211 void takeprec (); | |
212 | |
213 /* | |
214 * MACRO | |
215 * xnew -- allocate storage | |
216 * | |
217 * SYNOPSIS | |
218 * Type *xnew (int n, Type); | |
219 */ | |
220 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type))) | |
221 | |
222 | |
223 | |
224 /* | |
225 * Symbol table stuff. | |
226 * | |
227 * Should probably be implemented with hash table; linked list for now. | |
228 */ | |
229 | |
230 enum sym_type | |
231 { | |
232 st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec | |
233 }; | |
234 | |
235 struct stab_entry | |
236 { | |
237 char *sym; | |
238 int symlen; | |
239 enum sym_type type; | |
240 struct stab_entry *next; | |
241 }; | |
242 | |
243 typedef struct stab_entry Stab_entry; | |
244 typedef Stab_entry *Stab; | |
245 | |
246 /* | |
247 * NAME | |
248 * Stab, Stab_entry, stab_create, stab_search, stab_find -- symbol table | |
249 * | |
250 * SYNOPSIS | |
251 * Types: Stab, Stab_entry, enum sym_type | |
252 * | |
253 * Stab * stab_create () | |
254 * | |
255 * Stab_entry * stab_find (stab, sym) | |
256 * Stab *stab; | |
257 * char *sym; | |
258 * | |
259 * Stab_entry * stab_search (stab, sym) | |
260 * Stab *stab; | |
261 * char *sym; | |
262 * | |
263 * DESCRIPTION | |
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). | |
270 * | |
271 * A Stab_entry is a structure that contains at least the following | |
272 * members: | |
273 * | |
274 * char *name; // must not be modified | |
275 * enum sym_type type; // should be set | |
276 * | |
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. | |
280 */ | |
281 | |
282 Stab * | |
283 stab_create () | |
284 { | |
285 Stab *sp; | |
286 sp = xnew (1, Stab); | |
287 *sp = NULL; /* a Stab starts out as a null Stab_entry* */ | |
288 return sp; | |
289 } | |
290 | |
291 Stab_entry * | |
292 stab_find (stab, sym, symlen) | |
293 Stab *stab; | |
294 register char *sym; | |
295 register int symlen; | |
296 { | |
297 register Stab_entry *se; | |
298 for (se = *stab; se != NULL; se = se->next) | |
299 { | |
300 if (se->symlen == symlen && strneq (se->sym, sym, symlen)) | |
301 return se; | |
302 } | |
303 | |
304 return NULL; | |
305 } | |
306 | |
307 Stab_entry * | |
308 stab_search (stab, sym, symlen) | |
309 register Stab *stab; | |
310 char *sym; | |
311 int symlen; | |
312 { | |
313 register Stab_entry *se; | |
314 se = stab_find (stab, sym, symlen); | |
315 | |
316 if (se == NULL) | |
317 { | |
318 /* make a new one */ | |
319 se = xnew (1, Stab_entry); | |
320 se->sym = savenstr (sym, symlen); | |
321 se->symlen = symlen; | |
322 se->type = st_none; | |
323 se->next = *stab; | |
324 *stab = se; | |
325 } | |
326 | |
327 return se; | |
328 } | |
329 | |
330 /* | |
331 * NAME | |
332 * stab_type -- type of a symbol table entry | |
333 * | |
334 * SYNOPSIS | |
335 * enum sym_type stab_type (Stab_entry *se); | |
336 * | |
337 * WARNING | |
338 * May evaluate its argument more than once. | |
339 */ | |
340 | |
341 #define stab_type(se) ((se)==NULL ? st_none : (se)->type) | |
342 | |
343 | |
344 | |
345 typedef int LINENO; | |
346 | |
347 typedef struct | |
348 { | |
349 char *p; | |
350 int len; | |
351 FILEPOS linestart; | |
352 LINENO lineno; | |
353 logical rewritten; | |
354 } TOKEN; | |
355 | |
356 | |
357 /* typedefs are recognized using a simple finite automaton. | |
358 * tydef is its state variable. | |
359 */ | |
360 typedef enum | |
361 { | |
362 none, begin, middle, end | |
363 } TYST; | |
364 | |
365 TYST tydef = none; | |
366 | |
367 | |
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. | |
373 */ | |
374 typedef enum | |
375 { | |
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 */ | |
381 } STRUCTST; | |
382 STRUCTST structdef = snone; | |
383 /* | |
384 * When structdef is stagseen, scolonseen, or sinbody, structtag is the | |
385 * struct tag, and structkey is the preceding struct-like keyword. | |
386 */ | |
387 char structtag[512]; | |
388 Stab_entry *structkey; | |
389 | |
390 /* | |
391 * Yet another little state machine to deal with preprocessor lines. | |
392 */ | |
393 typedef enum | |
394 { | |
395 dnone, /* nothing seen */ | |
396 dsharpseen, /* '#' seen as first char on line */ | |
397 ddefineseen, /* '#' and 'define' seen */ | |
398 dignorerest /* ignore rest of line */ | |
399 } DEFINEST; | |
400 DEFINEST definedef; | |
401 | |
402 /* | |
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. | |
406 * | |
407 * Note that this macro may only be evaluated inside C_entries(). It is | |
408 * for self-documentation only. | |
409 */ | |
410 #define LEVEL_OK_FOR_FUNCDEF() \ | |
727 | 411 (level==0 || (c_ext && level==1 && structdef==sinbody)) |
240 | 412 |
621 | 413 /* |
414 * next_token_is_func | |
415 * set this to TRUE, and the next token considered is called a function. | |
416 */ | |
417 logical next_token_is_func; | |
418 | |
240 | 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. | |
422 */ | |
423 #define C_PLPL 0x1 /* C++ */ | |
424 #define C_STAR 0x3 /* C* */ | |
425 | |
426 char searchar = '/'; /* use /.../ searches */ | |
427 | |
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) */ | |
432 | |
433 long linecharno; /* charno of start of line; not used by C, but | |
434 * by every other language. | |
435 */ | |
436 | |
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 */ | |
444 | |
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 */ | |
462 | |
463 /* Name this program was invoked with. */ | |
464 char *progname; | |
465 | |
621 | 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' }, | |
484 { 0 } | |
485 }; | |
486 | |
240 | 487 FILE *inf, /* ioptr for current input file */ |
488 *outf; /* ioptr for tags file */ | |
489 | |
490 NODE *head; /* the head of the binary tree of tags */ | |
491 | |
492 int permit_duplicates = 1; /* Nonzero means allow duplicate tags. */ | |
493 | |
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. */ | |
497 | |
498 struct linebuffer | |
499 { | |
500 long size; | |
501 char *buffer; | |
502 }; | |
503 | |
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 */ | |
507 | |
508 | |
509 void | |
621 | 510 print_version () |
511 { | |
512 #ifdef CTAGS | |
513 printf ("CTAGS "); | |
514 #ifdef ETAGS | |
515 printf ("and "); | |
516 #endif | |
517 #endif | |
518 #ifdef ETAGS | |
519 printf ("ETAGS "); | |
520 #endif | |
521 printf ("for Emacs version 19.0.\n"); | |
522 | |
523 exit (0); | |
524 } | |
525 | |
526 void | |
527 print_help () | |
528 { | |
529 printf ("These are the options accepted by %s. You may use unambiguous\n\ | |
530 abbreviations for the long option names.\n\n", progname); | |
531 | |
532 fputs ("\ | |
533 -a, --append\n\ | |
534 Append tag entries to existing tags file.\n\ | |
535 -C, --c++\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\ | |
539 -d, --defines\n\ | |
540 Create tag entries for #defines, too.", stdout); | |
541 | |
542 #ifdef ETAGS | |
543 fputs (" This is the default\n\ | |
544 behavior.", stdout); | |
545 #endif | |
546 | |
547 fputs ("\n\ | |
548 -D, --no-defines\n\ | |
549 Don't create tag entries for #defines.", stdout); | |
550 | |
551 #ifdef CTAGS | |
552 fputs (" This is the default\n\ | |
553 behavior.", stdout); | |
554 #endif | |
555 | |
556 puts ("\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\ | |
563 definition.\n\ | |
564 -t, --typedefs\n\ | |
565 Generate tag entries for typedefs. This is the default\n\ | |
566 behavior.\n\ | |
567 -T, --typedefs-and-c++\n\ | |
568 Generate tag entries for typedefs, struct/enum/union tags, and\n\ | |
569 C++ member functions."); | |
570 | |
571 #ifdef ETAGS | |
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."); | |
576 #endif | |
577 | |
578 #ifdef CTAGS | |
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\ | |
585 -u, --update\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\ | |
592 -v, --vgrind\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\ | |
596 -x, --cxref\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\ | |
600 which you like.\n\ | |
601 -w, --no-warn\n\ | |
602 Suppress warning messages about entries defined in multiple\n\ | |
603 files."); | |
604 #endif | |
605 | |
606 puts ("-V, --version\n\ | |
607 Print the version of the program.\n\ | |
608 -H, --help\n\ | |
609 Print this help message."); | |
610 | |
611 exit (0); | |
612 } | |
613 | |
614 | |
615 void | |
240 | 616 main (argc, argv) |
617 int argc; | |
618 char *argv[]; | |
619 { | |
620 char cmd[100]; | |
621 int i; | |
401 | 622 unsigned int nincluded_files = 0; |
462 | 623 char **included_files = (char **) alloca (argc * sizeof (char *)); |
240 | 624 char *this_file; |
625 #ifdef VMS | |
626 char got_err; | |
627 | |
628 extern char *gfnames (); | |
629 extern char *massage_name (); | |
630 #endif | |
631 | |
632 progname = argv[0]; | |
633 | |
634 #ifndef CTAGS | |
635 emacs_tags_format = 1; | |
636 #else | |
637 emacs_tags_format = 0; | |
638 #endif | |
639 | |
640 /* | |
641 * If etags, always find typedefs and structure tags. Why not? | |
642 * Also default is to find macro constants. | |
643 */ | |
644 if (emacs_tags_format) | |
645 typedefs = typedefs_and_cplusplus = constantypedefs = 1; | |
646 | |
621 | 647 for (;;) |
240 | 648 { |
621 | 649 int opt; |
650 opt = getopt_long (argc, argv, "aCdDo:StTi:BFuvxwVH", longopts, 0); | |
651 | |
652 if (opt == EOF) | |
653 break; | |
654 | |
655 switch (opt) | |
240 | 656 { |
621 | 657 case '\0': |
658 /* If getopt returns '\0', then it has already processed a | |
659 long-named option. We should do nothing. */ | |
660 break; | |
240 | 661 |
621 | 662 /* Common options. */ |
663 case 'a': | |
664 append_to_tagfile++; | |
665 break; | |
666 case 'C': | |
667 cplusplus = 1; | |
668 break; | |
669 case 'd': | |
670 constantypedefs = 1; | |
671 break; | |
672 case 'D': | |
673 constantypedefs = 0; | |
674 break; | |
675 case 'o': | |
676 if (outfile) | |
677 { | |
678 fprintf (stderr, | |
679 "%s: -o flag may only be given once\n", progname); | |
240 | 680 goto usage; |
681 } | |
621 | 682 outfile = optarg; |
683 break; | |
684 case 'S': | |
685 noindentypedefs++; | |
686 break; | |
687 case 't': | |
688 typedefs++; | |
689 break; | |
690 case 'T': | |
691 typedefs++; | |
692 typedefs_and_cplusplus++; | |
693 break; | |
694 case 'V': | |
695 print_version (); | |
696 break; | |
697 case 'H': | |
698 print_help (); | |
699 break; | |
700 | |
701 /* Etags options */ | |
702 case 'i': | |
703 if (!emacs_tags_format) | |
704 goto usage; | |
705 included_files[nincluded_files++] = optarg; | |
706 break; | |
707 | |
708 /* Ctags options. */ | |
709 case 'B': | |
710 searchar = '?'; | |
711 if (emacs_tags_format) | |
712 goto usage; | |
713 break; | |
714 case 'F': | |
715 searchar = '/'; | |
716 if (emacs_tags_format) | |
717 goto usage; | |
718 break; | |
719 case 'u': | |
720 update++; | |
721 if (emacs_tags_format) | |
722 goto usage; | |
723 break; | |
724 case 'v': | |
725 vgrind_style++; | |
726 /*FALLTHRU*/ | |
727 case 'x': | |
728 cxref_style++; | |
729 if (emacs_tags_format) | |
730 goto usage; | |
731 break; | |
732 case 'w': | |
733 no_warnings++; | |
734 if (emacs_tags_format) | |
735 goto usage; | |
736 break; | |
737 | |
738 default: | |
739 goto usage; | |
240 | 740 } |
741 } | |
742 | |
621 | 743 if (optind == argc) |
240 | 744 { |
621 | 745 fprintf (stderr, "%s: No input files specified.\n", progname); |
746 | |
240 | 747 usage: |
621 | 748 fprintf (stderr, "%s: Try '%s --help' for a complete list of options.\n", |
749 progname, progname); | |
240 | 750 exit (BAD); |
751 } | |
752 | |
753 if (outfile == 0) | |
754 { | |
755 outfile = emacs_tags_format ? "TAGS" : "tags"; | |
756 } | |
757 | |
758 init (); /* set up boolean "functions" */ | |
759 | |
760 initbuffer (&lb); | |
761 initbuffer (&lb1); | |
762 initbuffer (&filename_lb); | |
763 /* | |
764 * loop through files finding functions | |
765 */ | |
766 if (emacs_tags_format) | |
767 { | |
768 if (streq (outfile, "-")) | |
769 outf = stdout; | |
770 else | |
771 outf = fopen (outfile, append_to_tagfile ? "a" : "w"); | |
772 if (!outf) | |
773 { | |
774 perror (outfile); | |
775 exit (1); | |
776 } | |
777 } | |
778 | |
779 #ifdef VMS | |
621 | 780 argc -= optind; |
781 argv += optind; | |
782 while (gfnames (&argc, &argv, &got_err) != NULL) | |
240 | 783 { |
784 if (got_err) | |
785 { | |
786 error ("Can't find file %s\n", this_file); | |
787 argc--, argv++; | |
788 } | |
789 else | |
790 { | |
791 this_file = massage_name (this_file); | |
792 #if 0 | |
793 } | |
794 } /* solely to balance out the ifdef'd parens above */ | |
795 #endif | |
796 #else | |
621 | 797 for (; optind < argc; optind++) |
240 | 798 { |
621 | 799 this_file = argv[optind]; |
240 | 800 if (1) |
801 { | |
802 #endif | |
803 /* Input file named "-" means read file names from stdin | |
804 and use them. */ | |
805 if (streq (this_file, "-")) | |
806 { | |
807 while (!feof (stdin)) | |
808 { | |
809 (void) readline (&filename_lb, stdin); | |
810 if (strlen (filename_lb.buffer) > 0) | |
811 process_file (filename_lb.buffer); | |
812 } | |
813 } | |
814 else | |
815 process_file (this_file); | |
816 } | |
817 } | |
818 | |
819 if (emacs_tags_format) | |
820 { | |
401 | 821 while (nincluded_files-- > 0) |
822 fprintf (outf, "\f\n%s,include\n", *included_files++); | |
823 | |
240 | 824 (void) fclose (outf); |
825 exit (0); | |
826 } | |
827 | |
828 if (cxref_style) | |
829 { | |
830 put_entries (head); | |
831 exit (GOOD); | |
832 } | |
621 | 833 if (update) |
240 | 834 { |
621 | 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++) | |
240 | 838 { |
839 sprintf (cmd, | |
840 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS", | |
841 outfile, argv[i], outfile); | |
842 (void) system (cmd); | |
843 } | |
844 append_to_tagfile++; | |
845 } | |
846 outf = fopen (outfile, append_to_tagfile ? "a" : "w"); | |
847 if (outf == NULL) | |
848 { | |
849 perror (outfile); | |
850 exit (GOOD); | |
851 } | |
852 put_entries (head); | |
853 (void) fclose (outf); | |
854 if (update) | |
855 { | |
856 sprintf (cmd, "sort %s -o %s", outfile, outfile); | |
857 (void) system (cmd); | |
858 } | |
859 exit (GOOD); | |
860 } | |
861 | |
862 | |
863 /* | |
864 * This routine is called on each file argument. | |
865 */ | |
866 void | |
867 process_file (file) | |
868 char *file; | |
869 { | |
870 struct stat stat_buf; | |
871 | |
872 stat (file, &stat_buf); | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
873 if (!(stat_buf.st_mode & S_IFREG) |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
874 #ifdef S_IFLNK |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
875 || !(stat_buf.st_mode & S_IFLNK) |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
876 #endif |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
877 ) |
240 | 878 { |
879 fprintf (stderr, "Skipping %s: it is not a regular file.\n", file); | |
880 return; | |
881 } | |
882 | |
883 if (streq (file, outfile) && !streq (outfile, "-")) | |
884 { | |
885 fprintf (stderr, "Skipping inclusion of %s in self.\n", file); | |
886 return; | |
887 } | |
888 if (emacs_tags_format) | |
889 { | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
890 char *cp = etags_rindex (file, '/'); |
240 | 891 if (cp) |
892 ++cp; | |
893 else | |
894 cp = file; | |
895 } | |
896 find_entries (file); | |
897 if (emacs_tags_format) | |
898 { | |
899 fprintf (outf, "\f\n%s,%d\n", | |
900 file, total_size_of_entries (head)); | |
901 put_entries (head); | |
902 free_tree (head); | |
903 head = NULL; | |
904 } | |
905 } | |
906 | |
907 /* | |
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. | |
914 */ | |
915 void | |
916 init () | |
917 { | |
918 reg char *sp; | |
919 reg int i; | |
920 | |
921 for (i = 0; i < 0177; i++) | |
922 { | |
923 _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE; | |
924 _gd[i] = TRUE; | |
925 } | |
926 for (sp = white; *sp; sp++) | |
927 _wht[*sp] = TRUE; | |
928 for (sp = endtk; *sp; sp++) | |
929 _etk[*sp] = TRUE; | |
930 for (sp = intk; *sp; sp++) | |
931 _itk[*sp] = TRUE; | |
932 for (sp = begtk; *sp; sp++) | |
933 _btk[*sp] = TRUE; | |
934 for (sp = notgd; *sp; sp++) | |
935 _gd[*sp] = FALSE; | |
936 _wht[0] = _wht['\n']; | |
937 _etk[0] = _etk['\n']; | |
938 _btk[0] = _btk['\n']; | |
939 _itk[0] = _itk['\n']; | |
940 _gd[0] = _gd['\n']; | |
941 } | |
942 | |
943 /* | |
944 * This routine opens the specified file and calls the function | |
945 * which finds the function and type definitions. | |
946 */ | |
947 void | |
948 find_entries (file) | |
949 char *file; | |
950 { | |
951 char *cp; | |
952 void prolog_funcs (); | |
953 | |
954 inf = fopen (file, "r"); | |
955 if (inf == NULL) | |
956 { | |
957 perror (file); | |
958 return; | |
959 } | |
960 curfile = savestr (file); | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
961 cp = etags_rindex (file, '.'); |
240 | 962 |
963 header_file = (cp && (streq (cp + 1, "h"))); | |
964 | |
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"))) | |
968 { | |
969 TEX_funcs (inf); | |
970 goto close_and_return; | |
971 } | |
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"))) | |
979 { | |
980 L_funcs (inf); | |
981 goto close_and_return; | |
982 } | |
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)))) | |
996 { | |
997 Scheme_funcs (inf); | |
998 fclose (inf); | |
999 return; | |
1000 } | |
1001 /* Assume that ".s" or ".a" is assembly code. -wolfgang. */ | |
1002 if (cp && (cp[1] == 's' || cp[1] == 'a') && cp[2] == '\0') | |
1003 { | |
1004 Asm_funcs (inf); | |
1005 fclose (inf); | |
1006 return; | |
1007 } | |
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"))) | |
1014 { | |
1015 C_entries (C_PLPL); /* C++ */ | |
1016 goto close_and_return; | |
1017 } | |
1018 /* .cs or .hs: a C* file */ | |
1019 if (cp && (cp[1] == 'c' || cp[1] == 'h') && cp[2] == 's' && cp[3] == '\0') | |
1020 { | |
1021 C_entries (C_STAR); | |
1022 goto close_and_return; | |
1023 } | |
1024 /* .pl implies prolog source code */ | |
1025 if (cp && !strcmp (cp + 1, "pl")) | |
1026 { | |
1027 prolog_funcs (inf); | |
1028 goto close_and_return; | |
1029 } | |
1030 /* .p or .pas: a Pascal file */ | |
1031 if (cp && (streq (cp + 1, "p") | |
1032 || streq (cp + 1, "pas"))) | |
1033 { | |
1034 PAS_funcs (inf); | |
1035 goto close_and_return; | |
1036 } | |
1796
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1037 /* If .f or .for, assume it is fortran or nothing. */ |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1038 if (cp && (streq (cp + 1, "f") || streq (cp + 1, "for"))) |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1039 { |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1040 PF_funcs (inf); |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1041 goto close_and_return; |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1042 } |
240 | 1043 /* if not a .c or .h or .y file, try fortran */ |
1796
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1044 if (cp && ((cp[1] != 'c' |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1045 && cp[1] != 'h' |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1046 && cp[1] != 'y') |
00beac79bcc7
(find_entries): If filename ends in .f or .for, don't try anything but Fortran.
Richard M. Stallman <rms@gnu.org>
parents:
1040
diff
changeset
|
1047 || (cp[1] != 0 && cp[2] != 0))) |
240 | 1048 { |
1049 if (PF_funcs (inf) != 0) | |
1050 goto close_and_return; | |
1051 rewind (inf); /* no fortran tags found, try C */ | |
1052 } | |
1053 C_entries (cplusplus ? C_PLPL : 0); | |
1054 | |
1055 close_and_return: | |
1056 (void) fclose (inf); | |
1057 } | |
1058 | |
1059 /* Nonzero if string STR is composed of digits. */ | |
1060 | |
1061 int | |
1062 string_numeric_p (str) | |
1063 char *str; | |
1064 { | |
1065 while (*str) | |
1066 { | |
1067 if (*str < '0' || *str > '9') | |
1068 return 0; | |
1069 } | |
1070 return 1; | |
1071 } | |
1072 | |
1073 /* Record a tag. */ | |
1074 /* Should take a TOKEN* instead!! */ | |
1075 | |
1076 void | |
1077 pfnote (name, is_func, rewritten, linestart, linelen, lno, cno) | |
1078 char *name; /* tag name */ | |
1079 logical is_func; /* function or type name? */ | |
1080 logical rewritten; /* tag different from text of definition? */ | |
1081 char *linestart; | |
1082 int linelen; | |
1083 int lno; | |
1084 long cno; | |
1085 { | |
1086 register char *fp; | |
1087 register NODE *np; | |
1088 char tem[51]; | |
1089 char c; | |
1090 | |
1091 np = (NODE *) malloc (sizeof (NODE)); | |
1092 if (np == NULL) | |
1093 { | |
1094 if (!emacs_tags_format) | |
1095 { | |
1096 /* It's okay to output early in etags -- it only disrupts the | |
1097 * character count of the tag entries, which is no longer used | |
1098 * by tags.el anyway. | |
1099 */ | |
1100 error ("too many entries to sort"); | |
1101 } | |
1102 put_entries (head); | |
1103 free_tree (head); | |
1104 head = NULL; | |
1105 np = xnew (1, NODE); | |
1106 } | |
1107 /* If ctags mode, change name "main" to M<thisfilename>. */ | |
1108 if (!emacs_tags_format && !cxref_style && streq (name, "main")) | |
1109 { | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
1110 fp = etags_rindex (curfile, '/'); |
240 | 1111 name = concat ("M", fp == 0 ? curfile : fp + 1, ""); |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
1112 fp = etags_rindex (name, '.'); |
240 | 1113 if (fp && fp[1] != '\0' && fp[2] == '\0') |
1114 *fp = 0; | |
1115 rewritten = TRUE; | |
1116 } | |
1117 np->name = savestr (name); | |
1118 np->file = curfile; | |
1119 np->is_func = is_func; | |
1120 np->rewritten = rewritten; | |
1121 np->lno = lno; | |
1122 /* UNCOMMENT THE +1 HERE: */ | |
1123 np->cno = cno /* + 1 */ ; /* our char numbers are 0-base; emacs's are 1-base */ | |
1124 np->left = np->right = 0; | |
1125 if (emacs_tags_format) | |
1126 { | |
1127 c = linestart[linelen]; | |
1128 linestart[linelen] = 0; | |
1129 } | |
1130 else if (cxref_style == 0) | |
1131 { | |
1132 sprintf (tem, strlen (linestart) < 50 ? "%s$" : "%.50s", linestart); | |
1133 linestart = tem; | |
1134 } | |
1135 np->pat = savestr (linestart); | |
1136 if (emacs_tags_format) | |
1137 { | |
1138 linestart[linelen] = c; | |
1139 } | |
1140 | |
1141 add_node (np, &head); | |
1142 } | |
1143 | |
1144 /* | |
1145 * free_tree () | |
1146 * recurse on left children, iterate on right children. | |
1147 */ | |
1148 void | |
1149 free_tree (node) | |
1150 register NODE *node; | |
1151 { | |
1152 while (node) | |
1153 { | |
1154 register NODE *node_right = node->right; | |
1155 free_tree (node->left); | |
1156 free (node->name); | |
1157 free (node->pat); | |
1158 free ((char *) node); | |
1159 node = node_right; | |
1160 } | |
1161 } | |
1162 | |
1163 /* | |
1164 * add_node () | |
1165 * Adds a node to the tree of nodes. In etags mode, we don't keep | |
1166 * it sorted; we just keep a linear list. In ctags mode, maintain | |
1167 * an ordered tree, with no attempt at balancing. | |
1168 * | |
1169 * add_node is the only function allowed to add nodes, so it can | |
1170 * maintain state. | |
1171 */ | |
1172 void | |
1173 add_node (node, cur_node_p) | |
1174 NODE *node, **cur_node_p; | |
1175 { | |
1176 register int dif; | |
1177 register NODE *cur_node = *cur_node_p; | |
1178 static NODE *last_node = NULL;/* careful */ | |
1179 | |
1180 if (cur_node == NULL) | |
1181 { | |
1182 *cur_node_p = node; | |
1183 last_node = node; | |
1184 return; | |
1185 } | |
1186 | |
1187 if (emacs_tags_format) | |
1188 { | |
1189 /* Etags Mode */ | |
1190 if (!last_node) | |
1191 fatal ("internal error in add_node"); | |
1192 last_node->right = node; | |
1193 last_node = node; | |
1194 } | |
1195 else | |
1196 { | |
1197 /* Ctags Mode */ | |
1198 dif = strcmp (node->name, cur_node->name); | |
1199 | |
1200 /* | |
1201 * If this tag name matches an existing one, then | |
1202 * do not add the node, but maybe print a warning. | |
1203 */ | |
1204 if (!dif) | |
1205 { | |
1206 if (node->file == cur_node->file) | |
1207 { | |
1208 if (!no_warnings) | |
1209 { | |
1210 fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n", | |
1211 node->file, lineno, node->name); | |
1212 fprintf (stderr, "Second entry ignored\n"); | |
1213 } | |
1214 return; | |
1215 } | |
1216 if (!cur_node->been_warned && !no_warnings) | |
1217 { | |
1218 fprintf (stderr, | |
1219 "Duplicate entry in files %s and %s: %s (Warning only)\n", | |
1220 node->file, cur_node->file, node->name); | |
1221 } | |
1222 cur_node->been_warned = TRUE; | |
1223 return; | |
1224 } | |
1225 | |
1226 /* Maybe refuse to add duplicate nodes. */ | |
1227 if (!permit_duplicates) | |
1228 { | |
1229 if (!strcmp (node->name, cur_node->name) | |
1230 && !strcmp (node->file, cur_node->file)) | |
1231 return; | |
1232 } | |
1233 | |
1234 /* Actually add the node */ | |
1235 add_node (node, dif < 0 ? &cur_node->left : &cur_node->right); | |
1236 } | |
1237 } | |
1238 | |
1239 void | |
1240 put_entries (node) | |
1241 reg NODE *node; | |
1242 { | |
1243 reg char *sp; | |
1244 | |
1245 if (node == NULL) | |
1246 return; | |
1247 | |
1248 /* Output subentries that precede this one */ | |
1249 put_entries (node->left); | |
1250 | |
1251 /* Output this entry */ | |
1252 | |
1253 if (emacs_tags_format) | |
1254 { | |
1255 if (node->rewritten) | |
1256 { | |
1257 fprintf (outf, "%s\177%s\001%d,%d\n", | |
1258 node->name, node->pat, node->lno, node->cno); | |
1259 } | |
1260 else | |
1261 { | |
1262 fprintf (outf, "%s\177%d,%d\n", | |
1263 node->pat, node->lno, node->cno); | |
1264 } | |
1265 } | |
1266 else if (!cxref_style) | |
1267 { | |
1268 fprintf (outf, "%s\t%s\t", | |
1269 node->name, node->file); | |
1270 | |
1271 if (node->is_func) | |
1272 { /* a function */ | |
1273 putc (searchar, outf); | |
1274 putc ('^', outf); | |
1275 | |
1276 for (sp = node->pat; *sp; sp++) | |
1277 { | |
1278 if (*sp == '\\' || *sp == searchar) | |
1279 putc ('\\', outf); | |
1280 putc (*sp, outf); | |
1281 } | |
1282 putc (searchar, outf); | |
1283 } | |
1284 else | |
1285 { /* a typedef; text pattern inadequate */ | |
1286 fprintf (outf, "%d", node->lno); | |
1287 } | |
1288 putc ('\n', outf); | |
1289 } | |
1290 else if (vgrind_style) | |
1291 fprintf (stdout, "%s %s %d\n", | |
1292 node->name, node->file, (node->lno + 63) / 64); | |
1293 else | |
727 | 1294 fprintf (stdout, "%-16s %3d %-16s %s\n", |
240 | 1295 node->name, node->lno, node->file, node->pat); |
1296 | |
1297 /* Output subentries that follow this one */ | |
1298 put_entries (node->right); | |
1299 } | |
1300 | |
1301 /* Length of a number's decimal representation. */ | |
1302 int | |
1303 number_len (num) | |
1304 long num; | |
1305 { | |
1306 int len = 0; | |
1307 if (!num) | |
1308 return 1; | |
1309 for (; num; num /= 10) | |
1310 ++len; | |
1311 return len; | |
1312 } | |
1313 | |
1314 /* | |
1315 * Return total number of characters that put_entries will output for | |
1316 * the nodes in the subtree of the specified node. Works only if emacs_tags_format | |
1317 * is set, but called only in that case. This count is irrelevant with | |
1318 * the new tags.el, but is still supplied for backward compatibility. | |
1319 */ | |
1320 int | |
1321 total_size_of_entries (node) | |
1322 reg NODE *node; | |
1323 { | |
1324 reg int total; | |
1325 | |
1326 if (node == NULL) | |
1327 return 0; | |
1328 | |
1329 total = 0; | |
1330 for (; node; node = node->right) | |
1331 { | |
1332 /* Count left subentries. */ | |
1333 total += total_size_of_entries (node->left); | |
1334 | |
1335 /* Count this entry */ | |
1336 total += strlen (node->pat) + 1; | |
1337 total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1; | |
1338 if (node->rewritten) | |
1339 total += 1 + strlen (node->name); /* \001name */ | |
1340 } | |
1341 | |
1342 return total; | |
1343 } | |
1344 | |
1345 /* | |
1346 * The C symbol tables. | |
1347 */ | |
1348 | |
1349 Stab *C_stab, *C_PLPL_stab, *C_STAR_stab; | |
1350 | |
1351 /* | |
1352 * SYNOPSIS | |
1353 * Stab *get_C_stab (int c_ext); | |
1354 */ | |
1355 #define get_C_stab(c_ext) ((c_ext&C_STAR) ? C_STAR_stab : \ | |
1356 c_ext ? C_PLPL_stab : \ | |
1357 C_stab) | |
1358 | |
1359 void | |
1360 add_keyword (stab, sym, type) | |
1361 Stab *stab; | |
1362 char *sym; | |
1363 enum sym_type type; | |
1364 { | |
1365 stab_search (stab, sym, strlen (sym))->type = type; | |
1366 } | |
1367 | |
1368 Stab * | |
1369 C_create_stab (c_ext) | |
1370 int c_ext; | |
1371 { | |
1372 Stab *stab; | |
1373 | |
1374 stab = stab_create (); | |
1375 | |
1376 /* C, C++ and C* */ | |
1377 if (c_ext & C_PLPL) | |
1378 add_keyword (stab, "class", st_C_struct); | |
1379 if (c_ext & C_STAR) | |
1380 add_keyword (stab, "domain", st_C_struct); | |
1381 add_keyword (stab, "union", st_C_struct); | |
1382 add_keyword (stab, "struct", st_C_struct); | |
1383 add_keyword (stab, "enum", st_C_enum); | |
1384 add_keyword (stab, "typedef", st_C_typedef); | |
1385 add_keyword (stab, "define", st_C_define); | |
1386 add_keyword (stab, "long", st_C_typespec); | |
1387 add_keyword (stab, "short", st_C_typespec); | |
1388 add_keyword (stab, "int", st_C_typespec); | |
1389 add_keyword (stab, "char", st_C_typespec); | |
1390 add_keyword (stab, "float", st_C_typespec); | |
1391 add_keyword (stab, "double", st_C_typespec); | |
1392 add_keyword (stab, "signed", st_C_typespec); | |
1393 add_keyword (stab, "unsigned", st_C_typespec); | |
1394 add_keyword (stab, "const", st_C_typespec); | |
1395 add_keyword (stab, "volatile", st_C_typespec); | |
1396 | |
1397 return stab; | |
1398 } | |
1399 | |
1400 void | |
1401 C_create_stabs () | |
1402 { | |
1403 C_stab = C_create_stab (0); | |
1404 C_PLPL_stab = C_create_stab (C_PLPL); | |
1405 C_STAR_stab = C_create_stab (C_STAR | C_PLPL); | |
1406 } | |
1407 | |
1408 /* | |
1409 * C_entries () | |
1410 * This routine finds functions and typedefs in C syntax and adds them | |
1411 * to the list. | |
1412 */ | |
1413 | |
1414 #define CNL_SAVE_DEFINEDEF \ | |
1415 { \ | |
1416 prev_linepos = linepos; \ | |
1417 SET_FILEPOS (linepos, inf, charno); \ | |
1418 lineno++; \ | |
1419 charno += readline (&lb, inf); \ | |
1420 lp = lb.buffer; \ | |
1421 } | |
1422 | |
1423 #define CNL \ | |
1424 { \ | |
1425 CNL_SAVE_DEFINEDEF; \ | |
1426 definedef = dnone; \ | |
1427 } | |
1428 | |
1429 void | |
1430 C_entries (c_ext) | |
1431 int c_ext; /* extension of C? */ | |
1432 { | |
1433 register int c; /* latest char read; '\0' for end of line */ | |
1434 register int tokoff; /* offset in line of beginning of latest token */ | |
1435 register int toklen; /* length of latest token */ | |
1436 register char *lp; /* pointer one beyond the character `c' */ | |
1437 logical incomm, inquote, inchar, midtoken; | |
1438 int level; /* current curly brace level */ | |
1439 char tokb[BUFSIZ]; | |
1440 | |
1441 lineno = 0; | |
1442 charno = 0; | |
1443 lp = lb.buffer; | |
1444 *lp = 0; | |
1445 | |
1446 definedef = dnone; | |
1447 gotone = midtoken = inquote = inchar = incomm = FALSE; | |
1448 level = 0; | |
621 | 1449 tydef = none; |
1450 next_token_is_func = 0; | |
240 | 1451 |
1452 C_create_stabs (); | |
1453 | |
1454 while (!feof (inf)) | |
1455 { | |
1456 c = *lp++; | |
1457 if (c == '\\') | |
1458 { | |
621 | 1459 /* If we're at the end of the line, the next character is a |
1460 '\0'; don't skip it, because it's the thing that tells us | |
1461 to read the next line. */ | |
401 | 1462 if (*lp == 0) |
1463 continue; | |
1464 lp++; | |
240 | 1465 c = ' '; |
1466 } | |
1467 else if (incomm) | |
1468 { | |
401 | 1469 if (c == '*' && *lp == '/') |
240 | 1470 { |
401 | 1471 c = *lp++; |
1472 incomm = FALSE; | |
240 | 1473 } |
1474 } | |
1475 else if (inquote) | |
1476 { | |
1477 if (c == '"') | |
1478 inquote = FALSE; | |
727 | 1479 else if (c == '\\') |
1480 c = *lp++; | |
240 | 1481 } |
1482 else if (inchar) | |
1483 { | |
1484 if (c == '\'') | |
1485 inchar = FALSE; | |
1486 continue; | |
1487 } | |
1488 else | |
1489 switch (c) | |
1490 { | |
1491 case '"': | |
1492 inquote = TRUE; | |
1493 continue; | |
1494 case '\'': | |
1495 inchar = TRUE; | |
1496 continue; | |
1497 case '/': | |
1498 if (*lp == '*') | |
1499 { | |
1500 lp++; | |
1501 incomm = TRUE; | |
1502 } | |
1503 else if (c_ext && *lp == '/') | |
1504 { | |
727 | 1505 c = 0; |
1506 break; | |
240 | 1507 } |
1508 continue; | |
1509 case '#': | |
1510 if (lp == lb.buffer + 1 && definedef == dnone) | |
1511 definedef = dsharpseen; | |
1512 continue; | |
1513 | |
1514 /* | |
1515 * The next two are to help the strucdef state machine. | |
1516 * They break when they are finished, so they don't interfere | |
1517 * with anything else that is going on. | |
1518 */ | |
1519 case ':': | |
1520 if (structdef == stagseen) | |
1521 structdef = scolonseen; | |
1522 break; | |
1523 /* Not a struct definition when semicolon seen in non-sinbody context. */ | |
1524 case ';': | |
1525 if (structdef != snone && structdef != sinbody) | |
1526 { | |
1527 structdef = snone; | |
1528 (void) strcpy (structtag, "<error 1>"); | |
1529 } | |
1530 break; | |
1531 | |
1532 case '{': | |
1533 if (tydef == begin) | |
1534 { | |
1535 tydef = middle; | |
1536 } | |
1537 switch (structdef) | |
1538 { | |
1539 case skeyseen: /* unnamed struct */ | |
1540 structtag[0] = '\0'; | |
1541 /* FALLTHRU */ | |
1542 case stagseen: | |
1543 case scolonseen: /* named struct */ | |
1544 structdef = sinbody; | |
1545 break; | |
1546 } | |
1547 level++; | |
1548 continue; | |
1549 case '}': | |
1550 if (!noindentypedefs && lp == lb.buffer + 1) | |
1551 level = 0; /* reset level if first column */ | |
1552 else if (level > 0) | |
1553 level--; | |
1554 if (level == 0 && tydef == middle) | |
1555 { | |
1556 tydef = end; | |
1557 } | |
1558 if (level == 0) | |
1559 { | |
1560 structdef = snone; | |
1561 (void) strcpy (structtag, "<error 2>"); | |
1562 } | |
1563 continue; | |
1564 } | |
1565 if (LEVEL_OK_FOR_FUNCDEF () && !inquote && !incomm && gotone == FALSE) | |
1566 { | |
1567 if (midtoken) | |
1568 { | |
1569 if (endtoken (c)) | |
1570 { | |
1571 if (c_ext && c == ':' && *lp == ':' && intoken (*(lp + 1))) | |
1572 { | |
1573 /* | |
1574 * This handles :: in the middle, but not at beginning | |
1575 * of an identifier. | |
1576 */ | |
1577 lp += 2; | |
1578 toklen += 3; | |
1579 } | |
1580 else | |
1581 { | |
401 | 1582 /* The following is no longer true, |
1583 now that we advance to the next line | |
1584 at the end of processing the character. */ | |
240 | 1585 /* |
1586 * We've just finished lexing an identifier. | |
1587 * Note that if `c' is '\0', `lb' is the NEXT | |
1588 * line, `lp' points to the beginning of it, and | |
1589 * old pointers into `lb.buffer' may no longer be | |
1590 * valid, since `lb.buffer' may have been | |
1591 * reallocated. In this case (which corresponds | |
1592 * to an identifier followed immediately by a | |
1593 * newline), we re-read the line into lb1. | |
1594 * | |
1595 * This would be faster if the previous line's | |
1596 * buffer were always saved. | |
1597 */ | |
1598 logical is_func; | |
1599 char *tok_linebuf; | |
1600 TOKEN tok; | |
1601 logical bingo, tok_at_end_of_line; | |
1602 char *lp_tmp; /* addressable */ | |
1603 | |
401 | 1604 #if 0 |
240 | 1605 if (c == '\0') |
1606 { | |
1607 getline (GET_COOKIE (prev_linepos)); | |
1608 tok_linebuf = lb1.buffer; | |
1609 tok_at_end_of_line = TRUE; | |
1610 tok.linestart = prev_linepos; | |
1611 tok.lineno = lineno - 1; | |
1612 } | |
1613 else | |
401 | 1614 #endif |
240 | 1615 { |
1616 tok_linebuf = lb.buffer; | |
1617 tok_at_end_of_line = FALSE; | |
1618 tok.linestart = linepos; | |
1619 tok.lineno = lineno; | |
1620 } | |
1621 tok.p = tok_linebuf + tokoff; | |
1622 tok.len = toklen; | |
1623 tok.rewritten = FALSE; | |
1624 lp_tmp = lp; | |
1625 bingo = consider_token (c, &lp_tmp, &tok, | |
1626 &is_func, c_ext, level); | |
1627 lp = lp_tmp; | |
1628 if (bingo) | |
1629 { | |
1630 if (GET_CHARNO (tok.linestart) != GET_CHARNO (linepos) | |
1631 && !tok_at_end_of_line) | |
1632 { | |
1633 /* | |
1634 * Resynchronize tok.p to point into the right | |
1635 * linebuffer. | |
1636 */ | |
1637 getline (GET_COOKIE (tok.linestart)); | |
1638 if (!tok.rewritten) | |
1639 tok.p = lb1.buffer + (tok.p - tok_linebuf); | |
1640 tok_linebuf = lb1.buffer; | |
1641 } | |
1642 if (structdef == sinbody && definedef == dnone && is_func) | |
1643 { /* function defined in C++ class body */ | |
1644 sprintf (tokb, "%s::%.*s", | |
1645 structtag[0] == '\0' ? "_anonymous_" | |
1646 : structtag, | |
1647 tok.len, tok.p); | |
1648 tok.rewritten = TRUE; | |
1649 } | |
1650 else | |
1651 { | |
1652 sprintf (tokb, "%.*s", tok.len, tok.p); | |
1653 } | |
1654 pfnote (tokb, is_func, tok.rewritten, tok_linebuf, | |
1655 tokoff + toklen + (tok_at_end_of_line ? 0 : 1), | |
1656 tok.lineno, GET_CHARNO (tok.linestart)); | |
1657 gotone = is_func; /* function */ | |
1658 } | |
1659 midtoken = FALSE; | |
1660 } | |
1661 } | |
1662 else if (intoken (c)) | |
1663 toklen++; | |
1664 } | |
1665 else if (begtoken (c)) | |
1666 { | |
1667 tokoff = lp - 1 - lb.buffer; | |
1668 toklen = 1; | |
1669 midtoken = TRUE; | |
1670 } | |
1671 } | |
401 | 1672 /* Detect end of line, after having handled the last token on the line. */ |
1673 if (c == 0) | |
1674 { | |
1675 CNL; | |
1676 gotone = FALSE; | |
1677 } | |
240 | 1678 if (c == ';' && tydef == end) /* clean with typedefs */ |
1679 tydef = none; | |
1680 } | |
1681 } | |
1682 | |
1683 /* | |
1684 * consider_token () | |
1685 * checks to see if the current token is at the start of a | |
1686 * function, or corresponds to a typedef. It updates the input | |
1687 * line pointer *LPP so that the '(' will be in it when it returns. | |
1688 * | |
1689 * *IS_FUNC gets TRUE iff the token is a function. | |
1690 * C_EXT is which language we are looking at. | |
1691 * | |
1692 * In the future we will need some way to adjust where the end of | |
1693 * the token is; for instance, implementing the C++ keyword | |
1694 * `operator' properly will adjust the end of the token to be after | |
1695 * whatever follows `operator'. | |
1696 * | |
1697 * Globals | |
1698 * structdef IN OUT | |
1699 * definedef IN OUT | |
1700 * tydef IN OUT | |
1701 */ | |
1702 | |
1703 logical | |
1704 consider_token (c, lpp, tokp, is_func, c_ext, level) | |
1705 reg char c; /* IN: first char after the token */ | |
1706 char **lpp; /* IN OUT: *lpp points to 2nd char after the token */ | |
1707 reg TOKEN *tokp; /* IN */ | |
1708 logical *is_func; /* OUT */ | |
1709 int c_ext; /* IN */ | |
1710 int level; /* IN */ | |
1711 { | |
1712 reg char *lp = *lpp; | |
1713 logical firsttok; /* TRUE if have seen first token in ()'s */ | |
1714 Stab_entry *tokse = stab_find (get_C_stab (c_ext), tokp->p, tokp->len); | |
1715 enum sym_type toktype = stab_type (tokse); | |
1716 | |
1717 *is_func = TRUE; /* a function */ | |
1718 | |
1719 /* | |
1720 * Advance the definedef state machine. We set `gotone' for good measure; | |
1721 * it's redundant. | |
1722 */ | |
1723 switch (definedef) | |
1724 { | |
1725 case dnone: | |
1726 /* We're not on a preprocessor line. */ | |
1727 break; | |
1728 case dsharpseen: | |
1729 if (toktype == st_C_define) | |
1730 { | |
1731 definedef = ddefineseen; | |
1732 gotone = FALSE; | |
1733 } | |
1734 else | |
1735 { | |
1736 definedef = dignorerest; | |
1737 gotone = TRUE; | |
1738 } | |
1739 goto badone; | |
1740 case ddefineseen: | |
1741 /* | |
1742 * Make a tag for any macro. | |
1743 * This will flub up if there is a newline immediately following | |
1744 * the macro name. | |
1745 */ | |
1746 *is_func = (c == '('); | |
1747 definedef = dignorerest; | |
1748 gotone = TRUE; | |
1749 if (!*is_func && !constantypedefs) | |
1750 goto badone; | |
1751 goto goodone; | |
1752 case dignorerest: | |
1753 goto badone; | |
1754 default: | |
1755 error ("internal error: definedef value"); | |
1756 } | |
1757 | |
1758 /* | |
1759 * Skip whitespace and comments after the token. This loop should | |
1760 * also skip C++ comments. | |
1761 */ | |
1762 while (1) | |
1763 { | |
1764 /* At whitespace => skip it. */ | |
1765 if (iswhite (c)) | |
1766 { | |
1767 c = *lp++; | |
1768 } | |
1769 /* At a comment => skip to end of comment. */ | |
1770 else if (c == '/' && *lp == '*') | |
1771 { | |
1772 /* If we find a comment, skip it. */ | |
1773 while (!(c == '*' && *lp == '/')) | |
1774 { | |
1775 c = *lp++; | |
1776 if (c == 0) | |
1777 { | |
401 | 1778 lp--; |
1779 break; | |
240 | 1780 } |
1781 } | |
1782 if (c == '*' && *lp == '/') | |
1783 { | |
1784 lp++; /* lp now points past the '/' */ | |
1785 c = *lp++; /* c is now the --whatever-- after the '/' */ | |
1786 } | |
1787 } | |
1788 else | |
1789 break; | |
1790 | |
1791 /* If we arrived at eof or eol, decide which one it is. | |
1792 If it's eol, advance to the next line. */ | |
1793 | |
1794 if (c == 0) | |
1795 { | |
401 | 1796 lp--; |
1797 break; | |
240 | 1798 } |
1799 } | |
1800 | |
1801 /* | |
1802 * If you have custom token types, or when configuration files can | |
1803 * define custom token types, this switch will be larger. | |
1804 */ | |
1805 switch (toktype) | |
1806 { | |
1807 case st_C_typedef: | |
1808 if (typedefs) | |
1809 { | |
1810 tydef = begin; | |
1811 goto badone; | |
1812 } | |
1813 break; | |
1814 case st_C_typespec: | |
1815 if (tydef == begin || tydef == end) | |
1816 { | |
1817 tydef = end; | |
1818 goto badone; | |
1819 } | |
1820 break; | |
1821 } | |
1822 | |
1823 /* | |
1824 * This structdef business is currently only invoked when level==0. | |
1825 * It should be recursively invoked whatever the level, and a stack of | |
1826 * states kept, to allow for definitions of structs within structs. | |
1827 * | |
1828 * This structdef business is NOT invoked when we are ctags and the | |
1829 * file is plain C. This is because a struct tag may have the same | |
1830 * name as another tag, and this loses with ctags. | |
1831 * | |
1832 * This if statement deals with the tydef state machine as follows: if | |
1833 * tydef==begin and token is struct/union/class/enum, goto badone. | |
1834 * All the other code here is for the structdef state machine. | |
1835 */ | |
1836 switch (toktype) | |
1837 { | |
1838 case st_C_struct: | |
1839 case st_C_enum: | |
1840 if (tydef == begin || (typedefs_and_cplusplus && level == 0 && structdef == snone)) | |
1841 { | |
1842 structdef = skeyseen; | |
1843 structkey = tokse; | |
1844 } | |
1845 goto badone; | |
1846 } | |
1847 | |
1848 if (structdef == skeyseen) | |
1849 { | |
1850 /* If next char is '{' or (for C++) ':', found a structure tag. */ | |
1851 if (c == '{' || (c_ext && c == ':')) | |
1852 { | |
1853 /* | |
1854 * We should do this slightly differently for straight C: | |
1855 * instead of defining `tag', as we now do, we should define | |
1856 * `struct tag'. (Do this only if the find-tag defaulting is | |
1857 * done on a sophisticated per-mode basis, so that if the user | |
1858 * says meta-. anywhere in `struct foo', the default comes out | |
1859 * `struct foo', not `struct' or `foo'.) This will require | |
1860 * remembering which keyword (struct/union/class/enum) we saw, as a | |
1861 * Stab_entry* -- this will also make it possible to merge the | |
1862 * skeyseen and senumseen states, if we want. | |
1863 */ | |
1864 if (stab_type (structkey) == st_C_struct) | |
1865 { | |
1866 (void) strncpy (structtag, tokp->p, tokp->len); | |
1867 structtag[tokp->len] = '\0'; /* for struct/union/class */ | |
1868 structdef = stagseen; | |
1869 } | |
1870 else | |
1871 { | |
1872 structtag[0] = '\0'; /* for enum */ | |
1873 } | |
1874 *is_func = FALSE; /* not a function */ | |
1875 goto goodone; | |
1876 } | |
1877 else | |
1878 { | |
1879 /* Not a definition: reset structdef */ | |
1880 structdef = snone; | |
1881 (void) strcpy (structtag, "<error 3>"); | |
1882 } | |
1883 /* Now what? And how does/should this stuff interact with tydef?? */ | |
1884 /* Also maybe reset lp to *lpp for benefit of the function finding code. */ | |
1885 } | |
1886 if (tydef == begin) | |
1887 { | |
1888 tydef = end; | |
1889 goto badone; | |
1890 } | |
1891 if (tydef == end) | |
1892 { | |
1893 *is_func = 0; | |
1894 goto goodone; | |
1895 } | |
1896 /* Detect GNUmacs's function-defining macros. */ | |
621 | 1897 if (definedef == dnone) |
240 | 1898 { |
727 | 1899 if (strneq (tokp->p, "DEF", 3) |
1900 || strneq (tokp->p, "ENTRY", 5) | |
1901 || strneq (tokp->p, "SYSCALL", 7) | |
1902 || strneq (tokp->p, "PSEUDO", 6)) | |
621 | 1903 { |
1904 next_token_is_func = TRUE; | |
1905 goto badone; | |
1906 } | |
1907 else if (strneq (tokp->p, "EXFUN", 5)) | |
1908 { | |
1909 next_token_is_func = FALSE; | |
1910 goto badone; | |
1911 } | |
240 | 1912 } |
1913 if (next_token_is_func) | |
1914 { | |
1915 next_token_is_func = FALSE; | |
1916 goto goodone; | |
1917 } | |
1918 if (c != '(') | |
1919 goto badone; | |
1920 firsttok = FALSE; | |
1921 while ((c = *lp++) != ')') | |
1922 { | |
1923 if (c == 0) | |
1924 { | |
401 | 1925 lp--; |
1926 break; | |
240 | 1927 } |
1928 /* | |
1929 * This line used to confuse ctags: | |
1930 * int (*oldhup)(); | |
1931 * This fixes it. A nonwhite char before the first | |
1932 * token, other than a / (in case of a comment in there) | |
1933 * makes this not a declaration. | |
1934 */ | |
1935 if (begtoken (c) || c == '/') | |
1936 firsttok++; | |
1937 else if (!iswhite (c) && !firsttok) | |
1938 goto badone; | |
1939 } | |
1940 while (iswhite (c = *lp++)) | |
1941 { | |
1942 if (c == 0) | |
1943 { | |
401 | 1944 lp--; |
1945 break; | |
240 | 1946 } |
1947 } | |
1948 if (!isgood (c)) | |
1949 goto badone; | |
1950 | |
1951 goodone: | |
1952 *lpp = lp - 1; | |
1953 return TRUE; | |
1954 | |
1955 badone: | |
1956 *lpp = lp - 1; | |
1957 return FALSE; | |
1958 } | |
1959 | |
1960 void | |
1961 getline (atcookie) | |
1962 long atcookie; | |
1963 { | |
1964 long saveftell = ftell (inf); | |
1965 | |
1966 (void) fseek (inf, atcookie, 0); | |
1967 (void) readline (&lb1, inf); | |
1968 (void) fseek (inf, saveftell, 0); | |
1969 } | |
1970 | |
1971 /* Fortran parsing */ | |
1972 | |
1973 char *dbp; | |
1974 int pfcnt; | |
1975 | |
1976 int | |
1977 PF_funcs (fi) | |
1978 FILE *fi; | |
1979 { | |
1980 lineno = 0; | |
1981 charno = 0; | |
1982 pfcnt = 0; | |
1983 | |
1984 while (!feof (fi)) | |
1985 { | |
1986 lineno++; | |
1987 linecharno = charno; | |
1988 charno += readline (&lb, fi); | |
1989 dbp = lb.buffer; | |
1990 if (*dbp == '%') | |
1991 dbp++; /* Ratfor escape to fortran */ | |
1992 while (isspace (*dbp)) | |
1993 dbp++; | |
1994 if (*dbp == 0) | |
1995 continue; | |
1996 switch (*dbp | ' ') | |
1997 { | |
1998 case 'i': | |
1999 if (tail ("integer")) | |
2000 takeprec (); | |
2001 break; | |
2002 case 'r': | |
2003 if (tail ("real")) | |
2004 takeprec (); | |
2005 break; | |
2006 case 'l': | |
2007 if (tail ("logical")) | |
2008 takeprec (); | |
2009 break; | |
2010 case 'c': | |
2011 if (tail ("complex") || tail ("character")) | |
2012 takeprec (); | |
2013 break; | |
2014 case 'd': | |
2015 if (tail ("double")) | |
2016 { | |
2017 while (isspace (*dbp)) | |
2018 dbp++; | |
2019 if (*dbp == 0) | |
2020 continue; | |
2021 if (tail ("precision")) | |
2022 break; | |
2023 continue; | |
2024 } | |
2025 break; | |
2026 } | |
2027 while (isspace (*dbp)) | |
2028 dbp++; | |
2029 if (*dbp == 0) | |
2030 continue; | |
2031 switch (*dbp | ' ') | |
2032 { | |
2033 case 'f': | |
2034 if (tail ("function")) | |
2035 getit (); | |
2036 continue; | |
2037 case 's': | |
2038 if (tail ("subroutine")) | |
2039 getit (); | |
2040 continue; | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2041 case 'e': |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2042 if (tail ("entry")) |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2043 getit (); |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2044 continue; |
240 | 2045 case 'p': |
2046 if (tail ("program")) | |
2047 { | |
2048 getit (); | |
2049 continue; | |
2050 } | |
2051 if (tail ("procedure")) | |
2052 getit (); | |
2053 continue; | |
2054 } | |
2055 } | |
2056 return (pfcnt); | |
2057 } | |
2058 | |
2059 logical | |
2060 tail (cp) | |
2061 char *cp; | |
2062 { | |
2063 register int len = 0; | |
2064 | |
2065 while (*cp && (*cp & ~' ') == ((*(dbp + len)) & ~' ')) | |
2066 cp++, len++; | |
2067 if (*cp == 0) | |
2068 { | |
2069 dbp += len; | |
2070 return (1); | |
2071 } | |
2072 return (0); | |
2073 } | |
2074 | |
2075 void | |
2076 takeprec () | |
2077 { | |
2078 while (isspace (*dbp)) | |
2079 dbp++; | |
2080 if (*dbp != '*') | |
2081 return; | |
2082 dbp++; | |
2083 while (isspace (*dbp)) | |
2084 dbp++; | |
2085 if (!isdigit (*dbp)) | |
2086 { | |
2087 --dbp; /* force failure */ | |
2088 return; | |
2089 } | |
2090 do | |
2091 dbp++; | |
2092 while (isdigit (*dbp)); | |
2093 } | |
2094 | |
2095 void | |
2096 getit () | |
2097 { | |
2098 register char *cp; | |
2099 char c; | |
2100 char nambuf[BUFSIZ]; | |
2101 | |
2102 while (isspace (*dbp)) | |
2103 dbp++; | |
727 | 2104 if (*dbp == 0 |
2105 || (!isalpha (*dbp) | |
2106 && *dbp != '_' | |
2107 && *dbp != '$')) | |
240 | 2108 return; |
2109 for (cp = dbp + 1; *cp && (isalpha (*cp) || isdigit (*cp) | |
2110 || (*cp == '_') || (*cp == '$')); cp++) | |
2111 continue; | |
2112 c = cp[0]; | |
2113 cp[0] = 0; | |
2114 (void) strcpy (nambuf, dbp); | |
2115 cp[0] = c; | |
2116 pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
2117 pfcnt++; | |
2118 } | |
2119 | |
2120 /* Handle a file of assembler code. */ | |
2121 | |
2122 void | |
2123 Asm_funcs (fi) | |
2124 FILE *fi; | |
2125 { | |
2126 int i; | |
2127 register char c; | |
2128 | |
2129 lineno = 0; | |
2130 charno = 0; | |
2131 pfcnt = 0; | |
2132 | |
2133 while (!feof (fi)) | |
2134 { | |
2135 lineno++; | |
2136 linecharno = charno; | |
2137 charno += readline (&lb, fi); | |
2138 dbp = lb.buffer; | |
2139 | |
2140 for (i = 0; ((c = dbp[i]) && !isspace (c)) && (c != ':'); i++) | |
2141 ; | |
2142 | |
2143 if ((i > 0) && (c == ':')) | |
2144 getit (); | |
2145 } | |
2146 } | |
2147 | |
2148 /* Added by Mosur Mohan, 4/22/88 */ | |
2149 /* Pascal parsing */ | |
2150 | |
2151 #define GET_NEW_LINE \ | |
2152 { \ | |
2153 linecharno = charno; lineno++; \ | |
2154 charno += 1 + readline (&lb, inf); \ | |
2155 dbp = lb.buffer; \ | |
2156 } | |
2157 | |
2158 /* Locates tags for procedures & functions. | |
2159 * Doesn't do any type- or var-definitions. | |
2160 * It does look for the keyword "extern" or "forward" | |
2161 * immediately following the procedure statement; | |
2162 * if found, the tag is skipped. | |
2163 */ | |
2164 | |
2165 void | |
2166 PAS_funcs (fi) | |
2167 FILE *fi; | |
2168 { | |
2169 struct linebuffer tline; /* mostly copied from C_entries */ | |
2170 long save_lcno; | |
2171 int save_lineno; | |
2172 char c, *cp; | |
2173 char nambuf[BUFSIZ]; | |
2174 | |
2175 logical /* each of these flags is TRUE iff: */ | |
2176 incomm1, /* point is inside {..} comment */ | |
2177 incomm2, /* point is inside (*..*) comment */ | |
2178 inquote, /* point is inside '..' string */ | |
2179 get_tagname, /* point is after PROCEDURE/FUNCTION */ | |
2180 /* keyword, so next item = potential tag */ | |
2181 found_tag, /* point is after a potential tag */ | |
2182 inparms, /* point is within parameter-list */ | |
2183 verify_tag; /* point has passed the parm-list, so the */ | |
2184 /* next token will determine whether */ | |
2185 /* this is a FORWARD/EXTERN to be */ | |
2186 /* ignored, or whether it is a real tag */ | |
2187 | |
2188 lineno = 0; | |
2189 charno = 0; | |
2190 dbp = lb.buffer; | |
2191 *dbp = 0; | |
2192 initbuffer (&tline); | |
2193 | |
2194 incomm1 = incomm2 = inquote = FALSE; | |
2195 found_tag = FALSE; /* have a proc name; check if extern */ | |
2196 get_tagname = FALSE; /* have found "procedure" keyword */ | |
2197 inparms = FALSE; /* found '(' after "proc" */ | |
2198 verify_tag = FALSE; /* check if "extern" is ahead */ | |
2199 | |
2200 /* long main loop to get next char */ | |
2201 while (!feof (fi)) | |
2202 { | |
2203 c = *dbp++; | |
2204 if (c == 0) /* if end of line */ | |
2205 { | |
2206 GET_NEW_LINE; | |
2207 if (*dbp == 0) | |
2208 continue; | |
2209 if (!((found_tag && verify_tag) || | |
2210 get_tagname)) | |
2211 c = *dbp++; /* only if don't need *dbp pointing */ | |
2212 /* to the beginning of the name of */ | |
2213 /* the procedure or function */ | |
2214 } | |
2215 if (incomm1) /* within { - } comments */ | |
2216 { | |
2217 if (c == '}') | |
2218 incomm1 = FALSE; | |
2219 continue; | |
2220 } | |
2221 else if (incomm2) /* within (* - *) comments */ | |
2222 { | |
2223 if (c == '*') | |
2224 { | |
2225 while ((c = *dbp++) == '*') | |
2226 continue; | |
2227 if (c == 0) | |
2228 GET_NEW_LINE; | |
2229 if (c == ')') | |
2230 incomm2 = FALSE; | |
2231 } | |
2232 continue; | |
2233 } | |
2234 else if (inquote) | |
2235 { | |
2236 if (c == '\'') | |
2237 inquote = FALSE; | |
2238 continue; | |
2239 } | |
2240 else | |
2241 switch (c) | |
2242 { | |
2243 case '\'': | |
2244 inquote = TRUE; /* found first quote */ | |
2245 continue; | |
2246 case '{': /* found open-{-comment */ | |
2247 incomm1 = TRUE; | |
2248 continue; | |
2249 case '(': | |
2250 if (*dbp == '*') /* found open-(*-comment */ | |
2251 { | |
2252 incomm2 = TRUE; | |
2253 dbp++; | |
2254 } | |
2255 else if (found_tag) /* found '(' after tag, i.e., parm-list */ | |
2256 inparms = TRUE; | |
2257 continue; | |
2258 case ')': /* end of parms list */ | |
2259 if (inparms) | |
2260 inparms = FALSE; | |
2261 continue; | |
2262 case ';': | |
2263 if ((found_tag) && (!inparms)) /* end of proc or fn stmt */ | |
2264 { | |
2265 verify_tag = TRUE; | |
2266 break; | |
2267 } | |
2268 continue; | |
2269 } | |
2270 if ((found_tag) && (verify_tag) && (*dbp != ' ')) | |
2271 { | |
2272 /* check if this is an "extern" declaration */ | |
2273 if (*dbp == 0) | |
2274 continue; | |
2275 if ((*dbp == 'e') || (*dbp == 'E')) | |
2276 { | |
2277 if (tail ("extern")) /* superfluous, really! */ | |
2278 { | |
2279 found_tag = FALSE; | |
2280 verify_tag = FALSE; | |
2281 } | |
2282 } | |
2283 else if ((*dbp == 'f') || (*dbp == 'F')) | |
2284 { | |
2285 if (tail ("forward")) /* check for forward reference */ | |
2286 { | |
2287 found_tag = FALSE; | |
2288 verify_tag = FALSE; | |
2289 } | |
2290 } | |
2291 if ((found_tag) && (verify_tag)) /* not external proc, so make tag */ | |
2292 { | |
2293 found_tag = FALSE; | |
2294 verify_tag = FALSE; | |
2295 pfnote (nambuf, TRUE, FALSE, | |
2296 tline.buffer, cp - tline.buffer + 1, | |
2297 save_lineno, save_lcno); | |
2298 continue; | |
2299 } | |
2300 } | |
2301 if (get_tagname) /* grab name of proc or fn */ | |
2302 { | |
2303 if (*dbp == 0) | |
2304 continue; | |
2305 | |
2306 /* save all values for later tagging */ | |
2307 tline.size = lb.size; | |
2308 strcpy (tline.buffer, lb.buffer); | |
2309 save_lineno = lineno; | |
2310 save_lcno = linecharno; | |
2311 | |
2312 /* grab block name */ | |
2313 for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++) | |
2314 continue; | |
2315 c = cp[0]; | |
2316 cp[0] = 0; | |
2317 strcpy (nambuf, dbp); | |
2318 cp[0] = c; | |
2319 dbp = cp; /* restore dbp to e-o-token */ | |
2320 get_tagname = FALSE; | |
2321 found_tag = TRUE; | |
2322 continue; | |
2323 | |
2324 /* and proceed to check for "extern" */ | |
2325 } | |
2326 if ((!incomm1) && (!incomm2) && (!inquote) && | |
2327 (!found_tag) && (!get_tagname)) | |
2328 { | |
2329 /* check for proc/fn keywords */ | |
2330 switch (c | ' ') | |
2331 { | |
2332 case 'p': | |
2333 if (tail ("rocedure")) /* c = 'p', dbp has advanced */ | |
2334 get_tagname = TRUE; | |
2335 continue; | |
2336 case 'f': | |
2337 if (tail ("unction")) | |
2338 get_tagname = TRUE; | |
2339 continue; | |
2340 } | |
2341 } | |
2342 } /* while not e-o-f */ | |
2343 } | |
2344 | |
2345 /* | |
2346 * lisp tag functions | |
2347 * just look for (def or (DEF | |
2348 */ | |
2349 | |
2350 void | |
2351 L_funcs (fi) | |
2352 FILE *fi; | |
2353 { | |
2354 lineno = 0; | |
2355 charno = 0; | |
2356 pfcnt = 0; | |
2357 | |
2358 while (!feof (fi)) | |
2359 { | |
2360 lineno++; | |
2361 linecharno = charno; | |
2362 charno += readline (&lb, fi); | |
2363 dbp = lb.buffer; | |
2364 if (dbp[0] == '(') | |
2365 { | |
2366 if (L_isdef (dbp)) | |
2367 { | |
2368 while (!isspace (*dbp)) | |
2369 dbp++; | |
2370 while (isspace (*dbp)) | |
2371 dbp++; | |
2372 L_getit (); | |
2373 } | |
2374 else | |
2375 { | |
2376 /* Check for (foo::defmumble name-defined ... */ | |
2377 while (*dbp && *dbp != ':' && !isspace (*dbp) | |
2378 && *dbp != '(' && *dbp != ')') | |
2379 dbp++; | |
2380 if (*dbp == ':') | |
2381 { | |
2382 while (*dbp == ':') | |
2383 dbp++; | |
2384 | |
2385 if (L_isdef (dbp)) | |
2386 { | |
2387 while (!isspace (*dbp)) | |
2388 dbp++; | |
2389 while (isspace (*dbp)) | |
2390 dbp++; | |
2391 L_getit (); | |
2392 } | |
2393 } | |
2394 } | |
2395 } | |
2396 } | |
2397 } | |
2398 | |
2399 int | |
2400 L_isdef (dbp) | |
2401 char *dbp; | |
2402 { | |
2403 return ((dbp[1] == 'D' || dbp[1] == 'd') && | |
2404 (dbp[2] == 'E' || dbp[2] == 'e') && | |
2405 (dbp[3] == 'F' || dbp[3] == 'f')); | |
2406 } | |
2407 | |
2408 void | |
2409 L_getit () | |
2410 { | |
2411 register char *cp; | |
2412 char c; | |
2413 char nambuf[BUFSIZ]; | |
2414 | |
2415 if (*dbp == 0) | |
2416 return; | |
2417 for (cp = dbp + 1; *cp && *cp != '(' && *cp != ' '; cp++) | |
2418 continue; | |
2419 c = cp[0]; | |
2420 cp[0] = 0; | |
2421 (void) strcpy (nambuf, dbp); | |
2422 cp[0] = c; | |
2423 pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
2424 pfcnt++; | |
2425 } | |
2426 | |
2427 /* | |
2428 * Scheme tag functions | |
2429 * look for (def... xyzzy | |
2430 * look for (def... (xyzzy | |
2431 * look for (def ... ((...(xyzzy .... | |
2432 * look for (set! xyzzy | |
2433 */ | |
2434 | |
2435 static void get_scheme (); | |
2436 | |
2437 void | |
2438 Scheme_funcs (fi) | |
2439 FILE *fi; | |
2440 { | |
2441 lineno = 0; | |
2442 charno = 0; | |
2443 pfcnt = 0; | |
2444 | |
2445 while (!feof (fi)) | |
2446 { | |
2447 lineno++; | |
2448 linecharno = charno; | |
2449 charno += readline (&lb, fi); | |
2450 dbp = lb.buffer; | |
2451 if (dbp[0] == '(' && | |
2452 (dbp[1] == 'D' || dbp[1] == 'd') && | |
2453 (dbp[2] == 'E' || dbp[2] == 'e') && | |
2454 (dbp[3] == 'F' || dbp[3] == 'f')) | |
2455 { | |
2456 while (!isspace (*dbp)) | |
2457 dbp++; | |
2458 /* Skip over open parens and white space */ | |
2459 while (*dbp && (isspace (*dbp) || *dbp == '(')) | |
2460 dbp++; | |
2461 get_scheme (); | |
2462 } | |
2463 if (dbp[0] == '(' && | |
2464 (dbp[1] == 'S' || dbp[1] == 's') && | |
2465 (dbp[2] == 'E' || dbp[2] == 'e') && | |
2466 (dbp[3] == 'T' || dbp[3] == 't') && | |
2467 (dbp[4] == '!' || dbp[4] == '!') && | |
2468 (isspace (dbp[5]))) | |
2469 { | |
2470 while (!isspace (*dbp)) | |
2471 dbp++; | |
2472 /* Skip over white space */ | |
2473 while (isspace (*dbp)) | |
2474 dbp++; | |
2475 get_scheme (); | |
2476 } | |
2477 } | |
2478 } | |
2479 | |
2480 static void | |
2481 get_scheme () | |
2482 { | |
2483 register char *cp; | |
2484 char c; | |
2485 char nambuf[BUFSIZ]; | |
2486 | |
2487 if (*dbp == 0) | |
2488 return; | |
2489 /* Go till you get to white space or a syntactic break */ | |
2490 for (cp = dbp + 1; *cp && *cp != '(' && *cp != ')' && !isspace (*cp); cp++) | |
2491 continue; | |
2492 /* Null terminate the string there. */ | |
2493 c = cp[0]; | |
2494 cp[0] = 0; | |
2495 /* Copy the string */ | |
2496 strcpy (nambuf, dbp); | |
2497 /* Unterminate the string */ | |
2498 cp[0] = c; | |
2499 /* Announce the change */ | |
2500 pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno); | |
2501 pfcnt++; | |
2502 } | |
2503 | |
2504 /* Find tags in TeX and LaTeX input files. */ | |
2505 | |
2506 /* TEX_toktab is a table of TeX control sequences that define tags. | |
2507 Each TEX_tabent records one such control sequence. | |
2508 CONVERT THIS TO USE THE Stab TYPE!! */ | |
2509 | |
2510 struct TEX_tabent | |
2511 { | |
2512 char *name; | |
2513 int len; | |
2514 }; | |
2515 | |
2516 struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */ | |
2517 | |
2518 /* Default set of control sequences to put into TEX_toktab. | |
2519 The value of environment var TEXTAGS is prepended to this. */ | |
2520 | |
2521 static char *TEX_defenv = | |
2522 ":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout"; | |
2523 | |
2524 void TEX_mode (); | |
2525 struct TEX_tabent *TEX_decode_env (); | |
2526 void TEX_getit (); | |
2527 int TEX_Token (); | |
2528 | |
2529 static char TEX_esc = '\\'; | |
2530 static char TEX_opgrp = '{'; | |
2531 static char TEX_clgrp = '}'; | |
2532 | |
2533 /* | |
2534 * TeX/LaTeX scanning loop. | |
2535 */ | |
2536 | |
2537 void | |
2538 TEX_funcs (fi) | |
2539 FILE *fi; | |
2540 { | |
2541 char *lasthit; | |
2542 | |
2543 lineno = 0; | |
2544 charno = 0; | |
2545 pfcnt = 0; | |
2546 | |
2547 /* Select either \ or ! as escape character. */ | |
2548 TEX_mode (fi); | |
2549 | |
2550 /* Initialize token table once from environment. */ | |
2551 if (!TEX_toktab) | |
2552 TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv); | |
2553 | |
2554 while (!feof (fi)) | |
1040 | 2555 { /* Scan each line in file */ |
240 | 2556 lineno++; |
2557 linecharno = charno; | |
2558 charno += readline (&lb, fi); | |
2559 dbp = lb.buffer; | |
2560 lasthit = dbp; | |
1040 | 2561 while (dbp = etags_index (dbp, TEX_esc)) /* Look at each escape in line */ |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2562 { |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2563 register int i; |
240 | 2564 |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2565 if (!*(++dbp)) |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2566 break; |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2567 linecharno += dbp - lasthit; |
240 | 2568 lasthit = dbp; |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2569 i = TEX_Token (lasthit); |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2570 if (0 <= i) |
240 | 2571 { |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2572 TEX_getit (lasthit, TEX_toktab[i].len); |
1040 | 2573 break; /* We only save a line once */ |
240 | 2574 } |
2575 } | |
2576 } | |
2577 } | |
2578 | |
2579 #define TEX_LESC '\\' | |
2580 #define TEX_SESC '!' | |
2581 #define TEX_cmt '%' | |
2582 | |
2583 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */ | |
2584 /* chars accordingly. */ | |
2585 | |
2586 void | |
2587 TEX_mode (f) | |
2588 FILE *f; | |
2589 { | |
2590 int c; | |
2591 | |
2592 while ((c = getc (f)) != EOF) | |
2593 { | |
2594 /* Skip to next line if we hit the TeX comment char. */ | |
2595 if (c == TEX_cmt) | |
2596 while (c != '\n') | |
2597 c = getc (f); | |
2598 else if (c == TEX_LESC || c == TEX_SESC ) | |
2599 break; | |
2600 } | |
2601 | |
2602 if (c == TEX_LESC) | |
2603 { | |
2604 TEX_esc = TEX_LESC; | |
2605 TEX_opgrp = '{'; | |
2606 TEX_clgrp = '}'; | |
2607 } | |
2608 else | |
2609 { | |
2610 TEX_esc = TEX_SESC; | |
2611 TEX_opgrp = '<'; | |
2612 TEX_clgrp = '>'; | |
2613 } | |
2614 rewind (f); | |
2615 } | |
2616 | |
2617 /* Read environment and prepend it to the default string. */ | |
2618 /* Build token table. */ | |
2619 | |
2620 struct TEX_tabent * | |
2621 TEX_decode_env (evarname, defenv) | |
2622 char *evarname; | |
2623 char *defenv; | |
2624 { | |
2625 register char *env, *p; | |
2626 | |
2627 struct TEX_tabent *tab; | |
2628 int size, i; | |
2629 | |
2630 /* Append default string to environment. */ | |
2631 env = getenv (evarname); | |
2632 if (!env) | |
2633 env = defenv; | |
2634 else | |
2635 env = concat (env, defenv, ""); | |
2636 | |
2637 /* Allocate a token table */ | |
2638 for (size = 1, p = env; p;) | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2639 if ((p = etags_index (p, ':')) && *(++p)) |
240 | 2640 size++; |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2641 /* Add 1 to leave room for null terminator. */ |
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2642 tab = xnew (size + 1, struct TEX_tabent); |
240 | 2643 |
2644 /* Unpack environment string into token table. Be careful about */ | |
2645 /* zero-length strings (leading ':', "::" and trailing ':') */ | |
2646 for (i = 0; *env;) | |
2647 { | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2648 p = etags_index (env, ':'); |
240 | 2649 if (!p) /* End of environment string. */ |
2650 p = env + strlen (env); | |
2651 if (p - env > 0) | |
2652 { /* Only non-zero strings. */ | |
2653 tab[i].name = savenstr (env, p - env); | |
2654 tab[i].len = strlen (tab[i].name); | |
2655 i++; | |
2656 } | |
2657 if (*p) | |
2658 env = p + 1; | |
2659 else | |
2660 { | |
2661 tab[i].name = NULL; /* Mark end of table. */ | |
2662 tab[i].len = 0; | |
2663 break; | |
2664 } | |
2665 } | |
2666 return tab; | |
2667 } | |
2668 | |
2669 /* Record a tag defined by a TeX command of length LEN and starting at NAME. | |
2670 The name being defined actually starts at (NAME + LEN + 1). | |
2671 But we seem to include the TeX command in the tag name. */ | |
2672 | |
2673 void | |
2674 TEX_getit (name, len) | |
2675 char *name; | |
2676 int len; | |
2677 { | |
2678 char *p = name + len; | |
2679 char nambuf[BUFSIZ]; | |
2680 | |
2681 if (*name == 0) | |
2682 return; | |
2683 | |
2684 /* Let tag name extend to next group close (or end of line) */ | |
2685 while (*p && *p != TEX_clgrp) | |
2686 p++; | |
2687 (void) strncpy (nambuf, name, p - name); | |
2688 nambuf[p - name] = 0; | |
2689 | |
2690 pfnote (nambuf, TRUE, FALSE, lb.buffer, strlen (lb.buffer), lineno, linecharno); | |
2691 pfcnt++; | |
2692 } | |
2693 | |
2694 /* If the text at CP matches one of the tag-defining TeX command names, | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2695 return the etags_index of that command in TEX_toktab. |
240 | 2696 Otherwise return -1. */ |
2697 | |
2698 /* Keep the capital `T' in `Token' for dumb truncating compilers | |
2699 (this distinguishes it from `TEX_toktab' */ | |
2700 int | |
2701 TEX_Token (cp) | |
2702 char *cp; | |
2703 { | |
2704 int i; | |
2705 | |
2706 for (i = 0; TEX_toktab[i].len > 0; i++) | |
2707 if (strncmp (TEX_toktab[i].name, cp, TEX_toktab[i].len) == 0) | |
2708 return i; | |
2709 return -1; | |
2710 } | |
2711 | |
2712 /* Support for Prolog. */ | |
2713 | |
2714 /* whole head (not only functor, but also arguments) | |
2715 is gotten in compound term. */ | |
2716 | |
2717 void | |
2718 prolog_getit (s, lineno, linecharno) | |
2719 char *s; | |
2720 int lineno; | |
2721 long linecharno; | |
2722 { | |
2723 char nambuf[BUFSIZ], *save_s, tmpc; | |
2724 int insquote, npar; | |
2725 | |
2726 save_s = s; | |
2727 insquote = FALSE; | |
2728 npar = 0; | |
2729 while (1) | |
2730 { | |
2731 if (*s == '\0') /* syntax error. */ | |
2732 return; | |
2733 else if (insquote && *s == '\'' && *(s + 1) == '\'') | |
2734 s += 2; | |
2735 else if (*s == '\'') | |
2736 { | |
2737 insquote = !insquote; | |
2738 s++; | |
2739 } | |
2740 else if (!insquote && *s == '(') | |
2741 { | |
2742 npar++; | |
2743 s++; | |
2744 } | |
2745 else if (!insquote && *s == ')') | |
2746 { | |
2747 npar--; | |
2748 s++; | |
2749 if (npar == 0) | |
2750 break; | |
2751 else if (npar < 0) /* syntax error. */ | |
2752 return; | |
2753 } | |
2754 else if (!insquote && *s == '.' && (isspace (*(s + 1)) || *(s + 1) == '\0')) | |
2755 { /* fullstop. */ | |
2756 if (npar != 0) /* syntax error. */ | |
2757 return; | |
2758 s++; | |
2759 break; | |
2760 } | |
2761 else | |
2762 s++; | |
2763 } | |
2764 tmpc = *s; | |
2765 *s = '\0'; | |
2766 strcpy (nambuf, save_s); | |
2767 *s = tmpc; | |
2768 pfnote (nambuf, TRUE, save_s, strlen (nambuf), lineno, linecharno); | |
2769 } | |
2770 | |
2771 /* It is assumed that prolog predicate starts from column 0. */ | |
2772 | |
2773 void | |
2774 prolog_funcs (fi) | |
2775 FILE *fi; | |
2776 { | |
2777 void skip_comment (), prolog_getit (); | |
2778 | |
2779 lineno = linecharno = charno = 0; | |
2780 while (!feof (fi)) | |
2781 { | |
2782 lineno++; | |
2783 linecharno += charno; | |
2784 charno = readline (&lb, fi) + 1; /* 1 for newline. */ | |
2785 dbp = lb.buffer; | |
2786 if (isspace (dbp[0])) /* not predicate header. */ | |
2787 continue; | |
2788 else if (dbp[0] == '%') /* comment. */ | |
2789 continue; | |
2790 else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */ | |
2791 skip_comment (&lb, fi, &lineno, &linecharno); | |
2792 else /* found. */ | |
2793 prolog_getit (dbp, lineno, linecharno); | |
2794 } | |
2795 } | |
2796 | |
2797 void | |
2798 skip_comment (plb, fi, plineno, plinecharno) | |
2799 struct linebuffer *plb; | |
2800 FILE *fi; | |
2801 int *plineno; /* result */ | |
2802 long *plinecharno; /* result */ | |
2803 { | |
2804 while (!substr ("*/", plb->buffer)) | |
2805 { | |
2806 (*plineno)++; | |
2807 *plinecharno += readline (plb, fi) + 1; | |
2808 } /* 1 for newline. */ | |
2809 } | |
2810 | |
2811 /* Return TRUE if 'sub' exists somewhere in 's'. */ | |
2812 | |
2813 int | |
2814 substr (sub, s) | |
2815 char *sub; | |
2816 char *s; | |
2817 { | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2818 while (*s && (s = etags_index (s, *sub))) |
240 | 2819 if (prestr (sub, s)) |
2820 return (TRUE); | |
2821 else | |
2822 s++; | |
2823 return (FALSE); | |
2824 } | |
2825 | |
2826 /* Return TRUE if 'pre' is prefix of string 's'. */ | |
2827 | |
2828 int | |
2829 prestr (pre, s) | |
2830 char *pre; | |
2831 char *s; | |
2832 { | |
2833 if (*pre == '\0') | |
2834 return (TRUE); | |
2835 else if (*pre == *s) | |
2836 return (prestr (pre + 1, s + 1)); | |
2837 else | |
2838 return (FALSE); | |
2839 } | |
2840 | |
2841 /* Initialize a linebuffer for use */ | |
2842 | |
2843 void | |
2844 initbuffer (linebuffer) | |
2845 struct linebuffer *linebuffer; | |
2846 { | |
2847 linebuffer->size = 200; | |
2848 linebuffer->buffer = xnew (200, char); | |
2849 } | |
2850 | |
2851 /* | |
2852 * Read a line of text from `stream' into `linebuffer'. | |
2853 * Return the number of characters read from `stream', | |
2854 * which is the length of the line including the newline, if any. | |
2855 */ | |
2856 long | |
2857 readline (linebuffer, stream) | |
2858 struct linebuffer *linebuffer; | |
2859 register FILE *stream; | |
2860 { | |
2861 char *buffer = linebuffer->buffer; | |
2862 register char *p = linebuffer->buffer; | |
2863 register char *pend; | |
2864 int newline; /* 1 if ended with newline, 0 if ended with EOF */ | |
2865 | |
2866 pend = p + linebuffer->size; /* Separate to avoind 386/IX compiler bug. */ | |
2867 | |
2868 while (1) | |
2869 { | |
2870 register int c = getc (stream); | |
2871 if (p == pend) | |
2872 { | |
2873 linebuffer->size *= 2; | |
2874 buffer = (char *) xrealloc (buffer, linebuffer->size); | |
2875 p += buffer - linebuffer->buffer; | |
2876 pend = buffer + linebuffer->size; | |
2877 linebuffer->buffer = buffer; | |
2878 } | |
2879 if (c < 0 || c == '\n') | |
2880 { | |
2881 *p = 0; | |
2882 newline = (c == '\n' ? 1 : 0); | |
2883 break; | |
2884 } | |
2885 *p++ = c; | |
2886 } | |
2887 | |
2888 return p - buffer + newline; | |
2889 } | |
2890 | |
2891 char * | |
2892 savestr (cp) | |
2893 char *cp; | |
2894 { | |
2895 return savenstr (cp, strlen (cp)); | |
2896 } | |
2897 | |
2898 char * | |
2899 savenstr (cp, len) | |
2900 char *cp; | |
2901 int len; | |
2902 { | |
2903 register char *dp; | |
2904 | |
2905 dp = xnew (len + 1, char); | |
2906 (void) strncpy (dp, cp, len); | |
2907 dp[len] = '\0'; | |
2908 return dp; | |
2909 } | |
2910 | |
2911 /* | |
2912 * Return the ptr in sp at which the character c last | |
2913 * appears; NULL if not found | |
2914 * | |
2915 * Identical to v7 rindex, included for portability. | |
2916 */ | |
2917 | |
2918 char * | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2919 etags_rindex (sp, c) |
240 | 2920 register char *sp, c; |
2921 { | |
2922 register char *r; | |
2923 | |
2924 r = NULL; | |
2925 do | |
2926 { | |
2927 if (*sp == c) | |
2928 r = sp; | |
2929 } while (*sp++); | |
2930 return (r); | |
2931 } | |
2932 | |
774 | 2933 |
240 | 2934 /* |
2935 * Return the ptr in sp at which the character c first | |
2936 * appears; NULL if not found | |
2937 * | |
2938 * Identical to v7 index, included for portability. | |
2939 */ | |
2940 | |
2941 char * | |
1026
cc96e2df9b71
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
774
diff
changeset
|
2942 etags_index (sp, c) |
240 | 2943 register char *sp, c; |
2944 { | |
2945 do | |
2946 { | |
2947 if (*sp == c) | |
2948 return (sp); | |
2949 } while (*sp++); | |
2950 return (NULL); | |
2951 } | |
2952 | |
2953 /* Print error message and exit. */ | |
2954 | |
2955 /* VARARGS1 */ | |
2956 void | |
2957 fatal (s1, s2) | |
2958 char *s1, *s2; | |
2959 { | |
2960 error (s1, s2); | |
2961 exit (1); | |
2962 } | |
2963 | |
2964 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
2965 | |
2966 /* VARARGS1 */ | |
2967 void | |
2968 error (s1, s2) | |
2969 char *s1, *s2; | |
2970 { | |
2971 fprintf (stderr, "%s: ", progname); | |
2972 fprintf (stderr, s1, s2); | |
2973 fprintf (stderr, "\n"); | |
2974 } | |
2975 | |
2976 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */ | |
2977 | |
2978 char * | |
2979 concat (s1, s2, s3) | |
2980 char *s1, *s2, *s3; | |
2981 { | |
2982 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
2983 char *result = xnew (len1 + len2 + len3 + 1, char); | |
2984 | |
2985 (void) strcpy (result, s1); | |
2986 (void) strcpy (result + len1, s2); | |
2987 (void) strcpy (result + len1 + len2, s3); | |
2988 *(result + len1 + len2 + len3) = 0; | |
2989 | |
2990 return result; | |
2991 } | |
2992 | |
2993 /* Like malloc but get fatal error if memory is exhausted. */ | |
2994 | |
2995 char * | |
2996 xmalloc (size) | |
2997 int size; | |
2998 { | |
2999 char *result = malloc (size); | |
3000 if (!result) | |
3001 fatal ("virtual memory exhausted", 0); | |
3002 return result; | |
3003 } | |
3004 | |
3005 char * | |
3006 xrealloc (ptr, size) | |
3007 char *ptr; | |
3008 int size; | |
3009 { | |
3010 char *result = realloc (ptr, size); | |
3011 if (!result) | |
3012 fatal ("virtual memory exhausted"); | |
3013 return result; | |
3014 } |