etags.c 68.6 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Tags file maker to go with GNU Emacs
2
   Copyright (C) 1984, 1987, 1988, 1989, 1993 Free Software Foundation, Inc. and Ken Arnold
Jim Blandy's avatar
Jim Blandy committed
3

4
This file is not considered part of GNU Emacs.
Jim Blandy's avatar
Jim Blandy committed
5

6
This program is free software; you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
7
it under the terms of the GNU General Public License as published by
8 9
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
10

11
This program is distributed in the hope that it will be useful,
Jim Blandy's avatar
Jim Blandy committed
12 13 14 15 16
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
17 18
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
Jim Blandy's avatar
Jim Blandy committed
19 20 21 22 23 24 25 26

/*
 * Authors:
 *	Ctags originally by Ken Arnold.
 *	FORTRAN added by Jim Kleckner.
 *	Ed Pelegri-Llopart added C typedefs.
 *	Gnu Emacs TAGS format and modifications by RMS?
 *	Sam Kendall added C++.
27
 *
28
 *	Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer. 9.8
Jim Blandy's avatar
Jim Blandy committed
29 30
 */

31
#ifdef HAVE_CONFIG_H
32
#include <../src/config.h>
Roland McGrath's avatar
Roland McGrath committed
33 34
#endif

35 36 37 38 39
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>

40 41 42 43
#if !defined (S_ISREG) && defined (S_IFREG)
# define S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)
#endif

44 45
#include "getopt.h"

Jim Blandy's avatar
Jim Blandy committed
46 47 48 49 50
extern char *malloc (), *realloc ();
extern char *getenv ();
extern char *strcpy (), *strncpy ();
extern int strcmp ();

51 52
char *etags_index (), *etags_rindex ();
char *savenstr ();
Jim Blandy's avatar
Jim Blandy committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

/* Define the symbol ETAGS to make the program "etags",
 which makes emacs-style tag tables by default.
 Define CTAGS to make the program "ctags" compatible with the usual one.
 Define neither one to get behavior that depends
 on the name with which the program is invoked
 (but we don't normally compile it that way).  */

#if !defined(ETAGS) && !defined(CTAGS)
/* If neither is defined, program can be run as either. */
#define ETAGS
#define CTAGS
#endif

/* On VMS, CTAGS is not useful, so always do ETAGS.  */
#ifdef VMS
#ifndef ETAGS
#define ETAGS
#endif
#endif

/* Exit codes for success and failure.  */
#ifdef VMS
#define	GOOD	(1)
#define BAD	(0)
#else
#define	GOOD	(0)
#define	BAD	(1)
#endif

/*
 * The FILEPOS abstract type, which represents a position in a file,
 * plus the following accessor functions:
 *
 *	long GET_CHARNO (pos)
 *				returns absolute char number.
 *	void SET_FILEPOS (pos, fp, charno)
 *	    FILE *fp; long charno;
 *				sets `pos' from the current file
 *				position of `fp' and from `charno',
 *				which must be the absolute character
 *				number corresponding to the current
 *				position of `fp'.
 *
 * The `pos' parameter is an lvalue expression of type FILEPOS.
 * Parameters to the accessor functions are evaluated 0 or more times,
 * and so must have no side effects.
 *
 * FILEPOS objects can also be assigned and passed to and from
 * functions in the normal C manner.
 *
 * Implementation notes: the `+ 0' is to enforce rvalue-ness.
 */

#ifndef DEBUG
108
 /* real implementation */
Jim Blandy's avatar
Jim Blandy committed
109 110 111 112
typedef long FILEPOS;
#define GET_CHARNO(pos)	((pos) + 0)
#define SET_FILEPOS(pos, fp, cno)	((void) ((pos) = (cno)))
#else
113
 /* debugging implementation */
Jim Blandy's avatar
Jim Blandy committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
typedef struct
{
  long charno;
} FILEPOS;

#define GET_CHARNO(pos)	((pos).charno + 0)
#define SET_FILEPOS(pos, fp, cno)					\
    ((void) ((pos).charno = (cno),					\
	     (cno) != ftell (fp) ? (error ("SET_FILEPOS inconsistency"), 0) \
	     			 : 0))
#endif

#define streq(s, t)	(strcmp (s, t) == 0)
#define strneq(s, t, n)	(strncmp (s, t, n) == 0)
128
#define	logical		int
Jim Blandy's avatar
Jim Blandy committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

#define	TRUE	1
#define	FALSE	0

#define	iswhite(arg)	(_wht[arg])	/* T if char is white		*/
#define	begtoken(arg)	(_btk[arg])	/* T if char can start token	*/
#define	intoken(arg)	(_itk[arg])	/* T if char can be in token	*/
#define	endtoken(arg)	(_etk[arg])	/* T if char ends tokens	*/

#define	max(I1,I2)	((I1) > (I2) ? (I1) : (I2))

struct nd_st
{				/* sorting structure			*/
  char *name;			/* function or type name	*/
  char *file;			/* file name			*/
  logical is_func;		/* use pattern or line no	*/
145
  logical named;		/* list name separately		*/
Jim Blandy's avatar
Jim Blandy committed
146 147 148 149 150 151 152 153 154 155
  logical been_warned;		/* set if noticed dup		*/
  int lno;			/* line number tag is on	*/
  long cno;			/* character number line starts on */
  char *pat;			/* search pattern		*/
  struct nd_st *left, *right;	/* left and right sons		*/
};

long ftell ();
typedef struct nd_st NODE;

156 157 158
logical header_file;		/* TRUE if .h file, FALSE o.w.  */
/* boolean "functions" (see init)	*/
logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
Jim Blandy's avatar
Jim Blandy committed
159 160 161 162 163 164 165


char *concat ();
char *savenstr ();
char *savestr ();
char *xmalloc ();
char *xrealloc ();
166
int L_isdef (), L_isquote ();
Jim Blandy's avatar
Jim Blandy committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
int PF_funcs ();
int total_size_of_entries ();
logical consider_token ();
logical tail ();
long readline ();
void Asm_funcs ();
void C_entries ();
void L_funcs ();
void L_getit ();
void PAS_funcs ();
void Scheme_funcs ();
void TEX_funcs ();
void add_node ();
void error ();
void fatal ();
void find_entries ();
void free_tree ();
void getit ();
void getline ();
void init ();
void initbuffer ();
void initbuffer ();
void pfnote ();
void process_file ();
void put_entries ();
void takeprec ();

