(main): Avoid a buffer overrun with sprintf.
[bpt/emacs.git] / lib-src / etags.c
index 0928fad..b21175e 100644 (file)
@@ -1,5 +1,5 @@
-/* Tags file maker to go with GNU Emacs
-   Copyright (C) 1984, 87, 88, 89, 93, 94, 95
+/* Tags file maker to go with GNU Emacs           -*- coding: latin-1 -*-
+   Copyright (C) 1984, 1987-1989, 1993-1995, 1998-2001, 2002
    Free Software Foundation, Inc. and Ken Arnold
 
 This file is not considered part of GNU Emacs.
@@ -24,64 +24,149 @@ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  *     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.
- *     Regexp tags by Tom Tromey.
+ * 1989        Sam Kendall added C++.
+ * 1992 Joseph B. Wells improved C and C++ parsing.
+ * 1993        Francesco Potortì reorganised C and C++.
+ * 1994        Regexp tags by Tom Tromey.
+ * 2001 Nested classes by Francesco Potortì (ideas by Mykola Dzyuba).
  *
- *     Francesco Potorti` (F.Potorti@cnuce.cnr.it) is the current maintainer.
+ *     Francesco Potortì <pot@gnu.org> has maintained it since 1993.
  */
 
-char pot_etags_version[] = "@(#) pot revision number is 11.71";
+char pot_etags_version[] = "@(#) pot revision number is $Revision: 15.18 $";
 
 #define        TRUE    1
 #define        FALSE   0
 
-#ifndef DEBUG
-# define DEBUG FALSE
+#ifdef DEBUG
+#  undef DEBUG
+#  define DEBUG TRUE
+#else
+#  define DEBUG  FALSE
+#  define NDEBUG               /* disable assert */
+#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
+# define ETAGS_REGEXPS         /* use the regexp features */
+# define LONG_OPTIONS          /* accept long options */
+# ifndef PTR                   /* for Xemacs */
+#   define PTR void *
+# endif
+# ifndef __P                   /* for Xemacs */
+#   define __P(args) args
+# endif
+#else
+# if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
+#   define __P(args) args      /* use prototypes */
+#   define PTR void *          /* for generic pointers */
+# else
+#   define __P(args) ()                /* no prototypes */
+#   define const               /* remove const for old compilers' sake */
+#   define PTR long *          /* don't use void* */
+# endif
+#endif /* !HAVE_CONFIG_H */
+
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE 1         /* enables some compiler checks on GNU */
 #endif
 
+/* WIN32_NATIVE is for Xemacs.
+   MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
+#ifdef WIN32_NATIVE
+# undef MSDOS
+# undef  WINDOWSNT
+# define WINDOWSNT
+#endif /* WIN32_NATIVE */
+
 #ifdef MSDOS
-# include <string.h>
+# undef MSDOS
+# define MSDOS TRUE
 # include <fcntl.h>
 # include <sys/param.h>
+# include <io.h>
+# ifndef HAVE_CONFIG_H
+#   define DOS_NT
+#   include <sys/config.h>
+# endif
+#else
+# define MSDOS FALSE
 #endif /* MSDOS */
 
 #ifdef WINDOWSNT
 # include <stdlib.h>
 # include <fcntl.h>
 # include <string.h>
+# include <direct.h>
 # include <io.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
+# undef HAVE_NTGUI
+# undef  DOS_NT
+# define DOS_NT
+# ifndef HAVE_GETCWD
+#   define HAVE_GETCWD
+# endif /* undef HAVE_GETCWD */
+#else /* !WINDOWSNT */
+# ifdef STDC_HEADERS
+#  include <stdlib.h>
+#  include <string.h>
+# else
+    extern char *getenv ();
+# endif
+#endif /* !WINDOWSNT */
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#else
+# if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
+    extern char *getcwd (char *buf, size_t size);
+# endif
+#endif /* HAVE_UNISTD_H */
 
 #include <stdio.h>
 #include <ctype.h>
 #include <errno.h>
 #ifndef errno
-extern int errno;
+  extern int errno;
 #endif
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <assert.h>
+#ifdef NDEBUG
+# undef  assert                        /* some systems have a buggy assert.h */
+# define assert(x) ((void) 0)
+#endif
+
 #if !defined (S_ISREG) && defined (S_IFREG)
 # define S_ISREG(m)    (((m) & S_IFMT) == S_IFREG)
 #endif
 
-#include <getopt.h>
+#ifdef LONG_OPTIONS
+# include <getopt.h>
+#else
+# define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
+  extern char *optarg;
+  extern int optind, opterr;
+#endif /* LONG_OPTIONS */
 
 #ifdef ETAGS_REGEXPS
+# ifndef HAVE_CONFIG_H         /* this is a standalone compilation */
+#   ifdef __CYGWIN__           /* compiling on Cygwin */
+                            !!! NOTICE !!!
+ the regex.h distributed with Cygwin is not compatible with etags, alas!
+If you want regular expression support, you should delete this notice and
+             arrange to use the GNU regex.h and regex.c.
+#   endif
+# endif
 # include <regex.h>
 #endif /* ETAGS_REGEXPS */
 
 /* Define CTAGS to make the program "ctags" compatible with the usual one.
- Let it undefined to make the program "etags", which makes emacs-style
+ Leave 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
@@ -99,338 +184,479 @@ extern int errno;
 # define       BAD     1
 #endif
 
-/* C extensions. */
-#define C_PLPL 0x00001         /* C++ */
-#define C_STAR 0x00003         /* C* */
-#define YACC   0x10000         /* yacc file */
-
-#define streq(s,t)     ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strcmp(s,t))
-#define strneq(s,t,n)  ((DEBUG &&!(s)&&!(t)&&(abort(),1)) || !strncmp(s,t,n))
+#define streq(s,t)     (assert((s)!=NULL || (t)!=NULL), !strcmp (s, t))
+#define strneq(s,t,n)  (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
 
-#define lowcase(c)     tolower ((unsigned char)c)
+#define CHARS 256              /* 2^sizeof(char) */
+#define CHAR(x)                ((unsigned int)(x) & (CHARS - 1))
+#define        iswhite(c)      (_wht[CHAR(c)]) /* c is white */
+#define notinname(c)   (_nin[CHAR(c)]) /* c is not in a name */
+#define        begtoken(c)     (_btk[CHAR(c)]) /* c can start token */
+#define        intoken(c)      (_itk[CHAR(c)]) /* c can be in token */
+#define        endtoken(c)     (_etk[CHAR(c)]) /* c ends tokens */
 
-#define        iswhite(arg)    (_wht[arg])     /* T if char is white           */
-#define        begtoken(arg)   (_btk[arg])     /* T if char can start token    */
-#define        intoken(arg)    (_itk[arg])     /* T if char can be in token    */
-#define        endtoken(arg)   (_etk[arg])     /* T if char ends tokens        */
+#define ISALNUM(c)     isalnum (CHAR(c))
+#define ISALPHA(c)     isalpha (CHAR(c))
+#define ISDIGIT(c)     isdigit (CHAR(c))
+#define ISLOWER(c)     islower (CHAR(c))
 
-#ifdef DOS_NT
-# define absolutefn(fn) (fn[0] == '/' \
-                        || (fn[1] == ':' && fn[2] == '/'))
-#else
-# define absolutefn(fn) (fn[0] == '/')
-#endif
+#define lowcase(c)     tolower (CHAR(c))
+#define upcase(c)      toupper (CHAR(c))
 
 
 /*
- *     xnew -- allocate storage
+ *     xnew, xrnew -- allocate, reallocate storage
  *
  * SYNOPSIS:   Type *xnew (int n, Type);
+ *             void xrnew (OldPointer, int n, Type);
  */
-#define xnew(n,Type)   ((Type *) xmalloc ((n) * sizeof (Type)))
-
-typedef int logical;
-
-typedef struct nd_st
-{                              /* sorting structure            */
-  char *name;                  /* function or type name        */
-  char *file;                  /* file name                    */
-  logical is_func;             /* use pattern or line no       */
-  logical been_warned;         /* set if noticed dup           */
-  int lno;                     /* line number tag is on        */
-  long cno;                    /* character number line starts on */
-  char *pat;                   /* search pattern               */
-  struct nd_st *left, *right;  /* left and right sons          */
-} NODE;
-
-extern char *getenv ();
-
-char *concat ();
-char *savenstr (), *savestr ();
-char *etags_strchr (), *etags_strrchr ();
-char *etags_getcwd ();
-char *relative_filename (), *absolute_filename (), *absolute_dirname ();
-long *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 Erlang_functions;
-Lang_function Fortran_functions;
-Lang_function Yacc_entries;
-Lang_function Lisp_functions;
-Lang_function Pascal_functions;
-Lang_function Perl_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 C_entries ();
-void default_C_entries ();
-void plain_C_entries ();
-void Cplusplus_entries ();
-void Cstar_entries ();
-void Erlang_functions ();
-void Fortran_functions ();
-void Yacc_entries ();
-void Lisp_functions ();
-void Pascal_functions ();
-void Perl_functions ();
-void Prolog_functions ();
-void Scheme_functions ();
-void TeX_functions ();
-void just_read_file ();
-#endif
-
-Lang_function *get_language_from_name ();
-Lang_function *get_language_from_interpreter ();
-Lang_function *get_language_from_suffix ();
-int total_size_of_entries ();
-long readline ();
-long readline_internal ();
-#ifdef ETAGS_REGEXPS
-void add_regex ();
+#if DEBUG
+# include "chkmalloc.h"
+# define xnew(n,Type)    ((Type *) trace_malloc (__FILE__, __LINE__, \
+                                                 (n) * sizeof (Type)))
+# define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
+                                       (char *) (op), (n) * sizeof (Type)))
+#else
+# define xnew(n,Type)    ((Type *) xmalloc ((n) * sizeof (Type)))
+# define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
+                                       (char *) (op), (n) * sizeof (Type)))
 #endif
-void add_node ();
-void error ();
-void suggest_asking_for_help ();
-void fatal (), pfatal ();
-void find_entries ();
-void free_tree ();
-void getit ();
-void init ();
-void initbuffer ();
-void pfnote ();
-void process_file ();
-void put_entries ();
-void takeprec ();
 
-\f
-char searchar = '/';           /* use /.../ searches */
+#define bool int
 
-int lineno;                    /* line number of current line */
-long charno;                   /* current character number */
-long linecharno;               /* charno of start of line */
+typedef void Lang_function __P((FILE *));
 
-char *curfile;                 /* current input file name */
-char *tagfile;                 /* output file */
-char *progname;                        /* name this program was invoked with */
-char *cwd;                     /* current working directory */
-char *tagfiledir;              /* directory of tagfile */
+typedef struct
+{
+  char *suffix;                        /* file name suffix for this compressor */
+  char *command;               /* takes one arg and decompresses to stdout */
+} compressor;
 
-FILE *tagf;                    /* ioptr for tags file */
-NODE *head;                    /* the head of the binary tree of tags */
+typedef struct
+{
+  char *name;                  /* language name */
+  bool metasource;             /* source used to generate other sources */
+  Lang_function *function;     /* parse function */
+  char **filenames;            /* names of this language's files */
+  char **suffixes;             /* name suffixes of this language's files */
+  char **interpreters;         /* interpreters for this language */
+} language;
+
+typedef struct fdesc
+{
+  struct fdesc *next;          /* for the linked list */
+  char *infname;               /* uncompressed input file name */
+  char *infabsname;            /* absolute uncompressed input file name */
+  char *infabsdir;             /* absolute dir of input file */
+  char *taggedfname;           /* file name to write in tagfile */
+  language *lang;              /* language of file */
+  char *prop;                  /* file properties to write in tagfile */
+  bool usecharno;              /* etags tags shall contain char number */
+} fdesc;
+
+typedef struct node_st
+{                              /* sorting structure */
+  struct node_st *left, *right;        /* left and right sons */
+  fdesc *fdp;                  /* description of file to whom tag belongs */
+  char *name;                  /* tag name */
+  char *pat;                   /* search pattern */
+  bool valid;                  /* write this tag on the tag file */
+  bool is_func;                        /* function tag: use pattern in CTAGS mode */
+  bool been_warned;            /* warning already given for duplicated tag */
+  int lno;                     /* line number tag is on */
+  long cno;                    /* character number line starts on */
+} node;
 
 /*
- * 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.
+ * A `linebuffer' is a structure which holds a line of text.
+ * `readline_internal' reads a line from a stream into a linebuffer
+ * and works regardless of the length of the line.
+ * SIZE is the size of BUFFER, LEN is the length of the string in
+ * BUFFER after readline reads it.
  */
-#define GROW_LINEBUFFER(buf,toksize)                                   \
-while (buf.size < toksize)                                             \
-  buf.buffer = (char *) xrealloc (buf.buffer, buf.size *= 2)
-struct linebuffer
+typedef struct
 {
   long size;
+  int len;
   char *buffer;
-};
+} linebuffer;
 
-struct linebuffer lb;          /* the current line */
-struct linebuffer token_name;  /* used by C_entries as a temporary area */
-struct
+/* Used to support mixing of --lang and file names. */
+typedef struct
 {
-  long linepos;
-  struct linebuffer lb;                /* used by C_entries instead of lb */
-} lbs[2];
+  enum {
+    at_language,               /* a language specification */
+    at_regexp,                 /* a regular expression */
+    at_icregexp,               /* same, but with case ignored */
+    at_filename                        /* a file name */
+  } arg_type;                  /* argument type */
+  language *lang;              /* language associated with the argument */
+  char *what;                  /* the argument itself */
+} argument;
+
+#ifdef ETAGS_REGEXPS
+/* Structure defining a regular expression. */
+typedef struct pattern
+{
+  struct pattern *p_next;
+  language *lang;
+  char *regex;
+  struct re_pattern_buffer *pat;
+  struct re_registers regs;
+  char *name_pattern;
+  bool error_signaled;
+  bool ignore_case;
+} pattern;
+#endif /* ETAGS_REGEXPS */
+
+
+/* Many compilers barf on this:
+       Lang_function Ada_funcs;
+   so let's write it this way */
+static void Ada_funcs __P((FILE *));
+static void Asm_labels __P((FILE *));
+static void C_entries __P((int c_ext, FILE *));
+static void default_C_entries __P((FILE *));
+static void plain_C_entries __P((FILE *));
+static void Cjava_entries __P((FILE *));
+static void Cobol_paragraphs __P((FILE *));
+static void Cplusplus_entries __P((FILE *));
+static void Cstar_entries __P((FILE *));
+static void Erlang_functions __P((FILE *));
+static void Fortran_functions __P((FILE *));
+static void Yacc_entries __P((FILE *));
+static void Lisp_functions __P((FILE *));
+static void Makefile_targets __P((FILE *));
+static void Pascal_functions __P((FILE *));
+static void Perl_functions __P((FILE *));
+static void PHP_functions __P((FILE *));
+static void Postscript_functions __P((FILE *));
+static void Prolog_functions __P((FILE *));
+static void Python_functions __P((FILE *));
+static void Scheme_functions __P((FILE *));
+static void TeX_commands __P((FILE *));
+static void Texinfo_nodes __P((FILE *));
+static void just_read_file __P((FILE *));
+
+static void print_language_names __P((void));
+static void print_version __P((void));
+static void print_help __P((void));
+int main __P((int, char **));
+
+static compressor *get_compressor_from_suffix __P((char *, char **));
+static language *get_language_from_langname __P((const char *));
+static language *get_language_from_interpreter __P((char *));
+static language *get_language_from_filename __P((char *, bool));
+static long readline __P((linebuffer *, FILE *));
+static long readline_internal __P((linebuffer *, FILE *));
+static bool nocase_tail __P((char *));
+static char *get_tag __P((char *));
+
+#ifdef ETAGS_REGEXPS
+static void analyse_regex __P((char *, bool));
+static void add_regex __P((char *, bool, language *));
+static void free_patterns __P((void));
+#endif /* ETAGS_REGEXPS */
+static void error __P((const char *, const char *));
+static void suggest_asking_for_help __P((void));
+void fatal __P((char *, char *));
+static void pfatal __P((char *));
+static void add_node __P((node *, node **));
+
+static void init __P((void));
+static void initbuffer __P((linebuffer *));
+static void process_file __P((char *, language *));
+static void find_entries __P((FILE *));
+static void free_tree __P((node *));
+static void free_fdesc __P((fdesc *));
+static void pfnote __P((char *, bool, char *, int, int, long));
+static void new_pfnote __P((char *, int, bool, char *, int, int, long));
+static void invalidate_nodes __P((fdesc *, node **));
+static void put_entries __P((node *));
+
+static char *concat __P((char *, char *, char *));
+static char *skip_spaces __P((char *));
+static char *skip_non_spaces __P((char *));
+static char *savenstr __P((char *, int));
+static char *savestr __P((char *));
+static char *etags_strchr __P((const char *, int));
+static char *etags_strrchr __P((const char *, int));
+static bool strcaseeq __P((const char *, const char *));
+static char *etags_getcwd __P((void));
+static char *relative_filename __P((char *, char *));
+static char *absolute_filename __P((char *, char *));
+static char *absolute_dirname __P((char *, char *));
+static bool filename_is_absolute __P((char *f));
+static void canonicalize_filename __P((char *));
+static void linebuffer_setlen __P((linebuffer *, int));
+static PTR xmalloc __P((unsigned int));
+static PTR xrealloc __P((char *, unsigned int));
+
+\f
+static char searchar = '/';    /* use /.../ searches */
+
+static char *tagfile;          /* output file */
+static char *progname;         /* name this program was invoked with */
+static char *cwd;              /* current working directory */
+static char *tagfiledir;       /* directory of tagfile */
+static FILE *tagf;             /* ioptr for tags file */
+
+static fdesc *fdhead;          /* head of file description list */
+static fdesc *curfdp;          /* current file description */
+static int lineno;             /* line number of current line */
+static long charno;            /* current character number */
+static long linecharno;                /* charno of start of current line */
+static char *dbp;              /* pointer to start of current tag */
+
+static const int invalidcharno = -1;
+
+static node *nodehead;         /* the head of the binary tree of tags */
+static node *last_node;                /* the last node created */
+
+static linebuffer lb;          /* the current line */
 
 /* boolean "functions" (see init)      */
-logical _wht[0177], _etk[0177], _itk[0177], _btk[0177];
-char
+static bool _wht[CHARS], _nin[CHARS], _itk[CHARS], _btk[CHARS], _etk[CHARS];
+static char
   /* white chars */
-  *white = " \f\t\n\013",
+  *white = " \f\t\n\r\v",
+  /* not in a name */
+  *nonam = " \f\t\n\r()=,;",
   /* token ending chars */
-  *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
+  *endtk = " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
   /* token starting chars */
   *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
   /* valid in-token chars */
-  *intk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
+  *midtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
 
-logical append_to_tagfile;     /* -a: append to tags */
-/* The following three default to TRUE for etags, but to FALSE for ctags.  */
-logical typedefs;              /* -t: create tags for typedefs */
-logical typedefs_and_cplusplus;        /* -T: create tags for typedefs, level */
+static bool append_to_tagfile; /* -a: append to tags */
+/* The next four default to TRUE for etags, but to FALSE for ctags.  */
+static bool typedefs;          /* -t: create tags for C and Ada typedefs */
+static bool typedefs_or_cplusplus; /* -T: create tags for C typedefs, level */
                                /* 0 struct/enum/union decls, and C++ */
                                /* member functions. */
-logical constantypedefs;       /* -d: create tags for C #define and enum */
-                               /* constants.  Enum consts not implemented. */
+static bool constantypedefs;   /* -d: create tags for C #define, enum */
+                               /* constants and variables. */
                                /* -D: opposite of -d.  Default under ctags. */
-logical update;                        /* -u: update tags */
-logical vgrind_style;          /* -v: create vgrind style index output */
-logical no_warnings;           /* -w: suppress warnings */
-logical cxref_style;           /* -x: create cxref style output */
-logical cplusplus;             /* .[hc] means C++, not C */
-logical noindentypedefs;       /* -I: ignore indentation in C */
-
-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, 'I' },
-  { "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 }
-};
+static bool globals;           /* create tags for global variables */
+static bool declarations;      /* --declarations: tag them and extern in C&Co*/
+static bool members;           /* create tags for C member variables */
+static bool no_line_directive; /* ignore #line directives (undocumented) */
+static bool update;            /* -u: update tags */
+static bool vgrind_style;      /* -v: create vgrind style index output */
+static bool no_warnings;       /* -w: suppress warnings */
+static bool cxref_style;       /* -x: create cxref style output */
+static bool cplusplus;         /* .[hc] means C++, not C */
+static bool noindentypedefs;   /* -I: ignore indentation in C */
+static bool packages_only;     /* --packages-only: in Ada, only tag packages*/
 
 #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;
-};
+/* List of all regexps. */
+static pattern *p_head;
 
-/* Number of regexps found. */
-int num_patterns = 0;
+/* How many characters in the character set.  (From regex.c.)  */
+#define CHAR_SET_SIZE 256
+/* Translation table for case-insensitive matching. */
+static char lc_trans[CHAR_SET_SIZE];
+#endif /* ETAGS_REGEXPS */
 
-/* Array of all regexps. */
-struct pattern *patterns = NULL;
+#ifdef LONG_OPTIONS
+static struct option longopts[] =
+{
+  { "packages-only",      no_argument,      &packages_only,     TRUE  },
+  { "c++",               no_argument,       NULL,               'C'   },
+  { "declarations",      no_argument,       &declarations,      TRUE  },
+  { "no-line-directive",  no_argument,      &no_line_directive, TRUE  },
+  { "help",              no_argument,       NULL,               'h'   },
+  { "help",              no_argument,       NULL,               'H'   },
+  { "ignore-indentation", no_argument,      NULL,               'I'   },
+  { "language",           required_argument, NULL,                      'l'   },
+  { "members",           no_argument,       &members,           TRUE  },
+  { "no-members",        no_argument,       &members,           FALSE },
+  { "output",            required_argument, NULL,               'o'   },
+#ifdef ETAGS_REGEXPS
+  { "regex",             required_argument, NULL,               'r'   },
+  { "no-regex",                  no_argument,       NULL,               'R'   },
+  { "ignore-case-regex",  required_argument, NULL,              'c'   },
 #endif /* ETAGS_REGEXPS */
+  { "version",           no_argument,       NULL,               'V'   },
+
+#if CTAGS /* Etags options */
+  { "backward-search",   no_argument,       NULL,               'B'   },
+  { "cxref",             no_argument,       NULL,               'x'   },
+  { "defines",           no_argument,       NULL,               'd'   },
+  { "globals",           no_argument,       &globals,           TRUE  },
+  { "typedefs",                  no_argument,       NULL,               't'   },
+  { "typedefs-and-c++",          no_argument,       NULL,               'T'   },
+  { "update",            no_argument,       NULL,               'u'   },
+  { "vgrind",            no_argument,       NULL,               'v'   },
+  { "no-warn",           no_argument,       NULL,               'w'   },
+
+#else /* Ctags options */
+  { "append",            no_argument,       NULL,               'a'   },
+  { "no-defines",        no_argument,       NULL,               'D'   },
+  { "no-globals",        no_argument,       &globals,           FALSE },
+  { "include",           required_argument, NULL,               'i'   },
+#endif
+  { NULL }
+};
+#endif /* LONG_OPTIONS */
+
+static compressor compressors[] =
+{
+  { "z", "gzip -d -c"},
+  { "Z", "gzip -d -c"},
+  { "gz", "gzip -d -c"},
+  { "GZ", "gzip -d -c"},
+  { "bz2", "bzip2 -d -c" },
+  { NULL }
+};
 
 /*
  * Language stuff.
  */
 
-/* Non-NULL if language fixed. */
-Lang_function *lang_func = NULL;
+/* Ada code */
+static char *Ada_suffixes [] =
+  { "ads", "adb", "ada", NULL };
 
 /* Assembly code */
-char *Asm_suffixes [] = { "a", /* Unix assembler */
-                         "asm", /* Microcontroller assembly */
-                         "def", /* BSO/Tasking definition includes  */
-                         "inc", /* Microcontroller include files */
-                         "ins", /* Microcontroller include files */
-                         "s", "sa", /* Unix assembler */
-                         "src", /* BSO/Tasking C compiler output */
-                         NULL
-                       };
+static char *Asm_suffixes [] =
+  { "a",       /* Unix assembler */
+    "asm", /* Microcontroller assembly */
+    "def", /* BSO/Tasking definition includes  */
+    "inc", /* Microcontroller include files */
+    "ins", /* Microcontroller include files */
+    "s", "sa", /* Unix assembler */
+    "S",   /* cpp-processed Unix assembler */
+    "src", /* BSO/Tasking C compiler output */
+    NULL
+  };
 
 /* Note that .c and .h can be considered C++, if the --c++ flag was
-   given.  That is why default_C_entries is called here. */
-char *default_C_suffixes [] =
+   given, or if the `class' keyowrd is met inside the file.
+   That is why default_C_entries is called for these. */
+static char *default_C_suffixes [] =
   { "c", "h", NULL };
 
-/* .M is for Objective C++ files. */
-char *Cplusplus_suffixes [] =
-  { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx", "M", NULL};
+static char *Cplusplus_suffixes [] =
+  { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
+    "M",                       /* Objective C++ */
+    "pdb",                     /* Postscript with C syntax */
+    NULL };
+
+static char *Cjava_suffixes [] =
+  { "java", NULL };
 
-char *Cstar_suffixes [] =
+static char *Cobol_suffixes [] =
+  { "COB", "cob", NULL };
+
+static char *Cstar_suffixes [] =
   { "cs", "hs", NULL };
 
-char *Erlang_suffixes [] =
+static char *Erlang_suffixes [] =
   { "erl", "hrl", NULL };
 
-char *Fortran_suffixes [] =
+static char *Fortran_suffixes [] =
   { "F", "f", "f90", "for", NULL };
 
-char *Lisp_suffixes [] =
-  { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL };
+static char *Lisp_suffixes [] =
+  { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL };
+
+static char *Makefile_filenames [] =
+  { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL};
 
-char *Pascal_suffixes [] =
+static char *Pascal_suffixes [] =
   { "p", "pas", NULL };
 
-char *Perl_suffixes [] =
+static char *Perl_suffixes [] =
   { "pl", "pm", NULL };
-char *Perl_interpreters [] =
+
+static char *Perl_interpreters [] =
   { "perl", "@PERL@", NULL };
 
-char *plain_C_suffixes [] =
-  { "pc",                      /* Pro*C file */
+static char *PHP_suffixes [] =
+  { "php", "php3", "php4", NULL };
+
+static char *plain_C_suffixes [] =
+  { "lm",                      /* Objective lex file */
     "m",                       /* Objective C file */
-    "lm",                      /* Objective lex file */
+    "pc",                      /* Pro*C file */
      NULL };
 
-char *Prolog_suffixes [] =
+static char *Postscript_suffixes [] =
+  { "ps", "psw", NULL };       /* .psw is for PSWrap */
+
+static char *Prolog_suffixes [] =
   { "prolog", NULL };
 
+static char *Python_suffixes [] =
+  { "py", NULL };
+
 /* Can't do the `SCM' or `scm' prefix with a version number. */
