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