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

1.99    ! ray         1: /*     $OpenBSD: rcsprog.c,v 1.98 2006/04/12 22:54:23 ray 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.59      xsa        27: #include "includes.h"
1.1       deraadt    28:
1.9       joris      29: #include "rcsprog.h"
1.1       deraadt    30:
1.29      joris      31: #define RCS_CMD_MAXARG 128
1.93      xsa        32: #define RCSPROG_OPTSTRING      "A:a:b::c:e::hik:Ll::m:Mn:N:qt::TUu::Vx::z::"
1.56      joris      33:
                     34: #define DESC_PROMPT    "enter description, terminated with single '.' "      \
                     35:                        "or end of file:\nNOTE: This is NOT the log message!" \
                     36:                        "\n>> "
1.29      joris      37:
1.1       deraadt    38: const char rcs_version[] = "OpenCVS RCS version 3.6";
1.12      joris      39: int verbose = 1;
1.34      joris      40: int pipeout = 0;
1.1       deraadt    41:
1.87      ray        42: int     flags;
1.91      ray        43: int     rcsflags;
1.36      xsa        44: int     rcs_optind;
1.28      joris      45: char   *rcs_optarg;
1.42      xsa        46: char   *rcs_suffixes;
1.36      xsa        47: char   *rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.28      joris      48:
1.1       deraadt    49: struct rcs_prog {
1.26      deraadt    50:        char    *prog_name;
                     51:        int     (*prog_hdlr)(int, char **);
                     52:        void    (*prog_usage)(void);
1.1       deraadt    53: } programs[] = {
1.2       deraadt    54:        { "rcs",        rcs_main,       rcs_usage       },
1.17      joris      55:        { "ci",         checkin_main,   checkin_usage   },
1.11      joris      56:        { "co",         checkout_main,  checkout_usage  },
1.20      joris      57:        { "rcsclean",   rcsclean_main,  rcsclean_usage  },
1.18      joris      58:        { "rcsdiff",    rcsdiff_main,   rcsdiff_usage   },
1.33      xsa        59:        { "rcsmerge",   rcsmerge_main,  rcsmerge_usage  },
1.21      joris      60:        { "rlog",       rlog_main,      rlog_usage      },
1.22      joris      61:        { "ident",      ident_main,     ident_usage     },
1.1       deraadt    62: };
1.31      joris      63:
1.68      joris      64: struct cvs_wklhead rcs_temp_files;
                     65:
                     66: void sighdlr(int);
1.56      joris      67: static void rcs_set_description(RCSFILE *, const char *);
                     68: static void rcs_attach_symbol(RCSFILE *, const char *);
                     69:
1.78      ray        70: /* ARGSUSED */
1.31      joris      71: void
1.68      joris      72: sighdlr(int sig)
                     73: {
                     74:        cvs_worklist_clean(&rcs_temp_files, cvs_worklist_unlink);
                     75:        _exit(1);
                     76: }
                     77:
1.97      ray        78: /*
                     79:  * Allocate an RCSNUM and store in <rev>.
                     80:  */
1.68      joris      81: void
1.31      joris      82: rcs_set_rev(const char *str, RCSNUM **rev)
                     83: {
1.97      ray        84:        if (str == NULL || (*rev = rcsnum_parse(str)) == NULL)
1.55      xsa        85:                fatal("bad revision number '%s'", str);
1.47      xsa        86: }
                     87:
                     88: /*
                     89:  * rcs_get_mtime()
                     90:  *
                     91:  * Get <filename> last modified time.
                     92:  * Returns last modified time on success, or -1 on failure.
                     93:  */
                     94: time_t
                     95: rcs_get_mtime(const char *filename)
                     96: {
                     97:        struct stat st;
                     98:        time_t mtime;
                     99:
                    100:        if (stat(filename, &st) == -1) {
                    101:                cvs_log(LP_ERRNO, "failed to stat `%s'", filename);
                    102:                return (-1);
                    103:        }
                    104:        mtime = (time_t)st.st_mtimespec.tv_sec;
                    105:
                    106:        return (mtime);
                    107: }
                    108:
                    109: /*
                    110:  * rcs_set_mtime()
                    111:  *
                    112:  * Set <filename> last modified time to <mtime> if it's not set to -1.
                    113:  */