-char *Scheme_suffixes [] =
-  { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL };
+static char *Scheme_suffixes [] =
+  { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL };
 
-char *TeX_suffixes [] =
-  { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL };
+static char *TeX_suffixes [] =
+  { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL };
 
-char *Yacc_suffixes [] =
-  { "y", "ym", NULL };         /* .ym is Objective yacc file */
+static char *Texinfo_suffixes [] =
+  { "texi", "texinfo", "txi", NULL };
 
-/* Table of language names and corresponding functions, file suffixes
-   and interpreter names.
-   It is ok for a given function to be listed under more than one
-   name.  I just didn't. */
-struct lang_entry
-{
-  char *name;
-  Lang_function *function;
-  char **suffixes;
-  char **interpreters;
-};
+static char *Yacc_suffixes [] =
+  { "y", "y++", "ym", "yxx", "yy", NULL }; /* .ym is Objective yacc file */
+
+/*
+ * Table of languages.
+ *
+ * It is ok for a given function to be listed under more than one
+ * name.  I just didn't.
+ */
 
-struct lang_entry lang_names [] =
+static language lang_names [] =
 {
-  { "asm",     Asm_labels,         Asm_suffixes,         NULL              },
-  { "c",       default_C_entries,   default_C_suffixes,          NULL              },
-  { "c++",     Cplusplus_entries,   Cplusplus_suffixes,          NULL              },
-  { "c*",      Cstar_entries,      Cstar_suffixes,       NULL              },
-  { "erlang",  Erlang_functions,    Erlang_suffixes,     NULL              },
-  { "fortran", Fortran_functions,   Fortran_suffixes,    NULL              },
-  { "lisp",    Lisp_functions,     Lisp_suffixes,        NULL              },
-  { "pascal",  Pascal_functions,    Pascal_suffixes,     NULL              },
-  { "perl",    Perl_functions,     Perl_suffixes,        Perl_interpreters },
-  { "proc",    plain_C_entries,            plain_C_suffixes,     NULL              },
-  { "prolog",  Prolog_functions,    Prolog_suffixes,     NULL              },
-  { "scheme",  Scheme_functions,    Scheme_suffixes,     NULL              },
-  { "tex",     TeX_functions,      TeX_suffixes,         NULL              },
-  { "yacc",    Yacc_entries,       Yacc_suffixes,        NULL              },
-  { "auto", NULL },             /* default guessing scheme */
-  { "none", just_read_file },   /* regexp matching only */
-  { NULL, NULL }                /* end of list */
+  { "ada",      FALSE, Ada_funcs,            NULL, Ada_suffixes,        NULL },
+  { "asm",      FALSE, Asm_labels,           NULL, Asm_suffixes,        NULL },
+  { "c",        FALSE, default_C_entries,    NULL, default_C_suffixes,  NULL },
+  { "c++",      FALSE, Cplusplus_entries,    NULL, Cplusplus_suffixes,  NULL },
+  { "c*",       FALSE, Cstar_entries,        NULL, Cstar_suffixes,      NULL },
+  { "cobol",    FALSE, Cobol_paragraphs,     NULL, Cobol_suffixes,      NULL },
+  { "erlang",   FALSE, Erlang_functions,     NULL, Erlang_suffixes,     NULL },
+  { "fortran",  FALSE, Fortran_functions,    NULL, Fortran_suffixes,    NULL },
+  { "java",     FALSE, Cjava_entries,        NULL, Cjava_suffixes,      NULL },
+  { "lisp",     FALSE, Lisp_functions,       NULL, Lisp_suffixes,       NULL },
+  { "makefile", FALSE, Makefile_targets,     Makefile_filenames, NULL,  NULL },
+  { "pascal",   FALSE, Pascal_functions,     NULL, Pascal_suffixes,     NULL },
+  { "perl",     FALSE, Perl_functions,NULL, Perl_suffixes, Perl_interpreters },
+  { "php",      FALSE, PHP_functions,        NULL, PHP_suffixes,        NULL },
+  { "postscript",FALSE, Postscript_functions,NULL, Postscript_suffixes, NULL },
+  { "proc",     FALSE, plain_C_entries,      NULL, plain_C_suffixes,    NULL },
+  { "prolog",   FALSE, Prolog_functions,     NULL, Prolog_suffixes,     NULL },
+  { "python",   FALSE, Python_functions,     NULL, Python_suffixes,     NULL },
+  { "scheme",   FALSE, Scheme_functions,     NULL, Scheme_suffixes,     NULL },
+  { "tex",      FALSE, TeX_commands,         NULL, TeX_suffixes,        NULL },
+  { "texinfo",  FALSE, Texinfo_nodes,        NULL, Texinfo_suffixes,    NULL },
+  { "yacc",      TRUE, Yacc_entries,         NULL, Yacc_suffixes,       NULL },
+  { "auto", FALSE, NULL },             /* default guessing scheme */
+  { "none", FALSE, just_read_file },   /* regexp matching only */
+  { NULL, FALSE, NULL }                /* end of list */
 };
 
 \f
-void
+static void
 print_language_names ()
 {
-  struct lang_entry *lang;
-  char **ext;
+  language *lang;
+  char **name, **ext;
 
   puts ("\nThese are the currently supported languages, along with the\n\
-default file name suffixes:");
+default file names and dot suffixes:");
   for (lang = lang_names; lang->name != NULL; lang++)
     {
-      printf ("\t%s\t", lang->name);
+      printf ("  %-*s", 10, lang->name);
+      if (lang->filenames != NULL)
+       for (name = lang->filenames; *name != NULL; name++)
+         printf (" %s", *name);
       if (lang->suffixes != NULL)
        for (ext = lang->suffixes; *ext != NULL; ext++)
          printf (" .%s", *ext);
@@ -441,70 +667,110 @@ name suffix, and `none' means only do regexp processing on files.\n\
 If no language is specified and no matching suffix is found,\n\
 the first line of the file is read for a sharp-bang (#!) sequence\n\
 followed by the name of an interpreter.  If no such sequence is found,\n\
-Fortran is tried first; if no tags are found, C is tried next.");
+Fortran is tried first; if no tags are found, C is tried next.\n\
+When parsing any C file, a \"class\" keyword switches to C++.\n\
+Compressed files are supported using gzip and bzip2.");
 }
 
+#ifndef EMACS_NAME
+# define EMACS_NAME "GNU Emacs"
+#endif
 #ifndef VERSION
-# define VERSION "19"
+# define VERSION "21"
 #endif
-void
+static void
 print_version ()
 {
-  printf ("%s (GNU Emacs %s)\n", (CTAGS) ? "ctags" : "etags", VERSION);
-  puts ("Copyright (C) 1996 Free Software Foundation, Inc. and Ken Arnold");
+  printf ("%s (%s %s)\n", (CTAGS) ? "ctags" : "etags", EMACS_NAME, VERSION);
+  puts ("Copyright (C) 2002 Free Software Foundation, Inc. and Ken Arnold");
   puts ("This program is distributed under the same terms as Emacs");
 
   exit (GOOD);
 }
 
-void
+static 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.", progname);
-  if (!CTAGS)
-    printf ("  Absolute names are stored in the output file as they\n\
-are.  Relative ones are stored relative to the output file's directory.");
-  puts ("\n");
+  printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
+\n\
+These are the options accepted by %s.\n", progname, progname);
+#ifdef LONG_OPTIONS
+  puts ("You may use unambiguous abbreviations for the long option names.");
+#else
+  puts ("Long option names do not work with this executable, as it is not\n\
+linked with GNU getopt.");
+#endif /* LONG_OPTIONS */
+  puts ("  A - as file name means read names from stdin (one per line).\n\
+Absolute names are stored in the output file as they are.\n\
+Relative ones are stored relative to the output file's directory.\n");
 
-  puts ("-a, --append\n\
+  if (!CTAGS)
+    puts ("-a, --append\n\
         Append tag entries to existing tags file.");
 
+  puts ("--packages-only\n\
+        For Ada files, only generate tags for packages.");
+
   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.");
 
+  /* This option is mostly obsolete, because etags can now automatically
+     detect C++.  Retained for backward compatibility and for debugging and
+     experimentation.  In principle, we could want to tag as C++ even
+     before any "class" keyword.
   puts ("-C, --c++\n\
         Treat files whose name suffix defaults to C language as C++ files.");
+  */
+
+  puts ("--declarations\n\
+       In C and derived languages, create tags for function declarations,");
+  if (CTAGS)
+    puts ("\tand create tags for extern variables if --globals is used.");
+  else
+    puts
+      ("\tand create tags for extern variables unless --no-globals is used.");
 
   if (CTAGS)
     puts ("-d, --defines\n\
-        Create tag entries for constant C #defines, too.");
+        Create tag entries for C #define constants and enum constants, too.");
   else
     puts ("-D, --no-defines\n\
-        Don't create tag entries for constant C #defines.  This makes\n\
-       the tags file smaller.");
+        Don't create tag entries for C #define constants and enum constants.\n\
+       This makes the tags file smaller.");
 
   if (!CTAGS)
-    {
-      puts ("-i FILE, --include=FILE\n\
+    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\
+
+  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.");
-    }
+
+  if (CTAGS)
+    puts ("--globals\n\
+       Create tag entries for global variables in some languages.");
+  else
+    puts ("--no-globals\n\
+       Do not create tag entries for global variables in some\n\
+       languages.  This makes the tags file smaller.");
+  puts ("--members\n\
+       Create tag entries for member variables in C and derived languages.");
 
 #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 /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
+        Make a tag for each line matching pattern REGEXP in the following\n\
+       files.  {LANGUAGE}/REGEXP/ uses REGEXP for LANGUAGE files only.\n\
+       regexfile is a file containing one REGEXP per line.\n\
+       REGEXP is anchored (as if preceded by ^).\n\
+       The form /REGEXP/NAME/ creates a named tag.\n\
+       For example Tcl named tags can be created with:\n\
+       --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\"");
+  puts ("-c /REGEXP/, --ignore-case-regex=/REGEXP/ or --ignore-case-regex=@regexfile\n\
+        Like -r, --regex but ignore case when matching expressions.");
   puts ("-R, --no-regex\n\
         Don't create tags from regexps for the following files.");
 #endif /* ETAGS_REGEXPS */
@@ -519,17 +785,23 @@ are.  Relative ones are stored relative to the output file's directory.");
   if (CTAGS)
     {
       puts ("-t, --typedefs\n\
-        Generate tag entries for C typedefs.");
+        Generate tag entries for C and Ada 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\
+    }
+
+  if (CTAGS)
+    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.");
+
+  if (CTAGS)
+    {
       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\
@@ -552,27 +824,12 @@ are.  Relative ones are stored relative to the output file's directory.");
   print_language_names ();
 
   puts ("");
-  puts ("Report bugs to bug-gnu-emacs@prep.ai.mit.edu");
+  puts ("Report bugs to bug-gnu-emacs@gnu.org");
 
   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'
@@ -587,7 +844,7 @@ typedef struct      {
 
 /*
  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
+ returning in each successive call the next file name 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
@@ -596,7 +853,7 @@ typedef struct      {
  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
+ With each successive file name 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.
@@ -605,7 +862,7 @@ typedef struct      {
 #include       <rmsdef.h>
 #include       <descrip.h>
 #define                OUTSIZE MAX_FILE_SPEC_LEN
-short
+static short
 fn_exp (out, in)
      vspec *out;
      char *in;
@@ -613,7 +870,7 @@ fn_exp (out, in)
   static long context = 0;
   static struct dsc$descriptor_s o;
   static struct dsc$descriptor_s i;
-  static logical pass1 = TRUE;
+  static bool pass1 = TRUE;
   long status;
   short retval;
 
@@ -650,10 +907,10 @@ fn_exp (out, in)
   v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
   name of each file specified by the provided arg expanding wildcards.
 */
-char *
+static char *
 gfnames (arg, p_error)
      char *arg;
-     logical *p_error;
+     bool *p_error;
 {
   static vspec filename = {MAX_FILE_SPEC_LEN, "\0"};
 
@@ -675,7 +932,7 @@ gfnames (arg, p_error)
 system (cmd)
      char *cmd;
 {
-  fprintf (stderr, "system() function not implemented under VMS\n");
+  error ("%s", "system() function not implemented under VMS");
 }
 #endif
 
@@ -704,14 +961,13 @@ main (argc, argv)
      char *argv[];
 {
   int i;
-  unsigned int nincluded_files = 0;
-  char **included_files = xnew (argc, char *);
-  char *this_file;
+  unsigned int nincluded_files;
+  char **included_files;
   argument *argbuffer;
-  int current_arg = 0, file_count = 0;
-  struct linebuffer filename_lb;
+  int current_arg, file_count;
+  linebuffer filename_lb;
 #ifdef VMS
-  logical got_err;
+  bool got_err;
 #endif
 
 #ifdef DOS_NT
@@ -719,6 +975,10 @@ main (argc, argv)
 #endif /* DOS_NT */
 
   progname = argv[0];
+  nincluded_files = 0;
+  included_files = xnew (argc, char *);
+  current_arg = 0;
+  file_count = 0;
 
   /* Allocate enough no matter what happens.  Overkill, but each one
      is small. */
@@ -726,21 +986,41 @@ main (argc, argv)
 
 #ifdef ETAGS_REGEXPS
   /* Set syntax for regular expression routines. */
-  re_set_syntax (RE_SYNTAX_EMACS);
+  re_set_syntax (RE_SYNTAX_EMACS | RE_INTERVALS);
+  /* Translation table for case-insensitive search. */
+  for (i = 0; i < CHAR_SET_SIZE; i++)
+    lc_trans[i] = lowcase (i);
 #endif /* ETAGS_REGEXPS */
 
   /*
    * If etags, always find typedefs and structure tags.  Why not?
-   * Also default is to find macro constants.
+   * Also default to find macro constants, enum constants and
+   * global variables.
    */
   if (!CTAGS)
-    typedefs = typedefs_and_cplusplus = constantypedefs = TRUE;
+    {
+      typedefs = typedefs_or_cplusplus = constantypedefs = TRUE;
+      globals = TRUE;
+    }
 
   while (1)
     {
-      int opt = getopt_long (argc, argv,
-                            "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts, 0);
+      int opt;
+      char *optstring = "-";
+
+#ifdef ETAGS_REGEXPS
+      optstring = "-r:Rc:";
+#endif /* ETAGS_REGEXPS */
+
+#ifndef LONG_OPTIONS
+      optstring = optstring + 1;
+#endif /* LONG_OPTIONS */
 
+      optstring = concat (optstring,
+                         "Cf:Il:o:SVhH",
+                         (CTAGS) ? "BxdtTuvw" : "aDi:");
+
+      opt = getopt_long (argc, argv, optstring, longopts, 0);
       if (opt == EOF)
        break;
 
@@ -752,7 +1032,7 @@ main (argc, argv)
          break;
 
        case 1:
-         /* This means that a filename has been seen.  Record it. */
+         /* This means that a file name has been seen.  Record it. */
          argbuffer[current_arg].arg_type = at_filename;
          argbuffer[current_arg].what = optarg;
          ++current_arg;
@@ -760,24 +1040,12 @@ main (argc, argv)
          break;
 
          /* Common options. */
-       case 'a':
-         append_to_tagfile = TRUE;
-         break;
-       case 'C':
-         cplusplus = TRUE;
-         break;
-       case 'd':
-         constantypedefs = TRUE;
-         break;
-       case 'D':
-         constantypedefs = FALSE;
-         break;
+       case 'C': cplusplus = TRUE;             break;
        case 'f':               /* for compatibility with old makefiles */
        case 'o':
          if (tagfile)
            {
-             fprintf (stderr, "%s: -%c option may only be given once.\n",
-                      progname, opt);
+             error ("-o option may only be given once.", (char *)NULL);
              suggest_asking_for_help ();
            }
          tagfile = optarg;
@@ -787,11 +1055,16 @@ main (argc, argv)
          noindentypedefs = TRUE;
          break;
        case 'l':
-         argbuffer[current_arg].function = get_language_from_name (optarg);
-         argbuffer[current_arg].arg_type = at_language;
-         ++current_arg;
+         {
+           language *lang = get_language_from_langname (optarg);
+           if (lang != NULL)
+             {
+               argbuffer[current_arg].lang = lang;
+               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;
@@ -802,7 +1075,11 @@ main (argc, argv)
          argbuffer[current_arg].what = NULL;
          ++current_arg;
          break;
-#endif /* ETAGS_REGEXPS */
+        case 'c':
+         argbuffer[current_arg].arg_type = at_icregexp;
+         argbuffer[current_arg].what = optarg;
+         ++current_arg;
+         break;
        case 'V':
          print_version ();
          break;
@@ -810,35 +1087,21 @@ main (argc, argv)
        case 'H':
          print_help ();
          break;
-       case 't':
-         typedefs = TRUE;
-         break;
-       case 'T':
-         typedefs = typedefs_and_cplusplus = TRUE;
-         break;
-#if (!CTAGS)
+
          /* Etags options */
-       case 'i':
-         included_files[nincluded_files++] = optarg;
-         break;
-#else /* CTAGS */
+       case 'a': append_to_tagfile = TRUE;                     break;
+       case 'D': constantypedefs = FALSE;                      break;
+       case 'i': included_files[nincluded_files++] = optarg;   break;
+
          /* Ctags options. */
-       case 'B':
-         searchar = '?';
-         break;
-       case 'u':
-         update = TRUE;
-         break;
-       case 'v':
-         vgrind_style = TRUE;
-         /*FALLTHRU*/
-       case 'x':
-         cxref_style = TRUE;
-         break;
-       case 'w':
-         no_warnings = TRUE;
-         break;
-#endif /* CTAGS */
+       case 'B': searchar = '?';                               break;
+       case 'd': constantypedefs = TRUE;                       break;
+       case 't': typedefs = TRUE;                              break;
+       case 'T': typedefs = typedefs_or_cplusplus = TRUE;      break;
+       case 'u': update = TRUE;                                break;
+       case 'v': vgrind_style = TRUE;                    /*FALLTHRU*/
+       case 'x': cxref_style = TRUE;                           break;
+       case 'w': no_warnings = TRUE;                           break;
        default:
          suggest_asking_for_help ();
        }
@@ -854,7 +1117,7 @@ main (argc, argv)
 
   if (nincluded_files == 0 && file_count == 0)
     {
-      fprintf (stderr, "%s: No input files specified.\n", progname);
+      error ("no input files specified.", (char *)NULL);
       suggest_asking_for_help ();
     }
 
@@ -862,7 +1125,11 @@ main (argc, argv)
     tagfile = CTAGS ? "tags" : "TAGS";
   cwd = etags_getcwd ();       /* the current working directory */
   if (cwd[strlen (cwd) - 1] != '/')
-    cwd = concat (cwd, "/", "");
+    {
+      char *oldcwd = cwd;
+      cwd = concat (oldcwd, "/", "");
+      free (oldcwd);
+    }
   if (streq (tagfile, "-"))
     tagfiledir = cwd;
   else
@@ -871,9 +1138,6 @@ main (argc, argv)
   init ();                     /* set up boolean "functions" */
 
   initbuffer (&lb);
-  initbuffer (&token_name);
-  initbuffer (&lbs[0].lb);
-  initbuffer (&lbs[1].lb);
   initbuffer (&filename_lb);
 
   if (!CTAGS)
@@ -899,14 +1163,20 @@ main (argc, argv)
    */
   for (i = 0; i < current_arg; ++i)
     {
+      static language *lang;   /* non-NULL if language is forced */
+      char *this_file;
+
       switch (argbuffer[i].arg_type)
        {
        case at_language:
-         lang_func = argbuffer[i].function;
+         lang = argbuffer[i].lang;
          break;
 #ifdef ETAGS_REGEXPS
        case at_regexp:
-         add_regex (argbuffer[i].what);
+         analyse_regex (argbuffer[i].what, FALSE);
+         break;
+       case at_icregexp:
+         analyse_regex (argbuffer[i].what, TRUE);
          break;
 #endif
        case at_filename:
@@ -915,7 +1185,7 @@ main (argc, argv)
            {
              if (got_err)
                {
-                 error ("Can't find file %s\n", this_file);
+                 error ("can't find file %s\n", this_file);
                  argc--, argv++;
                }
              else
@@ -926,12 +1196,12 @@ main (argc, argv)
              this_file = argbuffer[i].what;
 #endif
              /* Input file named "-" means read file names from stdin
-                and use them. */
+                (one per line) and use them. */
              if (streq (this_file, "-"))
                while (readline_internal (&filename_lb, stdin) > 0)
-                 process_file (filename_lb.buffer);
+                 process_file (filename_lb.buffer, lang);
              else
-               process_file (this_file);
+               process_file (this_file, lang);
 #ifdef VMS
            }
 #endif
@@ -939,20 +1209,21 @@ main (argc, argv)
        }
     }
 
-  if (!CTAGS)
-    {
-      while (nincluded_files-- > 0)
-       fprintf (tagf, "\f\n%s,include\n", *included_files++);
-
-      fclose (tagf);
-      exit (GOOD);
-    }
+#ifdef ETAGS_REGEXPS
+  free_patterns ();
+#endif /* ETAGS_REGEXPS */
 
-  /* If CTAGS, we are here.  process_file did not write the tags yet,
-     because we want them ordered.  Let's do it now. */
-  if (cxref_style)
+  if (!CTAGS || cxref_style)
     {
-      put_entries (head);
+      put_entries (nodehead);
+      free_tree (nodehead);
+      nodehead = NULL;
+      if (!CTAGS)
+       while (nincluded_files-- > 0)
+         fprintf (tagf, "\f\n%s,include\n", *included_files++);
+
+      if (fclose (tagf) == EOF)
+       pfatal (tagfile);
       exit (GOOD);
     }
 
@@ -967,7 +1238,7 @@ main (argc, argv)
                   "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
                   tagfile, argbuffer[i].what, tagfile);
          if (system (cmd) != GOOD)
-           fatal ("failed to execute shell command");
+           fatal ("failed to execute shell command", (char *)NULL);
        }
       append_to_tagfile = TRUE;
     }
@@ -975,13 +1246,16 @@ main (argc, argv)
   tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
   if (tagf == NULL)
     pfatal (tagfile);
-  put_entries (head);
-  fclose (tagf);
+  put_entries (nodehead);
+  free_tree (nodehead);
+  nodehead = NULL;
+  if (fclose (tagf) == EOF)
+    pfatal (tagfile);
 
   if (update)
     {
-      char cmd[BUFSIZ];
-      sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
+      char cmd[2*BUFSIZ+10];
+      sprintf (cmd, "sort -o %.*s %.*s", BUFSIZ, tagfile, BUFSIZ, tagfile);
       exit (system (cmd));
     }
   return GOOD;
@@ -989,41 +1263,78 @@ main (argc, argv)
 
 
 /*
- * Return a Lang_function given the name.
+ * Return a compressor given the file name.  If EXTPTR is non-zero,
+ * return a pointer into FILE where the compressor-specific
+ * extension begins.  If no compressor is found, NULL is returned
+ * and EXTPTR is not significant.
+ * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
  */
-Lang_function *
-get_language_from_name (name)
-     char *name;
+static compressor *
+get_compressor_from_suffix (file, extptr)
+     char *file;
+     char **extptr;
 {
-  struct lang_entry *lang;
+  compressor *compr;
+  char *slash, *suffix;
+
+  /* This relies on FN to be after canonicalize_filename,
+     so we don't need to consider backslashes on DOS_NT.  */
+  slash = etags_strrchr (file, '/');
+  suffix = etags_strrchr (file, '.');
+  if (suffix == NULL || suffix < slash)
+    return NULL;
+  if (extptr != NULL)
+    *extptr = suffix;
+  suffix += 1;
+  /* Let those poor souls who live with DOS 8+3 file name limits get
+     some solace by treating foo.cgz as if it were foo.c.gz, etc.
+     Only the first do loop is run if not MSDOS */
+  do
+    {
+      for (compr = compressors; compr->suffix != NULL; compr++)
+       if (streq (compr->suffix, suffix))
+         return compr;
+      if (!MSDOS)
+       break;                  /* do it only once: not really a loop */
+      if (extptr != NULL)
+       *extptr = ++suffix;
+    } while (*suffix != '\0');
+  return NULL;
+}
+
 
-  if (name != NULL)
-    for (lang = lang_names; lang->name != NULL; lang++)
-      {
-       if (streq (name, lang->name))
-         return lang->function;
-      }
 
-  fprintf (stderr, "%s: language \"%s\" not recognized.\n",
-          progname, optarg);
-  suggest_asking_for_help ();
+/*
+ * Return a language given the name.
+ */
+static language *
+get_language_from_langname (name)
+     const char *name;
+{
+  language *lang;
+
+  if (name == NULL)
+    error ("empty language name", (char *)NULL);
+  else
+    {
+      for (lang = lang_names; lang->name != NULL; lang++)
+       if (streq (name, lang->name))
+         return lang;
+      error ("unknown language \"%s\"", name);
+    }
 
-  /* This point should never be reached.  The function should either
-     return a function pointer  or never return.  Note that a NULL
-     pointer cannot be considered as an error, as it means that the
-     language has not been explicitely imposed by the user ("auto"). */
-  return NULL;                 /* avoid warnings from compiler */
+  return NULL;
 }
 
 
 /*
- * Return a Lang_function given the interpreter name.
+ * Return a language given the interpreter name.
  */
-Lang_function *
+static language *
 get_language_from_interpreter (interpreter)
      char *interpreter;
 {
-  struct lang_entry *lang;
+  language *lang;
   char **iname;
 
   if (interpreter == NULL)
@@ -1032,7 +1343,7 @@ get_language_from_interpreter (interpreter)
     if (lang->interpreters != NULL)
       for (iname = lang->interpreters; *iname != NULL; iname++)
        if (streq (*iname, interpreter))
-           return lang->function;
+           return lang;
 
   return NULL;
 }
@@ -1040,160 +1351,290 @@ get_language_from_interpreter (interpreter)
 
 
 /*
- * Return a Lang_function given the file suffix.
+ * Return a language given the file name.
  */
-Lang_function *
-get_language_from_suffix (suffix)
-     char *suffix;
+static language *
+get_language_from_filename (file, case_sensitive)
+     char *file;
+     bool case_sensitive;
 {
-  struct lang_entry *lang;
-  char **ext;
+  language *lang;
+  char **name, **ext, *suffix;
 
+  /* Try whole file name first. */
+  for (lang = lang_names; lang->name != NULL; lang++)
+    if (lang->filenames != NULL)
+      for (name = lang->filenames; *name != NULL; name++)
+       if ((case_sensitive)
+           ? streq (*name, file)
+           : strcaseeq (*name, file))
+         return lang;
+
+  /* If not found, try suffix after last dot. */
+  suffix = etags_strrchr (file, '.');
   if (suffix == NULL)
     return NULL;
+  suffix += 1;
   for (lang = lang_names; lang->name != NULL; lang++)
     if (lang->suffixes != NULL)
       for (ext = lang->suffixes; *ext != NULL; ext++)
-       if (streq (*ext, suffix))
-           return lang->function;
-
+       if ((case_sensitive)
+           ? streq (*ext, suffix)
+           : strcaseeq (*ext, suffix))
+         return lang;
   return NULL;
 }
 
-
+\f
 /*
  * This routine is called on each file argument.
  */
-void
-process_file (file)
+static void
+process_file (file, lang)
      char *file;
+     language *lang;
 {
   struct stat stat_buf;
   FILE *inf;
-#ifdef DOS_NT
-  char *p;
+  static const fdesc emptyfdesc;
+  fdesc *fdp;
+  compressor *compr;
+  char *compressed_name, *uncompressed_name;
+  char *ext, *real_name;
+  int retval;
 
-  for (p = file; *p != '\0'; p++)
-    if (*p == '\\')
-      *p = '/';
-#endif
 
-  if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
+  canonicalize_filename (file);
+  if (streq (file, tagfile) && !streq (tagfile, "-"))
     {
-      fprintf (stderr, "Skipping %s: it is not a regular file.\n", file);
+      error ("skipping inclusion of %s in self.", file);
       return;
     }
-  if (streq (file, tagfile) && !streq (tagfile, "-"))
+  if ((compr = get_compressor_from_suffix (file, &ext)) == NULL)
     {
-      fprintf (stderr, "Skipping inclusion of %s in self.\n", file);
-      return;
+      compressed_name = NULL;
+      real_name = uncompressed_name = savestr (file);
     }
-  inf = fopen (file, "r");
-  if (inf == NULL)
+  else
     {
-      perror (file);
-      return;
+      real_name = compressed_name = savestr (file);
+      uncompressed_name = savenstr (file, ext - file);
     }
 
-  find_entries (file, inf);
-
-  if (!CTAGS)
+  /* If the canonicalized uncompressed name
+     has already been dealt with, skip it silently. */
+  for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
     {
-      char *filename;
+      assert (fdp->infname != NULL);
+      if (streq (uncompressed_name, fdp->infname))
+       goto cleanup;
+    }
 
-      if (absolutefn (file))
+  if (stat (real_name, &stat_buf) != 0)
+    {
+      /* Reset real_name and try with a different name. */
+      real_name = NULL;
+      if (compressed_name != NULL) /* try with the given suffix */
        {
-         /* file is an absolute filename.  Canonicalise it. */
-         filename = absolute_filename (file, cwd);
+         if (stat (uncompressed_name, &stat_buf) == 0)
+           real_name = uncompressed_name;
        }
-      else
+      else                     /* try all possible suffixes */
+       {
+         for (compr = compressors; compr->suffix != NULL; compr++)
+           {
+             compressed_name = concat (file, ".", compr->suffix);
+             if (stat (compressed_name, &stat_buf) != 0)
+               {
+                 if (MSDOS)
+                   {
+                     char *suf = compressed_name + strlen (file);
+                     size_t suflen = strlen (compr->suffix) + 1;
+                     for ( ; suf[1]; suf++, suflen--)
+                       {
+                         memmove (suf, suf + 1, suflen);
+                         if (stat (compressed_name, &stat_buf) == 0)
+                           {
+                             real_name = compressed_name;
+                             break;
+                           }
+                       }
+                     if (real_name != NULL)
+                       break;
+                   } /* MSDOS */
+                 free (compressed_name);
+                 compressed_name = NULL;
+               }
+             else
+               {
+                 real_name = compressed_name;
+                 break;
+               }
+           }
+       }
+      if (real_name == NULL)
        {
-         /* file is a filename relative to cwd.  Make it relative
-            to the directory of the tags file. */
-         filename = relative_filename (file, tagfiledir);
+         perror (file);
+         goto cleanup;
        }
-      fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
-      free (filename);
-      put_entries (head);
-      free_tree (head);
-      head = NULL;
+    } /* try with a different name */
+
+  if (!S_ISREG (stat_buf.st_mode))
+    {
+      error ("skipping %s: it is not a regular file.", real_name);
+      goto cleanup;
+    }
+  if (real_name == compressed_name)
+    {
+      char *cmd = concat (compr->command, " ", real_name);
+      inf = (FILE *) popen (cmd, "r");
+      free (cmd);
+    }
+  else
+    inf = fopen (real_name, "r");
+  if (inf == NULL)
+    {
+      perror (real_name);
+      goto cleanup;
     }
-}
 
-/*
- * 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"
- * of a char is TRUE if it is the string "white", else FALSE.
- */
-void
-init ()
-{
+  /* Create a new input file description entry. */
+  fdp = xnew (1, fdesc);
+  *fdp = emptyfdesc;
+  fdp->next = fdhead;
+  fdp->infname = savestr (uncompressed_name);
+  fdp->lang = lang;
+  fdp->infabsname = absolute_filename (uncompressed_name, cwd);
+  fdp->infabsdir = absolute_dirname (uncompressed_name, cwd);
+  if (filename_is_absolute (uncompressed_name))
+    {
+      /* file is an absolute file name.  Canonicalize it. */
+      fdp->taggedfname = absolute_filename (uncompressed_name, NULL);
+    }
+  else
+    {
+      /* file is a file name relative to cwd.  Make it relative
+        to the directory of the tags file. */
+      fdp->taggedfname = relative_filename (uncompressed_name, tagfiledir);
+    }
+  fdp->usecharno = TRUE;       /* use char position when making tags */
+  fdp->prop = NULL;
+
+  fdhead = fdp;
+  curfdp = fdhead;             /* the current file description */
+
+  find_entries (inf);
+
+  if (real_name == compressed_name)
+    retval = pclose (inf);
+  else
+    retval = fclose (inf);
+  if (retval < 0)
+    pfatal (file);
+
+  /* If not Ctags, and if this is not metasource and if it contained no #line
+     directives, we can write the tags and free all nodes pointing to
+     curfdp. */
+  if (!CTAGS
+      && curfdp->usecharno     /* no #line directives in this file */
+      && !curfdp->lang->metasource)
+    {
+      node *np, *prev;
+
+      /* Look for the head of the sublist relative to this file.  See add_node
+        for the structure of the node tree. */
+      prev = NULL;
+      for (np = nodehead; np != NULL; prev = np, np = np->left)
+       if (np->fdp == curfdp)
+         break;
+
+      /* If we generated tags for this file, write and delete them. */
+      if (np != NULL)
+       {
+         /* This is the head of the last sublist, if any.  The following
+            instructions depend on this being true. */
+         assert (np->left == NULL);
+
+         assert (fdhead == curfdp);
+         assert (last_node->fdp == curfdp);
+         put_entries (np);     /* write tags for file curfdp->taggedfname */
+         free_tree (np);       /* remove the written nodes */
+         if (prev == NULL)
+           nodehead = NULL;    /* no nodes left */
+         else
+           prev->left = NULL;  /* delete the pointer to the sublist */
+       }
+    }
+
+ cleanup:
+  if (compressed_name) free (compressed_name);
+  if (uncompressed_name) free (uncompressed_name);
+  last_node = NULL;
+  curfdp = NULL;
+  return;
+}
+
+/*
+ * 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"
+ * of a char is TRUE if it is the string "white", else FALSE.
+ */
+static void
+init ()
+{
   register char *sp;
   register int i;
 
-  for (i = 0; i < 0177; i++)
-    _wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
-  for (sp = white; *sp; sp++)
-    _wht[*sp] = TRUE;
-  for (sp = endtk; *sp; sp++)
-    _etk[*sp] = TRUE;
-  for (sp = intk; *sp; sp++)
-    _itk[*sp] = TRUE;
-  for (sp = begtk; *sp; sp++)
-    _btk[*sp] = TRUE;
-  _wht[0] = _wht['\n'];
-  _etk[0] = _etk['\n'];
-  _btk[0] = _btk['\n'];
-  _itk[0] = _itk['\n'];
+  for (i = 0; i < CHARS; i++)
+    iswhite(i) = notinname(i) = begtoken(i) = intoken(i) = endtoken(i) = FALSE;
+  for (sp = white; *sp != '\0'; sp++) iswhite (*sp) = TRUE;
+  for (sp = nonam; *sp != '\0'; sp++) notinname (*sp) = TRUE;
+  notinname('\0') = notinname('\n');
+  for (sp = begtk; *sp != '\0'; sp++) begtoken (*sp) = TRUE;
+  begtoken('\0') = begtoken('\n');
+  for (sp = midtk; *sp != '\0'; sp++) intoken (*sp) = TRUE;
+  intoken('\0') = intoken('\n');
+  for (sp = endtk; *sp != '\0'; sp++) endtoken (*sp) = TRUE;
+  endtoken('\0') = endtoken('\n');
 }
 
 /*
  * This routine opens the specified file and calls the function
  * which finds the function and type definitions.
  */
-void
-find_entries (file, inf)
-     char *file;
+static void
+find_entries (inf)
      FILE *inf;
 {
   char *cp;
-  Lang_function *function;
-  NODE *old_last_node;
-  extern NODE *last_node;
-
-
-  /* Memory leakage here: the memory block pointed by curfile is never
-     released.  The amount of memory leaked here is the sum of the
-     lengths of the input file names. */
-  curfile = savestr (file);
+  node *old_last_node;
+  language *lang = curfdp->lang;
+  Lang_function *parser = NULL;
 
   /* If user specified a language, use it. */
-  function = lang_func;
-  if (function != NULL)
+  if (lang != NULL && lang->function != NULL)
     {
-      function (inf);
-      fclose (inf);
-      return;
+      parser = lang->function;
     }
 
-  cp = etags_strrchr (file, '.');
-  if (cp != NULL)
+  /* Else try to guess the language given the file name. */
+  if (parser == NULL)
     {
-      cp += 1;
-      function = get_language_from_suffix (cp);
-      if (function != NULL)
+      lang = get_language_from_filename (curfdp->infname, TRUE);
+      if (lang != NULL && lang->function != NULL)
        {
-         function (inf);
-         fclose (inf);
-         return;
+         curfdp->lang = lang;
+         parser = lang->function;
        }
     }
 
-  /* Look for sharp-bang as the first two characters. */
-  if (readline_internal (&lb, inf) > 2
+  /* Else look for sharp-bang as the first two characters. */
+  if (parser == NULL
+      && readline_internal (&lb, inf) > 0
+      && lb.len >= 2
       && lb.buffer[0] == '#'
       && lb.buffer[1] == '!')
     {
@@ -1206,77 +1647,136 @@ find_entries (file, inf)
       if (lp != NULL)
        lp += 1;
       else
-       for (lp = lb.buffer+2; *lp != '\0' && isspace (*lp); lp++)
-         continue;
-      for (cp = lp; *cp != '\0' && !isspace (*cp); cp++)
-       continue;
+       lp = skip_spaces (lb.buffer + 2);
+      cp = skip_non_spaces (lp);
       *cp = '\0';
 
       if (strlen (lp) > 0)
        {
-         function = get_language_from_interpreter (lp);
-         if (function != NULL)
+         lang = get_language_from_interpreter (lp);
+         if (lang != NULL && lang->function != NULL)
            {
-             function (inf);
-             fclose (inf);
-             return;
+             curfdp->lang = lang;
+             parser = lang->function;
            }
        }
     }
-  rewind (inf);
 
-  /* Try Fortran. */
+  /* We rewind here, even if inf may be a pipe.  We fail if the
+     length of the first line is longer than the pipe block size,
+     which is unlikely. */
+  if (parser == NULL)
+    rewind (inf);
+
+  /* Else try to guess the language given the case insensitive file name. */
+  if (parser == NULL)
+    {
+      lang = get_language_from_filename (curfdp->infname, FALSE);
+      if (lang != NULL && lang->function != NULL)
+       {
+         curfdp->lang = lang;
+         parser = lang->function;
+       }
+    }
+
+  if (!no_line_directive
+      && curfdp->lang != NULL && curfdp->lang->metasource)
+    /* It may be that this is a bingo.y file, and we already parsed a bingo.c
+       file, or anyway we parsed a file that is automatically generated from
+       this one.  If this is the case, the bingo.c file contained #line
+       directives that generated tags pointing to this file.  Let's delete
+       them all before parsing this file, which is the real source. */
+    {
+      fdesc **fdpp = &fdhead;
+      while (*fdpp != NULL)
+       if (*fdpp != curfdp
+           && streq ((*fdpp)->taggedfname, curfdp->taggedfname))
+         /* We found one of those!  We must delete both the file description
+            and all tags referring to it. */
+         {
+           fdesc *badfdp = *fdpp;
+
+           if (DEBUG)
+             fprintf (stderr,
+                      "Removing references to \"%s\" obtained from \"%s\"\n",
+                      badfdp->taggedfname, badfdp->infname);
+
+           /* Delete the tags referring to badfdp. */
+           invalidate_nodes (badfdp, &nodehead);
+
+           *fdpp = badfdp->next; /* remove the bad description from the list */
+           free_fdesc (badfdp);
+         }
+       else
+         fdpp = &(*fdpp)->next; /* advance the list pointer */
+    }
+
+  if (parser != NULL)
+    {
+      parser (inf);
+      return;
+    }
+
+  /* Else try Fortran. */
   old_last_node = last_node;
+  curfdp->lang = get_language_from_langname ("fortran");
   Fortran_functions (inf);
 
-  /* No Fortran entries found.  Try C. */
   if (old_last_node == last_node)
+    /* No Fortran entries found.  Try C. */
     {
+      /* We do not tag if rewind fails.
+        Only the file name will be recorded in the tags file. */
       rewind (inf);
+      curfdp->lang = get_language_from_langname (cplusplus ? "c++" : "c");
       default_C_entries (inf);
     }
-  fclose (inf);
   return;
 }
+
 \f
 /* Record a tag. */
-void
+static void
 pfnote (name, is_func, linestart, linelen, lno, cno)
      char *name;               /* tag name, or NULL if unnamed */
-     logical is_func;          /* tag is a function */
+     bool is_func;             /* tag is a function */
      char *linestart;          /* start of the line where tag is */
      int linelen;              /* length of the line where tag is */
      int lno;                  /* line number */
      long cno;                 /* character number */
 {
-  register NODE *np;
+  register node *np;
 
   if (CTAGS && name == NULL)
     return;
 
-  np = xnew (1, NODE);
+  np = xnew (1, node);
 
   /* If ctags mode, change name "main" to M<thisfilename>. */
   if (CTAGS && !cxref_style && streq (name, "main"))
     {
-      register char *fp = etags_strrchr (curfile, '/');
-      np->name = concat ("M", fp == 0 ? curfile : fp + 1, "");
+      register char *fp = etags_strrchr (curfdp->taggedfname, '/');
+      np->name = concat ("M", fp == NULL ? curfdp->taggedfname : fp + 1, "");
       fp = etags_strrchr (np->name, '.');
-      if (fp && fp[1] != '\0' && fp[2] == '\0')
-       fp[0] = 0;
+      if (fp != NULL && fp[1] != '\0' && fp[2] == '\0')
+       fp[0] = '\0';
     }
   else
     np->name = name;
+  np->valid = TRUE;
   np->been_warned = FALSE;
-  np->file = curfile;
+  np->fdp = curfdp;
   np->is_func = is_func;
   np->lno = lno;
-  /* 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 expect 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 */ ;
+  if (np->fdp->usecharno)
+    /* 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 expect 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 */ ;
+  else
+    np->cno = invalidcharno;
   np->left = np->right = NULL;
   if (CTAGS && !cxref_style)
     {
@@ -1288,65 +1788,154 @@ pfnote (name, is_func, linestart, linelen, lno, cno)
   else
     np->pat = savenstr (linestart, linelen);
 
-  add_node (np, &head);
+  add_node (np, &nodehead);
+}
+
+/*
+ * TAGS format specification
+ * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
+ *
+ * pfnote should emit the optimized form [unnamed tag] only if:
+ *  1. name does not contain any of the characters " \t\r\n(),;";
+ *  2. linestart contains name as either a rightmost, or rightmost but
+ *     one character, substring;
+ *  3. the character, if any, immediately before name in linestart must
+ *     be one of the characters " \t(),;";
+ *  4. the character, if any, immediately after name in linestart must
+ *     also be one of the characters " \t(),;".
+ *
+ * The real implementation uses the notinname() macro, which recognises
+ * characters slightly different from " \t\r\n(),;".  See the variable
+ * `nonam'.
+ */
+#define traditional_tag_style TRUE
+static void
+new_pfnote (name, namelen, is_func, linestart, linelen, lno, cno)
+     char *name;               /* tag name, or NULL if unnamed */
+     int namelen;              /* tag length */
+     bool is_func;             /* tag is a function */
+     char *linestart;          /* start of the line where tag is */
+     int linelen;              /* length of the line where tag is */
+     int lno;                  /* line number */
+     long cno;                 /* character number */
+{
+  register char *cp;
+  bool named;
+
+  named = TRUE;
+  if (!CTAGS)
+    {
+      for (cp = name; !notinname (*cp); cp++)
+       continue;
+      if (*cp == '\0')                         /* rule #1 */
+       {
+         cp = linestart + linelen - namelen;
+         if (notinname (linestart[linelen-1]))
+           cp -= 1;                            /* rule #4 */
+         if (cp >= linestart                   /* rule #2 */
+             && (cp == linestart
+                 || notinname (cp[-1]))        /* rule #3 */
+             && strneq (name, cp, namelen))    /* rule #2 */
+           named = FALSE;      /* use unnamed tag */
+       }
+    }
+
+  if (named)
+    name = savenstr (name, namelen);
+  else
+    name = NULL;
+  pfnote (name, is_func, linestart, linelen, lno, cno);
 }
 
 /*
  * free_tree ()
  *     recurse on left children, iterate on right children.
  */
-void
-free_tree (node)
-     register NODE *node;
+static void
+free_tree (np)
+     register node *np;
 {
-  while (node)
+  while (np)
     {
-      register NODE *node_right = node->right;
-      free_tree (node->left);
-      if (node->name != NULL)
-       free (node->name);
-      free (node->pat);
-      free ((char *) node);
-      node = node_right;
+      register node *node_right = np->right;
+      free_tree (np->left);
+      if (np->name != NULL)
+       free (np->name);
+      free (np->pat);
+      free (np);
+      np = node_right;
     }
 }
 
+/*
+ * free_fdesc ()
+ *     delete a file description
+ */
+static void
+free_fdesc (fdp)
+     register fdesc *fdp;
+{
+  if (fdp->infname != NULL) free (fdp->infname);
+  if (fdp->infabsname != NULL) free (fdp->infabsname);
+  if (fdp->infabsdir != NULL) free (fdp->infabsdir);
+  if (fdp->taggedfname != NULL) free (fdp->taggedfname);
+  if (fdp->prop != NULL) free (fdp->prop);
+  free (fdp);
+}
+
 /*
  * add_node ()
- *     Adds a node to the tree of nodes.  In etags mode, we don't keep
- *     it sorted; we just keep a linear list.  In ctags mode, maintain
- *     an ordered tree, with no attempt at balancing.
+ *     Adds a node to the tree of nodes.  In etags mode, sort by file
+ *     name.  In ctags mode, sort by tag name.  Make no attempt at
+ *     balancing.
  *
  *     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;
+static void
+add_node (np, cur_node_p)
+     node *np, **cur_node_p;
 {
   register int dif;
-  register NODE *cur_node = *cur_node_p;
+  register node *cur_node = *cur_node_p;
 
   if (cur_node == NULL)
     {
-      *cur_node_p = node;
-      last_node = node;
+      *cur_node_p = np;
+      last_node = np;
       return;
     }
 
   if (!CTAGS)
+    /* Etags Mode */
     {
-      /* Etags Mode */
-      if (last_node == NULL)
-       fatal ("internal error in add_node", 0);
-      last_node->right = node;
-      last_node = node;
-    }
+      /* For each file name, tags are in a linked sublist on the right
+        pointer.  The first tags of different files are a linked list
+        on the left pointer.  last_node points to the end of the last
+        used sublist. */
+      if (last_node != NULL && last_node->fdp == np->fdp)
+       {
+         /* Let's use the same sublist as the last added node. */
+         assert (last_node->right == NULL);
+         last_node->right = np;
+         last_node = np;
+       }
+      else if (cur_node->fdp == np->fdp)
+       {
+         /* Scanning the list we found the head of a sublist which is
+            good for us.  Let's scan this sublist. */
+         add_node (np, &cur_node->right);
+       }
+      else
+       /* The head of this sublist is not good for us.  Let's try the
+          next one. */
+       add_node (np, &cur_node->left);
+    } /* if ETAGS mode */
+
   else
     {
       /* Ctags Mode */
-      dif = strcmp (node->name, cur_node->name);
+      dif = strcmp (np->name, cur_node->name);
 
       /*
        * If this tag name matches an existing one, then
@@ -1354,12 +1943,12 @@ add_node (node, cur_node_p)
        */
       if (!dif)
        {
-         if (streq (node->file, cur_node->file))
+         if (np->fdp == cur_node->fdp)
            {
              if (!no_warnings)
                {
                  fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
-                          node->file, lineno, node->name);
+                          np->fdp->infname, lineno, np->name);
                  fprintf (stderr, "Second entry ignored\n");
                }
            }
@@ -1368,154 +1957,240 @@ add_node (node, cur_node_p)
              fprintf
                (stderr,
                 "Duplicate entry in files %s and %s: %s (Warning only)\n",
-                node->file, cur_node->file, node->name);
+                np->fdp->infname, cur_node->fdp->infname, np->name);
              cur_node->been_warned = TRUE;
            }
          return;
        }
 
       /* Actually add the node */
