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

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