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