-      add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
-    }
+      add_node (np, dif < 0 ? &cur_node->left : &cur_node->right);
+    } /* if CTAGS mode */
 }
-\f
-void
-put_entries (node)
-     register NODE *node;
+
+/*
+ * invalidate_nodes ()
+ *     Scan the node tree and invalidate all nodes pointing to the
+ *     given file description (CTAGS case) or free them (ETAGS case).
+ */
+static void
+invalidate_nodes (badfdp, npp)
+     fdesc *badfdp;
+     node **npp;
 {
-  register char *sp;
+  node *np = *npp;
 
-  if (node == NULL)
+  if (np == NULL)
     return;
 
-  /* Output subentries that precede this one */
-  put_entries (node->left);
-
-  /* Output this entry */
-
-  if (!CTAGS)
+  if (CTAGS)
     {
-      if (node->name != NULL)
-       fprintf (tagf, "%s\177%s\001%d,%d\n",
-                node->pat, node->name, node->lno, node->cno);
-      else
-       fprintf (tagf, "%s\177%d,%d\n",
-                node->pat, node->lno, node->cno);
+      if (np->left != NULL)
+       invalidate_nodes (badfdp, &np->left);
+      if (np->fdp == badfdp)
+       np-> valid = FALSE;
+      if (np->right != NULL)
+       invalidate_nodes (badfdp, &np->right);
     }
   else
     {
-      if (node->name == NULL)
-       error ("internal error: NULL name in ctags mode.", 0);
-
-      if (cxref_style)
+      node **next = &np->left;
+      if (np->fdp == badfdp)
        {
-         if (vgrind_style)
-           fprintf (stdout, "%s %s %d\n",
-                    node->name, node->file, (node->lno + 63) / 64);
-         else
-           fprintf (stdout, "%-16s %3d %-16s %s\n",
-                    node->name, node->lno, node->file, node->pat);
-       }
-      else
-       {
-         fprintf (tagf, "%s\t%s\t", node->name, node->file);
-
-         if (node->is_func)
-           {                   /* a function */
-             putc (searchar, tagf);
-             putc ('^', tagf);
-
-             for (sp = node->pat; *sp; sp++)
-               {
-                 if (*sp == '\\' || *sp == searchar)
-                   putc ('\\', tagf);
-                 putc (*sp, tagf);
-               }
-             putc (searchar, tagf);
-           }
-         else
-           {                   /* a typedef; text pattern inadequate */
-             fprintf (tagf, "%d", node->lno);
-           }
-         putc ('\n', tagf);
+         *npp = *next;         /* detach the sublist from the list */
+         np->left = NULL;      /* isolate it */
+         free_tree (np);       /* free it */
        }
+      invalidate_nodes (badfdp, next);
     }
-
-  /* Output subentries that follow this one */
-  put_entries (node->right);
 }
 
-/* Length of a number's decimal representation. */
-int
+\f
+static int total_size_of_entries __P((node *));
+static int number_len __P((long));
+
+/* Length of a non-negative number's decimal representation. */
+static int
 number_len (num)
      long num;
 {
-  int len = 0;
-  if (!num)
-    return 1;
-  for (; num; num /= 10)
-    ++len;
+  int len = 1;
+  while ((num /= 10) > 0)
+    len += 1;
   return len;
 }
 
 /*
  * Return total number of characters that put_entries will output for
- * the nodes in the subtree of the specified node.  Works only if
- * 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.
+ * the nodes in the linked list at the right of the specified node.
+ * This count is irrelevant with etags.el since emacs 19.34 at least,
+ * but is still supplied for backward compatibility.
  */
-int
-total_size_of_entries (node)
-     register NODE *node;
+static int
+total_size_of_entries (np)
+     register node *np;
 {
-  register int total;
-
-  if (node == NULL)
-    return 0;
+  register int total = 0;
 
-  total = 0;
-  for (; node; node = node->right)
+  for (; np != NULL; np = np->right)
     {
-      /* Count left subentries. */
-      total += total_size_of_entries (node->left);
-
-      /* Count this entry */
-      total += strlen (node->pat) + 1;
-      total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
-      if (node->name != NULL)
-       total += 1 + strlen (node->name);       /* \001name */
+      total += strlen (np->pat) + 1;           /* pat\177 */
+      if (np->name != NULL)
+       total += strlen (np->name) + 1;         /* name\001 */
+      total += number_len ((long) np->lno) + 1;        /* lno, */
+      if (np->cno != invalidcharno)            /* cno */
+       total += number_len (np->cno);
+      total += 1;                              /* newline */
     }
 
   return total;
 }
