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