1 /* Compare RCS revisions. */
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.
7 This file is part of RCS.
9 RCS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 RCS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with RCS; see the file COPYING.
21 If not, write to the Free Software Foundation,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 Report problems and direct all questions to:
26 rcs-bugs@cs.purdue.edu
31 * $FreeBSD: src/gnu/usr.bin/rcs/rcsdiff/rcsdiff.c,v 1.8 1999/08/27 23:36:55 peter Exp $
32 * $DragonFly: src/gnu/usr.bin/rcs/rcsdiff/rcsdiff.c,v 1.2 2003/06/17 04:25:48 dillon Exp $
34 * Revision 5.19 1995/06/16 06:19:24 eggert
37 * Revision 5.18 1995/06/01 16:23:43 eggert
38 * (main): Pass "--binary" if -kb and if --binary makes a difference.
39 * Don't treat + options specially.
41 * Revision 5.17 1994/03/17 14:05:48 eggert
42 * Specify subprocess input via file descriptor, not file name. Remove lint.
44 * Revision 5.16 1993/11/09 17:40:15 eggert
45 * -V now prints version on stdout and exits. Don't print usage twice.
47 * Revision 5.15 1993/11/03 17:42:27 eggert
48 * Add -z. Ignore -T. Pass -Vn to `co'. Add Name keyword.
49 * Put revision numbers in -c output. Improve quality of diagnostics.
51 * Revision 5.14 1992/07/28 16:12:44 eggert
52 * Add -V. Use co -M for better dates with traditional diff -c.
54 * Revision 5.13 1992/02/17 23:02:23 eggert
55 * Output more readable context diff headers.
56 * Suppress needless checkout and comparison of identical revisions.
58 * Revision 5.12 1992/01/24 18:44:19 eggert
59 * Add GNU diff 1.15.2's new options. lint -> RCS_lint
61 * Revision 5.11 1992/01/06 02:42:34 eggert
62 * Update usage string.
64 * Revision 5.10 1991/10/07 17:32:46 eggert
67 * Revision 5.9 1991/08/19 03:13:55 eggert
68 * Add RCSINIT, -r$. Tune.
70 * Revision 5.8 1991/04/21 11:58:21 eggert
71 * Add -x, RCSINIT, MS-DOS support.
73 * Revision 5.7 1990/12/13 06:54:07 eggert
74 * GNU diff 1.15 has -u.
76 * Revision 5.6 1990/11/01 05:03:39 eggert
77 * Remove unneeded setid check.
79 * Revision 5.5 1990/10/04 06:30:19 eggert
80 * Accumulate exit status across files.
82 * Revision 5.4 1990/09/27 01:31:43 eggert
83 * Yield 1, not EXIT_FAILURE, when diffs are found.
85 * Revision 5.3 1990/09/11 02:41:11 eggert
86 * Simplify -kkvl test.
88 * Revision 5.2 1990/09/04 17:07:19 eggert
89 * Diff's argv was too small by 1.
91 * Revision 5.1 1990/08/29 07:13:55 eggert
94 * Revision 5.0 1990/08/22 08:12:46 eggert
95 * Add -k, -V. Don't use access(). Add setuid support.
96 * Remove compile-time limits; use malloc instead.
97 * Don't pass arguments with leading '+' to diff; GNU DIFF treats them as options.
98 * Add GNU diff's flags. Make lock and temp files faster and safer.
99 * Ansify and Posixate.
101 * Revision 4.6 89/05/01 15:12:27 narten
102 * changed copyright header to reflect current distribution rules
104 * Revision 4.5 88/08/09 19:12:41 eggert
105 * Use execv(), not system(); yield exit status like diff(1)s; allow cc -R.
107 * Revision 4.4 87/12/18 11:37:46 narten
108 * changes Jay Lepreau made in the 4.3 BSD version, to add support for
109 * "-i", "-w", and "-t" flags and to permit flags to be bundled together,
112 * Revision 4.3 87/10/18 10:31:42 narten
113 * Updating version numbers. Changes relative to 1.1 actually
116 * Revision 1.3 87/09/24 13:59:21 narten
117 * Sources now pass through lint (if you ignore printf/sprintf/fprintf
120 * Revision 1.2 87/03/27 14:22:15 jenkins
123 * Revision 4.1 83/05/03 22:13:19 wft
124 * Added default branch, option -q, exit status like diff.
125 * Added fterror() to replace faterror().
127 * Revision 3.6 83/01/15 17:52:40 wft
128 * Expanded mainprogram to handle multiple RCS files.
130 * Revision 3.5 83/01/06 09:33:45 wft
131 * Fixed passing of -c (context) option to diff.
133 * Revision 3.4 82/12/24 15:28:38 wft
134 * Added call to catchsig().
136 * Revision 3.3 82/12/10 16:08:17 wft
137 * Corrected checking of return code from diff; improved error msgs.
139 * Revision 3.2 82/12/04 13:20:09 wft
140 * replaced getdelta() with gettree(). Changed diagnostics.
142 * Revision 3.1 82/11/28 19:25:04 wft
149 static char const *setup_label P((struct buf*,char const*,char const[datesize]));
151 static void cleanup P((void));
153 static int exitstatus;
154 static RILE *workptr;
155 static struct stat workstat;
157 mainProg(rcsdiffId, "rcsdiff", "$DragonFly: src/gnu/usr.bin/rcs/rcsdiff/rcsdiff.c,v 1.2 2003/06/17 04:25:48 dillon Exp $")
159 static char const cmdusage[] =
160 "\nrcsdiff usage: rcsdiff -ksubst -q -rrev1 [-rrev2] -Vn -xsuff -zzone [diff options] file ...";
162 int revnums; /* counter for revision numbers given */
163 char const *rev1, *rev2; /* revision numbers from command line */
164 char const *xrev1, *xrev2; /* expanded revision numbers */
165 char const *expandarg, *lexpandarg, *suffixarg, *versionarg, *zonearg;
167 static struct buf labelbuf[2];
169 char const **diff_label1, **diff_label2;
170 char date2[datesize];
172 char const *cov[10 + !DIFF_L];
173 char const **diffv, **diffp, **diffpend; /* argv for subsidiary diff */
174 char const **pp, *p, *diffvstr;
176 struct buf numericrev; /* expanded revision number */
177 struct hshentries *gendeltas; /* deltas to be generated */
178 struct hshentry * target;
179 char *a, *dcp, **newargv;
180 int no_diff_means_no_output;
183 exitstatus = DIFF_SUCCESS;
185 bufautobegin(&commarg);
186 bufautobegin(&numericrev);
188 rev1 = rev2 = xrev2 = 0;
192 expandarg = suffixarg = versionarg = zonearg = 0;
193 no_diff_means_no_output = true;
194 suffixes = X_DEFAULT;
197 * Room for runv extra + args [+ --binary] [+ 2 labels]
198 * + 1 file + 1 trailing null.
200 diffv = tnalloc(char const*, 1 + argc + !!OPEN_O_BINARY + 2*DIFF_L + 2);
204 argc = getRCSINIT(argc, argv, &newargv);
206 while (a = *++argv, 0<--argc && *a++=='-') {
208 while ((c = *a++)) switch (c) {
211 case 1: rev1=a; break;
212 case 2: rev2=a; break;
213 default: error("too many revision numbers");
217 no_diff_means_no_output = false;
219 case 'C': case 'F': case 'I': case 'L': case 'W':
221 if (c == 'L' && file_labels++ == 2)
222 faterror("too many -L options");
230 faterror("-%c needs following argument%s",
237 no_diff_means_no_output = false;
240 case '0': case '1': case '2': case '3': case '4':
241 case '5': case '6': case '7': case '8': case '9':
242 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
243 case 'h': case 'i': case 'n': case 'p':
244 case 't': case 'u': case 'w':
252 suffixes = *argv + 2;
259 /* Ignore -T, so that RCSINIT can contain -T. */
265 setRCSversion(versionarg);
269 if (0 <= str2expmode(expandarg+2))
274 error("unknown option: %s%s", *argv, cmdusage);
277 if (dcp != *argv+1) {
281 } /* end of option processing */
283 for (pp = diffv+2, c = 0; pp<diffp; )
284 c += strlen(*pp++) + 1;
285 diffvstr = a = tnalloc(char, c + 1);
286 for (pp = diffv+2; pp<diffp; ) {
295 diff_label1 = diff_label2 = 0;
296 if (file_labels < 2) {
298 diff_label1 = diffp++;
299 diff_label2 = diffp++;
310 /* Now handle all pathnames. */
314 faterror("no input file%s", cmdusage);
316 for (; 0 < argc; cleanup(), ++argv, --argc) {
319 if (pairnames(argc, argv, rcsreadopen, true, false) <= 0)
321 diagnose("===================================================================\nRCS file: %s\n",RCSname);
323 /* Make sure work file is readable, and get its status. */
324 if (!(workptr = Iopen(workname, FOPEN_R_WORK, &workstat))) {
331 gettree(); /* reads in the delta tree */
334 rcserror("no revisions present");
337 if (revnums==0 || !*rev1)
338 rev1 = Dbranch ? Dbranch : Head->num;
340 if (!fexpandsym(rev1, &numericrev, workptr)) continue;
341 if (!(target=genrevs(numericrev.string,(char *)0,(char *)0,(char *)0,&gendeltas))) continue;
345 *diff_label1 = setup_label(&labelbuf[0], target->num, target->date);
348 lexpandarg = expandarg;
351 *rev2 ? rev2 : Dbranch ? Dbranch : Head->num,
356 if (!(target=genrevs(numericrev.string,(char *)0,(char *)0,(char *)0,&gendeltas))) continue;
358 if (no_diff_means_no_output && xrev1 == xrev2)
363 && Expand == KEYVAL_EXPAND
364 && WORKMODE(RCSstat.st_mode,true) == workstat.st_mode
366 lexpandarg = "-kkvl";
371 *diff_label2 = setup_label(&labelbuf[1], target->num, target->date);
373 time2date(workstat.st_mtime, date2);
374 *diff_label2 = setup_label(&labelbuf[1], (char*)0, date2);
378 diagnose("retrieving revision %s\n", xrev1);
379 bufscpy(&commarg, "-p");
380 bufscat(&commarg, rev1); /* not xrev1, for $Name's sake */
382 pp = &cov[3 + !DIFF_L];
383 *pp++ = commarg.string;
384 if (lexpandarg) *pp++ = lexpandarg;
385 if (suffixarg) *pp++ = suffixarg;
386 if (versionarg) *pp++ = versionarg;
387 if (zonearg) *pp++ = zonearg;
393 if (Expand == BINARY_EXPAND)
394 *diffp++ = "--binary";
396 diffp[0] = maketemp(0);
397 if (runv(-1, diffp[0], cov)) {
398 rcserror("co failed");
403 if (*workname == '-') {
404 char *dp = ftnalloc(char, strlen(workname)+3);
408 VOID strcpy(dp, workname);
411 diagnose("retrieving revision %s\n",xrev2);
412 bufscpy(&commarg, "-p");
413 bufscat(&commarg, rev2); /* not xrev2, for $Name's sake */
414 cov[3 + !DIFF_L] = commarg.string;
415 diffp[1] = maketemp(1);
416 if (runv(-1, diffp[1], cov)) {
417 rcserror("co failed");
422 diagnose("diff%s -r%s %s\n", diffvstr, xrev1, workname);
424 diagnose("diff%s -r%s -r%s\n", diffvstr, xrev1, xrev2);
427 switch (runv(-1, (char*)0, diffv)) {
431 if (exitstatus == DIFF_SUCCESS)
432 exitstatus = DIFF_FAILURE;
435 workerror("diff failed");
440 exitmain(exitstatus);
446 if (nerror) exitstatus = DIFF_TROUBLE;
452 # define exiterr rdiffExit
463 setup_label(b, num, date)
466 char const date[datesize];
469 char datestr[datesize + zonelenmax];
470 VOID date2str(date, datestr);
474 + (num ? strlen(num) : 0)
478 VOID sprintf(p, "-L%s\t%s\t%s", workname, datestr, num);
480 VOID sprintf(p, "-L%s\t%s", workname, datestr);