* etags.c (pfatal): New function.
[bpt/emacs.git] / lib-src / etags.c
index fed3f58..4bccca0 100644 (file)
@@ -1,83 +1,95 @@
 /* Tags file maker to go with GNU Emacs
-   Copyright (C) 1984, 1987, 1988, 1989 Free Software Foundation, Inc. and Ken Arnold
+   Copyright (C) 1984,87,88,89,93,94 Free Software Foundation, Inc. and Ken Arnold
+This file is not considered part of GNU Emacs.
 
-This file is part of GNU Emacs.
-
-GNU Emacs is free software; you can redistribute it and/or modify
+This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
-any later version.
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
 
-GNU Emacs is distributed in the hope that it will be useful,
+This program is distributed in the hope that it will be useful,
 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
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 /*
  * Authors:
  *     Ctags originally by Ken Arnold.
- *     FORTRAN added by Jim Kleckner.
+ *     Fortran added by Jim Kleckner.
  *     Ed Pelegri-Llopart added C typedefs.
  *     Gnu Emacs TAGS format and modifications by RMS?
  *     Sam Kendall added C++.
+ *     Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
+#ifdef ETAGS_REGEXPS
+ *     Regexp tags by Tom Tromey.
+#endif
+ *
+ *     Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
  */
 
+char pot_etags_version[] = "@(#) pot revision number is 11.14";
+
+#ifdef MSDOS
+#include <fcntl.h>
+#include <sys/param.h>
+#endif /* MSDOS */
+
+#ifdef WINDOWSNT
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#define MAXPATHLEN _MAX_PATH
+#endif
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+/* 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
+#endif
+
 #include <stdio.h>
 #include <ctype.h>
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#ifdef __GNUC__
-#define        alloca  __builtin_alloca
-#else
-#ifdef sparc
-#include <alloca.h>
-#else
-extern char *alloca ();
-#endif
+#if !defined (S_ISREG) && defined (S_IFREG)
+# define S_ISREG(m)    (((m) & S_IFMT) == S_IFREG)
 #endif
 
-extern char *malloc (), *realloc ();
-extern char *getenv ();
-extern char *index (), *rindex ();
-extern char *strcpy (), *strncpy ();
-extern int strcmp ();
+#include <getopt.h>
 
-#ifdef hpux
-#define notdef
-#endif
+#ifdef ETAGS_REGEXPS
+#include <regex.h>
+#endif /* ETAGS_REGEXPS */
 
-/* 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
+extern char *getenv ();
 
-/* On VMS, CTAGS is not useful, so always do ETAGS.  */
-#ifdef VMS
-#ifndef ETAGS
-#define ETAGS
-#endif
+/* 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
 #endif
 
 /* Exit codes for success and failure.  */
 #ifdef VMS
-#define        GOOD    (1)
-#define BAD    (0)
+#define        GOOD    1
+#define BAD    0
 #else
-#define        GOOD    (0)
-#define        BAD     (1)
+#define        GOOD    0
+#define        BAD     1
 #endif
 
 /*
@@ -86,8 +98,6 @@ extern int strcmp ();
  *
  *     long GET_CHARNO (pos)
  *                             returns absolute char number.
- *     long GET_COOKIE (pos)
- *                             returns ftell () cookie.
  *     void SET_FILEPOS (pos, fp, charno)
  *         FILE *fp; long charno;
  *                             sets `pos' from the current file
@@ -105,44 +115,34 @@ extern int strcmp ();
  *
  * Implementation notes: the `+ 0' is to enforce rvalue-ness.
  */
-#ifdef VMS
-typedef struct
-{
-  long cookie;
-  long charno;
-} FILEPOS;
 
-#define GET_CHARNO(pos)        ((pos).charno + 0)
-#define GET_COOKIE(pos)        ((pos).cookie + 0)
-#define SET_FILEPOS(pos, fp, cno) \
-    ((void) ((pos).cookie = ftell (fp), (pos).charno = (cno)))
-#else
 #ifndef DEBUG
- /* UNIX real implementation */
+#define DEBUG FALSE
+
+ /* real implementation */
 typedef long FILEPOS;
-#define GET_CHARNO(pos)        ((pos) + 0)
-#define GET_COOKIE(pos)        GET_CHARNO (pos)
-#define SET_FILEPOS(pos, fp, cno)      ((void) ((pos) = (cno)))
+#define GET_CHARNO(pos)                ((pos) + 0)
+#define SET_FILEPOS(pos,fp,cno)        ((void) ((pos) = (cno)))
+
 #else
- /* UNIX debugging implementation */
+#undef DEBUG
+#define DEBUG TRUE
+
+ /* debugging implementation */
 typedef struct
 {
   long charno;
 } FILEPOS;
 
 #define GET_CHARNO(pos)        ((pos).charno + 0)
-#define GET_COOKIE(pos)        GET_CHARNO (pos)
-#define SET_FILEPOS(pos, fp, cno)                                      \
+#define SET_FILEPOS(pos,fp,cno)                                                \
     ((void) ((pos).charno = (cno),                                     \
             (cno) != ftell (fp) ? (error ("SET_FILEPOS inconsistency"), 0) \
                                 : 0))
 #endif
-#endif
 
-#define streq(s, t)    (strcmp (s, t) == 0)
-#define strneq(s, t, n)        (strncmp (s, t, n) == 0)
-#define        reg     register
-#define        logical char
+#define streq(s,t)     (strcmp (s, t) == 0)
+#define strneq(s,t,n)  (strncmp (s, t, n) == 0)
 
 #define        TRUE    1
 #define        FALSE   0
@@ -151,16 +151,17 @@ typedef struct
 #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        isgood(arg)     (_gd[arg])      /* T if char can be after ')'   */
 
 #define        max(I1,I2)      ((I1) > (I2) ? (I1) : (I2))
 
+typedef int logical;
+
 struct nd_st
 {                              /* sorting structure                    */
   char *name;                  /* function or type name        */
   char *file;                  /* file name                    */
   logical is_func;             /* use pattern or line no       */
-  logical rewritten;           /* list name separately         */
+  logical named;               /* list name separately         */
   logical been_warned;         /* set if noticed dup           */
   int lno;                     /* line number tag is on        */
   long cno;                    /* character number line starts on */
@@ -168,43 +169,67 @@ struct nd_st
   struct nd_st *left, *right;  /* left and right sons          */
 };
 
-long ftell ();
 typedef struct nd_st NODE;
 
-logical gotone,                        /* found a func already on line */
- /* boolean "func" (see init)  */
-  header_file,                 /* TRUE if .h file, FALSE o.w.  */
-  _wht[0177], _etk[0177], _itk[0177], _btk[0177], _gd[0177];
+/* boolean "functions" (see init)      */
+logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
 
+char *cwd;                     /* current working directory */
+char *tagfiledir;              /* directory of tagfile */
 
 char *concat ();
-char *savenstr ();
-char *savestr ();
-char *xmalloc ();
-char *xrealloc ();
-int L_isdef ();
-int PF_funcs ();
+char *savenstr (), *savestr ();
+char *etags_strchr (), *etags_strrchr ();
+char *etags_getcwd ();
+char *relative_filename (), *absolute_filename (), *absolute_dirname ();
+char *xmalloc (), *xrealloc ();
+
+typedef void Lang_function ();
+#if FALSE                              /* many compilers barf on this */
+Lang_function Asm_labels;
+Lang_function default_C_entries;
+Lang_function C_entries;
+Lang_function Cplusplus_entries;
+Lang_function Cstar_entries;
+Lang_function Fortran_functions;
+Lang_function Yacc_entries;
+Lang_function Lisp_functions;
+Lang_function Pascal_functions;
+Lang_function Prolog_functions;
+Lang_function Scheme_functions;
+Lang_function TeX_functions;
+Lang_function just_read_file;
+#else                          /* so let's write it this way */
+void Asm_labels ();
+void default_C_entries ();
+void C_entries ();
+void Cplusplus_entries ();
+void Cstar_entries ();
+void Fortran_functions ();
+void Yacc_entries ();
+void Lisp_functions ();
+void Pascal_functions ();
+void Prolog_functions ();
+void Scheme_functions ();
+void TeX_functions ();
+void just_read_file ();
+#endif
+
+logical get_language ();
 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 ();
+long readline_internal ();
+#ifdef ETAGS_REGEXPS
+void add_regex ();
+#endif
 void add_node ();
 void error ();
-void fatal ();
-void find_entries ();
+void fatal (), pfatal ();
+logical find_entries ();
 void free_tree ();
 void getit ();
-void getline ();
 void init ();
 void initbuffer ();
-void initbuffer ();
 void pfnote ();
 void process_file ();
 void put_entries ();
@@ -217,128 +242,15 @@ void takeprec ();
  * SYNOPSIS
  *     Type *xnew (int n, Type);
  */
-#define xnew(n, Type)  ((Type *) xmalloc ((n) * sizeof (Type)))
-\f
-
+#define xnew(n,Type)   ((Type *) xmalloc ((n) * sizeof (Type)))
 
 /*
- *     Symbol table stuff.
- *
- * Should probably be implemented with hash table; linked list for now.
+ *     Symbol table types.
  */
-
 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)
 \f
 
 
@@ -348,105 +260,42 @@ typedef struct
 {
   char *p;
   int len;
-  FILEPOS linestart;
   LINENO lineno;
-  logical rewritten;
+  logical named;
 } TOKEN;
-\f
-
- /* typedefs are recognized using a simple finite automaton.
-  * tydef is its state variable.
-  */
-typedef enum
-{
-  none, begin, middle, end
-} TYST;
-
-TYST tydef = none;
-
-
- /* 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 */
-  sinbody                      /* in a class body: recognize member func defs */
-} STRUCTST;
-STRUCTST structdef = snone;
-/*
- * When structdef is stagseen, scolonseen, or sinbody, structtag is the
- * struct tag, and structkey is the preceding struct-like keyword.
- */
-char structtag[512];
-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 */
-  dignorerest                  /* ignore rest of line */
-} DEFINEST;
-DEFINEST definedef;
-
-/*
- * LEVEL_OK_FOR_FUNCDEF allows C++ function definition within class body.
- * Currently tydef and structdef stuff (typedefs and struct definitions) are
- * only noticed when level==0, but that may change.
- *
- * Note that this macro may only be evaluated inside C_entries().  It is
- * for self-documentation only.
- */
-#define LEVEL_OK_FOR_FUNCDEF()                                 \
-       (level==0 || c_ext && level==1 && structdef==sinbody)
 
-/* C extensions.  Currently all listed extensions are C++ dialects, so
- * `c_ext' is used as an abbreviation for `c_ext&C_PLPL'.  If a non-C++
- * dialect is added, this must change.
+/* C extensions.
  */
-#define C_PLPL 0x1             /* C++ */
-#define C_STAR 0x3             /* C* */
+#define C_PLPL 0x00001         /* C++ */
+#define C_STAR 0x00003         /* C* */
+#define YACC   0x10000         /* yacc file */
 
 char searchar = '/';           /* use /.../ searches           */
 
 LINENO lineno;                 /* line number of current line */
 long charno;                   /* current character number */
-FILEPOS linepos;               /* start of line (C only) */
-FILEPOS prev_linepos;          /* start of previous line (C only) */
 
 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                          */
- *endtk = " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars                   */
*begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$",    /* token starting chars                 */
- *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789",   /* valid in-token chars                 */
*notgd = ",;";                        /* non-valid after-function chars       */
-
-int file_num;                  /* current file number                  */
+ *tagfile,                     /* output file                          */
+ *white = " \f\t\n\013",       /* white chars                          */
+ *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
                              /* token starting chars                 */
+ *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
                              /* valid in-token chars                 */
+ *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
+
 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 */
+                               /* member functions. */
 int constantypedefs;           /* -d: create tags for C #define and enum */
-                               /* constants. Default under etags.  Enum */
-                               /* constants not implemented. */
+                               /* constants.  Enum consts not implemented. */
                                /* -D: opposite of -d.  Default under ctags. */
 int update;                    /* -u: update tags */
 int vgrind_style;              /* -v: create vgrind style index output */
@@ -458,17 +307,40 @@ int noindentypedefs;              /* -S: ignore indentation in C */
 /* Name this program was invoked with.  */
 char *progname;
 
-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.  */
+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' },
+  { "help",                    no_argument,       NULL, 'H' },
+  { "ignore-indentation",      no_argument,       NULL, 'S' },
+  { "include",                 required_argument, NULL, 'i' },
+  { "language",                 required_argument, NULL, 'l' },
+  { "no-defines",              no_argument,       NULL, 'D' },
+  { "no-regex",                        no_argument,       NULL, 'R' },
+  { "no-warn",                 no_argument,       NULL, 'w' },
+  { "output",                  required_argument, NULL, 'o' },
+  { "regex",                   required_argument, NULL, 'r' },
+  { "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 }
+};
 
-/* 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.  */
+FILE *tagf;                    /* ioptr for tags file */
+NODE *head;                    /* the head of the binary tree of tags */
+logical permit_duplicates = TRUE; /* 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;
@@ -476,10 +348,407 @@ struct linebuffer
 };
 
 struct linebuffer lb;          /* the current line */
-struct linebuffer lb1;         /* sometimes, a previous line in which a token lies */
 struct linebuffer filename_lb; /* used to read in filenames */
