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