/*
 * MACRO
 *	xnew -- allocate storage
 *
 * SYNOPSIS
 *	Type *xnew (int n, Type);
 */
#define xnew(n, Type)	((Type *) xmalloc ((n) * sizeof (Type)))



/*
 *	Symbol table stuff.
 *
 * Should probably be implemented with hash table; linked list for now.
 */

enum sym_type
{
  st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
};

struct stab_entry
{
  char *sym;
  int symlen;
  enum sym_type type;
  struct stab_entry *next;
};

typedef struct stab_entry Stab_entry;
typedef Stab_entry *Stab;

/*
 * NAME
 *	Stab, Stab_entry, stab_create, stab_search, stab_find -- symbol table
 *
 * SYNOPSIS
 *	Types: Stab, Stab_entry, enum sym_type
 *
 *	Stab * stab_create ()
 *
 *	Stab_entry * stab_find (stab, sym)
 *	Stab *stab;
 *	char *sym;
 *
 *	Stab_entry * stab_search (stab, sym)
 *	Stab *stab;
 *	char *sym;
 *
 * DESCRIPTION
 *	stab_create creates a Stab, a symbol table object, and returns a
 *	pointer to it.  stab_find finds a symbol in a Stab; it returns a
 *	pointer to the Stab_entry if found, otherwise NULL.  stab_search
 *	is like stab_find, except that it creates a new Stab_entry,
 *	initialized with type = st_none, if one did not exist already
 *	(it never returns NULL).
 *
 *	A Stab_entry is a structure that contains at least the following
 *	members:
 *
 *		char *name;		// must not be modified
 *		enum sym_type type;	// should be set
 *
 *	The type field is initially set to st_none; it should be set to
 *	something else by the caller of stab_search.  Other possible values
 *	of an enum sym_type can be added.
 */

Stab *
stab_create ()
{
  Stab *sp;
  sp = xnew (1, Stab);
  *sp = NULL;			/* a Stab starts out as a null Stab_entry* */
  return sp;
}

Stab_entry *
stab_find (stab, sym, symlen)
     Stab *stab;
     register char *sym;
     register int symlen;
{
  register Stab_entry *se;
  for (se = *stab; se != NULL; se = se->next)
    {
      if (se->symlen == symlen && strneq (se->sym, sym, symlen))
	return se;
    }

  return NULL;
}

Stab_entry *
stab_search (stab, sym, symlen)
     register Stab *stab;
     char *sym;
     int symlen;
{
  register Stab_entry *se;
  se = stab_find (stab, sym, symlen);

  if (se == NULL)
    {
      /* make a new one */
      se = xnew (1, Stab_entry);
      se->sym = savenstr (sym, symlen);
      se->symlen = symlen;
      se->type = st_none;
      se->next = *stab;
      *stab = se;
    }

  return se;
}

/*
 * NAME
 *	stab_type -- type of a symbol table entry
 *
 * SYNOPSIS
 *	enum sym_type stab_type (Stab_entry *se);
 *
 * WARNING
 *	May evaluate its argument more than once.
 */

#define stab_type(se)	((se)==NULL ? st_none : (se)->type)



typedef int LINENO;

typedef struct
{
  char *p;
  int len;
  LINENO lineno;
333
  logical named;
Jim Blandy's avatar
Jim Blandy committed
334 335
} TOKEN;

336
/* C extensions.
Jim Blandy's avatar
Jim Blandy committed
337
 */
338 339 340
#define C_PLPL	0x00001		/* C++ */
#define C_STAR	0x00003		/* C* */
#define YACC	0x10000		/* yacc file */
Jim Blandy's avatar
Jim Blandy committed
341 342 343 344 345 346 347 348 349 350 351 352 353

char searchar = '/';		/* use /.../ searches 		*/

LINENO lineno;			/* line number of current line */
long charno;			/* current character number */

long linecharno;		/* charno of start of line; not used by C, but
				 * by every other language.
				 */

char *curfile,			/* current input file name		*/
 *outfile,			/* output file				*/
 *white = " \f\t\n",		/* white chars				*/
354 355
 *endtk = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?",	/* token ending chars	*/
				/* token starting chars			*/
356
 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
357 358
				  /* valid in-token chars		*/
 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
Jim Blandy's avatar
Jim Blandy committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

int append_to_tagfile;		/* -a: append to tags */
int emacs_tags_format;		/* emacs style output (no -e option any more) */
/* The following three default to 1 for etags, but to 0 for ctags.  */
int typedefs;			/* -t: create tags for typedefs */
int typedefs_and_cplusplus;	/* -T: create tags for typedefs, level */
				/* 0 struct/enum/union decls, and C++ */
				/* member functions */
int constantypedefs;		/* -d: create tags for C #define and enum */
				/* constants. Default under etags.  Enum */
				/* constants not implemented. */
				/* -D: opposite of -d.  Default under ctags. */
int update;			/* -u: update tags */
int vgrind_style;		/* -v: create vgrind style index output */
int no_warnings;		/* -w: suppress warnings */
int cxref_style;		/* -x: create cxref style output */
int cplusplus;			/* .[hc] means C++, not C */
int noindentypedefs;		/* -S: ignore indentation in C */

/* Name this program was invoked with.  */
char *progname;

Jim Blandy's avatar
Jim Blandy committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
struct option longopts[] = {
  { "append",			no_argument,	   NULL, 'a' },
  { "backward-search",		no_argument,	   NULL, 'B' }, 
  { "c++",			no_argument,	   NULL, 'C' },
  { "cxref",			no_argument,	   NULL, 'x' },
  { "defines",			no_argument,	   NULL, 'd' },
  { "forward-search",		no_argument,	   NULL, 'F' }, 
  { "help",			no_argument,	   NULL, 'H' },
  { "ignore-indentation",	no_argument,	   NULL, 'S' },
  { "include",			required_argument, NULL, 'i' },
  { "no-defines",		no_argument,	   NULL, 'D' },
  { "no-warn",			no_argument,	   NULL, 'w' },
  { "output",			required_argument, NULL, 'o' },
  { "typedefs",			no_argument,	   NULL, 't' },
  { "typedefs-and-c++",		no_argument,	   NULL, 'T' },
  { "update",			no_argument,	   NULL, 'u' }, 
  { "version",			no_argument,	   NULL, 'V' },
  { "vgrind",			no_argument,	   NULL, 'v' }, 
  { 0 }
};

