[BACK]Return to ci.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/ci.c, Revision 1.205

1.205   ! ray         1: /*     $OpenBSD: ci.c,v 1.204 2008/03/22 21:15:54 millert Exp $        */
1.1       niallo      2: /*
1.98      niallo      3:  * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org>
1.1       niallo      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
1.143     niallo      7:  * modification, are permitted provided that the following conditions
1.1       niallo      8:  * are met:
                      9:  *
1.143     niallo     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       niallo     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.195     xsa        27: #include <sys/stat.h>
                     28:
                     29: #include <ctype.h>
                     30: #include <err.h>
                     31: #include <fcntl.h>
                     32: #include <stdio.h>
                     33: #include <stdlib.h>
                     34: #include <string.h>
                     35: #include <unistd.h>
1.1       niallo     36:
1.92      xsa        37: #include "rcsprog.h"
1.4       niallo     38: #include "diff.h"
1.9       niallo     39:
1.157     ray        40: #define CI_OPTSTRING   "d::f::I::i::j::k::l::M::m::N:n:qr::s:Tt::u::Vw:x::z::"
1.107     deraadt    41: #define DATE_NOW       -1
                     42: #define DATE_MTIME     -2
                     43:
                     44: #define KW_ID          "Id"
                     45: #define KW_AUTHOR      "Author"
                     46: #define KW_DATE                "Date"
                     47: #define KW_STATE       "State"
                     48: #define KW_REVISION    "Revision"
                     49:
                     50: #define KW_TYPE_ID             1
                     51: #define KW_TYPE_AUTHOR         2
                     52: #define KW_TYPE_DATE           3
                     53: #define KW_TYPE_STATE          4
                     54: #define KW_TYPE_REVISION       5
                     55:
                     56: #define KW_NUMTOKS_ID          10
                     57: #define KW_NUMTOKS_AUTHOR      3
                     58: #define KW_NUMTOKS_DATE                4
                     59: #define KW_NUMTOKS_STATE       3
                     60: #define KW_NUMTOKS_REVISION    3
1.58      niallo     61:
1.183     ray        62: /* Maximum number of tokens in a keyword. */
                     63: #define KW_NUMTOKS_MAX         10
                     64:
1.115     niallo     65: #define RCSNUM_ZERO_ENDING(x) (x->rn_id[x->rn_len - 1] == 0)
                     66:
1.98      niallo     67: extern struct rcs_kw rcs_expkw[];
                     68:
1.158     joris      69: static int workfile_fd;
                     70:
1.58      niallo     71: struct checkin_params {
                     72:        int flags, openflags;
                     73:        mode_t fmode;
                     74:        time_t date;
                     75:        RCSFILE *file;
                     76:        RCSNUM *frev, *newrev;
1.177     ray        77:        const char *description, *symbol;
1.180     niallo     78:        char fpath[MAXPATHLEN], *rcs_msg, *username, *filename;
1.177     ray        79:        char *author, *state;
1.180     niallo     80:        BUF *deltatext;
1.58      niallo     81: };
                     82:
1.94      niallo     83: static int      checkin_attach_symbol(struct checkin_params *);
                     84: static int      checkin_checklock(struct checkin_params *);
1.180     niallo     85: static BUF     *checkin_diff_file(struct checkin_params *);
1.154     xsa        86: static char    *checkin_getlogmsg(RCSNUM *, RCSNUM *, int);
1.68      xsa        87: static int      checkin_init(struct checkin_params *);
1.183     ray        88: static int      checkin_keywordscan(BUF *, RCSNUM **, time_t *, char **,
1.98      niallo     89:     char **);
1.107     deraadt    90: static int      checkin_keywordtype(char *);
1.158     joris      91: static void     checkin_mtimedate(struct checkin_params *);
1.107     deraadt    92: static void     checkin_parsekeyword(char *, RCSNUM **, time_t *, char **,
1.98      niallo     93:     char **);
1.94      niallo     94: static int      checkin_update(struct checkin_params *);
1.200     niallo     95: static int      checkin_revert(struct checkin_params *);
1.4       niallo     96:
1.1       niallo     97: void
                     98: checkin_usage(void)
                     99: {
                    100:        fprintf(stderr,
1.193     niallo    101:            "usage: ci [-qV] [-d[date]] [-f[rev]] [-I[rev]] [-i[rev]]\n"
1.198     sobrado   102:            "          [-j[rev]] [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg]\n"
                    103:            "          [-Nsymbol] [-nsymbol] [-r[rev]] [-sstate] [-tstr]\n"
                    104:            "          [-u[rev]] [-wusername] [-xsuffixes] [-ztz] file ...\n");
1.58      niallo    105: }
                    106:
1.1       niallo    107: /*
                    108:  * checkin_main()
                    109:  *
                    110:  * Handler for the `ci' program.
                    111:  * Returns 0 on success, or >0 on error.
                    112:  */
                    113: int
                    114: checkin_main(int argc, char **argv)
                    115: {
1.163     joris     116:        int fd;
1.58      niallo    117:        int i, ch, status;
1.197     bluhm     118:        int base_flags, base_openflags;
1.139     ray       119:        char *rev_str;
1.58      niallo    120:        struct checkin_params pb;
1.1       niallo    121:
1.58      niallo    122:        pb.date = DATE_NOW;
                    123:        pb.file = NULL;
1.98      niallo    124:        pb.rcs_msg = pb.username = pb.author = pb.state = NULL;
1.177     ray       125:        pb.description = pb.symbol = NULL;
                    126:        pb.deltatext = NULL;
1.58      niallo    127:        pb.newrev =  NULL;
1.128     niallo    128:        pb.fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.197     bluhm     129:        status = 0;
                    130:        base_flags = INTERACTIVE;
                    131:        base_openflags = RCS_RDWR|RCS_CREATE|RCS_PARSE_FULLY;
1.139     ray       132:        rev_str = NULL;
1.9       niallo    133:
1.58      niallo    134:        while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1       niallo    135:                switch (ch) {
1.20      niallo    136:                case 'd':
1.25      niallo    137:                        if (rcs_optarg == NULL)
1.58      niallo    138:                                pb.date = DATE_MTIME;
1.162     joris     139:                        else if ((pb.date = rcs_date_parse(rcs_optarg)) <= 0)
1.160     xsa       140:                                errx(1, "invalid date");
1.20      niallo    141:                        break;
1.29      niallo    142:                case 'f':
1.139     ray       143:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     144:                        base_flags |= FORCE;
1.29      niallo    145:                        break;
1.119     xsa       146:                case 'I':
1.139     ray       147:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     148:                        base_flags |= INTERACTIVE;
1.119     xsa       149:                        break;
1.58      niallo    150:                case 'i':
1.139     ray       151:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     152:                        base_openflags |= RCS_CREATE;
                    153:                        base_flags |= CI_INIT;
1.58      niallo    154:                        break;
                    155:                case 'j':
1.139     ray       156:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     157:                        base_openflags &= ~RCS_CREATE;
                    158:                        base_flags &= ~CI_INIT;
1.58      niallo    159:                        break;
1.98      niallo    160:                case 'k':
1.139     ray       161:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     162:                        base_flags |= CI_KEYWORDSCAN;
1.98      niallo    163:                        break;
1.30      niallo    164:                case 'l':
1.139     ray       165:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     166:                        base_flags |= CO_LOCK;
1.54      niallo    167:                        break;
                    168:                case 'M':
1.139     ray       169:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     170:                        base_flags |= CO_REVDATE;
1.30      niallo    171:                        break;
1.1       niallo    172:                case 'm':
1.58      niallo    173:                        pb.rcs_msg = rcs_optarg;
1.87      xsa       174:                        if (pb.rcs_msg == NULL)
1.160     xsa       175:                                errx(1, "missing message for -m option");
1.197     bluhm     176:                        base_flags &= ~INTERACTIVE;
1.3       joris     177:                        break;
1.42      niallo    178:                case 'N':
1.197     bluhm     179:                        base_flags |= CI_SYMFORCE;
1.152     ray       180:                        /* FALLTHROUGH */
1.38      niallo    181:                case 'n':
1.177     ray       182:                        pb.symbol = rcs_optarg;
1.87      xsa       183:                        if (rcs_sym_check(pb.symbol) != 1)
1.160     xsa       184:                                errx(1, "invalid symbol `%s'", pb.symbol);
1.38      niallo    185:                        break;
1.3       joris     186:                case 'q':
1.197     bluhm     187:                        base_flags |= QUIET;
1.1       niallo    188:                        break;
1.30      niallo    189:                case 'r':
1.139     ray       190:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     191:                        base_flags |= CI_DEFAULT;
1.9       niallo    192:                        break;
1.51      niallo    193:                case 's':
1.58      niallo    194:                        pb.state = rcs_optarg;
1.87      xsa       195:                        if (rcs_state_check(pb.state) < 0)
1.160     xsa       196:                                errx(1, "invalid state `%s'", pb.state);
1.67      xsa       197:                        break;
                    198:                case 'T':
1.197     bluhm     199:                        base_flags |= PRESERVETIME;
1.51      niallo    200:                        break;
1.80      niallo    201:                case 't':
1.157     ray       202:                        /* Ignore bare -t; kept for backwards compatibility. */
                    203:                        if (rcs_optarg == NULL)
                    204:                                break;
                    205:                        pb.description = rcs_optarg;
1.197     bluhm     206:                        base_flags |= DESCRIPTION;
1.80      niallo    207:                        break;
1.9       niallo    208:                case 'u':
1.139     ray       209:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.197     bluhm     210:                        base_flags |= CO_UNLOCK;
1.9       niallo    211:                        break;
1.30      niallo    212:                case 'V':
                    213:                        printf("%s\n", rcs_version);
                    214:                        exit(0);
1.31      niallo    215:                case 'w':
1.151     ray       216:                        if (pb.author != NULL)
                    217:                                xfree(pb.author);
1.84      joris     218:                        pb.author = xstrdup(rcs_optarg);
1.64      xsa       219:                        break;
                    220:                case 'x':
1.125     ray       221:                        /* Use blank extension if none given. */
                    222:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.110     joris     223:                        break;
                    224:                case 'z':
                    225:                        timezone_flag = rcs_optarg;
1.31      niallo    226:                        break;
1.1       niallo    227:                default:
                    228:                        (usage)();
                    229:                        exit(1);
                    230:                }
                    231:        }
                    232:
1.24      joris     233:        argc -= rcs_optind;
                    234:        argv += rcs_optind;
                    235:
1.1       niallo    236:        if (argc == 0) {
1.155     xsa       237:                warnx("no input file");
1.1       niallo    238:                (usage)();
                    239:                exit(1);
                    240:        }
                    241:
1.86      xsa       242:        if ((pb.username = getlogin()) == NULL)
1.160     xsa       243:                err(1, "getlogin");
1.31      niallo    244:
1.194     otto      245:        /* If -x flag was not given, use default. */
                    246:        if (rcs_suffixes == NULL)
                    247:                rcs_suffixes = RCS_DEFAULT_SUFFIX;
                    248:
