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

1.109   ! xsa         1: /*     $OpenBSD: rcsprog.c,v 1.108 2006/04/19 06:53:41 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) {
                     98:                cvs_log(LP_ERRNO, "failed to stat `%s'", filename);
                     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)
                    211:                cvs_log(LP_ERR, "unknown option -%c", opt);
                    212:        else if (ret == 1)
                    213:                cvs_log(LP_ERR, "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.75      ray       355:                        cvs_log(LP_ERRNO, "%s", rcspath);
                    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)
                    377:                cvs_log(LP_WARN, "redefinition of revision number");
                    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.9       joris     426:        cvs_log_init(LD_STD, 0);
1.68      joris     427:        SLIST_INIT(&rcs_temp_files);
1.1       deraadt   428:
1.29      joris     429:        cmd_argc = 0;
1.30      joris     430:        cmd_argv[cmd_argc++] = argv[0];
1.29      joris     431:        if ((rcsinit = getenv("RCSINIT")) != NULL) {
                    432:                ret = rcs_init(rcsinit, cmd_argv + 1,
                    433:                    RCS_CMD_MAXARG - 1);
                    434:                if (ret < 0) {
                    435:                        cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
                    436:                        exit (1);
                    437:                }
                    438:
                    439:                cmd_argc += ret;
                    440:        }
1.36      xsa       441:
                    442:        if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
                    443:                rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29      joris     444:
                    445:        for (ret = 1; ret < argc; ret++)
                    446:                cmd_argv[cmd_argc++] = argv[ret];
1.68      joris     447:
                    448:        signal(SIGHUP, sighdlr);
                    449:        signal(SIGINT, sighdlr);
                    450:        signal(SIGQUIT, sighdlr);
                    451:        signal(SIGABRT, sighdlr);
                    452:        signal(SIGALRM, sighdlr);
                    453:        signal(SIGTERM, sighdlr);
1.29      joris     454:
1.1       deraadt   455:        for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2       deraadt   456:                if (strcmp(__progname, programs[i].prog_name) == 0) {
                    457:                        usage = programs[i].prog_usage;
1.29      joris     458:                        ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2       deraadt   459:                        break;
                    460:                }
1.100     joris     461:
                    462:        /* clean up temporary files */
                    463:        cvs_worklist_run(&rcs_temp_files, cvs_worklist_unlink);
