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