Jim Blandy's avatar
Jim Blandy committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
FILE *inf,			/* ioptr for current input file		*/
 *outf;				/* ioptr for tags file			*/

NODE *head;			/* the head of the binary tree of tags	*/

int permit_duplicates = 1;	/* Nonzero means allow duplicate tags.  */

/* A `struct linebuffer' is a structure which holds a line of text.
 `readline' reads a line from a stream into a linebuffer
 and works regardless of the length of the line.  */

struct linebuffer
{
  long size;
  char *buffer;
};

struct linebuffer lb;		/* the current line */
struct linebuffer filename_lb;	/* used to read in filenames */
421 422 423 424 425
struct
{
  FILEPOS linepos;
  struct linebuffer lb;		/* used by C_entries instead of lb */
} lbs[2];
Jim Blandy's avatar
Jim Blandy committed
426

Jim Blandy's avatar
Jim Blandy committed
427 428 429 430 431 432 433 434 435 436 437 438
void
print_version ()
{
#ifdef CTAGS
  printf ("CTAGS ");
#ifdef ETAGS
  printf ("and ");
#endif
#endif
#ifdef ETAGS
  printf ("ETAGS ");
#endif
439
  printf ("for Emacs version 19.\n");
Jim Blandy's avatar
Jim Blandy committed
440 441 442 443 444 445 446 447 448 449

  exit (0);
}

void
print_help ()
{
  printf ("These are the options accepted by %s.  You may use unambiguous\n\
abbreviations for the long option names.\n\n", progname);

450 451 452
  puts ("-a, --append\n\
        Append tag entries to existing tags file.");
  puts ("-C, --c++\n\
Jim Blandy's avatar
Jim Blandy committed
453 454
        Treat files with `.c' and `.h' extensions as C++ code, not C\n\
        code.  Files with `.C', `.H', `.cxx', `.hxx', or `.cc'\n\
455 456
        extensions are always assumed to be C++ code.");
  fputs ("-d, --defines\n\
Jim Blandy's avatar
Jim Blandy committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        Create tag entries for #defines, too.", stdout);

#ifdef ETAGS
  fputs ("  This is the default\n\
        behavior.", stdout);
#endif

  fputs ("\n\
-D, --no-defines\n\
        Don't create tag entries for #defines.", stdout);

#ifdef CTAGS
  fputs ("  This is the default\n\
        behavior.", stdout);
#endif

  puts ("\n\
-o FILE, --output=FILE\n\
        Write the tags to FILE.\n\
-S, --ignore-indentation\n\
        Don't rely on indentation quite as much as normal.  Currently,\n\
        this means not to assume that a closing brace in the first\n\
        column is the final brace of a function or structure\n\
480 481
        definition.");
  puts ("-t, --typedefs\n\
Jim Blandy's avatar
Jim Blandy committed
482
        Generate tag entries for typedefs.  This is the default\n\
483 484
        behavior.");
  puts ("-T, --typedefs-and-c++\n\
Jim Blandy's avatar
Jim Blandy committed
485 486 487 488 489 490 491 492 493 494 495 496 497
        Generate tag entries for typedefs, struct/enum/union tags, and\n\
        C++ member functions.");

#ifdef ETAGS
  puts ("-i FILE, --include=FILE\n\
        Include a note in tag file indicating that, when searching for\n\
        a tag, one should also consult the tags file FILE after\n\
        checking the current file.");
#endif

#ifdef CTAGS
  puts ("-B, --backward-search\n\
        Write the search commands for the tag entries using '?', the\n\
498 499
        backward-search command.");
  puts ("-F, --forward-search\n\
Jim Blandy's avatar
Jim Blandy committed
500
        Write the search commands for the tag entries using '/', the\n\
501 502
        forward-search command.");
  puts ("-u, --update\n\
Jim Blandy's avatar
Jim Blandy committed
503 504 505 506 507
        Update the tag entries for the given files, leaving tag\n\
        entries for other files in place.  Currently, this is\n\
        implemented by deleting the existing entries for the given\n\
        files and then rewriting the new entries at the end of the\n\
        tags file.  It is often faster to simply rebuild the entire\n\
508 509
        tag file than to use this.");
  puts ("-v, --vgrind\n\
Jim Blandy's avatar
Jim Blandy committed
510 511
        Generates an index of items intended for human consumption,\n\
        similar to the output of vgrind.  The index is sorted, and\n\
512 513
        gives the page number of each item.");
  puts ("-x, --cxref\n\
Jim Blandy's avatar
Jim Blandy committed
514 515 516
        Like --vgrind, but in the style of cxref, rather than vgrind.\n\
        The output uses line numbers instead of page numbers, but\n\
        beyond that the differences are cosmetic; try both to see\n\
517 518
        which you like.");
  puts ("-w, --no-warn\n\
Jim Blandy's avatar
Jim Blandy committed
519 520 521 522 523 524 525 526 527 528 529 530 531
        Suppress warning messages about entries defined in multiple\n\
        files.");
#endif

  puts ("-V, --version\n\
        Print the version of the program.\n\
-H, --help\n\
        Print this help message.");

  exit (0);
}


Jim Blandy's avatar
Jim Blandy committed
532 533 534 535 536 537 538
void
main (argc, argv)
     int argc;
     char *argv[];
{
  char cmd[100];
  int i;
Roland McGrath's avatar
Roland McGrath committed
539
  unsigned int nincluded_files = 0;
540
  char **included_files = xnew (argc, char *);
Jim Blandy's avatar
Jim Blandy committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
  char *this_file;
#ifdef VMS
  char got_err;

  extern char *gfnames ();
  extern char *massage_name ();
#endif

  progname = argv[0];

#ifndef CTAGS
  emacs_tags_format = 1;
#else
  emacs_tags_format = 0;
#endif

  /*
   * If etags, always find typedefs and structure tags.  Why not?
   * Also default is to find macro constants.
   */
  if (emacs_tags_format)
    typedefs = typedefs_and_cplusplus = constantypedefs = 1;

Jim Blandy's avatar
Jim Blandy committed
564
  for (;;)
Jim Blandy's avatar
Jim Blandy committed
565
    {
Jim Blandy's avatar
Jim Blandy committed
566
      int opt;
567
      opt = getopt_long (argc, argv, "aCdDo:f:StTi:BFuvxwVH", longopts, 0);
Jim Blandy's avatar
Jim Blandy committed
568 569 570 571 572

      if (opt == EOF)
	break;

      switch (opt)
Jim Blandy's avatar
Jim Blandy committed
573
	{
Jim Blandy's avatar
Jim Blandy committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	case '\0':
	  /* If getopt returns '\0', then it has already processed a
	     long-named option.  We should do nothing.  */
	  break;

	  /* Common options. */
	case 'a':
	  append_to_tagfile++;
	  break;
	case 'C':
	  cplusplus = 1;
	  break;
	case 'd':
	  constantypedefs = 1;
	  break;
	case 'D':
	  constantypedefs = 0;
	  break;
592
	case 'f':
Jim Blandy's avatar
Jim Blandy committed
593 594
	case 'o':
	  if (outfile)
Jim Blandy's avatar
Jim Blandy committed
595
	    {
Jim Blandy's avatar
Jim Blandy committed
596
	      fprintf (stderr,
597
		       "%s: -%c flag may only be given once\n", progname, opt);
Jim Blandy's avatar
Jim Blandy committed
598 599
	      goto usage;
	    }
Jim Blandy's avatar
Jim Blandy committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	  outfile = optarg;
	  break;
	case 'S':
	  noindentypedefs++;
	  break;
	case 't':
	  typedefs++;
	  break;
	case 'T':
	  typedefs++;
	  typedefs_and_cplusplus++;
	  break;
	case 'V':
	  print_version ();
	  break;
	case 'H':
	  print_help ();
	  break;

	  /* Etags options */
	case 'i':
	  if (!emacs_tags_format)
	    goto usage;
	  included_files[nincluded_files++] = optarg;
	  break;

	  /* Ctags options. */
	case 'B':
	  searchar = '?';
	  if (emacs_tags_format)
	    goto usage;
	  break;
	case 'F':
	  searchar = '/';
	  if (emacs_tags_format)
	    goto usage;
	  break;
	case 'u':
	  update++;
	  if (emacs_tags_format)
	    goto usage;
	  break;
	case 'v':
	  vgrind_style++;
	  /*FALLTHRU*/
	case 'x':
	  cxref_style++;
	  if (emacs_tags_format)
	    goto usage;
	  break;
	case 'w':
	  no_warnings++;
	  if (emacs_tags_format)
	    goto usage;
	  break;

	default:
	  goto usage;
Jim Blandy's avatar
Jim Blandy committed
658 659 660
	}
    }

661
  if (optind == argc && nincluded_files == 0)
Jim Blandy's avatar
Jim Blandy committed
662
    {
Jim Blandy's avatar
Jim Blandy committed
663 664
      fprintf (stderr, "%s: No input files specified.\n", progname);

Jim Blandy's avatar
Jim Blandy committed
665
    usage:
666
      fprintf (stderr, "%s: Try `%s --help' for a complete list of options.\n",
Jim Blandy's avatar
Jim Blandy committed
667
	       progname, progname);
Jim Blandy's avatar
Jim Blandy committed
668 669 670 671 672 673 674 675 676 677 678
      exit (BAD);
    }

  if (outfile == 0)
    {
      outfile = emacs_tags_format ? "TAGS" : "tags";
    }

  init ();			/* set up boolean "functions"		*/

  initbuffer (&lb);