1.1       deraadt   464:
1.23      niallo    465:        exit(ret);
1.76      ray       466:        /* NOTREACHED */
1.1       deraadt   467: }
                    468:
                    469:
                    470: void
                    471: rcs_usage(void)
                    472: {
                    473:        fprintf(stderr,
1.66      jmc       474:            "usage: rcs [-ehIiLMqTUV] [-Aoldfile] [-ausers] [-b[rev]]\n"
1.92      ray       475:            "           [-cstring] [-e[users]] [-kmode] [-l[rev]] [-mrev:msg]\n"
1.101     joris     476:            "           [-orange] [-sstate[:rev]] [-tfile|str] [-u[rev]]\n"
1.67      xsa       477:            "           [-xsuffixes] file ...\n");
1.1       deraadt   478: }
                    479:
                    480: /*
                    481:  * rcs_main()
                    482:  *
                    483:  * Handler for the `rcs' program.
                    484:  * Returns 0 on success, or >0 on error.
                    485:  */
                    486: int
                    487: rcs_main(int argc, char **argv)
                    488: {
1.87      ray       489:        int i, j, ch, kflag, lkmode;
1.39      xsa       490:        char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56      joris     491:        char *logstr, *logmsg, *nflag, *descfile;
1.101     joris     492:        char *alist, *comment, *elist, *lrev, *urev, *orange;
1.1       deraadt   493:        mode_t fmode;
1.39      xsa       494:        RCSFILE *file, *oldfile;
1.24      joris     495:        RCSNUM *logrev;
1.39      xsa       496:        struct rcs_access *acp;
1.48      xsa       497:        time_t rcs_mtime = -1;
1.1       deraadt   498:
1.95      xsa       499:        kflag = RCS_KWEXP_ERR;
                    500:        lkmode = -1;
1.89      niallo    501:        fmode =  S_IRUSR|S_IRGRP|S_IROTH;
1.58      niallo    502:        flags = RCS_RDWR|RCS_PARSE_FULLY;
1.92      ray       503:        lrev = urev = descfile = nflag = NULL;
1.101     joris     504:        logstr = alist = comment = elist = orange = NULL;
1.1       deraadt   505:
1.51      xsa       506:        while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1       deraadt   507:                switch (ch) {
                    508:                case 'A':
1.39      xsa       509:                        if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
                    510:                                exit(1);
1.56      joris     511:                        rcsflags |= CO_ACLAPPEND;
1.1       deraadt   512:                        break;
                    513:                case 'a':
1.28      joris     514:                        alist = rcs_optarg;
1.1       deraadt   515:                        break;
                    516:                case 'c':
1.28      joris     517:                        comment = rcs_optarg;
1.1       deraadt   518:                        break;
                    519:                case 'e':
1.28      joris     520:                        elist = rcs_optarg;
1.87      ray       521:                        rcsflags |= RCSPROG_EFLAG;
1.1       deraadt   522:                        break;
                    523:                case 'i':
                    524:                        flags |= RCS_CREATE;
                    525:                        break;
                    526:                case 'k':
1.28      joris     527:                        kflag = rcs_kflag_get(rcs_optarg);
1.1       deraadt   528:                        if (RCS_KWEXP_INVAL(kflag)) {
                    529:                                cvs_log(LP_ERR,
                    530:                                    "invalid keyword substitution mode `%s'",
1.28      joris     531:                                    rcs_optarg);
1.1       deraadt   532:                                exit(1);
                    533:                        }
                    534:                        break;
                    535:                case 'L':
                    536:                        if (lkmode == RCS_LOCK_LOOSE)
                    537:                                cvs_log(LP_WARN, "-U overriden by -L");
                    538:                        lkmode = RCS_LOCK_STRICT;
                    539:                        break;
1.92      ray       540:                case 'l':
                    541:                        /* XXX - Check with -u flag. */
                    542:                        lrev = rcs_optarg;
                    543:                        rcsflags |= RCSPROG_LFLAG;
                    544:                        break;
1.24      joris     545:                case 'm':
1.53      joris     546:                        logstr = xstrdup(rcs_optarg);
1.24      joris     547:                        break;
1.1       deraadt   548:                case 'M':
                    549:                        /* ignore for the moment */
                    550:                        break;
1.56      joris     551:                case 'n':
                    552:                        nflag = xstrdup(rcs_optarg);
                    553:                        break;
                    554:                case 'N':
                    555:                        nflag = xstrdup(rcs_optarg);
1.87      ray       556:                        rcsflags |= RCSPROG_NFLAG;
1.56      joris     557:                        break;
1.101     joris     558:                case 'o':
                    559:                        orange = xstrdup(rcs_optarg);
                    560:                        break;
1.12      joris     561:                case 'q':
1.108     xsa       562:                        rcsflags |= QUIET;
1.46      xsa       563:                        break;
1.56      joris     564:                case 't':
                    565:                        descfile = rcs_optarg;
1.87      ray       566:                        rcsflags |= RCSPROG_TFLAG;
1.56      joris     567:                        break;
1.46      xsa       568:                case 'T':
1.56      joris     569:                        rcsflags |= PRESERVETIME;
1.12      joris     570:                        break;
1.1       deraadt   571:                case 'U':
                    572:                        if (lkmode == RCS_LOCK_STRICT)
                    573:                                cvs_log(LP_WARN, "-L overriden by -U");
                    574:                        lkmode = RCS_LOCK_LOOSE;
                    575:                        break;
1.92      ray       576:                case 'u':
                    577:                        /* XXX - Check with -l flag. */
                    578:                        urev = rcs_optarg;
                    579:                        rcsflags |= RCSPROG_UFLAG;
                    580:                        break;
1.1       deraadt   581:                case 'V':
                    582:                        printf("%s\n", rcs_version);
                    583:                        exit(0);
1.76      ray       584:                        /* NOTREACHED */
1.45      xsa       585:                case 'x':
1.85      ray       586:                        /* Use blank extension if none given. */
                    587:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.51      xsa       588:                        break;
                    589:                case 'z':
                    590:                        /*
                    591:                         * kept for compatibility
                    592:                         */
1.45      xsa       593:                        break;
1.1       deraadt   594:                default:
1.2       deraadt   595:                        (usage)();
1.1       deraadt   596:                        exit(1);
                    597:                }
                    598:        }
                    599:
1.28      joris     600:        argc -= rcs_optind;
                    601:        argv += rcs_optind;
1.30      joris     602:
1.1       deraadt   603:        if (argc == 0) {
                    604:                cvs_log(LP_ERR, "no input file");
1.5       joris     605:                (usage)();
1.1       deraadt   606:                exit(1);
                    607:        }
                    608:
                    609:        for (i = 0; i < argc; i++) {
1.9       joris     610:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     611:                        continue;
1.6       joris     612:
1.108     xsa       613:                if (!(rcsflags & QUIET))
1.35      niallo    614:                        printf("RCS file: %s\n", fpath);
1.48      xsa       615:
1.49      niallo    616:                if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6       joris     617:                        continue;
1.1       deraadt   618:
1.87      ray       619:                if (rcsflags & RCSPROG_TFLAG)
                    620:                        rcs_set_description(file, descfile);
                    621:                else if (flags & RCS_CREATE)
1.56      joris     622:                        rcs_set_description(file, NULL);
                    623:
                    624:                if (rcsflags & PRESERVETIME)
1.48      xsa       625:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    626:
1.56      joris     627:                if (nflag != NULL)
                    628:                        rcs_attach_symbol(file, nflag);
                    629:
1.24      joris     630:                if (logstr != NULL) {
                    631:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    632:                                cvs_log(LP_ERR, "missing log message");
                    633:                                rcs_close(file);
                    634:                                continue;
                    635:                        }
                    636:
                    637:                        *logmsg++ = '\0';
                    638:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48      xsa       639:                                cvs_log(LP_ERR,
                    640:                                    "'%s' bad revision number", logstr);
1.24      joris     641:                                rcs_close(file);
                    642:                                continue;
                    643:                        }
                    644:
                    645:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    646:                                cvs_log(LP_ERR,
                    647:                                    "failed to set logmsg for '%s' to '%s'",
                    648:                                    logstr, logmsg);
                    649:                                rcs_close(file);
1.25      joris     650:                                rcsnum_free(logrev);
1.24      joris     651:                                continue;
                    652:                        }
                    653:
                    654:                        rcsnum_free(logrev);
1.39      xsa       655:                }
                    656:
                    657:                /* entries to add from <oldfile> */
1.56      joris     658:                if (rcsflags & CO_ACLAPPEND) {
1.39      xsa       659:                        /* XXX */
                    660:                        if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
                    661:                                exit(1);
                    662:
                    663:                        TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
                    664:                                rcs_access_add(file, acp->ra_name);
                    665:
                    666:                        rcs_close(oldfile);
1.24      joris     667:                }
                    668:
1.1       deraadt   669:                /* entries to add to the access list */
                    670:                if (alist != NULL) {
1.86      pat       671:                        struct cvs_argvector *aargv;
1.1       deraadt   672:
1.79      xsa       673:                        aargv = cvs_strsplit(alist, ",");
1.86      pat       674:                        for (j = 0; aargv->argv[j] != NULL; j++)
                    675:                                rcs_access_add(file, aargv->argv[j]);
1.1       deraadt   676:
1.86      pat       677:                        cvs_argv_destroy(aargv);
1.1       deraadt   678:                }
                    679:
                    680:                if (comment != NULL)
                    681:                        rcs_comment_set(file, comment);
1.82      xsa       682:
                    683:                if (elist != NULL) {
1.86      pat       684:                        struct cvs_argvector *eargv;
1.82      xsa       685:
                    686:                        eargv = cvs_strsplit(elist, ",");
1.86      pat       687:                        for (j = 0; eargv->argv[j] != NULL; j++)
                    688:                                rcs_access_remove(file, eargv->argv[j]);
1.82      xsa       689:
1.86      pat       690:                        cvs_argv_destroy(eargv);
1.87      ray       691:                } else if (rcsflags & RCSPROG_EFLAG) {
1.82      xsa       692:                        struct rcs_access *rap;
                    693:
                    694:                        /* XXX rcs_access_remove(file, NULL); ?? */
                    695:                        while (!TAILQ_EMPTY(&(file->rf_access))) {
                    696:                                rap = TAILQ_FIRST(&(file->rf_access));
                    697:                                TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
                    698:                                xfree(rap->ra_name);
                    699:                                xfree(rap);
                    700:                        }
                    701:                        /* not synced anymore */
                    702:                        file->rf_flags &= ~RCS_SYNCED;
                    703:                }
