1*86d7f5d3SJohn Marino /* Compare working files, ignoring RCS keyword strings. */
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino /*****************************************************************************
4*86d7f5d3SJohn Marino * rcsfcmp()
5*86d7f5d3SJohn Marino * Testprogram: define FCMPTEST
6*86d7f5d3SJohn Marino *****************************************************************************
7*86d7f5d3SJohn Marino */
8*86d7f5d3SJohn Marino
9*86d7f5d3SJohn Marino /* Copyright 1982, 1988, 1989 Walter Tichy
10*86d7f5d3SJohn Marino Copyright 1990, 1991, 1992, 1993, 1994, 1995 Paul Eggert
11*86d7f5d3SJohn Marino Distributed under license by the Free Software Foundation, Inc.
12*86d7f5d3SJohn Marino
13*86d7f5d3SJohn Marino This file is part of RCS.
14*86d7f5d3SJohn Marino
15*86d7f5d3SJohn Marino RCS is free software; you can redistribute it and/or modify
16*86d7f5d3SJohn Marino it under the terms of the GNU General Public License as published by
17*86d7f5d3SJohn Marino the Free Software Foundation; either version 2, or (at your option)
18*86d7f5d3SJohn Marino any later version.
19*86d7f5d3SJohn Marino
20*86d7f5d3SJohn Marino RCS is distributed in the hope that it will be useful,
21*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
22*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23*86d7f5d3SJohn Marino GNU General Public License for more details.
24*86d7f5d3SJohn Marino
25*86d7f5d3SJohn Marino You should have received a copy of the GNU General Public License
26*86d7f5d3SJohn Marino along with RCS; see the file COPYING.
27*86d7f5d3SJohn Marino If not, write to the Free Software Foundation,
28*86d7f5d3SJohn Marino 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29*86d7f5d3SJohn Marino
30*86d7f5d3SJohn Marino Report problems and direct all questions to:
31*86d7f5d3SJohn Marino
32*86d7f5d3SJohn Marino rcs-bugs@cs.purdue.edu
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino */
35*86d7f5d3SJohn Marino
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino
38*86d7f5d3SJohn Marino
39*86d7f5d3SJohn Marino
40*86d7f5d3SJohn Marino /*
41*86d7f5d3SJohn Marino * $FreeBSD: src/gnu/usr.bin/rcs/lib/rcsfcmp.c,v 1.7 1999/08/27 23:36:45 peter Exp $
42*86d7f5d3SJohn Marino * $DragonFly: src/gnu/usr.bin/rcs/lib/rcsfcmp.c,v 1.2 2003/06/17 04:25:47 dillon Exp $
43*86d7f5d3SJohn Marino *
44*86d7f5d3SJohn Marino * Revision 5.14 1995/06/16 06:19:24 eggert
45*86d7f5d3SJohn Marino * Update FSF address.
46*86d7f5d3SJohn Marino *
47*86d7f5d3SJohn Marino * Revision 5.13 1995/06/01 16:23:43 eggert
48*86d7f5d3SJohn Marino * (rcsfcmp): Add -kb support.
49*86d7f5d3SJohn Marino *
50*86d7f5d3SJohn Marino * Revision 5.12 1994/03/17 14:05:48 eggert
51*86d7f5d3SJohn Marino * Normally calculate the $Log prefix from context, not from RCS file.
52*86d7f5d3SJohn Marino * Calculate line numbers correctly even if the $Log prefix contains newlines.
53*86d7f5d3SJohn Marino * Remove lint.
54*86d7f5d3SJohn Marino *
55*86d7f5d3SJohn Marino * Revision 5.11 1993/11/03 17:42:27 eggert
56*86d7f5d3SJohn Marino * Fix yet another off-by-one error when comparing Log string expansions.
57*86d7f5d3SJohn Marino *
58*86d7f5d3SJohn Marino * Revision 5.10 1992/07/28 16:12:44 eggert
59*86d7f5d3SJohn Marino * Statement macro names now end in _.
60*86d7f5d3SJohn Marino *
61*86d7f5d3SJohn Marino * Revision 5.9 1991/10/07 17:32:46 eggert
62*86d7f5d3SJohn Marino * Count log lines correctly.
63*86d7f5d3SJohn Marino *
64*86d7f5d3SJohn Marino * Revision 5.8 1991/08/19 03:13:55 eggert
65*86d7f5d3SJohn Marino * Tune.
66*86d7f5d3SJohn Marino *
67*86d7f5d3SJohn Marino * Revision 5.7 1991/04/21 11:58:22 eggert
68*86d7f5d3SJohn Marino * Fix errno bug. Add MS-DOS support.
69*86d7f5d3SJohn Marino *
70*86d7f5d3SJohn Marino * Revision 5.6 1991/02/28 19:18:47 eggert
71*86d7f5d3SJohn Marino * Open work file at most once.
72*86d7f5d3SJohn Marino *
73*86d7f5d3SJohn Marino * Revision 5.5 1990/11/27 09:26:05 eggert
74*86d7f5d3SJohn Marino * Fix comment leader bug.
75*86d7f5d3SJohn Marino *
76*86d7f5d3SJohn Marino * Revision 5.4 1990/11/01 05:03:42 eggert
77*86d7f5d3SJohn Marino * Permit arbitrary data in logs and comment leaders.
78*86d7f5d3SJohn Marino *
79*86d7f5d3SJohn Marino * Revision 5.3 1990/09/11 02:41:15 eggert
80*86d7f5d3SJohn Marino * Don't ignore differences inside keyword strings if -ko is set.
81*86d7f5d3SJohn Marino *
82*86d7f5d3SJohn Marino * Revision 5.1 1990/08/29 07:13:58 eggert
83*86d7f5d3SJohn Marino * Clean old log messages too.
84*86d7f5d3SJohn Marino *
85*86d7f5d3SJohn Marino * Revision 5.0 1990/08/22 08:12:49 eggert
86*86d7f5d3SJohn Marino * Don't append "checked in with -k by " log to logs,
87*86d7f5d3SJohn Marino * so that checking in a program with -k doesn't change it.
88*86d7f5d3SJohn Marino * Ansify and Posixate. Remove lint.
89*86d7f5d3SJohn Marino *
90*86d7f5d3SJohn Marino * Revision 4.5 89/05/01 15:12:42 narten
91*86d7f5d3SJohn Marino * changed copyright header to reflect current distribution rules
92*86d7f5d3SJohn Marino *
93*86d7f5d3SJohn Marino * Revision 4.4 88/08/09 19:12:50 eggert
94*86d7f5d3SJohn Marino * Shrink stdio code size.
95*86d7f5d3SJohn Marino *
96*86d7f5d3SJohn Marino * Revision 4.3 87/12/18 11:40:02 narten
97*86d7f5d3SJohn Marino * lint cleanups (Guy Harris)
98*86d7f5d3SJohn Marino *
99*86d7f5d3SJohn Marino * Revision 4.2 87/10/18 10:33:06 narten
100*86d7f5d3SJohn Marino * updting version number. Changes relative to 1.1 actually relative to
101*86d7f5d3SJohn Marino * 4.1
102*86d7f5d3SJohn Marino *
103*86d7f5d3SJohn Marino * Revision 1.2 87/03/27 14:22:19 jenkins
104*86d7f5d3SJohn Marino * Port to suns
105*86d7f5d3SJohn Marino *
106*86d7f5d3SJohn Marino * Revision 4.1 83/05/10 16:24:04 wft
107*86d7f5d3SJohn Marino * Marker matching now uses trymatch(). Marker pattern is now
108*86d7f5d3SJohn Marino * checked precisely.
109*86d7f5d3SJohn Marino *
110*86d7f5d3SJohn Marino * Revision 3.1 82/12/04 13:21:40 wft
111*86d7f5d3SJohn Marino * Initial revision.
112*86d7f5d3SJohn Marino *
113*86d7f5d3SJohn Marino */
114*86d7f5d3SJohn Marino
115*86d7f5d3SJohn Marino /*
116*86d7f5d3SJohn Marino #define FCMPTEST
117*86d7f5d3SJohn Marino */
118*86d7f5d3SJohn Marino /* Testprogram; prints out whether two files are identical,
119*86d7f5d3SJohn Marino * except for keywords
120*86d7f5d3SJohn Marino */
121*86d7f5d3SJohn Marino
122*86d7f5d3SJohn Marino #include "rcsbase.h"
123*86d7f5d3SJohn Marino
124*86d7f5d3SJohn Marino libId(fcmpId, "$DragonFly: src/gnu/usr.bin/rcs/lib/rcsfcmp.c,v 1.2 2003/06/17 04:25:47 dillon Exp $")
125*86d7f5d3SJohn Marino
126*86d7f5d3SJohn Marino static int discardkeyval P((int,RILE*));
127*86d7f5d3SJohn Marino static int
discardkeyval(c,f)128*86d7f5d3SJohn Marino discardkeyval(c, f)
129*86d7f5d3SJohn Marino register int c;
130*86d7f5d3SJohn Marino register RILE *f;
131*86d7f5d3SJohn Marino {
132*86d7f5d3SJohn Marino for (;;)
133*86d7f5d3SJohn Marino switch (c) {
134*86d7f5d3SJohn Marino case KDELIM:
135*86d7f5d3SJohn Marino case '\n':
136*86d7f5d3SJohn Marino return c;
137*86d7f5d3SJohn Marino default:
138*86d7f5d3SJohn Marino Igeteof_(f, c, return EOF;)
139*86d7f5d3SJohn Marino break;
140*86d7f5d3SJohn Marino }
141*86d7f5d3SJohn Marino }
142*86d7f5d3SJohn Marino
143*86d7f5d3SJohn Marino int
rcsfcmp(xfp,xstatp,uname,delta)144*86d7f5d3SJohn Marino rcsfcmp(xfp, xstatp, uname, delta)
145*86d7f5d3SJohn Marino register RILE *xfp;
146*86d7f5d3SJohn Marino struct stat const *xstatp;
147*86d7f5d3SJohn Marino char const *uname;
148*86d7f5d3SJohn Marino struct hshentry const *delta;
149*86d7f5d3SJohn Marino /* Compare the files xfp and uname. Return zero
150*86d7f5d3SJohn Marino * if xfp has the same contents as uname and neither has keywords,
151*86d7f5d3SJohn Marino * otherwise -1 if they are the same ignoring keyword values,
152*86d7f5d3SJohn Marino * and 1 if they differ even ignoring
153*86d7f5d3SJohn Marino * keyword values. For the LOG-keyword, rcsfcmp skips the log message
154*86d7f5d3SJohn Marino * given by the parameter delta in xfp. Thus, rcsfcmp returns nonpositive
155*86d7f5d3SJohn Marino * if xfp contains the same as uname, with the keywords expanded.
156*86d7f5d3SJohn Marino * Implementation: character-by-character comparison until $ is found.
157*86d7f5d3SJohn Marino * If a $ is found, read in the marker keywords; if they are real keywords
158*86d7f5d3SJohn Marino * and identical, read in keyword value. If value is terminated properly,
159*86d7f5d3SJohn Marino * disregard it and optionally skip log message; otherwise, compare value.
160*86d7f5d3SJohn Marino */
161*86d7f5d3SJohn Marino {
162*86d7f5d3SJohn Marino register int xc, uc;
163*86d7f5d3SJohn Marino char xkeyword[keylength+2];
164*86d7f5d3SJohn Marino int eqkeyvals;
165*86d7f5d3SJohn Marino register RILE *ufp;
166*86d7f5d3SJohn Marino register int xeof, ueof;
167*86d7f5d3SJohn Marino register char * tp;
168*86d7f5d3SJohn Marino register char const *sp;
169*86d7f5d3SJohn Marino register size_t leaderlen;
170*86d7f5d3SJohn Marino int result;
171*86d7f5d3SJohn Marino enum markers match1;
172*86d7f5d3SJohn Marino struct stat ustat;
173*86d7f5d3SJohn Marino
174*86d7f5d3SJohn Marino if (!(ufp = Iopen(uname, FOPEN_R_WORK, &ustat))) {
175*86d7f5d3SJohn Marino efaterror(uname);
176*86d7f5d3SJohn Marino }
177*86d7f5d3SJohn Marino xeof = ueof = false;
178*86d7f5d3SJohn Marino if (MIN_UNEXPAND <= Expand) {
179*86d7f5d3SJohn Marino if (!(result = xstatp->st_size!=ustat.st_size)) {
180*86d7f5d3SJohn Marino # if large_memory && maps_memory
181*86d7f5d3SJohn Marino result = !!memcmp(xfp->base,ufp->base,(size_t)xstatp->st_size);
182*86d7f5d3SJohn Marino # else
183*86d7f5d3SJohn Marino for (;;) {
184*86d7f5d3SJohn Marino /* get the next characters */
185*86d7f5d3SJohn Marino Igeteof_(xfp, xc, xeof=true;)
186*86d7f5d3SJohn Marino Igeteof_(ufp, uc, ueof=true;)
187*86d7f5d3SJohn Marino if (xeof | ueof)
188*86d7f5d3SJohn Marino goto eof;
189*86d7f5d3SJohn Marino if (xc != uc)
190*86d7f5d3SJohn Marino goto return1;
191*86d7f5d3SJohn Marino }
192*86d7f5d3SJohn Marino # endif
193*86d7f5d3SJohn Marino }
194*86d7f5d3SJohn Marino } else {
195*86d7f5d3SJohn Marino xc = 0;
196*86d7f5d3SJohn Marino uc = 0; /* Keep lint happy. */
197*86d7f5d3SJohn Marino leaderlen = 0;
198*86d7f5d3SJohn Marino result = 0;
199*86d7f5d3SJohn Marino
200*86d7f5d3SJohn Marino for (;;) {
201*86d7f5d3SJohn Marino if (xc != KDELIM) {
202*86d7f5d3SJohn Marino /* get the next characters */
203*86d7f5d3SJohn Marino Igeteof_(xfp, xc, xeof=true;)
204*86d7f5d3SJohn Marino Igeteof_(ufp, uc, ueof=true;)
205*86d7f5d3SJohn Marino if (xeof | ueof)
206*86d7f5d3SJohn Marino goto eof;
207*86d7f5d3SJohn Marino } else {
208*86d7f5d3SJohn Marino /* try to get both keywords */
209*86d7f5d3SJohn Marino tp = xkeyword;
210*86d7f5d3SJohn Marino for (;;) {
211*86d7f5d3SJohn Marino Igeteof_(xfp, xc, xeof=true;)
212*86d7f5d3SJohn Marino Igeteof_(ufp, uc, ueof=true;)
213*86d7f5d3SJohn Marino if (xeof | ueof)
214*86d7f5d3SJohn Marino goto eof;
215*86d7f5d3SJohn Marino if (xc != uc)
216*86d7f5d3SJohn Marino break;
217*86d7f5d3SJohn Marino switch (xc) {
218*86d7f5d3SJohn Marino default:
219*86d7f5d3SJohn Marino if (xkeyword+keylength <= tp)
220*86d7f5d3SJohn Marino break;
221*86d7f5d3SJohn Marino *tp++ = xc;
222*86d7f5d3SJohn Marino continue;
223*86d7f5d3SJohn Marino case '\n': case KDELIM: case VDELIM:
224*86d7f5d3SJohn Marino break;
225*86d7f5d3SJohn Marino }
226*86d7f5d3SJohn Marino break;
227*86d7f5d3SJohn Marino }
228*86d7f5d3SJohn Marino if (
229*86d7f5d3SJohn Marino (xc==KDELIM || xc==VDELIM) && (uc==KDELIM || uc==VDELIM) &&
230*86d7f5d3SJohn Marino (*tp = xc, (match1 = trymatch(xkeyword)) != Nomatch)
231*86d7f5d3SJohn Marino ) {
232*86d7f5d3SJohn Marino #ifdef FCMPTEST
233*86d7f5d3SJohn Marino VOID printf("found common keyword %s\n",xkeyword);
234*86d7f5d3SJohn Marino #endif
235*86d7f5d3SJohn Marino result = -1;
236*86d7f5d3SJohn Marino for (;;) {
237*86d7f5d3SJohn Marino if (xc != uc) {
238*86d7f5d3SJohn Marino xc = discardkeyval(xc, xfp);
239*86d7f5d3SJohn Marino uc = discardkeyval(uc, ufp);
240*86d7f5d3SJohn Marino if ((xeof = xc==EOF) | (ueof = uc==EOF))
241*86d7f5d3SJohn Marino goto eof;
242*86d7f5d3SJohn Marino eqkeyvals = false;
243*86d7f5d3SJohn Marino break;
244*86d7f5d3SJohn Marino }
245*86d7f5d3SJohn Marino switch (xc) {
246*86d7f5d3SJohn Marino default:
247*86d7f5d3SJohn Marino Igeteof_(xfp, xc, xeof=true;)
248*86d7f5d3SJohn Marino Igeteof_(ufp, uc, ueof=true;)
249*86d7f5d3SJohn Marino if (xeof | ueof)
250*86d7f5d3SJohn Marino goto eof;
251*86d7f5d3SJohn Marino continue;
252*86d7f5d3SJohn Marino
253*86d7f5d3SJohn Marino case '\n': case KDELIM:
254*86d7f5d3SJohn Marino eqkeyvals = true;
255*86d7f5d3SJohn Marino break;
256*86d7f5d3SJohn Marino }
257*86d7f5d3SJohn Marino break;
258*86d7f5d3SJohn Marino }
259*86d7f5d3SJohn Marino if (xc != uc)
260*86d7f5d3SJohn Marino goto return1;
261*86d7f5d3SJohn Marino if (xc==KDELIM) {
262*86d7f5d3SJohn Marino /* Skip closing KDELIM. */
263*86d7f5d3SJohn Marino Igeteof_(xfp, xc, xeof=true;)
264*86d7f5d3SJohn Marino Igeteof_(ufp, uc, ueof=true;)
265*86d7f5d3SJohn Marino if (xeof | ueof)
266*86d7f5d3SJohn Marino goto eof;
267*86d7f5d3SJohn Marino /* if the keyword is LOG, also skip the log message in xfp*/
268*86d7f5d3SJohn Marino if (match1==Log) {
269*86d7f5d3SJohn Marino /* first, compute the number of line feeds in log msg */
270*86d7f5d3SJohn Marino int lncnt;
271*86d7f5d3SJohn Marino size_t ls, ccnt;
272*86d7f5d3SJohn Marino sp = delta->log.string;
273*86d7f5d3SJohn Marino ls = delta->log.size;
274*86d7f5d3SJohn Marino if (ls<sizeof(ciklog)-1 || memcmp(sp,ciklog,sizeof(ciklog)-1)) {
275*86d7f5d3SJohn Marino /*
276*86d7f5d3SJohn Marino * This log message was inserted. Skip its header.
277*86d7f5d3SJohn Marino * The number of newlines to skip is
278*86d7f5d3SJohn Marino * 1 + (C+1)*(1+L+1), where C is the number of newlines
279*86d7f5d3SJohn Marino * in the comment leader, and L is the number of
280*86d7f5d3SJohn Marino * newlines in the log string.
281*86d7f5d3SJohn Marino */
282*86d7f5d3SJohn Marino int c1 = 1;
283*86d7f5d3SJohn Marino for (ccnt=Comment.size; ccnt--; )
284*86d7f5d3SJohn Marino c1 += Comment.string[ccnt] == '\n';
285*86d7f5d3SJohn Marino lncnt = 2*c1 + 1;
286*86d7f5d3SJohn Marino while (ls--) if (*sp++=='\n') lncnt += c1;
287*86d7f5d3SJohn Marino for (;;) {
288*86d7f5d3SJohn Marino if (xc=='\n')
289*86d7f5d3SJohn Marino if(--lncnt==0) break;
290*86d7f5d3SJohn Marino Igeteof_(xfp, xc, goto returnresult;)
291*86d7f5d3SJohn Marino }
292*86d7f5d3SJohn Marino /* skip last comment leader */
293*86d7f5d3SJohn Marino /* Can't just skip another line here, because there may be */
294*86d7f5d3SJohn Marino /* additional characters on the line (after the Log....$) */
295*86d7f5d3SJohn Marino ccnt = RCSversion<VERSION(5) ? Comment.size : leaderlen;
296*86d7f5d3SJohn Marino do {
297*86d7f5d3SJohn Marino Igeteof_(xfp, xc, goto returnresult;)
298*86d7f5d3SJohn Marino /*
299*86d7f5d3SJohn Marino * Read to the end of the comment leader or '\n',
300*86d7f5d3SJohn Marino * whatever comes first, because the leader's
301*86d7f5d3SJohn Marino * trailing white space was probably stripped.
302*86d7f5d3SJohn Marino */
303*86d7f5d3SJohn Marino } while (ccnt-- && (xc!='\n' || --c1));
304*86d7f5d3SJohn Marino }
305*86d7f5d3SJohn Marino }
306*86d7f5d3SJohn Marino } else {
307*86d7f5d3SJohn Marino /* both end in the same character, but not a KDELIM */
308*86d7f5d3SJohn Marino /* must compare string values.*/
309*86d7f5d3SJohn Marino #ifdef FCMPTEST
310*86d7f5d3SJohn Marino VOID printf("non-terminated keywords %s, potentially different values\n",xkeyword);
311*86d7f5d3SJohn Marino #endif
312*86d7f5d3SJohn Marino if (!eqkeyvals)
313*86d7f5d3SJohn Marino goto return1;
314*86d7f5d3SJohn Marino }
315*86d7f5d3SJohn Marino }
316*86d7f5d3SJohn Marino }
317*86d7f5d3SJohn Marino if (xc != uc)
318*86d7f5d3SJohn Marino goto return1;
319*86d7f5d3SJohn Marino if (xc == '\n')
320*86d7f5d3SJohn Marino leaderlen = 0;
321*86d7f5d3SJohn Marino else
322*86d7f5d3SJohn Marino leaderlen++;
323*86d7f5d3SJohn Marino }
324*86d7f5d3SJohn Marino }
325*86d7f5d3SJohn Marino
326*86d7f5d3SJohn Marino eof:
327*86d7f5d3SJohn Marino if (xeof==ueof)
328*86d7f5d3SJohn Marino goto returnresult;
329*86d7f5d3SJohn Marino return1:
330*86d7f5d3SJohn Marino result = 1;
331*86d7f5d3SJohn Marino returnresult:
332*86d7f5d3SJohn Marino Ifclose(ufp);
333*86d7f5d3SJohn Marino return result;
334*86d7f5d3SJohn Marino }
335*86d7f5d3SJohn Marino
336*86d7f5d3SJohn Marino
337*86d7f5d3SJohn Marino
338*86d7f5d3SJohn Marino #ifdef FCMPTEST
339*86d7f5d3SJohn Marino
340*86d7f5d3SJohn Marino char const cmdid[] = "rcsfcmp";
341*86d7f5d3SJohn Marino
main(argc,argv)342*86d7f5d3SJohn Marino main(argc, argv)
343*86d7f5d3SJohn Marino int argc; char *argv[];
344*86d7f5d3SJohn Marino /* first argument: comment leader; 2nd: log message, 3rd: expanded file,
345*86d7f5d3SJohn Marino * 4th: unexpanded file
346*86d7f5d3SJohn Marino */
347*86d7f5d3SJohn Marino { struct hshentry delta;
348*86d7f5d3SJohn Marino
349*86d7f5d3SJohn Marino Comment.string = argv[1];
350*86d7f5d3SJohn Marino Comment.size = strlen(argv[1]);
351*86d7f5d3SJohn Marino delta.log.string = argv[2];
352*86d7f5d3SJohn Marino delta.log.size = strlen(argv[2]);
353*86d7f5d3SJohn Marino if (rcsfcmp(Iopen(argv[3], FOPEN_R_WORK, (struct stat*)0), argv[4], &delta))
354*86d7f5d3SJohn Marino VOID printf("files are the same\n");
355*86d7f5d3SJohn Marino else VOID printf("files are different\n");
356*86d7f5d3SJohn Marino }
357*86d7f5d3SJohn Marino #endif
358