679 680
  initbuffer (&lbs[0].lb);
  initbuffer (&lbs[1].lb);
Jim Blandy's avatar
Jim Blandy committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
  initbuffer (&filename_lb);
  /*
   * loop through files finding functions
   */
  if (emacs_tags_format)
    {
      if (streq (outfile, "-"))
	outf = stdout;
      else
	outf = fopen (outfile, append_to_tagfile ? "a" : "w");
      if (!outf)
	{
	  perror (outfile);
	  exit (1);
	}
    }

#ifdef VMS
Jim Blandy's avatar
Jim Blandy committed
699 700 701
  argc -= optind;
  argv += optind;
  while (gfnames (&argc, &argv, &got_err) != NULL)
Jim Blandy's avatar
Jim Blandy committed
702 703 704 705 706 707 708 709 710 711 712
    {
      if (got_err)
	{
	  error ("Can't find file %s\n", this_file);
	  argc--, argv++;
	}
      else
	{
	  this_file = massage_name (this_file);
#if 0
	}
713
    }			/* solely to balance out the ifdef'd parens above */
Jim Blandy's avatar
Jim Blandy committed
714 715
#endif
#else
Jim Blandy's avatar
Jim Blandy committed
716
  for (; optind < argc; optind++)
Jim Blandy's avatar
Jim Blandy committed
717
    {
Jim Blandy's avatar
Jim Blandy committed
718
      this_file = argv[optind];
Jim Blandy's avatar
Jim Blandy committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
      if (1)
	{
#endif
	  /* Input file named "-" means read file names from stdin
	     and use them.  */
	  if (streq (this_file, "-"))
	    {
	      while (!feof (stdin))
		{
		  (void) readline (&filename_lb, stdin);
		  if (strlen (filename_lb.buffer) > 0)
		    process_file (filename_lb.buffer);
		}
	    }
	  else
	    process_file (this_file);
	}
    }

  if (emacs_tags_format)
    {
Roland McGrath's avatar
Roland McGrath committed
740 741 742
      while (nincluded_files-- > 0)
	fprintf (outf, "\f\n%s,include\n", *included_files++);

Jim Blandy's avatar
Jim Blandy committed
743 744 745 746 747 748 749 750 751
      (void) fclose (outf);
      exit (0);
    }

  if (cxref_style)
    {
      put_entries (head);
      exit (GOOD);
    }
Jim Blandy's avatar
Jim Blandy committed
752
  if (update)
