etags.c 73.3 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Tags file maker to go with GNU Emacs
Karl Heuer's avatar
Karl Heuer committed
2
   Copyright (C) 1984,87,88,89,93,94 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

/*
 * Authors:
 *	Ctags originally by Ken Arnold.
23
 *	Fortran added by Jim Kleckner.
Jim Blandy's avatar
Jim Blandy committed
24 25 26
 *	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.
Jim Blandy's avatar
Jim Blandy committed
29 30
 */

31
char pot_etags_version[] = "@(#) pot revision number is 10.32";
32

33 34
#ifdef MSDOS
#include <fcntl.h>
35
#include <sys/param.h>
36 37
#endif /* MSDOS */

38
#ifdef HAVE_CONFIG_H
39
#include <../src/config.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
40 41 42 43
/* On some systems, Emacs defines static as nothing
   for the sake of unexec.  We don't want that here
   since we don't use unexec.  */
#undef static
Roland McGrath's avatar
Roland McGrath committed
44 45
#endif

46 47 48 49 50
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>

51 52 53 54
#if !defined (S_ISREG) && defined (S_IFREG)
# define S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)
#endif

55 56
#include "getopt.h"

Jim Blandy's avatar
Jim Blandy committed
57
extern char *getenv ();
Karl Heuer's avatar
Karl Heuer committed
58 59 60
#ifndef MSDOS
extern FILE *popen ();
#endif
Jim Blandy's avatar
Jim Blandy committed
61

62 63 64 65 66 67 68 69
/* Define CTAGS to make the program "ctags" compatible with the usual one.
 Let it undefined to make the program "etags", which makes emacs-style
 tag tables and tags typedefs, #defines and struct/union/enum by default. */
#ifdef CTAGS
# undef  CTAGS
# define CTAGS TRUE
#else
# define CTAGS FALSE
Jim Blandy's avatar
Jim Blandy committed
70 71 72 73
#endif

/* Exit codes for success and failure.  */
#ifdef VMS
74 75
#define	GOOD	1
#define BAD	0
Jim Blandy's avatar
Jim Blandy committed
76
#else
77 78
#define	GOOD	0
#define	BAD	1
Jim Blandy's avatar
Jim Blandy committed
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
#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
106
 /* real implementation */
Jim Blandy's avatar
Jim Blandy committed
107 108 109 110
typedef long FILEPOS;
#define GET_CHARNO(pos)	((pos) + 0)
#define SET_FILEPOS(pos, fp, cno)	((void) ((pos) = (cno)))
#else
111
 /* debugging implementation */
Jim Blandy's avatar
Jim Blandy committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
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)
126
#define	logical		int
Jim Blandy's avatar
Jim Blandy committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

#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	*/
143
  logical named;		/* list name separately		*/
Jim Blandy's avatar
Jim Blandy committed
144 145 146 147 148 149 150 151 152
  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		*/
};

typedef struct nd_st NODE;

153 154 155
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
156

157
char *cwd;			/* current working directory */
158
char *tagfiledir;		/* directory of tagfile */
Jim Blandy's avatar
Jim Blandy committed
159 160

char *concat ();
161
char *savenstr (), *savestr ();
162 163
char *etags_strchr (), *etags_strrchr ();
char *etags_getcwd ();
164 165
char *relative_filename (), *absolute_filename (), *absolute_dirname ();
char *xmalloc (), *xrealloc ();
Jim Blandy's avatar
Jim Blandy committed
166 167
int total_size_of_entries ();
long readline ();
168 169

void Asm_labels ();
Jim Blandy's avatar
Jim Blandy committed
170
void C_entries ();
171 172 173 174 175 176
int Fortran_functions ();
void Lisp_functions ();
void Pascal_functions ();
void Prolog_functions ();
void Scheme_functions ();
void TeX_functions ();
Jim Blandy's avatar
Jim Blandy committed
177 178 179
void add_node ();
void error ();
void fatal ();
180
logical find_entries ();
Jim Blandy's avatar
Jim Blandy committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
void free_tree ();
void getit ();
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)))

