xref: /netbsd/external/gpl2/rcs/dist/src/rcsmerge.c (revision 0d69c6cf)
1 /*	$NetBSD: rcsmerge.c,v 1.2 2016/01/14 04:22:39 christos Exp $	*/
2 
3 /* Merge RCS revisions.  */
4 
5 /* Copyright 1982, 1988, 1989 Walter Tichy
6    Copyright 1990, 1991, 1992, 1993, 1994, 1995 Paul Eggert
7    Distributed under license by the Free Software Foundation, Inc.
8 
9 This file is part of RCS.
10 
11 RCS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15 
16 RCS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20 
21 You should have received a copy of the GNU General Public License
22 along with RCS; see the file COPYING.
23 If not, write to the Free Software Foundation,
24 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 
26 Report problems and direct all questions to:
27 
28     rcs-bugs@cs.purdue.edu
29 
30 */
31 
32 /*
33  * Log: rcsmerge.c,v
34  * Revision 5.15  1995/06/16 06:19:24  eggert
35  * Update FSF address.
36  *
37  * Revision 5.14  1995/06/01 16:23:43  eggert
38  * (main): Report an error if -kb, so don't worry about binary stdout.
39  * Punctuate messages properly.  Rewrite to avoid `goto end'.
40  *
41  * Revision 5.13  1994/03/17 14:05:48  eggert
42  * Specify subprocess input via file descriptor, not file name.  Remove lint.
43  *
44  * Revision 5.12  1993/11/09 17:40:15  eggert
45  * -V now prints version on stdout and exits.  Don't print usage twice.
46  *
47  * Revision 5.11  1993/11/03 17:42:27  eggert
48  * Add -A, -E, -e, -z.  Ignore -T.  Allow up to three file labels.
49  * Pass -Vn to `co'.  Pass unexpanded revision name to `co', so that Name works.
50  *
51  * Revision 5.10  1992/07/28  16:12:44  eggert
52  * Add -V.
53  *
54  * Revision 5.9  1992/01/24  18:44:19  eggert
55  * lint -> RCS_lint
56  *
57  * Revision 5.8  1992/01/06  02:42:34  eggert
58  * Update usage string.
59  *
60  * Revision 5.7  1991/11/20  17:58:09  eggert
61  * Don't Iopen(f, "r+"); it's not portable.
62  *
63  * Revision 5.6  1991/08/19  03:13:55  eggert
64  * Add -r$.  Tune.
65  *
66  * Revision 5.5  1991/04/21  11:58:27  eggert
67  * Add -x, RCSINIT, MS-DOS support.
68  *
69  * Revision 5.4  1991/02/25  07:12:43  eggert
70  * Merging a revision to itself is no longer an error.
71  *
72  * Revision 5.3  1990/11/01  05:03:50  eggert
73  * Remove unneeded setid check.
74  *
75  * Revision 5.2  1990/09/04  08:02:28  eggert
76  * Check for I/O error when reading working file.
77  *
78  * Revision 5.1  1990/08/29  07:14:04  eggert
79  * Add -q.  Pass -L options to merge.
80  *
81  * Revision 5.0  1990/08/22  08:13:41  eggert
82  * Propagate merge's exit status.
83  * Remove compile-time limits; use malloc instead.
84  * Make lock and temp files faster and safer.  Ansify and Posixate.  Add -V.
85  * Don't use access().  Tune.
86  *
87  * Revision 4.5  89/05/01  15:13:16  narten
88  * changed copyright header to reflect current distribution rules
89  *
90  * Revision 4.4  88/08/09  19:13:13  eggert
91  * Beware merging into a readonly file.
92  * Beware merging a revision to itself (no change).
93  * Use execv(), not system(); yield exit status like diff(1)'s.
94  *
95  * Revision 4.3  87/10/18  10:38:02  narten
96  * Updating version numbers. Changes relative to version 1.1
97  * actually relative to 4.1
98  *
99  * Revision 1.3  87/09/24  14:00:31  narten
100  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
101  * warnings)
102  *
103  * Revision 1.2  87/03/27  14:22:36  jenkins
104  * Port to suns
105  *
106  * Revision 4.1  83/03/28  11:14:57  wft
107  * Added handling of default branch.
108  *
109  * Revision 3.3  82/12/24  15:29:00  wft
110  * Added call to catchsig().
111  *
112  * Revision 3.2  82/12/10  21:32:02  wft
113  * Replaced getdelta() with gettree(); improved error messages.
114  *
115  * Revision 3.1  82/11/28  19:27:44  wft
116  * Initial revision.
117  *
118  */
119 #include "rcsbase.h"
120 
121 static char const co[] = CO;
122 
123 mainProg(rcsmergeId, "rcsmerge", "Id: rcsmerge.c,v 5.15 1995/06/16 06:19:24 eggert Exp ")
124 {
125 	static char const cmdusage[] =
126 		"\nrcsmerge usage: rcsmerge -rrev1 [-rrev2] -ksubst -{pq}[rev] -Vn -xsuff -zzone file";
127 	static char const quietarg[] = "-q";
128 
129 	register int i;
130 	char *a, **newargv;
131 	char const *arg[3];
132 	char const *rev[3], *xrev[3]; /*revision numbers*/
133 	char const *edarg, *expandarg, *suffixarg, *versionarg, *zonearg;
134         int tostdout;
135 	int status;
136 	RILE *workptr;
137 	struct buf commarg;
138 	struct buf numericrev; /* holds expanded revision number */
139 	struct hshentries *gendeltas; /* deltas to be generated */
140         struct hshentry * target;
141 
142 	bufautobegin(&commarg);
143 	bufautobegin(&numericrev);
144 	edarg = rev[1] = rev[2] = 0;
145 	status = 0; /* Keep lint happy.  */
146 	tostdout = false;
147 	expandarg = suffixarg = versionarg = zonearg = quietarg; /* no-op */
148 	suffixes = X_DEFAULT;
149 
150 	argc = getRCSINIT(argc, argv, &newargv);
151 	argv = newargv;
152 	while (a = *++argv,  0<--argc && *a++=='-') {
153 		switch (*a++) {
154                 case 'p':
155                         tostdout=true;
156 			goto revno;
157 
158 		case 'q':
159 			quietflag = true;
160 		revno:
161 			if (!*a)
162 				break;
163                         /* falls into -r */
164                 case 'r':
165 			if (!rev[1])
166 				rev[1] = a;
167 			else if (!rev[2])
168 				rev[2] = a;
169 			else
170 				error("too many revision numbers");
171                         break;
172 
173 		case 'A': case 'E': case 'e':
174 			if (*a)
175 				goto unknown;
176 			edarg = *argv;
177 			break;
178 
179 		case 'x':
180 			suffixarg = *argv;
181 			suffixes = a;
182 			break;
183 		case 'z':
184 			zonearg = *argv;
185 			zone_set(a);
186 			break;
187 		case 'T':
188 			/* Ignore -T, so that RCSINIT can contain -T.  */
189 			if (*a)
190 				goto unknown;
191 			break;
192 		case 'V':
193 			versionarg = *argv;
194 			setRCSversion(versionarg);
195 			break;
196 
197 		case 'k':
198 			expandarg = *argv;
199 			if (0 <= str2expmode(expandarg+2))
200 			    break;
201 			/* fall into */
202                 default:
203 		unknown:
204 			error("unknown option: %s%s", *argv, cmdusage);
205                 };
206         } /* end of option processing */
207 
208 	if (!rev[1]) faterror("no base revision number given");
209 
210 	/* Now handle all pathnames.  */
211 
212 	if (!nerror) {
213 	    if (argc < 1)
214 		faterror("no input file%s", cmdusage);
215 	    if (0 < pairnames(argc, argv, rcsreadopen, true, false)) {
216 
217                 if (argc>2  ||  (argc==2 && argv[1]))
218 			warn("excess arguments ignored");
219 		if (Expand == BINARY_EXPAND)
220 			workerror("merging binary files");
221 		diagnose("RCS file: %s\n", RCSname);
222 		if (!(workptr = Iopen(workname, FOPEN_R_WORK, (struct stat*)0)))
223 			efaterror(workname);
224 
225                 gettree();  /* reads in the delta tree */
226 
227 		if (!Head) rcsfaterror("no revisions present");
228 
229 		if (!*rev[1])
230 			rev[1]  =  Dbranch ? Dbranch : Head->num;
231 		if (fexpandsym(rev[1], &numericrev, workptr)
232 		    && (target=genrevs(numericrev.string, (char *)0, (char *)0, (char*)0, &gendeltas))
233 		) {
234 		  xrev[1] = target->num;
235 		  if (!rev[2] || !*rev[2])
236 			rev[2]  =  Dbranch ? Dbranch : Head->num;
237 		  if (fexpandsym(rev[2], &numericrev, workptr)
238 		      && (target=genrevs(numericrev.string, (char *)0, (char *)0, (char *)0, &gendeltas))
239 		  ) {
240 		    xrev[2] = target->num;
241 
242 		    if (strcmp(xrev[1],xrev[2]) == 0) {
243 		      if (tostdout) {
244 			fastcopy(workptr, stdout);
245 			Ofclose(stdout);
246 		      }
247 		    } else {
248 		      Izclose(&workptr);
249 
250 		      for (i=1; i<=2; i++) {
251 			diagnose("retrieving revision %s\n", xrev[i]);
252 			bufscpy(&commarg, "-p");
253 			bufscat(&commarg, rev[i]); /* not xrev[i], for $Name's sake */
254 			if (run(
255 				-1,
256 				/* Do not collide with merger.c maketemp().  */
257 				arg[i] = maketemp(i+2),
258 				co, quietarg, commarg.string,
259 				expandarg, suffixarg, versionarg, zonearg,
260 				RCSname, (char*)0
261 			))
262 				rcsfaterror("co failed");
263 		      }
264 		      diagnose("Merging differences between %s and %s into %s%s\n",
265 			       xrev[1], xrev[2], workname,
266 			       tostdout?"; result to stdout":"");
267 
268 		      arg[0] = xrev[0] = workname;
269 		      status = merge(tostdout, edarg, xrev, arg);
270 		    }
271 		  }
272 		}
273 
274 		Izclose(&workptr);
275 	    }
276         }
277 	tempunlink();
278 	exitmain(nerror ? DIFF_TROUBLE : status);
279 }
280 
281 #if RCS_lint
282 #	define exiterr rmergeExit
283 #endif
284 	void
exiterr()285 exiterr()
286 {
287 	tempunlink();
288 	_exit(DIFF_TROUBLE);
289 }
290