Jim Blandy's avatar
Jim Blandy committed
753
    {
Jim Blandy's avatar
Jim Blandy committed
754 755 756
      /* update cannot be set under VMS, so we may assume that argc
	 and argv have not been munged.  */
      for (i = optind; i < argc; i++)
Jim Blandy's avatar
Jim Blandy committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	{
	  sprintf (cmd,
		   "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
		   outfile, argv[i], outfile);
	  (void) system (cmd);
	}
      append_to_tagfile++;
    }
  outf = fopen (outfile, append_to_tagfile ? "a" : "w");
  if (outf == NULL)
    {
      perror (outfile);
      exit (GOOD);
    }
  put_entries (head);
  (void) fclose (outf);
  if (update)
    {
      sprintf (cmd, "sort %s -o %s", outfile, outfile);
      (void) system (cmd);
    }
  exit (GOOD);
}


/*
 * This routine is called on each file argument.
 */
void
process_file (file)
     char *file;
{
  struct stat stat_buf;

791
  if (stat (file, &stat_buf) || !S_ISREG (stat_buf.st_mode))
Jim Blandy's avatar
Jim Blandy committed
792 793 794 795 796 797 798 799 800 801 802 803 804
    {
      fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
      return;
    }

  if (streq (file, outfile) && !streq (outfile, "-"))
    {
      fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
      return;
    }
  find_entries (file);
  if (emacs_tags_format)
    {
805
      fprintf (outf, "\f\n%s,%d\n", file, total_size_of_entries (head));
Jim Blandy's avatar
Jim Blandy committed
806 807 808 809 810 811 812
      put_entries (head);
      free_tree (head);
      head = NULL;
    }
}

/*
813
 * This routine sets up the boolean pseudo-functions which work
814
 * by setting boolean flags dependent upon the corresponding character
Jim Blandy's avatar
Jim Blandy committed
815 816 817 818 819 820 821 822
 * Every char which is NOT in that string is not a white char.  Therefore,
 * all of the array "_wht" is set to FALSE, and then the elements
 * subscripted by the chars in "white" are set to TRUE.  Thus "_wht"
 * of a char is TRUE if it is the string "white", else FALSE.
 */
void
init ()
{
823 824
  register char *sp;
  register int i;
Jim Blandy's avatar
Jim Blandy committed
825 826

  for (i = 0; i < 0177; i++)
827
    _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
Jim Blandy's avatar
Jim Blandy committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
  for (sp = white; *sp; sp++)
    _wht[*sp] = TRUE;
  for (sp = endtk; *sp; sp++)
    _etk[*sp] = TRUE;
  for (sp = intk; *sp; sp++)
    _itk[*sp] = TRUE;
  for (sp = begtk; *sp; sp++)
    _btk[*sp] = TRUE;
  _wht[0] = _wht['\n'];
  _etk[0] = _etk['\n'];
  _btk[0] = _btk['\n'];
  _itk[0] = _itk['\n'];
}

/*
 * This routine opens the specified file and calls the function
 * which finds the function and type definitions.
 */
void
find_entries (file)
     char *file;
{
  char *cp;
  void prolog_funcs ();

  inf = fopen (file, "r");
  if (inf == NULL)
    {
      perror (file);
      return;
    }
  curfile = savestr (file);
860
  cp = etags_rindex (file, '.');
Jim Blandy's avatar
Jim Blandy committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899

  header_file = (cp && (streq (cp + 1, "h")));

  /* .tex, .aux or .bbl implies LaTeX source code */
  if (cp && (streq (cp + 1, "tex") || streq (cp + 1, "aux")
	     || streq (cp + 1, "bbl")))
    {
      TEX_funcs (inf);
      goto close_and_return;
    }
  /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
  if (cp && (streq (cp + 1, "l")
	     || streq (cp + 1, "el")
	     || streq (cp + 1, "lsp")
	     || streq (cp + 1, "lisp")
	     || streq (cp + 1, "cl")
	     || streq (cp + 1, "clisp")))
    {
      L_funcs (inf);
      goto close_and_return;
    }
  /* .scm or .sm or .scheme or ... implies scheme source code */
  if (cp && (streq (cp + 1, "sm")
	     || streq (cp + 1, "scm")
	     || streq (cp + 1, "scheme")
	     || streq (cp + 1, "t")
	     || streq (cp + 1, "sch")
	     || streq (cp + 1, "SM")
	     || streq (cp + 1, "SCM")
	     /* The `SCM' or `scm' prefix with a version number */
             || (cp[-1] == 'm' && cp[-2] == 'c' && cp[-3] == 's'
		 && string_numeric_p (cp + 1))
             || (cp[-1] == 'M' && cp[-2] == 'C' && cp[-3] == 'S'
		 && string_numeric_p (cp + 1))))
    {
      Scheme_funcs (inf);
      fclose (inf);
      return;
    }
900 901 902 903 904
  /* Assume that ".s" or ".a" is assembly code. -wolfgang.
     Or even ".sa". */
  if (cp && (streq (cp + 1, "s")
	     || streq (cp + 1, "a")
	     || streq (cp + 1, "sa")))
Jim Blandy's avatar
Jim Blandy committed
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    {
      Asm_funcs (inf);
      fclose (inf);
      return;
    }
  /* .C or .H or .cxx or .hxx or .cc: a C++ file */
  if (cp && (streq (cp + 1, "C")
	     || streq (cp + 1, "H")
	     || streq (cp + 1, "cxx")
	     || streq (cp + 1, "hxx")
	     || streq (cp + 1, "cc")))
    {
      C_entries (C_PLPL);	/* C++ */
      goto close_and_return;
    }
  /* .cs or .hs: a C* file */
921 922
  if (cp && (streq (cp + 1, "cs")
	     || streq (cp + 1, "hs")))
Jim Blandy's avatar
Jim Blandy committed
923 924 925 926
    {
      C_entries (C_STAR);
      goto close_and_return;
    }
927 928 929 930 931 932
  /* .y: a yacc file */
  if (cp && (streq (cp + 1, "y")))
    {
      C_entries (YACC);
      goto close_and_return;
    }
Jim Blandy's avatar
Jim Blandy committed
933
  /* .pl implies prolog source code */
934
  if (cp && streq (cp + 1, "pl"))
Jim Blandy's avatar
Jim Blandy committed
935 936 937 938 939 940 941 942 943 944 945
    {
      prolog_funcs (inf);
      goto close_and_return;
    }
  /* .p or .pas: a Pascal file */
  if (cp && (streq (cp + 1, "p")
	     || streq (cp + 1, "pas")))
    {
      PAS_funcs (inf);
      goto close_and_return;
    }
946
  /* If .f or .for, assume it is fortran or nothing.  */
947 948
  if (cp && (streq (cp + 1, "f")
	     || streq (cp + 1, "for")))
949 950 951 952
    {
      PF_funcs (inf);
      goto close_and_return;
    }
Jim Blandy's avatar
Jim Blandy committed
953
  /* if not a .c or .h or .y file, try fortran */
954 955 956 957
  if (cp && ((cp[1] != 'c'
	      && cp[1] != 'h'
	      && cp[1] != 'y')
	     || (cp[1] != 0 && cp[2] != 0)))
Jim Blandy's avatar
Jim Blandy committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    {
      if (PF_funcs (inf) != 0)
	goto close_and_return;
      rewind (inf);		/* no fortran tags found, try C */
    }
  C_entries (cplusplus ? C_PLPL : 0);

close_and_return:
  (void) fclose (inf);
}

