XEmacs 21.4.17 "Jumbo Shrimp".
[chise/xemacs-chise.git.1] / lib-src / etags.c
index e9ce59f..d59e2e8 100644 (file)
@@ -1,5 +1,5 @@
-/* Tags file maker to go with GNU Emacs
-   Copyright (C) 1984, 87, 88, 89, 93, 94, 95, 98
+/* Tags file maker to go with GNU Emacs           -*- coding: latin-1 -*-
+   Copyright (C) 1984, 87, 88, 89, 93, 94, 95, 98, 99, 2000, 2001
    Free Software Foundation, Inc. and Ken Arnold
 
 This file is not considered part of GNU Emacs.
@@ -24,23 +24,57 @@ 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++.
+ * 1993        Francesco Potortì reorganised C and C++ based on work by Joe Wells.
+ * 1994        Regexp tags by Tom Tromey.
+ * 2001 Nested classes by Francesco Potortì based on work 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 13.7";
+char pot_etags_version[] = "@(#) pot revision number is 14.15";
 
 #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
+
+#if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
+# define P_(proto) proto
+#else
+# define P_(proto) ()
+#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 */
+#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
+# undef MSDOS
+# define MSDOS TRUE
 # include <fcntl.h>
 # include <sys/param.h>
 # include <io.h>
@@ -48,41 +82,37 @@ char pot_etags_version[] = "@(#) pot revision number is 13.7";
 #   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
-# ifdef HAVE_CONFIG_H
-#   undef HAVE_NTGUI
-# else
-#   define DOS_NT
+# undef HAVE_NTGUI
+# undef  DOS_NT
+# define DOS_NT
+# ifndef HAVE_GETCWD
 #   define HAVE_GETCWD
-# endif /* not HAVE_CONFIG_H */
-#endif /* WINDOWSNT */
-
-#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 */
-#endif /* HAVE_CONFIG_H */
-
-#if !defined (WINDOWSNT) && defined (STDC_HEADERS)
-#include <stdlib.h>
-#include <string.h>
-#endif
+# 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
-# ifdef HAVE_GETCWD
-    extern char *getcwd ();
+# if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
+    extern char *getcwd (char *buf, size_t size);
 # endif
 #endif /* HAVE_UNISTD_H */
 
@@ -95,6 +125,12 @@ char pot_etags_version[] = "@(#) pot revision number is 13.7";
 #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
@@ -130,53 +166,51 @@ char pot_etags_version[] = "@(#) pot revision number is 13.7";
 # define       BAD     1
 #endif
 
-/* C extensions. */
-#define C_PLPL 0x00001         /* C++ */
-#define C_STAR 0x00003         /* C* */
-#define C_JAVA 0x00005         /* JAVA */
-#define YACC   0x10000         /* yacc file */
-
-#define streq(s,t)     ((DEBUG && (s) == NULL && (t) == NULL   \
-                         && (abort (), 1)) || !strcmp (s, t))
-#define strneq(s,t,n)  ((DEBUG && (s) == NULL && (t) == NULL   \
-                         && (abort (), 1)) || !strncmp (s, t, n))
-
-#define lowcase(c)     tolower ((char)c)
+#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 CHARS 256              /* 2^sizeof(char) */
-#define CHAR(x)                ((unsigned int)x & (CHARS - 1))
+#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 ISALNUM(c)     isalnum (CHAR(c))
+#define ISALPHA(c)     isalpha (CHAR(c))
+#define ISDIGIT(c)     isdigit (CHAR(c))
+#define ISLOWER(c)     islower (CHAR(c))
+
+#define lowcase(c)     tolower (CHAR(c))
+#define upcase(c)      toupper (CHAR(c))
+
 
 /*
  *     xnew, xrnew -- allocate, reallocate storage
  *
  * SYNOPSIS:   Type *xnew (int n, Type);
- *             Type *xrnew (OldPointer, int n, Type);
+ *             void xrnew (OldPointer, int n, Type);
  */
-#ifdef chkmalloc
+#if DEBUG
 # include "chkmalloc.h"
 # define xnew(n,Type)    ((Type *) trace_malloc (__FILE__, __LINE__, \
                                                  (n) * sizeof (Type)))
-# define xrnew(op,n,Type) ((Type *) trace_realloc (__FILE__, __LINE__, \
-                                                  (op), (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) ((Type *) xrealloc ((op), (n) * sizeof (Type)))
+# define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
+                                       (char *) (op), (n) * sizeof (Type)))
 #endif
 
 typedef int bool;
 
-typedef void Lang_function ();
+typedef void Lang_function P_((FILE *));
 
 typedef struct
 {
   char *suffix;
-  int sufflen;
   char *command;               /* Takes one arg and decompresses to stdout */
 } compressor;
 
@@ -184,89 +218,11 @@ typedef struct
 {
   char *name;
   Lang_function *function;
+  char **filenames;
   char **suffixes;
   char **interpreters;
 } language;
 
-extern char *getenv ();
-
-/* Many compilers barf on this:
-       Lang_function Asm_labels;
-   so let's write it this way */
-void Asm_labels ();
-void C_entries ();
-void default_C_entries ();
-void plain_C_entries ();
-void Cjava_entries ();
-void Cobol_paragraphs ();
-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 Postscript_functions ();
-void Prolog_functions ();
-void Python_functions ();
-void Scheme_functions ();
-void TeX_functions ();
-void just_read_file ();
-
-compressor *get_compressor_from_suffix ();
-language *get_language_from_name ();
-language *get_language_from_interpreter ();
-language *get_language_from_suffix ();
-int total_size_of_entries ();
-long readline (), readline_internal ();
-#ifdef ETAGS_REGEXPS
-void analyse_regex ();
-void add_regex ();
-void free_patterns ();
-#endif /* ETAGS_REGEXPS */
-void error ();
-void suggest_asking_for_help ();
-void fatal (), pfatal ();
-void add_node ();
-
-void init ();
-void initbuffer ();
-void find_entries ();
-void free_tree ();
-void pfnote (), new_pfnote ();
-void process_file ();
-void put_entries ();
-void takeprec ();
-
-char *concat ();
-char *skip_spaces (), *skip_non_spaces ();
-char *savenstr (), *savestr ();
-char *etags_strchr (), *etags_strrchr ();
-char *etags_getcwd ();
-char *relative_filename (), *absolute_filename (), *absolute_dirname ();
-bool filename_is_absolute ();
-void canonicalize_filename ();
-void grow_linebuffer ();
-long *xmalloc (), *xrealloc ();
-
-\f
-char searchar = '/';           /* use /.../ searches */
-
-char *tagfile;                 /* output file */
-char *progname;                        /* name this program was invoked with */
-char *cwd;                     /* current working directory */
-char *tagfiledir;              /* directory of tagfile */
-FILE *tagf;                    /* ioptr for tags file */
-
-char *curfile;                 /* current input file name */
-language *curlang;             /* current language */
-
-int lineno;                    /* line number of current line */
-long charno;                   /* current character number */
-long linecharno;               /* charno of start of current line */
-char *dbp;                     /* pointer to start of current tag */
-
 typedef struct node_st
 {                              /* sorting structure            */
   char *name;                  /* function or type name        */
@@ -279,8 +235,6 @@ typedef struct node_st
   struct node_st *left, *right;        /* left and right sons          */
 } node;
 
-node *head;                    /* the head of the binary tree of tags */
-
 /*
  * A `linebuffer' is a structure which holds a line of text.
  * `readline_internal' reads a line from a stream into a linebuffer
@@ -295,19 +249,112 @@ typedef struct
   char *buffer;
 } linebuffer;
 
+/* 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 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 int number_len P_((long));
+
+static compressor *get_compressor_from_suffix P_((char *, char **));
+static language *get_language_from_langname P_((char *));
+static language *get_language_from_interpreter P_((char *));
+static language *get_language_from_filename P_((char *));
+static int total_size_of_entries P_((node *));
+static long readline P_((linebuffer *, FILE *));
+static long readline_internal P_((linebuffer *, FILE *));
+static void 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 find_entries P_((char *, FILE *));
+static void free_tree P_((node *));
+static void pfnote P_((char *, bool, char *, int, int, long));
+static void new_pfnote P_((char *, int, bool, char *, int, int, long));
+static void process_file P_((char *));
+static void put_entries P_((node *));
+static void takeprec P_((void));
+
+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 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));
+long *xmalloc P_((unsigned int));
+long *xrealloc P_((char *, unsigned int));
+
+\f
+char searchar = '/';           /* use /.../ searches */
+
+char *tagfile;                 /* output file */
+char *progname;                        /* name this program was invoked with */
+char *cwd;                     /* current working directory */
+char *tagfiledir;              /* directory of tagfile */
+FILE *tagf;                    /* ioptr for tags file */
+
+char *curfile;                 /* current input file name */
+language *curlang;             /* current language */
+
+int lineno;                    /* line number of current line */
+long charno;                   /* current character number */
+long linecharno;               /* charno of start of current line */
+char *dbp;                     /* pointer to start of current tag */
+
+node *head;                    /* the head of the binary tree of tags */
+
 linebuffer lb;                 /* the current line */
-linebuffer token_name;         /* used by C_entries as a temporary area */
-struct
-{
-  long linepos;
-  linebuffer lb;               /* used by C_entries instead of lb */
-} lbs[2];
 
 /* boolean "functions" (see init)      */
 bool _wht[CHARS], _nin[CHARS], _itk[CHARS], _btk[CHARS], _etk[CHARS];
 char
   /* white chars */
-  *white = " \f\t\n\r",
+  *white = " \f\t\n\r\v",
   /* not in a name */
   *nonam = " \f\t\n\r(=,[;",
   /* token ending chars */
@@ -319,13 +366,14 @@ char
 
 bool append_to_tagfile;                /* -a: append to tags */
 /* The following four default to TRUE for etags, but to FALSE for ctags.  */
-bool typedefs;                 /* -t: create tags for C typedefs */
-bool typedefs_and_cplusplus;   /* -T: create tags for C typedefs, level */
+bool typedefs;                 /* -t: create tags for C and Ada typedefs */
+bool typedefs_or_cplusplus;    /* -T: create tags for C typedefs, level */
                                /* 0 struct/enum/union decls, and C++ */
                                /* member functions. */
 bool constantypedefs;          /* -d: create tags for C #define, enum */
                                /* constants and variables. */
                                /* -D: opposite of -d.  Default under ctags. */
+bool declarations;             /* --declarations: tag them and extern in C&Co*/
 bool globals;                  /* create tags for global variables */
 bool members;                  /* create tags for C member variables */
 bool update;                   /* -u: update tags */
@@ -334,37 +382,41 @@ bool no_warnings;         /* -w: suppress warnings */
 bool cxref_style;              /* -x: create cxref style output */
 bool cplusplus;                        /* .[hc] means C++, not C */
 bool noindentypedefs;          /* -I: ignore indentation in C */
+bool packages_only;            /* --packages-only: in Ada, only tag packages*/
 
 #ifdef LONG_OPTIONS
 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'   },
