Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / ctags / ctags.c
1 /*
2  * Copyright (c) 1987, 1993, 1994, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static const char sccsid[] = "@(#)ctags.c       8.4 (Berkeley) 2/7/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/usr.bin/ctags/ctags.c,v 1.7.2.1 2001/09/18 04:16:53 mikeh Exp $";
46 #endif /* not lint */
47
48 #include <err.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54
55 #include "ctags.h"
56
57 /*
58  * ctags: create a tags file
59  */
60
61 NODE    *head;                  /* head of the sorted binary tree */
62
63                                 /* boolean "func" (see init()) */
64 bool    _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
65
66 FILE    *inf;                   /* ioptr for current input file */
67 FILE    *outf;                  /* ioptr for tags file */
68
69 long    lineftell;              /* ftell after getc( inf ) == '\n' */
70
71 int     lineno;                 /* line number of current line */
72 int     dflag;                  /* -d: non-macro defines */
73 int     tflag;                  /* -t: create tags for typedefs */
74 int     vflag;                  /* -v: vgrind style index output */
75 int     wflag;                  /* -w: suppress warnings */
76 int     xflag;                  /* -x: cxref style output */
77
78 char    *curfile;               /* current input file name */
79 char    searchar = '/';         /* use /.../ searches by default */
80 char    lbuf[LINE_MAX];
81
82 void    init __P((void));
83 void    find_entries __P((char *));
84 static void usage __P((void));
85
86 int
87 main(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
181 static void
182 usage()
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  */
198 void
199 init()
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  */
230 void
231 find_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 }