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