1.94      xsa       114: void
1.47      xsa       115: rcs_set_mtime(const char *filename, time_t mtime)
                    116: {
                    117:        static struct timeval tv[2];
                    118:
                    119:        if (mtime == -1)
1.94      xsa       120:                return;
1.47      xsa       121:
                    122:        tv[0].tv_sec = mtime;
                    123:        tv[1].tv_sec = tv[0].tv_sec;
                    124:
1.94      xsa       125:        if (utimes(filename, tv) == -1)
                    126:                fatal("error setting utimes: %s", strerror(errno));
1.31      joris     127: }
1.1       deraadt   128:
1.9       joris     129: int
1.29      joris     130: rcs_init(char *envstr, char **argv, int argvlen)
                    131: {
                    132:        u_int i;
                    133:        int argc, error;
                    134:        char linebuf[256],  *lp, *cp;
                    135:
                    136:        strlcpy(linebuf, envstr, sizeof(linebuf));
                    137:        memset(argv, 0, argvlen * sizeof(char *));
                    138:
                    139:        error = argc = 0;
                    140:        for (lp = linebuf; lp != NULL;) {
1.61      xsa       141:                cp = strsep(&lp, " \t\b\f\n\r\t\v");
1.29      joris     142:                if (cp == NULL)
                    143:                        break;
                    144:                else if (*cp == '\0')
                    145:                        continue;
                    146:
                    147:                if (argc == argvlen) {
                    148:                        error++;
                    149:                        break;
                    150:                }
                    151:
1.53      joris     152:                argv[argc] = xstrdup(cp);
1.29      joris     153:                argc++;
                    154:        }
                    155:
                    156:        if (error != 0) {
                    157:                for (i = 0; i < (u_int)argc; i++)
1.53      joris     158:                        xfree(argv[i]);
1.29      joris     159:                argc = -1;
                    160:        }
                    161:
                    162:        return (argc);
                    163: }
                    164:
                    165: int
1.28      joris     166: rcs_getopt(int argc, char **argv, const char *optstr)
                    167: {
                    168:        char *a;
                    169:        const char *c;
                    170:        static int i = 1;
                    171:        int opt, hasargument, ret;
                    172:
                    173:        hasargument = 0;
                    174:        rcs_optarg = NULL;
                    175:
                    176:        if (i >= argc)
                    177:                return (-1);
                    178:
                    179:        a = argv[i++];
                    180:        if (*a++ != '-')
                    181:                return (-1);
                    182:
                    183:        ret = 0;
                    184:        opt = *a;
                    185:        for (c = optstr; *c != '\0'; c++) {
                    186:                if (*c == opt) {
                    187:                        a++;
                    188:                        ret = opt;
                    189:
                    190:                        if (*(c + 1) == ':') {
                    191:                                if (*(c + 2) == ':') {
                    192:                                        if (*a != '\0')
                    193:                                                hasargument = 1;
                    194:                                } else {
                    195:                                        if (*a != '\0') {
                    196:                                                hasargument = 1;
                    197:                                        } else {
                    198:                                                ret = 1;
                    199:                                                break;
                    200:                                        }
                    201:                                }
                    202:                        }
                    203:
                    204:                        if (hasargument == 1)
                    205:                                rcs_optarg = a;
                    206:
                    207:                        if (ret == opt)
                    208:                                rcs_optind++;
                    209:                        break;
                    210:                }
                    211:        }
                    212:
                    213:        if (ret == 0)
                    214:                cvs_log(LP_ERR, "unknown option -%c", opt);
                    215:        else if (ret == 1)
                    216:                cvs_log(LP_ERR, "missing argument for option -%c", opt);
1.74      ray       217:
                    218:        return (ret);
                    219: }
                    220:
                    221: /*
                    222:  * rcs_choosefile()
                    223:  *
                    224:  * Given a relative filename, decide where the corresponding RCS file
                    225:  * should be.  Tries each extension until a file is found.  If no file
                    226:  * was found, returns a path with the first extension.
                    227:  *
                    228:  * Returns pointer to a char array on success, NULL on failure.
                    229:  */
                    230: char *
                    231: rcs_choosefile(const char *filename)
                    232: {
                    233:        struct stat sb;
                    234:        char *ext, name[MAXPATHLEN], *next, *ptr, rcsdir[MAXPATHLEN],
                    235:            *ret, *suffixes, rcspath[MAXPATHLEN];
                    236:
                    237:        /* If -x flag was not given, use default. */
                    238:        if (rcs_suffixes == NULL)
                    239:                rcs_suffixes = RCS_DEFAULT_SUFFIX;
                    240:
                    241:        /*
                    242:         * If `filename' contains a directory, `rcspath' contains that
                    243:         * directory, including a trailing slash.  Otherwise `rcspath'
                    244:         * contains an empty string.
                    245:         */
                    246:        if (strlcpy(rcspath, filename, sizeof(rcspath)) >= sizeof(rcspath))
                    247:                return (NULL);
                    248:        /* If `/' is found, end string after `/'. */
                    249:        if ((ptr = strrchr(rcspath, '/')) != NULL)
                    250:                *(++ptr) = '\0';
                    251:        else
                    252:                rcspath[0] = '\0';
                    253:
                    254:        /* Append RCS/ to `rcspath' if it exists. */
                    255:        if (strlcpy(rcsdir, rcspath, sizeof(rcsdir)) >= sizeof(rcsdir) ||
                    256:            strlcat(rcsdir, RCSDIR, sizeof(rcsdir)) >= sizeof(rcsdir))
                    257:                return (NULL);
                    258:        if ((stat(rcsdir, &sb) == 0) && (sb.st_mode & S_IFDIR))
                    259:                if (strlcpy(rcspath, rcsdir, sizeof(rcspath)) >= sizeof(rcspath) ||
                    260:                    strlcat(rcspath, "/", sizeof(rcspath)) >= sizeof(rcspath))
                    261:                        return (NULL);
                    262:
                    263:        /* Name of file without path. */
                    264:        if ((ptr = strrchr(filename, '/')) == NULL) {
                    265:                if (strlcpy(name, filename, sizeof(name)) >= sizeof(name))
                    266:                        return (NULL);
                    267:        } else {
                    268:                /* Skip `/'. */
                    269:                if (strlcpy(name, ptr + 1, sizeof(name)) >= sizeof(name))
                    270:                        return (NULL);
                    271:        }
                    272:
                    273:        /* Name of RCS file without an extension. */
                    274:        if (strlcat(rcspath, name, sizeof(rcspath)) >= sizeof(rcspath))
                    275:                return (NULL);
                    276:
                    277:        /*
                    278:         * If only the empty suffix was given, use existing rcspath.
                    279:         * This ensures that there is at least one suffix for strsep().
                    280:         */
                    281:        if (strcmp(rcs_suffixes, "") == 0) {
1.77      ray       282:                ret = xstrdup(rcspath);
1.74      ray       283:                return (ret);
                    284:        }
                    285:
                    286:        /*
                    287:         * Cycle through slash-separated `rcs_suffixes', appending each
                    288:         * extension to `rcspath' and testing if the file exists.  If it
                    289:         * does, return that string.  Otherwise return path with first
                    290:         * extension.
                    291:         */
1.77      ray       292:        suffixes = xstrdup(rcs_suffixes);
1.74      ray       293:        for (ret = NULL, next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
                    294:                char fpath[MAXPATHLEN];
                    295:
                    296:                /* Construct RCS file path. */
                    297:                if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
1.83      ray       298:                    strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath))
                    299:                        goto out;
