Annotation of src/usr.bin/ctags/ctags.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: ctags.c,v 1.3 1997/01/15 23:42:23 millert Exp $ */
1.1 deraadt 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. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95";
46: #endif
1.4 ! deraadt 47: static char rcsid[] = "$OpenBSD: ctags.c,v 1.3 1997/01/15 23:42:23 millert Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <err.h>
51: #include <limits.h>
52: #include <stdio.h>
53: #include <string.h>
54: #include <stdlib.h>
55: #include <unistd.h>
56:
57: #include "ctags.h"
58:
59: /*
60: * ctags: create a tags file
61: */
62:
63: NODE *head; /* head of the sorted binary tree */
64:
65: /* boolean "func" (see init()) */
66: bool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
67:
68: FILE *inf; /* ioptr for current input file */
69: FILE *outf; /* ioptr for tags file */
70:
71: long lineftell; /* ftell after getc( inf ) == '\n' */
72:
73: int lineno; /* line number of current line */
74: int dflag; /* -d: non-macro defines */
75: int tflag; /* -t: create tags for typedefs */
76: int vflag; /* -v: vgrind style index output */
77: int wflag; /* -w: suppress warnings */
78: int xflag; /* -x: cxref style output */
79:
80: char *curfile; /* current input file name */
81: char searchar = '/'; /* use /.../ searches by default */
82: char lbuf[LINE_MAX];
83:
84: void init __P((void));
85: void find_entries __P((char *));
86:
87: int
88: main(argc, argv)
89: int argc;
90: char **argv;
91: {
92: static 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 */
98: char cmd[100]; /* too ugly to explain */
99:
100: aflag = uflag = NO;
1.3 millert 101: while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
1.1 deraadt 102: switch(ch) {
103: case 'B':
104: searchar = '?';
105: break;
106: case 'F':
107: searchar = '/';
108: break;
109: case 'a':
110: aflag++;
111: break;
112: case 'd':
113: dflag++;
114: break;
115: case 'f':
116: outfile = optarg;
117: break;
118: case 't':
119: tflag++;
120: break;
121: case 'u':
122: uflag++;
123: break;
124: case 'w':
125: wflag++;
126: break;
127: case 'v':
128: vflag++;
129: case 'x':
130: xflag++;
131: break;
132: case '?':
133: default:
134: goto usage;
135: }
136: argv += optind;
137: argc -= optind;
138: if (!argc) {
139: usage: (void)fprintf(stderr,
140: "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
141: exit(1);
142: }
143:
144: init();
145:
146: for (exit_val = step = 0; step < argc; ++step)
147: if (!(inf = fopen(argv[step], "r"))) {
148: warn("%s", argv[step]);
149: exit_val = 1;
150: }
151: else {
152: curfile = argv[step];
153: find_entries(argv[step]);
154: (void)fclose(inf);
155: }
156:
1.4 ! deraadt 157: if (head) {
1.1 deraadt 158: if (xflag)
159: put_entries(head);
160: else {
161: if (uflag) {
162: for (step = 0; step < argc; step++) {
163: (void)sprintf(cmd,
164: "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
165: outfile, argv[step],
166: outfile);
167: system(cmd);
168: }
169: ++aflag;
170: }
171: if (!(outf = fopen(outfile, aflag ? "a" : "w")))
172: err(exit_val, "%s", outfile);
173: put_entries(head);
174: (void)fclose(outf);
175: if (uflag) {
176: (void)sprintf(cmd, "sort -o %s %s",
177: outfile, outfile);
178: system(cmd);
179: }
180: }
1.4 ! deraadt 181: }
1.1 deraadt 182: exit(exit_val);
183: }
184:
185: /*
186: * init --
187: * this routine sets up the boolean psuedo-functions which work by
188: * setting boolean flags dependent upon the corresponding character.
189: * Every char which is NOT in that string is false with respect to
190: * the pseudo-function. Therefore, all of the array "_wht" is NO
191: * by default and then the elements subscripted by the chars in
192: * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in
193: * the string CWHITE, else NO.
194: */
195: void
196: init()
197: {
198: int i;
199: unsigned char *sp;
200:
201: for (i = 0; i < 256; i++) {
202: _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
203: _gd[i] = YES;
204: }
205: #define CWHITE " \f\t\n"
206: for (sp = CWHITE; *sp; sp++) /* white space chars */
207: _wht[*sp] = YES;
208: #define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
209: for (sp = CTOKEN; *sp; sp++) /* token ending chars */
210: _etk[*sp] = YES;
211: #define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
212: for (sp = CINTOK; *sp; sp++) /* valid in-token chars */
213: _itk[*sp] = YES;
214: #define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
215: for (sp = CBEGIN; *sp; sp++) /* token starting chars */
216: _btk[*sp] = YES;
217: #define CNOTGD ",;"
218: for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */
219: _gd[*sp] = NO;
220: }
221:
222: /*
223: * find_entries --
224: * this routine opens the specified file and calls the function
225: * which searches the file.
226: */
227: void
228: find_entries(file)
229: char *file;
230: {
231: char *cp;
232:
233: lineno = 0; /* should be 1 ?? KB */
234: if (cp = strrchr(file, '.')) {
235: if (cp[1] == 'l' && !cp[2]) {
236: int c;
237:
238: for (;;) {
239: if (GETC(==, EOF))
240: return;
241: if (!iswhite(c)) {
242: rewind(inf);
243: break;
244: }
245: }
246: #define LISPCHR ";(["
247: /* lisp */ if (strchr(LISPCHR, c)) {
248: l_entries();
249: return;
250: }
251: /* lex */ else {
252: /*
253: * we search all 3 parts of a lex file
254: * for C references. This may be wrong.
255: */
256: toss_yysec();
257: (void)strcpy(lbuf, "%%$");
258: pfnote("yylex", lineno);
259: rewind(inf);
260: }
261: }
262: /* yacc */ else if (cp[1] == 'y' && !cp[2]) {
263: /*
264: * we search only the 3rd part of a yacc file
265: * for C references. This may be wrong.
266: */
267: toss_yysec();
268: (void)strcpy(lbuf, "%%$");
269: pfnote("yyparse", lineno);
270: y_entries();
271: }
272: /* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
273: if (PF_funcs())
274: return;
275: rewind(inf);
276: }
277: }
278: /* C */ c_entries();
279: }