+struct
+{
+  FILEPOS linepos;
+  struct linebuffer lb;                /* used by C_entries instead of lb */
+} lbs[2];
+
+#ifdef ETAGS_REGEXPS
+/* Structure defining a regular expression.  Elements are 
+   the compiled pattern, and the name string. */
+struct pattern
+{
+  struct re_pattern_buffer *pattern;
+  struct re_registers regs;
+  char *name_pattern;
+  logical error_signaled;
+};
+
+/* Number of regexps found. */
+int num_patterns = 0;
+
+/* Array of all regexps. */
+struct pattern *patterns = NULL;
+#endif /* ETAGS_REGEXPS */
+
+/* Language stuff. */
+struct lang_entry
+{
+  char *extension;
+  Lang_function *function;
+};
+
+/* Table of language names and corresponding functions. */
+/* It is ok for a given function to be listed under more than one
+   name.  I just didn't. */
+/* "auto" language reverts to default behavior. */
+struct lang_entry lang_names[] =
+{
+  { "asm", Asm_labels },
+  { "c", default_C_entries },
+  { "c++", Cplusplus_entries },
+  { "c*", Cstar_entries },
+  { "fortran", Fortran_functions },
+  { "lisp", Lisp_functions },
+  { "none", just_read_file },
+  { "pascal", Pascal_functions },
+  { "scheme" , Scheme_functions },
+  { "tex", TeX_functions },
+  { "auto", NULL },
+  { NULL, NULL }
+};
+
+/* Table of file extensions and corresponding language functions. */
+struct lang_entry lang_extensions[] =
+{
+  /* Assume that ".s" or ".a" is assembly code. -wolfgang.
+     Or even ".sa". */
+  { "a", Asm_labels },         /* Unix assembler */
+  { "asm", Asm_labels },       /* Microcontroller assembly */
+  { "def", Asm_labels },       /* BSO/Tasking definition includes  */
+  { "inc", Asm_labels },       /* Microcontroller include files */
+  { "ins", Asm_labels },       /* Microcontroller include files */
+  { "s", Asm_labels },
+  { "sa", Asm_labels },                /* Unix assembler */
+  { "src", Asm_labels },       /* BSO/Tasking C compiler output */
+
+  /* .aux, .bbl, .clo, .cls, .dtx or .tex implies LaTeX source code. */
+  { "aux", TeX_functions },
+  { "bbl", TeX_functions },
+  { "clo", TeX_functions },
+  { "cls", TeX_functions },
+  { "dtx", TeX_functions },
+  { "sty", TeX_functions },
+  { "tex", TeX_functions },
+
+  /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
+  { "cl", Lisp_functions },
+  { "clisp", Lisp_functions },
+  { "el", Lisp_functions },
+  { "l", Lisp_functions },
+  { "lisp", Lisp_functions },
+  { "lsp", Lisp_functions },
+
+  /* .scm or .sm or .scheme implies scheme source code */
+  { "SCM", Scheme_functions },
+  { "SM", Scheme_functions },
+  { "oak", Scheme_functions },
+  { "sch", Scheme_functions },
+  { "scheme", Scheme_functions },
+  { "scm", Scheme_functions },
+  { "sm", Scheme_functions },
+  { "t", Scheme_functions },
+  /* FIXME Can't do the `SCM' or `scm' prefix with a version number */
+
+  /* Note that ".c" and ".h" can be considered C++, if the --c++
+     flag was given.  That is why default_C_entries is called here. */
+  { "c", default_C_entries },
+  { "h", default_C_entries },
+
+  /* .C or .H or .cpp or .cxx or .hxx or .hh or .cc or .cpp: a C++ file */
+  { "C", Cplusplus_entries },
+  { "H", Cplusplus_entries },
+  { "cc", Cplusplus_entries },
+  { "cpp", Cplusplus_entries },
+  { "cxx", Cplusplus_entries },
+  { "hh", Cplusplus_entries },
+  { "hxx", Cplusplus_entries },
+
+  /* .y: a yacc file */
+  { "y", Yacc_entries },
+
+  /* .cs or .hs: a C* file */
+  { "cs", Cstar_entries },
+  { "hs", Cstar_entries },
+
+  /* .f and .for are FORTRAN. */
+  { "F", Fortran_functions },
+  { "f", Fortran_functions },
+  { "for", Fortran_functions },
+
+  /* .pl implies prolog source code */
+  { "pl", Prolog_functions },
+
+  /* .p or .pas: a Pascal file */
+  { "p", Pascal_functions },
+  { "pas", Pascal_functions },
+
+  { NULL, NULL }
+};
+
+/* Non-NULL if language fixed. */
+Lang_function *lang_func = NULL;
+
+\f
+void
+print_language_names ()
+{
+  struct lang_entry *name, *ext;
+
+  puts ("\nThese are the currently supported languages, along with the\n\
+default extensions for files:");
+  for (name = lang_names; name->extension; ++name)
+    {
+      printf ("\t%s\t", name->extension);
+      for (ext = lang_extensions; ext->extension; ++ext)
+       if (name->function == ext->function)
+         printf (" .%s", ext->extension);
+      puts ("");
+    }
+  puts ("Where `auto' means use default language for files based on filename\n\
+extension, and `none' means only do regexp processing on files.\n\
+If no language is specified and no extension is found for some file,\n\
+Fortran is tried first; if no tags are found, C is tried next.");
+}
+
+void
+print_version ()
+{
+#ifdef VERSION
+  printf ("%s for Emacs version %s.\n", (CTAGS) ? "CTAGS" : "ETAGS", VERSION);
+#else
+  printf ("%s for Emacs version 19.\n", (CTAGS) ? "CTAGS" : "ETAGS");
+#endif  
+
+  exit (GOOD);
+}
+
+void
+print_help ()
+{
+  printf ("These are the options accepted by %s.  You may use unambiguous\n\
+abbreviations for the long option names.  A - as file name means read\n\
+names from stdin.\n\n", progname);
+
+  puts ("-a, --append\n\
+        Append tag entries to existing tags file.");
+
+  if (CTAGS)
+    puts ("-B, --backward-search\n\
+        Write the search commands for the tag entries using '?', the\n\
+        backward-search command instead of '/', the forward-search command.");
+
+  puts ("-C, --c++\n\
+        Treat files whose extension defaults to C language as C++ files.");
+
+  if (CTAGS)
+    puts ("-d, --defines\n\
+        Create tag entries for constant C #defines, too.");
+  else
+    puts ("-D, --no-defines\n\
+        Don't create tag entries for constant C #defines.  This makes\n\
+       the tags file smaller.");
+
+  if (!CTAGS)
+    {
+      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.");
+      puts ("-l LANG, --language=LANG\n\
+        Force the following files to be considered as written in the\n\
+       named language up to the next --language=LANG option.");
+    }
+
+#ifdef ETAGS_REGEXPS
+  puts ("-r /REGEXP/, --regex=/REGEXP/\n\
+        Make a tag for each line matching pattern REGEXP in the\n\
+       following files.  REGEXP is anchored (as if preceded by ^).\n\
+       The form /REGEXP/NAME/ creates a named tag.  For example Tcl\n\
+       named tags can be created with:\n\
+       --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
+  puts ("-R, --no-regex\n\
+        Don't create tags from regexps for the following files.");
+#endif /* ETAGS_REGEXPS */
+  puts ("-o FILE, --output=FILE\n\
+        Write the tags to FILE.");
+  puts ("-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\
+        definition in C and C++.");
+
+  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\
+        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\
+        tag file than to use this.");
+      puts ("-v, --vgrind\n\
+        Generates an index of items intended for human consumption,\n\
+        similar to the output of vgrind.  The index is sorted, and\n\
+        gives the page number of each item.");
+      puts ("-w, --no-warn\n\
+        Suppress warning messages about entries defined in multiple\n\
+        files.");
+      puts ("-x, --cxref\n\
+        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\
+        which you like.");
+    }
+
+  puts ("-V, --version\n\
+        Print the version of the program.\n\
+-h, --help\n\
+        Print this help message.");
+
+  print_language_names ();
+
+  exit (GOOD);
+}
+
 \f
+enum argument_type
+{
+  at_language,
+  at_regexp,
+  at_filename
+};
+
+/* This structure helps us allow mixing of --lang and filenames. */
+typedef struct
+{
+  enum argument_type arg_type;
+  char *what;
+  Lang_function *function;
+} ARGUMENT;
+
+#ifdef VMS                     /* VMS specific functions */
+
+#define        EOS     '\0'
+
+/* This is a BUG!  ANY arbitrary limit is a BUG!
+   Won't someone please fix this?  */
+#define        MAX_FILE_SPEC_LEN       255
+typedef struct {
+  short   curlen;
+  char    body[MAX_FILE_SPEC_LEN + 1];
+} vspec;
+
+/*
+ v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
+ returning in each successive call the next filename matching the input
+ spec. The function expects that each in_spec passed
+ to it will be processed to completion; in particular, up to and
+ including the call following that in which the last matching name
+ is returned, the function ignores the value of in_spec, and will
+ only start processing a new spec with the following call. 
+ If an error occurs, on return out_spec contains the value
+ of in_spec when the error occurred.
+
+ With each successive filename returned in out_spec, the
+ function's return value is one. When there are no more matching
+ names the function returns zero. If on the first call no file
+ matches in_spec, or there is any other error, -1 is returned. 
+*/
+
+#include       <rmsdef.h>
+#include       <descrip.h>
+#define                OUTSIZE MAX_FILE_SPEC_LEN
+short
+fn_exp (out, in)
+     vspec *out;
+     char *in;
+{
+  static long context = 0;
+  static struct dsc$descriptor_s o;
+  static struct dsc$descriptor_s i;
+  static logical pass1 = TRUE;
+  long status;
+  short retval;
+
+  if (pass1)
+    {
+      pass1 = FALSE;
+      o.dsc$a_pointer = (char *) out;
+      o.dsc$w_length = (short)OUTSIZE;
+      i.dsc$a_pointer = in;
+      i.dsc$w_length = (short)strlen(in);
+      i.dsc$b_dtype = DSC$K_DTYPE_T;
+      i.dsc$b_class = DSC$K_CLASS_S;
+      o.dsc$b_dtype = DSC$K_DTYPE_VT;
+      o.dsc$b_class = DSC$K_CLASS_VS;
+    }
+  if ((status = lib$find_file(&i, &o, &context, 0, 0)) == RMS$_NORMAL)
+    {
+      out->body[out->curlen] = EOS;
+      return 1;
+    }
+  else if (status == RMS$_NMF)
+    retval = 0;
+  else
+    {
+      strcpy(out->body, in);
+      retval = -1;
+    }
+  lib$find_file_end(&context);
+  pass1 = TRUE;
+  return retval;
+}      
+
+/*
+  v1.01 nmm 19-Aug-85 gfnames - return in successive calls the 
+  name of each file specified by the provided arg expanding wildcards.
+*/
+char *
+gfnames (arg, p_error)
+     char *arg;
+     logical *p_error;
+{
+  static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
+
+  switch (fn_exp (&filename, arg))
+    {
+    case 1:
+      *p_error = FALSE;
+      return filename.body;
+    case 0:
+      *p_error = FALSE;
+      return NULL;
+    default:
+      *p_error = TRUE;
+      return filename.body;
+    }
+}
+
+#ifndef OLD  /* Newer versions of VMS do provide `system'.  */
+system (cmd)
+     char *cmd;
+{
+  fprintf (stderr, "system() function not implemented under VMS\n");
+}
+#endif
 
+#define        VERSION_DELIM   ';'
+char *massage_name (s)
+     char *s;
+{
+  char *start = s;     
+
+  for ( ; *s; s++)
+    if (*s == VERSION_DELIM)
+      {
+       *s = EOS;
+       break;
+      }
+    else
+      *s = tolower(*s);
+  return start;
+}
+#endif /* VMS */
+
+\f
 void
 main (argc, argv)
      int argc;
@@ -487,218 +756,262 @@ main (argc, argv)
 {
   char cmd[100];
   int i;
-  int outfflag = 0;
   unsigned int nincluded_files = 0;
-  char **included_files = (char **) alloca (argc * sizeof (char *));
+  char **included_files = xnew (argc, char *);
   char *this_file;
+  ARGUMENT *argbuffer;
+  int current_arg = 0, file_count = 0;
 #ifdef VMS
-  char got_err;
-
-  extern char *gfnames ();
-  extern char *massage_name ();
+  logical got_err;
 #endif
+#ifdef DOS_NT
+  _fmode = O_BINARY;   /* all of files are treated as binary files */
+#endif /* DOS_NT */
 
   progname = argv[0];
 
-#ifndef CTAGS
-  emacs_tags_format = 1;
-#else
-  emacs_tags_format = 0;
-#endif
+  /* Allocate enough no matter what happens.  Overkill, but each one
+     is small. */
+  argbuffer = xnew (argc, ARGUMENT);
+
+#ifdef ETAGS_REGEXPS
+  /* Set syntax for regular expression routines. */
+  re_set_syntax (RE_SYNTAX_EMACS);
+#endif /* ETAGS_REGEXPS */
 
   /*
    * If etags, always find typedefs and structure tags.  Why not?
    * Also default is to find macro constants.
    */
-  if (emacs_tags_format)
+  if (!CTAGS)
     typedefs = typedefs_and_cplusplus = constantypedefs = 1;
 
-  for (; argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0'; argc--, argv++)
+  while (1)
     {
-      for (i = 1; argv[1][i]; i++)
+      int opt = getopt_long (argc, argv,
+                            "-aCdDf:l:o:r:RStTi:BuvxwVhH", longopts, 0);
+
+      if (opt == EOF)
+       break;
+
+      switch (opt)
        {
-         switch (argv[1][i])
-           {
-             /* Common options. */
-           case 'a':
-             append_to_tagfile++;
-             break;
-           case 'C':
-             cplusplus = 1;
-             break;
-           case 'd':
-             constantypedefs = 1;
-             break;
-           case 'D':
-             constantypedefs = 0;
-             break;
-           case 'o':
-             if (outfflag)
-               {
-                 fprintf (stderr,
-                          "%s: -o flag may only be given once\n", progname);
-                 goto usage;
-               }
-             outfflag++, argc--;
-             argv++;
-             if (argc <= 1 || argv[1][0] == '\0')
-               {
-                 fprintf (stderr,
-                          "%s: -o flag must be followed by a filename\n",
-                          progname);
-                 goto usage;
-               }
-             outfile = argv[1];
-             goto next_arg;
-           case 'S':
-             noindentypedefs++;
-             break;
-           case 't':
-             typedefs++;
-             break;
-           case 'T':
-             typedefs++;
-             typedefs_and_cplusplus++;
-             break;
+       case 0:
+         /* If getopt returns 0, then it has already processed a
+            long-named option.  We should do nothing.  */
+         break;
 
-             /* Etags options */
-           case 'i':
-             if (!emacs_tags_format)
-               goto usage;
-             --argc;
-             ++argv;
-             if (argc <= 1 || argv[1][0] == '\0')
-               {
-                 fprintf (stderr,
-                          "%s: -i flag must be followed by a filename\n",
-                          progname);
-                 goto usage;
-               }
-             included_files[nincluded_files++] = argv[1];
-             goto next_arg;
-
-             /* 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;
+       case 1:
+         /* This means that a filename has been seen.  Record it. */
+         argbuffer[current_arg].arg_type = at_filename;
+         argbuffer[current_arg].what = optarg;
+         ++current_arg;
+         ++file_count;
+         break;
 
-           default:
+         /* Common options. */
+       case 'a':
+         append_to_tagfile++;
+         break;
+       case 'C':
+         cplusplus = 1;
+         break;
+       case 'd':
+         constantypedefs = 1;
+         break;
+       case 'D':
+         constantypedefs = 0;
+         break;
+       case 'f':               /* for compatibility with old makefiles */
+       case 'o':
+         if (tagfile)
+           {
+             fprintf(stderr,
+                     "%s: -%c flag may only be given once.\n", progname, opt);
              goto usage;
            }
-       }
-    next_arg:;
-    }
-
-  if (argc <= 1)
-    {
-    usage:
-      fprintf (stderr, "Usage:\n");
-#ifndef CTAGS
-      fprintf (stderr, "\tetags [-aDiS] [-o tagsfile] file ...\n");
-#else
-      fprintf (stderr, "\tctags [-aBdeFTStuwvx] [-o tagsfile] file ...\n");
-#endif
-      exit (BAD);
-    }
-
-  if (outfile == 0)
-    {
-      outfile = emacs_tags_format ? "TAGS" : "tags";
-    }
-
-  init ();                     /* set up boolean "functions"           */
+         tagfile = optarg;
+         break;
+       case 'l':
+         if (!get_language (optarg, &argbuffer[current_arg].function))
+           {
+             fprintf (stderr, "%s: language \"%s\" not recognized.\n",
+                      progname, optarg);
+             goto usage;
+           }
+         argbuffer[current_arg].arg_type = at_language;
+         ++current_arg;
+         break;
+#ifdef ETAGS_REGEXPS
+       case 'r':
+         argbuffer[current_arg].arg_type = at_regexp;
+         argbuffer[current_arg].what = optarg;
+         ++current_arg;
+         break;
+       case 'R':
+         argbuffer[current_arg].arg_type = at_regexp;
+         argbuffer[current_arg].what = NULL;
+         ++current_arg;
+         break;
+#endif /* ETAGS_REGEXPS */
+       case 'S':
+         noindentypedefs++;
+         break;
+       case 'V':
+         print_version ();
+         break;
+       case 'h':
+       case 'H':
+         print_help ();
+         break;
+       case 't':
+         typedefs++;
+         break;
+       case 'T':
+         typedefs++;
+         typedefs_and_cplusplus++;
+         break;
+#if (!CTAGS)
+         /* Etags options */
+       case 'i':
+         included_files[nincluded_files++] = optarg;
+         break;
+#else /* CTAGS */
+         /* Ctags options. */
+       case 'B':
+         searchar = '?';
+         break;
+       case 'u':
+         update++;
+         break;
+       case 'v':
+         vgrind_style++;
+         /*FALLTHRU*/
+       case 'x':
+         cxref_style++;
+         break;
+       case 'w':
+         no_warnings++;
+         break;
+#endif /* CTAGS */
+       default:
+         fprintf (stderr,
+                  "%s: -%c flag not recognised.\n", progname, opt);
+         goto usage;
+       }
+    }
+
+  for (; optind < argc; ++optind)
+    {
+      argbuffer[current_arg].arg_type = at_filename;
+      argbuffer[current_arg].what = argv[optind];
+      ++current_arg;
+      ++file_count;
+    }
+
+  if (nincluded_files == 0 && file_count == 0)
+    {
+      fprintf (stderr, "%s: No input files specified.\n", progname);
+
+    usage:
+      fprintf (stderr, "%s: Try `%s --help' for a complete list of options.\n",
+              progname, progname);
+      exit (BAD);
+    }
+
+  if (tagfile == NULL)
+    {
+      tagfile = CTAGS ? "tags" : "TAGS";
+    }
+  cwd = etags_getcwd ();       /* the current working directory */
+  strcat (cwd, "/");
+  if (streq (tagfile, "-"))
+    {
+      tagfiledir = cwd;
+    }
+  else
+    {
+      tagfiledir = absolute_dirname (tagfile, cwd);
+    }
+
+  init ();                     /* set up boolean "functions" */
 
   initbuffer (&lb);
-  initbuffer (&lb1);
+  initbuffer (&lbs[0].lb);
+  initbuffer (&lbs[1].lb);
   initbuffer (&filename_lb);
-  /*
-   * loop through files finding functions
-   */
-  if (emacs_tags_format)
+
+  if (!CTAGS)
     {
-      if (streq (outfile, "-"))
-       outf = stdout;
+      if (streq (tagfile, "-"))
+       tagf = stdout;
       else
-       outf = fopen (outfile, append_to_tagfile ? "a" : "w");
-      if (!outf)
-       {
-         perror (outfile);
-         exit (1);
-       }
+       tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
+      if (tagf == NULL)
+       pfatal (tagfile);
     }
 
-  file_num = 1;
-#ifdef VMS
-  for (argc--, argv++;
-       (this_file = gfnames (&argc, &argv, &got_err)) != NULL; file_num++)
+  /*
+   * Loop through files finding functions.
+   */
+  for (i = 0; i < current_arg; ++i)
     {
-      if (got_err)
-       {
-         error ("Can't find file %s\n", this_file);
-         argc--, argv++;
-       }
-      else
+      switch (argbuffer[i].arg_type)
        {
-         this_file = massage_name (this_file);
-#if 0
-       }
-    }                          /* solely to balance out the ifdef'd parens above */
+       case at_language:
+         lang_func = argbuffer[i].function;
+         break;
+#ifdef ETAGS_REGEXPS
+       case at_regexp:
+         add_regex (argbuffer[i].what);
+         break;
 #endif
+       case at_filename:
+#ifdef VMS
+         while ((this_file = gfnames (argbuffer[i].what, &got_err)) != NULL)
+           {
+             if (got_err)
+               {
+                 error ("Can't find file %s\n", this_file);
+                 argc--, argv++;
+               }
+             else
+               {
+                 this_file = massage_name (this_file);
+               }
 #else
-  for (; file_num < argc; file_num++)
-    {
-      this_file = argv[file_num];
-      if (1)
-       {
+             this_file = argbuffer[i].what;
 #endif
-         /* Input file named "-" means read file names from stdin
-            and use them.  */
-         if (streq (this_file, "-"))
-           {
-             while (!feof (stdin))
+             /* Input file named "-" means read file names from stdin
+                and use them. */
+             if (streq (this_file, "-"))
                {
-                 (void) readline (&filename_lb, stdin);
-                 if (strlen (filename_lb.buffer) > 0)
-                   process_file (filename_lb.buffer);
+                 while (!feof (stdin))
+                   {
+                     /* Use readline_internal so that regexp matching */
+                     /* is not done on filenames. */
+                     (void) readline_internal (&filename_lb, stdin);
+                     if (strlen (filename_lb.buffer) > 0)
+                       process_file (filename_lb.buffer);
+                   }
                }
+             else
+               process_file (this_file);
+#ifdef VMS
            }
-         else
-           process_file (this_file);
+#endif
+         break;
        }
     }
 
-  if (emacs_tags_format)
+  if (!CTAGS)
     {
       while (nincluded_files-- > 0)
-       fprintf (outf, "\f\n%s,include\n", *included_files++);
+       fprintf (tagf, "\f\n%s,include\n", *included_files++);
 
-      (void) fclose (outf);
-      exit (0);
+      (void) fclose (tagf);
+      exit (GOOD);
     }
 
   if (cxref_style)
@@ -706,34 +1019,59 @@ main (argc, argv)
       put_entries (head);
       exit (GOOD);
     }
-  if (update)                  /* update cannot be set under VMS */
+  if (update)
     {
-      for (i = 1; i < argc; i++)
+      for (i = 0; i < current_arg; ++i)
        {
+         if (argbuffer[i].arg_type == at_language)
+           continue;
          sprintf (cmd,
                   "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
-                  outfile, argv[i], outfile);
+                  tagfile, argbuffer[i].what, tagfile);
          (void) system (cmd);
        }
       append_to_tagfile++;
     }
