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