ctags.c revision 97577
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 97577 2002-05-30 11:36:12Z 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 */ 9897574Stjr char *cmd; 991590Srgrimes 1001590Srgrimes aflag = uflag = NO; 10197577Stjr tflag = YES; 10297577Stjr while ((ch = getopt(argc, argv, "BFTadf:tuwvx")) != -1) 1031590Srgrimes switch(ch) { 1041590Srgrimes case 'B': 1051590Srgrimes searchar = '?'; 1061590Srgrimes break; 1071590Srgrimes case 'F': 1081590Srgrimes searchar = '/'; 1091590Srgrimes break; 11097577Stjr case 'T': 11197577Stjr tflag = NO; 11297577Stjr break; 1131590Srgrimes case 'a': 1141590Srgrimes aflag++; 1151590Srgrimes break; 1161590Srgrimes case 'd': 1171590Srgrimes dflag++; 1181590Srgrimes break; 1191590Srgrimes case 'f': 1201590Srgrimes outfile = optarg; 1211590Srgrimes break; 1221590Srgrimes case 't': 12397577Stjr tflag = YES; 1241590Srgrimes break; 1251590Srgrimes case 'u': 1261590Srgrimes uflag++; 1271590Srgrimes break; 1281590Srgrimes case 'w': 1291590Srgrimes wflag++; 1301590Srgrimes break; 1311590Srgrimes case 'v': 1321590Srgrimes vflag++; 1331590Srgrimes case 'x': 1341590Srgrimes xflag++; 1351590Srgrimes break; 1361590Srgrimes case '?': 1371590Srgrimes default: 13827097Scharnier usage(); 1391590Srgrimes } 1401590Srgrimes argv += optind; 1411590Srgrimes argc -= optind; 14227097Scharnier if (!argc) 14327097Scharnier usage(); 1441590Srgrimes 1451590Srgrimes init(); 1461590Srgrimes 1471590Srgrimes for (exit_val = step = 0; step < argc; ++step) 1481590Srgrimes if (!(inf = fopen(argv[step], "r"))) { 1491590Srgrimes warn("%s", argv[step]); 1501590Srgrimes exit_val = 1; 1511590Srgrimes } 1521590Srgrimes else { 1531590Srgrimes curfile = argv[step]; 1541590Srgrimes find_entries(argv[step]); 1551590Srgrimes (void)fclose(inf); 1561590Srgrimes } 1571590Srgrimes 15848465Sbillf if (head) { 1591590Srgrimes if (xflag) 1601590Srgrimes put_entries(head); 1611590Srgrimes else { 1621590Srgrimes if (uflag) { 1631590Srgrimes for (step = 0; step < argc; step++) { 16497574Stjr (void)asprintf(&cmd, 16597574Stjr "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS", 16697574Stjr outfile, argv[step], outfile); 16797574Stjr if (cmd == NULL) 16897574Stjr err(1, "out of space"); 1691590Srgrimes system(cmd); 17097574Stjr free(cmd); 17197574Stjr cmd = NULL; 1721590Srgrimes } 1731590Srgrimes ++aflag; 1741590Srgrimes } 1751590Srgrimes if (!(outf = fopen(outfile, aflag ? "a" : "w"))) 17697331Stjr err(1, "%s", outfile); 1771590Srgrimes put_entries(head); 1781590Srgrimes (void)fclose(outf); 1791590Srgrimes if (uflag) { 18097574Stjr (void)asprintf(&cmd, "sort -o %s %s", 18197574Stjr outfile, outfile); 18297574Stjr if (cmd == NULL) 18397574Stjr err(1, "out of space"); 1841590Srgrimes system(cmd); 18597574Stjr free(cmd); 18697574Stjr cmd = NULL; 1871590Srgrimes } 1881590Srgrimes } 18948465Sbillf } 1901590Srgrimes exit(exit_val); 1911590Srgrimes} 1921590Srgrimes 19327097Scharnierstatic void 19427097Scharnierusage() 19527097Scharnier{ 19697577Stjr (void)fprintf(stderr, "usage: ctags [-BFTaduwvx] [-f tagsfile] file ...\n"); 19727097Scharnier exit(1); 19827097Scharnier} 19927097Scharnier 2001590Srgrimes/* 2011590Srgrimes * init -- 20287750Scharnier * this routine sets up the boolean pseudo-functions which work by 2031590Srgrimes * setting boolean flags dependent upon the corresponding character. 2041590Srgrimes * Every char which is NOT in that string is false with respect to 2051590Srgrimes * the pseudo-function. Therefore, all of the array "_wht" is NO 2061590Srgrimes * by default and then the elements subscripted by the chars in 2071590Srgrimes * CWHITE are set to YES. Thus, "_wht" of a char is YES if it is in 2081590Srgrimes * the string CWHITE, else NO. 2091590Srgrimes */ 2101590Srgrimesvoid 2111590Srgrimesinit() 2121590Srgrimes{ 2131590Srgrimes int i; 21487215Smarkm const unsigned char *sp; 2151590Srgrimes 2161590Srgrimes for (i = 0; i < 256; i++) { 2171590Srgrimes _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO; 2181590Srgrimes _gd[i] = YES; 2191590Srgrimes } 2201590Srgrimes#define CWHITE " \f\t\n" 2211590Srgrimes for (sp = CWHITE; *sp; sp++) /* white space chars */ 2221590Srgrimes _wht[*sp] = YES; 2231590Srgrimes#define CTOKEN " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?" 2241590Srgrimes for (sp = CTOKEN; *sp; sp++) /* token ending chars */ 2251590Srgrimes _etk[*sp] = YES; 2261590Srgrimes#define CINTOK "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789" 2271590Srgrimes for (sp = CINTOK; *sp; sp++) /* valid in-token chars */ 2281590Srgrimes _itk[*sp] = YES; 2291590Srgrimes#define CBEGIN "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" 2301590Srgrimes for (sp = CBEGIN; *sp; sp++) /* token starting chars */ 2311590Srgrimes _btk[*sp] = YES; 2321590Srgrimes#define CNOTGD ",;" 2331590Srgrimes for (sp = CNOTGD; *sp; sp++) /* invalid after-function chars */ 2341590Srgrimes _gd[*sp] = NO; 2351590Srgrimes} 2361590Srgrimes 2371590Srgrimes/* 2381590Srgrimes * find_entries -- 2391590Srgrimes * this routine opens the specified file and calls the function 2401590Srgrimes * which searches the file. 2411590Srgrimes */ 2421590Srgrimesvoid 2431590Srgrimesfind_entries(file) 2441590Srgrimes char *file; 2451590Srgrimes{ 2461590Srgrimes char *cp; 2471590Srgrimes 2481590Srgrimes lineno = 0; /* should be 1 ?? KB */ 24932069Salex if ((cp = strrchr(file, '.'))) { 2501590Srgrimes if (cp[1] == 'l' && !cp[2]) { 2511590Srgrimes int c; 2521590Srgrimes 2531590Srgrimes for (;;) { 2541590Srgrimes if (GETC(==, EOF)) 2551590Srgrimes return; 2561590Srgrimes if (!iswhite(c)) { 2571590Srgrimes rewind(inf); 2581590Srgrimes break; 2591590Srgrimes } 2601590Srgrimes } 2611590Srgrimes#define LISPCHR ";([" 2621590Srgrimes/* lisp */ if (strchr(LISPCHR, c)) { 2631590Srgrimes l_entries(); 2641590Srgrimes return; 2651590Srgrimes } 2661590Srgrimes/* lex */ else { 2671590Srgrimes /* 2681590Srgrimes * we search all 3 parts of a lex file 2691590Srgrimes * for C references. This may be wrong. 2701590Srgrimes */ 2711590Srgrimes toss_yysec(); 2721590Srgrimes (void)strcpy(lbuf, "%%$"); 2731590Srgrimes pfnote("yylex", lineno); 2741590Srgrimes rewind(inf); 2751590Srgrimes } 2761590Srgrimes } 2771590Srgrimes/* yacc */ else if (cp[1] == 'y' && !cp[2]) { 2781590Srgrimes /* 2791590Srgrimes * we search only the 3rd part of a yacc file 2801590Srgrimes * for C references. This may be wrong. 2811590Srgrimes */ 2821590Srgrimes toss_yysec(); 2831590Srgrimes (void)strcpy(lbuf, "%%$"); 2841590Srgrimes pfnote("yyparse", lineno); 2851590Srgrimes y_entries(); 2861590Srgrimes } 2871590Srgrimes/* fortran */ else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) { 2881590Srgrimes if (PF_funcs()) 2891590Srgrimes return; 2901590Srgrimes rewind(inf); 2911590Srgrimes } 2921590Srgrimes } 2931590Srgrimes/* C */ c_entries(); 2941590Srgrimes} 295