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