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

1.104   ! xsa         1: /*     $OpenBSD: rcsprog.c,v 1.103 2006/04/14 23:29:01 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 *);
1.56      joris      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;
1.103     joris     234:        char *p, *ext, name[MAXPATHLEN], *next, *ptr, rcsdir[MAXPATHLEN],
1.74      ray       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);
1.102     deraadt   258:        if (stat(rcsdir, &sb) == 0 && (sb.st_mode & S_IFDIR))
1.74      ray       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:
1.103     joris     296:                if ((p = strrchr(rcspath, ',')) != NULL) {
                    297:                        if (!strcmp(p, ext)) {
                    298:                                if (stat(rcspath, &sb) == 0) {
                    299:                                        ret = xstrdup(rcspath);
                    300:                                        goto out;
                    301:                                }
                    302:                        }
                    303:
                    304:                        continue;
                    305:                }
                    306:
1.74      ray       307:                /* Construct RCS file path. */
                    308:                if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
1.83      ray       309:                    strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath))
                    310:                        goto out;
1.74      ray       311:
                    312:                /* Don't use `filename' as RCS file. */
                    313:                if (strcmp(fpath, filename) == 0)
                    314:                        continue;
                    315:
                    316:                if (stat(fpath, &sb) == 0) {
1.77      ray       317:                        ret = xstrdup(fpath);
1.83      ray       318:                        goto out;
1.74      ray       319:                }
                    320:        }
                    321:
                    322:        /*
1.83      ray       323:         * `ret' is still NULL.  No RCS file with any extension exists
1.74      ray       324:         * so we use the first extension.
1.83      ray       325:         *
                    326:         * `suffixes' should now be NUL separated, so the first
                    327:         * extension can be read just by reading `suffixes'.
1.74      ray       328:         */
1.83      ray       329:        if (strlcat(rcspath, suffixes, sizeof(rcspath)) >=
                    330:            sizeof(rcspath))
                    331:                goto out;
                    332:        ret = xstrdup(rcspath);
1.28      joris     333:
1.83      ray       334: out:
                    335:        /* `ret' may be NULL, which indicates an error. */
1.81      ray       336:        xfree(suffixes);
1.28      joris     337:        return (ret);
                    338: }
                    339:
1.75      ray       340: /*
                    341:  * Find the name of an RCS file, given a file name `fname'.  If an RCS
                    342:  * file is found, the name is copied to the `len' sized buffer `out'.
                    343:  * Returns 0 if RCS file was found, -1 otherwise.
                    344:  */