1.1       deraadt   704:
1.95      xsa       705:                rcs_kwexp_set(file, kflag);
1.1       deraadt   706:
                    707:                if (lkmode != -1)
1.84      xsa       708:                        (void)rcs_lock_setmode(file, lkmode);
1.92      ray       709:
                    710:                if (rcsflags & RCSPROG_LFLAG) {
                    711:                        RCSNUM *rev;
                    712:                        const char *username;
1.98      ray       713:                        char rev_str[16];
1.92      ray       714:
                    715:                        if ((username = getlogin()) == NULL)
                    716:                                fatal("could not get username");
                    717:                        if (lrev == NULL) {
                    718:                                rev = rcsnum_alloc();
                    719:                                rcsnum_cpy(file->rf_head, rev, 0);
                    720:                        } else if ((rev = rcsnum_parse(lrev)) == NULL) {
                    721:                                cvs_log(LP_ERR, "unable to unlock file");
                    722:                                rcs_close(file);
                    723:                                continue;
                    724:                        }
1.98      ray       725:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       726:                        /* Make sure revision exists. */
                    727:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       728:                                fatal("%s: can't lock nonexisting revision %s",
                    729:                                    fpath, rev_str);
                    730:                        if (rcs_lock_add(file, username, rev) != -1 &&
1.108     xsa       731:                            !(rcsflags & QUIET))
1.98      ray       732:                                printf("%s locked\n", rev_str);
1.92      ray       733:                        rcsnum_free(rev);
                    734:                }
                    735:
                    736:                if (rcsflags & RCSPROG_UFLAG) {
                    737:                        RCSNUM *rev;
                    738:                        const char *username;
1.98      ray       739:                        char rev_str[16];
1.92      ray       740:
                    741:                        if ((username = getlogin()) == NULL)
                    742:                                fatal("could not get username");
                    743:                        if (urev == NULL) {
                    744:                                rev = rcsnum_alloc();
                    745:                                rcsnum_cpy(file->rf_head, rev, 0);
                    746:                        } else if ((rev = rcsnum_parse(urev)) == NULL) {
                    747:                                cvs_log(LP_ERR, "unable to unlock file");
                    748:                                rcs_close(file);
                    749:                                continue;
                    750:                        }
1.98      ray       751:                        rcsnum_tostr(rev, rev_str, sizeof(rev_str));
1.92      ray       752:                        /* Make sure revision exists. */
                    753:                        if (rcs_findrev(file, rev) == NULL)
1.98      ray       754:                                fatal("%s: can't unlock nonexisting revision %s",
                    755:                                    fpath, rev_str);
                    756:                        if (rcs_lock_remove(file, username, rev) == -1 &&
1.108     xsa       757:                            !(rcsflags & QUIET))
1.98      ray       758:                                cvs_log(LP_ERR,
                    759:                                    "%s: warning: No locks are set.", fpath);
1.92      ray       760:                        rcsnum_free(rev);
                    761:                }
1.1       deraadt   762:
1.101     joris     763:                if (orange != NULL) {
                    764:                        struct rcs_delta *rdp;
                    765:                        char b[16];
                    766:
                    767:                        rcs_rev_select(file, orange);
                    768:                        TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
                    769:                                /*
                    770:                                 * Delete selected revisions.
                    771:                                 */
                    772:                                if (rdp->rd_flags & RCS_RD_SELECT) {
                    773:                                        rcsnum_tostr(rdp->rd_num, b, sizeof(b));
                    774:                                        printf("deleting revision %s\n", b);
                    775:                                        (void)rcs_rev_remove(file, rdp->rd_num);
                    776:                                }
                    777:                        }
                    778:                }
                    779:
1.1       deraadt   780:                rcs_close(file);
1.48      xsa       781:
1.56      joris     782:                if (rcsflags & PRESERVETIME)
1.48      xsa       783:                        rcs_set_mtime(fpath, rcs_mtime);
1.9       joris     784:
1.108     xsa       785:                if (!(rcsflags & QUIET))
1.12      joris     786:                        printf("done\n");
1.1       deraadt   787:        }
1.24      joris     788:
                    789:        if (logstr != NULL)