/*
201
 *	Symbol table types.
Jim Blandy's avatar
Jim Blandy committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
 */
enum sym_type
{
  st_none, st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
};



typedef int LINENO;

typedef struct
{
  char *p;
  int len;
  LINENO lineno;
217
  logical named;
Jim Blandy's avatar
Jim Blandy committed
218 219
} TOKEN;

220
/* C extensions.
Jim Blandy's avatar
Jim Blandy committed
221
 */
222 223 224
#define C_PLPL	0x00001		/* C++ */
#define C_STAR	0x00003		/* C* */
#define YACC	0x10000		/* yacc file */
Jim Blandy's avatar
Jim Blandy committed
225 226 227 228 229 230 231 232 233 234 235

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		*/
236
 *tagfile,			/* output file				*/
Jim Blandy's avatar
Jim Blandy committed
237
 *white = " \f\t\n",		/* white chars				*/
238 239
 *endtk = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?",	/* token ending chars	*/
				/* token starting chars			*/
240
 *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
241
				/* valid in-token chars			*/
242
 *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
Jim Blandy's avatar
Jim Blandy committed
243 244 245 246 247 248

int append_to_tagfile;		/* -a: append to tags */
/* 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++ */
249
				/* member functions. */
Jim Blandy's avatar
Jim Blandy committed
250
int constantypedefs;		/* -d: create tags for C #define and enum */
251
				/* constants.  Enum consts not implemented. */
Jim Blandy's avatar
Jim Blandy committed
252 253 254 255 256 257 258 259 260 261 262
				/* -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
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
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' },
  { "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 }
};

283 284 285
FILE *tagf;			/* ioptr for tags file */
NODE *head;			/* the head of the binary tree of tags */
logical permit_duplicates = TRUE; /* allow duplicate tags */
Jim Blandy's avatar
Jim Blandy committed
286 287 288 289 290 291 292 293 294 295 296 297 298

/* 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 */
299 300 301 302 303
struct
{
  FILEPOS linepos;
  struct linebuffer lb;		/* used by C_entries instead of lb */
} lbs[2];
Jim Blandy's avatar
Jim Blandy committed
304

Jim Blandy's avatar
Jim Blandy committed
305 306 307
void
print_version ()
{
308
#ifdef VERSION
309
  printf ("%s for Emacs version %s.\n", (CTAGS) ? "CTAGS" : "ETAGS", VERSION);
310 311 312
#else
  printf ("%s for Emacs version 19.\n", (CTAGS) ? "CTAGS" : "ETAGS");
#endif  
Jim Blandy's avatar
Jim Blandy committed
313

314
  exit (GOOD);
Jim Blandy's avatar
Jim Blandy committed
315 316 317 318 319 320
}

void
print_help ()
{
  printf ("These are the options accepted by %s.  You may use unambiguous\n\
321 322
abbreviations for the long option names.  A - as file name means read file\n\
names from stdin.\n\n", progname);
Jim Blandy's avatar
Jim Blandy committed
323

324 325
  puts ("-a, --append\n\
        Append tag entries to existing tags file.");
326

327 328
  if (CTAGS)
    puts ("-B, --backward-search\n\
329
        Write the search commands for the tag entries using '?', the\n\
330
        backward-search command instead of '/', the forward-search command.");
331

332
  puts ("-C, --c++\n\
Jim Blandy's avatar
Jim Blandy committed
333 334
        Treat files with `.c' and `.h' extensions as C++ code, not C\n\
        code.  Files with `.C', `.H', `.cxx', `.hxx', or `.cc'\n\
335
        extensions are always assumed to be C++ code.");
Jim Blandy's avatar
Jim Blandy committed
336

337 338 339 340 341 342 343
  if (CTAGS)
    puts ("-d, --defines\n\
        Create tag entries for C #defines, too.");
  else
    puts ("-D, --no-defines\n\
        Don't create tag entries for C #defines.  This makes the tags\n\
	file smaller.");
Jim Blandy's avatar
Jim Blandy committed
344

345 346
  if (!CTAGS)
    puts ("-i FILE, --include=FILE\n\
347 348 349
        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.");
Jim Blandy's avatar
Jim Blandy committed
350

351 352 353
  puts ("-o FILE, --output=FILE\n\
        Write the tags to FILE.");
  puts ("-S, --ignore-indentation\n\
Jim Blandy's avatar
Jim Blandy committed
354 355 356
        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\
357
        definition in C and C++.");
Jim Blandy's avatar
Jim Blandy committed
358

359 360 361 362 363 364 365 366
  if (CTAGS)
    {
      puts ("-t, --typedefs\n\
        Generate tag entries for C typedefs.");
      puts ("-T, --typedefs-and-c++\n\
        Generate tag entries for C typedefs, C struct/enum/union tags,\n\
        and C++ member functions.");
      puts ("-u, --update\n\
Jim Blandy's avatar
Jim Blandy committed
367 368 369 370 371
        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\
372
        tag file than to use this.");
373
      puts ("-v, --vgrind\n\
Jim Blandy's avatar
Jim Blandy committed
374 375
        Generates an index of items intended for human consumption,\n\
        similar to the output of vgrind.  The index is sorted, and\n\
376
        gives the page number of each item.");
377
      puts ("-x, --cxref\n\
Jim Blandy's avatar
Jim Blandy committed
378 379 380
        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\
381
        which you like.");
382
      puts ("-w, --no-warn\n\
Jim Blandy's avatar
Jim Blandy committed
383 384
        Suppress warning messages about entries defined in multiple\n\
        files.");
385
    }
Jim Blandy's avatar
Jim Blandy committed
386 387 388 389 390 391

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

392
  exit (GOOD);
Jim Blandy's avatar
Jim Blandy committed
393 394 395
}


Jim Blandy's avatar
Jim Blandy committed
396 397 398 399 400 401 402
void
main (argc, argv)
     int argc;
     char *argv[];
{
  char cmd[100];
  int i;
Roland McGrath's avatar
Roland McGrath committed
403
  unsigned int nincluded_files = 0;
404
  char **included_files = xnew (argc, char *);
Jim Blandy's avatar
Jim Blandy committed
405 406 407 408 409 410 411
  char *this_file;
#ifdef VMS
  char got_err;

  extern char *gfnames ();
  extern char *massage_name ();
#endif
412
 
413
#ifdef MSDOS
414
  _fmode = O_BINARY;   /* all of files are treated as binary files */
415 416
#endif /* MSDOS */

Jim Blandy's avatar
Jim Blandy committed
417 418 419 420 421 422
  progname = argv[0];

  /*
   * If etags, always find typedefs and structure tags.  Why not?
   * Also default is to find macro constants.
   */
423
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
424 425
    typedefs = typedefs_and_cplusplus = constantypedefs = 1;

Jim Blandy's avatar
Jim Blandy committed
426
  for (;;)
Jim Blandy's avatar
Jim Blandy committed
427
    {
Jim Blandy's avatar
Jim Blandy committed
428
      int opt;
429
      opt = getopt_long (argc, argv, "aCdDf:o:StTi:BuvxwVH", longopts, 0);
Jim Blandy's avatar
Jim Blandy committed
430 431 432 433 434

      if (opt == EOF)
	break;

      switch (opt)
Jim Blandy's avatar
Jim Blandy committed
435
	{
436 437
	case 0:
	  /* If getopt returns 0, then it has already processed a
Jim Blandy's avatar
Jim Blandy committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	     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;
454
	case 'f':		/* for compatibility with old makefiles */
Jim Blandy's avatar
Jim Blandy committed
455
	case 'o':
456
	  if (tagfile)
Jim Blandy's avatar
Jim Blandy committed
457
	    {
Jim Blandy's avatar
Jim Blandy committed
458
	      fprintf (stderr,
459
		       "%s: -%c flag may only be given once\n", progname, opt);
Jim Blandy's avatar
Jim Blandy committed
460 461
	      goto usage;
	    }
462
	  tagfile = optarg;
Jim Blandy's avatar
Jim Blandy committed
463 464 465 466 467 468 469 470 471 472 473
	  break;
	case 'S':
	  noindentypedefs++;
	  break;
	case 'V':
	  print_version ();
	  break;
	case 'H':
	  print_help ();
	  break;

474 475
#if (!CTAGS)

Jim Blandy's avatar
Jim Blandy committed
476 477 478 479 480
	  /* Etags options */
	case 'i':
	  included_files[nincluded_files++] = optarg;
	  break;

481 482
#else /* CTAGS */

Jim Blandy's avatar
Jim Blandy committed
483 484 485 486
	  /* Ctags options. */
	case 'B':
	  searchar = '?';
	  break;
487 488 489 490 491 492
	case 't':
	  typedefs++;
	  break;
	case 'T':
	  typedefs++;
	  typedefs_and_cplusplus++;
Jim Blandy's avatar
Jim Blandy committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506
	  break;
	case 'u':
	  update++;
	  break;
	case 'v':
	  vgrind_style++;
	  /*FALLTHRU*/
	case 'x':
	  cxref_style++;
	  break;
	case 'w':
	  no_warnings++;
	  break;

507 508
#endif /* CTAGS */

Jim Blandy's avatar
Jim Blandy committed
509 510
	default:
	  goto usage;
Jim Blandy's avatar
Jim Blandy committed
511 512 513
	}
    }

514
  if (optind == argc && nincluded_files == 0)
Jim Blandy's avatar
Jim Blandy committed
515
    {
Jim Blandy's avatar
Jim Blandy committed
516 517
      fprintf (stderr, "%s: No input files specified.\n", progname);

Jim Blandy's avatar
Jim Blandy committed
518
    usage:
519
      fprintf (stderr, "%s: Try `%s --help' for a complete list of options.\n",
Jim Blandy's avatar
Jim Blandy committed
520
	       progname, progname);
Jim Blandy's avatar
Jim Blandy committed
521 522 523
      exit (BAD);
    }

524
  if (tagfile == NULL)
Jim Blandy's avatar
Jim Blandy committed
525
    {
526
      tagfile = CTAGS ? "tags" : "TAGS";
Jim Blandy's avatar
Jim Blandy committed
527
    }
528
  cwd = etags_getcwd ();	/* the current working directory */
529
  strcat (cwd, "/");
530
  if (streq (tagfile, "-"))
531
    {
532
      tagfiledir = cwd;
533 534 535
    }
  else
    {
536
      tagfiledir = absolute_dirname (tagfile, cwd);
537
    }
Jim Blandy's avatar
Jim Blandy committed
538 539 540 541

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

  initbuffer (&lb);
542 543
  initbuffer (&lbs[0].lb);
  initbuffer (&lbs[1].lb);
Jim Blandy's avatar
Jim Blandy committed
544 545 546 547
  initbuffer (&filename_lb);
  /*
   * loop through files finding functions
   */
548
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
549
    {
550 551
      if (streq (tagfile, "-"))
	tagf = stdout;
Jim Blandy's avatar
Jim Blandy committed
552
      else
553 554
	tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
      if (tagf == NULL)
Jim Blandy's avatar
Jim Blandy committed
555
	{
556
	  perror (tagfile);
557
	  exit (BAD);
Jim Blandy's avatar
Jim Blandy committed
558 559 560 561
	}
    }

#ifdef VMS
Jim Blandy's avatar
Jim Blandy committed
562 563 564
  argc -= optind;
  argv += optind;
  while (gfnames (&argc, &argv, &got_err) != NULL)
Jim Blandy's avatar
Jim Blandy committed
565 566 567 568 569 570 571 572 573 574 575
    {
      if (got_err)
	{
	  error ("Can't find file %s\n", this_file);
	  argc--, argv++;
	}
      else
	{
	  this_file = massage_name (this_file);
#if 0
	}
576
    }			/* solely to balance out the ifdef'd parens above */
Jim Blandy's avatar
Jim Blandy committed
577 578
#endif
#else
Jim Blandy's avatar
Jim Blandy committed
579
  for (; optind < argc; optind++)
Jim Blandy's avatar
Jim Blandy committed
580
    {
Jim Blandy's avatar
Jim Blandy committed
581
      this_file = argv[optind];
Jim Blandy's avatar
Jim Blandy committed
582
#endif
583
      /* Input file named "-" means read file names from stdin and use them. */
584 585 586
      if (streq (this_file, "-"))
	{
	  while (!feof (stdin))
Jim Blandy's avatar
Jim Blandy committed
587
	    {
588 589 590
	      (void) readline (&filename_lb, stdin);
	      if (strlen (filename_lb.buffer) > 0)
		process_file (filename_lb.buffer);
Jim Blandy's avatar
Jim Blandy committed
591 592
	    }
	}
593 594 595
      else
	process_file (this_file);
    }
Jim Blandy's avatar
Jim Blandy committed
596

597
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
598
    {
Roland McGrath's avatar
Roland McGrath committed
599
      while (nincluded_files-- > 0)
600
	fprintf (tagf, "\f\n%s,include\n", *included_files++);
Roland McGrath's avatar
Roland McGrath committed
601

602
      (void) fclose (tagf);
603
      exit (GOOD);
Jim Blandy's avatar
Jim Blandy committed
604 605 606 607 608 609 610
    }

  if (cxref_style)
    {
      put_entries (head);
      exit (GOOD);
    }
Jim Blandy's avatar
Jim Blandy committed
611
  if (update)
Jim Blandy's avatar
Jim Blandy committed
612
    {
Jim Blandy's avatar
Jim Blandy committed
613 614 615
      /* 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
616 617 618
	{
	  sprintf (cmd,
		   "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
619
		   tagfile, argv[i], tagfile);
Jim Blandy's avatar
Jim Blandy committed
620 621 622 623
	  (void) system (cmd);
	}
      append_to_tagfile++;
    }
624 625
  tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
  if (tagf == NULL)
Jim Blandy's avatar
Jim Blandy committed
626
    {
627
      perror (tagfile);
Jim Blandy's avatar
Jim Blandy committed
628 629 630
      exit (GOOD);
    }
  put_entries (head);
631
  (void) fclose (tagf);
Jim Blandy's avatar
Jim Blandy committed
632 633
  if (update)
    {
634
      sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
Jim Blandy's avatar
Jim Blandy committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
      (void) system (cmd);
    }
  exit (GOOD);
}


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

650
  if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
Jim Blandy's avatar
Jim Blandy committed
651 652 653 654
    {
      fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
      return;
    }
655
  if (streq (file, tagfile) && !streq (tagfile, "-"))
Jim Blandy's avatar
Jim Blandy committed
656 657 658 659
    {
      fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
      return;
    }
660 661 662 663
  if (!find_entries (file))
    {
      return;
    }
664
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
665
    {
666 667 668 669 670 671 672 673 674 675 676
      char *filename;

      if (file[0] == '/')
	{
	  /* file is an absolute filename.  Canonicalise it. */
	  filename = absolute_filename (file, cwd);
	}
      else
	{
	  /* file is a filename relative to cwd.  Make it relative
	     to the directory of the tags file. */
677
	  filename = relative_filename (file, tagfiledir);
678
	}
679
      fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
Jim Blandy's avatar
Jim Blandy committed
680 681 682 683 684 685 686
      put_entries (head);
      free_tree (head);
      head = NULL;
    }
}