-  outf = fopen (outfile, append_to_tagfile ? "a" : "w");
-  if (outf == NULL)
+  tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
+  if (tagf == NULL)
     {
-      perror (outfile);
+      perror (tagfile);
       exit (GOOD);
     }
   put_entries (head);
-  (void) fclose (outf);
+  (void) fclose (tagf);
   if (update)
     {
-      sprintf (cmd, "sort %s -o %s", outfile, outfile);
+      sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
       (void) system (cmd);
     }
   exit (GOOD);
 }
 
 
+/*
+ * Set the language, given the name.
+ */
+logical
+get_language (language, func)
+     char *language;
+     Lang_function **func;
+{
+  struct lang_entry *lang;
+
+  for (lang = lang_names; lang->extension; ++lang)
+    {
+      if (streq (language, lang->extension))
+       {
+         *func = lang->function;
+         return TRUE;
+       }
+    }
+
+  return FALSE;
+}
+
+
 /*
  * This routine is called on each file argument.
  */
@@ -743,31 +1081,36 @@ process_file (file)
 {
   struct stat stat_buf;
 
-  stat (file, &stat_buf);
-  if (!(stat_buf.st_mode & S_IFREG) || !(stat_buf.st_mode & S_IFLNK))
+  if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
     {
       fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
       return;
     }
-
-  if (streq (file, outfile) && !streq (outfile, "-"))
+  if (streq (file, tagfile) && !streq (tagfile, "-"))
     {
       fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
       return;
     }
-  if (emacs_tags_format)
+  if (!find_entries (file))
     {
-      char *cp = rindex (file, '/');
-      if (cp)
-       ++cp;
-      else
-       cp = file;
+      return;
     }
-  find_entries (file);
-  if (emacs_tags_format)
+  if (!CTAGS)
     {
-      fprintf (outf, "\f\n%s,%d\n",
-              file, total_size_of_entries (head));
+      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. */
+         filename = relative_filename (file, tagfiledir);
+       }
+      fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
       put_entries (head);
       free_tree (head);
       head = NULL;
@@ -775,8 +1118,8 @@ process_file (file)
 }
 
 /*
- * This routine sets up the boolean psuedo-functions which work
- * by seting boolean flags dependent upon the corresponding character
+ * This routine sets up the boolean pseudo-functions which work
+ * by setting boolean flags dependent upon the corresponding character
  * 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"
@@ -785,14 +1128,11 @@ process_file (file)
 void
 init ()
 {
-  reg char *sp;
-  reg int i;
+  register char *sp;
+  register int i;
 
   for (i = 0; i < 0177; i++)
-    {
-      _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
-      _gd[i] = TRUE;
-    }
+    _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
   for (sp = white; *sp; sp++)
     _wht[*sp] = TRUE;
   for (sp = endtk; *sp; sp++)
@@ -801,145 +1141,75 @@ init ()
     _itk[*sp] = TRUE;
   for (sp = begtk; *sp; sp++)
     _btk[*sp] = TRUE;
-  for (sp = notgd; *sp; sp++)
-    _gd[*sp] = FALSE;
   _wht[0] = _wht['\n'];
   _etk[0] = _etk['\n'];
   _btk[0] = _btk['\n'];
   _itk[0] = _itk['\n'];
-  _gd[0] = _gd['\n'];
 }
 
 /*
  * This routine opens the specified file and calls the function
  * which finds the function and type definitions.
  */