1.74      ray       300:
                    301:                /* Don't use `filename' as RCS file. */
                    302:                if (strcmp(fpath, filename) == 0)
                    303:                        continue;
                    304:
                    305:                if (stat(fpath, &sb) == 0) {
1.77      ray       306:                        ret = xstrdup(fpath);
1.83      ray       307:                        goto out;
1.74      ray       308:                }
                    309:        }
                    310:
                    311:        /*
1.83      ray       312:         * `ret' is still NULL.  No RCS file with any extension exists
1.74      ray       313:         * so we use the first extension.
1.83      ray       314:         *
                    315:         * `suffixes' should now be NUL separated, so the first
                    316:         * extension can be read just by reading `suffixes'.
1.74      ray       317:         */
1.83      ray       318:        if (strlcat(rcspath, suffixes, sizeof(rcspath)) >=
                    319:            sizeof(rcspath))
                    320:                goto out;
                    321:        ret = xstrdup(rcspath);
1.28      joris     322:
1.83      ray       323: out:
                    324:        /* `ret' may be NULL, which indicates an error. */
1.81      ray       325:        xfree(suffixes);
1.28      joris     326:        return (ret);
                    327: }
                    328:
1.75      ray       329: /*
                    330:  * Find the name of an RCS file, given a file name `fname'.  If an RCS
                    331:  * file is found, the name is copied to the `len' sized buffer `out'.
                    332:  * Returns 0 if RCS file was found, -1 otherwise.
                    333:  */