1.28      joris     345: int
1.9       joris     346: rcs_statfile(char *fname, char *out, size_t len)
                    347: {
                    348:        struct stat st;
1.75      ray       349:        char *rcspath;
1.9       joris     350:
1.75      ray       351:        if ((rcspath = rcs_choosefile(fname)) == NULL)
                    352:                fatal("rcs_statfile: path truncation");
1.9       joris     353:
1.80      ray       354:        /* Error out if file not found and we are not creating one. */
1.87      ray       355:        if (stat(rcspath, &st) == -1 && !(flags & RCS_CREATE)) {
1.102     deraadt   356:                if (strcmp(__progname, "rcsclean") != 0 &&
                    357:                    strcmp(__progname, "ci") != 0)
1.75      ray       358:                        cvs_log(LP_ERRNO, "%s", rcspath);
                    359:                xfree(rcspath);
1.9       joris     360:                return (-1);
                    361:        }
                    362:
1.75      ray       363:        if (strlcpy(out, rcspath, len) >= len)
1.57      xsa       364:                fatal("rcs_statfile: path truncation");
1.75      ray       365:
                    366:        xfree(rcspath);
1.9       joris     367:
                    368:        return (0);
1.97      ray       369: }
                    370:
                    371: /*
                    372:  * Set <str> to <new_str>.  Print warning if <str> is redefined.
                    373:  */
                    374: void
                    375: rcs_setrevstr(char **str, char *new_str)
                    376: {
                    377:        if (new_str == NULL)
                    378:                return;
                    379:        if (*str != NULL)
                    380:                cvs_log(LP_WARN, "redefinition of revision number");
                    381:        *str = new_str;
                    382: }
                    383:
                    384: /*
                    385:  * Set <str1> or <str2> to <new_str>, depending on which is not set.
                    386:  * If both are set, error out.
                    387:  */
                    388: void
                    389: rcs_setrevstr2(char **str1, char **str2, char *new_str)
                    390: {
                    391:        if (new_str == NULL)
                    392:                return;
                    393:        if (*str1 == NULL)
                    394:                *str1 = new_str;
                    395:        else if (*str2 == NULL)
                    396:                *str2 = new_str;
                    397:        else
                    398:                fatal("too many revision numbers");
1.99      ray       399: }
                    400:
                    401: /*
                    402:  * Get revision from file.  The revision can be specified as a symbol or
                    403:  * a revision number.
                    404:  */
                    405: RCSNUM *
                    406: rcs_getrevnum(const char *rev_str, RCSFILE *file)
                    407: {
                    408:        RCSNUM *rev;
                    409:
                    410:        /* Search for symbol. */
                    411:        rev = rcs_sym_getrev(file, rev_str);
                    412:
                    413:        /* Search for revision number. */
                    414:        if (rev == NULL)
                    415:                rev = rcsnum_parse(rev_str);
                    416:
                    417:        return (rev);
1.9       joris     418: }
1.1       deraadt   419:
                    420: int
                    421: main(int argc, char **argv)
                    422: {
                    423:        u_int i;
1.29      joris     424:        char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
                    425:        int ret, cmd_argc;
1.1       deraadt   426:
                    427:        ret = -1;
1.28      joris     428:        rcs_optind = 1;
1.9       joris     429:        cvs_log_init(LD_STD, 0);
1.68      joris     430:        SLIST_INIT(&rcs_temp_files);
1.1       deraadt   431:
1.29      joris     432:        cmd_argc = 0;
1.30      joris     433:        cmd_argv[cmd_argc++] = argv[0];
1.29      joris     434:        if ((rcsinit = getenv("RCSINIT")) != NULL) {
                    435:                ret = rcs_init(rcsinit, cmd_argv + 1,
                    436:                    RCS_CMD_MAXARG - 1);
                    437:                if (ret < 0) {
                    438:                        cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
                    439:                        exit (1);
                    440:                }
                    441:
                    442:                cmd_argc += ret;
                    443:        }
1.36      xsa       444:
                    445:        if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
                    446:                rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29      joris     447:
                    448:        for (ret = 1; ret < argc; ret++)
                    449:                cmd_argv[cmd_argc++] = argv[ret];
1.68      joris     450:
                    451:        signal(SIGHUP, sighdlr);
                    452:        signal(SIGINT, sighdlr);
                    453:        signal(SIGQUIT, sighdlr);
                    454:        signal(SIGABRT, sighdlr);
                    455:        signal(SIGALRM, sighdlr);
                    456:        signal(SIGTERM, sighdlr);
1.29      joris     457:
1.1       deraadt   458:        for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2       deraadt   459:                if (strcmp(__progname, programs[i].prog_name) == 0) {
                    460:                        usage = programs[i].prog_usage;
1.29      joris     461:                        ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2       deraadt   462:                        break;
                    463:                }
1.100     joris     464:
                    465:        /* clean up temporary files */
                    466:        cvs_worklist_run(&rcs_temp_files, cvs_worklist_unlink);
1.1       deraadt   467:
1.23      niallo    468:        exit(ret);
1.76      ray       469:        /* NOTREACHED */
1.1       deraadt   470: }
                    471:
                    472:
                    473: void
                    474: rcs_usage(void)
                    475: {
                    476:        fprintf(stderr,
1.66      jmc       477:            "usage: rcs [-ehIiLMqTUV] [-Aoldfile] [-ausers] [-b[rev]]\n"
1.92      ray       478:            "           [-cstring] [-e[users]] [-kmode] [-l[rev]] [-mrev:msg]\n"
1.101     joris     479:            "           [-orange] [-sstate[:rev]] [-tfile|str] [-u[rev]]\n"
1.67      xsa       480:            "           [-xsuffixes] file ...\n");
1.1       deraadt   481: }
                    482:
                    483: /*
                    484:  * rcs_main()
                    485:  *
                    486:  * Handler for the `rcs' program.
                    487:  * Returns 0 on success, or >0 on error.
                    488:  */
                    489: int
                    490: rcs_main(int argc, char **argv)
                    491: {
1.87      ray       492:        int i, j, ch, kflag, lkmode;
1.39      xsa       493:        char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56      joris     494:        char *logstr, *logmsg, *nflag, *descfile;
1.101     joris     495:        char *alist, *comment, *elist, *lrev, *urev, *orange;
1.1       deraadt   496:        mode_t fmode;
1.39      xsa       497:        RCSFILE *file, *oldfile;
1.24      joris     498:        RCSNUM *logrev;
1.39      xsa       499:        struct rcs_access *acp;
1.48      xsa       500:        time_t rcs_mtime = -1;
1.1       deraadt   501:
1.95      xsa       502:        kflag = RCS_KWEXP_ERR;
                    503:        lkmode = -1;
1.89      niallo    504:        fmode =  S_IRUSR|S_IRGRP|S_IROTH;
1.58      niallo    505:        flags = RCS_RDWR|RCS_PARSE_FULLY;
1.92      ray       506:        lrev = urev = descfile = nflag = NULL;
1.101     joris     507:        logstr = alist = comment = elist = orange = NULL;
1.1       deraadt   508:
1.51      xsa       509:        while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1       deraadt   510:                switch (ch) {
                    511:                case 'A':
1.39      xsa       512:                        if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
                    513:                                exit(1);
1.56      joris     514:                        rcsflags |= CO_ACLAPPEND;
1.1       deraadt   515:                        break;
                    516:                case 'a':
1.28      joris     517:                        alist = rcs_optarg;
1.1       deraadt   518:                        break;
                    519:                case 'c':
1.28      joris     520:                        comment = rcs_optarg;
1.1       deraadt   521:                        break;
                    522:                case 'e':
1.28      joris     523:                        elist = rcs_optarg;
1.87      ray       524:                        rcsflags |= RCSPROG_EFLAG;
1.1       deraadt   525:                        break;
                    526:                case 'h':
1.2       deraadt   527:                        (usage)();
1.1       deraadt   528:                        exit(0);
1.76      ray       529:                        /* NOTREACHED */
1.1       deraadt   530:                case 'i':
                    531:                        flags |= RCS_CREATE;
                    532:                        break;
                    533:                case 'k':
1.28      joris     534:                        kflag = rcs_kflag_get(rcs_optarg);
1.1       deraadt   535:                        if (RCS_KWEXP_INVAL(kflag)) {
                    536:                                cvs_log(LP_ERR,
                    537:                                    "invalid keyword substitution mode `%s'",
1.28      joris     538:                                    rcs_optarg);
1.1       deraadt   539:                                exit(1);
                    540:                        }
                    541:                        break;
                    542:                case 'L':
                    543:                        if (lkmode == RCS_LOCK_LOOSE)
                    544:                                cvs_log(LP_WARN, "-U overriden by -L");
                    545:                        lkmode = RCS_LOCK_STRICT;
                    546:                        break;
1.92      ray       547:                case 'l':
                    548:                        /* XXX - Check with -u flag. */
                    549:                        lrev = rcs_optarg;
                    550:                        rcsflags |= RCSPROG_LFLAG;
                    551:                        break;
1.24      joris     552:                case 'm':
1.53      joris     553:                        logstr = xstrdup(rcs_optarg);
1.24      joris     554:                        break;
1.1       deraadt   555:                case 'M':
                    556:                        /* ignore for the moment */
                    557:                        break;
1.56      joris     558:                case 'n':
                    559:                        nflag = xstrdup(rcs_optarg);
                    560:                        break;
                    561:                case 'N':
                    562:                        nflag = xstrdup(rcs_optarg);
1.87      ray       563:                        rcsflags |= RCSPROG_NFLAG;
1.56      joris     564:                        break;
1.101     joris     565:                case 'o':
                    566:                        orange = xstrdup(rcs_optarg);
                    567:                        break;
1.12      joris     568:                case 'q':
                    569:                        verbose = 0;
1.46      xsa       570:                        break;
1.56      joris     571:                case 't':
                    572:                        descfile = rcs_optarg;
1.87      ray       573:                        rcsflags |= RCSPROG_TFLAG;
1.56      joris     574:                        break;
1.46      xsa       575:                case 'T':
1.56      joris     576:                        rcsflags |= PRESERVETIME;
1.12      joris     577:                        break;
1.1       deraadt   578:                case 'U':
                    579:                        if (lkmode == RCS_LOCK_STRICT)
                    580:                                cvs_log(LP_WARN, "-L overriden by -U");
                    581:                        lkmode = RCS_LOCK_LOOSE;
                    582:                        break;
1.92      ray       583:                case 'u':
                    584:                        /* XXX - Check with -l flag. */
                    585:                        urev = rcs_optarg;
                    586:                        rcsflags |= RCSPROG_UFLAG;
                    587:                        break;
1.1       deraadt   588:                case 'V':
                    589:                        printf("%s\n", rcs_version);
                    590:                        exit(0);
1.76      ray       591:                        /* NOTREACHED */
1.45      xsa       592:                case 'x':
1.85      ray       593:                        /* Use blank extension if none given. */
                    594:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.51      xsa       595:                        break;
                    596:                case 'z':
                    597:                        /*
                    598:                         * kept for compatibility
                    599:                         */
1.45      xsa       600:                        break;
1.1       deraadt   601:                default:
1.2       deraadt   602:                        (usage)();
1.1       deraadt   603:                        exit(1);
                    604:                }
                    605:        }
                    606:
1.28      joris     607:        argc -= rcs_optind;
                    608:        argv += rcs_optind;
1.30      joris     609:
1.1       deraadt   610:        if (argc == 0) {
                    611:                cvs_log(LP_ERR, "no input file");
1.5       joris     612:                (usage)();
1.1       deraadt   613:                exit(1);
                    614:        }
                    615:
                    616:        for (i = 0; i < argc; i++) {
1.9       joris     617:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     618:                        continue;
1.6       joris     619:
1.35      niallo    620:                if (verbose == 1)
                    621:                        printf("RCS file: %s\n", fpath);
1.48      xsa       622:
1.49      niallo    623:                if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6       joris     624:                        continue;
1.1       deraadt   625:
1.87      ray       626:                if (rcsflags & RCSPROG_TFLAG)
                    627:                        rcs_set_description(file, descfile);
                    628:                else if (flags & RCS_CREATE)
1.56      joris     629:                        rcs_set_description(file, NULL);
                    630:
                    631:                if (rcsflags & PRESERVETIME)
1.48      xsa       632:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    633:
1.56      joris     634:                if (nflag != NULL)
                    635:                        rcs_attach_symbol(file, nflag);
                    636:
1.24      joris     637:                if (logstr != NULL) {
                    638:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    639:                                cvs_log(LP_ERR, "missing log message");
                    640:                                rcs_close(file);
                    641:                                continue;
                    642:                        }
                    643:
                    644:                        *logmsg++ = '\0';
                    645:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48      xsa       646:                                cvs_log(LP_ERR,
                    647:                                    "'%s' bad revision number", logstr);
1.24      joris     648:                                rcs_close(file);
                    649:                                continue;
                    650:                        }
                    651:
                    652:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    653:                                cvs_log(LP_ERR,
                    654:                                    "failed to set logmsg for '%s' to '%s'",
                    655:                                    logstr, logmsg);
                    656:                                rcs_close(file);
