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

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