1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/* Support routines for GNU DIFF.
Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002
Free Software Foundation, Inc.
This file is part of GNU DIFF.
GNU DIFF is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU DIFF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*
This file, coming from the source code of GNU DIFF,
has been modified by Ivano Primi <ivprimi@libero.it>
so that it could be merged into the source code of Numdiff.
Numdiff is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Numdiff is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "numdiff.h"
#include <error.h>
#include <xalloc.h>
/* Use when a system call returns non-zero status.
NAME should normally be the file name. */
void
perror_with_name (char const *name)
{
error (0, errno, "%s", name);
}
/* Use when a system call returns non-zero status and that is fatal. */
void
pfatal_with_name (char const *name)
{
int e = errno;
error (EXIT_TROUBLE, e, "%s", name);
abort ();
}
/* Compare two lines (typically one from each input file)
according to the command line options.
For efficiency, this is invoked only when the lines do not match exactly
but an option like -i might cause us to ignore the difference.
Return nonzero if the lines differ. */
/*
* lines_differ() is used in "inout.c" to compare lines coming possibly
* from the same file.
*/
bool
lines_differ (char const *s1, char const *s2, int index1, int index2, argslist* argl)
{
const unsigned long fieldno_upper_limit = 8*FIELDMASK_SIZE;
register char const *f1 = s1;
register char const *f2 = s2;
char *e1, *e2, ch1, ch2;
unsigned long fieldno1, fieldno2;
int f1_is_num, f2_is_num, f1_is_blurred, f2_is_blurred;
/* Cache often-used quantities in local variables to help the compiler. */
int ignore_case = argl->optmask & _SI_MASK;
char* ifs = (index1) ? argl->ifs2 : argl->ifs1;
const struct numfmt* pnf = (index1) ? &argl->nf2 : &argl->nf1;
unsigned char* ghostmask = (index1) ? argl->ghostmask2 : argl->ghostmask1;
unsigned char* pblurmask = (index1) ? argl->pblurmask2 : argl->pblurmask1;
unsigned char* tblurmask = (index1) ? argl->tblurmask2 : argl->tblurmask1; /* s1 */
char* Ifs = (index2) ? argl->ifs2 : argl->ifs1;
const struct numfmt* Pnf = (index2) ? &argl->nf2 : &argl->nf1;
unsigned char* Ghostmask = (index2) ? argl->ghostmask2 : argl->ghostmask1;
unsigned char* Pblurmask = (index2) ? argl->pblurmask2 : argl->pblurmask1;
unsigned char* Tblurmask = (index2) ? argl->tblurmask2 : argl->tblurmask1; /* s2 */
ifs = (!ifs) ? IFS : ifs;
Ifs = (!Ifs) ? IFS : Ifs;
for (; *f1 != '\n' && (strchr (ifs, *f1)); f1++);
for (; *f2 != '\n' && (strchr (Ifs, *f2)); f2++);
fieldno1 = fieldno2 = 0;
while (*f1 != '\n' && *f2 != '\n')
{
/*
Ignore the fields selected through the option -X
*/
while ( *f1 != '\n' && fieldno1 < fieldno_upper_limit && (ghostmask[fieldno1 >> 3] & 0x80 >> (fieldno1 & 0x7)) )
{
/* First move `f1' to the begin of the next field */
for (; *f1 != '\n' && !strchr (ifs, *f1); f1++);
for (; *f1 != '\n' && (strchr (ifs, *f1)); f1++);
/* and then increment the field index */
fieldno1++;
}
if ( fieldno1 >= fieldno_upper_limit )
{
fprintf (stderr, _("*** Fatal error occurred in function %s:\n%s"),
__FUNCTION__,
_("*** a very long line has been encountered which contains\n*** too many fields to be correctly handled\n"));
exit (EXIT_TROUBLE);
}
while ( *f2 != '\n' && fieldno2 < fieldno_upper_limit && (Ghostmask[fieldno2 >> 3] & 0x80 >> (fieldno2 & 0x7)) )
{
/* First move `f2' to the begin of the next field */
for (; *f2 != '\n' && !strchr (Ifs, *f2); f2++);
for (; *f2 != '\n' && (strchr (Ifs, *f2)); f2++);
/* and then increment the field index */
fieldno2++;
}
if ( fieldno2 >= fieldno_upper_limit )
{
fprintf (stderr, _("*** Fatal error occurred in function %s:\n%s"),
__FUNCTION__,
_("*** a very long line has been encountered which contains\n*** too many fields to be correctly handled\n"));
exit (EXIT_TROUBLE);
}
if (*f1 != '\n' && *f2 != '\n')
{
/* Find the ends of the fields */
for (e1 = (char*)f1; *e1 != '\n' && !strchr (ifs, *e1); e1++);
for (e2 = (char*)f2; *e2 != '\n' && !strchr (Ifs, *e2); e2++);
/*
Mark the ends of the fields before calling acxnum().
But before doing this, save the original characters placed
just after the fields.
*/
ch1 = *e1; ch2 = *e2;
*e1 = *e2 = '\0';
/* Determine the types of the fields */
f1_is_num = acxnum (f1, pnf) >= e1;
f2_is_num = acxnum (f2, Pnf) >= e2;
/* Determine whether the fields are blurred or not */
f1_is_blurred = (tblurmask[fieldno1 >> 3] & 0x80 >> (fieldno1 & 0x7)) ||
((pblurmask[fieldno1 >> 3] & 0x80 >> (fieldno1 & 0x7)) && (f1_is_num));
f2_is_blurred = (Tblurmask[fieldno2 >> 3] & 0x80 >> (fieldno2 & 0x7)) ||
((Pblurmask[fieldno2 >> 3] & 0x80 >> (fieldno2 & 0x7)) && (f2_is_num));
if (f1_is_blurred != f2_is_blurred)
return 1;
else if ((f1_is_blurred))
{
/*
`f1' and `f2' are both blurred
*/
*e1 = ch1;
*e2 = ch2;
f1 = e1;
f2 = e2;
}
else
{
/*
Neither `f1' nor `f2' is blurred
*/
if (f1_is_num != f2_is_num)
/*
If one field is numeric but the other one not,
then the two lines are surely different :)
*/
return 1;
else if ( (f1_is_num) )
{
/*
If both fields are numeric,
then perform a reduction
to a standard numerical format before
the byte-by-byte comparison.
*/
if ( (compare_numeric_strings (f1, pnf, f2, Pnf)) )
return 1;
else
{
*e1 = ch1;
*e2 = ch2;
f1 = e1;
f2 = e2;
}
}
else
{
/*
If the fields are not numeric,
then go on with byte-by-byte comparison,
*/
*e1 = ch1;
*e2 = ch2;
if ((ignore_case))
for (; f1 < e1 && f2 < e2 && TOLOWER(*f1) == TOLOWER(*f2); f1++, f2++);
else
for (; f1 < e1 && f2 < e2 && *f1 == *f2; f1++, f2++);
if (f1 < e1 || f2 < e2)
return 1;
/*
else: We have automatically
f1 == e1 && f2 == e2
*/
}
}
/*
Move to the next field
and increase the field number
*/
for (; *f1 != '\n' && (strchr (ifs, *f1)); f1++);
fieldno1++;
for (; *f2 != '\n' && (strchr (Ifs, *f2)); f2++);
fieldno2++;
} /* End else --> if (*f1 != '\n' && *f2!= '\n') */
} /* end while (*f1 != '\n' && *f2 != '\n') */
/*
Ignore the fields selected through the option -X
*/
while ( *f1 != '\n' && fieldno1 < fieldno_upper_limit && (ghostmask[fieldno1 >> 3] & 0x80 >> (fieldno1 & 0x7)) )
{
/* First move `f1' to the begin of the next field */
for (; *f1 != '\n' && !strchr (ifs, *f1); f1++);
for (; *f1 != '\n' && (strchr (ifs, *f1)); f1++);
/* and then increment the field index */
fieldno1++;
}
if ( fieldno1 >= fieldno_upper_limit )
{
fprintf (stderr, _("*** Fatal error occurred in function %s:\n%s"),
__FUNCTION__,
_("*** a very long line has been encountered which contains\n*** too many fields to be correctly handled\n"));
exit (EXIT_TROUBLE);
}
while ( *f2 != '\n' && fieldno2 < fieldno_upper_limit && (Ghostmask[fieldno2 >> 3] & 0x80 >> (fieldno2 & 0x7)) )
{
/* First move `f2' to the begin of the next field */
for (; *f2 != '\n' && !strchr (Ifs, *f2); f2++);
for (; *f2 != '\n' && (strchr (Ifs, *f2)); f2++);
/* and then increment the field index */
fieldno2++;
}
if ( fieldno2 >= fieldno_upper_limit )
{
fprintf (stderr, _("*** Fatal error occurred in function %s:\n%s"),
__FUNCTION__,
_("*** a very long line has been encountered which contains\n*** too many fields to be correctly handled\n"));
exit (EXIT_TROUBLE);
}
return (*f1 != *f2);
}
/* Divide SCRIPT into pieces by calling HUNKFUN and
print each piece with PRINTFUN.
Both functions take one arg, an edit script.
HUNKFUN is called with the tail of the script
and returns the last link that belongs together with the start
of the tail.
PRINTFUN takes a subscript which belongs together (with a null
link at the end) and prints it. */
void
print_script (struct change *script,
void (*printfun) (struct change *))
{
struct change *next = script;
struct change *this;
while ((next))
{
this = next;
/* Disconnect them from the rest of the changes,
making them a hunk, and remember the rest for next iteration. */
next = this->link;
this->link = 0;
#ifdef _DEBUG_SCRIPT_
debug_script (this);
#endif
/* Print this hunk. */
(*printfun) (this);
/* Reconnect the script so it will all be freed properly. */
this->link = next;
}
}
/* Look at a hunk of edit script and report the range of lines in each file
that it applies to. HUNK is the start of the hunk, which is a chain
of `struct change'. The first and last line numbers of file 0 are stored in
*FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
Note that these are internal line numbers that count from 0.
If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
Return UNCHANGED if only ignorable lines are inserted or deleted,
OLD if lines of file 0 are deleted,
NEW if lines of file 1 are inserted,
and CHANGED if both kinds of changes are found. */
enum changes
analyze_hunk (struct change *hunk,
lin *first0, lin *last0,
lin *first1, lin *last1)
{
struct change *next;
lin l0, l1;
lin show_from, show_to;
show_from = show_to = 0;
*first0 = hunk->line0;
*first1 = hunk->line1;
next = hunk;
do
{
l0 = next->line0 + next->deleted - 1;
l1 = next->line1 + next->inserted - 1;
show_from += next->deleted;
show_to += next->inserted;
}
while ((next = next->link) != 0);
*last0 = l0;
*last1 = l1;
return (show_from ? OLD : UNCHANGED) | (show_to ? NEW : UNCHANGED);
}
/* Yield a new block of SIZE bytes, initialized to zero. */
void *
zalloc (size_t size)
{
void *p = xmalloc (size);
memset (p, 0, size);
return p;
}
#ifdef _DEBUG_SCRIPT_
void
debug_script (struct change *sp)
{
fflush (stdout);
for (; sp; sp = sp->link)
{
long line0 = sp->line0;
long line1 = sp->line1;
long deleted = sp->deleted;
long inserted = sp->inserted;
fprintf (stderr, "%3ld %3ld delete %ld insert %ld\n",
line0, line1, deleted, inserted);
}
fflush (stderr);
}
#endif
|