1.28      joris     334: int
1.9       joris     335: rcs_statfile(char *fname, char *out, size_t len)
                    336: {
                    337:        struct stat st;
1.75      ray       338:        char *rcspath;
1.9       joris     339:
1.75      ray       340:        /* XXX - do this in rcs_choosefile? */
                    341:        if ((rcspath = rcs_choosefile(fname)) == NULL)
                    342:                fatal("rcs_statfile: path truncation");
1.9       joris     343:
1.80      ray       344:        /* Error out if file not found and we are not creating one. */
1.87      ray       345:        if (stat(rcspath, &st) == -1 && !(flags & RCS_CREATE)) {
1.44      niallo    346:                if ((strcmp(__progname, "rcsclean") != 0)
                    347:                    && (strcmp(__progname, "ci") != 0))
1.75      ray       348:                        cvs_log(LP_ERRNO, "%s", rcspath);
                    349:                xfree(rcspath);
1.9       joris     350:                return (-1);
                    351:        }
                    352:
1.75      ray       353:        if (strlcpy(out, rcspath, len) >= len)
1.57      xsa       354:                fatal("rcs_statfile: path truncation");
1.75      ray       355:
                    356:        xfree(rcspath);
1.9       joris     357:
                    358:        return (0);
1.97      ray       359: }
                    360:
                    361: /*
                    362:  * Set <str> to <new_str>.  Print warning if <str> is redefined.
                    363:  */
                    364: void
                    365: rcs_setrevstr(char **str, char *new_str)
                    366: {
                    367:        if (new_str == NULL)
                    368:                return;
                    369:        if (*str != NULL)
                    370:                cvs_log(LP_WARN, "redefinition of revision number");
                    371:        *str = new_str;
                    372: }
                    373:
                    374: /*
                    375:  * Set <str1> or <str2> to <new_str>, depending on which is not set.
                    376:  * If both are set, error out.
                    377:  */
                    378: void
                    379: rcs_setrevstr2(char **str1, char **str2, char *new_str)
                    380: {
                    381:        if (new_str == NULL)
                    382:                return;
                    383:        if (*str1 == NULL)
                    384:                *str1 = new_str;
                    385:        else if (*str2 == NULL)
                    386:                *str2 = new_str;
                    387:        else
                    388:                fatal("too many revision numbers");
1.99    ! ray       389: }
        !           390:
        !           391: /*
        !           392:  * Get revision from file.  The revision can be specified as a symbol or
        !           393:  * a revision number.
        !           394:  */
        !           395: RCSNUM *
        !           396: rcs_getrevnum(const char *rev_str, RCSFILE *file)
        !           397: {
        !           398:        RCSNUM *rev;
        !           399:
        !           400:        /* Search for symbol. */
        !           401:        rev = rcs_sym_getrev(file, rev_str);
        !           402:
        !           403:        /* Search for revision number. */
        !           404:        if (rev == NULL)
        !           405:                rev = rcsnum_parse(rev_str);
        !           406:
        !           407:        return (rev);
1.9       joris     408: }
1.1       deraadt   409:
                    410: int
                    411: main(int argc, char **argv)
                    412: {
                    413:        u_int i;
1.29      joris     414:        char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
                    415:        int ret, cmd_argc;
1.1       deraadt   416:
                    417:        ret = -1;
1.28      joris     418:        rcs_optind = 1;
1.9       joris     419:        cvs_log_init(LD_STD, 0);
1.68      joris     420:        SLIST_INIT(&rcs_temp_files);
1.1       deraadt   421:
1.29      joris     422:        cmd_argc = 0;
1.30      joris     423:        cmd_argv[cmd_argc++] = argv[0];
1.29      joris     424:        if ((rcsinit = getenv("RCSINIT")) != NULL) {
                    425:                ret = rcs_init(rcsinit, cmd_argv + 1,
                    426:                    RCS_CMD_MAXARG - 1);
                    427:                if (ret < 0) {
                    428:                        cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
                    429:                        exit (1);
                    430:                }
                    431:
                    432:                cmd_argc += ret;
                    433:        }
1.36      xsa       434:
                    435:        if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
                    436:                rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29      joris     437:
                    438:        for (ret = 1; ret < argc; ret++)
                    439:                cmd_argv[cmd_argc++] = argv[ret];
1.68      joris     440:
                    441:        signal(SIGHUP, sighdlr);
                    442:        signal(SIGINT, sighdlr);
                    443:        signal(SIGQUIT, sighdlr);
                    444:        signal(SIGABRT, sighdlr);
                    445:        signal(SIGALRM, sighdlr);
                    446:        signal(SIGTERM, sighdlr);
1.29      joris     447:
1.1       deraadt   448:        for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2       deraadt   449:                if (strcmp(__progname, programs[i].prog_name) == 0) {
                    450:                        usage = programs[i].prog_usage;
1.29      joris     451:                        ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2       deraadt   452:                        break;
                    453:                }
1.1       deraadt   454:
1.23      niallo    455:        exit(ret);
1.76      ray       456:        /* NOTREACHED */
1.1       deraadt   457: }
                    458:
                    459:
                    460: void
                    461: rcs_usage(void)
                    462: {
                    463:        fprintf(stderr,
1.66      jmc       464:            "usage: rcs [-ehIiLMqTUV] [-Aoldfile] [-ausers] [-b[rev]]\n"
1.92      ray       465:            "           [-cstring] [-e[users]] [-kmode] [-l[rev]] [-mrev:msg]\n"
                    466:            "           [-orev] [-sstate[:rev]] [-tfile|str] [-u[rev]]\n"
1.67      xsa       467:            "           [-xsuffixes] file ...\n");
1.1       deraadt   468: }
                    469:
                    470: /*
                    471:  * rcs_main()
                    472:  *
                    473:  * Handler for the `rcs' program.
                    474:  * Returns 0 on success, or >0 on error.
                    475:  */
                    476: int
                    477: rcs_main(int argc, char **argv)
                    478: {
1.87      ray       479:        int i, j, ch, kflag, lkmode;
1.39      xsa       480:        char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56      joris     481:        char *logstr, *logmsg, *nflag, *descfile;
1.92      ray       482:        char *alist, *comment, *elist, *lrev, *urev;
1.1       deraadt   483:        mode_t fmode;
1.39      xsa       484:        RCSFILE *file, *oldfile;
1.24      joris     485:        RCSNUM *logrev;
1.39      xsa       486:        struct rcs_access *acp;
1.48      xsa       487:        time_t rcs_mtime = -1;
1.1       deraadt   488:
1.95      xsa       489:        kflag = RCS_KWEXP_ERR;
                    490:        lkmode = -1;
1.89      niallo    491:        fmode =  S_IRUSR|S_IRGRP|S_IROTH;
1.58      niallo    492:        flags = RCS_RDWR|RCS_PARSE_FULLY;
1.92      ray       493:        lrev = urev = descfile = nflag = NULL;
1.39      xsa       494:        logstr = alist = comment = elist = NULL;
1.1       deraadt   495:
1.51      xsa       496:        while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1       deraadt   497:                switch (ch) {
                    498:                case 'A':
1.39      xsa       499:                        if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
                    500:                                exit(1);
1.56      joris     501:                        rcsflags |= CO_ACLAPPEND;
1.1       deraadt   502:                        break;
                    503:                case 'a':
1.28      joris     504:                        alist = rcs_optarg;
1.1       deraadt   505:                        break;
                    506:                case 'c':
1.28      joris     507:                        comment = rcs_optarg;
1.1       deraadt   508:                        break;
                    509:                case 'e':
1.28      joris     510:                        elist = rcs_optarg;
1.87      ray       511:                        rcsflags |= RCSPROG_EFLAG;
1.1       deraadt   512:                        break;
                    513:                case 'h':
1.2       deraadt   514:                        (usage)();
1.1       deraadt   515:                        exit(0);
1.76      ray       516:                        /* NOTREACHED */
1.1       deraadt   517:                case 'i':
                    518:                        flags |= RCS_CREATE;
                    519:                        break;
                    520:                case 'k':
1.28      joris     521:                        kflag = rcs_kflag_get(rcs_optarg);
1.1       deraadt   522:                        if (RCS_KWEXP_INVAL(kflag)) {
                    523:                                cvs_log(LP_ERR,
                    524:                                    "invalid keyword substitution mode `%s'",
1.28      joris     525:                                    rcs_optarg);
1.1       deraadt   526:                                exit(1);
                    527:                        }
                    528:                        break;
                    529:                case 'L':
                    530:                        if (lkmode == RCS_LOCK_LOOSE)
                    531:                                cvs_log(LP_WARN, "-U overriden by -L");
                    532:                        lkmode = RCS_LOCK_STRICT;
                    533:                        break;
1.92      ray       534:                case 'l':
                    535:                        /* XXX - Check with -u flag. */
                    536:                        lrev = rcs_optarg;
                    537:                        rcsflags |= RCSPROG_LFLAG;
                    538:                        break;
1.24      joris     539:                case 'm':
1.53      joris     540:                        logstr = xstrdup(rcs_optarg);
1.24      joris     541:                        break;
1.1       deraadt   542:                case 'M':
                    543:                        /* ignore for the moment */
                    544:                        break;
1.56      joris     545:                case 'n':
                    546:                        nflag = xstrdup(rcs_optarg);
                    547:                        break;
                    548:                case 'N':
                    549:                        nflag = xstrdup(rcs_optarg);
1.87      ray       550:                        rcsflags |= RCSPROG_NFLAG;
1.56      joris     551:                        break;
1.12      joris     552:                case 'q':
                    553:                        verbose = 0;
1.46      xsa       554:                        break;
1.56      joris     555:                case 't':
                    556:                        descfile = rcs_optarg;
1.87      ray       557:                        rcsflags |= RCSPROG_TFLAG;
1.56      joris     558:                        break;
1.46      xsa       559:                case 'T':
1.56      joris     560:                        rcsflags |= PRESERVETIME;
1.12      joris     561:                        break;
1.1       deraadt   562:                case 'U':
                    563:                        if (lkmode == RCS_LOCK_STRICT)
                    564:                                cvs_log(LP_WARN, "-L overriden by -U");
                    565:                        lkmode = RCS_LOCK_LOOSE;
                    566:                        break;
1.92      ray       567:                case 'u':
                    568:                        /* XXX - Check with -l flag. */
                    569:                        urev = rcs_optarg;
                    570:                        rcsflags |= RCSPROG_UFLAG;
                    571:                        break;
1.1       deraadt   572:                case 'V':
                    573:                        printf("%s\n", rcs_version);
                    574:                        exit(0);
1.76      ray       575:                        /* NOTREACHED */
1.45      xsa       576:                case 'x':
1.85      ray       577:                        /* Use blank extension if none given. */
                    578:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.51      xsa       579:                        break;
                    580:                case 'z':
                    581:                        /*
                    582:                         * kept for compatibility
                    583:                         */
1.45      xsa       584:                        break;
1.1       deraadt   585:                default:
1.2       deraadt   586:                        (usage)();
1.1       deraadt   587:                        exit(1);
                    588:                }
                    589:        }
                    590:
