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