Annotation of src/usr.bin/rcs/rcsmerge.c, Revision 1.4
1.4 ! xsa 1: /* $OpenBSD: rcsmerge.c,v 1.3 2005/10/23 04:25:34 joris 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:
41: static int kflag = RCS_KWEXP_ERR;
42:
43: int
44: rcsmerge_main(int argc, char **argv)
45: {
46: int i, ch;
1.2 joris 47: char *fcont, fpath[MAXPATHLEN];
1.1 xsa 48: RCSFILE *file;
1.2 joris 49: RCSNUM *baserev, *rev2, *frev;
50: BUF *bp;
1.1 xsa 51:
1.2 joris 52: baserev = rev2 = RCS_HEAD_REV;
1.1 xsa 53:
1.2 joris 54: while ((ch = rcs_getopt(argc, argv, "k:p::qr:TV")) != -1) {
1.1 xsa 55: switch (ch) {
56: case 'k':
57: kflag = rcs_kflag_get(rcs_optarg);
58: if (RCS_KWEXP_INVAL(kflag)) {
59: cvs_log(LP_ERR,
60: "invalid RCS keyword expansion mode");
61: (usage)();
62: exit(1);
63: }
64: break;
1.2 joris 65: case 'p':
66: rcs_set_rev(rcs_optarg, &baserev);
67: pipeout = 1;
68: break;
1.1 xsa 69: case 'q':
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);
88: default:
89: break;
90: }
91: }
92:
93: argc -= rcs_optind;
94: argv += rcs_optind;
95:
96: if (argc < 0) {
97: cvs_log(LP_ERR, "no input file");
98: (usage)();
99: exit(1);
100: }
101:
1.2 joris 102: if (baserev == RCS_HEAD_REV) {
1.4 ! xsa 103: cvs_log(LP_ERR, "no base revision number given");
1.2 joris 104: (usage)();
105: exit(1);
106: }
107:
1.1 xsa 108: for (i = 0; i < argc; i++) {
109: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
110: continue;
111:
112: if ((file = rcs_open(fpath, RCS_READ)) == NULL)
113: continue;
1.2 joris 114:
115: if (rev2 == RCS_HEAD_REV)
116: frev = file->rf_head;
117: else
118: frev = rev2;
119:
120: if ((bp = cvs_diff3(file, argv[i], baserev, frev)) == NULL) {
121: cvs_log(LP_ERR, "failed to merge");
122: rcs_close(file);
123: continue;
124: }
125:
126: if (pipeout == 1) {
127: if (cvs_buf_putc(bp, '\0') < 0) {
128: rcs_close(file);
129: continue;
130: }
131:
132: fcont = cvs_buf_release(bp);
133: printf("%s", fcont);
134: free(fcont);
135: } else {
136: /* XXX mode */
137: if (cvs_buf_write(bp, argv[i], 0644) < 0)
138: cvs_log(LP_ERR, "failed to write new file");
139:
140: cvs_buf_free(bp);
1.3 joris 141: }
142:
143: if (diff3_conflicts > 0) {
144: cvs_log(LP_WARN, "%d conflict%s found during merge",
145: diff3_conflicts, (diff3_conflicts > 1) ? "s": "");
1.2 joris 146: }
1.1 xsa 147:
148: rcs_close(file);
149: }
150:
151: return (0);
152: }
153:
154: void
155: rcsmerge_usage(void)
156: {
157: fprintf(stderr,
158: "usage: rcsmerge [-qTV] [-kmode] [-rrev] file ...\n");
159: }