+
+static void
+put_entries (np)
+     register node *np;
+{
+  register char *sp;
+  static fdesc *fdp = NULL;
+
+  if (np == NULL)
+    return;
+
+  /* Output subentries that precede this one */
+  if (CTAGS)
+    put_entries (np->left);
+
+  /* Output this entry */
+  if (np->valid)
+    {
+      if (!CTAGS)
+       {
+         /* Etags mode */
+         if (fdp != np->fdp)
+           {
+             fdp = np->fdp;
+             fprintf (tagf, "\f\n%s,%d\n",
+                      fdp->taggedfname, total_size_of_entries (np));
+           }
+         fputs (np->pat, tagf);
+         fputc ('\177', tagf);
+         if (np->name != NULL)
+           {
+             fputs (np->name, tagf);
+             fputc ('\001', tagf);
+           }
+         fprintf (tagf, "%d,", np->lno);
+         if (np->cno != invalidcharno)
+           fprintf (tagf, "%ld", np->cno);
+         fputs ("\n", tagf);
+       }
+      else
+       {
+         /* Ctags mode */
+         if (np->name == NULL)
+           error ("internal error: NULL name in ctags mode.", (char *)NULL);
+
+         if (cxref_style)
+           {
+             if (vgrind_style)
+               fprintf (stdout, "%s %s %d\n",
+                        np->name, np->fdp->taggedfname, (np->lno + 63) / 64);
+             else
+               fprintf (stdout, "%-16s %3d %-16s %s\n",
+                        np->name, np->lno, np->fdp->taggedfname, np->pat);
+           }
+         else
+           {
+             fprintf (tagf, "%s\t%s\t", np->name, np->fdp->taggedfname);
+
+             if (np->is_func)
+               {               /* function or #define macro with args */
+                 putc (searchar, tagf);
+                 putc ('^', tagf);
+
+                 for (sp = np->pat; *sp; sp++)
+                   {
+                     if (*sp == '\\' || *sp == searchar)
+                       putc ('\\', tagf);
+                     putc (*sp, tagf);
+                   }
+                 putc (searchar, tagf);
+               }
+             else
+               {               /* anything else; text pattern inadequate */
+                 fprintf (tagf, "%d", np->lno);
+               }
+             putc ('\n', tagf);
+           }
+       }
+    } /* if this node contains a valid tag */
+
+  /* Output subentries that follow this one */
+  put_entries (np->right);
+  if (!CTAGS)
+    put_entries (np->left);
+}
+
 \f
+/* C extensions. */
+#define C_EXT  0x00fff         /* C extensions */
+#define C_PLAIN 0x00000                /* C */
+#define C_PLPL 0x00001         /* C++ */
+#define C_STAR 0x00003         /* C* */
+#define C_JAVA 0x00005         /* JAVA */
+#define C_AUTO  0x01000                /* C, but switch to C++ if `class' is met */
+#define YACC   0x10000         /* yacc file */
+
 /*
  * The C symbol tables.
  */
 enum sym_type
 {
-  st_none, st_C_objprot, st_C_objimpl, st_C_objend, st_C_gnumacro,
-  st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
+  st_none,
+  st_C_objprot, st_C_objimpl, st_C_objend,
+  st_C_gnumacro,
+  st_C_ignore,
+  st_C_javastruct,
+  st_C_operator,
+  st_C_class, st_C_template,
+  st_C_struct, st_C_extern, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
 };
 
+static unsigned int hash __P((const char *, unsigned int));
+static struct C_stab_entry * in_word_set __P((const char *, unsigned int));
+static enum sym_type C_symtype __P((char *, int, int));
+
 /* Feed stuff between (but not including) %[ and %] lines to:
       gperf -c -k 1,3 -o -p -r -t
 %[
 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
 %%
+if,            0,      st_C_ignore
+for,           0,      st_C_ignore
+while,         0,      st_C_ignore
+switch,                0,      st_C_ignore
+return,                0,      st_C_ignore
 @interface,    0,      st_C_objprot
 @protocol,     0,      st_C_objprot
 @implementation,0,     st_C_objimpl
 @end,          0,      st_C_objend
-class,         C_PLPL, st_C_struct
+import,                C_JAVA, st_C_ignore
+package,       C_JAVA, st_C_ignore
+friend,                C_PLPL, st_C_ignore
+extends,       C_JAVA, st_C_javastruct
+implements,    C_JAVA, st_C_javastruct
+interface,     C_JAVA, st_C_struct
+class,         0,      st_C_class
 namespace,     C_PLPL, st_C_struct
 domain,        C_STAR, st_C_struct
 union,         0,      st_C_struct
 struct,        0,      st_C_struct
+extern,        0,      st_C_extern
 enum,          0,      st_C_enum
 typedef,       0,      st_C_typedef
 define,        0,      st_C_define
+operator,      C_PLPL, st_C_operator
+template,      0,      st_C_template
 bool,          C_PLPL, st_C_typespec
 long,          0,      st_C_typespec
 short,         0,      st_C_typespec
@@ -1527,7 +2202,6 @@ 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
@@ -1544,120 +2218,167 @@ PSEUDO,               0,      st_C_gnumacro
 #EXFUN,                0,      st_C_gnumacro
 #DEFVAR_,      0,      st_C_gnumacro
 %]
-and replace lines between %< and %> with its output. */
+and replace lines between %< and %> with its output,
+then make in_word_set and C_stab_entry static. */
 /*%<*/
-/* C code produced by gperf version 2.1 (K&R C version) */
+/* C code produced by gperf version 2.7.1 (19981006 egcs) */
 /* Command-line: gperf -c -k 1,3 -o -p -r -t  */
-
-
 struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
 
-#define MIN_WORD_LENGTH 3
+#define TOTAL_KEYWORDS 47
+#define MIN_WORD_LENGTH 2
 #define MAX_WORD_LENGTH 15
-#define MIN_HASH_VALUE 34
-#define MAX_HASH_VALUE 121
-/*
-   34 keywords
-   88 is the maximum key range
-*/
+#define MIN_HASH_VALUE 18
+#define MAX_HASH_VALUE 138
+/* maximum key range = 121, duplicates = 0 */
 
-static int
+#ifdef __GNUC__
+__inline
+#endif
+static unsigned int
 hash (str, len)
-     register char *str;
-     register unsigned int  len;
+     register const char *str;
+     register unsigned int len;
 {
-  static unsigned char hash_table[] =
+  static unsigned char asso_values[] =
     {
-     121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
-     121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
-     121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
-     121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
-     121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
-     121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
-     121, 121, 121, 121,  45, 121, 121, 121,  16,  19,
-      61, 121, 121, 121, 121, 121, 121, 121, 121, 121,
-      10, 121, 121,  20,  53, 121, 121, 121, 121, 121,
-     121, 121, 121, 121, 121, 121, 121,  41,  45,  22,
-      60,  47,  37,  28, 121,  55, 121, 121,  20,  14,
-      29,  30,   5, 121,  50,  59,  30,  54,   6, 121,
-     121, 121, 121, 121, 121, 121, 121, 121,
-  };
-  return len + hash_table[str[2]] + hash_table[str[0]];
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139,  63, 139, 139, 139,  33,  44,
+       62, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+       42, 139, 139,  12,  32, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139,  34,  59,  37,
+       24,  58,  33,   3, 139,  16, 139, 139,  42,  60,
+       18,  11,  39, 139,  23,  57,   4,  63,   6,  20,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
+      139, 139, 139, 139, 139, 139
+    };
+  register int hval = len;
+
+  switch (hval)
+    {
+      default:
+      case 3:
+        hval += asso_values[(unsigned char)str[2]];
+      case 2:
+      case 1:
+        hval += asso_values[(unsigned char)str[0]];
+        break;
+    }
+  return hval;
 }
 
-struct C_stab_entry *
+#ifdef __GNUC__
+__inline
+#endif
+static struct C_stab_entry *
 in_word_set (str, len)
-     register char *str;
+     register const char *str;
      register unsigned int len;
 {
-
-  static struct C_stab_entry  wordlist[] =
+  static struct C_stab_entry wordlist[] =
     {
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"volatile",     0,      st_C_typespec},
-      {"PSEUDO",               0,      st_C_gnumacro},
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"typedef",      0,      st_C_typedef},
-      {"typename",     C_PLPL, st_C_typespec},
-      {"",}, {"",}, {"",}, 
-      {"SYSCALL",      0,      st_C_gnumacro},
-      {"",}, {"",}, {"",}, 
-      {"mutable",      C_PLPL, st_C_typespec},
-      {"namespace",    C_PLPL, st_C_struct},
-      {"long",         0,      st_C_typespec},
-      {"",}, {"",}, 
-      {"const",        0,      st_C_typespec},
-      {"",}, {"",}, {"",}, 
-      {"explicit",     C_PLPL, st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, 
-      {"void",         0,      st_C_typespec},
-      {"",}, 
-      {"char",         0,      st_C_typespec},
-      {"class",        C_PLPL, st_C_struct},
-      {"",}, {"",}, {"",}, 
-      {"float",        0,      st_C_typespec},
-      {"",}, 
-      {"@implementation", 0,   st_C_objimpl},
-      {"auto",         0,      st_C_typespec},
-      {"",}, 
-      {"ENTRY",                0,      st_C_gnumacro},
-      {"@end",                 0,      st_C_objend},
-      {"bool",                 C_PLPL, st_C_typespec},
-      {"domain",       C_STAR, st_C_struct},
-      {"",}, 
-      {"DEFUN",                0,      st_C_gnumacro},
-      {"extern",       0,      st_C_typespec},
-      {"@interface",   0,      st_C_objprot},
-      {"",}, {"",}, {"",}, 
-      {"int",          0,      st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, 
-      {"signed",       0,      st_C_typespec},
-      {"short",        0,      st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"define",       0,      st_C_define},
-      {"@protocol",    0,      st_C_objprot},
-      {"enum",         0,      st_C_enum},
-      {"static",       0,      st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"union",        0,      st_C_struct},
-      {"struct",       0,      st_C_struct},
-      {"",}, {"",}, {"",}, {"",}, 
-      {"double",       0,      st_C_typespec},
-      {"unsigned",     0,      st_C_typespec},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {"if",           0,      st_C_ignore},
+      {""}, {""}, {""}, {""},
+      {"int",          0,      st_C_typespec},
+      {""}, {""},
+      {"void",         0,      st_C_typespec},
+      {""}, {""},
+      {"interface",    C_JAVA, st_C_struct},
+      {""},
+      {"SYSCALL",      0,      st_C_gnumacro},
+      {""},
+      {"return",               0,      st_C_ignore},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {"while",                0,      st_C_ignore},
+      {"auto",         0,      st_C_typespec},
+      {""}, {""}, {""}, {""}, {""}, {""},
+      {"float",        0,      st_C_typespec},
+      {"typedef",      0,      st_C_typedef},
+      {"typename",     C_PLPL, st_C_typespec},
+      {""}, {""}, {""},
+      {"friend",               C_PLPL, st_C_ignore},
+      {"volatile",     0,      st_C_typespec},
+      {""}, {""},
+      {"for",          0,      st_C_ignore},
+      {"const",        0,      st_C_typespec},
+      {"import",               C_JAVA, st_C_ignore},
+      {""},
+      {"define",       0,      st_C_define},
+      {"long",         0,      st_C_typespec},
+      {"implements",   C_JAVA, st_C_javastruct},
+      {"signed",       0,      st_C_typespec},
+      {""},
+      {"extern",       0,      st_C_extern},
+      {"extends",      C_JAVA, st_C_javastruct},
+      {""},
+      {"mutable",      C_PLPL, st_C_typespec},
+      {"template",     0,      st_C_template},
+      {"short",        0,      st_C_typespec},
+      {"bool",         C_PLPL, st_C_typespec},
+      {"char",         0,      st_C_typespec},
+      {"class",        0,      st_C_class},
+      {"operator",     C_PLPL, st_C_operator},
+      {""},
+      {"switch",               0,      st_C_ignore},
+      {""},
+      {"ENTRY",                0,      st_C_gnumacro},
+      {""},
+      {"package",      C_JAVA, st_C_ignore},
+      {"union",        0,      st_C_struct},
+      {"@end",         0,      st_C_objend},
+      {"struct",       0,      st_C_struct},
+      {"namespace",    C_PLPL, st_C_struct},
+      {""}, {""},
+      {"domain",       C_STAR, st_C_struct},
+      {"@interface",   0,      st_C_objprot},
+      {"PSEUDO",               0,      st_C_gnumacro},
+      {"double",       0,      st_C_typespec},
+      {""},
+      {"@protocol",    0,      st_C_objprot},
+      {""},
+      {"static",       0,      st_C_typespec},
+      {""}, {""},
+      {"DEFUN",                0,      st_C_gnumacro},
+      {""}, {""}, {""}, {""},
+      {"explicit",     C_PLPL, st_C_typespec},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {""},
+      {"enum",         0,      st_C_enum},
+      {""}, {""},
+      {"unsigned",     0,      st_C_typespec},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {"@implementation",0,    st_C_objimpl}
     };
 
   if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
     {
       register int key = hash (str, len);
 
-      if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
+      if (key <= MAX_HASH_VALUE && key >= 0)
         {
-          register char *s = wordlist[key].name;
+          register const char *s = wordlist[key].name;
 
-          if (*s == *str && !strncmp (str + 1, s + 1, len - 1))
+          if (*str == *s && !strncmp (str + 1, s + 1, len - 1))
             return &wordlist[key];
         }
     }
@@ -1665,79 +2386,78 @@ in_word_set (str, len)
 }
 /*%>*/
 
-enum sym_type
-C_symtype(str, len, c_ext)
+static enum sym_type
+C_symtype (str, len, c_ext)
      char *str;
      int len;
      int c_ext;
 {
-  register struct C_stab_entry *se = in_word_set(str, len);
+  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.
 */
-enum
+/*
+ * C functions and variables are recognized using a simple
* finite automaton.  fvdef is its state variable.
+ */
+static 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 */
-} funcdef;
-
-
- /*
-  * typedefs are recognized using a simple finite automaton.
-  * typdef is its state variable.
-  */
-enum
+  fvnone,                      /* nothing seen */
+  fdefunkey,                   /* Emacs DEFUN keyword seen */
+  fdefunname,                  /* Emacs DEFUN name seen */
+  foperator,                   /* func: operator keyword seen (cplpl) */
+  fvnameseen,                  /* function or variable name seen */
+  fstartlist,                  /* func: just after open parenthesis */
+  finlist,                     /* func: in parameter list */
+  flistseen,                   /* func: after parameter list */
+  fignore,                     /* func: before open brace */
+  vignore                      /* var-like: ignore until ';' */
+} fvdef;
+
+static bool fvextern;          /* func or var: extern keyword seen; */
+
+/*
+ * typedefs are recognized using a simple finite automaton.
+ * typdef is its state variable.
+ */
+static enum
 {
   tnone,                       /* nothing seen */
-  ttypedseen,                  /* typedef keyword seen */
+  tkeyseen,                    /* typedef keyword seen */
+  ttypeseen,                   /* defined type seen */
   tinbody,                     /* inside typedef body */
   tend,                                /* just before typedef tag */
   tignore                      /* junk after typedef tag */
 } typdef;
 
-
- /*
-  * struct-like structures (enum, struct and union) are recognized
-  * using another simple finite automaton.  `structdef' is its state
-  * variable.
-  */
-enum
+/*
+ * struct-like structures (enum, struct and union) are recognized
+ * using another simple finite automaton.  `structdef' is its state
+ * variable.
+ */
+static enum
 {
-  snone,                       /* nothing seen yet */
+  snone,                       /* nothing seen yet,
+                                  or in struct body if cblev > 0 */
   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*/
+  sintemplate,                 /* inside template (ignore) */
+  scolonseen                   /* colon seen after struct-like tag */
 } structdef;
 
-/*
- * 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 = "<uninited>";
-enum sym_type structtype;
-
 /*
  * When objdef is different from onone, objtag is the name of the class.
  */
-char *objtag = "<uninited>";
+static char *objtag = "<uninited>";
 
 /*
  * Yet another little state machine to deal with preprocessor lines.
  */
-enum
+static enum
 {
   dnone,                       /* nothing seen */
   dsharpseen,                  /* '#' seen as first char on line */
@@ -1747,8 +2467,9 @@ enum
 
 /*
  * State machine for Objective C protocols and implementations.
+ * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
  */
-enum
+static enum
 {
   onone,                       /* nothing seen */
   oprotocol,                   /* @interface or @protocol seen */
@@ -1764,56 +2485,157 @@ enum
   oignore                      /* wait for @end */
 } objdef;
 
-/*
- * 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).
+ * Use this structure to keep info about the token read, and how it
+ * should be tagged.  Used by the make_C_tag function to build a tag.
  */
-logical yacc_rules;
+static struct tok
+{
+  bool valid;
+  bool named;
+  int offset;
+  int length;
+  int lineno;
+  long linepos;
+  char *line;
+} token;                       /* latest token read */
+static linebuffer token_name;  /* its name */
 
 /*
- * methodlen is the length of the method name stored in token_name.
+ * Variables and functions for dealing with nested structures.
+ * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
  */
-int methodlen;
+static void pushclass_above __P((int, char *, int));
+static void popclass_above __P((int));
+static void write_classname __P((linebuffer *, char *qualifier));
+
+static struct {
+  char **cname;                        /* nested class names */
+  int *cblev;                  /* nested class curly brace level */
+  int nl;                      /* class nesting level (elements used) */
+  int size;                    /* length of the array */
+} cstack;                      /* stack for nested declaration tags */
+/* Current struct nesting depth (namespace, class, struct, union, enum). */
+#define nestlev                (cstack.nl)
+/* After struct keyword or in struct body, not inside an nested function. */
+#define instruct       (structdef == snone && nestlev > 0                      \
+                        && cblev == cstack.cblev[nestlev-1] + 1)
+
+static void
+pushclass_above (cblev, str, len)
+     int cblev;
+     char *str;
+     int len;
+{
+  int nl;
+
+  popclass_above (cblev);
+  nl = cstack.nl;
+  if (nl >= cstack.size)
+    {
+      int size = cstack.size *= 2;
+      xrnew (cstack.cname, size, char *);
+      xrnew (cstack.cblev, size, int);
+    }
+  assert (nl == 0 || cstack.cblev[nl-1] < cblev);
+  cstack.cname[nl] = (str == NULL) ? NULL : savenstr (str, len);
+  cstack.cblev[nl] = cblev;
+  cstack.nl = nl + 1;
+}
+
+static void
+popclass_above (cblev)
+     int cblev;
+{
+  int nl;
+
+  for (nl = cstack.nl - 1;
+       nl >= 0 && cstack.cblev[nl] >= cblev;
+       nl--)
+    {
+      if (cstack.cname[nl] != NULL)
+       free (cstack.cname[nl]);
+      cstack.nl = nl;
+    }
+}
+
+static void
+write_classname (cn, qualifier)
+     linebuffer *cn;
+     char *qualifier;
+{
+  int i, len;
+  int qlen = strlen (qualifier);
+
+  if (cstack.nl == 0 || cstack.cname[0] == NULL)
+    {
+      len = 0;
+      cn->len = 0;
+      cn->buffer[0] = '\0';
+    }
+  else
+    {
+      len = strlen (cstack.cname[0]);
+      linebuffer_setlen (cn, len);
+      strcpy (cn->buffer, cstack.cname[0]);
+    }
+  for (i = 1; i < cstack.nl; i++)
+    {
+      char *s;
+      int slen;
+
+      s = cstack.cname[i];
+      if (s == NULL)
+       continue;
+      slen = strlen (s);
+      len += slen + qlen;
+      linebuffer_setlen (cn, len);
+      strncat (cn->buffer, qualifier, qlen);
+      strncat (cn->buffer, s, slen);
+    }
+}
+
+\f
+static bool consider_token __P((char *, int, int, int *, int, int, bool *));
+static void make_C_tag __P((bool));
 
 /*
  * 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.
+ *     function or variable, or corresponds to a typedef, or
+ *     is a struct/union/enum tag, or #define, or an enum constant.
  *
- *     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'.
+ *     *IS_FUNC gets TRUE iff the token is a function or #define macro
+ *     with args.  C_EXTP points to which language we are looking at.
  *
  * Globals
- *     funcdef                 IN OUT
+ *     fvdef                   IN OUT
  *     structdef               IN OUT
  *     definedef               IN OUT
  *     typdef                  IN OUT
  *     objdef                  IN OUT
- *     next_token_is_func      IN OUT
  */
 
-logical
-consider_token (str, len, c, c_ext, cblev, parlev, is_func)
+static bool
+consider_token (str, len, c, c_extp, cblev, parlev, is_func_or_var)
      register char *str;       /* IN: token pointer */
      register int len;         /* IN: token length */
-     register char c;          /* IN: first char after the token */
-     int c_ext;                        /* IN: C extensions mask */
+     register int c;           /* IN: first char after the token */
+     int *c_extp;              /* IN, OUT: C extensions mask */
      int cblev;                        /* IN: curly brace level */
      int parlev;               /* IN: parenthesis level */
-     logical *is_func;         /* OUT: function found */
+     bool *is_func_or_var;     /* OUT: function or variable found */
 {
-  enum sym_type toktype = C_symtype (str, len, c_ext);
+  /* When structdef is stagseen, scolonseen, or snone with cblev > 0,
+     structtype is the type of the preceding struct-like keyword, and
+     structcblev is the curly brace level where it has been seen. */
+  static enum sym_type structtype;
+  static int structcblev;
+  static enum sym_type toktype;
+
+
+  toktype = C_symtype (str, len, *c_extp);
 
   /*
    * Advance the definedef state machine.
@@ -1822,6 +2644,11 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
     {
     case dnone:
       /* We're not on a preprocessor line. */
+      if (toktype == st_C_gnumacro)
+       {
+         fvdef = fdefunkey;
+         return FALSE;
+       }
       break;
     case dsharpseen:
       if (toktype == st_C_define)
@@ -1839,15 +2666,15 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
        * and constantypedefs is FALSE.
        */
       definedef = dignorerest;
-      *is_func = (c == '(');
-      if (!*is_func && !constantypedefs)
+      *is_func_or_var = (c == '(');
+      if (!*is_func_or_var && !constantypedefs)
        return FALSE;
       else
        return TRUE;
     case dignorerest:
       return FALSE;
     default:
-      error ("internal error: definedef value.", 0);
+      error ("internal error: definedef value.", (char *)NULL);
     }
 
   /*
@@ -1859,28 +2686,36 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
       if (toktype == st_C_typedef)
        {
          if (typedefs)
-           typdef = ttypedseen;
-         funcdef = fnone;
+           typdef = tkeyseen;
+         fvextern = FALSE;
+         fvdef = fvnone;
          return FALSE;
        }
       break;
-    case ttypedseen:
+    case tkeyseen:
       switch (toktype)
        {
        case st_none:
        case st_C_typespec:
-         typdef = tend;
-         break;
+       case st_C_class:
        case st_C_struct:
        case st_C_enum:
+         typdef = ttypeseen;
          break;
        }
-      /* Do not return here, so the structdef stuff has a chance. */
+      break;
+    case ttypeseen:
+      if (structdef == snone && fvdef == fvnone)
+       {
+         fvdef = fvnameseen;
+         return TRUE;
+       }
       break;
     case tend:
       switch (toktype)
        {
        case st_C_typespec:
+       case st_C_class:
        case st_C_struct:
        case st_C_enum:
          return FALSE;
@@ -1889,68 +2724,50 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
     }
 
   /*
-   * 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_javastruct:
+      if (structdef == stagseen)
+        structdef = scolonseen;
+      return FALSE;
+    case st_C_template:
+    case st_C_class:
+      if (cblev == 0
+         && (*c_extp & C_AUTO) /* automatic detection of C++ language */
+         && definedef == dnone && structdef == snone
+         && typdef == tnone && fvdef == fvnone)
+       *c_extp = (*c_extp | C_PLPL) & ~C_AUTO;
+      if (toktype == st_C_template)
+       break;
+      /* FALLTHRU */
     case st_C_struct:
     case st_C_enum:
-      if (typdef == ttypedseen
-         || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
+      if (parlev == 0
+         && fvdef != vignore
+         && (typdef == tkeyseen
+             || (typedefs_or_cplusplus && structdef == snone)))
        {
          structdef = skeyseen;
          structtype = toktype;
+         structcblev = cblev;
        }
       return FALSE;
     }
+
   if (structdef == skeyseen)
     {
-      /* Save the tag for struct/union/class, for functions that may be
-         defined inside. */
-      if (structtype == st_C_struct)
-       structtag = savenstr (str, len);
-      else
-       structtag = "<enum>";
       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 && toktype == st_C_gnumacro)
-    {
-      next_token_is_func = TRUE;
-      return FALSE;
-    }
-  if (next_token_is_func)
-    {
-      next_token_is_func = FALSE;
-      funcdef = fignore;
-      *is_func = TRUE;
-      return TRUE;
-    }
+    definedef = dnone;
 
-  /*
-   * Detecting Objective C constructs.
-   */
+  /* Detect Objective C constructs. */
   switch (objdef)
     {
     case onone:
@@ -1965,7 +2782,7 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
        }
       break;
     case oimplementation:
-      /* Save the class tag for functions that may be defined inside. */
+      /* Save the class tag for functions or variables defined inside. */
       objtag = savenstr (str, len);
       objdef = oinbody;
       return FALSE;
@@ -1973,11 +2790,11 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
       /* Save the class tag for categories. */
       objtag = savenstr (str, len);
       objdef = otagseen;
-      *is_func = TRUE;
+      *is_func_or_var = TRUE;
       return TRUE;
     case oparenseen:
       objdef = ocatseen;
-      *is_func = TRUE;
+      *is_func_or_var = TRUE;
       return TRUE;
     case oinbody:
       break;
@@ -1985,10 +2802,9 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
       if (parlev == 0)
        {
          objdef = omethodtag;
-         methodlen = len;
-         GROW_LINEBUFFER (token_name, methodlen+1);
+         linebuffer_setlen (&token_name, len);
          strncpy (token_name.buffer, str, len);
-         token_name.buffer[methodlen] = '\0';
+         token_name.buffer[len] = '\0';
          return TRUE;
        }
       return FALSE;
@@ -2000,8 +2816,7 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
       if (parlev == 0)
        {
          objdef = omethodtag;
-         methodlen += len;
-         GROW_LINEBUFFER (token_name, methodlen+1);
+         linebuffer_setlen (&token_name, token_name.len + len);
          strncat (token_name.buffer, str, len);
          return TRUE;
        }
@@ -2020,53 +2835,83 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func)
       return FALSE;
     }
 
-  /* A function? */
+  /* A function, variable or enum constant? */
   switch (toktype)
     {
+    case st_C_extern:
+      fvextern = TRUE;
+      /* FALLTHRU */
     case st_C_typespec:
-      if (funcdef != finlist && funcdef != fignore)
-        funcdef = fnone;               /* should be useless */
+      if (fvdef != finlist && fvdef != fignore && fvdef != vignore)
+       fvdef = fvnone;         /* should be useless */
       return FALSE;
-    default:
-      if (funcdef == fnone)
+    case st_C_ignore:
+      fvextern = FALSE;
+      fvdef = vignore;
+      return FALSE;
+    case st_C_operator:
+      fvdef = foperator;
+      *is_func_or_var = TRUE;
+      return TRUE;
+    case st_none:
+      if (constantypedefs
+         && structdef == snone
+         && structtype == st_C_enum && cblev > structcblev)
+       return TRUE;            /* enum constant */
+      switch (fvdef)
        {
-         funcdef = ftagseen;
-         *is_func = TRUE;
+       case fdefunkey:
+         if (cblev > 0)
+           break;
+         fvdef = fdefunname;   /* GNU macro */
+         *is_func_or_var = TRUE;
+         return TRUE;
+       case fvnone:
+         if ((strneq (str, "asm", 3) && endtoken (str[3]))
+             || (strneq (str, "__asm__", 7) && endtoken (str[7])))
+           {
+             fvdef = vignore;
+             return FALSE;
+           }
+         if ((*c_extp & C_PLPL) && strneq (str+len-10, "::operator", 10))
+           {
+             fvdef = foperator;
+             *is_func_or_var = TRUE;
+             return TRUE;
+           }
+         if (cblev > 0 && !instruct)
+           break;
+         fvdef = fvnameseen;   /* function or variable */
+         *is_func_or_var = TRUE;
          return TRUE;
        }
+      break;
     }
 
   return FALSE;
 }
 