/*
687
 * This routine sets up the boolean pseudo-functions which work
688
 * by setting boolean flags dependent upon the corresponding character
Jim Blandy's avatar
Jim Blandy committed
689 690 691 692 693 694 695 696
 * 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 ()
{
697 698
  register char *sp;
  register int i;
Jim Blandy's avatar
Jim Blandy committed
699 700

  for (i = 0; i < 0177; i++)
701
    _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
Jim Blandy's avatar
Jim Blandy committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
  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.
 */
720
logical
Jim Blandy's avatar
Jim Blandy committed
721 722 723
find_entries (file)
     char *file;
{
724 725
  char *cp, *cp1;
  FILE *inf;
Jim Blandy's avatar
Jim Blandy committed
726 727 728 729 730

  inf = fopen (file, "r");
  if (inf == NULL)
    {
      perror (file);
731
      return FALSE;
Jim Blandy's avatar
Jim Blandy committed
732 733
    }
  curfile = savestr (file);
734
  cp = etags_strrchr (file, '.');
735
  cp1 = cp + 1;
Jim Blandy's avatar
Jim Blandy committed
736

737
  header_file = (cp && (streq (cp1, "h")));
Jim Blandy's avatar
Jim Blandy committed
738 739

  /* .tex, .aux or .bbl implies LaTeX source code */
740 741
  if (cp && (streq (cp1, "tex") || streq (cp1, "aux")
	     || streq (cp1, "bbl")))
Jim Blandy's avatar
Jim Blandy committed
742
    {
743
      TeX_functions (inf);
Jim Blandy's avatar
Jim Blandy committed
744 745 746
      goto close_and_return;
    }
  /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
747 748 749 750 751 752 753 754
  if (cp && (streq (cp1, "l")
	     || streq (cp1, "el")
	     || streq (cp1, "lsp")
	     || streq (cp1, "lisp")
	     || streq (cp1, "cl")
	     || streq (cp1, "clisp")))
    {
      Lisp_functions (inf);
Jim Blandy's avatar
Jim Blandy committed
755 756 757
      goto close_and_return;
    }
  /* .scm or .sm or .scheme or ... implies scheme source code */
758 759 760 761 762 763 764 765
  if (cp && (streq (cp1, "sm")
	     || streq (cp1, "scm")
	     || streq (cp1, "scheme")
	     || streq (cp1, "t")
	     || streq (cp1, "sch")
	     || streq (cp1, "ss")
	     || streq (cp1, "SM")
	     || streq (cp1, "SCM")
Jim Blandy's avatar
Jim Blandy committed
766 767
	     /* The `SCM' or `scm' prefix with a version number */
             || (cp[-1] == 'm' && cp[-2] == 'c' && cp[-3] == 's'
768
		 && string_numeric_p (cp1))
Jim Blandy's avatar
Jim Blandy committed
769
             || (cp[-1] == 'M' && cp[-2] == 'C' && cp[-3] == 'S'
770
		 && string_numeric_p (cp1))))
Jim Blandy's avatar
Jim Blandy committed
771
    {
772
      Scheme_functions (inf);
773
      goto close_and_return;
Jim Blandy's avatar
Jim Blandy committed
774
    }
775 776 777 778 779 780 781 782 783 784 785
  /* Assembly code */
  if (cp && (streq (cp1, "s")
 	     || streq (cp1, "a")    /* Unix assembler */
 	     || streq (cp1, "sa")   /* Unix assembler */
 	     || streq (cp1, "asm")  /* Microcontroller assembly */
 	     || streq (cp1, "src")  /* BSO/Tasking C compiler output */
 	     || streq (cp1, "def")  /* BSO/Tasking definition includes  */
 	     || streq (cp1, "ins")  /* Microcontroller include files */
 	     || streq (cp1, "inc")))/* Microcontroller include files */
    {
      Asm_labels (inf);
786
      goto close_and_return;
Jim Blandy's avatar
Jim Blandy committed
787
    }
788
  /* .C or .H or .cxx or .hxx or .cc or .cpp: a C++ file */
789 790
  if (cp && (streq (cp1, "C")
	     || streq (cp1, "H")
791
	     || streq (cp1, "cpp")
792 793 794
	     || streq (cp1, "cxx")
	     || streq (cp1, "hxx")
	     || streq (cp1, "cc")))
Jim Blandy's avatar
Jim Blandy committed
795
    {
796
      C_entries (C_PLPL, inf);	/* C++ */
Jim Blandy's avatar
Jim Blandy committed
797 798 799
      goto close_and_return;
    }
  /* .cs or .hs: a C* file */
800 801
  if (cp && (streq (cp1, "cs")
	     || streq (cp1, "hs")))
Jim Blandy's avatar
Jim Blandy committed
802
    {
803
      C_entries (C_STAR, inf);
Jim Blandy's avatar
Jim Blandy committed
804 805
      goto close_and_return;
    }
806
  /* .y: a yacc file */
807
  if (cp && (streq (cp1, "y")))
808
    {
809
      C_entries (YACC, inf);
810 811
      goto close_and_return;
    }
Jim Blandy's avatar
Jim Blandy committed
812
  /* .pl implies prolog source code */
813
  if (cp && streq (cp1, "pl"))
Jim Blandy's avatar
Jim Blandy committed
814
    {
815
      Prolog_functions (inf);
Jim Blandy's avatar
Jim Blandy committed
816 817 818
      goto close_and_return;
    }
  /* .p or .pas: a Pascal file */
819 820
  if (cp && (streq (cp1, "p")
	     || streq (cp1, "pas")))
Jim Blandy's avatar
Jim Blandy committed
821
    {
822
      Pascal_functions (inf);
Jim Blandy's avatar
Jim Blandy committed
823 824
      goto close_and_return;
    }
825
  /* If .f or .for, assume it is fortran or nothing.  */
826 827
  if (cp && (streq (cp1, "f")
	     || streq (cp1, "for")))
828
    {
829
      (void) Fortran_functions (inf);
830 831
      goto close_and_return;
    }
Jim Blandy's avatar
Jim Blandy committed
832
  /* if not a .c or .h or .y file, try fortran */
833 834 835 836
  if (cp && ((cp[1] != 'c'
	      && cp[1] != 'h'
	      && cp[1] != 'y')
	     || (cp[1] != 0 && cp[2] != 0)))
Jim Blandy's avatar
Jim Blandy committed
837
    {
838
      if (Fortran_functions (inf) != 0)
Jim Blandy's avatar
Jim Blandy committed
839 840 841
	goto close_and_return;
      rewind (inf);		/* no fortran tags found, try C */
    }
842
  C_entries (cplusplus ? C_PLPL : 0, inf);
Jim Blandy's avatar
Jim Blandy committed
843 844 845

close_and_return:
  (void) fclose (inf);
846
  return TRUE;
Jim Blandy's avatar
Jim Blandy committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
}