1.28      joris     591:        argc -= rcs_optind;
                    592:        argv += rcs_optind;
1.30      joris     593:
1.1       deraadt   594:        if (argc == 0) {
                    595:                cvs_log(LP_ERR, "no input file");
1.5       joris     596:                (usage)();
1.1       deraadt   597:                exit(1);
                    598:        }
                    599:
                    600:        for (i = 0; i < argc; i++) {
1.9       joris     601:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     602:                        continue;
1.6       joris     603:
1.35      niallo    604:                if (verbose == 1)
                    605:                        printf("RCS file: %s\n", fpath);
1.48      xsa       606:
1.49      niallo    607:                if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6       joris     608:                        continue;
1.1       deraadt   609:
1.87      ray       610:                if (rcsflags & RCSPROG_TFLAG)
                    611:                        rcs_set_description(file, descfile);
                    612:                else if (flags & RCS_CREATE)
1.56      joris     613:                        rcs_set_description(file, NULL);
                    614:
                    615:                if (rcsflags & PRESERVETIME)
1.48      xsa       616:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    617:
1.56      joris     618:                if (nflag != NULL)
                    619:                        rcs_attach_symbol(file, nflag);
                    620:
1.24      joris     621:                if (logstr != NULL) {
                    622:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    623:                                cvs_log(LP_ERR, "missing log message");
                    624:                                rcs_close(file);
                    625:                                continue;
                    626:                        }
                    627:
                    628:                        *logmsg++ = '\0';
                    629:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48      xsa       630:                                cvs_log(LP_ERR,
                    631:                                    "'%s' bad revision number", logstr);
1.24      joris     632:                                rcs_close(file);
                    633:                                continue;
                    634:                        }
                    635:
                    636:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    637:                                cvs_log(LP_ERR,
                    638:                                    "failed to set logmsg for '%s' to '%s'",
                    639:                                    logstr, logmsg);
                    640:                                rcs_close(file);
