Annotation of src/usr.bin/rcs/rcsmerge.c, Revision 1.25
1.25 ! ray 1: /* $OpenBSD: rcsmerge.c,v 1.24 2006/04/12 08:23:30 ray 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: static int kflag = RCS_KWEXP_ERR;
33:
34: int
35: rcsmerge_main(int argc, char **argv)
36: {
37: int i, ch;
1.24 ray 38: char *fcont, fpath[MAXPATHLEN], r1[16], r2[16], *rev_str1, *rev_str2;
1.1 xsa 39: RCSFILE *file;
1.24 ray 40: RCSNUM *rev1, *rev2;
1.2 joris 41: BUF *bp;
1.1 xsa 42:
1.24 ray 43: rev1 = rev2 = NULL;
44: rev_str1 = rev_str2 = NULL;
1.1 xsa 45:
1.23 ray 46: while ((ch = rcs_getopt(argc, argv, "AEek:p::q::r:TVx::z:")) != -1) {
1.1 xsa 47: switch (ch) {
1.14 xsa 48: case 'A': case 'E': case 'e':
49: break;
1.1 xsa 50: case 'k':
51: kflag = rcs_kflag_get(rcs_optarg);
52: if (RCS_KWEXP_INVAL(kflag)) {
53: cvs_log(LP_ERR,
54: "invalid RCS keyword expansion mode");
55: (usage)();
56: exit(1);
57: }
58: break;
1.2 joris 59: case 'p':
1.24 ray 60: rcs_setrevstr2(&rev_str1, &rev_str2, rcs_optarg);
1.2 joris 61: pipeout = 1;
62: break;
1.1 xsa 63: case 'q':
1.24 ray 64: rcs_setrevstr2(&rev_str1, &rev_str2, rcs_optarg);
1.1 xsa 65: verbose = 0;
66: break;
67: case 'r':
1.24 ray 68: rcs_setrevstr2(&rev_str1, &rev_str2, rcs_optarg);
1.1 xsa 69: break;
70: case 'T':
71: /*
72: * kept for compatibility
73: */
74: break;
75: case 'V':
76: printf("%s\n", rcs_version);
77: exit(0);
1.21 ray 78: /* NOTREACHED */
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:
87: break;
88: }
89: }
90:
91: argc -= rcs_optind;
92: argv += rcs_optind;
93:
94: if (argc < 0) {
95: cvs_log(LP_ERR, "no input file");
96: (usage)();
97: exit(1);
98: }
99:
1.24 ray 100: if (rev_str1 == NULL) {
1.4 xsa 101: cvs_log(LP_ERR, "no base revision number given");
1.2 joris 102: (usage)();
103: exit(1);
104: }
105:
1.1 xsa 106: for (i = 0; i < argc; i++) {
107: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
108: continue;
109:
110: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
111: continue;
1.2 joris 112:
1.18 xsa 113: if (verbose == 1)
114: fprintf(stderr, "RCS file: %s\n", fpath);
1.14 xsa 115:
1.24 ray 116: if (rev1 != NULL) {
117: rcsnum_free(rev1);
118: rev1 = NULL;
119: }
120: if (rev2 != NULL) {
121: rcsnum_free(rev2);
122: rev2 = NULL;
123: }
124:
1.25 ! ray 125: if ((rev1 = rcs_getrevnum(rev_str1, file)) == NULL)
! 126: fatal("invalid revision: %s", rev_str1);
! 127: if (rev_str2 != NULL) {
! 128: if ((rev2 = rcs_getrevnum(rev_str2, file)) == NULL)
! 129: fatal("invalid revision: %s", rev_str2);
! 130: } else {
1.24 ray 131: rev2 = rcsnum_alloc();
132: rcsnum_cpy(file->rf_head, rev2, 0);
133: }
1.8 xsa 134:
1.24 ray 135: if (rcsnum_cmp(rev1, rev2, 0) == 0) {
1.14 xsa 136: rcs_close(file);
137: continue;
138: }
1.15 xsa 139:
1.24 ray 140: if (verbose == 1) {
141: (void)rcsnum_tostr(rev1, r1, sizeof(r1));
142: (void)rcsnum_tostr(rev2, r2, sizeof(r2));
1.15 xsa 143:
1.18 xsa 144: fprintf(stderr, "Merging differences between %s and "
145: "%s into %s%s\n", r1, r2, argv[i],
146: (pipeout == 1) ? "; result to stdout":"");
1.24 ray 147: }
1.2 joris 148:
1.24 ray 149: if ((bp = cvs_diff3(file, argv[i], rev1, rev2,
150: verbose)) == NULL) {
1.2 joris 151: cvs_log(LP_ERR, "failed to merge");
152: rcs_close(file);
153: continue;
154: }
155:
156: if (pipeout == 1) {
1.22 xsa 157: cvs_buf_putc(bp, '\0');
1.2 joris 158: fcont = cvs_buf_release(bp);
159: printf("%s", fcont);
1.11 joris 160: xfree(fcont);
1.2 joris 161: } else {
162: /* XXX mode */
163: if (cvs_buf_write(bp, argv[i], 0644) < 0)
164: cvs_log(LP_ERR, "failed to write new file");
165:
166: cvs_buf_free(bp);
1.3 joris 167: }
1.1 xsa 168: rcs_close(file);
169: }
170:
171: return (0);
172: }
173:
174: void
175: rcsmerge_usage(void)
176: {
177: fprintf(stderr,
1.19 xsa 178: "usage: rcsmerge [-AEeV] [-kmode] [-p[rev]] [-q[rev]]\n"
179: " [-rrev] [-xsuffixes] [-ztz] file ...\n");
1.1 xsa 180: }