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