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

1.106   ! ray         1: /*     $OpenBSD: rcsprog.c,v 1.105 2006/04/17 12:11:07 xsa 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.106   ! ray        32: #define RCSPROG_OPTSTRING      "A:a:b::c:e::ik: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 'i':
                    527:                        flags |= RCS_CREATE;
                    528:                        break;
                    529:                case 'k':
1.28      joris     530:                        kflag = rcs_kflag_get(rcs_optarg);
1.1       deraadt   531:                        if (RCS_KWEXP_INVAL(kflag)) {
                    532:                                cvs_log(LP_ERR,
                    533:                                    "invalid keyword substitution mode `%s'",
1.28      joris     534:                                    rcs_optarg);
1.1       deraadt   535:                                exit(1);
                    536:                        }
                    537:                        break;
                    538:                case 'L':
                    539:                        if (lkmode == RCS_LOCK_LOOSE)
                    540:                                cvs_log(LP_WARN, "-U overriden by -L");
                    541:                        lkmode = RCS_LOCK_STRICT;
                    542:                        break;
1.92      ray       543:                case 'l':
                    544:                        /* XXX - Check with -u flag. */
                    545:                        lrev = rcs_optarg;
                    546:                        rcsflags |= RCSPROG_LFLAG;
                    547:                        break;
1.24      joris     548:                case 'm':
1.53      joris     549:                        logstr = xstrdup(rcs_optarg);
1.24      joris     550:                        break;
1.1       deraadt   551:                case 'M':
                    552:                        /* ignore for the moment */
                    553:                        break;
1.56      joris     554:                case 'n':
                    555:                        nflag = xstrdup(rcs_optarg);
                    556:                        break;
                    557:                case 'N':
                    558:                        nflag = xstrdup(rcs_optarg);
1.87      ray       559:                        rcsflags |= RCSPROG_NFLAG;
1.56      joris     560:                        break;
1.101     joris     561:                case 'o':
                    562:                        orange = xstrdup(rcs_optarg);
                    563:                        break;
1.12      joris     564:                case 'q':
                    565:                        verbose = 0;
1.46      xsa       566:                        break;
1.56      joris     567:                case 't':
                    568:                        descfile = rcs_optarg;
1.87      ray       569:                        rcsflags |= RCSPROG_TFLAG;
1.56      joris     570:                        break;
1.46      xsa       571:                case 'T':
1.56      joris     572:                        rcsflags |= PRESERVETIME;
1.12      joris     573:                        break;
1.1       deraadt   574:                case 'U':
                    575:                        if (lkmode == RCS_LOCK_STRICT)
                    576:                                cvs_log(LP_WARN, "-L overriden by -U");
                    577:                        lkmode = RCS_LOCK_LOOSE;
                    578:                        break;
1.92      ray       579:                case 'u':
                    580:                        /* XXX - Check with -l flag. */
                    581:                        urev = rcs_optarg;
                    582:                        rcsflags |= RCSPROG_UFLAG;
                    583:                        break;
1.1       deraadt   584:                case 'V':
                    585:                        printf("%s\n", rcs_version);
                    586:                        exit(0);
1.76      ray       587:                        /* NOTREACHED */
1.45      xsa       588:                case 'x':
1.85      ray       589:                        /* Use blank extension if none given. */
                    590:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.51      xsa       591:                        break;
                    592:                case 'z':
                    593:                        /*
                    594:                         * kept for compatibility
                    595:                         */
1.45      xsa       596:                        break;
1.1       deraadt   597:                default:
1.2       deraadt   598:                        (usage)();
1.1       deraadt   599:                        exit(1);
                    600:                }
                    601:        }
                    602:
1.28      joris     603:        argc -= rcs_optind;
                    604:        argv += rcs_optind;
