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