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