| 1 |
/* #ifdef-format output routines for GNU DIFF. |
| 2 |
|
| 3 |
Copyright (C) 1989, 1991-1994, 2001-2002, 2004, 2006, 2009-2013, 2015-2025 |
| 4 |
Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This file is part of GNU DIFF. |
| 7 |
|
| 8 |
GNU DIFF is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY. No author or distributor |
| 10 |
accepts responsibility to anyone for the consequences of using it |
| 11 |
or for whether it serves any particular purpose or works at all, |
| 12 |
unless he says so in writing. Refer to the GNU General Public |
| 13 |
License for full details. |
| 14 |
|
| 15 |
Everyone is granted permission to copy, modify and redistribute |
| 16 |
GNU DIFF, but only under the conditions described in the |
| 17 |
GNU General Public License. A copy of this license is |
| 18 |
supposed to have been given to you along with GNU DIFF so you |
| 19 |
can know your rights and responsibilities. It should be in a |
| 20 |
file named COPYING. Among other things, the copyright notice |
| 21 |
and this notice must be preserved on all copies. */ |
| 22 |
|
| 23 |
#include "diff.h" |
| 24 |
|
| 25 |
#include <c-ctype.h> |
| 26 |
#include <xmalloca.h> |
| 27 |
|
| 28 |
struct group |
| 29 |
{ |
| 30 |
struct file_data const *file; |
| 31 |
lin from, upto; /* start and limit lines for this group of lines */ |
| 32 |
}; |
| 33 |
|
| 34 |
static char const *format_group (FILE *, char const *, char, |
| 35 |
struct group const *); |
| 36 |
static char const *do_printf_spec (FILE *, char const *, |
| 37 |
struct file_data const *, lin, |
| 38 |
struct group const *); |
| 39 |
static char const *scan_char_literal (char const *, char *); |
| 40 |
static lin groups_letter_value (struct group const *, char); |
| 41 |
static void format_ifdef (char const *, lin, lin, lin, lin); |
| 42 |
static void print_ifdef_hunk (struct change *); |
| 43 |
static void print_ifdef_lines (FILE *, char const *, struct group const *); |
| 44 |
|
| 45 |
static lin next_line0; |
| 46 |
static lin next_line1; |
| 47 |
|
| 48 |
/* Print the edit-script SCRIPT as a merged #ifdef file. */ |
| 49 |
|
| 50 |
void |
| 51 |
print_ifdef_script (struct change *script) |
| 52 |
{ |
| 53 |
next_line0 = next_line1 = - curr.file[0].prefix_lines; |
| 54 |
print_script (script, find_change, print_ifdef_hunk); |
| 55 |
if (next_line0 < curr.file[0].valid_lines |
| 56 |
|| next_line1 < curr.file[1].valid_lines) |
| 57 |
{ |
| 58 |
begin_output (); |
| 59 |
format_ifdef (group_format[UNCHANGED], |
| 60 |
next_line0, curr.file[0].valid_lines, |
| 61 |
next_line1, curr.file[1].valid_lines); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/* Print a hunk of an ifdef diff. |
| 66 |
This is a contiguous portion of a complete edit script, |
| 67 |
describing changes in consecutive lines. */ |
| 68 |
|
| 69 |
static void |
| 70 |
print_ifdef_hunk (struct change *hunk) |
| 71 |
{ |
| 72 |
/* Determine range of line numbers involved in each file. */ |
| 73 |
lin first0, last0, first1, last1; |
| 74 |
enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1); |
| 75 |
if (!changes) |
| 76 |
return; |
| 77 |
|
| 78 |
begin_output (); |
| 79 |
|
| 80 |
/* Print lines up to this change. */ |
| 81 |
if (next_line0 < first0 || next_line1 < first1) |
| 82 |
format_ifdef (group_format[UNCHANGED], |
| 83 |
next_line0, first0, |
| 84 |
next_line1, first1); |
| 85 |
|
| 86 |
/* Print this change. */ |
| 87 |
next_line0 = last0 + 1; |
| 88 |
next_line1 = last1 + 1; |
| 89 |
format_ifdef (group_format[changes], |
| 90 |
first0, next_line0, |
| 91 |
first1, next_line1); |
| 92 |
} |
| 93 |
|
| 94 |
/* Print a set of lines according to FORMAT. |
| 95 |
Lines BEG0 up to END0 are from the first file; |
| 96 |
lines BEG1 up to END1 are from the second file. */ |
| 97 |
|
| 98 |
static void |
| 99 |
format_ifdef (char const *format, lin beg0, lin end0, lin beg1, lin end1) |
| 100 |
{ |
| 101 |
format_group (outfile, format, '\0', |
| 102 |
((struct group const[]) |
| 103 |
{{.file = &curr.file[0], .from = beg0, .upto = end0}, |
| 104 |
{.file = &curr.file[1], .from = beg1, .upto = end1}})); |
| 105 |
} |
| 106 |
|
| 107 |
/* Print to file OUT a set of lines according to FORMAT. |
| 108 |
The format ends at the first free instance of ENDCHAR. |
| 109 |
Yield the address of the terminating character. |
| 110 |
GROUPS specifies which lines to print. |
| 111 |
If OUT is zero, do not actually print anything; just scan the format. */ |
| 112 |
|
| 113 |
static char const * |
| 114 |
format_group (FILE *out, char const *format, char endchar, |
| 115 |
struct group const *groups) |
| 116 |
{ |
| 117 |
char const *f = format; |
| 118 |
|
| 119 |
for (char c; (c = *f) != endchar && c; ) |
| 120 |
{ |
| 121 |
char const *f1 = ++f; |
| 122 |
if (c == '%') |
| 123 |
{ |
| 124 |
c = *f++; |
| 125 |
switch (c) |
| 126 |
{ |
| 127 |
case '%': |
| 128 |
break; |
| 129 |
|
| 130 |
case '(': |
| 131 |
/* Print if-then-else format e.g. '%(n=1?thenpart:elsepart)'. */ |
| 132 |
{ |
| 133 |
intmax_t value[2]; |
| 134 |
|
| 135 |
for (int i = 0; i < 2; i++) |
| 136 |
{ |
| 137 |
if (c_isdigit (*f)) |
| 138 |
{ |
| 139 |
char *fend; |
| 140 |
errno = 0; |
| 141 |
value[i] = strtoimax (f, &fend, 10); |
| 142 |
if (errno) |
| 143 |
goto bad_format; |
| 144 |
f = fend; |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
value[i] = groups_letter_value (groups, *f); |
| 149 |
if (value[i] < 0) |
| 150 |
goto bad_format; |
| 151 |
f++; |
| 152 |
} |
| 153 |
if (*f++ != "=?"[i]) |
| 154 |
goto bad_format; |
| 155 |
} |
| 156 |
|
| 157 |
bool equal_values = value[0] == value[1]; |
| 158 |
FILE *thenout = equal_values ? out : nullptr; |
| 159 |
FILE *elseout = equal_values ? nullptr : out; |
| 160 |
f = format_group (thenout, f, ':', groups); |
| 161 |
if (*f) |
| 162 |
{ |
| 163 |
f = format_group (elseout, f + 1, ')', groups); |
| 164 |
if (*f) |
| 165 |
f++; |
| 166 |
} |
| 167 |
} |
| 168 |
continue; |
| 169 |
|
| 170 |
case '<': |
| 171 |
/* Print lines deleted from first file. */ |
| 172 |
print_ifdef_lines (out, line_format[OLD], &groups[0]); |
| 173 |
continue; |
| 174 |
|
| 175 |
case '=': |
| 176 |
/* Print common lines. */ |
| 177 |
print_ifdef_lines (out, line_format[UNCHANGED], &groups[0]); |
| 178 |
continue; |
| 179 |
|
| 180 |
case '>': |
| 181 |
/* Print lines inserted from second file. */ |
| 182 |
print_ifdef_lines (out, line_format[NEW], &groups[1]); |
| 183 |
continue; |
| 184 |
|
| 185 |
default: |
| 186 |
f = do_printf_spec (out, f - 2, nullptr, 0, groups); |
| 187 |
if (f) |
| 188 |
continue; |
| 189 |
/* Fall through. */ |
| 190 |
bad_format: |
| 191 |
c = '%'; |
| 192 |
f = f1; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if (out) |
| 198 |
putc (c, out); |
| 199 |
} |
| 200 |
|
| 201 |
return f; |
| 202 |
} |
| 203 |
|
| 204 |
/* For the line group pair G, return the number corresponding to LETTER. |
| 205 |
Return -1 if LETTER is not a group format letter. */ |
| 206 |
static lin |
| 207 |
groups_letter_value (struct group const *g, char letter) |
| 208 |
{ |
| 209 |
switch (letter) |
| 210 |
{ |
| 211 |
case 'E': letter = 'e'; g++; break; |
| 212 |
case 'F': letter = 'f'; g++; break; |
| 213 |
case 'L': letter = 'l'; g++; break; |
| 214 |
case 'M': letter = 'm'; g++; break; |
| 215 |
case 'N': letter = 'n'; g++; break; |
| 216 |
} |
| 217 |
|
| 218 |
switch (letter) |
| 219 |
{ |
| 220 |
case 'e': return translate_line_number (g->file, g->from) - 1; |
| 221 |
case 'f': return translate_line_number (g->file, g->from); |
| 222 |
case 'l': return translate_line_number (g->file, g->upto) - 1; |
| 223 |
case 'm': return translate_line_number (g->file, g->upto); |
| 224 |
case 'n': return g->upto - g->from; |
| 225 |
default: return -1; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/* Print to file OUT, using FORMAT to print the line group GROUP. |
| 230 |
But do nothing if OUT is zero. */ |
| 231 |
static void |
| 232 |
print_ifdef_lines (FILE *out, char const *format, |
| 233 |
struct group const *group) |
| 234 |
{ |
| 235 |
if (!out) |
| 236 |
return; |
| 237 |
|
| 238 |
struct file_data const *file = group->file; |
| 239 |
char const *const *linbuf = file->linbuf; |
| 240 |
lin from = group->from, upto = group->upto; |
| 241 |
|
| 242 |
/* If possible, use a single fwrite; it's faster. */ |
| 243 |
if (!expand_tabs && format[0] == '%') |
| 244 |
{ |
| 245 |
if (format[1] == 'l' && format[2] == '\n' && !format[3] && from < upto) |
| 246 |
{ |
| 247 |
fwrite (linbuf[from], sizeof (char), |
| 248 |
linbuf[upto] + (linbuf[upto][-1] != '\n') - linbuf[from], |
| 249 |
out); |
| 250 |
return; |
| 251 |
} |
| 252 |
if (format[1] == 'L' && !format[2]) |
| 253 |
{ |
| 254 |
fwrite (linbuf[from], sizeof (char), |
| 255 |
linbuf[upto] - linbuf[from], out); |
| 256 |
return; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
for (; from < upto; from++) |
| 261 |
{ |
| 262 |
char c; |
| 263 |
char const *f = format; |
| 264 |
|
| 265 |
while ((c = *f++)) |
| 266 |
{ |
| 267 |
char const *f1 = f; |
| 268 |
if (c == '%') |
| 269 |
{ |
| 270 |
c = *f++; |
| 271 |
switch (c) |
| 272 |
{ |
| 273 |
case '%': |
| 274 |
break; |
| 275 |
|
| 276 |
case 'l': |
| 277 |
output_1_line (linbuf[from], |
| 278 |
(linbuf[from + 1] |
| 279 |
- (linbuf[from + 1][-1] == '\n')), |
| 280 |
nullptr, nullptr); |
| 281 |
continue; |
| 282 |
|
| 283 |
case 'L': |
| 284 |
output_1_line (linbuf[from], linbuf[from + 1], |
| 285 |
nullptr, nullptr); |
| 286 |
continue; |
| 287 |
|
| 288 |
default: |
| 289 |
f = do_printf_spec (out, f - 2, file, from, nullptr); |
| 290 |
if (f) |
| 291 |
continue; |
| 292 |
c = '%'; |
| 293 |
f = f1; |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
putc (c, out); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
static char const * |
| 304 |
do_printf_spec (FILE *out, char const *spec, |
| 305 |
struct file_data const *file, lin n, |
| 306 |
struct group const *groups) |
| 307 |
{ |
| 308 |
char const *f = spec; |
| 309 |
char c; |
| 310 |
|
| 311 |
/* Scan printf-style SPEC of the form %[-'0]*[0-9]*(.[0-9]*)?[cdoxX]. */ |
| 312 |
dassert (*f == '%'); |
| 313 |
f++; |
| 314 |
while ((c = *f++) == '-' || c == '\'' || c == '0') |
| 315 |
continue; |
| 316 |
while (c_isdigit (c)) |
| 317 |
c = *f++; |
| 318 |
if (c == '.') |
| 319 |
while (c_isdigit (c = *f++)) |
| 320 |
continue; |
| 321 |
char c1 = *f++; |
| 322 |
|
| 323 |
switch (c) |
| 324 |
{ |
| 325 |
case 'c': |
| 326 |
if (c1 != '\'') |
| 327 |
return nullptr; |
| 328 |
else |
| 329 |
{ |
| 330 |
char value; |
| 331 |
f = scan_char_literal (f, &value); |
| 332 |
if (!f) |
| 333 |
return nullptr; |
| 334 |
if (out) |
| 335 |
putc (value, out); |
| 336 |
} |
| 337 |
break; |
| 338 |
|
| 339 |
case 'd': case 'o': case 'x': case 'X': |
| 340 |
{ |
| 341 |
lin value; |
| 342 |
|
| 343 |
if (file) |
| 344 |
{ |
| 345 |
if (c1 != 'n') |
| 346 |
return nullptr; |
| 347 |
value = translate_line_number (file, n); |
| 348 |
} |
| 349 |
else |
| 350 |
{ |
| 351 |
value = groups_letter_value (groups, c1); |
| 352 |
if (value < 0) |
| 353 |
return nullptr; |
| 354 |
} |
| 355 |
|
| 356 |
if (out) |
| 357 |
{ |
| 358 |
/* For example, if the spec is "%3xn" and pI is "l", use the printf |
| 359 |
format spec "%3lx". Here the spec prefix is "%3". */ |
| 360 |
idx_t spec_prefix_len = f - spec - 2; |
| 361 |
idx_t pI_len = sizeof pI - 1; |
| 362 |
char *format = xmalloca (spec_prefix_len + pI_len + 2); |
| 363 |
char *p = mempcpy (format, spec, spec_prefix_len); |
| 364 |
p = stpcpy (p, pI); |
| 365 |
*p++ = c; |
| 366 |
*p = '\0'; |
| 367 |
fprintf (out, format, value); |
| 368 |
freea (format); |
| 369 |
} |
| 370 |
} |
| 371 |
break; |
| 372 |
|
| 373 |
default: |
| 374 |
return nullptr; |
| 375 |
} |
| 376 |
|
| 377 |
return f; |
| 378 |
} |
| 379 |
|
| 380 |
/* Scan the character literal represented in the string LIT; LIT points just |
| 381 |
after the initial apostrophe. Put the literal's value into *VALPTR. |
| 382 |
Yield the address of the first character after the closing apostrophe, |
| 383 |
or a null pointer if the literal is ill-formed. */ |
| 384 |
static char const * |
| 385 |
scan_char_literal (char const *lit, char *valptr) |
| 386 |
{ |
| 387 |
char const *p = lit; |
| 388 |
char value; |
| 389 |
char c = *p++; |
| 390 |
|
| 391 |
switch (c) |
| 392 |
{ |
| 393 |
case 0: |
| 394 |
case '\'': |
| 395 |
return nullptr; |
| 396 |
|
| 397 |
case '\\': |
| 398 |
value = '\0'; |
| 399 |
while ((c = *p++) != '\'') |
| 400 |
{ |
| 401 |
unsigned int digit = c - '0'; |
| 402 |
if (8 <= digit) |
| 403 |
return nullptr; |
| 404 |
value = 8 * value + digit; |
| 405 |
} |
| 406 |
ptrdiff_t digits = p - lit - 2; |
| 407 |
if (! (1 <= digits && digits <= 3)) |
| 408 |
return nullptr; |
| 409 |
break; |
| 410 |
|
| 411 |
default: |
| 412 |
value = c; |
| 413 |
if (*p++ != '\'') |
| 414 |
return nullptr; |
| 415 |
break; |
| 416 |
} |
| 417 |
|
| 418 |
*valptr = value; |
| 419 |
return p; |
| 420 |
} |