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