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

1.82    ! xsa         1: /*     $OpenBSD: rcsprog.c,v 1.81 2006/03/21 02:50:15 ray 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.56      joris      32: #define RCSPROG_OPTSTRING      "A:a:b::c:e::hik:Lm:Mn:N:qt::TUVx:z:"
                     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.82    ! xsa        42: #define RCS_EFLAG      (1<<0)
        !            43: #define RCS_NFLAG      (1<<1)
        !            44: #define RCS_TFLAG      (1<<2)
1.56      joris      45: static int rcsflags = 0;
                     46:
1.36      xsa        47: int     rcs_optind;
1.28      joris      48: char   *rcs_optarg;
1.42      xsa        49: char   *rcs_suffixes;
1.36      xsa        50: char   *rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.28      joris      51:
1.1       deraadt    52: struct rcs_prog {
1.26      deraadt    53:        char    *prog_name;
                     54:        int     (*prog_hdlr)(int, char **);
                     55:        void    (*prog_usage)(void);
1.1       deraadt    56: } programs[] = {
1.2       deraadt    57:        { "rcs",        rcs_main,       rcs_usage       },
1.17      joris      58:        { "ci",         checkin_main,   checkin_usage   },
1.11      joris      59:        { "co",         checkout_main,  checkout_usage  },
1.20      joris      60:        { "rcsclean",   rcsclean_main,  rcsclean_usage  },
1.18      joris      61:        { "rcsdiff",    rcsdiff_main,   rcsdiff_usage   },
1.33      xsa        62:        { "rcsmerge",   rcsmerge_main,  rcsmerge_usage  },
1.21      joris      63:        { "rlog",       rlog_main,      rlog_usage      },
1.22      joris      64:        { "ident",      ident_main,     ident_usage     },
1.1       deraadt    65: };
1.31      joris      66:
1.68      joris      67: struct cvs_wklhead rcs_temp_files;
                     68:
                     69: void sighdlr(int);
1.56      joris      70: static void rcs_set_description(RCSFILE *, const char *);
                     71: static void rcs_attach_symbol(RCSFILE *, const char *);
                     72:
1.78      ray        73: /* ARGSUSED */
1.31      joris      74: void
1.68      joris      75: sighdlr(int sig)
                     76: {
                     77:        cvs_worklist_clean(&rcs_temp_files, cvs_worklist_unlink);
                     78:        _exit(1);
                     79: }
                     80:
                     81: void
