Annotation of src/usr.bin/ctags/ctags.c, Revision 1.19
1.19 ! cheloha 1: /* $OpenBSD: ctags.c,v 1.18 2015/10/09 01:37:07 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.
1.9 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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()) */
1.14 tobias 49: bool _wht[256], _itk[256], _btk[256];
1.1 deraadt 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:
1.6 millert 66: void init(void);
67: void find_entries(char *);
1.16 semarie 68: void preload_entries(char *, int, char *[]);
1.1 deraadt 69:
70: int
1.10 deraadt 71: main(int argc, char *argv[])
1.1 deraadt 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 */
1.17 deraadt 79:
1.18 deraadt 80: if (pledge("stdio rpath wpath cpath", NULL) == -1)
81: err(1, "pledge");
1.1 deraadt 82:
83: aflag = uflag = NO;
1.3 millert 84: while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
1.1 deraadt 85: switch(ch) {
86: case 'B':
87: searchar = '?';
88: break;
89: case 'F':
90: searchar = '/';
91: break;
92: case 'a':
1.15 deraadt 93: aflag = 1;
1.1 deraadt 94: break;
95: case 'd':
1.15 deraadt 96: dflag = 1;
1.1 deraadt 97: break;
98: case 'f':
99: outfile = optarg;
100: break;
101: case 't':
1.13 millert 102: /* backwards compatibility */
1.1 deraadt 103: break;
104: case 'u':
1.15 deraadt 105: uflag = 1;
1.1 deraadt 106: break;
107: case 'w':
1.15 deraadt 108: wflag = 1;
1.1 deraadt 109: break;
110: case 'v':
1.15 deraadt 111: vflag = 1;
1.1 deraadt 112: case 'x':
1.15 deraadt 113: xflag = 1;
1.1 deraadt 114: break;
115: default:
116: goto usage;
117: }
118: argv += optind;
119: argc -= optind;
120: if (!argc) {
121: usage: (void)fprintf(stderr,
1.13 millert 122: "usage: ctags [-aBdFuvwx] [-f tagsfile] file ...\n");
1.1 deraadt 123: exit(1);
124: }
125:
126: init();
1.16 semarie 127: if (uflag && !vflag && !xflag)
128: preload_entries(outfile, argc, argv);
1.1 deraadt 129:
130: for (exit_val = step = 0; step < argc; ++step)
131: if (!(inf = fopen(argv[step], "r"))) {
132: warn("%s", argv[step]);
133: exit_val = 1;
134: }
135: else {
136: curfile = argv[step];
137: find_entries(argv[step]);
138: (void)fclose(inf);
139: }
140:
1.4 deraadt 141: if (head) {
1.1 deraadt 142: if (xflag)
143: put_entries(head);
144: else {
145: if (!(outf = fopen(outfile, aflag ? "a" : "w")))
146: err(exit_val, "%s", outfile);
147: put_entries(head);
148: (void)fclose(outf);
149: }
1.4 deraadt 150: }
1.1 deraadt 151: exit(exit_val);
152: }
153:
154: /*
155: * init --
156: * this routine sets up the boolean psuedo-functions which work by
157: * setting boolean flags dependent upon the corresponding character.
158: * Every char which is NOT in that string is false with respect to
159: * the pseudo-function. Therefore, all of the array "_wht" is NO
160: * by default and then the elements subscripted by the chars in
161: * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in
162: * the string CWHITE, else NO.
163: */
164: void
1.10 deraadt 165: init(void)
1.1 deraadt 166: {
167: int i;
168: unsigned char *sp;
169:
1.14 tobias 170: for (i = 0; i < 256; i++)
171: _wht[i] = _itk[i] = _btk[i] = NO;
1.1 deraadt 172: #define CWHITE " \f\t\n"
173: for (sp = CWHITE; *sp; sp++) /* white space chars */
174: _wht[*sp] = YES;
175: #define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
176: for (sp = CINTOK; *sp; sp++) /* valid in-token chars */
177: _itk[*sp] = YES;
178: #define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
179: for (sp = CBEGIN; *sp; sp++) /* token starting chars */
180: _btk[*sp] = YES;
181: }
182:
183: /*
184: * find_entries --
185: * this routine opens the specified file and calls the function
186: * which searches the file.
187: */
188: void
1.10 deraadt 189: find_entries(char *file)
1.1 deraadt 190: {
191: char *cp;
192:
193: lineno = 0; /* should be 1 ?? KB */
1.5 deraadt 194: if ((cp = strrchr(file, '.'))) {
1.1 deraadt 195: if (cp[1] == 'l' && !cp[2]) {
196: int c;
197:
198: for (;;) {
199: if (GETC(==, EOF))
200: return;
201: if (!iswhite(c)) {
202: rewind(inf);
203: break;
204: }
205: }
206: #define LISPCHR ";(["
207: /* lisp */ if (strchr(LISPCHR, c)) {
208: l_entries();
209: return;
210: }
211: /* lex */ else {
212: /*
213: * we search all 3 parts of a lex file
214: * for C references. This may be wrong.
215: */
216: toss_yysec();
1.7 deraadt 217: (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1 deraadt 218: pfnote("yylex", lineno);
219: rewind(inf);
220: }
221: }
222: /* yacc */ else if (cp[1] == 'y' && !cp[2]) {
223: /*
224: * we search only the 3rd part of a yacc file
225: * for C references. This may be wrong.
226: */
227: toss_yysec();
1.7 deraadt 228: (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1 deraadt 229: pfnote("yyparse", lineno);
230: y_entries();
231: }
232: /* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
233: if (PF_funcs())
234: return;
235: rewind(inf);
236: }
237: }
238: /* C */ c_entries();
1.16 semarie 239: }
240:
241: void
242: preload_entries(char *tagsfile, int argc, char *argv[])
243: {
244: FILE *fp;
245: char line[LINE_MAX];
246: char *entry = NULL;
247: char *file = NULL;
248: char *pattern = NULL;
249: char *eol;
250: int i;
251:
252: in_preload = YES;
253:
254: if ((fp = fopen(tagsfile, "r")) == NULL)
255: err(1, "preload_entries: %s", tagsfile);
256:
257: while (1) {
258: next:
259: if (fgets(line, sizeof(line), fp) == NULL)
260: break;
261:
262: if ((eol = strchr(line, '\n')) == NULL)
263: errx(1, "preload_entries: line too long");
264: *eol = '\0';
265:
266: /* extract entry */
267: entry = line;
268: if ((file = strchr(line, '\t')) == NULL)
269: errx(1, "preload_entries: couldn't parse entry: %s",
270: tagsfile);
271: *file = '\0';
272:
273: /* extract file */
274: file++;
275: if ((pattern = strchr(file, '\t')) == NULL)
276: errx(1, "preload_entries: couldn't parse filename: %s",
277: tagsfile);
278: *pattern = '\0';
279:
280: /* skip this file ? */
281: for(i = 0; i < argc; i++)
282: if (strcmp(file, argv[i]) == 0)
283: goto next;
284:
285: /* rest of string is pattern */
286: pattern++;
287:
288: /* grab searchar, and don't keep it around the pattern */
289: if ((pattern[0] == '/' || pattern[0] == '?')
290: && pattern[1] == '^') {
291:
292: i = strlen(pattern);
293: if (pattern[i-1] == pattern[0])
294: /* remove searchar at end */
295: pattern[i-1] = '\0';
296: else
297: errx(1, "preload_entries: couldn't parse "
298: "pattern: %s", tagsfile);
299:
300: /* remove searchar at begin */
301: pattern += 2;
302: }
303:
304: /* add entry */
305: if ((curfile = strdup(file)) == NULL)
306: err(1, "preload_entries: strdup");
307: (void)strlcpy(lbuf, pattern, sizeof(lbuf));
308: pfnote(entry, 0);
309: }
310: if (ferror(fp))
311: err(1, "preload_entries: fgets");
312:
313: (void)fclose(fp);
314: in_preload = NO;
1.1 deraadt 315: }