+\f
 /*
- * C_entries ()
- *     This routine finds functions, typedefs, #define's and
- *     struct/union/enum definitions in C syntax and adds them
- *     to the list.
+ * C_entries often keeps pointers to tokens or lines which are older than
+ * the line currently read.  By keeping two line buffers, and switching
+ * them at end of line, it is possible to use those pointers.
  */
-typedef struct
+static struct
 {
-  logical valid;
-  char *str;
-  logical named;
-  int linelen;
-  int lineno;
   long linepos;
-  char *buffer;
-} TOKEN;
+  linebuffer lb;
+} lbs[2];
 
 #define current_lb_is_new (newndx == curndx)
 #define switch_line_buffers() (curndx = 1 - curndx)
 
 #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)
 
-#define CNL_SAVE_DEFINEDEF                                             \
+#define CNL_SAVE_DEFINEDEF()                                           \
 do {                                                                   \
   curlinepos = charno;                                                 \
   lineno++;                                                            \
@@ -2077,29 +2922,59 @@ do {                                                                    \
   newndx = curndx;                                                     \
 } while (0)
 
-#define CNL                                                            \
+#define CNL()                                                          \
 do {                                                                   \
-  CNL_SAVE_DEFINEDEF;                                                  \
-  if (savetok.valid)                                                   \
+  CNL_SAVE_DEFINEDEF();                                                        \
+  if (savetoken.valid)                                                 \
     {                                                                  \
-      tok = savetok;                                                   \
-      savetok.valid = FALSE;                                           \
+      token = savetoken;                                               \
+      savetoken.valid = FALSE;                                         \
     }                                                                  \
   definedef = dnone;                                                   \
 } while (0)
 
-/* Ideally this macro should never be called wihen tok.valid is FALSE,
-   but this would mean that the state machines always guess right. */
-#define make_tag(isfun)  do \
-if (tok.valid) {                                                       \
-  char *name = NULL;                                                   \
-  if (CTAGS || tok.named)                                              \
-    name = savestr (token_name.buffer);                                        \
-  pfnote (name, isfun, tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
-  tok.valid = FALSE;                                                   \
-} while (0)
 
-void
+static void
+make_C_tag (isfun)
+     bool isfun;
+{
+  /* This function should never be called when token.valid is FALSE, but
+     we must protect against invalid input or internal errors. */
+  if (DEBUG || token.valid)
+    {
+      if (traditional_tag_style)
+       {
+         /* This was the original code.  Now we call new_pfnote instead,
+            which uses the new method for naming tags (see new_pfnote). */
+         char *name = NULL;
+
+         if (CTAGS || token.named)
+           name = savestr (token_name.buffer);
+         if (DEBUG && !token.valid)
+           {
+             if (token.named)
+               name = concat (name, "##invalid##", "");
+             else
+               name = savestr ("##invalid##");
+           }
+         pfnote (name, isfun, token.line,
+                 token.offset+token.length+1, token.lineno, token.linepos);
+       }
+      else
+       new_pfnote (token_name.buffer, token_name.len, isfun, token.line,
+                   token.offset+token.length+1, token.lineno, token.linepos);
+      token.valid = FALSE;
+    }
+}
+
+
+/*
+ * C_entries ()
+ *     This routine finds functions, variables, typedefs,
+ *     #define's, enum constants and struct/union/enum definitions in
+ *     C syntax and adds them to the list.
+ */
+static void
 C_entries (c_ext, inf)
      int c_ext;                        /* extension of C */
      FILE *inf;                        /* input file */
@@ -2107,30 +2982,51 @@ C_entries (c_ext, inf)
   register char c;             /* latest char read; '\0' for end of line */
   register char *lp;           /* pointer one beyond the character `c' */
   int curndx, newndx;          /* indices for current and new lb */
-  TOKEN tok;                   /* latest token read */
   register int tokoff;         /* offset in line of start of current token */
   register int toklen;         /* length of current token */
+  char *qualifier;             /* string used to qualify names */
+  int qlen;                    /* length of qualifier */
   int cblev;                   /* current curly brace level */
   int parlev;                  /* current parenthesis level */
-  logical incomm, inquote, inchar, quotednl, midtoken;
-  logical cplpl;
-  TOKEN savetok;               /* token saved during preprocessor handling */
+  int typdefcblev;             /* cblev where a typedef struct body begun */
+  bool incomm, inquote, inchar, quotednl, midtoken;
+  bool cplpl, cjava;
+  bool yacc_rules;             /* in the rules part of a yacc file */
+  struct tok savetoken;                /* token saved during preprocessor handling */
 
 
+  initbuffer (&token_name);
+  initbuffer (&lbs[0].lb);
+  initbuffer (&lbs[1].lb);
+  if (cstack.size == 0)
+    {
+      cstack.size = (DEBUG) ? 1 : 4;
+      cstack.nl = 0;
+      cstack.cname = xnew (cstack.size, char *);
+      cstack.cblev = xnew (cstack.size, int);
+    }
+
+  tokoff = toklen = typdefcblev = 0; /* keep compiler quiet */
   curndx = newndx = 0;
   lineno = 0;
   charno = 0;
   lp = curlb.buffer;
   *lp = 0;
 
-  funcdef = fnone; typdef = tnone; structdef = snone;
-  definedef = dnone; objdef = onone;
-  next_token_is_func = yacc_rules = FALSE;
+  fvdef = fvnone; fvextern = FALSE; typdef = tnone;
+  structdef = snone; definedef = dnone; objdef = onone;
+  yacc_rules = FALSE;
   midtoken = inquote = inchar = incomm = quotednl = FALSE;
-  tok.valid = savetok.valid = FALSE;
+  token.valid = savetoken.valid = FALSE;
   cblev = 0;
   parlev = 0;
-  cplpl = c_ext & C_PLPL;
+  cplpl = (c_ext & C_PLPL) == C_PLPL;
+  cjava = (c_ext & C_JAVA) == C_JAVA;
+  if (cjava)
+    { qualifier = "."; qlen = 1; }
+  else
+    { qualifier = "::"; qlen = 2; }
+
 
   while (!feof (inf))
     {
@@ -2162,7 +3058,7 @@ C_entries (c_ext, inf)
            case '\0':
              /* Newlines inside comments do not end macro definitions in
                 traditional cpp. */
-             CNL_SAVE_DEFINEDEF;
+             CNL_SAVE_DEFINEDEF ();
              break;
            }
          continue;
@@ -2178,7 +3074,7 @@ C_entries (c_ext, inf)
              /* Newlines inside strings do not end macro definitions
                 in traditional cpp, even though compilers don't
                 usually accept them. */
-             CNL_SAVE_DEFINEDEF;
+             CNL_SAVE_DEFINEDEF ();
              break;
            }
          continue;
@@ -2189,7 +3085,7 @@ C_entries (c_ext, inf)
            {
            case '\0':
              /* Hmmm, something went wrong. */
-             CNL;
+             CNL ();
              /* FALLTHRU */
            case '\'':
              inchar = FALSE;
@@ -2202,13 +3098,26 @@ C_entries (c_ext, inf)
          {
          case '"':
            inquote = TRUE;
-           if (funcdef != finlist && funcdef != fignore)
-             funcdef = fnone;
+           switch (fvdef)
+             {
+             case fdefunkey:
+             case fstartlist:
+             case finlist:
+             case fignore:
+             case vignore:
+               break;
+             default:
+               fvextern = FALSE;
+               fvdef = fvnone;
+             }
            continue;
          case '\'':
            inchar = TRUE;
-           if (funcdef != finlist && funcdef != fignore)
-             funcdef = fnone;
+           if (fvdef != finlist && fvdef != fignore && fvdef !=vignore)
+             {
+               fvextern = FALSE;
+               fvdef = fvnone;
+             }
            continue;
          case '/':
            if (*lp == '*')
@@ -2227,11 +3136,10 @@ C_entries (c_ext, inf)
          case '%':
            if ((c_ext & YACC) && *lp == '%')
              {
-               /* entering or exiting rules section in yacc file */
+               /* Entering or exiting rules section in yacc file. */
                lp++;
-               definedef = dnone; funcdef = fnone;
+               definedef = dnone; fvdef = fvnone; fvextern = FALSE;
                typdef = tnone; structdef = snone;
-               next_token_is_func = FALSE;
                midtoken = inquote = inchar = incomm = quotednl = FALSE;
                cblev = 0;
                yacc_rules = !yacc_rules;
@@ -2243,7 +3151,7 @@ C_entries (c_ext, inf)
            if (definedef == dnone)
              {
                char *cp;
-               logical cpptoken = TRUE;
+               bool cpptoken = TRUE;
 
                /* Look back on this line.  If all blanks, or nonblanks
                   followed by an end of comment, this is a preprocessor
@@ -2267,102 +3175,151 @@ C_entries (c_ext, inf)
          } /* 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
+      /* Consider token only if some involved conditions are satisfied. */
+      if (typdef != tignore
          && definedef != dignorerest
-         && funcdef != finlist)
+         && fvdef != finlist
+         && structdef != sintemplate
+         && (definedef != dnone
+             || structdef != scolonseen))
        {
          if (midtoken)
            {
              if (endtoken (c))
                {
-                 if (c == ':' && cplpl && *lp == ':' && begtoken(*(lp + 1)))
+                 if (c == ':' && cplpl && *lp == ':' && begtoken (lp[1]))
                    {
                      /*
                       * This handles :: in the middle, but not at the
-                      * beginning of an identifier.
+                      * beginning of an identifier.  Also, space-separated
+                      * :: is not recognised.
                       */
                      lp += 2;
-                     toklen += 3;
+                     toklen += 2;
+                     c = lp[-1];
+                     goto still_in_token;
                    }
                  else
                    {
-                     logical is_func = FALSE;
+                     bool funorvar = FALSE;
 
                      if (yacc_rules
                          || consider_token (newlb.buffer + tokoff, toklen, c,
-                                            c_ext, cblev, parlev, &is_func))
+                                            &c_ext, cblev, parlev, &funorvar))
                        {
-                         if (structdef == sinbody
-                             && definedef == dnone
-                             && is_func)
-                           /* function defined in C++ class body */
+                         if (fvdef == foperator)
                            {
-                             GROW_LINEBUFFER (token_name,
-                                              strlen(structtag)+2+toklen+1);
-                             strcpy (token_name.buffer, structtag);
-                             strcat (token_name.buffer, "::");
+                             char *oldlp = lp;
+                             lp = skip_spaces (lp-1);
+                             if (*lp != '\0')
+                               lp += 1;
+                             while (*lp != '\0'
+                                    && !iswhite (*lp) && *lp != '(')
+                               lp += 1;
+                             c = *lp++;
+                             toklen += lp - oldlp;
+                           }
+                         token.named = FALSE;
+                         if ((c_ext & C_EXT)   /* not pure C */
+                             && nestlev > 0 && definedef == dnone)
+                           /* in struct body */
+                           {
+                              write_classname (&token_name, qualifier);
+                             linebuffer_setlen (&token_name,
+                                                token_name.len+qlen+toklen);
+                             strcat (token_name.buffer, qualifier);
                              strncat (token_name.buffer,
-                                      newlb.buffer+tokoff, toklen);
-                             tok.named = TRUE;
+                                      newlb.buffer + tokoff, toklen);
+                             token.named = TRUE;
                            }
                          else if (objdef == ocatseen)
                            /* Objective C category */
                            {
-                             GROW_LINEBUFFER (token_name,
-                                              strlen(objtag)+2+toklen+1);
+                             int len = strlen (objtag) + 2 + toklen;
+                             linebuffer_setlen (&token_name, len);
                              strcpy (token_name.buffer, objtag);
                              strcat (token_name.buffer, "(");
                              strncat (token_name.buffer,
-                                      newlb.buffer+tokoff, toklen);
+                                      newlb.buffer + tokoff, toklen);
                              strcat (token_name.buffer, ")");
-                             tok.named = TRUE;
+                             token.named = TRUE;
                            }
                          else if (objdef == omethodtag
                                   || objdef == omethodparm)
                            /* Objective C method */
                            {
-                             tok.named = TRUE;
+                             token.named = TRUE;
+                           }
+                         else if (fvdef == fdefunname)
+                           /* GNU DEFUN and similar macros */
+                           {
+                             bool defun = (newlb.buffer[tokoff] == 'F');
+                             int off = tokoff;
+                             int len = toklen;
+
+                             /* Rewrite the tag so that emacs lisp DEFUNs
+                                can be found by their elisp name */
+                             if (defun)
+                               {
+                                 off += 1;
+                                 len -= 1;
+                               }
+                             len = toklen;
+                             linebuffer_setlen (&token_name, len);
+                             strncpy (token_name.buffer,
+                                      newlb.buffer + off, len);
+                             token_name.buffer[len] = '\0';
+                             if (defun)
+                               while (--len >= 0)
+                                 if (token_name.buffer[len] == '_')
+                                   token_name.buffer[len] = '-';
+                             token.named = defun;
                            }
                          else
                            {
-                             GROW_LINEBUFFER (token_name, toklen+1);
+                             linebuffer_setlen (&token_name, toklen);
                              strncpy (token_name.buffer,
-                                      newlb.buffer+tokoff, toklen);
+                                      newlb.buffer + tokoff, toklen);
                              token_name.buffer[toklen] = '\0';
-                             if (structdef == stagseen
-                                 || typdef == tend
-                                 || (is_func
-                                     && definedef == dignorerest)) /* macro */
-                               tok.named = TRUE;
-                             else
-                               tok.named = FALSE;
+                             /* Name macros and members. */
+                             token.named = (structdef == stagseen
+                                            || typdef == ttypeseen
+                                            || typdef == tend
+                                            || (funorvar
+                                                && definedef == dignorerest)
+                                            || (funorvar
+                                                && definedef == dnone
+                                                && structdef == snone
+                                                && cblev > 0));
                            }
-                         tok.lineno = lineno;
-                         tok.linelen = tokoff + toklen + 1;
-                         tok.buffer = newlb.buffer;
-                         tok.linepos = newlinepos;
-                         tok.valid = TRUE;
+                         token.lineno = lineno;
+                         token.offset = tokoff;
+                         token.length = toklen;
+                         token.line = newlb.buffer;
+                         token.linepos = newlinepos;
+                         token.valid = TRUE;
 
                          if (definedef == dnone
-                             && (funcdef == ftagseen
+                             && (fvdef == fvnameseen
+                                 || fvdef == foperator
                                  || structdef == stagseen
                                  || typdef == tend
+                                 || typdef == ttypeseen
                                  || objdef != onone))
                            {
                              if (current_lb_is_new)
                                switch_line_buffers ();
                            }
-                         else
-                           make_tag (is_func);
+                         else if (definedef != dnone
+                                  || fvdef == fdefunname
+                                  || instruct)
+                           make_C_tag (funorvar);
                        }
                      midtoken = FALSE;
                    }
                } /* if (endtoken (c)) */
              else if (intoken (c))
+               still_in_token:
                {
                  toklen++;
                  continue;
@@ -2373,24 +3330,27 @@ C_entries (c_ext, inf)
              switch (definedef)
                {
                case dnone:
-                 switch (funcdef)
+                 switch (fvdef)
                    {
                    case fstartlist:
-                     funcdef = finlist;
+                     fvdef = finlist;
                      continue;
                    case flistseen:
-                     make_tag (TRUE);
-                     funcdef = fignore;
+                     make_C_tag (TRUE); /* a function */
+                     fvdef = fignore;
                      break;
-                   case ftagseen:
-                     funcdef = fnone;
+                   case fvnameseen:
+                     fvdef = fvnone;
                      break;
                    }
-                 if (structdef == stagseen)
-                   structdef = snone;
+                 if (structdef == stagseen && !cjava)
+                   {
+                     popclass_above (cblev);
+                     structdef = snone;
+                   }
                  break;
                case dsharpseen:
-                 savetok = tok;
+                 savetoken = token;
                }
              if (!yacc_rules || lp == newlb.buffer + 1)
                {
@@ -2408,58 +3368,78 @@ C_entries (c_ext, inf)
       switch (c)
        {
        case ':':
+         if (yacc_rules && token.offset == 0 && token.valid)
+           {
+             make_C_tag (FALSE); /* a yacc function */
+             break;
+           }
          if (definedef != dnone)
            break;
          switch (objdef)
            {
            case  otagseen:
              objdef = oignore;
-             make_tag (TRUE);
+             make_C_tag (TRUE); /* an Objective C class */
              break;
            case omethodtag:
            case omethodparm:
              objdef = omethodcolon;
-             methodlen += 1;
-             GROW_LINEBUFFER (token_name, methodlen+1);
+             linebuffer_setlen (&token_name, token_name.len + 1);
              strcat (token_name.buffer, ":");
              break;
            }
          if (structdef == stagseen)
            structdef = scolonseen;
-         else
-           switch (funcdef)
-             {
-             case ftagseen:
-               if (yacc_rules)
-                 {
-                   make_tag (FALSE);
-                   funcdef = fignore;
-                 }
-               break;
-             case fstartlist:
-               funcdef = fnone;
-               break;
-             }
          break;
        case ';':
          if (definedef != dnone)
            break;
-         if (cblev == 0)
-           switch (typdef)
-             {
-             case tend:
-               make_tag (FALSE);
-               /* FALLTHRU */
-             default:
-               typdef = tnone;
-             }
-         if (funcdef != fignore)
+         switch (typdef)
            {
-             funcdef = fnone;
-             /* The following instruction invalidates the token.
-                Probably the token should be invalidated in all
-                other cases  where some state machine is reset. */
-             tok.valid = FALSE;
+           case tend:
+           case ttypeseen:
+             make_C_tag (FALSE); /* a typedef */
+             typdef = tnone;
+             fvdef = fvnone;
+             break;
+           case tnone:
+           case tinbody:
+           case tignore:
+             switch (fvdef)
+               {
+               case fignore:
+                 if (typdef == tignore)
+                   fvdef = fvnone;
+                 break;
+               case fvnameseen:
+                 if ((globals && cblev == 0 && (!fvextern || declarations))
+                     || (members && instruct))
+                   make_C_tag (FALSE); /* a variable */
+                 fvextern = FALSE;
+                 fvdef = fvnone;
+                 token.valid = FALSE;
+                 break;
+               case flistseen:
+                 if ((declarations && typdef == tnone && !instruct)
+                     || (members && typdef != tignore && instruct))
+                   make_C_tag (TRUE);  /* a function declaration */
+                 /* FALLTHRU */
+               default:
+                 fvextern = FALSE;
+                 fvdef = fvnone;
+                 if (declarations
+                     && structdef == stagseen && (c_ext & C_PLPL))
+                   make_C_tag (FALSE); /* forward declaration */
+                 else
+                   /* The following instruction invalidates the token.
+                      Probably the token should be invalidated in all other
+                      cases where some state machine is reset prematurely. */
+                   token.valid = FALSE;
+               } /* switch (fvdef) */
+             /* FALLTHRU */
+           default:
+             if (!instruct)
+               typdef = tnone;
            }
          if (structdef == stagseen)
            structdef = snone;
@@ -2471,57 +3451,102 @@ C_entries (c_ext, inf)
            {
            case omethodtag:
            case omethodparm:
-             make_tag (TRUE);
+             make_C_tag (TRUE); /* an Objective C method */
              objdef = oinbody;
              break;
            }
-         if (funcdef != finlist && funcdef != fignore)
-           funcdef = fnone;
+         switch (fvdef)
+           {
+           case fdefunkey:
+           case foperator:
+           case fstartlist:
+           case finlist:
+           case fignore:
+           case vignore:
+             break;
+           case fdefunname:
+             fvdef = fignore;
+             break;
+           case fvnameseen:    /* a variable */
+             if ((globals && cblev == 0 && (!fvextern || declarations))
+                 || (members && instruct))
+               make_C_tag (FALSE);
+             break;
+           case flistseen:     /* a function */
+             if ((declarations && typdef == tnone && !instruct)
+                 || (members && typdef != tignore && instruct))
+               {
+                 make_C_tag (TRUE); /* a function declaration */
+                 fvdef = fvnameseen;
+               }
+             else if (!declarations)
+               fvdef = fvnone;
+             token.valid = FALSE;
+             break;
+           default:
+             fvdef = fvnone;
+           }
          if (structdef == stagseen)
            structdef = snone;
          break;
        case '[':
          if (definedef != dnone)
            break;
-         if (cblev == 0 && typdef == tend)
+         if (structdef == stagseen)
+           structdef = snone;
+         switch (typdef)
            {
+           case ttypeseen:
+           case tend:
              typdef = tignore;
-             make_tag (FALSE);
+             make_C_tag (FALSE);       /* a typedef */
+             break;
+           case tnone:
+           case tinbody:
+             switch (fvdef)
+               {
+               case foperator:
+               case finlist:
+               case fignore:
+               case vignore:
+                 break;
+               case fvnameseen:
+                 if ((members && cblev == 1)
+                     || (globals && cblev == 0
+                         && (!fvextern || declarations)))
+                   make_C_tag (FALSE); /* a variable */
+                 /* FALLTHRU */
+               default:
+                 fvdef = fvnone;
+               }
              break;
            }
-         if (funcdef != finlist && funcdef != fignore)
-           funcdef = fnone;
-         if (structdef == stagseen)
-           structdef = snone;
          break;
        case '(':
          if (definedef != dnone)
            break;
          if (objdef == otagseen && parlev == 0)
            objdef = oparenseen;
-         switch (funcdef)
+         switch (fvdef)
            {
-           case fnone:
-             switch (typdef)
+           case fvnameseen:
+             if (typdef == ttypeseen
+                 && *lp != '*'
+                 && !instruct)
                {
-               case ttypedseen:
-               case tend:
-                 /* Make sure that the next char is not a '*'.
-                    This handles constructs like:
+                 /* This handles constructs like:
                     typedef void OperatorFun (int fun); */
-                 if (*lp != '*')
-                   {
-                     typdef = tignore;
-                     make_tag (FALSE);
-                   }
+                 make_C_tag (FALSE);
+                 typdef = tignore;
+                 fvdef = fignore;
                  break;
-               } /* switch (typdef) */
-             break;
-           case ftagseen:
-             funcdef = fstartlist;
+               }
+             /* FALLTHRU */
+           case foperator:
+             fvdef = fstartlist;
              break;
            case flistseen:
-             funcdef = finlist;
+             fvdef = finlist;
              break;
            }
          parlev++;
@@ -2531,22 +3556,24 @@ C_entries (c_ext, inf)
            break;
          if (objdef == ocatseen && parlev == 1)
            {
-             make_tag (TRUE);
+             make_C_tag (TRUE); /* an Objective C category */
              objdef = oignore;
            }
          if (--parlev == 0)
            {
-             switch (funcdef)
+             switch (fvdef)
                {
                case fstartlist:
                case finlist:
-                 funcdef = flistseen;
+                 fvdef = flistseen;
                  break;
                }
-             if (cblev == 0 && typdef == tend)
+             if (!instruct
+                 && (typdef == tend
+                     || typdef == ttypeseen))
                {
                  typdef = tignore;
-                 make_tag (FALSE);
+                 make_C_tag (FALSE); /* a typedef */
                }
            }
          else if (parlev < 0)  /* can happen due to ill-conceived #if's. */
@@ -2555,53 +3582,60 @@ C_entries (c_ext, inf)
        case '{':
          if (definedef != dnone)
            break;
-         if (typdef == ttypedseen)
-           typdef = tinbody;
-         switch (structdef)
+         if (typdef == ttypeseen)
            {
-           case skeyseen:      /* unnamed struct */
-             structtag = "_anonymous_";
-             structdef = sinbody;
-             break;
-           case stagseen:
-           case scolonseen:    /* named struct */
-             structdef = sinbody;
-             make_tag (FALSE);
-             break;
+             /* Whenever typdef is set to tinbody (currently only
+                here), typdefcblev should be set to cblev. */
+             typdef = tinbody;
+             typdefcblev = cblev;
            }
-         switch (funcdef)
+         switch (fvdef)
            {
            case flistseen:
-             make_tag (TRUE);
+             make_C_tag (TRUE);    /* a function */
              /* FALLTHRU */
            case fignore:
-             funcdef = fnone;
+             fvdef = fvnone;
              break;
-           case fnone:
+           case fvnone:
              switch (objdef)
                {
                case otagseen:
-                 make_tag (TRUE);
+                 make_C_tag (TRUE); /* an Objective C class */
                  objdef = oignore;
                  break;
                case omethodtag:
                case omethodparm:
-                 make_tag (TRUE);
+                 make_C_tag (TRUE); /* an Objective C method */
                  objdef = oinbody;
                  break;
                default:
-                 /* Neutralize `extern "C" {' grot and look inside structs. */
-                 if (cblev == 0 && structdef == snone && typdef == tnone)
+                 /* Neutralize `extern "C" {' grot. */
+                 if (cblev == 0 && structdef == snone && nestlev == 0
+                     && typdef == tnone)
                    cblev = -1;
                }
            }
+         switch (structdef)
+           {
+           case skeyseen:         /* unnamed struct */
+             pushclass_above (cblev, NULL, 0);
+             structdef = snone;
+             break;
+           case stagseen:         /* named struct or enum */
+           case scolonseen:       /* a class */
+             pushclass_above (cblev, token.line+token.offset, token.length);
+             structdef = snone;
+             make_C_tag (FALSE);  /* a struct or enum */
+             break;
+           }
          cblev++;
          break;
        case '*':
          if (definedef != dnone)
            break;
-         if (funcdef == fstartlist)
-           funcdef = fnone;    /* avoid tagging `foo' in `foo (*bar()) ()' */
+         if (fvdef == fstartlist)
+           fvdef = fvnone;     /* avoid tagging `foo' in `foo (*bar()) ()' */
          break;
        case '}':
          if (definedef != dnone)
@@ -2613,22 +3647,48 @@ C_entries (c_ext, inf)
            }
          else if (cblev > 0)
            cblev--;
-         if (cblev == 0)
+         popclass_above (cblev);
+         structdef = snone;
+         /* Only if typdef == tinbody is typdefcblev significant. */
+         if (typdef == tinbody && cblev <= typdefcblev)
            {
-             if (typdef == tinbody)
-               typdef = tend;
-             /* Memory leakage here: the string pointed by structtag is
-                never released, because I fear to miss something and
-                break things while freeing the area.  The amount of
-                memory leaked here is the sum of the lengths of the
-                struct tags.
-             if (structdef == sinbody)
-               free (structtag); */
-
-             structdef = snone;
-             structtag = "<error>";
+             assert (cblev == typdefcblev);
+             typdef = tend;
            }
          break;
+       case '=':
+         if (definedef != dnone)
+           break;
+         switch (fvdef)
+           {
+           case foperator:
+           case finlist:
+           case fignore:
+           case vignore:
+             break;
+           case fvnameseen:
+             if ((members && cblev == 1)
+                 || (globals && cblev == 0 && (!fvextern || declarations)))
+               make_C_tag (FALSE); /* a variable */
+             /* FALLTHRU */
+           default:
+             fvdef = vignore;
+           }
+         break;
+       case '<':
+         if (cplpl && structdef == stagseen)
+           {
+             structdef = sintemplate;
+             break;
+           }
+         goto resetfvdef;
+       case '>':
+         if (structdef == sintemplate)
+           {
+             structdef = stagseen;
+             break;
+           }
+         goto resetfvdef;
        case '+':
        case '-':
          if (objdef == oinbody && cblev == 0)
@@ -2637,44 +3697,57 @@ C_entries (c_ext, inf)
              break;
            }
          /* FALLTHRU */
-       case '=': case '#': case '~': case '&': case '%': case '/':
-       case '|': case '^': case '!': case '<': case '>': case '.': case '?':
+       resetfvdef:
+       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;
+         /* These surely cannot follow a function tag in C. */
+         switch (fvdef)
+           {
+           case foperator:
+           case finlist:
+           case fignore:
+           case vignore:
+             break;
+           default:
+             fvdef = fvnone;
+           }
          break;
        case '\0':
          if (objdef == otagseen)
            {
-             make_tag (TRUE);
+             make_C_tag (TRUE); /* an Objective C class */
              objdef = oignore;
            }
          /* If a macro spans multiple lines don't reset its state. */
          if (quotednl)
-           CNL_SAVE_DEFINEDEF;
+           CNL_SAVE_DEFINEDEF ();
          else
-           CNL;
+           CNL ();
          break;
        } /* switch (c) */
 
     } /* while not eof */