/* Nonzero if string STR is composed of digits.  */

int
string_numeric_p (str)
     char *str;
{
  while (*str)
    {
      if (*str < '0' || *str > '9')
	return 0;
    }
  return 1;
}

/* Record a tag. */
/* Should take a TOKEN* instead!! */
void
986
pfnote (name, is_func, named, linestart, linelen, lno, cno)
Jim Blandy's avatar
Jim Blandy committed
987 988
     char *name;		/* tag name */
     logical is_func;		/* function or type name? */
989
     logical named;		/* tag different from text of definition? */
Jim Blandy's avatar
Jim Blandy committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
     char *linestart;
     int linelen;
     int lno;
     long cno;
{
  register char *fp;
  register NODE *np;
  char tem[51];
  char c;

  np = (NODE *) malloc (sizeof (NODE));
  if (np == NULL)
    {
      if (!emacs_tags_format)
	{
	  /* It's okay to output early in etags -- it only disrupts the
	   * character count of the tag entries, which is no longer used
	   * by tags.el anyway.
	   */
	  error ("too many entries to sort");
	}
      put_entries (head);
      free_tree (head);
      head = NULL;
      np = xnew (1, NODE);
    }
  /* If ctags mode, change name "main" to M<thisfilename>. */
  if (!emacs_tags_format && !cxref_style && streq (name, "main"))
    {
1019
      fp = etags_rindex (curfile, '/');
Jim Blandy's avatar
Jim Blandy committed
1020
      name = concat ("M", fp == 0 ? curfile : fp + 1, "");
1021
      fp = etags_rindex (name, '.');
Jim Blandy's avatar
Jim Blandy committed
1022 1023
      if (fp && fp[1] != '\0' && fp[2] == '\0')
	*fp = 0;
1024
      named = TRUE;
Jim Blandy's avatar
Jim Blandy committed
1025 1026 1027 1028
    }
  np->name = savestr (name);
  np->file = curfile;
  np->is_func = is_func;
1029
  np->named = named;
Jim Blandy's avatar
Jim Blandy committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
  np->lno = lno;
  /* UNCOMMENT THE +1 HERE: */
  np->cno = cno /* + 1 */ ;	/* our char numbers are 0-base; emacs's are 1-base */
  np->left = np->right = 0;
  if (emacs_tags_format)
    {
      c = linestart[linelen];
      linestart[linelen] = 0;
    }
  else if (cxref_style == 0)
    {
      sprintf (tem, strlen (linestart) < 50 ? "%s$" : "%.50s", linestart);
      linestart = tem;
    }
  np->pat = savestr (linestart);
  if (emacs_tags_format)
    {
      linestart[linelen] = c;
    }

  add_node (np, &head);
}

/*
 * free_tree ()
 *	recurse on left children, iterate on right children.
 */
void
free_tree (node)
     register NODE *node;
{
  while (node)
    {
      register NODE *node_right = node->right;
      free_tree (node->left);
      free (node->name);
      free (node->pat);
      free ((char *) node);
      node = node_right;
    }
}

/*
 * add_node ()
 *	Adds a node to the tree of nodes.  In etags mode, we don't keep
 *	it sorted; we just keep a linear list.  In ctags mode, maintain
 *	an ordered tree, with no attempt at balancing.
 *
 *	add_node is the only function allowed to add nodes, so it can
 *	maintain state.
 */
1081 1082 1083 1084
/* Must avoid static vars within functions since some systems
   #define static as nothing.  */
static NODE *last_node = NULL;

Jim Blandy's avatar
Jim Blandy committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
void
add_node (node, cur_node_p)
     NODE *node, **cur_node_p;
{
  register int dif;
  register NODE *cur_node = *cur_node_p;

  if (cur_node == NULL)
    {
      *cur_node_p = node;
      last_node = node;
      return;
    }

  if (emacs_tags_format)
    {
      /* Etags Mode */
      if (!last_node)
	fatal ("internal error in add_node");
      last_node->right = node;
      last_node = node;
    }
  else
    {
      /* Ctags Mode */
      dif = strcmp (node->name, cur_node->name);

      /*
       * If this tag name matches an existing one, then
       * do not add the node, but maybe print a warning.
       */
      if (!dif)
	{
	  if (node->file == cur_node->file)
	    {
	      if (!no_warnings)
		{
		  fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
			   node->file, lineno, node->name);
		  fprintf (stderr, "Second entry ignored\n");
		}
	      return;
	    }
	  if (!cur_node->been_warned && !no_warnings)
	    {
	      fprintf (stderr,
		  "Duplicate entry in files %s and %s: %s (Warning only)\n",
		       node->file, cur_node->file, node->name);
	    }
	  cur_node->been_warned = TRUE;
	  return;
	}

      /* Maybe refuse to add duplicate nodes.  */
      if (!permit_duplicates)
	{
	  if (!strcmp (node->name, cur_node->name)
	      && !strcmp (node->file, cur_node->file))
	    return;
	}

      /* Actually add the node */
      add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
    }
}

void
put_entries (node)
1153
     register NODE *node;