1.1       niallo    249:        for (i = 0; i < argc; i++) {
1.197     bluhm     250:                /*
                    251:                 * The pb.flags and pb.openflags may change during
                    252:                 * loop iteration so restore them for each file.
                    253:                 */
                    254:                pb.flags = base_flags;
                    255:                pb.openflags = base_openflags;
                    256:
1.58      niallo    257:                pb.filename = argv[i];
1.194     otto      258:                rcs_strip_suffix(pb.filename);
1.1       niallo    259:
1.158     joris     260:                if ((workfile_fd = open(pb.filename, O_RDONLY)) == -1)
1.160     xsa       261:                        err(1, "%s", pb.filename);
1.158     joris     262:
1.175     ray       263:                /* Find RCS file path. */
                    264:                fd = rcs_choosefile(pb.filename, pb.fpath, sizeof(pb.fpath));
1.163     joris     265:
                    266:                if (fd < 0) {
1.71      niallo    267:                        if (pb.openflags & RCS_CREATE)
                    268:                                pb.flags |= NEWFILE;
                    269:                        else {
1.188     ray       270:                                /* XXX - Check if errno == ENOENT. */
1.155     xsa       271:                                warnx("No existing RCS file");
1.71      niallo    272:                                status = 1;
1.158     joris     273:                                (void)close(workfile_fd);
1.71      niallo    274:                                continue;
                    275:                        }
1.66      niallo    276:                } else {
                    277:                        if (pb.flags & CI_INIT) {
1.155     xsa       278:                                warnx("%s already exists", pb.fpath);
1.66      niallo    279:                                status = 1;
1.163     joris     280:                                (void)close(fd);
1.158     joris     281:                                (void)close(workfile_fd);
1.66      niallo    282:                                continue;
                    283:                        }
1.58      niallo    284:                        pb.openflags &= ~RCS_CREATE;
1.66      niallo    285:                }
1.63      niallo    286:
1.163     joris     287:                pb.file = rcs_open(pb.fpath, fd, pb.openflags, pb.fmode);
1.87      xsa       288:                if (pb.file == NULL)
1.161     xsa       289:                        errx(1, "failed to open rcsfile `%s'", pb.fpath);
1.29      niallo    290:
1.185     ray       291:                if ((pb.flags & DESCRIPTION) &&
                    292:                    rcs_set_description(pb.file, pb.description) == -1)
1.186     joris     293:                        err(1, "%s", pb.filename);
1.157     ray       294:
1.154     xsa       295:                if (!(pb.flags & QUIET))
1.173     xsa       296:                        (void)fprintf(stderr,
                    297:                            "%s  <--  %s\n", pb.fpath, pb.filename);
1.139     ray       298:
                    299:                /* XXX - Should we rcsnum_free(pb.newrev)? */
                    300:                if (rev_str != NULL)
1.141     ray       301:                        if ((pb.newrev = rcs_getrevnum(rev_str, pb.file)) ==
                    302:                            NULL)
1.160     xsa       303:                                errx(1, "invalid revision: %s", rev_str);
1.107     deraadt   304:
1.148     niallo    305:                if (!(pb.flags & NEWFILE))
                    306:                        pb.flags |= CI_SKIPDESC;
1.158     joris     307:
1.179     david     308:                /* XXX - support for committing to a file without revisions */
1.142     joris     309:                if (pb.file->rf_ndelta == 0) {
                    310:                        pb.flags |= NEWFILE;
                    311:                        pb.file->rf_flags |= RCS_CREATE;
                    312:                }
                    313:
1.158     joris     314:                /*
                    315:                 * workfile_fd will be closed in checkin_init or
                    316:                 * checkin_update
                    317:                 */
1.149     ray       318:                if (pb.flags & NEWFILE) {
                    319:                        if (checkin_init(&pb) == -1)
                    320:                                status = 1;
                    321:                } else {
                    322:                        if (checkin_update(&pb) == -1)
                    323:                                status = 1;
                    324:                }
1.114     niallo    325:
1.129     niallo    326:                rcs_close(pb.file);
1.178     niallo    327:                pb.newrev = NULL;
1.4       niallo    328:        }
                    329:
1.197     bluhm     330:        if (!(base_flags & QUIET) && status == 0)
1.173     xsa       331:                (void)fprintf(stderr, "done\n");
1.93      xsa       332:
1.16      niallo    333:        return (status);
1.4       niallo    334: }
                    335:
1.59      niallo    336: /*
                    337:  * checkin_diff_file()
                    338:  *
1.65      xsa       339:  * Generate the diff between the working file and a revision.
1.190     niallo    340:  * Returns pointer to a BUF on success, NULL on failure.
1.59      niallo    341:  */
1.180     niallo    342: static BUF *
1.58      niallo    343: checkin_diff_file(struct checkin_params *pb)
1.4       niallo    344: {
1.168     xsa       345:        char *path1, *path2;
1.4       niallo    346:        BUF *b1, *b2, *b3;
1.201     xsa       347:        char rbuf[RCS_REV_BUFSZ];
1.4       niallo    348:
1.112     joris     349:        b1 = b2 = b3 = NULL;
1.204     millert   350:        path1 = path2 = NULL;
1.58      niallo    351:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4       niallo    352:
1.162     joris     353:        if ((b1 = rcs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
1.155     xsa       354:                warnx("failed to load file: `%s'", pb->filename);
1.112     joris     355:                goto out;
1.1       niallo    356:        }
                    357:
1.58      niallo    358:        if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.155     xsa       359:                warnx("failed to load revision");
1.112     joris     360:                goto out;
1.4       niallo    361:        }
                    362:
1.172     ray       363:        if ((b3 = rcs_buf_alloc(128, BUF_AUTOEXT)) == NULL) {
1.155     xsa       364:                warnx("failed to allocated buffer for diff");
1.112     joris     365:                goto out;
1.4       niallo    366:        }
                    367:
1.168     xsa       368:        (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
1.181     ray       369:        rcs_buf_write_stmp(b1, path1);
1.112     joris     370:
1.162     joris     371:        rcs_buf_free(b1);
1.112     joris     372:        b1 = NULL;
1.4       niallo    373:
1.168     xsa       374:        (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
1.181     ray       375:        rcs_buf_write_stmp(b2, path2);
1.112     joris     376:
1.162     joris     377:        rcs_buf_free(b2);
1.112     joris     378:        b2 = NULL;
1.4       niallo    379:
1.5       niallo    380:        diff_format = D_RCSDIFF;
1.205   ! ray       381:        if (diffreg(path1, path2, b3, D_FORCEASCII) == D_ERROR)
1.170     xsa       382:                goto out;
1.4       niallo    383:
1.180     niallo    384:        return (b3);
1.112     joris     385: out:
                    386:        if (b1 != NULL)
1.162     joris     387:                rcs_buf_free(b1);
1.112     joris     388:        if (b2 != NULL)
1.162     joris     389:                rcs_buf_free(b2);
1.112     joris     390:        if (b3 != NULL)
1.162     joris     391:                rcs_buf_free(b3);
1.168     xsa       392:        if (path1 != NULL)
                    393:                xfree(path1);
                    394:        if (path2 != NULL)
                    395:                xfree(path2);
1.4       niallo    396:
1.180     niallo    397:        return (NULL);
1.6       niallo    398: }
                    399:
                    400: /*
1.65      xsa       401:  * checkin_getlogmsg()
1.59      niallo    402:  *
1.6       niallo    403:  * Get log message from user interactively.
1.59      niallo    404:  * Returns pointer to a char array on success, NULL on failure.
1.6       niallo    405:  */
                    406: static char *
1.154     xsa       407: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2, int flags)
1.6       niallo    408: {
1.201     xsa       409:        char   *rcs_msg, nrev[RCS_REV_BUFSZ], prev[RCS_REV_BUFSZ];
1.153     ray       410:        const char *prompt =
                    411:            "enter log message, terminated with a single '.' or end of file:\n";
1.6       niallo    412:        RCSNUM *tmprev;
                    413:
1.7       niallo    414:        rcs_msg = NULL;
1.6       niallo    415:        tmprev = rcsnum_alloc();
                    416:        rcsnum_cpy(rev, tmprev, 16);
1.9       niallo    417:        rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12      niallo    418:        if (rev2 == NULL)
                    419:                rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
                    420:        else
                    421:                rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6       niallo    422:        rcsnum_free(tmprev);
                    423:
1.154     xsa       424:        if (!(flags & QUIET))
1.174     xsa       425:                (void)fprintf(stderr, "new revision: %s; "
                    426:                    "previous revision: %s\n", nrev, prev);
1.32      joris     427:
1.153     ray       428:        rcs_msg = rcs_prompt(prompt);
                    429:
1.58      niallo    430:        return (rcs_msg);
                    431: }
                    432:
                    433: /*
1.63      niallo    434:  * checkin_update()
                    435:  *
                    436:  * Do a checkin to an existing RCS file.
                    437:  *
                    438:  * On success, return 0. On error return -1.
                    439:  */
                    440: static int
                    441: checkin_update(struct checkin_params *pb)
                    442: {
1.201     xsa       443:        char numb1[RCS_REV_BUFSZ], numb2[RCS_REV_BUFSZ];
1.128     niallo    444:        struct stat st;
1.63      niallo    445:        BUF *bp;
1.149     ray       446:
1.82      joris     447:        /*
                    448:         * XXX this is wrong, we need to get the revision the user
1.85      xsa       449:         * has the lock for. So we can decide if we want to create a
1.82      joris     450:         * branch or not. (if it's not current HEAD we need to branch).
                    451:         */
1.63      niallo    452:        pb->frev = pb->file->rf_head;
                    453:
1.126     niallo    454:        /* Load file contents */
1.162     joris     455:        if ((bp = rcs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL)
1.200     niallo    456:                return (-1);
1.126     niallo    457:
1.115     niallo    458:        /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
1.147     deraadt   459:        if (pb->newrev != NULL && RCSNUM_ZERO_ENDING(pb->newrev))
1.115     niallo    460:                pb->newrev = rcsnum_inc(pb->newrev);
                    461:
1.82      joris     462:        if (checkin_checklock(pb) < 0)
1.200     niallo    463:                return (-1);
1.82      joris     464:
                    465:        /* If revision passed on command line is less than HEAD, bail.
                    466:         * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
                    467:         * there is no lock set for the user.
                    468:         */
1.147     deraadt   469:        if (pb->newrev != NULL &&
1.203     niallo    470:            rcsnum_cmp(pb->newrev, pb->frev, 0) != -1) {
1.155     xsa       471:                warnx("%s: revision %s too low; must be higher than %s",
1.77      xsa       472:                    pb->file->rf_path,
                    473:                    rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
                    474:                    rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.200     niallo    475:                return (-1);
1.63      niallo    476:        }
                    477:
1.104     niallo    478:        /*
                    479:         * Set the date of the revision to be the last modification
                    480:         * time of the working file if -d has no argument.
                    481:         */
1.158     joris     482:        if (pb->date == DATE_MTIME)
                    483:                checkin_mtimedate(pb);
1.104     niallo    484:
                    485:        /* Date from argv/mtime must be more recent than HEAD */
                    486:        if (pb->date != DATE_NOW) {
                    487:                time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
                    488:                if (pb->date <= head_date) {
1.113     xsa       489:                        char dbuf1[256], dbuf2[256], *fmt;
                    490:                        struct tm *t, *t_head;
                    491:
                    492:                        fmt = "%Y/%m/%d %H:%M:%S";
                    493:
1.164     ray       494:                        t = gmtime(&pb->date);
1.113     xsa       495:                        strftime(dbuf1, sizeof(dbuf1), fmt, t);
1.164     ray       496:                        t_head = gmtime(&head_date);
1.113     xsa       497:                        strftime(dbuf2, sizeof(dbuf2), fmt, t_head);
                    498:
1.191     krw       499:                        errx(1, "%s: Date %s precedes %s in revision %s.",
1.113     xsa       500:                            pb->file->rf_path, dbuf1, dbuf2,
1.104     niallo    501:                            rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
                    502:                }
                    503:        }
                    504:
1.73      xsa       505:        /* Get RCS patch */
1.63      niallo    506:        if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
1.155     xsa       507:                warnx("failed to get diff");
1.200     niallo    508:                return (-1);
1.63      niallo    509:        }
                    510:
                    511:        /*
                    512:         * If -f is not specified and there are no differences, tell
                    513:         * the user and revert to latest version.
                    514:         */
1.180     niallo    515:        if (!(pb->flags & FORCE) && (rcs_buf_len(pb->deltatext) < 1)) {
1.200     niallo    516:                if (checkin_revert(pb) == -1)
                    517:                        return (-1);
                    518:                else
                    519:                        return (0);
1.63      niallo    520:        }
                    521:
1.73      xsa       522:        /* If no log message specified, get it interactively. */
1.189     millert   523:        if (pb->flags & INTERACTIVE) {
                    524:                if (pb->rcs_msg != NULL) {
                    525:                        fprintf(stderr,
                    526:                            "reuse log message of previous file? [yn](y): ");
                    527:                        if (rcs_yesno('y') != 'y') {
                    528:                                xfree(pb->rcs_msg);
                    529:                                pb->rcs_msg = NULL;
                    530:                        }
                    531:                }
                    532:                if (pb->rcs_msg == NULL)
                    533:                        pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
                    534:                            pb->flags);
                    535:        }
1.63      niallo    536:
1.199     xsa       537:        if ((rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) &&
                    538:            (rcs_lock_getmode(pb->file) != RCS_LOCK_LOOSE)) {
1.63      niallo    539:                if (rcs_errno != RCS_ERR_NOENT)
1.155     xsa       540:                        warnx("failed to remove lock");
1.82      joris     541:                else if (!(pb->flags & CO_LOCK))
1.155     xsa       542:                        warnx("previous revision was not locked; "
1.82      joris     543:                            "ignoring -l option");
1.63      niallo    544:        }
                    545:
1.73      xsa       546:        /* Current head revision gets the RCS patch as rd_text */
1.87      xsa       547:        if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
1.160     xsa       548:                errx(1, "failed to set new rd_text for head rev");
1.63      niallo    549:
1.73      xsa       550:        /* Now add our new revision */
1.63      niallo    551:        if (rcs_rev_add(pb->file,
                    552:            (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82      joris     553:            pb->rcs_msg, pb->date, pb->author) != 0) {
1.155     xsa       554:                warnx("failed to add new revision");
1.200     niallo    555:                return (-1);
1.63      niallo    556:        }
                    557:
                    558:        /*
                    559:         * If we are checking in to a non-default (ie user-specified)
                    560:         * revision, set head to this revision.
                    561:         */
1.131     xsa       562:        if (pb->newrev != NULL) {
                    563:                if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160     xsa       564:                        errx(1, "rcs_head_set failed");
1.131     xsa       565:        } else
1.63      niallo    566:                pb->newrev = pb->file->rf_head;
                    567:
1.73      xsa       568:        /* New head revision has to contain entire file; */
1.180     niallo    569:        if (rcs_deltatext_set(pb->file, pb->frev, bp) == -1)
1.160     xsa       570:                errx(1, "failed to set new head revision");
1.63      niallo    571:
1.73      xsa       572:        /* Attach a symbolic name to this revision if specified. */
1.147     deraadt   573:        if (pb->symbol != NULL &&
                    574:            (checkin_attach_symbol(pb) < 0))
1.200     niallo    575:                return (-1);
1.63      niallo    576:
1.73      xsa       577:        /* Set the state of this revision if specified. */
1.63      niallo    578:        if (pb->state != NULL)
                    579:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    580:
1.128     niallo    581:        /* Maintain RCSFILE permissions */
1.159     xsa       582:        if (fstat(workfile_fd, &st) == -1)
1.160     xsa       583:                err(1, "%s", pb->filename);
1.128     niallo    584:
                    585:        /* Strip all the write bits */
1.189     millert   586:        pb->file->rf_mode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
1.128     niallo    587:
1.158     joris     588:        (void)close(workfile_fd);
1.63      niallo    589:        (void)unlink(pb->filename);
1.140     deraadt   590:
1.128     niallo    591:        /* Write out RCSFILE before calling checkout_rev() */
                    592:        rcs_write(pb->file);
1.63      niallo    593:
1.73      xsa       594:        /* Do checkout if -u or -l are specified. */
1.147     deraadt   595:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
                    596:            !(pb->flags & CI_DEFAULT))
1.63      niallo    597:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89      joris     598:                    pb->username, pb->author, NULL, NULL);
1.192     millert   599:
                    600:        if ((pb->flags & INTERACTIVE) && (pb->rcs_msg[0] == '\0')) {
                    601:                xfree(pb->rcs_msg);     /* free empty log message */
                    602:                pb->rcs_msg = NULL;
                    603:        }
1.200     niallo    604:
1.63      niallo    605:        return (0);
                    606: }
                    607:
                    608: /*
1.58      niallo    609:  * checkin_init()
1.65      xsa       610:  *
1.58      niallo    611:  * Does an initial check in, just enough to create the new ,v file
1.63      niallo    612:  * On success, return 0. On error return -1.
1.58      niallo    613:  */
1.63      niallo    614: static int
1.58      niallo    615: checkin_init(struct checkin_params *pb)
                    616: {
1.157     ray       617:        BUF *bp;
1.201     xsa       618:        char numb[RCS_REV_BUFSZ];
1.115     niallo    619:        int fetchlog = 0;
1.128     niallo    620:        struct stat st;
1.58      niallo    621:
1.115     niallo    622:        /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
1.147     deraadt   623:        if (pb->newrev != NULL && RCSNUM_ZERO_ENDING(pb->newrev)) {
1.115     niallo    624:                pb->frev = rcsnum_alloc();
                    625:                rcsnum_cpy(pb->newrev, pb->frev, 0);
                    626:                pb->newrev = rcsnum_inc(pb->newrev);
                    627:                fetchlog = 1;
                    628:        }
                    629:
1.73      xsa       630:        /* Load file contents */
1.162     joris     631:        if ((bp = rcs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL)
1.200     niallo    632:                return (-1);
1.58      niallo    633:
1.98      niallo    634:        /* Get default values from working copy if -k specified */
                    635:        if (pb->flags & CI_KEYWORDSCAN)
1.183     ray       636:                checkin_keywordscan(bp, &pb->newrev,
1.180     niallo    637:                    &pb->date, &pb->state, &pb->author);
1.98      niallo    638:
1.148     niallo    639:        if (pb->flags & CI_SKIPDESC)
1.142     joris     640:                goto skipdesc;
                    641:
1.73      xsa       642:        /* Get description from user */
1.185     ray       643:        if (pb->description == NULL &&
                    644:            rcs_set_description(pb->file, NULL) == -1) {
1.186     joris     645:                warn("%s", pb->filename);
1.200     niallo    646:                return (-1);
1.185     ray       647:        }
1.142     joris     648:
                    649: skipdesc:
1.58      niallo    650:
1.115     niallo    651:        /*
1.188     ray       652:         * If the user had specified a zero-ending revision number e.g. 4.0
1.115     niallo    653:         * emulate odd GNU behaviour and fetch log message.
                    654:         */
                    655:        if (fetchlog == 1) {
1.154     xsa       656:                pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
                    657:                    pb->flags);
1.115     niallo    658:                rcsnum_free(pb->frev);
                    659:        }
1.133     joris     660:
1.97      niallo    661:        /*
                    662:         * Set the date of the revision to be the last modification
                    663:         * time of the working file if -d has no argument.
                    664:         */
1.158     joris     665:        if (pb->date == DATE_MTIME)
                    666:                checkin_mtimedate(pb);
1.97      niallo    667:
1.73      xsa       668:        /* Now add our new revision */
1.96      niallo    669:        if (rcs_rev_add(pb->file,
                    670:            (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.107     deraadt   671:            (pb->rcs_msg == NULL ? "Initial revision" : pb->rcs_msg),
1.103     niallo    672:            pb->date, pb->author) != 0) {
1.155     xsa       673:                warnx("failed to add new revision");
1.200     niallo    674:                return (-1);
1.63      niallo    675:        }
1.133     joris     676:
1.63      niallo    677:        /*
                    678:         * If we are checking in to a non-default (ie user-specified)
                    679:         * revision, set head to this revision.
                    680:         */
1.131     xsa       681:        if (pb->newrev != NULL) {
                    682:                if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160     xsa       683:                        errx(1, "rcs_head_set failed");
1.131     xsa       684:        } else
1.63      niallo    685:                pb->newrev = pb->file->rf_head;
                    686:
1.73      xsa       687:        /* New head revision has to contain entire file; */
1.180     niallo    688:        if (rcs_deltatext_set(pb->file, pb->file->rf_head, bp) == -1) {
1.155     xsa       689:                warnx("failed to set new head revision");
1.200     niallo    690:                return (-1);
1.58      niallo    691:        }
1.133     joris     692:
1.73      xsa       693:        /* Attach a symbolic name to this revision if specified. */
1.188     ray       694:        if (pb->symbol != NULL && checkin_attach_symbol(pb) < 0)
1.200     niallo    695:                return (-1);
1.66      niallo    696:
1.73      xsa       697:        /* Set the state of this revision if specified. */
1.66      niallo    698:        if (pb->state != NULL)
                    699:                (void)rcs_state_set(pb->file, pb->newrev, pb->state);
                    700:
1.128     niallo    701:        /* Inherit RCSFILE permissions from file being checked in */
1.159     xsa       702:        if (fstat(workfile_fd, &st) == -1)
1.160     xsa       703:                err(1, "%s", pb->filename);
1.158     joris     704:
1.128     niallo    705:        /* Strip all the write bits */
1.189     millert   706:        pb->file->rf_mode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
1.128     niallo    707:
1.158     joris     708:        (void)close(workfile_fd);
1.66      niallo    709:        (void)unlink(pb->filename);
                    710:
1.128     niallo    711:        /* Write out RCSFILE before calling checkout_rev() */
                    712:        rcs_write(pb->file);
                    713:
1.73      xsa       714:        /* Do checkout if -u or -l are specified. */
1.147     deraadt   715:        if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
                    716:            !(pb->flags & CI_DEFAULT)) {
1.66      niallo    717:                checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89      joris     718:                    pb->username, pb->author, NULL, NULL);
1.128     niallo    719:        }
                    720:
1.154     xsa       721:        if (!(pb->flags & QUIET)) {
1.119     xsa       722:                fprintf(stderr, "initial revision: %s\n",
                    723:                    rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
                    724:        }
1.93      xsa       725:
1.63      niallo    726:        return (0);
1.58      niallo    727: }
                    728:
1.59      niallo    729: /*
                    730:  * checkin_attach_symbol()
                    731:  *
                    732:  * Attempt to attach the specified symbol to the revision.
                    733:  * On success, return 0. On error return -1.
                    734:  */
1.58      niallo    735: static int
                    736: checkin_attach_symbol(struct checkin_params *pb)
                    737: {
1.201     xsa       738:        char rbuf[RCS_REV_BUFSZ];
1.58      niallo    739:        int ret;
1.154     xsa       740:        if (!(pb->flags & QUIET))
1.58      niallo    741:                printf("symbol: %s\n", pb->symbol);
1.130     xsa       742:        if (pb->flags & CI_SYMFORCE) {
                    743:                if (rcs_sym_remove(pb->file, pb->symbol) < 0) {
                    744:                        if (rcs_errno != RCS_ERR_NOENT) {
1.155     xsa       745:                                warnx("problem removing symbol: %s",
                    746:                                    pb->symbol);
1.130     xsa       747:                                return (-1);
                    748:                        }
                    749:                }
                    750:        }
1.147     deraadt   751:        if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1) &&
                    752:            (rcs_errno == RCS_ERR_DUPENT)) {
1.58      niallo    753:                rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
                    754:                    rbuf, sizeof(rbuf));