/* 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
866
pfnote (name, is_func, named, linestart, linelen, lno, cno)
Jim Blandy's avatar
Jim Blandy committed
867 868
     char *name;		/* tag name */
     logical is_func;		/* function or type name? */
869
     logical named;		/* tag different from text of definition? */
Jim Blandy's avatar
Jim Blandy committed
870 871 872 873 874 875 876 877 878 879
     char *linestart;
     int linelen;
     int lno;
     long cno;
{
  register char *fp;
  register NODE *np;
  char tem[51];
  char c;

880
  np = xnew (1, NODE);
Jim Blandy's avatar
Jim Blandy committed
881 882
  if (np == NULL)
    {
883
      if (CTAGS)
Jim Blandy's avatar
Jim Blandy committed
884 885 886 887 888
	{
	  /* 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.
	   */
889
	  error ("too many entries to sort", 0);
Jim Blandy's avatar
Jim Blandy committed
890 891 892 893 894 895 896
	}
      put_entries (head);
      free_tree (head);
      head = NULL;
      np = xnew (1, NODE);
    }
  /* If ctags mode, change name "main" to M<thisfilename>. */
897
  if (CTAGS && !cxref_style && streq (name, "main"))
Jim Blandy's avatar
Jim Blandy committed
898
    {
899
      fp = etags_strrchr (curfile, '/');
Jim Blandy's avatar
Jim Blandy committed
900
      name = concat ("M", fp == 0 ? curfile : fp + 1, "");
901
      fp = etags_strrchr (name, '.');
Jim Blandy's avatar
Jim Blandy committed
902 903
      if (fp && fp[1] != '\0' && fp[2] == '\0')
	*fp = 0;
904
      named = TRUE;
Jim Blandy's avatar
Jim Blandy committed
905 906 907 908
    }
  np->name = savestr (name);
  np->file = curfile;
  np->is_func = is_func;
909
  np->named = named;
Jim Blandy's avatar
Jim Blandy committed
910 911 912 913
  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;
914
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
915 916 917 918 919 920 921 922 923 924
    {
      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);
925
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    {
      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.
 */
961 962
/* Must avoid static vars within functions since some systems
   #define static as nothing.  */
963
NODE *last_node = NULL;
964

Jim Blandy's avatar
Jim Blandy committed
965 966 967 968 969 970 971 972 973 974 975 976 977 978
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;
    }

979
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
980 981
    {
      /* Etags Mode */
982 983
      if (last_node == NULL)
	fatal ("internal error in add_node", 0);
Jim Blandy's avatar
Jim Blandy committed
984 985 986 987 988 989 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 1019 1020
      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)
	{
1021 1022
	  if (streq (node->name, cur_node->name)
	      && streq (node->file, cur_node->file))
Jim Blandy's avatar
Jim Blandy committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	    return;
	}

      /* Actually add the node */
      add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
    }
}

