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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef lint 31static const char copyright[] = 32"@(#) Copyright (c) 1987, 1993, 1994, 1995\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34#endif 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95"; 39#endif 40#endif 41 42#include <sys/cdefs.h> 43#include <sys/types.h> 44#include <sys/wait.h> 45__FBSDID("$FreeBSD$"); 46 47#include <err.h> 48#include <limits.h> 49#include <locale.h> 50#include <regex.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <unistd.h> 55 56#include "ctags.h" 57 58/* 59 * ctags: create a tags file 60 */ 61 62NODE *head; /* head of the sorted binary tree */ 63 64 /* boolean "func" (see init()) */ 65bool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256]; 66 67FILE *inf; /* ioptr for current input file */ 68FILE *outf; /* ioptr for tags file */ 69 70long lineftell; /* ftell after getc( inf ) == '\n' */ 71 72int lineno; /* line number of current line */ 73int dflag; /* -d: non-macro defines */ 74int tflag; /* -t: create tags for typedefs */ 75int vflag; /* -v: vgrind style index output */ 76int wflag; /* -w: suppress warnings */ 77int xflag; /* -x: cxref style output */ 78 79char *curfile; /* current input file name */ 80char searchar = '/'; /* use /.../ searches by default */ 81char lbuf[LINE_MAX]; 82 83void init(void); 84void find_entries(char *); 85static void usage(void); 86 87int 88main(int argc, char **argv) 89{ 90 static const char *outfile = "tags"; /* output file */ 91 int aflag; /* -a: append to tags */ 92 int uflag; /* -u: update tags */ 93 int exit_val; /* exit value */ 94 int step; /* step through args */ 95 int ch; /* getopts char */ 96 97 setlocale(LC_ALL, ""); 98 99 aflag = uflag = NO; 100 tflag = YES; 101 while ((ch = getopt(argc, argv, "BFTadf:tuwvx")) != -1) 102 switch(ch) { 103 case 'B': 104 searchar = '?'; 105 break; 106 case 'F': 107 searchar = '/'; 108 break; 109 case 'T': 110 tflag = NO; 111 break; 112 case 'a': 113 aflag++; 114 break; 115 case 'd': 116 dflag++; 117 break; 118 case 'f': 119 outfile = optarg; 120 break; 121 case 't': 122 tflag = YES; 123 break; 124 case 'u': 125 uflag++; 126 break; 127 case 'w': 128 wflag++; 129 break; 130 case 'v': 131 vflag++; 132 case 'x': 133 xflag++; 134 break; 135 case '?': 136 default: 137 usage(); 138 } 139 argv += optind; 140 argc -= optind; 141 if (!argc) 142 usage(); 143 144 if (!xflag) 145 setlocale(LC_COLLATE, "C"); 146 147 init(); 148 149 for (exit_val = step = 0; step < argc; ++step) 150 if (!(inf = fopen(argv[step], "r"))) { 151 warn("%s", argv[step]); 152 exit_val = 1; 153 } 154 else { 155 curfile = argv[step]; 156 find_entries(argv[step]); 157 (void)fclose(inf); 158 } 159 160 if (head) { 161 if (xflag) 162 put_entries(head); 163 else { 164 if (uflag) { 165 FILE *oldf; 166 regex_t *regx; 167 168 if ((oldf = fopen(outfile, "r")) == NULL) 169 err(1, "opening %s", outfile); 170 if (unlink(outfile)) 171 err(1, "unlinking %s", outfile); 172 if ((outf = fopen(outfile, "w")) == NULL) 173 err(1, "recreating %s", outfile); 174 if ((regx = calloc(argc, sizeof(regex_t))) == NULL) 175 err(1, "RE alloc"); 176 for (step = 0; step < argc; step++) { 177 (void)strcpy(lbuf, "\t"); 178 (void)strlcat(lbuf, argv[step], LINE_MAX); 179 (void)strlcat(lbuf, "\t", LINE_MAX); 180 if (regcomp(regx + step, lbuf, 181 REG_NOSPEC)) 182 warn("RE compilation failed"); 183 } 184nextline: 185 while (fgets(lbuf, LINE_MAX, oldf)) { 186 for (step = 0; step < argc; step++) 187 if (regexec(regx + step, 188 lbuf, 0, NULL, 0) == 0) 189 goto nextline; 190 fputs(lbuf, outf); 191 } 192 for (step = 0; step < argc; step++) 193 regfree(regx + step); 194 free(regx); 195 fclose(oldf); 196 fclose(outf); 197 ++aflag; 198 } 199 if (!(outf = fopen(outfile, aflag ? "a" : "w"))) 200 err(1, "%s", outfile); 201 put_entries(head); 202 (void)fclose(outf); 203 if (uflag) { 204 pid_t pid; 205 206 if ((pid = fork()) == -1) 207 err(1, "fork failed"); 208 else if (pid == 0) { 209 execlp("sort", "sort", "-o", outfile, 210 outfile, NULL); 211 err(1, "exec of sort failed"); 212 } 213 /* Just assume the sort went OK. The old code 214 did not do any checks either. */ 215 (void)wait(NULL); 216 } 217 } 218 } 219 exit(exit_val); 220} 221 222static void 223usage(void) 224{ 225 (void)fprintf(stderr, "usage: ctags [-BFTaduwvx] [-f tagsfile] file ...\n"); 226 exit(1); 227} 228 229/* 230 * init -- 231 * this routine sets up the boolean pseudo-functions which work by 232 * setting boolean flags dependent upon the corresponding character. 233 * Every char which is NOT in that string is false with respect to 234 * the pseudo-function. Therefore, all of the array "_wht" is NO 235 * by default and then the elements subscripted by the chars in 236 * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in 237 * the string CWHITE, else NO. 238 */ 239void 240init(void) 241{ 242 int i; 243 const unsigned char *sp; 244 245 for (i = 0; i < 256; i++) { 246 _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO; 247 _gd[i] = YES; 248 } 249#define CWHITE " \f\t\n" 250 for (sp = CWHITE; *sp; sp++) /* white space chars */ 251 _wht[*sp] = YES; 252#define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?" 253 for (sp = CTOKEN; *sp; sp++) /* token ending chars */ 254 _etk[*sp] = YES; 255#define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789" 256 for (sp = CINTOK; *sp; sp++) /* valid in-token chars */ 257 _itk[*sp] = YES; 258#define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" 259 for (sp = CBEGIN; *sp; sp++) /* token starting chars */ 260 _btk[*sp] = YES; 261#define CNOTGD ",;" 262 for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */ 263 _gd[*sp] = NO; 264} 265 266/* 267 * find_entries -- 268 * this routine opens the specified file and calls the function 269 * which searches the file. 270 */ 271void 272find_entries(char *file) 273{ 274 char *cp; 275 276 lineno = 0; /* should be 1 ?? KB */ 277 if ((cp = strrchr(file, '.'))) { 278 if (cp[1] == 'l' && !cp[2]) { 279 int c; 280 281 for (;;) { 282 if (GETC(==, EOF)) 283 return; 284 if (!iswhite(c)) { 285 rewind(inf); 286 break; 287 } 288 } 289#define LISPCHR ";([" 290/* lisp */ if (strchr(LISPCHR, c)) { 291 l_entries(); 292 return; 293 } 294/* lex */ else { 295 /* 296 * we search all 3 parts of a lex file 297 * for C references. This may be wrong. 298 */ 299 toss_yysec(); 300 (void)strcpy(lbuf, "%%$"); 301 pfnote("yylex", lineno); 302 rewind(inf); 303 } 304 } 305/* yacc */ else if (cp[1] == 'y' && !cp[2]) { 306 /* 307 * we search only the 3rd part of a yacc file 308 * for C references. This may be wrong. 309 */ 310 toss_yysec(); 311 (void)strcpy(lbuf, "%%$"); 312 pfnote("yyparse", lineno); 313 y_entries(); 314 } 315/* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) { 316 if (PF_funcs()) 317 return; 318 rewind(inf); 319 } 320 } 321/* C */ c_entries(); 322} 323