ctags.c revision 97331
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"; 3887249Smarkm#endif 391590Srgrimes 4087628Sdwmalone#if 0 411590Srgrimes#ifndef lint 4287628Sdwmalonestatic char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95"; 4381782Smikeh#endif 4487628Sdwmalone#endif 451590Srgrimes 4687628Sdwmalone#include <sys/cdefs.h> 4787628Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/ctags/ctags.c 97331 2002-05-27 03:54:45Z tjr $"); 4887628Sdwmalone 491590Srgrimes#include <err.h> 501590Srgrimes#include <limits.h> 511590Srgrimes#include <stdio.h> 5287750Scharnier#include <stdlib.h> 531590Srgrimes#include <string.h> 541590Srgrimes#include <unistd.h> 551590Srgrimes 561590Srgrimes#include "ctags.h" 571590Srgrimes 581590Srgrimes/* 591590Srgrimes * ctags: create a tags file 601590Srgrimes */ 611590Srgrimes 621590SrgrimesNODE *head; /* head of the sorted binary tree */ 631590Srgrimes 641590Srgrimes /* boolean "func" (see init()) */ 651590Srgrimesbool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256]; 661590Srgrimes 671590SrgrimesFILE *inf; /* ioptr for current input file */ 681590SrgrimesFILE *outf; /* ioptr for tags file */ 691590Srgrimes 701590Srgrimeslong lineftell; /* ftell after getc( inf ) == '\n' */ 711590Srgrimes 721590Srgrimesint lineno; /* line number of current line */ 731590Srgrimesint dflag; /* -d: non-macro defines */ 741590Srgrimesint tflag; /* -t: create tags for typedefs */ 751590Srgrimesint vflag; /* -v: vgrind style index output */ 761590Srgrimesint wflag; /* -w: suppress warnings */ 771590Srgrimesint xflag; /* -x: cxref style output */ 781590Srgrimes 791590Srgrimeschar *curfile; /* current input file name */ 801590Srgrimeschar searchar = '/'; /* use /.../ searches by default */ 811590Srgrimeschar lbuf[LINE_MAX]; 821590Srgrimes 8392920Simpvoid init(void); 8492920Simpvoid find_entries(char *); 8592920Simpstatic void usage(void); 861590Srgrimes 871590Srgrimesint 881590Srgrimesmain(argc, argv) 891590Srgrimes int argc; 901590Srgrimes char **argv; 911590Srgrimes{ 9287215Smarkm static const char *outfile = "tags"; /* output file */ 931590Srgrimes int aflag; /* -a: append to tags */ 941590Srgrimes int uflag; /* -u: update tags */ 951590Srgrimes int exit_val; /* exit value */ 961590Srgrimes int step; /* step through args */ 971590Srgrimes int ch; /* getopts char */ 981590Srgrimes char cmd[100]; /* too ugly to explain */ 991590Srgrimes 1001590Srgrimes aflag = uflag = NO; 10124360Simp while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1) 1021590Srgrimes switch(ch) { 1031590Srgrimes case 'B': 1041590Srgrimes searchar = '?'; 1051590Srgrimes break; 1061590Srgrimes case 'F': 1071590Srgrimes searchar = '/'; 1081590Srgrimes break; 1091590Srgrimes case 'a': 1101590Srgrimes aflag++; 1111590Srgrimes break; 1121590Srgrimes case 'd': 1131590Srgrimes dflag++; 1141590Srgrimes break; 1151590Srgrimes case 'f': 1161590Srgrimes outfile = optarg; 1171590Srgrimes break; 1181590Srgrimes case 't': 1191590Srgrimes tflag++; 1201590Srgrimes break; 1211590Srgrimes case 'u': 1221590Srgrimes uflag++; 1231590Srgrimes break; 1241590Srgrimes case 'w': 1251590Srgrimes wflag++; 1261590Srgrimes break; 1271590Srgrimes case 'v': 1281590Srgrimes vflag++; 1291590Srgrimes case 'x': 1301590Srgrimes xflag++; 1311590Srgrimes break; 1321590Srgrimes case '?': 1331590Srgrimes default: 13427097Scharnier usage(); 1351590Srgrimes } 1361590Srgrimes argv += optind; 1371590Srgrimes argc -= optind; 13827097Scharnier if (!argc) 13927097Scharnier usage(); 1401590Srgrimes 1411590Srgrimes init(); 1421590Srgrimes 1431590Srgrimes for (exit_val = step = 0; step < argc; ++step) 1441590Srgrimes if (!(inf = fopen(argv[step], "r"))) { 1451590Srgrimes warn("%s", argv[step]); 1461590Srgrimes exit_val = 1; 1471590Srgrimes } 1481590Srgrimes else { 1491590Srgrimes curfile = argv[step]; 1501590Srgrimes find_entries(argv[step]); 1511590Srgrimes (void)fclose(inf); 1521590Srgrimes } 1531590Srgrimes 15448465Sbillf if (head) { 1551590Srgrimes if (xflag) 1561590Srgrimes put_entries(head); 1571590Srgrimes else { 1581590Srgrimes if (uflag) { 1591590Srgrimes for (step = 0; step < argc; step++) { 1601590Srgrimes (void)sprintf(cmd, 1611590Srgrimes "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS", 1621590Srgrimes outfile, argv[step], 1631590Srgrimes outfile); 1641590Srgrimes system(cmd); 1651590Srgrimes } 1661590Srgrimes ++aflag; 1671590Srgrimes } 1681590Srgrimes if (!(outf = fopen(outfile, aflag ? "a" : "w"))) 16997331Stjr err(1, "%s", outfile); 1701590Srgrimes put_entries(head); 1711590Srgrimes (void)fclose(outf); 1721590Srgrimes if (uflag) { 1731590Srgrimes (void)sprintf(cmd, "sort -o %s %s", 1741590Srgrimes outfile, outfile); 1751590Srgrimes system(cmd); 1761590Srgrimes } 1771590Srgrimes } 17848465Sbillf } 1791590Srgrimes exit(exit_val); 1801590Srgrimes} 1811590Srgrimes 18227097Scharnierstatic void 18327097Scharnierusage() 18427097Scharnier{ 18527097Scharnier (void)fprintf(stderr, "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n"); 18627097Scharnier exit(1); 18727097Scharnier} 18827097Scharnier 1891590Srgrimes/* 1901590Srgrimes * init -- 19187750Scharnier * this routine sets up the boolean pseudo-functions which work by 1921590Srgrimes * setting boolean flags dependent upon the corresponding character. 1931590Srgrimes * Every char which is NOT in that string is false with respect to 1941590Srgrimes * the pseudo-function. Therefore, all of the array "_wht" is NO 1951590Srgrimes * by default and then the elements subscripted by the chars in 1961590Srgrimes * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in 1971590Srgrimes * the string CWHITE, else NO. 1981590Srgrimes */ 1991590Srgrimesvoid 2001590Srgrimesinit() 2011590Srgrimes{ 2021590Srgrimes int i; 20387215Smarkm const unsigned char *sp; 2041590Srgrimes 2051590Srgrimes for (i = 0; i < 256; i++) { 2061590Srgrimes _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO; 2071590Srgrimes _gd[i] = YES; 2081590Srgrimes } 2091590Srgrimes#define CWHITE " \f\t\n" 2101590Srgrimes for (sp = CWHITE; *sp; sp++) /* white space chars */ 2111590Srgrimes _wht[*sp] = YES; 2121590Srgrimes#define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?" 2131590Srgrimes for (sp = CTOKEN; *sp; sp++) /* token ending chars */ 2141590Srgrimes _etk[*sp] = YES; 2151590Srgrimes#define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789" 2161590Srgrimes for (sp = CINTOK; *sp; sp++) /* valid in-token chars */ 2171590Srgrimes _itk[*sp] = YES; 2181590Srgrimes#define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" 2191590Srgrimes for (sp = CBEGIN; *sp; sp++) /* token starting chars */ 2201590Srgrimes _btk[*sp] = YES; 2211590Srgrimes#define CNOTGD ",;" 2221590Srgrimes for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */ 2231590Srgrimes _gd[*sp] = NO; 2241590Srgrimes} 2251590Srgrimes 2261590Srgrimes/* 2271590Srgrimes * find_entries -- 2281590Srgrimes * this routine opens the specified file and calls the function 2291590Srgrimes * which searches the file. 2301590Srgrimes */ 2311590Srgrimesvoid 2321590Srgrimesfind_entries(file) 2331590Srgrimes char *file; 2341590Srgrimes{ 2351590Srgrimes char *cp; 2361590Srgrimes 2371590Srgrimes lineno = 0; /* should be 1 ?? KB */ 23832069Salex if ((cp = strrchr(file, '.'))) { 2391590Srgrimes if (cp[1] == 'l' && !cp[2]) { 2401590Srgrimes int c; 2411590Srgrimes 2421590Srgrimes for (;;) { 2431590Srgrimes if (GETC(==, EOF)) 2441590Srgrimes return; 2451590Srgrimes if (!iswhite(c)) { 2461590Srgrimes rewind(inf); 2471590Srgrimes break; 2481590Srgrimes } 2491590Srgrimes } 2501590Srgrimes#define LISPCHR ";([" 2511590Srgrimes/* lisp */ if (strchr(LISPCHR, c)) { 2521590Srgrimes l_entries(); 2531590Srgrimes return; 2541590Srgrimes } 2551590Srgrimes/* lex */ else { 2561590Srgrimes /* 2571590Srgrimes * we search all 3 parts of a lex file 2581590Srgrimes * for C references. This may be wrong. 2591590Srgrimes */ 2601590Srgrimes toss_yysec(); 2611590Srgrimes (void)strcpy(lbuf, "%%$"); 2621590Srgrimes pfnote("yylex", lineno); 2631590Srgrimes rewind(inf); 2641590Srgrimes } 2651590Srgrimes } 2661590Srgrimes/* yacc */ else if (cp[1] == 'y' && !cp[2]) { 2671590Srgrimes /* 2681590Srgrimes * we search only the 3rd part of a yacc file 2691590Srgrimes * for C references. This may be wrong. 2701590Srgrimes */ 2711590Srgrimes toss_yysec(); 2721590Srgrimes (void)strcpy(lbuf, "%%$"); 2731590Srgrimes pfnote("yyparse", lineno); 2741590Srgrimes y_entries(); 2751590Srgrimes } 2761590Srgrimes/* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) { 2771590Srgrimes if (PF_funcs()) 2781590Srgrimes return; 2791590Srgrimes rewind(inf); 2801590Srgrimes } 2811590Srgrimes } 2821590Srgrimes/* C */ c_entries(); 2831590Srgrimes} 284