[BACK]Return to ctags.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ctags

Annotation of src/usr.bin/ctags/ctags.c, Revision 1.7

1.7     ! deraadt     1: /*     $OpenBSD: ctags.c,v 1.6 2002/02/16 21:27:45 millert Exp $       */
1.1       deraadt     2: /*     $NetBSD: ctags.c,v 1.4 1995/09/02 05:57:23 jtc Exp $    */
                      3:
                      4: /*
                      5:  * Copyright (c) 1987, 1993, 1994, 1995
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)ctags.c    8.4 (Berkeley) 2/7/95";
                     46: #endif
1.7     ! deraadt    47: static char rcsid[] = "$OpenBSD: ctags.c,v 1.6 2002/02/16 21:27:45 millert Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <err.h>
                     51: #include <limits.h>
                     52: #include <stdio.h>
                     53: #include <string.h>
                     54: #include <stdlib.h>
                     55: #include <unistd.h>
                     56:
                     57: #include "ctags.h"
                     58:
                     59: /*
                     60:  * ctags: create a tags file
                     61:  */
                     62:
                     63: NODE   *head;                  /* head of the sorted binary tree */
                     64:
                     65:                                /* boolean "func" (see init()) */
                     66: bool   _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
                     67:
                     68: FILE   *inf;                   /* ioptr for current input file */
                     69: FILE   *outf;                  /* ioptr for tags file */
                     70:
                     71: long   lineftell;              /* ftell after getc( inf ) == '\n' */
                     72:
                     73: int    lineno;                 /* line number of current line */
                     74: int    dflag;                  /* -d: non-macro defines */
                     75: int    tflag;                  /* -t: create tags for typedefs */
                     76: int    vflag;                  /* -v: vgrind style index output */
                     77: int    wflag;                  /* -w: suppress warnings */
                     78: int    xflag;                  /* -x: cxref style output */
                     79:
                     80: char   *curfile;               /* current input file name */
                     81: char   searchar = '/';         /* use /.../ searches by default */
                     82: char   lbuf[LINE_MAX];
                     83:
1.6       millert    84: void   init(void);
                     85: void   find_entries(char *);
