2 * Copyright (c) 1987, 1993, 1994, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95";
46 #include <sys/cdefs.h>
47 #include <sys/types.h>
49 __FBSDID("$FreeBSD$");
63 * ctags: create a tags file
66 NODE *head; /* head of the sorted binary tree */
68 /* boolean "func" (see init()) */
69 bool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
71 FILE *inf; /* ioptr for current input file */
72 FILE *outf; /* ioptr for tags file */
74 long lineftell; /* ftell after getc( inf ) == '\n' */
76 int lineno; /* line number of current line */
77 int dflag; /* -d: non-macro defines */
78 int tflag; /* -t: create tags for typedefs */
79 int vflag; /* -v: vgrind style index output */
80 int wflag; /* -w: suppress warnings */
81 int xflag; /* -x: cxref style output */
83 char *curfile; /* current input file name */
84 char searchar = '/'; /* use /.../ searches by default */
88 void find_entries(char *);
89 static void usage(void);
92 main(int argc, char **argv)
94 static const char *outfile = "tags"; /* output file */
95 int aflag; /* -a: append to tags */
96 int uflag; /* -u: update tags */
97 int exit_val; /* exit value */
98 int step; /* step through args */
99 int ch; /* getopts char */
101 setlocale(LC_ALL, "");
105 while ((ch = getopt(argc, argv, "BFTadf:tuwvx")) != -1)
149 setlocale(LC_COLLATE, "C");
153 for (exit_val = step = 0; step < argc; ++step)
154 if (!(inf = fopen(argv[step], "r"))) {
155 warn("%s", argv[step]);
159 curfile = argv[step];
160 find_entries(argv[step]);
172 if ((oldf = fopen(outfile, "r")) == NULL)
173 err(1, "opening %s", outfile);
175 err(1, "unlinking %s", outfile);
176 if ((outf = fopen(outfile, "w")) == NULL)
177 err(1, "recreating %s", outfile);
178 if ((regx = calloc(argc, sizeof(regex_t))) == NULL)
180 for (step = 0; step < argc; step++) {
181 (void)strcpy(lbuf, "\t");
182 (void)strlcat(lbuf, argv[step], LINE_MAX);
183 (void)strlcat(lbuf, "\t", LINE_MAX);
184 if (regcomp(regx + step, lbuf,
186 warn("RE compilation failed");
189 while (fgets(lbuf, LINE_MAX, oldf)) {
190 for (step = 0; step < argc; step++)
191 if (regexec(regx + step,
192 lbuf, 0, NULL, 0) == 0)
196 for (step = 0; step < argc; step++)
197 regfree(regx + step);
203 if (!(outf = fopen(outfile, aflag ? "a" : "w")))
204 err(1, "%s", outfile);
210 if ((pid = fork()) == -1)
211 err(1, "fork failed");
213 execlp("sort", "sort", "-o", outfile,
215 err(1, "exec of sort failed");
217 /* Just assume the sort went OK. The old code
218 did not do any checks either. */
229 (void)fprintf(stderr, "usage: ctags [-BFTaduwvx] [-f tagsfile] file ...\n");
235 * this routine sets up the boolean pseudo-functions which work by
236 * setting boolean flags dependent upon the corresponding character.
237 * Every char which is NOT in that string is false with respect to
238 * the pseudo-function. Therefore, all of the array "_wht" is NO
239 * by default and then the elements subscripted by the chars in
240 * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in
241 * the string CWHITE, else NO.
249 for (i = 0; i < 256; i++) {
250 _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
253 #define CWHITE " \f\t\n"
254 for (sp = CWHITE; *sp; sp++) /* white space chars */
256 #define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
257 for (sp = CTOKEN; *sp; sp++) /* token ending chars */
259 #define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
260 for (sp = CINTOK; *sp; sp++) /* valid in-token chars */
262 #define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
263 for (sp = CBEGIN; *sp; sp++) /* token starting chars */
266 for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */
272 * this routine opens the specified file and calls the function
273 * which searches the file.
276 find_entries(char *file)
280 lineno = 0; /* should be 1 ?? KB */
281 if ((cp = strrchr(file, '.'))) {
282 if (cp[1] == 'l' && !cp[2]) {
293 #define LISPCHR ";(["
294 /* lisp */ if (strchr(LISPCHR, c)) {
300 * we search all 3 parts of a lex file
301 * for C references. This may be wrong.
304 (void)strcpy(lbuf, "%%$");
305 pfnote("yylex", lineno);
309 /* yacc */ else if (cp[1] == 'y' && !cp[2]) {
311 * we search only the 3rd part of a yacc file
312 * for C references. This may be wrong.
315 (void)strcpy(lbuf, "%%$");
316 pfnote("yyparse", lineno);
319 /* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {