Annotation of src/usr.bin/rcs/ci.c, Revision 1.202
1.202 ! ray 1: /* $OpenBSD: ci.c,v 1.201 2007/06/30 08:23:49 xsa 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 *);
1.200 niallo 95: static int 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.198 sobrado 102: " [-j[rev]] [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg]\n"
103: " [-Nsymbol] [-nsymbol] [-r[rev]] [-sstate] [-tstr]\n"
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.201 xsa 347: char rbuf[RCS_REV_BUFSZ];
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.202 ! ray 380: if (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.201 xsa 408: char *rcs_msg, nrev[RCS_REV_BUFSZ], prev[RCS_REV_BUFSZ];
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.201 xsa 442: char numb1[RCS_REV_BUFSZ], numb2[RCS_REV_BUFSZ];
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.200 niallo 455: return (-1);
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.200 niallo 462: return (-1);
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.200 niallo 474: return (-1);
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.200 niallo 507: return (-1);
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.200 niallo 515: if (checkin_revert(pb) == -1)
516: return (-1);
517: else
518: return (0);
1.63 niallo 519: }
520:
1.73 xsa 521: /* If no log message specified, get it interactively. */
1.189 millert 522: if (pb->flags & INTERACTIVE) {
523: if (pb->rcs_msg != NULL) {
524: fprintf(stderr,
525: "reuse log message of previous file? [yn](y): ");
526: if (rcs_yesno('y') != 'y') {
527: xfree(pb->rcs_msg);
528: pb->rcs_msg = NULL;
529: }
530: }
531: if (pb->rcs_msg == NULL)
532: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
533: pb->flags);
534: }
1.63 niallo 535:
1.199 xsa 536: if ((rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) &&
537: (rcs_lock_getmode(pb->file) != RCS_LOCK_LOOSE)) {
1.63 niallo 538: if (rcs_errno != RCS_ERR_NOENT)
1.155 xsa 539: warnx("failed to remove lock");
1.82 joris 540: else if (!(pb->flags & CO_LOCK))
1.155 xsa 541: warnx("previous revision was not locked; "
1.82 joris 542: "ignoring -l option");
1.63 niallo 543: }
544:
1.73 xsa 545: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 546: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
1.160 xsa 547: errx(1, "failed to set new rd_text for head rev");
1.63 niallo 548:
1.73 xsa 549: /* Now add our new revision */
1.63 niallo 550: if (rcs_rev_add(pb->file,
551: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 552: pb->rcs_msg, pb->date, pb->author) != 0) {
1.155 xsa 553: warnx("failed to add new revision");
1.200 niallo 554: return (-1);
1.63 niallo 555: }
556:
557: /*
558: * If we are checking in to a non-default (ie user-specified)
559: * revision, set head to this revision.
560: */
1.131 xsa 561: if (pb->newrev != NULL) {
562: if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160 xsa 563: errx(1, "rcs_head_set failed");
1.131 xsa 564: } else
1.63 niallo 565: pb->newrev = pb->file->rf_head;
566:
1.73 xsa 567: /* New head revision has to contain entire file; */
1.180 niallo 568: if (rcs_deltatext_set(pb->file, pb->frev, bp) == -1)
1.160 xsa 569: errx(1, "failed to set new head revision");
1.63 niallo 570:
1.73 xsa 571: /* Attach a symbolic name to this revision if specified. */
1.147 deraadt 572: if (pb->symbol != NULL &&
573: (checkin_attach_symbol(pb) < 0))
1.200 niallo 574: return (-1);
1.63 niallo 575:
1.73 xsa 576: /* Set the state of this revision if specified. */
1.63 niallo 577: if (pb->state != NULL)
578: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
579:
1.128 niallo 580: /* Maintain RCSFILE permissions */
1.159 xsa 581: if (fstat(workfile_fd, &st) == -1)
1.160 xsa 582: err(1, "%s", pb->filename);
1.128 niallo 583:
584: /* Strip all the write bits */
1.189 millert 585: pb->file->rf_mode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
1.128 niallo 586:
1.158 joris 587: (void)close(workfile_fd);
1.63 niallo 588: (void)unlink(pb->filename);
1.140 deraadt 589:
1.128 niallo 590: /* Write out RCSFILE before calling checkout_rev() */
591: rcs_write(pb->file);
1.63 niallo 592:
1.73 xsa 593: /* Do checkout if -u or -l are specified. */
1.147 deraadt 594: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
595: !(pb->flags & CI_DEFAULT))
1.63 niallo 596: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 597: pb->username, pb->author, NULL, NULL);
1.192 millert 598:
599: if ((pb->flags & INTERACTIVE) && (pb->rcs_msg[0] == '\0')) {
600: xfree(pb->rcs_msg); /* free empty log message */
601: pb->rcs_msg = NULL;
602: }
1.200 niallo 603:
1.63 niallo 604: return (0);
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.201 xsa 617: char numb[RCS_REV_BUFSZ];
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.200 niallo 631: return (-1);
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.200 niallo 645: return (-1);
1.185 ray 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.200 niallo 673: return (-1);
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.200 niallo 689: return (-1);
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.200 niallo 694: return (-1);
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.58 niallo 726: }
727:
1.59 niallo 728: /*
729: * checkin_attach_symbol()
730: *
731: * Attempt to attach the specified symbol to the revision.
732: * On success, return 0. On error return -1.
733: */
1.58 niallo 734: static int
735: checkin_attach_symbol(struct checkin_params *pb)
736: {
1.201 xsa 737: char rbuf[RCS_REV_BUFSZ];
1.58 niallo 738: int ret;
1.154 xsa 739: if (!(pb->flags & QUIET))
1.58 niallo 740: printf("symbol: %s\n", pb->symbol);
1.130 xsa 741: if (pb->flags & CI_SYMFORCE) {
742: if (rcs_sym_remove(pb->file, pb->symbol) < 0) {
743: if (rcs_errno != RCS_ERR_NOENT) {
1.155 xsa 744: warnx("problem removing symbol: %s",
745: pb->symbol);
1.130 xsa 746: return (-1);
747: }
748: }
749: }
1.147 deraadt 750: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1) &&
751: (rcs_errno == RCS_ERR_DUPENT)) {
1.58 niallo 752: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
753: rbuf, sizeof(rbuf));
1.155 xsa 754: warnx("symbolic name %s already bound to %s", pb->symbol, rbuf);
1.58 niallo 755: return (-1);
756: } else if (ret == -1) {
1.155 xsa 757: warnx("problem adding symbol: %s", pb->symbol);
1.58 niallo 758: return (-1);
759: }
760: return (0);
761: }
762:
1.59 niallo 763: /*
764: * checkin_revert()
765: *
766: * If there are no differences between the working file and the latest revision
767: * and the -f flag is not specified, simply revert to the latest version and
768: * warn the user.
769: *
770: */
1.200 niallo 771: static int
1.58 niallo 772: checkin_revert(struct checkin_params *pb)
773: {
1.201 xsa 774: char rbuf[RCS_REV_BUFSZ];
1.58 niallo 775:
776: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.173 xsa 777:
778: if (!(pb->flags & QUIET))
779: (void)fprintf(stderr, "file is unchanged; reverting "
780: "to previous revision %s\n", rbuf);
781:
1.200 niallo 782: /* Attach a symbolic name to this revision if specified. */
783: if (pb->symbol != NULL) {
784: if (checkin_checklock(pb) == -1)
785: return (-1);
786:
787: pb->newrev = pb->frev;
788: if (checkin_attach_symbol(pb) == -1)
789: return (-1);
790: }
791:
1.137 niallo 792: pb->flags |= CO_REVERT;
1.158 joris 793: (void)close(workfile_fd);
1.58 niallo 794: (void)unlink(pb->filename);
1.200 niallo 795:
796: /* If needed, write out RCSFILE before calling checkout_rev() */
797: if (pb->symbol != NULL)
798: rcs_write(pb->file);
799:
1.58 niallo 800: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
801: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 802: pb->flags, pb->username, pb->author, NULL, NULL);
1.200 niallo 803:
804: return (0);
1.58 niallo 805: }
806:
1.59 niallo 807: /*
808: * checkin_checklock()
809: *
810: * Check for the existence of a lock on the file. If there are no locks, or it
811: * is not locked by the correct user, return -1. Otherwise, return 0.
812: */
1.58 niallo 813: static int
814: checkin_checklock(struct checkin_params *pb)
815: {
816: struct rcs_lock *lkp;
1.199 xsa 817:
818: if (rcs_lock_getmode(pb->file) == RCS_LOCK_LOOSE)
819: return (0);
1.58 niallo 820:
1.82 joris 821: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
1.147 deraadt 822: if (!strcmp(lkp->rl_name, pb->username) &&
823: !rcsnum_cmp(lkp->rl_num, pb->frev, 0))
1.82 joris 824: return (0);
1.58 niallo 825: }
1.32 joris 826:
1.155 xsa 827: warnx("%s: no lock set by %s", pb->file->rf_path, pb->username);
1.82 joris 828: return (-1);
1.61 niallo 829: }
830:
831: /*
1.62 niallo 832: * checkin_mtimedate()
1.61 niallo 833: *
834: * Set the date of the revision to be the last modification
1.62 niallo 835: * time of the working file.
1.61 niallo 836: */
1.158 joris 837: static void
1.62 niallo 838: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 839: {
840: struct stat sb;
1.158 joris 841:
1.159 xsa 842: if (fstat(workfile_fd, &sb) == -1)
1.160 xsa 843: err(1, "%s", pb->filename);
1.158 joris 844:
1.61 niallo 845: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.98 niallo 846: }
847:
848: /*
849: * checkin_keywordscan()
850: *
851: * Searches working file for keyword values to determine its revision
852: * number, creation date and author, and uses these values instead of
853: * calculating them locally.
854: *
855: * Params: The data buffer to scan and pointers to pointers of variables in
856: * which to store the outputs.
857: *
858: * On success, return 0. On error return -1.
859: */
860: static int
1.183 ray 861: checkin_keywordscan(BUF *data, RCSNUM **rev, time_t *date, char **author,
1.98 niallo 862: char **state)
863: {
1.183 ray 864: BUF *buf;
865: size_t left;
1.167 ray 866: u_int j;
1.183 ray 867: char *kwstr;
868: unsigned char *c, *end, *start;
1.98 niallo 869:
1.183 ray 870: end = rcs_buf_get(data) + rcs_buf_len(data) - 1;
1.167 ray 871: kwstr = NULL;
1.98 niallo 872:
1.183 ray 873: left = rcs_buf_len(data);
874: for (c = rcs_buf_get(data);
875: c <= end && (c = memchr(c, '$', left)) != NULL;
876: left = end - c + 1) {
1.167 ray 877: size_t len;
1.98 niallo 878:
1.167 ray 879: start = c;
880: c++;
881: if (!isalpha(*c))
882: continue;
883:
884: /* look for any matching keywords */
885: for (j = 0; j < 10; j++) {
886: len = strlen(rcs_expkw[j].kw_str);
1.183 ray 887: if (left < len)
888: continue;
889: if (memcmp(c, rcs_expkw[j].kw_str, len) != 0) {
1.167 ray 890: kwstr = rcs_expkw[j].kw_str;
891: break;
1.98 niallo 892: }
1.167 ray 893: }
1.98 niallo 894:
1.167 ray 895: /* unknown keyword, continue looking */
896: if (kwstr == NULL)
897: continue;
898:
899: c += len;
1.183 ray 900: if (c > end) {
901: kwstr = NULL;
902: break;
903: }
904: if (*c != ':') {
905: kwstr = NULL;
1.167 ray 906: continue;
1.183 ray 907: }
1.167 ray 908:
909: /* Find end of line or end of keyword. */
1.183 ray 910: while (++c <= end) {
911: if (*c == '\n') {
912: /* Skip newline since it is definitely not `$'. */
913: ++c;
914: goto loopend;
915: }
916: if (*c == '$')
917: break;
918: }
1.167 ray 919:
1.183 ray 920: len = c - start + 1;
921: buf = rcs_buf_alloc(len + 1, 0);
922: rcs_buf_append(buf, start, len);
923:
924: /* XXX - Not binary safe. */
925: rcs_buf_putc(buf, '\0');
926: checkin_parsekeyword(rcs_buf_get(buf), rev, date, author, state);
1.196 deraadt 927: loopend:;
1.98 niallo 928: }
1.167 ray 929: if (kwstr == NULL)
1.98 niallo 930: return (-1);
931: else
932: return (0);
933: }
934:
935: /*
936: * checkin_keywordtype()
937: *
938: * Given an RCS keyword string, determine what type of string it is.
939: * This enables us to know what data should be in it.
940: *
941: * Returns type on success, or -1 on failure.
1.107 deraadt 942: */
1.98 niallo 943: static int
944: checkin_keywordtype(char *keystring)
945: {
946: char *p;
1.107 deraadt 947:
1.98 niallo 948: p = keystring;
1.118 deraadt 949: p++;
1.98 niallo 950: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
951: return (KW_TYPE_ID);
952: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
953: return (KW_TYPE_AUTHOR);
954: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
955: return (KW_TYPE_DATE);
956: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
957: return (KW_TYPE_STATE);
958: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
959: return (KW_TYPE_REVISION);
960: else
961: return (-1);
962: }
963:
964: /*
965: * checkin_parsekeyword()
966: *
967: * Do the actual parsing of an RCS keyword string, setting the values passed
968: * to the function to whatever is found.
969: *
1.183 ray 970: * XXX - Don't error out on malformed keywords.
1.98 niallo 971: */
972: static void
1.183 ray 973: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
1.98 niallo 974: char **author, char **state)
975: {
1.183 ray 976: char *tokens[KW_NUMTOKS_MAX], *p, *datestring;
1.99 niallo 977: int i = 0;
1.107 deraadt 978:
1.183 ray 979: for ((p = strtok(keystring, " ")); p; (p = strtok(NULL, " "))) {
980: if (i < KW_NUMTOKS_MAX - 1)
981: tokens[i++] = p;
982: else
983: break;
984: }
985:
1.107 deraadt 986: /* Parse data out of the expanded keyword */
1.98 niallo 987: switch (checkin_keywordtype(keystring)) {
988: case KW_TYPE_ID:
1.183 ray 989: if (i < 3)
990: break;
1.98 niallo 991: /* only parse revision if one is not already set */
992: if (*rev == NULL) {
993: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1.160 xsa 994: errx(1, "could not parse rcsnum");
1.98 niallo 995: }
1.183 ray 996:
997: if (i < 5)
998: break;
1.184 ray 999: (void)xasprintf(&datestring, "%s %s", tokens[3], tokens[4]);
1000: if ((*date = rcs_date_parse(datestring)) <= 0)
1.166 ray 1001: errx(1, "could not parse date");
1.98 niallo 1002: xfree(datestring);
1.183 ray 1003:
1004: if (i < 6)
1005: break;
1006: if (*author != NULL)
1007: xfree(*author);
1008: *author = xstrdup(tokens[5]);
1009:
1010: if (i < 7)
1011: break;
1012: if (*state != NULL)
1013: xfree(*state);
1014: *state = xstrdup(tokens[6]);
1.98 niallo 1015: break;
1016: case KW_TYPE_AUTHOR:
1.183 ray 1017: if (i < 2)
1018: break;
1.98 niallo 1019: if (*author != NULL)
1020: xfree(*author);
1.135 ray 1021: *author = xstrdup(tokens[1]);
1.98 niallo 1022: break;
1023: case KW_TYPE_DATE:
1.183 ray 1024: if (i < 3)
1025: break;
1.184 ray 1026: (void)xasprintf(&datestring, "%s %s", tokens[1], tokens[2]);
1.162 joris 1027: if ((*date = rcs_date_parse(datestring)) <= 0)
1.166 ray 1028: errx(1, "could not parse date");
1.98 niallo 1029: xfree(datestring);
1030: break;
1031: case KW_TYPE_STATE:
1.183 ray 1032: if (i < 2)
1033: break;
1.98 niallo 1034: if (*state != NULL)
1035: xfree(*state);
1.135 ray 1036: *state = xstrdup(tokens[1]);
1.98 niallo 1037: break;
1038: case KW_TYPE_REVISION:
1.183 ray 1039: if (i < 2)
1040: break;
1.98 niallo 1041: /* only parse revision if one is not already set */
1042: if (*rev != NULL)
1043: break;
1044: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1.160 xsa 1045: errx(1, "could not parse rcsnum");
1.98 niallo 1046: break;
1047: }
1.1 niallo 1048: }