-/* Tags file maker to go with GNU Emacs
- Copyright (C) 1984, 87, 88, 89, 93, 94, 95, 98
- Free Software Foundation, Inc. and Ken Arnold
+/* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
+ Copyright (C) 1984, 1987, 1988, 1989, 1993, 1994, 1995,
+ 1998, 1999, 2000, 2001, 2002, 2003, 2004,
+ 2005, 2006 Free Software Foundation, Inc. and Ken Arnold
-This file is not considered part of GNU Emacs.
+ This file is not considered part of GNU Emacs.
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software Foundation,
-Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/*
* Authors:
* Fortran added by Jim Kleckner.
* Ed Pelegri-Llopart added C typedefs.
* Gnu Emacs TAGS format and modifications by RMS?
- * Sam Kendall added C++.
- * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
- * Regexp tags by Tom Tromey.
+ * 1989 Sam Kendall added C++.
+ * 1992 Joseph B. Wells improved C and C++ parsing.
+ * 1993 Francesco Potortì reorganised C and C++.
+ * 1994 Line-by-line regexp tags by Tom Tromey.
+ * 2001 Nested classes by Francesco Potortì (concept by Mykola Dzyuba).
+ * 2002 #line directives by Francesco Potortì.
*
- * Francesco Potorti` (F.Potorti@cnuce.cnr.it) is the current maintainer.
+ * Francesco Potortì <pot@gnu.org> has maintained and improved it since 1993.
*/
-char pot_etags_version[] = "@(#) pot revision number is 12.11";
+/*
+ * If you want to add support for a new language, start by looking at the LUA
+ * language, which is the simplest. Alternatively, consider shipping a
+ * configuration file containing regexp definitions for etags.
+ */
+
+char pot_etags_version[] = "@(#) pot revision number is 17.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
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+ /* On some systems, Emacs defines static as nothing for the sake
+ of unexec. We don't want that here since we don't use unexec. */
+# undef static
+# define ETAGS_REGEXPS /* use the regexp features */
+# define LONG_OPTIONS /* accept long options */
+# ifndef PTR /* for Xemacs */
+# define PTR void *
+# endif
+# ifndef __P /* for Xemacs */
+# define __P(args) args
+# endif
+#else /* no config.h */
+# if defined(__STDC__) && (__STDC__ || defined(__SUNPRO_C))
+# define __P(args) args /* use prototypes */
+# define PTR void * /* for generic pointers */
+# else /* not standard C */
+# define __P(args) () /* no prototypes */
+# define const /* remove const for old compilers' sake */
+# define PTR long * /* don't use void* */
+# endif
+#endif /* !HAVE_CONFIG_H */
+
+#ifndef _GNU_SOURCE
+# define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
+#endif
+
+#ifdef LONG_OPTIONS
+# undef LONG_OPTIONS
+# define LONG_OPTIONS TRUE
+#else
+# define LONG_OPTIONS FALSE
#endif
+/* WIN32_NATIVE is for Xemacs.
+ MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
+#ifdef WIN32_NATIVE
+# undef MSDOS
+# undef WINDOWSNT
+# define WINDOWSNT
+#endif /* WIN32_NATIVE */
+
#ifdef MSDOS
-# include <string.h>
+# undef MSDOS
+# define MSDOS TRUE
# include <fcntl.h>
# include <sys/param.h>
+# include <io.h>
+# ifndef HAVE_CONFIG_H
+# define DOS_NT
+# include <sys/config.h>
+# endif
+#else
+# define MSDOS FALSE
#endif /* MSDOS */
#ifdef WINDOWSNT
# include <stdlib.h>
# include <fcntl.h>
# include <string.h>
+# include <direct.h>
# include <io.h>
# define MAXPATHLEN _MAX_PATH
-#endif
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
- /* On some systems, Emacs defines static as nothing for the sake
- of unexec. We don't want that here since we don't use unexec. */
-# undef static
-# define ETAGS_REGEXPS
-# define LONG_OPTIONS
-#endif
+# undef HAVE_NTGUI
+# undef DOS_NT
+# define DOS_NT
+# ifndef HAVE_GETCWD
+# define HAVE_GETCWD
+# endif /* undef HAVE_GETCWD */
+#else /* not WINDOWSNT */
+# ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <string.h>
+# else /* no standard C headers */
+ extern char *getenv ();
+# ifdef VMS
+# define EXIT_SUCCESS 1
+# define EXIT_FAILURE 0
+# else /* no VMS */
+# define EXIT_SUCCESS 0
+# define EXIT_FAILURE 1
+# endif
+# endif
+#endif /* !WINDOWSNT */
-#if !defined (MSDOS) && !defined (WINDOWSNT) && defined (STDC_HEADERS)
-#include <stdlib.h>
-#include <string.h>
-#endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#else
+# if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
+ extern char *getcwd (char *buf, size_t size);
+# endif
+#endif /* HAVE_UNISTD_H */
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#ifndef errno
-extern int errno;
+ extern int errno;
#endif
#include <sys/types.h>
#include <sys/stat.h>
-/* This is to declare getcwd. */
-#ifdef HAVE_UNISTD_H
-#include <unistd.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
-#ifdef LONG_OPTIONS
+#if LONG_OPTIONS
# include <getopt.h>
#else
# define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
#endif /* LONG_OPTIONS */
#ifdef ETAGS_REGEXPS
+# ifndef HAVE_CONFIG_H /* this is a standalone compilation */
+# ifdef __CYGWIN__ /* compiling on Cygwin */
+ !!! NOTICE !!!
+ the regex.h distributed with Cygwin is not compatible with etags, alas!
+If you want regular expression support, you should delete this notice and
+ arrange to use the GNU regex.h and regex.c.
+# endif
+# endif
# include <regex.h>
#endif /* ETAGS_REGEXPS */
# define CTAGS FALSE
#endif
-/* Exit codes for success and failure. */
-#ifdef VMS
-# define GOOD 1
-# define BAD 0
-#else
-# define GOOD 0
-# 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 strcaseeq(s,t) (assert((s)!=NULL && (t)!=NULL), !etags_strcasecmp (s, t))
+#define strneq(s,t,n) (assert((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
+#define strncaseeq(s,t,n) (assert((s)!=NULL && (t)!=NULL), !etags_strncasecmp (s, t, n))
#define CHARS 256 /* 2^sizeof(char) */
-#define CHAR(x) ((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 CHAR(x) ((unsigned int)(x) & (CHARS - 1))
+#define iswhite(c) (_wht[CHAR(c)]) /* c is white (see white) */
+#define notinname(c) (_nin[CHAR(c)]) /* c is not in a name (see nonam) */
+#define begtoken(c) (_btk[CHAR(c)]) /* c can start token (see begtk) */
+#define intoken(c) (_itk[CHAR(c)]) /* c can be in token (see midtk) */
+#define endtoken(c) (_etk[CHAR(c)]) /* c ends tokens (see endtk) */
-#ifdef DOS_NT
-# define absolutefn(fn) (fn[0] == '/' \
- || (fn[1] == ':' && fn[2] == '/'))
-#else
-# define absolutefn(fn) (fn[0] == '/')
-#endif
+#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 -- allocate storage
+ * xnew, xrnew -- allocate, reallocate storage
*
* SYNOPSIS: Type *xnew (int n, Type);
+ * void xrnew (OldPointer, int n, Type);
*/
-#ifdef chkmalloc
+#if DEBUG
# include "chkmalloc.h"
-# define xnew(n,Type) ((Type *) trace_xmalloc (__FILE__, __LINE__, \
- (n) * sizeof (Type)))
+# define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
+ (n) * sizeof (Type)))
+# define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
+ (char *) (op), (n) * sizeof (Type)))
#else
-# define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
+# define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
+# define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
+ (char *) (op), (n) * sizeof (Type)))
#endif
-typedef int bool;
+#define bool int
+
+typedef void Lang_function __P((FILE *));
-typedef struct nd_st
-{ /* sorting structure */
- char *name; /* function or type name */
- char *file; /* file name */
- bool is_func; /* use pattern or line no */
- bool been_warned; /* set if noticed dup */
- int lno; /* line number tag is on */
+typedef struct
+{
+ char *suffix; /* file name suffix for this compressor */
+ char *command; /* takes one arg and decompresses to stdout */
+} compressor;
+
+typedef struct
+{
+ char *name; /* language name */
+ char *help; /* detailed help for the language */
+ Lang_function *function; /* parse function */
+ char **suffixes; /* name suffixes of this language's files */
+ char **filenames; /* names of this language's files */
+ char **interpreters; /* interpreters for this language */
+ bool metasource; /* source used to generate other sources */
+} language;
+
+typedef struct fdesc
+{
+ struct fdesc *next; /* for the linked list */
+ char *infname; /* uncompressed input file name */
+ char *infabsname; /* absolute uncompressed input file name */
+ char *infabsdir; /* absolute dir of input file */
+ char *taggedfname; /* file name to write in tagfile */
+ language *lang; /* language of file */
+ char *prop; /* file properties to write in tagfile */
+ bool usecharno; /* etags tags shall contain char number */
+ bool written; /* entry written in the tags file */
+} fdesc;
+
+typedef struct node_st
+{ /* sorting structure */
+ struct node_st *left, *right; /* left and right sons */
+ fdesc *fdp; /* description of file to whom tag belongs */
+ char *name; /* tag name */
+ char *regex; /* search regexp */
+ bool valid; /* write this tag on the tag file */
+ bool is_func; /* function tag: use regexp in CTAGS mode */
+ bool been_warned; /* warning already given for duplicated tag */
+ int lno; /* line number tag is on */
long cno; /* character number line starts on */
- char *pat; /* search pattern */
- struct nd_st *left, *right; /* left and right sons */
-} NODE;
+} node;
+
+/*
+ * A `linebuffer' is a structure which holds a line of text.
+ * `readline_internal' reads a line from a stream into a linebuffer
+ * and works regardless of the length of the line.
+ * SIZE is the size of BUFFER, LEN is the length of the string in
+ * BUFFER after readline reads it.
+ */
+typedef struct
+{
+ long size;
+ int len;
+ char *buffer;
+} linebuffer;
-extern char *getenv ();
+/* Used to support mixing of --lang and file names. */
+typedef struct
+{
+ enum {
+ at_language, /* a language specification */
+ at_regexp, /* a regular expression */
+ at_filename, /* a file name */
+ at_stdin, /* read from stdin here */
+ at_end /* stop parsing the list */
+ } arg_type; /* argument type */
+ language *lang; /* language associated with the argument */
+ char *what; /* the argument itself */
+} argument;
+
+#ifdef ETAGS_REGEXPS
+/* Structure defining a regular expression. */
+typedef struct regexp
+{
+ struct regexp *p_next; /* pointer to next in list */
+ language *lang; /* if set, use only for this language */
+ char *pattern; /* the regexp pattern */
+ char *name; /* tag name */
+ struct re_pattern_buffer *pat; /* the compiled pattern */
+ struct re_registers regs; /* re registers */
+ bool error_signaled; /* already signaled for this regexp */
+ bool force_explicit_name; /* do not allow implict tag name */
+ bool ignore_case; /* ignore case when matching */
+ bool multi_line; /* do a multi-line match on the whole file */
+} regexp;
+#endif /* ETAGS_REGEXPS */
-char *concat ();
-char *savenstr (), *savestr ();
-char *etags_strchr (), *etags_strrchr ();
-char *etags_getcwd ();
-char *relative_filename (), *absolute_filename (), *absolute_dirname ();
-void grow_linebuffer ();
-long *xmalloc (), *xrealloc ();
-typedef void Lang_function ();
/* Many compilers barf on this:
- Lang_function Asm_labels;
+ Lang_function Ada_funcs;
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 Scheme_functions ();
-void TeX_functions ();
-void just_read_file ();
-
-Lang_function *get_language_from_name ();
-Lang_function *get_language_from_interpreter ();
-Lang_function *get_language_from_suffix ();
-int total_size_of_entries ();
-long readline ();
-long readline_internal ();
+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 Forth_words __P((FILE *));
+static void Fortran_functions __P((FILE *));
+static void HTML_labels __P((FILE *));
+static void Lisp_functions __P((FILE *));
+static void Lua_functions __P((FILE *));
+static void Makefile_targets __P((FILE *));
+static void Pascal_functions __P((FILE *));
+static void Perl_functions __P((FILE *));
+static void PHP_functions __P((FILE *));
+static void PS_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 Yacc_entries __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((argument *));
+int main __P((int, char **));
+
+static compressor *get_compressor_from_suffix __P((char *, char **));
+static language *get_language_from_langname __P((const char *));
+static language *get_language_from_interpreter __P((char *));
+static language *get_language_from_filename __P((char *, bool));
+static void readline __P((linebuffer *, FILE *));
+static long readline_internal __P((linebuffer *, FILE *));
+static bool nocase_tail __P((char *));
+static void get_tag __P((char *, char **));
+
#ifdef ETAGS_REGEXPS
-void analyse_regex ();
-void add_regex ();
+static void analyse_regex __P((char *));
+static void free_regexps __P((void));
+static void regex_tag_multiline __P((void));
#endif /* ETAGS_REGEXPS */
-void add_node ();
-void error ();
-void suggest_asking_for_help ();
-void fatal (), pfatal ();
-void find_entries ();
-void free_tree ();
-void getit ();
-void init ();
-void initbuffer ();
-void pfnote (), new_pfnote ();
-void process_file ();
-void put_entries ();
-void takeprec ();
+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 process_file_name __P((char *, language *));
+static void process_file __P((FILE *, char *, language *));
+static void find_entries __P((FILE *));
+static void free_tree __P((node *));
+static void free_fdesc __P((fdesc *));
+static void pfnote __P((char *, bool, char *, int, int, long));
+static void make_tag __P((char *, int, bool, char *, int, int, long));
+static void invalidate_nodes __P((fdesc *, node **));
+static void put_entries __P((node *));
+
+static char *concat __P((char *, char *, char *));
+static char *skip_spaces __P((char *));
+static char *skip_non_spaces __P((char *));
+static char *savenstr __P((char *, int));
+static char *savestr __P((char *));
+static char *etags_strchr __P((const char *, int));
+static char *etags_strrchr __P((const char *, int));
+static int etags_strcasecmp __P((const char *, const char *));
+static int etags_strncasecmp __P((const char *, 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_init __P((linebuffer *));
+static void linebuffer_setlen __P((linebuffer *, int));
+static PTR xmalloc __P((unsigned int));
+static PTR xrealloc __P((char *, unsigned int));
\f
-char searchar = '/'; /* use /.../ searches */
+static char searchar = '/'; /* use /.../ searches */
-int lineno; /* line number of current line */
-long charno; /* current character number */
-long linecharno; /* charno of start of line */
+static char *tagfile; /* output file */
+static char *progname; /* name this program was invoked with */
+static char *cwd; /* current working directory */
+static char *tagfiledir; /* directory of tagfile */
+static FILE *tagf; /* ioptr for tags file */
-char *curfile; /* current input file name */
-char *tagfile; /* output file */
-char *progname; /* name this program was invoked with */
-char *cwd; /* current working directory */
-char *tagfiledir; /* directory of tagfile */
+static fdesc *fdhead; /* head of file description list */
+static fdesc *curfdp; /* current file description */
+static int lineno; /* line number of current line */
+static long charno; /* current character number */
+static long linecharno; /* charno of start of current line */
+static char *dbp; /* pointer to start of current tag */
-FILE *tagf; /* ioptr for tags file */
-NODE *head; /* the head of the binary tree of tags */
+static const int invalidcharno = -1;
-/*
- * A `struct linebuffer' is a structure which holds a line of text.
- * `readline' reads a line from a stream into a linebuffer and works
- * regardless of the length of the line.
- * SIZE is the size of BUFFER, LEN is the length of the string in
- * BUFFER after readline reads it.
- */
-struct linebuffer
-{
- long size;
- int len;
- char *buffer;
-};
+static node *nodehead; /* the head of the binary tree of tags */
+static node *last_node; /* the last node created */
-struct linebuffer lb; /* the current line */
-struct linebuffer token_name; /* used by C_entries as a temporary area */
-struct
-{
- long linepos;
- struct linebuffer lb; /* used by C_entries instead of lb */
-} lbs[2];
+static linebuffer lb; /* the current line */
+static linebuffer filebuf; /* a buffer containing the whole file */
+static linebuffer token_name; /* a buffer containing a tag name */
/* boolean "functions" (see init) */
-bool _wht[CHARS], _nin[CHARS], _itk[CHARS], _btk[CHARS], _etk[CHARS];
-char
+static bool _wht[CHARS], _nin[CHARS], _itk[CHARS], _btk[CHARS], _etk[CHARS];
+static char
/* white chars */
- *white = " \f\t\n\013",
+ *white = " \f\t\n\r\v",
/* not in a name */
- *nonam =" \f\t\n\013(=,[;",
+ *nonam = " \f\t\n\r()=,;", /* look at make_tag before modifying! */
/* token ending chars */
- *endtk = " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?",
+ *endtk = " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
/* token starting chars */
*begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
/* valid in-token chars */
*midtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
-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 */
+static bool append_to_tagfile; /* -a: append to tags */
+/* The next four default to TRUE for etags, but to FALSE for ctags. */
+static bool typedefs; /* -t: create tags for C and Ada typedefs */
+static bool typedefs_or_cplusplus; /* -T: create tags for C typedefs, level */
/* 0 struct/enum/union decls, and C++ */
/* member functions. */
-bool constantypedefs; /* -d: create tags for C #define, enum */
+static bool constantypedefs; /* -d: create tags for C #define, enum */
/* constants and variables. */
/* -D: opposite of -d. Default under ctags. */
-bool globals; /* create tags for C global variables */
-bool members; /* create tags for C member variables */
-bool update; /* -u: update tags */
-bool vgrind_style; /* -v: create vgrind style index output */
-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 */
+static bool globals; /* create tags for global variables */
+static bool declarations; /* --declarations: tag them and extern in C&Co*/
+static bool members; /* create tags for C member variables */
+static bool no_line_directive; /* ignore #line directives (undocumented) */
+static bool update; /* -u: update tags */
+static bool vgrind_style; /* -v: create vgrind style index output */
+static bool no_warnings; /* -w: suppress warnings */
+static bool cxref_style; /* -x: create cxref style output */
+static bool cplusplus; /* .[hc] means C++, not C */
+static bool ignoreindent; /* -I: ignore indentation in C */
+static bool packages_only; /* --packages-only: in Ada, only tag packages*/
+
+/* STDIN is defined in LynxOS system headers */
+#ifdef STDIN
+# undef STDIN
+#endif
-#ifdef LONG_OPTIONS
-struct option longopts[] =
+#define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
+static bool parsing_stdin; /* --parse-stdin used */
+
+#ifdef ETAGS_REGEXPS
+static regexp *p_head; /* list of all regexps */
+static bool need_filebuf; /* some regexes are multi-line */
+#else
+# define need_filebuf FALSE
+#endif /* ETAGS_REGEXPS */
+
+#if LONG_OPTIONS
+static 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 }
+ { "append", no_argument, NULL, 'a' },
+ { "packages-only", no_argument, &packages_only, TRUE },
+ { "c++", no_argument, NULL, 'C' },
+ { "declarations", no_argument, &declarations, TRUE },
+ { "no-line-directive", no_argument, &no_line_directive, TRUE },
+ { "help", no_argument, NULL, 'h' },
+ { "help", no_argument, NULL, 'H' },
+ { "ignore-indentation", no_argument, NULL, 'I' },
+ { "language", required_argument, NULL, 'l' },
+ { "members", no_argument, &members, TRUE },
+ { "no-members", no_argument, &members, FALSE },
+ { "output", required_argument, NULL, 'o' },
+#ifdef ETAGS_REGEXPS
+ { "regex", required_argument, NULL, 'r' },
+ { "no-regex", no_argument, NULL, 'R' },
+ { "ignore-case-regex", required_argument, NULL, 'c' },
+#endif /* ETAGS_REGEXPS */
+ { "parse-stdin", required_argument, NULL, STDIN },
+ { "version", no_argument, NULL, 'V' },
+
+#if CTAGS /* Ctags options */
+ { "backward-search", no_argument, NULL, 'B' },
+ { "cxref", no_argument, NULL, 'x' },
+ { "defines", no_argument, NULL, 'd' },
+ { "globals", no_argument, &globals, TRUE },
+ { "typedefs", no_argument, NULL, 't' },
+ { "typedefs-and-c++", no_argument, NULL, 'T' },
+ { "update", no_argument, NULL, 'u' },
+ { "vgrind", no_argument, NULL, 'v' },
+ { "no-warn", no_argument, NULL, 'w' },
+
+#else /* Etags options */
+ { "no-defines", no_argument, NULL, 'D' },
+ { "no-globals", no_argument, &globals, FALSE },
+ { "include", required_argument, NULL, 'i' },
+#endif
+ { NULL }
};
#endif /* LONG_OPTIONS */
-#ifdef ETAGS_REGEXPS
-/* Structure defining a regular expression. Elements are
- the compiled pattern, and the name string. */
-struct pattern
+static compressor compressors[] =
{
- struct re_pattern_buffer *pattern;
- struct re_registers regs;
- char *name_pattern;
- bool error_signaled;
+ { "z", "gzip -d -c"},
+ { "Z", "gzip -d -c"},
+ { "gz", "gzip -d -c"},
+ { "GZ", "gzip -d -c"},
+ { "bz2", "bzip2 -d -c" },
+ { NULL }
};
-/* Number of regexps found. */
-int num_patterns = 0;
-
-/* Array of all regexps. */
-struct pattern *patterns = NULL;
-#endif /* ETAGS_REGEXPS */
-
/*
* Language stuff.
*/
-/* Non-NULL if language fixed. */
-Lang_function *lang_func = NULL;
+/* Ada code */
+static char *Ada_suffixes [] =
+ { "ads", "adb", "ada", NULL };
+static char Ada_help [] =
+"In Ada code, functions, procedures, packages, tasks and types are\n\
+tags. Use the `--packages-only' option to create tags for\n\
+packages only.\n\
+Ada tag names have suffixes indicating the type of entity:\n\
+ Entity type: Qualifier:\n\
+ ------------ ----------\n\
+ function /f\n\
+ procedure /p\n\
+ package spec /s\n\
+ package body /b\n\
+ type /t\n\
+ task /k\n\
+Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
+body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
+will just search for any tag `bidule'.";
/* Assembly code */
-char *Asm_suffixes [] = { "a", /* Unix assembler */
- "asm", /* Microcontroller assembly */
- "def", /* BSO/Tasking definition includes */
- "inc", /* Microcontroller include files */
- "ins", /* Microcontroller include files */
- "s", "sa", /* Unix assembler */
- "S", /* cpp-processed Unix assembler */
- "src", /* BSO/Tasking C compiler output */
- NULL
- };
+static char *Asm_suffixes [] =
+ { "a", /* Unix assembler */
+ "asm", /* Microcontroller assembly */
+ "def", /* BSO/Tasking definition includes */
+ "inc", /* Microcontroller include files */
+ "ins", /* Microcontroller include files */
+ "s", "sa", /* Unix assembler */
+ "S", /* cpp-processed Unix assembler */
+ "src", /* BSO/Tasking C compiler output */
+ NULL
+ };
+static char Asm_help [] =
+"In assembler code, labels appearing at the beginning of a line,\n\
+followed by a colon, are tags.";
+
/* Note that .c and .h can be considered C++, if the --c++ flag was
- given. That is why default_C_entries is called here. */
-char *default_C_suffixes [] =
+ given, or if the `class' or `template' keyowrds are met inside the file.
+ That is why default_C_entries is called for these. */
+static char *default_C_suffixes [] =
{ "c", "h", NULL };
-
-char *Cplusplus_suffixes [] =
- { "C", "H", "c++", "cc", "cpp", "cxx", "h++", "hh", "hpp", "hxx",
+static char default_C_help [] =
+"In C code, any C function or typedef is a tag, and so are\n\
+definitions of `struct', `union' and `enum'. `#define' macro\n\
+definitions and `enum' constants are tags unless you specify\n\
+`--no-defines'. Global variables are tags unless you specify\n\
+`--no-globals'. Use of `--no-globals' and `--no-defines'\n\
+can make the tags table file much smaller.\n\
+You can tag function declarations and external variables by\n\
+using `--declarations', and struct members by using `--members'.";
+
+static char *Cplusplus_suffixes [] =
+ { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
"M", /* Objective C++ */
"pdb", /* Postscript with C syntax */
NULL };
-
-char *Cjava_suffixes [] =
+static char Cplusplus_help [] =
+"In C++ code, all the tag constructs of C code are tagged. (Use\n\
+--help --lang=c --lang=c++ for full help.)\n\
+In addition to C tags, member functions are also recognized, and\n\
+optionally member variables if you use the `--members' option.\n\
+Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
+and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
+`operator+'.";
+
+static char *Cjava_suffixes [] =
{ "java", NULL };
+static char Cjava_help [] =
+"In Java code, all the tags constructs of C and C++ code are\n\
+tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
-char *Cobol_suffixes [] =
+
+static char *Cobol_suffixes [] =
{ "COB", "cob", NULL };
+static char Cobol_help [] =
+"In Cobol code, tags are paragraph names; that is, any word\n\
+starting in column 8 and followed by a period.";
-char *Cstar_suffixes [] =
+static char *Cstar_suffixes [] =
{ "cs", "hs", NULL };
-char *Erlang_suffixes [] =
+static char *Erlang_suffixes [] =
{ "erl", "hrl", NULL };
+static char Erlang_help [] =
+"In Erlang code, the tags are the functions, records and macros\n\
+defined in the file.";
-char *Fortran_suffixes [] =
- { "F", "f", "f90", "for", NULL };
+char *Forth_suffixes [] =
+ { "fth", "tok", NULL };
+static char Forth_help [] =
+"In Forth code, tags are words defined by `:',\n\
+constant, code, create, defer, value, variable, buffer:, field.";
-char *Lisp_suffixes [] =
- { "cl", "clisp", "el", "l", "lisp", "lsp", "ml", NULL };
+static char *Fortran_suffixes [] =
+ { "F", "f", "f90", "for", NULL };
+static char Fortran_help [] =
+"In Fortran code, functions, subroutines and block data are tags.";
+
+static char *HTML_suffixes [] =
+ { "htm", "html", "shtml", NULL };
+static char HTML_help [] =
+"In HTML input files, the tags are the `title' and the `h1', `h2',\n\
+`h3' headers. Also, tags are `name=' in anchors and all\n\
+occurrences of `id='.";
+
+static char *Lisp_suffixes [] =
+ { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL };
+static char Lisp_help [] =
+"In Lisp code, any function defined with `defun', any variable\n\
+defined with `defvar' or `defconst', and in general the first\n\
+argument of any expression that starts with `(def' in column zero\n\
+is a tag.";
+
+static char *Lua_suffixes [] =
+ { "lua", "LUA", NULL };
+static char Lua_help [] =
+"In Lua scripts, all functions are tags.";
+
+static char *Makefile_filenames [] =
+ { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL};
+static char Makefile_help [] =
+"In makefiles, targets are tags; additionally, variables are tags\n\
+unless you specify `--no-globals'.";
+
+static char *Objc_suffixes [] =
+ { "lm", /* Objective lex file */
+ "m", /* Objective C file */
+ NULL };
+static char Objc_help [] =
+"In Objective C code, tags include Objective C definitions for classes,\n\
+class categories, methods and protocols. Tags for variables and\n\
+functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.";
-char *Pascal_suffixes [] =
+static char *Pascal_suffixes [] =
{ "p", "pas", NULL };
+static char Pascal_help [] =
+"In Pascal code, the tags are the functions and procedures defined\n\
+in the file.";
-char *Perl_suffixes [] =
+static char *Perl_suffixes [] =
{ "pl", "pm", NULL };
-char *Perl_interpreters [] =
+static char *Perl_interpreters [] =
{ "perl", "@PERL@", NULL };
-
-char *plain_C_suffixes [] =
+static char Perl_help [] =
+"In Perl code, the tags are the packages, subroutines and variables\n\
+defined by the `package', `sub', `my' and `local' keywords. Use\n\
+`--globals' if you want to tag global variables. Tags for\n\
+subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
+defined in the default package is `main::SUB'.";
+
+static char *PHP_suffixes [] =
+ { "php", "php3", "php4", NULL };
+static char PHP_help [] =
+"In PHP code, tags are functions, classes and defines. When using\n\
+the `--members' option, vars are tags too.";
+
+static char *plain_C_suffixes [] =
{ "pc", /* Pro*C file */
- "m", /* Objective C file */
- "lm", /* Objective lex file */
NULL };
-char *Postscript_suffixes [] =
- { "ps", NULL };
+static char *PS_suffixes [] =
+ { "ps", "psw", NULL }; /* .psw is for PSWrap */
+static char PS_help [] =
+"In PostScript code, the tags are the functions.";
-char *Prolog_suffixes [] =
+static char *Prolog_suffixes [] =
{ "prolog", NULL };
+static char Prolog_help [] =
+"In Prolog code, tags are predicates and rules at the beginning of\n\
+line.";
+
+static char *Python_suffixes [] =
+ { "py", NULL };
+static char Python_help [] =
+"In Python code, `def' or `class' at the beginning of a line\n\
+generate a tag.";
/* Can't do the `SCM' or `scm' prefix with a version number. */
-char *Scheme_suffixes [] =
- { "SCM", "SM", "oak", "sch", "scheme", "scm", "sm", "t", NULL };
+static char *Scheme_suffixes [] =
+ { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL };
+static char Scheme_help [] =
+"In Scheme code, tags include anything defined with `def' or with a\n\
+construct whose name starts with `def'. They also include\n\
+variables set with `set!' at top level in the file.";
+
+static char *TeX_suffixes [] =
+ { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL };
+static char TeX_help [] =
+"In LaTeX text, the argument of any of the commands `\\chapter',\n\
+`\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
+`\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
+`\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
+`\\newenvironment' or `\\renewenvironment' is a tag.\n\
+\n\
+Other commands can be specified by setting the environment variable\n\
+`TEXTAGS' to a colon-separated list like, for example,\n\
+ TEXTAGS=\"mycommand:myothercommand\".";
-char *TeX_suffixes [] =
- { "TeX", "bib", "clo", "cls", "ltx", "sty", "tex", NULL };
-char *Yacc_suffixes [] =
- { "y", "ym", "yy", "yxx", "y++", NULL }; /* .ym is Objective yacc file */
+static char *Texinfo_suffixes [] =
+ { "texi", "texinfo", "txi", NULL };
+static char Texinfo_help [] =
+"for texinfo files, lines starting with @node are tagged.";
-/* Table of language names and corresponding functions, file suffixes
- and interpreter names.
- It is ok for a given function to be listed under more than one
- name. I just didn't. */
-struct lang_entry
-{
- char *name;
- Lang_function *function;
- char **suffixes;
- char **interpreters;
-};
+static char *Yacc_suffixes [] =
+ { "y", "y++", "ym", "yxx", "yy", NULL }; /* .ym is Objective yacc file */
+static char Yacc_help [] =
+"In Bison or Yacc input files, each rule defines as a tag the\n\
+nonterminal it constructs. The portions of the file that contain\n\
+C code are parsed as C code (use --help --lang=c --lang=yacc\n\
+for full help).";
+
+static char auto_help [] =
+"`auto' is not a real language, it indicates to use\n\
+a default language for files base on file name suffix and file contents.";
+
+static char none_help [] =
+"`none' is not a real language, it indicates to only do\n\
+regexp processing on files.";
+
+static char no_lang_help [] =
+"No detailed help available for this language.";
+
+
+/*
+ * Table of languages.
+ *
+ * It is ok for a given function to be listed under more than one
+ * name. I just didn't.
+ */
-struct lang_entry lang_names [] =
+static language lang_names [] =
{
- { "asm", Asm_labels, Asm_suffixes, NULL },
- { "c", default_C_entries, default_C_suffixes, NULL },
- { "c++", Cplusplus_entries, Cplusplus_suffixes, NULL },
- { "c*", Cstar_entries, Cstar_suffixes, NULL },
- { "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 },
- { "scheme", Scheme_functions, Scheme_suffixes, NULL },
- { "tex", TeX_functions, TeX_suffixes, NULL },
- { "yacc", Yacc_entries, Yacc_suffixes, NULL },
- { "auto", NULL }, /* default guessing scheme */
- { "none", just_read_file }, /* regexp matching only */
- { NULL, NULL } /* end of list */
+ { "ada", Ada_help, Ada_funcs, Ada_suffixes },
+ { "asm", Asm_help, Asm_labels, Asm_suffixes },
+ { "c", default_C_help, default_C_entries, default_C_suffixes },
+ { "c++", Cplusplus_help, Cplusplus_entries, Cplusplus_suffixes },
+ { "c*", no_lang_help, Cstar_entries, Cstar_suffixes },
+ { "cobol", Cobol_help, Cobol_paragraphs, Cobol_suffixes },
+ { "erlang", Erlang_help, Erlang_functions, Erlang_suffixes },
+ { "forth", Forth_help, Forth_words, Forth_suffixes },
+ { "fortran", Fortran_help, Fortran_functions, Fortran_suffixes },
+ { "html", HTML_help, HTML_labels, HTML_suffixes },
+ { "java", Cjava_help, Cjava_entries, Cjava_suffixes },
+ { "lisp", Lisp_help, Lisp_functions, Lisp_suffixes },
+ { "lua", Lua_help, Lua_functions, Lua_suffixes },
+ { "makefile", Makefile_help,Makefile_targets,NULL,Makefile_filenames},
+ { "objc", Objc_help, plain_C_entries, Objc_suffixes },
+ { "pascal", Pascal_help, Pascal_functions, Pascal_suffixes },
+ { "perl",Perl_help,Perl_functions,Perl_suffixes,NULL,Perl_interpreters},
+ { "php", PHP_help, PHP_functions, PHP_suffixes },
+ { "postscript",PS_help, PS_functions, PS_suffixes },
+ { "proc", no_lang_help, plain_C_entries, plain_C_suffixes },
+ { "prolog", Prolog_help, Prolog_functions, Prolog_suffixes },
+ { "python", Python_help, Python_functions, Python_suffixes },
+ { "scheme", Scheme_help, Scheme_functions, Scheme_suffixes },
+ { "tex", TeX_help, TeX_commands, TeX_suffixes },
+ { "texinfo", Texinfo_help, Texinfo_nodes, Texinfo_suffixes },
+ { "yacc", Yacc_help,Yacc_entries,Yacc_suffixes,NULL,NULL,TRUE},
+ { "auto", auto_help }, /* default guessing scheme */
+ { "none", none_help, just_read_file }, /* regexp matching only */
+ { NULL } /* end of list */
};
\f
-void
+static void
print_language_names ()
{
- struct lang_entry *lang;
- char **ext;
+ language *lang;
+ char **name, **ext;
puts ("\nThese are the currently supported languages, along with the\n\
-default file name suffixes:");
+default file names and dot suffixes:");
for (lang = lang_names; lang->name != NULL; lang++)
{
- printf ("\t%s\t", lang->name);
+ printf (" %-*s", 10, lang->name);
+ if (lang->filenames != NULL)
+ for (name = lang->filenames; *name != NULL; name++)
+ printf (" %s", *name);
if (lang->suffixes != NULL)
for (ext = lang->suffixes; *ext != NULL; ext++)
printf (" .%s", *ext);
puts ("");
}
- puts ("Where `auto' means use default language for files based on file\n\
+ puts ("where `auto' means use default language for files based on file\n\
name suffix, and `none' means only do regexp processing on files.\n\
If no language is specified and no matching suffix is found,\n\
the first line of the file is read for a sharp-bang (#!) sequence\n\
followed by the name of an interpreter. If no such sequence is found,\n\
-Fortran is tried first; if no tags are found, C is tried next.");
+Fortran is tried first; if no tags are found, C is tried next.\n\
+When parsing any C file, a \"class\" or \"template\" keyword\n\
+switches to C++.");
+ puts ("Compressed files are supported using gzip and bzip2.\n\
+\n\
+For detailed help on a given language use, for example,\n\
+etags --help --lang=ada.");
}
+#ifndef EMACS_NAME
+# define EMACS_NAME "standalone"
+#endif
#ifndef VERSION
-# define VERSION "19"
+# define VERSION "version"
#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) 2006 Free Software Foundation, Inc. and Ken Arnold");
puts ("This program is distributed under the same terms as Emacs");
- exit (GOOD);
+ exit (EXIT_SUCCESS);
}
-void
-print_help ()
+static void
+print_help (argbuffer)
+ argument *argbuffer;
{
+ bool help_for_lang = FALSE;
+
+ for (; argbuffer->arg_type != at_end; argbuffer++)
+ if (argbuffer->arg_type == at_language)
+ {
+ if (help_for_lang)
+ puts ("");
+ puts (argbuffer->lang->help);
+ help_for_lang = TRUE;
+ }
+
+ if (help_for_lang)
+ exit (EXIT_SUCCESS);
+
printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
\n\
These are the options accepted by %s.\n", progname, progname);
-#ifdef LONG_OPTIONS
- puts ("You may use unambiguous abbreviations for the long option names.");
-#else
- puts ("Long option names do not work with this executable, as it is not\n\
+ if (LONG_OPTIONS)
+ puts ("You may use unambiguous abbreviations for the long option names.");
+ else
+ puts ("Long option names do not work with this executable, as it is not\n\
linked with GNU getopt.");
-#endif /* LONG_OPTIONS */
- puts ("A - as file name means read names from stdin.");
- if (!CTAGS)
- printf (" Absolute names are stored in the output file as they\n\
-are. Relative ones are stored relative to the output file's directory.");
- puts ("\n");
+ puts (" A - as file name means read names from stdin (one per line).\n\
+Absolute names are stored in the output file as they are.\n\
+Relative ones are stored relative to the output file's directory.\n");
puts ("-a, --append\n\
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" or "template" 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\
This makes the tags file smaller.");
if (!CTAGS)
- {
- puts ("-i FILE, --include=FILE\n\
+ puts ("-i FILE, --include=FILE\n\
Include a note in tag file indicating that, when searching for\n\
a tag, one should also consult the tags file FILE after\n\
checking the current file.");
- puts ("-l LANG, --language=LANG\n\
+
+ puts ("-l LANG, --language=LANG\n\
Force the following files to be considered as written in the\n\
named language up to the next --language=LANG option.");
- }
if (CTAGS)
puts ("--globals\n\
- Create tag entries for global variables in C and derived languages.");
+ Create tag entries for global variables in some languages.");
else
puts ("--no-globals\n\
- Do not create tag entries for global variables in C and\n\
- derived languages. This makes the tags file smaller.");
+ Do not create tag entries for global variables in some\n\
+ languages. This makes the tags file smaller.");
puts ("--members\n\
- Create tag entries for member variables in C and derived languages.");
+ Create tag entries for members of structures in some languages.");
#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\
- --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
+ puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
+ Make a tag for each line matching a regular expression pattern\n\
+ in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
+ files only. REGEXFILE is a file containing one REGEXP per line.\n\
+ REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
+ optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
+ puts (" If TAGNAME/ is present, the tags created are named.\n\
+ For example Tcl named tags can be created with:\n\
+ --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
+ MODS are optional one-letter modifiers: `i' means to ignore case,\n\
+ `m' means to allow multi-line matches, `s' implies `m' and\n\
+ causes dot to match any character, including newline.");
puts ("-R, --no-regex\n\
Don't create tags from regexps for the following files.");
#endif /* ETAGS_REGEXPS */
+ puts ("-I, --ignore-indentation\n\
+ In C and C++ do not assume that a closing brace in the first\n\
+ column is the final brace of a function or structure definition.");
puts ("-o FILE, --output=FILE\n\
Write the tags to FILE.");
- puts ("-I, --ignore-indentation\n\
- Don't rely on indentation quite as much as normal. Currently,\n\
- this means not to assume that a closing brace in the first\n\
- column is the final brace of a function or structure\n\
- definition in C and C++.");
+ puts ("--parse-stdin=NAME\n\
+ Read from standard input and record tags as belonging to file NAME.");
if (CTAGS)
{
puts ("-t, --typedefs\n\
- Generate tag entries for C typedefs.");
+ Generate tag entries for C and Ada typedefs.");
puts ("-T, --typedefs-and-c++\n\
Generate tag entries for C typedefs, C struct/enum/union tags,\n\
and C++ member functions.");
- puts ("-u, --update\n\
+ }
+
+ if (CTAGS)
+ puts ("-u, --update\n\
Update the tag entries for the given files, leaving tag\n\
entries for other files in place. Currently, this is\n\
implemented by deleting the existing entries for the given\n\
files and then rewriting the new entries at the end of the\n\
tags file. It is often faster to simply rebuild the entire\n\
tag file than to use this.");
+
+ if (CTAGS)
+ {
puts ("-v, --vgrind\n\
- Generates an index of items intended for human consumption,\n\
- similar to the output of vgrind. The index is sorted, and\n\
- gives the page number of each item.");
+ Print on the standard output an index of items intended for\n\
+ human consumption, similar to the output of vgrind. The index\n\
+ is sorted, and gives the page number of each item.");
puts ("-w, --no-warn\n\
Suppress warning messages about entries defined in multiple\n\
files.");
puts ("-V, --version\n\
Print the version of the program.\n\
-h, --help\n\
- Print this help message.");
+ Print this help message.\n\
+ Followed by one or more `--language' options prints detailed\n\
+ help about tag generation for the specified languages.");
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);
+ exit (EXIT_SUCCESS);
}
\f
-enum argument_type
-{
- at_language,
- at_regexp,
- at_filename
-};
-
-/* This structure helps us allow mixing of --lang and file names. */
-typedef struct
-{
- enum argument_type arg_type;
- char *what;
- Lang_function *function;
-} argument;
-
#ifdef VMS /* VMS specific functions */
#define EOS '\0'
#include <rmsdef.h>
#include <descrip.h>
#define OUTSIZE MAX_FILE_SPEC_LEN
-short
+static short
fn_exp (out, in)
vspec *out;
char *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;
int i;
unsigned int nincluded_files;
char **included_files;
- char *this_file;
argument *argbuffer;
int current_arg, file_count;
- struct linebuffer filename_lb;
+ linebuffer filename_lb;
+ bool help_asked = FALSE;
#ifdef VMS
bool got_err;
#endif
+ char *optstring;
+ int opt;
+
#ifdef DOS_NT
_fmode = O_BINARY; /* all of files are treated as binary files */
is small. */
argbuffer = xnew (argc, argument);
-#ifdef ETAGS_REGEXPS
- /* Set syntax for regular expression routines. */
- re_set_syntax (RE_SYNTAX_EMACS);
-#endif /* ETAGS_REGEXPS */
-
/*
* If etags, always find typedefs and structure tags. Why not?
- * Also default is to find macro constants, 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;
- members = FALSE;
}
- while (1)
- {
- int opt;
- char *optstring;
-
+ /* When the optstring begins with a '-' getopt_long does not rearrange the
+ non-options arguments to be at the end, but leaves them alone. */
+ optstring = "-";
#ifdef ETAGS_REGEXPS
- optstring = "-aCdDf:Il:o:r:RStTi:BuvxwVhH";
-#else
- optstring = "-aCdDf:Il:o:StTi:BuvxwVhH";
+ optstring = "-r:Rc:";
#endif /* ETAGS_REGEXPS */
+ if (!LONG_OPTIONS)
+ optstring += 1; /* remove the initial '-' */
+ optstring = concat (optstring,
+ "aCf:Il:o:SVhH",
+ (CTAGS) ? "BxdtTuvw" : "Di:");
+
+ while ((opt = getopt_long (argc, argv, optstring, longopts, NULL)) != EOF)
+ switch (opt)
+ {
+ case 0:
+ /* If getopt returns 0, then it has already processed a
+ long-named option. We should do nothing. */
+ break;
-#ifndef LONG_OPTIONS
- optstring = optstring + 1;
-#endif /* LONG_OPTIONS */
+ case 1:
+ /* This means that a file name has been seen. Record it. */
+ argbuffer[current_arg].arg_type = at_filename;
+ argbuffer[current_arg].what = optarg;
+ ++current_arg;
+ ++file_count;
+ break;
- opt = getopt_long (argc, argv, optstring, longopts, 0);
- if (opt == EOF)
+ case STDIN:
+ /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
+ argbuffer[current_arg].arg_type = at_stdin;
+ argbuffer[current_arg].what = optarg;
+ ++current_arg;
+ ++file_count;
+ if (parsing_stdin)
+ fatal ("cannot parse standard input more than once", (char *)NULL);
+ parsing_stdin = TRUE;
break;
- switch (opt)
+ /* Common options. */
+ case 'a': append_to_tagfile = TRUE; break;
+ case 'C': cplusplus = TRUE; break;
+ case 'f': /* for compatibility with old makefiles */
+ case 'o':
+ if (tagfile)
+ {
+ error ("-o option may only be given once.", (char *)NULL);
+ suggest_asking_for_help ();
+ /* NOTREACHED */
+ }
+ tagfile = optarg;
+ break;
+ case 'I':
+ case 'S': /* for backward compatibility */
+ ignoreindent = TRUE;
+ break;
+ case 'l':
{
- case 0:
- /* If getopt returns 0, then it has already processed a
- long-named option. We should do nothing. */
- break;
-
- case 1:
- /* This means that a file name has been seen. Record it. */
- argbuffer[current_arg].arg_type = at_filename;
- argbuffer[current_arg].what = optarg;
- ++current_arg;
- ++file_count;
- break;
-
- /* Common options. */
- case 'a': append_to_tagfile = TRUE; break;
- case 'C': cplusplus = TRUE; break;
- case 'd': constantypedefs = TRUE; break;
- case 'D': constantypedefs = FALSE; break;
- case 'f': /* for compatibility with old makefiles */
- case 'o':
- if (tagfile)
+ language *lang = get_language_from_langname (optarg);
+ if (lang != NULL)
{
- error ("-%c option may only be given once.", opt);
- suggest_asking_for_help ();
+ argbuffer[current_arg].lang = lang;
+ argbuffer[current_arg].arg_type = at_language;
+ ++current_arg;
}
- tagfile = optarg;
- break;
- case 'I':
- case 'S': /* for backward compatibility */
- noindentypedefs = TRUE;
- break;
- case 'l':
- argbuffer[current_arg].function = get_language_from_name (optarg);
- argbuffer[current_arg].arg_type = at_language;
- ++current_arg;
- break;
-#ifdef ETAGS_REGEXPS
- case 'r':
- argbuffer[current_arg].arg_type = at_regexp;
- argbuffer[current_arg].what = optarg;
- ++current_arg;
- break;
- case 'R':
- argbuffer[current_arg].arg_type = at_regexp;
- argbuffer[current_arg].what = NULL;
- ++current_arg;
- break;
-#endif /* ETAGS_REGEXPS */
- case 'V':
- print_version ();
- break;
- case 'h':
- case 'H':
- print_help ();
- break;
- case 't':
- typedefs = TRUE;
- break;
- case 'T':
- typedefs = typedefs_and_cplusplus = TRUE;
- break;
-#if (!CTAGS)
- /* Etags options */
- case 'i':
- included_files[nincluded_files++] = optarg;
- break;
-#else /* CTAGS */
- /* Ctags options. */
- case 'B': searchar = '?'; break;
- case 'u': update = TRUE; break;
- case 'v': vgrind_style = TRUE; /*FALLTHRU*/
- case 'x': cxref_style = TRUE; break;
- case 'w': no_warnings = TRUE; break;
-#endif /* CTAGS */
- default:
- suggest_asking_for_help ();
}
- }
+ break;
+ case 'c':
+ /* Backward compatibility: support obsolete --ignore-case-regexp. */
+ optarg = concat (optarg, "i", ""); /* memory leak here */
+ /* FALLTHRU */
+ case 'r':
+ argbuffer[current_arg].arg_type = at_regexp;
+ argbuffer[current_arg].what = optarg;
+ ++current_arg;
+ break;
+ case 'R':
+ argbuffer[current_arg].arg_type = at_regexp;
+ argbuffer[current_arg].what = NULL;
+ ++current_arg;
+ break;
+ case 'V':
+ print_version ();
+ break;
+ case 'h':
+ case 'H':
+ help_asked = TRUE;
+ break;
- for (; optind < argc; ++optind)
+ /* Etags options */
+ case 'D': constantypedefs = FALSE; break;
+ case 'i': included_files[nincluded_files++] = optarg; break;
+
+ /* Ctags options. */
+ case 'B': searchar = '?'; break;
+ case 'd': constantypedefs = TRUE; break;
+ case 't': typedefs = TRUE; break;
+ case 'T': typedefs = typedefs_or_cplusplus = TRUE; break;
+ case 'u': update = TRUE; break;
+ case 'v': vgrind_style = TRUE; /*FALLTHRU*/
+ case 'x': cxref_style = TRUE; break;
+ case 'w': no_warnings = TRUE; break;
+ default:
+ suggest_asking_for_help ();
+ /* NOTREACHED */
+ }
+
+ /* No more options. Store the rest of arguments. */
+ for (; optind < argc; optind++)
{
argbuffer[current_arg].arg_type = at_filename;
argbuffer[current_arg].what = argv[optind];
++file_count;
}
+ argbuffer[current_arg].arg_type = at_end;
+
+ if (help_asked)
+ print_help (argbuffer);
+ /* NOTREACHED */
+
if (nincluded_files == 0 && file_count == 0)
{
- error ("no input files specified.", 0);
+ error ("no input files specified.", (char *)NULL);
suggest_asking_for_help ();
+ /* NOTREACHED */
}
if (tagfile == NULL)
cwd = concat (oldcwd, "/", "");
free (oldcwd);
}
- if (streq (tagfile, "-"))
+ /* Relative file names are made relative to the current directory. */
+ if (streq (tagfile, "-")
+ || strneq (tagfile, "/dev/", 5))
tagfiledir = cwd;
else
tagfiledir = absolute_dirname (tagfile, cwd);
init (); /* set up boolean "functions" */
- initbuffer (&lb);
- initbuffer (&token_name);
- initbuffer (&lbs[0].lb);
- initbuffer (&lbs[1].lb);
- initbuffer (&filename_lb);
+ linebuffer_init (&lb);
+ linebuffer_init (&filename_lb);
+ linebuffer_init (&filebuf);
+ linebuffer_init (&token_name);
if (!CTAGS)
{
/*
* Loop through files finding functions.
*/
- for (i = 0; i < current_arg; ++i)
+ for (i = 0; i < current_arg; i++)
{
+ static language *lang; /* non-NULL if language is forced */
+ char *this_file;
+
switch (argbuffer[i].arg_type)
{
case at_language:
- lang_func = argbuffer[i].function;
+ lang = argbuffer[i].lang;
break;
#ifdef ETAGS_REGEXPS
case at_regexp:
this_file = argbuffer[i].what;
#endif
/* Input file named "-" means read file names from stdin
- and use them. */
+ (one per line) and use them. */
if (streq (this_file, "-"))
- while (readline_internal (&filename_lb, stdin) > 0)
- process_file (filename_lb.buffer);
+ {
+ if (parsing_stdin)
+ fatal ("cannot parse standard input AND read file names from it",
+ (char *)NULL);
+ while (readline_internal (&filename_lb, stdin) > 0)
+ process_file_name (filename_lb.buffer, lang);
+ }
else
- process_file (this_file);
+ process_file_name (this_file, lang);
#ifdef VMS
}
#endif
break;
+ case at_stdin:
+ this_file = argbuffer[i].what;
+ process_file (stdin, this_file, lang);
+ break;
}
}
- if (!CTAGS)
+#ifdef ETAGS_REGEXPS
+ free_regexps ();
+#endif /* ETAGS_REGEXPS */
+ free (lb.buffer);
+ free (filebuf.buffer);
+ free (token_name.buffer);
+
+ if (!CTAGS || cxref_style)
{
- while (nincluded_files-- > 0)
- fprintf (tagf, "\f\n%s,include\n", *included_files++);
+ /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */
+ put_entries (nodehead);
+ free_tree (nodehead);
+ nodehead = NULL;
+ if (!CTAGS)
+ {
+ fdesc *fdp;
- fclose (tagf);
- exit (GOOD);
- }
+ /* Output file entries that have no tags. */
+ for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
+ if (!fdp->written)
+ fprintf (tagf, "\f\n%s,0\n", fdp->taggedfname);
- /* If CTAGS, we are here. process_file did not write the tags yet,
- because we want them ordered. Let's do it now. */
- if (cxref_style)
- {
- put_entries (head);
- exit (GOOD);
+ while (nincluded_files-- > 0)
+ fprintf (tagf, "\f\n%s,include\n", *included_files++);
+
+ if (fclose (tagf) == EOF)
+ pfatal (tagfile);
+ }
+
+ exit (EXIT_SUCCESS);
}
if (update)
char cmd[BUFSIZ];
for (i = 0; i < current_arg; ++i)
{
- if (argbuffer[i].arg_type != at_filename)
- continue;
+ switch (argbuffer[i].arg_type)
+ {
+ case at_filename:
+ case at_stdin:
+ break;
+ default:
+ continue; /* the for loop */
+ }
sprintf (cmd,
"mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
tagfile, argbuffer[i].what, tagfile);
- if (system (cmd) != GOOD)
+ if (system (cmd) != EXIT_SUCCESS)
fatal ("failed to execute shell command", (char *)NULL);
}
append_to_tagfile = TRUE;
tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
if (tagf == NULL)
pfatal (tagfile);
- put_entries (head);
- fclose (tagf);
+ put_entries (nodehead); /* write all the tags (CTAGS) */
+ free_tree (nodehead);
+ nodehead = NULL;
+ if (fclose (tagf) == EOF)
+ pfatal (tagfile);
- if (update)
+ if (CTAGS)
+ if (append_to_tagfile || update)
+ {
+ char cmd[2*BUFSIZ+10];
+ sprintf (cmd, "sort -o %.*s %.*s", BUFSIZ, tagfile, BUFSIZ, tagfile);
+ exit (system (cmd));
+ }
+ return EXIT_SUCCESS;
+}
+
+
+/*
+ * 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)
+ */
+static compressor *
+get_compressor_from_suffix (file, extptr)
+ char *file;
+ char **extptr;
+{
+ compressor *compr;
+ char *slash, *suffix;
+
+ /* This relies on FN to be after canonicalize_filename,
+ so we don't need to consider backslashes on DOS_NT. */
+ slash = etags_strrchr (file, '/');
+ suffix = etags_strrchr (file, '.');
+ if (suffix == NULL || suffix < slash)
+ return NULL;
+ if (extptr != NULL)
+ *extptr = suffix;
+ suffix += 1;
+ /* Let those poor souls who live with DOS 8+3 file name limits get
+ some solace by treating foo.cgz as if it were foo.c.gz, etc.
+ Only the first do loop is run if not MSDOS */
+ do
{
- char cmd[BUFSIZ];
- sprintf (cmd, "sort %s -o %s", tagfile, tagfile);
- exit (system (cmd));
- }
- return GOOD;
+ 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;
}
+
/*
- * Return a Lang_function given the name.
+ * Return a language given the name.
*/
-Lang_function *
-get_language_from_name (name)
- char *name;
+static language *
+get_language_from_langname (name)
+ const char *name;
{
- struct lang_entry *lang;
+ language *lang;
- if (name != NULL)
- for (lang = lang_names; lang->name != NULL; lang++)
- {
+ if (name == NULL)
+ error ("empty language name", (char *)NULL);
+ else
+ {
+ for (lang = lang_names; lang->name != NULL; lang++)
if (streq (name, lang->name))
- return lang->function;
- }
-
- error ("language \"%s\" not recognized.", optarg);
- suggest_asking_for_help ();
+ return lang;
+ error ("unknown language \"%s\"", name);
+ }
- /* This point should never be reached. The function should either
- return a function pointer or never return. Note that a NULL
- pointer cannot be considered as an error, as it means that the
- language has not been explicitely imposed by the user ("auto"). */
- return NULL; /* avoid warnings from compiler */
+ return NULL;
}
/*
- * Return a Lang_function given the interpreter name.
+ * Return a language given the interpreter name.
*/
-Lang_function *
+static language *
get_language_from_interpreter (interpreter)
char *interpreter;
{
- struct lang_entry *lang;
+ language *lang;
char **iname;
if (interpreter == NULL)
if (lang->interpreters != NULL)
for (iname = lang->interpreters; *iname != NULL; iname++)
if (streq (*iname, interpreter))
- return lang->function;
+ return lang;
return NULL;
}
/*
- * Return a Lang_function given the file suffix.
+ * Return a language given the file name.
*/
-Lang_function *
-get_language_from_suffix (suffix)
- char *suffix;
+static language *
+get_language_from_filename (file, case_sensitive)
+ char *file;
+ bool case_sensitive;
{
- struct lang_entry *lang;
- char **ext;
+ language *lang;
+ char **name, **ext, *suffix;
+ /* Try whole file name first. */
+ for (lang = lang_names; lang->name != NULL; lang++)
+ if (lang->filenames != NULL)
+ for (name = lang->filenames; *name != NULL; name++)
+ if ((case_sensitive)
+ ? streq (*name, file)
+ : strcaseeq (*name, file))
+ return lang;
+
+ /* If not found, try suffix after last dot. */
+ suffix = etags_strrchr (file, '.');
if (suffix == NULL)
return NULL;
+ suffix += 1;
for (lang = lang_names; lang->name != NULL; lang++)
if (lang->suffixes != NULL)
for (ext = lang->suffixes; *ext != NULL; ext++)
- if (streq (*ext, suffix))
- return lang->function;
-
+ if ((case_sensitive)
+ ? streq (*ext, suffix)
+ : strcaseeq (*ext, suffix))
+ return lang;
return NULL;
}
-
+\f
/*
* This routine is called on each file argument.
*/
-void
-process_file (file)
+static void
+process_file_name (file, lang)
char *file;
+ language *lang;
{
struct stat stat_buf;
FILE *inf;
-#ifdef DOS_NT
- char *p;
+ fdesc *fdp;
+ compressor *compr;
+ char *compressed_name, *uncompressed_name;
+ char *ext, *real_name;
+ int retval;
- for (p = file; *p != '\0'; p++)
- if (*p == '\\')
- *p = '/';
-#endif
-
- if (stat (file, &stat_buf) == 0 && !S_ISREG (stat_buf.st_mode))
- {
- error ("skipping %s: it is not a regular file.", file);
- return;
- }
+ canonicalize_filename (file);
if (streq (file, tagfile) && !streq (tagfile, "-"))
{
error ("skipping inclusion of %s in self.", file);
return;
}
- inf = fopen (file, "r");
- if (inf == NULL)
+ if ((compr = get_compressor_from_suffix (file, &ext)) == NULL)
{
- perror (file);
- return;
+ compressed_name = NULL;
+ real_name = uncompressed_name = savestr (file);
+ }
+ else
+ {
+ real_name = compressed_name = savestr (file);
+ uncompressed_name = savenstr (file, ext - file);
}
- find_entries (file, inf);
-
- if (!CTAGS)
+ /* If the canonicalized uncompressed name
+ has already been dealt with, skip it silently. */
+ for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
{
- char *filename;
+ assert (fdp->infname != NULL);
+ if (streq (uncompressed_name, fdp->infname))
+ goto cleanup;
+ }
- if (absolutefn (file))
+ if (stat (real_name, &stat_buf) != 0)
+ {
+ /* Reset real_name and try with a different name. */
+ real_name = NULL;
+ if (compressed_name != NULL) /* try with the given suffix */
{
- /* file is an absolute file name. Canonicalise it. */
- filename = absolute_filename (file, cwd);
+ if (stat (uncompressed_name, &stat_buf) == 0)
+ real_name = uncompressed_name;
}
- else
+ else /* try all possible suffixes */
+ {
+ for (compr = compressors; compr->suffix != NULL; compr++)
+ {
+ compressed_name = concat (file, ".", compr->suffix);
+ if (stat (compressed_name, &stat_buf) != 0)
+ {
+ if (MSDOS)
+ {
+ char *suf = compressed_name + strlen (file);
+ size_t suflen = strlen (compr->suffix) + 1;
+ for ( ; suf[1]; suf++, suflen--)
+ {
+ memmove (suf, suf + 1, suflen);
+ if (stat (compressed_name, &stat_buf) == 0)
+ {
+ real_name = compressed_name;
+ break;
+ }
+ }
+ if (real_name != NULL)
+ break;
+ } /* MSDOS */
+ free (compressed_name);
+ compressed_name = NULL;
+ }
+ else
+ {
+ real_name = compressed_name;
+ break;
+ }
+ }
+ }
+ if (real_name == NULL)
+ {
+ perror (file);
+ goto cleanup;
+ }
+ } /* try with a different name */
+
+ if (!S_ISREG (stat_buf.st_mode))
+ {
+ error ("skipping %s: it is not a regular file.", real_name);
+ goto cleanup;
+ }
+ if (real_name == compressed_name)
+ {
+ char *cmd = concat (compr->command, " ", real_name);
+ inf = (FILE *) popen (cmd, "r");
+ free (cmd);
+ }
+ else
+ inf = fopen (real_name, "r");
+ if (inf == NULL)
+ {
+ perror (real_name);
+ goto cleanup;
+ }
+
+ process_file (inf, uncompressed_name, lang);
+
+ if (real_name == compressed_name)
+ retval = pclose (inf);
+ else
+ retval = fclose (inf);
+ if (retval < 0)
+ pfatal (file);
+
+ cleanup:
+ if (compressed_name) free (compressed_name);
+ if (uncompressed_name) free (uncompressed_name);
+ last_node = NULL;
+ curfdp = NULL;
+ return;
+}
+
+static void
+process_file (fh, fn, lang)
+ FILE *fh;
+ char *fn;
+ language *lang;
+{
+ static const fdesc emptyfdesc;
+ fdesc *fdp;
+
+ /* Create a new input file description entry. */
+ fdp = xnew (1, fdesc);
+ *fdp = emptyfdesc;
+ fdp->next = fdhead;
+ fdp->infname = savestr (fn);
+ fdp->lang = lang;
+ fdp->infabsname = absolute_filename (fn, cwd);
+ fdp->infabsdir = absolute_dirname (fn, cwd);
+ if (filename_is_absolute (fn))
+ {
+ /* An absolute file name. Canonicalize it. */
+ fdp->taggedfname = absolute_filename (fn, NULL);
+ }
+ else
+ {
+ /* A file name relative to cwd. Make it relative
+ to the directory of the tags file. */
+ fdp->taggedfname = relative_filename (fn, tagfiledir);
+ }
+ fdp->usecharno = TRUE; /* use char position when making tags */
+ fdp->prop = NULL;
+ fdp->written = FALSE; /* not written on tags file yet */
+
+ fdhead = fdp;
+ curfdp = fdhead; /* the current file description */
+
+ find_entries (fh);
+
+ /* If not Ctags, and if this is not metasource and if it contained no #line
+ directives, we can write the tags and free all nodes pointing to
+ curfdp. */
+ if (!CTAGS
+ && curfdp->usecharno /* no #line directives in this file */
+ && !curfdp->lang->metasource)
+ {
+ node *np, *prev;
+
+ /* Look for the head of the sublist relative to this file. See add_node
+ for the structure of the node tree. */
+ prev = NULL;
+ for (np = nodehead; np != NULL; prev = np, np = np->left)
+ if (np->fdp == curfdp)
+ break;
+
+ /* If we generated tags for this file, write and delete them. */
+ if (np != NULL)
{
- /* file is a file name relative to cwd. Make it relative
- to the directory of the tags file. */
- filename = relative_filename (file, tagfiledir);
+ /* This is the head of the last sublist, if any. The following
+ instructions depend on this being true. */
+ assert (np->left == NULL);
+
+ assert (fdhead == curfdp);
+ assert (last_node->fdp == curfdp);
+ put_entries (np); /* write tags for file curfdp->taggedfname */
+ free_tree (np); /* remove the written nodes */
+ if (prev == NULL)
+ nodehead = NULL; /* no nodes left */
+ else
+ prev->left = NULL; /* delete the pointer to the sublist */
}
- fprintf (tagf, "\f\n%s,%d\n", filename, total_size_of_entries (head));
- free (filename);
- put_entries (head);
- free_tree (head);
- head = NULL;
}
}
/*
* This routine sets up the boolean pseudo-functions which work
- * by setting boolean flags dependent upon the corresponding character
+ * by setting boolean flags dependent upon the corresponding character.
* Every char which is NOT in that string is not a white char. Therefore,
* all of the array "_wht" is set to FALSE, and then the elements
* subscripted by the chars in "white" are set to TRUE. Thus "_wht"
* of a char is TRUE if it is the string "white", else FALSE.
*/
-void
+static void
init ()
{
register char *sp;
register int i;
for (i = 0; i < CHARS; i++)
- _wht[i] = _nin[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
- for (sp = white; *sp; sp++) _wht[*sp] = TRUE; _wht[0] = _wht['\n'];
- for (sp = nonam; *sp; sp++) _nin[*sp] = TRUE; _nin[0] = _nin['\n'];
- for (sp = endtk; *sp; sp++) _etk[*sp] = TRUE; _etk[0] = _etk['\n'];
- for (sp = midtk; *sp; sp++) _itk[*sp] = TRUE; _btk[0] = _btk['\n'];
- for (sp = begtk; *sp; sp++) _btk[*sp] = TRUE; _itk[0] = _itk['\n'];
+ iswhite(i) = notinname(i) = begtoken(i) = intoken(i) = endtoken(i) = FALSE;
+ for (sp = white; *sp != '\0'; sp++) iswhite (*sp) = TRUE;
+ for (sp = nonam; *sp != '\0'; sp++) notinname (*sp) = TRUE;
+ notinname('\0') = notinname('\n');
+ for (sp = begtk; *sp != '\0'; sp++) begtoken (*sp) = TRUE;
+ begtoken('\0') = begtoken('\n');
+ for (sp = midtk; *sp != '\0'; sp++) intoken (*sp) = TRUE;
+ intoken('\0') = intoken('\n');
+ for (sp = endtk; *sp != '\0'; sp++) endtoken (*sp) = TRUE;
+ endtoken('\0') = endtoken('\n');
}
/*
* This routine opens the specified file and calls the function
* which finds the function and type definitions.
*/
-void
-find_entries (file, inf)
- char *file;
+static void
+find_entries (inf)
FILE *inf;
{
char *cp;
- Lang_function *function;
- NODE *old_last_node;
- extern NODE *last_node;
-
-
- /* Memory leakage here: the memory block pointed by curfile is never
- released. The amount of memory leaked here is the sum of the
- lengths of the input file names. */
- curfile = savestr (file);
+ language *lang = curfdp->lang;
+ Lang_function *parser = NULL;
/* If user specified a language, use it. */
- function = lang_func;
- if (function != NULL)
+ if (lang != NULL && lang->function != NULL)
{
- function (inf);
- fclose (inf);
- return;
+ parser = lang->function;
}
- cp = etags_strrchr (file, '.');
- if (cp != NULL)
+ /* Else try to guess the language given the file name. */
+ if (parser == NULL)
{
- cp += 1;
- function = get_language_from_suffix (cp);
- if (function != NULL)
+ lang = get_language_from_filename (curfdp->infname, TRUE);
+ if (lang != NULL && lang->function != NULL)
{
- function (inf);
- fclose (inf);
- return;
+ curfdp->lang = lang;
+ parser = lang->function;
}
}
- /* Look for sharp-bang as the first two characters. */
- if (readline_internal (&lb, inf)
+ /* Else look for sharp-bang as the first two characters. */
+ if (parser == NULL
+ && readline_internal (&lb, inf) > 0
&& lb.len >= 2
&& lb.buffer[0] == '#'
&& lb.buffer[1] == '!')
if (lp != NULL)
lp += 1;
else
- for (lp = lb.buffer+2; *lp != '\0' && isspace (*lp); lp++)
- continue;
- for (cp = lp; *cp != '\0' && !isspace (*cp); cp++)
- continue;
+ lp = skip_spaces (lb.buffer + 2);
+ cp = skip_non_spaces (lp);
*cp = '\0';
if (strlen (lp) > 0)
{
- function = get_language_from_interpreter (lp);
- if (function != NULL)
+ lang = get_language_from_interpreter (lp);
+ if (lang != NULL && lang->function != NULL)
{
- function (inf);
- fclose (inf);
- return;
+ curfdp->lang = lang;
+ parser = lang->function;
}
}
}
- rewind (inf);
- /* Try Fortran. */
- old_last_node = last_node;
- Fortran_functions (inf);
+ /* We rewind here, even if inf may be a pipe. We fail if the
+ length of the first line is longer than the pipe block size,
+ which is unlikely. */
+ rewind (inf);
- /* No Fortran entries found. Try C. */
- if (old_last_node == last_node)
+ /* Else try to guess the language given the case insensitive file name. */
+ if (parser == NULL)
{
- rewind (inf);
- default_C_entries (inf);
+ lang = get_language_from_filename (curfdp->infname, FALSE);
+ if (lang != NULL && lang->function != NULL)
+ {
+ curfdp->lang = lang;
+ parser = lang->function;
+ }
}
- fclose (inf);
- return;
-}
-\f
-/* Record a tag. */
-void
-pfnote (name, is_func, linestart, linelen, lno, cno)
- char *name; /* tag name, or NULL if unnamed */
- bool is_func; /* tag is a function */
- char *linestart; /* start of the line where tag is */
- int linelen; /* length of the line where tag is */
- int lno; /* line number */
- long cno; /* character number */
-{
- register NODE *np;
- if (CTAGS && name == NULL)
- return;
+ /* Else try Fortran or C. */
+ if (parser == NULL)
+ {
+ node *old_last_node = last_node;
- np = xnew (1, NODE);
+ curfdp->lang = get_language_from_langname ("fortran");
+ find_entries (inf);
- /* If ctags mode, change name "main" to M<thisfilename>. */
- if (CTAGS && !cxref_style && streq (name, "main"))
- {
- register char *fp = etags_strrchr (curfile, '/');
- np->name = concat ("M", fp == 0 ? curfile : fp + 1, "");
- fp = etags_strrchr (np->name, '.');
- if (fp && fp[1] != '\0' && fp[2] == '\0')
- fp[0] = 0;
+ if (old_last_node == last_node)
+ /* No Fortran entries found. Try C. */
+ {
+ /* We do not tag if rewind fails.
+ Only the file name will be recorded in the tags file. */
+ rewind (inf);
+ curfdp->lang = get_language_from_langname (cplusplus ? "c++" : "c");
+ find_entries (inf);
+ }
+ return;
}
- else
- np->name = name;
- np->been_warned = FALSE;
- np->file = curfile;
- np->is_func = is_func;
- np->lno = lno;
- /* Our char numbers are 0-base, because of C language tradition?
- ctags compatibility? old versions compatibility? I don't know.
- Anyway, since emacs's are 1-base we expect etags.el to take care
- of the difference. If we wanted to have 1-based numbers, we would
- uncomment the +1 below. */
- np->cno = cno /* + 1 */ ;
- np->left = np->right = NULL;
- if (CTAGS && !cxref_style)
+
+ if (!no_line_directive
+ && curfdp->lang != NULL && curfdp->lang->metasource)
+ /* It may be that this is a bingo.y file, and we already parsed a bingo.c
+ file, or anyway we parsed a file that is automatically generated from
+ this one. If this is the case, the bingo.c file contained #line
+ directives that generated tags pointing to this file. Let's delete
+ them all before parsing this file, which is the real source. */
{
- if (strlen (linestart) < 50)
- np->pat = concat (linestart, "$", "");
- else
- np->pat = savenstr (linestart, 50);
+ fdesc **fdpp = &fdhead;
+ while (*fdpp != NULL)
+ if (*fdpp != curfdp
+ && streq ((*fdpp)->taggedfname, curfdp->taggedfname))
+ /* We found one of those! We must delete both the file description
+ and all tags referring to it. */
+ {
+ fdesc *badfdp = *fdpp;
+
+ /* Delete the tags referring to badfdp->taggedfname
+ that were obtained from badfdp->infname. */
+ invalidate_nodes (badfdp, &nodehead);
+
+ *fdpp = badfdp->next; /* remove the bad description from the list */
+ free_fdesc (badfdp);
+ }
+ else
+ fdpp = &(*fdpp)->next; /* advance the list pointer */
}
- else
- np->pat = savenstr (linestart, linelen);
- add_node (np, &head);
+ assert (parser != NULL);
+
+ /* Generic initialisations before reading from file. */
+ linebuffer_setlen (&filebuf, 0); /* reset the file buffer */
+
+ /* Generic initialisations before parsing file with readline. */
+ lineno = 0; /* reset global line number */
+ charno = 0; /* reset global char number */
+ linecharno = 0; /* reset global char number of line start */
+
+ parser (inf);
+
+#ifdef ETAGS_REGEXPS
+ regex_tag_multiline ();
+#endif /* ETAGS_REGEXPS */
}
-/* Date: Wed, 22 Jan 1997 02:56:31 -0500
- * From: Sam Kendall <kendall@cybercom.net>
- * Subject: Proposal for firming up the TAGS format specification
- * To: F.Potorti@cnuce.cnr.it
+\f
+/*
+ * Check whether an implicitly named tag should be created,
+ * then call `pfnote'.
+ * NAME is a string that is internally copied by this function.
+ *
+ * TAGS format specification
+ * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
+ * The following is explained in some more detail in etc/ETAGS.EBNF.
*
- * pfnote should emit the optimized form [unnamed tag] only if:
- * 1. name does not contain any of the characters " \t\r\n()";
- * 2. linestart contains name as either a rightmost, or rightmost but
+ * make_tag creates tags with "implicit tag names" (unnamed tags)
+ * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
+ * 1. NAME does not contain any of the characters in NONAM;
+ * 2. LINESTART contains name as either a rightmost, or rightmost but
* one character, substring;
- * 3. the character, if any, immediately before name in linestart must
- * be one of the characters " \t()";
- * 4. the character, if any, immediately after name in linestart must
- * also be one of the characters " \t()".
+ * 3. the character, if any, immediately before NAME in LINESTART must
+ * be a character in NONAM;
+ * 4. the character, if any, immediately after NAME in LINESTART must
+ * also be a character in NONAM.
+ *
+ * The implementation uses the notinname() macro, which recognises the
+ * characters stored in the string `nonam'.
+ * etags.el needs to use the same characters that are in NONAM.
*/
-#define traditional_tag_style TRUE
-void
-new_pfnote (name, namelen, is_func, linestart, linelen, lno, cno)
+static void
+make_tag (name, namelen, is_func, linestart, linelen, lno, cno)
char *name; /* tag name, or NULL if unnamed */
int namelen; /* tag length */
bool is_func; /* tag is a function */
int lno; /* line number */
long cno; /* character number */
{
- register char *cp;
- bool named;
+ bool named = (name != NULL && namelen > 0);
- named = TRUE;
- if (!CTAGS)
+ if (!CTAGS && named) /* maybe set named to false */
+ /* Let's try to make an implicit tag name, that is, create an unnamed tag
+ such that etags.el can guess a name from it. */
{
- for (cp = name; !notinname (*cp); cp++)
- continue;
- if (*cp == '\0') /* rule #1 */
+ int i;
+ register char *cp = name;
+
+ for (i = 0; i < namelen; i++)
+ if (notinname (*cp++))
+ break;
+ if (i == namelen) /* rule #1 */
{
cp = linestart + linelen - namelen;
if (notinname (linestart[linelen-1]))
&& (cp == linestart
|| notinname (cp[-1])) /* rule #3 */
&& strneq (name, cp, namelen)) /* rule #2 */
- named = FALSE; /* use unnamed tag */
+ named = FALSE; /* use implicit tag name */
}
}
-
+
if (named)
name = savenstr (name, namelen);
else
pfnote (name, is_func, linestart, linelen, lno, cno);
}
+/* Record a tag. */
+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 */
+ char *linestart; /* start of the line where tag is */
+ int linelen; /* length of the line where tag is */
+ int lno; /* line number */
+ long cno; /* character number */
+{
+ register node *np;
+
+ assert (name == NULL || name[0] != '\0');
+ if (CTAGS && name == NULL)
+ return;
+
+ np = xnew (1, node);
+
+ /* If ctags mode, change name "main" to M<thisfilename>. */
+ if (CTAGS && !cxref_style && streq (name, "main"))
+ {
+ register char *fp = etags_strrchr (curfdp->taggedfname, '/');
+ np->name = concat ("M", fp == NULL ? curfdp->taggedfname : fp + 1, "");
+ fp = etags_strrchr (np->name, '.');
+ if (fp != NULL && fp[1] != '\0' && fp[2] == '\0')
+ fp[0] = '\0';
+ }
+ else
+ np->name = name;
+ np->valid = TRUE;
+ np->been_warned = FALSE;
+ np->fdp = curfdp;
+ np->is_func = is_func;
+ np->lno = lno;
+ if (np->fdp->usecharno)
+ /* Our char numbers are 0-base, because of C language tradition?
+ ctags compatibility? old versions compatibility? I don't know.
+ Anyway, since emacs's are 1-base we expect etags.el to take care
+ of the difference. If we wanted to have 1-based numbers, we would
+ uncomment the +1 below. */
+ np->cno = cno /* + 1 */ ;
+ else
+ np->cno = invalidcharno;
+ np->left = np->right = NULL;
+ if (CTAGS && !cxref_style)
+ {
+ if (strlen (linestart) < 50)
+ np->regex = concat (linestart, "$", "");
+ else
+ np->regex = savenstr (linestart, 50);
+ }
+ else
+ np->regex = savenstr (linestart, linelen);
+
+ add_node (np, &nodehead);
+}
+
/*
* free_tree ()
* recurse on left children, iterate on right children.
*/
-void
-free_tree (node)
- register NODE *node;
+static void
+free_tree (np)
+ register node *np;
{
- while (node)
+ while (np)
{
- register NODE *node_right = node->right;
- free_tree (node->left);
- if (node->name != NULL)
- free (node->name);
- free (node->pat);
- free ((char *) node);
- node = node_right;
+ register node *node_right = np->right;
+ free_tree (np->left);
+ if (np->name != NULL)
+ free (np->name);
+ free (np->regex);
+ free (np);
+ np = node_right;
}
}
+/*
+ * free_fdesc ()
+ * delete a file description
+ */
+static void
+free_fdesc (fdp)
+ register fdesc *fdp;
+{
+ if (fdp->infname != NULL) free (fdp->infname);
+ if (fdp->infabsname != NULL) free (fdp->infabsname);
+ if (fdp->infabsdir != NULL) free (fdp->infabsdir);
+ if (fdp->taggedfname != NULL) free (fdp->taggedfname);
+ if (fdp->prop != NULL) free (fdp->prop);
+ free (fdp);
+}
+
/*
* add_node ()
- * Adds a node to the tree of nodes. In etags mode, we don't keep
- * it sorted; we just keep a linear list. In ctags mode, maintain
- * an ordered tree, with no attempt at balancing.
+ * Adds a node to the tree of nodes. In etags mode, sort by file
+ * name. In ctags mode, sort by tag name. Make no attempt at
+ * balancing.
*
* add_node is the only function allowed to add nodes, so it can
* maintain state.
*/
-NODE *last_node = NULL;
-void
-add_node (node, cur_node_p)
- NODE *node, **cur_node_p;
+static void
+add_node (np, cur_node_p)
+ node *np, **cur_node_p;
{
register int dif;
- register NODE *cur_node = *cur_node_p;
+ register node *cur_node = *cur_node_p;
if (cur_node == NULL)
{
- *cur_node_p = node;
- last_node = node;
+ *cur_node_p = np;
+ last_node = np;
return;
}
if (!CTAGS)
+ /* Etags Mode */
{
- /* Etags Mode */
- if (last_node == NULL)
- fatal ("internal error in add_node", (char *)NULL);
- last_node->right = node;
- last_node = node;
- }
+ /* For each file name, tags are in a linked sublist on the right
+ pointer. The first tags of different files are a linked list
+ on the left pointer. last_node points to the end of the last
+ used sublist. */
+ if (last_node != NULL && last_node->fdp == np->fdp)
+ {
+ /* Let's use the same sublist as the last added node. */
+ assert (last_node->right == NULL);
+ last_node->right = np;
+ last_node = np;
+ }
+ else if (cur_node->fdp == np->fdp)
+ {
+ /* Scanning the list we found the head of a sublist which is
+ good for us. Let's scan this sublist. */
+ add_node (np, &cur_node->right);
+ }
+ else
+ /* The head of this sublist is not good for us. Let's try the
+ next one. */
+ add_node (np, &cur_node->left);
+ } /* if ETAGS mode */
+
else
{
/* Ctags Mode */
- dif = strcmp (node->name, cur_node->name);
+ dif = strcmp (np->name, cur_node->name);
/*
* If this tag name matches an existing one, then
*/
if (!dif)
{
- if (streq (node->file, cur_node->file))
+ if (np->fdp == cur_node->fdp)
{
if (!no_warnings)
{
fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
- node->file, lineno, node->name);
+ np->fdp->infname, lineno, np->name);
fprintf (stderr, "Second entry ignored\n");
}
}
fprintf
(stderr,
"Duplicate entry in files %s and %s: %s (Warning only)\n",
- node->file, cur_node->file, node->name);
+ np->fdp->infname, cur_node->fdp->infname, np->name);
cur_node->been_warned = TRUE;
}
return;
}
/* Actually add the node */
- add_node (node, dif < 0 ? &cur_node->left : &cur_node->right);
- }
+ add_node (np, dif < 0 ? &cur_node->left : &cur_node->right);
+ } /* if CTAGS mode */
}
-\f
-void
-put_entries (node)
- register NODE *node;
+
+/*
+ * invalidate_nodes ()
+ * Scan the node tree and invalidate all nodes pointing to the
+ * given file description (CTAGS case) or free them (ETAGS case).
+ */
+static void
+invalidate_nodes (badfdp, npp)
+ fdesc *badfdp;
+ node **npp;
{
- register char *sp;
+ node *np = *npp;
- if (node == NULL)
+ if (np == NULL)
return;
- /* Output subentries that precede this one */
- put_entries (node->left);
-
- /* Output this entry */
-
- if (!CTAGS)
+ if (CTAGS)
{
- if (node->name != NULL)
- fprintf (tagf, "%s\177%s\001%d,%ld\n",
- node->pat, node->name, node->lno, node->cno);
- else
- fprintf (tagf, "%s\177%d,%ld\n",
- node->pat, node->lno, node->cno);
+ if (np->left != NULL)
+ invalidate_nodes (badfdp, &np->left);
+ if (np->fdp == badfdp)
+ np->valid = FALSE;
+ if (np->right != NULL)
+ invalidate_nodes (badfdp, &np->right);
}
else
{
- if (node->name == NULL)
- error ("internal error: NULL name in ctags mode.", (char *)NULL);
-
- if (cxref_style)
+ assert (np->fdp != NULL);
+ if (np->fdp == badfdp)
{
- if (vgrind_style)
- fprintf (stdout, "%s %s %d\n",
- node->name, node->file, (node->lno + 63) / 64);
- else
- fprintf (stdout, "%-16s %3d %-16s %s\n",
- node->name, node->lno, node->file, node->pat);
+ *npp = np->left; /* detach the sublist from the list */
+ np->left = NULL; /* isolate it */
+ free_tree (np); /* free it */
+ invalidate_nodes (badfdp, npp);
}
else
- {
- fprintf (tagf, "%s\t%s\t", node->name, node->file);
-
- if (node->is_func)
- { /* a function */
- putc (searchar, tagf);
- putc ('^', tagf);
-
- for (sp = node->pat; *sp; sp++)
- {
- if (*sp == '\\' || *sp == searchar)
- putc ('\\', tagf);
- putc (*sp, tagf);
- }
- putc (searchar, tagf);
- }
- else
- { /* a typedef; text pattern inadequate */
- fprintf (tagf, "%d", node->lno);
- }
- putc ('\n', tagf);
- }
+ invalidate_nodes (badfdp, &np->left);
}
-
- /* Output subentries that follow this one */
- put_entries (node->right);
}
-/* Length of a number's decimal representation. */
-int
+\f
+static int total_size_of_entries __P((node *));
+static int number_len __P((long));
+
+/* Length of a non-negative number's decimal representation. */
+static int
number_len (num)
long num;
{
- int len = 0;
- if (!num)
- return 1;
- for (; num; num /= 10)
- ++len;
+ int len = 1;
+ while ((num /= 10) > 0)
+ len += 1;
return len;
}
/*
* Return total number of characters that put_entries will output for
- * the nodes in the subtree of the specified node. Works only if
- * we are not ctags, but called only in that case. This count
- * is irrelevant with the new tags.el, but is still supplied for
- * backward compatibility.
+ * the nodes in the linked list at the right of the specified node.
+ * This count is irrelevant with etags.el since emacs 19.34 at least,
+ * but is still supplied for backward compatibility.
*/
-int
-total_size_of_entries (node)
- register NODE *node;
+static int
+total_size_of_entries (np)
+ register node *np;
{
- register int total;
+ register int total = 0;
- if (node == NULL)
- return 0;
+ for (; np != NULL; np = np->right)
+ if (np->valid)
+ {
+ total += strlen (np->regex) + 1; /* pat\177 */
+ if (np->name != NULL)
+ total += strlen (np->name) + 1; /* name\001 */
+ total += number_len ((long) np->lno) + 1; /* lno, */
+ if (np->cno != invalidcharno) /* cno */
+ total += number_len (np->cno);
+ total += 1; /* newline */
+ }
+
+ return total;
+}
+
+static void
+put_entries (np)
+ register node *np;
+{
+ register char *sp;
+ static fdesc *fdp = NULL;
- total = 0;
- for (; node; node = node->right)
+ if (np == NULL)
+ return;
+
+ /* Output subentries that precede this one */
+ if (CTAGS)
+ put_entries (np->left);
+
+ /* Output this entry */
+ if (np->valid)
{
- /* Count left subentries. */
- total += total_size_of_entries (node->left);
-
- /* Count this entry */
- total += strlen (node->pat) + 1;
- total += number_len ((long) node->lno) + 1 + number_len (node->cno) + 1;
- if (node->name != NULL)
- total += 1 + strlen (node->name); /* \001name */
- }
+ if (!CTAGS)
+ {
+ /* Etags mode */
+ if (fdp != np->fdp)
+ {
+ fdp = np->fdp;
+ fprintf (tagf, "\f\n%s,%d\n",
+ fdp->taggedfname, total_size_of_entries (np));
+ fdp->written = TRUE;
+ }
+ fputs (np->regex, tagf);
+ fputc ('\177', tagf);
+ if (np->name != NULL)
+ {
+ fputs (np->name, tagf);
+ fputc ('\001', tagf);
+ }
+ fprintf (tagf, "%d,", np->lno);
+ if (np->cno != invalidcharno)
+ fprintf (tagf, "%ld", np->cno);
+ fputs ("\n", tagf);
+ }
+ else
+ {
+ /* Ctags mode */
+ if (np->name == NULL)
+ error ("internal error: NULL name in ctags mode.", (char *)NULL);
- return total;
+ if (cxref_style)
+ {
+ if (vgrind_style)
+ fprintf (stdout, "%s %s %d\n",
+ np->name, np->fdp->taggedfname, (np->lno + 63) / 64);
+ else
+ fprintf (stdout, "%-16s %3d %-16s %s\n",
+ np->name, np->lno, np->fdp->taggedfname, np->regex);
+ }
+ else
+ {
+ fprintf (tagf, "%s\t%s\t", np->name, np->fdp->taggedfname);
+
+ if (np->is_func)
+ { /* function or #define macro with args */
+ putc (searchar, tagf);
+ putc ('^', tagf);
+
+ for (sp = np->regex; *sp; sp++)
+ {
+ if (*sp == '\\' || *sp == searchar)
+ putc ('\\', tagf);
+ putc (*sp, tagf);
+ }
+ putc (searchar, tagf);
+ }
+ else
+ { /* anything else; text pattern inadequate */
+ fprintf (tagf, "%d", np->lno);
+ }
+ putc ('\n', tagf);
+ }
+ }
+ } /* if this node contains a valid tag */
+
+ /* Output subentries that follow this one */
+ put_entries (np->right);
+ if (!CTAGS)
+ put_entries (np->left);
}
+
\f
+/* C extensions. */
+#define C_EXT 0x00fff /* C extensions */
+#define C_PLAIN 0x00000 /* C */
+#define C_PLPL 0x00001 /* C++ */
+#define C_STAR 0x00003 /* C* */
+#define C_JAVA 0x00005 /* JAVA */
+#define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
+#define YACC 0x10000 /* yacc file */
+
/*
* The C symbol tables.
*/
st_none,
st_C_objprot, st_C_objimpl, st_C_objend,
st_C_gnumacro,
- st_C_ignore,
+ st_C_ignore, st_C_attribute,
st_C_javastruct,
- st_C_struct, st_C_enum, st_C_define, st_C_typedef, st_C_typespec
+ st_C_operator,
+ st_C_class, st_C_template,
+ st_C_struct, st_C_extern, st_C_enum, st_C_define, st_C_typedef
};
+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
+ gperf -m 5
%[
+%compare-strncmp
+%enum
+%struct-type
struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
%%
-@interface, 0, st_C_objprot
-@protocol, 0, st_C_objprot
-@implementation,0, st_C_objimpl
-@end, 0, st_C_objend
-import, C_JAVA, st_C_ignore
-package, C_JAVA, st_C_ignore
-friend, C_PLPL, st_C_ignore
-extends, C_JAVA, st_C_javastruct
-implements, C_JAVA, st_C_javastruct
-class, C_PLPL, st_C_struct
-namespace, C_PLPL, st_C_struct
-domain, C_STAR, st_C_struct
-union, 0, st_C_struct
-struct, 0, st_C_struct
-enum, 0, st_C_enum
-typedef, 0, st_C_typedef
-define, 0, st_C_define
-bool, C_PLPL, st_C_typespec
-long, 0, st_C_typespec
-short, 0, st_C_typespec
-int, 0, st_C_typespec
-char, 0, st_C_typespec
-float, 0, st_C_typespec
-double, 0, st_C_typespec
-signed, 0, st_C_typespec
-unsigned, 0, st_C_typespec
-auto, 0, st_C_typespec
-void, 0, st_C_typespec
-extern, 0, st_C_typespec
-static, 0, st_C_typespec
-const, 0, st_C_typespec
-volatile, 0, st_C_typespec
-explicit, C_PLPL, st_C_typespec
-mutable, C_PLPL, st_C_typespec
-typename, C_PLPL, st_C_typespec
+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
+__attribute__, 0, st_C_attribute
+@interface, 0, st_C_objprot
+@protocol, 0, st_C_objprot
+@implementation,0, st_C_objimpl
+@end, 0, st_C_objend
+import, (C_JAVA & !C_PLPL), st_C_ignore
+package, (C_JAVA & !C_PLPL), st_C_ignore
+friend, C_PLPL, st_C_ignore
+extends, (C_JAVA & !C_PLPL), st_C_javastruct
+implements, (C_JAVA & !C_PLPL), st_C_javastruct
+interface, (C_JAVA & !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
+operator, C_PLPL, st_C_operator
+template, 0, st_C_template
# DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
-DEFUN, 0, st_C_gnumacro
-SYSCALL, 0, st_C_gnumacro
-ENTRY, 0, st_C_gnumacro
-PSEUDO, 0, st_C_gnumacro
+DEFUN, 0, st_C_gnumacro
+SYSCALL, 0, st_C_gnumacro
+ENTRY, 0, st_C_gnumacro
+PSEUDO, 0, st_C_gnumacro
# These are defined inside C functions, so currently they are not met.
# EXFUN used in glibc, DEFVAR_* in emacs.
-#EXFUN, 0, st_C_gnumacro
-#DEFVAR_, 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:
+ - remove the #if characterset check
+ - make in_word_set static and not inline. */
/*%<*/
-/* starting time is 10:31:16 */
-/* C code produced by gperf version 2.1 (K&R C version) */
-/* Command-line: gperf -c -k 1,3 -o -p -r -t */
-
+/* C code produced by gperf version 3.0.1 */
+/* Command-line: gperf -m 5 */
+/* Computed positions: -k'1-2' */
struct C_stab_entry { char *name; int c_ext; enum sym_type type; };
+/* maximum key range = 31, duplicates = 0 */
-#define MIN_WORD_LENGTH 3
-#define MAX_WORD_LENGTH 15
-#define MIN_HASH_VALUE 15
-#define MAX_HASH_VALUE 128
-/*
- 39 keywords
- 114 is the maximum key range
-*/
-
-static int
+#ifdef __GNUC__
+__inline
+#else
+#ifdef __cplusplus
+inline
+#endif
+#endif
+static unsigned int
hash (str, len)
- register char *str;
- register unsigned int len;
+ register const char *str;
+ register unsigned int len;
{
- static unsigned char hash_table[] =
+ static unsigned char asso_values[] =
{
- 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
- 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
- 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
- 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
- 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
- 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
- 128, 128, 128, 128, 39, 128, 128, 128, 54, 48,
- 46, 128, 128, 128, 128, 128, 128, 128, 128, 128,
- 28, 128, 128, 40, 32, 128, 128, 128, 128, 128,
- 128, 128, 128, 128, 128, 128, 128, 24, 30, 47,
- 62, 7, 60, 27, 128, 60, 128, 128, 59, 16,
- 31, 23, 45, 128, 4, 14, 2, 55, 5, 128,
- 128, 128, 128, 128, 128, 128, 128, 128,
- };
- return len + hash_table[str[2]] + hash_table[str[0]];
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 1, 34, 34, 34, 14, 14,
+ 34, 34, 34, 34, 34, 34, 34, 34, 13, 34,
+ 13, 34, 34, 12, 34, 34, 34, 34, 34, 11,
+ 34, 34, 34, 34, 34, 8, 34, 11, 34, 12,
+ 11, 0, 1, 34, 7, 0, 34, 34, 11, 9,
+ 0, 4, 0, 34, 7, 4, 14, 21, 34, 15,
+ 0, 2, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
+ 34, 34, 34, 34, 34, 34
+ };
+ return len + asso_values[(unsigned char)str[1]] + asso_values[(unsigned char)str[0]];
}
-struct C_stab_entry *
+static struct C_stab_entry *
in_word_set (str, len)
- register char *str;
+ register const char *str;
register unsigned int len;
{
+ enum
+ {
+ TOTAL_KEYWORDS = 31,
+ MIN_WORD_LENGTH = 2,
+ MAX_WORD_LENGTH = 15,
+ MIN_HASH_VALUE = 3,
+ MAX_HASH_VALUE = 33
+ };
- static struct C_stab_entry wordlist[] =
+ static struct C_stab_entry wordlist[] =
{
- {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
- {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
- {"extern", 0, st_C_typespec},
- {"extends", C_JAVA, st_C_javastruct},
- {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
- {"struct", 0, st_C_struct},
- {"mutable", C_PLPL, st_C_typespec},
- {"",}, {"",}, {"",}, {"",},
- {"auto", 0, st_C_typespec},
- {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
- {"",}, {"",},
- {"short", 0, st_C_typespec},
- {"",},
- {"static", 0, st_C_typespec},
- {"",}, {"",},
- {"signed", 0, st_C_typespec},
- {"",}, {"",}, {"",}, {"",},
- {"@protocol", 0, st_C_objprot},
- {"",},
- {"typedef", 0, st_C_typedef},
- {"typename", C_PLPL, st_C_typespec},
- {"namespace", C_PLPL, st_C_struct},
- {"bool", C_PLPL, st_C_typespec},
- {"",}, {"",},
- {"explicit", C_PLPL, st_C_typespec},
- {"",}, {"",}, {"",}, {"",},
- {"int", 0, st_C_typespec},
- {"enum", 0, st_C_enum},
- {"",}, {"",},
- {"void", 0, st_C_typespec},
- {"@implementation", 0, st_C_objimpl},
- {"",},
- {"volatile", 0, st_C_typespec},
- {"",},
- {"@end", 0, st_C_objend},
- {"char", 0, st_C_typespec},
- {"class", C_PLPL, st_C_struct},
- {"unsigned", 0, st_C_typespec},
- {"",}, {"",},
- {"@interface", 0, st_C_objprot},
- {"",},
- {"PSEUDO", 0, st_C_gnumacro},
- {"const", 0, st_C_typespec},
- {"domain", C_STAR, st_C_struct},
- {"ENTRY", 0, st_C_gnumacro},
- {"",},
- {"SYSCALL", 0, st_C_gnumacro},
- {"float", 0, st_C_typespec},
- {"",}, {"",}, {"",}, {"",}, {"",},
- {"long", 0, st_C_typespec},
- {"",}, {"",}, {"",}, {"",},
- {"package", C_JAVA, st_C_ignore},
- {"",}, {"",}, {"",}, {"",}, {"",},
- {"DEFUN", 0, st_C_gnumacro},
- {"",}, {"",}, {"",}, {"",}, {"",},
- {"import", C_JAVA, st_C_ignore},
- {"",}, {"",}, {"",},
- {"implements", C_JAVA, st_C_javastruct},
- {"",}, {"",}, {"",}, {"",},
- {"union", 0, st_C_struct},
- {"",}, {"",},
- {"double", 0, st_C_typespec},
- {"",}, {"",},
- {"friend", C_PLPL, st_C_ignore},
- {"",},
- {"define", 0, st_C_define},
+ {""}, {""}, {""},
+ {"if", 0, st_C_ignore},
+ {"enum", 0, st_C_enum},
+ {"@end", 0, st_C_objend},
+ {"extern", 0, st_C_extern},
+ {"extends", (C_JAVA & !C_PLPL), st_C_javastruct},
+ {"for", 0, st_C_ignore},
+ {"interface", (C_JAVA & !C_PLPL), st_C_struct},
+ {"@protocol", 0, st_C_objprot},
+ {"@interface", 0, st_C_objprot},
+ {"operator", C_PLPL, st_C_operator},
+ {"return", 0, st_C_ignore},
+ {"friend", C_PLPL, st_C_ignore},
+ {"import", (C_JAVA & !C_PLPL), st_C_ignore},
+ {"@implementation",0, st_C_objimpl},
+ {"define", 0, st_C_define},
+ {"package", (C_JAVA & !C_PLPL), st_C_ignore},
+ {"implements", (C_JAVA & !C_PLPL), st_C_javastruct},
+ {"namespace", C_PLPL, st_C_struct},
+ {"domain", C_STAR, st_C_struct},
+ {"template", 0, st_C_template},
+ {"typedef", 0, st_C_typedef},
+ {"struct", 0, st_C_struct},
+ {"switch", 0, st_C_ignore},
+ {"union", 0, st_C_struct},
+ {"while", 0, st_C_ignore},
+ {"class", 0, st_C_class},
+ {"__attribute__", 0, st_C_attribute},
+ {"SYSCALL", 0, st_C_gnumacro},
+ {"PSEUDO", 0, st_C_gnumacro},
+ {"ENTRY", 0, st_C_gnumacro},
+ {"DEFUN", 0, st_C_gnumacro}
};
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
{
register int key = hash (str, len);
- if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE)
+ if (key <= MAX_HASH_VALUE && key >= 0)
{
- register char *s = wordlist[key].name;
+ register const char *s = wordlist[key].name;
- if (*s == *str && !strncmp (str + 1, s + 1, len - 1))
+ if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')
return &wordlist[key];
}
}
return 0;
}
-/* ending time is 10:31:16 */
/*%>*/
-enum sym_type
+static enum sym_type
C_symtype (str, len, c_ext)
char *str;
int len;
return st_none;
return se->type;
}
+
\f
- /*
- * C functions and variables are recognized using a simple
- * finite automaton. fvdef is its state variable.
- */
-enum
+/*
+ * Ignoring __attribute__ ((list))
+ */
+static bool inattribute; /* looking at an __attribute__ construct */
+
+/*
+ * C functions and variables are recognized using a simple
+ * finite automaton. fvdef is its state variable.
+ */
+static enum
{
fvnone, /* nothing seen */
+ fdefunkey, /* Emacs DEFUN keyword seen */
+ fdefunname, /* Emacs DEFUN name seen */
+ foperator, /* func: operator keyword seen (cplpl) */
fvnameseen, /* function or variable name seen */
fstartlist, /* func: just after open parenthesis */
finlist, /* func: in parameter list */
vignore /* var-like: ignore until ';' */
} fvdef;
+static bool fvextern; /* func or var: extern keyword seen; */
- /*
- * typedefs are recognized using a simple finite automaton.
- * typdef is its state variable.
- */
-enum
+/*
+ * typedefs are recognized using a simple finite automaton.
+ * typdef is its state variable.
+ */
+static enum
{
tnone, /* nothing seen */
- ttypedseen, /* typedef keyword seen */
+ tkeyseen, /* typedef keyword seen */
+ ttypeseen, /* defined type seen */
tinbody, /* inside typedef body */
tend, /* just before typedef tag */
tignore /* junk after typedef tag */
} typdef;
-
- /*
- * struct-like structures (enum, struct and union) are recognized
- * using another simple finite automaton. `structdef' is its state
- * variable.
- */
-enum
+/*
+ * struct-like structures (enum, struct and union) are recognized
+ * using another simple finite automaton. `structdef' is its state
+ * variable.
+ */
+static enum
{
- snone, /* nothing seen yet */
+ snone, /* nothing seen yet,
+ or in struct body if bracelev > 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*/
+ scolonseen /* colon seen after struct-like tag */
} structdef;
-/*
- * When structdef is stagseen, scolonseen, or sinbody, structtag is the
- * struct tag, and structtype is the type of the preceding struct-like
- * keyword.
- */
-char *structtag = "<uninited>";
-enum sym_type structtype;
-
/*
* When objdef is different from onone, objtag is the name of the class.
*/
-char *objtag = "<uninited>";
+static char *objtag = "<uninited>";
/*
* Yet another little state machine to deal with preprocessor lines.
*/
-enum
+static enum
{
dnone, /* nothing seen */
dsharpseen, /* '#' seen as first char on line */
/*
* 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
+static enum
{
onone, /* nothing seen */
oprotocol, /* @interface or @protocol seen */
* 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
+static struct tok
{
- bool valid;
- char *str;
- bool named;
- int linelen;
- int lineno;
- long linepos;
- char *buffer;
-} TOKEN;
-TOKEN tok; /* latest token read */
-
-
-/*
- * Set this to TRUE, and the next token considered is called a function.
- * Used only for GNU emacs's function-defining macros.
- */
-bool next_token_is_func;
-
-/*
- * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
- */
-bool yacc_rules;
+ char *line; /* string containing the token */
+ int offset; /* where the token starts in LINE */
+ int length; /* token length */
+ /*
+ The previous members can be used to pass strings around for generic
+ purposes. The following ones specifically refer to creating tags. In this
+ case the token contained here is the pattern that will be used to create a
+ tag.
+ */
+ bool valid; /* do not create a tag; the token should be
+ invalidated whenever a state machine is
+ reset prematurely */
+ bool named; /* create a named tag */
+ int lineno; /* source line number of tag */
+ long linepos; /* source char number of tag */
+} token; /* latest token read */
/*
- * methodlen is the length of the method name stored in token_name.
+ * Variables and functions for dealing with nested structures.
+ * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
*/
-int methodlen;
-
-/*
- * consider_token ()
- * checks to see if the current token is at the start of a
- * function or variable, or corresponds to a typedef, or
- * 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.
- *
- * In the future we will need some way to adjust where the end of
- * the token is; for instance, implementing the C++ keyword
- * `operator' properly will adjust the end of the token to be after
- * whatever follows `operator'.
- *
- * Globals
- * fvdef IN OUT
- * structdef IN OUT
- * 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)
- 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 */
- 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);
-
- /*
- * Advance the definedef state machine.
- */
- switch (definedef)
- {
- case dnone:
- /* We're not on a preprocessor line. */
- break;
- case dsharpseen:
- if (toktype == st_C_define)
- {
- definedef = ddefineseen;
- }
- else
- {
- definedef = dignorerest;
- }
- return FALSE;
- case ddefineseen:
- /*
- * Make a tag for any macro, unless it is a constant
- * and constantypedefs is FALSE.
- */
- definedef = dignorerest;
- *is_func_or_var = (c == '(');
- if (!*is_func_or_var && !constantypedefs)
- return FALSE;
- else
- return TRUE;
- case dignorerest:
- return FALSE;
- default:
- error ("internal error: definedef value.", (char *)NULL);
- }
-
- /*
- * Now typedefs
- */
- switch (typdef)
- {
- case tnone:
- if (toktype == st_C_typedef)
- {
- if (typedefs)
- typdef = ttypedseen;
- fvdef = fvnone;
- return FALSE;
- }
- break;
- case ttypedseen:
- switch (toktype)
- {
- case st_none:
- case st_C_typespec:
- typdef = tend;
- break;
- case st_C_struct:
- case st_C_enum:
- break;
- }
- /* Do not return here, so the structdef stuff has a chance. */
- break;
- case tend:
- switch (toktype)
- {
- case st_C_typespec:
- case st_C_struct:
- case st_C_enum:
- return FALSE;
- }
- return TRUE;
- }
-
- /*
- * This structdef business is currently only invoked when cblev==0.
- * It should be recursively invoked whatever the curly brace level,
- * and a stack of states kept, to allow for definitions of structs
- * within structs.
- *
- * This structdef business is NOT invoked when we are ctags and the
- * file is plain C. This is because a struct tag may have the same
- * name as another tag, and this loses with ctags.
- */
- switch (toktype)
- {
- case st_C_javastruct:
- if (structdef == stagseen)
- structdef = scolonseen;
- return FALSE;
- break;
- case st_C_struct:
- case st_C_enum:
- if (typdef == ttypedseen
- || (typedefs_and_cplusplus && cblev == 0 && structdef == snone))
- {
- structdef = skeyseen;
- structtype = toktype;
- }
- return FALSE;
- }
+static void pushclass_above __P((int, char *, int));
+static void popclass_above __P((int));
+static void write_classname __P((linebuffer *, char *qualifier));
+
+static struct {
+ char **cname; /* nested class names */
+ int *bracelev; /* nested class 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 a nested function. */
+#define instruct (structdef == snone && nestlev > 0 \
+ && bracelev == cstack.bracelev[nestlev-1] + 1)
+
+static void
+pushclass_above (bracelev, str, len)
+ int bracelev;
+ char *str;
+ int len;
+{
+ int nl;
- if (structdef == skeyseen)
+ popclass_above (bracelev);
+ nl = cstack.nl;
+ if (nl >= cstack.size)
{
- /* 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;
+ int size = cstack.size *= 2;
+ xrnew (cstack.cname, size, char *);
+ xrnew (cstack.bracelev, size, int);
}
+ assert (nl == 0 || cstack.bracelev[nl-1] < bracelev);
+ cstack.cname[nl] = (str == NULL) ? NULL : savenstr (str, len);
+ cstack.bracelev[nl] = bracelev;
+ cstack.nl = nl + 1;
+}
+
+static void
+popclass_above (bracelev)
+ int bracelev;
+{
+ int nl;
- /* Avoid entering fvdef stuff if typdef is going on. */
- if (typdef != tnone)
+ for (nl = cstack.nl - 1;
+ nl >= 0 && cstack.bracelev[nl] >= bracelev;
+ nl--)
{
- definedef = dnone;
- return FALSE;
+ 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);
- /* 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)
+ if (cstack.nl == 0 || cstack.cname[0] == NULL)
{
- next_token_is_func = TRUE;
- return FALSE;
+ len = 0;
+ cn->len = 0;
+ cn->buffer[0] = '\0';
}
- if (next_token_is_func)
+ else
{
- next_token_is_func = FALSE;
- fvdef = fignore;
- *is_func_or_var = TRUE;
- return TRUE;
+ len = strlen (cstack.cname[0]);
+ linebuffer_setlen (cn, len);
+ strcpy (cn->buffer, cstack.cname[0]);
}
-
- /* Detect Objective C constructs. */
- switch (objdef)
+ for (i = 1; i < cstack.nl; i++)
{
- case onone:
- switch (toktype)
- {
- case st_C_objprot:
- objdef = oprotocol;
- return FALSE;
- case st_C_objimpl:
- objdef = oimplementation;
- return FALSE;
- }
- break;
- case oimplementation:
- /* Save the class tag for functions or variables defined inside. */
- objtag = savenstr (str, len);
- objdef = oinbody;
- return FALSE;
- case oprotocol:
- /* Save the class tag for categories. */
- objtag = savenstr (str, len);
- objdef = otagseen;
- *is_func_or_var = TRUE;
- return TRUE;
- case oparenseen:
- objdef = ocatseen;
- *is_func_or_var = TRUE;
- return TRUE;
- case oinbody:
- break;
- case omethodsign:
- if (parlev == 0)
- {
- objdef = omethodtag;
- methodlen = len;
- grow_linebuffer (&token_name, methodlen + 1);
- strncpy (token_name.buffer, str, len);
- token_name.buffer[methodlen] = '\0';
- token_name.len = methodlen;
- return TRUE;
- }
- return FALSE;
- case omethodcolon:
- if (parlev == 0)
- objdef = omethodparm;
- return FALSE;
- case omethodparm:
- if (parlev == 0)
- {
- objdef = omethodtag;
- methodlen += len;
- grow_linebuffer (&token_name, methodlen + 1);
- strncat (token_name.buffer, str, len);
- token_name.len = methodlen;
- return TRUE;
- }
- return FALSE;
- case oignore:
- if (toktype == st_C_objend)
- {
- /* Memory leakage here: the string pointed by objtag is
- never released, because many tests would be needed to
- avoid breaking on incorrect input code. The amount of
- memory leaked here is the sum of the lengths of the
- class tags.
- free (objtag); */
- objdef = onone;
- }
- return FALSE;
+ char *s;
+ int slen;
+
+ s = cstack.cname[i];
+ if (s == NULL)
+ continue;
+ slen = strlen (s);
+ len += slen + qlen;
+ linebuffer_setlen (cn, len);
+ strncat (cn->buffer, qualifier, qlen);
+ strncat (cn->buffer, s, slen);
}
+}
+
+\f
+static bool consider_token __P((char *, int, int, int *, int, int, bool *));
+static void make_C_tag __P((bool));
+
+/*
+ * consider_token ()
+ * checks to see if the current token is at the start of a
+ * function or variable, or corresponds to a typedef, or
+ * 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_EXTP points to which language we are looking at.
+ *
+ * Globals
+ * fvdef IN OUT
+ * structdef IN OUT
+ * definedef IN OUT
+ * typdef IN OUT
+ * objdef IN OUT
+ */
+
+static bool
+consider_token (str, len, c, c_extp, bracelev, parlev, is_func_or_var)
+ register char *str; /* IN: token pointer */
+ register int len; /* IN: token length */
+ register int c; /* IN: first char after the token */
+ int *c_extp; /* IN, OUT: C extensions mask */
+ int bracelev; /* IN: brace level */
+ int parlev; /* IN: parenthesis level */
+ bool *is_func_or_var; /* OUT: function or variable found */
+{
+ /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
+ structtype is the type of the preceding struct-like keyword, and
+ structbracelev is the brace level where it has been seen. */
+ static enum sym_type structtype;
+ static int structbracelev;
+ static enum sym_type toktype;
+
- /* A function, variable or enum constant? */
- switch (toktype)
+ toktype = C_symtype (str, len, *c_extp);
+
+ /*
+ * Skip __attribute__
+ */
+ if (toktype == st_C_attribute)
{
- case st_C_typespec:
- if (fvdef != finlist && fvdef != fignore && fvdef != vignore)
- fvdef = fvnone; /* should be useless */
- return FALSE;
- case st_C_ignore:
- fvdef = vignore;
+ inattribute = TRUE;
return FALSE;
- case st_none:
- if (constantypedefs && structdef == sinbody && structtype == st_C_enum)
- return TRUE;
- if (fvdef == fvnone)
- {
+ }
+
+ /*
+ * Advance the definedef state machine.
+ */
+ switch (definedef)
+ {
+ 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)
+ {
+ definedef = ddefineseen;
+ }
+ else
+ {
+ definedef = dignorerest;
+ }
+ return FALSE;
+ case ddefineseen:
+ /*
+ * Make a tag for any macro, unless it is a constant
+ * and constantypedefs is FALSE.
+ */
+ definedef = dignorerest;
+ *is_func_or_var = (c == '(');
+ if (!*is_func_or_var && !constantypedefs)
+ return FALSE;
+ else
+ return TRUE;
+ case dignorerest:
+ return FALSE;
+ default:
+ error ("internal error: definedef value.", (char *)NULL);
+ }
+
+ /*
+ * Now typedefs
+ */
+ switch (typdef)
+ {
+ case tnone:
+ if (toktype == st_C_typedef)
+ {
+ if (typedefs)
+ typdef = tkeyseen;
+ fvextern = FALSE;
+ fvdef = fvnone;
+ return FALSE;
+ }
+ break;
+ case tkeyseen:
+ switch (toktype)
+ {
+ case st_none:
+ case st_C_class:
+ case st_C_struct:
+ case st_C_enum:
+ typdef = ttypeseen;
+ }
+ break;
+ case ttypeseen:
+ if (structdef == snone && fvdef == fvnone)
+ {
+ fvdef = fvnameseen;
+ return TRUE;
+ }
+ break;
+ case tend:
+ switch (toktype)
+ {
+ case st_C_class:
+ case st_C_struct:
+ case st_C_enum:
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ /*
+ * 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.
+ */
+ switch (toktype)
+ {
+ case st_C_javastruct:
+ if (structdef == stagseen)
+ structdef = scolonseen;
+ return FALSE;
+ case st_C_template:
+ case st_C_class:
+ if ((*c_extp & C_AUTO) /* automatic detection of C++ language */
+ && bracelev == 0
+ && definedef == dnone && structdef == snone
+ && typdef == tnone && fvdef == fvnone)
+ *c_extp = (*c_extp | C_PLPL) & ~C_AUTO;
+ if (toktype == st_C_template)
+ break;
+ /* FALLTHRU */
+ case st_C_struct:
+ case st_C_enum:
+ if (parlev == 0
+ && fvdef != vignore
+ && (typdef == tkeyseen
+ || (typedefs_or_cplusplus && structdef == snone)))
+ {
+ structdef = skeyseen;
+ structtype = toktype;
+ structbracelev = bracelev;
+ if (fvdef == fvnameseen)
+ fvdef = fvnone;
+ }
+ return FALSE;
+ }
+
+ if (structdef == skeyseen)
+ {
+ structdef = stagseen;
+ return TRUE;
+ }
+
+ if (typdef != tnone)
+ definedef = dnone;
+
+ /* Detect Objective C constructs. */
+ switch (objdef)
+ {
+ case onone:
+ switch (toktype)
+ {
+ case st_C_objprot:
+ objdef = oprotocol;
+ return FALSE;
+ case st_C_objimpl:
+ objdef = oimplementation;
+ return FALSE;
+ }
+ break;
+ case oimplementation:
+ /* Save the class tag for functions or variables defined inside. */
+ objtag = savenstr (str, len);
+ objdef = oinbody;
+ return FALSE;
+ case oprotocol:
+ /* Save the class tag for categories. */
+ objtag = savenstr (str, len);
+ objdef = otagseen;
+ *is_func_or_var = TRUE;
+ return TRUE;
+ case oparenseen:
+ objdef = ocatseen;
+ *is_func_or_var = TRUE;
+ return TRUE;
+ case oinbody:
+ break;
+ case omethodsign:
+ if (parlev == 0)
+ {
+ fvdef = fvnone;
+ objdef = omethodtag;
+ linebuffer_setlen (&token_name, len);
+ strncpy (token_name.buffer, str, len);
+ token_name.buffer[len] = '\0';
+ return TRUE;
+ }
+ return FALSE;
+ case omethodcolon:
+ if (parlev == 0)
+ objdef = omethodparm;
+ return FALSE;
+ case omethodparm:
+ if (parlev == 0)
+ {
+ fvdef = fvnone;
+ objdef = omethodtag;
+ linebuffer_setlen (&token_name, token_name.len + len);
+ strncat (token_name.buffer, str, len);
+ return TRUE;
+ }
+ return FALSE;
+ case oignore:
+ if (toktype == st_C_objend)
+ {
+ /* Memory leakage here: the string pointed by objtag is
+ never released, because many tests would be needed to
+ avoid breaking on incorrect input code. The amount of
+ memory leaked here is the sum of the lengths of the
+ class tags.
+ free (objtag); */
+ objdef = onone;
+ }
+ return FALSE;
+ }
+
+ /* A function, variable or enum constant? */
+ switch (toktype)
+ {
+ case st_C_extern:
+ fvextern = TRUE;
+ switch (fvdef)
+ {
+ case finlist:
+ case flistseen:
+ case fignore:
+ case vignore:
+ break;
+ default:
+ fvdef = fvnone;
+ }
+ return FALSE;
+ case st_C_ignore:
+ fvextern = FALSE;
+ fvdef = vignore;
+ return FALSE;
+ case st_C_operator:
+ fvdef = foperator;
+ *is_func_or_var = TRUE;
+ return TRUE;
+ case st_none:
+ if (constantypedefs
+ && structdef == snone
+ && structtype == st_C_enum && bracelev > structbracelev)
+ return TRUE; /* enum constant */
+ switch (fvdef)
+ {
+ case fdefunkey:
+ if (bracelev > 0)
+ break;
+ fvdef = fdefunname; /* GNU macro */
+ *is_func_or_var = TRUE;
+ return TRUE;
+ case fvnone:
+ switch (typdef)
+ {
+ case ttypeseen:
+ return FALSE;
+ case tnone:
+ if ((strneq (str, "asm", 3) && endtoken (str[3]))
+ || (strneq (str, "__asm__", 7) && endtoken (str[7])))
+ {
+ fvdef = vignore;
+ return FALSE;
+ }
+ break;
+ }
+ /* FALLTHRU */
+ case fvnameseen:
+ if (len >= 10 && strneq (str+len-10, "::operator", 10))
+ {
+ if (*c_extp & C_AUTO) /* automatic detection of C++ */
+ *c_extp = (*c_extp | C_PLPL) & ~C_AUTO;
+ fvdef = foperator;
+ *is_func_or_var = TRUE;
+ return TRUE;
+ }
+ if (bracelev > 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.
*/
+static 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 \
+#define plainc ((c_ext & C_EXT) == C_PLAIN)
+#define cplpl (c_ext & C_PLPL)
+#define cjava ((c_ext & C_JAVA) == C_JAVA)
+
+#define CNL_SAVE_DEFINEDEF() \
do { \
curlinepos = charno; \
- lineno++; \
- linecharno = charno; \
- charno += readline (&curlb, inf); \
+ readline (&curlb, inf); \
lp = curlb.buffer; \
quotednl = FALSE; \
newndx = curndx; \
} while (0)
-#define CNL \
+#define CNL() \
do { \
- CNL_SAVE_DEFINEDEF; \
- if (savetok.valid) \
+ CNL_SAVE_DEFINEDEF(); \
+ if (savetoken.valid) \
{ \
- tok = savetok; \
- savetok.valid = FALSE; \
+ token = savetoken; \
+ savetoken.valid = FALSE; \
} \
definedef = dnone; \
} while (0)
-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 (traditional_tag_style)
- {
- /* This was the original code. Now we call new_pfnote instead,
- which uses the new method for naming tags (see new_pfnote). */
- char *name = NULL;
-
- if (CTAGS || tok.named)
- name = savestr (token_name.buffer);
- pfnote (name, isfun,
- tok.buffer, tok.linelen, tok.lineno, tok.linepos);
- }
- else
- new_pfnote (token_name.buffer, token_name.len, isfun,
- tok.buffer, tok.linelen, tok.lineno, tok.linepos);
- tok.valid = FALSE;
- }
- else if (DEBUG)
- abort ();
+ if (!DEBUG && !token.valid)
+ return;
+
+ if (token.valid)
+ make_tag (token_name.buffer, token_name.len, isfun, token.line,
+ token.offset+token.length+1, token.lineno, token.linepos);
+ else /* this case is optimised away if !DEBUG */
+ make_tag (concat ("INVALID TOKEN:-->", token_name.buffer, ""),
+ token_name.len + 17, isfun, token.line,
+ token.offset+token.length+1, token.lineno, token.linepos);
+
+ token.valid = FALSE;
}
-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 */
register int toklen; /* length of current token */
char *qualifier; /* string used to qualify names */
int qlen; /* length of qualifier */
- int cblev; /* current curly brace level */
+ int bracelev; /* current brace level */
+ int bracketlev; /* current bracket level */
int parlev; /* current parenthesis level */
+ int attrparlev; /* __attribute__ parenthesis level */
+ int templatelev; /* current template level */
+ int typdefbracelev; /* bracelev 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 */
+ linebuffer_init (&lbs[0].lb);
+ linebuffer_init (&lbs[1].lb);
+ if (cstack.size == 0)
+ {
+ cstack.size = (DEBUG) ? 1 : 4;
+ cstack.nl = 0;
+ cstack.cname = xnew (cstack.size, char *);
+ cstack.bracelev = xnew (cstack.size, int);
+ }
+
+ tokoff = toklen = typdefbracelev = 0; /* keep compiler quiet */
curndx = newndx = 0;
- lineno = 0;
- charno = 0;
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;
- cblev = 0;
- parlev = 0;
- cplpl = (c_ext & C_PLPL) == C_PLPL;
- cjava = (c_ext & C_JAVA) == C_JAVA;
+ token.valid = savetoken.valid = FALSE;
+ bracelev = bracketlev = parlev = attrparlev = templatelev = 0;
if (cjava)
{ qualifier = "."; qlen = 1; }
else
{ qualifier = "::"; qlen = 2; }
+
while (!feof (inf))
{
c = *lp++;
if (c == '\\')
{
- /* If we're at the end of the line, the next character is a
- '\0'; don't skip it, because it's the thing that tells us
+ /* If we are at the end of the line, the next character is a
+ '\0'; do not skip it, because it is what tells us
to read the next line. */
if (*lp == '\0')
{
case '\0':
/* Newlines inside comments do not end macro definitions in
traditional cpp. */
- CNL_SAVE_DEFINEDEF;
+ CNL_SAVE_DEFINEDEF ();
break;
}
continue;
/* Newlines inside strings do not end macro definitions
in traditional cpp, even though compilers don't
usually accept them. */
- CNL_SAVE_DEFINEDEF;
+ CNL_SAVE_DEFINEDEF ();
break;
}
continue;
{
case '\0':
/* Hmmm, something went wrong. */
- CNL;
+ CNL ();
/* FALLTHRU */
case '\'':
inchar = FALSE;
}
continue;
}
- else
- switch (c)
- {
- case '"':
- inquote = TRUE;
- if (fvdef != finlist && fvdef != fignore && fvdef !=vignore)
- fvdef = fvnone;
- continue;
- case '\'':
- inchar = TRUE;
- if (fvdef != finlist && fvdef != fignore && fvdef !=vignore)
- fvdef = fvnone;
- continue;
- case '/':
- if (*lp == '*')
- {
- lp++;
- incomm = TRUE;
+ else if (bracketlev > 0)
+ {
+ switch (c)
+ {
+ case ']':
+ if (--bracketlev > 0)
continue;
- }
- else if (/* cplpl && */ *lp == '/')
- {
- c = '\0';
- break;
- }
- else
break;
- case '%':
- if ((c_ext & YACC) && *lp == '%')
- {
- /* entering or exiting rules section in yacc file */
- lp++;
- definedef = dnone; fvdef = fvnone;
- typdef = tnone; structdef = snone;
- next_token_is_func = FALSE;
- midtoken = inquote = inchar = incomm = quotednl = FALSE;
- cblev = 0;
- yacc_rules = !yacc_rules;
- continue;
- }
- else
+ case '\0':
+ CNL_SAVE_DEFINEDEF ();
break;
- case '#':
- if (definedef == dnone)
- {
- char *cp;
- bool cpptoken = TRUE;
-
- /* Look back on this line. If all blanks, or nonblanks
- followed by an end of comment, this is a preprocessor
- token. */
- for (cp = newlb.buffer; cp < lp-1; cp++)
- if (!iswhite (*cp))
- {
- if (*cp == '*' && *(cp+1) == '/')
- {
- cp++;
- cpptoken = TRUE;
- }
- else
- cpptoken = FALSE;
- }
- if (cpptoken)
- definedef = dsharpseen;
- } /* if (definedef == dnone) */
-
+ }
+ continue;
+ }
+ else switch (c)
+ {
+ case '"':
+ inquote = TRUE;
+ if (inattribute)
+ break;
+ switch (fvdef)
+ {
+ case fdefunkey:
+ case fstartlist:
+ case finlist:
+ case fignore:
+ case vignore:
+ break;
+ default:
+ fvextern = FALSE;
+ fvdef = fvnone;
+ }
+ continue;
+ case '\'':
+ inchar = TRUE;
+ if (inattribute)
+ break;
+ if (fvdef != finlist && fvdef != fignore && fvdef !=vignore)
+ {
+ fvextern = FALSE;
+ fvdef = fvnone;
+ }
+ continue;
+ case '/':
+ if (*lp == '*')
+ {
+ lp++;
+ incomm = TRUE;
+ continue;
+ }
+ else if (/* cplpl && */ *lp == '/')
+ {
+ c = '\0';
+ break;
+ }
+ else
+ break;
+ case '%':
+ if ((c_ext & YACC) && *lp == '%')
+ {
+ /* Entering or exiting rules section in yacc file. */
+ lp++;
+ definedef = dnone; fvdef = fvnone; fvextern = FALSE;
+ typdef = tnone; structdef = snone;
+ midtoken = inquote = inchar = incomm = quotednl = FALSE;
+ bracelev = 0;
+ yacc_rules = !yacc_rules;
+ continue;
+ }
+ else
+ break;
+ case '#':
+ if (definedef == dnone)
+ {
+ char *cp;
+ bool cpptoken = TRUE;
+
+ /* Look back on this line. If all blanks, or nonblanks
+ followed by an end of comment, this is a preprocessor
+ token. */
+ for (cp = newlb.buffer; cp < lp-1; cp++)
+ if (!iswhite (*cp))
+ {
+ if (*cp == '*' && *(cp+1) == '/')
+ {
+ cp++;
+ cpptoken = TRUE;
+ }
+ else
+ cpptoken = FALSE;
+ }
+ if (cpptoken)
+ definedef = dsharpseen;
+ } /* if (definedef == dnone) */
+ continue;
+ case '[':
+ bracketlev++;
continue;
- } /* switch (c) */
+ } /* 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
+ && templatelev == 0
+ && (definedef != dnone
+ || structdef != scolonseen)
+ && !inattribute)
{
if (midtoken)
{
if (endtoken (c))
{
- if (c == ':' && cplpl && *lp == ':' && begtoken(*(lp + 1)))
+ if (c == ':' && *lp == ':' && begtoken (lp[1]))
+ /* This handles :: in the middle,
+ but not at the beginning of an identifier.
+ Also, space-separated :: is not recognised. */
{
- /*
- * This handles :: in the middle, but not at the
- * beginning of an identifier.
- */
+ if (c_ext & C_AUTO) /* automatic detection of C++ */
+ c_ext = (c_ext | C_PLPL) & ~C_AUTO;
lp += 2;
- toklen += 3;
+ toklen += 2;
+ c = lp[-1];
+ goto still_in_token;
}
else
{
if (yacc_rules
|| consider_token (newlb.buffer + tokoff, toklen, c,
- c_ext, cblev, parlev, &funorvar))
+ &c_ext, bracelev, parlev,
+ &funorvar))
{
- tok.named = FALSE;
- if (structdef == sinbody
- && definedef == dnone
- && funorvar)
- /* function or var defined in C++ class body */
+ if (fvdef == foperator)
{
- int 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 (!plainc
+ && 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
+ && bracelev > 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);
}
+ else /* not yacc and consider_token failed */
+ {
+ if (inattribute && fvdef == fignore)
+ {
+ /* We have just met __attribute__ after a
+ function parameter list: do not tag the
+ function again. */
+ fvdef = fvnone;
+ }
+ }
midtoken = FALSE;
}
} /* if (endtoken (c)) */
else if (intoken (c))
+ still_in_token:
{
toklen++;
continue;
switch (fvdef)
{
case fstartlist:
+ /* This prevents tagging fb in
+ void (__attribute__((noreturn)) *fb) (void);
+ Fixing this is not easy and not very important. */
fvdef = finlist;
continue;
case flistseen:
- make_C_tag (TRUE); /* a function */
- fvdef = fignore;
- break;
- case fvnameseen:
- fvdef = fvnone;
+ if (plainc || declarations)
+ {
+ make_C_tag (TRUE); /* a function */
+ fvdef = fignore;
+ }
break;
}
if (structdef == stagseen && !cjava)
- structdef = snone;
+ {
+ popclass_above (bracelev);
+ structdef = snone;
+ }
break;
case dsharpseen:
- savetok = tok;
+ savetoken = token;
+ break;
}
if (!yacc_rules || lp == newlb.buffer + 1)
{
switch (c)
{
case ':':
+ if (inattribute)
+ break;
+ if (yacc_rules && token.offset == 0 && token.valid)
+ {
+ make_C_tag (FALSE); /* a yacc function */
+ break;
+ }
if (definedef != dnone)
break;
switch (objdef)
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;
- }
+ {
+ structdef = scolonseen;
+ break;
+ }
+ /* Should be useless, but may be work as a safety net. */
+ if (cplpl && fvdef == flistseen)
+ {
+ make_C_tag (TRUE); /* a function */
+ fvdef = fignore;
+ break;
+ }
break;
case ';':
- if (definedef != dnone)
+ if (definedef != dnone || inattribute)
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 || cplpl)
+ fvdef = fvnone;
+ break;
+ case fvnameseen:
+ if ((globals && bracelev == 0 && (!fvextern || declarations))
+ || (members && instruct))
+ make_C_tag (FALSE); /* a variable */
+ fvextern = FALSE;
+ fvdef = fvnone;
+ token.valid = FALSE;
+ break;
+ case flistseen:
+ if ((declarations
+ && (cplpl || !instruct)
+ && (typdef == tnone || (typdef != tignore && instruct)))
+ || (members
+ && plainc && instruct))
+ make_C_tag (TRUE); /* a function */
+ /* FALLTHRU */
+ default:
+ fvextern = FALSE;
+ fvdef = fvnone;
+ if (declarations
+ && cplpl && structdef == stagseen)
+ make_C_tag (FALSE); /* forward declaration */
+ else
+ 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;
break;
case ',':
- if (definedef != dnone)
+ if (definedef != dnone || inattribute)
break;
switch (objdef)
{
}
switch (fvdef)
{
+ case fdefunkey:
+ case foperator:
+ case fstartlist:
case finlist:
case fignore:
case vignore:
break;
+ case fdefunname:
+ fvdef = fignore;
+ break;
case fvnameseen:
- if ((globals && cblev == 0) || (members && cblev == 1))
- make_C_tag (FALSE); /* a variable */
+ if (parlev == 0
+ && ((globals
+ && bracelev == 0
+ && templatelev == 0
+ && (!fvextern || declarations))
+ || (members && instruct)))
+ make_C_tag (FALSE); /* a variable */
+ break;
+ case flistseen:
+ if ((declarations && typdef == tnone && !instruct)
+ || (members && typdef != tignore && instruct))
+ {
+ make_C_tag (TRUE); /* a function */
+ fvdef = fvnameseen;
+ }
+ else if (!declarations)
+ fvdef = fvnone;
+ token.valid = FALSE;
break;
default:
fvdef = fvnone;
if (structdef == stagseen)
structdef = snone;
break;
- case '[':
- if (definedef != dnone)
+ case ']':
+ if (definedef != dnone || inattribute)
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 finlist:
- case fignore:
- case vignore:
+ case tnone:
+ case tinbody:
+ switch (fvdef)
+ {
+ case foperator:
+ case finlist:
+ case fignore:
+ case vignore:
+ break;
+ case fvnameseen:
+ if ((members && bracelev == 1)
+ || (globals && bracelev == 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 (inattribute)
+ {
+ attrparlev++;
+ break;
+ }
if (definedef != dnone)
break;
if (objdef == otagseen && parlev == 0)
objdef = oparenseen;
switch (fvdef)
{
- case fvnone:
- switch (typdef)
- {
- case ttypedseen:
- case tend:
- if (tok.valid && *lp != '*')
- {
- /* This handles constructs like:
- typedef void OperatorFun (int fun); */
- make_C_tag (FALSE);
- typdef = tignore;
- }
- break;
- } /* switch (typdef) */
- break;
case fvnameseen:
+ if (typdef == ttypeseen
+ && *lp != '*'
+ && !instruct)
+ {
+ /* This handles constructs like:
+ typedef void OperatorFun (int fun); */
+ make_C_tag (FALSE);
+ typdef = tignore;
+ fvdef = fignore;
+ break;
+ }
+ /* FALLTHRU */
+ case foperator:
fvdef = fstartlist;
break;
case flistseen:
parlev++;
break;
case ')':
+ if (inattribute)
+ {
+ if (--attrparlev == 0)
+ inattribute = FALSE;
+ break;
+ }
if (definedef != dnone)
break;
if (objdef == ocatseen && parlev == 1)
fvdef = flistseen;
break;
}
- if (cblev == 0 && typdef == tend)
+ if (!instruct
+ && (typdef == tend
+ || typdef == ttypeseen))
{
typdef = tignore;
make_C_tag (FALSE); /* a typedef */
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;
+ /* Whenever typdef is set to tinbody (currently only
+ here), typdefbracelev should be set to bracelev. */
+ typdef = tinbody;
+ typdefbracelev = bracelev;
}
switch (fvdef)
{
case flistseen:
- make_C_tag (TRUE); /* a function */
+ make_C_tag (TRUE); /* a function */
/* FALLTHRU */
case fignore:
fvdef = fvnone;
break;
default:
/* Neutralize `extern "C" {' grot. */
- if (cblev == 0 && structdef == snone && typdef == tnone)
- cblev = -1;
+ if (bracelev == 0 && structdef == snone && nestlev == 0
+ && typdef == tnone)
+ bracelev = -1;
}
+ break;
+ }
+ switch (structdef)
+ {
+ case skeyseen: /* unnamed struct */
+ pushclass_above (bracelev, NULL, 0);
+ structdef = snone;
+ break;
+ case stagseen: /* named struct or enum */
+ case scolonseen: /* a class */
+ pushclass_above (bracelev,token.line+token.offset, token.length);
+ structdef = snone;
+ make_C_tag (FALSE); /* a struct or enum */
+ break;
}
- cblev++;
+ bracelev++;
break;
case '*':
if (definedef != dnone)
break;
if (fvdef == fstartlist)
- fvdef = fvnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
+ {
+ fvdef = fvnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
+ token.valid = FALSE;
+ }
break;
case '}':
if (definedef != dnone)
break;
- if (!noindentypedefs && lp == newlb.buffer + 1)
+ if (!ignoreindent && lp == newlb.buffer + 1)
{
- cblev = 0; /* reset curly brace level if first column */
+ if (bracelev != 0)
+ token.valid = FALSE;
+ bracelev = 0; /* reset brace level if first column */
parlev = 0; /* also reset paren level, just in case... */
}
- else if (cblev > 0)
- cblev--;
- if (cblev == 0)
+ else if (bracelev > 0)
+ bracelev--;
+ else
+ token.valid = FALSE; /* something gone amiss, token unreliable */
+ popclass_above (bracelev);
+ structdef = snone;
+ /* Only if typdef == tinbody is typdefbracelev significant. */
+ if (typdef == tinbody && bracelev <= typdefbracelev)
{
- 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 (bracelev == typdefbracelev);
+ typdef = tend;
}
break;
case '=':
break;
switch (fvdef)
{
+ case foperator:
case finlist:
case fignore:
case vignore:
break;
case fvnameseen:
- if ((globals && cblev == 0) || (members && cblev == 1))
+ if ((members && bracelev == 1)
+ || (globals && bracelev == 0 && (!fvextern || declarations)))
make_C_tag (FALSE); /* a variable */
/* FALLTHRU */
default:
fvdef = vignore;
}
break;
+ case '<':
+ if (cplpl
+ && (structdef == stagseen || fvdef == fvnameseen))
+ {
+ templatelev++;
+ break;
+ }
+ goto resetfvdef;
+ case '>':
+ if (templatelev > 0)
+ {
+ templatelev--;
+ break;
+ }
+ goto resetfvdef;
case '+':
case '-':
- if (objdef == oinbody && cblev == 0)
+ if (objdef == oinbody && bracelev == 0)
{
objdef = omethodsign;
break;
}
/* FALLTHRU */
- case '#': case '~': case '&': case '%': case '/': case '|':
- case '^': case '!': case '<': case '>': case '.': case '?': case ']':
+ resetfvdef:
+ case '#': case '~': case '&': case '%': case '/':
+ case '|': case '^': case '!': case '.': case '?':
if (definedef != dnone)
break;
- /* These surely cannot follow a function tag. */
- if (fvdef != finlist && fvdef != fignore && fvdef != vignore)
- fvdef = fvnone;
+ /* These surely cannot follow a function tag in C. */
+ switch (fvdef)
+ {
+ case foperator:
+ case finlist:
+ case fignore:
+ case vignore:
+ break;
+ default:
+ fvdef = fvnone;
+ }
break;
case '\0':
if (objdef == otagseen)
}
/* If a macro spans multiple lines don't reset its state. */
if (quotednl)
- CNL_SAVE_DEFINEDEF;
+ CNL_SAVE_DEFINEDEF ();
else
- CNL;
+ CNL ();
break;
} /* switch (c) */
} /* while not eof */
+
+ free (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;
{
}
/* Always do C++. */
-void
+static void
Cplusplus_entries (inf)
FILE *inf;
{
}
/* Always do Java. */
-void
+static void
Cjava_entries (inf)
FILE *inf;
{
}
/* Always do C*. */
-void
+static void
Cstar_entries (inf)
FILE *inf;
{
}
/* Always do Yacc. */
-void
+static void
Yacc_entries (inf)
FILE *inf;
{
C_entries (YACC, inf);
}
-\f
-/* Fortran parsing */
-char *dbp;
+\f
+/* Useful macros. */
+#define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
+ for (; /* loop initialization */ \
+ !feof (file_pointer) /* loop test */ \
+ && /* instructions at start of loop */ \
+ (readline (&line_buffer, file_pointer), \
+ char_pointer = line_buffer.buffer, \
+ TRUE); \
+ )
+
+#define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \
+ ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
+ && strneq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
+ && notinname ((cp)[sizeof(kw)-1]) /* end of kw */ \
+ && ((cp) = skip_spaces((cp)+sizeof(kw)-1))) /* skip spaces */
+
+/* Similar to LOOKING_AT but does not use notinname, does not skip */
+#define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \
+ ((assert("" kw), TRUE) /* syntax error if not a literal string */ \
+ && strncaseeq ((cp), kw, sizeof(kw)-1) /* cp points at kw */ \
+ && ((cp) += sizeof(kw)-1)) /* skip spaces */
-bool
-tail (cp)
- char *cp;
+/*
+ * Read a file, but do no processing. This is used to do regexp
+ * matching on files that have no language defined.
+ */
+static void
+just_read_file (inf)
+ FILE *inf;
{
- register int len = 0;
+ register char *dummy;
- while (*cp && lowcase(*cp) == lowcase(dbp[len]))
- cp++, len++;
- if (*cp == '\0' && !intoken(dbp[len]))
- {
- dbp += len;
- return TRUE;
- }
- return FALSE;
+ LOOP_ON_INPUT_LINES (inf, lb, dummy)
+ continue;
}
-void
-takeprec ()
+\f
+/* Fortran parsing */
+
+static void F_takeprec __P((void));
+static void F_getit __P((FILE *));
+
+static void
+F_takeprec ()
{
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
if (*dbp != '*')
return;
dbp++;
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
if (strneq (dbp, "(*)", 3))
{
dbp += 3;
return;
}
- if (!isdigit (*dbp))
+ if (!ISDIGIT (*dbp))
{
--dbp; /* force failure */
return;
}
do
dbp++;
- while (isdigit (*dbp));
+ while (ISDIGIT (*dbp));
}
-void
-getit (inf)
+static void
+F_getit (inf)
FILE *inf;
{
register char *cp;
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
if (*dbp == '\0')
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
+ readline (&lb, inf);
dbp = lb.buffer;
if (dbp[5] != '&')
return;
dbp += 6;
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
}
- if (!isalpha (*dbp)
- && *dbp != '_'
- && *dbp != '$')
+ if (!ISALPHA (*dbp) && *dbp != '_' && *dbp != '$')
return;
- for (cp = dbp + 1;
- (*cp
- && (isalpha (*cp) || isdigit (*cp) || (*cp == '_') || (*cp == '$')));
- cp++)
+ for (cp = dbp + 1; *cp != '\0' && intoken (*cp); cp++)
continue;
- pfnote (savenstr (dbp, cp-dbp), TRUE,
- lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ make_tag (dbp, cp-dbp, TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
}
-void
+
+static void
Fortran_functions (inf)
FILE *inf;
{
- lineno = 0;
- charno = 0;
-
- while (!feof (inf))
+ LOOP_ON_INPUT_LINES (inf, lb, dbp)
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
- dbp = lb.buffer;
if (*dbp == '%')
dbp++; /* Ratfor escape to fortran */
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
if (*dbp == '\0')
continue;
switch (lowcase (*dbp))
{
case 'i':
- if (tail ("integer"))
- takeprec ();
+ if (nocase_tail ("integer"))
+ F_takeprec ();
break;
case 'r':
- if (tail ("real"))
- takeprec ();
+ if (nocase_tail ("real"))
+ F_takeprec ();
break;
case 'l':
- if (tail ("logical"))
- takeprec ();
+ if (nocase_tail ("logical"))
+ F_takeprec ();
break;
case 'c':
- if (tail ("complex") || tail ("character"))
- takeprec ();
+ if (nocase_tail ("complex") || nocase_tail ("character"))
+ F_takeprec ();
break;
case 'd':
- if (tail ("double"))
+ if (nocase_tail ("double"))
{
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
if (*dbp == '\0')
continue;
- if (tail ("precision"))
+ if (nocase_tail ("precision"))
break;
continue;
}
break;
}
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
if (*dbp == '\0')
continue;
switch (lowcase (*dbp))
{
case 'f':
- if (tail ("function"))
- getit (inf);
+ if (nocase_tail ("function"))
+ F_getit (inf);
continue;
case 's':
- if (tail ("subroutine"))
- getit (inf);
+ if (nocase_tail ("subroutine"))
+ F_getit (inf);
continue;
case 'e':
- if (tail ("entry"))
- getit (inf);
- continue;
- case 'p':
- if (tail ("program"))
- {
- getit (inf);
- continue;
- }
- if (tail ("procedure"))
- getit (inf);
+ if (nocase_tail ("entry"))
+ F_getit (inf);
continue;
case 'b':
- if (tail ("blockdata") || tail ("block data"))
+ if (nocase_tail ("blockdata") || nocase_tail ("block data"))
{
- while (isspace (*dbp))
- dbp++;
+ dbp = skip_spaces (dbp);
if (*dbp == '\0') /* assume un-named */
- pfnote (savestr ("blockdata"), TRUE, lb.buffer,
- dbp - lb.buffer, lineno, linecharno);
+ make_tag ("blockdata", 9, TRUE,
+ lb.buffer, dbp - lb.buffer, lineno, linecharno);
else
- getit (inf); /* look for name */
+ F_getit (inf); /* look for name */
}
continue;
}
}
}
+
\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]'
+ * Ada parsing
+ * Original code by
+ * Philippe Waroquiers (1998)
*/
-void
-Asm_labels (inf)
+
+static void Ada_getit __P((FILE *, char *));
+
+/* Once we are positioned after an "interesting" keyword, let's get
+ the real tag value necessary. */
+static void
+Ada_getit (inf, name_qualifier)
FILE *inf;
+ char *name_qualifier;
{
register char *cp;
-
- lineno = 0;
- charno = 0;
+ char *name;
+ char c;
while (!feof (inf))
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
- cp = lb.buffer;
+ dbp = skip_spaces (dbp);
+ if (*dbp == '\0'
+ || (dbp[0] == '-' && dbp[1] == '-'))
+ {
+ readline (&lb, inf);
+ dbp = lb.buffer;
+ }
+ switch (lowcase(*dbp))
+ {
+ case 'b':
+ if (nocase_tail ("body"))
+ {
+ /* Skipping body of procedure body or package body or ....
+ resetting qualifier to body instead of spec. */
+ name_qualifier = "/b";
+ continue;
+ }
+ break;
+ case 't':
+ /* Skipping type of task type or protected type ... */
+ if (nocase_tail ("type"))
+ continue;
+ break;
+ }
+ if (*dbp == '"')
+ {
+ dbp += 1;
+ for (cp = dbp; *cp != '\0' && *cp != '"'; cp++)
+ continue;
+ }
+ else
+ {
+ dbp = skip_spaces (dbp);
+ for (cp = dbp;
+ (*cp != '\0'
+ && (ISALPHA (*cp) || ISDIGIT (*cp) || *cp == '_' || *cp == '.'));
+ cp++)
+ continue;
+ if (cp == dbp)
+ return;
+ }
+ c = *cp;
+ *cp = '\0';
+ name = concat (dbp, name_qualifier, "");
+ *cp = c;
+ make_tag (name, strlen (name), TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ free (name);
+ if (c == '"')
+ dbp = cp + 1;
+ return;
+ }
+}
+
+static void
+Ada_funcs (inf)
+ FILE *inf;
+{
+ bool inquote = FALSE;
+ bool skip_till_semicolumn = 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;
+ }
+
+ if (skip_till_semicolumn)
+ {
+ if (*dbp == ';')
+ skip_till_semicolumn = FALSE;
+ dbp++;
+ continue; /* advance char */
+ }
+
+ /* Search for beginning of a token. */
+ if (!begtoken (*dbp))
+ {
+ dbp++;
+ continue; /* advance char */
+ }
+
+ /* We are at the beginning of a token. */
+ switch (lowcase(*dbp))
+ {
+ case 'f':
+ if (!packages_only && nocase_tail ("function"))
+ Ada_getit (inf, "/f");
+ else
+ break; /* from switch */
+ continue; /* advance char */
+ case 'p':
+ if (!packages_only && nocase_tail ("procedure"))
+ Ada_getit (inf, "/p");
+ else if (nocase_tail ("package"))
+ Ada_getit (inf, "/s");
+ else if (nocase_tail ("protected")) /* protected type */
+ Ada_getit (inf, "/t");
+ else
+ break; /* from switch */
+ continue; /* advance char */
+
+ case 'u':
+ if (typedefs && !packages_only && nocase_tail ("use"))
+ {
+ /* when tagging types, avoid tagging use type Pack.Typename;
+ for this, we will skip everything till a ; */
+ skip_till_semicolumn = TRUE;
+ continue; /* advance char */
+ }
+
+ case 't':
+ if (!packages_only && nocase_tail ("task"))
+ Ada_getit (inf, "/k");
+ else if (typedefs && !packages_only && nocase_tail ("type"))
+ {
+ Ada_getit (inf, "/t");
+ while (*dbp != '\0')
+ dbp += 1;
+ }
+ else
+ break; /* from switch */
+ continue; /* advance char */
+ }
+
+ /* Look for the end of the token. */
+ while (!endtoken (*dbp))
+ dbp++;
+
+ } /* advance char */
+ } /* advance line */
+}
+
+\f
+/*
+ * Unix and microcontroller assembly tag handling
+ * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
+ * Idea by Bob Weiner, Motorola Inc. (1994)
+ */
+static void
+Asm_labels (inf)
+ FILE *inf;
+{
+ register char *cp;
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
+ {
/* If first char is alphabetic or one of [_.$], test for colon
following identifier. */
- if (isalpha (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
+ 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))
- {
- /* Found end of label, so copy it and add it to the table. */
- pfnote (savenstr(lb.buffer, cp-lb.buffer), TRUE,
+ if (*cp == ':' || iswhite (*cp))
+ /* Found end of label, so copy it and add it to the table. */
+ make_tag (lb.buffer, cp - lb.buffer, TRUE,
lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
- }
}
}
}
+
\f
/*
- * Perl support by Bart Robinson <lomew@cs.utah.edu>
- * Perl sub names: look for /^sub[ \t\n]+[^ \t\n{]+/
+ * Perl support
+ * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
+ * Perl variable names: /^(my|local).../
+ * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
+ * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
+ * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
*/
-void
+static void
Perl_functions (inf)
FILE *inf;
{
+ char *package = savestr ("main"); /* current package name */
register char *cp;
- lineno = 0;
- charno = 0;
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
+ {
+ skip_spaces(cp);
- while (!feof (inf))
+ if (LOOKING_AT (cp, "package"))
+ {
+ free (package);
+ get_tag (cp, &package);
+ }
+ else if (LOOKING_AT (cp, "sub"))
+ {
+ char *pos;
+ char *sp = cp;
+
+ while (!notinname (*cp))
+ cp++;
+ if (cp == sp)
+ continue; /* nothing found */
+ if ((pos = etags_strchr (sp, ':')) != NULL
+ && pos < cp && pos[1] == ':')
+ /* The name is already qualified. */
+ make_tag (sp, cp - sp, TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ else
+ /* Qualify it. */
+ {
+ char savechar, *name;
+
+ savechar = *cp;
+ *cp = '\0';
+ name = concat (package, "::", sp);
+ *cp = savechar;
+ make_tag (name, strlen(name), TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ free (name);
+ }
+ }
+ else if (globals) /* only if we are tagging global vars */
+ {
+ /* Skip a qualifier, if any. */
+ bool qual = LOOKING_AT (cp, "my") || LOOKING_AT (cp, "local");
+ /* After "my" or "local", but before any following paren or space. */
+ char *varstart = cp;
+
+ if (qual /* should this be removed? If yes, how? */
+ && (*cp == '$' || *cp == '@' || *cp == '%'))
+ {
+ varstart += 1;
+ do
+ cp++;
+ while (ISALNUM (*cp) || *cp == '_');
+ }
+ else if (qual)
+ {
+ /* Should be examining a variable list at this point;
+ could insist on seeing an open parenthesis. */
+ while (*cp != '\0' && *cp != ';' && *cp != '=' && *cp != ')')
+ cp++;
+ }
+ else
+ continue;
+
+ make_tag (varstart, cp - varstart, FALSE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ }
+ }
+}
+
+
+/*
+ * Python support
+ * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
+ * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
+ * More ideas by seb bacon <seb@jamkit.com> (2002)
+ */
+static void
+Python_functions (inf)
+ FILE *inf;
+{
+ register char *cp;
+
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
- cp = lb.buffer;
+ cp = skip_spaces (cp);
+ if (LOOKING_AT (cp, "def") || LOOKING_AT (cp, "class"))
+ {
+ char *name = cp;
+ while (!notinname (*cp) && *cp != ':')
+ cp++;
+ make_tag (name, cp - name, TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ }
+ }
+}
- if (*cp++ == 's' && *cp++ == 'u' && *cp++ == 'b' && isspace (*cp++))
+\f
+/*
+ * PHP support
+ * Look for:
+ * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
+ * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
+ * - /^[ \t]*define\(\"[^\"]+/
+ * Only with --members:
+ * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
+ * Idea by Diez B. Roggisch (2001)
+ */
+static void
+PHP_functions (inf)
+ FILE *inf;
+{
+ register char *cp, *name;
+ bool search_identifier = FALSE;
+
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
+ {
+ cp = skip_spaces (cp);
+ name = cp;
+ if (search_identifier
+ && *cp != '\0')
+ {
+ while (!notinname (*cp))
+ cp++;
+ make_tag (name, cp - name, TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ search_identifier = FALSE;
+ }
+ else if (LOOKING_AT (cp, "function"))
+ {
+ if(*cp == '&')
+ cp = skip_spaces (cp+1);
+ if(*cp != '\0')
+ {
+ name = cp;
+ while (!notinname (*cp))
+ cp++;
+ make_tag (name, cp - name, TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ }
+ else
+ search_identifier = TRUE;
+ }
+ else if (LOOKING_AT (cp, "class"))
+ {
+ if (*cp != '\0')
+ {
+ name = cp;
+ while (*cp != '\0' && !iswhite (*cp))
+ cp++;
+ make_tag (name, cp - name, FALSE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ }
+ else
+ search_identifier = TRUE;
+ }
+ else if (strneq (cp, "define", 6)
+ && (cp = skip_spaces (cp+6))
+ && *cp++ == '('
+ && (*cp == '"' || *cp == '\''))
{
- while (*cp && isspace (*cp))
+ char quote = *cp++;
+ name = cp;
+ while (*cp != quote && *cp != '\0')
cp++;
- while (*cp && ! isspace (*cp) && *cp != '{')
+ make_tag (name, cp - name, FALSE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ }
+ else if (members
+ && LOOKING_AT (cp, "var")
+ && *cp == '$')
+ {
+ name = cp;
+ while (!notinname(*cp))
cp++;
- pfnote (savenstr (lb.buffer, cp-lb.buffer), TRUE,
- lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ make_tag (name, cp - name, FALSE,
+ 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.
+ * Idea by Corny de Souza (1993)
*/
-void
+static void
Cobol_paragraphs (inf)
FILE *inf;
{
- register char *cp;
-
- lineno = 0;
- charno = 0;
+ register char *bp, *ep;
- while (!feof (inf))
+ LOOP_ON_INPUT_LINES (inf, lb, bp)
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
-
if (lb.len < 9)
continue;
- dbp = lb.buffer + 8;
+ bp += 8;
/* If eoln, compiler option or comment ignore whole line. */
- if (dbp[-1] != ' ' || !isalnum (dbp[0]))
+ if (bp[-1] != ' ' || !ISALNUM (bp[0]))
continue;
- for (cp = dbp; isalnum (*cp) || *cp == '-'; cp++)
+ for (ep = bp; ISALNUM (*ep) || *ep == '-'; ep++)
continue;
- if (*cp++ == '.')
- pfnote (savenstr (dbp, cp-dbp), TRUE,
- lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ if (*ep++ == '.')
+ make_tag (bp, ep - bp, TRUE,
+ lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
}
}
+
\f
-/* Added by Mosur Mohan, 4/22/88 */
-/* Pascal parsing */
+/*
+ * Makefile support
+ * Ideas 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 == ':' || (globals && *bp == '='))
+ make_tag (lb.buffer, bp - lb.buffer, TRUE,
+ lb.buffer, bp - lb.buffer + 1, lineno, linecharno);
+ }
+}
+\f
/*
+ * Pascal parsing
+ * Original code by Mosur K. Mohan (1989)
+ *
* Locates tags for procedures & functions. Doesn't do any type- or
* var-definitions. It does look for the keyword "extern" or
* "forward" immediately following the procedure statement; if found,
* the tag is skipped.
*/
-void
+static void
Pascal_functions (inf)
FILE *inf;
{
- struct linebuffer tline; /* mostly copied from C_entries */
+ linebuffer tline; /* mostly copied from C_entries */
long save_lcno;
- int save_lineno, save_len;
- char c, *cp, *namebuf;
+ int save_lineno, namelen, taglen;
+ char c, *name;
bool /* each of these flags is TRUE iff: */
incomment, /* point is inside a comment */
is a FORWARD/EXTERN to be ignored, or
whether it is a real tag */
- lineno = 0;
- charno = 0;
+ save_lcno = save_lineno = namelen = taglen = 0; /* keep compiler quiet */
+ name = NULL; /* keep compiler quiet */
dbp = lb.buffer;
*dbp = '\0';
- save_len = 0;
- initbuffer (&tline);
+ linebuffer_init (&tline);
incomment = inquote = FALSE;
found_tag = FALSE; /* have a proc name; check if extern */
- get_tagname = FALSE; /* have found "procedure" keyword */
+ get_tagname = FALSE; /* found "procedure" keyword */
inparms = FALSE; /* found '(' after "proc" */
verify_tag = FALSE; /* check if "extern" is ahead */
- /* long main loop to get next char */
- while (!feof (inf))
+
+ while (!feof (inf)) /* long main loop to get next char */
{
c = *dbp++;
if (c == '\0') /* if end of line */
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
+ readline (&lb, inf);
dbp = lb.buffer;
if (*dbp == '\0')
continue;
- if (!((found_tag && verify_tag) ||
- get_tagname))
+ if (!((found_tag && verify_tag)
+ || get_tagname))
c = *dbp++; /* only if don't need *dbp pointing
to the beginning of the name of
the procedure or function */
}
if (found_tag && verify_tag && (*dbp != ' '))
{
- /* check if this is an "extern" declaration */
+ /* Check if this is an "extern" declaration. */
if (*dbp == '\0')
continue;
if (lowcase (*dbp == 'e'))
{
- if (tail ("extern")) /* superfluous, really! */
+ if (nocase_tail ("extern")) /* superfluous, really! */
{
found_tag = FALSE;
verify_tag = FALSE;
}
else if (lowcase (*dbp) == 'f')
{
- if (tail ("forward")) /* check for forward reference */
+ if (nocase_tail ("forward")) /* check for forward reference */
{
found_tag = FALSE;
verify_tag = FALSE;
{
found_tag = FALSE;
verify_tag = FALSE;
- pfnote (namebuf, TRUE,
- tline.buffer, save_len, save_lineno, save_lcno);
+ make_tag (name, namelen, TRUE,
+ tline.buffer, taglen, save_lineno, save_lcno);
continue;
}
}
if (get_tagname) /* grab name of proc or fn */
{
+ char *cp;
+
if (*dbp == '\0')
continue;
- /* save all values for later tagging */
- grow_linebuffer (&tline, lb.len + 1);
+ /* Find block name. */
+ for (cp = dbp + 1; *cp != '\0' && !endtoken (*cp); cp++)
+ continue;
+
+ /* Save all values for later tagging. */
+ linebuffer_setlen (&tline, lb.len);
strcpy (tline.buffer, lb.buffer);
save_lineno = lineno;
save_lcno = linecharno;
+ name = tline.buffer + (dbp - lb.buffer);
+ namelen = cp - dbp;
+ taglen = cp - lb.buffer + 1;
- /* grab block name */
- for (cp = dbp + 1; *cp != '\0' && !endtoken (*cp); cp++)
- continue;
- namebuf = savenstr (dbp, cp-dbp);
dbp = cp; /* set dbp to e-o-token */
- save_len = dbp - lb.buffer + 1;
get_tagname = FALSE;
found_tag = TRUE;
continue;
- /* and proceed to check for "extern" */
+ /* And proceed to check for "extern". */
}
else if (!incomment && !inquote && !found_tag)
{
- /* check for proc/fn keywords */
+ /* Check for proc/fn keywords. */
switch (lowcase (c))
{
case 'p':
- if (tail ("rocedure")) /* c = 'p', dbp has advanced */
+ if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
get_tagname = TRUE;
continue;
case 'f':
- if (tail ("unction"))
+ if (nocase_tail ("unction"))
get_tagname = TRUE;
continue;
}
}
- } /* while not eof */
+ } /* while not eof */
free (tline.buffer);
}
+
\f
/*
- * lisp tag functions
+ * Lisp tag functions
* look for (def or (DEF, quote or QUOTE
*/
-int
-L_isdef (strp)
- register char *strp;
-{
- return ((strp[1] == 'd' || strp[1] == 'D')
- && (strp[2] == 'e' || strp[2] == 'E')
- && (strp[3] == 'f' || strp[3] == 'F'));
-}
-int
-L_isquote (strp)
- register char *strp;
-{
- return ((*(++strp) == 'q' || *strp == 'Q')
- && (*(++strp) == 'u' || *strp == 'U')
- && (*(++strp) == 'o' || *strp == 'O')
- && (*(++strp) == 't' || *strp == 'T')
- && (*(++strp) == 'e' || *strp == 'E')
- && isspace (*(++strp)));
-}
+static void L_getit __P((void));
-void
+static void
L_getit ()
{
- register char *cp;
-
if (*dbp == '\'') /* Skip prefix quote */
dbp++;
- else if (*dbp == '(' && L_isquote (dbp)) /* Skip "(quote " */
+ else if (*dbp == '(')
{
- dbp += 7;
- while (isspace (*dbp))
- dbp++;
+ dbp++;
+ /* Try to skip "(quote " */
+ if (!LOOKING_AT (dbp, "quote") && !LOOKING_AT (dbp, "QUOTE"))
+ /* Ok, then skip "(" before name in (defstruct (foo)) */
+ dbp = skip_spaces (dbp);
}
- for (cp = dbp /*+1*/;
- *cp != '\0' && *cp != '(' && *cp != ' ' && *cp != ')';
- cp++)
- continue;
- if (cp == dbp)
- return;
-
- pfnote (savenstr (dbp, cp-dbp), TRUE,
- lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ get_tag (dbp, NULL);
}
-void
+static void
Lisp_functions (inf)
FILE *inf;
{
- lineno = 0;
- charno = 0;
-
- while (!feof (inf))
+ LOOP_ON_INPUT_LINES (inf, lb, dbp)
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
- dbp = lb.buffer;
- if (dbp[0] == '(')
+ if (dbp[0] != '(')
+ continue;
+
+ if (strneq (dbp+1, "def", 3) || strneq (dbp+1, "DEF", 3))
{
- if (L_isdef (dbp))
- {
- while (!isspace (*dbp))
- dbp++;
- while (isspace (*dbp))
- dbp++;
- L_getit ();
- }
- else
+ dbp = skip_non_spaces (dbp);
+ dbp = skip_spaces (dbp);
+ L_getit ();
+ }
+ else
+ {
+ /* Check for (foo::defmumble name-defined ... */
+ do
+ dbp++;
+ while (!notinname (*dbp) && *dbp != ':');
+ if (*dbp == ':')
{
- /* Check for (foo::defmumble name-defined ... */
do
dbp++;
- while (*dbp && !isspace (*dbp)
- && *dbp != ':' && *dbp != '(' && *dbp != ')');
- if (*dbp == ':')
- {
- do
- dbp++;
- while (*dbp == ':');
+ while (*dbp == ':');
- if (L_isdef (dbp - 1))
- {
- while (!isspace (*dbp))
- dbp++;
- while (isspace (*dbp))
- dbp++;
- L_getit ();
- }
+ if (strneq (dbp, "def", 3) || strneq (dbp, "DEF", 3))
+ {
+ dbp = skip_non_spaces (dbp);
+ dbp = skip_spaces (dbp);
+ L_getit ();
}
}
}
}
}
+
+\f
+/*
+ * Lua script language parsing
+ * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
+ *
+ * "function" and "local function" are tags if they start at column 1.
+ */
+static void
+Lua_functions (inf)
+ FILE *inf;
+{
+ register char *bp;
+
+ LOOP_ON_INPUT_LINES (inf, lb, bp)
+ {
+ if (bp[0] != 'f' && bp[0] != 'l')
+ continue;
+
+ LOOKING_AT (bp, "local"); /* skip possible "local" */
+
+ if (LOOKING_AT (bp, "function"))
+ get_tag (bp, NULL);
+ }
+}
+
\f
/*
- * Postscript tag functions
+ * Postscript tags
* Just look for lines where the first character is '/'
- * Richard Mlynarik <mly@adoc.xerox.com>
+ * Also look at "defineps" for PSWrap
+ * Ideas by:
+ * Richard Mlynarik <mly@adoc.xerox.com> (1997)
+ * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
*/
-void
-Postscript_functions (inf)
+static void
+PS_functions (inf)
FILE *inf;
{
- lineno = 0;
- charno = 0;
+ register char *bp, *ep;
- while (!feof (inf))
+ LOOP_ON_INPUT_LINES (inf, lb, bp)
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
- dbp = lb.buffer;
- if (dbp[0] == '/')
+ if (bp[0] == '/')
{
- register char *cp;
- for (cp = dbp+1;
- *cp != '\0' && *cp != ' ' && *cp != '{';
- cp++)
+ for (ep = bp+1;
+ *ep != '\0' && *ep != ' ' && *ep != '{';
+ ep++)
continue;
- pfnote (savenstr (dbp, cp-dbp), TRUE,
- lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ make_tag (bp, ep - bp, TRUE,
+ lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
}
+ else if (LOOKING_AT (bp, "defineps"))
+ get_tag (bp, NULL);
}
}
+\f
+/*
+ * Forth tags
+ * Ignore anything after \ followed by space or in ( )
+ * Look for words defined by :
+ * Look for constant, code, create, defer, value, and variable
+ * OBP extensions: Look for buffer:, field,
+ * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004)
+ */
+static void
+Forth_words (inf)
+ FILE *inf;
+{
+ register char *bp;
+
+ LOOP_ON_INPUT_LINES (inf, lb, bp)
+ while ((bp = skip_spaces (bp))[0] != '\0')
+ if (bp[0] == '\\' && iswhite(bp[1]))
+ break; /* read next line */
+ else if (bp[0] == '(' && iswhite(bp[1]))
+ do /* skip to ) or eol */
+ bp++;
+ while (*bp != ')' && *bp != '\0');
+ else if ((bp[0] == ':' && iswhite(bp[1]) && bp++)
+ || LOOKING_AT_NOCASE (bp, "constant")
+ || LOOKING_AT_NOCASE (bp, "code")
+ || LOOKING_AT_NOCASE (bp, "create")
+ || LOOKING_AT_NOCASE (bp, "defer")
+ || LOOKING_AT_NOCASE (bp, "value")
+ || LOOKING_AT_NOCASE (bp, "variable")
+ || LOOKING_AT_NOCASE (bp, "buffer:")
+ || LOOKING_AT_NOCASE (bp, "field"))
+ get_tag (skip_spaces (bp), NULL); /* Yay! A definition! */
+ else
+ bp = skip_non_spaces (bp);
+}
+
\f
/*
* Scheme tag functions
* look for (def... xyzzy
- * look for (def... (xyzzy
- * look for (def ... ((...(xyzzy ....
- * look for (set! xyzzy
+ * (def... (xyzzy
+ * (def ... ((...(xyzzy ....
+ * (set! xyzzy
+ * Original code by Ken Haase (1985?)
*/
-
-void get_scheme ();
-
-void
+static void
Scheme_functions (inf)
FILE *inf;
{
- lineno = 0;
- charno = 0;
+ register char *bp;
- while (!feof (inf))
+ LOOP_ON_INPUT_LINES (inf, lb, bp)
{
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
- dbp = lb.buffer;
- if (dbp[0] == '(' &&
- (dbp[1] == 'D' || dbp[1] == 'd') &&
- (dbp[2] == 'E' || dbp[2] == 'e') &&
- (dbp[3] == 'F' || dbp[3] == 'f'))
+ if (strneq (bp, "(def", 4) || strneq (bp, "(DEF", 4))
{
- while (!isspace (*dbp))
- dbp++;
+ bp = skip_non_spaces (bp+4);
/* Skip over open parens and white space */
- while (*dbp && (isspace (*dbp) || *dbp == '('))
- dbp++;
- get_scheme ();
- }
- if (dbp[0] == '(' &&
- (dbp[1] == 'S' || dbp[1] == 's') &&
- (dbp[2] == 'E' || dbp[2] == 'e') &&
- (dbp[3] == 'T' || dbp[3] == 't') &&
- (dbp[4] == '!' || dbp[4] == '!') &&
- (isspace (dbp[5])))
- {
- while (!isspace (*dbp))
- dbp++;
- /* Skip over white space */
- while (isspace (*dbp))
- dbp++;
- get_scheme ();
+ while (notinname (*bp))
+ bp++;
+ get_tag (bp, NULL);
}
+ if (LOOKING_AT (bp, "(SET!") || LOOKING_AT (bp, "(set!"))
+ get_tag (bp, NULL);
}
}
-void
-get_scheme ()
-{
- register char *cp;
-
- if (*dbp == '\0')
- return;
- /* Go till you get to white space or a syntactic break */
- for (cp = dbp + 1;
- *cp && *cp != '(' && *cp != ')' && !isspace (*cp);
- cp++)
- continue;
- pfnote (savenstr (dbp, cp-dbp), TRUE,
- lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
-}
\f
/* Find tags in TeX and LaTeX input files. */
/* TEX_toktab is a table of TeX control sequences that define tags.
- Each TEX_tabent records one such control sequence.
- CONVERT THIS TO USE THE Stab TYPE!! */
-struct TEX_tabent
-{
- char *name;
- int len;
-};
+ * Each entry records one such control sequence.
+ *
+ * Original code from who knows whom.
+ * Ideas by:
+ * Stefan Monnier (2002)
+ */
-struct TEX_tabent *TEX_toktab = NULL; /* Table with tag tokens */
+static linebuffer *TEX_toktab = NULL; /* Table with tag tokens */
/* Default set of control sequences to put into TEX_toktab.
The value of environment var TEXTAGS is prepended to this. */
-
-char *TEX_defenv = "\
+static char *TEX_defenv = "\
:chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
-:part:appendix:entry:index";
+:part:appendix:entry:index:def\
+:newcommand:renewcommand:newenvironment:renewenvironment";
-void TEX_mode ();
-struct TEX_tabent *TEX_decode_env ();
-int TEX_Token ();
+static void TEX_mode __P((FILE *));
+static void TEX_decode_env __P((char *, char *));
-char TEX_esc = '\\';
-char TEX_opgrp = '{';
-char TEX_clgrp = '}';
+static char TEX_esc = '\\';
+static char TEX_opgrp = '{';
+static char TEX_clgrp = '}';
/*
* TeX/LaTeX scanning loop.
*/
-void
-TeX_functions (inf)
+static void
+TeX_commands (inf)
FILE *inf;
{
- char *lasthit;
- register int i;
-
- lineno = 0;
- charno = 0;
+ char *cp;
+ linebuffer *key;
/* Select either \ or ! as escape character. */
TEX_mode (inf);
/* Initialize token table once from environment. */
- if (!TEX_toktab)
- TEX_toktab = TEX_decode_env ("TEXTAGS", TEX_defenv);
+ if (TEX_toktab == NULL)
+ TEX_decode_env ("TEXTAGS", TEX_defenv);
- while (!feof (inf))
- { /* Scan each line in file */
- lineno++;
- linecharno = charno;
- charno += readline (&lb, inf);
- dbp = lb.buffer;
- lasthit = dbp;
- while (dbp = etags_strchr (dbp, TEX_esc)) /* Look at each esc in line */
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
+ {
+ /* Look at each TEX keyword in line. */
+ for (;;)
{
- if (!*(++dbp))
- break;
- linecharno += dbp - lasthit;
- lasthit = dbp;
- i = TEX_Token (lasthit);
- if (i >= 0)
- {
- /* We seem to include the TeX command in the tag name.
- register char *p;
- for (p = lasthit + TEX_toktab[i].len;
- *p != '\0' && *p != TEX_clgrp;
- p++)
- continue; */
- pfnote (/*savenstr (lasthit, p-lasthit)*/ (char *)NULL, TRUE,
- lb.buffer, lb.len, lineno, linecharno);
- break; /* We only tag a line once */
- }
+ /* Look for a TEX escape. */
+ while (*cp++ != TEX_esc)
+ if (cp[-1] == '\0' || cp[-1] == '%')
+ goto tex_next_line;
+
+ for (key = TEX_toktab; key->buffer != NULL; key++)
+ if (strneq (cp, key->buffer, key->len))
+ {
+ register char *p;
+ int namelen, linelen;
+ bool opgrp = FALSE;
+
+ cp = skip_spaces (cp + key->len);
+ if (*cp == TEX_opgrp)
+ {
+ opgrp = TRUE;
+ cp++;
+ }
+ for (p = cp;
+ (!iswhite (*p) && *p != '#' &&
+ *p != TEX_opgrp && *p != TEX_clgrp);
+ p++)
+ continue;
+ namelen = p - cp;
+ linelen = lb.len;
+ if (!opgrp || *p == TEX_clgrp)
+ {
+ while (*p != '\0' && *p != TEX_opgrp && *p != TEX_clgrp)
+ *p++;
+ linelen = p - lb.buffer + 1;
+ }
+ make_tag (cp, namelen, TRUE,
+ lb.buffer, linelen, lineno, linecharno);
+ goto tex_next_line; /* We only tag a line once */
+ }
}
+ tex_next_line:
+ ;
}
}
#define TEX_LESC '\\'
#define TEX_SESC '!'
-#define TEX_cmt '%'
/* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
chars accordingly. */
-void
+static void
TEX_mode (inf)
FILE *inf;
{
while ((c = getc (inf)) != EOF)
{
/* Skip to next line if we hit the TeX comment char. */
- if (c == TEX_cmt)
+ if (c == '%')
while (c != '\n')
c = getc (inf);
else if (c == TEX_LESC || c == TEX_SESC )
TEX_opgrp = '<';
TEX_clgrp = '>';
}
+ /* If the input file is compressed, inf is a pipe, and rewind may fail.
+ No attempt is made to correct the situation. */
rewind (inf);
}
/* Read environment and prepend it to the default string.
Build token table. */
-struct TEX_tabent *
+static void
TEX_decode_env (evarname, defenv)
char *evarname;
char *defenv;
{
register char *env, *p;
-
- struct TEX_tabent *tab;
- int size, i;
+ int i, len;
/* Append default string to environment. */
env = getenv (evarname);
}
/* Allocate a token table */
- for (size = 1, p = env; p;)
- if ((p = etags_strchr (p, ':')) && *(++p))
- size++;
- /* Add 1 to leave room for null terminator. */
- tab = xnew (size + 1, struct TEX_tabent);
+ for (len = 1, p = env; p;)
+ if ((p = etags_strchr (p, ':')) && *++p != '\0')
+ len++;
+ TEX_toktab = xnew (len, linebuffer);
/* Unpack environment string into token table. Be careful about */
/* zero-length strings (leading ':', "::" and trailing ':') */
- for (i = 0; *env;)
+ for (i = 0; *env != '\0';)
{
p = etags_strchr (env, ':');
if (!p) /* End of environment string. */
p = env + strlen (env);
if (p - env > 0)
{ /* Only non-zero strings. */
- tab[i].name = savenstr (env, p - env);
- tab[i].len = strlen (tab[i].name);
+ TEX_toktab[i].buffer = savenstr (env, p - env);
+ TEX_toktab[i].len = p - env;
i++;
}
if (*p)
env = p + 1;
else
{
- tab[i].name = NULL; /* Mark end of table. */
- tab[i].len = 0;
+ TEX_toktab[i].buffer = NULL; /* Mark end of table. */
+ TEX_toktab[i].len = 0;
break;
}
}
- return tab;
}
-/* If the text at CP matches one of the tag-defining TeX command names,
- return the pointer to the first occurrence of that command in TEX_toktab.
- Otherwise return -1.
- Keep the capital `T' in `Token' for dumb truncating compilers
- (this distinguishes it from `TEX_toktab' */
-int
-TEX_Token (cp)
- char *cp;
+\f
+/* Texinfo support. Dave Love, Mar. 2000. */
+static void
+Texinfo_nodes (inf)
+ FILE * inf;
{
- int i;
+ char *cp, *start;
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
+ if (LOOKING_AT (cp, "@node"))
+ {
+ start = cp;
+ while (*cp != '\0' && *cp != ',')
+ cp++;
+ make_tag (start, cp - start, TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ }
+}
+
+\f
+/*
+ * HTML support.
+ * Contents of <title>, <h1>, <h2>, <h3> are tags.
+ * Contents of <a name=xxx> are tags with name xxx.
+ *
+ * Francesco Potortì, 2002.
+ */
+static void
+HTML_labels (inf)
+ FILE * inf;
+{
+ bool getnext = FALSE; /* next text outside of HTML tags is a tag */
+ bool skiptag = FALSE; /* skip to the end of the current HTML tag */
+ bool intag = FALSE; /* inside an html tag, looking for ID= */
+ bool inanchor = FALSE; /* when INTAG, is an anchor, look for NAME= */
+ char *end;
+
- for (i = 0; TEX_toktab[i].len > 0; i++)
- if (strneq (TEX_toktab[i].name, cp, TEX_toktab[i].len))
- return i;
- return -1;
+ linebuffer_setlen (&token_name, 0); /* no name in buffer */
+
+ LOOP_ON_INPUT_LINES (inf, lb, dbp)
+ for (;;) /* loop on the same line */
+ {
+ if (skiptag) /* skip HTML tag */
+ {
+ while (*dbp != '\0' && *dbp != '>')
+ dbp++;
+ if (*dbp == '>')
+ {
+ dbp += 1;
+ skiptag = FALSE;
+ continue; /* look on the same line */
+ }
+ break; /* go to next line */
+ }
+
+ else if (intag) /* look for "name=" or "id=" */
+ {
+ while (*dbp != '\0' && *dbp != '>'
+ && lowcase (*dbp) != 'n' && lowcase (*dbp) != 'i')
+ dbp++;
+ if (*dbp == '\0')
+ break; /* go to next line */
+ if (*dbp == '>')
+ {
+ dbp += 1;
+ intag = FALSE;
+ continue; /* look on the same line */
+ }
+ if ((inanchor && LOOKING_AT_NOCASE (dbp, "name="))
+ || LOOKING_AT_NOCASE (dbp, "id="))
+ {
+ bool quoted = (dbp[0] == '"');
+
+ if (quoted)
+ for (end = ++dbp; *end != '\0' && *end != '"'; end++)
+ continue;
+ else
+ for (end = dbp; *end != '\0' && intoken (*end); end++)
+ continue;
+ linebuffer_setlen (&token_name, end - dbp);
+ strncpy (token_name.buffer, dbp, end - dbp);
+ token_name.buffer[end - dbp] = '\0';
+
+ dbp = end;
+ intag = FALSE; /* we found what we looked for */
+ skiptag = TRUE; /* skip to the end of the tag */
+ getnext = TRUE; /* then grab the text */
+ continue; /* look on the same line */
+ }
+ dbp += 1;
+ }
+
+ else if (getnext) /* grab next tokens and tag them */
+ {
+ dbp = skip_spaces (dbp);
+ if (*dbp == '\0')
+ break; /* go to next line */
+ if (*dbp == '<')
+ {
+ intag = TRUE;
+ inanchor = (lowcase (dbp[1]) == 'a' && !intoken (dbp[2]));
+ continue; /* look on the same line */
+ }
+
+ for (end = dbp + 1; *end != '\0' && *end != '<'; end++)
+ continue;
+ make_tag (token_name.buffer, token_name.len, TRUE,
+ dbp, end - dbp, lineno, linecharno);
+ linebuffer_setlen (&token_name, 0); /* no name in buffer */
+ getnext = FALSE;
+ break; /* go to next line */
+ }
+
+ else /* look for an interesting HTML tag */
+ {
+ while (*dbp != '\0' && *dbp != '<')
+ dbp++;
+ if (*dbp == '\0')
+ break; /* go to next line */
+ intag = TRUE;
+ if (lowcase (dbp[1]) == 'a' && !intoken (dbp[2]))
+ {
+ inanchor = TRUE;
+ continue; /* look on the same line */
+ }
+ else if (LOOKING_AT_NOCASE (dbp, "<title>")
+ || LOOKING_AT_NOCASE (dbp, "<h1>")
+ || LOOKING_AT_NOCASE (dbp, "<h2>")
+ || LOOKING_AT_NOCASE (dbp, "<h3>"))
+ {
+ intag = FALSE;
+ getnext = TRUE;
+ continue; /* look on the same line */
+ }
+ dbp += 1;
+ }
+ }
}
+
\f
/*
- * Prolog support (rewritten) by Anders Lindgren, Mar. 96
+ * Prolog support
*
- * Assumes that the predicate starts at column 0.
- * Only the first clause of a predicate is added.
+ * Assumes that the predicate or rule starts at column 0.
+ * Only the first clause of a predicate or rule is added.
+ * Original code by Sunichirou Sugou (1989)
+ * Rewritten by Anders Lindgren (1996)
*/
-int prolog_pred ();
-void prolog_skip_comment ();
-int prolog_atom ();
-int eat_white ();
+static int prolog_pr __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;
{
- char * last;
+ char *cp, *last;
int len;
int allocated;
len = 0;
last = NULL;
- lineno = 0;
- linecharno = 0;
- charno = 0;
-
- while (!feof (inf))
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
{
- lineno++;
- linecharno += charno;
- charno = readline (&lb, inf);
- dbp = lb.buffer;
- if (dbp[0] == '\0') /* Empty line */
+ if (cp[0] == '\0') /* Empty line */
continue;
- else if (isspace (dbp[0])) /* Not a predicate */
+ else if (iswhite (cp[0])) /* Not a predicate */
continue;
- else if (dbp[0] == '/' && dbp[1] == '*') /* comment. */
+ else if (cp[0] == '/' && cp[1] == '*') /* comment. */
prolog_skip_comment (&lb, inf);
- else if (len = prolog_pred (dbp, last))
+ else if ((len = prolog_pr (cp, last)) > 0)
{
- /* Predicate. Store the function name so that we only
- generate a tag for the first clause. */
+ /* Predicate or rule. Store the function name so that we
+ only generate a tag for the first clause. */
if (last == NULL)
last = xnew(len + 1, char);
else if (len + 1 > allocated)
- last = (char *) xrealloc(last, len + 1);
+ xrnew (last, len + 1, char);
allocated = len + 1;
- strncpy (last, dbp, len);
+ strncpy (last, cp, len);
last[len] = '\0';
}
}
}
-void
+static void
prolog_skip_comment (plb, inf)
- struct linebuffer *plb;
+ linebuffer *plb;
FILE *inf;
{
char *cp;
for (cp = plb->buffer; *cp != '\0'; cp++)
if (cp[0] == '*' && cp[1] == '/')
return;
- lineno++;
- linecharno += readline (plb, inf);
+ readline (plb, inf);
}
while (!feof(inf));
}
/*
- * A predicate definition is added if it matches:
+ * A predicate or rule definition is added if it matches:
* <beginning of line><Prolog Atom><whitespace>(
+ * or <beginning of line><Prolog Atom><whitespace>:-
*
* It is added to the tags database if it doesn't match the
* name of the previous clause header.
*
- * Return the size of the name of the predicate, or 0 if no header
- * was found.
+ * Return the size of the name of the predicate or rule, or 0 if no
+ * header was found.
*/
-int
-prolog_pred (s, last)
+static int
+prolog_pr (s, last)
char *s;
char *last; /* Name of last clause. */
{
return 0;
len = pos;
- pos += eat_white (s, pos);
+ pos = skip_spaces (s + pos) - s;
- if ((s[pos] == '(') || (s[pos] == '.'))
- {
- if (s[pos] == '(')
- pos++;
-
- /* Save only the first clause. */
- if (last == NULL
- || len != strlen (last)
- || !strneq (s, last, len))
+ if ((s[pos] == '.'
+ || (s[pos] == '(' && (pos += 1))
+ || (s[pos] == ':' && s[pos + 1] == '-' && (pos += 2)))
+ && (last == NULL /* save only the first clause */
+ || len != (int)strlen (last)
+ || !strneq (s, last, len)))
{
- pfnote (savenstr (s, len), TRUE,
- s, pos, lineno, linecharno);
+ make_tag (s, len, TRUE, s, pos, lineno, linecharno);
return len;
}
- }
- return 0;
+ else
+ return 0;
}
/*
* - A quoted arbitrary string. Single quotes can escape themselves.
* Backslash quotes everything.
*/
-int
+static int
prolog_atom (s, pos)
char *s;
int 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++;
}
{
pos++;
- while (1)
+ for (;;)
{
if (s[pos] == '\'')
{
return -1;
}
-/* Consume whitespace. Return the number of bytes eaten. */
-int
-eat_white (s, pos)
- char *s;
- int pos;
-{
- int origpos = pos;
-
- origpos = pos;
-
- while (isspace (s[pos]))
- pos++;
-
- return pos - origpos;
-}
\f
-/*
- * Support for Erlang -- Anders Lindgren, Feb 1996.
+/*
+ * Support for Erlang
*
* Generates tags for functions, defines, and records.
- *
* Assumes that Erlang functions start at column 0.
+ * Original code by Anders Lindgren (1996)
*/
-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 *));
-void
+static void
Erlang_functions (inf)
FILE *inf;
{
- char * last;
+ char *cp, *last;
int len;
int allocated;
len = 0;
last = NULL;
- lineno = 0;
- linecharno = 0;
- charno = 0;
-
- while (!feof (inf))
+ LOOP_ON_INPUT_LINES (inf, lb, cp)
{
- lineno++;
- linecharno += charno;
- charno = readline (&lb, inf);
- dbp = lb.buffer;
- if (dbp[0] == '\0') /* Empty line */
+ if (cp[0] == '\0') /* Empty line */
continue;
- else if (isspace (dbp[0])) /* Not function nor attribute */
+ else if (iswhite (cp[0])) /* Not function nor attribute */
continue;
- else if (dbp[0] == '%') /* comment */
+ else if (cp[0] == '%') /* comment */
continue;
- else if (dbp[0] == '"') /* Sometimes, strings start in column one */
+ else if (cp[0] == '"') /* Sometimes, strings start in column one */
continue;
- else if (dbp[0] == '-') /* attribute, e.g. "-define" */
+ else if (cp[0] == '-') /* attribute, e.g. "-define" */
{
- erlang_attribute (dbp);
+ erlang_attribute (cp);
last = NULL;
}
- else if (len = erlang_func (dbp, last))
+ else if ((len = erlang_func (cp, last)) > 0)
{
- /*
+ /*
* Function. Store the function name so that we only
* generates a tag for the first clause.
*/
if (last == NULL)
last = xnew (len + 1, char);
else if (len + 1 > allocated)
- last = (char *) xrealloc (last, len + 1);
+ xrnew (last, len + 1, char);
allocated = len + 1;
- strncpy (last, dbp, len);
+ strncpy (last, cp, len);
last[len] = '\0';
}
}
* Return the size of the name of the function, or 0 if no function
* was found.
*/
-int
+static int
erlang_func (s, last)
char *s;
char *last; /* Name of last clause. */
int pos;
int len;
- pos = erlang_atom (s, 0);
+ pos = erlang_atom (s);
if (pos < 1)
return 0;
len = pos;
- pos += eat_white (s, pos);
+ pos = skip_spaces (s + pos) - s;
/* Save only the first clause. */
if (s[pos++] == '('
&& (last == NULL
- || len != strlen (last)
+ || len != (int)strlen (last)
|| !strneq (s, last, len)))
{
- pfnote (savenstr (s, len), TRUE,
- s, pos, lineno, linecharno);
+ make_tag (s, len, TRUE, s, pos, lineno, linecharno);
return len;
}
/*
- * Handle attributes. Currently, tags are generated for defines
+ * Handle attributes. Currently, tags are generated for defines
* and records.
*
* They are on the form:
* -define(Foo(M, N), M+N).
* -record(graph, {vtab = notable, cyclic = true}).
*/
-void
+static void
erlang_attribute (s)
char *s;
{
- int pos;
- int len;
+ char *cp = s;
- if (strneq (s, "-define", 7) || strneq (s, "-record", 7))
+ if ((LOOKING_AT (cp, "-define") || LOOKING_AT (cp, "-record"))
+ && *cp++ == '(')
{
- pos = 7 + eat_white (s, pos);
- if (s[pos++] == '(')
- {
- pos += eat_white (s, pos);
- if (len = erlang_atom (s, pos))
- pfnote (savenstr (& s[pos], len), TRUE,
- s, pos + len, lineno, linecharno);
- }
+ int len = erlang_atom (skip_spaces (cp));
+ if (len > 0)
+ make_tag (cp, len, TRUE, s, cp + len - s, lineno, linecharno);
}
return;
}
* Consume an Erlang atom (or variable).
* Return the number of bytes consumed, or -1 if there was an error.
*/
-int
-erlang_atom (s, pos)
+static int
+erlang_atom (s)
char *s;
- int pos;
{
- int origpos;
+ int pos = 0;
- 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] == '_')
+ do
pos++;
- return pos - origpos;
+ while (ISALNUM (s[pos]) || s[pos] == '_');
}
else if (s[pos] == '\'')
{
+ for (pos++; s[pos] != '\''; pos++)
+ if (s[pos] == '\0' /* multiline quoted atoms are ignored */
+ || (s[pos] == '\\' && s[++pos] == '\0'))
+ return 0;
pos++;
-
- while (1)
- {
- if (s[pos] == '\'')
- {
- pos++;
- break;
- }
- else if (s[pos] == '\0')
- /* Multiline quoted atoms are ignored. */
- return -1;
- else if (s[pos] == '\\')
- {
- if (s[pos+1] == '\0')
- return -1;
- pos += 2;
- }
- else
- pos++;
- }
- return pos - origpos;
}
- else
- return -1;
+
+ return pos;
}
+
\f
#ifdef ETAGS_REGEXPS
-/* Take a string like "/blah/" and turn it into "blah", making sure
- that the first and last characters are the same, and handling
- quoted separator characters. Actually, stops on the 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 __P((char *));
+static void add_regex __P((char *, language *));
+static char *substitute __P((char *, char *, struct re_registers *));
+
+/*
+ * Take a string like "/blah/" and turn it into "blah", verifying
+ * that the first and last characters are the same, and handling
+ * quoted separator characters. Actually, stops on the occurrence of
+ * an unquoted separator. Also process \t, \n, etc. and turn into
+ * appropriate characters. Works in place. Null terminates name string.
+ * Returns pointer to terminating separator, or NULL for
+ * unterminated regexps.
+ */
+static char *
scan_separators (name)
char *name;
{
{
if (quoted)
{
- if (*name == 't')
- *copyto++ = '\t';
- else if (*name == sep)
- *copyto++ = sep;
- else
+ switch (*name)
{
- /* Something else is quoted, so preserve the quote. */
- *copyto++ = '\\';
- *copyto++ = *name;
+ case 'a': *copyto++ = '\007'; break; /* BEL (bell) */
+ case 'b': *copyto++ = '\b'; break; /* BS (back space) */
+ case 'd': *copyto++ = 0177; break; /* DEL (delete) */
+ case 'e': *copyto++ = 033; break; /* ESC (delete) */
+ case 'f': *copyto++ = '\f'; break; /* FF (form feed) */
+ case 'n': *copyto++ = '\n'; break; /* NL (new line) */
+ case 'r': *copyto++ = '\r'; break; /* CR (carriage return) */
+ case 't': *copyto++ = '\t'; break; /* TAB (horizontal tab) */
+ case 'v': *copyto++ = '\v'; break; /* VT (vertical tab) */
+ default:
+ if (*name == sep)
+ *copyto++ = sep;
+ else
+ {
+ /* Something else is quoted, so preserve the quote. */
+ *copyto++ = '\\';
+ *copyto++ = *name;
+ }
+ break;
}
quoted = FALSE;
}
else
*copyto++ = *name;
}
+ if (*name != sep)
+ name = NULL; /* signal unterminated regexp */
/* Terminate copied string. */
*copyto = '\0';
}
/* Look at the argument of --regex or --no-regex and do the right
- thing. */
-void
+ thing. Same for each line of a regexp file. */
+static void
analyse_regex (regex_arg)
char *regex_arg;
{
- struct stat stat_buf;
-
if (regex_arg == NULL)
{
- /* Remove existing regexps. */
- num_patterns = 0;
- patterns = NULL;
- return;
- }
- if (regex_arg[0] == '\0')
- {
- error ("missing regexp", (char *)NULL);
+ free_regexps (); /* --no-regex: remove existing regexps */
return;
}
- if (regex_arg[0] == '@'
- && stat (regex_arg + 1, &stat_buf) == 0)
+
+ /* A real --regexp option or a line in a regexp file. */
+ switch (regex_arg[0])
{
- FILE *regexfp;
- struct linebuffer regexbuf;
- char *regexfile = regex_arg + 1;
+ /* Comments in regexp file or null arg to --regex. */
+ case '\0':
+ case ' ':
+ case '\t':
+ break;
+
+ /* Read a regex file. This is recursive and may result in a
+ loop, which will stop when the file descriptors are exhausted. */
+ case '@':
+ {
+ FILE *regexfp;
+ linebuffer regexbuf;
+ char *regexfile = regex_arg + 1;
+
+ /* regexfile is a file containing regexps, one per line. */
+ regexfp = fopen (regexfile, "r");
+ if (regexfp == NULL)
+ {
+ pfatal (regexfile);
+ return;
+ }
+ linebuffer_init (®exbuf);
+ while (readline_internal (®exbuf, regexfp) > 0)
+ analyse_regex (regexbuf.buffer);
+ free (regexbuf.buffer);
+ fclose (regexfp);
+ }
+ break;
- /* regexfile is a file containing regexps, one per line. */
- regexfp = fopen (regexfile, "r");
- if (regexfp == NULL)
- {
- perror (regexfile);
+ /* Regexp to be used for a specific language only. */
+ case '{':
+ {
+ language *lang;
+ char *lang_name = regex_arg + 1;
+ char *cp;
+
+ for (cp = lang_name; *cp != '}'; cp++)
+ if (*cp == '\0')
+ {
+ error ("unterminated language name in regex: %s", regex_arg);
+ return;
+ }
+ *cp++ = '\0';
+ lang = get_language_from_langname (lang_name);
+ if (lang == NULL)
return;
- }
- initbuffer (®exbuf);
- while (readline_internal (®exbuf, regexfp))
- add_regex (regexbuf.buffer);
- free (regexbuf.buffer);
- fclose (regexfp);
- }
- else
- {
- add_regex (regex_arg);
+ add_regex (cp, lang);
+ }
+ break;
+
+ /* Regexp to be used for any language. */
+ default:
+ add_regex (regex_arg, 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)
+/* Separate the regexp pattern, compile it,
+ and care for optional name and modifiers. */
+static void
+add_regex (regexp_pattern, lang)
char *regexp_pattern;
+ language *lang;
{
- char *name;
+ static struct re_pattern_buffer zeropattern;
+ char sep, *pat, *name, *modifiers;
const char *err;
struct re_pattern_buffer *patbuf;
+ regexp *rp;
+ bool
+ force_explicit_name = TRUE, /* do not use implicit tag names */
+ ignore_case = FALSE, /* case is significant */
+ multi_line = FALSE, /* matches are done one line at a time */
+ single_line = FALSE; /* dot does not match newline */
- if (regexp_pattern[strlen(regexp_pattern)-1] != regexp_pattern[0])
+ if (strlen(regexp_pattern) < 3)
{
- error ("%s: unterminated regexp", regexp_pattern);
+ error ("null regexp", (char *)NULL);
return;
}
+ sep = regexp_pattern[0];
name = scan_separators (regexp_pattern);
- if (regexp_pattern[0] == '\0')
+ if (name == NULL)
{
- error ("null regexp", (char *)NULL);
+ error ("%s: unterminated regexp", regexp_pattern);
+ return;
+ }
+ if (name[1] == sep)
+ {
+ error ("null name for regexp \"%s\"", regexp_pattern);
return;
}
- (void) scan_separators (name);
+ modifiers = scan_separators (name);
+ if (modifiers == NULL) /* no terminating separator --> no name */
+ {
+ modifiers = name;
+ name = "";
+ }
+ else
+ modifiers += 1; /* skip separator */
+
+ /* Parse regex modifiers. */
+ for (; modifiers[0] != '\0'; modifiers++)
+ switch (modifiers[0])
+ {
+ case 'N':
+ if (modifiers == name)
+ error ("forcing explicit tag name but no name, ignoring", NULL);
+ force_explicit_name = TRUE;
+ break;
+ case 'i':
+ ignore_case = TRUE;
+ break;
+ case 's':
+ single_line = TRUE;
+ /* FALLTHRU */
+ case 'm':
+ multi_line = TRUE;
+ need_filebuf = TRUE;
+ break;
+ default:
+ {
+ char wrongmod [2];
+ wrongmod[0] = modifiers[0];
+ wrongmod[1] = '\0';
+ error ("invalid regexp modifier `%s', ignoring", wrongmod);
+ }
+ break;
+ }
patbuf = xnew (1, struct re_pattern_buffer);
- patbuf->translate = NULL;
- patbuf->fastmap = NULL;
- patbuf->buffer = NULL;
- patbuf->allocated = 0;
+ *patbuf = zeropattern;
+ if (ignore_case)
+ {
+ static char lc_trans[CHARS];
+ int i;
+ for (i = 0; i < CHARS; i++)
+ lc_trans[i] = lowcase (i);
+ patbuf->translate = lc_trans; /* translation table to fold case */
+ }
+
+ if (multi_line)
+ pat = concat ("^", regexp_pattern, ""); /* anchor to beginning of line */
+ else
+ pat = regexp_pattern;
+
+ if (single_line)
+ re_set_syntax (RE_SYNTAX_EMACS | RE_DOT_NEWLINE);
+ else
+ re_set_syntax (RE_SYNTAX_EMACS);
- re_syntax_options = RE_INTERVALS;
- err = re_compile_pattern (regexp_pattern, strlen (regexp_pattern), patbuf);
+ err = re_compile_pattern (pat, strlen (regexp_pattern), patbuf);
+ if (multi_line)
+ free (pat);
if (err != NULL)
{
error ("%s while compiling pattern", err);
return;
}
- num_patterns += 1;
- if (num_patterns == 1)
- patterns = xnew (1, struct pattern);
- else
- patterns = ((struct pattern *)
- xrealloc (patterns,
- (num_patterns * sizeof (struct pattern))));
- patterns[num_patterns - 1].pattern = patbuf;
- patterns[num_patterns - 1].name_pattern = savestr (name);
- patterns[num_patterns - 1].error_signaled = FALSE;
+ rp = p_head;
+ p_head = xnew (1, regexp);
+ p_head->pattern = savestr (regexp_pattern);
+ p_head->p_next = rp;
+ p_head->lang = lang;
+ p_head->pat = patbuf;
+ p_head->name = savestr (name);
+ p_head->error_signaled = FALSE;
+ p_head->force_explicit_name = force_explicit_name;
+ p_head->ignore_case = ignore_case;
+ p_head->multi_line = multi_line;
}
/*
* Do the substitutions indicated by the regular expression and
* arguments.
*/
-char *
+static char *
substitute (in, out, regs)
char *in, *out;
struct re_registers *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];
size -= 1;
/* Allocate space and do the substitutions. */
+ assert (size >= 0);
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);
*t++ = *out;
*t = '\0';
- if (DEBUG && (t > result + size || t - result != strlen (result)))
- abort ();
+ assert (t <= result + size);
+ assert (t - result == (int)strlen (result));
return result;
}
-\f
+
+/* Deallocate all regexps. */
+static void
+free_regexps ()
+{
+ regexp *rp;
+ while (p_head != NULL)
+ {
+ rp = p_head->p_next;
+ free (p_head->pattern);
+ free (p_head->name);
+ free (p_head);
+ p_head = rp;
+ }
+ return;
+}
+
+/*
+ * Reads the whole file as a single string from `filebuf' and looks for
+ * multi-line regular expressions, creating tags on matches.
+ * readline already dealt with normal regexps.
+ *
+ * Idea by Ben Wing <ben@666.com> (2002).
+ */
+static void
+regex_tag_multiline ()
+{
+ char *buffer = filebuf.buffer;
+ regexp *rp;
+ char *name;
+
+ for (rp = p_head; rp != NULL; rp = rp->p_next)
+ {
+ int match = 0;
+
+ if (!rp->multi_line)
+ continue; /* skip normal regexps */
+
+ /* Generic initialisations before parsing file from memory. */
+ lineno = 1; /* reset global line number */
+ charno = 0; /* reset global char number */
+ linecharno = 0; /* reset global char number of line start */
+
+ /* Only use generic regexps or those for the current language. */
+ if (rp->lang != NULL && rp->lang != curfdp->lang)
+ continue;
+
+ while (match >= 0 && match < filebuf.len)
+ {
+ match = re_search (rp->pat, buffer, filebuf.len, charno,
+ filebuf.len - match, &rp->regs);
+ switch (match)
+ {
+ case -2:
+ /* Some error. */
+ if (!rp->error_signaled)
+ {
+ error ("regexp stack overflow while matching \"%s\"",
+ rp->pattern);
+ rp->error_signaled = TRUE;
+ }
+ break;
+ case -1:
+ /* No match. */
+ break;
+ default:
+ if (match == rp->regs.end[0])
+ {
+ if (!rp->error_signaled)
+ {
+ error ("regexp matches the empty string: \"%s\"",
+ rp->pattern);
+ rp->error_signaled = TRUE;
+ }
+ match = -3; /* exit from while loop */
+ break;
+ }
+
+ /* Match occurred. Construct a tag. */
+ while (charno < rp->regs.end[0])
+ if (buffer[charno++] == '\n')
+ lineno++, linecharno = charno;
+ name = rp->name;
+ if (name[0] == '\0')
+ name = NULL;
+ else /* make a named tag */
+ name = substitute (buffer, rp->name, &rp->regs);
+ if (rp->force_explicit_name)
+ /* Force explicit tag name, if a name is there. */
+ pfnote (name, TRUE, buffer + linecharno,
+ charno - linecharno + 1, lineno, linecharno);
+ else
+ make_tag (name, strlen (name), TRUE, buffer + linecharno,
+ charno - linecharno + 1, lineno, linecharno);
+ break;
+ }
+ }
+ }
+}
+
#endif /* ETAGS_REGEXPS */
-/* Initialize a linebuffer for use */
-void
-initbuffer (linebuffer)
- struct linebuffer *linebuffer;
+
+\f
+static bool
+nocase_tail (cp)
+ char *cp;
+{
+ register int len = 0;
+
+ while (*cp != '\0' && lowcase (*cp) == lowcase (dbp[len]))
+ cp++, len++;
+ if (*cp == '\0' && !intoken (dbp[len]))
+ {
+ dbp += len;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+static void
+get_tag (bp, namepp)
+ register char *bp;
+ char **namepp;
{
- linebuffer->size = 200;
- linebuffer->buffer = xnew (200, char);
+ register char *cp = bp;
+
+ if (*bp != '\0')
+ {
+ /* Go till you get to white space or a syntactic break */
+ for (cp = bp + 1; !notinname (*cp); cp++)
+ continue;
+ make_tag (bp, cp - bp, TRUE,
+ lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
+ }
+
+ if (namepp != NULL)
+ *namepp = savenstr (bp, cp - bp);
}
/*
- * Read a line of text from `stream' into `linebuffer'.
- * Return the number of characters read from `stream',
- * which is the length of the line including the newline, if any.
+ * Read a line of text from `stream' into `lbp', excluding the
+ * newline or CR-NL, if any. Return the number of characters read from
+ * `stream', which is the length of the line including the newline.
+ *
+ * On DOS or Windows we do not count the CR character, if any before the
+ * NL, in the returned length; this mirrors the behavior of Emacs on those
+ * platforms (for text files, it translates CR-NL to NL as it reads in the
+ * file).
+ *
+ * If multi-line regular expressions are requested, each line read is
+ * appended to `filebuf'.
*/
-long
-readline_internal (linebuffer, stream)
- struct linebuffer *linebuffer;
+static long
+readline_internal (lbp, stream)
+ linebuffer *lbp;
register FILE *stream;
{
- char *buffer = linebuffer->buffer;
- register char *p = linebuffer->buffer;
+ char *buffer = lbp->buffer;
+ register char *p = lbp->buffer;
register char *pend;
int chars_deleted;
- pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
+ pend = p + lbp->size; /* Separate to avoid 386/IX compiler bug. */
- while (1)
+ for (;;)
{
register int c = getc (stream);
if (p == pend)
{
- linebuffer->size *= 2;
- buffer = (char *) xrealloc (buffer, linebuffer->size);
- p += buffer - linebuffer->buffer;
- pend = buffer + linebuffer->size;
- linebuffer->buffer = buffer;
+ /* We're at the end of linebuffer: expand it. */
+ lbp->size *= 2;
+ xrnew (buffer, lbp->size, char);
+ p += buffer - lbp->buffer;
+ pend = buffer + lbp->size;
+ lbp->buffer = buffer;
}
if (c == EOF)
{
}
*p++ = c;
}
- linebuffer->len = p - buffer;
+ lbp->len = p - buffer;
+
+ if (need_filebuf /* we need filebuf for multi-line regexps */
+ && chars_deleted > 0) /* not at EOF */
+ {
+ while (filebuf.size <= filebuf.len + lbp->len + 1) /* +1 for \n */
+ {
+ /* Expand filebuf. */
+ filebuf.size *= 2;
+ xrnew (filebuf.buffer, filebuf.size, char);
+ }
+ strncpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len);
+ filebuf.len += lbp->len;
+ filebuf.buffer[filebuf.len++] = '\n';
+ filebuf.buffer[filebuf.len] = '\0';
+ }
- return linebuffer->len + chars_deleted;
+ return lbp->len + chars_deleted;
}
/*
* Like readline_internal, above, but in addition try to match the
- * input line against any existing regular expressions.
+ * input line against relevant regular expressions and manage #line
+ * directives.
*/
-long
-readline (linebuffer, stream)
- struct linebuffer *linebuffer;
+static void
+readline (lbp, stream)
+ linebuffer *lbp;
FILE *stream;
{
- /* Read new line. */
- long result = readline_internal (linebuffer, stream);
-#ifdef ETAGS_REGEXPS
- int i;
-
- /* Match against all listed patterns. */
- if (linebuffer->len > 0)
- for (i = 0; i < num_patterns; ++i)
- {
- int match = re_match (patterns[i].pattern, linebuffer->buffer,
- linebuffer->len, 0, &patterns[i].regs);
- switch (match)
- {
- case -2:
- /* Some error. */
- if (!patterns[i].error_signaled)
- {
- error ("error while matching pattern %d", i);
- patterns[i].error_signaled = TRUE;
- }
- break;
- case -1:
- /* No match. */
- break;
- default:
- /* Match occurred. Construct a tag. */
- if (patterns[i].name_pattern[0] != '\0')
- {
- /* Make a named tag. */
- char *name = substitute (linebuffer->buffer,
- patterns[i].name_pattern,
- &patterns[i].regs);
- if (name != NULL)
- pfnote (name, TRUE,
- linebuffer->buffer, match, lineno, linecharno);
- }
- else
- {
- /* Make an unnamed tag. */
- pfnote ((char *)NULL, TRUE,
- linebuffer->buffer, match, lineno, linecharno);
- }
- break;
- }
- }
-#endif /* ETAGS_REGEXPS */
-
- return result;
-}
+ long result;
-/*
- * Read a file, but do no processing. This is used to do regexp
- * matching on files that have no language defined.
- */
-void
-just_read_file (inf)
- FILE *inf;
-{
- lineno = 0;
- charno = 0;
+ linecharno = charno; /* update global char number of line start */
+ result = readline_internal (lbp, stream); /* read line */
+ lineno += 1; /* increment global line number */
+ charno += result; /* increment global char number */
- while (!feof (inf))
+ /* Honour #line directives. */
+ if (!no_line_directive)
{
- ++lineno;
- linecharno = charno;
- charno += readline (&lb, inf);
- }
+ static bool discard_until_line_directive;
+
+ /* Check whether this is a #line directive. */
+ if (result > 12 && strneq (lbp->buffer, "#line ", 6))
+ {
+ int start, lno;
+
+ if (DEBUG) start = 0; /* shut up the compiler */
+ if (sscanf (lbp->buffer, "#line %d \"%n", &lno, &start) == 1)
+ {
+ char *endp = lbp->buffer + start;
+
+ assert (start > 0);
+ while ((endp = etags_strchr (endp, '"')) != NULL
+ && endp[-1] == '\\')
+ endp++;
+ if (endp != NULL)
+ /* Ok, this is a real #line directive. Let's deal with it. */
+ {
+ char *taggedabsname; /* absolute name of original file */
+ char *taggedfname; /* name of original file as given */
+ char *name; /* temp var */
+
+ discard_until_line_directive = FALSE; /* found it */
+ name = lbp->buffer + start;
+ *endp = '\0';
+ canonicalize_filename (name); /* for DOS */
+ taggedabsname = absolute_filename (name, curfdp->infabsdir);
+ if (filename_is_absolute (name)
+ || filename_is_absolute (curfdp->infname))
+ taggedfname = savestr (taggedabsname);
+ else
+ taggedfname = relative_filename (taggedabsname,tagfiledir);
+
+ if (streq (curfdp->taggedfname, taggedfname))
+ /* The #line directive is only a line number change. We
+ deal with this afterwards. */
+ free (taggedfname);
+ else
+ /* The tags following this #line directive should be
+ attributed to taggedfname. In order to do this, set
+ curfdp accordingly. */
+ {
+ fdesc *fdp; /* file description pointer */
+
+ /* Go look for a file description already set up for the
+ file indicated in the #line directive. If there is
+ one, use it from now until the next #line
+ directive. */
+ for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
+ if (streq (fdp->infname, curfdp->infname)
+ && streq (fdp->taggedfname, taggedfname))
+ /* If we remove the second test above (after the &&)
+ then all entries pertaining to the same file are
+ coalesced in the tags file. If we use it, then
+ entries pertaining to the same file but generated
+ from different files (via #line directives) will
+ go into separate sections in the tags file. These
+ alternatives look equivalent. The first one
+ destroys some apparently useless information. */
+ {
+ curfdp = fdp;
+ free (taggedfname);
+ break;
+ }
+ /* Else, if we already tagged the real file, skip all
+ input lines until the next #line directive. */
+ if (fdp == NULL) /* not found */
+ for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
+ if (streq (fdp->infabsname, taggedabsname))
+ {
+ discard_until_line_directive = TRUE;
+ free (taggedfname);
+ break;
+ }
+ /* Else create a new file description and use that from
+ now on, until the next #line directive. */
+ if (fdp == NULL) /* not found */
+ {
+ fdp = fdhead;
+ fdhead = xnew (1, fdesc);
+ *fdhead = *curfdp; /* copy curr. file description */
+ fdhead->next = fdp;
+ fdhead->infname = savestr (curfdp->infname);
+ fdhead->infabsname = savestr (curfdp->infabsname);
+ fdhead->infabsdir = savestr (curfdp->infabsdir);
+ fdhead->taggedfname = taggedfname;
+ fdhead->usecharno = FALSE;
+ fdhead->prop = NULL;
+ fdhead->written = FALSE;
+ curfdp = fdhead;
+ }
+ }
+ free (taggedabsname);
+ lineno = lno - 1;
+ readline (lbp, stream);
+ return;
+ } /* if a real #line directive */
+ } /* if #line is followed by a a number */
+ } /* if line begins with "#line " */
+
+ /* If we are here, no #line directive was found. */
+ if (discard_until_line_directive)
+ {
+ if (result > 0)
+ {
+ /* Do a tail recursion on ourselves, thus discarding the contents
+ of the line buffer. */
+ readline (lbp, stream);
+ return;
+ }
+ /* End of file. */
+ discard_until_line_directive = FALSE;
+ return;
+ }
+ } /* if #line directives should be considered */
+
+#ifdef ETAGS_REGEXPS
+ {
+ int match;
+ regexp *rp;
+ char *name;
+
+ /* Match against relevant regexps. */
+ if (lbp->len > 0)
+ for (rp = p_head; rp != NULL; rp = rp->p_next)
+ {
+ /* Only use generic regexps or those for the current language.
+ Also do not use multiline regexps, which is the job of
+ regex_tag_multiline. */
+ if ((rp->lang != NULL && rp->lang != fdhead->lang)
+ || rp->multi_line)
+ continue;
+
+ match = re_match (rp->pat, lbp->buffer, lbp->len, 0, &rp->regs);
+ switch (match)
+ {
+ case -2:
+ /* Some error. */
+ if (!rp->error_signaled)
+ {
+ error ("regexp stack overflow while matching \"%s\"",
+ rp->pattern);
+ rp->error_signaled = TRUE;
+ }
+ break;
+ case -1:
+ /* No match. */
+ break;
+ case 0:
+ /* Empty string matched. */
+ if (!rp->error_signaled)
+ {
+ error ("regexp matches the empty string: \"%s\"", rp->pattern);
+ rp->error_signaled = TRUE;
+ }
+ break;
+ default:
+ /* Match occurred. Construct a tag. */
+ name = rp->name;
+ if (name[0] == '\0')
+ name = NULL;
+ else /* make a named tag */
+ name = substitute (lbp->buffer, rp->name, &rp->regs);
+ if (rp->force_explicit_name)
+ /* Force explicit tag name, if a name is there. */
+ pfnote (name, TRUE, lbp->buffer, match, lineno, linecharno);
+ else
+ make_tag (name, strlen (name), TRUE,
+ lbp->buffer, match, lineno, linecharno);
+ break;
+ }
+ }
+ }
+#endif /* ETAGS_REGEXPS */
}
\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;
{
* 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;
* 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
if (*sp == c)
r = sp;
} while (*sp++);
- return r;
+ return (char *)r;
}
-
/*
* Return the ptr in sp at which the character c first
* appears; NULL if not found
*
- * Identical to System V strchr, included for portability.
+ * Identical to POSIX strchr, included for portability.
*/
-char *
+static char *
etags_strchr (sp, c)
- register char *sp, c;
+ register const char *sp;
+ register int c;
{
do
{
if (*sp == c)
- return sp;
+ return (char *)sp;
} while (*sp++);
return NULL;
}
+/*
+ * Compare two strings, ignoring case for alphabetic characters.
+ *
+ * Same as BSD's strcasecmp, included for portability.
+ */
+static int
+etags_strcasecmp (s1, s2)
+ register const char *s1;
+ register const char *s2;
+{
+ while (*s1 != '\0'
+ && (ISALPHA (*s1) && ISALPHA (*s2)
+ ? lowcase (*s1) == lowcase (*s2)
+ : *s1 == *s2))
+ s1++, s2++;
+
+ return (ISALPHA (*s1) && ISALPHA (*s2)
+ ? lowcase (*s1) - lowcase (*s2)
+ : *s1 - *s2);
+}
+
+/*
+ * Compare two strings, ignoring case for alphabetic characters.
+ * Stop after a given number of characters
+ *
+ * Same as BSD's strncasecmp, included for portability.
+ */
+static int
+etags_strncasecmp (s1, s2, n)
+ register const char *s1;
+ register const char *s2;
+ register int n;
+{
+ while (*s1 != '\0' && n-- > 0
+ && (ISALPHA (*s1) && ISALPHA (*s2)
+ ? lowcase (*s1) == lowcase (*s2)
+ : *s1 == *s2))
+ s1++, s2++;
+
+ if (n < 0)
+ return 0;
+ else
+ return (ISALPHA (*s1) && ISALPHA (*s2)
+ ? lowcase (*s1) - lowcase (*s2)
+ : *s1 - *s2);
+}
+
+/* Skip spaces (end of string is not space), return new pointer. */
+static char *
+skip_spaces (cp)
+ char *cp;
+{
+ while (iswhite (*cp))
+ cp++;
+ return cp;
+}
+
+/* Skip non spaces, except end of string, return new pointer. */
+static char *
+skip_non_spaces (cp)
+ char *cp;
+{
+ while (*cp != '\0' && !iswhite (*cp))
+ cp++;
+ return cp;
+}
+
/* Print error message and exit. */
void
fatal (s1, s2)
char *s1, *s2;
{
error (s1, s2);
- exit (BAD);
+ exit (EXIT_FAILURE);
}
-void
+static void
pfatal (s1)
char *s1;
{
perror (s1);
- exit (BAD);
+ exit (EXIT_FAILURE);
}
-void
+static void
suggest_asking_for_help ()
{
- fprintf (stderr, "\tTry `%s --help' for a complete list of options.\n",
- progname);
- exit (BAD);
+ fprintf (stderr, "\tTry `%s %s' for a complete list of options.\n",
+ progname, LONG_OPTIONS ? "--help" : "-h");
+ exit (EXIT_FAILURE);
}
/* 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);
/* Return a newly-allocated string whose contents
concatenate those of s1, s2, s3. */
-char *
+static char *
concat (s1, s2, s3)
char *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 ()
{
-#if defined (HAVE_GETCWD) || defined (WINDOWSNT)
+#ifdef HAVE_GETCWD
int bufsize = 200;
char *path = xnew (bufsize, char);
path = xnew (bufsize, char);
}
-#if WINDOWSNT
- {
- /* Convert backslashes to slashes. */
- char *p;
- for (p = path; *p != '\0'; p++)
- if (*p == '\\')
- *p = '/';
- }
-#endif
-
+ canonicalize_filename (path);
return path;
#else /* not HAVE_GETCWD */
-#ifdef MSDOS
+#if MSDOS
+
char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
getwd (path);
return strdup (path);
#else /* not MSDOS */
- struct linebuffer path;
+ linebuffer path;
FILE *pipe;
- initbuffer (&path);
+ linebuffer_init (&path);
pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
if (pipe == NULL || readline_internal (&path, pipe) == 0)
pfatal ("pwd");
#endif /* not HAVE_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 *
+/* Return a newly allocated string containing the file name of FILE
+ relative to the absolute directory DIR (which should end with a slash). */
+static char *
relative_filename (file, dir)
char *file, *dir;
{
- char *fp, *dp, *abs, *res;
+ char *fp, *dp, *afn, *res;
int i;
/* Find the common root of file and dir (with a trailing slash). */
- abs = absolute_filename (file, cwd);
- fp = abs;
+ afn = absolute_filename (file, cwd);
+ fp = afn;
dp = dir;
while (*fp++ == *dp++)
continue;
fp--, dp--; /* back to the first differing char */
- do { /* look at the equal chars until '/' */
- if (fp == abs) return abs; /* first char differs, give up */
+#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 != '/');
+ while (*fp != '/');
/* Build a sequence of "../" strings for the resulting relative file name. */
i = 0;
/* Add the file name relative to the common root of file and dir. */
strcat (res, fp + 1);
- free (abs);
+ free (afn);
return res;
}
-/* Return a newly allocated string containing the
- absolute file name of FILE given CWD (which should
- end with a slash). */
-char *
-absolute_filename (file, cwd)
- char *file, *cwd;
+/* Return a newly allocated string containing the absolute file name
+ of FILE given DIR (which should end with a slash). */
+static char *
+absolute_filename (file, dir)
+ char *file, *dir;
{
char *slashp, *cp, *res;
- if (absolutefn (file))
+ if (filename_is_absolute (file))
res = savestr (file);
#ifdef DOS_NT
/* We don't support non-absolute file names with a drive
fatal ("%s: relative file names with drive letters not supported", file);
#endif
else
- res = concat (cwd, file, "");
+ res = concat (dir, file, "");
/* Delete the "/dirname/.." and "/." substrings. */
slashp = etags_strchr (res, '/');
cp = slashp;
do
cp--;
- while (cp >= res && !absolutefn (cp));
+ while (cp >= res && !filename_is_absolute (cp));
if (cp < res)
cp = slashp; /* the absolute name begins with "/.." */
#ifdef DOS_NT
slashp = etags_strchr (slashp + 1, '/');
}
-
+
if (res[0] == '\0')
return savestr ("/");
else
}
/* Return a newly allocated string containing the absolute
- file name of dir where FILE resides given CWD (which should
+ file name of dir where FILE resides given DIR (which should
end with a slash). */
-char *
-absolute_dirname (file, cwd)
- char *file, *cwd;
+static char *
+absolute_dirname (file, dir)
+ char *file, *dir;
{
char *slashp, *res;
char save;
-#ifdef DOS_NT
- char *p;
-
- for (p = file; *p != '\0'; p++)
- if (*p == '\\')
- *p = '/';
-#endif
+ canonicalize_filename (file);
slashp = etags_strrchr (file, '/');
if (slashp == NULL)
- return savestr (cwd);
+ return savestr (dir);
save = slashp[1];
slashp[1] = '\0';
- res = absolute_filename (file, cwd);
+ res = absolute_filename (file, dir);
slashp[1] = save;
return res;
}
-/* Increase the size of a linebuffer. */
-void
-grow_linebuffer (bufp, toksize)
- struct linebuffer *bufp;
+/* Whether the argument string is an absolute file name. The argument
+ string must have been canonicalized with canonicalize_filename. */
+static bool
+filename_is_absolute (fn)
+ char *fn;
+{
+ return (fn[0] == '/'
+#ifdef DOS_NT
+ || (ISALPHA(fn[0]) && fn[1] == ':' && fn[2] == '/')
+#endif
+ );
+}
+
+/* Translate backslashes into slashes. Works in place. */
+static void
+canonicalize_filename (fn)
+ register char *fn;
+{
+#ifdef DOS_NT
+ /* Canonicalize drive letter case. */
+ if (fn[0] != '\0' && fn[1] == ':' && ISLOWER (fn[0]))
+ fn[0] = upcase (fn[0]);
+ /* Convert backslashes to slashes. */
+ for (; *fn != '\0'; fn++)
+ if (*fn == '\\')
+ *fn = '/';
+#else
+ /* No action. */
+ fn = NULL; /* shut up the compiler */
+#endif
+}
+
+\f
+/* Initialize a linebuffer for use */
+static void
+linebuffer_init (lbp)
+ linebuffer *lbp;
+{
+ lbp->size = (DEBUG) ? 3 : 200;
+ lbp->buffer = xnew (lbp->size, char);
+ lbp->buffer[0] = '\0';
+ lbp->len = 0;
+}
+
+/* Set the minimum size of a string contained in a linebuffer. */
+static void
+linebuffer_setlen (lbp, toksize)
+ linebuffer *lbp;
int toksize;
{
- while (bufp->size < toksize)
- bufp->size *= 2;
- bufp->buffer = (char *) xrealloc (bufp->buffer, bufp->size);
+ while (lbp->size <= toksize)
+ {
+ lbp->size *= 2;
+ xrnew (lbp->buffer, lbp->size, char);
+ }
+ lbp->len = toksize;
}
-/* Like malloc but get fatal error if memory is exhausted. */
-long *
+/* Like malloc but get fatal error if memory is exhausted. */
+static PTR
xmalloc (size)
unsigned int size;
{
- long *result = (long *) malloc (size);
+ PTR result = (PTR) malloc (size);
if (result == NULL)
fatal ("virtual memory exhausted", (char *)NULL);
return result;
}
-long *
+static PTR
xrealloc (ptr, size)
char *ptr;
unsigned int size;
{
- long *result = (long *) realloc (ptr, size);
+ PTR result = (PTR) realloc (ptr, size);
if (result == NULL)
fatal ("virtual memory exhausted", (char *)NULL);
return result;
}
+
+/*
+ * Local Variables:
+ * indent-tabs-mode: t
+ * tab-width: 8
+ * fill-column: 79
+ * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
+ * End:
+ */
+
+/* arch-tag: 8a9b748d-390c-4922-99db-2eeefa921051
+ (do not change this comment) */
+
+/* etags.c ends here */