1.25      joris     641:                                rcsnum_free(logrev);
1.24      joris     642:                                continue;
                    643:                        }
                    644:
                    645:                        rcsnum_free(logrev);
1.39      xsa       646:                }
                    647:
                    648:                /* entries to add from <oldfile> */
1.56      joris     649:                if (rcsflags & CO_ACLAPPEND) {
1.39      xsa       650:                        /* XXX */
                    651:                        if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
                    652:                                exit(1);
                    653:
                    654:                        TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
                    655:                                rcs_access_add(file, acp->ra_name);
                    656:
                    657:                        rcs_close(oldfile);
1.24      joris     658:                }
                    659:
1.1       deraadt   660:                /* entries to add to the access list */
                    661:                if (alist != NULL) {
1.86      pat       662:                        struct cvs_argvector *aargv;
1.1       deraadt   663:
1.79      xsa       664:                        aargv = cvs_strsplit(alist, ",");
1.86      pat       665:                        for (j = 0; aargv->argv[j] != NULL; j++)
                    666:                                rcs_access_add(file, aargv->argv[j]);
1.1       deraadt   667:
1.86      pat       668:                        cvs_argv_destroy(aargv);
1.1       deraadt   669:                }
                    670:
                    671:                if (comment != NULL)
                    672:                        rcs_comment_set(file, comment);
