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

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