[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.2

1.2     ! deraadt     1: /*     $OpenBSD: ctags.c,v 1.4 1995/09/02 05:57:23 jtc 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.2     ! deraadt    47: static char rcsid[] = "$OpenBSD: ctags.c,v 1.4 1995/09/02 05:57:23 jtc 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:
                     84: void   init __P((void));
                     85: void   find_entries __P((char *));
                     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 */
                     98:        char    cmd[100];                       /* too ugly to explain */
                     99:
                    100:        aflag = uflag = NO;
                    101:        while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != EOF)
                    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:
                    157:        if (head)
                    158:                if (xflag)
                    159:                        put_entries(head);
                    160:                else {
                    161:                        if (uflag) {
                    162:                                for (step = 0; step < argc; step++) {
                    163:                                        (void)sprintf(cmd,
                    164:                                                "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
                    165:                                                        outfile, argv[step],
                    166:                                                        outfile);
                    167:                                        system(cmd);
                    168:                                }
                    169:                                ++aflag;
                    170:                        }
                    171:                        if (!(outf = fopen(outfile, aflag ? "a" : "w")))
                    172:                                err(exit_val, "%s", outfile);
                    173:                        put_entries(head);
                    174:                        (void)fclose(outf);
                    175:                        if (uflag) {
                    176:                                (void)sprintf(cmd, "sort -o %s %s",
                    177:                                                outfile, outfile);
                    178:                                system(cmd);
                    179:                        }
                    180:                }
                    181:        exit(exit_val);
                    182: }
                    183:
                    184: /*
                    185:  * init --
                    186:  *     this routine sets up the boolean psuedo-functions which work by
                    187:  *     setting boolean flags dependent upon the corresponding character.
                    188:  *     Every char which is NOT in that string is false with respect to
                    189:  *     the pseudo-function.  Therefore, all of the array "_wht" is NO
                    190:  *     by default and then the elements subscripted by the chars in
                    191:  *     CWHITE are set to YES.  Thus, "_wht" of a char is YES if it is in
                    192:  *     the string CWHITE, else NO.
                    193:  */
                    194: void
                    195: init()
                    196: {
                    197:        int             i;
                    198:        unsigned char   *sp;
                    199:
                    200:        for (i = 0; i < 256; i++) {
                    201:                _wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
                    202:                _gd[i] = YES;
                    203:        }
                    204: #define        CWHITE  " \f\t\n"
                    205:        for (sp = CWHITE; *sp; sp++)    /* white space chars */
                    206:                _wht[*sp] = YES;
                    207: #define        CTOKEN  " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
                    208:        for (sp = CTOKEN; *sp; sp++)    /* token ending chars */
                    209:                _etk[*sp] = YES;
                    210: #define        CINTOK  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
                    211:        for (sp = CINTOK; *sp; sp++)    /* valid in-token chars */
                    212:                _itk[*sp] = YES;
                    213: #define        CBEGIN  "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
                    214:        for (sp = CBEGIN; *sp; sp++)    /* token starting chars */
                    215:                _btk[*sp] = YES;
                    216: #define        CNOTGD  ",;"
                    217:        for (sp = CNOTGD; *sp; sp++)    /* invalid after-function chars */
                    218:                _gd[*sp] = NO;
                    219: }
                    220:
                    221: /*
                    222:  * find_entries --
                    223:  *     this routine opens the specified file and calls the function
                    224:  *     which searches the file.
                    225:  */
                    226: void
                    227: find_entries(file)
                    228:        char    *file;
                    229: {
                    230:        char    *cp;
                    231:
                    232:        lineno = 0;                             /* should be 1 ?? KB */
                    233:        if (cp = strrchr(file, '.')) {
                    234:                if (cp[1] == 'l' && !cp[2]) {
                    235:                        int     c;
                    236:
                    237:                        for (;;) {
                    238:                                if (GETC(==, EOF))
                    239:                                        return;
                    240:                                if (!iswhite(c)) {
                    241:                                        rewind(inf);
                    242:                                        break;
                    243:                                }
                    244:                        }
                    245: #define        LISPCHR ";(["
                    246: /* lisp */             if (strchr(LISPCHR, c)) {
                    247:                                l_entries();
                    248:                                return;
                    249:                        }
                    250: /* lex */              else {
                    251:                                /*
                    252:                                 * we search all 3 parts of a lex file
                    253:                                 * for C references.  This may be wrong.
                    254:                                 */
                    255:                                toss_yysec();
                    256:                                (void)strcpy(lbuf, "%%$");
                    257:                                pfnote("yylex", lineno);
                    258:                                rewind(inf);
                    259:                        }
                    260:                }
                    261: /* yacc */     else if (cp[1] == 'y' && !cp[2]) {
                    262:                        /*
                    263:                         * we search only the 3rd part of a yacc file
                    264:                         * for C references.  This may be wrong.
                    265:                         */
                    266:                        toss_yysec();
                    267:                        (void)strcpy(lbuf, "%%$");
                    268:                        pfnote("yyparse", lineno);
                    269:                        y_entries();
                    270:                }
                    271: /* fortran */  else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
                    272:                        if (PF_funcs())
                    273:                                return;
                    274:                        rewind(inf);
                    275:                }
                    276:        }
                    277: /* C */        c_entries();
                    278: }