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

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