1.31      joris      82: rcs_set_rev(const char *str, RCSNUM **rev)
                     83: {
1.32      joris      84:        if (str == NULL)
                     85:                return;
                     86:
1.52      joris      87:        if ((*rev != NULL) && (*rev != RCS_HEAD_REV))
1.31      joris      88:                cvs_log(LP_WARN, "redefinition of revision number");
                     89:
1.55      xsa        90:        if ((*rev = rcsnum_parse(str)) == NULL)
                     91:                fatal("bad revision number '%s'", str);
1.47      xsa        92: }
                     93:
                     94: /*
                     95:  * rcs_get_mtime()
                     96:  *
                     97:  * Get <filename> last modified time.
                     98:  * Returns last modified time on success, or -1 on failure.
                     99:  */
                    100: time_t
                    101: rcs_get_mtime(const char *filename)
                    102: {
                    103:        struct stat st;
                    104:        time_t mtime;
                    105:
                    106:        if (stat(filename, &st) == -1) {
                    107:                cvs_log(LP_ERRNO, "failed to stat `%s'", filename);
                    108:                return (-1);
                    109:        }
                    110:        mtime = (time_t)st.st_mtimespec.tv_sec;
                    111:
                    112:        return (mtime);
                    113: }
                    114:
                    115: /*
                    116:  * rcs_set_mtime()
                    117:  *
                    118:  * Set <filename> last modified time to <mtime> if it's not set to -1.
                    119:  * Returns 0 on success, or -1 on failure.
                    120:  */
                    121: int
                    122: rcs_set_mtime(const char *filename, time_t mtime)
                    123: {
                    124:        static struct timeval tv[2];
                    125:
                    126:        if (mtime == -1)
                    127:                return (0);
                    128:
                    129:        tv[0].tv_sec = mtime;
                    130:        tv[1].tv_sec = tv[0].tv_sec;
                    131:
                    132:        if (utimes(filename, tv) == -1) {
                    133:                cvs_log(LP_ERRNO, "error setting utimes");
                    134:                return (-1);
                    135:        }
                    136:
                    137:        return (0);
1.31      joris     138: }
1.1       deraadt   139:
1.9       joris     140: int
1.29      joris     141: rcs_init(char *envstr, char **argv, int argvlen)
                    142: {
                    143:        u_int i;
                    144:        int argc, error;
                    145:        char linebuf[256],  *lp, *cp;
                    146:
                    147:        strlcpy(linebuf, envstr, sizeof(linebuf));
                    148:        memset(argv, 0, argvlen * sizeof(char *));
                    149:
                    150:        error = argc = 0;
                    151:        for (lp = linebuf; lp != NULL;) {
1.61      xsa       152:                cp = strsep(&lp, " \t\b\f\n\r\t\v");
1.29      joris     153:                if (cp == NULL)
                    154:                        break;
                    155:                else if (*cp == '\0')
                    156:                        continue;
                    157:
                    158:                if (argc == argvlen) {
                    159:                        error++;
                    160:                        break;
                    161:                }
                    162:
1.53      joris     163:                argv[argc] = xstrdup(cp);
1.29      joris     164:                argc++;
                    165:        }
                    166:
                    167:        if (error != 0) {
                    168:                for (i = 0; i < (u_int)argc; i++)
1.53      joris     169:                        xfree(argv[i]);
1.29      joris     170:                argc = -1;
                    171:        }
                    172:
                    173:        return (argc);
                    174: }
                    175:
                    176: int
