Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * @(#) Copyright (c) 1987, 1993, 1994, 1995 The Regents of the University of California.  All rights reserved.
34  * @(#)ctags.c  8.4 (Berkeley) 2/7/95
35  * $FreeBSD: src/usr.bin/ctags/ctags.c,v 1.7.2.1 2001/09/18 04:16:53 mikeh Exp $
36  * $DragonFly: src/usr.bin/ctags/ctags.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
37  */
38
39 #include <err.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #include "ctags.h"
47
48 /*
49  * ctags: create a tags file
50  */
51
52 NODE    *head;                  /* head of the sorted binary tree */
53
54                                 /* boolean "func" (see init()) */
55 bool    _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
56
57 FILE    *inf;                   /* ioptr for current input file */
58 FILE    *outf;                  /* ioptr for tags file */
59
60 long    lineftell;              /* ftell after getc( inf ) == '\n' */
61
62 int     lineno;                 /* line number of current line */
63 int     dflag;                  /* -d: non-macro defines */
64 int     tflag;                  /* -t: create tags for typedefs */
65 int     vflag;                  /* -v: vgrind style index output */
66 int     wflag;                  /* -w: suppress warnings */
67 int     xflag;                  /* -x: cxref style output */
68
69 char    *curfile;               /* current input file name */
70 char    searchar = '/';         /* use /.../ searches by default */
71 char    lbuf[LINE_MAX];
72
73 void    init __P((void));
74 void    find_entries __P((char *));
75 static void usage __P((void));
76
77 int
78 main(argc, argv)
79         int     argc;
80         char    **argv;
81 {
82         static char     *outfile = "tags";      /* output file */
83         int     aflag;                          /* -a: append to tags */
84         int     uflag;                          /* -u: update tags */
85         int     exit_val;                       /* exit value */
86         int     step;                           /* step through args */
87         int     ch;                             /* getopts char */
88         char    cmd[100];                       /* too ugly to explain */
89
90         aflag = uflag = NO;
91         while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
92                 switch(ch) {
93                 case 'B':
94                         searchar = '?';
95                         break;
96                 case 'F':
97                         searchar = '/';
98                         break;
99                 case 'a':
100                         aflag++;
101                         break;
102                 case 'd':
103                         dflag++;
104                         break;
105                 case 'f':
106                         outfile = optarg;
107                         break;
108                 case 't':
109                         tflag++;
110                         break;
111                 case 'u':
112                         uflag++;
113                         break;
114                 case 'w':
115                         wflag++;
116                         break;
117                 case 'v':
118                         vflag++;
119                 case 'x':
120                         xflag++;
121                         break;
122                 case '?':
123                 default:
124                         usage();
125                 }
126         argv += optind;
127         argc -= optind;
128         if (!argc)
129                 usage();
130
131         init();
132
133         for (exit_val = step = 0; step < argc; ++step)
134                 if (!(inf = fopen(argv[step], "r"))) {
135                         warn("%s", argv[step]);
136                         exit_val = 1;
137                 }
138                 else {
139                         curfile = argv[step];
140                         find_entries(argv[step]);
141                         (void)fclose(inf);
142                 }
143
144         if (head) {
145                 if (xflag)
146                         put_entries(head);
147                 else {
148                         if (uflag) {
149                                 for (step = 0; step < argc; step++) {
150                                         (void)sprintf(cmd,
151                                                 "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
152                                                         outfile, argv[step],
153                                                         outfile);
154                                         system(cmd);
155                                 }
156                                 ++aflag;
157                         }
158                         if (!(outf = fopen(outfile, aflag ? "a" : "w")))
159                                 err(exit_val, "%s", outfile);
160                         put_entries(head);
161                         (void)fclose(outf);
162                         if (uflag) {
163                                 (void)sprintf(cmd, "sort -o %s %s",
164                                                 outfile, outfile);
165                                 system(cmd);
166                         }
167                 }
168         }
169         exit(exit_val);
170 }
171
172 static void
173 usage()
174 {
175         (void)fprintf(stderr, "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
176         exit(1);
177 }
178
179 /*
180  * init --
181  *      this routine sets up the boolean psuedo-functions which work by
182  *      setting boolean flags dependent upon the corresponding character.
183  *      Every char which is NOT in that string is false with respect to
184  *      the pseudo-function.  Therefore, all of the array "_wht" is NO
185  *      by default and then the elements subscripted by the chars in
186  *      CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
187  *      the string CWHITE, else NO.
188  */
189 void
190 init()
191 {
192         int             i;
193         unsigned char   *sp;
194
195         for (i = 0; i < 256; i++) {
196                 _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
197                 _gd[i] = YES;
198         }
199 #define CWHITE  " \f\t\n"
200         for (sp = CWHITE; *sp; sp++)    /* white space chars */
201                 _wht[*sp] = YES;
202 #define CTOKEN  " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
203         for (sp = CTOKEN; *sp; sp++)    /* token ending chars */
204                 _etk[*sp] = YES;
205 #define CINTOK  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
206         for (sp = CINTOK; *sp; sp++)    /* valid in-token chars */
207                 _itk[*sp] = YES;
208 #define CBEGIN  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
209         for (sp = CBEGIN; *sp; sp++)    /* token starting chars */
210                 _btk[*sp] = YES;
211 #define CNOTGD  ",;"
212         for (sp = CNOTGD; *sp; sp++)    /* invalid after-function chars */
213                 _gd[*sp] = NO;
214 }
215
216 /*
217  * find_entries --
218  *      this routine opens the specified file and calls the function
219  *      which searches the file.
220  */
221 void
222 find_entries(file)
223         char    *file;
224 {
225         char    *cp;
226
227         lineno = 0;                             /* should be 1 ?? KB */
228         if ((cp = strrchr(file, '.'))) {
229                 if (cp[1] == 'l' && !cp[2]) {
230                         int     c;
231
232                         for (;;) {
233                                 if (GETC(==, EOF))
234                                         return;
235                                 if (!iswhite(c)) {
236                                         rewind(inf);
237                                         break;
238                                 }
239                         }
240 #define LISPCHR ";(["
241 /* lisp */              if (strchr(LISPCHR, c)) {
242                                 l_entries();
243                                 return;
244                         }
245 /* lex */               else {
246                                 /*
247                                  * we search all 3 parts of a lex file
248                                  * for C references.  This may be wrong.
249                                  */
250                                 toss_yysec();
251                                 (void)strcpy(lbuf, "%%$");
252                                 pfnote("yylex", lineno);
253                                 rewind(inf);
254                         }
255                 }
256 /* yacc */      else if (cp[1] == 'y' && !cp[2]) {
257                         /*
258                          * we search only the 3rd part of a yacc file
259                          * for C references.  This may be wrong.
260                          */
261                         toss_yysec();
262                         (void)strcpy(lbuf, "%%$");
263                         pfnote("yyparse", lineno);
264                         y_entries();
265                 }
266 /* fortran */   else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
267                         if (PF_funcs())
268                                 return;
269                         rewind(inf);
270                 }
271         }
272 /* C */ c_entries();
273 }