-void
+logical
 find_entries (file)
      char *file;
 {
+  FILE *inf;
   char *cp;
-  void prolog_funcs ();
+  struct lang_entry *lang;
+  NODE *old_last_node;
+  extern NODE *last_node;
 
   inf = fopen (file, "r");
   if (inf == NULL)
     {
       perror (file);
-      return;
+      return FALSE;
     }
   curfile = savestr (file);
-  cp = rindex (file, '.');
-
-  header_file = (cp && (streq (cp + 1, "h")));
+  cp = etags_strrchr (file, '.');
 
-  /* .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;
-    }
-  /* Assume that ".s" or ".a" is assembly code. -wolfgang.  */
-  if (cp && (cp[1] == 's' || cp[1] == 'a') && cp[2] == '\0')
+  /* If user specified a language, use it. */
+  if (lang_func != NULL)
     {
-      Asm_funcs (inf);
+      lang_func (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 */
-  if (cp && (cp[1] == 'c' || cp[1] == 'h') && cp[2] == 's' && cp[3] == '\0')
-    {
-      C_entries (C_STAR);
-      goto close_and_return;
+      return TRUE;
     }
-  /* .pl implies prolog source code */
-  if (cp && !strcmp (cp + 1, "pl"))
-    {
-      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;
-    }
-  /* if not a .c or .h or .y file, try fortran */
-  else if (cp && ((cp[1] != 'c' && cp[1] != 'h' && cp[1] != 'y')
-                 || (cp[1] != 0 && cp[2] != 0)))
+
+  if (cp)
     {
-      if (PF_funcs (inf) != 0)
-       goto close_and_return;
-      rewind (inf);            /* no fortran tags found, try C */
+      ++cp;
+      for (lang = lang_extensions; lang->extension; ++lang)
+       {
+         if (streq (cp, lang->extension))
+           {
+             lang->function (inf);
+             fclose (inf);
+             return TRUE;
+           }
+       }
     }
-  C_entries (cplusplus ? C_PLPL : 0);
 
-close_and_return:
-  (void) fclose (inf);
-}
-
-/* Nonzero if string STR is composed of digits.  */
+  /* Try Fortran. */
+  old_last_node = last_node;
+  Fortran_functions (inf);
 
-int
-string_numeric_p (str)
-     char *str;
-{
-  while (*str)
-    {
-      if (*str < '0' || *str > '9')
-       return 0;
-    }
-  return 1;
+  /* No Fortran entries found.  Try C. */
+  if (old_last_node == last_node)
+    default_C_entries (inf);
+  fclose (inf);
+  return TRUE;
 }
 \f
 /* Record a tag. */
 /* Should take a TOKEN* instead!! */
-
 void
-pfnote (name, is_func, rewritten, linestart, linelen, lno, cno)
+pfnote (name, is_func, named, linestart, linelen, lno, cno)
      char *name;               /* tag name */
      logical is_func;          /* function or type name? */
-     logical rewritten;                /* tag different from text of definition? */
+     logical named;            /* tag different from text of definition? */
      char *linestart;
      int linelen;
      int lno;
@@ -950,16 +1220,16 @@ pfnote (name, is_func, rewritten, linestart, linelen, lno, cno)
   char tem[51];
   char c;
 
-  np = (NODE *) malloc (sizeof (NODE));
+  np = xnew (1, NODE);
   if (np == NULL)
     {
-      if (!emacs_tags_format)
+      if (CTAGS)
        {
          /* 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");
+         error ("too many entries to sort", 0);
        }
       put_entries (head);
       free_tree (head);
@@ -967,24 +1237,28 @@ pfnote (name, is_func, rewritten, linestart, linelen, lno, cno)
       np = xnew (1, NODE);
     }
   /* If ctags mode, change name "main" to M<thisfilename>. */
-  if (!emacs_tags_format && !cxref_style && streq (name, "main"))
+  if (CTAGS && !cxref_style && streq (name, "main"))
     {
-      fp = rindex (curfile, '/');
+      fp = etags_strrchr (curfile, '/');
       name = concat ("M", fp == 0 ? curfile : fp + 1, "");
-      fp = rindex (name, '.');
+      fp = etags_strrchr (name, '.');
       if (fp && fp[1] != '\0' && fp[2] == '\0')
        *fp = 0;
-      rewritten = TRUE;
+      named = TRUE;
     }
   np->name = savestr (name);
   np->file = curfile;
   np->is_func = is_func;
-  np->rewritten = rewritten;
+  np->named = named;
   np->lno = lno;
-  /* UNCOMMENT THE +1 HERE: */
-  np->cno = cno /* + 1 */ ;    /* our char numbers are 0-base; emacs's are 1-base */
+  /* Our char numbers are 0-base, because of C language tradition?
+     ctags compatibility?  old versions compatibility?   I don't know.
+     Anyway, since emacs's are 1-base we espect etags.el to take care
+     of the difference.  If we wanted to have 1-based numbers, we would
+     uncomment the +1 below. */
+  np->cno = cno /* + 1 */ ;
   np->left = np->right = 0;
-  if (emacs_tags_format)
+  if (!CTAGS)
     {
       c = linestart[linelen];
       linestart[linelen] = 0;
@@ -995,7 +1269,7 @@ pfnote (name, is_func, rewritten, linestart, linelen, lno, cno)
       linestart = tem;
     }
   np->pat = savestr (linestart);
-  if (emacs_tags_format)
+  if (!CTAGS)
     {
       linestart[linelen] = c;
     }
@@ -1031,13 +1305,13 @@ free_tree (node)
  *     add_node is the only function allowed to add nodes, so it can
  *     maintain state.
  */
+NODE *last_node = NULL;
 void
 add_node (node, cur_node_p)
      NODE *node, **cur_node_p;
 {
   register int dif;
   register NODE *cur_node = *cur_node_p;
-  static NODE *last_node = NULL;/* careful */
 
   if (cur_node == NULL)
     {
@@ -1046,11 +1320,11 @@ add_node (node, cur_node_p)
       return;
     }
 
-  if (emacs_tags_format)
+  if (!CTAGS)
     {
       /* Etags Mode */
-      if (!last_node)
-       fatal ("internal error in add_node");
+      if (last_node == NULL)
+       fatal ("internal error in add_node", 0);
       last_node->right = node;
       last_node = node;
     }
@@ -1088,8 +1362,8 @@ add_node (node, cur_node_p)
       /* Maybe refuse to add duplicate nodes.  */
       if (!permit_duplicates)
        {
-         if (!strcmp (node->name, cur_node->name)
-             && !strcmp (node->file, cur_node->file))
+         if (streq (node->name, cur_node->name)
+             && streq (node->file, cur_node->file))
            return;
        }
 
@@ -1100,9 +1374,9 @@ add_node (node, cur_node_p)
 \f
 void
 put_entries (node)
-     reg NODE *node;
+     register NODE *node;
 {
-  reg char *sp;
+  register char *sp;
 
   if (node == NULL)
     return;
@@ -1112,48 +1386,50 @@ put_entries (node)
 
   /* Output this entry */
 
-  if (emacs_tags_format)
+  if (!CTAGS)
     {
-      if (node->rewritten)
+      if (node->named)
        {
-         fprintf (outf, "%s\177%s\001%d,%d\n",
-                  node->name, node->pat, node->lno, node->cno);
+         fprintf (tagf, "%s\177%s\001%d,%d\n",
+                  node->pat, node->name,
+                  node->lno, node->cno);
        }
       else
        {
-         fprintf (outf, "%s\177%d,%d\n",
-                  node->pat, node->lno, node->cno);
+         fprintf (tagf, "%s\177%d,%d\n",
+                  node->pat,
+                  node->lno, node->cno);
        }
     }
   else if (!cxref_style)
     {
-      fprintf (outf, "%s\t%s\t",
+      fprintf (tagf, "%s\t%s\t",
               node->name, node->file);
 
       if (node->is_func)
        {                       /* a function */
-         putc (searchar, outf);
-         putc ('^', outf);
+         putc (searchar, tagf);
+         putc ('^', tagf);
 
          for (sp = node->pat; *sp; sp++)
            {
              if (*sp == '\\' || *sp == searchar)
-               putc ('\\', outf);
-             putc (*sp, outf);
+               putc ('\\', tagf);
+             putc (*sp, tagf);
            }
-         putc (searchar, outf);
+         putc (searchar, tagf);
        }
       else
        {                       /* a typedef; text pattern inadequate */
-         fprintf (outf, "%d", node->lno);
+         fprintf (tagf, "%d", node->lno);
        }
-      putc ('\n', outf);
+      putc ('\n', tagf);
     }
   else if (vgrind_style)
     fprintf (stdout, "%s %s %d\n",
             node->name, node->file, (node->lno + 63) / 64);
   else
-    fprintf (stdout, "%-16s%4d %-16s %s\n",
+    fprintf (stdout, "%-16s %3d %-16s %s\n",
             node->name, node->lno, node->file, node->pat);
 
   /* Output subentries that follow this one */
@@ -1175,15 +1451,16 @@ number_len (num)
 
 /*
  * 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.
+ * 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.
  */
 int
 total_size_of_entries (node)
-     reg NODE *node;
+     register NODE *node;
 {
-  reg int total;
+  register int total;
 
   if (node == NULL)
     return 0;
@@ -1197,7 +1474,7 @@ total_size_of_entries (node)
       /* Count this entry */
       total += strlen (node->pat) + 1;
       total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
-      if (node->rewritten)
+      if (node->named)
        total += 1 + strlen (node->name);       /* \001name */
     }
 
@@ -1208,617 +1485,984 @@ total_size_of_entries (node)
  * The C symbol tables.
  */
 
-Stab *C_stab, *C_PLPL_stab, *C_STAR_stab;
-
+/* Feed stuff between (but not including) %[ and %] lines to:
+      gperf -c -k1,3 -o -p -r -t
+%[
+struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
+%%
+class,         C_PLPL, st_C_struct
+domain,        C_STAR, st_C_struct
+union,         0,      st_C_struct
+struct,        0,      st_C_struct
+enum,          0,      st_C_enum
+typedef,       0,      st_C_typedef
+define,        0,      st_C_define
+long,          0,      st_C_typespec
+short,         0,      st_C_typespec
+int,           0,      st_C_typespec
+char,          0,      st_C_typespec
+float,         0,      st_C_typespec
+double,        0,      st_C_typespec
+signed,        0,      st_C_typespec
+unsigned,      0,      st_C_typespec
+auto,          0,      st_C_typespec
+void,          0,      st_C_typespec
+extern,        0,      st_C_typespec
+static,        0,      st_C_typespec
+const,         0,      st_C_typespec
+volatile,      0,      st_C_typespec
+%]
+and replace lines between %< and %> with its output. */
+/*%<*/
+/* C code produced by gperf version 1.8.1 (K&R C version) */
+/* Command-line: gperf -c -k1,3 -o -p -r -t  */
+
+
+struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
+
+#define MIN_WORD_LENGTH 3
+#define MAX_WORD_LENGTH 8
+#define MIN_HASH_VALUE 10
+#define MAX_HASH_VALUE 62
 /*
- * SYNOPSIS
- *     Stab *get_C_stab (int c_ext);
- */
-#define get_C_stab(c_ext) ((c_ext&C_STAR) ? C_STAR_stab :              \
-                          c_ext ? C_PLPL_stab :                        \
-                          C_stab)
+   21 keywords
+   53 is the maximum key range
+*/
+
+static int
+hash (str, len)
+     register char  *str;
+     register int  len;
+{
+  static unsigned char hash_table[] =
+    {
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
+     62, 62, 62, 62, 62, 62, 62,  2, 62,  7,
+      6,  9, 15, 30, 62, 24, 62, 62,  1, 24,
+      7, 27, 13, 62, 19, 26, 18, 27,  1, 62,
+     62, 62, 62, 62, 62, 62, 62, 62,
+  };
+  return len + hash_table[str[2]] + hash_table[str[0]];
+}
 
-void
-add_keyword (stab, sym, type)
-     Stab *stab;
-     char *sym;
-     enum sym_type type;
+struct C_stab_entry *
+in_word_set  (str, len)
+     register char *str;
+     register int len;
 {
-  stab_search (stab, sym, strlen (sym))->type = type;
+
+  static struct C_stab_entry  wordlist[] =
+    {
+      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
+      {"",}, 
+      {"volatile",     0,      st_C_typespec},
+      {"",}, 
+      {"long",         0,      st_C_typespec},
+      {"char",         0,      st_C_typespec},
+      {"class",        C_PLPL, st_C_struct},
+      {"",}, {"",}, {"",}, {"",}, 
+      {"const",        0,      st_C_typespec},
+      {"",}, {"",}, {"",}, {"",}, 
+      {"auto",         0,      st_C_typespec},
+      {"",}, {"",}, 
+      {"define",       0,      st_C_define},
+      {"",}, 
+      {"void",         0,      st_C_typespec},
+      {"",}, {"",}, {"",}, 
+      {"extern",       0,      st_C_typespec},
+      {"static",       0,      st_C_typespec},
+      {"",}, 
+      {"domain",       C_STAR, st_C_struct},
+      {"",}, 
+      {"typedef",      0,      st_C_typedef},
+      {"double",       0,      st_C_typespec},
+      {"enum",         0,      st_C_enum},
+      {"",}, {"",}, {"",}, {"",}, 
+      {"int",          0,      st_C_typespec},
+      {"",}, 
+      {"float",        0,      st_C_typespec},
+      {"",}, {"",}, {"",}, 
+      {"struct",       0,      st_C_struct},
+      {"",}, {"",}, {"",}, {"",}, 
+      {"union",        0,      st_C_struct},
+      {"",}, 
+      {"short",        0,      st_C_typespec},
+      {"",}, {"",}, 
+      {"unsigned",     0,      st_C_typespec},
+      {"signed",       0,      st_C_typespec},
+    };
+
+  if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
+    {
+      register int key = hash (str, len);
+
+      if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
+        {
+          register char *s = wordlist[key].name;
+
+          if (*s == *str && strneq (str + 1, s + 1, len - 1))
+            return &wordlist[key];
+        }
+    }
+  return 0;
 }
+/*%>*/
 
-Stab *
-C_create_stab (c_ext)
+enum sym_type
+C_symtype(str, len, c_ext)
+     char *str;
+     int len;
      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);
-  add_keyword (stab, "const", st_C_typespec);
-  add_keyword (stab, "volatile", st_C_typespec);
-
-  return stab;
+  register struct C_stab_entry *se = in_word_set(str, len);
+
+  if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
+    return st_none;
+  return se->type;
 }
+\f
+ /*
+  * C functions are recognized using a simple finite automaton.
+  * funcdef is its state variable.
+  */
+typedef enum
+{
+  fnone,                       /* nothing seen */
+  ftagseen,                    /* function-like tag seen */
+  fstartlist,                  /* just after open parenthesis */
+  finlist,                     /* in parameter list */
+  flistseen,                   /* after parameter list */
+  fignore                      /* before open brace */
+} FUNCST;
+FUNCST funcdef;
+
+
+ /*
+  * typedefs are recognized using a simple finite automaton.
+  * typeddef is its state variable.
+  */
+typedef enum
+{
+  tnone,                       /* nothing seen */
+  ttypedseen,                  /* typedef keyword seen */
+  tinbody,                     /* inside typedef body */
+  tend,                                /* just before typedef tag */
+  tignore                      /* junk after typedef tag */
+} TYPEDST;
+TYPEDST typdef;
+
+
+ /* 
+  * struct-like structures (enum, struct and union) are recognized
+  * using another simple finite automaton.  `structdef' is its state
+  * variable.
+  */
+typedef enum
+{
+  snone,                       /* nothing seen yet */
+  skeyseen,                    /* struct-like keyword seen */
+  stagseen,                    /* struct-like tag seen */
+  scolonseen,                  /* colon seen after struct-like tag */
+  sinbody                      /* in struct body: recognize member func defs*/
+} STRUCTST;
+STRUCTST structdef;
 
-void
-C_create_stabs ()
+/*
+ * When structdef is stagseen, scolonseen, or sinbody, structtag is the
+ * struct tag, and structtype is the type of the preceding struct-like  
+ * keyword.
+ */
+char structtag[BUFSIZ];
+enum sym_type structtype;
+
+/*
+ * 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 */
+  dignorerest                  /* ignore rest of line */
+} DEFINEST;
+DEFINEST definedef;
+
+/*
+ * Set this to TRUE, and the next token considered is called a function.
+ * Used only for GNU emacs'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;
+
+/*
+ * consider_token ()
+ *     checks to see if the current token is at the start of a
+ *     function, or corresponds to a typedef, or is a struct/union/enum
+ *     tag.
+ *
+ *     *IS_FUNC gets TRUE iff the token is a function or macro with args.
+ *     C_EXT is which language we are looking at.
+ *
+ *     In the future we will need some way to adjust where the end of
+ *     the token is; for instance, implementing the C++ keyword
+ *     `operator' properly will adjust the end of the token to be after
+ *     whatever follows `operator'.
+ *
+ * Globals
+ *     funcdef                 IN OUT
+ *     structdef               IN OUT
+ *     definedef               IN OUT
+ *     typdef                  IN OUT
+ *     next_token_is_func      IN OUT
+ */
+
+logical
+consider_token (c, tokp, c_ext, cblev, is_func)
+     register char c;          /* IN: first char after the token */
+     register TOKEN *tokp;     /* IN: token pointer */
+     int c_ext;                        /* IN: C extensions mask */
+     int cblev;                        /* IN: curly brace level */
+     logical *is_func;         /* OUT: function found */
 {
-  C_stab = C_create_stab (0);
-  C_PLPL_stab = C_create_stab (C_PLPL);
-  C_STAR_stab = C_create_stab (C_STAR | C_PLPL);
+  enum sym_type toktype = C_symtype(tokp->p, tokp->len, c_ext);
+
+  /*
+   * Advance the definedef state machine.
+   */
+  switch (definedef)
+    {
+    case dnone:
+      /* We're not on a preprocessor line. */
+      break;
+    case dsharpseen:
+      if (toktype == st_C_define)
+       {
+         definedef = ddefineseen;
+       }
+      else
+       {
+         definedef = dignorerest;
+       }
+      return FALSE;
+    case ddefineseen:
+      /*
+       * Make a tag for any macro, unless it is a constant
+       * and constantypedefs is FALSE.
+       */
+      definedef = dignorerest;
+      *is_func = (c == '(');
+      if (!*is_func && !constantypedefs)
+       return FALSE;
+      else
+       return TRUE;
+    case dignorerest:
+      return FALSE;
+    default:
+      error ("internal error: definedef value.", 0);
+    }
+
+  /*
+   * Now typedefs
+   */
+  switch (typdef)
+    {
+    case tnone:
+      if (toktype == st_C_typedef)
+       {
+         if (typedefs)
+           typdef = ttypedseen;
+         funcdef = fnone;
+         return FALSE;
+       }
+      break;
+    case ttypedseen:
+      switch (toktype)
+       {
+       case st_none:
+       case st_C_typespec:
+         typdef = tend;
+         break;
+       case st_C_struct:
+       case st_C_enum:
+         break;
+       }
+      /* Do not return here, so the structdef stuff has a chance. */
+      break;
+    case tend:
+      switch (toktype)
+       {
+       case st_C_typespec:
+       case st_C_struct:
+       case st_C_enum:
+         return FALSE;
+       }
+      return TRUE;
+    }
+
+  /*
+   * This structdef business is currently only invoked when cblev==0.
+   * It should be recursively invoked whatever the curly brace level,
+   * and a stack of states kept, to allow for definitions of structs
+   * within structs.
+   *
+   * This structdef business is NOT invoked when we are ctags and the
+   * file is plain C.  This is because a struct tag may have the same
+   * name as another tag, and this loses with ctags.
+   *
+   * This if statement deals with the typdef state machine as 
+   * follows: if typdef==ttypedseen and token is struct/union/class/enum,
+   * return FALSE.  All the other code here is for the structdef 
+   * state machine.
+   */
+  switch (toktype)
+    {
+    case st_C_struct:
+    case st_C_enum:
+      if (typdef == ttypedseen
+         || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
+       {
+         structdef = skeyseen;
+         structtype = toktype;
+       }
+      return FALSE;
+    }
+  if (structdef == skeyseen)
+    {
+      if (structtype == st_C_struct)
+       {
+         strncpy (structtag, tokp->p, tokp->len);
+         structtag[tokp->len] = '\0';  /* for struct/union/class */
+       }
+      else
+       {
+         structtag[0] = '\0';  /* for enum (why is it treated differently?) */
+       }
+      structdef = stagseen;
+      return TRUE;
+    }
+
+  /* Avoid entering funcdef stuff if typdef is going on. */
+  if (typdef != tnone)
+    {
+      definedef = dnone;
+      return FALSE;
+    }
+
+  /* Detect GNU macros. */
+  if (definedef == dnone)
+    if (strneq (tokp->p, "DEFUN", 5) /* Used in emacs */
+#if FALSE      
+          These are defined inside C functions, so currently they
+          are not met anyway.
+       || strneq (tokp->p, "EXFUN", 5) /* Used in glibc */
+       || strneq (tokp->p, "DEFVAR_", 7) /* Used in emacs */
+#endif
+       || strneq (tokp->p, "SYSCALL", 7) /* Used in glibc (mach) */
+       || strneq (tokp->p, "ENTRY", 5) /* Used in glibc */
+       || strneq (tokp->p, "PSEUDO", 6)) /* Used in glibc */
+
+      {
+       next_token_is_func = TRUE;
+       return FALSE;
+      }
+  if (next_token_is_func)
+    {
+      next_token_is_func = FALSE;
+      funcdef = fignore;
+      *is_func = TRUE;
+      return TRUE;
+    }
+
+  /* A function? */
+  switch (toktype)
+    {
+    case st_C_typespec:
+      if (funcdef != finlist && funcdef != fignore)
+        funcdef = fnone;               /* should be useless */
+      return FALSE;
+    default:
+      if (funcdef == fnone)
+       {
+         funcdef = ftagseen;
+         *is_func = TRUE;
+         return TRUE;
+       }
+    }
+
+  return FALSE;
 }
-\f
+
 /*
  * C_entries ()
- *     This routine finds functions and typedefs in C syntax and adds them
+ *     This routine finds functions, typedefs, #define's and
+ *     struct/union/enum definitions in C syntax and adds them
  *     to the list.
  */
 
+#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)
+
+/* 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)
+
 #define CNL_SAVE_DEFINEDEF                                             \
-{                                                                      \
-  prev_linepos = linepos;                                              \
-  SET_FILEPOS (linepos, inf, charno);                                  \
+do {                                                                   \
+  SET_FILEPOS (curlinepos, inf, charno);                               \
   lineno++;                                                            \
-  charno += readline (&lb, inf);                                       \
-  lp = lb.buffer;                                                      \
-}
+  charno += readline (&curlb, inf);                                    \
+  lp = curlb.buffer;                                                   \
+  quotednl = FALSE;                                                    \
+  newndx = curndx;                                                     \
+} while (0)
 
 #define CNL                                                            \
-{                                                                      \
+do {                                                                   \
   CNL_SAVE_DEFINEDEF;                                                  \
+  if (TOKEN_SAVED_P)                                                   \
+    RESTORE_TOKEN;                                                     \
   definedef = dnone;                                                   \
-}
+} while (0)
+
+#define MAKE_TAG_FROM_NEW_LB(isfun)  pfnote (nameb, isfun, tok.named,  \
+  newlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (newlinepos))
+#define MAKE_TAG_FROM_OTH_LB(isfun)  pfnote (nameb, isfun, tok.named,  \
+  othlb.buffer, tokoff + toklen + 1, tok.lineno, GET_CHARNO (othlinepos))
 
 void
-C_entries (c_ext)
-     int c_ext;                        /* extension of C? */
+C_entries (c_ext, inf)
+     int c_ext;                        /* extension of C */
+     FILE *inf;                        /* input file */
 {
-  register int c;              /* latest char read; '\0' for end of line */
-  register int tokoff;         /* offset in line of beginning of latest token */
-  register int toklen;         /* length of latest token */
+  register char c;             /* latest char read; '\0' for end of line */
   register char *lp;           /* pointer one beyond the character `c' */
-  logical incomm, inquote, inchar, midtoken;
-  int level;                   /* current curly brace level */
-  char tokb[BUFSIZ];
-
+  int curndx, newndx;          /* indices for current and new lb */
+  TOKEN tok;                   /* latest token read for funcdef & structdef */
+  char nameb[BUFSIZ];          /* latest token name for funcdef & structdef */
+  register int tokoff;         /* offset in line of start of latest token */
+  register int toklen;         /* length of latest token */
+  int cblev;                   /* current curly brace level */
+  int parlev;                  /* current parenthesis level */
+  logical incomm, inquote, inchar, quotednl, midtoken;
+  logical cplpl;
+  TOKEN savetok;               /* saved token during preprocessor handling */
+  char savenameb[BUFSIZ];      /* ouch! */
+
+  savetok.lineno = 0;
+  curndx = newndx = 0;
   lineno = 0;
   charno = 0;
-  lp = lb.buffer;
+  lp = curlb.buffer;
   *lp = 0;
 
-  definedef = dnone;
-  gotone = midtoken = inquote = inchar = incomm = FALSE;
-  level = 0;
-
-  C_create_stabs ();
+  definedef = dnone; funcdef = fnone; typdef = tnone; structdef = snone;
+  next_token_is_func = yacc_rules = FALSE;
+  midtoken = inquote = inchar = incomm = quotednl = FALSE;
+  cblev = 0;
+  parlev = 0;
+  cplpl = c_ext & C_PLPL;
 
   while (!feof (inf))
     {
       c = *lp++;
       if (c == '\\')
        {
-         if (*lp == 0)
-           continue;
+         /* 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.  */
+         if (*lp == '\0')
+           {
+             quotednl = TRUE;
+             continue;
+           }
          lp++;
          c = ' ';
        }
       else if (incomm)
        {
-         if (c == '*' && *lp == '/')
+         switch (c)
            {
-             c = *lp++;
-             incomm = FALSE;
+           case '*':
+             if (*lp == '/')
+               {
+                 c = *lp++;
+                 incomm = FALSE;
+               }
+             break;
+           case '\0':
+             /* Newlines inside comments do not end macro definitions in
+                traditional cpp. */
+             CNL_SAVE_DEFINEDEF;
+             break;
            }
+         continue;
        }
       else if (inquote)
        {
-         /*
-         * Too dumb to know about \" not being magic, but
-         * they usually occur in pairs anyway.
-         */
-         if (c == '"')
-           inquote = FALSE;
+         switch (c)
+           {
+           case '"':
+             inquote = FALSE;
+             break;
+           case '\0':
+             /* Newlines inside strings do not end macro definitions
+                in traditional cpp, even though compilers don't
+                usually accept them. */
+             CNL_SAVE_DEFINEDEF;
+             break;
+           }
          continue;
        }
       else if (inchar)
        {
-         if (c == '\'')
-           inchar = FALSE;
+         switch (c)
+           {
+           case '\0':
+             /* Hmmm, something went wrong. */
+             CNL;
+             /* FALLTHRU */
+           case '\'':
+             inchar = FALSE;
+             break;
+           }
          continue;
        }
-      else
+      else 
        switch (c)
          {
          case '"':
            inquote = TRUE;
+           if (funcdef != finlist && funcdef != fignore)
+             funcdef = fnone;
            continue;
          case '\'':
            inchar = TRUE;
+           if (funcdef != finlist && funcdef != fignore)
+             funcdef = fnone;
            continue;
          case '/':
            if (*lp == '*')
              {
                lp++;
                incomm = TRUE;
+               continue;
              }
-           else if (c_ext && *lp == '/')
-             {
-               c = 0;          /* C++ comment: skip rest of line */
-             }
-           continue;
-         case '#':
-           if (lp == lb.buffer + 1 && definedef == dnone)
-             definedef = dsharpseen;
-           continue;
-
-           /*
-            * The next two are to help the strucdef state machine.
-            * They break when they are finished, so they don't interfere
-            * with anything else that is going on.
-            */
-         case ':':
-           if (structdef == stagseen)
-             structdef = scolonseen;
-           break;
-           /* Not a struct definition when semicolon seen in non-sinbody context. */
-         case ';':
-           if (structdef != snone && structdef != sinbody)
-             {
-               structdef = snone;
-               (void) strcpy (structtag, "<error 1>");
-             }
-           break;
-
-         case '{':
-           if (tydef == begin)
-             {
-               tydef = middle;
-             }
-           switch (structdef)
+           else if (cplpl && *lp == '/')
              {
-             case skeyseen:    /* unnamed struct */
-               structtag[0] = '\0';
-               /* FALLTHRU */
-             case stagseen:
-             case scolonseen:  /* named struct */
-               structdef = sinbody;
+               c = 0;
                break;
              }
-           level++;
-           continue;
-         case '}':
-           if (!noindentypedefs && lp == lb.buffer + 1)
-             level = 0;        /* reset level if first column */
-           else if (level > 0)
-             level--;
-           if (level == 0 && tydef == middle)
+           else
+             break;
+         case '%':
+           if ((c_ext & YACC) && *lp == '%')
              {
-               tydef = end;
-             }
-           if (level == 0)
+               /* entering or exiting rules section in yacc file */
+               lp++;
+               definedef = dnone; funcdef = fnone;
+               typdef = tnone; structdef = snone;
+               next_token_is_func = FALSE;
+               midtoken = inquote = inchar = incomm = quotednl = FALSE;
+               cblev = 0;
+               yacc_rules = !yacc_rules;
+               continue;
+             }
+           else
+             break;
+         case '#':
+           if (definedef == dnone)
              {
-               structdef = snone;
-               (void) strcpy (structtag, "<error 2>");
-             }
+               char *cp;
+               logical cpptoken = TRUE;
+
+               /* Look back on this line.  If all blanks, or nonblanks
+                  followed by an end of comment, this is a preprocessor
+                  token. */
+               for (cp = newlb.buffer; cp < lp-1; cp++)
+                 if (!iswhite (*cp))
+                   {
+                     if (*cp == '*' && *(cp+1) == '/')
+                       {
+                         cp++;
+                         cpptoken = TRUE;
+                       }
+                     else
+                       cpptoken = FALSE;
+                   }
+               if (cpptoken)
+                 definedef = dsharpseen;
+             } /* if (definedef == dnone) */
+
            continue;
-         }
-      if (LEVEL_OK_FOR_FUNCDEF () && !inquote && !incomm && gotone == FALSE)
+         } /* switch (c) */
+
+
+      /* Consider token only if some complicated conditions are satisfied. */
+      if ((definedef != dnone
+          || (cblev == 0 && structdef != scolonseen)
+          || (cblev == 1 && cplpl && structdef == sinbody))
+         && typdef != tignore
+         && definedef != dignorerest
+         && funcdef != finlist)
        {
          if (midtoken)
            {
              if (endtoken (c))
                {
-                 if (c_ext && c == ':' && *lp == ':' && intoken (*(lp + 1)))
+                 if (cplpl && c == ':' && *lp == ':' && begtoken(*(lp + 1)))
                    {
                      /*
-                      * This handles :: in the middle, but not at beginning
-                      * of an identifier.
+                      * This handles :: in the middle, but not at the
+                      * beginning of an identifier.
                       */
                      lp += 2;
                      toklen += 3;
                    }
                  else
                    {
-                     /* The following is no longer true,
-                        now that we advance to the next line
-                        at the end of processing the character.  */
-                     /*
-                      * We've just finished lexing an identifier.
-                      * Note that if `c' is '\0', `lb' is the NEXT
-                      * line, `lp' points to the beginning of it, and
-                      * old pointers into `lb.buffer' may no longer be
-                      * valid, since `lb.buffer' may have been
-                      * reallocated.  In this case (which corresponds
-                      * to an identifier followed immediately by a
-                      * newline), we re-read the line into lb1.
-                      *
-                      * This would be faster if the previous line's
-                      * buffer were always saved.
-                      */
-                     logical is_func;
-                     char *tok_linebuf;
-                     TOKEN tok;
-                     logical bingo, tok_at_end_of_line;
-                     char *lp_tmp;     /* addressable */
-
-#if 0
-                     if (c == '\0')
-                       {
-                         getline (GET_COOKIE (prev_linepos));
-                         tok_linebuf = lb1.buffer;
-                         tok_at_end_of_line = TRUE;
-                         tok.linestart = prev_linepos;
-                         tok.lineno = lineno - 1;
-                       }
-                     else
-#endif
-                       {
-                         tok_linebuf = lb.buffer;
-                         tok_at_end_of_line = FALSE;
-                         tok.linestart = linepos;
-                         tok.lineno = lineno;
-                       }
-                     tok.p = tok_linebuf + tokoff;
+                     logical is_func = FALSE;
+
+                     tok.lineno = lineno;
+                     tok.p = newlb.buffer + tokoff;
                      tok.len = toklen;
-                     tok.rewritten = FALSE;
-                     lp_tmp = lp;
-                     bingo = consider_token (c, &lp_tmp, &tok,
-                                             &is_func, c_ext, level);
-                     lp = lp_tmp;
-                     if (bingo)
+                     tok.named = FALSE;
+                     if (yacc_rules
+                         || consider_token (c, &tok, c_ext, cblev, &is_func))
                        {
-                         if (GET_CHARNO (tok.linestart) != GET_CHARNO (linepos)
-                             && !tok_at_end_of_line)
+                         if (structdef == sinbody
+                             && definedef == dnone
+                             && is_func)
+                           /* function defined in C++ class body */
                            {
-                             /*
-                              * Resynchronize tok.p to point into the right
-                              * linebuffer.
-                              */
-                             getline (GET_COOKIE (tok.linestart));
-                             if (!tok.rewritten)
-                               tok.p = lb1.buffer + (tok.p - tok_linebuf);
-                             tok_linebuf = lb1.buffer;
-                           }
-                         if (structdef == sinbody && definedef == dnone && is_func)
-                           {   /* function defined in C++ class body */
-                             sprintf (tokb, "%s::%.*s",
-                                      structtag[0] == '\0' ? "_anonymous_"
-                                      : structtag,
+                             sprintf (nameb, "%s::%.*s",
+                                      ((structtag[0] == '\0')
+                                       ? "_anonymous_" : structtag),
                                       tok.len, tok.p);
-                             tok.rewritten = TRUE;
+                             tok.named = TRUE;
                            }
                          else
                            {
-                             sprintf (tokb, "%.*s", tok.len, tok.p);
+                             sprintf (nameb, "%.*s", tok.len, tok.p);
+                           }
+
+                         if (structdef == stagseen
+                             || typdef == tend
+                             || (is_func
+                                 && definedef == dignorerest)) /* macro */
+                           tok.named = TRUE;
+
+                         if (definedef == dnone
+                             && (funcdef == ftagseen
+                                 || structdef == stagseen
+                                 || typdef == tend))
+                           {
+                             if (newndx == curndx)
+                               curndx = 1 - curndx; /* switch line buffers */
                            }
-                         pfnote (tokb, is_func, tok.rewritten, tok_linebuf,
-                            tokoff + toklen + (tok_at_end_of_line ? 0 : 1),
-                                 tok.lineno, GET_CHARNO (tok.linestart));
-                         gotone = is_func;     /* function */
+                         else
+                           MAKE_TAG_FROM_NEW_LB (is_func);
                        }
                      midtoken = FALSE;
                    }
-               }
+               } /* if (endtoken (c)) */
              else if (intoken (c))
-               toklen++;
-           }
+               {
+                 toklen++;
+                 continue;
+               }
+           } /* if (midtoken) */
          else if (begtoken (c))
            {
-             tokoff = lp - 1 - lb.buffer;
-             toklen = 1;
-             midtoken = TRUE;
+             switch (definedef)
+               {
+               case dnone:
+                 switch (funcdef)
+                   {
+                   case fstartlist:
+                     funcdef = finlist;
+                     continue;
+                   case flistseen:
+                     MAKE_TAG_FROM_OTH_LB (TRUE);
+                     funcdef = fignore;
+                     break;
+                   case ftagseen:
+                     funcdef = fnone;
+                     break;
+                   }
+                 if (structdef == stagseen)
+                   structdef = snone;
+                 break;
+               case dsharpseen:
+                 /* Take a quick peek ahead for a define directive,
+                    so we can avoid saving the token when not absolutely
+                    necessary. [This is a speed hack.] */
+                 if (c == 'd' && strneq (lp, "efine", 5)
+                     && iswhite (*(lp + 5)))
+                   {
+                     SAVE_TOKEN;
+                     definedef = ddefineseen;
+                     lp += 6;
+                   }
+                 else
+                   definedef = dignorerest;
+                 continue;
+               }
+             if (!yacc_rules || lp == newlb.buffer + 1)
+               {
+                 tokoff = lp - 1 - newlb.buffer;
+                 toklen = 1;
+                 midtoken = TRUE;
+               }
+             continue;
            }
-       }
-      /* Detect end of line, after having handled the last token on the line.  */
-      if (c == 0)
-       {
-         CNL;
-         gotone = FALSE;
-       }
-      if (c == ';' && tydef == end)    /* clean with typedefs */
-       tydef = none;
-    }
-}
-
-/*
- * consider_token ()
- *     checks to see if the current token is at the start of a
- *     function, or corresponds to a typedef.  It updates the input
- *     line pointer *LPP so that the '(' will be in it when it returns.
- *
- *     *IS_FUNC gets TRUE iff the token is a function.
- *     C_EXT is which language we are looking at.
- *
- *     In the future we will need some way to adjust where the end of
- *     the token is; for instance, implementing the C++ keyword
- *     `operator' properly will adjust the end of the token to be after
- *     whatever follows `operator'.
- *
- * Globals
- *     structdef       IN OUT
- *     definedef       IN OUT
- *     tydef           IN OUT
- */
-
-logical
-consider_token (c, lpp, tokp, is_func, c_ext, level)
-     reg char c;               /* IN: first char after the token */
-     char **lpp;               /* IN OUT: *lpp points to 2nd char after the token */
-     reg TOKEN *tokp;          /* IN */
-     logical *is_func;         /* OUT */
-     int c_ext;                        /* IN */
-     int level;                        /* IN */
-{
-  reg char *lp = *lpp;
-  /*
-   * next_token_is_func
-   *   set this to TRUE, and the next token considered is called a function.
-   */
-  static logical next_token_is_func;
-  logical firsttok;            /* TRUE if have seen first token in ()'s */
-  Stab_entry *tokse = stab_find (get_C_stab (c_ext), tokp->p, tokp->len);
-  enum sym_type toktype = stab_type (tokse);
-
-  *is_func = TRUE;             /* a function */
+       } /* if must look at token */
 
-  /*
-   * Advance the definedef state machine.  We set `gotone' for good measure;
-   * it's redundant.
-   */
-  switch (definedef)
-    {
-    case dnone:
-      /* We're not on a preprocessor line. */
-      break;
-    case dsharpseen:
-      if (toktype == st_C_define)
-       {
-         definedef = ddefineseen;
-         gotone = FALSE;
-       }
-      else
-       {
-         definedef = dignorerest;
-         gotone = TRUE;
-       }
-      goto badone;
-    case ddefineseen:
-      /*
-       * Make a tag for any macro.
-       * This will flub up if there is a newline immediately following
-       * the macro name.
-       */
-      *is_func = (c == '(');
-      definedef = dignorerest;
-      gotone = TRUE;
-      if (!*is_func && !constantypedefs)
-       goto badone;
-      goto goodone;
-    case dignorerest:
-      goto badone;
-    default:
-      error ("internal error: definedef value");
-    }
 
-  /*
-   * Skip whitespace and comments after the token.  This loop should
-   * also skip C++ comments.
-   */
-  while (1)
-    {
-      /* At whitespace => skip it.  */
-      if (iswhite (c))
-       {
-         c = *lp++;
-       }
-      /* At a comment => skip to end of comment.  */
-      else if (c == '/' && *lp == '*')
+      /* Detect end of line, colon, comma, semicolon and various braces
+        after having handled a token.*/
+      switch (c)
        {
-         /* If we find a comment, skip it.  */
-         while (!(c == '*' && *lp == '/'))
+       case ':':
+         if (definedef != dnone)
+           break;
+         if (structdef == stagseen)
+           structdef = scolonseen;
+         else
+           switch (funcdef)
+             {
+             case ftagseen:
+               if (yacc_rules)
+                 {
+                   MAKE_TAG_FROM_OTH_LB (FALSE);
+                   funcdef = fignore;
+                 }
+               break;
+             case fstartlist:
+               funcdef = fnone;
+               break;
+             }
+         break;
+       case ';':
+         if (definedef != dnone)
+           break;
+         if (cblev == 0)
+           switch (typdef)
+             {
+             case tend:
+               MAKE_TAG_FROM_OTH_LB (FALSE);
+               /* FALLTHRU */
+             default:
+               typdef = tnone;
+             }
+         if (funcdef != fignore)
+           funcdef = fnone;
+         if (structdef == stagseen)
+           structdef = snone;
+         break;
+       case ',':
+         if (definedef != dnone)
+           break;
+         if (funcdef != finlist && funcdef != fignore)
+           funcdef = fnone;
+         if (structdef == stagseen)
+           structdef = snone;
+         break;
+       case '[':
+         if (definedef != dnone)
+           break;
+         if (cblev == 0 && typdef == tend)
+           {
+             typdef = tignore;
+             MAKE_TAG_FROM_OTH_LB (FALSE);
+             break;
+           }
+         if (funcdef != finlist && funcdef != fignore)
+           funcdef = fnone;
+         if (structdef == stagseen)
+           structdef = snone;
+         break;
+       case '(':
+         if (definedef != dnone)
+           break;
+         switch (funcdef)
+           {
+           case fnone:
+             switch (typdef)
+               {
+               case ttypedseen:
+               case tend:
+                 /* Make sure that the next char is not a '*'.
+                    This handles constructs like:
+                    typedef void OperatorFun (int fun); */
+                 if (*lp != '*')
+                   {
+                     typdef = tignore;
+                     MAKE_TAG_FROM_OTH_LB (FALSE);
+                   }
+                 break;
+               } /* switch (typdef) */
+             break;
+           case ftagseen:
+             funcdef = fstartlist;
+             break;
+           case flistseen:
+             funcdef = finlist;
+             break;
+           }
+         parlev++;
+         break;
+       case ')':
+         if (definedef != dnone)
+           break;
+         if (--parlev == 0)
            {
-             c = *lp++;
-             if (c == 0)
+             switch (funcdef)
                {
-                 lp--;
+               case fstartlist:
+               case finlist:
+                 funcdef = flistseen;
                  break;
                }
+             if (cblev == 0 && typdef == tend)
+               {
+                 typdef = tignore;
+                 MAKE_TAG_FROM_OTH_LB (FALSE);
+               }
+           }
+         else if (parlev < 0)  /* can happen due to ill-conceived #if's. */
+           parlev = 0;
+         break;
+       case '{':
+         if (definedef != dnone)
+           break;
+         if (typdef == ttypedseen)
+           typdef = tinbody;
+         switch (structdef)
+           {
+           case skeyseen:      /* unnamed struct */
+             structtag[0] = '\0';
+             structdef = sinbody;
+             break;
+           case stagseen:
+           case scolonseen:    /* named struct */
+             structdef = sinbody;
+             MAKE_TAG_FROM_OTH_LB (FALSE);
+             break;
            }
-         if (c == '*' && *lp == '/')
+         switch (funcdef)
            {
-             lp++;             /* lp now points past the '/' */
-             c = *lp++;        /* c is now the --whatever-- after the '/' */
+           case flistseen:
+             MAKE_TAG_FROM_OTH_LB (TRUE);
+             /* FALLTHRU */
+           case fignore:
+             funcdef = fnone;
+             break;
+           case fnone:
+             /* Neutralize `extern "C" {' grot and look inside structs. */
+             if (cblev == 0 && structdef == snone && typdef == tnone)
+               cblev = -1;
            }
-       }
-      else
-       break;
-
-      /* If we arrived at eof or eol, decide which one it is.
-        If it's eol, advance to the next line.  */
-
-      if (c == 0)
-       {
-         lp--;
+         cblev++;
          break;
-       }
-    }
-
-  /*
-   * If you have custom token types, or when configuration files can
-   * define custom token types, this switch will be larger.
-   */
-  switch (toktype)
-    {
-    case st_C_typedef:
-      if (typedefs)
-       {
-         tydef = begin;
-         goto badone;
-       }
-      break;
-    case st_C_typespec:
-      if (tydef == begin || tydef == end)
-       {
-         tydef = end;
-         goto badone;
-       }
-      break;
-    }
-
-  /*
-   * This structdef business is currently only invoked when level==0.
-   * It should be recursively invoked whatever the level, and a stack of
-   * states kept, to allow for definitions of structs within structs.
-   *
-   * This structdef business is NOT invoked when we are ctags and the
-   * file is plain C.  This is because a struct tag may have the same
-   * name as another tag, and this loses with ctags.
-   *
-   * This if statement deals with the tydef state machine as follows: if
-   * tydef==begin and token is struct/union/class/enum, goto badone.
-   * All the other code here is for the structdef state machine.
-   */
-  switch (toktype)
-    {
-    case st_C_struct:
-    case st_C_enum:
-      if (tydef == begin || (typedefs_and_cplusplus && level == 0 && structdef == snone))
-       {
-         structdef = skeyseen;
-         structkey = tokse;
-       }
-      goto badone;
-    }
-
-  if (structdef == skeyseen)
-    {
-      /* If next char is '{' or (for C++) ':', found a structure tag. */
-      if (c == '{' || (c_ext && c == ':'))
-       {
-         /*
-          * We should do this slightly differently for straight C:
-          * instead of defining `tag', as we now do, we should define
-          * `struct tag'.  (Do this only if the find-tag defaulting is
-          * done on a sophisticated per-mode basis, so that if the user
-          * says meta-. anywhere in `struct foo', the default comes out
-          * `struct foo', not `struct' or `foo'.)  This will require
-          * remembering which keyword (struct/union/class/enum) we saw, as a
-          * Stab_entry* -- this will also make it possible to merge the
-          * skeyseen and senumseen states, if we want.
-          */
-         if (stab_type (structkey) == st_C_struct)
+       case '*':
+         if (definedef != dnone)
+           break;
+         if (funcdef == fstartlist)
+           funcdef = fnone;    /* avoid tagging `foo' in `foo (*bar()) ()' */
+         break;
+       case '}':
+         if (definedef != dnone)
+           break;
+         if (!noindentypedefs && lp == newlb.buffer + 1)
            {
-             (void) strncpy (structtag, tokp->p, tokp->len);
-             structtag[tokp->len] = '\0';      /* for struct/union/class */
-             structdef = stagseen;
+             cblev = 0;        /* reset curly brace level if first column */
+             parlev = 0;       /* also reset paren level, just in case... */
            }
-         else
+         else if (cblev > 0)
+           cblev--;
+         if (cblev == 0)
            {
-             structtag[0] = '\0';      /* for enum */
+             if (typdef == tinbody)
+               typdef = tend;
+             structdef = snone;
+             strcpy (structtag, "<error 2>");
            }
-         *is_func = FALSE;     /* not a function */
-         goto goodone;
-       }
-      else
-       {
-         /* Not a definition: reset structdef */
-         structdef = snone;
-         (void) strcpy (structtag, "<error 3>");
-       }
-      /* Now what?  And how does/should this stuff interact with tydef?? */
-      /* Also maybe reset lp to *lpp for benefit of the function finding code. */
-    }
-  if (tydef == begin)
-    {
-      tydef = end;
-      goto badone;
-    }
-  if (tydef == end)
-    {
-      *is_func = 0;
-      goto goodone;
-    }
-  /* Detect GNUmacs's function-defining macros. */
-  if (definedef == dnone && strneq (tokp->p, "DEF", 3))
-    {
-      next_token_is_func = TRUE;
-      goto badone;
-    }
-  if (next_token_is_func)
-    {
-      next_token_is_func = FALSE;
-      goto goodone;
-    }
-  if (c != '(')
-    goto badone;
-  firsttok = FALSE;
-  while ((c = *lp++) != ')')
-    {
-      if (c == 0)
-       {
-         lp--;
          break;
-       }
-      /*
-       * This line used to confuse ctags:
-       *       int     (*oldhup)();
-       * This fixes it. A nonwhite char before the first
-       * token, other than a / (in case of a comment in there)
-       * makes this not a declaration.
-       */
-      if (begtoken (c) || c == '/')
-       firsttok++;
-      else if (!iswhite (c) && !firsttok)
-       goto badone;
-    }
-  while (iswhite (c = *lp++))
-    {
-      if (c == 0)
-       {
-         lp--;
+       case '=':
+       case '#': case '+': case '-': case '~': case '&': case '%': case '/':
+       case '|': case '^': case '!': case '<': case '>': case '.': case '?':
+         if (definedef != dnone)
+           break;
+         /* These surely cannot follow a function tag. */
+         if (funcdef != finlist && funcdef != fignore)
+           funcdef = fnone;
          break;
-       }
-    }
-  if (!isgood (c))
-    goto badone;
+       case '\0':
+         /* If a macro spans multiple lines don't reset its state. */
+         if (quotednl)
+           CNL_SAVE_DEFINEDEF;
+         else
+           CNL;
+         break;
+       } /* switch (c) */
 