1.82      xsa       673:
                    674:                if (elist != NULL) {
1.86      pat       675:                        struct cvs_argvector *eargv;
1.82      xsa       676:
                    677:                        eargv = cvs_strsplit(elist, ",");
1.86      pat       678:                        for (j = 0; eargv->argv[j] != NULL; j++)
                    679:                                rcs_access_remove(file, eargv->argv[j]);
1.82      xsa       680:
1.86      pat       681:                        cvs_argv_destroy(eargv);
1.87      ray       682:                } else if (rcsflags & RCSPROG_EFLAG) {
1.82      xsa       683:                        struct rcs_access *rap;
                    684:
                    685:                        /* XXX rcs_access_remove(file, NULL); ?? */
                    686:                        while (!TAILQ_EMPTY(&(file->rf_access))) {
                    687:                                rap = TAILQ_FIRST(&(file->rf_access));
                    688:                                TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
                    689:                                xfree(rap->ra_name);
                    690:                                xfree(rap);
                    691:                        }
                    692:                        /* not synced anymore */
                    693:                        file->rf_flags &= ~RCS_SYNCED;
                    694:                }
1.1       deraadt   695:
1.95      xsa       696:                rcs_kwexp_set(file, kflag);
1.1       deraadt   697:
                    698:                if (lkmode != -1)
1.84      xsa       699:                        (void)rcs_lock_setmode(file, lkmode);
1.92      ray       700:
                    701:                if (rcsflags & RCSPROG_LFLAG) {
                    702:                        RCSNUM *rev;
                    703:                        const char *username;
1.98      ray       704:                        char rev_str[16];
1.92      ray       705:
                    706:                        if ((username = getlogin()) == NULL)
                    707:                                fatal("could not get username");
                    708:                        if (lrev == NULL) {
                    709:                                rev = rcsnum_alloc();
                    710:                                rcsnum_cpy(file->rf_head, rev, 0);
                    711:                        } else if ((rev = rcsnum_parse(lrev)) == NULL) {
                    712:                                cvs_log(LP_ERR, "unable to unlock file");
                    713:                                rcs_close(file);
                    714:                                continue;
                    715:                        }
1.98      ray       716:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       717:                        /* Make sure revision exists. */
                    718:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       719:                                fatal("%s: can't lock nonexisting revision %s",
                    720:                                    fpath, rev_str);
                    721:                        if (rcs_lock_add(file, username, rev) != -1 &&
                    722:                            verbose == 1)
                    723:                                printf("%s locked\n", rev_str);
1.92      ray       724:                        rcsnum_free(rev);
                    725:                }
                    726:
                    727:                if (rcsflags & RCSPROG_UFLAG) {
                    728:                        RCSNUM *rev;
                    729:                        const char *username;
1.98      ray       730:                        char rev_str[16];
1.92      ray       731:
                    732:                        if ((username = getlogin()) == NULL)
                    733:                                fatal("could not get username");
                    734:                        if (urev == NULL) {
                    735:                                rev = rcsnum_alloc();
                    736:                                rcsnum_cpy(file->rf_head, rev, 0);
                    737:                        } else if ((rev = rcsnum_parse(urev)) == NULL) {
                    738:                                cvs_log(LP_ERR, "unable to unlock file");
                    739:                                rcs_close(file);
                    740:                                continue;
                    741:                        }
1.98      ray       742:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       743:                        /* Make sure revision exists. */
                    744:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       745:                                fatal("%s: can't unlock nonexisting revision %s",
                    746:                                    fpath, rev_str);
                    747:                        if (rcs_lock_remove(file, username, rev) == -1 &&
                    748:                            verbose == 1)
                    749:                                cvs_log(LP_ERR,
                    750:                                    "%s: warning: No locks are set.", fpath);
1.92      ray       751:                        rcsnum_free(rev);
                    752:                }
1.1       deraadt   753:
                    754:                rcs_close(file);
1.48      xsa       755:
1.56      joris     756:                if (rcsflags & PRESERVETIME)
1.48      xsa       757:                        rcs_set_mtime(fpath, rcs_mtime);
1.9       joris     758:
1.14      xsa       759:                if (verbose == 1)
1.12      joris     760:                        printf("done\n");
1.1       deraadt   761:        }
1.24      joris     762:
                    763:        if (logstr != NULL)