1.25      joris     657:                                rcsnum_free(logrev);
1.24      joris     658:                                continue;
                    659:                        }
                    660:
                    661:                        rcsnum_free(logrev);
1.39      xsa       662:                }
                    663:
                    664:                /* entries to add from <oldfile> */
1.56      joris     665:                if (rcsflags & CO_ACLAPPEND) {
1.39      xsa       666:                        /* XXX */
                    667:                        if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
                    668:                                exit(1);
                    669:
                    670:                        TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
                    671:                                rcs_access_add(file, acp->ra_name);
                    672:
                    673:                        rcs_close(oldfile);
1.24      joris     674:                }
                    675:
1.1       deraadt   676:                /* entries to add to the access list */
                    677:                if (alist != NULL) {
1.86      pat       678:                        struct cvs_argvector *aargv;
1.1       deraadt   679:
1.79      xsa       680:                        aargv = cvs_strsplit(alist, ",");
1.86      pat       681:                        for (j = 0; aargv->argv[j] != NULL; j++)
                    682:                                rcs_access_add(file, aargv->argv[j]);
1.1       deraadt   683:
1.86      pat       684:                        cvs_argv_destroy(aargv);
1.1       deraadt   685:                }
                    686:
                    687:                if (comment != NULL)
                    688:                        rcs_comment_set(file, comment);
