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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95";
42 #include <sys/cdefs.h>
43 #include <sys/types.h>
45 __FBSDID("$FreeBSD$");
59 * ctags: create a tags file
62 NODE *head; /* head of the sorted binary tree */
64 /* boolean "func" (see init()) */
65 bool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
67 FILE *inf; /* ioptr for current input file */
68 FILE *outf; /* ioptr for tags file */
70 long lineftell; /* ftell after getc( inf ) == '\n' */
72 int lineno; /* line number of current line */
73 int dflag; /* -d: non-macro defines */
74 int tflag; /* -t: create tags for typedefs */
75 int vflag; /* -v: vgrind style index output */
76 int wflag; /* -w: suppress warnings */
77 int xflag; /* -x: cxref style output */
79 char *curfile; /* current input file name */
80 char searchar = '/'; /* use /.../ searches by default */
84 void find_entries(char *);
85 static void usage(void);
88 main(int argc, char **argv)
90 static const char *outfile = "tags"; /* output file */
91 int aflag; /* -a: append to tags */
92 int uflag; /* -u: update tags */
93 int exit_val; /* exit value */
94 int step; /* step through args */
95 int ch; /* getopts char */
97 setlocale(LC_ALL, "");
101 while ((ch = getopt(argc, argv, "BFTadf:tuwvx")) != -1)
145 setlocale(LC_COLLATE, "C");
149 for (exit_val = step = 0; step < argc; ++step)
150 if (!(inf = fopen(argv[step], "r"))) {
151 warn("%s", argv[step]);
155 curfile = argv[step];
156 find_entries(argv[step]);
168 if ((oldf = fopen(outfile, "r")) == NULL)
169 err(1, "opening %s", outfile);
171 err(1, "unlinking %s", outfile);
172 if ((outf = fopen(outfile, "w")) == NULL)
173 err(1, "recreating %s", outfile);
174 if ((regx = calloc(argc, sizeof(regex_t))) == NULL)
176 for (step = 0; step < argc; step++) {
177 (void)strcpy(lbuf, "\t");
178 (void)strlcat(lbuf, argv[step], LINE_MAX);
179 (void)strlcat(lbuf, "\t", LINE_MAX);
180 if (regcomp(regx + step, lbuf,
182 warn("RE compilation failed");
185 while (fgets(lbuf, LINE_MAX, oldf)) {
186 for (step = 0; step < argc; step++)
187 if (regexec(regx + step,
188 lbuf, 0, NULL, 0) == 0)
192 for (step = 0; step < argc; step++)
193 regfree(regx + step);
199 if (!(outf = fopen(outfile, aflag ? "a" : "w")))
200 err(1, "%s", outfile);
206 if ((pid = fork()) == -1)
207 err(1, "fork failed");
209 execlp("sort", "sort", "-o", outfile,
211 err(1, "exec of sort failed");
213 /* Just assume the sort went OK. The old code
214 did not do any checks either. */
225 (void)fprintf(stderr, "usage: ctags [-BFTaduwvx] [-f tagsfile] file ...\n");
231 * this routine sets up the boolean pseudo-functions which work by
232 * setting boolean flags dependent upon the corresponding character.
233 * Every char which is NOT in that string is false with respect to
234 * the pseudo-function. Therefore, all of the array "_wht" is NO
235 * by default and then the elements subscripted by the chars in
236 * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in
237 * the string CWHITE, else NO.
243 const unsigned char *sp;
245 for (i = 0; i < 256; i++) {
246 _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
249 #define CWHITE " \f\t\n"
250 for (sp = CWHITE; *sp; sp++) /* white space chars */
252 #define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
253 for (sp = CTOKEN; *sp; sp++) /* token ending chars */
255 #define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
256 for (sp = CINTOK; *sp; sp++) /* valid in-token chars */
258 #define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
259 for (sp = CBEGIN; *sp; sp++) /* token starting chars */
262 for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */
268 * this routine opens the specified file and calls the function
269 * which searches the file.
272 find_entries(char *file)
276 lineno = 0; /* should be 1 ?? KB */
277 if ((cp = strrchr(file, '.'))) {
278 if (cp[1] == 'l' && !cp[2]) {
289 #define LISPCHR ";(["
290 /* lisp */ if (strchr(LISPCHR, c)) {
296 * we search all 3 parts of a lex file
297 * for C references. This may be wrong.
300 (void)strcpy(lbuf, "%%$");
301 pfnote("yylex", lineno);
305 /* yacc */ else if (cp[1] == 'y' && !cp[2]) {
307 * we search only the 3rd part of a yacc file
308 * for C references. This may be wrong.
311 (void)strcpy(lbuf, "%%$");
312 pfnote("yyparse", lineno);
315 /* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {