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

1.100   ! joris       1: /*     $OpenBSD: rcsprog.c,v 1.99 2006/04/13 00:58:25 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.100   ! joris     454:
        !           455:        /* clean up temporary files */
        !           456:        cvs_worklist_run(&rcs_temp_files, cvs_worklist_unlink);
1.1       deraadt   457:
1.23      niallo    458:        exit(ret);
1.76      ray       459:        /* NOTREACHED */
1.1       deraadt   460: }
                    461:
                    462:
                    463: void
                    464: rcs_usage(void)
                    465: {
                    466:        fprintf(stderr,
1.66      jmc       467:            "usage: rcs [-ehIiLMqTUV] [-Aoldfile] [-ausers] [-b[rev]]\n"
1.92      ray       468:            "           [-cstring] [-e[users]] [-kmode] [-l[rev]] [-mrev:msg]\n"
                    469:            "           [-orev] [-sstate[:rev]] [-tfile|str] [-u[rev]]\n"
1.67      xsa       470:            "           [-xsuffixes] file ...\n");
1.1       deraadt   471: }
                    472:
                    473: /*
                    474:  * rcs_main()
                    475:  *
                    476:  * Handler for the `rcs' program.
                    477:  * Returns 0 on success, or >0 on error.
                    478:  */
                    479: int
                    480: rcs_main(int argc, char **argv)
                    481: {
1.87      ray       482:        int i, j, ch, kflag, lkmode;
1.39      xsa       483:        char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56      joris     484:        char *logstr, *logmsg, *nflag, *descfile;
1.92      ray       485:        char *alist, *comment, *elist, *lrev, *urev;
1.1       deraadt   486:        mode_t fmode;
1.39      xsa       487:        RCSFILE *file, *oldfile;
1.24      joris     488:        RCSNUM *logrev;
1.39      xsa       489:        struct rcs_access *acp;
1.48      xsa       490:        time_t rcs_mtime = -1;
1.1       deraadt   491:
1.95      xsa       492:        kflag = RCS_KWEXP_ERR;
                    493:        lkmode = -1;
1.89      niallo    494:        fmode =  S_IRUSR|S_IRGRP|S_IROTH;
1.58      niallo    495:        flags = RCS_RDWR|RCS_PARSE_FULLY;
1.92      ray       496:        lrev = urev = descfile = nflag = NULL;
1.39      xsa       497:        logstr = alist = comment = elist = NULL;
1.1       deraadt   498:
1.51      xsa       499:        while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1       deraadt   500:                switch (ch) {
                    501:                case 'A':
1.39      xsa       502:                        if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
                    503:                                exit(1);
1.56      joris     504:                        rcsflags |= CO_ACLAPPEND;
1.1       deraadt   505:                        break;
                    506:                case 'a':
1.28      joris     507:                        alist = rcs_optarg;
1.1       deraadt   508:                        break;
                    509:                case 'c':
1.28      joris     510:                        comment = rcs_optarg;
1.1       deraadt   511:                        break;
                    512:                case 'e':
1.28      joris     513:                        elist = rcs_optarg;
1.87      ray       514:                        rcsflags |= RCSPROG_EFLAG;
1.1       deraadt   515:                        break;
                    516:                case 'h':
1.2       deraadt   517:                        (usage)();
1.1       deraadt   518:                        exit(0);
1.76      ray       519:                        /* NOTREACHED */
1.1       deraadt   520:                case 'i':
                    521:                        flags |= RCS_CREATE;
                    522:                        break;
                    523:                case 'k':
1.28      joris     524:                        kflag = rcs_kflag_get(rcs_optarg);
1.1       deraadt   525:                        if (RCS_KWEXP_INVAL(kflag)) {
                    526:                                cvs_log(LP_ERR,
                    527:                                    "invalid keyword substitution mode `%s'",
1.28      joris     528:                                    rcs_optarg);
1.1       deraadt   529:                                exit(1);
                    530:                        }
                    531:                        break;
                    532:                case 'L':
                    533:                        if (lkmode == RCS_LOCK_LOOSE)
                    534:                                cvs_log(LP_WARN, "-U overriden by -L");
                    535:                        lkmode = RCS_LOCK_STRICT;
                    536:                        break;
1.92      ray       537:                case 'l':
                    538:                        /* XXX - Check with -u flag. */
                    539:                        lrev = rcs_optarg;
                    540:                        rcsflags |= RCSPROG_LFLAG;
                    541:                        break;
1.24      joris     542:                case 'm':
1.53      joris     543:                        logstr = xstrdup(rcs_optarg);
1.24      joris     544:                        break;
1.1       deraadt   545:                case 'M':
                    546:                        /* ignore for the moment */
                    547:                        break;
1.56      joris     548:                case 'n':
                    549:                        nflag = xstrdup(rcs_optarg);
                    550:                        break;
                    551:                case 'N':
                    552:                        nflag = xstrdup(rcs_optarg);
1.87      ray       553:                        rcsflags |= RCSPROG_NFLAG;
1.56      joris     554:                        break;
1.12      joris     555:                case 'q':
                    556:                        verbose = 0;
1.46      xsa       557:                        break;
1.56      joris     558:                case 't':
                    559:                        descfile = rcs_optarg;
1.87      ray       560:                        rcsflags |= RCSPROG_TFLAG;
1.56      joris     561:                        break;
1.46      xsa       562:                case 'T':
1.56      joris     563:                        rcsflags |= PRESERVETIME;
1.12      joris     564:                        break;
1.1       deraadt   565:                case 'U':
                    566:                        if (lkmode == RCS_LOCK_STRICT)
                    567:                                cvs_log(LP_WARN, "-L overriden by -U");
                    568:                        lkmode = RCS_LOCK_LOOSE;
                    569:                        break;
1.92      ray       570:                case 'u':
                    571:                        /* XXX - Check with -l flag. */
                    572:                        urev = rcs_optarg;
                    573:                        rcsflags |= RCSPROG_UFLAG;
                    574:                        break;
1.1       deraadt   575:                case 'V':
                    576:                        printf("%s\n", rcs_version);
                    577:                        exit(0);
1.76      ray       578:                        /* NOTREACHED */
1.45      xsa       579:                case 'x':
1.85      ray       580:                        /* Use blank extension if none given. */
                    581:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.51      xsa       582:                        break;
                    583:                case 'z':
                    584:                        /*
                    585:                         * kept for compatibility
                    586:                         */
1.45      xsa       587:                        break;
1.1       deraadt   588:                default:
1.2       deraadt   589:                        (usage)();
1.1       deraadt   590:                        exit(1);
                    591:                }
                    592:        }
                    593:
1.28      joris     594:        argc -= rcs_optind;
                    595:        argv += rcs_optind;
1.30      joris     596:
1.1       deraadt   597:        if (argc == 0) {
                    598:                cvs_log(LP_ERR, "no input file");
1.5       joris     599:                (usage)();
1.1       deraadt   600:                exit(1);
                    601:        }
                    602:
                    603:        for (i = 0; i < argc; i++) {
1.9       joris     604:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     605:                        continue;
1.6       joris     606:
1.35      niallo    607:                if (verbose == 1)
                    608:                        printf("RCS file: %s\n", fpath);
1.48      xsa       609:
1.49      niallo    610:                if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6       joris     611:                        continue;
1.1       deraadt   612:
1.87      ray       613:                if (rcsflags & RCSPROG_TFLAG)
                    614:                        rcs_set_description(file, descfile);
                    615:                else if (flags & RCS_CREATE)
1.56      joris     616:                        rcs_set_description(file, NULL);
                    617:
                    618:                if (rcsflags & PRESERVETIME)
1.48      xsa       619:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    620:
1.56      joris     621:                if (nflag != NULL)
                    622:                        rcs_attach_symbol(file, nflag);
                    623:
1.24      joris     624:                if (logstr != NULL) {
                    625:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    626:                                cvs_log(LP_ERR, "missing log message");
                    627:                                rcs_close(file);
                    628:                                continue;
                    629:                        }
                    630:
                    631:                        *logmsg++ = '\0';
                    632:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48      xsa       633:                                cvs_log(LP_ERR,
                    634:                                    "'%s' bad revision number", logstr);
1.24      joris     635:                                rcs_close(file);
                    636:                                continue;
                    637:                        }
                    638:
                    639:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    640:                                cvs_log(LP_ERR,
                    641:                                    "failed to set logmsg for '%s' to '%s'",
                    642:                                    logstr, logmsg);
                    643:                                rcs_close(file);
1.25      joris     644:                                rcsnum_free(logrev);
1.24      joris     645:                                continue;
                    646:                        }
                    647:
                    648:                        rcsnum_free(logrev);
1.39      xsa       649:                }
                    650:
                    651:                /* entries to add from <oldfile> */
1.56      joris     652:                if (rcsflags & CO_ACLAPPEND) {
1.39      xsa       653:                        /* XXX */
                    654:                        if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
                    655:                                exit(1);
                    656:
                    657:                        TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
                    658:                                rcs_access_add(file, acp->ra_name);
                    659:
                    660:                        rcs_close(oldfile);
1.24      joris     661:                }
                    662:
1.1       deraadt   663:                /* entries to add to the access list */
                    664:                if (alist != NULL) {
1.86      pat       665:                        struct cvs_argvector *aargv;
1.1       deraadt   666:
1.79      xsa       667:                        aargv = cvs_strsplit(alist, ",");
1.86      pat       668:                        for (j = 0; aargv->argv[j] != NULL; j++)
                    669:                                rcs_access_add(file, aargv->argv[j]);
1.1       deraadt   670:
1.86      pat       671:                        cvs_argv_destroy(aargv);
1.1       deraadt   672:                }
                    673:
                    674:                if (comment != NULL)
                    675:                        rcs_comment_set(file, comment);
1.82      xsa       676:
                    677:                if (elist != NULL) {
1.86      pat       678:                        struct cvs_argvector *eargv;
1.82      xsa       679:
                    680:                        eargv = cvs_strsplit(elist, ",");
1.86      pat       681:                        for (j = 0; eargv->argv[j] != NULL; j++)
                    682:                                rcs_access_remove(file, eargv->argv[j]);
1.82      xsa       683:
1.86      pat       684:                        cvs_argv_destroy(eargv);
1.87      ray       685:                } else if (rcsflags & RCSPROG_EFLAG) {
1.82      xsa       686:                        struct rcs_access *rap;
                    687:
                    688:                        /* XXX rcs_access_remove(file, NULL); ?? */
                    689:                        while (!TAILQ_EMPTY(&(file->rf_access))) {
                    690:                                rap = TAILQ_FIRST(&(file->rf_access));
                    691:                                TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
                    692:                                xfree(rap->ra_name);
                    693:                                xfree(rap);
                    694:                        }
                    695:                        /* not synced anymore */
                    696:                        file->rf_flags &= ~RCS_SYNCED;
                    697:                }
1.1       deraadt   698:
1.95      xsa       699:                rcs_kwexp_set(file, kflag);
1.1       deraadt   700:
                    701:                if (lkmode != -1)
1.84      xsa       702:                        (void)rcs_lock_setmode(file, lkmode);
1.92      ray       703:
                    704:                if (rcsflags & RCSPROG_LFLAG) {
                    705:                        RCSNUM *rev;
                    706:                        const char *username;
1.98      ray       707:                        char rev_str[16];
1.92      ray       708:
                    709:                        if ((username = getlogin()) == NULL)
                    710:                                fatal("could not get username");
                    711:                        if (lrev == NULL) {
                    712:                                rev = rcsnum_alloc();
                    713:                                rcsnum_cpy(file->rf_head, rev, 0);
                    714:                        } else if ((rev = rcsnum_parse(lrev)) == NULL) {
                    715:                                cvs_log(LP_ERR, "unable to unlock file");
                    716:                                rcs_close(file);
                    717:                                continue;
                    718:                        }
1.98      ray       719:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       720:                        /* Make sure revision exists. */
                    721:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       722:                                fatal("%s: can't lock nonexisting revision %s",
                    723:                                    fpath, rev_str);
                    724:                        if (rcs_lock_add(file, username, rev) != -1 &&
                    725:                            verbose == 1)
                    726:                                printf("%s locked\n", rev_str);
1.92      ray       727:                        rcsnum_free(rev);
                    728:                }
                    729:
                    730:                if (rcsflags & RCSPROG_UFLAG) {
                    731:                        RCSNUM *rev;
                    732:                        const char *username;
1.98      ray       733:                        char rev_str[16];
1.92      ray       734:
                    735:                        if ((username = getlogin()) == NULL)
                    736:                                fatal("could not get username");
                    737:                        if (urev == NULL) {
                    738:                                rev = rcsnum_alloc();
                    739:                                rcsnum_cpy(file->rf_head, rev, 0);
                    740:                        } else if ((rev = rcsnum_parse(urev)) == NULL) {
                    741:                                cvs_log(LP_ERR, "unable to unlock file");
                    742:                                rcs_close(file);
                    743:                                continue;
                    744:                        }
1.98      ray       745:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       746:                        /* Make sure revision exists. */
                    747:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       748:                                fatal("%s: can't unlock nonexisting revision %s",
                    749:                                    fpath, rev_str);
                    750:                        if (rcs_lock_remove(file, username, rev) == -1 &&
                    751:                            verbose == 1)
                    752:                                cvs_log(LP_ERR,
                    753:                                    "%s: warning: No locks are set.", fpath);
1.92      ray       754:                        rcsnum_free(rev);
                    755:                }
1.1       deraadt   756:
                    757:                rcs_close(file);
1.48      xsa       758:
1.56      joris     759:                if (rcsflags & PRESERVETIME)
1.48      xsa       760:                        rcs_set_mtime(fpath, rcs_mtime);
1.9       joris     761:
1.14      xsa       762:                if (verbose == 1)
1.12      joris     763:                        printf("done\n");
1.1       deraadt   764:        }
1.24      joris     765:
                    766:        if (logstr != NULL)
