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