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

Annotation of src/usr.bin/rcs/rlog.c, Revision 1.24

1.24    ! jmc         1: /*     $OpenBSD: rlog.c,v 1.23 2006/01/25 08:02:26 xsa Exp $   */
1.1       joris       2: /*
                      3:  * Copyright (c) 2005 Joris Vink <joris@openbsd.org>
1.20      xsa         4:  * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1       joris       5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  *
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. The name of the author may not be used to endorse or promote products
                     14:  *    derived from this software without specific prior written permission.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     17:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     18:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     19:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     20:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     21:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     22:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     23:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     24:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     25:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
1.18      xsa        28: #include "includes.h"
1.1       joris      29:
1.19      xsa        30: #include "rcsprog.h"
1.3       niallo     31: #include "diff.h"
1.1       joris      32:
1.23      xsa        33: static int       rlog_file(const char *, const char *);
1.20      xsa        34: static void      rlog_rev_print(struct rcs_delta *);
                     35: static char    **rlog_strsplit(char *, const char *);
1.1       joris      36:
                     37: #define REVSEP         "----------------------------"
                     38: #define REVEND \
1.6       xsa        39:  "============================================================================="
1.1       joris      40:
1.23      xsa        41: static int hflag, Lflag, lflag, tflag, Nflag, wflag;
                     42: static char *llist = NULL;
1.20      xsa        43: static char *slist = NULL;
                     44: static char *wlist = NULL;
1.23      xsa        45: static RCSFILE *file;
1.1       joris      46:
1.21      xsa        47: void
                     48: rlog_usage(void)
                     49: {
                     50:        fprintf(stderr,
1.24    ! jmc        51:            "usage: rlog [-hLNqRtV] [-l[lockers]] [-sstates] [-w[logins]]\n"
1.23      xsa        52:            "            [-xsuffixes] file ...\n");
1.21      xsa        53: }
                     54:
1.1       joris      55: int
                     56: rlog_main(int argc, char **argv)
                     57: {
                     58:        int Rflag;
                     59:        int i, ch;
                     60:        char fpath[MAXPATHLEN];
                     61:
                     62:        hflag = Rflag = 0;
1.23      xsa        63:        while ((ch = rcs_getopt(argc, argv, "hLl::NqRs:TtVw::x:")) != -1) {
1.1       joris      64:                switch (ch) {
                     65:                case 'h':
                     66:                        hflag = 1;
                     67:                        break;
1.11      xsa        68:                case 'L':
                     69:                        Lflag = 1;
                     70:                        break;
1.23      xsa        71:                case 'l':
                     72:                        lflag = 1;
                     73:                        llist = rcs_optarg;
                     74:                        break;
1.1       joris      75:                case 'N':
                     76:                        Nflag = 1;
                     77:                        break;
                     78:                case 'q':
                     79:                        verbose = 0;
                     80:                        break;
                     81:                case 'R':
                     82:                        Rflag = 1;
                     83:                        break;
1.20      xsa        84:                case 's':
                     85:                        slist = rcs_optarg;
                     86:                        break;
1.8       xsa        87:                case 'T':
                     88:                        /*
                     89:                         * kept for compatibility
                     90:                         */
                     91:                        break;
1.1       joris      92:                case 't':
                     93:                        tflag = 1;
                     94:                        break;
                     95:                case 'V':
                     96:                        printf("%s\n", rcs_version);
                     97:                        exit(0);
1.20      xsa        98:                case 'w':
                     99:                        wflag = 1;
                    100:                        wlist = rcs_optarg;
                    101:                        break;
1.14      xsa       102:                case 'x':
                    103:                        rcs_suffixes = rcs_optarg;
                    104:                        break;
1.1       joris     105:                default:
1.21      xsa       106:                        (usage());
                    107:                        exit(1);
1.1       joris     108:                }
                    109:        }
                    110:
1.5       joris     111:        argc -= rcs_optind;
                    112:        argv += rcs_optind;
1.1       joris     113:
                    114:        if (argc == 0) {
                    115:                cvs_log(LP_ERR, "no input file");
                    116:                (usage)();
                    117:                exit(1);
                    118:        }
                    119:
1.7       xsa       120:        if ((hflag == 1) && (tflag == 1)) {
1.6       xsa       121:                cvs_log(LP_WARN, "warning: -t overrides -h.");
1.7       xsa       122:                hflag = 0;
                    123:        }