1.155     xsa       755:                warnx("symbolic name %s already bound to %s", pb->symbol, rbuf);
1.58      niallo    756:                return (-1);
                    757:        } else if (ret == -1) {
1.155     xsa       758:                warnx("problem adding symbol: %s", pb->symbol);
1.58      niallo    759:                return (-1);
                    760:        }
                    761:        return (0);
                    762: }
                    763:
1.59      niallo    764: /*
                    765:  * checkin_revert()
                    766:  *
                    767:  * If there are no differences between the working file and the latest revision
                    768:  * and the -f flag is not specified, simply revert to the latest version and
                    769:  * warn the user.
                    770:  *
                    771:  */
1.200     niallo    772: static int
1.58      niallo    773: checkin_revert(struct checkin_params *pb)
                    774: {
1.201     xsa       775:        char rbuf[RCS_REV_BUFSZ];
1.58      niallo    776:
                    777:        rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.173     xsa       778:
                    779:        if (!(pb->flags & QUIET))
                    780:                (void)fprintf(stderr, "file is unchanged; reverting "
                    781:                    "to previous revision %s\n", rbuf);
                    782:
1.200     niallo    783:        /* Attach a symbolic name to this revision if specified. */
                    784:        if (pb->symbol != NULL) {
                    785:                if (checkin_checklock(pb) == -1)
                    786:                        return (-1);
                    787:
                    788:                pb->newrev = pb->frev;
                    789:                if (checkin_attach_symbol(pb) == -1)
                    790:                        return (-1);
                    791:        }
                    792:
1.137     niallo    793:        pb->flags |= CO_REVERT;
1.158     joris     794:        (void)close(workfile_fd);
1.58      niallo    795:        (void)unlink(pb->filename);
1.200     niallo    796:
                    797:        /* If needed, write out RCSFILE before calling checkout_rev() */
                    798:        if (pb->symbol != NULL)
                    799:                rcs_write(pb->file);
                    800:
1.58      niallo    801:        if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
                    802:                checkout_rev(pb->file, pb->frev, pb->filename,
1.89      joris     803:                    pb->flags, pb->username, pb->author, NULL, NULL);
1.200     niallo    804:
                    805:        return (0);
1.58      niallo    806: }
                    807:
1.59      niallo    808: /*
                    809:  * checkin_checklock()
                    810:  *
                    811:  * Check for the existence of a lock on the file.  If there are no locks, or it
                    812:  * is not locked by the correct user, return -1.  Otherwise, return 0.
                    813:  */
1.58      niallo    814: static int
                    815: checkin_checklock(struct checkin_params *pb)
                    816: {
                    817:        struct rcs_lock *lkp;
1.199     xsa       818:
                    819:        if (rcs_lock_getmode(pb->file) == RCS_LOCK_LOOSE)
                    820:                return (0);
1.58      niallo    821:
1.82      joris     822:        TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
1.147     deraadt   823:                if (!strcmp(lkp->rl_name, pb->username) &&
                    824:                    !rcsnum_cmp(lkp->rl_num, pb->frev, 0))
1.82      joris     825:                        return (0);
1.58      niallo    826:        }
1.32      joris     827:
1.155     xsa       828:        warnx("%s: no lock set by %s", pb->file->rf_path, pb->username);
1.82      joris     829:        return (-1);
1.61      niallo    830: }
                    831:
                    832: /*
1.62      niallo    833:  * checkin_mtimedate()
1.61      niallo    834:  *
                    835:  * Set the date of the revision to be the last modification
1.62      niallo    836:  * time of the working file.
1.61      niallo    837:  */
1.158     joris     838: static void
1.62      niallo    839: checkin_mtimedate(struct checkin_params *pb)
1.61      niallo    840: {
                    841:        struct stat sb;
1.158     joris     842:
1.159     xsa       843:        if (fstat(workfile_fd, &sb) == -1)
1.160     xsa       844:                err(1, "%s", pb->filename);
1.158     joris     845:
1.61      niallo    846:        pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.98      niallo    847: }
                    848:
                    849: /*
                    850:  * checkin_keywordscan()
                    851:  *
                    852:  * Searches working file for keyword values to determine its revision
                    853:  * number, creation date and author, and uses these values instead of
                    854:  * calculating them locally.
                    855:  *
                    856:  * Params: The data buffer to scan and pointers to pointers of variables in
                    857:  * which to store the outputs.
                    858:  *
                    859:  * On success, return 0. On error return -1.
                    860:  */
                    861: static int