Jim Blandy's avatar
Jim Blandy committed
1154
{
1155
  register char *sp;
Jim Blandy's avatar
Jim Blandy committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

  if (node == NULL)
    return;

  /* Output subentries that precede this one */
  put_entries (node->left);

  /* Output this entry */

  if (emacs_tags_format)
    {
1167
      if (node->named)
Jim Blandy's avatar
Jim Blandy committed
1168 1169
	{
	  fprintf (outf, "%s\177%s\001%d,%d\n",
1170 1171
		   node->pat, node->name,
		   node->lno, node->cno);
Jim Blandy's avatar
Jim Blandy committed
1172 1173 1174 1175
	}
      else
	{
	  fprintf (outf, "%s\177%d,%d\n",
1176 1177
		   node->pat,
		   node->lno, node->cno);
Jim Blandy's avatar
Jim Blandy committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	}
    }
  else if (!cxref_style)
    {
      fprintf (outf, "%s\t%s\t",
	       node->name, node->file);

      if (node->is_func)
	{			/* a function */
	  putc (searchar, outf);
	  putc ('^', outf);

	  for (sp = node->pat; *sp; sp++)
	    {
	      if (*sp == '\\' || *sp == searchar)
		putc ('\\', outf);
	      putc (*sp, outf);
	    }
	  putc (searchar, outf);
	}
      else
	{			/* a typedef; text pattern inadequate */
	  fprintf (outf, "%d", node->lno);
	}
      putc ('\n', outf);
    }
  else if (vgrind_style)
    fprintf (stdout, "%s %s %d\n",
	     node->name, node->file, (node->lno + 63) / 64);
  else
Jim Blandy's avatar
Jim Blandy committed
1208
    fprintf (stdout, "%-16s %3d %-16s %s\n",
Jim Blandy's avatar
Jim Blandy committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	     node->name, node->lno, node->file, node->pat);

  /* Output subentries that follow this one */
  put_entries (node->right);
}

/* Length of a number's decimal representation. */
int
number_len (num)
     long num;
{
  int len = 0;
  if (!num)
    return 1;
  for (; num; num /= 10)
    ++len;
  return len;
}

/*
 * Return total number of characters that put_entries will output for
 * the nodes in the subtree of the specified node.  Works only if emacs_tags_format
 * is set, but called only in that case.  This count is irrelevant with
 * the new tags.el, but is still supplied for backward compatibility.
 */
int
total_size_of_entries (node)
1236
     register NODE *node;
Jim Blandy's avatar
Jim Blandy committed
1237
{
1238
  register int total;
Jim Blandy's avatar
Jim Blandy committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

  if (node == NULL)
    return 0;

  total = 0;
  for (; node; node = node->right)
    {
      /* Count left subentries. */
      total += total_size_of_entries (node->left);

      /* Count this entry */
      total += strlen (node->pat) + 1;
      total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
1252
      if (node->named)
Jim Blandy's avatar
Jim Blandy committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	total += 1 + strlen (node->name);	/* \001name */
    }

  return total;
}

/*
 * The C symbol tables.
 */

Stab *C_stab, *C_PLPL_stab, *C_STAR_stab;

/*
 * SYNOPSIS
 *	Stab *get_C_stab (int c_ext);
 */
1269 1270
#define get_C_stab(c_ext) ((c_ext & C_STAR) ? C_STAR_stab :		\
			   (c_ext & C_PLPL) ? C_PLPL_stab :		\
Jim Blandy's avatar
Jim Blandy committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
			   C_stab)

void
add_keyword (stab, sym, type)
     Stab *stab;
     char *sym;
     enum sym_type type;
{
  stab_search (stab, sym, strlen (sym))->type = type;
}

Stab *
C_create_stab (c_ext)
     int c_ext;
{
  Stab *stab;

  stab = stab_create ();

  /* C, C++ and C* */
  if (c_ext & C_PLPL)
    add_keyword (stab, "class", st_C_struct);
  if (c_ext & C_STAR)
    add_keyword (stab, "domain", st_C_struct);
  add_keyword (stab, "union", st_C_struct);
  add_keyword (stab, "struct", st_C_struct);
  add_keyword (stab, "enum", st_C_enum);
  add_keyword (stab, "typedef", st_C_typedef);
  add_keyword (stab, "define", st_C_define);
  add_keyword (stab, "long", st_C_typespec);
  add_keyword (stab, "short", st_C_typespec);
  add_keyword (stab, "int", st_C_typespec);
  add_keyword (stab, "char", st_C_typespec);
  add_keyword (stab, "float", st_C_typespec);
  add_keyword (stab, "double", st_C_typespec);
  add_keyword (stab, "signed", st_C_typespec);
  add_keyword (stab, "unsigned", st_C_typespec);
1308 1309 1310 1311
  add_keyword (stab, "auto", st_C_typespec);
  add_keyword (stab, "void", st_C_typespec);
  add_keyword (stab, "extern", st_C_typespec);
  add_keyword (stab, "static", st_C_typespec);
Jim Blandy's avatar
Jim Blandy committed
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
  add_keyword (stab, "const", st_C_typespec);
  add_keyword (stab, "volatile", st_C_typespec);

  return stab;
}

void
C_create_stabs ()
{
  C_stab = C_create_stab (0);
  C_PLPL_stab = C_create_stab (C_PLPL);
  C_STAR_stab = C_create_stab (C_STAR | C_PLPL);
}

1326 1327 1328 1329 1330 1331
 /*
  * C functions are recognized using a simple finite automaton.
  * funcdef is its state variable.
  */
typedef enum
{
1332 1333
  fnone,			/* nothing seen */
  ftagseen,			/* function-like tag seen */
1334
  fstartlist,			/* just after open parenthesis */
1335 1336
  finlist,			/* in parameter list */
  flistseen,			/* after parameter list */
1337
  fignore			/* before open brace */
1338 1339 1340 1341 1342 1343 1344 1345 1346
} FUNCST;
FUNCST funcdef;


 /* typedefs are recognized using a simple finite automaton.
  * typeddef is its state variable.
  */