+
+  free (token_name.buffer);
+  free (lbs[0].lb.buffer);
+  free (lbs[1].lb.buffer);
 }
 
 /*
  * Process either a C++ file or a C file depending on the setting
  * of a global flag.
  */
-void
+static void
 default_C_entries (inf)
      FILE *inf;
 {
-  C_entries (cplusplus ? C_PLPL : 0, inf);
+  C_entries (cplusplus ? C_PLPL : C_AUTO, inf);
 }
 
-/* Always do plain ANSI C. */
-void
+/* Always do plain C. */
+static void
 plain_C_entries (inf)
      FILE *inf;
 {
@@ -2682,15 +3755,23 @@ plain_C_entries (inf)
 }
 
 /* Always do C++. */
-void
+static void
 Cplusplus_entries (inf)
      FILE *inf;
 {
   C_entries (C_PLPL, inf);
 }
 
+/* Always do Java. */
+static void
+Cjava_entries (inf)
+     FILE *inf;
+{
+  C_entries (C_JAVA, inf);
+}
+
 /* Always do C*. */
-void
+static void
 Cstar_entries (inf)
      FILE *inf;
 {
@@ -2698,66 +3779,79 @@ Cstar_entries (inf)
 }
 
 /* Always do Yacc. */
-void
+static void
 Yacc_entries (inf)
      FILE *inf;
 {
   C_entries (YACC, inf);
 }
-\f
-/* Fortran parsing */
 
-char *dbp;
+\f
+/* Useful macros. */
+#define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer)   \
+  for (lineno = charno = 0;    /* loop initialization */               \
+       !feof (file_pointer)    /* loop test */                         \
+       && (lineno++,           /* instructions at start of loop */     \
+          linecharno = charno,                                         \
+          charno += readline (&line_buffer, file_pointer),             \
+          char_pointer = lb.buffer,                                    \
+          TRUE);                                                       \
+      )
+#define LOOKING_AT(cp, keyword)        /* keyword is a constant string */      \
+  (strneq ((cp), keyword, sizeof(keyword)-1) /* cp points at keyword */        \
+   && notinname ((cp)[sizeof(keyword)-1])      /* end of keyword */    \
+   && ((cp) = skip_spaces((cp)+sizeof(keyword)-1))) /* skip spaces */
 
-logical
-tail (cp)
-     char *cp;
+/*
+ * Read a file, but do no processing.  This is used to do regexp
+ * matching on files that have no language defined.
+ */
+static void
+just_read_file (inf)
+     FILE *inf;
 {
-  register int len = 0;
+  register char *dummy;
 
-  while (*cp && lowcase(*cp) == lowcase(dbp[len]))
-    cp++, len++;
-  if (*cp == '\0' && !intoken(dbp[len]))
-    {
-      dbp += len;
-      return TRUE;
-    }
-  return FALSE;
+  LOOP_ON_INPUT_LINES (inf, lb, dummy)
+    continue;
 }
 
-void
-takeprec ()
+\f
+/* Fortran parsing */
+
+static void F_takeprec __P((void));
+static void F_getit __P((FILE *));
+
+static void
+F_takeprec ()
 {
-  while (isspace (*dbp))
-    dbp++;
+  dbp = skip_spaces (dbp);
   if (*dbp != '*')
     return;
   dbp++;
-  while (isspace (*dbp))
-    dbp++;
+  dbp = skip_spaces (dbp);
   if (strneq (dbp, "(*)", 3))
     {
       dbp += 3;
       return;
     }
-  if (!isdigit (*dbp))
+  if (!ISDIGIT (*dbp))
     {
       --dbp;                   /* force failure */
       return;
     }
   do
     dbp++;
-  while (isdigit (*dbp));
+  while (ISDIGIT (*dbp));
 }
 
