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