1.53      joris     767:                xfree(logstr);
1.1       deraadt   768:
1.56      joris     769:        if (nflag != NULL)
                    770:                xfree(nflag);
                    771:
1.1       deraadt   772:        return (0);
1.56      joris     773: }
                    774:
                    775: static void
                    776: rcs_attach_symbol(RCSFILE *file, const char *symname)
                    777: {
                    778:        char *rnum;
                    779:        RCSNUM *rev;
                    780:        char rbuf[16];
                    781:        int rm;
                    782:
                    783:        rm = 0;
                    784:        rev = NULL;
                    785:        if ((rnum = strrchr(symname, ':')) != NULL) {
                    786:                if (rnum[1] == '\0')
                    787:                        rev = file->rf_head;
                    788:                *(rnum++) = '\0';
                    789:        } else {
                    790:                rm = 1;
                    791:        }
                    792:
                    793:        if (rev == NULL && rm != 1) {
                    794:                if ((rev = rcsnum_parse(rnum)) == NULL)
                    795:                        fatal("bad revision %s", rnum);
                    796:        }
                    797:
1.87      ray       798:        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     799:                rm = 1;
                    800:
                    801:        if (rm == 1) {
                    802:                if (rcs_sym_remove(file, symname) < 0) {
                    803:                        if ((rcs_errno == RCS_ERR_NOENT) &&
1.87      ray       804:                            !(rcsflags & RCSPROG_NFLAG))
1.56      joris     805:                                cvs_log(LP_WARN,
                    806:                                    "can't delete nonexisting symbol %s", symname);
                    807:                } else {
1.87      ray       808:                        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     809:                                rm = 0;
                    810:                }
                    811:        }
                    812:
                    813:        if (rm == 0) {
                    814:                if ((rcs_sym_add(file, symname, rev) < 0) &&
                    815:                    (rcs_errno == RCS_ERR_DUPENT)) {
                    816:                        rcsnum_tostr(rcs_sym_getrev(file, symname),
                    817:                            rbuf, sizeof(rbuf));
                    818:                        fatal("symbolic name %s already bound to %s",
                    819:                            symname, rbuf);
                    820:                }
                    821:        }
                    822: }
                    823:
1.88      ray       824: /*
                    825:  * Load description from <in> to <file>.
                    826:  * If <in> starts with a `-', <in> is taken as the description.
                    827:  * Otherwise <in> is the name of the file containing the description.
                    828:  * If <in> is NULL, the description is read from stdin.
                    829:  */
1.56      joris     830: static void
                    831: rcs_set_description(RCSFILE *file, const char *in)
                    832: {
                    833:        BUF *bp;
                    834:        char *content, buf[128];
                    835:
                    836:        content = NULL;
1.88      ray       837:        /* Description is in file <in>. */
                    838:        if (in != NULL && *in != '-')
1.56      joris     839:                bp = cvs_buf_load(in, BUF_AUTOEXT);
1.88      ray       840:        /* Description is in <in>. */
                    841:        else if (in != NULL) {
                    842:                size_t len;
                    843:                const char *desc;
                    844:
                    845:                /* Skip leading `-'. */
                    846:                desc = in + 1;
                    847:                len = strlen(desc);
                    848:
                    849:                bp = cvs_buf_alloc(len + 1, BUF_AUTOEXT);
                    850:                cvs_buf_append(bp, desc, len);
                    851:        /* Get description from stdin. */
1.56      joris     852:        } else {
                    853:                bp = cvs_buf_alloc(64, BUF_AUTOEXT);
                    854:
1.90      xsa       855:                if (isatty(STDIN_FILENO))
                    856:                        (void)fprintf(stderr, "%s", DESC_PROMPT);
1.56      joris     857:                for (;;) {
1.88      ray       858:                        /* XXX - fgetln() may be more elegant. */
1.56      joris     859:                        fgets(buf, sizeof(buf), stdin);
1.88      ray       860:                        if (feof(stdin) || ferror(stdin) ||
                    861:                            strcmp(buf, ".\n") == 0 ||
                    862:                            strcmp(buf, ".") == 0)
1.56      joris     863:                                break;
                    864:                        cvs_buf_append(bp, buf, strlen(buf));
1.90      xsa       865:                        if (isatty(STDIN_FILENO))
                    866:                                (void)fprintf(stderr, ">> ");
1.56      joris     867:                }
                    868:        }
                    869:
                    870:        cvs_buf_putc(bp, '\0');
                    871:        content = cvs_buf_release(bp);
                    872:
                    873:        rcs_desc_set(file, content);
1.70      joris     874:        xfree(content);
1.1       deraadt   875: }