-void
-getit (inf)
+static void
+F_getit (inf)
      FILE *inf;
 {
   register char *cp;
 
-  while (isspace (*dbp))
-    dbp++;
+  dbp = skip_spaces (dbp);
   if (*dbp == '\0')
     {
       lineno++;
@@ -2767,193 +3861,549 @@ getit (inf)
       if (dbp[5] != '&')
        return;
       dbp += 6;
-      while (isspace (*dbp))
-       dbp++;
+      dbp = skip_spaces (dbp);
     }
-  if (!isalpha (*dbp)
-      && *dbp != '_'
-      && *dbp != '$')
+  if (!ISALPHA (*dbp) && *dbp != '_' && *dbp != '$')
     return;
-  for (cp = dbp + 1;
-       (*cp
-       && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
-       cp++)
+  for (cp = dbp + 1; *cp != '\0' && intoken (*cp); cp++)
     continue;
-  pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
+  pfnote (savenstr (dbp, cp-dbp), TRUE,
          lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
 }
 
-void
+
+static void
 Fortran_functions (inf)
      FILE *inf;
 {
-  lineno = 0;
-  charno = 0;
-
-  while (!feof (inf))
+  LOOP_ON_INPUT_LINES (inf, lb, dbp)
     {
-      lineno++;
-      linecharno = charno;
-      charno += readline (&lb, inf);
-      dbp = lb.buffer;
       if (*dbp == '%')
        dbp++;                  /* Ratfor escape to fortran */
-      while (isspace (*dbp))
-       dbp++;
+      dbp = skip_spaces (dbp);
       if (*dbp == '\0')
        continue;
       switch (lowcase (*dbp))
        {
-       case 'i':
-         if (tail ("integer"))
-           takeprec ();
-         break;
-       case 'r':
-         if (tail ("real"))
-           takeprec ();
-         break;
-       case 'l':
-         if (tail ("logical"))
-           takeprec ();
-         break;
-       case 'c':
-         if (tail ("complex") || tail ("character"))
-           takeprec ();
-         break;
-       case 'd':
-         if (tail ("double"))
+       case 'i':
+         if (nocase_tail ("integer"))
+           F_takeprec ();
+         break;
+       case 'r':
+         if (nocase_tail ("real"))
+           F_takeprec ();
+         break;
+       case 'l':
+         if (nocase_tail ("logical"))
+           F_takeprec ();
+         break;
+       case 'c':
+         if (nocase_tail ("complex") || nocase_tail ("character"))
+           F_takeprec ();
+         break;
+       case 'd':
+         if (nocase_tail ("double"))
+           {
+             dbp = skip_spaces (dbp);
+             if (*dbp == '\0')
+               continue;
+             if (nocase_tail ("precision"))
+               break;
+             continue;
+           }
+         break;
+       }
+      dbp = skip_spaces (dbp);
+      if (*dbp == '\0')
+       continue;
+      switch (lowcase (*dbp))
+       {
+       case 'f':
+         if (nocase_tail ("function"))
+           F_getit (inf);
+         continue;
+       case 's':
+         if (nocase_tail ("subroutine"))
+           F_getit (inf);
+         continue;
+       case 'e':
+         if (nocase_tail ("entry"))
+           F_getit (inf);
+         continue;
+       case 'b':
+         if (nocase_tail ("blockdata") || nocase_tail ("block data"))
+           {
+             dbp = skip_spaces (dbp);
+             if (*dbp == '\0') /* assume un-named */
+               pfnote (savestr ("blockdata"), TRUE,
+                       lb.buffer, dbp - lb.buffer, lineno, linecharno);
+             else
+               F_getit (inf);  /* look for name */
+           }
+         continue;
+       }
+    }
+}
+
+\f
+/*
+ * Ada parsing
+ * Original code by
+ * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be> (1998)
+ */
+
+static void Ada_getit __P((FILE *, char *));
+
+/* Once we are positioned after an "interesting" keyword, let's get
+   the real tag value necessary. */
+static void
+Ada_getit (inf, name_qualifier)
+     FILE *inf;
+     char *name_qualifier;
+{
+  register char *cp;
+  char *name;
+  char c;
+
+  while (!feof (inf))
+    {
+      dbp = skip_spaces (dbp);
+      if (*dbp == '\0'
+         || (dbp[0] == '-' && dbp[1] == '-'))
+       {
+         lineno++;
+         linecharno = charno;
+         charno += readline (&lb, inf);
+         dbp = lb.buffer;
+       }
+      switch (lowcase(*dbp))
+        {
+        case 'b':
+          if (nocase_tail ("body"))
+            {
+              /* Skipping body of   procedure body   or   package body or ....
+                resetting qualifier to body instead of spec. */
+              name_qualifier = "/b";
+              continue;
+            }
+          break;
+        case 't':
+          /* Skipping type of   task type   or   protected type ... */
+          if (nocase_tail ("type"))
+            continue;
+          break;
+        }
+      if (*dbp == '"')
+       {
+         dbp += 1;
+         for (cp = dbp; *cp != '\0' && *cp != '"'; cp++)
+           continue;
+       }
+      else
+       {
+         dbp = skip_spaces (dbp);
+         for (cp = dbp;
+              (*cp != '\0'
+               && (ISALPHA (*cp) || ISDIGIT (*cp) || *cp == '_' || *cp == '.'));
+              cp++)
+           continue;
+         if (cp == dbp)
+           return;
+       }
+      c = *cp;
+      *cp = '\0';
+      name = concat (dbp, name_qualifier, "");
+      *cp = c;
+      pfnote (name, TRUE, lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+      if (c == '"')
+       dbp = cp + 1;
+      return;
+    }
+}
+
+static void
+Ada_funcs (inf)
+     FILE *inf;
+{
+  bool inquote = FALSE;
+
+  LOOP_ON_INPUT_LINES (inf, lb, dbp)
+    {
+      while (*dbp != '\0')
+       {
+         /* Skip a string i.e. "abcd". */
+         if (inquote || (*dbp == '"'))
+           {
+             dbp = etags_strchr ((inquote) ? dbp : dbp+1, '"');
+             if (dbp != NULL)
+               {
+                 inquote = FALSE;
+                 dbp += 1;
+                 continue;     /* advance char */
+               }
+             else
+               {
+                 inquote = TRUE;
+                 break;        /* advance line */
+               }
+           }
+
+         /* Skip comments. */
+         if (dbp[0] == '-' && dbp[1] == '-')
+           break;              /* advance line */
+
+         /* Skip character enclosed in single quote i.e. 'a'
+            and skip single quote starting an attribute i.e. 'Image. */
+         if (*dbp == '\'')
+           {
+             dbp++ ;
+             if (*dbp != '\0')
+               dbp++;
+             continue;
+           }
+
+         /* Search for beginning of a token.  */
+         if (!begtoken (*dbp))
+           {
+             dbp++;
+             continue;         /* advance char */
+           }
+
+         /* We are at the beginning of a token. */
+         switch (lowcase(*dbp))
+           {
+           case 'f':
+             if (!packages_only && nocase_tail ("function"))
+               Ada_getit (inf, "/f");
+             else
+               break;          /* from switch */
+             continue;         /* advance char */
+           case 'p':
+             if (!packages_only && nocase_tail ("procedure"))
+               Ada_getit (inf, "/p");
+             else if (nocase_tail ("package"))
+               Ada_getit (inf, "/s");
+             else if (nocase_tail ("protected")) /* protected type */
+               Ada_getit (inf, "/t");
+             else
+               break;          /* from switch */
+             continue;         /* advance char */
+           case 't':
+             if (!packages_only && nocase_tail ("task"))
+               Ada_getit (inf, "/k");
+             else if (typedefs && !packages_only && nocase_tail ("type"))
+               {
+                 Ada_getit (inf, "/t");
+                 while (*dbp != '\0')
+                   dbp += 1;
+               }
+             else
+               break;          /* from switch */
+             continue;         /* advance char */
+           }
+
+         /* Look for the end of the token. */
+         while (!endtoken (*dbp))
+           dbp++;
+
+       } /* advance char */
+    } /* advance line */
+}
+
+\f
+/*
+ * Unix and microcontroller assembly tag handling
+ * Labels:  /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
+ * Idea by Bob Weiner, Motorola Inc. (1994)
+ */
+static void
+Asm_labels (inf)
+     FILE *inf;
+{
+  register char *cp;
+
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
+    {
+      /* 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 == ':' || iswhite (*cp))
+           {
+             /* Found end of label, so copy it and add it to the table. */
+             pfnote (savenstr(lb.buffer, cp-lb.buffer), TRUE,
+                     lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+           }
+       }
+    }
+}
+
+\f
+/*
+ * Perl support
+ * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
+ * Perl variable names: /^(my|local).../
+ * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
+ * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
+ * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
+ */
+static void
+Perl_functions (inf)
+     FILE *inf;
+{
+  char *package = savestr ("main"); /* current package name */
+  register char *cp;
+
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
+    {
+      skip_spaces(cp);
+
+      if (LOOKING_AT (cp, "package"))
+       {
+         free (package);
+         package = get_tag (cp);
+         if (package == NULL)  /* can't parse package name */
+           package = savestr ("");
+         else
+           package = savestr(package); /* make a copy */
+       }
+      else if (LOOKING_AT (cp, "sub"))
+       {
+         char *name, *fullname, *pos;
+         char *sp = cp;
+
+         while (!notinname (*cp))
+           cp++;
+         if (cp == sp)
+           continue;
+         name = savenstr (sp, cp-sp);
+         if ((pos = etags_strchr (name, ':')) != NULL && pos[1] == ':')
+           fullname = name;
+         else
+           fullname = concat (package, "::", name);
+         pfnote (fullname, TRUE,
+                 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+         if (name != fullname)
+           free (name);
+       }
+       else if (globals                /* only if tagging global vars is enabled */
+               && (LOOKING_AT (cp, "my") || LOOKING_AT (cp, "local")))
+       {
+         /* After "my" or "local", but before any following paren or space. */
+         char *varname = NULL;
+
+         if (*cp == '$' || *cp == '@' || *cp == '%')
+           {
+             char* varstart = ++cp;
+             while (ISALNUM (*cp) || *cp == '_')
+               cp++;
+             varname = savenstr (varstart, cp-varstart);
+           }
+         else
+           {
+             /* Should be examining a variable list at this point;
+                could insist on seeing an open parenthesis. */
+             while (*cp != '\0' && *cp != ';' && *cp != '=' &&  *cp != ')')
+               cp++;
+           }
+
+         /* Perhaps I should back cp up one character, so the TAGS table
+            doesn't mention (and so depend upon) the following char. */
+         pfnote (varname, FALSE,
+                 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+       }
+    }
+}
+
+
+/*
+ * Python support
+ * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
+ * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
+ * More ideas by seb bacon <seb@jamkit.com> (2002)
+ */
+static void
+Python_functions (inf)
+     FILE *inf;
+{
+  register char *cp;
+
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
+    {
+      cp = skip_spaces (cp);
+      if (LOOKING_AT (cp, "def") || LOOKING_AT (cp, "class"))
+       {
+         char *name = cp;
+         while (!notinname (*cp) && *cp != ':')
+           cp++;
+         pfnote (savenstr (name, cp-name), TRUE,
+                 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+       }
+    }
+}
+
+\f
+/*
+ * PHP support
+ * Look for:
+ *  - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
+ *  - /^[ \t]*class[ \t\n]+[^ \t\n]+/
+ *  - /^[ \t]*define\(\"[^\"]+/
+ * Only with --members:
+ *  - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
+ * Idea by Diez B. Roggisch (2001)
+ */
+static void
+PHP_functions (inf)
+     FILE *inf;
+{
+  register char *cp, *name;
+  bool search_identifier = FALSE;
+
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
+    {
+      cp = skip_spaces (cp);
+      name = cp;
+      if (search_identifier
+         && *cp != '\0')
+       {
+         while (!notinname (*cp))
+           cp++;
+         pfnote (savenstr (name, cp-name), TRUE,
+                 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+         search_identifier = FALSE;
+       }
+      else if (LOOKING_AT (cp, "function"))
+       {
+         if(*cp == '&')
+           cp = skip_spaces (cp+1);
+         if(*cp != '\0')
            {
-             while (isspace (*dbp))
-               dbp++;
-             if (*dbp == '\0')
-               continue;
-             if (tail ("precision"))
-               break;
-             continue;
+             name = cp;
+             while (!notinname (*cp))
+               cp++;
+             pfnote (savenstr (name, cp-name), TRUE,
+                     lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
            }
-         break;
+         else
+           search_identifier = TRUE;
        }
-      while (isspace (*dbp))
-       dbp++;
-      if (*dbp == '\0')
-       continue;
-      switch (lowcase (*dbp))
+      else if (LOOKING_AT (cp, "class"))
        {
-       case 'f':
-         if (tail ("function"))
-           getit (inf);
-         continue;
-       case 's':
-         if (tail ("subroutine"))
-           getit (inf);
-         continue;
-       case 'e':
-         if (tail ("entry"))
-           getit (inf);
-         continue;
-       case 'p':
-         if (tail ("program"))
+         if (*cp != '\0')
            {
-             getit (inf);
-             continue;
+             name = cp;
+             while (*cp != '\0' && !iswhite (*cp))
+               cp++;
+             pfnote (savenstr (name, cp-name), FALSE,
+                     lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
            }
-         if (tail ("procedure"))
-           getit (inf);
-         continue;
+         else
+           search_identifier = TRUE;
+       }
+      else if (strneq (cp, "define", 6)
+              && (cp = skip_spaces (cp+6))
+              && *cp++ == '('
+              && (*cp == '"' || *cp == '\''))
+       {
+         char quote = *cp++;
+         name = cp;
+         while (*cp != quote && *cp != '\0')
+           cp++;
+         pfnote (savenstr (name, cp-name), FALSE,
+                 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+       }
+      else if (members
+              && LOOKING_AT (cp, "var")
+              && *cp == '$')
+       {
+         name = cp;
+         while (!notinname(*cp))
+           cp++;
+         pfnote (savenstr (name, cp-name), FALSE,
+                 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
        }
     }
 }
+
 \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]'
+ * Cobol tag functions
+ * We could look for anything that could be a paragraph name.
+ * i.e. anything that starts in column 8 is one word and ends in a full stop.
+ * Idea by Corny de Souza (1993)
  */
-void
-Asm_labels (inf)
+static void
+Cobol_paragraphs (inf)
      FILE *inf;
 {
-  register char *cp;
-
-  lineno = 0;
-  charno = 0;
+  register char *bp, *ep;
 
-  while (!feof (inf))
+  LOOP_ON_INPUT_LINES (inf, lb, bp)
     {
-      lineno++;
-      linecharno = charno;
-      charno += readline (&lb, inf);
-      cp = lb.buffer;
+      if (lb.len < 9)
+       continue;
+      bp += 8;
 
-      /* 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. */
-             pfnote ((CTAGS) ? savenstr(lb.buffer, cp-lb.buffer) : NULL, TRUE,
-                     lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-           }
-       }
+      /* If eoln, compiler option or comment ignore whole line. */
+      if (bp[-1] != ' ' || !ISALNUM (bp[0]))
+        continue;
+
+      for (ep = bp; ISALNUM (*ep) || *ep == '-'; ep++)
+       continue;
+      if (*ep++ == '.')
+       pfnote (savenstr (bp, ep-bp), TRUE,
+               lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
     }
 }
+
 \f
 /*
- * Perl support by Bart Robinson <lomew@cs.utah.edu>
- * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
+ * Makefile support
+ * Idea by Assar Westerlund <assar@sics.se> (2001)
  */
-void
-Perl_functions (inf)
+static void
+Makefile_targets (inf)
      FILE *inf;
 {
-  register char *cp;
+  register char *bp;
 
-  lineno = 0;
-  charno = 0;
-
-  while (!feof (inf))
+  LOOP_ON_INPUT_LINES (inf, lb, bp)
     {
-      lineno++;
-      linecharno = charno;
-      charno += readline (&lb, inf);
-      cp = lb.buffer;
-
-      if (*cp++ == 's' && *cp++ == 'u' && *cp++ == 'b' && isspace(*cp++))
-       {
-         while (*cp && isspace(*cp))
-           cp++;
-         while (*cp && ! isspace(*cp) && *cp != '{')
-           cp++;
-         pfnote ((CTAGS) ? savenstr (lb.buffer, cp-lb.buffer) : NULL, TRUE,
-                 lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-       }
+      if (*bp == '\t' || *bp == '#')
+       continue;
+      while (*bp != '\0' && *bp != '=' && *bp != ':')
+       bp++;
+      if (*bp == ':')
+       pfnote (savenstr (lb.buffer, bp - lb.buffer), TRUE,
+               lb.buffer, bp - lb.buffer + 1, lineno, linecharno);
     }
 }
-\f
-/* Added by Mosur Mohan, 4/22/88 */
-/* Pascal parsing                */
 
+\f
 /*
+ * Pascal parsing
+ * Original code by Mosur K. Mohan (1989)
+ *
  *  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
+static void
 Pascal_functions (inf)
      FILE *inf;
 {
-  struct linebuffer tline;     /* mostly copied from C_entries */
+  linebuffer tline;            /* mostly copied from C_entries */
   long save_lcno;
   int save_lineno, save_len;
   char c, *cp, *namebuf;
 
-  logical                      /* each of these flags is TRUE iff: */
+  bool                         /* each of these flags is TRUE iff: */
     incomment,                 /* point is inside a comment */
     inquote,                   /* point is inside '..' string */
     get_tagname,               /* point is after PROCEDURE/FUNCTION
@@ -2965,11 +4415,12 @@ Pascal_functions (inf)
                                   is a FORWARD/EXTERN to be ignored, or
                                   whether it is a real tag */
 
+  save_lcno = save_lineno = save_len = 0; /* keep compiler quiet */
+  namebuf = NULL;              /* keep compiler quiet */
   lineno = 0;
   charno = 0;
   dbp = lb.buffer;
   *dbp = '\0';
-  save_len = 0;
   initbuffer (&tline);
 
   incomment = inquote = FALSE;
@@ -2978,8 +4429,8 @@ Pascal_functions (inf)
   inparms = FALSE;             /* found '(' after "proc"            */
   verify_tag = FALSE;          /* check if "extern" is ahead        */
 
-  /* long main loop to get next char */
-  while (!feof (inf))
+
+  while (!feof (inf))          /* long main loop to get next char */
     {
       c = *dbp++;
       if (c == '\0')           /* if end of line */
@@ -2990,8 +4441,8 @@ Pascal_functions (inf)
          dbp = lb.buffer;
          if (*dbp == '\0')
            continue;
-         if (!((found_tag && verify_tag) ||
-               get_tagname))
+         if (!((found_tag && verify_tag)
+               || get_tagname))
            c = *dbp++;         /* only if don't need *dbp pointing
                                   to the beginning of the name of
                                   the procedure or function */
@@ -3050,7 +4501,7 @@ Pascal_functions (inf)
            continue;
          if (lowcase (*dbp == 'e'))
            {
-             if (tail ("extern"))      /* superfluous, really! */
+             if (nocase_tail ("extern")) /* superfluous, really! */
                {
                  found_tag = FALSE;
                  verify_tag = FALSE;
@@ -3058,7 +4509,7 @@ Pascal_functions (inf)
            }
          else if (lowcase (*dbp) == 'f')
            {
-             if (tail ("forward"))     /*  check for forward reference */
+             if (nocase_tail ("forward")) /*  check for forward reference */
                {
                  found_tag = FALSE;
                  verify_tag = FALSE;
@@ -3079,15 +4530,15 @@ Pascal_functions (inf)
            continue;
 
          /* save all values for later tagging */
-         GROW_LINEBUFFER (tline, strlen (lb.buffer) + 1);
+         linebuffer_setlen (&tline, lb.len);
          strcpy (tline.buffer, lb.buffer);
          save_lineno = lineno;
          save_lcno = linecharno;
 
          /* grab block name */
-         for (cp = dbp + 1; *cp && (!endtoken (*cp)); cp++)
+         for (cp = dbp + 1; *cp != '\0' && !endtoken (*cp); cp++)
            continue;
-         namebuf = (CTAGS) ? savenstr (dbp, cp-dbp) : NULL;
+         namebuf = savenstr (dbp, cp-dbp);
          dbp = cp;             /* set dbp to e-o-token */
          save_len = dbp - lb.buffer + 1;
          get_tagname = FALSE;
@@ -3102,11 +4553,11 @@ Pascal_functions (inf)
          switch (lowcase (c))
            {
            case 'p':
-             if (tail ("rocedure"))    /* c = 'p', dbp has advanced */
+             if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
                get_tagname = TRUE;
              continue;
            case 'f':
-             if (tail ("unction"))
+             if (nocase_tail ("unction"))
                get_tagname = TRUE;
              continue;
            }
@@ -3115,173 +4566,131 @@ Pascal_functions (inf)
 
   free (tline.buffer);
 }
+
 \f
 /*
- * lisp tag functions
+ * Lisp tag functions
  *  look for (def or (DEF, quote or QUOTE
  */
-int
-L_isdef (strp)
-     register char *strp;
-{
-  return ((strp[1] == 'd' || strp[1] == 'D')
-         && (strp[2] == 'e' || strp[2] == 'E')
-         && (strp[3] == 'f' || strp[3] == 'F'));
-}
 
-int
-L_isquote (strp)
-     register char *strp;
-{
-  return ((*(++strp) == 'q' || *strp == 'Q')
-         && (*(++strp) == 'u' || *strp == 'U')
-         && (*(++strp) == 'o' || *strp == 'O')
-         && (*(++strp) == 't' || *strp == 'T')
-         && (*(++strp) == 'e' || *strp == 'E')
-         && isspace(*(++strp)));
-}
+static void L_getit __P((void));
 
-void
+static void
 L_getit ()
 {
-  register char *cp;
-
   if (*dbp == '\'')            /* Skip prefix quote */
     dbp++;
-  else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
+  else if (*dbp == '(')
   {
-    dbp += 7;
-    while (isspace(*dbp))
-      dbp++;
+    dbp++;
+    /* Try to skip "(quote " */
+    if (!LOOKING_AT (dbp, "quote") && !LOOKING_AT (dbp, "QUOTE"))
+      /* Ok, then skip "(" before name in (defstruct (foo)) */
+      dbp = skip_spaces (dbp);
   }
-  for (cp = dbp /*+1*/;
-       *cp && *cp != '(' && *cp != ' ' && *cp != ')';
-       cp++)
-    continue;
-  if (cp == dbp)
-    return;
-
-  pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
-         lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+  get_tag (dbp);
 }
 
-void
+static void
 Lisp_functions (inf)
      FILE *inf;
 {
-  lineno = 0;
-  charno = 0;
-
-  while (!feof (inf))
+  LOOP_ON_INPUT_LINES (inf, lb, dbp)
     {
-      lineno++;
-      linecharno = charno;
-      charno += readline (&lb, inf);
-      dbp = lb.buffer;
-      if (dbp[0] == '(')
+      if (dbp[0] != '(')
+       continue;
+
+      if (strneq (dbp+1, "def", 3) || strneq (dbp+1, "DEF", 3))
        {
-         if (L_isdef (dbp))
-           {
-             while (!isspace (*dbp))
-               dbp++;
-             while (isspace (*dbp))
-               dbp++;
-             L_getit ();
-           }
-         else
+         dbp = skip_non_spaces (dbp);
+         dbp = skip_spaces (dbp);
+         L_getit ();
+       }
+      else
+       {
+         /* Check for (foo::defmumble name-defined ... */
+         do
+           dbp++;
+         while (!notinname (*dbp) && *dbp != ':');
+         if (*dbp == ':')
            {
-             /* Check for (foo::defmumble name-defined ... */
              do
                dbp++;
-             while (*dbp && !isspace (*dbp)
-                    && *dbp != ':' && *dbp != '(' && *dbp != ')');
-             if (*dbp == ':')
-               {
-                 do
-                   dbp++;
-                 while (*dbp == ':');
+             while (*dbp == ':');
 
-                 if (L_isdef (dbp - 1))
-                   {
-                     while (!isspace (*dbp))
-                       dbp++;
-                     while (isspace (*dbp))
-                       dbp++;
-                     L_getit ();
-                   }
+             if (strneq (dbp, "def", 3) || strneq (dbp, "DEF", 3))
+               {
+                 dbp = skip_non_spaces (dbp);
+                 dbp = skip_spaces (dbp);
+                 L_getit ();
                }
            }
        }
     }
 }
+
+\f
+/*
+ * Postscript tag functions
+ * Just look for lines where the first character is '/'
+ * Also look at "defineps" for PSWrap
+ * Ideas by:
+ *   Richard Mlynarik <mly@adoc.xerox.com> (1997)
+ *   Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
+ */
+static void
+Postscript_functions (inf)
+     FILE *inf;
+{
+  register char *bp, *ep;
+
+  LOOP_ON_INPUT_LINES (inf, lb, bp)
+    {
+      if (bp[0] == '/')
+       {
+         for (ep = bp+1;
+              *ep != '\0' && *ep != ' ' && *ep != '{';
+              ep++)
+           continue;
+         pfnote (savenstr (bp, ep-bp), TRUE,
+                 lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
+       }
+      else if (LOOKING_AT (bp, "defineps"))
+       get_tag (bp);
+    }
+}
+
 \f
 /*
  * Scheme tag functions
  * look for (def... xyzzy
- * look for (def... (xyzzy
- * look for (def ... ((...(xyzzy ....
- * look for (set! xyzzy
+ *          (def... (xyzzy
+ *          (def ... ((...(xyzzy ....
+ *          (set! xyzzy
+ * Original code by Ken Haase (1985?)
  */
 
-void get_scheme ();
-
-void
+static void
 Scheme_functions (inf)
      FILE *inf;
 {
-  lineno = 0;
-  charno = 0;
+  register char *bp;
 
-  while (!feof (inf))
+  LOOP_ON_INPUT_LINES (inf, lb, bp)
     {
-      lineno++;
-      linecharno = charno;
-      charno += readline (&lb, inf);
-      dbp = lb.buffer;
-      if (dbp[0] == '(' &&
-         (dbp[1] == 'D' || dbp[1] == 'd') &&
-         (dbp[2] == 'E' || dbp[2] == 'e') &&
-         (dbp[3] == 'F' || dbp[3] == 'f'))
+      if (strneq (bp, "(def", 4) || strneq (bp, "(DEF", 4))
        {
-         while (!isspace (*dbp))
-           dbp++;
+         bp = skip_non_spaces (bp+4);
          /* Skip over open parens and white space */
-         while (*dbp && (isspace (*dbp) || *dbp == '('))
-           dbp++;
-         get_scheme ();
-       }
-      if (dbp[0] == '(' &&
-         (dbp[1] == 'S' || dbp[1] == 's') &&
-         (dbp[2] == 'E' || dbp[2] == 'e') &&
-         (dbp[3] == 'T' || dbp[3] == 't') &&
-         (dbp[4] == '!' || dbp[4] == '!') &&
-         (isspace (dbp[5])))
-       {
-         while (!isspace (*dbp))
-           dbp++;
-         /* Skip over white space */
-         while (isspace (*dbp))
-           dbp++;
-         get_scheme ();
+         while (notinname (*bp))
+           bp++;
+         get_tag (bp);
        }
+      if (LOOKING_AT (bp, "(SET!") || LOOKING_AT (bp, "(set!"))
+       get_tag (bp);
     }
 }
 
-void
-get_scheme ()
-{
-  register char *cp;
-
-  if (*dbp == '\0')
-    return;
-  /* Go till you get to white space or a syntactic break */
-  for (cp = dbp + 1;
-       *cp && *cp != '(' && *cp != ')' && !isspace (*cp);
-       cp++)
-    continue;
-  pfnote ((CTAGS) ? savenstr (dbp, cp-dbp) : NULL, TRUE,
-         lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-}
 \f
 /* Find tags in TeX and LaTeX input files.  */
 
@@ -3294,37 +4703,32 @@ struct TEX_tabent
   int len;
 };
 
-struct TEX_tabent *TEX_toktab = NULL;  /* Table with tag tokens */
+static 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.  */
 
-char *TEX_defenv = "\
+static char *TEX_defenv = "\
 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
 :part:appendix:entry:index";
 
-void TEX_mode ();
-struct TEX_tabent *TEX_decode_env ();
-int TEX_Token ();
-#if TeX_named_tokens
-void TEX_getit ();
-#endif
+static void TEX_mode __P((FILE *));
+static struct TEX_tabent *TEX_decode_env __P((char *, char *));
+static int TEX_Token __P((char *));
 
-char TEX_esc = '\\';
-char TEX_opgrp = '{';
-char TEX_clgrp = '}';
+static char TEX_esc = '\\';
+static char TEX_opgrp = '{';
+static char TEX_clgrp = '}';
 
 /*
  * TeX/LaTeX scanning loop.
  */
-void
-TeX_functions (inf)
+static void
+TeX_commands (inf)
      FILE *inf;
 {
-  char *lasthit;
-
-  lineno = 0;
-  charno = 0;
+  char *cp, *lasthit;
+  register int i;
 
   /* Select either \ or ! as escape character.  */
   TEX_mode (inf);
@@ -3333,30 +4737,31 @@ TeX_functions (inf)
   if (!TEX_toktab)
     TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
 
-  while (!feof (inf))
-    {                          /* Scan each line in file */
-      lineno++;
-      linecharno = charno;
-      charno += readline (&lb, inf);
-      dbp = lb.buffer;
-      lasthit = dbp;
-      while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
+    {
+      lasthit = cp;
+      /* Look at each esc in line. */
+      while ((cp = etags_strchr (cp, TEX_esc)) != NULL)
        {
-         register int i;
-
-         if (!*(++dbp))
+         if (*++cp == '\0')
            break;
-         linecharno += dbp - lasthit;
-         lasthit = dbp;
+         linecharno += cp - lasthit;
+         lasthit = cp;
          i = TEX_Token (lasthit);
-         if (0 <= i)
+         if (i >= 0)
            {
-             pfnote (NULL, TRUE,
-                     lb.buffer, strlen (lb.buffer), lineno, linecharno);
-#if TeX_named_tokens
-             TEX_getit (lasthit, TEX_toktab[i].len);
-#endif
-             break;            /* We only save a line once */
+             register char *p;
+             for (lasthit += TEX_toktab[i].len;
+                  *lasthit == TEX_esc || *lasthit == TEX_opgrp;
+                  lasthit++)
+               continue;
+             for (p = lasthit;
+                  !iswhite (*p) && *p != TEX_opgrp && *p != TEX_clgrp;
+                  p++)
+               continue;
+             pfnote (savenstr (lasthit, p-lasthit), TRUE,
+                     lb.buffer, lb.len, lineno, linecharno);
+             break;            /* We only tag a line once */
            }
        }
     }
@@ -3368,7 +4773,7 @@ TeX_functions (inf)
 
 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
    chars accordingly. */
-void
+static void
 TEX_mode (inf)
      FILE *inf;
 {
@@ -3396,12 +4801,14 @@ TEX_mode (inf)
       TEX_opgrp = '<';
       TEX_clgrp = '>';
     }
+  /* If the input file is compressed, inf is a pipe, and rewind may fail.
+     No attempt is made to correct the situation. */
   rewind (inf);
 }
 
 /* Read environment and prepend it to the default string.
    Build token table. */
-struct TEX_tabent *
+static struct TEX_tabent *
 TEX_decode_env (evarname, defenv)
      char *evarname;
      char *defenv;
@@ -3416,11 +4823,14 @@ TEX_decode_env (evarname, defenv)
   if (!env)
     env = defenv;
   else
-    env = concat (env, defenv, "");
+    {
+      char *oldenv = env;
+      env = concat (oldenv, defenv, "");
+    }
 
   /* Allocate a token table */
   for (size = 1, p = env; p;)
-    if ((p = etags_strchr (p, ':')) && *(++p))
+    if ((p = etags_strchr (p, ':')) && *++p != '\0')
       size++;
   /* Add 1 to leave room for null terminator.  */
   tab = xnew (size + 1, struct TEX_tabent);
@@ -3450,34 +4860,12 @@ TEX_decode_env (evarname, defenv)
   return tab;
 }
 
-#if TeX_named_tokens
-/* 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;
-     int len;
-{
-  char *p = name + len;
-
-  if (*name == '\0')
-    return;
-
-  /* Let tag name extend to next group close (or end of line) */
-  while (*p && *p != TEX_clgrp)
-    p++;
-  pfnote (savenstr (name, p-name), TRUE,
-         lb.buffer, strlen (lb.buffer), lineno, linecharno);
-}
-#endif
-
 /* If the text at CP matches one of the tag-defining TeX command names,
    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
+   Keep the capital `T' in `token' for dumb truncating compilers
    (this distinguishes it from `TEX_toktab' */
-int
+static int
 TEX_Token (cp)
      char *cp;
 {
@@ -3488,21 +4876,43 @@ TEX_Token (cp)
       return i;
   return -1;
 }
+
+\f
+/* Texinfo support.  Dave Love, Mar. 2000.  */
+static void
+Texinfo_nodes (inf)
+     FILE * inf;
+{
+  char *cp, *start;
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
+    if (LOOKING_AT (cp, "@node"))
+      {
+       start = cp;
+       while (*cp != '\0' && *cp != ',')
+         cp++;
+       pfnote (savenstr (start, cp - start), TRUE,
+               lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+      }
+}
+
 \f
 /*
- * Prolog support (rewritten) by Anders Lindgren, Mar. 96
+ * Prolog support
  *
- * Assumes that the predicate starts at column 0.
- * Only the first clause of a predicate is added. 
+ * Assumes that the predicate or rule starts at column 0.
+ * Only the first clause of a predicate or rule is added.
+ * Original code by Sunichirou Sugou (1989)
+ * Rewritten by Anders Lindgren (1996)
  */
-void
+static int prolog_pr __P((char *, char *));
+static void prolog_skip_comment __P((linebuffer *, FILE *));
+static int prolog_atom __P((char *, int));
+
+static void
 Prolog_functions (inf)
      FILE *inf;
 {
-  int prolog_pred ();
-  void prolog_skip_comment ();
-
-  char * last;
+  char *cp, *last;
   int len;
   int allocated;
 
@@ -3510,41 +4920,33 @@ Prolog_functions (inf)
   len = 0;
   last = NULL;
 
-  lineno = 0;
-  linecharno = 0;
-  charno = 0;
-
-  while (!feof (inf))
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
     {
-      lineno++;
-      linecharno += charno;
-      charno = readline (&lb, inf);
-      dbp = lb.buffer;
-      if (dbp[0] == '\0')      /* Empty line */
+      if (cp[0] == '\0')       /* Empty line */
        continue;
-      else if (isspace (dbp[0])) /* Not a predicate */
+      else if (iswhite (cp[0])) /* Not a predicate */
        continue;
-      else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
-       prolog_skip_comment (&lb, inf, &lineno, &linecharno);
-      else if (len = prolog_pred (dbp, last)) 
+      else if (cp[0] == '/' && cp[1] == '*')   /* comment. */
+       prolog_skip_comment (&lb, inf);
+      else if ((len = prolog_pr (cp, last)) > 0)
        {
-         /* Predicate.  Store the function name so that we only
-          * generates a tag for the first clause.  */
+         /* Predicate or rule.  Store the function name so that we
+            only generate a tag for the first clause.  */
          if (last == NULL)
            last = xnew(len + 1, char);
          else if (len + 1 > allocated)
-           last = (char *) xrealloc(last, len + 1);
+           xrnew (last, len + 1, char);
          allocated = len + 1;
-         strncpy (last, dbp, len);
+         strncpy (last, cp, len);
          last[len] = '\0';
        }
     }
 }
 
 
-void
+static void
 prolog_skip_comment (plb, inf)
-     struct linebuffer *plb;
+     linebuffer *plb;
      FILE *inf;
 {
   char *cp;
@@ -3561,49 +4963,43 @@ prolog_skip_comment (plb, inf)
 }
 
 /*
- * A predicate definition is added if it matches:
+ * A predicate or rule definition is added if it matches:
  *     <beginning of line><Prolog Atom><whitespace>(
+ * or  <beginning of line><Prolog Atom><whitespace>:-
  *
  * It is added to the tags database if it doesn't match the
  * name of the previous clause header.
  *
- * Return the size of the name of the predicate, or 0 if no header
- * was found.
+ * Return the size of the name of the predicate or rule, or 0 if no
+ * header was found.
  */
-int
-prolog_pred (s, last)
+static int
+prolog_pr (s, last)
      char *s;
      char *last;               /* Name of last clause. */
 {
-  int prolog_atom();
-  int prolog_white();
-
   int pos;
   int len;
 
-  pos = prolog_atom(s, 0);
+  pos = prolog_atom (s, 0);
   if (pos < 1)
     return 0;
 
   len = pos;
-  pos += prolog_white(s, pos);
-
-  if ((s[pos] == '(') || (s[pos] == '.'))
-    {
-      if (s[pos] == '(')
-       pos++;
-
-      /* Save only the first clause. */
-      if ((last == NULL) ||
-         (len != strlen(last)) ||
-         (strncmp(s, last, len) != 0))
+  pos = skip_spaces (s + pos) - s;
+
+  if ((s[pos] == '.'
+       || (s[pos] == '(' && (pos += 1))
+       || (s[pos] == ':' && s[pos + 1] == '-' && (pos += 2)))
+      && (last == NULL         /* save only the first clause */
+         || len != strlen (last)
+         || !strneq (s, last, len)))
        {
-         pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
-                 s, pos, lineno, linecharno);
+         pfnote (savenstr (s, len), TRUE, s, pos, lineno, linecharno);
          return len;
        }
-    }
-  return 0;
+  else
+    return 0;
 }
 
 /*
@@ -3615,7 +5011,7 @@ prolog_pred (s, last)
  * - A quoted arbitrary string. Single quotes can escape themselves.
  *   Backslash quotes everything.
  */
-int
+static int
 prolog_atom (s, pos)
      char *s;
      int pos;
@@ -3624,11 +5020,11 @@ prolog_atom (s, pos)
 
   origpos = pos;
 
-  if (islower(s[pos]) || (s[pos] == '_'))
+  if (ISLOWER(s[pos]) || (s[pos] == '_'))
     {
       /* The atom is unquoted. */
       pos++;
-      while (isalnum(s[pos]) || (s[pos] == '_'))
+      while (ISALNUM(s[pos]) || (s[pos] == '_'))
        {
          pos++;
        }
@@ -3638,7 +5034,7 @@ prolog_atom (s, pos)
     {
       pos++;
 
-      while (1) 
+      while (1)
        {
          if (s[pos] == '\'')
            {
@@ -3665,37 +5061,23 @@ prolog_atom (s, pos)
     return -1;
 }
 
-/* Consume whitespace.  Return the number of bytes eaten. */
-int
-prolog_white (s, pos)
-     char *s;
-     int pos;
-{
-  int origpos;
-
-  origpos = pos;
-
-  while (isspace(s[pos]))
-    pos++;
-
-  return pos - origpos;
-}
 \f
-/* 
- * Support for Erlang  --  Anders Lindgren, Feb 1996.
+/*
+ * Support for Erlang
  *
  * Generates tags for functions, defines, and records.
- *
  * Assumes that Erlang functions start at column 0.
+ * Original code by Anders Lindgren (1996)
  */
-void
+static int erlang_func __P((char *, char *));
+static void erlang_attribute __P((char *));
+static int erlang_atom __P((char *, int));
+
+static void
 Erlang_functions (inf)
      FILE *inf;
 {
-  int erlang_func ();
-  void erlang_attribute ();
-
-  char * last;
+  char *cp, *last;
   int len;
   int allocated;
 
@@ -3703,41 +5085,33 @@ Erlang_functions (inf)
   len = 0;
   last = NULL;
 
-  lineno = 0;
-  linecharno = 0;
-  charno = 0;
-
-  while (!feof (inf))
+  LOOP_ON_INPUT_LINES (inf, lb, cp)
     {
-      lineno++;
-      linecharno += charno;
-      charno = readline (&lb, inf);
-      dbp = lb.buffer;
-      if (dbp[0] == '\0')      /* Empty line */
+      if (cp[0] == '\0')       /* Empty line */
        continue;
-      else if (isspace (dbp[0])) /* Not function nor attribute */
+      else if (iswhite (cp[0])) /* Not function nor attribute */
        continue;
-      else if (dbp[0] == '%')  /* comment */
+      else if (cp[0] == '%')   /* comment */
        continue;
-      else if (dbp[0] == '"')  /* Sometimes, strings start in column one */
+      else if (cp[0] == '"')   /* Sometimes, strings start in column one */
        continue;
-      else if (dbp[0] == '-')  /* attribute, e.g. "-define" */
+      else if (cp[0] == '-')   /* attribute, e.g. "-define" */
        {
-         erlang_attribute(dbp);
+         erlang_attribute (cp);
          last = NULL;
        }
-      else if (len = erlang_func (dbp, last)) 
+      else if ((len = erlang_func (cp, last)) > 0)
        {
-         /* 
+         /*
           * Function.  Store the function name so that we only
           * generates a tag for the first clause.
           */
          if (last == NULL)
-           last = xnew(len + 1, char);
+           last = xnew (len + 1, char);
          else if (len + 1 > allocated)
-           last = (char *) xrealloc(last, len + 1);
+           xrnew (last, len + 1, char);
          allocated = len + 1;
-         strncpy (last, dbp, len);
+         strncpy (last, cp, len);
          last[len] = '\0';
        }
     }
@@ -3754,42 +5128,37 @@ Erlang_functions (inf)
  * Return the size of the name of the function, or 0 if no function
  * was found.
  */
-int
+static int
 erlang_func (s, last)
      char *s;
      char *last;               /* Name of last clause. */
 {
-  int erlang_atom ();
-  int erlang_white ();
-
   int pos;
   int len;
 
-  pos = erlang_atom(s, 0);
+  pos = erlang_atom (s, 0);
   if (pos < 1)
     return 0;
 
   len = pos;
-  pos += erlang_white(s, pos);
+  pos = skip_spaces (s + pos) - s;
 
-  if (s[pos++] == '(')
-    {
-      /* Save only the first clause. */
-      if ((last == NULL) ||
-         (len != strlen(last)) ||
-         (strncmp(s, last, len) != 0))
+  /* Save only the first clause. */
+  if (s[pos++] == '('
+      && (last == NULL
+         || len != (int)strlen (last)
+         || !strneq (s, last, len)))
        {
-         pfnote ((CTAGS) ? savenstr (s, len) : NULL, TRUE,
-                 s, pos, lineno, linecharno);
+         pfnote (savenstr (s, len), TRUE, s, pos, lineno, linecharno);
          return len;
        }
-    }
+
   return 0;
 }
 
 
 /*
- * Handle attributes.  Currently, tags are generated for defines 
+ * Handle attributes.  Currently, tags are generated for defines
  * and records.
  *
  * They are on the form:
@@ -3797,31 +5166,22 @@ erlang_func (s, last)
  * -define(Foo(M, N), M+N).
  * -record(graph, {vtab = notable, cyclic = true}).
  */
-void
+static void
 erlang_attribute (s)
      char *s;
 {
-  int erlang_atom ();
-  int erlang_white ();
-
   int pos;
   int len;
 
-  if ((strncmp(s, "-define", 7) == 0) ||
-      (strncmp(s, "-record", 7) == 0))
+  if (LOOKING_AT (s, "-define") || LOOKING_AT (s, "-record"))
     {
-      pos = 7;
-      pos += erlang_white(s, pos);
-
-      if (s[pos++] == '(') 
+      if (s[pos++] == '(')
        {
-         pos += erlang_white(s, pos);
-       
-         if (len = erlang_atom(s, pos))
-           {
-             pfnote ((CTAGS) ? savenstr (& s[pos], len) : NULL, TRUE,
-                     s, pos + len, lineno, linecharno);
-           }
+         pos = skip_spaces (s + pos) - s;
+         len = erlang_atom (s, pos);
+         if (len != 0)
+           pfnote (savenstr (& s[pos], len), TRUE,
+                   s, pos + len, lineno, linecharno);
        }
     }
   return;
@@ -3832,7 +5192,7 @@ erlang_attribute (s)
  * Consume an Erlang atom (or variable).
  * Return the number of bytes consumed, or -1 if there was an error.
  */
-int
+static int
 erlang_atom (s, pos)
      char *s;
      int pos;
@@ -3841,11 +5201,11 @@ erlang_atom (s, pos)
 
   origpos = pos;
 
-  if (isalpha (s[pos]) || s[pos] == '_')
+  if (ISALPHA (s[pos]) || s[pos] == '_')
     {
       /* The atom is unquoted. */
       pos++;
-      while (isalnum (s[pos]) || s[pos] == '_')
+      while (ISALNUM (s[pos]) || s[pos] == '_')
        pos++;
       return pos - origpos;
     }
@@ -3853,7 +5213,7 @@ erlang_atom (s, pos)
     {
       pos++;
 
-      while (1) 
+      while (1)
        {
          if (s[pos] == '\'')
            {
@@ -3878,36 +5238,27 @@ erlang_atom (s, pos)
     return -1;
 }
 
-/* Consume whitespace.  Return the number of bytes eaten */
-int
-erlang_white (s, pos)
-     char *s;
-     int pos;
-{
-  int origpos;
-
-  origpos = pos;
-
-  while (isspace (s[pos]))
-    pos++;
-
-  return pos - origpos;
-}
 \f
 #ifdef ETAGS_REGEXPS
+
+static char *scan_separators __P((char *));
+static void analyse_regex __P((char *, bool));
+static void add_regex __P((char *, bool, language *));
+static char *substitute __P((char *, char *, struct re_registers *));
+
 /* 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 occurrence of
    an unquoted separator.  Also turns "\t" into a Tab character.
    Returns pointer to terminating separator.  Works in place.  Null
    terminates name string. */
-char *
+static char *
 scan_separators (name)
      char *name;
 {
   char sep = name[0];
   char *copyto = name;
-  logical quoted = FALSE;
+  bool quoted = FALSE;
 
   for (++name; *name != '\0'; ++name)
     {
@@ -3938,29 +5289,94 @@ scan_separators (name)
   return name;
 }
 
+/* Look at the argument of --regex or --no-regex and do the right
+   thing.  Same for each line of a regexp file. */
+static void
+analyse_regex (regex_arg, ignore_case)
+     char *regex_arg;
+     bool ignore_case;
+{
+  if (regex_arg == NULL)
+    {
+      free_patterns ();                /* --no-regex: remove existing regexps */
+      return;
+    }
+
+  /* A real --regexp option or a line in a regexp file. */
+  switch (regex_arg[0])
+    {
+      /* Comments in regexp file or null arg to --regex. */
+    case '\0':
+    case ' ':
+    case '\t':
+      break;
+
+      /* Read a regex file.  This is recursive and may result in a
+        loop, which will stop when the file descriptors are exhausted. */
+    case '@':
+      {
+       FILE *regexfp;
+       linebuffer regexbuf;
+       char *regexfile = regex_arg + 1;
+
+       /* regexfile is a file containing regexps, one per line. */
+       regexfp = fopen (regexfile, "r");
+       if (regexfp == NULL)
+         {
+           pfatal (regexfile);
+           return;
+         }
+       initbuffer (&regexbuf);
+       while (readline_internal (&regexbuf, regexfp) > 0)
+         analyse_regex (regexbuf.buffer, ignore_case);
+       free (regexbuf.buffer);
+       fclose (regexfp);
+      }
+      break;
+
+      /* Regexp to be used for a specific language only. */
+    case '{':
+      {
+       language *lang;
+       char *lang_name = regex_arg + 1;
+       char *cp;
+
+       for (cp = lang_name; *cp != '}'; cp++)
+         if (*cp == '\0')
+           {
+             error ("unterminated language name in regex: %s", regex_arg);
+             return;
+           }
+       *cp = '\0';
+       lang = get_language_from_langname (lang_name);
+       if (lang == NULL)
+         return;
+       add_regex (cp + 1, ignore_case, lang);
+      }
+      break;
+
+      /* Regexp to be used for any language. */
+    default:
+      add_regex (regex_arg, ignore_case, NULL);
+      break;
+    }
+}
+
 /* 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)
+static void
+add_regex (regexp_pattern, ignore_case, lang)
      char *regexp_pattern;
+     bool ignore_case;
+     language *lang;
 {
+  static struct re_pattern_buffer zeropattern;
   char *name;
   const char *err;
   struct re_pattern_buffer *patbuf;
+  pattern *pp;
 
-  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);
@@ -3969,16 +5385,15 @@ add_regex (regexp_pattern)
   name = scan_separators (regexp_pattern);
   if (regexp_pattern[0] == '\0')
     {
-      error ("null regexp", 0);
+      error ("null regexp", (char *)NULL);
       return;
     }
   (void) scan_separators (name);
 
   patbuf = xnew (1, struct re_pattern_buffer);
-  patbuf->translate = NULL;
-  patbuf->fastmap = NULL;
-  patbuf->buffer = NULL;
-  patbuf->allocated = 0;
+  *patbuf = zeropattern;
+  if (ignore_case)
+    patbuf->translate = lc_trans;      /* translation table to fold case  */
 
   err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
   if (err != NULL)
@@ -3987,114 +5402,162 @@ add_regex (regexp_pattern)
       return;
     }
 
-  num_patterns += 1;
-  if (num_patterns == 1)
-    patterns = xnew (1, struct pattern);
-  else
-    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;
+  pp = p_head;
+  p_head = xnew (1, pattern);
+  p_head->regex = savestr (regexp_pattern);
+  p_head->p_next = pp;
+  p_head->lang = lang;
+  p_head->pat = patbuf;
+  p_head->name_pattern = savestr (name);
+  p_head->error_signaled = FALSE;
+  p_head->ignore_case = ignore_case;
 }
 
 /*
  * Do the substitutions indicated by the regular expression and
  * arguments.
  */
-char *
+static char *
 substitute (in, out, regs)
      char *in, *out;
      struct re_registers *regs;
 {
-  char *result = NULL, *t;
-  int size = 0;
+  char *result, *t;
+  int size, dig, diglen;
+
+  result = NULL;
+  size = strlen (out);
+
+  /* Pass 1: figure out how much to allocate by finding all \N strings. */
+  if (out[size - 1] == '\\')
+    fatal ("pattern error in \"%s\"", out);
+  for (t = etags_strchr (out, '\\');
+       t != NULL;
+       t = etags_strchr (t + 2, '\\'))
+    if (ISDIGIT (t[1]))
+      {
+       dig = t[1] - '0';
+       diglen = regs->end[dig] - regs->start[dig];
+       size += diglen - 2;
+      }
+    else
+      size -= 1;
+
+  /* Allocate space and do the substitutions. */
+  result = xnew (size + 1, char);
+
+  for (t = result; *out != '\0'; out++)
+    if (*out == '\\' && ISDIGIT (*++out))
+      {
+       dig = *out - '0';
+       diglen = regs->end[dig] - regs->start[dig];
+       strncpy (t, in + regs->start[dig], diglen);
+       t += diglen;
+      }
+    else
+      *t++ = *out;
+  *t = '\0';
+
+  assert (t <= result + size && t - result == (int)strlen (result));
+
+  return result;
+}
 
-  /* Pass 1: figure out how much size to allocate. */
-  for (t = out; *t; ++t)
+/* Deallocate all patterns. */
+static void
+free_patterns ()
+{
+  pattern *pp;
+  while (p_head != NULL)
     {
-      if (*t == '\\')
-       {
-         ++t;
-         if (!*t)
-           {
-             fprintf (stderr, "%s: pattern substitution ends prematurely\n",
-                      progname);
-             return NULL;
-           }
-         if (isdigit (*t))
-           {
-             int dig = *t - '0';
-             size += regs->end[dig] - regs->start[dig];
-           }
-       }
+      pp = p_head->p_next;
+      free (p_head->regex);
+      free (p_head->name_pattern);
+      free (p_head);
+      p_head = pp;
     }
+  return;
+}
+#endif /* ETAGS_REGEXPS */
 
-  /* Allocate space and do the substitutions. */
-  result = xnew (size + 1, char);
-  size = 0;
-  for (; *out; ++out)
+\f
+static bool
+nocase_tail (cp)
+     char *cp;
+{
+  register int len = 0;
+
+  while (*cp != '\0' && lowcase (*cp) == lowcase (dbp[len]))
+    cp++, len++;
+  if (*cp == '\0' && !intoken (dbp[len]))
     {
-      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
-           result[size++] = *out;
-       }
-      else
-       result[size++] = *out;
+      dbp += len;
+      return TRUE;
     }
-  result[size] = '\0';
+  return FALSE;
+}
 
-  return result;
+static char *
+get_tag (bp)
+     register char *bp;
+{
+  register char *cp, *name;
+
+  if (*bp == '\0')
+    return NULL;
+  /* Go till you get to white space or a syntactic break */
+  for (cp = bp + 1; !notinname (*cp); cp++)
+    continue;
+  name = savenstr (bp, cp-bp);
+  pfnote (name, TRUE,
+         lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+  return name;
 }
-\f
-#endif /* ETAGS_REGEXPS */
+
 /* Initialize a linebuffer for use */
-void
-initbuffer (linebuffer)
-     struct linebuffer *linebuffer;
+static void
+initbuffer (lbp)
+     linebuffer *lbp;
 {
-  linebuffer->size = 200;
-  linebuffer->buffer = xnew (200, char);
+  lbp->size = (DEBUG) ? 3 : 200;
+  lbp->buffer = xnew (lbp->size, char);
+  lbp->buffer[0] = '\0';
+  lbp->len = 0;
 }
 
 /*
- * Read a line of text from `stream' into `linebuffer'.
- * Return the number of characters read from `stream',
- * which is the length of the line including the newline, if any.
+ * Read a line of text from `stream' into `lbp', excluding the
+ * newline or CR-NL, if any.  Return the number of characters read from
+ * `stream', which is the length of the line including the newline.
+ *
+ * On DOS or Windows we do not count the CR character, if any, before the
+ * NL, in the returned length; this mirrors the behavior of emacs on those
+ * platforms (for text files, it translates CR-NL to NL as it reads in the
+ * file).
  */
-long
-readline_internal (linebuffer, stream)
-     struct linebuffer *linebuffer;
+static long
+readline_internal (lbp, stream)
+     linebuffer *lbp;
      register FILE *stream;
 {
-  char *buffer = linebuffer->buffer;
-  register char *p = linebuffer->buffer;
+  char *buffer = lbp->buffer;
+  register char *p = lbp->buffer;
   register char *pend;
   int chars_deleted;
 
-  pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug.  */
+  pend = p + lbp->size;                /* Separate to avoid 386/IX compiler bug.  */
 
   while (1)
     {
       register int c = getc (stream);
       if (p == pend)
        {
-         linebuffer->size *= 2;
-         buffer = (char *) xrealloc (buffer, linebuffer->size);
-         p += buffer - linebuffer->buffer;
-         pend = buffer + linebuffer->size;
-         linebuffer->buffer = buffer;
+         /* We're at the end of linebuffer: expand it. */
+         lbp->size *= 2;
+         xrnew (buffer, lbp->size, char);
+         p += buffer - lbp->buffer;
+         pend = buffer + lbp->size;
+         lbp->buffer = buffer;
        }
       if (c == EOF)
        {
@@ -4106,7 +5569,7 @@ readline_internal (linebuffer, stream)
        {
          if (p > buffer && p[-1] == '\r')
            {
-             *--p = '\0';
+             p -= 1;
 #ifdef DOS_NT
             /* Assume CRLF->LF translation will be performed by Emacs
                when loading this file, so CRs won't appear in the buffer.
@@ -4120,100 +5583,202 @@ readline_internal (linebuffer, stream)
            }
          else
            {
-             *p = '\0';
              chars_deleted = 1;
            }
+         *p = '\0';
          break;
        }
       *p++ = c;
     }
+  lbp->len = p - buffer;
 
-  return p - buffer + chars_deleted;
+  return lbp->len + chars_deleted;
 }
 
 /*
- * Like readline_internal, above, but try to match the input
- * line against any existing regular expressions.
+ * Like readline_internal, above, but in addition try to match the
+ * input line against relevant regular expressions.
  */
-long
-readline (linebuffer, stream)
-     struct linebuffer *linebuffer;
+static long
+readline (lbp, stream)
+     linebuffer *lbp;
      FILE *stream;
 {
   /* Read new line. */
-  long result = readline_internal (linebuffer, stream);
-#ifdef ETAGS_REGEXPS
-  int i;
+  long result = readline_internal (lbp, stream);
 
-  /* Match against all listed patterns. */
-  for (i = 0; i < num_patterns; ++i)
+  /* Honour #line directives. */
+  if (!no_line_directive)
     {
-      int match = re_match (patterns[i].pattern, linebuffer->buffer,
-                           (int)result, 0, &patterns[i].regs);
-      switch (match)
+      static bool discard_until_line_directive;
+
+      /* Check whether this is a #line directive. */
+      if (result > 12 && strneq (lbp->buffer, "#line ", 6))
        {
-       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')
+         int start, lno;
+
+         if (DEBUG) start = 0; /* shut up the compiler */
+         if (sscanf (lbp->buffer, "#line %d \"%n", &lno, &start) == 1)
            {
-             /* Make a named tag. */
-             char *name = substitute (linebuffer->buffer,
-                                      patterns[i].name_pattern,
-                                      &patterns[i].regs);
-             if (name != NULL)
-               pfnote (name, TRUE,
-                       linebuffer->buffer, match, lineno, linecharno);
-           }
-         else
+             char *endp = lbp->buffer + start;
+
+             assert (start > 0);
+             while ((endp = etags_strchr (endp, '"')) != NULL
+                    && endp[-1] == '\\')
+               endp++;
+             if (endp != NULL)
+               /* Ok, this is a real #line directive.  Let's deal with it. */
+               {
+                 char *taggedabsname;  /* absolute name of original file */
+                 char *taggedfname;    /* name of original file as given */
+                 char *name;           /* temp var */
+
+                 discard_until_line_directive = FALSE; /* found it */
+                 name = lbp->buffer + start;
+                 *endp = '\0';
+                 canonicalize_filename (name); /* for DOS */
+                 taggedabsname = absolute_filename (name, curfdp->infabsdir);
+                 if (filename_is_absolute (name)
+                     || filename_is_absolute (curfdp->infname))
+                   taggedfname = savestr (taggedabsname);
+                 else
+                   taggedfname = relative_filename (taggedabsname,tagfiledir);
+
+                 if (streq (curfdp->taggedfname, taggedfname))
+                   /* The #line directive is only a line number change.  We
+                      deal with this afterwards. */
+                   free (taggedfname);
+                 else
+                   /* The tags following this #line directive should be
+                      attributed to taggedfname.  In order to do this, set
+                      curfdp accordingly. */
+                   {
+                     fdesc *fdp; /* file description pointer */
+
+                     /* Go look for a file description already set up for the
+                        file indicated in the #line directive.  If there is
+                        one, use it from now until the next #line
+                        directive. */
+                     for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
+                       if (streq (fdp->infname, curfdp->infname)
+                           && streq (fdp->taggedfname, taggedfname))
+                         /* If we remove the second test above (after the &&)
+                            then all entries pertaining to the same file are
+                            coalesced in the tags file.  If we use it, then
+                            entries pertaining to the same file but generated
+                            from different files (via #line directives) will
+                            go into separate sections in the tags file.  These
+                            alternatives look equivalent.  The first one
+                            destroys some apparently useless information. */
+                         {
+                           curfdp = fdp;
+                           free (taggedfname);
+                           break;
+                         }
+                     /* Else, if we already tagged the real file, skip all
+                        input lines until the next #line directive. */
+                     if (fdp == NULL) /* not found */
+                       for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
+                         if (streq (fdp->infabsname, taggedabsname))
+                           {
+                             discard_until_line_directive = TRUE;
+                             free (taggedfname);
+                             break;
+                           }
+                     /* Else create a new file description and use that from
+                        now on, until the next #line directive. */
+                     if (fdp == NULL) /* not found */
+                       {
+                         fdp = fdhead;
+                         fdhead = xnew (1, fdesc);
+                         *fdhead = *curfdp; /* copy curr. file description */
+                         fdhead->next = fdp;
+                         fdhead->infname = savestr (curfdp->infname);
+                         fdhead->infabsname = savestr (curfdp->infabsname);
+                         fdhead->infabsdir = savestr (curfdp->infabsdir);
+                         fdhead->taggedfname = taggedfname;
+                         fdhead->usecharno = FALSE;
+                         curfdp = fdhead;
+                       }
+                   }
+                 free (taggedabsname);
+                 lineno = lno;
+                 return readline (lbp, stream);
+               } /* if a real #line directive */
+           } /* if #line is followed by a a number */
+       } /* if line begins with "#line " */
+
+      /* If we are here, no #line directive was found. */
+      if (discard_until_line_directive)
+       {
+         if (result > 0)
+           /* Do a tail recursion on ourselves, thus discarding the contents
+              of the line buffer. */
+           return readline (lbp, stream);
+         /* End of file. */
+         discard_until_line_directive = FALSE;
+         return 0;
+       }
+    } /* if #line directives should be considered */
+
+#ifdef ETAGS_REGEXPS
+  {
+    int match;
+    pattern *pp;
+
+    /* Match against relevant patterns. */
+    if (lbp->len > 0)
+      for (pp = p_head; pp != NULL; pp = pp->p_next)
+       {
+         /* Only use generic regexps or those for the current language. */
+         if (pp->lang != NULL && pp->lang != fdhead->lang)
+           continue;
+
+         match = re_match (pp->pat, lbp->buffer, lbp->len, 0, &pp->regs);
+         switch (match)
            {
-             /* Make an unnamed tag. */
-             pfnote (NULL, TRUE,
-                     linebuffer->buffer, match, lineno, linecharno);
+           case -2:
+             /* Some error. */
+             if (!pp->error_signaled)
+               {
+                 error ("error while matching \"%s\"", pp->regex);
+                 pp->error_signaled = TRUE;
+               }
+             break;
+           case -1:
+             /* No match. */
+             break;
+           default:
+             /* Match occurred.  Construct a tag. */
+             if (pp->name_pattern[0] != '\0')
+               {
+                 /* Make a named tag. */
+                 char *name = substitute (lbp->buffer,
+                                          pp->name_pattern, &pp->regs);
+                 if (name != NULL)
+                   pfnote (name, TRUE, lbp->buffer, match, lineno, linecharno);
+               }
+             else
+               {
+                 /* Make an unnamed tag. */
+                 pfnote ((char *)NULL, TRUE,
+                         lbp->buffer, match, lineno, linecharno);
+               }
+             break;
            }
-         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;
-{
-  lineno = 0;
-  charno = 0;
-
-  while (!feof (inf))
-    {
-      ++lineno;
-      linecharno = charno;
-      charno += readline (&lb, inf) + 1;
-    }
-}
-
 \f
 /*
  * Return a pointer to a space of size strlen(cp)+1 allocated
  * with xnew where the string CP has been copied.
  */
-char *
+static char *
 savestr (cp)
      char *cp;
 {
@@ -4224,7 +5789,7 @@ savestr (cp)
  * Return a pointer to a space of size LEN+1 allocated with xnew where
  * the string CP has been copied for at most the first LEN characters.
  */
-char *
+static char *
 savenstr (cp, len)
      char *cp;
      int len;
@@ -4241,13 +5806,14 @@ savenstr (cp, len)
  * Return the ptr in sp at which the character c last
  * appears; NULL if not found
  *
- * Identical to System V strrchr, included for portability.
+ * Identical to POSIX strrchr, included for portability.
  */
-char *
+static char *
 etags_strrchr (sp, c)
-     register char *sp, c;
+     register const char *sp;
+     register int c;
 {
-  register char *r;
+  register const char *r;
 
   r = NULL;
   do
@@ -4255,28 +5821,68 @@ etags_strrchr (sp, c)
       if (*sp == c)
        r = sp;
   } while (*sp++);
-  return r;
+  return (char *)r;
 }
 
-
 /*
  * Return the ptr in sp at which the character c first
  * appears; NULL if not found
  *
- * Identical to System V strchr, included for portability.
+ * Identical to POSIX strchr, included for portability.
  */
-char *
+static char *
 etags_strchr (sp, c)
-     register char *sp, c;
+     register const char *sp;
+     register int c;
 {
   do
     {
       if (*sp == c)
-       return sp;
+       return (char *)sp;
     } while (*sp++);
   return NULL;
 }
 
+/*
+ * Return TRUE if the two strings are equal, ignoring case for alphabetic
+ * characters.
+ *
+ * Analogous to BSD's strcasecmp, included for portability.
+ */
+static bool
+strcaseeq (s1, s2)
+     register const char *s1;
+     register const char *s2;
+{
+  while (*s1 != '\0'
+        && (ISALPHA (*s1) && ISALPHA (*s2)
+            ? lowcase (*s1) == lowcase (*s2)
+            : *s1 == *s2))
+    s1++, s2++;
+
+  return (*s1 == *s2);
+}
+
+/* Skip spaces, return new pointer. */
+static char *
+skip_spaces (cp)
+     char *cp;
+{
+  while (iswhite (*cp))
+    cp++;
+  return cp;
+}
+
+/* Skip non spaces, return new pointer. */
+static char *
+skip_non_spaces (cp)
+     char *cp;
+{
+  while (*cp != '\0' && !iswhite (*cp))
+    cp++;
+  return cp;
+}
+
 /* Print error message and exit.  */
 void
 fatal (s1, s2)
@@ -4286,7 +5892,7 @@ fatal (s1, s2)
   exit (BAD);
 }
 
-void
+static void
 pfatal (s1)
      char *s1;
 {
@@ -4294,18 +5900,24 @@ pfatal (s1)
   exit (BAD);
 }
 
-void
+static void
 suggest_asking_for_help ()
 {
-  fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
-          progname);
+  fprintf (stderr, "\tTry `%s %s' for a complete list of options.\n",
+          progname,
+#ifdef LONG_OPTIONS
+          "--help"
+#else
+          "-h"
+#endif
+          );
   exit (BAD);
 }
 
 /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
-void
+static void
 error (s1, s2)
-     char *s1, *s2;
+     const char *s1, *s2;
 {
   fprintf (stderr, "%s: ", progname);
   fprintf (stderr, s1, s2);
@@ -4314,7 +5926,7 @@ error (s1, s2)
 
 /* Return a newly-allocated string whose contents
    concatenate those of s1, s2, s3.  */
-char *
+static char *
 concat (s1, s2, s3)
      char *s1, *s2, *s3;
 {
@@ -4328,36 +5940,32 @@ concat (s1, s2, s3)
 
   return result;
 }
+
 \f
 /* Does the same work as the system V getcwd, but does not need to
    guess the buffer size in advance. */
-char *
+static char *
 etags_getcwd ()
 {
 #ifdef HAVE_GETCWD
   int bufsize = 200;
   char *path = xnew (bufsize, char);
-  char *p;
 
   while (getcwd (path, bufsize) == NULL)
     {
       if (errno != ERANGE)
        pfatal ("getcwd");
       bufsize *= 2;
+      free (path);
       path = xnew (bufsize, char);
     }
 
-  /* Convert backslashes to slashes.  */
-#if WINDOWSNT
-  for (p = path; *p != '\0'; p++)
-    if (*p == '\\')
-      *p = '/';
-#endif
-
+  canonicalize_filename (path);
   return path;
 
 #else /* not HAVE_GETCWD */
-#ifdef MSDOS
+#if MSDOS
+
   char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS.  */
 
   getwd (path);
@@ -4370,7 +5978,7 @@ etags_getcwd ()
 
   return strdup (path);
 #else /* not MSDOS */
-  struct linebuffer path;
+  linebuffer path;
   FILE *pipe;
 
   initbuffer (&path);
@@ -4384,62 +5992,64 @@ etags_getcwd ()
 #endif /* 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 *
+/* Return a newly allocated string containing the file name of FILE
+   relative to the absolute directory DIR (which should end with a slash). */
+static char *
 relative_filename (file, dir)
      char *file, *dir;
 {
-  char *fp, *dp, *abs, *res;
+  char *fp, *dp, *afn, *res;
+  int i;
 
-  /* Find the common root of file and dir. */
-  abs = absolute_filename (file, cwd);
-  fp = abs;
+  /* Find the common root of file and dir (with a trailing slash). */
+  afn = absolute_filename (file, cwd);
+  fp = afn;
   dp = dir;
   while (*fp++ == *dp++)
     continue;
-  do
-    {
-      fp--;
-      dp--;
-    }
+  fp--, dp--;                  /* back to the first differing char */
+#ifdef DOS_NT
+  if (fp == afn && afn[0] != '/') /* cannot build a relative name */
+    return afn;
+#endif
+  do                           /* look at the equal chars until '/' */
+    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, "../", "");
-    }
+  /* Build a sequence of "../" strings for the resulting relative file name. */
+  i = 0;
+  while ((dp = etags_strchr (dp + 1, '/')) != NULL)
+    i += 1;
+  res = xnew (3*i + strlen (fp + 1) + 1, char);
+  res[0] = '\0';
+  while (i-- > 0)
+    strcat (res, "../");
 
-  /* Add the filename relative to the common root of file and dir. */
-  res = concat (res, fp + 1, "");
-  free (abs);
+  /* Add the file name relative to the common root of file and dir. */
+  strcat (res, fp + 1);
+  free (afn);
 
   return res;
 }
 
-/* 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;
+/* Return a newly allocated string containing the absolute file name
+   of FILE given DIR (which should end with a slash). */
+static char *
+absolute_filename (file, dir)
+     char *file, *dir;
 {
   char *slashp, *cp, *res;
 
-  if (absolutefn (file))
-    res = concat (file, "", "");
+  if (filename_is_absolute (file))
+    res = savestr (file);
 #ifdef DOS_NT
-  /* We don't support non-absolute filenames with a drive
+  /* We don't support non-absolute file names with a drive
      letter, like `d:NAME' (it's too much hassle).  */
   else if (file[1] == ':')
-    fatal ("%s: relative filenames with drive letters not supported", file);
+    fatal ("%s: relative file names with drive letters not supported", file);
 #endif
   else
-    res = concat (cwd, file, "");
+    res = concat (dir, file, "");
 
   /* Delete the "/dirname/.." and "/." substrings. */
   slashp = etags_strchr (res, '/');
@@ -4453,25 +6063,17 @@ absolute_filename (file, cwd)
              cp = slashp;
              do
                cp--;
-             while (cp >= res && !absolutefn (cp));
-             if (*cp == '/')
-               {
-                 strcpy (cp, slashp + 3);
-               }
+             while (cp >= res && !filename_is_absolute (cp));
+             if (cp < res)
+               cp = slashp;    /* the absolute name begins with "/.." */
 #ifdef DOS_NT
              /* Under MSDOS and NT we get `d:/NAME' as absolute
-                filename, so the luser could say `d:/../NAME'.
+                file name, so the luser could say `d:/../NAME'.
                 We silently treat this as `d:/NAME'.  */
-             else if (cp[1] == ':')
-               strcpy (cp + 3, slashp + 4);
+             else if (cp[0] != '/')
+               cp = slashp;
 #endif
-             else              /* else (cp == res) */
-               {
-                 if (slashp[3] != '\0')
-                   strcpy (cp, slashp + 4);
-                 else
-                   return ".";
-               }
+             strcpy (cp, slashp + 3);
              slashp = cp;
              continue;
            }
@@ -4485,55 +6087,108 @@ absolute_filename (file, cwd)
       slashp = etags_strchr (slashp + 1, '/');
     }
 
-  return res;
+  if (res[0] == '\0')
+    return savestr ("/");
+  else
+    return res;
 }
 
 /* Return a newly allocated string containing the absolute
-   filename of dir where FILE resides given CWD (which should
+   file name of dir where FILE resides given DIR (which should
    end with a slash). */
-char *
-absolute_dirname (file, cwd)
-     char *file, *cwd;
+static char *
+absolute_dirname (file, dir)
+     char *file, *dir;
 {
   char *slashp, *res;
   char save;
-#ifdef DOS_NT
-  char *p;
-
-  for (p = file; *p != '\0'; p++)
-    if (*p == '\\')
-      *p = '/';
-#endif
 
+  canonicalize_filename (file);
   slashp = etags_strrchr (file, '/');
   if (slashp == NULL)
-    return cwd;
+    return savestr (dir);
   save = slashp[1];
   slashp[1] = '\0';
-  res = absolute_filename (file, cwd);
+  res = absolute_filename (file, dir);
   slashp[1] = save;
 
   return res;
 }
 
+/* Whether the argument string is an absolute file name.  The argument
+   string must have been canonicalized with canonicalize_filename. */
+static bool
+filename_is_absolute (fn)
+     char *fn;
+{
+  return (fn[0] == '/'
+#ifdef DOS_NT
+         || (ISALPHA(fn[0]) && fn[1] == ':' && fn[2] == '/')
+#endif
+         );
+}
+
+/* Translate backslashes into slashes.  Works in place. */
+static void
+canonicalize_filename (fn)
+     register char *fn;
+{
+#ifdef DOS_NT
+  /* Canonicalize drive letter case.  */
+  if (fn[0] != '\0' && fn[1] == ':' && ISLOWER (fn[0]))
+    fn[0] = upcase (fn[0]);
+  /* Convert backslashes to slashes.  */
+  for (; *fn != '\0'; fn++)
+    if (*fn == '\\')
+      *fn = '/';
+#else
+  /* No action. */
+  fn = NULL;                   /* shut up the compiler */
+#endif
+}
+
+/* Set the minimum size of a string contained in a linebuffer. */
+static void
+linebuffer_setlen (lbp, toksize)
+     linebuffer *lbp;
+     int toksize;
+{
+  while (lbp->size <= toksize)
+    {
+      lbp->size *= 2;
+      xrnew (lbp->buffer, lbp->size, char);
+    }
+  lbp->len = toksize;
+}
+
 /* Like malloc but get fatal error if memory is exhausted.  */
-long *
+static PTR
 xmalloc (size)
      unsigned int size;
 {
-  long *result = (long *) malloc (size);
+  PTR result = (PTR) malloc (size);
   if (result == NULL)
-    fatal ("virtual memory exhausted", 0);
+    fatal ("virtual memory exhausted", (char *)NULL);
   return result;
 }
 
-long *
+static PTR
 xrealloc (ptr, size)
      char *ptr;
      unsigned int size;
 {
-  long *result =  (long *) realloc (ptr, size);
+  PTR result = (PTR) realloc (ptr, size);
   if (result == NULL)
-    fatal ("virtual memory exhausted");
+    fatal ("virtual memory exhausted", (char *)NULL);
   return result;
 }
+
+/*
+ * Local Variables:
+ * c-indentation-style: gnu
+ * indent-tabs-mode: t
+ * tab-width: 8
+ * fill-column: 79
+ * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node")
+ * End:
+ */