1.30      joris     605:
1.1       deraadt   606:        if (argc == 0) {
                    607:                cvs_log(LP_ERR, "no input file");
1.5       joris     608:                (usage)();
1.1       deraadt   609:                exit(1);
                    610:        }
                    611:
                    612:        for (i = 0; i < argc; i++) {
1.9       joris     613:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     614:                        continue;
1.6       joris     615:
1.35      niallo    616:                if (verbose == 1)
                    617:                        printf("RCS file: %s\n", fpath);
1.48      xsa       618:
1.49      niallo    619:                if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6       joris     620:                        continue;
1.1       deraadt   621:
1.87      ray       622:                if (rcsflags & RCSPROG_TFLAG)
                    623:                        rcs_set_description(file, descfile);
                    624:                else if (flags & RCS_CREATE)
1.56      joris     625:                        rcs_set_description(file, NULL);
                    626:
                    627:                if (rcsflags & PRESERVETIME)
1.48      xsa       628:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    629:
1.56      joris     630:                if (nflag != NULL)
                    631:                        rcs_attach_symbol(file, nflag);
                    632:
1.24      joris     633:                if (logstr != NULL) {
                    634:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    635:                                cvs_log(LP_ERR, "missing log message");
                    636:                                rcs_close(file);
                    637:                                continue;
                    638:                        }
                    639:
                    640:                        *logmsg++ = '\0';
                    641:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48      xsa       642:                                cvs_log(LP_ERR,
                    643:                                    "'%s' bad revision number", logstr);
1.24      joris     644:                                rcs_close(file);
                    645:                                continue;
                    646:                        }
                    647:
                    648:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    649:                                cvs_log(LP_ERR,
                    650:                                    "failed to set logmsg for '%s' to '%s'",
                    651:                                    logstr, logmsg);
                    652:                                rcs_close(file);
1.25      joris     653:                                rcsnum_free(logrev);
1.24      joris     654:                                continue;
                    655:                        }
                    656:
                    657:                        rcsnum_free(logrev);
1.39      xsa       658:                }
                    659:
                    660:                /* entries to add from <oldfile> */
1.56      joris     661:                if (rcsflags & CO_ACLAPPEND) {
1.39      xsa       662:                        /* XXX */
                    663:                        if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
                    664:                                exit(1);
                    665:
                    666:                        TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
                    667:                                rcs_access_add(file, acp->ra_name);
                    668:
                    669:                        rcs_close(oldfile);
1.24      joris     670:                }
                    671:
1.1       deraadt   672:                /* entries to add to the access list */
                    673:                if (alist != NULL) {
1.86      pat       674:                        struct cvs_argvector *aargv;
1.1       deraadt   675:
1.79      xsa       676:                        aargv = cvs_strsplit(alist, ",");
1.86      pat       677:                        for (j = 0; aargv->argv[j] != NULL; j++)
                    678:                                rcs_access_add(file, aargv->argv[j]);
1.1       deraadt   679:
1.86      pat       680:                        cvs_argv_destroy(aargv);
1.1       deraadt   681:                }
                    682:
                    683:                if (comment != NULL)
                    684:                        rcs_comment_set(file, comment);
1.82      xsa       685:
                    686:                if (elist != NULL) {
1.86      pat       687:                        struct cvs_argvector *eargv;
1.82      xsa       688:
                    689:                        eargv = cvs_strsplit(elist, ",");
1.86      pat       690:                        for (j = 0; eargv->argv[j] != NULL; j++)
                    691:                                rcs_access_remove(file, eargv->argv[j]);
1.82      xsa       692:
1.86      pat       693:                        cvs_argv_destroy(eargv);
1.87      ray       694:                } else if (rcsflags & RCSPROG_EFLAG) {
1.82      xsa       695:                        struct rcs_access *rap;
                    696:
                    697:                        /* XXX rcs_access_remove(file, NULL); ?? */
                    698:                        while (!TAILQ_EMPTY(&(file->rf_access))) {
                    699:                                rap = TAILQ_FIRST(&(file->rf_access));
                    700:                                TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
                    701:                                xfree(rap->ra_name);
                    702:                                xfree(rap);
                    703:                        }
                    704:                        /* not synced anymore */
                    705:                        file->rf_flags &= ~RCS_SYNCED;
                    706:                }