1.82      xsa       689:
                    690:                if (elist != NULL) {
1.86      pat       691:                        struct cvs_argvector *eargv;
1.82      xsa       692:
                    693:                        eargv = cvs_strsplit(elist, ",");
1.86      pat       694:                        for (j = 0; eargv->argv[j] != NULL; j++)
                    695:                                rcs_access_remove(file, eargv->argv[j]);
1.82      xsa       696:
1.86      pat       697:                        cvs_argv_destroy(eargv);
1.87      ray       698:                } else if (rcsflags & RCSPROG_EFLAG) {
1.82      xsa       699:                        struct rcs_access *rap;
                    700:
                    701:                        /* XXX rcs_access_remove(file, NULL); ?? */
                    702:                        while (!TAILQ_EMPTY(&(file->rf_access))) {
                    703:                                rap = TAILQ_FIRST(&(file->rf_access));
                    704:                                TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
                    705:                                xfree(rap->ra_name);
                    706:                                xfree(rap);
                    707:                        }
                    708:                        /* not synced anymore */
                    709:                        file->rf_flags &= ~RCS_SYNCED;
                    710:                }
1.1       deraadt   711:
1.95      xsa       712:                rcs_kwexp_set(file, kflag);
1.1       deraadt   713:
                    714:                if (lkmode != -1)
1.84      xsa       715:                        (void)rcs_lock_setmode(file, lkmode);
1.92      ray       716:
                    717:                if (rcsflags & RCSPROG_LFLAG) {
                    718:                        RCSNUM *rev;
                    719:                        const char *username;
1.98      ray       720:                        char rev_str[16];
1.92      ray       721:
                    722:                        if ((username = getlogin()) == NULL)
                    723:                                fatal("could not get username");
                    724:                        if (lrev == NULL) {
                    725:                                rev = rcsnum_alloc();
                    726:                                rcsnum_cpy(file->rf_head, rev, 0);
                    727:                        } else if ((rev = rcsnum_parse(lrev)) == NULL) {
                    728:                                cvs_log(LP_ERR, "unable to unlock file");
                    729:                                rcs_close(file);
                    730:                                continue;
                    731:                        }
1.98      ray       732:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       733:                        /* Make sure revision exists. */
                    734:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       735:                                fatal("%s: can't lock nonexisting revision %s",
                    736:                                    fpath, rev_str);
                    737:                        if (rcs_lock_add(file, username, rev) != -1 &&
                    738:                            verbose == 1)
                    739:                                printf("%s locked\n", rev_str);
1.92      ray       740:                        rcsnum_free(rev);
                    741:                }
                    742:
                    743:                if (rcsflags & RCSPROG_UFLAG) {
                    744:                        RCSNUM *rev;
                    745:                        const char *username;
1.98      ray       746:                        char rev_str[16];
1.92      ray       747:
                    748:                        if ((username = getlogin()) == NULL)
                    749:                                fatal("could not get username");
                    750:                        if (urev == NULL) {
                    751:                                rev = rcsnum_alloc();
                    752:                                rcsnum_cpy(file->rf_head, rev, 0);
                    753:                        } else if ((rev = rcsnum_parse(urev)) == NULL) {
                    754:                                cvs_log(LP_ERR, "unable to unlock file");
                    755:                                rcs_close(file);
                    756:                                continue;
                    757:                        }
1.98      ray       758:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       759:                        /* Make sure revision exists. */
                    760:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       761:                                fatal("%s: can't unlock nonexisting revision %s",
                    762:                                    fpath, rev_str);
                    763:                        if (rcs_lock_remove(file, username, rev) == -1 &&
                    764:                            verbose == 1)
                    765:                                cvs_log(LP_ERR,
                    766:                                    "%s: warning: No locks are set.", fpath);
1.92      ray       767:                        rcsnum_free(rev);
                    768:                }
