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