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