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

Annotation of src/usr.bin/rcs/rcsprog.c, Revision 1.51

1.51    ! xsa         1: /*     $OpenBSD: rcsprog.c,v 1.50 2005/11/28 14:43:59 xsa Exp $        */
1.1       deraadt     2: /*
                      3:  * Copyright (c) 2005 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.6       joris      27: #include <sys/param.h>
1.1       deraadt    28: #include <sys/wait.h>
1.8       joris      29: #include <sys/stat.h>
1.1       deraadt    30:
1.3       joris      31: #include <ctype.h>
1.1       deraadt    32: #include <err.h>
1.3       joris      33: #include <errno.h>
1.1       deraadt    34: #include <pwd.h>
1.3       joris      35: #include <string.h>
1.1       deraadt    36: #include <stdio.h>
                     37: #include <stdlib.h>
                     38: #include <unistd.h>
                     39:
                     40: #include "log.h"
                     41: #include "rcs.h"
1.9       joris      42: #include "rcsprog.h"
1.1       deraadt    43:
1.29      joris      44: #define RCS_CMD_MAXARG 128
1.42      xsa        45: #define RCS_DEFAULT_SUFFIX     ",v/"
1.51    ! xsa        46: #define RCSPROG_OPTSTRING      "A:a:b::c:e::hik:Lm:MqTUVx:z:"
1.29      joris      47:
1.1       deraadt    48: const char rcs_version[] = "OpenCVS RCS version 3.6";
1.12      joris      49: int verbose = 1;
1.34      joris      50: int pipeout = 0;
1.1       deraadt    51:
1.36      xsa        52: int     rcs_optind;
1.28      joris      53: char   *rcs_optarg;
1.42      xsa        54: char   *rcs_suffixes;
1.36      xsa        55: char   *rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.28      joris      56:
1.1       deraadt    57: struct rcs_prog {
1.26      deraadt    58:        char    *prog_name;
                     59:        int     (*prog_hdlr)(int, char **);
                     60:        void    (*prog_usage)(void);
1.1       deraadt    61: } programs[] = {
1.2       deraadt    62:        { "rcs",        rcs_main,       rcs_usage       },
1.17      joris      63:        { "ci",         checkin_main,   checkin_usage   },
1.11      joris      64:        { "co",         checkout_main,  checkout_usage  },
1.20      joris      65:        { "rcsclean",   rcsclean_main,  rcsclean_usage  },
1.18      joris      66:        { "rcsdiff",    rcsdiff_main,   rcsdiff_usage   },
1.33      xsa        67:        { "rcsmerge",   rcsmerge_main,  rcsmerge_usage  },
1.21      joris      68:        { "rlog",       rlog_main,      rlog_usage      },
1.22      joris      69:        { "ident",      ident_main,     ident_usage     },
1.1       deraadt    70: };
1.31      joris      71:
                     72: void
                     73: rcs_set_rev(const char *str, RCSNUM **rev)
                     74: {
1.32      joris      75:        if (str == NULL)
                     76:                return;
                     77:
1.31      joris      78:        if (*rev != RCS_HEAD_REV)
                     79:                cvs_log(LP_WARN, "redefinition of revision number");
                     80:
                     81:        if ((*rev = rcsnum_parse(str)) == NULL) {
                     82:                cvs_log(LP_ERR, "bad revision number '%s'", str);
                     83:                exit (1);
                     84:        }
1.47      xsa        85: }
                     86:
                     87: /*
                     88:  * rcs_get_mtime()
                     89:  *
                     90:  * Get <filename> last modified time.
                     91:  * Returns last modified time on success, or -1 on failure.
                     92:  */
                     93: time_t
                     94: rcs_get_mtime(const char *filename)
                     95: {
                     96:        struct stat st;
                     97:        time_t mtime;
                     98:
                     99:        if (stat(filename, &st) == -1) {
                    100:                cvs_log(LP_ERRNO, "failed to stat `%s'", filename);
                    101:                return (-1);
                    102:        }
                    103:        mtime = (time_t)st.st_mtimespec.tv_sec;
                    104:
                    105:        return (mtime);
                    106: }
                    107:
                    108: /*
                    109:  * rcs_set_mtime()
                    110:  *
                    111:  * Set <filename> last modified time to <mtime> if it's not set to -1.
                    112:  * Returns 0 on success, or -1 on failure.
                    113:  */
                    114: int
                    115: rcs_set_mtime(const char *filename, time_t mtime)
                    116: {
                    117:        static struct timeval tv[2];
                    118:
                    119:        if (mtime == -1)
                    120:                return (0);
                    121:
                    122:        tv[0].tv_sec = mtime;
                    123:        tv[1].tv_sec = tv[0].tv_sec;
                    124:
                    125:        if (utimes(filename, tv) == -1) {
                    126:                cvs_log(LP_ERRNO, "error setting utimes");
                    127:                return (-1);
                    128:        }
                    129:
                    130:        return (0);
1.31      joris     131: }
1.1       deraadt   132:
1.9       joris     133: int
1.29      joris     134: rcs_init(char *envstr, char **argv, int argvlen)
                    135: {
                    136:        u_int i;
                    137:        int argc, error;
                    138:        char linebuf[256],  *lp, *cp;
                    139:
                    140:        strlcpy(linebuf, envstr, sizeof(linebuf));
                    141:        memset(argv, 0, argvlen * sizeof(char *));
                    142:
                    143:        error = argc = 0;
                    144:        for (lp = linebuf; lp != NULL;) {
                    145:                cp = strsep(&lp, " \t\b\f\n\r\t\v");;
                    146:                if (cp == NULL)
                    147:                        break;
                    148:                else if (*cp == '\0')
                    149:                        continue;
                    150:
                    151:                if (argc == argvlen) {
                    152:                        error++;
                    153:                        break;
                    154:                }
                    155:
                    156:                argv[argc] = strdup(cp);
                    157:                if (argv[argc] == NULL) {
                    158:                        cvs_log(LP_ERRNO, "failed to copy argument");
                    159:                        error++;
                    160:                        break;
                    161:                }
                    162:
                    163:                argc++;
                    164:        }
                    165:
                    166:        if (error != 0) {
                    167:                for (i = 0; i < (u_int)argc; i++)
                    168:                        free(argv[i]);
                    169:                argc = -1;
                    170:        }
                    171:
                    172:        return (argc);
                    173: }
                    174:
                    175: int
1.28      joris     176: rcs_getopt(int argc, char **argv, const char *optstr)
                    177: {
                    178:        char *a;
                    179:        const char *c;
                    180:        static int i = 1;
                    181:        int opt, hasargument, ret;
                    182:
                    183:        hasargument = 0;
                    184:        rcs_optarg = NULL;
                    185:
                    186:        if (i >= argc)
                    187:                return (-1);
                    188:
                    189:        a = argv[i++];
                    190:        if (*a++ != '-')
                    191:                return (-1);
                    192:
                    193:        ret = 0;
                    194:        opt = *a;
                    195:        for (c = optstr; *c != '\0'; c++) {
                    196:                if (*c == opt) {
                    197:                        a++;
                    198:                        ret = opt;
                    199:
                    200:                        if (*(c + 1) == ':') {
                    201:                                if (*(c + 2) == ':') {
                    202:                                        if (*a != '\0')
                    203:                                                hasargument = 1;
                    204:                                } else {
                    205:                                        if (*a != '\0') {
                    206:                                                hasargument = 1;
                    207:                                        } else {
                    208:                                                ret = 1;
                    209:                                                break;
                    210:                                        }
                    211:                                }
                    212:                        }
                    213:
                    214:                        if (hasargument == 1)
                    215:                                rcs_optarg = a;
                    216:
                    217:                        if (ret == opt)
                    218:                                rcs_optind++;
                    219:                        break;
                    220:                }
                    221:        }
                    222:
                    223:        if (ret == 0)
                    224:                cvs_log(LP_ERR, "unknown option -%c", opt);
                    225:        else if (ret == 1)
                    226:                cvs_log(LP_ERR, "missing argument for option -%c", opt);
                    227:
                    228:        return (ret);
                    229: }
                    230:
                    231: int
1.9       joris     232: rcs_statfile(char *fname, char *out, size_t len)
                    233: {
1.42      xsa       234:        int l, found, strdir;
                    235:        char defaultsuffix[] = RCS_DEFAULT_SUFFIX;
1.9       joris     236:        char filev[MAXPATHLEN], fpath[MAXPATHLEN];
1.42      xsa       237:        char *ext, *slash;
1.9       joris     238:        struct stat st;
                    239:
1.42      xsa       240:        strdir = found = 0;
                    241:
                    242:        /* we might have gotten a RCS file as argument */
                    243:        if ((ext = strchr(fname, ',')) != NULL)
                    244:                *ext = '\0';
                    245:
                    246:        /* we might have gotten the RCS/ dir in the argument string */
                    247:        if (strstr(fname, RCSDIR) != NULL)
                    248:                strdir = 1;
                    249:
                    250:        if (rcs_suffixes != NULL)
                    251:                ext = rcs_suffixes;
                    252:        else
                    253:                ext = defaultsuffix;
1.43      xsa       254:
1.42      xsa       255:        for (;;) {
                    256:                /*
                    257:                 * GNU documentation says -x,v/ specifies two suffixes,
                    258:                 * namely the ,v one and an empty one (which matches
                    259:                 * everything).
                    260:                 * The problem is that they don't follow this rule at
                    261:                 * all, and their documentation seems flawed.
                    262:                 * We try to be compatible, so let's do so.
                    263:                 */
                    264:                if (*ext == '\0')
                    265:                        break;
                    266:
                    267:                if ((slash = strchr(ext, '/')) != NULL)
                    268:                        *slash = '\0';
1.9       joris     269:
1.42      xsa       270:                l = snprintf(filev, sizeof(filev), "%s%s", fname, ext);
1.43      xsa       271:                if (l == -1 || l >= (int)sizeof(filev)) {
                    272:                        errno = ENAMETOOLONG;
                    273:                        cvs_log(LP_ERRNO, "%s", filev);
1.9       joris     274:                        return (-1);
1.43      xsa       275:                }
1.42      xsa       276:
                    277:                if ((strdir == 0) &&
                    278:                    (stat(RCSDIR, &st) != -1) && (st.st_mode & S_IFDIR)) {
                    279:                        l = snprintf(fpath, sizeof(fpath), "%s/%s",
                    280:                            RCSDIR, filev);
1.43      xsa       281:                        if (l == -1 || l >= (int)sizeof(fpath)) {
                    282:                                errno = ENAMETOOLONG;
                    283:                                cvs_log(LP_ERRNO, "%s", fpath);
1.42      xsa       284:                                return (-1);
1.43      xsa       285:                        }
1.42      xsa       286:                } else {
                    287:                        strlcpy(fpath, filev, sizeof(fpath));
                    288:                }
                    289:
                    290:                if (stat(fpath, &st) != -1) {
                    291:                        found++;
                    292:                        break;
                    293:                }
                    294:
                    295:                if (slash == NULL)
                    296:                        break;
                    297:
                    298:                *slash++ = '/';
                    299:                ext = slash;
1.9       joris     300:        }
                    301:
1.42      xsa       302:        if (found != 1) {
1.44      niallo    303:                if ((strcmp(__progname, "rcsclean") != 0)
                    304:                    && (strcmp(__progname, "ci") != 0))
1.20      joris     305:                        cvs_log(LP_ERRNO, "%s", fpath);
1.9       joris     306:                return (-1);
                    307:        }
                    308:
                    309:        strlcpy(out, fpath, len);
                    310:
                    311:        return (0);
                    312: }
1.1       deraadt   313:
                    314: int
                    315: main(int argc, char **argv)
                    316: {
                    317:        u_int i;
1.29      joris     318:        char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
                    319:        int ret, cmd_argc;
1.1       deraadt   320:
                    321:        ret = -1;
1.28      joris     322:        rcs_optind = 1;
1.9       joris     323:        cvs_log_init(LD_STD, 0);
1.1       deraadt   324:
1.29      joris     325:        cmd_argc = 0;
1.30      joris     326:        cmd_argv[cmd_argc++] = argv[0];
1.29      joris     327:        if ((rcsinit = getenv("RCSINIT")) != NULL) {
                    328:                ret = rcs_init(rcsinit, cmd_argv + 1,
                    329:                    RCS_CMD_MAXARG - 1);
                    330:                if (ret < 0) {
                    331:                        cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
                    332:                        exit (1);
                    333:                }
                    334:
                    335:                cmd_argc += ret;
                    336:        }
1.36      xsa       337:
                    338:        if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
                    339:                rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29      joris     340:
                    341:        for (ret = 1; ret < argc; ret++)
                    342:                cmd_argv[cmd_argc++] = argv[ret];
                    343:
1.1       deraadt   344:        for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2       deraadt   345:                if (strcmp(__progname, programs[i].prog_name) == 0) {
                    346:                        usage = programs[i].prog_usage;
1.29      joris     347:                        ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2       deraadt   348:                        break;
                    349:                }
1.1       deraadt   350:
1.23      niallo    351:        exit(ret);
1.1       deraadt   352: }
                    353:
                    354:
                    355: void
                    356: rcs_usage(void)
                    357: {
                    358:        fprintf(stderr,
1.50      xsa       359:            "usage: rcs [-hiLMTUV] [-Aoldfile] [-ausers] [-b[rev]] [-cstring]\n"
1.51    ! xsa       360:            "           [-eusers] [-kmode] [-mrev:log] [-xsuffixes] [-ztz] file ...\n");
1.1       deraadt   361: }
                    362:
                    363: /*
                    364:  * rcs_main()
                    365:  *
                    366:  * Handler for the `rcs' program.
                    367:  * Returns 0 on success, or >0 on error.
                    368:  */
                    369: int
                    370: rcs_main(int argc, char **argv)
                    371: {
                    372:        int i, ch, flags, kflag, lkmode;
1.39      xsa       373:        char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.24      joris     374:        char *logstr, *logmsg;
1.39      xsa       375:        char *alist, *comment, *elist, *unp, *sp;
1.1       deraadt   376:        mode_t fmode;
1.39      xsa       377:        RCSFILE *file, *oldfile;
1.24      joris     378:        RCSNUM *logrev;
1.39      xsa       379:        struct rcs_access *acp;
1.48      xsa       380:        time_t rcs_mtime = -1;
1.1       deraadt   381:
                    382:        kflag = lkmode = -1;
                    383:        fmode = 0;
                    384:        flags = RCS_RDWR;
1.39      xsa       385:        logstr = alist = comment = elist = NULL;
1.1       deraadt   386:
1.51    ! xsa       387:        while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1       deraadt   388:                switch (ch) {
                    389:                case 'A':
1.39      xsa       390:                        if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
                    391:                                exit(1);
1.41      niallo    392:                        flags |= CO_ACLAPPEND;
1.1       deraadt   393:                        break;
                    394:                case 'a':
1.28      joris     395:                        alist = rcs_optarg;
1.1       deraadt   396:                        break;
                    397:                case 'c':
1.28      joris     398:                        comment = rcs_optarg;
1.1       deraadt   399:                        break;
                    400:                case 'e':
1.28      joris     401:                        elist = rcs_optarg;
1.1       deraadt   402:                        break;
                    403:                case 'h':
1.2       deraadt   404:                        (usage)();
1.1       deraadt   405:                        exit(0);
                    406:                case 'i':
                    407:                        flags |= RCS_CREATE;
                    408:                        break;
                    409:                case 'k':
1.28      joris     410:                        kflag = rcs_kflag_get(rcs_optarg);
1.1       deraadt   411:                        if (RCS_KWEXP_INVAL(kflag)) {
                    412:                                cvs_log(LP_ERR,
                    413:                                    "invalid keyword substitution mode `%s'",
1.28      joris     414:                                    rcs_optarg);
1.1       deraadt   415:                                exit(1);
                    416:                        }
                    417:                        break;
                    418:                case 'L':
                    419:                        if (lkmode == RCS_LOCK_LOOSE)
                    420:                                cvs_log(LP_WARN, "-U overriden by -L");
                    421:                        lkmode = RCS_LOCK_STRICT;
                    422:                        break;
1.24      joris     423:                case 'm':
1.28      joris     424:                        if ((logstr = strdup(rcs_optarg)) == NULL) {
1.24      joris     425:                                cvs_log(LP_ERRNO, "failed to copy logstring");
                    426:                                exit(1);
                    427:                        }
                    428:                        break;
1.1       deraadt   429:                case 'M':
                    430:                        /* ignore for the moment */
                    431:                        break;
1.12      joris     432:                case 'q':
                    433:                        verbose = 0;
1.46      xsa       434:                        break;
                    435:                case 'T':
                    436:                        flags |= PRESERVETIME;
1.12      joris     437:                        break;
1.1       deraadt   438:                case 'U':
                    439:                        if (lkmode == RCS_LOCK_STRICT)
                    440:                                cvs_log(LP_WARN, "-L overriden by -U");
                    441:                        lkmode = RCS_LOCK_LOOSE;
                    442:                        break;
                    443:                case 'V':
                    444:                        printf("%s\n", rcs_version);
                    445:                        exit(0);
1.45      xsa       446:                case 'x':
                    447:                        rcs_suffixes = rcs_optarg;
1.51    ! xsa       448:                        break;
        !           449:                case 'z':
        !           450:                        /*
        !           451:                         * kept for compatibility
        !           452:                         */
1.45      xsa       453:                        break;
1.1       deraadt   454:                default:
1.2       deraadt   455:                        (usage)();
1.1       deraadt   456:                        exit(1);
                    457:                }
                    458:        }
                    459:
1.28      joris     460:        argc -= rcs_optind;
                    461:        argv += rcs_optind;
1.30      joris     462:
1.1       deraadt   463:        if (argc == 0) {
                    464:                cvs_log(LP_ERR, "no input file");
1.5       joris     465:                (usage)();
1.1       deraadt   466:                exit(1);
                    467:        }
                    468:
                    469:        for (i = 0; i < argc; i++) {
1.9       joris     470:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     471:                        continue;
1.6       joris     472:
1.35      niallo    473:                if (verbose == 1)
                    474:                        printf("RCS file: %s\n", fpath);
1.48      xsa       475:
1.49      niallo    476:                if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6       joris     477:                        continue;
1.1       deraadt   478:
1.48      xsa       479:                if (flags & PRESERVETIME)
                    480:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    481:
1.24      joris     482:                if (logstr != NULL) {
                    483:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    484:                                cvs_log(LP_ERR, "missing log message");
                    485:                                rcs_close(file);
                    486:                                continue;
                    487:                        }
                    488:
                    489:                        *logmsg++ = '\0';
                    490:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48      xsa       491:                                cvs_log(LP_ERR,
                    492:                                    "'%s' bad revision number", logstr);
1.24      joris     493:                                rcs_close(file);
                    494:                                continue;
                    495:                        }
                    496:
                    497:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    498:                                cvs_log(LP_ERR,
                    499:                                    "failed to set logmsg for '%s' to '%s'",
                    500:                                    logstr, logmsg);
                    501:                                rcs_close(file);
1.25      joris     502:                                rcsnum_free(logrev);
1.24      joris     503:                                continue;
                    504:                        }
                    505:
                    506:                        rcsnum_free(logrev);
1.39      xsa       507:                }
                    508:
                    509:                /* entries to add from <oldfile> */
1.41      niallo    510:                if (flags & CO_ACLAPPEND) {
1.39      xsa       511:                        /* XXX */
                    512:                        if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
                    513:                                exit(1);
                    514:
                    515:                        TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
                    516:                                rcs_access_add(file, acp->ra_name);
                    517:
                    518:                        rcs_close(oldfile);
1.24      joris     519:                }
                    520:
1.1       deraadt   521:                /* entries to add to the access list */
                    522:                if (alist != NULL) {
                    523:                        unp = alist;
                    524:                        do {
                    525:                                sp = strchr(unp, ',');
                    526:                                if (sp != NULL)
                    527:                                        *(sp++) = '\0';
                    528:
                    529:                                rcs_access_add(file, unp);
                    530:
                    531:                                unp = sp;
                    532:                        } while (sp != NULL);
                    533:                }
                    534:
                    535:                if (comment != NULL)
                    536:                        rcs_comment_set(file, comment);
                    537:
                    538:                if (kflag != -1)
                    539:                        rcs_kwexp_set(file, kflag);
                    540:
                    541:                if (lkmode != -1)
                    542:                        rcs_lock_setmode(file, lkmode);
                    543:
                    544:                rcs_close(file);
1.48      xsa       545:
                    546:                if (flags & PRESERVETIME)
                    547:                        rcs_set_mtime(fpath, rcs_mtime);
1.9       joris     548:
1.14      xsa       549:                if (verbose == 1)
1.12      joris     550:                        printf("done\n");
1.1       deraadt   551:        }
1.24      joris     552:
                    553:        if (logstr != NULL)
                    554:                free(logstr);
1.1       deraadt   555:
                    556:        return (0);
                    557: }