1 /* $NetBSD: ctags.c,v 1.11 2005/02/17 17:29:58 xtraeme Exp $ */
4 * Copyright (c) 1987, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__COPYRIGHT) && !defined(lint)
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\
39 The Regents of the University of California. All rights reserved.");
42 #if defined(__RCSID) && !defined(lint)
44 static char sccsid
[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95";
46 __RCSID("$NetBSD: ctags.c,v 1.11 2005/02/17 17:29:58 xtraeme Exp $");
59 * ctags: create a tags file
62 NODE
*head
; /* head of the sorted binary tree */
64 /* boolean "func" (see init()) */
65 bool _wht
[256], _etk
[256], _itk
[256], _btk
[256], _gd
[256];
67 FILE *inf
; /* ioptr for current input file */
68 FILE *outf
; /* ioptr for tags file */
70 long lineftell
; /* ftell after getc( inf ) == '\n' */
72 int lineno
; /* line number of current line */
73 int dflag
; /* -d: non-macro defines */
74 int tflag
; /* -t: create tags for typedefs */
75 int vflag
; /* -v: vgrind style index output */
76 int wflag
; /* -w: suppress warnings */
77 int xflag
; /* -x: cxref style output */
79 char *curfile
; /* current input file name */
80 char searchar
= '/'; /* use /.../ searches by default */
84 void find_entries(char *);
87 main(int argc
, char **argv
)
89 static const char *outfile
= "tags"; /* output file */
90 int aflag
; /* -a: append to tags */
91 int uflag
; /* -u: update tags */
92 int exit_val
; /* exit value */
93 int step
; /* step through args */
94 int ch
; /* getopts char */
95 char cmd
[100]; /* too ugly to explain */
98 while ((ch
= getopt(argc
, argv
, "BFadf:tuwvx")) != -1)
136 usage
: (void)fprintf(stderr
,
137 "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
143 for (exit_val
= step
= 0; step
< argc
; ++step
)
144 if (!(inf
= fopen(argv
[step
], "r"))) {
145 warn("%s", argv
[step
]);
149 curfile
= argv
[step
];
150 find_entries(argv
[step
]);
159 for (step
= 0; step
< argc
; step
++) {
160 (void)snprintf(cmd
, sizeof(cmd
),
161 "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
168 if (!(outf
= fopen(outfile
, aflag
? "a" : "w")))
169 err(exit_val
, "%s", outfile
);
173 (void)snprintf(cmd
, sizeof(cmd
),
174 "sort -o %s %s", outfile
, outfile
);
184 * this routine sets up the boolean psuedo-functions which work by
185 * setting boolean flags dependent upon the corresponding character.
186 * Every char which is NOT in that string is false with respect to
187 * the pseudo-function. Therefore, all of the array "_wht" is NO
188 * by default and then the elements subscripted by the chars in
189 * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in
190 * the string CWHITE, else NO.
196 unsigned const char *sp
;
198 for (i
= 0; i
< 256; i
++) {
199 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = NO
;
202 #define CWHITE " \f\t\n"
203 for (sp
= CWHITE
; *sp
; sp
++) /* white space chars */
205 #define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
206 for (sp
= CTOKEN
; *sp
; sp
++) /* token ending chars */
208 #define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
209 for (sp
= CINTOK
; *sp
; sp
++) /* valid in-token chars */
211 #define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
212 for (sp
= CBEGIN
; *sp
; sp
++) /* token starting chars */
215 for (sp
= CNOTGD
; *sp
; sp
++) /* invalid after-function chars */
221 * this routine opens the specified file and calls the function
222 * which searches the file.
225 find_entries(char *file
)
229 lineno
= 0; /* should be 1 ?? KB */
230 if ((cp
= strrchr(file
, '.')) != NULL
) {
231 if (cp
[1] == 'l' && !cp
[2]) {
242 #define LISPCHR ";(["
243 /* lisp */ if (strchr(LISPCHR
, c
)) {
249 * we search all 3 parts of a lex file
250 * for C references. This may be wrong.
253 (void)strlcpy(lbuf
, "%%$", sizeof(lbuf
));
254 pfnote("yylex", lineno
);
258 /* yacc */ else if (cp
[1] == 'y' && !cp
[2]) {
260 * we search only the 3rd part of a yacc file
261 * for C references. This may be wrong.
264 (void)strlcpy(lbuf
, "%%$", sizeof(lbuf
));
265 pfnote("yyparse", lineno
);
268 /* fortran */ else if ((cp
[1] != 'c' && cp
[1] != 'h') && !cp
[2]) {