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