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