-goodone:
-  *lpp = lp - 1;
-  return TRUE;
+    } /* while not eof */
+}
 
-badone:
-  *lpp = lp - 1;
-  return FALSE;
+/*
+ * Process either a C++ file or a C file depending on the setting
+ * of a global flag.
+ */
+void
+default_C_entries (inf)
+     FILE *inf;
+{
+  C_entries (cplusplus ? C_PLPL : 0, inf);
+}
+
+/* Always do C++. */
+void
+Cplusplus_entries (inf)
+     FILE *inf;
+{
+  C_entries (C_PLPL, inf);
 }
 
+/* Always do C*. */
 void
-getline (atcookie)
-     long atcookie;
+Cstar_entries (inf)
+     FILE *inf;
 {
-  long saveftell = ftell (inf);
+  C_entries (C_STAR, inf);
+}
 
-  (void) fseek (inf, atcookie, 0);
-  (void) readline (&lb1, inf);
-  (void) fseek (inf, saveftell, 0);
+/* Always do Yacc. */
+void
+Yacc_entries (inf)
+     FILE *inf;
+{
+  C_entries (YACC, inf);
 }
 \f
 /* Fortran parsing */
@@ -1826,19 +2470,97 @@ getline (atcookie)
 char *dbp;
 int pfcnt;
 
