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