1.1       deraadt   769:
1.101     joris     770:                if (orange != NULL) {
                    771:                        struct rcs_delta *rdp;
                    772:                        char b[16];
                    773:
                    774:                        rcs_rev_select(file, orange);
                    775:                        TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
                    776:                                /*
                    777:                                 * Delete selected revisions.
                    778:                                 */
                    779:                                if (rdp->rd_flags & RCS_RD_SELECT) {
                    780:                                        rcsnum_tostr(rdp->rd_num, b, sizeof(b));
                    781:                                        printf("deleting revision %s\n", b);
                    782:                                        (void)rcs_rev_remove(file, rdp->rd_num);
                    783:                                }
                    784:                        }
                    785:                }
                    786:
1.1       deraadt   787:                rcs_close(file);
1.48      xsa       788:
1.56      joris     789:                if (rcsflags & PRESERVETIME)
1.48      xsa       790:                        rcs_set_mtime(fpath, rcs_mtime);
1.9       joris     791:
1.14      xsa       792:                if (verbose == 1)
1.12      joris     793:                        printf("done\n");
1.1       deraadt   794:        }
1.24      joris     795:
                    796:        if (logstr != NULL)
1.53      joris     797:                xfree(logstr);
1.1       deraadt   798:
1.56      joris     799:        if (nflag != NULL)
                    800:                xfree(nflag);
                    801:
1.1       deraadt   802:        return (0);
1.56      joris     803: }
                    804:
                    805: static void
                    806: rcs_attach_symbol(RCSFILE *file, const char *symname)
                    807: {
                    808:        char *rnum;
                    809:        RCSNUM *rev;
                    810:        char rbuf[16];
                    811:        int rm;
                    812:
                    813:        rm = 0;
                    814:        rev = NULL;
                    815:        if ((rnum = strrchr(symname, ':')) != NULL) {
                    816:                if (rnum[1] == '\0')
                    817:                        rev = file->rf_head;
                    818:                *(rnum++) = '\0';
                    819:        } else {
                    820:                rm = 1;
                    821:        }
                    822:
                    823:        if (rev == NULL && rm != 1) {
                    824:                if ((rev = rcsnum_parse(rnum)) == NULL)
                    825:                        fatal("bad revision %s", rnum);
                    826:        }
                    827:
1.87      ray       828:        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     829:                rm = 1;
                    830:
                    831:        if (rm == 1) {
                    832:                if (rcs_sym_remove(file, symname) < 0) {
1.102     deraadt   833:                        if (rcs_errno == RCS_ERR_NOENT &&
1.87      ray       834:                            !(rcsflags & RCSPROG_NFLAG))
1.56      joris     835:                                cvs_log(LP_WARN,
                    836:                                    "can't delete nonexisting symbol %s", symname);
                    837:                } else {
1.87      ray       838:                        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     839:                                rm = 0;
                    840:                }
                    841:        }
                    842:
                    843:        if (rm == 0) {
1.102     deraadt   844:                if (rcs_sym_add(file, symname, rev) < 0 &&
                    845:                    rcs_errno == RCS_ERR_DUPENT) {
1.56      joris     846:                        rcsnum_tostr(rcs_sym_getrev(file, symname),
                    847:                            rbuf, sizeof(rbuf));
                    848:                        fatal("symbolic name %s already bound to %s",
                    849:                            symname, rbuf);
                    850:                }
                    851:        }
                    852: }
                    853:
1.88      ray       854: /*
                    855:  * Load description from <in> to <file>.
                    856:  * If <in> starts with a `-', <in> is taken as the description.
                    857:  * Otherwise <in> is the name of the file containing the description.
                    858:  * If <in> is NULL, the description is read from stdin.
                    859:  */