-int
-PF_funcs (fi)
-     FILE *fi;
+logical
+tail (cp)
+     char *cp;
+{
+  register int len = 0;
+
+  while (*cp && (*cp | ' ') == (dbp[len] | ' '))
+    cp++, len++;
+  if (*cp == 0)
+    {
+      dbp += len;
+      return TRUE;
+    }
+  return FALSE;
+}
+
+void
+takeprec ()
+{
+  while (isspace (*dbp))
+    dbp++;
+  if (*dbp != '*')
+    return;
+  dbp++;
+  while (isspace (*dbp))
+    dbp++;
+  if (tail ("(*)"))
+    return;
+  if (!isdigit (*dbp))
+    {
+      --dbp;                   /* force failure */
+      return;
+    }
+  do
+    dbp++;
+  while (isdigit (*dbp));
+}
+
+void
+getit (inf)
+     FILE *inf;
+{
+  register char *cp;
+  char c;
+  char nambuf[BUFSIZ];
+
+  while (isspace (*dbp))
+    dbp++;
+  if (*dbp == '\0')
+    {
+      lineno++;
+      linecharno = charno;
+      charno += readline (&lb, inf);
+      dbp = lb.buffer;
+      if (dbp[5] != '&')
+       return;
+      dbp += 6;
+      while (isspace (*dbp))
+       dbp++;
+    }
+  if (!isalpha (*dbp)
+      && *dbp != '_'
+      && *dbp != '$')
+    return;
+  for (cp = dbp + 1;
+       (*cp
+       && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
+       cp++)
+    continue;
+  c = *cp;
+  *cp = '\0';
+  strcpy (nambuf, dbp);
+  *cp = c;
+  pfnote (nambuf, TRUE, FALSE, lb.buffer,
+         cp - lb.buffer + 1, lineno, linecharno);
+  pfcnt++;
+}
+
+void
+Fortran_functions (inf)
+     FILE *inf;
 {
   lineno = 0;
   charno = 0;
   pfcnt = 0;
 
-  while (!feof (fi))
+  while (!feof (inf))
     {
       lineno++;
       linecharno = charno;
-      charno += readline (&lb, fi);
+      charno += readline (&lb, inf);
       dbp = lb.buffer;
       if (*dbp == '%')
        dbp++;                  /* Ratfor escape to fortran */
@@ -1885,109 +2607,73 @@ PF_funcs (fi)
        {
        case 'f':
          if (tail ("function"))
-           getit ();
+           getit (inf);
          continue;
        case 's':
          if (tail ("subroutine"))
-           getit ();
+           getit (inf);
+         continue;
+       case 'e':
+         if (tail ("entry"))
+           getit (inf);
          continue;
        case 'p':
          if (tail ("program"))
            {
-             getit ();
+             getit (inf);
              continue;
            }
          if (tail ("procedure"))
-           getit ();
+           getit (inf);
          continue;
        }
     }
-  return (pfcnt);
-}
-
-logical
-tail (cp)
-     char *cp;
-{
-  register int len = 0;
-
-  while (*cp && (*cp & ~' ') == ((*(dbp + len)) & ~' '))
-    cp++, len++;
-  if (*cp == 0)
-    {
-      dbp += len;
-      return (1);
-    }
-  return (0);
-}
-
-void
-takeprec ()
-{
-  while (isspace (*dbp))
-    dbp++;
-  if (*dbp != '*')
-    return;
-  dbp++;
-  while (isspace (*dbp))
-    dbp++;
-  if (!isdigit (*dbp))
-    {
-      --dbp;                   /* force failure */
-      return;
-    }
-  do
-    dbp++;
-  while (isdigit (*dbp));
 }
-
+\f
+/*
+ * Bob Weiner, Motorola Inc., 4/3/94
+ * Unix and microcontroller assembly tag handling
+ * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
+ */
 void
-getit ()
+Asm_labels (inf)
+     FILE *inf;
 {
+  char nambuf[BUFSIZ];
   register char *cp;
   char c;
-  char nambuf[BUFSIZ];
-
-  while (isspace (*dbp))
-    dbp++;
-  if (*dbp == 0 || (!isalpha (*dbp)) && (*dbp != '_') && (*dbp != '$'))
-    return;
-  for (cp = dbp + 1; *cp && (isalpha (*cp) || isdigit (*cp)
-                            || (*cp == '_') || (*cp == '$')); cp++)
-    continue;
-  c = cp[0];
-  cp[0] = 0;
-  (void) strcpy (nambuf, dbp);
-  cp[0] = c;
-  pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-  pfcnt++;
-}
-
-/* Handle a file of assembler code.  */
-
-void
-Asm_funcs (fi)
-     FILE *fi;
-{
-  int i;
-  register char c;
 
   lineno = 0;
   charno = 0;
   pfcnt = 0;
 
-  while (!feof (fi))
+  while (!feof (inf))
     {
       lineno++;
       linecharno = charno;
-      charno += readline (&lb, fi);
-      dbp = lb.buffer;
-
-      for (i = 0; ((c = dbp[i]) && !isspace (c)) && (c != ':'); i++)
-       ;
-
-      if ((i > 0) && (c == ':'))
-       getit ();
+      charno += readline (&lb, inf);
+      cp = lb.buffer;
+
+      /* If first char is alphabetic or one of [_.$], test for colon
+        following identifier. */
+      if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
+       {
+         /* Read past label. */
+         cp++;
+         while (isalnum (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
+           cp++;
+         if (*cp == ':' || isspace (*cp))
+           {
+             /* Found end of label, so copy it and add it to the table. */
+             c = *cp;
+             *cp = '\0';
+             strcpy (nambuf, lb.buffer);
+             *cp = c;
+             pfnote (nambuf, TRUE, FALSE, lb.buffer,
+                     cp - lb.buffer + 1, lineno, linecharno);
+             pfcnt++;
+           }
+       }
     }
 }
 \f
@@ -2001,16 +2687,15 @@ Asm_funcs (fi)
   dbp = lb.buffer; \
 }
 
-/*  Locates tags for procedures & functions.
- *  Doesn't do any type- or var-definitions.
- *  It does look for the keyword "extern" or "forward"
- *  immediately following the procedure statement;
- *  if found, the tag is skipped.
+/*
+ *  Locates tags for procedures & functions.  Doesn't do any type- or
+ *  var-definitions.  It does look for the keyword "extern" or
+ *  "forward" immediately following the procedure statement; if found,
+ *  the tag is skipped.
  */
-
 void
-PAS_funcs (fi)
-     FILE *fi;
+Pascal_functions (inf)
+     FILE *inf;
 {
   struct linebuffer tline;     /* mostly copied from C_entries */
   long save_lcno;
@@ -2019,8 +2704,7 @@ PAS_funcs (fi)
   char nambuf[BUFSIZ];
 
   logical                      /* each of these flags is TRUE iff: */
-    incomm1,                   /* point is inside {..} comment */
-    incomm2,                   /* point is inside (*..*) comment */
+    incomment,                 /* point is inside a comment */
     inquote,                   /* point is inside '..' string */
     get_tagname,               /* point is after PROCEDURE/FUNCTION */
   /*   keyword, so next item = potential tag */
@@ -2037,14 +2721,14 @@ PAS_funcs (fi)
   *dbp = 0;
   initbuffer (&tline);
 
-  incomm1 = incomm2 = inquote = FALSE;
+  incomment = inquote = FALSE;
   found_tag = FALSE;           /* have a proc name; check if extern */
   get_tagname = FALSE;         /* have found "procedure" keyword    */
   inparms = FALSE;             /* found '(' after "proc"            */
   verify_tag = FALSE;          /* check if "extern" is ahead        */
 
   /* long main loop to get next char */
-  while (!feof (fi))
+  while (!feof (inf))
     {
       c = *dbp++;
       if (c == 0)              /* if end of line */
@@ -2058,22 +2742,14 @@ PAS_funcs (fi)
          /* to the beginning of the name of  */
          /* the procedure or function        */
        }
-      if (incomm1)             /* within { - } comments */
-       {
-         if (c == '}')
-           incomm1 = FALSE;
-         continue;
-       }
-      else if (incomm2)                /* within (* - *) comments */
+      if (incomment)
        {
-         if (c == '*')
+         if (c == '}')         /* within { - } comments */
+           incomment = FALSE;
+         else if (c == '*' && dbp[1] == ')') /* within (* - *) comments */
            {
-             while ((c = *dbp++) == '*')
-               continue;
-             if (c == 0)
-               GET_NEW_LINE;
-             if (c == ')')
-               incomm2 = FALSE;
+             dbp++;
+             incomment = FALSE;
            }
          continue;
        }
@@ -2083,19 +2759,19 @@ PAS_funcs (fi)
            inquote = FALSE;
          continue;
        }
     else
+ else
        switch (c)
          {
          case '\'':
            inquote = TRUE;     /* found first quote */
            continue;
          case '{':             /* found open-{-comment */
-           incomm1 = TRUE;
+           incomment = TRUE;
            continue;
          case '(':
            if (*dbp == '*')    /* found open-(*-comment */
              {
-               incomm2 = TRUE;
+               incomment = TRUE;
                dbp++;
              }
            else if (found_tag) /* found '(' after tag, i.e., parm-list */
@@ -2134,7 +2810,7 @@ PAS_funcs (fi)
                  verify_tag = FALSE;
                }
            }
-         if ((found_tag) && (verify_tag))      /* not external proc, so make tag */
+         if ((found_tag) && (verify_tag)) /* not external proc, so make tag */
            {
              found_tag = FALSE;
              verify_tag = FALSE;
@@ -2169,8 +2845,7 @@ PAS_funcs (fi)
 
          /* and proceed to check for "extern" */
        }
-      if ((!incomm1) && (!incomm2) && (!inquote) &&
-         (!found_tag) && (!get_tagname))
+      if (!incomment && !inquote && !found_tag && !get_tagname)
        {
          /* check for proc/fn keywords */
          switch (c | ' ')
@@ -2185,27 +2860,77 @@ PAS_funcs (fi)
              continue;
            }
        }
