Annotation of src/usr.bin/rcs/rcsmerge.c, Revision 1.9
1.9 ! xsa 1: /* $OpenBSD: rcsmerge.c,v 1.8 2005/11/02 13:21:37 xsa Exp $ */
1.1 xsa 2: /*
3: * Copyright (c) 2005 Xavier Santolaria <xsa@openbsd.org>
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:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <stdio.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34:
35: #include "log.h"
36: #include "rcs.h"
37: #include "diff.h"
38: #include "rcsprog.h"
39:
40: static int kflag = RCS_KWEXP_ERR;
41:
42: int
43: rcsmerge_main(int argc, char **argv)
44: {
45: int i, ch;
1.2 joris 46: char *fcont, fpath[MAXPATHLEN];
1.1 xsa 47: RCSFILE *file;
1.2 joris 48: RCSNUM *baserev, *rev2, *frev;
49: BUF *bp;
1.1 xsa 50:
1.2 joris 51: baserev = rev2 = RCS_HEAD_REV;
1.1 xsa 52:
1.9 ! xsa 53: while ((ch = rcs_getopt(argc, argv, "k:p::q::r:TVx:")) != -1) {
1.1 xsa 54: switch (ch) {
55: case 'k':
56: kflag = rcs_kflag_get(rcs_optarg);
57: if (RCS_KWEXP_INVAL(kflag)) {
58: cvs_log(LP_ERR,
59: "invalid RCS keyword expansion mode");
60: (usage)();
61: exit(1);
62: }
63: break;
1.2 joris 64: case 'p':
65: rcs_set_rev(rcs_optarg, &baserev);
66: pipeout = 1;
67: break;
1.1 xsa 68: case 'q':
1.7 xsa 69: rcs_set_rev(rcs_optarg, &baserev);
1.1 xsa 70: verbose = 0;
71: break;
72: case 'r':
1.2 joris 73: if (baserev == RCS_HEAD_REV)
74: rcs_set_rev(rcs_optarg, &baserev);
75: else if (rev2 == RCS_HEAD_REV)
76: rcs_set_rev(rcs_optarg, &rev2);
77: else
78: cvs_log(LP_WARN, "ignored excessive -r option");
1.1 xsa 79: break;
80: case 'T':
81: /*
82: * kept for compatibility
83: */
84: break;
85: case 'V':
86: printf("%s\n", rcs_version);
87: exit(0);
1.9 ! xsa 88: case 'x':
! 89: rcs_suffixes = rcs_optarg;
! 90: break;
1.1 xsa 91: default:
92: break;
93: }
94: }
95:
96: argc -= rcs_optind;
97: argv += rcs_optind;
98:
99: if (argc < 0) {
100: cvs_log(LP_ERR, "no input file");
101: (usage)();
102: exit(1);
103: }
104:
1.2 joris 105: if (baserev == RCS_HEAD_REV) {
1.4 xsa 106: cvs_log(LP_ERR, "no base revision number given");
1.2 joris 107: (usage)();
108: exit(1);
109: }
110:
1.1 xsa 111: for (i = 0; i < argc; i++) {
112: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
113: continue;
114:
115: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
116: continue;
1.2 joris 117:
118: if (rev2 == RCS_HEAD_REV)
119: frev = file->rf_head;
120: else
121: frev = rev2;
1.8 xsa 122:
123: printf("RCS file: %s\n", fpath);
1.2 joris 124:
125: if ((bp = cvs_diff3(file, argv[i], baserev, frev)) == NULL) {
126: cvs_log(LP_ERR, "failed to merge");
127: rcs_close(file);
128: continue;
129: }
130:
131: if (pipeout == 1) {
132: if (cvs_buf_putc(bp, '\0') < 0) {
133: rcs_close(file);
134: continue;
135: }
136:
137: fcont = cvs_buf_release(bp);
138: printf("%s", fcont);
139: free(fcont);
140: } else {
141: /* XXX mode */
142: if (cvs_buf_write(bp, argv[i], 0644) < 0)
143: cvs_log(LP_ERR, "failed to write new file");
144:
145: cvs_buf_free(bp);
1.3 joris 146: }
147:
148: if (diff3_conflicts > 0) {
149: cvs_log(LP_WARN, "%d conflict%s found during merge",
150: diff3_conflicts, (diff3_conflicts > 1) ? "s": "");
1.2 joris 151: }
1.1 xsa 152:
153: rcs_close(file);
154: }
155:
156: return (0);
157: }
158:
159: void
160: rcsmerge_usage(void)
161: {
162: fprintf(stderr,
1.7 xsa 163: "usage: rcsmerge [-TV] [-kmode] [-p[rev]] [-q[rev]] "
164: "[-rrev] file ...\n");
1.1 xsa 165: }