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

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