2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95";
44 #include <sys/cdefs.h>
45 #include <sys/types.h>
47 __FBSDID("$FreeBSD$");
61 * ctags: create a tags file
64 NODE *head; /* head of the sorted binary tree */
66 /* boolean "func" (see init()) */
67 bool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
69 FILE *inf; /* ioptr for current input file */
70 FILE *outf; /* ioptr for tags file */
72 long lineftell; /* ftell after getc( inf ) == '\n' */
74 int lineno; /* line number of current line */
75 int dflag; /* -d: non-macro defines */
76 int tflag; /* -t: create tags for typedefs */
77 int vflag; /* -v: vgrind style index output */
78 int wflag; /* -w: suppress warnings */
79 int xflag; /* -x: cxref style output */
81 char *curfile; /* current input file name */
82 char searchar = '/'; /* use /.../ searches by default */
86 void find_entries(char *);
87 static void usage(void);
90 main(int argc, char **argv)
92 static const char *outfile = "tags"; /* output file */
93 int aflag; /* -a: append to tags */
94 int uflag; /* -u: update tags */
95 int exit_val; /* exit value */
96 int step; /* step through args */
97 int ch; /* getopts char */
99 setlocale(LC_ALL, "");
103 while ((ch = getopt(argc, argv, "BFTadf:tuwvx")) != -1)
147 setlocale(LC_COLLATE, "C");
151 for (exit_val = step = 0; step < argc; ++step)
152 if (!(inf = fopen(argv[step], "r"))) {
153 warn("%s", argv[step]);
157 curfile = argv[step];
158 find_entries(argv[step]);
170 if ((oldf = fopen(outfile, "r")) == NULL)
171 err(1, "opening %s", outfile);
173 err(1, "unlinking %s", outfile);
174 if ((outf = fopen(outfile, "w")) == NULL)
175 err(1, "recreating %s", outfile);
176 if ((regx = calloc(argc, sizeof(regex_t))) == NULL)
178 for (step = 0; step < argc; step++) {
179 (void)strcpy(lbuf, "\t");
180 (void)strlcat(lbuf, argv[step], LINE_MAX);
181 (void)strlcat(lbuf, "\t", LINE_MAX);
182 if (regcomp(regx + step, lbuf,
184 warn("RE compilation failed");
187 while (fgets(lbuf, LINE_MAX, oldf)) {
188 for (step = 0; step < argc; step++)
189 if (regexec(regx + step,
190 lbuf, 0, NULL, 0) == 0)
194 for (step = 0; step < argc; step++)
195 regfree(regx + step);
201 if (!(outf = fopen(outfile, aflag ? "a" : "w")))
202 err(1, "%s", outfile);
208 if ((pid = fork()) == -1)
209 err(1, "fork failed");
211 execlp("sort", "sort", "-o", outfile,
213 err(1, "exec of sort failed");
215 /* Just assume the sort went OK. The old code
216 did not do any checks either. */
227 (void)fprintf(stderr, "usage: ctags [-BFTaduwvx] [-f tagsfile] file ...\n");
233 * this routine sets up the boolean pseudo-functions which work by
234 * setting boolean flags dependent upon the corresponding character.
235 * Every char which is NOT in that string is false with respect to
236 * the pseudo-function. Therefore, all of the array "_wht" is NO
237 * by default and then the elements subscripted by the chars in
238 * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in
239 * the string CWHITE, else NO.
245 const unsigned char *sp;
247 for (i = 0; i < 256; i++) {
248 _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
251 #define CWHITE " \f\t\n"
252 for (sp = CWHITE; *sp; sp++) /* white space chars */
254 #define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
255 for (sp = CTOKEN; *sp; sp++) /* token ending chars */
257 #define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
258 for (sp = CINTOK; *sp; sp++) /* valid in-token chars */
260 #define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
261 for (sp = CBEGIN; *sp; sp++) /* token starting chars */
264 for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */
270 * this routine opens the specified file and calls the function
271 * which searches the file.
274 find_entries(char *file)
278 lineno = 0; /* should be 1 ?? KB */
279 if ((cp = strrchr(file, '.'))) {
280 if (cp[1] == 'l' && !cp[2]) {
291 #define LISPCHR ";(["
292 /* lisp */ if (strchr(LISPCHR, c)) {
298 * we search all 3 parts of a lex file
299 * for C references. This may be wrong.
302 (void)strcpy(lbuf, "%%$");
303 pfnote("yylex", lineno);
307 /* yacc */ else if (cp[1] == 'y' && !cp[2]) {
309 * we search only the 3rd part of a yacc file
310 * for C references. This may be wrong.
313 (void)strcpy(lbuf, "%%$");
314 pfnote("yyparse", lineno);
317 /* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {