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

1.10    ! deraadt     1: /*     $OpenBSD: ctags.c,v 1.9 2003/06/03 02:56:07 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.
1.9       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: static char copyright[] =
                     35: "@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)ctags.c    8.4 (Berkeley) 2/7/95";
                     42: #endif
1.10    ! deraadt    43: static char rcsid[] = "$OpenBSD: ctags.c,v 1.9 2003/06/03 02:56:07 millert Exp $";
1.1       deraadt    44: #endif /* not lint */
                     45:
                     46: #include <err.h>
                     47: #include <limits.h>
                     48: #include <stdio.h>
                     49: #include <string.h>
                     50: #include <stdlib.h>
                     51: #include <unistd.h>
                     52:
                     53: #include "ctags.h"
                     54:
                     55: /*
                     56:  * ctags: create a tags file
                     57:  */
                     58:
                     59: NODE   *head;                  /* head of the sorted binary tree */
                     60:
                     61:                                /* boolean "func" (see init()) */
                     62: bool   _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
                     63:
                     64: FILE   *inf;                   /* ioptr for current input file */
                     65: FILE   *outf;                  /* ioptr for tags file */
                     66:
                     67: long   lineftell;              /* ftell after getc( inf ) == '\n' */
                     68:
                     69: int    lineno;                 /* line number of current line */
                     70: int    dflag;                  /* -d: non-macro defines */
                     71: int    tflag;                  /* -t: create tags for typedefs */
                     72: int    vflag;                  /* -v: vgrind style index output */
                     73: int    wflag;                  /* -w: suppress warnings */
                     74: int    xflag;                  /* -x: cxref style output */
                     75:
                     76: char   *curfile;               /* current input file name */
                     77: char   searchar = '/';         /* use /.../ searches by default */
                     78: char   lbuf[LINE_MAX];
                     79:
1.6       millert    80: void   init(void);
                     81: void   find_entries(char *);
1.1       deraadt    82:
                     83: int
1.10    ! deraadt    84: main(int argc, char *argv[])
1.1       deraadt    85: {
                     86:        static char     *outfile = "tags";      /* output file */
                     87:        int     aflag;                          /* -a: append to tags */
                     88:        int     uflag;                          /* -u: update tags */
                     89:        int     exit_val;                       /* exit value */
                     90:        int     step;                           /* step through args */
                     91:        int     ch;                             /* getopts char */
1.5       deraadt    92:        char    *cmd;
1.1       deraadt    93:
                     94:        aflag = uflag = NO;
1.3       millert    95:        while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
1.1       deraadt    96:                switch(ch) {
                     97:                case 'B':
                     98:                        searchar = '?';
                     99:                        break;
                    100:                case 'F':
                    101:                        searchar = '/';
                    102:                        break;
                    103:                case 'a':
                    104:                        aflag++;
                    105:                        break;
                    106:                case 'd':
                    107:                        dflag++;
                    108:                        break;
                    109:                case 'f':
                    110:                        outfile = optarg;
                    111:                        break;
                    112:                case 't':
                    113:                        tflag++;
                    114:                        break;
                    115:                case 'u':
                    116:                        uflag++;
                    117:                        break;
                    118:                case 'w':
                    119:                        wflag++;
                    120:                        break;
                    121:                case 'v':
                    122:                        vflag++;
                    123:                case 'x':
                    124:                        xflag++;
                    125:                        break;
                    126:                case '?':
                    127:                default:
                    128:                        goto usage;
                    129:                }
                    130:        argv += optind;
                    131:        argc -= optind;
                    132:        if (!argc) {
                    133: usage:         (void)fprintf(stderr,
                    134:                        "usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
                    135:                exit(1);
                    136:        }
                    137:
                    138:        init();
                    139:
                    140:        for (exit_val = step = 0; step < argc; ++step)
                    141:                if (!(inf = fopen(argv[step], "r"))) {
                    142:                        warn("%s", argv[step]);
                    143:                        exit_val = 1;
                    144:                }
                    145:                else {
                    146:                        curfile = argv[step];
                    147:                        find_entries(argv[step]);
                    148:                        (void)fclose(inf);
                    149:                }
                    150:
1.4       deraadt   151:        if (head) {
1.1       deraadt   152:                if (xflag)
                    153:                        put_entries(head);
                    154:                else {
                    155:                        if (uflag) {
                    156:                                for (step = 0; step < argc; step++) {
1.8       pvalchev  157:                                        if (asprintf(&cmd,
1.5       deraadt   158:                                            "mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
1.8       pvalchev  159:                                            outfile, argv[step], outfile) == -1)
1.5       deraadt   160:                                                err(1, "out of space");
1.1       deraadt   161:                                        system(cmd);
1.5       deraadt   162:                                        free(cmd);
                    163:                                        cmd = NULL;
1.1       deraadt   164:                                }
                    165:                                ++aflag;
                    166:                        }
                    167:                        if (!(outf = fopen(outfile, aflag ? "a" : "w")))
                    168:                                err(exit_val, "%s", outfile);
                    169:                        put_entries(head);
                    170:                        (void)fclose(outf);
                    171:                        if (uflag) {
1.8       pvalchev  172:                                if (asprintf(&cmd, "sort -o %s %s",
                    173:                                    outfile, outfile) == -1)
1.5       deraadt   174:                                                err(1, "out of space");
1.1       deraadt   175:                                system(cmd);
1.5       deraadt   176:                                free(cmd);
                    177:                                cmd = NULL;
1.1       deraadt   178:                        }
                    179:                }
1.4       deraadt   180:        }
1.1       deraadt   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
1.10    ! deraadt   195: init(void)
1.1       deraadt   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
1.10    ! deraadt   227: find_entries(char *file)
1.1       deraadt   228: {
                    229:        char    *cp;
                    230:
                    231:        lineno = 0;                             /* should be 1 ?? KB */
1.5       deraadt   232:        if ((cp = strrchr(file, '.'))) {
1.1       deraadt   233:                if (cp[1] == 'l' && !cp[2]) {
                    234:                        int     c;
                    235:
                    236:                        for (;;) {
                    237:                                if (GETC(==, EOF))
                    238:                                        return;
                    239:                                if (!iswhite(c)) {
                    240:                                        rewind(inf);
                    241:                                        break;
                    242:                                }
                    243:                        }
                    244: #define        LISPCHR ";(["
                    245: /* lisp */             if (strchr(LISPCHR, c)) {
                    246:                                l_entries();
                    247:                                return;
                    248:                        }
                    249: /* lex */              else {
                    250:                                /*
                    251:                                 * we search all 3 parts of a lex file
                    252:                                 * for C references.  This may be wrong.
                    253:                                 */
                    254:                                toss_yysec();
1.7       deraadt   255:                                (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1       deraadt   256:                                pfnote("yylex", lineno);
                    257:                                rewind(inf);
                    258:                        }
                    259:                }
                    260: /* yacc */     else if (cp[1] == 'y' && !cp[2]) {
                    261:                        /*
                    262:                         * we search only the 3rd part of a yacc file
                    263:                         * for C references.  This may be wrong.
                    264:                         */
                    265:                        toss_yysec();
1.7       deraadt   266:                        (void)strlcpy(lbuf, "%%$", sizeof lbuf);
1.1       deraadt   267:                        pfnote("yyparse", lineno);
                    268:                        y_entries();
                    269:                }
                    270: /* fortran */  else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
                    271:                        if (PF_funcs())
                    272:                                return;
                    273:                        rewind(inf);
                    274:                }
                    275:        }
                    276: /* C */        c_entries();
                    277: }