1.56      joris     860: static void
                    861: rcs_set_description(RCSFILE *file, const char *in)
                    862: {
                    863:        BUF *bp;
                    864:        char *content, buf[128];
                    865:
                    866:        content = NULL;
1.88      ray       867:        /* Description is in file <in>. */
                    868:        if (in != NULL && *in != '-')
1.56      joris     869:                bp = cvs_buf_load(in, BUF_AUTOEXT);
1.88      ray       870:        /* Description is in <in>. */
                    871:        else if (in != NULL) {
                    872:                size_t len;
                    873:                const char *desc;
                    874:
                    875:                /* Skip leading `-'. */
                    876:                desc = in + 1;
                    877:                len = strlen(desc);
                    878:
                    879:                bp = cvs_buf_alloc(len + 1, BUF_AUTOEXT);
                    880:                cvs_buf_append(bp, desc, len);
                    881:        /* Get description from stdin. */
1.56      joris     882:        } else {
                    883:                bp = cvs_buf_alloc(64, BUF_AUTOEXT);
                    884:
1.90      xsa       885:                if (isatty(STDIN_FILENO))
                    886:                        (void)fprintf(stderr, "%s", DESC_PROMPT);
1.56      joris     887:                for (;;) {
1.88      ray       888:                        /* XXX - fgetln() may be more elegant. */
1.56      joris     889:                        fgets(buf, sizeof(buf), stdin);
1.88      ray       890:                        if (feof(stdin) || ferror(stdin) ||
                    891:                            strcmp(buf, ".\n") == 0 ||
                    892:                            strcmp(buf, ".") == 0)
1.56      joris     893:                                break;
                    894:                        cvs_buf_append(bp, buf, strlen(buf));
1.90      xsa       895:                        if (isatty(STDIN_FILENO))
                    896:                                (void)fprintf(stderr, ">> ");
1.56      joris     897:                }
                    898:        }
                    899:
                    900:        cvs_buf_putc(bp, '\0');
                    901:        content = cvs_buf_release(bp);
                    902:
                    903:        rcs_desc_set(file, content);
1.70      joris     904:        xfree(content);
1.101     joris     905: }
                    906:
1.104   ! xsa       907: u_int
1.101     joris     908: rcs_rev_select(RCSFILE *file, char *range)
                    909: {
                    910:        int i;
                    911:        u_int nrev;
                    912:        char *ep;
                    913:        char *lstr, *rstr;
                    914:        struct rcs_delta *rdp;
                    915:        struct cvs_argvector *revargv, *revrange;
                    916:        RCSNUM lnum, rnum;
                    917:
                    918:        nrev = 0;
                    919:        (void)memset(&lnum, 0, sizeof(lnum));
                    920:        (void)memset(&rnum, 0, sizeof(rnum));
                    921:
                    922:        if (range == NULL) {
                    923:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    924:                        if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
                    925:                                rdp->rd_flags |= RCS_RD_SELECT;
                    926:                                return (1);
                    927:                        }
                    928:                return (0);
                    929:        }
                    930:
                    931:        revargv = cvs_strsplit(range, ",");
                    932:        for (i = 0; revargv->argv[i] != NULL; i++) {
                    933:                revrange = cvs_strsplit(revargv->argv[i], ":");
                    934:                if (revrange->argv[0] == NULL)
                    935:                        /* should not happen */
                    936:                        fatal("invalid revision range: %s", revargv->argv[i]);
                    937:                else if (revrange->argv[1] == NULL)
                    938:                        lstr = rstr = revrange->argv[0];
                    939:                else {
                    940:                        if (revrange->argv[2] != NULL)
                    941:                                fatal("invalid revision range: %s",
                    942:                                        revargv->argv[i]);
                    943:                        lstr = revrange->argv[0];
                    944:                        rstr = revrange->argv[1];
                    945:                        if (strcmp(lstr, "") == 0)
                    946:                                lstr = NULL;
                    947:                        if (strcmp(rstr, "") == 0)
                    948:                                rstr = NULL;
                    949:                }
                    950:
                    951:                if (lstr == NULL)
                    952:                        lstr = RCS_HEAD_INIT;
                    953:                if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
                    954:                        fatal("invalid revision: %s", lstr);
                    955:
                    956:                if (rstr != NULL) {
                    957:                        if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
                    958:                                fatal("invalid revision: %s", rstr);
                    959:                } else
                    960:                        rcsnum_cpy(file->rf_head, &rnum, 0);
                    961:
                    962:                cvs_argv_destroy(revrange);
                    963:
                    964:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
1.102     deraadt   965:                        if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
                    966:                            rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
1.101     joris     967:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    968:                                rdp->rd_flags |= RCS_RD_SELECT;
                    969:                                nrev++;
                    970:                        }
                    971:        }
                    972:        cvs_argv_destroy(revargv);
                    973:
                    974:        if (lnum.rn_id != NULL)
                    975:                xfree(lnum.rn_id);
                    976:        if (rnum.rn_id != NULL)
                    977:                xfree(rnum.rn_id);
                    978:
                    979:        return (nrev);
1.1       deraadt   980: }