Annotation of src/usr.bin/rcs/rcsmerge.c, Revision 1.44
1.44 ! xsa 1: /* $OpenBSD: rcsmerge.c,v 1.43 2006/05/09 14:03:55 jmc Exp $ */
1.1 xsa 2: /*
1.14 xsa 3: * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.1 xsa 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 conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions 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.12 xsa 27: #include "includes.h"
1.1 xsa 28:
1.13 xsa 29: #include "rcsprog.h"
1.1 xsa 30: #include "diff.h"
31:
32: int
33: rcsmerge_main(int argc, char **argv)
34: {
1.44 ! xsa 35: int fd, ch, flags, kflag, status;
1.24 ray 36: char *fcont, fpath[MAXPATHLEN], r1[16], r2[16], *rev_str1, *rev_str2;
1.1 xsa 37: RCSFILE *file;
1.24 ray 38: RCSNUM *rev1, *rev2;
1.2 joris 39: BUF *bp;
1.1 xsa 40:
1.27 xsa 41: flags = 0;
42: kflag = RCS_KWEXP_ERR;
1.44 ! xsa 43: status = D_ERROR;
1.24 ray 44: rev1 = rev2 = NULL;
45: rev_str1 = rev_str2 = NULL;
1.1 xsa 46:
1.34 ray 47: while ((ch = rcs_getopt(argc, argv, "AEek:p::q::r::TVx::z:")) != -1) {
1.1 xsa 48: switch (ch) {
1.14 xsa 49: case 'A': case 'E': case 'e':
50: break;
1.1 xsa 51: case 'k':
52: kflag = rcs_kflag_get(rcs_optarg);
53: if (RCS_KWEXP_INVAL(kflag)) {
1.31 xsa 54: warnx("invalid RCS keyword substitution mode");
1.1 xsa 55: (usage)();
1.38 xsa 56: exit(D_ERROR);
1.1 xsa 57: }
58: break;
1.2 joris 59: case 'p':
1.24 ray 60: rcs_setrevstr2(&rev_str1, &rev_str2, rcs_optarg);
1.28 xsa 61: flags |= PIPEOUT;
1.2 joris 62: break;
1.1 xsa 63: case 'q':
1.24 ray 64: rcs_setrevstr2(&rev_str1, &rev_str2, rcs_optarg);
1.27 xsa 65: flags |= QUIET;
1.1 xsa 66: break;
67: case 'r':
1.34 ray 68: rcs_setrevstr2(&rev_str1, &rev_str2,
69: rcs_optarg ? rcs_optarg : "");
1.1 xsa 70: break;
71: case 'T':
72: /*
73: * kept for compatibility
74: */
75: break;
76: case 'V':
77: printf("%s\n", rcs_version);
78: exit(0);
1.9 xsa 79: case 'x':
1.23 ray 80: /* Use blank extension if none given. */
81: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.17 joris 82: break;
83: case 'z':
84: timezone_flag = rcs_optarg;
1.9 xsa 85: break;
1.1 xsa 86: default:
1.26 ray 87: (usage)();
1.38 xsa 88: exit(D_ERROR);
1.1 xsa 89: }
90: }
91:
92: argc -= rcs_optind;
93: argv += rcs_optind;
94:
1.44 ! xsa 95: if (rev_str1 == NULL) {
! 96: warnx("no base revision number given");
1.1 xsa 97: (usage)();
1.38 xsa 98: exit(D_ERROR);
1.1 xsa 99: }
100:
1.44 ! xsa 101: if (argc < 1) {
! 102: warnx("no input file");
1.2 joris 103: (usage)();
1.38 xsa 104: exit(D_ERROR);
1.2 joris 105: }
106:
1.44 ! xsa 107: if (argc > 2 || (argc == 2 && argv[1] != NULL))
! 108: warnx("warning: excess arguments ignored");
! 109:
! 110: if ((fd = rcs_statfile(argv[0], fpath, sizeof(fpath), flags)) < 0)
! 111: return (status);
1.24 ray 112:
1.44 ! xsa 113: if (!(flags & QUIET))
! 114: (void)fprintf(stderr, "RCS file: %s\n", fpath);
1.8 xsa 115:
1.44 ! xsa 116: if ((file = rcs_open(fpath, fd, RCS_READ)) == NULL)
! 117: return (status);
! 118:
! 119: if (strcmp(rev_str1, "") == 0) {
! 120: rev1 = rcsnum_alloc();
! 121: rcsnum_cpy(file->rf_head, rev1, 0);
! 122: } else if ((rev1 = rcs_getrevnum(rev_str1, file)) == NULL)
! 123: errx(D_ERROR, "invalid revision: %s", rev_str1);
! 124:
! 125: if (rev_str2 != NULL && strcmp(rev_str2, "") != 0) {
! 126: if ((rev2 = rcs_getrevnum(rev_str2, file)) == NULL)
! 127: errx(D_ERROR, "invalid revision: %s", rev_str2);
! 128: } else {
! 129: rev2 = rcsnum_alloc();
! 130: rcsnum_cpy(file->rf_head, rev2, 0);
! 131: }
1.15 xsa 132:
1.44 ! xsa 133: if (rcsnum_cmp(rev1, rev2, 0) == 0)
! 134: goto out;
1.2 joris 135:
1.44 ! xsa 136: if ((bp = rcs_diff3(file, argv[0], rev1, rev2,
! 137: !(flags & QUIET))) == NULL) {
! 138: warnx("failed to merge");
! 139: goto out;
! 140: }
1.2 joris 141:
1.44 ! xsa 142: if (!(flags & QUIET)) {
! 143: (void)rcsnum_tostr(rev1, r1, sizeof(r1));
! 144: (void)rcsnum_tostr(rev2, r2, sizeof(r2));
! 145:
! 146: (void)fprintf(stderr, "Merging differences between %s and "
! 147: "%s into %s%s\n", r1, r2, argv[0],
! 148: (flags & PIPEOUT) ? "; result to stdout":"");
! 149: }
1.41 xsa 150:
1.44 ! xsa 151: if (diff3_conflicts != 0)
! 152: status = D_OVERLAPS;
! 153: else
! 154: status = 0;
! 155:
! 156: if (flags & PIPEOUT) {
! 157: rcs_buf_putc(bp, '\0');
! 158: fcont = rcs_buf_release(bp);
! 159: printf("%s", fcont);
! 160: xfree(fcont);
! 161: } else {
! 162: /* XXX mode */
! 163: if (rcs_buf_write(bp, argv[0], 0644) < 0)
! 164: warnx("rcs_buf_write failed");
1.2 joris 165:
1.44 ! xsa 166: rcs_buf_free(bp);
1.1 xsa 167: }
1.44 ! xsa 168:
! 169: out:
! 170: rcs_close(file);
! 171:
! 172: if (rev1 != NULL)
! 173: rcsnum_free(rev1);
! 174: if (rev2 != NULL)
! 175: rcsnum_free(rev2);
1.1 xsa 176:
1.41 xsa 177: return (status);
1.1 xsa 178: }
179:
180: void
181: rcsmerge_usage(void)
182: {
183: fprintf(stderr,
1.43 jmc 184: "usage: rcsmerge [-EV] [-kmode] [-p[rev]] [-q[rev]]\n"
1.32 jmc 185: " [-xsuffixes] [-ztz] -rrev file ...\n");
1.1 xsa 186: }