Annotation of src/usr.bin/rcs/ci.c, Revision 1.166
1.166 ! ray 1: /* $OpenBSD: ci.c,v 1.165 2006/04/29 05:10:16 ray Exp $ */
1.1 niallo 2: /*
1.98 niallo 3: * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org>
1.1 niallo 4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
1.143 niallo 7: * modification, are permitted provided that the following conditions
1.1 niallo 8: * are met:
9: *
1.143 niallo 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 niallo 12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.91 xsa 27: #include "includes.h"
1.1 niallo 28:
1.92 xsa 29: #include "rcsprog.h"
1.4 niallo 30: #include "diff.h"
1.9 niallo 31:
1.157 ray 32: #define CI_OPTSTRING "d::f::I::i::j::k::l::M::m::N:n:qr::s:Tt::u::Vw:x::z::"
1.107 deraadt 33: #define DATE_NOW -1
34: #define DATE_MTIME -2
35:
36: #define KW_ID "Id"
37: #define KW_AUTHOR "Author"
38: #define KW_DATE "Date"
39: #define KW_STATE "State"
40: #define KW_REVISION "Revision"
41:
42: #define KW_TYPE_ID 1
43: #define KW_TYPE_AUTHOR 2
44: #define KW_TYPE_DATE 3
45: #define KW_TYPE_STATE 4
46: #define KW_TYPE_REVISION 5
47:
48: #define KW_NUMTOKS_ID 10
49: #define KW_NUMTOKS_AUTHOR 3
50: #define KW_NUMTOKS_DATE 4
51: #define KW_NUMTOKS_STATE 3
52: #define KW_NUMTOKS_REVISION 3
1.58 niallo 53:
1.115 niallo 54: #define RCSNUM_ZERO_ENDING(x) (x->rn_id[x->rn_len - 1] == 0)
55:
1.98 niallo 56: extern struct rcs_kw rcs_expkw[];
57:
1.158 joris 58: static int workfile_fd;
59:
1.58 niallo 60: struct checkin_params {
61: int flags, openflags;
62: mode_t fmode;
63: time_t date;
64: RCSFILE *file;
65: RCSNUM *frev, *newrev;
66: char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.151 ray 67: char *author, *description, *state, *symbol;
1.58 niallo 68: };
69:
1.94 niallo 70: static int checkin_attach_symbol(struct checkin_params *);
71: static int checkin_checklock(struct checkin_params *);
1.68 xsa 72: static char *checkin_diff_file(struct checkin_params *);
1.154 xsa 73: static char *checkin_getlogmsg(RCSNUM *, RCSNUM *, int);
1.68 xsa 74: static int checkin_init(struct checkin_params *);
1.107 deraadt 75: static int checkin_keywordscan(char *, RCSNUM **, time_t *, char **,
1.98 niallo 76: char **);
1.107 deraadt 77: static int checkin_keywordtype(char *);
1.158 joris 78: static void checkin_mtimedate(struct checkin_params *);
1.107 deraadt 79: static void checkin_parsekeyword(char *, RCSNUM **, time_t *, char **,
1.98 niallo 80: char **);
1.94 niallo 81: static int checkin_update(struct checkin_params *);
82: static void checkin_revert(struct checkin_params *);
1.4 niallo 83:
1.1 niallo 84: void
85: checkin_usage(void)
86: {
87: fprintf(stderr,
1.119 xsa 88: "usage: ci [-jMNqV] [-d[date]] [-f[rev]] [-I[rev]] [-i[rev]]\n"
89: " [-j[rev]] [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg]\n"
1.165 ray 90: " [-Nsymbol] [-nsymbol] [-r[rev]] [-sstate] [-tstr]\n"
1.119 xsa 91: " [-u[rev]] [-wusername] [-xsuffixes] [-ztz] file ...\n");
1.58 niallo 92: }
93:
1.1 niallo 94: /*
95: * checkin_main()
96: *
97: * Handler for the `ci' program.
98: * Returns 0 on success, or >0 on error.
99: */
100: int
101: checkin_main(int argc, char **argv)
102: {
1.163 joris 103: int fd;
1.58 niallo 104: int i, ch, status;
1.139 ray 105: char *rev_str;
1.58 niallo 106: struct checkin_params pb;
1.1 niallo 107:
1.58 niallo 108: pb.date = DATE_NOW;
109: pb.file = NULL;
1.98 niallo 110: pb.rcs_msg = pb.username = pb.author = pb.state = NULL;
1.134 niallo 111: pb.symbol = pb.description = pb.deltatext = NULL;
1.58 niallo 112: pb.newrev = NULL;
1.128 niallo 113: pb.flags = status = 0;
114: pb.fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.120 joris 115: pb.flags = INTERACTIVE;
1.90 niallo 116: pb.openflags = RCS_RDWR|RCS_CREATE|RCS_PARSE_FULLY;
1.139 ray 117: rev_str = NULL;
1.9 niallo 118:
1.58 niallo 119: while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1 niallo 120: switch (ch) {
1.20 niallo 121: case 'd':
1.25 niallo 122: if (rcs_optarg == NULL)
1.58 niallo 123: pb.date = DATE_MTIME;
1.162 joris 124: else if ((pb.date = rcs_date_parse(rcs_optarg)) <= 0)
1.160 xsa 125: errx(1, "invalid date");
1.20 niallo 126: break;
1.29 niallo 127: case 'f':
1.139 ray 128: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 129: pb.flags |= FORCE;
1.29 niallo 130: break;
1.119 xsa 131: case 'I':
1.139 ray 132: rcs_setrevstr(&rev_str, rcs_optarg);
1.119 xsa 133: pb.flags |= INTERACTIVE;
134: break;
1.58 niallo 135: case 'i':
1.139 ray 136: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 137: pb.openflags |= RCS_CREATE;
1.66 niallo 138: pb.flags |= CI_INIT;
1.58 niallo 139: break;
140: case 'j':
1.139 ray 141: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 142: pb.openflags &= ~RCS_CREATE;
1.66 niallo 143: pb.flags &= ~CI_INIT;
1.58 niallo 144: break;
1.98 niallo 145: case 'k':
1.139 ray 146: rcs_setrevstr(&rev_str, rcs_optarg);
1.98 niallo 147: pb.flags |= CI_KEYWORDSCAN;
148: break;
1.30 niallo 149: case 'l':
1.139 ray 150: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 151: pb.flags |= CO_LOCK;
1.54 niallo 152: break;
153: case 'M':
1.139 ray 154: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 155: pb.flags |= CO_REVDATE;
1.30 niallo 156: break;
1.1 niallo 157: case 'm':
1.58 niallo 158: pb.rcs_msg = rcs_optarg;
1.87 xsa 159: if (pb.rcs_msg == NULL)
1.160 xsa 160: errx(1, "missing message for -m option");
1.58 niallo 161: pb.flags &= ~INTERACTIVE;
1.3 joris 162: break;
1.42 niallo 163: case 'N':
1.58 niallo 164: pb.flags |= CI_SYMFORCE;
1.152 ray 165: /* FALLTHROUGH */
1.38 niallo 166: case 'n':
1.151 ray 167: if (pb.symbol != NULL)
168: xfree(pb.symbol);
1.84 joris 169: pb.symbol = xstrdup(rcs_optarg);
1.87 xsa 170: if (rcs_sym_check(pb.symbol) != 1)
1.160 xsa 171: errx(1, "invalid symbol `%s'", pb.symbol);
1.38 niallo 172: break;
1.3 joris 173: case 'q':
1.154 xsa 174: pb.flags |= QUIET;
1.1 niallo 175: break;
1.30 niallo 176: case 'r':
1.139 ray 177: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 178: pb.flags |= CI_DEFAULT;
1.9 niallo 179: break;
1.51 niallo 180: case 's':
1.58 niallo 181: pb.state = rcs_optarg;
1.87 xsa 182: if (rcs_state_check(pb.state) < 0)
1.160 xsa 183: errx(1, "invalid state `%s'", pb.state);
1.67 xsa 184: break;
185: case 'T':
186: pb.flags |= PRESERVETIME;
1.51 niallo 187: break;
1.80 niallo 188: case 't':
1.157 ray 189: /* Ignore bare -t; kept for backwards compatibility. */
190: if (rcs_optarg == NULL)
191: break;
192: pb.description = rcs_optarg;
193: pb.flags |= DESCRIPTION;
1.80 niallo 194: break;
1.9 niallo 195: case 'u':
1.139 ray 196: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 197: pb.flags |= CO_UNLOCK;
1.9 niallo 198: break;
1.30 niallo 199: case 'V':
200: printf("%s\n", rcs_version);
201: exit(0);
1.121 ray 202: /* NOTREACHED */
1.31 niallo 203: case 'w':
1.151 ray 204: if (pb.author != NULL)
205: xfree(pb.author);
1.84 joris 206: pb.author = xstrdup(rcs_optarg);
1.64 xsa 207: break;
208: case 'x':
1.125 ray 209: /* Use blank extension if none given. */
210: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.110 joris 211: break;
212: case 'z':
213: timezone_flag = rcs_optarg;
1.31 niallo 214: break;
1.1 niallo 215: default:
216: (usage)();
217: exit(1);
218: }
219: }
220:
1.24 joris 221: argc -= rcs_optind;
222: argv += rcs_optind;
223:
1.1 niallo 224: if (argc == 0) {
1.155 xsa 225: warnx("no input file");
1.1 niallo 226: (usage)();
227: exit(1);
228: }
229:
1.86 xsa 230: if ((pb.username = getlogin()) == NULL)
1.160 xsa 231: err(1, "getlogin");
1.31 niallo 232:
1.1 niallo 233: for (i = 0; i < argc; i++) {
1.58 niallo 234: pb.filename = argv[i];
1.1 niallo 235:
1.158 joris 236: if ((workfile_fd = open(pb.filename, O_RDONLY)) == -1)
1.160 xsa 237: err(1, "%s", pb.filename);
1.158 joris 238:
1.58 niallo 239: /*
240: * Test for existence of ,v file. If we are expected to
241: * create one, set NEWFILE flag.
242: */
1.163 joris 243: fd = rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath),
244: pb.flags);
245:
246: if (fd < 0) {
1.71 niallo 247: if (pb.openflags & RCS_CREATE)
248: pb.flags |= NEWFILE;
249: else {
1.155 xsa 250: warnx("No existing RCS file");
1.71 niallo 251: status = 1;
1.158 joris 252: (void)close(workfile_fd);
1.71 niallo 253: continue;
254: }
1.66 niallo 255: } else {
256: if (pb.flags & CI_INIT) {
1.155 xsa 257: warnx("%s already exists", pb.fpath);
1.66 niallo 258: status = 1;
1.163 joris 259: (void)close(fd);
1.158 joris 260: (void)close(workfile_fd);
1.66 niallo 261: continue;
262: }
1.58 niallo 263: pb.openflags &= ~RCS_CREATE;
1.66 niallo 264: }
1.158 joris 265:
1.63 niallo 266: /*
267: * If we are to create a new ,v file, we must decide where it
268: * should go.
269: */
1.163 joris 270: if (pb.flags & NEWFILE)
271: fd = rcs_choosefile(pb.filename,
272: pb.fpath, sizeof(pb.fpath));
1.63 niallo 273:
1.163 joris 274: pb.file = rcs_open(pb.fpath, fd, pb.openflags, pb.fmode);
1.87 xsa 275: if (pb.file == NULL)
1.161 xsa 276: errx(1, "failed to open rcsfile `%s'", pb.fpath);
1.29 niallo 277:
1.157 ray 278: if (pb.flags & DESCRIPTION)
279: rcs_set_description(pb.file, pb.description);
280:
1.154 xsa 281: if (!(pb.flags & QUIET))
1.58 niallo 282: printf("%s <-- %s\n", pb.fpath, pb.filename);
1.139 ray 283:
284: /* XXX - Should we rcsnum_free(pb.newrev)? */
285: if (rev_str != NULL)
1.141 ray 286: if ((pb.newrev = rcs_getrevnum(rev_str, pb.file)) ==
287: NULL)
1.160 xsa 288: errx(1, "invalid revision: %s", rev_str);
1.107 deraadt 289:
1.148 niallo 290: if (!(pb.flags & NEWFILE))
291: pb.flags |= CI_SKIPDESC;
1.158 joris 292:
1.142 joris 293: /* XXX - support for commiting to a file without revisions */
294: if (pb.file->rf_ndelta == 0) {
295: pb.flags |= NEWFILE;
296: pb.file->rf_flags |= RCS_CREATE;
297: }
298:
1.158 joris 299: /*
300: * workfile_fd will be closed in checkin_init or
301: * checkin_update
302: */
1.149 ray 303: if (pb.flags & NEWFILE) {
304: if (checkin_init(&pb) == -1)
305: status = 1;
306: } else {
307: if (checkin_update(&pb) == -1)
308: status = 1;
309: }
1.114 niallo 310:
311: /* reset NEWFILE flag */
312: pb.flags &= ~NEWFILE;
1.129 niallo 313:
314: rcs_close(pb.file);
1.4 niallo 315: }
316:
1.154 xsa 317: if (!(pb.flags & QUIET) && status == 0)
1.93 xsa 318: printf("done\n");
319:
1.16 niallo 320: return (status);
1.4 niallo 321: }
322:
1.59 niallo 323: /*
324: * checkin_diff_file()
325: *
1.65 xsa 326: * Generate the diff between the working file and a revision.
1.59 niallo 327: * Returns pointer to a char array on success, NULL on failure.
328: */
1.4 niallo 329: static char *
1.58 niallo 330: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 331: {
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.166 ! ray 355: if (strlcpy(path1, rcs_tmpdir, sizeof(path1)) >= sizeof(path1) ||
! 356: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1)) >= sizeof(path1))
! 357: errx(1, "path truncated");
1.162 joris 358: rcs_buf_write_stmp(b1, path1, 0600);
1.112 joris 359:
1.162 joris 360: rcs_buf_free(b1);
1.112 joris 361: b1 = NULL;
1.4 niallo 362:
1.166 ! ray 363: if (strlcpy(path2, rcs_tmpdir, sizeof(path2)) >= sizeof(path2) ||
! 364: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2)) >= sizeof(path2))
! 365: errx(1, "path truncated");
1.162 joris 366: rcs_buf_write_stmp(b2, path2, 0600);
1.112 joris 367:
1.162 joris 368: rcs_buf_free(b2);
1.112 joris 369: b2 = NULL;
1.4 niallo 370:
1.5 niallo 371: diff_format = D_RCSDIFF;
1.162 joris 372: rcs_diffreg(path1, path2, b3);
1.4 niallo 373:
1.162 joris 374: rcs_buf_putc(b3, '\0');
375: deltatext = (char *)rcs_buf_release(b3);
1.112 joris 376: b3 = NULL;
377:
378: out:
379: if (b1 != NULL)
1.162 joris 380: rcs_buf_free(b1);
1.112 joris 381: if (b2 != NULL)
1.162 joris 382: rcs_buf_free(b2);
1.112 joris 383: if (b3 != NULL)
1.162 joris 384: rcs_buf_free(b3);
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.126 niallo 511: goto fail;
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.84 joris 570: xfree(pb->deltatext);
571: xfree(filec);
1.158 joris 572: (void)close(workfile_fd);
1.63 niallo 573: (void)unlink(pb->filename);
1.140 deraadt 574:
1.128 niallo 575: /* Write out RCSFILE before calling checkout_rev() */
576: rcs_write(pb->file);
1.63 niallo 577:
1.73 xsa 578: /* Do checkout if -u or -l are specified. */
1.147 deraadt 579: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
580: !(pb->flags & CI_DEFAULT))
1.63 niallo 581: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 582: pb->username, pb->author, NULL, NULL);
1.63 niallo 583:
584: if (pb->flags & INTERACTIVE) {
1.84 joris 585: xfree(pb->rcs_msg);
1.63 niallo 586: pb->rcs_msg = NULL;
587: }
588: return (0);
1.126 niallo 589:
590: fail:
1.149 ray 591: if (filec != NULL)
592: xfree(filec);
1.134 niallo 593: if (pb->deltatext != NULL)
594: xfree(pb->deltatext);
1.126 niallo 595: return (-1);
1.63 niallo 596: }
597:
598: /*
1.58 niallo 599: * checkin_init()
1.65 xsa 600: *
1.58 niallo 601: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 602: * On success, return 0. On error return -1.
1.58 niallo 603: */
1.63 niallo 604: static int
1.58 niallo 605: checkin_init(struct checkin_params *pb)
606: {
1.157 ray 607: BUF *bp;
1.93 xsa 608: char *filec, numb[64];
1.115 niallo 609: int fetchlog = 0;
1.128 niallo 610: struct stat st;
1.58 niallo 611:
1.149 ray 612: filec = NULL;
613:
1.115 niallo 614: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
1.147 deraadt 615: if (pb->newrev != NULL && RCSNUM_ZERO_ENDING(pb->newrev)) {
1.115 niallo 616: pb->frev = rcsnum_alloc();
617: rcsnum_cpy(pb->newrev, pb->frev, 0);
618: pb->newrev = rcsnum_inc(pb->newrev);
619: fetchlog = 1;
620: }
621:
1.73 xsa 622: /* Load file contents */
1.162 joris 623: if ((bp = rcs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL)
1.149 ray 624: goto fail;
1.58 niallo 625:
1.162 joris 626: rcs_buf_putc(bp, '\0');
627: filec = (char *)rcs_buf_release(bp);
1.58 niallo 628:
1.98 niallo 629: /* Get default values from working copy if -k specified */
630: if (pb->flags & CI_KEYWORDSCAN)
631: checkin_keywordscan(filec, &pb->newrev, &pb->date, &pb->state,
632: &pb->author);
633:
1.148 niallo 634: if (pb->flags & CI_SKIPDESC)
1.142 joris 635: goto skipdesc;
636:
1.73 xsa 637: /* Get description from user */
1.80 niallo 638: if (pb->description == NULL)
1.157 ray 639: rcs_set_description(pb->file, NULL);
1.142 joris 640:
641: skipdesc:
1.58 niallo 642:
1.115 niallo 643: /*
644: * If the user had specified a zero-ending revision number e.g. 4
645: * emulate odd GNU behaviour and fetch log message.
646: */
647: if (fetchlog == 1) {
1.154 xsa 648: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev,
649: pb->flags);
1.115 niallo 650: rcsnum_free(pb->frev);
651: }
1.133 joris 652:
1.97 niallo 653: /*
654: * Set the date of the revision to be the last modification
655: * time of the working file if -d has no argument.
656: */
1.158 joris 657: if (pb->date == DATE_MTIME)
658: checkin_mtimedate(pb);
1.97 niallo 659:
1.73 xsa 660: /* Now add our new revision */
1.96 niallo 661: if (rcs_rev_add(pb->file,
662: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.107 deraadt 663: (pb->rcs_msg == NULL ? "Initial revision" : pb->rcs_msg),
1.103 niallo 664: pb->date, pb->author) != 0) {
1.155 xsa 665: warnx("failed to add new revision");
1.126 niallo 666: goto fail;
1.63 niallo 667: }
1.133 joris 668:
1.63 niallo 669: /*
670: * If we are checking in to a non-default (ie user-specified)
671: * revision, set head to this revision.
672: */
1.131 xsa 673: if (pb->newrev != NULL) {
674: if (rcs_head_set(pb->file, pb->newrev) < 0)
1.160 xsa 675: errx(1, "rcs_head_set failed");
1.131 xsa 676: } else
1.63 niallo 677: pb->newrev = pb->file->rf_head;
678:
1.73 xsa 679: /* New head revision has to contain entire file; */
1.66 niallo 680: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.155 xsa 681: warnx("failed to set new head revision");
1.126 niallo 682: goto fail;
1.58 niallo 683: }
1.133 joris 684:
1.73 xsa 685: /* Attach a symbolic name to this revision if specified. */
1.147 deraadt 686: if (pb->symbol != NULL &&
687: (checkin_attach_symbol(pb) < 0))
1.126 niallo 688: goto fail;
1.66 niallo 689:
1.73 xsa 690: /* Set the state of this revision if specified. */
1.66 niallo 691: if (pb->state != NULL)
692: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
693:
1.128 niallo 694: /* Inherit RCSFILE permissions from file being checked in */
1.159 xsa 695: if (fstat(workfile_fd, &st) == -1)
1.160 xsa 696: err(1, "%s", pb->filename);
1.158 joris 697:
1.128 niallo 698: /* Strip all the write bits */
699: pb->file->rf_mode = st.st_mode &
700: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
701:
1.84 joris 702: xfree(filec);
1.158 joris 703: (void)close(workfile_fd);
1.66 niallo 704: (void)unlink(pb->filename);
705:
1.128 niallo 706: /* Write out RCSFILE before calling checkout_rev() */
707: rcs_write(pb->file);
708:
1.73 xsa 709: /* Do checkout if -u or -l are specified. */
1.147 deraadt 710: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK)) &&
711: !(pb->flags & CI_DEFAULT)) {
1.66 niallo 712: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 713: pb->username, pb->author, NULL, NULL);
1.128 niallo 714: }
715:
1.154 xsa 716: if (!(pb->flags & QUIET)) {
1.119 xsa 717: fprintf(stderr, "initial revision: %s\n",
718: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
719: }
1.93 xsa 720:
1.63 niallo 721: return (0);
1.126 niallo 722: fail:
1.149 ray 723: if (filec != NULL)
724: xfree(filec);
1.126 niallo 725: return (-1);
1.58 niallo 726: }
727:
1.59 niallo 728: /*
729: * checkin_attach_symbol()
730: *
731: * Attempt to attach the specified symbol to the revision.
732: * On success, return 0. On error return -1.
733: */
1.58 niallo 734: static int
735: checkin_attach_symbol(struct checkin_params *pb)
736: {
737: char rbuf[16];
738: int ret;
1.154 xsa 739: if (!(pb->flags & QUIET))
1.58 niallo 740: printf("symbol: %s\n", pb->symbol);
1.130 xsa 741: if (pb->flags & CI_SYMFORCE) {
742: if (rcs_sym_remove(pb->file, pb->symbol) < 0) {
743: if (rcs_errno != RCS_ERR_NOENT) {
1.155 xsa 744: warnx("problem removing symbol: %s",
745: pb->symbol);
1.130 xsa 746: return (-1);
747: }
748: }
749: }
1.147 deraadt 750: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1) &&
751: (rcs_errno == RCS_ERR_DUPENT)) {
1.58 niallo 752: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
753: rbuf, sizeof(rbuf));
1.155 xsa 754: warnx("symbolic name %s already bound to %s", pb->symbol, rbuf);
1.58 niallo 755: return (-1);
756: } else if (ret == -1) {
1.155 xsa 757: warnx("problem adding symbol: %s", pb->symbol);
1.58 niallo 758: return (-1);
759: }
760: return (0);
761: }
762:
1.59 niallo 763: /*
764: * checkin_revert()
765: *
766: * If there are no differences between the working file and the latest revision
767: * and the -f flag is not specified, simply revert to the latest version and
768: * warn the user.
769: *
770: */
1.58 niallo 771: static void
772: checkin_revert(struct checkin_params *pb)
773: {
774: char rbuf[16];
775:
776: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.155 xsa 777: warnx("file is unchanged; reverting to previous revision %s", rbuf);
1.137 niallo 778: pb->flags |= CO_REVERT;
1.158 joris 779: (void)close(workfile_fd);
1.58 niallo 780: (void)unlink(pb->filename);
781: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
782: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 783: pb->flags, pb->username, pb->author, NULL, NULL);
1.127 xsa 784: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0)
785: if (rcs_errno != RCS_ERR_NOENT)
1.155 xsa 786: warnx("failed to remove lock");
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;
844: u_int j, found;
1.98 niallo 845: char *c, *kwstr, *start, buf[128];
846:
847: c = start = kwstr = NULL;
848:
1.122 ray 849: found = 0;
1.98 niallo 850:
1.122 ray 851: for (c = data; *c != '\0'; c++) {
1.98 niallo 852: if (*c == '$') {
853: start = c;
1.118 deraadt 854: c++;
1.98 niallo 855: if (!isalpha(*c)) {
856: c = start;
857: continue;
858: }
859: /* look for any matching keywords */
1.107 deraadt 860: found = 0;
861: for (j = 0; j < 10; j++) {
862: if (!strncmp(c, rcs_expkw[j].kw_str,
863: strlen(rcs_expkw[j].kw_str))) {
864: found = 1;
865: kwstr = rcs_expkw[j].kw_str;
866: break;
867: }
868: }
869:
870: /* unknown keyword, continue looking */
871: if (found == 0) {
872: c = start;
873: continue;
874: }
1.98 niallo 875:
876: c += strlen(kwstr);
877: if (*c != ':' && *c != '$') {
878: c = start;
879: continue;
880: }
881:
882: if (*c == ':') {
883: while (*c++) {
884: if (*c == '$') {
885: end = c - start + 2;
1.166 ! ray 886: if (strlcpy(buf, start, end) >= end)
1.160 xsa 887: errx(1, "keyword buffer"
1.98 niallo 888: " too small!");
889: checkin_parsekeyword(buf, rev,
1.107 deraadt 890: date, author, state);
1.98 niallo 891: break;
892: }
893: }
894:
895: if (*c != '$') {
896: c = start;
897: continue;
898: }
899: }
900: }
901: }
902: if (found == 0)
903: return (-1);
904: else
905: return (0);
906: }
907:
908: /*
909: * checkin_keywordtype()
910: *
911: * Given an RCS keyword string, determine what type of string it is.
912: * This enables us to know what data should be in it.
913: *
914: * Returns type on success, or -1 on failure.
1.107 deraadt 915: */
1.98 niallo 916: static int
917: checkin_keywordtype(char *keystring)
918: {
919: char *p;
1.107 deraadt 920:
1.98 niallo 921: p = keystring;
1.118 deraadt 922: p++;
1.98 niallo 923: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
924: return (KW_TYPE_ID);
925: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
926: return (KW_TYPE_AUTHOR);
927: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
928: return (KW_TYPE_DATE);
929: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
930: return (KW_TYPE_STATE);
931: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
932: return (KW_TYPE_REVISION);
933: else
934: return (-1);
935: }
936:
937: /*
938: * checkin_parsekeyword()
939: *
940: * Do the actual parsing of an RCS keyword string, setting the values passed
941: * to the function to whatever is found.
942: *
943: */
944: static void
945: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
946: char **author, char **state)
947: {
948: char *tokens[10], *p, *datestring;
949: size_t len = 0;
1.99 niallo 950: int i = 0;
1.107 deraadt 951:
952: /* Parse data out of the expanded keyword */
1.98 niallo 953: switch (checkin_keywordtype(keystring)) {
954: case KW_TYPE_ID:
1.101 niallo 955: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 956: (p = strtok(NULL, " "))) {
1.99 niallo 957: if (i < KW_NUMTOKS_ID - 1)
958: tokens[i++] = p;
1.107 deraadt 959: }
1.99 niallo 960: tokens[i] = NULL;
1.98 niallo 961: if (*author != NULL)
962: xfree(*author);
963: if (*state != NULL)
964: xfree(*state);
965: /* only parse revision if one is not already set */
966: if (*rev == NULL) {
967: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1.160 xsa 968: errx(1, "could not parse rcsnum");
1.98 niallo 969: }
1.135 ray 970: *author = xstrdup(tokens[5]);
1.146 pat 971: *state = xstrdup(tokens[6]);
1.98 niallo 972: len = strlen(tokens[3]) + strlen(tokens[4]) + 2;
973: datestring = xmalloc(len);
1.166 ! ray 974: if (strlcpy(datestring, tokens[3], len) >= len ||
! 975: strlcat(datestring, " ", len) >= len ||
! 976: strlcat(datestring, tokens[4], len) >= len)
! 977: errx(1, "date too long");
1.162 joris 978: if ((*date = rcs_date_parse(datestring)) <= 0)
1.166 ! ray 979: errx(1, "could not parse date");
1.98 niallo 980: xfree(datestring);
981: break;
982: case KW_TYPE_AUTHOR:
1.101 niallo 983: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 984: (p = strtok(NULL, " "))) {
1.99 niallo 985: if (i < KW_NUMTOKS_AUTHOR - 1)
986: tokens[i++] = p;
1.107 deraadt 987: }
1.98 niallo 988: if (*author != NULL)
989: xfree(*author);
1.135 ray 990: *author = xstrdup(tokens[1]);
1.98 niallo 991: break;
992: case KW_TYPE_DATE:
1.101 niallo 993: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 994: (p = strtok(NULL, " "))) {
1.99 niallo 995: if (i < KW_NUMTOKS_DATE - 1)
996: tokens[i++] = p;
1.98 niallo 997: }
998: len = strlen(tokens[1]) + strlen(tokens[2]) + 2;
999: datestring = xmalloc(len);
1.166 ! ray 1000: if (strlcpy(datestring, tokens[1], len) >= len ||
! 1001: strlcat(datestring, " ", len) >= len ||
! 1002: strlcat(datestring, tokens[2], len) >= len)
! 1003: errx(1, "date too long");
1.162 joris 1004: if ((*date = rcs_date_parse(datestring)) <= 0)
1.166 ! ray 1005: errx(1, "could not parse date");
1.98 niallo 1006: xfree(datestring);
1007: break;
1008: case KW_TYPE_STATE:
1.101 niallo 1009: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1010: (p = strtok(NULL, " "))) {
1.99 niallo 1011: if (i < KW_NUMTOKS_STATE - 1)
1012: tokens[i++] = p;
1.107 deraadt 1013: }
1.98 niallo 1014: if (*state != NULL)
1015: xfree(*state);
1.135 ray 1016: *state = xstrdup(tokens[1]);
1.98 niallo 1017: break;
1018: case KW_TYPE_REVISION:
1019: /* only parse revision if one is not already set */
1020: if (*rev != NULL)
1021: break;
1.102 niallo 1022: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1023: (p = strtok(NULL, " "))) {
1.99 niallo 1024: if (i < KW_NUMTOKS_REVISION - 1)
1025: tokens[i++] = p;
1.107 deraadt 1026: }
1.98 niallo 1027: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1.160 xsa 1028: errx(1, "could not parse rcsnum");
1.98 niallo 1029: break;
1030: }
1.1 niallo 1031: }