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

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