GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ctags/ctags.c Lines: 0 115 0.0 %
Date: 2016-12-06 Branches: 0 95 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ctags.c,v 1.18 2015/10/09 01:37:07 deraadt Exp $	*/
2
/*	$NetBSD: ctags.c,v 1.4 1995/09/02 05:57:23 jtc Exp $	*/
3
4
/*
5
 * Copyright (c) 1987, 1993, 1994, 1995
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include <err.h>
34
#include <limits.h>
35
#include <stdio.h>
36
#include <string.h>
37
#include <stdlib.h>
38
#include <unistd.h>
39
40
#include "ctags.h"
41
42
/*
43
 * ctags: create a tags file
44
 */
45
46
NODE	*head;			/* head of the sorted binary tree */
47
48
				/* boolean "func" (see init()) */
49
bool	_wht[256], _itk[256], _btk[256];
50
51
FILE	*inf;			/* ioptr for current input file */
52
FILE	*outf;			/* ioptr for tags file */
53
54
long	lineftell;		/* ftell after getc( inf ) == '\n' */
55
56
int	lineno;			/* line number of current line */
57
int	dflag;			/* -d: non-macro defines */
58
int	vflag;			/* -v: vgrind style index output */
59
int	wflag;			/* -w: suppress warnings */
60
int	xflag;			/* -x: cxref style output */
61
62
char	*curfile;		/* current input file name */
63
char	searchar = '/';		/* use /.../ searches by default */
64
char	lbuf[LINE_MAX];
65
66
void	init(void);
67
void	find_entries(char *);
68
void	preload_entries(char *, int, char *[]);
69
70
int
71
main(int argc, char *argv[])
72
{
73
	static char	*outfile = "tags";	/* output file */
74
	int	aflag;				/* -a: append to tags */
75
	int	uflag;				/* -u: update tags */
76
	int	exit_val;			/* exit value */
77
	int	step;				/* step through args */
78
	int	ch;				/* getopts char */
79
80
	if (pledge("stdio rpath wpath cpath", NULL) == -1)
81
		err(1, "pledge");
82
83
	aflag = uflag = NO;
84
	while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
85
		switch(ch) {
86
		case 'B':
87
			searchar = '?';
88
			break;
89
		case 'F':
90
			searchar = '/';
91
			break;
92
		case 'a':
93
			aflag = 1;
94
			break;
95
		case 'd':
96
			dflag = 1;
97
			break;
98
		case 'f':
99
			outfile = optarg;
100
			break;
101
		case 't':
102
			/* backwards compatibility */
103
			break;
104
		case 'u':
105
			uflag = 1;
106
			break;
107
		case 'w':
108
			wflag = 1;
109
			break;
110
		case 'v':
111
			vflag = 1;
112
		case 'x':
113
			xflag = 1;
114
			break;
115
		case '?':
116
		default:
117
			goto usage;
118
		}
119
	argv += optind;
120
	argc -= optind;
121
	if (!argc) {
122
usage:		(void)fprintf(stderr,
123
			"usage: ctags [-aBdFuvwx] [-f tagsfile] file ...\n");
124
		exit(1);
125
	}
126
127
	init();
128
	if (uflag && !vflag && !xflag)
129
		preload_entries(outfile, argc, argv);
130
131
	for (exit_val = step = 0; step < argc; ++step)
132
		if (!(inf = fopen(argv[step], "r"))) {
133
			warn("%s", argv[step]);
134
			exit_val = 1;
135
		}
136
		else {
137
			curfile = argv[step];
138
			find_entries(argv[step]);
139
			(void)fclose(inf);
140
		}
141
142
	if (head) {
143
		if (xflag)
144
			put_entries(head);
145
		else {
146
			if (!(outf = fopen(outfile, aflag ? "a" : "w")))
147
				err(exit_val, "%s", outfile);
148
			put_entries(head);
149
			(void)fclose(outf);
150
		}
151
	}
152
	exit(exit_val);
153
}
154
155
/*
156
 * init --
157
 *	this routine sets up the boolean psuedo-functions which work by
158
 *	setting boolean flags dependent upon the corresponding character.
159
 *	Every char which is NOT in that string is false with respect to
160
 *	the pseudo-function.  Therefore, all of the array "_wht" is NO
161
 *	by default and then the elements subscripted by the chars in
162
 *	CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
163
 *	the string CWHITE, else NO.
164
 */
