ctags.c revision 48465
11590Srgrimes/* 227097Scharnier * Copyright (c) 1987, 1993, 1994, 1995 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 3541568Sarchiestatic const char copyright[] = 3627097Scharnier"@(#) Copyright (c) 1987, 1993, 1994, 1995\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 4141568Sarchiestatic const char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95"; 421590Srgrimes#endif /* not lint */ 431590Srgrimes 441590Srgrimes#include <err.h> 451590Srgrimes#include <limits.h> 461590Srgrimes#include <stdio.h> 471590Srgrimes#include <string.h> 481590Srgrimes#include <stdlib.h> 491590Srgrimes#include <unistd.h> 501590Srgrimes 511590Srgrimes#include "ctags.h" 521590Srgrimes 531590Srgrimes/* 541590Srgrimes * ctags: create a tags file 551590Srgrimes */ 561590Srgrimes 571590SrgrimesNODE *head; /* head of the sorted binary tree */ 581590Srgrimes 591590Srgrimes /* boolean "func" (see init()) */ 601590Srgrimesbool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256]; 611590Srgrimes 621590SrgrimesFILE *inf; /* ioptr for current input file */ 631590SrgrimesFILE *outf; /* ioptr for tags file */ 641590Srgrimes 651590Srgrimeslong lineftell; /* ftell after getc( inf ) == '\n' */ 661590Srgrimes 671590Srgrimesint lineno; /* line number of current line */ 681590Srgrimesint dflag; /* -d: non-macro defines */ 691590Srgrimesint tflag; /* -t: create tags for typedefs */ 701590Srgrimesint vflag; /* -v: vgrind style index output */ 711590Srgrimesint wflag; /* -w: suppress warnings */ 721590Srgrimesint xflag; /* -x: cxref style output */ 731590Srgrimes 741590Srgrimeschar *curfile; /* current input file name */ 751590Srgrimeschar searchar = '/'; /* use /.../ searches by default */ 761590Srgrimeschar lbuf[LINE_MAX]; 771590Srgrimes 781590Srgrimesvoid init __P((void)); 791590Srgrimesvoid find_entries __P((char *)); 8027097Scharnierstatic void usage __P((void)); 811590Srgrimes 821590Srgrimesint 831590Srgrimesmain(argc, argv) 841590Srgrimes int argc; 851590Srgrimes char **argv; 861590Srgrimes{ 871590Srgrimes static char *outfile = "tags"; /* output file */ 881590Srgrimes int aflag; /* -a: append to tags */ 891590Srgrimes int uflag; /* -u: update tags */ 901590Srgrimes int exit_val; /* exit value */ 911590Srgrimes int step; /* step through args */ 921590Srgrimes int ch; /* getopts char */ 931590Srgrimes char cmd[100]; /* too ugly to explain */ 941590Srgrimes 951590Srgrimes aflag = uflag = NO; 9624360Simp while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1) 971590Srgrimes switch(ch) { 981590Srgrimes case 'B': 991590Srgrimes searchar = '?'; 1001590Srgrimes break; 1011590Srgrimes case 'F': 1021590Srgrimes searchar = '/'; 1031590Srgrimes break; 1041590Srgrimes case 'a': 1051590Srgrimes aflag++; 1061590Srgrimes break; 1071590Srgrimes case 'd': 1081590Srgrimes dflag++; 1091590Srgrimes break; 1101590Srgrimes case 'f': 1111590Srgrimes outfile = optarg; 1121590Srgrimes break; 1131590Srgrimes case 't': 1141590Srgrimes tflag++; 1151590Srgrimes break; 1161590Srgrimes case 'u': 1171590Srgrimes uflag++; 1181590Srgrimes break; 1191590Srgrimes case 'w': 1201590Srgrimes wflag++; 1211590Srgrimes break; 1221590Srgrimes case 'v': 1231590Srgrimes vflag++; 1241590Srgrimes case 'x': 1251590Srgrimes xflag++; 1261590Srgrimes break; 1271590Srgrimes case '?': 1281590Srgrimes default: 12927097Scharnier usage(); 1301590Srgrimes } 1311590Srgrimes argv += optind; 1321590Srgrimes argc -= optind; 13327097Scharnier if (!argc) 13427097Scharnier usage(); 1351590Srgrimes 1361590Srgrimes init(); 1371590Srgrimes 1381590Srgrimes for (exit_val = step = 0; step < argc; ++step) 1391590Srgrimes if (!(inf = fopen(argv[step], "r"))) { 1401590Srgrimes warn("%s", argv[step]); 1411590Srgrimes exit_val = 1; 1421590Srgrimes } 1431590Srgrimes else { 1441590Srgrimes curfile = argv[step]; 1451590Srgrimes find_entries(argv[step]); 1461590Srgrimes (void)fclose(inf); 1471590Srgrimes } 1481590Srgrimes 14948465Sbillf if (head) { 1501590Srgrimes if (xflag) 1511590Srgrimes put_entries(head); 1521590Srgrimes else { 1531590Srgrimes if (uflag) { 1541590Srgrimes for (step = 0; step < argc; step++) { 1551590Srgrimes (void)sprintf(cmd, 1561590Srgrimes "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS", 1571590Srgrimes outfile, argv[step], 1581590Srgrimes outfile); 1591590Srgrimes system(cmd); 1601590Srgrimes } 1611590Srgrimes ++aflag; 1621590Srgrimes } 1631590Srgrimes if (!(outf = fopen(outfile, aflag ? "a" : "w"))) 1641590Srgrimes err(exit_val, "%s", outfile); 1651590Srgrimes put_entries(head); 1661590Srgrimes (void)fclose(outf); 1671590Srgrimes if (uflag) { 1681590Srgrimes (void)sprintf(cmd, "sort -o %s %s", 1691590Srgrimes outfile, outfile); 1701590Srgrimes system(cmd); 1711590Srgrimes } 1721590Srgrimes } 17348465Sbillf } 1741590Srgrimes exit(exit_val); 1751590Srgrimes} 1761590Srgrimes 17727097Scharnierstatic void 17827097Scharnierusage() 17927097Scharnier{ 18027097Scharnier (void)fprintf(stderr, "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n"); 18127097Scharnier exit(1); 18227097Scharnier} 18327097Scharnier 1841590Srgrimes/* 1851590Srgrimes * init -- 1861590Srgrimes * this routine sets up the boolean psuedo-functions which work by 1871590Srgrimes * setting boolean flags dependent upon the corresponding character. 1881590Srgrimes * Every char which is NOT in that string is false with respect to 1891590Srgrimes * the pseudo-function. Therefore, all of the array "_wht" is NO 1901590Srgrimes * by default and then the elements subscripted by the chars in 1911590Srgrimes * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in 1921590Srgrimes * the string CWHITE, else NO. 1931590Srgrimes */ 1941590Srgrimesvoid 1951590Srgrimesinit() 1961590Srgrimes{ 1971590Srgrimes int i; 1981590Srgrimes unsigned char *sp; 1991590Srgrimes 2001590Srgrimes for (i = 0; i < 256; i++) { 2011590Srgrimes _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO; 2021590Srgrimes _gd[i] = YES; 2031590Srgrimes } 2041590Srgrimes#define CWHITE " \f\t\n" 2051590Srgrimes for (sp = CWHITE; *sp; sp++) /* white space chars */ 2061590Srgrimes _wht[*sp] = YES; 2071590Srgrimes#define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?" 2081590Srgrimes for (sp = CTOKEN; *sp; sp++) /* token ending chars */ 2091590Srgrimes _etk[*sp] = YES; 2101590Srgrimes#define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789" 2111590Srgrimes for (sp = CINTOK; *sp; sp++) /* valid in-token chars */ 2121590Srgrimes _itk[*sp] = YES; 2131590Srgrimes#define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" 2141590Srgrimes for (sp = CBEGIN; *sp; sp++) /* token starting chars */ 2151590Srgrimes _btk[*sp] = YES; 2161590Srgrimes#define CNOTGD ",;" 2171590Srgrimes for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */ 2181590Srgrimes _gd[*sp] = NO; 2191590Srgrimes} 2201590Srgrimes 2211590Srgrimes/* 2221590Srgrimes * find_entries -- 2231590Srgrimes * this routine opens the specified file and calls the function 2241590Srgrimes * which searches the file. 2251590Srgrimes */ 2261590Srgrimesvoid 2271590Srgrimesfind_entries(file) 2281590Srgrimes char *file; 2291590Srgrimes{ 2301590Srgrimes char *cp; 2311590Srgrimes 2321590Srgrimes lineno = 0; /* should be 1 ?? KB */ 23332069Salex if ((cp = strrchr(file, '.'))) { 2341590Srgrimes if (cp[1] == 'l' && !cp[2]) { 2351590Srgrimes int c; 2361590Srgrimes 2371590Srgrimes for (;;) { 2381590Srgrimes if (GETC(==, EOF)) 2391590Srgrimes return; 2401590Srgrimes if (!iswhite(c)) { 2411590Srgrimes rewind(inf); 2421590Srgrimes break; 2431590Srgrimes } 2441590Srgrimes } 2451590Srgrimes#define LISPCHR ";([" 2461590Srgrimes/* lisp */ if (strchr(LISPCHR, c)) { 2471590Srgrimes l_entries(); 2481590Srgrimes return; 2491590Srgrimes } 2501590Srgrimes/* lex */ else { 2511590Srgrimes /* 2521590Srgrimes * we search all 3 parts of a lex file 2531590Srgrimes * for C references. This may be wrong. 2541590Srgrimes */ 2551590Srgrimes toss_yysec(); 2561590Srgrimes (void)strcpy(lbuf, "%%$"); 2571590Srgrimes pfnote("yylex", lineno); 2581590Srgrimes rewind(inf); 2591590Srgrimes } 2601590Srgrimes } 2611590Srgrimes/* yacc */ else if (cp[1] == 'y' && !cp[2]) { 2621590Srgrimes /* 2631590Srgrimes * we search only the 3rd part of a yacc file 2641590Srgrimes * for C references. This may be wrong. 2651590Srgrimes */ 2661590Srgrimes toss_yysec(); 2671590Srgrimes (void)strcpy(lbuf, "%%$"); 2681590Srgrimes pfnote("yyparse", lineno); 2691590Srgrimes y_entries(); 2701590Srgrimes } 2711590Srgrimes/* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) { 2721590Srgrimes if (PF_funcs()) 2731590Srgrimes return; 2741590Srgrimes rewind(inf); 2751590Srgrimes } 2761590Srgrimes } 2771590Srgrimes/* C */ c_entries(); 2781590Srgrimes} 279