-    }                          /* while not e-o-f */
+    }                          /* while not eof */
+}
+\f
+/*
+ * lisp tag functions
+ * just look for (def or (DEF
+ */
+
+int
+L_isdef (tokp)
+     register char *tokp;
+{
+  return ((tokp[1] == 'd' || tokp[1] == 'D')
+         && (tokp[2] == 'e' || tokp[2] == 'E')
+         && (tokp[3] == 'f' || tokp[3] == 'F'));
+}
+
+int
+L_isquote (tokp)
+     register char *tokp;
+{
+  return ((*(++tokp) == 'q' || *tokp == 'Q')
+         && (*(++tokp) == 'u' || *tokp == 'U')
+         && (*(++tokp) == 'o' || *tokp == 'O')
+         && (*(++tokp) == 't' || *tokp == 'T')
+         && (*(++tokp) == 'e' || *tokp == 'E')
+         && isspace(*(++tokp)));
+}
+
+void
+L_getit ()
+{
+  register char *cp;
+  char c;
+  char nambuf[BUFSIZ];
+
+  if (*dbp == '\'')            /* Skip prefix quote */
+    dbp++;
+  else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
+  {
+    dbp += 7;
+    while (isspace(*dbp))
+      dbp++;
+  }
+  for (cp = dbp /*+1*/; *cp && *cp != '(' && *cp != ' ' && *cp != ')'; cp++)
+    continue;
+  if (cp == dbp)
+    return;
+  
+  c = cp[0];
+  cp[0] = 0;
+  strcpy (nambuf, dbp);
+  cp[0] = c;
+  pfnote (nambuf, TRUE, FALSE, lb.buffer,
+         cp - lb.buffer + 1, lineno, linecharno);
+  pfcnt++;
 }
-\f
-/*
- * lisp tag functions
- * just look for (def or (DEF
- */
 
 void