-  { "no-defines",              no_argument,       NULL,     'D'   },
-  { "globals",                 no_argument,       &globals, TRUE  },
-  { "no-globals",              no_argument,       &globals, FALSE },
-  { "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'   },
-  { "members",                 no_argument,       &members, TRUE  },
-  { "no-members",              no_argument,       &members, FALSE },
-  { "no-warn",                 no_argument,       NULL,     'w'   },
-  { "output",                  required_argument, NULL,     'o'   },
-#ifdef ETAGS_REGEXPS  
-  { "regex",                   required_argument, NULL,     'r'   },
-  { "no-regex",                        no_argument,       NULL,     'R'   },
-#endif /* ETAGS_REGEXPS */  
-  { "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 }
+  { "packages-only",      no_argument,      &packages_only, TRUE  },
+  { "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'   },
+  { "declarations",      no_argument,       &declarations,  TRUE  },
+  { "no-defines",        no_argument,       NULL,           'D'   },
+  { "globals",           no_argument,       &globals,       TRUE  },
+  { "no-globals",        no_argument,       &globals,       FALSE },
+  { "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'   },
+  { "members",           no_argument,       &members,       TRUE  },
+  { "no-members",        no_argument,       &members,       FALSE },
+  { "no-warn",           no_argument,       NULL,           'w'   },
+  { "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 */
+  { "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'   },
+  { NULL }
 };
 #endif /* LONG_OPTIONS */
 
@@ -382,17 +434,22 @@ typedef struct pattern
   bool error_signaled;
 } pattern;
 
-/* Array of all regexps. */
+/* List of all regexps. */
 pattern *p_head = NULL;
+
+/* How many characters in the character set.  (From regex.c.)  */
+#define CHAR_SET_SIZE 256
+/* Translation table for case-insensitive matching. */
+char lc_trans[CHAR_SET_SIZE];
 #endif /* ETAGS_REGEXPS */
 
 compressor compressors[] =
 {
-  { "z", 1, "gzip -d -c"},
-  { "Z", 1, "gzip -d -c"},
-  { "gz", 2, "gzip -d -c"},
-  { "GZ", 2, "gzip -d -c"},
-  { "bz2", 3, "bzip2 -d -c" },
+  { "z", "gzip -d -c"},
+  { "Z", "gzip -d -c"},
+  { "gz", "gzip -d -c"},
+  { "GZ", "gzip -d -c"},
+  { "bz2", "bzip2 -d -c" },
   { NULL }
 };
 
@@ -403,6 +460,10 @@ compressor compressors[] =
 /* Non-NULL if language fixed. */
 language *forced_lang = NULL;
 
+/* Ada code */
+char *Ada_suffixes [] =
+  { "ads", "adb", "ada", NULL };
+
 /* Assembly code */
 char *Asm_suffixes [] = { "a", /* Unix assembler */
                          "asm", /* Microcontroller assembly */
@@ -416,12 +477,13 @@ char *Asm_suffixes [] = { "a",    /* Unix assembler */
                        };
 
 /* Note that .c and .h can be considered C++, if the --c++ flag was
-   given.  That is why default_C_entries is called here. */
+   given, or if the `class' keyowrd is met inside the file.
+   That is why default_C_entries is called for these. */
 char *default_C_suffixes [] =
   { "c", "h", NULL };
 
 char *Cplusplus_suffixes [] =
-  { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx",
+  { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
     "M",                       /* Objective C++ */
     "pdb",                     /* Postscript with C syntax */
     NULL };
@@ -442,7 +504,10 @@ char *Fortran_suffixes [] =
   { "F", "f", "f90", "for", NULL };
 
 char *Lisp_suffixes [] =
-  { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL };
+  { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL };
+
+char *Makefile_filenames [] =
+  { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL};
 
 char *Pascal_suffixes [] =
   { "p", "pas", NULL };
@@ -453,13 +518,13 @@ char *Perl_interpreters [] =
   { "perl", "@PERL@", NULL };
 
 char *plain_C_suffixes [] =
-  { "pc",                      /* Pro*C file */
+  { "lm",                      /* Objective lex file */
     "m",                       /* Objective C file */
-    "lm",                      /* Objective lex file */
+    "pc",                      /* Pro*C file */
      NULL };
 
 char *Postscript_suffixes [] =
-  { "ps", NULL };
+  { "ps", "psw", NULL };       /* .psw is for PSWrap */
 
 char *Prolog_suffixes [] =
   { "prolog", NULL };
@@ -469,13 +534,16 @@ char *Python_suffixes [] =
 
 /* Can't do the `SCM' or `scm' prefix with a version number. */
 char *Scheme_suffixes [] =
-  { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "ss", "t", NULL };
+  { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL };
 
 char *TeX_suffixes [] =
-  { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL };
+  { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL };
+
+char *Texinfo_suffixes [] =
+  { "texi", "texinfo", "txi", NULL };
 
 char *Yacc_suffixes [] =
-  { "y", "ym", "yy", "yxx", "y++", NULL }; /* .ym is Objective yacc file */
+  { "y", "y++", "ym", "yxx", "yy", NULL }; /* .ym is Objective yacc file */
 
 /*
  * Table of languages.
@@ -486,40 +554,47 @@ char *Yacc_suffixes [] =
 
 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              },
-  { "cobol",   Cobol_paragraphs,    Cobol_suffixes,       NULL              },
-  { "erlang",  Erlang_functions,    Erlang_suffixes,      NULL              },
-  { "fortran", Fortran_functions,   Fortran_suffixes,     NULL              },
-  { "java",    Cjava_entries,       Cjava_suffixes,       NULL              },
-  { "lisp",    Lisp_functions,      Lisp_suffixes,        NULL              },
-  { "pascal",  Pascal_functions,    Pascal_suffixes,      NULL              },
-  { "perl",    Perl_functions,      Perl_suffixes,        Perl_interpreters },
-  { "postscript", Postscript_functions, Postscript_suffixes, NULL           },
-  { "proc",    plain_C_entries,     plain_C_suffixes,     NULL              },
-  { "prolog",  Prolog_functions,    Prolog_suffixes,      NULL              },
-  { "python",  Python_functions,    Python_suffixes,      NULL              },
-  { "scheme",  Scheme_functions,    Scheme_suffixes,      NULL              },
-  { "tex",     TeX_functions,       TeX_suffixes,         NULL              },
-  { "yacc",    Yacc_entries,        Yacc_suffixes,        NULL              },
+  { "ada",               Ada_funcs,            NULL, Ada_suffixes,             NULL },
+  { "asm",               Asm_labels,           NULL, Asm_suffixes,             NULL },
+  { "c",                 default_C_entries,    NULL, default_C_suffixes,       NULL },
+  { "c++",               Cplusplus_entries,    NULL, Cplusplus_suffixes,       NULL },
+  { "c*",                Cstar_entries,        NULL, Cstar_suffixes,           NULL },
+  { "cobol",             Cobol_paragraphs,     NULL, Cobol_suffixes,           NULL },
+  { "erlang",            Erlang_functions,     NULL, Erlang_suffixes,          NULL },
+  { "fortran",           Fortran_functions,    NULL, Fortran_suffixes,         NULL },
+  { "java",              Cjava_entries,        NULL, Cjava_suffixes,           NULL },
+  { "lisp",              Lisp_functions,       NULL, Lisp_suffixes,            NULL },
+  { "makefile",   Makefile_targets,     Makefile_filenames, NULL,      NULL },
+  { "pascal",            Pascal_functions,     NULL, Pascal_suffixes,          NULL },
+  { "perl",              Perl_functions,     NULL, Perl_suffixes, Perl_interpreters },
+  { "postscript", Postscript_functions, NULL, Postscript_suffixes,     NULL },
+  { "proc",              plain_C_entries,      NULL, plain_C_suffixes,         NULL },
+  { "prolog",            Prolog_functions,     NULL, Prolog_suffixes,          NULL },
+  { "python",            Python_functions,     NULL, Python_suffixes,          NULL },
+  { "scheme",            Scheme_functions,     NULL, Scheme_suffixes,          NULL },
+  { "tex",               TeX_commands,         NULL, TeX_suffixes,             NULL },
+  { "texinfo",           Texinfo_nodes,        NULL, Texinfo_suffixes,         NULL },
+  { "yacc",              Yacc_entries,         NULL, Yacc_suffixes,            NULL },
   { "auto", NULL },             /* default guessing scheme */
   { "none", just_read_file },   /* regexp matching only */
   { NULL, NULL }                /* end of list */
 };
+
 \f
-void
+static void
 print_language_names ()
 {
   language *lang;
-  char **ext;
+  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);
@@ -531,23 +606,27 @@ 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.\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 "20"
+# 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) 1999 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 ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
@@ -568,13 +647,29 @@ Relative ones are stored relative to the output file's directory.");
   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\
@@ -607,12 +702,15 @@ Relative ones are stored relative to the output file's directory.");
 
 #ifdef ETAGS_REGEXPS
   puts ("-r /REGEXP/, --regex=/REGEXP/ or --regex=@regexfile\n\
-        Make a tag for each line matching pattern REGEXP in the\n\
-       following files.  regexfile is a file containing one REGEXP\n\
-       per line.  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\
+        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 */
@@ -627,7 +725,7 @@ 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.");
@@ -660,7 +758,7 @@ 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);
 }
@@ -670,7 +768,8 @@ enum argument_type
 {
   at_language,
   at_regexp,
-  at_filename
+  at_filename,
+  at_icregexp
 };
 
 /* This structure helps us allow mixing of --lang and file names. */
@@ -713,7 +812,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;
@@ -758,7 +857,7 @@ 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;
      bool *p_error;
@@ -839,17 +938,21 @@ main (argc, argv)
 #ifdef ETAGS_REGEXPS
   /* Set syntax for regular expression routines. */
   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, enum constants and
-   * global variables. 
+   * 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;
+      declarations = FALSE;
       members = FALSE;
     }
 
@@ -859,7 +962,7 @@ main (argc, argv)
       char *optstring;
 
 #ifdef ETAGS_REGEXPS
-      optstring = "-aCdDf:Il:o:r:RStTi:BuvxwVhH";
+      optstring = "-aCdDf:Il:o:r:c:RStTi:BuvxwVhH";
 #else
       optstring = "-aCdDf:Il:o:StTi:BuvxwVhH";
 #endif /* ETAGS_REGEXPS */
@@ -896,7 +999,7 @@ main (argc, argv)
        case 'o':
          if (tagfile)
            {
-             error ("-%c option may only be given once.", opt);
+             error ("-o option may only be given once.", (char *)NULL);
              suggest_asking_for_help ();
            }
          tagfile = optarg;
@@ -907,7 +1010,7 @@ main (argc, argv)
          break;
        case 'l':
          {
-           language *lang = get_language_from_name (optarg);
+           language *lang = get_language_from_langname (optarg);
            if (lang != NULL)
              {
                argbuffer[current_arg].lang = lang;
@@ -927,6 +1030,11 @@ main (argc, argv)
          argbuffer[current_arg].what = NULL;
          ++current_arg;
          break;
+        case 'c':
+         argbuffer[current_arg].arg_type = at_icregexp;
+         argbuffer[current_arg].what = optarg;
+         ++current_arg;
+         break;
 #endif /* ETAGS_REGEXPS */
        case 'V':
          print_version ();
@@ -939,7 +1047,7 @@ main (argc, argv)
          typedefs = TRUE;
          break;
        case 'T':
-         typedefs = typedefs_and_cplusplus = TRUE;
+         typedefs = typedefs_or_cplusplus = TRUE;
          break;
 #if (!CTAGS)
          /* Etags options */
@@ -969,7 +1077,7 @@ main (argc, argv)
 
   if (nincluded_files == 0 && file_count == 0)
     {
-      error ("no input files specified.", 0);
+      error ("no input files specified.", (char *)NULL);
       suggest_asking_for_help ();
     }
 
@@ -990,9 +1098,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)
@@ -1025,7 +1130,10 @@ main (argc, argv)
          break;
 #ifdef ETAGS_REGEXPS
        case at_regexp:
-         analyse_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:
@@ -1117,23 +1225,42 @@ main (argc, argv)
 
 
 /*
- * Return a compressor given the file name.
- * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca>
+ * 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)
  */
-compressor *
-get_compressor_from_suffix (file)
+static compressor *
+get_compressor_from_suffix (file, extptr)
      char *file;
+     char **extptr;
 {
   compressor *compr;
-  char *suffix;
+  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)
+  if (suffix == NULL || suffix < slash)
     return NULL;
+  if (extptr != NULL)
+    *extptr = suffix;
   suffix += 1;
-  for (compr = compressors; compr->suffix != NULL; compr++)
-    if (streq (compr->suffix, suffix))
-      return compr;
+  /* 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;
 }
 
@@ -1142,8 +1269,8 @@ get_compressor_from_suffix (file)
 /*
  * Return a language given the name.
  */
-language *
-get_language_from_name (name)
+static language *
+get_language_from_langname (name)
      char *name;
 {
   language *lang;
@@ -1165,7 +1292,7 @@ get_language_from_name (name)
 /*
  * Return a language given the interpreter name.
  */
-language *
+static language *
 get_language_from_interpreter (interpreter)
      char *interpreter;
 {
@@ -1188,13 +1315,21 @@ get_language_from_interpreter (interpreter)
 /*
  * Return a language given the file name.
  */
-language *
-get_language_from_suffix (file)
+static language *
+get_language_from_filename (file)
      char *file;
 {
   language *lang;
-  char **ext, *suffix;
+  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 (streq (*name, file))
+         return lang;
+
+  /* If not found, try suffix after last dot. */
   suffix = etags_strrchr (file, '.');
   if (suffix == NULL)
     return NULL;
@@ -1212,7 +1347,7 @@ get_language_from_suffix (file)
 /*
  * This routine is called on each file argument.
  */
-void
+static void
 process_file (file)
      char *file;
 {
@@ -1220,7 +1355,8 @@ process_file (file)
   FILE *inf;
   compressor *compr;
   char *compressed_name, *uncompressed_name;
-  char *real_name;
+  char *ext, *real_name;
+
 
   canonicalize_filename (file);
   if (streq (file, tagfile) && !streq (tagfile, "-"))
@@ -1228,7 +1364,7 @@ process_file (file)
       error ("skipping inclusion of %s in self.", file);
       return;
     }
-  if ((compr = get_compressor_from_suffix (file)) == NULL)
+  if ((compr = get_compressor_from_suffix (file, &ext)) == NULL)
     {
       compressed_name = NULL;
       real_name = uncompressed_name = savestr (file);
@@ -1236,8 +1372,29 @@ process_file (file)
   else
     {
       real_name = compressed_name = savestr (file);
-      uncompressed_name = savenstr (file, strlen(file) - compr->sufflen - 1);
+      uncompressed_name = savenstr (file, ext - file);
     }
+
+  /* If the canonicalised uncompressed name has already be dealt with,
+     skip it silently, else add it to the list. */
+  {
+    typedef struct processed_file
+    {
+      char *filename;
+      struct processed_file *next;
+    } processed_file;
+    static processed_file *pf_head = NULL;
+    register processed_file *fnp;
+
+    for (fnp = pf_head; fnp != NULL; fnp = fnp->next)
+      if (streq (uncompressed_name, fnp->filename))
+       goto exit;
+    fnp = pf_head;
+    pf_head = xnew (1, struct processed_file);
+    pf_head->filename = savestr (uncompressed_name);
+    pf_head->next = fnp;
+  }
+
   if (stat (real_name, &stat_buf) != 0)
     {
       /* Reset real_name and try with a different name. */
@@ -1253,7 +1410,26 @@ process_file (file)
            {
              compressed_name = concat (file, ".", compr->suffix);
              if (stat (compressed_name, &stat_buf) != 0)
-               free (compressed_name);
+               {
+                 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;
@@ -1276,10 +1452,10 @@ process_file (file)
   if (real_name == compressed_name)
     {
       char *cmd = concat (compr->command, " ", real_name);
-      inf = popen (cmd, "r");
+      inf = (FILE *) popen (cmd, "r");
       free (cmd);
     }
-  else 
+  else
     inf = fopen (real_name, "r");
   if (inf == NULL)
     {
@@ -1330,7 +1506,7 @@ process_file (file)
  * 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
+static void
 init ()
 {
   register char *sp;
@@ -1340,13 +1516,12 @@ init ()
     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;
-  for (sp = begtk; *sp != '\0'; sp++) begtoken (*sp) = TRUE;
-  for (sp = midtk; *sp != '\0'; sp++) intoken (*sp) = TRUE;
-  for (sp = endtk; *sp != '\0'; sp++) endtoken (*sp) = TRUE;
-  iswhite('\0') = iswhite('\n');
   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');
 }
 
@@ -1356,7 +1531,7 @@ init ()
  */
 node *last_node = NULL;
 
-void
+static void
 find_entries (file, inf)
      char *file;
      FILE *inf;
@@ -1382,7 +1557,7 @@ find_entries (file, inf)
     }
 
   /* Try to guess the language given the file name. */
-  lang = get_language_from_suffix (file);
+  lang = get_language_from_filename (file);
   if (lang != NULL && lang->function != NULL)
     {
       curlang = lang;
@@ -1427,7 +1602,7 @@ find_entries (file, inf)
 
   /* Try Fortran. */
   old_last_node = last_node;
-  curlang = get_language_from_name ("fortran");
+  curlang = get_language_from_langname ("fortran");
   Fortran_functions (inf);
 
   /* No Fortran entries found.  Try C. */
@@ -1436,14 +1611,15 @@ find_entries (file, inf)
       /* We do not tag if rewind fails.
         Only the file name will be recorded in the tags file. */
       rewind (inf);
-      curlang = get_language_from_name (cplusplus ? "c++" : "c");
+      curlang = get_language_from_langname (cplusplus ? "c++" : "c");
       default_C_entries (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 */
      bool is_func;             /* tag is a function */
@@ -1494,10 +1670,9 @@ pfnote (name, is_func, linestart, linelen, lno, cno)
   add_node (np, &head);
 }
 
-/* Date: Wed, 22 Jan 1997 02:56:31 -0500 [last amended 18 Sep 1997]
- * From: Sam Kendall <kendall@mv.mv.com>
- * Subject: Proposal for firming up the TAGS format specification
- * To: F.Potorti@cnuce.cnr.it
+/*
+ * 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(),;";
@@ -1513,7 +1688,7 @@ pfnote (name, is_func, linestart, linelen, lno, cno)
  * `nonam'.
  */
 #define traditional_tag_style TRUE
-void
+static void
 new_pfnote (name, namelen, is_func, linestart, linelen, lno, cno)
      char *name;               /* tag name, or NULL if unnamed */
      int namelen;              /* tag length */
@@ -1543,7 +1718,7 @@ new_pfnote (name, namelen, is_func, linestart, linelen, lno, cno)
            named = FALSE;      /* use unnamed tag */
        }
     }
-  
+
   if (named)
     name = savenstr (name, namelen);
   else
@@ -1555,7 +1730,7 @@ new_pfnote (name, namelen, is_func, linestart, linelen, lno, cno)
  * free_tree ()
  *     recurse on left children, iterate on right children.
  */
-void
+static void
 free_tree (np)
      register node *np;
 {
@@ -1580,7 +1755,7 @@ free_tree (np)
  *     add_node is the only function allowed to add nodes, so it can
  *     maintain state.
  */
-void
+static void
 add_node (np, cur_node_p)
      node *np, **cur_node_p;
 {
@@ -1637,8 +1812,9 @@ add_node (np, cur_node_p)
       add_node (np, dif < 0 ? &cur_node->left : &cur_node->right);
     }
 }
+
 \f
-void
+static void
 put_entries (np)
      register node *np;
 {
@@ -1705,7 +1881,7 @@ put_entries (np)
 }
 
 /* Length of a number's decimal representation. */
-int
+static int
 number_len (num)
      long num;
 {
@@ -1722,7 +1898,7 @@ number_len (num)
  * is irrelevant with the new tags.el, but is still supplied for
  * backward compatibility.
  */
-int
+static int
 total_size_of_entries (np)
      register node *np;
 {
@@ -1745,7 +1921,17 @@ total_size_of_entries (np)
 
   return total;
 }
+
 \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.
  */
@@ -1757,14 +1943,25 @@ enum sym_type
   st_C_ignore,
   st_C_javastruct,
   st_C_operator,
-  st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
+  st_C_class,
+  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
+   then put a `static' keyword in front of the in_word_set function.
 %[
 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
@@ -1775,11 +1972,12 @@ 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,         C_PLPL, 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
@@ -1795,7 +1993,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
@@ -1812,115 +2009,154 @@ 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 static. */
 /*%<*/
-/* C code produced by gperf version 2.5 (GNU 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 TOTAL_KEYWORDS 41
-#define MIN_WORD_LENGTH 3
+#define TOTAL_KEYWORDS 46
+#define MIN_WORD_LENGTH 2
 #define MAX_WORD_LENGTH 15
-#define MIN_HASH_VALUE 20
-#define MAX_HASH_VALUE 136
-/* maximum key range = 117, duplicates = 0 */
+#define MIN_HASH_VALUE 13
+#define MAX_HASH_VALUE 121
+/* maximum key range = 109, duplicates = 0 */
 
+#ifdef __GNUC__
+__inline
+#endif
 static unsigned int
 hash (str, len)
-     register char *str;
-     register int unsigned len;
+     register const char *str;
+     register unsigned int len;
 {
   static unsigned char asso_values[] =
     {
-     137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
-     137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
-     137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
-     137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
-     137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
-     137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
-     137, 137, 137, 137,  58, 137, 137, 137,  38,  37,
-      45, 137, 137, 137, 137, 137, 137, 137, 137, 137,
-      62, 137, 137,  14,  16, 137, 137, 137, 137, 137,
-     137, 137, 137, 137, 137, 137, 137,  26,  16,  51,
-      18,  61,   5,  19, 137,  23, 137, 137,  32,  63,
-      54,  10,  26, 137,  24,  42,  30,  18,  46, 137,
-     137, 137, 137, 137, 137, 137, 137, 137,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122,  57, 122, 122, 122,  55,   6,
+       60, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+       51, 122, 122,  10,   2, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122,   2,  52,  59,
+       49,  38,  56,  41, 122,  22, 122, 122,   9,  32,
+       33,  60,  26, 122,   1,  28,  46,  59,  44,  51,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
+      122, 122, 122, 122, 122, 122
     };
-  return len + asso_values[str[2]] + asso_values[str[0]];
+  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[] =
     {
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"",}, {"",}, 
-      {"float",        0,      st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"define",       0,      st_C_define},
-      {"bool",                 C_PLPL, st_C_typespec},
-      {"",}, {"",}, {"",}, 
-      {"friend",               C_PLPL, st_C_ignore},
-      {"SYSCALL",      0,      st_C_gnumacro},
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"double",       0,      st_C_typespec},
-      {"",}, {"",}, {"",}, 
-      {"union",        0,      st_C_struct},
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"import",               C_JAVA, st_C_ignore},
-      {"int",          0,      st_C_typespec},
-      {"short",        0,      st_C_typespec},
-      {"ENTRY",                0,      st_C_gnumacro},
-      {"implements",           C_JAVA, st_C_javastruct},
-      {"auto",         0,      st_C_typespec},
-      {"",}, 
-      {"interface",    C_JAVA, st_C_struct},
-      {"typedef",      0,      st_C_typedef},
-      {"typename",     C_PLPL, st_C_typespec},
-      {"",}, {"",}, 
-      {"signed",       0,      st_C_typespec},
-      {"unsigned",     0,      st_C_typespec},
-      {"",}, {"",}, {"",}, 
-      {"struct",       0,      st_C_struct},
-      {"void",         0,      st_C_typespec},
-      {"static",       0,      st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, 
-      {"operator",     C_PLPL, st_C_operator},
-      {"",}, 
-      {"char",         0,      st_C_typespec},
-      {"class",        C_PLPL, st_C_struct},
-      {"enum",         0,      st_C_enum},
-      {"package",      C_JAVA, st_C_ignore},
-      {"",}, 
-      {"volatile",     0,      st_C_typespec},
-      {"domain",       C_STAR, st_C_struct},
-      {"DEFUN",                0,      st_C_gnumacro},
-      {"",}, 
-      {"long",         0,      st_C_typespec},
-      {"@protocol",    0,      st_C_objprot},
-      {"",}, {"",}, {"",}, 
-      {"explicit",     C_PLPL, st_C_typespec},
-      {"",}, 
-      {"extern",       0,      st_C_typespec},
-      {"extends",      C_JAVA, st_C_javastruct},
-      {"",}, 
-      {"mutable",      C_PLPL, st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, 
-      {"PSEUDO",               0,      st_C_gnumacro},
-      {"",}, {"",}, {"",}, {"",}, 
-      {"const",        0,      st_C_typespec},
-      {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"@end",                 0,      st_C_objend},
-      {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"@interface",   0,      st_C_objprot},
-      {"",}, {"",}, {"",}, 
-      {"namespace",    C_PLPL, st_C_struct},
-      {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, 
-      {"@implementation", 0,   st_C_objimpl},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {""}, {""}, {""}, {""},
+      {"ENTRY",                0,      st_C_gnumacro},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {""},
+      {"if",           0,      st_C_ignore},
+      {""}, {""},
+      {"SYSCALL",      0,      st_C_gnumacro},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {"struct",       0,      st_C_struct},
+      {"static",       0,      st_C_typespec},
+      {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
+      {"long",         0,      st_C_typespec},
+      {""}, {""}, {""}, {""}, {""},
+      {"auto",         0,      st_C_typespec},
+      {"return",               0,      st_C_ignore},
+      {"import",               C_JAVA, st_C_ignore},
+      {""},
+      {"switch",               0,      st_C_ignore},
+      {""},
+      {"implements",   C_JAVA, st_C_javastruct},
+      {""},
+      {"for",          0,      st_C_ignore},
+      {"volatile",     0,      st_C_typespec},
+      {""},
+      {"PSEUDO",               0,      st_C_gnumacro},
+      {""},
+      {"char",         0,      st_C_typespec},
+      {"class",        0,      st_C_class},
+      {"@protocol",    0,      st_C_objprot},
+      {""}, {""},
+      {"void",         0,      st_C_typespec},
+      {"int",          0,      st_C_typespec},
+      {"explicit",     C_PLPL, st_C_typespec},
+      {""},
+      {"namespace",    C_PLPL, st_C_struct},
+      {"signed",       0,      st_C_typespec},
+      {""},
+      {"interface",    C_JAVA, st_C_struct},
+      {"while",                0,      st_C_ignore},
+      {"typedef",      0,      st_C_typedef},
+      {"typename",     C_PLPL, st_C_typespec},
+      {""}, {""}, {""},
+      {"friend",               C_PLPL, st_C_ignore},
+      {"mutable",      C_PLPL, st_C_typespec},
+      {"union",        0,      st_C_struct},
+      {"domain",       C_STAR, st_C_struct},
+      {""}, {""},
+      {"extern",       0,      st_C_extern},
+      {"extends",      C_JAVA, st_C_javastruct},
+      {"package",      C_JAVA, st_C_ignore},
+      {"short",        0,      st_C_typespec},
+      {"@end",         0,      st_C_objend},
+      {"unsigned",     0,      st_C_typespec},
+      {""},
+      {"const",        0,      st_C_typespec},
+      {""}, {""},
+      {"@interface",   0,      st_C_objprot},
+      {"enum",         0,      st_C_enum},
+      {""}, {""},
+      {"@implementation",0,    st_C_objimpl},
+      {""},
+      {"operator",     C_PLPL, st_C_operator},
+      {""}, {""}, {""}, {""},
+      {"define",       0,      st_C_define},
+      {""}, {""},
+      {"double",       0,      st_C_typespec},
+      {""},
+      {"bool",         C_PLPL, st_C_typespec},
+      {""}, {""}, {""},
+      {"DEFUN",                0,      st_C_gnumacro},
+      {"float",        0,      st_C_typespec}
     };
 
   if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
@@ -1929,9 +2165,9 @@ in_word_set (str, len)
 
       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];
         }
     }
@@ -1939,7 +2175,7 @@ in_word_set (str, len)
 }
 /*%>*/
 
-enum sym_type
+static enum sym_type
 C_symtype (str, len, c_ext)
      char *str;
      int len;
@@ -1951,14 +2187,17 @@ C_symtype (str, len, c_ext)
     return st_none;
   return se->type;
 }
+
 \f
- /*
-  * C functions and variables are recognized using a simple
-  * finite automaton.  fvdef is its state variable.
-  */
+/*
+ * C functions and variables are recognized using a simple
+ * finite automaton.  fvdef 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 */
@@ -1968,44 +2207,38 @@ enum
   vignore                      /* var-like: ignore until ';' */
 } fvdef;
 
+bool fvextern;                 /* func or var: extern keyword seen; */
 
- /*
-  * typedefs are recognized using a simple finite automaton.
-  * typdef is its state variable.
-  */
+/*
+ * typedefs are recognized using a simple finite automaton.
+ * typdef is its state variable.
+ */
 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.
-  */
+/*
+ * struct-like structures (enum, struct and union) are recognized
+ * using another simple finite automaton.  `structdef' is its state
+ * variable.
+ */
 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>";
@@ -2023,7 +2256,7 @@ enum
 
 /*
  * State machine for Objective C protocols and implementations.
- * Tom R.Hageman <tom@basil.icce.rug.nl>
+ * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
  */
 enum
 {
@@ -2046,34 +2279,115 @@ enum
  * 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.
  */
-typedef struct
+struct tok
 {
   bool valid;
-  char *str;
   bool named;
-  int linelen;
+  int offset;
+  int length;
   int lineno;
   long linepos;
-  char *buffer;
-} token;
-
-token tok;                     /* latest token read */
+  char *line;
+} token;                       /* latest token read */
+linebuffer token_name;         /* its name */
 
 /*
- * Set this to TRUE, and the next token considered is called a function.
- * Used only for GNU emacs's function-defining macros.
+ * Variables and functions for dealing with nested structures.
+ * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
  */
-bool next_token_is_func;
+static void pushclass_above P_((int, char *, int));
+static void popclass_above P_((int));
+static void write_classname P_((linebuffer *, char *qualifier));
+
+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;
 
-/*
- * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
- */
-bool yacc_rules;
+  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;
+}
 
-/*
- * methodlen is the length of the method name stored in token_name.
- */
-int methodlen;
+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 ()
@@ -2082,7 +2396,7 @@ int methodlen;
  *     is a struct/union/enum tag, or #define, or an enum constant.
  *
  *     *IS_FUNC gets TRUE iff the token is a function or #define macro
- *     with args.  C_EXT is which language we are looking at.
+ *     with args.  C_EXTP points to which language we are looking at.
  *
  * Globals
  *     fvdef                   IN OUT
@@ -2090,20 +2404,27 @@ int methodlen;
  *     definedef               IN OUT
  *     typdef                  IN OUT
  *     objdef                  IN OUT
- *     next_token_is_func      IN OUT
  */
 
-bool
-consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
+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 */
      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.
@@ -2112,6 +2433,11 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
     {
     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)
@@ -2149,28 +2475,36 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
       if (toktype == st_C_typedef)
        {
          if (typedefs)
-           typdef = ttypedseen;
+           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;
@@ -2179,11 +2513,6 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
     }
 
   /*
-   * 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.
@@ -2194,62 +2523,35 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
       if (structdef == stagseen)
         structdef = scolonseen;
       return FALSE;
+    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;
+      /* 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 and variables
-        that may be defined inside. */
-      if (structtype == st_C_struct)
-       structtag = savenstr (str, len);
-      else
-       structtag = "<enum>";
       structdef = stagseen;
       return TRUE;
     }
 
-  /* Avoid entering fvdef stuff if typdef is going on. */
   if (typdef != tnone)
-    {
-      definedef = dnone;
-      return FALSE;
-    }
-
-  /* Detect GNU macros.
-
-     DEFUN note for writers of emacs C code:
-      The DEFUN macro, used in emacs C source code, has a first arg
-     that is a string (the lisp function name), and a second arg that
-     is a C function name.  Since etags skips strings, the second arg
-     is tagged.  This is unfortunate, as it would be better to tag the
-     first arg.  The simplest way to deal with this problem would be
-     to name the tag with a name built from the function name, by
-     removing the initial 'F' character and substituting '-' for '_'.
-     Anyway, this assumes that the conventions of naming lisp
-     functions will never change.  Currently, this method is not
-     implemented, so writers of emacs code are recommended to put the
-     first two args of a DEFUN on the same line. */
-  if (definedef == dnone && toktype == st_C_gnumacro)
-    {
-      next_token_is_func = TRUE;
-      return FALSE;
-    }
-  if (next_token_is_func)
-    {
-      next_token_is_func = FALSE;
-      fvdef = fignore;
-      *is_func_or_var = TRUE;
-      return TRUE;
-    }
+    definedef = dnone;
 
   /* Detect Objective C constructs. */
   switch (objdef)
@@ -2286,11 +2588,9 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
       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.len = methodlen;
+         token_name.buffer[len] = '\0';
          return TRUE;
        }
       return FALSE;
@@ -2302,10 +2602,8 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
       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);
-         token_name.len = methodlen;
          return TRUE;
        }
       return FALSE;
@@ -2326,11 +2624,15 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
   /* A function, variable or enum constant? */
   switch (toktype)
     {
+    case st_C_extern:
+      fvextern = TRUE;
+      /* FALLTHRU */
     case st_C_typespec:
-      if (fvdef != finlist && fvdef != fignore  && fvdef != vignore)
-        fvdef = fvnone;                /* should be useless */
+      if (fvdef != finlist && fvdef != fignore && fvdef != vignore)
+       fvdef = fvnone;         /* should be useless */
       return FALSE;
     case st_C_ignore:
+      fvextern = FALSE;
       fvdef = vignore;
       return FALSE;
     case st_C_operator:
@@ -2338,33 +2640,61 @@ consider_token (str, len, c, c_ext, cblev, parlev, is_func_or_var)
       *is_func_or_var = TRUE;
       return TRUE;
     case st_none:
-      if (constantypedefs && structdef == sinbody && structtype == st_C_enum)
-       return TRUE;
-      if (fvdef == fvnone)
+      if (constantypedefs
+         && structdef == snone
+         && structtype == st_C_enum && cblev > structcblev)
+       return TRUE;            /* enum constant */
+      switch (fvdef)
        {
+       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, variables, typedefs,
- *     #define's, enum constants 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.
  */
+struct
+{
+  long linepos;
+  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()                                           \
@@ -2381,22 +2711,22 @@ do {                                                                    \
 #define CNL()                                                          \
 do {                                                                   \
   CNL_SAVE_DEFINEDEF();                                                        \
-  if (savetok.valid)                                                   \
+  if (savetoken.valid)                                                 \
     {                                                                  \
-      tok = savetok;                                                   \
-      savetok.valid = FALSE;                                           \
+      token = savetoken;                                               \
+      savetoken.valid = FALSE;                                         \
     }                                                                  \
   definedef = dnone;                                                   \
 } while (0)
 
 
-void
+static void
 make_C_tag (isfun)
      bool isfun;
 {
-  /* This function should never be called when tok.valid is FALSE, but
+  /* This function should never be called when token.valid is FALSE, but
      we must protect against invalid input or internal errors. */
-  if (tok.valid)
+  if (DEBUG || token.valid)
     {
       if (traditional_tag_style)
        {
@@ -2404,22 +2734,33 @@ make_C_tag (isfun)
             which uses the new method for naming tags (see new_pfnote). */
          char *name = NULL;
 
-         if (CTAGS || tok.named)
+         if (CTAGS || token.named)
            name = savestr (token_name.buffer);
-         pfnote (name, isfun,
-                 tok.buffer, tok.linelen, tok.lineno, tok.linepos);
+         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,
-                   tok.buffer, tok.linelen, tok.lineno, tok.linepos);
-      tok.valid = FALSE;
+       new_pfnote (token_name.buffer, token_name.len, isfun, token.line,
+                   token.offset+token.length+1, token.lineno, token.linepos);
+      token.valid = FALSE;
     }
-  else if (DEBUG)
-    abort ();
 }
 
 
-void
+/*
+ * 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 */
@@ -2433,11 +2774,24 @@ C_entries (c_ext, inf)
   int qlen;                    /* length of qualifier */
   int cblev;                   /* current curly brace level */
   int parlev;                  /* current parenthesis level */
+  int typdefcblev;             /* cblev where a typedef struct body begun */
   bool incomm, inquote, inchar, quotednl, midtoken;
   bool cplpl, cjava;
-  token savetok;               /* token saved during preprocessor handling */
+  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 = 0;         /* keep compiler quiet */
   curndx = newndx = 0;
   lineno = 0;
@@ -2445,12 +2799,13 @@ C_entries (c_ext, inf)
   lp = curlb.buffer;
   *lp = 0;
 
-  fvdef = fvnone; 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;
+  typdefcblev = 0;
   parlev = 0;
   cplpl = (c_ext & C_PLPL) == C_PLPL;
   cjava = (c_ext & C_JAVA) == C_JAVA;
@@ -2459,6 +2814,7 @@ C_entries (c_ext, inf)
   else
     { qualifier = "::"; qlen = 2; }
 
+
   while (!feof (inf))
     {
       c = *lp++;
@@ -2529,13 +2885,26 @@ C_entries (c_ext, inf)
          {
          case '"':
            inquote = TRUE;
-           if (fvdef != finlist && fvdef != fignore && fvdef !=vignore)
-             fvdef = fvnone;
+           switch (fvdef)
+             {
+             case fdefunkey:
+             case fstartlist:
+             case finlist:
+             case fignore:
+             case vignore:
+               break;
+             default:
+               fvextern = FALSE;
+               fvdef = fvnone;
+             }
            continue;
          case '\'':
            inchar = TRUE;
            if (fvdef != finlist && fvdef != fignore && fvdef !=vignore)
-             fvdef = fvnone;
+             {
+               fvextern = FALSE;
+               fvdef = fvnone;
+             }
            continue;
          case '/':
            if (*lp == '*')
@@ -2554,11 +2923,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; fvdef = fvnone;
+               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;
@@ -2594,27 +2962,29 @@ 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)
-          || (structdef == sinbody && structtype == st_C_enum))
-         && typdef != tignore
+      /* Consider token only if some involved conditions are satisfied. */
+      if (typdef != tignore
          && definedef != dignorerest
-         && fvdef != 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
                    {
@@ -2622,90 +2992,121 @@ C_entries (c_ext, inf)
 
                      if (yacc_rules
                          || consider_token (newlb.buffer + tokoff, toklen, c,
-                                            c_ext, cblev, parlev, &funorvar))
+                                            &c_ext, cblev, parlev, &funorvar))
                        {
-                         tok.named = FALSE;
-                         if (structdef == sinbody
-                             && definedef == dnone
-                             && funorvar)
-                           /* function or var defined in C++ class body */
+                         if (fvdef == foperator)
                            {
-                             int len;
-                             if (fvdef == foperator)
-                               {
-                                 char *oldlp = lp;
-                                 lp = skip_spaces (lp-1);
-                                 while (*lp != '\0'
-                                        && !isspace (*lp) && *lp != '(')
-                                   lp += 1;
-                                 c = *lp++;
-                                 toklen += lp - oldlp;
-                               }
-
-                             len = strlen (structtag) + qlen + toklen;
-                             grow_linebuffer (&token_name, len + 1);
-                             strcpy (token_name.buffer, structtag);
+                             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);
-                             token_name.len = len;
-                             tok.named = TRUE;
+                             token.named = TRUE;
                            }
                          else if (objdef == ocatseen)
                            /* Objective C category */
                            {
                              int len = strlen (objtag) + 2 + toklen;
-                             grow_linebuffer (&token_name, len + 1);
+                             linebuffer_setlen (&token_name, len);
                              strcpy (token_name.buffer, objtag);
                              strcat (token_name.buffer, "(");
                              strncat (token_name.buffer,
                                       newlb.buffer + tokoff, toklen);
                              strcat (token_name.buffer, ")");
-                             token_name.len = len;
-                             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);
                              token_name.buffer[toklen] = '\0';
-                             token_name.len = toklen;
-                             /* Name macros. */
-                             tok.named = (structdef == stagseen
-                                          || typdef == tend
-                                          || (funorvar
-                                              && definedef == dignorerest));
+                             /* 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
                              && (fvdef == fvnameseen
                                  || fvdef == foperator
                                  || structdef == stagseen
                                  || typdef == tend
+                                 || typdef == ttypeseen
                                  || objdef != onone))
                            {
                              if (current_lb_is_new)
                                switch_line_buffers ();
                            }
-                         else
+                         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;
@@ -2730,10 +3131,13 @@ C_entries (c_ext, inf)
                      break;
                    }
                  if (structdef == stagseen && !cjava)
-                   structdef = snone;
+                   {
+                     popclass_above (cblev);
+                     structdef = snone;
+                   }
                  break;
                case dsharpseen:
-                 savetok = tok;
+                 savetoken = token;
                }
              if (!yacc_rules || lp == newlb.buffer + 1)
                {
@@ -2751,6 +3155,11 @@ 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)
@@ -2762,55 +3171,62 @@ C_entries (c_ext, inf)
            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, ":");
-             token_name.len = methodlen;
              break;
            }
          if (structdef == stagseen)
            structdef = scolonseen;
-         else
-           switch (fvdef)
-             {
-             case fvnameseen:
-               if (yacc_rules)
-                 {
-                   make_C_tag (FALSE); /* a yacc function */
-                   fvdef = fignore;
-                 }
-               break;
-             case fstartlist:
-               fvdef = fvnone;
-               break;
-             }
          break;
        case ';':
          if (definedef != dnone)
            break;
-         if (cblev == 0)
-           switch (typdef)
-             {
-             case tend:
-               make_C_tag (FALSE); /* a typedef */
-               /* FALLTHRU */
-             default:
-               typdef = tnone;
-             }
-         switch (fvdef)
+         switch (typdef)
            {
-           case fignore:
+           case tend:
+           case ttypeseen:
+             make_C_tag (FALSE); /* a typedef */
+             typdef = tnone;
+             fvdef = fvnone;
              break;
-           case fvnameseen:
-             if ((globals && cblev == 0) || (members && cblev == 1))
-               make_C_tag (FALSE); /* a variable */
+           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:
-             fvdef = fvnone;
-             /* 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;
+             if (!instruct)
+               typdef = tnone;
            }
          if (structdef == stagseen)
            structdef = snone;
@@ -2828,14 +3244,31 @@ C_entries (c_ext, inf)
            }
          switch (fvdef)
            {
+           case fdefunkey:
            case foperator:
+           case fstartlist:
            case finlist:
            case fignore:
            case vignore:
              break;
-           case fvnameseen:
-             if ((globals && cblev == 0) || (members && cblev == 1))
-               make_C_tag (FALSE); /* a variable */
+           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;
@@ -2846,28 +3279,35 @@ C_entries (c_ext, inf)
        case '[':
          if (definedef != dnone)
            break;
-         if (cblev == 0 && typdef == tend)
+         if (structdef == stagseen)
+           structdef = snone;
+         switch (typdef)
            {
+           case ttypeseen:
+           case tend:
              typdef = tignore;
              make_C_tag (FALSE);       /* a typedef */
              break;
-           }
-         switch (fvdef)
-           {
-           case foperator:
-           case finlist:
-           case fignore:
-           case vignore:
+           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;
-           case fvnameseen:
-             if ((globals && cblev == 0) || (members && cblev == 1))
-               make_C_tag (FALSE); /* a variable */
-             /* FALLTHRU */
-           default:
-             fvdef = fvnone;
            }
-         if (structdef == stagseen)
-           structdef = snone;
          break;
        case '(':
          if (definedef != dnone)
@@ -2876,23 +3316,20 @@ C_entries (c_ext, inf)
            objdef = oparenseen;
          switch (fvdef)
            {
-           case fvnone:
-             switch (typdef)
+           case fvnameseen:
+             if (typdef == ttypeseen
+                 && *lp != '*'
+                 && !instruct)
                {
-               case ttypedseen:
-               case tend:
-                 if (tok.valid && *lp != '*')
-                   {
-                     /* This handles constructs like:
-                        typedef void OperatorFun (int fun); */
-                     make_C_tag (FALSE);
-                     typdef = tignore;
-                   }
+                 /* This handles constructs like:
+                    typedef void OperatorFun (int fun); */
+                 make_C_tag (FALSE);
+                 typdef = tignore;
+                 fvdef = fignore;
                  break;
-               } /* switch (typdef) */
-             break;
-           case foperator:     /* operator() is not handled */
-           case fvnameseen:
+               }
+             /* FALLTHRU */
+           case foperator:
              fvdef = fstartlist;
              break;
            case flistseen:
@@ -2918,7 +3355,9 @@ C_entries (c_ext, inf)
                  fvdef = flistseen;
                  break;
                }
-             if (cblev == 0 && typdef == tend)
+             if (!instruct
+                 && (typdef == tend
+                     || typdef == ttypeseen))
                {
                  typdef = tignore;
                  make_C_tag (FALSE); /* a typedef */
@@ -2930,24 +3369,15 @@ C_entries (c_ext, inf)
        case '{':
          if (definedef != dnone)
            break;
-         if (typdef == ttypedseen)
-           typdef = tinbody;
-         switch (structdef)
+         if (typdef == ttypeseen)
            {
-           case skeyseen:      /* unnamed struct */
-             structdef = sinbody;
-             structtag = "_anonymous_";
-             break;
-           case stagseen:
-           case scolonseen:    /* named struct */
-             structdef = sinbody;
-             make_C_tag (FALSE);       /* a struct */
-             break;
+             typdefcblev = cblev;
+             typdef = tinbody;
            }
          switch (fvdef)
            {
            case flistseen:
-             make_C_tag (TRUE); /* a function */
+             make_C_tag (TRUE);    /* a function */
              /* FALLTHRU */
            case fignore:
              fvdef = fvnone;
@@ -2966,10 +3396,24 @@ C_entries (c_ext, inf)
                  break;
                default:
                  /* Neutralize `extern "C" {' grot. */
-                 if (cblev == 0 && structdef == snone && typdef == tnone)
+                 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 '*':
@@ -2988,20 +3432,12 @@ C_entries (c_ext, inf)
            }
          else if (cblev > 0)
            cblev--;
-         if (cblev == 0)
+         popclass_above (cblev);
+         structdef = snone;
+         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 '=':
@@ -3015,13 +3451,28 @@ C_entries (c_ext, inf)
            case vignore:
              break;
            case fvnameseen:
-             if ((globals && cblev == 0) || (members && cblev == 1))
+             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)
@@ -3030,8 +3481,9 @@ C_entries (c_ext, inf)
              break;
            }
          /* FALLTHRU */
+       resetfvdef:
        case '#': case '~': case '&': case '%': case '/': case '|':
-       case '^': case '!': case '<': case '>': case '.': case '?': case ']':
+       case '^': case '!': case '.': case '?': case ']':
          if (definedef != dnone)
            break;
          /* These surely cannot follow a function tag in C. */
@@ -3061,21 +3513,25 @@ C_entries (c_ext, inf)
        } /* 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;
 {
@@ -3083,7 +3539,7 @@ plain_C_entries (inf)
 }
 
 /* Always do C++. */
-void
+static void
 Cplusplus_entries (inf)
      FILE *inf;
 {
@@ -3091,7 +3547,7 @@ Cplusplus_entries (inf)
 }
 
 /* Always do Java. */
-void
+static void
 Cjava_entries (inf)
      FILE *inf;
 {
@@ -3099,7 +3555,7 @@ Cjava_entries (inf)
 }
 
 /* Always do C*. */
-void
+static void
 Cstar_entries (inf)
      FILE *inf;
 {
@@ -3107,14 +3563,15 @@ Cstar_entries (inf)
 }
 
 /* Always do Yacc. */
-void
+static void
 Yacc_entries (inf)
      FILE *inf;
 {
   C_entries (YACC, inf);
 }
+
 \f
-/* A useful macro. */  
+/* A useful macro. */
 #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer)   \
   for (lineno = charno = 0;    /* loop initialization */               \
        !feof (file_pointer)    /* loop test */                         \
@@ -3130,7 +3587,7 @@ Yacc_entries (inf)
  * Read a file, but do no processing.  This is used to do regexp
  * matching on files that have no language defined.
  */
-void
+static void
 just_read_file (inf)
      FILE *inf;
 {
@@ -3139,18 +3596,23 @@ just_read_file (inf)
   LOOP_ON_INPUT_LINES (inf, lb, dummy)
     continue;
 }
+
 \f
 /* Fortran parsing */
 
-bool
+static bool tail P_((char *));
+static void takeprec P_((void));
+static void getit P_((FILE *));
+
+static bool
 tail (cp)
      char *cp;
 {
   register int len = 0;
 
-  while (*cp && lowcase(*cp) == lowcase(dbp[len]))
+  while (*cp != '\0' && lowcase (*cp) == lowcase (dbp[len]))
     cp++, len++;
-  if (*cp == '\0' && !intoken(dbp[len]))
+  if (*cp == '\0' && !intoken (dbp[len]))
     {
       dbp += len;
       return TRUE;
@@ -3158,7 +3620,7 @@ tail (cp)
   return FALSE;
 }
 
-void
+static void
 takeprec ()
 {
   dbp = skip_spaces (dbp);
@@ -3171,17 +3633,17 @@ takeprec ()
       dbp += 3;
       return;
     }
-  if (!isdigit (*dbp))
+  if (!ISDIGIT (*dbp))
     {
       --dbp;                   /* force failure */
       return;
     }
   do
     dbp++;
-  while (isdigit (*dbp));
+  while (ISDIGIT (*dbp));
 }
 
-void
+static void
 getit (inf)
      FILE *inf;
 {
@@ -3199,18 +3661,16 @@ getit (inf)
       dbp += 6;
       dbp = skip_spaces (dbp);
     }
-  if (!isalpha (*dbp)
-      && *dbp != '_'
-      && *dbp != '$')
+  if (!ISALPHA (*dbp) && *dbp != '_' && *dbp != '$')
     return;
-  for (cp = dbp + 1; *cp && intoken (*cp); cp++)
+  for (cp = dbp + 1; *cp != '\0' && intoken (*cp); cp++)
     continue;
   pfnote (savenstr (dbp, cp-dbp), TRUE,
          lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
 }
 
 
-void
+static void
 Fortran_functions (inf)
      FILE *inf;
 {
@@ -3268,10 +3728,6 @@ Fortran_functions (inf)
          if (tail ("entry"))
            getit (inf);
          continue;
-       case 'p':
-         if (tail ("program") || tail ("procedure"))
-           getit (inf);
-         continue;
        case 'b':
          if (tail ("blockdata") || tail ("block data"))
            {
@@ -3286,13 +3742,183 @@ Fortran_functions (inf)
        }
     }
 }
+
+\f
+/*
+ * Ada parsing
+ * Philippe Waroquiers <philippe.waroquiers@eurocontrol.be> (1998)
+ */
+
+static void adagetit P_((FILE *, char *));
+
+/* Once we are positioned after an "interesting" keyword, let's get
+   the real tag value necessary. */
+static void
+adagetit (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 (*dbp)
+        {
+        case 'b':
+        case 'B':
+          if (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':
+        case 'T':
+          /* Skipping type of   task type   or   protected type ... */
+          if (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 (*dbp)
+           {
+           case 'f':
+           case 'F':
+             if (!packages_only && tail ("function"))
+               adagetit (inf, "/f");
+             else
+               break;          /* from switch */
+             continue;         /* advance char */
+           case 'p':
+           case 'P':
+             if (!packages_only && tail ("procedure"))
+               adagetit (inf, "/p");
+             else if (tail ("package"))
+               adagetit (inf, "/s");
+             else if (tail ("protected")) /* protected type */
+               adagetit (inf, "/t");
+             else
+               break;          /* from switch */
+             continue;         /* advance char */
+           case 't':
+           case 'T':
+             if (!packages_only && tail ("task"))
+               adagetit (inf, "/k");
+             else if (typedefs && !packages_only && tail ("type"))
+               {
+                 adagetit (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
 /*
  * Bob Weiner, Motorola Inc., 4/3/94
  * Unix and microcontroller assembly tag handling
  * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
  */
-void
+static void
 Asm_labels (inf)
      FILE *inf;
 {
@@ -3302,29 +3928,31 @@ Asm_labels (inf)
     {
       /* If first char is alphabetic or one of [_.$], test for colon
         following identifier. */
-      if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
+      if (ISALPHA (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
        {
          /* Read past label. */
          cp++;
-         while (isalnum (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
+         while (ISALNUM (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
            cp++;
-         if (*cp == ':' || isspace (*cp))
+         if (*cp == ':' || iswhite (*cp))
            {
              /* Found end of label, so copy it and add it to the table. */
-             pfnote ((CTAGS) ? savenstr(lb.buffer, cp-lb.buffer) : NULL, TRUE,
+             pfnote (savenstr(lb.buffer, cp-lb.buffer), TRUE,
                      lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
            }
        }
     }
 }
+
 \f
 /*
- * Perl support by Bart Robinson <lomew@cs.utah.edu>
- *              enhanced by Michael Ernst <mernst@alum.mit.edu>
+ * Perl support
  * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
  * Perl variable names: /^(my|local).../
+ * Bart Robinson <lomew@cs.utah.edu> (1995)
+ * Michael Ernst <mernst@alum.mit.edu> (1997)
  */
-void
+static void
 Perl_functions (inf)
      FILE *inf;
 {
@@ -3334,18 +3962,18 @@ Perl_functions (inf)
     {
       if (*cp++ == 's'
          && *cp++ == 'u'
-         && *cp++ == 'b' && isspace (*cp++))
+         && *cp++ == 'b' && iswhite (*cp++))
        {
          cp = skip_spaces (cp);
          if (*cp != '\0')
            {
              char *sp = cp;
              while (*cp != '\0'
-                    && !isspace (*cp) && *cp != '{' && *cp != '(')
+                    && !iswhite (*cp) && *cp != '{' && *cp != '(')
                cp++;
              pfnote (savenstr (sp, cp-sp), TRUE,
                      lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-           }         
+           }
        }
        else if (globals                /* only if tagging global vars is enabled */
                && ((cp = lb.buffer,
@@ -3357,7 +3985,7 @@ Perl_functions (inf)
                        && *cp++ == 'c'
                        && *cp++ == 'a'
                        && *cp++ == 'l'))
-               && (*cp == '(' || isspace (*cp)))
+               && (*cp == '(' || iswhite (*cp)))
        {
          /* After "my" or "local", but before any following paren or space. */
          char *varname = NULL;
@@ -3366,7 +3994,7 @@ Perl_functions (inf)
          if (*cp == '$' || *cp == '@' || *cp == '%')
            {
              char* varstart = ++cp;
-             while (isalnum (*cp) || *cp == '_')
+             while (ISALNUM (*cp) || *cp == '_')
                cp++;
              varname = savenstr (varstart, cp-varstart);
            }
@@ -3377,7 +4005,7 @@ Perl_functions (inf)
              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 ((CTAGS) ? savenstr (lb.buffer, cp-lb.buffer) : varname,
@@ -3385,12 +4013,15 @@ Perl_functions (inf)
        }
     }
 }
+
 \f
 /*
- * Python support by Eric S. Raymond <esr@thyrsus.com>
- * Look for /^def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
+ * Python support
+ * Look for /^[ \t\n]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
+ * Eric S. Raymond <esr@thyrsus.com> (1997)
+ * Sean Legassick <sean@informage.net> (2004)
  */
-void
+static void
 Python_functions (inf)
      FILE *inf;
 {
@@ -3398,12 +4029,13 @@ Python_functions (inf)
 
   LOOP_ON_INPUT_LINES (inf, lb, cp)
     {
+      cp = skip_spaces (cp);
       if (*cp++ == 'd'
          && *cp++ == 'e'
-         && *cp++ == 'f' && isspace (*cp++))
+         && *cp++ == 'f' && iswhite (*cp++))
        {
          cp = skip_spaces (cp);
-         while (*cp != '\0' && !isspace (*cp) && *cp != '(' && *cp != ':')
+         while (*cp != '\0' && !iswhite (*cp) && *cp != '(' && *cp != ':')
            cp++;
          pfnote (NULL, TRUE,
                  lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
@@ -3414,23 +4046,24 @@ Python_functions (inf)
          && *cp++ == 'l'
          && *cp++ == 'a'
          && *cp++ == 's'
-         && *cp++ == 's' && isspace (*cp++))
+         && *cp++ == 's' && iswhite (*cp++))
        {
          cp = skip_spaces (cp);
-         while (*cp != '\0' && !isspace (*cp) && *cp != '(' && *cp != ':')
+         while (*cp != '\0' && !iswhite (*cp) && *cp != '(' && *cp != ':')
            cp++;
          pfnote (NULL, TRUE,
                  lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
        }
     }
 }
+
 \f
 /* Idea by Corny de Souza
  * 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.
  */
-void
+static void
 Cobol_paragraphs (inf)
      FILE *inf;
 {
@@ -3443,16 +4076,40 @@ Cobol_paragraphs (inf)
       bp += 8;
 
       /* If eoln, compiler option or comment ignore whole line. */
-      if (bp[-1] != ' ' || !isalnum (bp[0]))
+      if (bp[-1] != ' ' || !ISALNUM (bp[0]))
         continue;
 
-      for (ep = bp; isalnum (*ep) || *ep == '-'; ep++)
+      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
+/*
+ * Makefile support
+ * Idea by Assar Westerlund <assar@sics.se> (2001)
+ */
+static void
+Makefile_targets (inf)
+     FILE *inf;
+{
+  register char *bp;
+
+  LOOP_ON_INPUT_LINES (inf, lb, bp)
+    {
+      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                */
@@ -3463,7 +4120,7 @@ Cobol_paragraphs (inf)
  *  "forward" immediately following the procedure statement; if found,
  *  the tag is skipped.
  */
-void
+static void
 Pascal_functions (inf)
      FILE *inf;
 {
@@ -3498,8 +4155,8 @@ Pascal_functions (inf)
   inparms = FALSE;             /* found '(' after "proc"            */
   verify_tag = FALSE;          /* check if "extern" is ahead        */
 
-  
-  while (!feof (inf))          /* long main loop to get next char */  
+
+  while (!feof (inf))          /* long main loop to get next char */
     {
       c = *dbp++;
       if (c == '\0')           /* if end of line */
@@ -3599,7 +4256,7 @@ Pascal_functions (inf)
            continue;
 
          /* save all values for later tagging */
-         grow_linebuffer (&tline, lb.len + 1);
+         linebuffer_setlen (&tline, lb.len);
          strcpy (tline.buffer, lb.buffer);
          save_lineno = lineno;
          save_lcno = linecharno;
@@ -3635,12 +4292,18 @@ Pascal_functions (inf)
 
   free (tline.buffer);
 }
+
 \f
 /*
- * lisp tag functions
+ * Lisp tag functions
  *  look for (def or (DEF, quote or QUOTE
  */
-int
+
+static int L_isdef P_((char *));
+static int L_isquote P_((char *));
+static void L_getit P_((void));
+
+static int
 L_isdef (strp)
      register char *strp;
 {
@@ -3649,7 +4312,7 @@ L_isdef (strp)
          && (strp[3] == 'f' || strp[3] == 'F'));
 }
 
-int
+static int
 L_isquote (strp)
      register char *strp;
 {
@@ -3658,10 +4321,10 @@ L_isquote (strp)
          && (*++strp == 'o' || *strp == 'O')
          && (*++strp == 't' || *strp == 'T')
          && (*++strp == 'e' || *strp == 'E')
-         && isspace (*++strp));
+         && iswhite (*++strp));
 }
 
-void
+static void
 L_getit ()
 {
   register char *cp;
@@ -3678,7 +4341,7 @@ L_getit ()
   }
 
   for (cp = dbp /*+1*/;
-       *cp != '\0' && *cp != '(' && *cp != ' ' && *cp != ')';
+       *cp != '\0' && *cp != '(' && !iswhite(*cp) && *cp != ')';
        cp++)
     continue;
   if (cp == dbp)
@@ -3688,7 +4351,7 @@ L_getit ()
          lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
 }
 
-void
+static void
 Lisp_functions (inf)
      FILE *inf;
 {
@@ -3707,7 +4370,7 @@ Lisp_functions (inf)
              /* Check for (foo::defmumble name-defined ... */
              do
                dbp++;
-             while (*dbp != '\0' && !isspace (*dbp)
+             while (*dbp != '\0' && !iswhite (*dbp)
                     && *dbp != ':' && *dbp != '(' && *dbp != ')');
              if (*dbp == ':')
                {
@@ -3726,13 +4389,16 @@ Lisp_functions (inf)
        }
     }
 }
+
 \f
 /*
  * Postscript tag functions
  * Just look for lines where the first character is '/'
- * Richard Mlynarik <mly@adoc.xerox.com>
+ * Also look at "defineps" for PSWrap
+ * Richard Mlynarik <mly@adoc.xerox.com> (1997)
+ * Ideas by Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
  */
-void 
+static void
 Postscript_functions (inf)
      FILE *inf;
 {
@@ -3746,9 +4412,15 @@ Postscript_functions (inf)
               *ep != '\0' && *ep != ' ' && *ep != '{';
               ep++)
            continue;
-         pfnote ((CTAGS) ? savenstr (bp, ep-bp) : NULL, TRUE,
+         pfnote (savenstr (bp, ep-bp), TRUE,
                  lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
        }
+      else if (strneq (bp, "defineps", 8))
+       {
+         bp = skip_non_spaces (bp);
+         bp = skip_spaces (bp);
+         get_tag (bp);
+       }
     }
 }
 
@@ -3761,54 +4433,39 @@ Postscript_functions (inf)
  * look for (set! xyzzy
  */
 
-void get_scheme ();
-
-void
+static void
 Scheme_functions (inf)
      FILE *inf;
 {
-  LOOP_ON_INPUT_LINES (inf, lb, dbp)
+  register char *bp;
+
+  LOOP_ON_INPUT_LINES (inf, lb, bp)
     {
-      if (dbp[0] == '('
-         && (dbp[1] == 'D' || dbp[1] == 'd')
-         && (dbp[2] == 'E' || dbp[2] == 'e')
-         && (dbp[3] == 'F' || dbp[3] == 'f'))
+      if (bp[0] == '('
+         && (bp[1] == 'D' || bp[1] == 'd')
+         && (bp[2] == 'E' || bp[2] == 'e')
+         && (bp[3] == 'F' || bp[3] == 'f'))
        {
-         dbp = skip_non_spaces (dbp);
+         bp = skip_non_spaces (bp);
          /* Skip over open parens and white space */
-         while (isspace (*dbp) || *dbp == '(')
-           dbp++;
-         get_scheme ();
+         while (iswhite (*bp) || *bp == '(')
+           bp++;
+         get_tag (bp);
        }
-      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])))
+      if (bp[0] == '('
+         && (bp[1] == 'S' || bp[1] == 's')
+         && (bp[2] == 'E' || bp[2] == 'e')
+         && (bp[3] == 'T' || bp[3] == 't')
+         && (bp[4] == '!' || bp[4] == '!')
+         && (iswhite (bp[5])))
        {
-         dbp = skip_non_spaces (dbp);
-         dbp = skip_spaces (dbp);
-         get_scheme ();
+         bp = skip_non_spaces (bp);
+         bp = skip_spaces (bp);
+         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 != '\0' && *cp != '(' && *cp != ')' && !isspace (*cp);
-       cp++)
-    continue;
-  pfnote (savenstr (dbp, cp-dbp), TRUE,
-         lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-}
 \f
 /* Find tags in TeX and LaTeX input files.  */
 
@@ -3830,9 +4487,9 @@ 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 ();
+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 = '{';
@@ -3841,8 +4498,8 @@ char TEX_clgrp = '}';
 /*
  * TeX/LaTeX scanning loop.
  */
-void
-TeX_functions (inf)
+static void
+TeX_commands (inf)
      FILE *inf;
 {
   char *cp, *lasthit;
@@ -3888,7 +4545,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;
 {
@@ -3923,7 +4580,7 @@ TEX_mode (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;
@@ -3980,7 +4637,7 @@ TEX_decode_env (evarname, defenv)
    Otherwise return -1.
    Keep the capital `T' in `token' for dumb truncating compilers
    (this distinguishes it from `TEX_toktab' */
-int
+static int
 TEX_Token (cp)
      char *cp;
 {
@@ -3991,18 +4648,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 ((*cp++ == '@'
+          && *cp++ == 'n'
+          && *cp++ == 'o'
+          && *cp++ == 'd'
+          && *cp++ == 'e' && iswhite (*cp++)))
+       {
+         start = cp = skip_spaces(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
  *
  * Assumes that the predicate starts at column 0.
- * Only the first clause of a predicate is added. 
+ * Only the first clause of a predicate is added.
  */
-int prolog_pred ();
-void prolog_skip_comment ();
-int prolog_atom ();
+static int prolog_pred P_((char *, char *));
+static void prolog_skip_comment P_((linebuffer *, FILE *));
+static int prolog_atom P_((char *, int));
 
-void
+static void
 Prolog_functions (inf)
      FILE *inf;
 {
@@ -4018,7 +4700,7 @@ Prolog_functions (inf)
     {
       if (cp[0] == '\0')       /* Empty line */
        continue;
-      else if (isspace (cp[0])) /* Not a predicate */
+      else if (iswhite (cp[0])) /* Not a predicate */
        continue;
       else if (cp[0] == '/' && cp[1] == '*')   /* comment. */
        prolog_skip_comment (&lb, inf);
@@ -4029,7 +4711,7 @@ Prolog_functions (inf)
          if (last == NULL)
            last = xnew(len + 1, char);
          else if (len + 1 > allocated)
-           last = xrnew (last, len + 1, char);
+           xrnew (last, len + 1, char);
          allocated = len + 1;
          strncpy (last, cp, len);
          last[len] = '\0';
@@ -4038,7 +4720,7 @@ Prolog_functions (inf)
 }
 
 
-void
+static void
 prolog_skip_comment (plb, inf)
      linebuffer *plb;
      FILE *inf;
@@ -4066,7 +4748,7 @@ prolog_skip_comment (plb, inf)
  * Return the size of the name of the predicate, or 0 if no header
  * was found.
  */
-int
+static int
 prolog_pred (s, last)
      char *s;
      char *last;               /* Name of last clause. */
@@ -4091,8 +4773,7 @@ prolog_pred (s, last)
          || 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;
        }
     }
@@ -4108,7 +4789,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;
@@ -4117,11 +4798,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++;
        }
@@ -4131,7 +4812,7 @@ prolog_atom (s, pos)
     {
       pos++;
 
-      while (1) 
+      while (1)
        {
          if (s[pos] == '\'')
            {
@@ -4157,19 +4838,20 @@ prolog_atom (s, pos)
   else
     return -1;
 }
+
 \f
-/* 
+/*
  * Support for Erlang  --  Anders Lindgren, Feb 1996.
  *
  * Generates tags for functions, defines, and records.
  *
  * Assumes that Erlang functions start at column 0.
  */
-int erlang_func ();
-void erlang_attribute ();
-int erlang_atom ();
+static int erlang_func P_((char *, char *));
+static void erlang_attribute P_((char *));
+static int erlang_atom P_((char *, int));
 
-void
+static void
 Erlang_functions (inf)
      FILE *inf;
 {
@@ -4185,7 +4867,7 @@ Erlang_functions (inf)
     {
       if (cp[0] == '\0')       /* Empty line */
        continue;
-      else if (isspace (cp[0])) /* Not function nor attribute */
+      else if (iswhite (cp[0])) /* Not function nor attribute */
        continue;
       else if (cp[0] == '%')   /* comment */
        continue;
@@ -4198,14 +4880,14 @@ Erlang_functions (inf)
        }
       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);
          else if (len + 1 > allocated)
-           last = xrnew (last, len + 1, char);
+           xrnew (last, len + 1, char);
          allocated = len + 1;
          strncpy (last, cp, len);
          last[len] = '\0';
@@ -4224,7 +4906,7 @@ 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. */
@@ -4245,8 +4927,7 @@ erlang_func (s, last)
          || 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;
        }
 
@@ -4255,7 +4936,7 @@ erlang_func (s, last)
 
 
 /*
- * Handle attributes.  Currently, tags are generated for defines 
+ * Handle attributes.  Currently, tags are generated for defines
  * and records.
  *
  * They are on the form:
@@ -4263,7 +4944,7 @@ erlang_func (s, last)
  * -define(Foo(M, N), M+N).
  * -record(graph, {vtab = notable, cyclic = true}).
  */
-void
+static void
 erlang_attribute (s)
      char *s;
 {
@@ -4273,12 +4954,12 @@ erlang_attribute (s)
   if (strneq (s, "-define", 7) || strneq (s, "-record", 7))
     {
       pos = skip_spaces (s + 7) - s;
-      if (s[pos++] == '(') 
+      if (s[pos++] == '(')
        {
          pos = skip_spaces (s + pos) - s;
          len = erlang_atom (s, pos);
          if (len != 0)
-           pfnote ((CTAGS) ? savenstr (& s[pos], len) : NULL, TRUE,
+           pfnote (savenstr (& s[pos], len), TRUE,
                    s, pos + len, lineno, linecharno);
        }
     }
@@ -4290,7 +4971,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;
@@ -4299,11 +4980,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;
     }
@@ -4311,7 +4992,7 @@ erlang_atom (s, pos)
     {
       pos++;
 
-      while (1) 
+      while (1)
        {
          if (s[pos] == '\'')
            {
@@ -4335,16 +5016,22 @@ erlang_atom (s, pos)
   else
     return -1;
 }
+
 \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;
 {
@@ -4383,9 +5070,10 @@ scan_separators (name)
 
 /* Look at the argument of --regex or --no-regex and do the right
    thing.  Same for each line of a regexp file. */
-void
-analyse_regex (regex_arg)
+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 */
@@ -4416,7 +5104,7 @@ analyse_regex (regex_arg)
          }
        initbuffer (&regexbuf);
        while (readline_internal (&regexbuf, regexfp) > 0)
-         analyse_regex (regexbuf.buffer);
+         analyse_regex (regexbuf.buffer, ignore_case);
        free (regexbuf.buffer);
        fclose (regexfp);
       }
@@ -4436,27 +5124,29 @@ analyse_regex (regex_arg)
              return;
            }
        *cp = '\0';
-       lang = get_language_from_name (lang_name);
+       lang = get_language_from_langname (lang_name);
        if (lang == NULL)
          return;
-       add_regex (cp + 1, lang);
+       add_regex (cp + 1, ignore_case, lang);
       }
       break;
 
       /* Regexp to be used for any language. */
     default:
-      add_regex (regex_arg, NULL);
+      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, lang)
+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;
@@ -4477,10 +5167,9 @@ add_regex (regexp_pattern, lang)
   (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;
+  /* Translation table to fold case if appropriate. */
+  patbuf->translate = (ignore_case) ? lc_trans : NULL;
 
   err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
   if (err != NULL)
@@ -4503,7 +5192,7 @@ add_regex (regexp_pattern, lang)
  * Do the substitutions indicated by the regular expression and
  * arguments.
  */
-char *
+static char *
 substitute (in, out, regs)
      char *in, *out;
      struct re_registers *regs;
@@ -4520,7 +5209,7 @@ substitute (in, out, regs)
   for (t = etags_strchr (out, '\\');
        t != NULL;
        t = etags_strchr (t + 2, '\\'))
-    if (isdigit (t[1]))
+    if (ISDIGIT (t[1]))
       {
        dig = t[1] - '0';
        diglen = regs->end[dig] - regs->start[dig];
@@ -4533,9 +5222,8 @@ substitute (in, out, regs)
   result = xnew (size + 1, char);
 
   for (t = result; *out != '\0'; out++)
-    if (*out == '\\' && isdigit (*++out))
+    if (*out == '\\' && ISDIGIT (*++out))
       {
-       /* Using "dig2" satisfies my debugger.  Bleah. */
        dig = *out - '0';
        diglen = regs->end[dig] - regs->start[dig];
        strncpy (t, in + regs->start[dig], diglen);
@@ -4545,14 +5233,13 @@ substitute (in, out, regs)
       *t++ = *out;
   *t = '\0';
 
-  if (DEBUG && (t > result + size || t - result != (int)strlen (result)))
-    abort ();
+  assert (t <= result + size && t - result == (int)strlen (result));
 
   return result;
 }
 
 /* Deallocate all patterns. */
-void
+static void
 free_patterns ()
 {
   pattern *pp;
@@ -4566,15 +5253,35 @@ free_patterns ()
     }
   return;
 }
-\f
 #endif /* ETAGS_REGEXPS */
+
+\f
+static void
+get_tag (bp)
+     register char *bp;
+{
+  register char *cp;
+
+  if (*bp == '\0')
+    return;
+  /* Go till you get to white space or a syntactic break */
+  for (cp = bp + 1;
+       *cp != '\0' && *cp != '(' && *cp != ')' && !iswhite (*cp);
+       cp++)
+    continue;
+  pfnote (savenstr (bp, cp-bp), TRUE,
+         lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+}
+
 /* Initialize a linebuffer for use */
-void
+static void
 initbuffer (lbp)
      linebuffer *lbp;
 {
-  lbp->size = 200;
-  lbp->buffer = xnew (200, char);
+  lbp->size = (DEBUG) ? 3 : 200;
+  lbp->buffer = xnew (lbp->size, char);
+  lbp->buffer[0] = '\0';
+  lbp->len = 0;
 }
 
 /*
@@ -4587,7 +5294,7 @@ initbuffer (lbp)
  * platforms (for text files, it translates CR-NL to NL as it reads in the
  * file).
  */
-long
+static long
 readline_internal (lbp, stream)
      linebuffer *lbp;
      register FILE *stream;
@@ -4606,7 +5313,7 @@ readline_internal (lbp, stream)
        {
          /* We're at the end of linebuffer: expand it. */
          lbp->size *= 2;
-         buffer = xrnew (buffer, lbp->size, char);
+         xrnew (buffer, lbp->size, char);
          p += buffer - lbp->buffer;
          pend = buffer + lbp->size;
          lbp->buffer = buffer;
@@ -4651,7 +5358,7 @@ readline_internal (lbp, stream)
  * Like readline_internal, above, but in addition try to match the
  * input line against relevant regular expressions.
  */
-long
+static long
 readline (lbp, stream)
      linebuffer *lbp;
      FILE *stream;
@@ -4704,15 +5411,16 @@ readline (lbp, stream)
          }
       }
 #endif /* ETAGS_REGEXPS */
-  
+
   return result;
 }
+
 \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;
 {
@@ -4723,7 +5431,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;
@@ -4740,13 +5448,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
@@ -4754,7 +5463,7 @@ etags_strrchr (sp, c)
       if (*sp == c)
        r = sp;
   } while (*sp++);
-  return r;
+  return (char *)r;
 }
 
 
@@ -4762,36 +5471,37 @@ etags_strrchr (sp, c)
  * 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;
 }
 
 /* Skip spaces, return new pointer. */
-char *
+static char *
 skip_spaces (cp)
      char *cp;
 {
-  while (isspace (*cp))                /* isspace('\0')==FALSE */
+  while (iswhite (*cp))
     cp++;
   return cp;
 }
 
 /* Skip non spaces, return new pointer. */
-char *
+static char *
 skip_non_spaces (cp)
      char *cp;
 {
-  while (!iswhite (*cp))       /* iswhite('\0')==TRUE */
+  while (*cp != '\0' && !iswhite (*cp))
     cp++;
   return cp;
 }
@@ -4805,7 +5515,7 @@ fatal (s1, s2)
   exit (BAD);
 }
 
-void
+static void
 pfatal (s1)
      char *s1;
 {
@@ -4813,7 +5523,7 @@ pfatal (s1)
   exit (BAD);
 }
 
-void
+static void
 suggest_asking_for_help ()
 {
   fprintf (stderr, "\tTry `%s %s' for a complete list of options.\n",
@@ -4828,9 +5538,9 @@ suggest_asking_for_help ()
 }
 
 /* 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);
@@ -4839,7 +5549,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;
 {
@@ -4853,10 +5563,11 @@ 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
@@ -4876,7 +5587,8 @@ etags_getcwd ()
   return path;
 
 #else /* not HAVE_GETCWD */
-#ifdef MSDOS
+#if MSDOS
+
   char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS.  */
 
   getwd (path);
@@ -4905,7 +5617,7 @@ etags_getcwd ()
 
 /* Return a newly allocated string containing the file name of FILE
    relative to the absolute directory DIR (which should end with a slash). */
-char *
+static char *
 relative_filename (file, dir)
      char *file, *dir;
 {
@@ -4919,6 +5631,10 @@ relative_filename (file, dir)
   while (*fp++ == *dp++)
     continue;
   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 != '/');
@@ -4941,7 +5657,7 @@ relative_filename (file, dir)
 
 /* Return a newly allocated string containing the absolute file name
    of FILE given DIR (which should end with a slash). */
-char *
+static char *
 absolute_filename (file, dir)
      char *file, *dir;
 {
@@ -4993,7 +5709,7 @@ absolute_filename (file, dir)
 
       slashp = etags_strchr (slashp + 1, '/');
     }
-  
+
   if (res[0] == '\0')
     return savestr ("/");
   else
@@ -5003,7 +5719,7 @@ absolute_filename (file, dir)
 /* Return a newly allocated string containing the absolute
    file name of dir where FILE resides given DIR (which should
    end with a slash). */
-char *
+static char *
 absolute_dirname (file, dir)
      char *file, *dir;
 {
@@ -5024,23 +5740,27 @@ absolute_dirname (file, dir)
 
 /* Whether the argument string is an absolute file name.  The argument
    string must have been canonicalized with canonicalize_filename. */
-bool
+static bool
 filename_is_absolute (fn)
      char *fn;
 {
   return (fn[0] == '/'
 #ifdef DOS_NT
-         || (isalpha(fn[0]) && fn[1] == ':' && fn[2] == '/')
+         || (ISALPHA(fn[0]) && fn[1] == ':' && fn[2] == '/')
 #endif
          );
 }
 
 /* Translate backslashes into slashes.  Works in place. */
-void
+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 = '/';
@@ -5050,15 +5770,18 @@ canonicalize_filename (fn)
 #endif
 }
 
-/* Increase the size of a linebuffer. */
-void
-grow_linebuffer (lbp, toksize)
+/* 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;
-  lbp->buffer = xrnew (lbp->buffer, lbp->size, char);
+  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.  */