1.183     ray       862: checkin_keywordscan(BUF *data, RCSNUM **rev, time_t *date, char **author,
1.98      niallo    863:     char **state)
                    864: {
1.183     ray       865:        BUF *buf;
                    866:        size_t left;
1.167     ray       867:        u_int j;
1.183     ray       868:        char *kwstr;
                    869:        unsigned char *c, *end, *start;
1.98      niallo    870:
1.183     ray       871:        end = rcs_buf_get(data) + rcs_buf_len(data) - 1;
1.167     ray       872:        kwstr = NULL;
1.98      niallo    873:
1.183     ray       874:        left = rcs_buf_len(data);
                    875:        for (c = rcs_buf_get(data);
                    876:            c <= end && (c = memchr(c, '$', left)) != NULL;
                    877:            left = end - c + 1) {
1.167     ray       878:                size_t len;
1.98      niallo    879:
1.167     ray       880:                start = c;
                    881:                c++;
                    882:                if (!isalpha(*c))
                    883:                        continue;
                    884:
                    885:                /* look for any matching keywords */
                    886:                for (j = 0; j < 10; j++) {
                    887:                        len = strlen(rcs_expkw[j].kw_str);
1.183     ray       888:                        if (left < len)
                    889:                                continue;
                    890:                        if (memcmp(c, rcs_expkw[j].kw_str, len) != 0) {
1.167     ray       891:                                kwstr = rcs_expkw[j].kw_str;
                    892:                                break;
1.98      niallo    893:                        }
1.167     ray       894:                }
1.98      niallo    895:
1.167     ray       896:                /* unknown keyword, continue looking */
                    897:                if (kwstr == NULL)
                    898:                        continue;
                    899:
                    900:                c += len;
1.183     ray       901:                if (c > end) {
                    902:                        kwstr = NULL;
                    903:                        break;
                    904:                }
                    905:                if (*c != ':') {
                    906:                        kwstr = NULL;
1.167     ray       907:                        continue;
1.183     ray       908:                }
1.167     ray       909:
                    910:                /* Find end of line or end of keyword. */
1.183     ray       911:                while (++c <= end) {
                    912:                        if (*c == '\n') {
                    913:                                /* Skip newline since it is definitely not `$'. */
                    914:                                ++c;
                    915:                                goto loopend;
                    916:                        }
                    917:                        if (*c == '$')
                    918:                                break;
                    919:                }
1.167     ray       920:
1.183     ray       921:                len = c - start + 1;
                    922:                buf = rcs_buf_alloc(len + 1, 0);
                    923:                rcs_buf_append(buf, start, len);
                    924:
                    925:                /* XXX - Not binary safe. */
                    926:                rcs_buf_putc(buf, '\0');
                    927:                checkin_parsekeyword(rcs_buf_get(buf), rev, date, author, state);
1.196     deraadt   928: loopend:;
1.98      niallo    929:        }
1.167     ray       930:        if (kwstr == NULL)
1.98      niallo    931:                return (-1);
                    932:        else
                    933:                return (0);
                    934: }
                    935:
                    936: /*
                    937:  * checkin_keywordtype()
                    938:  *
                    939:  * Given an RCS keyword string, determine what type of string it is.
                    940:  * This enables us to know what data should be in it.
                    941:  *
                    942:  * Returns type on success, or -1 on failure.
1.107     deraadt   943:  */
1.98      niallo    944: static int
                    945: checkin_keywordtype(char *keystring)
                    946: {
                    947:        char *p;
1.107     deraadt   948:
1.98      niallo    949:        p = keystring;
1.118     deraadt   950:        p++;
1.98      niallo    951:        if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
                    952:                return (KW_TYPE_ID);
                    953:        else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
                    954:                return (KW_TYPE_AUTHOR);
                    955:        else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
                    956:                return (KW_TYPE_DATE);
                    957:        else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
                    958:                return (KW_TYPE_STATE);
                    959:        else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
                    960:                return (KW_TYPE_REVISION);
                    961:        else
                    962:                return (-1);
                    963: }
                    964:
                    965: /*
                    966:  * checkin_parsekeyword()
                    967:  *
                    968:  * Do the actual parsing of an RCS keyword string, setting the values passed
                    969:  * to the function to whatever is found.
                    970:  *
1.183     ray       971:  * XXX - Don't error out on malformed keywords.
1.98      niallo    972:  */
                    973: static void
