Annotation of src/usr.bin/rcs/ci.c, Revision 1.216
1.216 ! guenther 1: /* $OpenBSD: ci.c,v 1.215 2013/04/17 00:20:52 deraadt 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"
1.214 guenther 45: #define KW_OPENBSD "OpenBSD"
1.107 deraadt 46: #define KW_AUTHOR "Author"
47: #define KW_DATE "Date"
48: #define KW_STATE "State"
49: #define KW_REVISION "Revision"
50:
51: #define KW_TYPE_ID 1
52: #define KW_TYPE_AUTHOR 2
53: #define KW_TYPE_DATE 3
54: #define KW_TYPE_STATE 4
55: #define KW_TYPE_REVISION 5
1.58 niallo 56:
1.183 ray 57: /* Maximum number of tokens in a keyword. */
58: #define KW_NUMTOKS_MAX 10
59:
1.115 niallo 60: #define RCSNUM_ZERO_ENDING(x) (x->rn_id[x->rn_len - 1] == 0)
61:
1.98 niallo 62: extern struct rcs_kw rcs_expkw[];
63:
1.158 joris 64: static int workfile_fd;
65:
1.58 niallo 66: struct checkin_params {
67: int flags, openflags;
68: mode_t fmode;
69: time_t date;
70: RCSFILE *file;
71: RCSNUM *frev, *newrev;
1.177 ray 72: const char *description, *symbol;
1.180 niallo 73: char fpath[MAXPATHLEN], *rcs_msg, *username, *filename;
1.177 ray 74: char *author, *state;
1.180 niallo 75: BUF *deltatext;
1.58 niallo 76: };
77:
1.94 niallo 78: static int checkin_attach_symbol(struct checkin_params *);
79: static int checkin_checklock(struct checkin_params *);
1.180 niallo 80: static BUF *checkin_diff_file(struct checkin_params *);
1.154 xsa 81: static char *checkin_getlogmsg(RCSNUM *, RCSNUM *, int);
1.68 xsa 82: static int checkin_init(struct checkin_params *);
1.183 ray 83: static int checkin_keywordscan(BUF *, RCSNUM **, time_t *, char **,
1.98 niallo 84: char **);
1.107 deraadt 85: static int checkin_keywordtype(char *);
1.158 joris 86: static void checkin_mtimedate(struct checkin_params *);
1.107 deraadt 87: static void checkin_parsekeyword(char *, RCSNUM **, time_t *, char **,
1.98 niallo 88: char **);
1.94 niallo 89: static int checkin_update(struct checkin_params *);
1.200 niallo 90: static int checkin_revert(struct checkin_params *);
1.4 niallo 91:
1.1 niallo 92: void
93: checkin_usage(void)
94: {
95: fprintf(stderr,
1.193 niallo 96: "usage: ci [-qV] [-d[date]] [-f[rev]] [-I[rev]] [-i[rev]]\n"
1.198 sobrado 97: " [-j[rev]] [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg]\n"
1.213 sobrado 98: " [-Nsymbol] [-nsymbol] [-r[rev]] [-sstate] [-t[str]]\n"
1.198 sobrado 99: " [-u[rev]] [-wusername] [-xsuffixes] [-ztz] file ...\n");
1.58 niallo 100: }
101:
1.1 niallo 102: /*
103: * checkin_main()
104: *
105: * Handler for the `ci' program.
106: * Returns 0 on success, or >0 on error.
107: */
108: int
109: checkin_main(int argc, char **argv)
110: {
1.163 joris 111: int fd;
1.58 niallo 112: int i, ch, status;
1.197 bluhm 113: int base_flags, base_openflags;
1.139 ray 114: char *rev_str;
1.58 niallo 115: struct checkin_params pb;
1.1 niallo 116:
1.58 niallo 117: pb.date = DATE_NOW;
118: pb.file = NULL;
1.98 niallo 119: pb.rcs_msg = pb.username = pb.author = pb.state = NULL;
1.177 ray 120: pb.description = pb.symbol = NULL;
121: pb.deltatext = NULL;
1.58 niallo 122: pb.newrev = NULL;
1.128 niallo 123: pb.fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.197 bluhm 124: status = 0;
125: base_flags = INTERACTIVE;
126: base_openflags = RCS_RDWR|RCS_CREATE|RCS_PARSE_FULLY;
1.139 ray 127: rev_str = NULL;
1.9 niallo 128:
1.58 niallo 129: while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1 niallo 130: switch (ch) {
1.20 niallo 131: case 'd':
1.25 niallo 132: if (rcs_optarg == NULL)
1.58 niallo 133: pb.date = DATE_MTIME;
1.210 ray 134: else if ((pb.date = date_parse(rcs_optarg)) == -1)
1.160 xsa 135: errx(1, "invalid date");
1.20 niallo 136: break;
1.29 niallo 137: case 'f':
1.139 ray 138: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 139: base_flags |= FORCE;
1.29 niallo 140: break;
1.119 xsa 141: case 'I':
1.139 ray 142: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 143: base_flags |= INTERACTIVE;
1.119 xsa 144: break;
1.58 niallo 145: case 'i':
1.139 ray 146: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 147: base_openflags |= RCS_CREATE;
148: base_flags |= CI_INIT;
1.58 niallo 149: break;
150: case 'j':
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;
1.98 niallo 155: case 'k':
1.139 ray 156: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 157: base_flags |= CI_KEYWORDSCAN;
1.98 niallo 158: break;
1.30 niallo 159: case 'l':
1.139 ray 160: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 161: base_flags |= CO_LOCK;
1.54 niallo 162: break;
163: case 'M':
1.139 ray 164: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 165: base_flags |= CO_REVDATE;
1.30 niallo 166: break;
1.1 niallo 167: case 'm':
1.58 niallo 168: pb.rcs_msg = rcs_optarg;
1.87 xsa 169: if (pb.rcs_msg == NULL)
1.160 xsa 170: errx(1, "missing message for -m option");
1.197 bluhm 171: base_flags &= ~INTERACTIVE;
1.3 joris 172: break;
1.42 niallo 173: case 'N':
1.197 bluhm 174: base_flags |= CI_SYMFORCE;
1.152 ray 175: /* FALLTHROUGH */
1.38 niallo 176: case 'n':
1.177 ray 177: pb.symbol = rcs_optarg;
1.87 xsa 178: if (rcs_sym_check(pb.symbol) != 1)
1.160 xsa 179: errx(1, "invalid symbol `%s'", pb.symbol);
1.38 niallo 180: break;
1.3 joris 181: case 'q':
1.197 bluhm 182: base_flags |= QUIET;
1.1 niallo 183: break;
1.30 niallo 184: case 'r':
1.139 ray 185: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 186: base_flags |= CI_DEFAULT;
1.9 niallo 187: break;
1.51 niallo 188: case 's':
1.58 niallo 189: pb.state = rcs_optarg;
1.87 xsa 190: if (rcs_state_check(pb.state) < 0)
1.160 xsa 191: errx(1, "invalid state `%s'", pb.state);
1.67 xsa 192: break;
193: case 'T':
1.197 bluhm 194: base_flags |= PRESERVETIME;
1.51 niallo 195: break;
1.80 niallo 196: case 't':
1.157 ray 197: /* Ignore bare -t; kept for backwards compatibility. */
198: if (rcs_optarg == NULL)
199: break;
200: pb.description = rcs_optarg;
1.197 bluhm 201: base_flags |= DESCRIPTION;
1.80 niallo 202: break;
1.9 niallo 203: case 'u':
1.139 ray 204: rcs_setrevstr(&rev_str, rcs_optarg);
1.197 bluhm 205: base_flags |= CO_UNLOCK;
1.9 niallo 206: break;
1.30 niallo 207: case 'V':
208: printf("%s\n", rcs_version);
209: exit(0);
1.31 niallo 210: case 'w':
1.151 ray 211: if (pb.author != NULL)
212: xfree(pb.author);
1.84 joris 213: pb.author = xstrdup(rcs_optarg);
1.64 xsa 214: break;
215: case 'x':
1.125 ray 216: /* Use blank extension if none given. */
217: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.110 joris 218: break;
219: case 'z':
220: timezone_flag = rcs_optarg;
1.31 niallo 221: break;
1.1 niallo 222: default:
223: (usage)();
224: exit(1);
225: }
226: }
227:
1.24 joris 228: argc -= rcs_optind;
229: argv += rcs_optind;
230:
1.1 niallo 231: if (argc == 0) {
1.155 xsa 232: warnx("no input file");
1.1 niallo 233: (usage)();
234: exit(1);
235: }
236:
1.86 xsa 237: if ((pb.username = getlogin()) == NULL)
1.160 xsa 238: err(1, "getlogin");
1.194 otto 239:
1.1 niallo 240: for (i = 0; i < argc; i++) {
1.197 bluhm 241: /*
242: * The pb.flags and pb.openflags may change during
243: * loop iteration so restore them for each file.
244: */
245: pb.flags = base_flags;
246: pb.openflags = base_openflags;
247:
1.58 niallo 248: pb.filename = argv[i];
1.194 otto 249: rcs_strip_suffix(pb.filename);
1.1 niallo 250:
1.158 joris 251: if ((workfile_fd = open(pb.filename, O_RDONLY)) == -1)
1.160 xsa 252: err(1, "%s", pb.filename);
1.158 joris 253:
1.175 ray 254: /* Find RCS file path. */
255: fd = rcs_choosefile(pb.filename, pb.fpath, sizeof(pb.fpath));
1.163 joris 256:
257: if (fd < 0) {
1.71 niallo 258: if (pb.openflags & RCS_CREATE)
259: pb.flags |= NEWFILE;
260: else {
1.188 ray 261: /* XXX - Check if errno == ENOENT. */
1.155 xsa 262: warnx("No existing RCS file");
1.71 niallo 263: status = 1;
1.158 joris 264: (void)close(workfile_fd);
1.71 niallo 265: continue;
266: }
1.66 niallo 267: } else {
268: if (pb.flags & CI_INIT) {
1.155 xsa 269: warnx("%s already exists", pb.fpath);
1.66 niallo 270: status = 1;
1.163 joris 271: (void)close(fd);
1.158 joris 272: (void)close(workfile_fd);
1.66 niallo 273: continue;
274: }
1.58 niallo 275: pb.openflags &= ~RCS_CREATE;
1.66 niallo 276: }
1.63 niallo 277:
1.163 joris 278: pb.file = rcs_open(pb.fpath, fd, pb.openflags, pb.fmode);
1.87 xsa 279: if (pb.file == NULL)
1.161 xsa 280: errx(1, "failed to open rcsfile `%s'", pb.fpath);
1.29 niallo 281:
1.185 ray 282: if ((pb.flags & DESCRIPTION) &&
283: rcs_set_description(pb.file, pb.description) == -1)
1.186 joris 284: err(1, "%s", pb.filename);
1.157 ray 285:
1.154 xsa 286: if (!(pb.flags & QUIET))
1.173 xsa 287: (void)fprintf(stderr,
288: "%s <-- %s\n", pb.fpath, pb.filename);
1.139 ray 289:
290: /* XXX - Should we rcsnum_free(pb.newrev)? */
291: if (rev_str != NULL)
1.141 ray 292: if ((pb.newrev = rcs_getrevnum(rev_str, pb.file)) ==
293: NULL)
1.160 xsa 294: errx(1, "invalid revision: %s", rev_str);
1.107 deraadt 295:
1.148 niallo 296: if (!(pb.flags & NEWFILE))
297: pb.flags |= CI_SKIPDESC;
1.158 joris 298:
1.179 david 299: /* XXX - support for committing to a file without revisions */
1.142 joris 300: if (pb.file->rf_ndelta == 0) {
301: pb.flags |= NEWFILE;
302: pb.file->rf_flags |= RCS_CREATE;
303: }
304:
1.158 joris 305: /*
306: * workfile_fd will be closed in checkin_init or
307: * checkin_update
308: */
1.149 ray 309: if (pb.flags & NEWFILE) {
310: if (checkin_init(&pb) == -1)
311: status = 1;
312: } else {
313: if (checkin_update(&pb) == -1)
314: status = 1;
315: }
1.114 niallo 316:
1.129 niallo 317: rcs_close(pb.file);
1.178 niallo 318: pb.newrev = NULL;
1.4 niallo 319: }
320:
1.197 bluhm 321: if (!(base_flags & QUIET) && status == 0)
1.173 xsa 322: (void)fprintf(stderr, "done\n");
1.93 xsa 323:
1.16 niallo 324: return (status);
1.4 niallo 325: }
326:
1.59 niallo 327: /*
328: * checkin_diff_file()
329: *
1.65 xsa 330: * Generate the diff between the working file and a revision.
1.190 niallo 331: * Returns pointer to a BUF on success, NULL on failure.
1.59 niallo 332: */
1.180 niallo 333: static BUF *
1.58 niallo 334: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 335: {
1.168 xsa 336: char *path1, *path2;
1.4 niallo 337: BUF *b1, *b2, *b3;
338:
1.112 joris 339: b1 = b2 = b3 = NULL;
1.204 millert 340: path1 = path2 = NULL;
1.4 niallo 341:
1.209 ray 342: if ((b1 = buf_load(pb->filename)) == NULL) {
1.155 xsa 343: warnx("failed to load file: `%s'", pb->filename);
1.112 joris 344: goto out;
1.1 niallo 345: }
346:
1.58 niallo 347: if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.155 xsa 348: warnx("failed to load revision");
1.112 joris 349: goto out;
1.4 niallo 350: }
1.206 millert 351: b2 = rcs_kwexp_buf(b2, pb->file, pb->frev);
1.209 ray 352: b3 = buf_alloc(128);
1.4 niallo 353:
1.168 xsa 354: (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
1.207 ray 355: buf_write_stmp(b1, path1);
1.112 joris 356:
1.207 ray 357: buf_free(b1);
1.112 joris 358: b1 = NULL;
1.4 niallo 359:
1.168 xsa 360: (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
1.207 ray 361: buf_write_stmp(b2, path2);
1.112 joris 362:
1.207 ray 363: buf_free(b2);
1.112 joris 364: b2 = NULL;
1.4 niallo 365:
1.5 niallo 366: diff_format = D_RCSDIFF;
1.205 ray 367: if (diffreg(path1, path2, b3, D_FORCEASCII) == D_ERROR)
1.170 xsa 368: goto out;
1.4 niallo 369:
1.180 niallo 370: return (b3);
1.112 joris 371: out:
372: if (b1 != NULL)
1.207 ray 373: buf_free(b1);
1.112 joris 374: if (b2 != NULL)
1.207 ray 375: buf_free(b2);
1.112 joris 376: if (b3 != NULL)
1.207 ray 377: buf_free(b3);
1.168 xsa 378: if (path1 != NULL)
379: xfree(path1);
380: if (path2 != NULL)
381: xfree(path2);
1.4 niallo 382:
1.180 niallo 383: return (NULL);
1.6 niallo 384: }
385:
386: /*
1.65 xsa 387: * checkin_getlogmsg()
1.59 niallo 388: *
1.6 niallo 389: * Get log message from user interactively.
1.59 niallo 390: * Returns pointer to a char array on success, NULL on failure.
1.6 niallo 391: */
392: static char *
1.154 xsa 393: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2, int flags)
1.6 niallo 394: {
1.201 xsa 395: char *rcs_msg, nrev[RCS_REV_BUFSZ], prev[RCS_REV_BUFSZ];
1.153 ray 396: const char *prompt =
397: "enter log message, terminated with a single '.' or end of file:\n";
1.6 niallo 398: RCSNUM *tmprev;
399:
1.7 niallo 400: rcs_msg = NULL;
1.6 niallo 401: tmprev = rcsnum_alloc();
402: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 403: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 404: if (rev2 == NULL)
405: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
406: else
407: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 408: rcsnum_free(tmprev);
409:
1.154 xsa 410: if (!(flags & QUIET))
1.174 xsa 411: (void)fprintf(stderr, "new revision: %s; "
412: "previous revision: %s\n", nrev, prev);
1.32 joris 413:
1.153 ray 414: rcs_msg = rcs_prompt(prompt);
415:
1.58 niallo 416: return (rcs_msg);
417: }
418:
419: /*
1.63 niallo 420: * checkin_update()
421: *
422: * Do a checkin to an existing RCS file.
423: *
424: * On success, return 0. On error return -1.
425: */
426: static int
427: checkin_update(struct checkin_params *pb)
428: {
1.201 xsa 429: char numb1[RCS_REV_BUFSZ], numb2[RCS_REV_BUFSZ];
1.128 niallo 430: struct stat st;
1.63 niallo 431: BUF *bp;
1.149 ray 432:
1.82 joris 433: /*
434: * XXX this is wrong, we need to get the revision the user
1.85 xsa 435: * has the lock for. So we can decide if we want to create a
1.82 joris 436: * branch or not. (if it's not current HEAD we need to branch).
437: */
1.63 niallo 438: pb->frev = pb->file->rf_head;
439:
1.126 niallo 440: /* Load file contents */
1.209 ray 441: if ((bp = buf_load(pb->filename)) == NULL)
1.200 niallo 442: return (-1);
1.126 niallo 443:
1.115 niallo 444: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
1.147 deraadt 445: if (pb->newrev != NULL && RCSNUM_ZERO_ENDING(pb->newrev))
1.115 niallo 446: pb->newrev = rcsnum_inc(pb->newrev);
447:
1.82 joris 448: if (checkin_checklock(pb) < 0)
1.200 niallo 449: return (-1);
1.82 joris 450:
451: /* If revision passed on command line is less than HEAD, bail.
452: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
453: * there is no lock set for the user.
454: */
1.147 deraadt 455: if (pb->newrev != NULL &&
1.203 niallo 456: rcsnum_cmp(pb->newrev, pb->frev, 0) != -1) {
1.155 xsa 457: warnx("%s: revision %s too low; must be higher than %s",
1.77 xsa 458: pb->file->rf_path,
459: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
460: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.200 niallo 461: return (-1);
1.63 niallo 462: }
463:
1.104 niallo 464: /*
465: * Set the date of the revision to be the last modification
466: * time of the working file if -d has no argument.
467: */
1.158 joris 468: if (pb->date == DATE_MTIME)
469: checkin_mtimedate(pb);
1.104 niallo 470:
471: /* Date from argv/mtime must be more recent than HEAD */
472: if (pb->date != DATE_NOW) {
473: time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
474: if (pb->date <= head_date) {
1.216 ! guenther 475: static const char fmt[] = "%Y/%m/%d %H:%M:%S";
! 476: char dbuf1[256], dbuf2[256];
1.113 xsa 477: struct tm *t, *t_head;
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.215 deraadt 831: pb->date = 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.214 guenther 937: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0 ||
938: strncmp(p, KW_OPENBSD, strlen(KW_OPENBSD)) == 0)
1.98 niallo 939: return (KW_TYPE_ID);
940: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
941: return (KW_TYPE_AUTHOR);
942: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
943: return (KW_TYPE_DATE);
944: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
945: return (KW_TYPE_STATE);
946: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
947: return (KW_TYPE_REVISION);
948: else
949: return (-1);
950: }
951:
952: /*
953: * checkin_parsekeyword()
954: *
955: * Do the actual parsing of an RCS keyword string, setting the values passed
956: * to the function to whatever is found.
957: *
1.183 ray 958: * XXX - Don't error out on malformed keywords.
1.98 niallo 959: */
960: static void
1.183 ray 961: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
1.98 niallo 962: char **author, char **state)
963: {
1.183 ray 964: char *tokens[KW_NUMTOKS_MAX], *p, *datestring;
1.99 niallo 965: int i = 0;
1.107 deraadt 966:
1.183 ray 967: for ((p = strtok(keystring, " ")); p; (p = strtok(NULL, " "))) {
968: if (i < KW_NUMTOKS_MAX - 1)
969: tokens[i++] = p;
970: else
971: break;
972: }
973:
1.107 deraadt 974: /* Parse data out of the expanded keyword */
1.98 niallo 975: switch (checkin_keywordtype(keystring)) {
976: case KW_TYPE_ID:
1.183 ray 977: if (i < 3)
978: break;
1.98 niallo 979: /* only parse revision if one is not already set */
980: if (*rev == NULL) {
981: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1.160 xsa 982: errx(1, "could not parse rcsnum");
1.98 niallo 983: }
1.183 ray 984:
985: if (i < 5)
986: break;
1.184 ray 987: (void)xasprintf(&datestring, "%s %s", tokens[3], tokens[4]);
1.210 ray 988: if ((*date = date_parse(datestring)) == -1)
1.166 ray 989: errx(1, "could not parse date");
1.98 niallo 990: xfree(datestring);
1.183 ray 991:
992: if (i < 6)
993: break;
994: if (*author != NULL)
995: xfree(*author);
996: *author = xstrdup(tokens[5]);
997:
998: if (i < 7)
999: break;
1000: if (*state != NULL)
1001: xfree(*state);
1002: *state = xstrdup(tokens[6]);
1.98 niallo 1003: break;
1004: case KW_TYPE_AUTHOR:
1.183 ray 1005: if (i < 2)
1006: break;
1.98 niallo 1007: if (*author != NULL)
1008: xfree(*author);
1.135 ray 1009: *author = xstrdup(tokens[1]);
1.98 niallo 1010: break;
1011: case KW_TYPE_DATE:
1.183 ray 1012: if (i < 3)
1013: break;
1.184 ray 1014: (void)xasprintf(&datestring, "%s %s", tokens[1], tokens[2]);
1.210 ray 1015: if ((*date = date_parse(datestring)) == -1)
1.166 ray 1016: errx(1, "could not parse date");
1.98 niallo 1017: xfree(datestring);
1018: break;
1019: case KW_TYPE_STATE:
1.183 ray 1020: if (i < 2)
1021: break;
1.98 niallo 1022: if (*state != NULL)
1023: xfree(*state);
1.135 ray 1024: *state = xstrdup(tokens[1]);
1.98 niallo 1025: break;
1026: case KW_TYPE_REVISION:
1.183 ray 1027: if (i < 2)
1028: break;
1.98 niallo 1029: /* only parse revision if one is not already set */
1030: if (*rev != NULL)
1031: break;
1032: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1.160 xsa 1033: errx(1, "could not parse rcsnum");
1.98 niallo 1034: break;
1035: }
1.1 niallo 1036: }