1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1987, 1993, 1994, 1995
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/wait.h>
35
36#include <err.h>
37#include <errno.h>
38#include <limits.h>
39#include <locale.h>
40#include <regex.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#include "ctags.h"
47
48/*
49 * ctags: create a tags file
50 */
51
52NODE	*head;			/* head of the sorted binary tree */
53
54				/* boolean "func" (see init()) */
55bool	_wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
56
57FILE	*inf;			/* ioptr for current input file */
58FILE	*outf;			/* ioptr for tags file */
59
60long	lineftell;		/* ftell after getc( inf ) == '\n' */
61
62int	lineno;			/* line number of current line */
63int	dflag;			/* -d: non-macro defines */
64int	tflag;			/* -t: create tags for typedefs */
65int	vflag;			/* -v: vgrind style index output */
66int	wflag;			/* -w: suppress warnings */
67int	xflag;			/* -x: cxref style output */
68
69char	*curfile;		/* current input file name */
70char	searchar = '/';		/* use /.../ searches by default */
71char	lbuf[LINE_MAX];
72
73void	init(void);
74void	find_entries(char *);
75static void usage(void) __dead2;
76
77int
78main(int argc, char **argv)
79{
80	static const char	*outfile = "tags";	/* output file */
81	int	aflag;				/* -a: append to tags */
82	int	uflag;				/* -u: update tags */
83	int	exit_val;			/* exit value */
84	int	step;				/* step through args */
85	int	ch;				/* getopts char */
86
87	setlocale(LC_ALL, "");
88
89	aflag = uflag = false;
90	tflag = true;
91	while ((ch = getopt(argc, argv, "BFTadf:tuwvx")) != -1)
92		switch(ch) {
93		case 'B':
94			searchar = '?';
95			break;
96		case 'F':
97			searchar = '/';
98			break;
99		case 'T':
100			tflag = false;
101			break;
102		case 'a':
103			aflag++;
104			break;
105		case 'd':
106			dflag++;
107			break;
108		case 'f':
109			outfile = optarg;
110			break;
111		case 't':
112			tflag = true;
113			break;
114		case 'u':
115			uflag++;
116			break;
117		case 'w':
118			wflag++;
119			break;
120		case 'v':
121			vflag++;
122		case 'x':
123			xflag++;
124			break;
125		case '?':
126		default:
127			usage();
128		}
129	argv += optind;
130	argc -= optind;
131	if (!argc)
132		usage();
133
134	if (strcmp(outfile, "-") == 0)
135		outfile = "/dev/stdout";
136
137	if (!xflag)
138		setlocale(LC_COLLATE, "C");
139
140	init();
141
142	for (exit_val = step = 0; step < argc; ++step)
143		if (!(inf = fopen(argv[step], "r"))) {
144			warn("%s", argv[step]);
145			exit_val = 1;
146		}
147		else {
148			curfile = argv[step];
149			find_entries(argv[step]);
150			(void)fclose(inf);
151		}
152
153	if (head) {
154		if (xflag)
155			put_entries(head);
156		else {
157			if (uflag) {
158				struct stat sb;
159				FILE *oldf;
160				regex_t *regx;
161
162				if ((oldf = fopen(outfile, "r")) == NULL) {
163					if (errno == ENOENT) {
164						uflag = 0;
165						goto udone;
166					}
167					err(1, "opening %s", outfile);
168				}
169				if (fstat(fileno(oldf), &sb) != 0 ||
170				    !S_ISREG(sb.st_mode)) {
171					fclose(oldf);
172					uflag = 0;
173					goto udone;
174				}
175				if (unlink(outfile))
176					err(1, "unlinking %s", outfile);
177				if ((outf = fopen(outfile, "w")) == NULL)
178					err(1, "recreating %s", outfile);
179				if ((regx = calloc(argc, sizeof(regex_t))) == NULL)
180					err(1, "RE alloc");
181				for (step = 0; step < argc; step++) {
182					(void)strcpy(lbuf, "\t");
183					(void)strlcat(lbuf, argv[step], LINE_MAX);
184					(void)strlcat(lbuf, "\t", LINE_MAX);
185					if (regcomp(regx + step, lbuf,
186					    REG_NOSPEC))
187						warn("RE compilation failed");
188				}
189nextline:
190				while (fgets(lbuf, LINE_MAX, oldf)) {
191					for (step = 0; step < argc; step++)
192						if (regexec(regx + step,
193						    lbuf, 0, NULL, 0) == 0)
194							goto nextline;
195					fputs(lbuf, outf);
196				}
197				for (step = 0; step < argc; step++)
198					regfree(regx + step);
199				free(regx);
200				fclose(oldf);
201				fclose(outf);
202				++aflag;
203			}
204udone:
205			if (!(outf = fopen(outfile, aflag ? "a" : "w")))
206				err(1, "%s", outfile);
207			put_entries(head);
208			(void)fclose(outf);
209			if (uflag) {
210				pid_t pid;
211
212				if ((pid = fork()) == -1)
213					err(1, "fork failed");
214				else if (pid == 0) {
215					execlp("sort", "sort", "-o", outfile,
216					    outfile, NULL);
217					err(1, "exec of sort failed");
218				}
219				/* Just assume the sort went OK. The old code
220				   did not do any checks either. */
221				(void)wait(NULL);
222			}
223		}
224	}
225	if (ferror(stdout) != 0 || fflush(stdout) != 0)
226		err(1, "stdout");
227	exit(exit_val);
228}
229
230static void
231usage(void)
232{
233	(void)fprintf(stderr, "usage: ctags [-BFTaduwvx] [-f tagsfile] file ...\n");
234	exit(1);
235}
236
237/*
238 * init --
239 *	this routine sets up the boolean pseudo-functions which work by
240 *	setting boolean flags dependent upon the corresponding character.
241 *	Every char which is NOT in that string is false with respect to
242 *	the pseudo-function.  Therefore, all of the array "_wht" is NO
243 *	by default and then the elements subscripted by the chars in
244 *	CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
245 *	the string CWHITE, else NO.
246 */
247void
248init(void)
249{
250	int		i;
251	const unsigned char	*sp;
252
253	for (i = 0; i < 256; i++) {
254		_wht[i] = _etk[i] = _itk[i] = _btk[i] = false;
255		_gd[i] = true;
256	}
257#define	CWHITE	" \f\t\n"
258	for (sp = CWHITE; *sp; sp++)	/* white space chars */
259		_wht[*sp] = true;
260#define	CTOKEN	" \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
261	for (sp = CTOKEN; *sp; sp++)	/* token ending chars */
262		_etk[*sp] = true;
263#define	CINTOK	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
264	for (sp = CINTOK; *sp; sp++)	/* valid in-token chars */
265		_itk[*sp] = true;
266#define	CBEGIN	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
267	for (sp = CBEGIN; *sp; sp++)	/* token starting chars */
268		_btk[*sp] = true;
269#define	CNOTGD	",;"
270	for (sp = CNOTGD; *sp; sp++)	/* invalid after-function chars */
271		_gd[*sp] = false;
272}
273
274/*
275 * find_entries --
276 *	this routine opens the specified file and calls the function
277 *	which searches the file.
278 */
279void
280find_entries(char *file)
281{
282	char	*cp;
283
284	lineno = 0;				/* should be 1 ?? KB */
285	if ((cp = strrchr(file, '.'))) {
286		if (cp[1] == 'l' && !cp[2]) {
287			int	c;
288
289			for (;;) {
290				if (GETC(==, EOF))
291					return;
292				if (!iswhite(c)) {
293					rewind(inf);
294					break;
295				}
296			}
297#define	LISPCHR	";(["
298/* lisp */		if (strchr(LISPCHR, c)) {
299				l_entries();
300				return;
301			}
302/* lex */		else {
303				/*
304				 * we search all 3 parts of a lex file
305				 * for C references.  This may be wrong.
306				 */
307				toss_yysec();
308				(void)strcpy(lbuf, "%%$");
309				pfnote("yylex", lineno);
310				rewind(inf);
311			}
312		}
313/* yacc */	else if (cp[1] == 'y' && !cp[2]) {
314			/*
315			 * we search only the 3rd part of a yacc file
316			 * for C references.  This may be wrong.
317			 */
318			toss_yysec();
319			(void)strcpy(lbuf, "%%$");
320			pfnote("yyparse", lineno);
321			y_entries();
322		}
323/* fortran */	else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
324			if (PF_funcs())
325				return;
326			rewind(inf);
327		}
328	}
329/* C */	c_entries();
330}
331