1.6       xsa       124:
1.1       joris     125:        for (i = 0; i < argc; i++) {
                    126:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    127:                        continue;
                    128:
1.17      niallo    129:                if ((file = rcs_open(fpath, RCS_READ|RCS_PARSE_FULLY)) == NULL)
1.11      xsa       130:                        continue;
                    131:
                    132:                if ((Lflag == 1) && (TAILQ_EMPTY(&(file->rf_locks)))) {
                    133:                        rcs_close(file);
                    134:                        continue;
                    135:                }
                    136:
1.9       xsa       137:                if (Rflag == 1) {
                    138:                        printf("%s\n", fpath);
1.11      xsa       139:                        rcs_close(file);
1.9       xsa       140:                        continue;
                    141:                }
                    142:
1.23      xsa       143:                rlog_file(argv[i], fpath);
1.9       xsa       144:
1.1       joris     145:                rcs_close(file);
                    146:        }
                    147:
                    148:        return (0);
                    149: }
                    150:
                    151: static int
1.23      xsa       152: rlog_file(const char *fname, const char *fpath)
1.1       joris     153: {
                    154:        char numb[64];
                    155:        struct rcs_sym *sym;
                    156:        struct rcs_access *acp;
1.20      xsa       157:        struct rcs_delta *rdp;
1.10      xsa       158:        struct rcs_lock *lkp;
1.1       joris     159:
1.12      xsa       160:        printf("\nRCS file: %s", fpath);
1.6       xsa       161:        printf("\nWorking file: %s", fname);
1.1       joris     162:        printf("\nhead:");
                    163:        if (file->rf_head != NULL)
                    164:                printf(" %s", rcsnum_tostr(file->rf_head, numb, sizeof(numb)));
                    165:
                    166:        printf("\nbranch:");
                    167:        if (rcs_branch_get(file) != NULL) {
                    168:                printf(" %s", rcsnum_tostr(rcs_branch_get(file),
                    169:                    numb, sizeof(numb)));
                    170:        }
                    171:
                    172:        printf("\nlocks: %s", (file->rf_flags & RCS_SLOCK) ? "strict" : "");
1.10      xsa       173:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list)
                    174:                printf("\n\t%s: %s", lkp->rl_name,
                    175:                    rcsnum_tostr(lkp->rl_num, numb, sizeof(numb)));
1.1       joris     176:        printf("\naccess list:\n");
                    177:        TAILQ_FOREACH(acp, &(file->rf_access), ra_list)
                    178:                printf("\t%s\n", acp->ra_name);
                    179:
                    180:        if (Nflag == 0) {
                    181:                printf("symbolic names:\n");
                    182:                TAILQ_FOREACH(sym, &(file->rf_symbols), rs_list) {
                    183:                        printf("\t%s: %s\n", sym->rs_name,
                    184:                            rcsnum_tostr(sym->rs_num, numb, sizeof(numb)));
                    185:                }
                    186:        }
                    187:
                    188:        printf("keyword substitution: %s\n",
                    189:            file->rf_expand == NULL ? "kv" : file->rf_expand);
                    190:
1.20      xsa       191:        printf("total revisions: %u", file->rf_ndelta);
                    192:
                    193:        if ((hflag == 0) && (tflag == 0))
                    194:                printf(";\tselected revisions:"); /* XXX */
                    195:
                    196:        printf("\n");
                    197:
1.1       joris     198:
                    199:        if ((hflag == 0) || (tflag == 1))
1.13      xsa       200:                printf("description:\n%s", file->rf_desc);
1.1       joris     201:
1.20      xsa       202:        if ((hflag == 0) && (tflag == 0)) {
                    203:                TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list)
                    204:                        rlog_rev_print(rdp);
                    205:        }
1.1       joris     206:
                    207:        printf("%s\n", REVEND);
                    208:        return (0);
1.16      xsa       209: }
                    210:
                    211: static void
1.20      xsa       212: rlog_rev_print(struct rcs_delta *rdp)
1.16      xsa       213: {
1.20      xsa       214:        int i, found;
                    215:        char *author, numb[64];
1.23      xsa       216:        char **largv, **sargv, **wargv;
1.20      xsa       217:
                    218:        i = found = 0;
                    219:        author = NULL;
                    220:
1.23      xsa       221:        /* -l[lockers] */
                    222:        if (lflag == 1) {
                    223:                /* if no locks at all, abort. */
                    224:                if (TAILQ_EMPTY(&(file->rf_locks)))
                    225:                        return;
                    226:                else
                    227:                        if (rdp->rd_locker != NULL)
                    228:                                found++;
                    229:
                    230:                if (llist != NULL) {
                    231:                        /* if locker is empty, no need to go further. */
                    232:                        if (rdp->rd_locker == NULL)
                    233:                                return;
                    234:                        largv = rlog_strsplit(llist, ",");
                    235:                        for (i = 0; largv[i] != NULL; i++) {
                    236:                                if (strcmp(rdp->rd_locker, largv[i]) == 0) {
                    237:                                        found++;
                    238:                                        break;
                    239:                                }
                    240:                                found = 0;
                    241:                        }
                    242:                }
                    243:        }
1.20      xsa       244:        /* -sstates */
                    245:        if (slist != NULL) {
                    246:                sargv = rlog_strsplit(slist, ",");
                    247:                for (i = 0; sargv[i] != NULL; i++) {
                    248:                        if (strcmp(rdp->rd_state, sargv[i]) == 0) {
                    249:                                found++;
                    250:                                break;
                    251:                        }
                    252:                        found = 0;
                    253:                }
                    254:        }
                    255:        /* -w[logins] */
                    256:        if (wflag == 1) {
                    257:                if (wlist != NULL) {
                    258:                        wargv = rlog_strsplit(wlist, ",");
                    259:                        for (i = 0; wargv[i] != NULL; i++) {
                    260:                                if (strcmp(rdp->rd_author, wargv[i]) == 0) {
                    261:                                        found++;
                    262:                                        break;
                    263:                                }
                    264:                                found = 0;
                    265:                        }
                    266:                } else {
                    267:                        if ((author = getlogin()) == NULL)
                    268:                                fatal("getlogin failed");
1.16      xsa       269:
1.20      xsa       270:                        if (strcmp(rdp->rd_author, author) == 0)
                    271:                                found++;
                    272:                }
                    273:        }
1.16      xsa       274:
1.20      xsa       275:        /* XXX dirty... */
1.23      xsa       276:        if (((((slist != NULL) && (wflag == 1)) ||
                    277:            ((slist != NULL) && (lflag == 1)) ||
                    278:            ((lflag == 1) && (wflag == 1))) && (found < 2)) ||
                    279:            ((((slist != NULL) && (lflag == 1) && (wflag == 1)) ||
                    280:            ((slist != NULL) || (lflag == 1) || (wflag == 1))) && (found == 0)))
1.20      xsa       281:                return;
                    282:
                    283:        printf("%s\n", REVSEP);
                    284:
                    285:        rcsnum_tostr(rdp->rd_num, numb, sizeof(numb));
                    286:
1.22      xsa       287:        printf("revision %s", numb);
                    288:        if (rdp->rd_locker != NULL)
                    289:                printf("\tlocked by: %s;", rdp->rd_locker);
                    290:        printf("\ndate: %d/%02d/%02d %02d:%02d:%02d;"
1.20      xsa       291:            "  author: %s;  state: %s;\n",
                    292:            rdp->rd_date.tm_year + 1900,
                    293:            rdp->rd_date.tm_mon + 1,
                    294:            rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
                    295:            rdp->rd_date.tm_min, rdp->rd_date.tm_sec,
                    296:            rdp->rd_author, rdp->rd_state);
                    297:        printf("%s", rdp->rd_log);
                    298: }
1.16      xsa       299:
1.20      xsa       300: /*
                    301:  * rlog_strsplit()
                    302:  *
                    303:  * Split a string <str> of <sep>-separated values and allocate
                    304:  * an argument vector for the values found.
                    305:  */
                    306: static char **
                    307: rlog_strsplit(char *str, const char *sep)
                    308: {
                    309:        char **argv, **nargv;
                    310:        char *cp, *p;
                    311:        int i = 0;
                    312:
                    313:        cp = xstrdup(str);
                    314:        argv = (char **)xmalloc((i+1) * sizeof(char *));
                    315:
                    316:        while ((p = strsep(&cp, sep)) != NULL) {
                    317:                argv[i++] = p;
                    318:                nargv = (char **)xrealloc((void *)argv, (i+1) * sizeof(char *));
                    319:                argv = nargv;
1.16      xsa       320:        }
1.20      xsa       321:        argv[i] = NULL;
                    322:
                    323:        return (argv);
1.1       joris     324: }