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)
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
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
43 * Revision 5.19 1995/06/16 06:19:24 eggert
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
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
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,
121 * Revision 4.3 87/10/18 10:31:42 narten
122 * Updating version numbers. Changes relative to 1.1 actually
125 * Revision 1.3 87/09/24 13:59:21 narten
126 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
129 * Revision 1.2 87/03/27 14:22:15 jenkins
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
158 static char const *setup_label
P((struct buf
*,char const*,char const[datesize
]));
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
;
176 static struct buf labelbuf
[2];
178 char const **diff_label1
, **diff_label2
;
179 char date2
[datesize
];
181 char const *cov
[10 + !DIFF_L
];
182 char const **diffv
, **diffp
, **diffpend
; /* argv for subsidiary diff */
183 char const **pp
, *p
, *diffvstr
;
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
;
192 exitstatus
= DIFF_SUCCESS
;
194 bufautobegin(&commarg
);
195 bufautobegin(&numericrev
);
197 rev1
= rev2
= xrev2
= 0;
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);
213 argc
= getRCSINIT(argc
, argv
, &newargv
);
215 while (a
= *++argv
, 0<--argc
&& *a
++=='-') {
217 while ((c
= *a
++)) switch (c
) {
220 case 1: rev1
=a
; break;
221 case 2: rev2
=a
; break;
222 default: error("too many revision numbers");
226 no_diff_means_no_output
= false;
228 case 'C': case 'F': case 'I': case 'L': case 'W': case 'U':
230 if (c
== 'L' && file_labels
++ == 2)
231 faterror("too many -L options");
239 faterror("-%c needs following argument%s",
246 no_diff_means_no_output
= false;
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':
261 suffixes
= *argv
+ 2;
268 /* Ignore -T, so that RCSINIT can contain -T. */
274 setRCSversion(versionarg
);
278 if (0 <= str2expmode(expandarg
+2))
283 error("unknown option: %s%s", *argv
, cmdusage
);
286 if (dcp
!= *argv
+1) {
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
; ) {
304 diff_label1
= diff_label2
= 0;
305 if (file_labels
< 2) {
307 diff_label1
= diffp
++;
308 diff_label2
= diffp
++;
319 /* Now handle all pathnames. */
323 faterror("no input file%s", cmdusage
);
325 for (; 0 < argc
; cleanup(), ++argv
, --argc
) {
328 if (pairnames(argc
, argv
, rcsreadopen
, true, false) <= 0)
330 diagnose("===================================================================\nRCS file: %s\n",RCSname
);
332 /* Make sure work file is readable, and get its status. */
333 if (!(workptr
= Iopen(workname
, FOPEN_R_WORK
, &workstat
))) {
340 gettree(); /* reads in the delta tree */
343 rcserror("no revisions present");
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;
354 *diff_label1
= setup_label(&labelbuf
[0], target
->num
, target
->date
);
357 lexpandarg
= expandarg
;
360 *rev2
? rev2
: Dbranch
? Dbranch
: Head
->num
,
365 if (!(target
=genrevs(numericrev
.string
,(char *)0,(char *)0,(char *)0,&gendeltas
))) continue;
367 if (no_diff_means_no_output
&& xrev1
== xrev2
)
372 && Expand
== KEYVAL_EXPAND
373 && WORKMODE(RCSstat
.st_mode
,true) == workstat
.st_mode
375 lexpandarg
= "-kkvl";
380 *diff_label2
= setup_label(&labelbuf
[1], target
->num
, target
->date
);
382 time2date(workstat
.st_mtime
, date2
);
383 *diff_label2
= setup_label(&labelbuf
[1], (char*)0, date2
);
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
;
402 if (Expand
== BINARY_EXPAND
)
403 *diffp
++ = "--binary";
405 diffp
[0] = maketemp(0);
406 if (runv(-1, diffp
[0], cov
)) {
407 rcserror("co failed");
412 if (*workname
== '-') {
413 char *dp
= ftnalloc(char, strlen(workname
)+3);
417 VOID
strcpy(dp
, workname
);
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");
431 diagnose("diff%s -r%s %s\n", diffvstr
, xrev1
, workname
);
433 diagnose("diff%s -r%s -r%s\n", diffvstr
, xrev1
, xrev2
);
436 switch (runv(-1, (char*)0, diffv
)) {
440 if (exitstatus
== DIFF_SUCCESS
)
441 exitstatus
= DIFF_FAILURE
;
444 workerror("diff failed");
449 exitmain(exitstatus
);
455 if (nerror
) exitstatus
= DIFF_TROUBLE
;
461 # define exiterr rdiffExit
472 setup_label(b
, num
, date
)
475 char const date
[datesize
];
478 char datestr
[datesize
+ zonelenmax
];
479 VOID
date2str(date
, datestr
);
483 + (num
? strlen(num
) : 0)
487 VOID
sprintf(p
, "-L%s\t%s\t%s", workname
, datestr
, num
);
489 VOID
sprintf(p
, "-L%s\t%s", workname
, datestr
);