Annotation of src/usr.bin/rcs/ci.c, Revision 1.125
1.125 ! ray 1: /* $OpenBSD: ci.c,v 1.124 2006/03/23 08:50:41 xsa Exp $ */
1.1 niallo 2: /*
1.98 niallo 3: * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org>
1.1 niallo 4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following cinditions
8: * are met:
9: *
10: * 1. Redistributions of source cide must retain the above cipyright
11: * notice, this list of cinditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.91 xsa 27: #include "includes.h"
1.1 niallo 28:
1.92 xsa 29: #include "rcsprog.h"
1.4 niallo 30: #include "diff.h"
1.9 niallo 31:
1.125 ! ray 32: #define CI_OPTSTRING "d::f::I::i::j::k::l::m:M::N:n:qr::s:Tt:u::Vw:x::z:"
1.107 deraadt 33: #define DATE_NOW -1
34: #define DATE_MTIME -2
35:
36: #define KW_ID "Id"
37: #define KW_AUTHOR "Author"
38: #define KW_DATE "Date"
39: #define KW_STATE "State"
40: #define KW_REVISION "Revision"
41:
42: #define KW_TYPE_ID 1
43: #define KW_TYPE_AUTHOR 2
44: #define KW_TYPE_DATE 3
45: #define KW_TYPE_STATE 4
46: #define KW_TYPE_REVISION 5
47:
48: #define KW_NUMTOKS_ID 10
49: #define KW_NUMTOKS_AUTHOR 3
50: #define KW_NUMTOKS_DATE 4
51: #define KW_NUMTOKS_STATE 3
52: #define KW_NUMTOKS_REVISION 3
1.58 niallo 53:
1.115 niallo 54: #define RCSNUM_ZERO_ENDING(x) (x->rn_id[x->rn_len - 1] == 0)
55:
1.98 niallo 56: extern struct rcs_kw rcs_expkw[];
57:
1.58 niallo 58: struct checkin_params {
59: int flags, openflags;
60: mode_t fmode;
61: time_t date;
62: RCSFILE *file;
63: RCSNUM *frev, *newrev;
64: char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.98 niallo 65: char *author, *state;
66: const char *symbol, *description;
1.58 niallo 67: };
68:
1.94 niallo 69: static int checkin_attach_symbol(struct checkin_params *);
70: static int checkin_checklock(struct checkin_params *);
1.68 xsa 71: static char *checkin_diff_file(struct checkin_params *);
72: static char *checkin_getdesc(void);
73: static char *checkin_getinput(const char *);
74: static char *checkin_getlogmsg(RCSNUM *, RCSNUM *);
75: static int checkin_init(struct checkin_params *);
1.107 deraadt 76: static int checkin_keywordscan(char *, RCSNUM **, time_t *, char **,
1.98 niallo 77: char **);
1.107 deraadt 78: static int checkin_keywordtype(char *);
1.94 niallo 79: static int checkin_mtimedate(struct checkin_params *);
1.107 deraadt 80: static void checkin_parsekeyword(char *, RCSNUM **, time_t *, char **,
1.98 niallo 81: char **);
1.94 niallo 82: static int checkin_update(struct checkin_params *);
83: static void checkin_revert(struct checkin_params *);
1.4 niallo 84:
1.1 niallo 85: void
86: checkin_usage(void)
87: {
88: fprintf(stderr,
1.119 xsa 89: "usage: ci [-jMNqV] [-d[date]] [-f[rev]] [-I[rev]] [-i[rev]]\n"
90: " [-j[rev]] [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg]\n"
91: " [-Nsymbol] [-nsymbol] [-r[rev]] [-sstate] [-tfile|str]\n"
92: " [-u[rev]] [-wusername] [-xsuffixes] [-ztz] file ...\n");
1.58 niallo 93: }
94:
1.55 niallo 95:
1.1 niallo 96:
97: /*
98: * checkin_main()
99: *
100: * Handler for the `ci' program.
101: * Returns 0 on success, or >0 on error.
102: */
103: int
104: checkin_main(int argc, char **argv)
105: {
1.58 niallo 106: int i, ch, status;
107: struct checkin_params pb;
1.1 niallo 108:
1.58 niallo 109: pb.date = DATE_NOW;
110: pb.file = NULL;
1.98 niallo 111: pb.rcs_msg = pb.username = pb.author = pb.state = NULL;
112: pb.symbol = pb.description = NULL;
1.58 niallo 113: pb.newrev = NULL;
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.115 niallo 466: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
467: if ((pb->newrev != NULL) && (RCSNUM_ZERO_ENDING(pb->newrev)))
468: pb->newrev = rcsnum_inc(pb->newrev);
469:
1.82 joris 470: if (checkin_checklock(pb) < 0)
471: return (-1);
472:
473: /* If revision passed on command line is less than HEAD, bail.
474: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
475: * there is no lock set for the user.
476: */
1.63 niallo 477: if ((pb->newrev != NULL)
478: && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77 xsa 479: cvs_log(LP_ERR,
480: "%s: revision %s too low; must be higher than %s",
481: pb->file->rf_path,
482: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
483: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63 niallo 484: rcs_close(pb->file);
485: return (-1);
486: }
487:
1.104 niallo 488: /*
489: * Set the date of the revision to be the last modification
490: * time of the working file if -d has no argument.
491: */
492: if (pb->date == DATE_MTIME
493: && (checkin_mtimedate(pb) < 0))
494: return (-1);
495:
496: /* Date from argv/mtime must be more recent than HEAD */
497: if (pb->date != DATE_NOW) {
498: time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
499: if (pb->date <= head_date) {
1.113 xsa 500: char dbuf1[256], dbuf2[256], *fmt;
501: struct tm *t, *t_head;
502:
503: fmt = "%Y/%m/%d %H:%M:%S";
504:
505: t = localtime(&pb->date);
506: strftime(dbuf1, sizeof(dbuf1), fmt, t);
507: t_head = localtime(&head_date);
508: strftime(dbuf2, sizeof(dbuf2), fmt, t_head);
509:
510: fatal("%s: Date %s preceeds %s in revision %s.",
511: pb->file->rf_path, dbuf1, dbuf2,
1.104 niallo 512: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
513: }
514: }
515:
1.73 xsa 516: /* Load file contents */
1.63 niallo 517: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
1.106 niallo 518: rcs_close(pb->file);
1.63 niallo 519: return (-1);
520: }
521:
1.124 xsa 522: cvs_buf_putc(bp, '\0');
1.63 niallo 523: filec = (char *)cvs_buf_release(bp);
524:
1.73 xsa 525: /* Get RCS patch */
1.63 niallo 526: if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
527: cvs_log(LP_ERR, "failed to get diff");
1.106 niallo 528: rcs_close(pb->file);
1.63 niallo 529: return (-1);
530: }
531:
532: /*
533: * If -f is not specified and there are no differences, tell
534: * the user and revert to latest version.
535: */
536: if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
537: checkin_revert(pb);
538: return (0);
539: }
540:
1.73 xsa 541: /* If no log message specified, get it interactively. */
1.63 niallo 542: if (pb->flags & INTERACTIVE)
543: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
544:
1.82 joris 545: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63 niallo 546: if (rcs_errno != RCS_ERR_NOENT)
1.82 joris 547: cvs_log(LP_WARN, "failed to remove lock");
548: else if (!(pb->flags & CO_LOCK))
549: cvs_log(LP_WARN, "previous revision was not locked; "
550: "ignoring -l option");
1.63 niallo 551: }
552:
1.73 xsa 553: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 554: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
555: fatal("failed to set new rd_text for head rev");
1.63 niallo 556:
1.73 xsa 557: /* Now add our new revision */
1.63 niallo 558: if (rcs_rev_add(pb->file,
559: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 560: pb->rcs_msg, pb->date, pb->author) != 0) {
1.63 niallo 561: cvs_log(LP_ERR, "failed to add new revision");
1.106 niallo 562: rcs_close(pb->file);
1.63 niallo 563: return (-1);
564: }
565:
566: /*
567: * If we are checking in to a non-default (ie user-specified)
568: * revision, set head to this revision.
569: */
570: if (pb->newrev != NULL)
571: rcs_head_set(pb->file, pb->newrev);
572: else
573: pb->newrev = pb->file->rf_head;
574:
1.73 xsa 575: /* New head revision has to contain entire file; */
1.107 deraadt 576: if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
1.87 xsa 577: fatal("failed to set new head revision");
1.63 niallo 578:
1.73 xsa 579: /* Attach a symbolic name to this revision if specified. */
1.63 niallo 580: if (pb->symbol != NULL
581: && (checkin_attach_symbol(pb) < 0))
582: return (-1);
583:
1.73 xsa 584: /* Set the state of this revision if specified. */
1.63 niallo 585: if (pb->state != NULL)
586: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
587:
1.84 joris 588: xfree(pb->deltatext);
589: xfree(filec);
1.63 niallo 590: (void)unlink(pb->filename);
591:
1.73 xsa 592: /* Do checkout if -u or -l are specified. */
1.63 niallo 593: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
594: && !(pb->flags & CI_DEFAULT))
595: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 596: pb->username, pb->author, NULL, NULL);
1.63 niallo 597:
598: /* File will NOW be synced */
599: rcs_close(pb->file);
600:
601: if (pb->flags & INTERACTIVE) {
1.84 joris 602: xfree(pb->rcs_msg);
1.63 niallo 603: pb->rcs_msg = NULL;
604: }
605: return (0);
606: }
607:
608: /*
1.58 niallo 609: * checkin_init()
1.65 xsa 610: *
1.58 niallo 611: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 612: * On success, return 0. On error return -1.
1.58 niallo 613: */
1.63 niallo 614: static int
1.58 niallo 615: checkin_init(struct checkin_params *pb)
616: {
1.80 niallo 617: BUF *bp, *dp;
1.93 xsa 618: char *filec, numb[64];
1.115 niallo 619: int fetchlog = 0;
1.80 niallo 620: const char *rcs_desc;
1.58 niallo 621:
1.115 niallo 622: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
623: if ((pb->newrev != NULL) && (RCSNUM_ZERO_ENDING(pb->newrev))) {
624: pb->frev = rcsnum_alloc();
625: rcsnum_cpy(pb->newrev, pb->frev, 0);
626: pb->newrev = rcsnum_inc(pb->newrev);
627: fetchlog = 1;
628: }
629:
1.73 xsa 630: /* Load file contents */
1.58 niallo 631: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
1.106 niallo 632: rcs_close(pb->file);
1.63 niallo 633: return (-1);
1.58 niallo 634: }
635:
1.124 xsa 636: cvs_buf_putc(bp, '\0');
1.58 niallo 637: filec = (char *)cvs_buf_release(bp);
638:
1.98 niallo 639: /* Get default values from working copy if -k specified */
640: if (pb->flags & CI_KEYWORDSCAN)
641: checkin_keywordscan(filec, &pb->newrev, &pb->date, &pb->state,
642: &pb->author);
643:
1.73 xsa 644: /* Get description from user */
1.80 niallo 645: if (pb->description == NULL)
646: rcs_desc = (const char *)checkin_getdesc();
647: else {
648: if (*pb->description == '-') {
649: pb->description++;
1.81 niallo 650: rcs_desc = (const char *)pb->description;
651: } else {
1.80 niallo 652: dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
653: if (dp == NULL) {
654: cvs_log(LP_ERR,
655: "failed to load description file '%s'",
656: pb->description);
1.84 joris 657: xfree(filec);
1.106 niallo 658: rcs_close(pb->file);
1.80 niallo 659: return (-1);
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))
681: return (-1);
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.106 niallo 689: rcs_close(pb->file);
1.63 niallo 690: return (-1);
691: }
692: /*
693: * If we are checking in to a non-default (ie user-specified)
694: * revision, set head to this revision.
695: */
696: if (pb->newrev != NULL)
697: rcs_head_set(pb->file, pb->newrev);
698: else
699: pb->newrev = pb->file->rf_head;
700:
1.73 xsa 701: /* New head revision has to contain entire file; */
1.66 niallo 702: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63 niallo 703: cvs_log(LP_ERR, "failed to set new head revision");
1.106 niallo 704: rcs_close(pb->file);
1.63 niallo 705: return (-1);
1.58 niallo 706: }
1.73 xsa 707: /* Attach a symbolic name to this revision if specified. */
1.66 niallo 708: if (pb->symbol != NULL
709: && (checkin_attach_symbol(pb) < 0))
710: return (-1);
711:
1.73 xsa 712: /* Set the state of this revision if specified. */
1.66 niallo 713: if (pb->state != NULL)
714: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
715:
1.84 joris 716: xfree(filec);
1.66 niallo 717: (void)unlink(pb->filename);
718:
1.73 xsa 719: /* Do checkout if -u or -l are specified. */
1.66 niallo 720: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
721: && !(pb->flags & CI_DEFAULT))
722: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 723: pb->username, pb->author, NULL, NULL);
1.63 niallo 724:
1.119 xsa 725: if (verbose == 1) {
726: fprintf(stderr, "initial revision: %s\n",
727: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
728: }
1.93 xsa 729:
1.66 niallo 730: /* File will NOW be synced */
731: rcs_close(pb->file);
1.93 xsa 732:
1.63 niallo 733: return (0);
1.58 niallo 734: }
735:
1.59 niallo 736: /*
737: * checkin_attach_symbol()
738: *
739: * Attempt to attach the specified symbol to the revision.
740: * On success, return 0. On error return -1.
741: */
1.58 niallo 742: static int
743: checkin_attach_symbol(struct checkin_params *pb)
744: {
745: char rbuf[16];
746: int ret;
747: if (verbose == 1)
748: printf("symbol: %s\n", pb->symbol);
749: if (pb->flags & CI_SYMFORCE)
750: rcs_sym_remove(pb->file, pb->symbol);
751: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
752: && (rcs_errno == RCS_ERR_DUPENT)) {
753: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
754: rbuf, sizeof(rbuf));
755: cvs_log(LP_ERR,
756: "symbolic name %s already bound to %s",
757: pb->symbol, rbuf);
758: rcs_close(pb->file);
759: return (-1);
760: } else if (ret == -1) {
761: cvs_log(LP_ERR, "problem adding symbol: %s",
762: pb->symbol);
763: rcs_close(pb->file);
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: rcs_close(pb->file);
792: }
793:
1.59 niallo 794: /*
795: * checkin_checklock()
796: *
797: * Check for the existence of a lock on the file. If there are no locks, or it
798: * is not locked by the correct user, return -1. Otherwise, return 0.
799: */
1.58 niallo 800: static int
801: checkin_checklock(struct checkin_params *pb)
802: {
803: struct rcs_lock *lkp;
804:
1.82 joris 805: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
806: if ((!strcmp(lkp->rl_name, pb->username)) &&
807: (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
808: return (0);
1.58 niallo 809: }
1.32 joris 810:
1.82 joris 811: cvs_log(LP_ERR,
812: "%s: no lock set by %s", pb->file->rf_path, pb->username);
813: rcs_close(pb->file);
814: return (-1);
1.61 niallo 815: }
816:
817: /*
1.62 niallo 818: * checkin_mtimedate()
1.61 niallo 819: *
820: * Set the date of the revision to be the last modification
1.62 niallo 821: * time of the working file.
1.61 niallo 822: *
823: * On success, return 0. On error return -1.
824: */
825: static int
1.62 niallo 826: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 827: {
828: struct stat sb;
829: if (stat(pb->filename, &sb) != 0) {
1.72 xsa 830: cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61 niallo 831: rcs_close(pb->file);
832: return (-1);
833: }
834: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58 niallo 835: return (0);
1.98 niallo 836: }
837:
838: /*
839: * checkin_keywordscan()
840: *
841: * Searches working file for keyword values to determine its revision
842: * number, creation date and author, and uses these values instead of
843: * calculating them locally.
844: *
845: * Params: The data buffer to scan and pointers to pointers of variables in
846: * which to store the outputs.
847: *
848: * On success, return 0. On error return -1.
849: */
850: static int
851: checkin_keywordscan(char *data, RCSNUM **rev, time_t *date, char **author,
852: char **state)
853: {
1.122 ray 854: size_t end;
855: u_int j, found;
1.98 niallo 856: char *c, *kwstr, *start, buf[128];
857:
858: c = start = kwstr = NULL;
859:
1.122 ray 860: found = 0;
1.98 niallo 861:
1.122 ray 862: for (c = data; *c != '\0'; c++) {
1.98 niallo 863: if (*c == '$') {
864: start = c;
1.118 deraadt 865: c++;
1.98 niallo 866: if (!isalpha(*c)) {
867: c = start;
868: continue;
869: }
870: /* look for any matching keywords */
1.107 deraadt 871: found = 0;
872: for (j = 0; j < 10; j++) {
873: if (!strncmp(c, rcs_expkw[j].kw_str,
874: strlen(rcs_expkw[j].kw_str))) {
875: found = 1;
876: kwstr = rcs_expkw[j].kw_str;
877: break;
878: }
879: }
880:
881: /* unknown keyword, continue looking */
882: if (found == 0) {
883: c = start;
884: continue;
885: }
1.98 niallo 886:
887: c += strlen(kwstr);
888: if (*c != ':' && *c != '$') {
889: c = start;
890: continue;
891: }
892:
893: if (*c == ':') {
894: while (*c++) {
895: if (*c == '$') {
896: end = c - start + 2;
897: if (end >= sizeof(buf))
898: fatal("keyword buffer"
899: " too small!");
900: strlcpy(buf, start, end);
901: checkin_parsekeyword(buf, rev,
1.107 deraadt 902: date, author, state);
1.98 niallo 903: break;
904: }
905: }
906:
907: if (*c != '$') {
908: c = start;
909: continue;
910: }
911: }
912: }
913: }
914: if (found == 0)
915: return (-1);
916: else
917: return (0);
918: }
919:
920: /*
921: * checkin_keywordtype()
922: *
923: * Given an RCS keyword string, determine what type of string it is.
924: * This enables us to know what data should be in it.
925: *
926: * Returns type on success, or -1 on failure.
1.107 deraadt 927: */
1.98 niallo 928: static int
929: checkin_keywordtype(char *keystring)
930: {
931: char *p;
1.107 deraadt 932:
1.98 niallo 933: p = keystring;
1.118 deraadt 934: p++;
1.98 niallo 935: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
936: return (KW_TYPE_ID);
937: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
938: return (KW_TYPE_AUTHOR);
939: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
940: return (KW_TYPE_DATE);
941: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
942: return (KW_TYPE_STATE);
943: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
944: return (KW_TYPE_REVISION);
945: else
946: return (-1);
947: }
948:
949: /*
950: * checkin_parsekeyword()
951: *
952: * Do the actual parsing of an RCS keyword string, setting the values passed
953: * to the function to whatever is found.
954: *
955: */
956: static void
957: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
958: char **author, char **state)
959: {
960: char *tokens[10], *p, *datestring;
961: size_t len = 0;
1.99 niallo 962: int i = 0;
1.107 deraadt 963:
964: /* Parse data out of the expanded keyword */
1.98 niallo 965: switch (checkin_keywordtype(keystring)) {
966: case KW_TYPE_ID:
1.101 niallo 967: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 968: (p = strtok(NULL, " "))) {
1.99 niallo 969: if (i < KW_NUMTOKS_ID - 1)
970: tokens[i++] = p;
1.107 deraadt 971: }
1.99 niallo 972: tokens[i] = NULL;
1.98 niallo 973: if (*author != NULL)
974: xfree(*author);
975: if (*state != NULL)
976: xfree(*state);
977: /* only parse revision if one is not already set */
978: if (*rev == NULL) {
979: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
980: fatal("could not parse rcsnum");
981: }
982: len = strlen(tokens[5]) + 1;
983: *author = xmalloc(len);
984: strlcpy(*author, tokens[5], len);
985: len = strlen(tokens[6]) + 1;
986: *state = xmalloc(len);
987: strlcpy(*state, tokens[6], len);
988: len = strlen(tokens[3]) + strlen(tokens[4]) + 2;
989: datestring = xmalloc(len);
990: strlcpy(datestring, tokens[3], len);
991: strlcat(datestring, " ", len);
992: strlcat(datestring, tokens[4], len);
993: if ((*date = cvs_date_parse(datestring)) <= 0)
994: fatal("could not parse date\n");
995: xfree(datestring);
996: break;
997: case KW_TYPE_AUTHOR:
1.101 niallo 998: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 999: (p = strtok(NULL, " "))) {
1.99 niallo 1000: if (i < KW_NUMTOKS_AUTHOR - 1)
1001: tokens[i++] = p;
1.107 deraadt 1002: }
1.98 niallo 1003: if (*author != NULL)
1004: xfree(*author);
1005: len = strlen(tokens[1]) + 1;
1006: *author = xmalloc(len);
1007: strlcpy(*author, tokens[1], len);
1008: break;
1009: case KW_TYPE_DATE:
1.101 niallo 1010: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1011: (p = strtok(NULL, " "))) {
1.99 niallo 1012: if (i < KW_NUMTOKS_DATE - 1)
1013: tokens[i++] = p;
1.98 niallo 1014: }
1015: len = strlen(tokens[1]) + strlen(tokens[2]) + 2;
1016: datestring = xmalloc(len);
1017: strlcpy(datestring, tokens[1], len);
1018: strlcat(datestring, " ", len);
1019: strlcat(datestring, tokens[2], len);
1020: if ((*date = cvs_date_parse(datestring)) <= 0)
1021: fatal("could not parse date\n");
1022: xfree(datestring);
1023: break;
1024: case KW_TYPE_STATE:
1.101 niallo 1025: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1026: (p = strtok(NULL, " "))) {
1.99 niallo 1027: if (i < KW_NUMTOKS_STATE - 1)
1028: tokens[i++] = p;
1.107 deraadt 1029: }
1.98 niallo 1030: if (*state != NULL)
1031: xfree(*state);
1032: len = strlen(tokens[1]) + 1;
1033: *state = xmalloc(len);
1034: strlcpy(*state, tokens[1], len);
1035: break;
1036: case KW_TYPE_REVISION:
1037: /* only parse revision if one is not already set */
1038: if (*rev != NULL)
1039: break;
1.102 niallo 1040: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1041: (p = strtok(NULL, " "))) {
1.99 niallo 1042: if (i < KW_NUMTOKS_REVISION - 1)
1043: tokens[i++] = p;
1.107 deraadt 1044: }
1.98 niallo 1045: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1046: fatal("could not parse rcsnum");
1047: break;
1048: }
1.1 niallo 1049: }