1.1       deraadt    86:
                     87: int
                     88: main(argc, argv)
                     89:        int     argc;
                     90:        char    **argv;
                     91: {
                     92:        static 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 */
1.5       deraadt    98:        char    *cmd;
1.1       deraadt    99:
                    100:        aflag = uflag = NO;
1.3       millert   101:        while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
1.1       deraadt   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:                        goto usage;
                    135:                }
                    136:        argv += optind;
                    137:        argc -= optind;
                    138:        if (!argc) {
                    139: usage:         (void)fprintf(stderr,
                    140:                        "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
                    141:                exit(1);
                    142:        }
                    143:
                    144:        init();
                    145:
                    146:        for (exit_val = step = 0; step < argc; ++step)
                    147:                if (!(inf = fopen(argv[step], "r"))) {
                    148:                        warn("%s", argv[step]);
                    149:                        exit_val = 1;
                    150:                }
                    151:                else {
                    152:                        curfile = argv[step];
                    153:                        find_entries(argv[step]);
                    154:                        (void)fclose(inf);
                    155:                }
                    156:
1.4       deraadt   157:        if (head) {
1.1       deraadt   158:                if (xflag)
                    159:                        put_entries(head);
                    160:                else {
                    161:                        if (uflag) {
                    162:                                for (step = 0; step < argc; step++) {
1.5       deraadt   163:                                        (void)asprintf(&cmd,
                    164:                                            "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
                    165:                                            outfile, argv[step], outfile);
                    166:                                        if (cmd == NULL)
                    167:                                                err(1, "out of space");
1.1       deraadt   168:                                        system(cmd);
1.5       deraadt   169:                                        free(cmd);
                    170:                                        cmd = NULL;
1.1       deraadt   171:                                }
                    172:                                ++aflag;
                    173:                        }
                    174:                        if (!(outf = fopen(outfile, aflag ? "a" : "w")))
                    175:                                err(exit_val, "%s", outfile);
                    176:                        put_entries(head);
                    177:                        (void)fclose(outf);
                    178:                        if (uflag) {
1.5       deraadt   179:                                (void)asprintf(&cmd, "sort -o %s %s",
                    180:                                    outfile, outfile);
                    181:                                if (cmd == NULL)
                    182:                                                err(1, "out of space");
1.1       deraadt   183:                                system(cmd);
1.5       deraadt   184:                                free(cmd);
                    185:                                cmd = NULL;
1.1       deraadt   186:                        }
                    187:                }
1.4       deraadt   188:        }
1.1       deraadt   189:        exit(exit_val);
                    190: }
                    191:
                    192: /*
                    193:  * init --
                    194:  *     this routine sets up the boolean psuedo-functions which work by
                    195:  *     setting boolean flags dependent upon the corresponding character.
                    196:  *     Every char which is NOT in that string is false with respect to
                    197:  *     the pseudo-function.  Therefore, all of the array "_wht" is NO
                    198:  *     by default and then the elements subscripted by the chars in
                    199:  *     CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
                    200:  *     the string CWHITE, else NO.
                    201:  */
                    202: void
                    203: init()
                    204: {
                    205:        int             i;
                    206:        unsigned char   *sp;
                    207:
                    208:        for (i = 0; i < 256; i++) {
                    209:                _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
                    210:                _gd[i] = YES;
                    211:        }
                    212: #define        CWHITE  " \f\t\n"
                    213:        for (sp = CWHITE; *sp; sp++)    /* white space chars */
                    214:                _wht[*sp] = YES;
                    215: #define        CTOKEN  " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
                    216:        for (sp = CTOKEN; *sp; sp++)    /* token ending chars */
                    217:                _etk[*sp] = YES;
                    218: #define        CINTOK  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
                    219:        for (sp = CINTOK; *sp; sp++)    /* valid in-token chars */
                    220:                _itk[*sp] = YES;
                    221: #define        CBEGIN  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
                    222:        for (sp = CBEGIN; *sp; sp++)    /* token starting chars */
                    223:                _btk[*sp] = YES;
                    224: #define        CNOTGD  ",;"
                    225:        for (sp = CNOTGD; *sp; sp++)    /* invalid after-function chars */
                    226:                _gd[*sp] = NO;
                    227: }
                    228:
                    229: /*
                    230:  * find_entries --
                    231:  *     this routine opens the specified file and calls the function
                    232:  *     which searches the file.
                    233:  */
                    234: void
                    235: find_entries(file)
                    236:        char    *file;
                    237: {
                    238:        char    *cp;
                    239:
                    240:        lineno = 0;                             /* should be 1 ?? KB */
1.5       deraadt   241:        if ((cp = strrchr(file, '.'))) {
1.1       deraadt   242:                if (cp[1] == 'l' && !cp[2]) {
                    243:                        int     c;
                    244:
                    245:                        for (;;) {
                    246:                                if (GETC(==, EOF))
                    247:                                        return;
                    248:                                if (!iswhite(c)) {
                    249:                                        rewind(inf);
                    250:                                        break;
                    251:                                }
                    252:                        }
                    253: #define        LISPCHR ";(["
                    254: /* lisp */             if (strchr(LISPCHR, c)) {
                    255:                                l_entries();
                    256:                                return;
                    257:                        }
                    258: /* lex */              else {
                    259:                                /*
                    260:                                 * we search all 3 parts of a lex file
                    261:                                 * for C references.  This may be wrong.
                    262:                                 */
                    263:                                toss_yysec();
1.7     ! deraadt   264:                                (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1       deraadt   265:                                pfnote("yylex", lineno);
                    266:                                rewind(inf);
                    267:                        }
                    268:                }
                    269: /* yacc */     else if (cp[1] == 'y' && !cp[2]) {
                    270:                        /*
                    271:                         * we search only the 3rd part of a yacc file
                    272:                         * for C references.  This may be wrong.
                    273:                         */
                    274:                        toss_yysec();
1.7     ! deraadt   275:                        (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1       deraadt   276:                        pfnote("yyparse", lineno);
                    277:                        y_entries();
                    278:                }
                    279: /* fortran */  else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
                    280:                        if (PF_funcs())
                    281:                                return;
                    282:                        rewind(inf);
                    283:                }
                    284:        }
                    285: /* C */        c_entries();
                    286: }