Annotation of src/usr.bin/ctags/ctags.c, Revision 1.8
1.8 ! pvalchev 1: /* $OpenBSD: ctags.c,v 1.7 2003/04/07 21:13:54 deraadt 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.8 ! pvalchev 47: static char rcsid[] = "$OpenBSD: ctags.c,v 1.7 2003/04/07 21:13:54 deraadt 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:
1.6 millert 84: void init(void);
85: void find_entries(char *);
1.1 deraadt 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 */
1.5 deraadt 98: char *cmd;
1.1 deraadt 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++) {
1.8 ! pvalchev 163: if (asprintf(&cmd,
1.5 deraadt 164: "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
1.8 ! pvalchev 165: outfile, argv[step], outfile) == -1)
1.5 deraadt 166: err(1, "out of space");
1.1 deraadt 167: system(cmd);
1.5 deraadt 168: free(cmd);
169: cmd = NULL;
1.1 deraadt 170: }
171: ++aflag;
172: }
173: if (!(outf = fopen(outfile, aflag ? "a" : "w")))
174: err(exit_val, "%s", outfile);
175: put_entries(head);
176: (void)fclose(outf);
177: if (uflag) {
1.8 ! pvalchev 178: if (asprintf(&cmd, "sort -o %s %s",
! 179: outfile, outfile) == -1)
1.5 deraadt 180: err(1, "out of space");
1.1 deraadt 181: system(cmd);
1.5 deraadt 182: free(cmd);
183: cmd = NULL;
1.1 deraadt 184: }
185: }
1.4 deraadt 186: }
1.1 deraadt 187: exit(exit_val);
188: }
189:
190: /*
191: * init --
192: * this routine sets up the boolean psuedo-functions which work by
193: * setting boolean flags dependent upon the corresponding character.
194: * Every char which is NOT in that string is false with respect to
195: * the pseudo-function. Therefore, all of the array "_wht" is NO
196: * by default and then the elements subscripted by the chars in
197: * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in
198: * the string CWHITE, else NO.
199: */
200: void
201: init()
202: {
203: int i;
204: unsigned char *sp;
205:
206: for (i = 0; i < 256; i++) {
207: _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
208: _gd[i] = YES;
209: }
210: #define CWHITE " \f\t\n"
211: for (sp = CWHITE; *sp; sp++) /* white space chars */
212: _wht[*sp] = YES;
213: #define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
214: for (sp = CTOKEN; *sp; sp++) /* token ending chars */
215: _etk[*sp] = YES;
216: #define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
217: for (sp = CINTOK; *sp; sp++) /* valid in-token chars */
218: _itk[*sp] = YES;
219: #define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
220: for (sp = CBEGIN; *sp; sp++) /* token starting chars */
221: _btk[*sp] = YES;
222: #define CNOTGD ",;"
223: for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */
224: _gd[*sp] = NO;
225: }
226:
227: /*
228: * find_entries --
229: * this routine opens the specified file and calls the function
230: * which searches the file.
231: */
232: void
233: find_entries(file)
234: char *file;
235: {
236: char *cp;
237:
238: lineno = 0; /* should be 1 ?? KB */
1.5 deraadt 239: if ((cp = strrchr(file, '.'))) {
1.1 deraadt 240: if (cp[1] == 'l' && !cp[2]) {
241: int c;
242:
243: for (;;) {
244: if (GETC(==, EOF))
245: return;
246: if (!iswhite(c)) {
247: rewind(inf);
248: break;
249: }
250: }
251: #define LISPCHR ";(["
252: /* lisp */ if (strchr(LISPCHR, c)) {
253: l_entries();
254: return;
255: }
256: /* lex */ else {
257: /*
258: * we search all 3 parts of a lex file
259: * for C references. This may be wrong.
260: */
261: toss_yysec();
1.7 deraadt 262: (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1 deraadt 263: pfnote("yylex", lineno);
264: rewind(inf);
265: }
266: }
267: /* yacc */ else if (cp[1] == 'y' && !cp[2]) {
268: /*
269: * we search only the 3rd part of a yacc file
270: * for C references. This may be wrong.
271: */
272: toss_yysec();
1.7 deraadt 273: (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1 deraadt 274: pfnote("yyparse", lineno);
275: y_entries();
276: }
277: /* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
278: if (PF_funcs())
279: return;
280: rewind(inf);
281: }
282: }
283: /* C */ c_entries();
284: }