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