Annotation of src/usr.bin/rcs/ci.c, Revision 1.218
1.218 ! otto 1: /* $OpenBSD: ci.c,v 1.217 2014/05/19 19:42:24 jca 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.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.151 ray 213: if (pb.author != NULL)
214: xfree(pb.author);
1.84 joris 215: pb.author = xstrdup(rcs_optarg);
1.64 xsa 216: break;
217: case 'x':
1.125 ray 218: /* Use blank extension if none given. */
219: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.110 joris 220: break;
221: case 'z':
222: timezone_flag = rcs_optarg;
1.31 niallo 223: break;
1.1 niallo 224: default:
225: (usage)();
226: }
227: }
228:
1.24 joris 229: argc -= rcs_optind;
230: argv += rcs_optind;
231:
1.1 niallo 232: if (argc == 0) {
1.155 xsa 233: warnx("no input file");
1.1 niallo 234: (usage)();
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: 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.217 jca 317: if (rev_str != NULL)
318: rcsnum_free(pb.newrev);
1.178 niallo 319: pb.newrev = NULL;
1.4 niallo 320: }
321:
1.197 bluhm 322: if (!(base_flags & QUIET) && status == 0)
1.173 xsa 323: (void)fprintf(stderr, "done\n");
1.93 xsa 324:
1.16 niallo 325: return (status);
1.4 niallo 326: }
327:
1.59 niallo 328: /*
329: * checkin_diff_file()
330: *
1.65 xsa 331: * Generate the diff between the working file and a revision.
1.190 niallo 332: * Returns pointer to a BUF on success, NULL on failure.
1.59 niallo 333: */
1.180 niallo 334: static BUF *
1.58 niallo 335: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 336: {
1.168 xsa 337: char *path1, *path2;
1.4 niallo 338: BUF *b1, *b2, *b3;
339:
1.112 joris 340: b1 = b2 = b3 = NULL;
1.204 millert 341: path1 = path2 = NULL;
1.4 niallo 342:
1.209 ray 343: if ((b1 = buf_load(pb->filename)) == NULL) {
1.155 xsa 344: warnx("failed to load file: `%s'", pb->filename);
1.112 joris 345: goto out;
1.1 niallo 346: }
347:
1.58 niallo 348: if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.155 xsa 349: warnx("failed to load revision");
1.112 joris 350: goto out;
1.4 niallo 351: }
1.206 millert 352: b2 = rcs_kwexp_buf(b2, pb->file, pb->frev);
1.209 ray 353: b3 = buf_alloc(128);
1.4 niallo 354:
1.168 xsa 355: (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
1.207 ray 356: buf_write_stmp(b1, path1);
1.112 joris 357:
1.207 ray 358: buf_free(b1);
1.112 joris 359: b1 = NULL;
1.4 niallo 360:
1.168 xsa 361: (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
1.207 ray 362: buf_write_stmp(b2, path2);
1.112 joris 363:
1.207 ray 364: buf_free(b2);
1.112 joris 365: b2 = NULL;
1.4 niallo 366:
1.5 niallo 367: diff_format = D_RCSDIFF;
1.205 ray 368: if (diffreg(path1, path2, b3, D_FORCEASCII) == D_ERROR)
1.170 xsa 369: goto out;
1.4 niallo 370:
1.180 niallo 371: return (b3);
1.112 joris 372: out:
373: if (b1 != NULL)
1.207 ray 374: buf_free(b1);
1.112 joris 375: if (b2 != NULL)
1.207 ray 376: buf_free(b2);
1.112 joris 377: if (b3 != NULL)
1.207 ray 378: buf_free(b3);
1.168 xsa 379: if (path1 != NULL)
380: xfree(path1);
381: if (path2 != NULL)
382: xfree(path2);
1.4 niallo 383:
1.180 niallo 384: return (NULL);
1.6 niallo 385: }
386:
387: /*
1.65 xsa 388: * checkin_getlogmsg()
1.59 niallo 389: *
1.6 niallo 390: * Get log message from user interactively.
1.59 niallo 391: * Returns pointer to a char array on success, NULL on failure.
1.6 niallo 392: */
393: static char *
1.154 xsa 394: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2, int flags)
1.6 niallo 395: {
1.201 xsa 396: char *rcs_msg, nrev[RCS_REV_BUFSZ], prev[RCS_REV_BUFSZ];
1.153 ray 397: const char *prompt =
398: "enter log message, terminated with a single '.' or end of file:\n";
1.6 niallo 399: RCSNUM *tmprev;
400:
1.7 niallo 401: rcs_msg = NULL;
1.6 niallo 402: tmprev = rcsnum_alloc();
403: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 404: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 405: if (rev2 == NULL)
406: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
407: else
408: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 409: rcsnum_free(tmprev);
410:
1.154 xsa 411: if (!(flags & QUIET))
1.174 xsa 412: (void)fprintf(stderr, "new revision: %s; "
413: "previous revision: %s\n", nrev, prev);
1.32 joris 414:
1.153 ray 415: rcs_msg = rcs_prompt(prompt);
416:
1.58 niallo 417: return (rcs_msg);
418: }
419:
420: /*
1.63 niallo 421: * checkin_update()
422: *
423: * Do a checkin to an existing RCS file.
424: *
425: * On success, return 0. On error return -1.
426: */
427: static int
428: checkin_update(struct checkin_params *pb)
429: {
1.201 xsa 430: char numb1[RCS_REV_BUFSZ], numb2[RCS_REV_BUFSZ];
1.128 niallo 431: struct stat st;
1.63 niallo 432: BUF *bp;
1.149 ray 433:
1.82 joris 434: /*
435: * XXX this is wrong, we need to get the revision the user
1.85 xsa 436: * has the lock for. So we can decide if we want to create a
1.82 joris 437: * branch or not. (if it's not current HEAD we need to branch).
438: */
1.63 niallo 439: pb->frev = pb->file->rf_head;
440:
1.126 niallo 441: /* Load file contents */
1.209 ray 442: if ((bp = buf_load(pb->filename)) == NULL)
1.200 niallo 443: return (-1);
1.126 niallo 444:
1.115 niallo 445: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
1.147 deraadt 446: if (pb->newrev != NULL && RCSNUM_ZERO_ENDING(pb->newrev))
1.115 niallo 447: pb->newrev = rcsnum_inc(pb->newrev);
448:
1.82 joris 449: if (checkin_checklock(pb) < 0)
1.200 niallo 450: return (-1);
1.82 joris 451:
452: /* If revision passed on command line is less than HEAD, bail.
453: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
454: * there is no lock set for the user.
455: */
1.147 deraadt 456: if (pb->newrev != NULL &&
1.203 niallo 457: rcsnum_cmp(pb->newrev, pb->frev, 0) != -1) {
1.155 xsa 458: warnx("%s: revision %s too low; must be higher than %s",
1.77 xsa 459: pb->file->rf_path,
460: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
461: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.200 niallo 462: return (-1);
1.63 niallo 463: }
464:
1.104 niallo 465: /*
466: * Set the date of the revision to be the last modification
467: * time of the working file if -d has no argument.
468: */
1.158 joris 469: if (pb->date == DATE_MTIME)
470: checkin_mtimedate(pb);
1.104 niallo 471:
472: /* Date from argv/mtime must be more recent than HEAD */
473: if (pb->date != DATE_NOW) {
474: time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
475: if (pb->date <= head_date) {
1.216 guenther 476: static const char fmt[] = "%Y/%m/%d %H:%M:%S";
477: char dbuf1[256], dbuf2[256];
1.113 xsa 478: struct tm *t, *t_head;
479:
1.164 ray 480: t = gmtime(&pb->date);
1.113 xsa 481: strftime(dbuf1, sizeof(dbuf1), fmt, t);
1.164 ray 482: t_head = gmtime(&head_date);
1.113 xsa 483: strftime(dbuf2, sizeof(dbuf2), fmt, t_head);
484:
1.191 krw 485: errx(1, "%s: Date %s precedes %s in revision %s.",
1.113 xsa 486: pb->file->rf_path, dbuf1, dbuf2,
1.104 niallo 487: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
488: }
489: }
490:
1.73 xsa 491: /* Get RCS patch */
1.63 niallo 492: if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
1.155 xsa 493: warnx("failed to get diff");
1.200 niallo 494: return (-1);
1.63 niallo 495: }
496:
497: /*
498: * If -f is not specified and there are no differences, tell
499: * the user and revert to latest version.
500: */
1.207 ray 501: if (!(pb->flags & FORCE) && (buf_len(pb->deltatext) < 1)) {
1.200 niallo 502: if (checkin_revert(pb) == -1)
503: return (-1);
504: else
505: return (0);
1.63 niallo 506: }
507:
1.73 xsa 508: /* If no log message specified, get it interactively. */
1.189 millert 509: if (pb->flags & INTERACTIVE) {
510: if (pb->rcs_msg != NULL) {
511: fprintf(stderr,
512: "reuse log message of previous file? [yn](y): ");
513: if (rcs_yesno('y') != 'y') {
514: xfree(pb->rcs_msg);
515: pb->rcs_msg = NULL;
516: }
517: }
518: if (pb->rcs_msg == NULL)
519: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
520: pb->flags);
521: }
1.63 niallo 522:
1.199 xsa 523: if ((rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) &&
524: (rcs_lock_getmode(pb->file) != RCS_LOCK_LOOSE)) {
1.63 niallo 525: if (rcs_errno != RCS_ERR_NOENT)
1.155 xsa 526: warnx("failed to remove lock");
1.82 joris 527: else if (!(pb->flags & CO_LOCK))
1.155 xsa 528: warnx("previous revision was not locked; "
1.82 joris 529: "ignoring -l option");
1.63 niallo 530: }
531:
1.73 xsa 532: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 533: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
1.160 xsa 534: errx(1, "failed to set new rd_text for head rev");
1.63 niallo 535:
1.73 xsa 536: /* Now add our new revision */
1.63 niallo 537: if (rcs_rev_add(pb->file,
538: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 539: pb->rcs_msg, pb->date, pb->author) != 0) {
1.155 xsa 540: warnx("failed to add new revision");
1.200 niallo 541: return (-1);
1.63 niallo 542: }
543:
544: /*
545: * If we are checking in to a non-default (ie user-specified)
546: * revision, set head to this revision.
547: */
1.131 xsa 548: if (pb->newrev != NULL) {
549: if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160 xsa 550: errx(1, "rcs_head_set failed");
1.131 xsa 551: } else
1.63 niallo 552: pb->newrev = pb->file->rf_head;
553:
1.73 xsa 554: /* New head revision has to contain entire file; */
1.180 niallo 555: if (rcs_deltatext_set(pb->file, pb->frev, bp) == -1)
1.160 xsa 556: errx(1, "failed to set new head revision");
1.63 niallo 557:
1.73 xsa 558: /* Attach a symbolic name to this revision if specified. */
1.147 deraadt 559: if (pb->symbol != NULL &&
560: (checkin_attach_symbol(pb) < 0))
1.200 niallo 561: return (-1);
1.63 niallo 562:
1.73 xsa 563: /* Set the state of this revision if specified. */
1.63 niallo 564: if (pb->state != NULL)
565: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
566:
1.128 niallo 567: /* Maintain RCSFILE permissions */
1.159 xsa 568: if (fstat(workfile_fd, &st) == -1)
1.160 xsa 569: err(1, "%s", pb->filename);
1.128 niallo 570:
571: /* Strip all the write bits */
1.189 millert 572: pb->file->rf_mode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
1.128 niallo 573:
1.158 joris 574: (void)close(workfile_fd);
1.63 niallo 575: (void)unlink(pb->filename);
1.140 deraadt 576:
1.128 niallo 577: /* Write out RCSFILE before calling checkout_rev() */
578: rcs_write(pb->file);
1.63 niallo 579:
1.73 xsa 580: /* Do checkout if -u or -l are specified. */
1.147 deraadt 581: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
582: !(pb->flags & CI_DEFAULT))
1.63 niallo 583: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 584: pb->username, pb->author, NULL, NULL);
1.192 millert 585:
586: if ((pb->flags & INTERACTIVE) && (pb->rcs_msg[0] == '\0')) {
587: xfree(pb->rcs_msg); /* free empty log message */
588: pb->rcs_msg = NULL;
589: }
1.200 niallo 590:
1.63 niallo 591: return (0);
592: }
593:
594: /*
1.58 niallo 595: * checkin_init()
1.65 xsa 596: *
1.58 niallo 597: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 598: * On success, return 0. On error return -1.
1.58 niallo 599: */
1.63 niallo 600: static int
1.58 niallo 601: checkin_init(struct checkin_params *pb)
602: {
1.157 ray 603: BUF *bp;
1.201 xsa 604: char numb[RCS_REV_BUFSZ];
1.115 niallo 605: int fetchlog = 0;
1.128 niallo 606: struct stat st;
1.58 niallo 607:
1.115 niallo 608: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
1.147 deraadt 609: if (pb->newrev != NULL && RCSNUM_ZERO_ENDING(pb->newrev)) {
1.115 niallo 610: pb->frev = rcsnum_alloc();
611: rcsnum_cpy(pb->newrev, pb->frev, 0);
612: pb->newrev = rcsnum_inc(pb->newrev);
613: fetchlog = 1;
614: }
615:
1.73 xsa 616: /* Load file contents */
1.209 ray 617: if ((bp = buf_load(pb->filename)) == NULL)
1.200 niallo 618: return (-1);
1.58 niallo 619:
1.98 niallo 620: /* Get default values from working copy if -k specified */
621: if (pb->flags & CI_KEYWORDSCAN)
1.183 ray 622: checkin_keywordscan(bp, &pb->newrev,
1.180 niallo 623: &pb->date, &pb->state, &pb->author);
1.98 niallo 624:
1.148 niallo 625: if (pb->flags & CI_SKIPDESC)
1.142 joris 626: goto skipdesc;
627:
1.73 xsa 628: /* Get description from user */
1.185 ray 629: if (pb->description == NULL &&
630: rcs_set_description(pb->file, NULL) == -1) {
1.186 joris 631: warn("%s", pb->filename);
1.200 niallo 632: return (-1);
1.185 ray 633: }
1.142 joris 634:
635: skipdesc:
1.58 niallo 636:
1.115 niallo 637: /*
1.188 ray 638: * If the user had specified a zero-ending revision number e.g. 4.0
1.115 niallo 639: * emulate odd GNU behaviour and fetch log message.
640: */
641: if (fetchlog == 1) {
1.154 xsa 642: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
643: pb->flags);
1.115 niallo 644: rcsnum_free(pb->frev);
645: }
1.133 joris 646:
1.97 niallo 647: /*
648: * Set the date of the revision to be the last modification
649: * time of the working file if -d has no argument.
650: */
1.158 joris 651: if (pb->date == DATE_MTIME)
652: checkin_mtimedate(pb);
1.97 niallo 653:
1.73 xsa 654: /* Now add our new revision */
1.96 niallo 655: if (rcs_rev_add(pb->file,
656: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.107 deraadt 657: (pb->rcs_msg == NULL ? "Initial revision" : pb->rcs_msg),
1.103 niallo 658: pb->date, pb->author) != 0) {
1.155 xsa 659: warnx("failed to add new revision");
1.200 niallo 660: return (-1);
1.63 niallo 661: }
1.133 joris 662:
1.63 niallo 663: /*
664: * If we are checking in to a non-default (ie user-specified)
665: * revision, set head to this revision.
666: */
1.131 xsa 667: if (pb->newrev != NULL) {
668: if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160 xsa 669: errx(1, "rcs_head_set failed");
1.131 xsa 670: } else
1.63 niallo 671: pb->newrev = pb->file->rf_head;
672:
1.73 xsa 673: /* New head revision has to contain entire file; */
1.180 niallo 674: if (rcs_deltatext_set(pb->file, pb->file->rf_head, bp) == -1) {
1.155 xsa 675: warnx("failed to set new head revision");
1.200 niallo 676: return (-1);
1.58 niallo 677: }
1.133 joris 678:
1.73 xsa 679: /* Attach a symbolic name to this revision if specified. */
1.188 ray 680: if (pb->symbol != NULL && checkin_attach_symbol(pb) < 0)
1.200 niallo 681: return (-1);
1.66 niallo 682:
1.73 xsa 683: /* Set the state of this revision if specified. */
1.66 niallo 684: if (pb->state != NULL)
685: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
686:
1.128 niallo 687: /* Inherit RCSFILE permissions from file being checked in */
1.159 xsa 688: if (fstat(workfile_fd, &st) == -1)
1.160 xsa 689: err(1, "%s", pb->filename);
1.158 joris 690:
1.128 niallo 691: /* Strip all the write bits */
1.189 millert 692: pb->file->rf_mode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
1.128 niallo 693:
1.158 joris 694: (void)close(workfile_fd);
1.66 niallo 695: (void)unlink(pb->filename);
696:
1.128 niallo 697: /* Write out RCSFILE before calling checkout_rev() */
698: rcs_write(pb->file);
699:
1.73 xsa 700: /* Do checkout if -u or -l are specified. */
1.147 deraadt 701: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
702: !(pb->flags & CI_DEFAULT)) {
1.66 niallo 703: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 704: pb->username, pb->author, NULL, NULL);
1.128 niallo 705: }
706:
1.154 xsa 707: if (!(pb->flags & QUIET)) {
1.119 xsa 708: fprintf(stderr, "initial revision: %s\n",
709: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
710: }
1.93 xsa 711:
1.63 niallo 712: return (0);
1.58 niallo 713: }
714:
1.59 niallo 715: /*
716: * checkin_attach_symbol()
717: *
718: * Attempt to attach the specified symbol to the revision.
719: * On success, return 0. On error return -1.
720: */
1.58 niallo 721: static int
722: checkin_attach_symbol(struct checkin_params *pb)
723: {
1.201 xsa 724: char rbuf[RCS_REV_BUFSZ];
1.58 niallo 725: int ret;
1.154 xsa 726: if (!(pb->flags & QUIET))
1.58 niallo 727: printf("symbol: %s\n", pb->symbol);
1.130 xsa 728: if (pb->flags & CI_SYMFORCE) {
729: if (rcs_sym_remove(pb->file, pb->symbol) < 0) {
730: if (rcs_errno != RCS_ERR_NOENT) {
1.155 xsa 731: warnx("problem removing symbol: %s",
732: pb->symbol);
1.130 xsa 733: return (-1);
734: }
735: }
736: }
1.147 deraadt 737: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1) &&
738: (rcs_errno == RCS_ERR_DUPENT)) {
1.58 niallo 739: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
740: rbuf, sizeof(rbuf));
1.155 xsa 741: warnx("symbolic name %s already bound to %s", pb->symbol, rbuf);
1.58 niallo 742: return (-1);
743: } else if (ret == -1) {
1.155 xsa 744: warnx("problem adding symbol: %s", pb->symbol);
1.58 niallo 745: return (-1);
746: }
747: return (0);
748: }
749:
1.59 niallo 750: /*
751: * checkin_revert()
752: *
753: * If there are no differences between the working file and the latest revision
754: * and the -f flag is not specified, simply revert to the latest version and
755: * warn the user.
756: *
757: */
1.200 niallo 758: static int
1.58 niallo 759: checkin_revert(struct checkin_params *pb)
760: {
1.201 xsa 761: char rbuf[RCS_REV_BUFSZ];
1.58 niallo 762:
763: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.173 xsa 764:
765: if (!(pb->flags & QUIET))
766: (void)fprintf(stderr, "file is unchanged; reverting "
767: "to previous revision %s\n", rbuf);
768:
1.200 niallo 769: /* Attach a symbolic name to this revision if specified. */
770: if (pb->symbol != NULL) {
771: if (checkin_checklock(pb) == -1)
772: return (-1);
773:
774: pb->newrev = pb->frev;
775: if (checkin_attach_symbol(pb) == -1)
776: return (-1);
777: }
778:
1.137 niallo 779: pb->flags |= CO_REVERT;
1.158 joris 780: (void)close(workfile_fd);
1.58 niallo 781: (void)unlink(pb->filename);
1.200 niallo 782:
783: /* If needed, write out RCSFILE before calling checkout_rev() */
784: if (pb->symbol != NULL)
785: rcs_write(pb->file);
786:
1.58 niallo 787: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
788: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 789: pb->flags, pb->username, pb->author, NULL, NULL);
1.200 niallo 790:
791: return (0);
1.58 niallo 792: }
793:
1.59 niallo 794: /*
795: * checkin_checklock()
796: *
797: * Check for the existence of a lock on the file. If there are no locks, or it
798: * is not locked by the correct user, return -1. Otherwise, return 0.
799: */
1.58 niallo 800: static int
801: checkin_checklock(struct checkin_params *pb)
802: {
803: struct rcs_lock *lkp;
1.199 xsa 804:
805: if (rcs_lock_getmode(pb->file) == RCS_LOCK_LOOSE)
806: return (0);
1.58 niallo 807:
1.82 joris 808: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
1.147 deraadt 809: if (!strcmp(lkp->rl_name, pb->username) &&
810: !rcsnum_cmp(lkp->rl_num, pb->frev, 0))
1.82 joris 811: return (0);
1.58 niallo 812: }
1.32 joris 813:
1.155 xsa 814: warnx("%s: no lock set by %s", pb->file->rf_path, pb->username);
1.82 joris 815: return (-1);
1.61 niallo 816: }
817:
818: /*
1.62 niallo 819: * checkin_mtimedate()
1.61 niallo 820: *
821: * Set the date of the revision to be the last modification
1.62 niallo 822: * time of the working file.
1.61 niallo 823: */
1.158 joris 824: static void
1.62 niallo 825: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 826: {
827: struct stat sb;
1.158 joris 828:
1.159 xsa 829: if (fstat(workfile_fd, &sb) == -1)
1.160 xsa 830: err(1, "%s", pb->filename);
1.158 joris 831:
1.215 deraadt 832: pb->date = sb.st_mtimespec.tv_sec;
1.98 niallo 833: }
834:
835: /*
836: * checkin_keywordscan()
837: *
838: * Searches working file for keyword values to determine its revision
839: * number, creation date and author, and uses these values instead of
840: * calculating them locally.
841: *
842: * Params: The data buffer to scan and pointers to pointers of variables in
843: * which to store the outputs.
844: *
845: * On success, return 0. On error return -1.
846: */
847: static int
1.183 ray 848: checkin_keywordscan(BUF *data, RCSNUM **rev, time_t *date, char **author,
1.98 niallo 849: char **state)
850: {
1.183 ray 851: BUF *buf;
852: size_t left;
1.167 ray 853: u_int j;
1.183 ray 854: char *kwstr;
855: unsigned char *c, *end, *start;
1.98 niallo 856:
1.207 ray 857: end = buf_get(data) + buf_len(data) - 1;
1.167 ray 858: kwstr = NULL;
1.98 niallo 859:
1.207 ray 860: left = buf_len(data);
861: for (c = buf_get(data);
1.183 ray 862: c <= end && (c = memchr(c, '$', left)) != NULL;
863: left = end - c + 1) {
1.167 ray 864: size_t len;
1.98 niallo 865:
1.167 ray 866: start = c;
867: c++;
868: if (!isalpha(*c))
869: continue;
870:
871: /* look for any matching keywords */
872: for (j = 0; j < 10; j++) {
873: len = strlen(rcs_expkw[j].kw_str);
1.183 ray 874: if (left < len)
875: continue;
876: if (memcmp(c, rcs_expkw[j].kw_str, len) != 0) {
1.167 ray 877: kwstr = rcs_expkw[j].kw_str;
878: break;
1.98 niallo 879: }
1.167 ray 880: }
1.98 niallo 881:
1.167 ray 882: /* unknown keyword, continue looking */
883: if (kwstr == NULL)
884: continue;
885:
886: c += len;
1.183 ray 887: if (c > end) {
888: kwstr = NULL;
889: break;
890: }
891: if (*c != ':') {
892: kwstr = NULL;
1.167 ray 893: continue;
1.183 ray 894: }
1.167 ray 895:
896: /* Find end of line or end of keyword. */
1.183 ray 897: while (++c <= end) {
898: if (*c == '\n') {
899: /* Skip newline since it is definitely not `$'. */
900: ++c;
901: goto loopend;
902: }
903: if (*c == '$')
904: break;
905: }
1.167 ray 906:
1.183 ray 907: len = c - start + 1;
1.209 ray 908: buf = buf_alloc(len + 1);
1.207 ray 909: buf_append(buf, start, len);
1.183 ray 910:
911: /* XXX - Not binary safe. */
1.207 ray 912: buf_putc(buf, '\0');
913: checkin_parsekeyword(buf_get(buf), rev, date, author, state);
1.208 ray 914: buf_free(buf);
1.196 deraadt 915: loopend:;
1.98 niallo 916: }
1.167 ray 917: if (kwstr == NULL)
1.98 niallo 918: return (-1);
919: else
920: return (0);
921: }
922:
923: /*
924: * checkin_keywordtype()
925: *
926: * Given an RCS keyword string, determine what type of string it is.
927: * This enables us to know what data should be in it.
928: *
929: * Returns type on success, or -1 on failure.
1.107 deraadt 930: */
1.98 niallo 931: static int
932: checkin_keywordtype(char *keystring)
933: {
934: char *p;
1.107 deraadt 935:
1.98 niallo 936: p = keystring;
1.118 deraadt 937: p++;
1.214 guenther 938: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0 ||
939: strncmp(p, KW_OPENBSD, strlen(KW_OPENBSD)) == 0)
1.98 niallo 940: return (KW_TYPE_ID);
941: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
942: return (KW_TYPE_AUTHOR);
943: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
944: return (KW_TYPE_DATE);
945: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
946: return (KW_TYPE_STATE);
947: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
948: return (KW_TYPE_REVISION);
949: else
950: return (-1);
951: }
952:
953: /*
954: * checkin_parsekeyword()
955: *
956: * Do the actual parsing of an RCS keyword string, setting the values passed
957: * to the function to whatever is found.
958: *
1.183 ray 959: * XXX - Don't error out on malformed keywords.
1.98 niallo 960: */
961: static void
1.183 ray 962: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
1.98 niallo 963: char **author, char **state)
964: {
1.183 ray 965: char *tokens[KW_NUMTOKS_MAX], *p, *datestring;
1.99 niallo 966: int i = 0;
1.107 deraadt 967:
1.183 ray 968: for ((p = strtok(keystring, " ")); p; (p = strtok(NULL, " "))) {
969: if (i < KW_NUMTOKS_MAX - 1)
970: tokens[i++] = p;
971: else
972: break;
973: }
974:
1.107 deraadt 975: /* Parse data out of the expanded keyword */
1.98 niallo 976: switch (checkin_keywordtype(keystring)) {
977: case KW_TYPE_ID:
1.183 ray 978: if (i < 3)
979: break;
1.98 niallo 980: /* only parse revision if one is not already set */
981: if (*rev == NULL) {
982: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1.160 xsa 983: errx(1, "could not parse rcsnum");
1.98 niallo 984: }
1.183 ray 985:
986: if (i < 5)
987: break;
1.184 ray 988: (void)xasprintf(&datestring, "%s %s", tokens[3], tokens[4]);
1.210 ray 989: if ((*date = date_parse(datestring)) == -1)
1.166 ray 990: errx(1, "could not parse date");
1.98 niallo 991: xfree(datestring);
1.183 ray 992:
993: if (i < 6)
994: break;
995: if (*author != NULL)
996: xfree(*author);
997: *author = xstrdup(tokens[5]);
998:
999: if (i < 7)
1000: break;
1001: if (*state != NULL)
1002: xfree(*state);
1003: *state = xstrdup(tokens[6]);
1.98 niallo 1004: break;
1005: case KW_TYPE_AUTHOR:
1.183 ray 1006: if (i < 2)
1007: break;
1.98 niallo 1008: if (*author != NULL)
1009: xfree(*author);
1.135 ray 1010: *author = xstrdup(tokens[1]);
1.98 niallo 1011: break;
1012: case KW_TYPE_DATE:
1.183 ray 1013: if (i < 3)
1014: break;
1.184 ray 1015: (void)xasprintf(&datestring, "%s %s", tokens[1], tokens[2]);
1.210 ray 1016: if ((*date = date_parse(datestring)) == -1)
1.166 ray 1017: errx(1, "could not parse date");
1.98 niallo 1018: xfree(datestring);
1019: break;
1020: case KW_TYPE_STATE:
1.183 ray 1021: if (i < 2)
1022: break;
1.98 niallo 1023: if (*state != NULL)
1024: xfree(*state);
1.135 ray 1025: *state = xstrdup(tokens[1]);
1.98 niallo 1026: break;
1027: case KW_TYPE_REVISION:
1.183 ray 1028: if (i < 2)
1029: break;
1.98 niallo 1030: /* only parse revision if one is not already set */
1031: if (*rev != NULL)
1032: break;
1033: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1.160 xsa 1034: errx(1, "could not parse rcsnum");
1.98 niallo 1035: break;
1036: }
1.1 niallo 1037: }