void
put_entries (node)
1033
     register NODE *node;
Jim Blandy's avatar
Jim Blandy committed
1034
{
1035
  register char *sp;
Jim Blandy's avatar
Jim Blandy committed
1036 1037 1038 1039 1040 1041 1042 1043 1044

  if (node == NULL)
    return;

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

  /* Output this entry */

1045
  if (!CTAGS)
Jim Blandy's avatar
Jim Blandy committed
1046
    {
1047
      if (node->named)
Jim Blandy's avatar
Jim Blandy committed
1048
	{
1049
	  fprintf (tagf, "%s\177%s\001%d,%d\n",
1050 1051
		   node->pat, node->name,
		   node->lno, node->cno);
Jim Blandy's avatar
Jim Blandy committed
1052 1053 1054
	}
      else
	{
1055
	  fprintf (tagf, "%s\177%d,%d\n",
1056 1057
		   node->pat,
		   node->lno, node->cno);
Jim Blandy's avatar
Jim Blandy committed
1058 1059 1060 1061
	}
    }
  else if (!cxref_style)
    {
1062
      fprintf (tagf, "%s\t%s\t",
Jim Blandy's avatar
Jim Blandy committed
1063 1064 1065 1066
	       node->name, node->file);

      if (node->is_func)
	{			/* a function */
1067 1068
	  putc (searchar, tagf);
	  putc ('^', tagf);
Jim Blandy's avatar
Jim Blandy committed
1069 1070 1071 1072

	  for (sp = node->pat; *sp; sp++)
	    {
	      if (*sp == '\\' || *sp == searchar)
1073 1074
		putc ('\\', tagf);
	      putc (*sp, tagf);
Jim Blandy's avatar
Jim Blandy committed
1075
	    }
1076
	  putc (searchar, tagf);
Jim Blandy's avatar
Jim Blandy committed
1077 1078 1079
	}
      else
	{			/* a typedef; text pattern inadequate */
1080
	  fprintf (tagf, "%d", node->lno);
Jim Blandy's avatar
Jim Blandy committed
1081
	}
1082
      putc ('\n', tagf);
Jim Blandy's avatar
Jim Blandy committed
1083 1084 1085 1086 1087
    }
  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
1088
    fprintf (stdout, "%-16s %3d %-16s %s\n",
Jim Blandy's avatar
Jim Blandy committed
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	     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
1110 1111 1112 1113
 * the nodes in the subtree of the specified node.  Works only if
 * we are not ctags, but called only in that case.  This count
 * is irrelevant with the new tags.el, but is still supplied for
 * backward compatibility.
Jim Blandy's avatar
Jim Blandy committed
1114 1115 1116
 */
int
total_size_of_entries (node)
1117
     register NODE *node;
Jim Blandy's avatar
Jim Blandy committed
1118
{
1119
  register int total;
Jim Blandy's avatar
Jim Blandy committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132

  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;
1133
      if (node->named)
Jim Blandy's avatar
Jim Blandy committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	total += 1 + strlen (node->name);	/* \001name */
    }

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

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155