1.1       deraadt   707:
1.95      xsa       708:                rcs_kwexp_set(file, kflag);
1.1       deraadt   709:
                    710:                if (lkmode != -1)
1.84      xsa       711:                        (void)rcs_lock_setmode(file, lkmode);
1.92      ray       712:
                    713:                if (rcsflags & RCSPROG_LFLAG) {
                    714:                        RCSNUM *rev;
                    715:                        const char *username;
1.98      ray       716:                        char rev_str[16];
1.92      ray       717:
                    718:                        if ((username = getlogin()) == NULL)
                    719:                                fatal("could not get username");
                    720:                        if (lrev == NULL) {
                    721:                                rev = rcsnum_alloc();
                    722:                                rcsnum_cpy(file->rf_head, rev, 0);
                    723:                        } else if ((rev = rcsnum_parse(lrev)) == NULL) {
                    724:                                cvs_log(LP_ERR, "unable to unlock file");
                    725:                                rcs_close(file);
                    726:                                continue;
                    727:                        }
1.98      ray       728:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       729:                        /* Make sure revision exists. */
                    730:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       731:                                fatal("%s: can't lock nonexisting revision %s",
                    732:                                    fpath, rev_str);
                    733:                        if (rcs_lock_add(file, username, rev) != -1 &&
                    734:                            verbose == 1)
                    735:                                printf("%s locked\n", rev_str);
1.92      ray       736:                        rcsnum_free(rev);
                    737:                }
                    738:
                    739:                if (rcsflags & RCSPROG_UFLAG) {
                    740:                        RCSNUM *rev;
                    741:                        const char *username;
1.98      ray       742:                        char rev_str[16];
1.92      ray       743:
                    744:                        if ((username = getlogin()) == NULL)
                    745:                                fatal("could not get username");
                    746:                        if (urev == NULL) {
                    747:                                rev = rcsnum_alloc();
                    748:                                rcsnum_cpy(file->rf_head, rev, 0);
                    749:                        } else if ((rev = rcsnum_parse(urev)) == NULL) {
                    750:                                cvs_log(LP_ERR, "unable to unlock file");
                    751:                                rcs_close(file);
                    752:                                continue;
                    753:                        }
1.98      ray       754:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       755:                        /* Make sure revision exists. */
                    756:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       757:                                fatal("%s: can't unlock nonexisting revision %s",
                    758:                                    fpath, rev_str);
                    759:                        if (rcs_lock_remove(file, username, rev) == -1 &&
                    760:                            verbose == 1)
                    761:                                cvs_log(LP_ERR,
                    762:                                    "%s: warning: No locks are set.", fpath);
1.92      ray       763:                        rcsnum_free(rev);
                    764:                }
1.1       deraadt   765:
1.101     joris     766:                if (orange != NULL) {
                    767:                        struct rcs_delta *rdp;
                    768:                        char b[16];
                    769:
                    770:                        rcs_rev_select(file, orange);
                    771:                        TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
                    772:                                /*
                    773:                                 * Delete selected revisions.
                    774:                                 */
                    775:                                if (rdp->rd_flags & RCS_RD_SELECT) {
                    776:                                        rcsnum_tostr(rdp->rd_num, b, sizeof(b));
                    777:                                        printf("deleting revision %s\n", b);
                    778:                                        (void)rcs_rev_remove(file, rdp->rd_num);
                    779:                                }
                    780:                        }
                    781:                }
                    782:
1.1       deraadt   783:                rcs_close(file);
1.48      xsa       784:
1.56      joris     785:                if (rcsflags & PRESERVETIME)
1.48      xsa       786:                        rcs_set_mtime(fpath, rcs_mtime);
1.9       joris     787:
1.14      xsa       788:                if (verbose == 1)
1.12      joris     789:                        printf("done\n");
1.1       deraadt   790:        }
1.24      joris     791:
                    792:        if (logstr != NULL)