1.28      joris     177: rcs_getopt(int argc, char **argv, const char *optstr)
                    178: {
                    179:        char *a;
                    180:        const char *c;
                    181:        static int i = 1;
                    182:        int opt, hasargument, ret;
                    183:
                    184:        hasargument = 0;
                    185:        rcs_optarg = NULL;
                    186:
                    187:        if (i >= argc)
                    188:                return (-1);
                    189:
                    190:        a = argv[i++];
                    191:        if (*a++ != '-')
                    192:                return (-1);
                    193:
                    194:        ret = 0;
                    195:        opt = *a;
                    196:        for (c = optstr; *c != '\0'; c++) {
                    197:                if (*c == opt) {
                    198:                        a++;
                    199:                        ret = opt;
                    200:
                    201:                        if (*(c + 1) == ':') {
                    202:                                if (*(c + 2) == ':') {
                    203:                                        if (*a != '\0')
                    204:                                                hasargument = 1;
                    205:                                } else {
                    206:                                        if (*a != '\0') {
                    207:                                                hasargument = 1;
                    208:                                        } else {
                    209:                                                ret = 1;
                    210:                                                break;
                    211:                                        }
                    212:                                }
                    213:                        }
                    214:
                    215:                        if (hasargument == 1)
                    216:                                rcs_optarg = a;
                    217:
                    218:                        if (ret == opt)
                    219:                                rcs_optind++;
                    220:                        break;
                    221:                }
                    222:        }
                    223:
                    224:        if (ret == 0)
                    225:                cvs_log(LP_ERR, "unknown option -%c", opt);
                    226:        else if (ret == 1)
                    227:                cvs_log(LP_ERR, "missing argument for option -%c", opt);
1.74      ray       228:
                    229:        return (ret);
                    230: }
                    231:
                    232: /*
                    233:  * rcs_choosefile()
                    234:  *
                    235:  * Given a relative filename, decide where the corresponding RCS file
                    236:  * should be.  Tries each extension until a file is found.  If no file
                    237:  * was found, returns a path with the first extension.
                    238:  *
                    239:  * Returns pointer to a char array on success, NULL on failure.
                    240:  */
                    241: char *
                    242: rcs_choosefile(const char *filename)
                    243: {
                    244:        struct stat sb;
                    245:        char *ext, name[MAXPATHLEN], *next, *ptr, rcsdir[MAXPATHLEN],
                    246:            *ret, *suffixes, rcspath[MAXPATHLEN];
                    247:
                    248:        /* If -x flag was not given, use default. */
                    249:        if (rcs_suffixes == NULL)
                    250:                rcs_suffixes = RCS_DEFAULT_SUFFIX;
                    251:
                    252:        /*
                    253:         * If `filename' contains a directory, `rcspath' contains that
                    254:         * directory, including a trailing slash.  Otherwise `rcspath'
                    255:         * contains an empty string.
                    256:         */
                    257:        if (strlcpy(rcspath, filename, sizeof(rcspath)) >= sizeof(rcspath))
                    258:                return (NULL);
                    259:        /* If `/' is found, end string after `/'. */
                    260:        if ((ptr = strrchr(rcspath, '/')) != NULL)
                    261:                *(++ptr) = '\0';
                    262:        else
                    263:                rcspath[0] = '\0';
                    264:
                    265:        /* Append RCS/ to `rcspath' if it exists. */
                    266:        if (strlcpy(rcsdir, rcspath, sizeof(rcsdir)) >= sizeof(rcsdir) ||
                    267:            strlcat(rcsdir, RCSDIR, sizeof(rcsdir)) >= sizeof(rcsdir))
                    268:                return (NULL);
                    269:        if ((stat(rcsdir, &sb) == 0) && (sb.st_mode & S_IFDIR))
                    270:                if (strlcpy(rcspath, rcsdir, sizeof(rcspath)) >= sizeof(rcspath) ||
                    271:                    strlcat(rcspath, "/", sizeof(rcspath)) >= sizeof(rcspath))
                    272:                        return (NULL);
                    273:
                    274:        /* Name of file without path. */
                    275:        if ((ptr = strrchr(filename, '/')) == NULL) {
                    276:                if (strlcpy(name, filename, sizeof(name)) >= sizeof(name))
                    277:                        return (NULL);
                    278:        } else {
                    279:                /* Skip `/'. */
                    280:                if (strlcpy(name, ptr + 1, sizeof(name)) >= sizeof(name))
                    281:                        return (NULL);
                    282:        }
                    283:
                    284:        /* Name of RCS file without an extension. */
                    285:        if (strlcat(rcspath, name, sizeof(rcspath)) >= sizeof(rcspath))
                    286:                return (NULL);
                    287:
                    288:        /*
                    289:         * If only the empty suffix was given, use existing rcspath.
                    290:         * This ensures that there is at least one suffix for strsep().
                    291:         */
                    292:        if (strcmp(rcs_suffixes, "") == 0) {
1.77      ray       293:                ret = xstrdup(rcspath);
1.74      ray       294:                return (ret);
                    295:        }
                    296:
                    297:        /*
                    298:         * Cycle through slash-separated `rcs_suffixes', appending each
                    299:         * extension to `rcspath' and testing if the file exists.  If it
                    300:         * does, return that string.  Otherwise return path with first
                    301:         * extension.
                    302:         */
1.77      ray       303:        suffixes = xstrdup(rcs_suffixes);
1.74      ray       304:        for (ret = NULL, next = suffixes; (ext = strsep(&next, "/")) != NULL;) {
                    305:                char fpath[MAXPATHLEN];
                    306:
                    307:                /* Construct RCS file path. */
                    308:                if (strlcpy(fpath, rcspath, sizeof(fpath)) >= sizeof(fpath) ||
                    309:                    strlcat(fpath, ext, sizeof(fpath)) >= sizeof(fpath)) {
                    310:                        xfree(suffixes);
                    311:                        return (NULL);
                    312:                }
                    313:
                    314:                /* Don't use `filename' as RCS file. */
                    315:                if (strcmp(fpath, filename) == 0)
                    316:                        continue;
                    317:
                    318:                if (stat(fpath, &sb) == 0) {
1.77      ray       319:                        ret = xstrdup(fpath);
1.74      ray       320:                        break;
                    321:                }
                    322:        }
                    323:
                    324:        /*
                    325:         * If `ret' is still NULL no RCS file with any extension exists
                    326:         * so we use the first extension.
                    327:         */
                    328:        if (ret == NULL) {
                    329:                /*
1.81      ray       330:                 * `suffixes' should now be NUL separated, so the first
                    331:                 * extension can be read just by reading `suffixes'.
1.74      ray       332:                 */
1.81      ray       333:                if (strlcat(rcspath, suffixes, sizeof(rcspath)) >= sizeof(rcspath)) {
1.74      ray       334:                        xfree(suffixes);
                    335:                        return (NULL);
                    336:                }
1.77      ray       337:                ret = xstrdup(rcspath);
1.74      ray       338:        }
1.28      joris     339:
1.81      ray       340:        xfree(suffixes);
1.28      joris     341:        return (ret);
                    342: }
                    343:
1.75      ray       344: /*
                    345:  * Find the name of an RCS file, given a file name `fname'.  If an RCS
                    346:  * file is found, the name is copied to the `len' sized buffer `out'.
                    347:  * Returns 0 if RCS file was found, -1 otherwise.
                    348:  */
1.28      joris     349: int
1.9       joris     350: rcs_statfile(char *fname, char *out, size_t len)
                    351: {
                    352:        struct stat st;
1.75      ray       353:        char *rcspath;
1.9       joris     354:
1.75      ray       355:        /* XXX - do this in rcs_choosefile? */
                    356:        if ((rcspath = rcs_choosefile(fname)) == NULL)
                    357:                fatal("rcs_statfile: path truncation");
1.9       joris     358:
1.80      ray       359:        /* Error out if file not found and we are not creating one. */
                    360:        if (stat(rcspath, &st) == -1 && !(rcsflags & RCS_CREATE)) {
1.44      niallo    361:                if ((strcmp(__progname, "rcsclean") != 0)
                    362:                    && (strcmp(__progname, "ci") != 0))
1.75      ray       363:                        cvs_log(LP_ERRNO, "%s", rcspath);
                    364:                xfree(rcspath);
1.9       joris     365:                return (-1);
                    366:        }
                    367:
1.75      ray       368:        if (strlcpy(out, rcspath, len) >= len)
1.57      xsa       369:                fatal("rcs_statfile: path truncation");
1.75      ray       370:
                    371:        xfree(rcspath);
1.9       joris     372:
                    373:        return (0);
                    374: }
1.1       deraadt   375:
                    376: int
                    377: main(int argc, char **argv)
                    378: {
                    379:        u_int i;
1.29      joris     380:        char *rcsinit, *cmd_argv[RCS_CMD_MAXARG];
                    381:        int ret, cmd_argc;
1.1       deraadt   382:
                    383:        ret = -1;
1.28      joris     384:        rcs_optind = 1;
1.9       joris     385:        cvs_log_init(LD_STD, 0);
1.68      joris     386:        SLIST_INIT(&rcs_temp_files);
1.1       deraadt   387:
1.29      joris     388:        cmd_argc = 0;
1.30      joris     389:        cmd_argv[cmd_argc++] = argv[0];
1.29      joris     390:        if ((rcsinit = getenv("RCSINIT")) != NULL) {
                    391:                ret = rcs_init(rcsinit, cmd_argv + 1,
                    392:                    RCS_CMD_MAXARG - 1);
                    393:                if (ret < 0) {
                    394:                        cvs_log(LP_ERRNO, "failed to prepend RCSINIT options");
                    395:                        exit (1);
                    396:                }
                    397:
                    398:                cmd_argc += ret;
                    399:        }
1.36      xsa       400:
                    401:        if ((rcs_tmpdir = getenv("TMPDIR")) == NULL)
                    402:                rcs_tmpdir = RCS_TMPDIR_DEFAULT;
1.29      joris     403:
                    404:        for (ret = 1; ret < argc; ret++)
                    405:                cmd_argv[cmd_argc++] = argv[ret];
1.68      joris     406:
                    407:        signal(SIGHUP, sighdlr);
                    408:        signal(SIGINT, sighdlr);
                    409:        signal(SIGQUIT, sighdlr);
                    410:        signal(SIGABRT, sighdlr);
                    411:        signal(SIGALRM, sighdlr);
                    412:        signal(SIGTERM, sighdlr);
1.29      joris     413:
1.1       deraadt   414:        for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++)
1.2       deraadt   415:                if (strcmp(__progname, programs[i].prog_name) == 0) {
                    416:                        usage = programs[i].prog_usage;
1.29      joris     417:                        ret = programs[i].prog_hdlr(cmd_argc, cmd_argv);
1.2       deraadt   418:                        break;
                    419:                }
