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