1.53      joris     793:                xfree(logstr);
1.1       deraadt   794:
1.56      joris     795:        if (nflag != NULL)
                    796:                xfree(nflag);
1.105     xsa       797:
                    798:        if (orange != NULL)
                    799:                xfree(orange);
1.56      joris     800:
1.1       deraadt   801:        return (0);
1.56      joris     802: }
                    803:
                    804: static void
                    805: rcs_attach_symbol(RCSFILE *file, const char *symname)
                    806: {
                    807:        char *rnum;
                    808:        RCSNUM *rev;
                    809:        char rbuf[16];
                    810:        int rm;
                    811:
                    812:        rm = 0;
                    813:        rev = NULL;
                    814:        if ((rnum = strrchr(symname, ':')) != NULL) {
                    815:                if (rnum[1] == '\0')
                    816:                        rev = file->rf_head;
                    817:                *(rnum++) = '\0';
                    818:        } else {
                    819:                rm = 1;
                    820:        }
                    821:
                    822:        if (rev == NULL && rm != 1) {
                    823:                if ((rev = rcsnum_parse(rnum)) == NULL)
                    824:                        fatal("bad revision %s", rnum);
                    825:        }
                    826:
1.87      ray       827:        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     828:                rm = 1;
                    829:
                    830:        if (rm == 1) {
                    831:                if (rcs_sym_remove(file, symname) < 0) {
1.102     deraadt   832:                        if (rcs_errno == RCS_ERR_NOENT &&
1.87      ray       833:                            !(rcsflags & RCSPROG_NFLAG))
1.56      joris     834:                                cvs_log(LP_WARN,
                    835:                                    "can't delete nonexisting symbol %s", symname);
                    836:                } else {
1.87      ray       837:                        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     838:                                rm = 0;
                    839:                }
                    840:        }
                    841:
                    842:        if (rm == 0) {
1.102     deraadt   843:                if (rcs_sym_add(file, symname, rev) < 0 &&
                    844:                    rcs_errno == RCS_ERR_DUPENT) {
1.56      joris     845:                        rcsnum_tostr(rcs_sym_getrev(file, symname),
                    846:                            rbuf, sizeof(rbuf));
                    847:                        fatal("symbolic name %s already bound to %s",
                    848:                            symname, rbuf);
                    849:                }
                    850:        }
                    851: }
                    852:
1.88      ray       853: /*
                    854:  * Load description from <in> to <file>.
                    855:  * If <in> starts with a `-', <in> is taken as the description.
                    856:  * Otherwise <in> is the name of the file containing the description.
                    857:  * If <in> is NULL, the description is read from stdin.
                    858:  */
1.56      joris     859: static void
                    860: rcs_set_description(RCSFILE *file, const char *in)
                    861: {
                    862:        BUF *bp;
                    863:        char *content, buf[128];
                    864:
                    865:        content = NULL;
1.88      ray       866:        /* Description is in file <in>. */
                    867:        if (in != NULL && *in != '-')
1.56      joris     868:                bp = cvs_buf_load(in, BUF_AUTOEXT);
1.88      ray       869:        /* Description is in <in>. */
                    870:        else if (in != NULL) {
                    871:                size_t len;
                    872:                const char *desc;
                    873:
                    874:                /* Skip leading `-'. */
                    875:                desc = in + 1;
                    876:                len = strlen(desc);
                    877:
                    878:                bp = cvs_buf_alloc(len + 1, BUF_AUTOEXT);
                    879:                cvs_buf_append(bp, desc, len);
                    880:        /* Get description from stdin. */
1.56      joris     881:        } else {
                    882:                bp = cvs_buf_alloc(64, BUF_AUTOEXT);
                    883:
1.90      xsa       884:                if (isatty(STDIN_FILENO))
                    885:                        (void)fprintf(stderr, "%s", DESC_PROMPT);
1.56      joris     886:                for (;;) {
1.88      ray       887:                        /* XXX - fgetln() may be more elegant. */
1.56      joris     888:                        fgets(buf, sizeof(buf), stdin);
1.88      ray       889:                        if (feof(stdin) || ferror(stdin) ||
                    890:                            strcmp(buf, ".\n") == 0 ||
                    891:                            strcmp(buf, ".") == 0)
1.56      joris     892:                                break;
                    893:                        cvs_buf_append(bp, buf, strlen(buf));
1.90      xsa       894:                        if (isatty(STDIN_FILENO))
                    895:                                (void)fprintf(stderr, ">> ");
1.56      joris     896:                }
                    897:        }
                    898:
                    899:        cvs_buf_putc(bp, '\0');
                    900:        content = cvs_buf_release(bp);
                    901:
                    902:        rcs_desc_set(file, content);
1.70      joris     903:        xfree(content);
1.101     joris     904: }
                    905:
