No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / usr.bin / rcs / rcsdiff / rcsdiff.c
blob867975418917790d034a011550547c67a4636b44
1 /* $NetBSD: rcsdiff.c,v 1.5 1999/07/22 01:48:09 hubertf Exp $ */
3 /* Compare RCS revisions. */
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.
9 This file is part of RCS.
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.
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.
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.
26 Report problems and direct all questions to:
28 rcs-bugs@cs.purdue.edu
33 * $Log: rcsdiff.c,v $
34 * Revision 1.5 1999/07/22 01:48:09 hubertf
35 * Allow -L on both files, not only one.
37 * Reported in PR 1947 by Niklas Hallqvist <niklas@filippa.appli.se> and
38 * also fed back to the GNU RCS maintainers.
40 * Revision 1.4 1996/10/15 07:00:40 veego
41 * Merge rcs 5.7.
43 * Revision 5.19 1995/06/16 06:19:24 eggert
44 * Update FSF address.
46 * Revision 5.18 1995/06/01 16:23:43 eggert
47 * (main): Pass "--binary" if -kb and if --binary makes a difference.
48 * Don't treat + options specially.
50 * Revision 5.17 1994/03/17 14:05:48 eggert
51 * Specify subprocess input via file descriptor, not file name. Remove lint.
53 * Revision 5.16 1993/11/09 17:40:15 eggert
54 * -V now prints version on stdout and exits. Don't print usage twice.
56 * Revision 5.15 1993/11/03 17:42:27 eggert
57 * Add -z. Ignore -T. Pass -Vn to `co'. Add Name keyword.
58 * Put revision numbers in -c output. Improve quality of diagnostics.
60 * Revision 5.14 1992/07/28 16:12:44 eggert
61 * Add -V. Use co -M for better dates with traditional diff -c.
63 * Revision 5.13 1992/02/17 23:02:23 eggert
64 * Output more readable context diff headers.
65 * Suppress needless checkout and comparison of identical revisions.
67 * Revision 5.12 1992/01/24 18:44:19 eggert
68 * Add GNU diff 1.15.2's new options. lint -> RCS_lint
70 * Revision 5.11 1992/01/06 02:42:34 eggert
71 * Update usage string.
73 * Revision 5.10 1991/10/07 17:32:46 eggert
74 * Remove lint.
76 * Revision 5.9 1991/08/19 03:13:55 eggert
77 * Add RCSINIT, -r$. Tune.
79 * Revision 5.8 1991/04/21 11:58:21 eggert
80 * Add -x, RCSINIT, MS-DOS support.
82 * Revision 5.7 1990/12/13 06:54:07 eggert
83 * GNU diff 1.15 has -u.
85 * Revision 5.6 1990/11/01 05:03:39 eggert
86 * Remove unneeded setid check.
88 * Revision 5.5 1990/10/04 06:30:19 eggert
89 * Accumulate exit status across files.
91 * Revision 5.4 1990/09/27 01:31:43 eggert
92 * Yield 1, not EXIT_FAILURE, when diffs are found.
94 * Revision 5.3 1990/09/11 02:41:11 eggert
95 * Simplify -kkvl test.
97 * Revision 5.2 1990/09/04 17:07:19 eggert
98 * Diff's argv was too small by 1.
100 * Revision 5.1 1990/08/29 07:13:55 eggert
101 * Add -kkvl.
103 * Revision 5.0 1990/08/22 08:12:46 eggert
104 * Add -k, -V. Don't use access(). Add setuid support.
105 * Remove compile-time limits; use malloc instead.
106 * Don't pass arguments with leading '+' to diff; GNU DIFF treats them as options.
107 * Add GNU diff's flags. Make lock and temp files faster and safer.
108 * Ansify and Posixate.
110 * Revision 4.6 89/05/01 15:12:27 narten
111 * changed copyright header to reflect current distribution rules
113 * Revision 4.5 88/08/09 19:12:41 eggert
114 * Use execv(), not system(); yield exit status like diff(1)s; allow cc -R.
116 * Revision 4.4 87/12/18 11:37:46 narten
117 * changes Jay Lepreau made in the 4.3 BSD version, to add support for
118 * "-i", "-w", and "-t" flags and to permit flags to be bundled together,
119 * merged in.
121 * Revision 4.3 87/10/18 10:31:42 narten
122 * Updating version numbers. Changes relative to 1.1 actually
123 * relative to 4.1
125 * Revision 1.3 87/09/24 13:59:21 narten
126 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
127 * warnings)
129 * Revision 1.2 87/03/27 14:22:15 jenkins
130 * Port to suns
132 * Revision 4.1 83/05/03 22:13:19 wft
133 * Added default branch, option -q, exit status like diff.
134 * Added fterror() to replace faterror().
136 * Revision 3.6 83/01/15 17:52:40 wft
137 * Expanded mainprogram to handle multiple RCS files.
139 * Revision 3.5 83/01/06 09:33:45 wft
140 * Fixed passing of -c (context) option to diff.
142 * Revision 3.4 82/12/24 15:28:38 wft
143 * Added call to catchsig().
145 * Revision 3.3 82/12/10 16:08:17 wft
146 * Corrected checking of return code from diff; improved error msgs.
148 * Revision 3.2 82/12/04 13:20:09 wft
149 * replaced getdelta() with gettree(). Changed diagnostics.
151 * Revision 3.1 82/11/28 19:25:04 wft
152 * Initial revision.
155 #include "rcsbase.h"
157 #if DIFF_L
158 static char const *setup_label P((struct buf*,char const*,char const[datesize]));
159 #endif
160 static void cleanup P((void));
162 static int exitstatus;
163 static RILE *workptr;
164 static struct stat workstat;
166 mainProg(rcsdiffId, "rcsdiff", "Id: rcsdiff.c,v 5.19 1995/06/16 06:19:24 eggert Exp")
168 static char const cmdusage[] =
169 "\nrcsdiff usage: rcsdiff -ksubst -q -rrev1 [-rrev2] -Vn -xsuff -zzone [diff options] file ...";
171 int revnums; /* counter for revision numbers given */
172 char const *rev1, *rev2; /* revision numbers from command line */
173 char const *xrev1, *xrev2; /* expanded revision numbers */
174 char const *expandarg, *lexpandarg, *suffixarg, *versionarg, *zonearg;
175 #if DIFF_L
176 static struct buf labelbuf[2];
177 int file_labels;
178 char const **diff_label1, **diff_label2;
179 char date2[datesize];
180 #endif
181 char const *cov[10 + !DIFF_L];
182 char const **diffv, **diffp, **diffpend; /* argv for subsidiary diff */
183 char const **pp, *p, *diffvstr;
184 struct buf commarg;
185 struct buf numericrev; /* expanded revision number */
186 struct hshentries *gendeltas; /* deltas to be generated */
187 struct hshentry * target;
188 char *a, *dcp, **newargv;
189 int no_diff_means_no_output;
190 register c;
192 exitstatus = DIFF_SUCCESS;
194 bufautobegin(&commarg);
195 bufautobegin(&numericrev);
196 revnums = 0;
197 rev1 = rev2 = xrev2 = 0;
198 #if DIFF_L
199 file_labels = 0;
200 #endif
201 expandarg = suffixarg = versionarg = zonearg = 0;
202 no_diff_means_no_output = true;
203 suffixes = X_DEFAULT;
206 * Room for runv extra + args [+ --binary] [+ 2 labels]
207 * + 1 file + 1 trailing null.
209 diffv = tnalloc(char const*, 1 + argc + !!OPEN_O_BINARY + 2*DIFF_L + 2);
210 diffp = diffv + 1;
211 *diffp++ = DIFF;
213 argc = getRCSINIT(argc, argv, &newargv);
214 argv = newargv;
215 while (a = *++argv, 0<--argc && *a++=='-') {
216 dcp = a;
217 while ((c = *a++)) switch (c) {
218 case 'r':
219 switch (++revnums) {
220 case 1: rev1=a; break;
221 case 2: rev2=a; break;
222 default: error("too many revision numbers");
224 goto option_handled;
225 case '-': case 'D':
226 no_diff_means_no_output = false;
227 /* fall into */
228 case 'C': case 'F': case 'I': case 'L': case 'W': case 'U':
229 #if DIFF_L
230 if (c == 'L' && file_labels++ == 2)
231 faterror("too many -L options");
232 #endif
233 *dcp++ = c;
234 if (*a)
235 do *dcp++ = *a++;
236 while (*a);
237 else {
238 if (!--argc)
239 faterror("-%c needs following argument%s",
240 c, cmdusage
242 *diffp++ = *argv++;
244 break;
245 case 'y':
246 no_diff_means_no_output = false;
247 /* fall into */
248 case 'B': case 'H':
249 case '0': case '1': case '2': case '3': case '4':
250 case '5': case '6': case '7': case '8': case '9':
251 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
252 case 'h': case 'i': case 'n': case 'p':
253 case 't': case 'u': case 'w':
254 *dcp++ = c;
255 break;
256 case 'q':
257 quietflag=true;
258 break;
259 case 'x':
260 suffixarg = *argv;
261 suffixes = *argv + 2;
262 goto option_handled;
263 case 'z':
264 zonearg = *argv;
265 zone_set(*argv + 2);
266 goto option_handled;
267 case 'T':
268 /* Ignore -T, so that RCSINIT can contain -T. */
269 if (*a)
270 goto unknown;
271 break;
272 case 'V':
273 versionarg = *argv;
274 setRCSversion(versionarg);
275 goto option_handled;
276 case 'k':
277 expandarg = *argv;
278 if (0 <= str2expmode(expandarg+2))
279 goto option_handled;
280 /* fall into */
281 default:
282 unknown:
283 error("unknown option: %s%s", *argv, cmdusage);
285 option_handled:
286 if (dcp != *argv+1) {
287 *dcp = 0;
288 *diffp++ = *argv;
290 } /* end of option processing */
292 for (pp = diffv+2, c = 0; pp<diffp; )
293 c += strlen(*pp++) + 1;
294 diffvstr = a = tnalloc(char, c + 1);
295 for (pp = diffv+2; pp<diffp; ) {
296 p = *pp++;
297 *a++ = ' ';
298 while ((*a = *p++))
299 a++;
301 *a = 0;
303 #if DIFF_L
304 diff_label1 = diff_label2 = 0;
305 if (file_labels < 2) {
306 if (!file_labels)
307 diff_label1 = diffp++;
308 diff_label2 = diffp++;
310 #endif
311 diffpend = diffp;
313 cov[1] = CO;
314 cov[2] = "-q";
315 # if !DIFF_L
316 cov[3] = "-M";
317 # endif
319 /* Now handle all pathnames. */
320 if (nerror)
321 cleanup();
322 else if (argc < 1)
323 faterror("no input file%s", cmdusage);
324 else
325 for (; 0 < argc; cleanup(), ++argv, --argc) {
326 ffree();
328 if (pairnames(argc, argv, rcsreadopen, true, false) <= 0)
329 continue;
330 diagnose("===================================================================\nRCS file: %s\n",RCSname);
331 if (!rev2) {
332 /* Make sure work file is readable, and get its status. */
333 if (!(workptr = Iopen(workname, FOPEN_R_WORK, &workstat))) {
334 eerror(workname);
335 continue;
340 gettree(); /* reads in the delta tree */
342 if (!Head) {
343 rcserror("no revisions present");
344 continue;
346 if (revnums==0 || !*rev1)
347 rev1 = Dbranch ? Dbranch : Head->num;
349 if (!fexpandsym(rev1, &numericrev, workptr)) continue;
350 if (!(target=genrevs(numericrev.string,(char *)0,(char *)0,(char *)0,&gendeltas))) continue;
351 xrev1=target->num;
352 #if DIFF_L
353 if (diff_label1)
354 *diff_label1 = setup_label(&labelbuf[0], target->num, target->date);
355 #endif
357 lexpandarg = expandarg;
358 if (revnums==2) {
359 if (!fexpandsym(
360 *rev2 ? rev2 : Dbranch ? Dbranch : Head->num,
361 &numericrev,
362 workptr
364 continue;
365 if (!(target=genrevs(numericrev.string,(char *)0,(char *)0,(char *)0,&gendeltas))) continue;
366 xrev2=target->num;
367 if (no_diff_means_no_output && xrev1 == xrev2)
368 continue;
369 } else if (
370 target->lockedby
371 && !lexpandarg
372 && Expand == KEYVAL_EXPAND
373 && WORKMODE(RCSstat.st_mode,true) == workstat.st_mode
375 lexpandarg = "-kkvl";
376 Izclose(&workptr);
377 #if DIFF_L
378 if (diff_label2)
379 if (revnums == 2)
380 *diff_label2 = setup_label(&labelbuf[1], target->num, target->date);
381 else {
382 time2date(workstat.st_mtime, date2);
383 *diff_label2 = setup_label(&labelbuf[1], (char*)0, date2);
385 #endif
387 diagnose("retrieving revision %s\n", xrev1);
388 bufscpy(&commarg, "-p");
389 bufscat(&commarg, rev1); /* not xrev1, for $Name's sake */
391 pp = &cov[3 + !DIFF_L];
392 *pp++ = commarg.string;
393 if (lexpandarg) *pp++ = lexpandarg;
394 if (suffixarg) *pp++ = suffixarg;
395 if (versionarg) *pp++ = versionarg;
396 if (zonearg) *pp++ = zonearg;
397 *pp++ = RCSname;
398 *pp = 0;
400 diffp = diffpend;
401 # if OPEN_O_BINARY
402 if (Expand == BINARY_EXPAND)
403 *diffp++ = "--binary";
404 # endif
405 diffp[0] = maketemp(0);
406 if (runv(-1, diffp[0], cov)) {
407 rcserror("co failed");
408 continue;
410 if (!rev2) {
411 diffp[1] = workname;
412 if (*workname == '-') {
413 char *dp = ftnalloc(char, strlen(workname)+3);
414 diffp[1] = dp;
415 *dp++ = '.';
416 *dp++ = SLASH;
417 VOID strcpy(dp, workname);
419 } else {
420 diagnose("retrieving revision %s\n",xrev2);
421 bufscpy(&commarg, "-p");
422 bufscat(&commarg, rev2); /* not xrev2, for $Name's sake */
423 cov[3 + !DIFF_L] = commarg.string;
424 diffp[1] = maketemp(1);
425 if (runv(-1, diffp[1], cov)) {
426 rcserror("co failed");
427 continue;
430 if (!rev2)
431 diagnose("diff%s -r%s %s\n", diffvstr, xrev1, workname);
432 else
433 diagnose("diff%s -r%s -r%s\n", diffvstr, xrev1, xrev2);
435 diffp[2] = 0;
436 switch (runv(-1, (char*)0, diffv)) {
437 case DIFF_SUCCESS:
438 break;
439 case DIFF_FAILURE:
440 if (exitstatus == DIFF_SUCCESS)
441 exitstatus = DIFF_FAILURE;
442 break;
443 default:
444 workerror("diff failed");
448 tempunlink();
449 exitmain(exitstatus);
452 static void
453 cleanup()
455 if (nerror) exitstatus = DIFF_TROUBLE;
456 Izclose(&finptr);
457 Izclose(&workptr);
460 #if RCS_lint
461 # define exiterr rdiffExit
462 #endif
463 void
464 exiterr()
466 tempunlink();
467 _exit(DIFF_TROUBLE);
470 #if DIFF_L
471 static char const *
472 setup_label(b, num, date)
473 struct buf *b;
474 char const *num;
475 char const date[datesize];
477 char *p;
478 char datestr[datesize + zonelenmax];
479 VOID date2str(date, datestr);
480 bufalloc(b,
481 strlen(workname)
482 + sizeof datestr + 4
483 + (num ? strlen(num) : 0)
485 p = b->string;
486 if (num)
487 VOID sprintf(p, "-L%s\t%s\t%s", workname, datestr, num);
488 else
489 VOID sprintf(p, "-L%s\t%s", workname, datestr);
490 return p;
492 #endif