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