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