1.183     ray       974: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
1.98      niallo    975:     char **author, char **state)
                    976: {
1.183     ray       977:        char *tokens[KW_NUMTOKS_MAX], *p, *datestring;
1.99      niallo    978:        int i = 0;
1.107     deraadt   979:
1.183     ray       980:        for ((p = strtok(keystring, " ")); p; (p = strtok(NULL, " "))) {
                    981:                if (i < KW_NUMTOKS_MAX - 1)
                    982:                        tokens[i++] = p;
                    983:                else
                    984:                        break;
                    985:        }
                    986:
1.107     deraadt   987:        /* Parse data out of the expanded keyword */
1.98      niallo    988:        switch (checkin_keywordtype(keystring)) {
                    989:        case KW_TYPE_ID:
1.183     ray       990:                if (i < 3)
                    991:                        break;
1.98      niallo    992:                /* only parse revision if one is not already set */
                    993:                if (*rev == NULL) {
                    994:                        if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1.160     xsa       995:                                errx(1, "could not parse rcsnum");
1.98      niallo    996:                }
1.183     ray       997:
                    998:                if (i < 5)
                    999:                        break;
1.184     ray      1000:                (void)xasprintf(&datestring, "%s %s", tokens[3], tokens[4]);
                   1001:                if ((*date = rcs_date_parse(datestring)) <= 0)
1.166     ray      1002:                        errx(1, "could not parse date");
1.98      niallo   1003:                xfree(datestring);
1.183     ray      1004:
                   1005:                if (i < 6)
                   1006:                        break;
                   1007:                if (*author != NULL)
                   1008:                        xfree(*author);
                   1009:                *author = xstrdup(tokens[5]);
                   1010:
                   1011:                if (i < 7)
                   1012:                        break;
                   1013:                if (*state != NULL)
                   1014:                        xfree(*state);
                   1015:                *state = xstrdup(tokens[6]);
1.98      niallo   1016:                break;
                   1017:        case KW_TYPE_AUTHOR:
1.183     ray      1018:                if (i < 2)
                   1019:                        break;
1.98      niallo   1020:                if (*author != NULL)
                   1021:                        xfree(*author);
1.135     ray      1022:                *author = xstrdup(tokens[1]);
1.98      niallo   1023:                break;
                   1024:        case KW_TYPE_DATE:
1.183     ray      1025:                if (i < 3)
                   1026:                        break;
1.184     ray      1027:                (void)xasprintf(&datestring, "%s %s", tokens[1], tokens[2]);
1.162     joris    1028:                if ((*date = rcs_date_parse(datestring)) <= 0)
1.166     ray      1029:                        errx(1, "could not parse date");
1.98      niallo   1030:                xfree(datestring);
                   1031:                break;
                   1032:        case KW_TYPE_STATE:
1.183     ray      1033:                if (i < 2)
                   1034:                        break;
1.98      niallo   1035:                if (*state != NULL)
                   1036:                        xfree(*state);
1.135     ray      1037:                *state = xstrdup(tokens[1]);
1.98      niallo   1038:                break;
                   1039:        case KW_TYPE_REVISION:
1.183     ray      1040:                if (i < 2)
                   1041:                        break;
1.98      niallo   1042:                /* only parse revision if one is not already set */
                   1043:                if (*rev != NULL)
                   1044:                        break;
                   1045:                if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1.160     xsa      1046:                        errx(1, "could not parse rcsnum");
1.98      niallo   1047:                break;
                   1048:        }
1.1       niallo   1049: }