1.1       deraadt   420:
1.23      niallo    421:        exit(ret);
1.76      ray       422:        /* NOTREACHED */
1.1       deraadt   423: }
                    424:
                    425:
                    426: void
                    427: rcs_usage(void)
                    428: {
                    429:        fprintf(stderr,
1.66      jmc       430:            "usage: rcs [-ehIiLMqTUV] [-Aoldfile] [-ausers] [-b[rev]]\n"
1.67      xsa       431:            "           [-cstring] [-e[users]] [-kmode] [-mrev:msg]\n"
1.66      jmc       432:            "           [-orev] [-sstate[:rev]] [-tfile|str]\n"
1.67      xsa       433:            "           [-xsuffixes] file ...\n");
1.1       deraadt   434: }
                    435:
                    436: /*
                    437:  * rcs_main()
                    438:  *
                    439:  * Handler for the `rcs' program.
                    440:  * Returns 0 on success, or >0 on error.
                    441:  */
                    442: int
                    443: rcs_main(int argc, char **argv)
                    444: {
1.79      xsa       445:        int i, j, ch, flags, kflag, lkmode;
1.39      xsa       446:        char fpath[MAXPATHLEN], ofpath[MAXPATHLEN];
1.56      joris     447:        char *logstr, *logmsg, *nflag, *descfile;
1.79      xsa       448:        char *alist, *comment, *elist;
1.1       deraadt   449:        mode_t fmode;
1.39      xsa       450:        RCSFILE *file, *oldfile;
1.24      joris     451:        RCSNUM *logrev;
1.39      xsa       452:        struct rcs_access *acp;
1.48      xsa       453:        time_t rcs_mtime = -1;
1.1       deraadt   454:
                    455:        kflag = lkmode = -1;
                    456:        fmode = 0;
1.58      niallo    457:        flags = RCS_RDWR|RCS_PARSE_FULLY;
1.56      joris     458:        descfile = nflag = NULL;
1.39      xsa       459:        logstr = alist = comment = elist = NULL;
1.1       deraadt   460:
1.51      xsa       461:        while ((ch = rcs_getopt(argc, argv, RCSPROG_OPTSTRING)) != -1) {
1.1       deraadt   462:                switch (ch) {
                    463:                case 'A':
1.39      xsa       464:                        if (rcs_statfile(rcs_optarg, ofpath, sizeof(ofpath)) < 0)
                    465:                                exit(1);
1.56      joris     466:                        rcsflags |= CO_ACLAPPEND;
1.1       deraadt   467:                        break;
                    468:                case 'a':
1.28      joris     469:                        alist = rcs_optarg;
1.1       deraadt   470:                        break;
                    471:                case 'c':
1.28      joris     472:                        comment = rcs_optarg;
1.1       deraadt   473:                        break;
                    474:                case 'e':
1.28      joris     475:                        elist = rcs_optarg;
1.82    ! xsa       476:                        rcsflags |= RCS_EFLAG;
1.1       deraadt   477:                        break;
                    478:                case 'h':
1.2       deraadt   479:                        (usage)();
1.1       deraadt   480:                        exit(0);
1.76      ray       481:                        /* NOTREACHED */
1.1       deraadt   482:                case 'i':
                    483:                        flags |= RCS_CREATE;
1.56      joris     484:                        rcsflags |= RCS_CREATE;
1.1       deraadt   485:                        break;
                    486:                case 'k':
1.28      joris     487:                        kflag = rcs_kflag_get(rcs_optarg);
1.1       deraadt   488:                        if (RCS_KWEXP_INVAL(kflag)) {
                    489:                                cvs_log(LP_ERR,
                    490:                                    "invalid keyword substitution mode `%s'",
1.28      joris     491:                                    rcs_optarg);
1.1       deraadt   492:                                exit(1);
                    493:                        }
                    494:                        break;
                    495:                case 'L':
                    496:                        if (lkmode == RCS_LOCK_LOOSE)
                    497:                                cvs_log(LP_WARN, "-U overriden by -L");
                    498:                        lkmode = RCS_LOCK_STRICT;
                    499:                        break;
1.24      joris     500:                case 'm':
1.53      joris     501:                        logstr = xstrdup(rcs_optarg);
1.24      joris     502:                        break;
1.1       deraadt   503:                case 'M':
                    504:                        /* ignore for the moment */
                    505:                        break;
1.56      joris     506:                case 'n':
                    507:                        nflag = xstrdup(rcs_optarg);
                    508:                        break;
                    509:                case 'N':
                    510:                        nflag = xstrdup(rcs_optarg);
                    511:                        rcsflags |= RCS_NFLAG;
                    512:                        break;
1.12      joris     513:                case 'q':
                    514:                        verbose = 0;
1.46      xsa       515:                        break;
1.56      joris     516:                case 't':
                    517:                        descfile = rcs_optarg;
                    518:                        rcsflags |= RCS_TFLAG;
                    519:                        break;
1.46      xsa       520:                case 'T':
1.56      joris     521:                        rcsflags |= PRESERVETIME;
1.12      joris     522:                        break;
1.1       deraadt   523:                case 'U':
                    524:                        if (lkmode == RCS_LOCK_STRICT)
                    525:                                cvs_log(LP_WARN, "-L overriden by -U");
                    526:                        lkmode = RCS_LOCK_LOOSE;
                    527:                        break;
                    528:                case 'V':
                    529:                        printf("%s\n", rcs_version);
                    530:                        exit(0);
1.76      ray       531:                        /* NOTREACHED */
1.45      xsa       532:                case 'x':
                    533:                        rcs_suffixes = rcs_optarg;
1.51      xsa       534:                        break;
                    535:                case 'z':
                    536:                        /*
                    537:                         * kept for compatibility
                    538:                         */
1.45      xsa       539:                        break;
1.1       deraadt   540:                default:
1.2       deraadt   541:                        (usage)();
1.1       deraadt   542:                        exit(1);
                    543:                }
                    544:        }
                    545:
1.28      joris     546:        argc -= rcs_optind;
                    547:        argv += rcs_optind;
1.30      joris     548:
1.1       deraadt   549:        if (argc == 0) {
                    550:                cvs_log(LP_ERR, "no input file");
1.5       joris     551:                (usage)();
1.1       deraadt   552:                exit(1);
                    553:        }
                    554:
                    555:        for (i = 0; i < argc; i++) {
1.9       joris     556:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
1.8       joris     557:                        continue;
1.6       joris     558:
1.35      niallo    559:                if (verbose == 1)
                    560:                        printf("RCS file: %s\n", fpath);
1.48      xsa       561:
1.49      niallo    562:                if ((file = rcs_open(fpath, flags, fmode)) == NULL)
1.6       joris     563:                        continue;
1.1       deraadt   564:
1.56      joris     565:                if (rcsflags & RCS_CREATE)
                    566:                        rcs_set_description(file, NULL);
                    567:
                    568:                if (rcsflags & RCS_TFLAG)
                    569:                        rcs_set_description(file, descfile);
                    570:
                    571:                if (rcsflags & PRESERVETIME)
1.48      xsa       572:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    573:
1.56      joris     574:                if (nflag != NULL)
                    575:                        rcs_attach_symbol(file, nflag);
                    576:
1.24      joris     577:                if (logstr != NULL) {
                    578:                        if ((logmsg = strchr(logstr, ':')) == NULL) {
                    579:                                cvs_log(LP_ERR, "missing log message");
                    580:                                rcs_close(file);
                    581:                                continue;
                    582:                        }
                    583:
                    584:                        *logmsg++ = '\0';
                    585:                        if ((logrev = rcsnum_parse(logstr)) == NULL) {
1.48      xsa       586:                                cvs_log(LP_ERR,
                    587:                                    "'%s' bad revision number", logstr);
1.24      joris     588:                                rcs_close(file);
                    589:                                continue;
                    590:                        }
                    591:
                    592:                        if (rcs_rev_setlog(file, logrev, logmsg) < 0) {
                    593:                                cvs_log(LP_ERR,
                    594:                                    "failed to set logmsg for '%s' to '%s'",
                    595:                                    logstr, logmsg);
                    596:                                rcs_close(file);
1.25      joris     597:                                rcsnum_free(logrev);
1.24      joris     598:                                continue;
                    599:                        }
                    600:
                    601:                        rcsnum_free(logrev);
1.39      xsa       602:                }
                    603:
                    604:                /* entries to add from <oldfile> */
1.56      joris     605:                if (rcsflags & CO_ACLAPPEND) {
1.39      xsa       606:                        /* XXX */
                    607:                        if ((oldfile = rcs_open(ofpath, RCS_READ)) == NULL)
                    608:                                exit(1);
                    609:
                    610:                        TAILQ_FOREACH(acp, &(oldfile->rf_access), ra_list)
                    611:                                rcs_access_add(file, acp->ra_name);
                    612:
                    613:                        rcs_close(oldfile);
1.24      joris     614:                }
                    615:
1.1       deraadt   616:                /* entries to add to the access list */
                    617:                if (alist != NULL) {
1.79      xsa       618:                        char **aargv;
1.1       deraadt   619:
1.79      xsa       620:                        aargv = cvs_strsplit(alist, ",");
                    621:                        for (j = 0; aargv[j] != NULL; j++)
                    622:                                rcs_access_add(file, aargv[j]);
1.1       deraadt   623:
1.79      xsa       624:                        xfree(aargv);
1.1       deraadt   625:                }
                    626:
                    627:                if (comment != NULL)
                    628:                        rcs_comment_set(file, comment);
1.82    ! xsa       629:
        !           630:                if (elist != NULL) {
        !           631:                        char **eargv;
        !           632:
        !           633:                        eargv = cvs_strsplit(elist, ",");
        !           634:                        for (j = 0; eargv[j] != NULL; j++)
        !           635:                                rcs_access_remove(file, eargv[j]);
        !           636:
        !           637:                        xfree(eargv);
        !           638:                } else if (rcsflags & RCS_EFLAG) {
        !           639:                        struct rcs_access *rap;
        !           640:
        !           641:                        /* XXX rcs_access_remove(file, NULL); ?? */
        !           642:                        while (!TAILQ_EMPTY(&(file->rf_access))) {
        !           643:                                rap = TAILQ_FIRST(&(file->rf_access));
        !           644:                                TAILQ_REMOVE(&(file->rf_access), rap, ra_list);
        !           645:                                xfree(rap->ra_name);
        !           646:                                xfree(rap);
        !           647:                        }
        !           648:                        /* not synced anymore */
        !           649:                        file->rf_flags &= ~RCS_SYNCED;
        !           650:                }
1.1       deraadt   651:
                    652:                if (kflag != -1)
                    653:                        rcs_kwexp_set(file, kflag);
                    654:
                    655:                if (lkmode != -1)
                    656:                        rcs_lock_setmode(file, lkmode);
                    657:
                    658:                rcs_close(file);
1.48      xsa       659:
1.56      joris     660:                if (rcsflags & PRESERVETIME)
1.48      xsa       661:                        rcs_set_mtime(fpath, rcs_mtime);
1.9       joris     662:
1.14      xsa       663:                if (verbose == 1)
1.12      joris     664:                        printf("done\n");
1.1       deraadt   665:        }
1.24      joris     666:
                    667:        if (logstr != NULL)
1.53      joris     668:                xfree(logstr);
1.1       deraadt   669:
1.56      joris     670:        if (nflag != NULL)
                    671:                xfree(nflag);
                    672:
1.1       deraadt   673:        return (0);
1.56      joris     674: }
                    675:
                    676: static void
                    677: rcs_attach_symbol(RCSFILE *file, const char *symname)
                    678: {
                    679:        char *rnum;
                    680:        RCSNUM *rev;
                    681:        char rbuf[16];
                    682:        int rm;
                    683:
                    684:        rm = 0;
                    685:        rev = NULL;
                    686:        if ((rnum = strrchr(symname, ':')) != NULL) {
                    687:                if (rnum[1] == '\0')
                    688:                        rev = file->rf_head;
                    689:                *(rnum++) = '\0';
                    690:        } else {
                    691:                rm = 1;
                    692:        }
                    693:
                    694:        if (rev == NULL && rm != 1) {
                    695:                if ((rev = rcsnum_parse(rnum)) == NULL)
                    696:                        fatal("bad revision %s", rnum);
                    697:        }
                    698:
                    699:        if (rcsflags & RCS_NFLAG)
                    700:                rm = 1;
                    701:
                    702:        if (rm == 1) {
                    703:                if (rcs_sym_remove(file, symname) < 0) {
                    704:                        if ((rcs_errno == RCS_ERR_NOENT) &&
                    705:                            !(rcsflags & RCS_NFLAG))
                    706:                                cvs_log(LP_WARN,
                    707:                                    "can't delete nonexisting symbol %s", symname);
                    708:                } else {
                    709:                        if (rcsflags & RCS_NFLAG)
                    710:                                rm = 0;
                    711:                }
                    712:        }
                    713:
                    714:        if (rm == 0) {
                    715:                if ((rcs_sym_add(file, symname, rev) < 0) &&
                    716:                    (rcs_errno == RCS_ERR_DUPENT)) {
                    717:                        rcsnum_tostr(rcs_sym_getrev(file, symname),
                    718:                            rbuf, sizeof(rbuf));
                    719:                        fatal("symbolic name %s already bound to %s",
                    720:                            symname, rbuf);
                    721:                }
                    722:        }
                    723: }
                    724:
                    725: static void
                    726: rcs_set_description(RCSFILE *file, const char *in)
                    727: {
                    728:        BUF *bp;
                    729:        char *content, buf[128];
                    730:
                    731:        content = NULL;
                    732:        if (in != NULL) {
                    733:                bp = cvs_buf_load(in, BUF_AUTOEXT);
                    734:        } else {
                    735:                bp = cvs_buf_alloc(64, BUF_AUTOEXT);
                    736:
                    737:                printf(DESC_PROMPT);
                    738:                for (;;) {
                    739:                        fgets(buf, sizeof(buf), stdin);
                    740:                        if (feof(stdin) || ferror(stdin) || buf[0] == '.')
                    741:                                break;
                    742:                        cvs_buf_append(bp, buf, strlen(buf));
                    743:                        printf(">> ");
                    744:                }
                    745:        }
                    746:
                    747:        cvs_buf_putc(bp, '\0');
                    748:        content = cvs_buf_release(bp);
                    749:
                    750:        rcs_desc_set(file, content);
1.70      joris     751:        xfree(content);
1.1       deraadt   752: }