ctags.c revision 87215
11590Srgrimes/*
227097Scharnier * Copyright (c) 1987, 1993, 1994, 1995
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3541568Sarchiestatic const char copyright[] =
3627097Scharnier"@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4181782Smikeh#if 0
4241568Sarchiestatic const char sccsid[] = "@(#)ctags.c	8.4 (Berkeley) 2/7/95";
4381782Smikeh#endif
4481782Smikehstatic const char rcsid[] =
4581782Smikeh  "$FreeBSD: head/usr.bin/ctags/ctags.c 87215 2001-12-02 13:36:14Z markm $";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes#include <err.h>
491590Srgrimes#include <limits.h>
501590Srgrimes#include <stdio.h>
511590Srgrimes#include <string.h>
521590Srgrimes#include <stdlib.h>
531590Srgrimes#include <unistd.h>
541590Srgrimes
551590Srgrimes#include "ctags.h"
561590Srgrimes
571590Srgrimes/*
581590Srgrimes * ctags: create a tags file
591590Srgrimes */
601590Srgrimes
611590SrgrimesNODE	*head;			/* head of the sorted binary tree */
621590Srgrimes
631590Srgrimes				/* boolean "func" (see init()) */
641590Srgrimesbool	_wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
651590Srgrimes
661590SrgrimesFILE	*inf;			/* ioptr for current input file */
671590SrgrimesFILE	*outf;			/* ioptr for tags file */
681590Srgrimes
691590Srgrimeslong	lineftell;		/* ftell after getc( inf ) == '\n' */
701590Srgrimes
711590Srgrimesint	lineno;			/* line number of current line */
721590Srgrimesint	dflag;			/* -d: non-macro defines */
731590Srgrimesint	tflag;			/* -t: create tags for typedefs */
741590Srgrimesint	vflag;			/* -v: vgrind style index output */
751590Srgrimesint	wflag;			/* -w: suppress warnings */
761590Srgrimesint	xflag;			/* -x: cxref style output */
771590Srgrimes
781590Srgrimeschar	*curfile;		/* current input file name */
791590Srgrimeschar	searchar = '/';		/* use /.../ searches by default */
801590Srgrimeschar	lbuf[LINE_MAX];
811590Srgrimes
821590Srgrimesvoid	init __P((void));
831590Srgrimesvoid	find_entries __P((char *));
8427097Scharnierstatic void usage __P((void));
851590Srgrimes
861590Srgrimesint
871590Srgrimesmain(argc, argv)
881590Srgrimes	int	argc;
891590Srgrimes	char	**argv;
901590Srgrimes{
9187215Smarkm	static const char	*outfile = "tags";	/* output file */
921590Srgrimes	int	aflag;				/* -a: append to tags */
931590Srgrimes	int	uflag;				/* -u: update tags */
941590Srgrimes	int	exit_val;			/* exit value */
951590Srgrimes	int	step;				/* step through args */
961590Srgrimes	int	ch;				/* getopts char */
971590Srgrimes	char	cmd[100];			/* too ugly to explain */
981590Srgrimes
991590Srgrimes	aflag = uflag = NO;
10024360Simp	while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
1011590Srgrimes		switch(ch) {
1021590Srgrimes		case 'B':
1031590Srgrimes			searchar = '?';
1041590Srgrimes			break;
1051590Srgrimes		case 'F':
1061590Srgrimes			searchar = '/';
1071590Srgrimes			break;
1081590Srgrimes		case 'a':
1091590Srgrimes			aflag++;
1101590Srgrimes			break;
1111590Srgrimes		case 'd':
1121590Srgrimes			dflag++;
1131590Srgrimes			break;
1141590Srgrimes		case 'f':
1151590Srgrimes			outfile = optarg;
1161590Srgrimes			break;
1171590Srgrimes		case 't':
1181590Srgrimes			tflag++;
1191590Srgrimes			break;
1201590Srgrimes		case 'u':
1211590Srgrimes			uflag++;
1221590Srgrimes			break;
1231590Srgrimes		case 'w':
1241590Srgrimes			wflag++;
1251590Srgrimes			break;
1261590Srgrimes		case 'v':
1271590Srgrimes			vflag++;
1281590Srgrimes		case 'x':
1291590Srgrimes			xflag++;
1301590Srgrimes			break;
1311590Srgrimes		case '?':
1321590Srgrimes		default:
13327097Scharnier			usage();
1341590Srgrimes		}
1351590Srgrimes	argv += optind;
1361590Srgrimes	argc -= optind;
13727097Scharnier	if (!argc)
13827097Scharnier		usage();
1391590Srgrimes
1401590Srgrimes	init();
1411590Srgrimes
1421590Srgrimes	for (exit_val = step = 0; step < argc; ++step)
1431590Srgrimes		if (!(inf = fopen(argv[step], "r"))) {
1441590Srgrimes			warn("%s", argv[step]);
1451590Srgrimes			exit_val = 1;
1461590Srgrimes		}
1471590Srgrimes		else {
1481590Srgrimes			curfile = argv[step];
1491590Srgrimes			find_entries(argv[step]);
1501590Srgrimes			(void)fclose(inf);
1511590Srgrimes		}
1521590Srgrimes
15348465Sbillf	if (head) {
1541590Srgrimes		if (xflag)
1551590Srgrimes			put_entries(head);
1561590Srgrimes		else {
1571590Srgrimes			if (uflag) {
1581590Srgrimes				for (step = 0; step < argc; step++) {
1591590Srgrimes					(void)sprintf(cmd,
1601590Srgrimes						"mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
1611590Srgrimes							outfile, argv[step],
1621590Srgrimes							outfile);
1631590Srgrimes					system(cmd);
1641590Srgrimes				}
1651590Srgrimes				++aflag;
1661590Srgrimes			}
1671590Srgrimes			if (!(outf = fopen(outfile, aflag ? "a" : "w")))
1681590Srgrimes				err(exit_val, "%s", outfile);
1691590Srgrimes			put_entries(head);
1701590Srgrimes			(void)fclose(outf);
1711590Srgrimes			if (uflag) {
1721590Srgrimes				(void)sprintf(cmd, "sort -o %s %s",
1731590Srgrimes						outfile, outfile);
1741590Srgrimes				system(cmd);
1751590Srgrimes			}
1761590Srgrimes		}
17748465Sbillf	}
1781590Srgrimes	exit(exit_val);
1791590Srgrimes}
1801590Srgrimes
18127097Scharnierstatic void
18227097Scharnierusage()
18327097Scharnier{
18427097Scharnier	(void)fprintf(stderr, "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
18527097Scharnier	exit(1);
18627097Scharnier}
18727097Scharnier
1881590Srgrimes/*
1891590Srgrimes * init --
1901590Srgrimes *	this routine sets up the boolean psuedo-functions which work by
1911590Srgrimes *	setting boolean flags dependent upon the corresponding character.
1921590Srgrimes *	Every char which is NOT in that string is false with respect to
1931590Srgrimes *	the pseudo-function.  Therefore, all of the array "_wht" is NO
1941590Srgrimes *	by default and then the elements subscripted by the chars in
1951590Srgrimes *	CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
1961590Srgrimes *	the string CWHITE, else NO.
1971590Srgrimes */
1981590Srgrimesvoid
1991590Srgrimesinit()
2001590Srgrimes{
2011590Srgrimes	int		i;
20287215Smarkm	const unsigned char	*sp;
2031590Srgrimes
2041590Srgrimes	for (i = 0; i < 256; i++) {
2051590Srgrimes		_wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
2061590Srgrimes		_gd[i] = YES;
2071590Srgrimes	}
2081590Srgrimes#define	CWHITE	" \f\t\n"
2091590Srgrimes	for (sp = CWHITE; *sp; sp++)	/* white space chars */
2101590Srgrimes		_wht[*sp] = YES;
2111590Srgrimes#define	CTOKEN	" \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
2121590Srgrimes	for (sp = CTOKEN; *sp; sp++)	/* token ending chars */
2131590Srgrimes		_etk[*sp] = YES;
2141590Srgrimes#define	CINTOK	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
2151590Srgrimes	for (sp = CINTOK; *sp; sp++)	/* valid in-token chars */
2161590Srgrimes		_itk[*sp] = YES;
2171590Srgrimes#define	CBEGIN	"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
2181590Srgrimes	for (sp = CBEGIN; *sp; sp++)	/* token starting chars */
2191590Srgrimes		_btk[*sp] = YES;
2201590Srgrimes#define	CNOTGD	",;"
2211590Srgrimes	for (sp = CNOTGD; *sp; sp++)	/* invalid after-function chars */
2221590Srgrimes		_gd[*sp] = NO;
2231590Srgrimes}
2241590Srgrimes
2251590Srgrimes/*
2261590Srgrimes * find_entries --
2271590Srgrimes *	this routine opens the specified file and calls the function
2281590Srgrimes *	which searches the file.
2291590Srgrimes */
2301590Srgrimesvoid
2311590Srgrimesfind_entries(file)
2321590Srgrimes	char	*file;
2331590Srgrimes{
2341590Srgrimes	char	*cp;
2351590Srgrimes
2361590Srgrimes	lineno = 0;				/* should be 1 ?? KB */
23732069Salex	if ((cp = strrchr(file, '.'))) {
2381590Srgrimes		if (cp[1] == 'l' && !cp[2]) {
2391590Srgrimes			int	c;
2401590Srgrimes
2411590Srgrimes			for (;;) {
2421590Srgrimes				if (GETC(==, EOF))
2431590Srgrimes					return;
2441590Srgrimes				if (!iswhite(c)) {
2451590Srgrimes					rewind(inf);
2461590Srgrimes					break;
2471590Srgrimes				}
2481590Srgrimes			}
2491590Srgrimes#define	LISPCHR	";(["
2501590Srgrimes/* lisp */		if (strchr(LISPCHR, c)) {
2511590Srgrimes				l_entries();
2521590Srgrimes				return;
2531590Srgrimes			}
2541590Srgrimes/* lex */		else {
2551590Srgrimes				/*
2561590Srgrimes				 * we search all 3 parts of a lex file
2571590Srgrimes				 * for C references.  This may be wrong.
2581590Srgrimes				 */
2591590Srgrimes				toss_yysec();
2601590Srgrimes				(void)strcpy(lbuf, "%%$");
2611590Srgrimes				pfnote("yylex", lineno);
2621590Srgrimes				rewind(inf);
2631590Srgrimes			}
2641590Srgrimes		}
2651590Srgrimes/* yacc */	else if (cp[1] == 'y' && !cp[2]) {
2661590Srgrimes			/*
2671590Srgrimes			 * we search only the 3rd part of a yacc file
2681590Srgrimes			 * for C references.  This may be wrong.
2691590Srgrimes			 */
2701590Srgrimes			toss_yysec();
2711590Srgrimes			(void)strcpy(lbuf, "%%$");
2721590Srgrimes			pfnote("yyparse", lineno);
2731590Srgrimes			y_entries();
2741590Srgrimes		}
2751590Srgrimes/* fortran */	else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
2761590Srgrimes			if (PF_funcs())
2771590Srgrimes				return;
2781590Srgrimes			rewind(inf);
2791590Srgrimes		}
2801590Srgrimes	}
2811590Srgrimes/* C */	c_entries();
2821590Srgrimes}
283