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