1.104     xsa       906: u_int
1.101     joris     907: rcs_rev_select(RCSFILE *file, char *range)
                    908: {
                    909:        int i;
                    910:        u_int nrev;
                    911:        char *ep;
                    912:        char *lstr, *rstr;
                    913:        struct rcs_delta *rdp;
                    914:        struct cvs_argvector *revargv, *revrange;
                    915:        RCSNUM lnum, rnum;
                    916:
                    917:        nrev = 0;
                    918:        (void)memset(&lnum, 0, sizeof(lnum));
                    919:        (void)memset(&rnum, 0, sizeof(rnum));
                    920:
                    921:        if (range == NULL) {
                    922:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    923:                        if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
                    924:                                rdp->rd_flags |= RCS_RD_SELECT;
                    925:                                return (1);
                    926:                        }
                    927:                return (0);
                    928:        }
                    929:
                    930:        revargv = cvs_strsplit(range, ",");
                    931:        for (i = 0; revargv->argv[i] != NULL; i++) {
                    932:                revrange = cvs_strsplit(revargv->argv[i], ":");
                    933:                if (revrange->argv[0] == NULL)
                    934:                        /* should not happen */
                    935:                        fatal("invalid revision range: %s", revargv->argv[i]);
                    936:                else if (revrange->argv[1] == NULL)
                    937:                        lstr = rstr = revrange->argv[0];
                    938:                else {
                    939:                        if (revrange->argv[2] != NULL)
                    940:                                fatal("invalid revision range: %s",
                    941:                                        revargv->argv[i]);
                    942:                        lstr = revrange->argv[0];
                    943:                        rstr = revrange->argv[1];
                    944:                        if (strcmp(lstr, "") == 0)
                    945:                                lstr = NULL;
                    946:                        if (strcmp(rstr, "") == 0)
                    947:                                rstr = NULL;
                    948:                }
                    949:
                    950:                if (lstr == NULL)
                    951:                        lstr = RCS_HEAD_INIT;
                    952:                if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
                    953:                        fatal("invalid revision: %s", lstr);
                    954:
                    955:                if (rstr != NULL) {
                    956:                        if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
                    957:                                fatal("invalid revision: %s", rstr);
                    958:                } else
                    959:                        rcsnum_cpy(file->rf_head, &rnum, 0);
                    960:
                    961:                cvs_argv_destroy(revrange);
                    962:
                    963:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
1.102     deraadt   964:                        if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
                    965:                            rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
1.101     joris     966:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    967:                                rdp->rd_flags |= RCS_RD_SELECT;
                    968:                                nrev++;
                    969:                        }
                    970:        }
                    971:        cvs_argv_destroy(revargv);
                    972:
                    973:        if (lnum.rn_id != NULL)
                    974:                xfree(lnum.rn_id);
                    975:        if (rnum.rn_id != NULL)
                    976:                xfree(rnum.rn_id);
                    977:
                    978:        return (nrev);
1.1       deraadt   979: }