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