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