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