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