Annotation of src/usr.bin/rcs/ci.c, Revision 1.192
1.192 ! millert 1: /* $OpenBSD: ci.c,v 1.191 2006/12/23 21:08:01 krw 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.190 niallo 322: * Returns pointer to a BUF on success, NULL on failure.
1.59 niallo 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.191 krw 480: errx(1, "%s: Date %s precedes %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.189 millert 502: if (pb->flags & INTERACTIVE) {
503: if (pb->rcs_msg != NULL) {
504: fprintf(stderr,
505: "reuse log message of previous file? [yn](y): ");
506: if (rcs_yesno('y') != 'y') {
507: xfree(pb->rcs_msg);
508: pb->rcs_msg = NULL;
509: }
510: }
511: if (pb->rcs_msg == NULL)
512: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
513: pb->flags);
514: }
1.63 niallo 515:
1.82 joris 516: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63 niallo 517: if (rcs_errno != RCS_ERR_NOENT)
1.155 xsa 518: warnx("failed to remove lock");
1.82 joris 519: else if (!(pb->flags & CO_LOCK))
1.155 xsa 520: warnx("previous revision was not locked; "
1.82 joris 521: "ignoring -l option");
1.63 niallo 522: }
523:
1.73 xsa 524: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 525: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
1.160 xsa 526: errx(1, "failed to set new rd_text for head rev");
1.63 niallo 527:
1.73 xsa 528: /* Now add our new revision */
1.63 niallo 529: if (rcs_rev_add(pb->file,
530: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 531: pb->rcs_msg, pb->date, pb->author) != 0) {
1.155 xsa 532: warnx("failed to add new revision");
1.126 niallo 533: goto fail;
1.63 niallo 534: }
535:
536: /*
537: * If we are checking in to a non-default (ie user-specified)
538: * revision, set head to this revision.
539: */
1.131 xsa 540: if (pb->newrev != NULL) {
541: if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160 xsa 542: errx(1, "rcs_head_set failed");
1.131 xsa 543: } else
1.63 niallo 544: pb->newrev = pb->file->rf_head;
545:
1.73 xsa 546: /* New head revision has to contain entire file; */
1.180 niallo 547: if (rcs_deltatext_set(pb->file, pb->frev, bp) == -1)
1.160 xsa 548: errx(1, "failed to set new head revision");
1.63 niallo 549:
1.73 xsa 550: /* Attach a symbolic name to this revision if specified. */
1.147 deraadt 551: if (pb->symbol != NULL &&
552: (checkin_attach_symbol(pb) < 0))
1.126 niallo 553: goto fail;
1.63 niallo 554:
1.73 xsa 555: /* Set the state of this revision if specified. */
1.63 niallo 556: if (pb->state != NULL)
557: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
558:
1.128 niallo 559: /* Maintain RCSFILE permissions */
1.159 xsa 560: if (fstat(workfile_fd, &st) == -1)
1.160 xsa 561: err(1, "%s", pb->filename);
1.128 niallo 562:
563: /* Strip all the write bits */
1.189 millert 564: pb->file->rf_mode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
1.128 niallo 565:
1.158 joris 566: (void)close(workfile_fd);
1.63 niallo 567: (void)unlink(pb->filename);
1.140 deraadt 568:
1.128 niallo 569: /* Write out RCSFILE before calling checkout_rev() */
570: rcs_write(pb->file);
1.63 niallo 571:
1.73 xsa 572: /* Do checkout if -u or -l are specified. */
1.147 deraadt 573: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
574: !(pb->flags & CI_DEFAULT))
1.63 niallo 575: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 576: pb->username, pb->author, NULL, NULL);
1.192 ! millert 577:
! 578: if ((pb->flags & INTERACTIVE) && (pb->rcs_msg[0] == '\0')) {
! 579: xfree(pb->rcs_msg); /* free empty log message */
! 580: pb->rcs_msg = NULL;
! 581: }
1.169 ray 582: out:
1.63 niallo 583: return (0);
1.126 niallo 584:
585: fail:
586: return (-1);
1.63 niallo 587: }
588:
589: /*
1.58 niallo 590: * checkin_init()
1.65 xsa 591: *
1.58 niallo 592: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 593: * On success, return 0. On error return -1.
1.58 niallo 594: */
1.63 niallo 595: static int
1.58 niallo 596: checkin_init(struct checkin_params *pb)
597: {
1.157 ray 598: BUF *bp;
1.180 niallo 599: char numb[64];
1.115 niallo 600: int fetchlog = 0;
1.128 niallo 601: struct stat st;
1.58 niallo 602:
1.115 niallo 603: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
1.147 deraadt 604: if (pb->newrev != NULL && RCSNUM_ZERO_ENDING(pb->newrev)) {
1.115 niallo 605: pb->frev = rcsnum_alloc();
606: rcsnum_cpy(pb->newrev, pb->frev, 0);
607: pb->newrev = rcsnum_inc(pb->newrev);
608: fetchlog = 1;
609: }
610:
1.73 xsa 611: /* Load file contents */
1.162 joris 612: if ((bp = rcs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL)
1.149 ray 613: goto fail;
1.58 niallo 614:
1.98 niallo 615: /* Get default values from working copy if -k specified */
616: if (pb->flags & CI_KEYWORDSCAN)
1.183 ray 617: checkin_keywordscan(bp, &pb->newrev,
1.180 niallo 618: &pb->date, &pb->state, &pb->author);
1.98 niallo 619:
1.148 niallo 620: if (pb->flags & CI_SKIPDESC)
1.142 joris 621: goto skipdesc;
622:
1.73 xsa 623: /* Get description from user */
1.185 ray 624: if (pb->description == NULL &&
625: rcs_set_description(pb->file, NULL) == -1) {
1.186 joris 626: warn("%s", pb->filename);
1.185 ray 627: goto fail;
628: }
1.142 joris 629:
630: skipdesc:
1.58 niallo 631:
1.115 niallo 632: /*
1.188 ray 633: * If the user had specified a zero-ending revision number e.g. 4.0
1.115 niallo 634: * emulate odd GNU behaviour and fetch log message.
635: */
636: if (fetchlog == 1) {
1.154 xsa 637: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
638: pb->flags);
1.115 niallo 639: rcsnum_free(pb->frev);
640: }
1.133 joris 641:
1.97 niallo 642: /*
643: * Set the date of the revision to be the last modification
644: * time of the working file if -d has no argument.
645: */
1.158 joris 646: if (pb->date == DATE_MTIME)
647: checkin_mtimedate(pb);
1.97 niallo 648:
1.73 xsa 649: /* Now add our new revision */
1.96 niallo 650: if (rcs_rev_add(pb->file,
651: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.107 deraadt 652: (pb->rcs_msg == NULL ? "Initial revision" : pb->rcs_msg),
1.103 niallo 653: pb->date, pb->author) != 0) {
1.155 xsa 654: warnx("failed to add new revision");
1.126 niallo 655: goto fail;
1.63 niallo 656: }
1.133 joris 657:
1.63 niallo 658: /*
659: * If we are checking in to a non-default (ie user-specified)
660: * revision, set head to this revision.
661: */
1.131 xsa 662: if (pb->newrev != NULL) {
663: if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160 xsa 664: errx(1, "rcs_head_set failed");
1.131 xsa 665: } else
1.63 niallo 666: pb->newrev = pb->file->rf_head;
667:
1.73 xsa 668: /* New head revision has to contain entire file; */
1.180 niallo 669: if (rcs_deltatext_set(pb->file, pb->file->rf_head, bp) == -1) {
1.155 xsa 670: warnx("failed to set new head revision");
1.126 niallo 671: goto fail;
1.58 niallo 672: }
1.133 joris 673:
1.73 xsa 674: /* Attach a symbolic name to this revision if specified. */
1.188 ray 675: if (pb->symbol != NULL && checkin_attach_symbol(pb) < 0)
1.126 niallo 676: goto fail;
1.66 niallo 677:
1.73 xsa 678: /* Set the state of this revision if specified. */
1.66 niallo 679: if (pb->state != NULL)
680: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
681:
1.128 niallo 682: /* Inherit RCSFILE permissions from file being checked in */
1.159 xsa 683: if (fstat(workfile_fd, &st) == -1)
1.160 xsa 684: err(1, "%s", pb->filename);
1.158 joris 685:
1.128 niallo 686: /* Strip all the write bits */
1.189 millert 687: pb->file->rf_mode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
1.128 niallo 688:
1.158 joris 689: (void)close(workfile_fd);
1.66 niallo 690: (void)unlink(pb->filename);
691:
1.128 niallo 692: /* Write out RCSFILE before calling checkout_rev() */
693: rcs_write(pb->file);
694:
1.73 xsa 695: /* Do checkout if -u or -l are specified. */
1.147 deraadt 696: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
697: !(pb->flags & CI_DEFAULT)) {
1.66 niallo 698: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 699: pb->username, pb->author, NULL, NULL);
1.128 niallo 700: }
701:
1.154 xsa 702: if (!(pb->flags & QUIET)) {
1.119 xsa 703: fprintf(stderr, "initial revision: %s\n",
704: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
705: }
1.93 xsa 706:
1.63 niallo 707: return (0);
1.126 niallo 708: fail:
709: return (-1);
1.58 niallo 710: }
711:
1.59 niallo 712: /*
713: * checkin_attach_symbol()
714: *
715: * Attempt to attach the specified symbol to the revision.
716: * On success, return 0. On error return -1.
717: */
1.58 niallo 718: static int
719: checkin_attach_symbol(struct checkin_params *pb)
720: {
721: char rbuf[16];
722: int ret;
1.154 xsa 723: if (!(pb->flags & QUIET))
1.58 niallo 724: printf("symbol: %s\n", pb->symbol);
1.130 xsa 725: if (pb->flags & CI_SYMFORCE) {
726: if (rcs_sym_remove(pb->file, pb->symbol) < 0) {
727: if (rcs_errno != RCS_ERR_NOENT) {
1.155 xsa 728: warnx("problem removing symbol: %s",
729: pb->symbol);
1.130 xsa 730: return (-1);
731: }
732: }
733: }
1.147 deraadt 734: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1) &&
735: (rcs_errno == RCS_ERR_DUPENT)) {
1.58 niallo 736: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
737: rbuf, sizeof(rbuf));
1.155 xsa 738: warnx("symbolic name %s already bound to %s", pb->symbol, rbuf);
1.58 niallo 739: return (-1);
740: } else if (ret == -1) {
1.155 xsa 741: warnx("problem adding symbol: %s", pb->symbol);
1.58 niallo 742: return (-1);
743: }
744: return (0);
745: }
746:
1.59 niallo 747: /*
748: * checkin_revert()
749: *
750: * If there are no differences between the working file and the latest revision
751: * and the -f flag is not specified, simply revert to the latest version and
752: * warn the user.
753: *
754: */
1.58 niallo 755: static void
756: checkin_revert(struct checkin_params *pb)
757: {
758: char rbuf[16];
759:
760: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.173 xsa 761:
762: if (!(pb->flags & QUIET))
763: (void)fprintf(stderr, "file is unchanged; reverting "
764: "to previous revision %s\n", rbuf);
765:
1.137 niallo 766: pb->flags |= CO_REVERT;
1.158 joris 767: (void)close(workfile_fd);
1.58 niallo 768: (void)unlink(pb->filename);
769: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
770: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 771: pb->flags, pb->username, pb->author, NULL, NULL);
1.58 niallo 772: }
773:
1.59 niallo 774: /*
775: * checkin_checklock()
776: *
777: * Check for the existence of a lock on the file. If there are no locks, or it
778: * is not locked by the correct user, return -1. Otherwise, return 0.
779: */
1.58 niallo 780: static int
781: checkin_checklock(struct checkin_params *pb)
782: {
783: struct rcs_lock *lkp;
784:
1.82 joris 785: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
1.147 deraadt 786: if (!strcmp(lkp->rl_name, pb->username) &&
787: !rcsnum_cmp(lkp->rl_num, pb->frev, 0))
1.82 joris 788: return (0);
1.58 niallo 789: }
1.32 joris 790:
1.155 xsa 791: warnx("%s: no lock set by %s", pb->file->rf_path, pb->username);
1.82 joris 792: return (-1);
1.61 niallo 793: }
794:
795: /*
1.62 niallo 796: * checkin_mtimedate()
1.61 niallo 797: *
798: * Set the date of the revision to be the last modification
1.62 niallo 799: * time of the working file.
1.61 niallo 800: */
1.158 joris 801: static void
1.62 niallo 802: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 803: {
804: struct stat sb;
1.158 joris 805:
1.159 xsa 806: if (fstat(workfile_fd, &sb) == -1)
1.160 xsa 807: err(1, "%s", pb->filename);
1.158 joris 808:
1.61 niallo 809: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.98 niallo 810: }
811:
812: /*
813: * checkin_keywordscan()
814: *
815: * Searches working file for keyword values to determine its revision
816: * number, creation date and author, and uses these values instead of
817: * calculating them locally.
818: *
819: * Params: The data buffer to scan and pointers to pointers of variables in
820: * which to store the outputs.
821: *
822: * On success, return 0. On error return -1.
823: */
824: static int
1.183 ray 825: checkin_keywordscan(BUF *data, RCSNUM **rev, time_t *date, char **author,
1.98 niallo 826: char **state)
827: {
1.183 ray 828: BUF *buf;
829: size_t left;
1.167 ray 830: u_int j;
1.183 ray 831: char *kwstr;
832: unsigned char *c, *end, *start;
1.98 niallo 833:
1.183 ray 834: end = rcs_buf_get(data) + rcs_buf_len(data) - 1;
1.167 ray 835: kwstr = NULL;
1.98 niallo 836:
1.183 ray 837: left = rcs_buf_len(data);
838: for (c = rcs_buf_get(data);
839: c <= end && (c = memchr(c, '$', left)) != NULL;
840: left = end - c + 1) {
1.167 ray 841: size_t len;
1.98 niallo 842:
1.167 ray 843: start = c;
844: c++;
845: if (!isalpha(*c))
846: continue;
847:
848: /* look for any matching keywords */
849: for (j = 0; j < 10; j++) {
850: len = strlen(rcs_expkw[j].kw_str);
1.183 ray 851: if (left < len)
852: continue;
853: if (memcmp(c, rcs_expkw[j].kw_str, len) != 0) {
1.167 ray 854: kwstr = rcs_expkw[j].kw_str;
855: break;
1.98 niallo 856: }
1.167 ray 857: }
1.98 niallo 858:
1.167 ray 859: /* unknown keyword, continue looking */
860: if (kwstr == NULL)
861: continue;
862:
863: c += len;
1.183 ray 864: if (c > end) {
865: kwstr = NULL;
866: break;
867: }
868: if (*c != ':') {
869: kwstr = NULL;
1.167 ray 870: continue;
1.183 ray 871: }
1.167 ray 872:
873: /* Find end of line or end of keyword. */
1.183 ray 874: while (++c <= end) {
875: if (*c == '\n') {
876: /* Skip newline since it is definitely not `$'. */
877: ++c;
878: goto loopend;
879: }
880: if (*c == '$')
881: break;
882: }
1.167 ray 883:
1.183 ray 884: len = c - start + 1;
885: buf = rcs_buf_alloc(len + 1, 0);
886: rcs_buf_append(buf, start, len);
887:
888: /* XXX - Not binary safe. */
889: rcs_buf_putc(buf, '\0');
890: checkin_parsekeyword(rcs_buf_get(buf), rev, date, author, state);
891: loopend:
1.98 niallo 892: }
1.167 ray 893: if (kwstr == NULL)
1.98 niallo 894: return (-1);
895: else
896: return (0);
897: }
898:
899: /*
900: * checkin_keywordtype()
901: *
902: * Given an RCS keyword string, determine what type of string it is.
903: * This enables us to know what data should be in it.
904: *
905: * Returns type on success, or -1 on failure.
1.107 deraadt 906: */
1.98 niallo 907: static int
908: checkin_keywordtype(char *keystring)
909: {
910: char *p;
1.107 deraadt 911:
1.98 niallo 912: p = keystring;
1.118 deraadt 913: p++;
1.98 niallo 914: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
915: return (KW_TYPE_ID);
916: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
917: return (KW_TYPE_AUTHOR);
918: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
919: return (KW_TYPE_DATE);
920: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
921: return (KW_TYPE_STATE);
922: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
923: return (KW_TYPE_REVISION);
924: else
925: return (-1);
926: }
927:
928: /*
929: * checkin_parsekeyword()
930: *
931: * Do the actual parsing of an RCS keyword string, setting the values passed
932: * to the function to whatever is found.
933: *
1.183 ray 934: * XXX - Don't error out on malformed keywords.
1.98 niallo 935: */
936: static void
1.183 ray 937: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
1.98 niallo 938: char **author, char **state)
939: {
1.183 ray 940: char *tokens[KW_NUMTOKS_MAX], *p, *datestring;
1.99 niallo 941: int i = 0;
1.107 deraadt 942:
1.183 ray 943: for ((p = strtok(keystring, " ")); p; (p = strtok(NULL, " "))) {
944: if (i < KW_NUMTOKS_MAX - 1)
945: tokens[i++] = p;
946: else
947: break;
948: }
949:
1.107 deraadt 950: /* Parse data out of the expanded keyword */
1.98 niallo 951: switch (checkin_keywordtype(keystring)) {
952: case KW_TYPE_ID:
1.183 ray 953: if (i < 3)
954: break;
1.98 niallo 955: /* only parse revision if one is not already set */
956: if (*rev == NULL) {
957: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1.160 xsa 958: errx(1, "could not parse rcsnum");
1.98 niallo 959: }
1.183 ray 960:
961: if (i < 5)
962: break;
1.184 ray 963: (void)xasprintf(&datestring, "%s %s", tokens[3], tokens[4]);
964: if ((*date = rcs_date_parse(datestring)) <= 0)
1.166 ray 965: errx(1, "could not parse date");
1.98 niallo 966: xfree(datestring);
1.183 ray 967:
968: if (i < 6)
969: break;
970: if (*author != NULL)
971: xfree(*author);
972: *author = xstrdup(tokens[5]);
973:
974: if (i < 7)
975: break;
976: if (*state != NULL)
977: xfree(*state);
978: *state = xstrdup(tokens[6]);
1.98 niallo 979: break;
980: case KW_TYPE_AUTHOR:
1.183 ray 981: if (i < 2)
982: break;
1.98 niallo 983: if (*author != NULL)
984: xfree(*author);
1.135 ray 985: *author = xstrdup(tokens[1]);
1.98 niallo 986: break;
987: case KW_TYPE_DATE:
1.183 ray 988: if (i < 3)
989: break;
1.184 ray 990: (void)xasprintf(&datestring, "%s %s", tokens[1], tokens[2]);
1.162 joris 991: if ((*date = rcs_date_parse(datestring)) <= 0)
1.166 ray 992: errx(1, "could not parse date");
1.98 niallo 993: xfree(datestring);
994: break;
995: case KW_TYPE_STATE:
1.183 ray 996: if (i < 2)
997: break;
1.98 niallo 998: if (*state != NULL)
999: xfree(*state);
1.135 ray 1000: *state = xstrdup(tokens[1]);
1.98 niallo 1001: break;
1002: case KW_TYPE_REVISION:
1.183 ray 1003: if (i < 2)
1004: break;
1.98 niallo 1005: /* only parse revision if one is not already set */
1006: if (*rev != NULL)
1007: break;
1008: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1.160 xsa 1009: errx(1, "could not parse rcsnum");
1.98 niallo 1010: break;
1011: }
1.1 niallo 1012: }