1.53      joris     790:                xfree(logstr);
1.1       deraadt   791:
1.56      joris     792:        if (nflag != NULL)
                    793:                xfree(nflag);
1.105     xsa       794:
                    795:        if (orange != NULL)
                    796:                xfree(orange);
1.56      joris     797:
1.1       deraadt   798:        return (0);
1.56      joris     799: }
                    800:
                    801: static void
                    802: rcs_attach_symbol(RCSFILE *file, const char *symname)
                    803: {
                    804:        char *rnum;
                    805:        RCSNUM *rev;
                    806:        char rbuf[16];
                    807:        int rm;
                    808:
                    809:        rm = 0;
                    810:        rev = NULL;
                    811:        if ((rnum = strrchr(symname, ':')) != NULL) {
                    812:                if (rnum[1] == '\0')
                    813:                        rev = file->rf_head;
                    814:                *(rnum++) = '\0';
                    815:        } else {
                    816:                rm = 1;
                    817:        }
                    818:
                    819:        if (rev == NULL && rm != 1) {
                    820:                if ((rev = rcsnum_parse(rnum)) == NULL)
                    821:                        fatal("bad revision %s", rnum);
                    822:        }
                    823:
1.87      ray       824:        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     825:                rm = 1;
                    826:
                    827:        if (rm == 1) {
                    828:                if (rcs_sym_remove(file, symname) < 0) {
1.102     deraadt   829:                        if (rcs_errno == RCS_ERR_NOENT &&
1.87      ray       830:                            !(rcsflags & RCSPROG_NFLAG))
1.56      joris     831:                                cvs_log(LP_WARN,
                    832:                                    "can't delete nonexisting symbol %s", symname);
                    833:                } else {
1.87      ray       834:                        if (rcsflags & RCSPROG_NFLAG)
1.56      joris     835:                                rm = 0;
                    836:                }
                    837:        }
                    838:
                    839:        if (rm == 0) {
1.102     deraadt   840:                if (rcs_sym_add(file, symname, rev) < 0 &&
                    841:                    rcs_errno == RCS_ERR_DUPENT) {
1.56      joris     842:                        rcsnum_tostr(rcs_sym_getrev(file, symname),
                    843:                            rbuf, sizeof(rbuf));
                    844:                        fatal("symbolic name %s already bound to %s",
                    845:                            symname, rbuf);
                    846:                }
                    847:        }
                    848: }
                    849:
1.88      ray       850: /*
                    851:  * Load description from <in> to <file>.
                    852:  * If <in> starts with a `-', <in> is taken as the description.
                    853:  * Otherwise <in> is the name of the file containing the description.
                    854:  * If <in> is NULL, the description is read from stdin.
                    855:  */
1.56      joris     856: static void
                    857: rcs_set_description(RCSFILE *file, const char *in)
                    858: {
                    859:        BUF *bp;
1.107     ray       860:        char *content;
1.56      joris     861:
1.88      ray       862:        /* Description is in file <in>. */
1.107     ray       863:        if (in != NULL && *in != '-') {
1.56      joris     864:                bp = cvs_buf_load(in, BUF_AUTOEXT);
1.107     ray       865:                cvs_buf_putc(bp, '\0');
                    866:                content = cvs_buf_release(bp);
1.88      ray       867:        /* Description is in <in>. */
1.107     ray       868:        } else if (in != NULL)
                    869:                /* Skip leading `-'. */
                    870:                content = xstrdup(in + 1);
                    871:        /* Get description from stdin. */
                    872:        else
                    873:                content = rcs_prompt(DESC_PROMPT);
1.88      ray       874:
1.107     ray       875:        rcs_desc_set(file, content);
                    876:        xfree(content);
                    877: }
                    878:
                    879: /*
                    880:  * Prompt for and store user's input in an allocated string.
                    881:  *
                    882:  * Returns the string's pointer.
                    883:  */
                    884: char *
                    885: rcs_prompt(const char *prompt)
                    886: {
                    887:        BUF *bp;
                    888:        size_t len;
                    889:        char *buf;
1.88      ray       890:
1.107     ray       891:        bp = cvs_buf_alloc(0, BUF_AUTOEXT);
                    892:        if (isatty(STDIN_FILENO))
                    893:                (void)fprintf(stderr, "%s", prompt);
                    894:        if (isatty(STDIN_FILENO))
                    895:                (void)fprintf(stderr, ">> ");
                    896:        while ((buf = fgetln(stdin, &len)) != NULL) {
                    897:                /* The last line may not be EOL terminated. */
                    898:                if (buf[0] == '.' && (len == 1 || buf[1] == '\n'))
                    899:                        break;
                    900:                else
                    901:                        cvs_buf_append(bp, buf, len);
1.56      joris     902:
1.90      xsa       903:                if (isatty(STDIN_FILENO))
1.107     ray       904:                        (void)fprintf(stderr, ">> ");
1.56      joris     905:        }
                    906:        cvs_buf_putc(bp, '\0');
                    907:
1.107     ray       908:        return (cvs_buf_release(bp));
1.101     joris     909: }
                    910:
