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