ctags.c revision 81782
138786Sdfr/*
238786Sdfr * Copyright (c) 1987, 1993, 1994, 1995
338786Sdfr *	The Regents of the University of California.  All rights reserved.
438786Sdfr *
538786Sdfr * Redistribution and use in source and binary forms, with or without
638786Sdfr * modification, are permitted provided that the following conditions
738786Sdfr * are met:
838786Sdfr * 1. Redistributions of source code must retain the above copyright
938786Sdfr *    notice, this list of conditions and the following disclaimer.
1038786Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1138786Sdfr *    notice, this list of conditions and the following disclaimer in the
1238786Sdfr *    documentation and/or other materials provided with the distribution.
1338786Sdfr * 3. All advertising materials mentioning features or use of this software
1438786Sdfr *    must display the following acknowledgement:
1538786Sdfr *	This product includes software developed by the University of
1638786Sdfr *	California, Berkeley and its contributors.
1738786Sdfr * 4. Neither the name of the University nor the names of its contributors
1838786Sdfr *    may be used to endorse or promote products derived from this software
1938786Sdfr *    without specific prior written permission.
2038786Sdfr *
2138786Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2238786Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2338786Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2438786Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2538786Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2638786Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2738786Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2838786Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2950476Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3038786Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3138786Sdfr * SUCH DAMAGE.
3249831Smpp */
3379538Sru
3438786Sdfr#ifndef lint
3538786Sdfrstatic const char copyright[] =
3638786Sdfr"@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
3738786Sdfr	The Regents of the University of California.  All rights reserved.\n";
3838786Sdfr#endif /* not lint */
3938786Sdfr
4084306Sru#ifndef lint
4184306Sru#if 0
4238786Sdfrstatic const char sccsid[] = "@(#)ctags.c	8.4 (Berkeley) 2/7/95";
4338786Sdfr#endif
4438786Sdfrstatic const char rcsid[] =
4538786Sdfr  "$FreeBSD: head/usr.bin/ctags/ctags.c 81782 2001-08-16 16:16:07Z mikeh $";
4638786Sdfr#endif /* not lint */
4738786Sdfr
48121380Shmp#include <err.h>
49121380Shmp#include <limits.h>
5038786Sdfr#include <stdio.h>
5138786Sdfr#include <string.h>
5238786Sdfr#include <stdlib.h>
5338786Sdfr#include <unistd.h>
5438786Sdfr
5538786Sdfr#include "ctags.h"
5638786Sdfr
5738786Sdfr/*
58147647Shmp * ctags: create a tags file
5938786Sdfr */
60
61NODE	*head;			/* head of the sorted binary tree */
62
63				/* boolean "func" (see init()) */
64bool	_wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
65
66FILE	*inf;			/* ioptr for current input file */
67FILE	*outf;			/* ioptr for tags file */
68
69long	lineftell;		/* ftell after getc( inf ) == '\n' */
70
71int	lineno;			/* line number of current line */
72int	dflag;			/* -d: non-macro defines */
73int	tflag;			/* -t: create tags for typedefs */
74int	vflag;			/* -v: vgrind style index output */
75int	wflag;			/* -w: suppress warnings */
76int	xflag;			/* -x: cxref style output */
77
78char	*curfile;		/* current input file name */
79char	searchar = '/';		/* use /.../ searches by default */
80char	lbuf[LINE_MAX];
81
82void	init __P((void));
83void	find_entries __P((char *));
84static void usage __P((void));
85
86int
87main(argc, argv)
88	int	argc;
89	char	**argv;
90{
91	static char	*outfile = "tags";	/* output file */
92	int	aflag;				/* -a: append to tags */
93	int	uflag;				/* -u: update tags */
94	int	exit_val;			/* exit value */
95	int	step;				/* step through args */
96	int	ch;				/* getopts char */
97	char	cmd[100];			/* too ugly to explain */
98
99	aflag = uflag = NO;
100	while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
101		switch(ch) {
102		case 'B':
103			searchar = '?';
104			break;
105		case 'F':
106			searchar = '/';
107			break;
108		case 'a':
109			aflag++;
110			break;
111		case 'd':
112			dflag++;
113			break;
114		case 'f':
115			outfile = optarg;
116			break;
117		case 't':
118			tflag++;
119			break;
120		case 'u':
121			uflag++;
122			break;
123		case 'w':
124			wflag++;
125			break;
126		case 'v':
127			vflag++;
128		case 'x':
129			xflag++;
130			break;
131		case '?':
132		default:
133			usage();
134		}
135	argv += optind;
136	argc -= optind;
137	if (!argc)
138		usage();
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				for (step = 0; step < argc; step++) {
159					(void)sprintf(cmd,
160						"mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
161							outfile, argv[step],
162							outfile);
163					system(cmd);
164				}
165				++aflag;
166			}
167			if (!(outf = fopen(outfile, aflag ? "a" : "w")))
168				err(exit_val, "%s", outfile);
169			put_entries(head);
170			(void)fclose(outf);
171			if (uflag) {
172				(void)sprintf(cmd, "sort -o %s %s",
173						outfile, outfile);
174				system(cmd);
175			}
176		}
177	}
178	exit(exit_val);
179}
180
181static void
182usage()
183{
184	(void)fprintf(stderr, "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
185	exit(1);
186}
187
188/*
189 * init --
190 *	this routine sets up the boolean psuedo-functions which work by
191 *	setting boolean flags dependent upon the corresponding character.
192 *	Every char which is NOT in that string is false with respect to
193 *	the pseudo-function.  Therefore, all of the array "_wht" is NO
194 *	by default and then the elements subscripted by the chars in
195 *	CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
196 *	the string CWHITE, else NO.
197 */
198void
199init()
200{
201	int		i;
202	unsigned char	*sp;
203
204	for (i = 0; i < 256; i++) {
205		_wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
206		_gd[i] = YES;
207	}
208#define	CWHITE	" \f\t\n"
209	for (sp = CWHITE; *sp; sp++)	/* white space chars */
210		_wht[*sp] = YES;
211#define	CTOKEN	" \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
212	for (sp = CTOKEN; *sp; sp++)	/* token ending chars */
213		_etk[*sp] = YES;
214#define	CINTOK	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
215	for (sp = CINTOK; *sp; sp++)	/* valid in-token chars */
216		_itk[*sp] = YES;
217#define	CBEGIN	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
218	for (sp = CBEGIN; *sp; sp++)	/* token starting chars */
219		_btk[*sp] = YES;
220#define	CNOTGD	",;"
221	for (sp = CNOTGD; *sp; sp++)	/* invalid after-function chars */
222		_gd[*sp] = NO;
223}
224
225/*
226 * find_entries --
227 *	this routine opens the specified file and calls the function
228 *	which searches the file.
229 */
230void
231find_entries(file)
232	char	*file;
233{
234	char	*cp;
235
236	lineno = 0;				/* should be 1 ?? KB */
237	if ((cp = strrchr(file, '.'))) {
238		if (cp[1] == 'l' && !cp[2]) {
239			int	c;
240
241			for (;;) {
242				if (GETC(==, EOF))
243					return;
244				if (!iswhite(c)) {
245					rewind(inf);
246					break;
247				}
248			}
249#define	LISPCHR	";(["
250/* lisp */		if (strchr(LISPCHR, c)) {
251				l_entries();
252				return;
253			}
254/* lex */		else {
255				/*
256				 * we search all 3 parts of a lex file
257				 * for C references.  This may be wrong.
258				 */
259				toss_yysec();
260				(void)strcpy(lbuf, "%%$");
261				pfnote("yylex", lineno);
262				rewind(inf);
263			}
264		}
265/* yacc */	else if (cp[1] == 'y' && !cp[2]) {
266			/*
267			 * we search only the 3rd part of a yacc file
268			 * for C references.  This may be wrong.
269			 */
270			toss_yysec();
271			(void)strcpy(lbuf, "%%$");
272			pfnote("yyparse", lineno);
273			y_entries();
274		}
275/* fortran */	else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
276			if (PF_funcs())
277				return;
278			rewind(inf);
279		}
280	}
281/* C */	c_entries();
282}
283