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