1.53      joris     764:                xfree(logstr);
1.1       deraadt   765:
1.56      joris     766:        if (nflag != NULL)
                    767:                xfree(nflag);
                    768:
1.1       deraadt   769:        return (0);
1.56      joris     770: }
                    771:
                    772: static void
                    773: rcs_attach_symbol(RCSFILE *file, const char *symname)
                    774: {
                    775:        char *rnum;
                    776:        RCSNUM *rev;
                    777:        char rbuf[16];
                    778:        int rm;
                    779:
                    780:        rm = 0;
                    781:        rev = NULL;
                    782:        if ((rnum = strrchr(symname, ':')) != NULL) {
                    783:                if (rnum[1] == '\0')
                    784:                        rev = file->rf_head;
                    785:                *(rnum++) = '\0';
                    786:        } else {
                    787:                rm = 1;
                    788:        }
                    789:
                    790:        if (rev == NULL && rm != 1) {
                    791:                if ((rev = rcsnum_parse(rnum)) == NULL)
                    792:                        fatal("bad revision %s", rnum);
                    793:        }
                    794:
1.87      ray       795:        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     796:                rm = 1;
                    797:
                    798:        if (rm == 1) {
                    799:                if (rcs_sym_remove(file, symname) < 0) {
                    800:                        if ((rcs_errno == RCS_ERR_NOENT) &&
1.87      ray       801:                            !(rcsflags & RCSPROG_NFLAG))
1.56      joris     802:                                cvs_log(LP_WARN,
                    803:                                    "can't delete nonexisting symbol %s", symname);
                    804:                } else {
1.87      ray       805:                        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     806:                                rm = 0;
                    807:                }
                    808:        }
                    809:
                    810:        if (rm == 0) {
                    811:                if ((rcs_sym_add(file, symname, rev) < 0) &&
                    812:                    (rcs_errno == RCS_ERR_DUPENT)) {
                    813:                        rcsnum_tostr(rcs_sym_getrev(file, symname),
                    814:                            rbuf, sizeof(rbuf));
                    815:                        fatal("symbolic name %s already bound to %s",
                    816:                            symname, rbuf);
                    817:                }
                    818:        }
                    819: }
                    820:
1.88      ray       821: /*
                    822:  * Load description from <in> to <file>.
                    823:  * If <in> starts with a `-', <in> is taken as the description.
                    824:  * Otherwise <in> is the name of the file containing the description.
                    825:  * If <in> is NULL, the description is read from stdin.
                    826:  */
1.56      joris     827: static void
                    828: rcs_set_description(RCSFILE *file, const char *in)
                    829: {
                    830:        BUF *bp;
                    831:        char *content, buf[128];
                    832:
                    833:        content = NULL;
1.88      ray       834:        /* Description is in file <in>. */
                    835:        if (in != NULL && *in != '-')
1.56      joris     836:                bp = cvs_buf_load(in, BUF_AUTOEXT);
1.88      ray       837:        /* Description is in <in>. */
                    838:        else if (in != NULL) {
                    839:                size_t len;
                    840:                const char *desc;
                    841:
                    842:                /* Skip leading `-'. */
                    843:                desc = in + 1;
                    844:                len = strlen(desc);
                    845:
                    846:                bp = cvs_buf_alloc(len + 1, BUF_AUTOEXT);
                    847:                cvs_buf_append(bp, desc, len);
                    848:        /* Get description from stdin. */
1.56      joris     849:        } else {
                    850:                bp = cvs_buf_alloc(64, BUF_AUTOEXT);
                    851:
1.90      xsa       852:                if (isatty(STDIN_FILENO))
                    853:                        (void)fprintf(stderr, "%s", DESC_PROMPT);
1.56      joris     854:                for (;;) {
1.88      ray       855:                        /* XXX - fgetln() may be more elegant. */
1.56      joris     856:                        fgets(buf, sizeof(buf), stdin);
1.88      ray       857:                        if (feof(stdin) || ferror(stdin) ||
                    858:                            strcmp(buf, ".\n") == 0 ||
                    859:                            strcmp(buf, ".") == 0)
1.56      joris     860:                                break;
                    861:                        cvs_buf_append(bp, buf, strlen(buf));
1.90      xsa       862:                        if (isatty(STDIN_FILENO))
                    863:                                (void)fprintf(stderr, ">> ");
1.56      joris     864:                }
                    865:        }
                    866:
                    867:        cvs_buf_putc(bp, '\0');
                    868:        content = cvs_buf_release(bp);
                    869:
                    870:        rcs_desc_set(file, content);
1.70      joris     871:        xfree(content);
1.1       deraadt   872: }