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