-L_funcs (fi)
-     FILE *fi;
+Lisp_functions (inf)
+     FILE *inf;
 {
   lineno = 0;
   charno = 0;
   pfcnt = 0;
 
-  while (!feof (fi))
+  while (!feof (inf))
     {
       lineno++;
       linecharno = charno;
-      charno += readline (&lb, fi);
+      charno += readline (&lb, inf);
       dbp = lb.buffer;
       if (dbp[0] == '(')
        {
@@ -2220,15 +2945,17 @@ L_funcs (fi)
          else
            {
              /* Check for (foo::defmumble name-defined ... */
-             while (*dbp && *dbp != ':' && !isspace (*dbp)
-                    && *dbp != '(' && *dbp != ')')
+             do
                dbp++;
+             while (*dbp && !isspace (*dbp)
+                    && *dbp != ':' && *dbp != '(' && *dbp != ')');
              if (*dbp == ':')
                {
-                 while (*dbp == ':')
+                 do
                    dbp++;
+                 while (*dbp == ':');
 
-                 if (L_isdef (dbp))
+                 if (L_isdef (dbp - 1))
                    {
                      while (!isspace (*dbp))
                        dbp++;
@@ -2241,34 +2968,6 @@ L_funcs (fi)
        }
     }
 }
-
-int
-L_isdef (dbp)
-     char *dbp;
-{
-  return ((dbp[1] == 'D' || dbp[1] == 'd') &&
-         (dbp[2] == 'E' || dbp[2] == 'e') &&
-         (dbp[3] == 'F' || dbp[3] == 'f'));
-}
-
-void
-L_getit ()
-{
-  register char *cp;
-  char c;
-  char nambuf[BUFSIZ];
-
-  if (*dbp == 0)
-    return;
-  for (cp = dbp + 1; *cp && *cp != '(' && *cp != ' '; cp++)
-    continue;
-  c = cp[0];
-  cp[0] = 0;
-  (void) strcpy (nambuf, dbp);
-  cp[0] = c;
-  pfnote (nambuf, TRUE, FALSE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-  pfcnt++;
-}
 \f
 /*
  * Scheme tag functions
@@ -2278,21 +2977,21 @@ L_getit ()
  * look for (set! xyzzy
  */
 
-static void get_scheme ();
+void get_scheme ();
 
 void
-Scheme_funcs (fi)
-     FILE *fi;
+Scheme_functions (inf)
+     FILE *inf;
 {
   lineno = 0;
   charno = 0;
   pfcnt = 0;
 
-  while (!feof (fi))
+  while (!feof (inf))
     {
       lineno++;
       linecharno = charno;
-      charno += readline (&lb, fi);
+      charno += readline (&lb, inf);
       dbp = lb.buffer;
       if (dbp[0] == '(' &&
          (dbp[1] == 'D' || dbp[1] == 'd') &&
@@ -2323,7 +3022,7 @@ Scheme_funcs (fi)
     }
 }
 
-static void
+void
 get_scheme ()
 {
   register char *cp;
@@ -2352,7 +3051,6 @@ get_scheme ()
 /* TEX_toktab is a table of TeX control sequences that define tags.
    Each TEX_tabent records one such control sequence.
    CONVERT THIS TO USE THE Stab TYPE!! */
-
 struct TEX_tabent
 {
   char *name;
@@ -2364,25 +3062,24 @@ struct TEX_tabent *TEX_toktab = NULL;   /* Table with tag tokens */
 /* Default set of control sequences to put into TEX_toktab.
    The value of environment var TEXTAGS is prepended to this.  */
 
-static char *TEX_defenv =
-":chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
+char *TEX_defenv = "\
+:chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
 
 void TEX_mode ();
 struct TEX_tabent *TEX_decode_env ();
 void TEX_getit ();
 int TEX_Token ();
 
-static char TEX_esc = '\\';
-static char TEX_opgrp = '{';
-static char TEX_clgrp = '}';
+char TEX_esc = '\\';
+char TEX_opgrp = '{';
+char TEX_clgrp = '}';
 
 /*
  * TeX/LaTeX scanning loop.
  */
-
 void
-TEX_funcs (fi)
-     FILE *fi;
+TeX_functions (inf)
+     FILE *inf;
 {
   char *lasthit;
 
@@ -2391,41 +3088,32 @@ TEX_funcs (fi)
   pfcnt = 0;
 
   /* Select either \ or ! as escape character.  */
-  TEX_mode (fi);
+  TEX_mode (inf);
 
   /* Initialize token table once from environment. */
   if (!TEX_toktab)
     TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
 
-  while (!feof (fi))
-    {
+  while (!feof (inf))
+    {                          /* Scan each line in file */
       lineno++;
       linecharno = charno;
-      charno += readline (&lb, fi);
+      charno += readline (&lb, inf);
       dbp = lb.buffer;
       lasthit = dbp;
+      while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
+       {
+         register int i;
 
-      while (!feof (fi))
-       {                       /* Scan each line in file */
-         lineno++;
-         linecharno = charno;
-         charno += readline (&lb, fi);
-         dbp = lb.buffer;
+         if (!*(++dbp))
+           break;
+         linecharno += dbp - lasthit;
          lasthit = dbp;
-         while (dbp = index (dbp, TEX_esc))    /* Look at each escape in line */
+         i = TEX_Token (lasthit);
+         if (0 <= i)
            {
-             register int i;
-
-             if (!*(++dbp))
-               break;
-             linecharno += dbp - lasthit;
-             lasthit = dbp;
-             i = TEX_Token (lasthit);
-             if (0 <= i)
-               {
-                 TEX_getit (lasthit, TEX_toktab[i].len);
-                 break;        /* We only save a line once */
-               }
+             TEX_getit (lasthit, TEX_toktab[i].len);
+             break;            /* We only save a line once */
            }
        }
     }
@@ -2435,21 +3123,20 @@ TEX_funcs (fi)
 #define TEX_SESC '!'
 #define TEX_cmt  '%'
 
-/* Figure out whether TeX's escapechar is '\\' or '!' and set grouping */
-/* chars accordingly. */
-
+/* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
+   chars accordingly. */
 void
-TEX_mode (f)
-     FILE *f;
+TEX_mode (inf)
+     FILE *inf;
 {
   int c;
 
-  while ((c = getc (f)) != EOF)
+  while ((c = getc (inf)) != EOF)
     {
       /* Skip to next line if we hit the TeX comment char. */
       if (c == TEX_cmt)
        while (c != '\n')
-         c = getc (f);
+         c = getc (inf);
       else if (c == TEX_LESC || c == TEX_SESC )
        break;
     }
@@ -2466,19 +3153,17 @@ TEX_mode (f)
       TEX_opgrp = '<';
       TEX_clgrp = '>';
     }
-  rewind (f);
+  rewind (inf);
 }
 
-/* Read environment and prepend it to the default string. */
-/* Build token table. */
-
+/* Read environment and prepend it to the default string.
+   Build token table. */
 struct TEX_tabent *
 TEX_decode_env (evarname, defenv)
      char *evarname;
      char *defenv;
 {
   register char *env, *p;
-  extern char *savenstr (), *index ();
 
   struct TEX_tabent *tab;
   int size, i;
@@ -2492,15 +3177,16 @@ TEX_decode_env (evarname, defenv)
 
   /* Allocate a token table */
   for (size = 1, p = env; p;)
-    if ((p = index (p, ':')) && *(++p))
+    if ((p = etags_strchr (p, ':')) && *(++p))
       size++;
-  tab = xnew (size, struct TEX_tabent);
+  /* Add 1 to leave room for null terminator.  */
+  tab = xnew (size + 1, struct TEX_tabent);
 
   /* Unpack environment string into token table. Be careful about */
   /* zero-length strings (leading ':', "::" and trailing ':') */
   for (i = 0; *env;)
     {
-      p = index (env, ':');
+      p = etags_strchr (env, ':');
       if (!p)                  /* End of environment string. */
        p = env + strlen (env);
       if (p - env > 0)
@@ -2524,7 +3210,6 @@ TEX_decode_env (evarname, defenv)
 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
    The name being defined actually starts at (NAME + LEN + 1).
    But we seem to include the TeX command in the tag name.  */
-
 void
 TEX_getit (name, len)
      char *name;
@@ -2539,7 +3224,7 @@ TEX_getit (name, len)
   /* Let tag name extend to next group close (or end of line) */
   while (*p && *p != TEX_clgrp)
     p++;
-  (void) strncpy (nambuf, name, p - name);
+  strncpy (nambuf, name, p - name);
   nambuf[p - name] = 0;
 
   pfnote (nambuf, TRUE, FALSE, lb.buffer, strlen (lb.buffer), lineno, linecharno);
@@ -2547,10 +3232,9 @@ TEX_getit (name, len)
 }
 
 /* If the text at CP matches one of the tag-defining TeX command names,
-   return the index of that command in TEX_toktab.
-   Otherwise return -1.  */
-
-/* Keep the capital `T' in `Token' for dumb truncating compilers
+   return the pointer to the first occurrence of that command in TEX_toktab.
+   Otherwise return -1.
+   Keep the capital `T' in `Token' for dumb truncating compilers
    (this distinguishes it from `TEX_toktab' */
 int
 TEX_Token (cp)
@@ -2559,21 +3243,18 @@ TEX_Token (cp)
   int i;
 
   for (i = 0; TEX_toktab[i].len > 0; i++)
-    if (strncmp (TEX_toktab[i].name, cp, TEX_toktab[i].len) == 0)
+    if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
       return i;
   return -1;
 }
 \f
 /* Support for Prolog.  */
 
-/* whole head (not only functor, but also arguments)
+/* Whole head (not only functor, but also arguments)
    is gotten in compound term. */
-
 void
-prolog_getit (s, lineno, linecharno)
+prolog_getit (s)
      char *s;
-     int lineno;
-     long linecharno;
 {
   char nambuf[BUFSIZ], *save_s, tmpc;
   int insquote, npar;
@@ -2620,81 +3301,233 @@ prolog_getit (s, lineno, linecharno)
   *s = '\0';
   strcpy (nambuf, save_s);
   *s = tmpc;
-  pfnote (nambuf, TRUE, save_s, strlen (nambuf), lineno, linecharno);
+  pfnote (nambuf, TRUE, FALSE, save_s, strlen (nambuf), lineno, linecharno);
 }
 
 /* It is assumed that prolog predicate starts from column 0. */
-
 void
-prolog_funcs (fi)
-     FILE *fi;
+Prolog_functions (inf)
+     FILE *inf;
 {
   void skip_comment (), prolog_getit ();
 
   lineno = linecharno = charno = 0;
-  while (!feof (fi))
+  while (!feof (inf))
     {
       lineno++;
       linecharno += charno;
-      charno = readline (&lb, fi) + 1; /* 1 for newline. */
+      charno = readline (&lb, inf) + 1;        /* 1 for newline. */
       dbp = lb.buffer;
       if (isspace (dbp[0]))    /* not predicate header. */
        continue;
       else if (dbp[0] == '%')  /* comment. */
        continue;
       else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
-       skip_comment (&lb, fi, &lineno, &linecharno);
+       skip_comment (&lb, inf, &lineno, &linecharno);
       else                     /* found. */
-       prolog_getit (dbp, lineno, linecharno);
+       prolog_getit (dbp);
     }
 }
 
 void
-skip_comment (plb, fi, plineno, plinecharno)
+skip_comment (plb, inf, plineno, plinecharno)
      struct linebuffer *plb;
-     FILE *fi;
+     FILE *inf;
      int *plineno;             /* result */
      long *plinecharno;                /* result */
 {
-  while (!substr ("*/", plb->buffer))
+  char *cp;
+
+  do
     {
+      for (cp = plb->buffer; *cp != '\0'; cp++)
+       if (cp[0] == '*' && cp[1] == '/')
+         return;
       (*plineno)++;
-      *plinecharno += readline (plb, fi) + 1;
-    }                          /* 1 for newline. */
+      *plinecharno += readline (plb, inf) + 1; /* 1 for newline. */
+    }
+  while (!feof(inf));
 }
+\f
+#ifdef ETAGS_REGEXPS
+/* Take a string like "/blah/" and turn it into "blah", making sure
+   that the first and last characters are the same, and handling
+   quoted separator characters.  Actually, stops on the occurence of
+   an unquoted separator.  Also turns "\t" into a Tab character.
+   Returns pointer to terminating separator.  Works in place.  Null
+   terminates name string. */
+char *
+scan_separators (name)
+     char *name;
+{
+  char sep = name[0];
+  char *copyto = name;
+  logical quoted = FALSE;
 
-/* Return TRUE if 'sub' exists somewhere in 's'. */
+  for (++name; *name != '\0'; ++name)
+    {
+      if (quoted)
+       {
+         if (*name == 't')
+           *copyto++ = '\t';
+         else if (*name == sep)
+           *copyto++ = sep;
+         else
+           {
+             /* Something else is quoted, so preserve the quote. */
+             *copyto++ = '\\';
+             *copyto++ = *name;
+           }
+         quoted = FALSE;
+       }
+      else if (*name == '\\')
+       quoted = TRUE;
+      else if (*name == sep)
+       break;
+      else
+       *copyto++ = *name;
+    }
 
-int
-substr (sub, s)
-     char *sub;
-     char *s;
-{
-  while (*s && (s = index (s, *sub)))
-    if (prestr (sub, s))
-      return (TRUE);
-    else
-      s++;
-  return (FALSE);
+  /* Terminate copied string. */
+  *copyto = '\0';
+  return name;
 }
 
-/* Return TRUE if 'pre' is prefix of string 's'. */
-
-int
-prestr (pre, s)
-     char *pre;
-     char *s;
+/* Turn a name, which is an ed-style (but Emacs syntax) regular
+   expression, into a real regular expression by compiling it. */
+void
+add_regex (regexp_pattern)
+     char *regexp_pattern;
 {
-  if (*pre == '\0')
-    return (TRUE);
-  else if (*pre == *s)
-    return (prestr (pre + 1, s + 1));
+  char *name;
+  const char *err;
+  struct re_pattern_buffer *patbuf;
+
+  if (regexp_pattern == NULL)
+    {
+      /* Remove existing regexps. */
+      num_patterns = 0;
+      patterns = NULL;
+      return;
+    }
+
+  if (regexp_pattern[0] == '\0')
+    {
+      error ("missing regexp", 0);
+      return;
+    }
+  if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
+    {
+      error ("%s: unterminated regexp", regexp_pattern);
+      return;
+    }
+  name = scan_separators (regexp_pattern);
+  if (regexp_pattern[0] == '\0')
+    {
+      error ("null regexp", 0);
+      return;
+    }
+  (void) scan_separators (name);
+
+  patbuf = xnew (1, struct re_pattern_buffer);
+  patbuf->translate = NULL;
+  patbuf->fastmap = NULL;
+  patbuf->buffer = NULL;
+  patbuf->allocated = 0;
+
+  err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
+  if (err != NULL)
+    {
+      error ("%s while compiling pattern", err);
+      return;
+    }
+
+  num_patterns += 1;
+  if (num_patterns == 1)
+    patterns = xnew (1, struct pattern);
   else
-    return (FALSE);
+    patterns = ((struct pattern *)
+               xrealloc (patterns,
+                         (num_patterns * sizeof (struct pattern))));
+  patterns[num_patterns - 1].pattern = patbuf;
+  patterns[num_patterns - 1].name_pattern = savestr (name);
+  patterns[num_patterns - 1].error_signaled = FALSE;
+}
+
+/*
+ * Do the subtitutions indicated by the regular expression and
+ * arguments.
+ */
+char *
+substitute (in, out, regs)
+     char *in, *out;
+     struct re_registers *regs;
+{
+  char *result = NULL, *t;
+  int size = 0;
+
+  /* Pass 1: figure out how much size to allocate. */
+  for (t = out; *t; ++t)
+    {
+      if (*t == '\\')
+       {
+         ++t;
+         if (!*t)
+           {
+             fprintf (stderr, "%s: pattern subtitution ends prematurely\n",
+                      progname);
+             return NULL;
+           }
+         if (isdigit (*t))
+           {
+             int dig = *t - '0';
+             size += regs->end[dig] - regs->start[dig];
+           }
+       }
+    }
+
+  /* Allocate space and do the substitutions. */
+  result = xnew (size + 1, char);
+  size = 0;
+  for (; *out; ++out)
+    {
+      if (*out == '\\')
+       {
+         ++out;
+         if (isdigit (*out))
+           {
+             /* Using "dig2" satisfies my debugger.  Bleah. */
+             int dig2 = *out - '0';
+             strncpy (result + size, in + regs->start[dig2],
+                      regs->end[dig2] - regs->start[dig2]);
+             size += regs->end[dig2] - regs->start[dig2];
+           }
+         else
+           {
+             switch (*out)
+               {
+               case '\t':
+                 result[size++] = '\t';
+                 break;
+               case '\\':
+                 *out = '\\';
+                 break;
+               default:
+                 result[size++] = *out;
+                 break;
+               }
+           }
+       }
+      else
+       result[size++] = *out;
+    }
+  result[size] = '\0';
+
+  return result;
 }
 \f
+#endif /* ETAGS_REGEXPS */
 /* Initialize a linebuffer for use */
-
 void
 initbuffer (linebuffer)
      struct linebuffer *linebuffer;
@@ -2709,16 +3542,16 @@ initbuffer (linebuffer)
  * which is the length of the line including the newline, if any.
  */
 long
-readline (linebuffer, stream)
+readline_internal (linebuffer, stream)
      struct linebuffer *linebuffer;
      register FILE *stream;
 {
   char *buffer = linebuffer->buffer;
   register char *p = linebuffer->buffer;
   register char *pend;
-  int newline;                 /* 1 if ended with newline, 0 if ended with EOF */
+  int chars_deleted;
 
-  pend = p + linebuffer->size; /* Separate to avoind 386/IX compiler bug.  */
+  pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug.  */
 
   while (1)
     {
@@ -2731,17 +3564,105 @@ readline (linebuffer, stream)
          pend = buffer + linebuffer->size;
          linebuffer->buffer = buffer;
        }
-      if (c < 0 || c == '\n')
+      if (c == EOF)
+       {
+         chars_deleted = 0;
+         break;
+       }
+      if (c == '\n')
        {
-         *p = 0;
-         newline = (c == '\n' ? 1 : 0);
+         if (p[-1] == '\r' && p > buffer)
+           {
+             *--p = '\0';
+             chars_deleted = 2;
+           }
+         else
+           {
+             *p = '\0';
+             chars_deleted = 1;
+           }
          break;
        }
       *p++ = c;
     }
 
-  return p - buffer + newline;
+  return p - buffer + chars_deleted;
+}
+
+/*
+ * Like readline_internal, above, but try to match the input
+ * line against any existing regular expressions.
+ */
+long
+readline (linebuffer, stream)
+     struct linebuffer *linebuffer;
+     FILE *stream;
+{
+  /* Read new line. */
+  int i;
+  long result = readline_internal (linebuffer, stream);
+
+#ifdef ETAGS_REGEXPS
+  /* Match against all listed patterns. */
+  for (i = 0; i < num_patterns; ++i)
+    {
+      int match = re_match (patterns[i].pattern, linebuffer->buffer,
+                           (int)result, 0, &patterns[i].regs);
+      switch (match)
+       {
+       case -2:
+         /* Some error. */
+         if (!patterns[i].error_signaled)
+           {
+             error ("error while matching pattern %d", i);
+             patterns[i].error_signaled = TRUE;
+           }
+         break;
+       case -1:
+         /* No match. */
+         break;
+       default:
+         /* Match occurred.  Construct a tag. */
+         if (patterns[i].name_pattern[0] != '\0')
+           {
+             /* Make a named tag. */
+             char *name = substitute (linebuffer->buffer,
+                                      patterns[i].name_pattern,
+                                      &patterns[i].regs);
+             if (name != NULL)
+               pfnote (name, TRUE, TRUE, linebuffer->buffer,
+                       match, lineno, linecharno);
+           }
+         else
+           {
+             /* Make an unnamed tag. */
+             pfnote (NULL, TRUE, FALSE, linebuffer->buffer,
+                     match, lineno, linecharno);
+           }
+         break;
+       }
+    }
+#endif /* ETAGS_REGEXPS */
+
+  return result;
+}
+
+/*
+ * Read a file, but do no processing.  This is used to do regexp
+ * matching on files that have no language defined.
+ */
+void
+just_read_file (inf)
+     FILE *inf;
+{
+  while (!feof (inf))
+    {
+      ++lineno;
+      linecharno = charno;
+      charno += readline (&lb, inf) + 1;
+    }
 }
+
 \f
 char *
 savestr (cp)
@@ -2758,21 +3679,19 @@ savenstr (cp, len)
   register char *dp;
 
   dp = xnew (len + 1, char);
-  (void) strncpy (dp, cp, len);
+  strncpy (dp, cp, len);
   dp[len] = '\0';
   return dp;
 }
 
-#ifdef notdef
 /*
  * Return the ptr in sp at which the character c last
  * appears; NULL if not found
  *
- * Identical to v7 rindex, included for portability.
+ * Identical to System V strrchr, included for portability.
  */
-
 char *
-rindex (sp, c)
+etags_strrchr (sp, c)
      register char *sp, c;
 {
   register char *r;
@@ -2783,44 +3702,46 @@ rindex (sp, c)
       if (*sp == c)
        r = sp;
   } while (*sp++);
-  return (r);
+  return r;
 }
 
+
 /*
  * Return the ptr in sp at which the character c first
  * appears; NULL if not found
  *
- * Identical to v7 index, included for portability.
+ * Identical to System V strchr, included for portability.
  */
-
 char *
-index (sp, c)
+etags_strchr (sp, c)
      register char *sp, c;
 {
   do
     {
       if (*sp == c)
-       return (sp);
-  } while (*sp++);
-  return (NULL);
+       return sp;
+    } while (*sp++);
+  return NULL;
 }
 
-#endif /* notdef */
-
 /* Print error message and exit.  */
-
-/* VARARGS1 */
 void
 fatal (s1, s2)
      char *s1, *s2;
 {
   error (s1, s2);
-  exit (1);
+  exit (BAD);
 }
 
-/* Print error message.  `s1' is printf control string, `s2' is arg for it. */
+void
+pfatal (s1)
+     char *s1;
+{
+  perror (s1);
+  exit (BAD);
+}
 
-/* VARARGS1 */
+/* Print error message.  `s1' is printf control string, `s2' is arg for it. */
 void
 error (s1, s2)
      char *s1, *s2;
@@ -2830,8 +3751,8 @@ error (s1, s2)
   fprintf (stderr, "\n");
 }
 
-/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
-
+/* Return a newly-allocated string whose contents
+   concatenate those of s1, s2, s3.  */
 char *
 concat (s1, s2, s3)
      char *s1, *s2, *s3;
@@ -2839,22 +3760,179 @@ concat (s1, s2, s3)
   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
   char *result = xnew (len1 + len2 + len3 + 1, char);
 
-  (void) strcpy (result, s1);
-  (void) strcpy (result + len1, s2);
-  (void) strcpy (result + len1 + len2, s3);
-  *(result + len1 + len2 + len3) = 0;
+  strcpy (result, s1);
+  strcpy (result + len1, s2);
+  strcpy (result + len1 + len2, s3);
+  result[len1 + len2 + len3] = '\0';
 
   return result;
 }
+\f
+/* Does the same work as the system V getcwd, but does not need to
+   guess buffer size in advance. */
+char *
+etags_getcwd ()
+#ifdef DOS_NT
+{
+  char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS.  */
 
-/* Like malloc but get fatal error if memory is exhausted.  */
+  getwd (path);
+  p = path;
+  while (*p)
+    if (*p == '\\')
+      *p++ = '/';
+    else
+      *p++ = tolower (*p);
+
+  return strdup (path);
+}
+#elif HAVE_GETCWD /* not DOS_NT */
+{
+  int bufsize = 200;
+  char *path = xnew (bufsize, char);
+
+  while (getcwd (path, bufsize) == NULL)
+    {
+      if (errno != ERANGE)
+       pfatal ("pwd");
+      bufsize *= 2;
+      path = xnew (bufsize, char);
+    }
+
+  return path;
+}
+#else /* not DOS_NT and not HAVE_GETCWD */
+{
+  struct linebuffer path;
+  FILE *pipe;
+
+  initbuffer (&path);
+  pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
+  if (pipe == NULL || readline_internal (&path, pipe) == 0)
+    pfatal ("pwd");
+  pclose (pipe);
+
+  return path.buffer;
+}
+#endif /* not DOS_NT and not HAVE_GETCWD */
+
+/* Return a newly allocated string containing the filename
+   of FILE relative to the absolute directory DIR (which
+   should end with a slash). */
+char *
+relative_filename (file, dir)
+     char *file, *dir;
+{
+  char *fp, *dp, *res;
+
+  /* Find the common root of file and dir. */
+  fp = absolute_filename (file, cwd);
+  dp = dir;
+  while (*fp++ == *dp++)
+    continue;
+  do
+    {
+      fp--;
+      dp--;
+    }
+  while (*fp != '/');
+
+  /* Build a sequence of "../" strings for the resulting relative filename. */
+  for (dp = etags_strchr (dp + 1, '/'), res = "";
+       dp != NULL;
+       dp = etags_strchr (dp + 1, '/'))
+    {
+      res = concat (res, "../", "");
+    }
 
+  /* Add the filename relative to the common root of file and dir. */
+  res = concat (res, fp + 1, "");
+
+  return res;                  /* temporary stub */
+}
+
+/* Return a newly allocated string containing the
+   absolute filename of FILE given CWD (which should
+   end with a slash). */
+char *
+absolute_filename (file, cwd)
+     char *file, *cwd;
+{
+  char *slashp, *cp, *res;
+
+  if (file[0] == '/')
+    res = concat (file, "", "");
+  else
+    res = concat (cwd, file, "");
+
+  /* Delete the "/dirname/.." and "/." substrings. */
+  slashp = etags_strchr (res, '/');
+  while (slashp != NULL && slashp[0] != '\0')
+    {
+      if (slashp[1] == '.')
+       {
+         if (slashp[2] == '.'
+             && (slashp[3] == '/' || slashp[3] == '\0'))
+           {
+             cp = slashp;
+             do
+               cp--;
+             while (cp >= res && *cp != '/');
+             if (*cp == '/')
+               {
+                 strcpy (cp, slashp + 3);
+               }
+             else              /* else (cp == res) */
+               {
+                 if (slashp[3] != '\0')
+                   strcpy (cp, slashp + 4);
+                 else
+                   return ".";
+               }
+             slashp = cp;
+             continue;
+           }
+         else if (slashp[2] == '/' || slashp[2] == '\0')
+           {
+             strcpy (slashp, slashp + 2);
+             continue;
+           }
+       }
+
+      slashp = etags_strchr (slashp + 1, '/');
+    }
+
+  return res;
+}
+
+/* Return a newly allocated string containing the absolute
+   filename of dir where FILE resides given CWD (which should
+   end with a slash). */
+char *
+absolute_dirname (file, cwd)
+     char *file, *cwd;
+{
+  char *slashp, *res;
+  char save;
+
+  slashp = etags_strrchr (file, '/');
+  if (slashp == NULL)
+    return cwd;
+  save = slashp[1];
+  slashp[1] = '\0';
+  res = absolute_filename (file, cwd);
+  slashp[1] = save;
+
+  return res;
+}
+
+/* Like malloc but get fatal error if memory is exhausted.  */
 char *
 xmalloc (size)
-     int size;
+     unsigned int size;
 {
-  char *result = malloc (size);
-  if (!result)
+  char *result = (char *) malloc (size);
+  if (result == NULL)
     fatal ("virtual memory exhausted", 0);
   return result;
 }
@@ -2862,10 +3940,10 @@ xmalloc (size)
 char *
 xrealloc (ptr, size)
      char *ptr;
-     int size;
+     unsigned int size;
 {
-  char *result = realloc (ptr, size);
-  if (!result)
+  char *result = (char *) realloc (ptr, size);
+  if (result == NULL)
     fatal ("virtual memory exhausted");
   return result;
 }