Remove __P macros from src/usr.bin and src/usr.sbin.
[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.4 2003/11/03 19:31:29 eirikn 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(void);
74 void    find_entries(char *);
75 static void usage(void);
76
77 int
78 main(int argc, char **argv)
79 {
80         static char     *outfile = "tags";      /* output file */
81         int     aflag;                          /* -a: append to tags */
82         int     uflag;                          /* -u: update tags */
83         int     exit_val;                       /* exit value */
84         int     step;                           /* step through args */
85         int     ch;                             /* getopts char */
86         char    cmd[100];                       /* too ugly to explain */
87
88         aflag = uflag = NO;
89         while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
90                 switch(ch) {
91                 case 'B':
92                         searchar = '?';
93                         break;
94                 case 'F':
95                         searchar = '/';
96                         break;
97                 case 'a':
98                         aflag++;
99                         break;
100                 case 'd':
101                         dflag++;
102                         break;
103                 case 'f':
104                         outfile = optarg;
105                         break;
106                 case 't':
107                         tflag++;
108                         break;
109                 case 'u':
110                         uflag++;
111                         break;
112                 case 'w':
113                         wflag++;
114                         break;
115                 case 'v':
116                         vflag++;
117                 case 'x':
118                         xflag++;
119                         break;
120                 case '?':
121                 default:
122                         usage();
123                 }
124         argv += optind;
125         argc -= optind;
126         if (!argc)
127                 usage();
128
129         init();
130
131         for (exit_val = step = 0; step < argc; ++step)
132                 if (!(inf = fopen(argv[step], "r"))) {
133                         warn("%s", argv[step]);
134                         exit_val = 1;
135                 }
136                 else {
137                         curfile = argv[step];
138                         find_entries(argv[step]);
139                         (void)fclose(inf);
140                 }
141
142         if (head) {
143                 if (xflag)
144                         put_entries(head);
145                 else {
146                         if (uflag) {
147                                 for (step = 0; step < argc; step++) {
148                                         (void)sprintf(cmd,
149                                                 "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
150                                                         outfile, argv[step],
151                                                         outfile);
152                                         system(cmd);
153                                 }
154                                 ++aflag;
155                         }
156                         if (!(outf = fopen(outfile, aflag ? "a" : "w")))
157                                 err(exit_val, "%s", outfile);
158                         put_entries(head);
159                         (void)fclose(outf);
160                         if (uflag) {
161                                 (void)sprintf(cmd, "sort -o %s %s",
162                                                 outfile, outfile);
163                                 system(cmd);
164                         }
165                 }
166         }
167         exit(exit_val);
168 }
169
170 static void
171 usage(void)
172 {
173         (void)fprintf(stderr, "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
174         exit(1);
175 }
176
177 /*
178  * init --
179  *      this routine sets up the boolean psuedo-functions which work by
180  *      setting boolean flags dependent upon the corresponding character.
181  *      Every char which is NOT in that string is false with respect to
182  *      the pseudo-function.  Therefore, all of the array "_wht" is NO
183  *      by default and then the elements subscripted by the chars in
184  *      CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
185  *      the string CWHITE, else NO.
186  */
187 void
188 init(void)
189 {
190         int             i;
191         unsigned char   *sp;
192
193         for (i = 0; i < 256; i++) {
194                 _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
195                 _gd[i] = YES;
196         }
197 #define CWHITE  " \f\t\n"
198         for (sp = CWHITE; *sp; sp++)    /* white space chars */
199                 _wht[*sp] = YES;
200 #define CTOKEN  " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
201         for (sp = CTOKEN; *sp; sp++)    /* token ending chars */
202                 _etk[*sp] = YES;
203 #define CINTOK  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
204         for (sp = CINTOK; *sp; sp++)    /* valid in-token chars */
205                 _itk[*sp] = YES;
206 #define CBEGIN  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
207         for (sp = CBEGIN; *sp; sp++)    /* token starting chars */
208                 _btk[*sp] = YES;
209 #define CNOTGD  ",;"
210         for (sp = CNOTGD; *sp; sp++)    /* invalid after-function chars */
211                 _gd[*sp] = NO;
212 }
213
214 /*
215  * find_entries --
216  *      this routine opens the specified file and calls the function
217  *      which searches the file.
218  */
219 void
220 find_entries(char *file)
221 {
222         char    *cp;
223
224         lineno = 0;                             /* should be 1 ?? KB */
225         if ((cp = strrchr(file, '.'))) {
226                 if (cp[1] == 'l' && !cp[2]) {
227                         int     c;
228
229                         for (;;) {
230                                 if (GETC(==, EOF))
231                                         return;
232                                 if (!iswhite(c)) {
233                                         rewind(inf);
234                                         break;
235                                 }
236                         }
237 #define LISPCHR ";(["
238 /* lisp */              if (strchr(LISPCHR, c)) {
239                                 l_entries();
240                                 return;
241                         }
242 /* lex */               else {
243                                 /*
244                                  * we search all 3 parts of a lex file
245                                  * for C references.  This may be wrong.
246                                  */
247                                 toss_yysec();
248                                 (void)strcpy(lbuf, "%%$");
249                                 pfnote("yylex", lineno);
250                                 rewind(inf);
251                         }
252                 }
253 /* yacc */      else if (cp[1] == 'y' && !cp[2]) {
254                         /*
255                          * we search only the 3rd part of a yacc file
256                          * for C references.  This may be wrong.
257                          */
258                         toss_yysec();
259                         (void)strcpy(lbuf, "%%$");
260                         pfnote("yyparse", lineno);
261                         y_entries();
262                 }
263 /* fortran */   else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
264                         if (PF_funcs())
265                                 return;
266                         rewind(inf);
267                 }
268         }
269 /* C */ c_entries();
270 }