Annotation of src/usr.bin/rcs/ci.c, Revision 1.173
1.173 ! xsa 1: /* $OpenBSD: ci.c,v 1.172 2006/05/10 01:10:23 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.31 niallo 202: case 'w':
1.151 ray 203: if (pb.author != NULL)
204: xfree(pb.author);
1.84 joris 205: pb.author = xstrdup(rcs_optarg);
1.64 xsa 206: break;
207: case 'x':
1.125 ray 208: /* Use blank extension if none given. */
209: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.110 joris 210: break;
211: case 'z':
212: timezone_flag = rcs_optarg;
1.31 niallo 213: break;
1.1 niallo 214: default:
215: (usage)();
216: exit(1);
217: }
218: }
219:
1.24 joris 220: argc -= rcs_optind;
221: argv += rcs_optind;
222:
1.1 niallo 223: if (argc == 0) {
1.155 xsa 224: warnx("no input file");
1.1 niallo 225: (usage)();
226: exit(1);
227: }
228:
1.86 xsa 229: if ((pb.username = getlogin()) == NULL)
1.160 xsa 230: err(1, "getlogin");
1.31 niallo 231:
1.1 niallo 232: for (i = 0; i < argc; i++) {
1.58 niallo 233: pb.filename = argv[i];
1.1 niallo 234:
1.158 joris 235: if ((workfile_fd = open(pb.filename, O_RDONLY)) == -1)
1.160 xsa 236: err(1, "%s", pb.filename);
1.158 joris 237:
1.58 niallo 238: /*
239: * Test for existence of ,v file. If we are expected to
240: * create one, set NEWFILE flag.
241: */
1.163 joris 242: fd = rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath),
243: pb.flags);
244:
245: if (fd < 0) {
1.71 niallo 246: if (pb.openflags & RCS_CREATE)
247: pb.flags |= NEWFILE;
248: else {
1.155 xsa 249: warnx("No existing RCS file");
1.71 niallo 250: status = 1;
1.158 joris 251: (void)close(workfile_fd);
1.71 niallo 252: continue;
253: }
1.66 niallo 254: } else {
255: if (pb.flags & CI_INIT) {
1.155 xsa 256: warnx("%s already exists", pb.fpath);
1.66 niallo 257: status = 1;
1.163 joris 258: (void)close(fd);
1.158 joris 259: (void)close(workfile_fd);
1.66 niallo 260: continue;
261: }
1.58 niallo 262: pb.openflags &= ~RCS_CREATE;
1.66 niallo 263: }
1.158 joris 264:
1.63 niallo 265: /*
266: * If we are to create a new ,v file, we must decide where it
267: * should go.
268: */
1.163 joris 269: if (pb.flags & NEWFILE)
270: fd = rcs_choosefile(pb.filename,
271: pb.fpath, sizeof(pb.fpath));
1.63 niallo 272:
1.163 joris 273: pb.file = rcs_open(pb.fpath, fd, pb.openflags, pb.fmode);
1.87 xsa 274: if (pb.file == NULL)
1.161 xsa 275: errx(1, "failed to open rcsfile `%s'", pb.fpath);
1.29 niallo 276:
1.157 ray 277: if (pb.flags & DESCRIPTION)
278: rcs_set_description(pb.file, pb.description);
279:
1.154 xsa 280: if (!(pb.flags & QUIET))
1.173 ! xsa 281: (void)fprintf(stderr,
! 282: "%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.173 ! xsa 318: (void)fprintf(stderr, "done\n");
1.93 xsa 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.172 ray 350: if ((b3 = rcs_buf_alloc(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.173 ! xsa 781:
! 782: if (!(pb->flags & QUIET))
! 783: (void)fprintf(stderr, "file is unchanged; reverting "
! 784: "to previous revision %s\n", rbuf);
! 785:
1.137 niallo 786: pb->flags |= CO_REVERT;
1.158 joris 787: (void)close(workfile_fd);
1.58 niallo 788: (void)unlink(pb->filename);
789: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
790: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 791: pb->flags, pb->username, pb->author, NULL, NULL);
1.58 niallo 792: }
793:
1.59 niallo 794: /*
795: * checkin_checklock()
796: *
797: * Check for the existence of a lock on the file. If there are no locks, or it
798: * is not locked by the correct user, return -1. Otherwise, return 0.
799: */
1.58 niallo 800: static int
801: checkin_checklock(struct checkin_params *pb)
802: {
803: struct rcs_lock *lkp;
804:
1.82 joris 805: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
1.147 deraadt 806: if (!strcmp(lkp->rl_name, pb->username) &&
807: !rcsnum_cmp(lkp->rl_num, pb->frev, 0))
1.82 joris 808: return (0);
1.58 niallo 809: }
1.32 joris 810:
1.155 xsa 811: warnx("%s: no lock set by %s", pb->file->rf_path, pb->username);
1.82 joris 812: return (-1);
1.61 niallo 813: }
814:
815: /*
1.62 niallo 816: * checkin_mtimedate()
1.61 niallo 817: *
818: * Set the date of the revision to be the last modification
1.62 niallo 819: * time of the working file.
1.61 niallo 820: */
1.158 joris 821: static void
1.62 niallo 822: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 823: {
824: struct stat sb;
1.158 joris 825:
1.159 xsa 826: if (fstat(workfile_fd, &sb) == -1)
1.160 xsa 827: err(1, "%s", pb->filename);
1.158 joris 828:
1.61 niallo 829: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.98 niallo 830: }
831:
832: /*
833: * checkin_keywordscan()
834: *
835: * Searches working file for keyword values to determine its revision
836: * number, creation date and author, and uses these values instead of
837: * calculating them locally.
838: *
839: * Params: The data buffer to scan and pointers to pointers of variables in
840: * which to store the outputs.
841: *
842: * On success, return 0. On error return -1.
843: */
844: static int
845: checkin_keywordscan(char *data, RCSNUM **rev, time_t *date, char **author,
846: char **state)
847: {
1.122 ray 848: size_t end;
1.167 ray 849: u_int j;
1.98 niallo 850: char *c, *kwstr, *start, buf[128];
851:
1.167 ray 852: kwstr = NULL;
1.98 niallo 853:
1.167 ray 854: for (c = data; (c = strchr(c, '$')) != NULL;) {
855: size_t len;
1.98 niallo 856:
1.167 ray 857: start = c;
858: c++;
859: if (!isalpha(*c))
860: continue;
861:
862: /* look for any matching keywords */
863: for (j = 0; j < 10; j++) {
864: len = strlen(rcs_expkw[j].kw_str);
865: if (strncmp(c, rcs_expkw[j].kw_str, len) != 0) {
866: kwstr = rcs_expkw[j].kw_str;
867: break;
1.98 niallo 868: }
1.167 ray 869: }
1.98 niallo 870:
1.167 ray 871: /* unknown keyword, continue looking */
872: if (kwstr == NULL)
873: continue;
874:
875: c += len;
876: if (*c != ':')
877: continue;
878:
879: /* Find end of line or end of keyword. */
880: c += strcspn(c, "$\n");
881: if (*c != '$')
882: continue;
883:
884: end = c - start + 2;
885: if (strlcpy(buf, start, end) >= end)
886: errx(1, "keyword buffer too small!");
887: checkin_parsekeyword(buf, rev, date, author, state);
1.98 niallo 888: }
1.167 ray 889: if (kwstr == NULL)
1.98 niallo 890: return (-1);
891: else
892: return (0);
893: }
894:
895: /*
896: * checkin_keywordtype()
897: *
898: * Given an RCS keyword string, determine what type of string it is.
899: * This enables us to know what data should be in it.
900: *
901: * Returns type on success, or -1 on failure.
1.107 deraadt 902: */
1.98 niallo 903: static int
904: checkin_keywordtype(char *keystring)
905: {
906: char *p;
1.107 deraadt 907:
1.98 niallo 908: p = keystring;
1.118 deraadt 909: p++;
1.98 niallo 910: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
911: return (KW_TYPE_ID);
912: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
913: return (KW_TYPE_AUTHOR);
914: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
915: return (KW_TYPE_DATE);
916: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
917: return (KW_TYPE_STATE);
918: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
919: return (KW_TYPE_REVISION);
920: else
921: return (-1);
922: }
923:
924: /*
925: * checkin_parsekeyword()
926: *
927: * Do the actual parsing of an RCS keyword string, setting the values passed
928: * to the function to whatever is found.
929: *
930: */
931: static void
932: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
933: char **author, char **state)
934: {
935: char *tokens[10], *p, *datestring;
936: size_t len = 0;
1.99 niallo 937: int i = 0;
1.107 deraadt 938:
939: /* Parse data out of the expanded keyword */
1.98 niallo 940: switch (checkin_keywordtype(keystring)) {
941: case KW_TYPE_ID:
1.101 niallo 942: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 943: (p = strtok(NULL, " "))) {
1.99 niallo 944: if (i < KW_NUMTOKS_ID - 1)
945: tokens[i++] = p;
1.107 deraadt 946: }
1.99 niallo 947: tokens[i] = NULL;
1.98 niallo 948: if (*author != NULL)
949: xfree(*author);
950: if (*state != NULL)
951: xfree(*state);
952: /* only parse revision if one is not already set */
953: if (*rev == NULL) {
954: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1.160 xsa 955: errx(1, "could not parse rcsnum");
1.98 niallo 956: }
1.135 ray 957: *author = xstrdup(tokens[5]);
1.146 pat 958: *state = xstrdup(tokens[6]);
1.98 niallo 959: len = strlen(tokens[3]) + strlen(tokens[4]) + 2;
960: datestring = xmalloc(len);
1.166 ray 961: if (strlcpy(datestring, tokens[3], len) >= len ||
962: strlcat(datestring, " ", len) >= len ||
963: strlcat(datestring, tokens[4], len) >= len)
964: errx(1, "date too long");
1.162 joris 965: if ((*date = rcs_date_parse(datestring)) <= 0)
1.166 ray 966: errx(1, "could not parse date");
1.98 niallo 967: xfree(datestring);
968: break;
969: case KW_TYPE_AUTHOR:
1.101 niallo 970: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 971: (p = strtok(NULL, " "))) {
1.99 niallo 972: if (i < KW_NUMTOKS_AUTHOR - 1)
973: tokens[i++] = p;
1.107 deraadt 974: }
1.98 niallo 975: if (*author != NULL)
976: xfree(*author);
1.135 ray 977: *author = xstrdup(tokens[1]);
1.98 niallo 978: break;
979: case KW_TYPE_DATE:
1.101 niallo 980: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 981: (p = strtok(NULL, " "))) {
1.99 niallo 982: if (i < KW_NUMTOKS_DATE - 1)
983: tokens[i++] = p;
1.98 niallo 984: }
985: len = strlen(tokens[1]) + strlen(tokens[2]) + 2;
986: datestring = xmalloc(len);
1.166 ray 987: if (strlcpy(datestring, tokens[1], len) >= len ||
988: strlcat(datestring, " ", len) >= len ||
989: strlcat(datestring, tokens[2], len) >= len)
990: errx(1, "date too long");
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.101 niallo 996: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 997: (p = strtok(NULL, " "))) {
1.99 niallo 998: if (i < KW_NUMTOKS_STATE - 1)
999: tokens[i++] = p;
1.107 deraadt 1000: }
1.98 niallo 1001: if (*state != NULL)
1002: xfree(*state);
1.135 ray 1003: *state = xstrdup(tokens[1]);
1.98 niallo 1004: break;
1005: case KW_TYPE_REVISION:
1006: /* only parse revision if one is not already set */
1007: if (*rev != NULL)
1008: break;
1.102 niallo 1009: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1010: (p = strtok(NULL, " "))) {
1.99 niallo 1011: if (i < KW_NUMTOKS_REVISION - 1)
1012: tokens[i++] = p;
1.107 deraadt 1013: }
1.98 niallo 1014: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1.160 xsa 1015: errx(1, "could not parse rcsnum");
1.98 niallo 1016: break;
1017: }
1.1 niallo 1018: }