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