1.104     xsa       911: u_int
1.101     joris     912: rcs_rev_select(RCSFILE *file, char *range)
                    913: {
                    914:        int i;
                    915:        u_int nrev;
                    916:        char *ep;
                    917:        char *lstr, *rstr;
                    918:        struct rcs_delta *rdp;
                    919:        struct cvs_argvector *revargv, *revrange;
                    920:        RCSNUM lnum, rnum;
                    921:
                    922:        nrev = 0;
                    923:        (void)memset(&lnum, 0, sizeof(lnum));
                    924:        (void)memset(&rnum, 0, sizeof(rnum));
                    925:
                    926:        if (range == NULL) {
                    927:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
                    928:                        if (rcsnum_cmp(rdp->rd_num, file->rf_head, 0) == 0) {
                    929:                                rdp->rd_flags |= RCS_RD_SELECT;
                    930:                                return (1);
                    931:                        }
                    932:                return (0);
                    933:        }
                    934:
                    935:        revargv = cvs_strsplit(range, ",");
                    936:        for (i = 0; revargv->argv[i] != NULL; i++) {
                    937:                revrange = cvs_strsplit(revargv->argv[i], ":");
                    938:                if (revrange->argv[0] == NULL)
                    939:                        /* should not happen */
                    940:                        fatal("invalid revision range: %s", revargv->argv[i]);
                    941:                else if (revrange->argv[1] == NULL)
                    942:                        lstr = rstr = revrange->argv[0];
                    943:                else {
                    944:                        if (revrange->argv[2] != NULL)
                    945:                                fatal("invalid revision range: %s",
                    946:                                        revargv->argv[i]);
                    947:                        lstr = revrange->argv[0];
                    948:                        rstr = revrange->argv[1];
                    949:                        if (strcmp(lstr, "") == 0)
                    950:                                lstr = NULL;
                    951:                        if (strcmp(rstr, "") == 0)
                    952:                                rstr = NULL;
                    953:                }
                    954:
                    955:                if (lstr == NULL)
                    956:                        lstr = RCS_HEAD_INIT;
                    957:                if (rcsnum_aton(lstr, &ep, &lnum) == 0 || (*ep != '\0'))
                    958:                        fatal("invalid revision: %s", lstr);
                    959:
                    960:                if (rstr != NULL) {
                    961:                        if (rcsnum_aton(rstr, &ep, &rnum) == 0 || (*ep != '\0'))
                    962:                                fatal("invalid revision: %s", rstr);
                    963:                } else
                    964:                        rcsnum_cpy(file->rf_head, &rnum, 0);
                    965:
                    966:                cvs_argv_destroy(revrange);
                    967:
                    968:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list)
1.102     deraadt   969:                        if (rcsnum_cmp(rdp->rd_num, &lnum, 0) <= 0 &&
                    970:                            rcsnum_cmp(rdp->rd_num, &rnum, 0) >= 0 &&
1.101     joris     971:                            !(rdp->rd_flags & RCS_RD_SELECT)) {
                    972:                                rdp->rd_flags |= RCS_RD_SELECT;
                    973:                                nrev++;
                    974:                        }
                    975:        }
                    976:        cvs_argv_destroy(revargv);
                    977:
                    978:        if (lnum.rn_id != NULL)
                    979:                xfree(lnum.rn_id);
                    980:        if (rnum.rn_id != NULL)
                    981:                xfree(rnum.rn_id);
                    982:
                    983:        return (nrev);
1.1       deraadt   984: }