165
void
166
init(void)
167
{
168
	int		i;
169
	unsigned char	*sp;
170
171
	for (i = 0; i < 256; i++)
172
		_wht[i] = _itk[i] = _btk[i] = NO;
173
#define	CWHITE	" \f\t\n"
174
	for (sp = CWHITE; *sp; sp++)	/* white space chars */
175
		_wht[*sp] = YES;
176
#define	CINTOK	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
177
	for (sp = CINTOK; *sp; sp++)	/* valid in-token chars */
178
		_itk[*sp] = YES;
179
#define	CBEGIN	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
180
	for (sp = CBEGIN; *sp; sp++)	/* token starting chars */
181
		_btk[*sp] = YES;
182
}
183
184
/*
185
 * find_entries --
186
 *	this routine opens the specified file and calls the function
187
 *	which searches the file.
188
 */
189
void
190
find_entries(char *file)
191
{
192
	char	*cp;
193
194
	lineno = 0;				/* should be 1 ?? KB */
195
	if ((cp = strrchr(file, '.'))) {
196
		if (cp[1] == 'l' && !cp[2]) {
197
			int	c;
198
199
			for (;;) {
200
				if (GETC(==, EOF))
201
					return;
202
				if (!iswhite(c)) {
203
					rewind(inf);
204
					break;
205
				}
206
			}
207
#define	LISPCHR	";(["
208
/* lisp */		if (strchr(LISPCHR, c)) {
209
				l_entries();
210
				return;
211
			}
212
/* lex */		else {
213
				/*
214
				 * we search all 3 parts of a lex file
215
				 * for C references.  This may be wrong.
216
				 */
217
				toss_yysec();
218
				(void)strlcpy(lbuf, "%%$", sizeof lbuf);
219
				pfnote("yylex", lineno);
220
				rewind(inf);
221
			}
222
		}
223
/* yacc */	else if (cp[1] == 'y' && !cp[2]) {
224
			/*
225
			 * we search only the 3rd part of a yacc file
226
			 * for C references.  This may be wrong.
227
			 */
228
			toss_yysec();
229
			(void)strlcpy(lbuf, "%%$", sizeof lbuf);
230
			pfnote("yyparse", lineno);
231
			y_entries();
232
		}
233
/* fortran */	else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
234
			if (PF_funcs())
235
				return;
236
			rewind(inf);
237
		}
238
	}
239
/* C */	c_entries();
240
}
241
242
void
243
preload_entries(char *tagsfile, int argc, char *argv[])
244
{
245
	FILE	*fp;
246
	char	 line[LINE_MAX];
247
	char	*entry = NULL;
248
	char	*file = NULL;
249
	char	*pattern = NULL;
250
	char	*eol;
251
	int	 i;
252
253
	in_preload = YES;
254
255
	if ((fp = fopen(tagsfile, "r")) == NULL)
256
		err(1, "preload_entries: %s", tagsfile);
257
258
	while (1) {
259
next:
260
		if (fgets(line, sizeof(line), fp) == NULL)
261
			break;
262
263
		if ((eol = strchr(line, '\n')) == NULL)
264
			errx(1, "preload_entries: line too long");
265
		*eol = '\0';
266
267
		/* extract entry */
268
		entry = line;
269
		if ((file = strchr(line, '\t')) == NULL)
270
			errx(1, "preload_entries: couldn't parse entry: %s",
271
			    tagsfile);
272
		*file = '\0';
273
274
		/* extract file */
275
		file++;
276
		if ((pattern = strchr(file, '\t')) == NULL)
277
			errx(1, "preload_entries: couldn't parse filename: %s",
278
			    tagsfile);
279
		*pattern = '\0';
280
281
		/* skip this file ? */
282
		for(i = 0; i < argc; i++)
283
			if (strcmp(file, argv[i]) == 0)
284
				goto next;
285
286
		/* rest of string is pattern */
287
		pattern++;
288
289
		/* grab searchar, and don't keep it around the pattern */
290
		if ((pattern[0] == '/' || pattern[0] == '?')
291
		    && pattern[1] == '^') {
292
293
			i = strlen(pattern);
294
			if (pattern[i-1] == pattern[0])
295
				/* remove searchar at end */
296
				pattern[i-1] = '\0';
297
			else
298
				errx(1, "preload_entries: couldn't parse "
299
				    "pattern: %s", tagsfile);
300
301
			/* remove searchar at begin */
302
			pattern += 2;
303
		}
304
305
		/* add entry */
306
		if ((curfile = strdup(file)) == NULL)
307
			err(1, "preload_entries: strdup");
308
		(void)strlcpy(lbuf, pattern, sizeof(lbuf));
309
		pfnote(entry, 0);
310
	}
311
	if (ferror(fp))
312
		err(1, "preload_entries: fgets");
313
314
	(void)fclose(fp);
315
	in_preload = NO;
316
}