typedef enum
{
1347 1348 1349
  tnone,			/* nothing seen */
  ttypedseen,			/* typedef keyword seen */
  tinbody,			/* inside typedef body */
1350
  tend				/* just before typedef tag */
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
} TYPEDST;
TYPEDST typdef;


 /* struct tags for C++ are recognized using another simple
  * finite automaton.  `structdef' is its state variable.
  * This machinery is only invoked for C++; otherwise structdef
  * should remain snone.  However, this machinery can easily be
  * adapted to find structure tags in normal C code.
  */
typedef enum
{
  snone,			/* nothing seen yet */
  skeyseen,			/* struct-like keyword seen */
  stagseen,			/* struct-like tag seen */
  scolonseen,			/* colon seen after struct-like tag */
1367
  sinbody			/* in struct body: recognize member func defs*/
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
} STRUCTST;
STRUCTST structdef;
/*
 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
 * struct tag, and structkey is the preceding struct-like keyword.
 */
char structtag[BUFSIZ];
Stab_entry *structkey;

/*
 * Yet another little state machine to deal with preprocessor lines.
 */
typedef enum
{
  dnone,			/* nothing seen */
  dsharpseen,			/* '#' seen as first char on line */
  ddefineseen,			/* '#' and 'define' seen */
1385
  dignorerest			/* ignore rest of line */
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
} DEFINEST;
DEFINEST definedef;

/*
 * Set this to TRUE, and the next token considered is called a function.
 * Used only for GNUmacs's function-defining macros.
 */
logical next_token_is_func;

/*
 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
 */
logical yacc_rules;

Jim Blandy's avatar
Jim Blandy committed
1400 1401
/*
 * C_entries ()
1402 1403
 *	This routine finds functions, typedefs, #define's and
 * 	struct/union/enum definitions in C syntax and adds them
Jim Blandy's avatar
Jim Blandy committed
1404 1405 1406
 *	to the list.
 */

1407 1408 1409 1410 1411 1412 1413
#define curlb (lbs[curndx].lb)
#define othlb (lbs[1-curndx].lb)
#define newlb (lbs[newndx].lb)
#define curlinepos (lbs[curndx].linepos)
#define othlinepos (lbs[1-curndx].linepos)
#define newlinepos (lbs[newndx].linepos)

1414 1415 1416 1417 1418 1419 1420 1421 1422
/* Save and restore token state.  This is used when preprocessor defines
   are handled, to avoid disturbing active function/typedef/struct states.  */
#define TOKEN_SAVED_P	(savetok.lineno > 0)
#define SAVE_TOKEN	(savetok = tok, savetok.p = (char *) tokoff,	\
			 savetok.len = toklen, strcpy(savenameb, nameb))
#define RESTORE_TOKEN	(tok = savetok, tokoff = (int) tok.p,		\
			 toklen = tok.len, strcpy(nameb, savenameb),	\
			 savetok.lineno = 0)

Jim Blandy's avatar
Jim Blandy committed
1423
#define CNL_SAVE_DEFINEDEF						\
1424 1425
do {									\
  SET_FILEPOS (curlinepos, inf, charno);				\
Jim Blandy's avatar
Jim Blandy committed
1426
  lineno++;								\
1427 1428 1429 1430 1431
  charno += readline (&curlb, inf);					\
  lp = curlb.buffer;							\
  quotednl = FALSE;							\
  newndx = curndx;							\
} while (FALSE)
Jim Blandy's avatar
Jim Blandy committed
1432 1433

#define CNL								\
1434
do {									\
Jim Blandy's avatar
Jim Blandy committed
1435
  CNL_SAVE_DEFINEDEF;							\
1436 1437
  if (TOKEN_SAVED_P)							\
    RESTORE_TOKEN;							\
Jim Blandy's avatar
Jim Blandy committed
1438
  definedef = dnone;							\
1439 1440
} while (FALSE)

1441
#define MAKE_TAG_FROM_NEW_LB(isfun)  pfnote (nameb, isfun, tok.named,	\
1442
  newlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (newlinepos))
1443
#define MAKE_TAG_FROM_OTH_LB(isfun)  pfnote (nameb, isfun, tok.named,	\
1444
  othlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (othlinepos))
Jim Blandy's avatar
Jim Blandy committed
1445 1446 1447 1448 1449

void
C_entries (c_ext)
     int c_ext;			/* extension of C? */
{
1450
  register char c;		/* latest char read; '\0' for end of line */
Jim Blandy's avatar
Jim Blandy committed
1451
  register char *lp;		/* pointer one beyond the character `c' */
1452 1453
  int curndx, newndx;		/* indices for current and new lb */
  TOKEN tok;			/* latest token read for funcdef & structdef */
1454
  char nameb[BUFSIZ];		/* latest token name for funcdef & structdef */
1455 1456
  register int tokoff;		/* offset in line of start of latest token */
  register int toklen;		/* length of latest token */
1457
  int cblev;			/* current curly brace level */
1458
  int parlev;			/* current parenthesis level */
1459 1460
  logical incomm, inquote, inchar, quotednl, midtoken;
  logical cplpl;
1461 1462 1463
  TOKEN savetok;		/* saved token during preprocessor handling */
  logical saveisfunc;
  char savenameb[BUFSIZ];	/* ouch! */
Jim Blandy's avatar
Jim Blandy committed
1464

1465
  savetok.lineno = 0;
1466
  curndx = newndx = 0;
Jim Blandy's avatar
Jim Blandy committed
1467 1468
  lineno = 0;
  charno = 0;
1469
  lp = curlb.buffer;
Jim Blandy's avatar
Jim Blandy committed
1470 1471
  *lp = 0;

1472 1473 1474
  definedef = dnone; funcdef = fnone; typdef= tnone; structdef= snone;
  next_token_is_func = yacc_rules = FALSE;
  midtoken = inquote = inchar = incomm = quotednl = FALSE;
1475
  cblev = 0;
1476
  parlev = 0;
1477
  cplpl = c_ext & C_PLPL;
Jim Blandy's avatar
Jim Blandy committed
1478 1479 1480 1481 1482 1483 1484 1485

  C_create_stabs ();

  while (!feof (inf))
    {
      c = *lp++;
      if (c == '\\')
	{
Jim Blandy's avatar
Jim Blandy committed
1486 1487 1488
	  /* If we're at the end of the line, the next character is a
	     '\0'; don't skip it, because it's the thing that tells us
	     to read the next line.  */
1489
	  if (*lp == '\0')
1490
	    {