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