| 1 |
/* GNU sdiff - side-by-side merge of file differences |
| 2 |
|
| 3 |
Copyright (C) 1992-1996, 1998, 2001-2002, 2004, 2006-2007, 2009-2013, |
| 4 |
2015-2025 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This file is part of GNU DIFF. |
| 7 |
|
| 8 |
This program is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "system.h" |
| 22 |
#include "paths.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <unlocked-io.h> |
| 26 |
|
| 27 |
#include <c-ctype.h> |
| 28 |
#include <c-stack.h> |
| 29 |
#include <dirname.h> |
| 30 |
#include <diagnose.h> |
| 31 |
#include <error.h> |
| 32 |
#include <exitfail.h> |
| 33 |
#include <file-type.h> |
| 34 |
#include <getopt.h> |
| 35 |
#include <progname.h> |
| 36 |
#include <quote.h> |
| 37 |
#include <system-quote.h> |
| 38 |
#include <version-etc.h> |
| 39 |
#include <xalloc.h> |
| 40 |
#include <xstdopen.h> |
| 41 |
|
| 42 |
/* The official name of this program (e.g., no 'g' prefix). */ |
| 43 |
static char const PROGRAM_NAME[] = "sdiff"; |
| 44 |
|
| 45 |
#define AUTHORS \ |
| 46 |
_("Thomas Lord") |
| 47 |
|
| 48 |
/* Size of chunks read from files which must be parsed into lines. */ |
| 49 |
enum { SDIFF_BUFSIZE = 65536 }; |
| 50 |
|
| 51 |
static char const *editor_program = DEFAULT_EDITOR_PROGRAM; |
| 52 |
static char const **diffargv; |
| 53 |
|
| 54 |
static char *volatile tmpname; |
| 55 |
static FILE *tmp; |
| 56 |
|
| 57 |
#if HAVE_WORKING_FORK |
| 58 |
static pid_t volatile diffpid; |
| 59 |
#endif |
| 60 |
|
| 61 |
struct line_filter; |
| 62 |
|
| 63 |
static void catchsig (int); |
| 64 |
static bool edit (struct line_filter *, char const *, lin, lin, struct line_filter *, char const *, lin, lin, FILE *); |
| 65 |
static bool interact (struct line_filter *, struct line_filter *, char const *, struct line_filter *, char const *, FILE *); |
| 66 |
static void checksigs (void); |
| 67 |
static void diffarg (char const *); |
| 68 |
static _Noreturn void fatal (char const *); |
| 69 |
static _Noreturn void perror_fatal (char const *); |
| 70 |
static void trapsigs (void); |
| 71 |
static void untrapsig (int); |
| 72 |
|
| 73 |
static int const sigs[] = { |
| 74 |
#ifdef SIGHUP |
| 75 |
SIGHUP, |
| 76 |
#endif |
| 77 |
#ifdef SIGQUIT |
| 78 |
SIGQUIT, |
| 79 |
#endif |
| 80 |
#ifdef SIGTERM |
| 81 |
SIGTERM, |
| 82 |
#endif |
| 83 |
#ifdef SIGXCPU |
| 84 |
SIGXCPU, |
| 85 |
#endif |
| 86 |
#ifdef SIGXFSZ |
| 87 |
SIGXFSZ, |
| 88 |
#endif |
| 89 |
#ifdef SIGPIPE |
| 90 |
SIGPIPE, |
| 91 |
#endif |
| 92 |
SIGINT |
| 93 |
}; |
| 94 |
enum |
| 95 |
{ |
| 96 |
NUM_SIGS = sizeof sigs / sizeof *sigs, |
| 97 |
handler_index_of_SIGINT = NUM_SIGS - 1 |
| 98 |
}; |
| 99 |
|
| 100 |
typedef void (*sighandler) (int); |
| 101 |
static void signal_handler (int, sighandler); |
| 102 |
|
| 103 |
#if HAVE_SIGACTION |
| 104 |
/* Prefer 'sigaction' if available, since 'signal' can lose signals. */ |
| 105 |
static struct sigaction initial_action[NUM_SIGS]; |
| 106 |
static sighandler |
| 107 |
initial_handler (int i) |
| 108 |
{ |
| 109 |
return initial_action[i].sa_handler; |
| 110 |
} |
| 111 |
#else |
| 112 |
static sighandler initial_action[NUM_SIGS]; |
| 113 |
static sighandler |
| 114 |
initial_handler (int i) |
| 115 |
{ |
| 116 |
return initial_action[i]; |
| 117 |
} |
| 118 |
#endif |
| 119 |
|
| 120 |
static bool diraccess (char const *); |
| 121 |
static int temporary_file (void); |
| 122 |
|
| 123 |
/* Options: */ |
| 124 |
|
| 125 |
/* Name of output file if -o specified. */ |
| 126 |
static char const *output; |
| 127 |
|
| 128 |
/* Do not print common lines. */ |
| 129 |
static bool suppress_common_lines; |
| 130 |
|
| 131 |
/* Value for the long option that does not have single-letter equivalents. */ |
| 132 |
enum |
| 133 |
{ |
| 134 |
DIFF_PROGRAM_OPTION = CHAR_MAX + 1, |
| 135 |
HELP_OPTION, |
| 136 |
STRIP_TRAILING_CR_OPTION, |
| 137 |
TABSIZE_OPTION |
| 138 |
}; |
| 139 |
|
| 140 |
static char const shortopts[] = "abBdEHiI:lo:stvw:WZ"; |
| 141 |
static struct option const longopts[] = |
| 142 |
{ |
| 143 |
{"diff-program", 1, 0, DIFF_PROGRAM_OPTION}, |
| 144 |
{"expand-tabs", 0, 0, 't'}, |
| 145 |
{"help", 0, 0, HELP_OPTION}, |
| 146 |
{"ignore-all-space", 0, 0, 'W'}, /* swap W and w for historical reasons */ |
| 147 |
{"ignore-blank-lines", 0, 0, 'B'}, |
| 148 |
{"ignore-case", 0, 0, 'i'}, |
| 149 |
{"ignore-matching-lines", 1, 0, 'I'}, |
| 150 |
{"ignore-space-change", 0, 0, 'b'}, |
| 151 |
{"ignore-tab-expansion", 0, 0, 'E'}, |
| 152 |
{"ignore-trailing-space", 0, 0, 'Z'}, |
| 153 |
{"left-column", 0, 0, 'l'}, |
| 154 |
{"minimal", 0, 0, 'd'}, |
| 155 |
{"output", 1, 0, 'o'}, |
| 156 |
{"speed-large-files", 0, 0, 'H'}, |
| 157 |
{"strip-trailing-cr", 0, 0, STRIP_TRAILING_CR_OPTION}, |
| 158 |
{"suppress-common-lines", 0, 0, 's'}, |
| 159 |
{"tabsize", 1, 0, TABSIZE_OPTION}, |
| 160 |
{"text", 0, 0, 'a'}, |
| 161 |
{"version", 0, 0, 'v'}, |
| 162 |
{"width", 1, 0, 'w'}, |
| 163 |
{0, 0, 0, 0} |
| 164 |
}; |
| 165 |
|
| 166 |
static void |
| 167 |
check_stdout (void) |
| 168 |
{ |
| 169 |
if (ferror (stdout)) |
| 170 |
fatal ("write failed"); |
| 171 |
else if (fclose (stdout) != 0) |
| 172 |
perror_fatal (_("standard output")); |
| 173 |
} |
| 174 |
|
| 175 |
static char const *const option_help_msgid[] = { |
| 176 |
N_("-o, --output=FILE operate interactively, sending output to FILE"), |
| 177 |
"", |
| 178 |
N_("-i, --ignore-case consider upper- and lower-case to be the same"), |
| 179 |
N_("-E, --ignore-tab-expansion ignore changes due to tab expansion"), |
| 180 |
N_("-Z, --ignore-trailing-space ignore white space at line end"), |
| 181 |
N_("-b, --ignore-space-change ignore changes in the amount of white space"), |
| 182 |
N_("-W, --ignore-all-space ignore all white space"), |
| 183 |
N_("-B, --ignore-blank-lines ignore changes whose lines are all blank"), |
| 184 |
N_("-I, --ignore-matching-lines=RE ignore changes all whose lines match RE"), |
| 185 |
N_(" --strip-trailing-cr strip trailing carriage return on input"), |
| 186 |
N_("-a, --text treat all files as text"), |
| 187 |
"", |
| 188 |
N_("-w, --width=NUM output at most NUM (default 130) print columns"), |
| 189 |
N_("-l, --left-column output only the left column of common lines"), |
| 190 |
N_("-s, --suppress-common-lines do not output common lines"), |
| 191 |
"", |
| 192 |
N_("-t, --expand-tabs expand tabs to spaces in output"), |
| 193 |
N_(" --tabsize=NUM tab stops at every NUM (default 8) print columns"), |
| 194 |
"", |
| 195 |
N_("-d, --minimal try hard to find a smaller set of changes"), |
| 196 |
N_("-H, --speed-large-files assume large files, many scattered small changes"), |
| 197 |
N_(" --diff-program=PROGRAM use PROGRAM to compare files"), |
| 198 |
"", |
| 199 |
N_(" --help display this help and exit"), |
| 200 |
N_("-v, --version output version information and exit"), |
| 201 |
nullptr |
| 202 |
}; |
| 203 |
|
| 204 |
static void |
| 205 |
usage (void) |
| 206 |
{ |
| 207 |
printf (_("Usage: %s [OPTION]... FILE1 FILE2\n"), squote (0, program_name)); |
| 208 |
printf ("%s\n\n", |
| 209 |
_("Side-by-side merge of differences between FILE1 and FILE2.")); |
| 210 |
|
| 211 |
fputs (_("\ |
| 212 |
Mandatory arguments to long options are mandatory for short options too.\n\ |
| 213 |
"), stdout); |
| 214 |
for (char const *const *p = option_help_msgid; *p; p++) |
| 215 |
if (**p) |
| 216 |
printf (" %s\n", _(*p)); |
| 217 |
else |
| 218 |
putchar ('\n'); |
| 219 |
printf ("\n%s\n%s\n", |
| 220 |
_("If a FILE is '-', read standard input."), |
| 221 |
_("Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.")); |
| 222 |
emit_bug_reporting_address (); |
| 223 |
} |
| 224 |
|
| 225 |
/* Clean up after a signal or other failure. This function is |
| 226 |
async-signal-safe. */ |
| 227 |
static void |
| 228 |
cleanup (_GL_UNUSED int signo) |
| 229 |
{ |
| 230 |
#if HAVE_WORKING_FORK |
| 231 |
if (0 < diffpid) |
| 232 |
kill (diffpid, SIGPIPE); |
| 233 |
#endif |
| 234 |
if (tmpname) |
| 235 |
unlink (tmpname); |
| 236 |
} |
| 237 |
|
| 238 |
static _Noreturn void |
| 239 |
exiterr (void) |
| 240 |
{ |
| 241 |
cleanup (0); |
| 242 |
untrapsig (0); |
| 243 |
checksigs (); |
| 244 |
exit (EXIT_TROUBLE); |
| 245 |
} |
| 246 |
|
| 247 |
static void |
| 248 |
fatal (char const *msgid) |
| 249 |
{ |
| 250 |
error (0, 0, "%s", _(msgid)); |
| 251 |
exiterr (); |
| 252 |
} |
| 253 |
|
| 254 |
static void |
| 255 |
perror_fatal (char const *msg) |
| 256 |
{ |
| 257 |
int e = errno; |
| 258 |
checksigs (); |
| 259 |
error (0, e, "%s", msg); |
| 260 |
exiterr (); |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
check_child_status (int werrno, int wstatus, int max_ok_status, |
| 265 |
char const *subsidiary_program) |
| 266 |
{ |
| 267 |
int status = (! werrno && WIFEXITED (wstatus) |
| 268 |
? WEXITSTATUS (wstatus) |
| 269 |
: INT_MAX); |
| 270 |
|
| 271 |
if (max_ok_status < status) |
| 272 |
{ |
| 273 |
error (0, werrno, |
| 274 |
_(status == 126 |
| 275 |
? "subsidiary program %s could not be invoked" |
| 276 |
: status == 127 |
| 277 |
? "subsidiary program %s not found" |
| 278 |
: status == INT_MAX |
| 279 |
? "subsidiary program %s failed" |
| 280 |
: "subsidiary program %s failed (exit status %d)"), |
| 281 |
quote (subsidiary_program), status); |
| 282 |
exiterr (); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
static FILE * |
| 287 |
ck_fopen (char const *fname, char const *type) |
| 288 |
{ |
| 289 |
FILE *r = fopen (fname, type); |
| 290 |
if (! r) |
| 291 |
perror_fatal (squote (0, fname)); |
| 292 |
return r; |
| 293 |
} |
| 294 |
|
| 295 |
static void |
| 296 |
ck_fclose (FILE *f) |
| 297 |
{ |
| 298 |
if (fclose (f)) |
| 299 |
perror_fatal ("fclose"); |
| 300 |
} |
| 301 |
|
| 302 |
static idx_t |
| 303 |
ck_fread (char *buf, idx_t size, FILE *f) |
| 304 |
{ |
| 305 |
idx_t r = fread (buf, sizeof (char), size, f); |
| 306 |
if (r == 0 && ferror (f)) |
| 307 |
perror_fatal (_("read failed")); |
| 308 |
return r; |
| 309 |
} |
| 310 |
|
| 311 |
static void |
| 312 |
ck_fwrite (char const *buf, idx_t size, FILE *f) |
| 313 |
{ |
| 314 |
if (fwrite (buf, sizeof (char), size, f) != size) |
| 315 |
perror_fatal (_("write failed")); |
| 316 |
} |
| 317 |
|
| 318 |
static void |
| 319 |
ck_fflush (FILE *f) |
| 320 |
{ |
| 321 |
if (fflush (f) != 0) |
| 322 |
perror_fatal (_("write failed")); |
| 323 |
} |
| 324 |
|
| 325 |
static char const * |
| 326 |
expand_name (char *name, bool is_dir, char const *other_name) |
| 327 |
{ |
| 328 |
if (STREQ (name, "-")) |
| 329 |
fatal ("cannot interactively merge standard input"); |
| 330 |
if (! is_dir) |
| 331 |
return name; |
| 332 |
else |
| 333 |
{ |
| 334 |
/* Yield NAME/BASE, where BASE is OTHER_NAME's basename. */ |
| 335 |
char const *base = last_component (other_name); |
| 336 |
idx_t namelen = strlen (name), baselen = base_len (base); |
| 337 |
bool insert_slash = *last_component (name) && name[namelen - 1] != '/'; |
| 338 |
char *r = ximalloc (namelen + insert_slash + baselen + 1); |
| 339 |
char *p = stpcpy (r, name); |
| 340 |
*p = '/'; |
| 341 |
p = mempcpy (p + insert_slash, base, baselen); |
| 342 |
*p = '\0'; |
| 343 |
return r; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
struct line_filter { |
| 348 |
FILE *infile; |
| 349 |
char *bufpos; |
| 350 |
char *buffer; |
| 351 |
char *buflim; |
| 352 |
}; |
| 353 |
|
| 354 |
static void |
| 355 |
lf_init (struct line_filter *lf, FILE *infile) |
| 356 |
{ |
| 357 |
lf->infile = infile; |
| 358 |
lf->bufpos = lf->buffer = lf->buflim = ximalloc (SDIFF_BUFSIZE + 1); |
| 359 |
lf->buflim[0] = '\n'; |
| 360 |
} |
| 361 |
|
| 362 |
/* Fill an exhausted line_filter buffer from its INFILE */ |
| 363 |
static idx_t |
| 364 |
lf_refill (struct line_filter *lf) |
| 365 |
{ |
| 366 |
idx_t s = ck_fread (lf->buffer, SDIFF_BUFSIZE, lf->infile); |
| 367 |
lf->bufpos = lf->buffer; |
| 368 |
lf->buflim = lf->buffer + s; |
| 369 |
lf->buflim[0] = '\n'; |
| 370 |
checksigs (); |
| 371 |
return s; |
| 372 |
} |
| 373 |
|
| 374 |
/* Advance LINES on LF's infile, copying lines to OUTFILE */ |
| 375 |
static void |
| 376 |
lf_copy (struct line_filter *lf, lin lines, FILE *outfile) |
| 377 |
{ |
| 378 |
char *start = lf->bufpos; |
| 379 |
|
| 380 |
while (lines) |
| 381 |
{ |
| 382 |
lf->bufpos = rawmemchr (lf->bufpos, '\n'); |
| 383 |
if (lf->bufpos == lf->buflim) |
| 384 |
{ |
| 385 |
ck_fwrite (start, lf->buflim - start, outfile); |
| 386 |
if (! lf_refill (lf)) |
| 387 |
return; |
| 388 |
start = lf->bufpos; |
| 389 |
} |
| 390 |
else |
| 391 |
{ |
| 392 |
--lines; |
| 393 |
++lf->bufpos; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
ck_fwrite (start, lf->bufpos - start, outfile); |
| 398 |
} |
| 399 |
|
| 400 |
/* Advance LINES on LF's infile without doing output */ |
| 401 |
static void |
| 402 |
lf_skip (struct line_filter *lf, lin lines) |
| 403 |
{ |
| 404 |
while (lines) |
| 405 |
{ |
| 406 |
lf->bufpos = rawmemchr (lf->bufpos, '\n'); |
| 407 |
if (lf->bufpos == lf->buflim) |
| 408 |
{ |
| 409 |
if (! lf_refill (lf)) |
| 410 |
break; |
| 411 |
} |
| 412 |
else |
| 413 |
{ |
| 414 |
--lines; |
| 415 |
++lf->bufpos; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/* Snarf a line into a buffer. Return EOF if EOF, 0 if error, 1 if OK. */ |
| 421 |
static int |
| 422 |
lf_snarf (struct line_filter *lf, char *buffer, idx_t bufsize) |
| 423 |
{ |
| 424 |
for (;;) |
| 425 |
{ |
| 426 |
char *start = lf->bufpos; |
| 427 |
char *next = rawmemchr (start, '\n'); |
| 428 |
idx_t s = next - start; |
| 429 |
if (bufsize <= s) |
| 430 |
return 0; |
| 431 |
buffer = mempcpy (buffer, start, s); |
| 432 |
bufsize -= s; |
| 433 |
if (next < lf->buflim) |
| 434 |
{ |
| 435 |
*buffer = '\0'; |
| 436 |
lf->bufpos = next + 1; |
| 437 |
return 1; |
| 438 |
} |
| 439 |
if (! lf_refill (lf)) |
| 440 |
return s ? 0 : EOF; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
int |
| 445 |
main (int argc, char *argv[]) |
| 446 |
{ |
| 447 |
exit_failure = EXIT_TROUBLE; |
| 448 |
initialize_main (&argc, &argv); |
| 449 |
set_program_name (argv[0]); |
| 450 |
setlocale (LC_ALL, ""); |
| 451 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 452 |
bindtextdomain ("gnulib", GNULIB_LOCALEDIR); |
| 453 |
textdomain (PACKAGE); |
| 454 |
c_stack_action (cleanup); |
| 455 |
xstdopen (); |
| 456 |
|
| 457 |
char const *prog = getenv ("EDITOR"); |
| 458 |
if (prog) |
| 459 |
editor_program = prog; |
| 460 |
|
| 461 |
diffarg (DEFAULT_DIFF_PROGRAM); |
| 462 |
|
| 463 |
/* Parse command line options. */ |
| 464 |
|
| 465 |
for (int c; |
| 466 |
0 <= (c = getopt_long (argc, argv, shortopts, longopts, 0)); ) |
| 467 |
switch (c) |
| 468 |
{ |
| 469 |
case 'a': |
| 470 |
diffarg ("-a"); |
| 471 |
break; |
| 472 |
|
| 473 |
case 'b': |
| 474 |
diffarg ("-b"); |
| 475 |
break; |
| 476 |
|
| 477 |
case 'B': |
| 478 |
diffarg ("-B"); |
| 479 |
break; |
| 480 |
|
| 481 |
case 'd': |
| 482 |
diffarg ("-d"); |
| 483 |
break; |
| 484 |
|
| 485 |
case 'E': |
| 486 |
diffarg ("-E"); |
| 487 |
break; |
| 488 |
|
| 489 |
case 'H': |
| 490 |
diffarg ("-H"); |
| 491 |
break; |
| 492 |
|
| 493 |
case 'i': |
| 494 |
diffarg ("-i"); |
| 495 |
break; |
| 496 |
|
| 497 |
case 'I': |
| 498 |
diffarg ("-I"); |
| 499 |
diffarg (optarg); |
| 500 |
break; |
| 501 |
|
| 502 |
case 'l': |
| 503 |
diffarg ("--left-column"); |
| 504 |
break; |
| 505 |
|
| 506 |
case 'o': |
| 507 |
output = optarg; |
| 508 |
break; |
| 509 |
|
| 510 |
case 's': |
| 511 |
suppress_common_lines = true; |
| 512 |
break; |
| 513 |
|
| 514 |
case 't': |
| 515 |
diffarg ("-t"); |
| 516 |
break; |
| 517 |
|
| 518 |
case 'v': |
| 519 |
version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, |
| 520 |
AUTHORS, nullptr); |
| 521 |
check_stdout (); |
| 522 |
return EXIT_SUCCESS; |
| 523 |
|
| 524 |
case 'w': |
| 525 |
diffarg ("-W"); |
| 526 |
diffarg (optarg); |
| 527 |
break; |
| 528 |
|
| 529 |
case 'W': |
| 530 |
diffarg ("-w"); |
| 531 |
break; |
| 532 |
|
| 533 |
case 'Z': |
| 534 |
diffarg ("-Z"); |
| 535 |
break; |
| 536 |
|
| 537 |
case DIFF_PROGRAM_OPTION: |
| 538 |
diffargv[0] = optarg; |
| 539 |
break; |
| 540 |
|
| 541 |
case HELP_OPTION: |
| 542 |
usage (); |
| 543 |
check_stdout (); |
| 544 |
return EXIT_SUCCESS; |
| 545 |
|
| 546 |
case STRIP_TRAILING_CR_OPTION: |
| 547 |
diffarg ("--strip-trailing-cr"); |
| 548 |
break; |
| 549 |
|
| 550 |
case TABSIZE_OPTION: |
| 551 |
diffarg ("--tabsize"); |
| 552 |
diffarg (optarg); |
| 553 |
break; |
| 554 |
|
| 555 |
default: |
| 556 |
try_help (nullptr, nullptr); |
| 557 |
} |
| 558 |
|
| 559 |
if (argc - optind != 2) |
| 560 |
{ |
| 561 |
if (argc - optind < 2) |
| 562 |
try_help ("missing operand after %s", quote (argv[argc - 1])); |
| 563 |
else |
| 564 |
try_help ("extra operand %s", quote (argv[optind + 2])); |
| 565 |
} |
| 566 |
|
| 567 |
if (! output) |
| 568 |
{ |
| 569 |
/* easy case: diff does everything for us */ |
| 570 |
if (suppress_common_lines) |
| 571 |
diffarg ("--suppress-common-lines"); |
| 572 |
diffarg ("-y"); |
| 573 |
diffarg ("--"); |
| 574 |
diffarg (argv[optind]); |
| 575 |
diffarg (argv[optind + 1]); |
| 576 |
diffarg (nullptr); |
| 577 |
execvp (diffargv[0], (char **) diffargv); |
| 578 |
perror_fatal (squote (0, diffargv[0])); |
| 579 |
} |
| 580 |
else |
| 581 |
{ |
| 582 |
bool leftdir = diraccess (argv[optind]); |
| 583 |
bool rightdir = diraccess (argv[optind + 1]); |
| 584 |
|
| 585 |
if (leftdir & rightdir) |
| 586 |
fatal ("both files to be compared are directories"); |
| 587 |
|
| 588 |
char const *lname |
| 589 |
= expand_name (argv[optind], leftdir, argv[optind + 1]); |
| 590 |
char const *rname |
| 591 |
= expand_name (argv[optind + 1], rightdir, argv[optind]); |
| 592 |
FILE *left = ck_fopen (lname, "re"); |
| 593 |
FILE *right = ck_fopen (rname, "re"); |
| 594 |
FILE *out = ck_fopen (output, "we"); |
| 595 |
|
| 596 |
diffarg ("--sdiff-merge-assist"); |
| 597 |
diffarg ("--"); |
| 598 |
diffarg (argv[optind]); |
| 599 |
diffarg (argv[optind + 1]); |
| 600 |
diffarg (nullptr); |
| 601 |
|
| 602 |
trapsigs (); |
| 603 |
|
| 604 |
FILE *diffout; |
| 605 |
#if ! HAVE_WORKING_FORK |
| 606 |
{ |
| 607 |
char *command = system_quote_argv (SCI_SYSTEM, (char **) diffargv); |
| 608 |
errno = 0; |
| 609 |
diffout = popen (command, "r"); |
| 610 |
if (! diffout) |
| 611 |
perror_fatal (command); |
| 612 |
free (command); |
| 613 |
} |
| 614 |
#else |
| 615 |
{ |
| 616 |
int diff_fds[2]; |
| 617 |
|
| 618 |
if (pipe (diff_fds) != 0) |
| 619 |
perror_fatal ("pipe"); |
| 620 |
|
| 621 |
diffpid = fork (); |
| 622 |
if (diffpid < 0) |
| 623 |
perror_fatal ("fork"); |
| 624 |
if (! diffpid) |
| 625 |
{ |
| 626 |
/* Alter the child's SIGINT and SIGPIPE handlers; |
| 627 |
this may munge the parent. |
| 628 |
The child ignores SIGINT in case the user interrupts the editor. |
| 629 |
The child does not ignore SIGPIPE, even if the parent does. */ |
| 630 |
if (initial_handler (handler_index_of_SIGINT) != SIG_IGN) |
| 631 |
signal_handler (SIGINT, SIG_IGN); |
| 632 |
signal_handler (SIGPIPE, SIG_DFL); |
| 633 |
close (diff_fds[0]); |
| 634 |
if (diff_fds[1] != STDOUT_FILENO) |
| 635 |
{ |
| 636 |
dup2 (diff_fds[1], STDOUT_FILENO); |
| 637 |
close (diff_fds[1]); |
| 638 |
} |
| 639 |
|
| 640 |
execvp (diffargv[0], (char **) diffargv); |
| 641 |
_exit (errno == ENOENT ? 127 : 126); |
| 642 |
} |
| 643 |
|
| 644 |
close (diff_fds[1]); |
| 645 |
diffout = fdopen (diff_fds[0], "r"); |
| 646 |
if (! diffout) |
| 647 |
perror_fatal ("fdopen"); |
| 648 |
} |
| 649 |
#endif |
| 650 |
|
| 651 |
struct line_filter lfilt, rfilt, diff_filt; |
| 652 |
lf_init (&diff_filt, diffout); |
| 653 |
lf_init (&lfilt, left); |
| 654 |
lf_init (&rfilt, right); |
| 655 |
|
| 656 |
bool interact_ok |
| 657 |
= interact (&diff_filt, &lfilt, lname, &rfilt, rname, out); |
| 658 |
|
| 659 |
ck_fclose (left); |
| 660 |
ck_fclose (right); |
| 661 |
ck_fclose (out); |
| 662 |
|
| 663 |
{ |
| 664 |
int wstatus; |
| 665 |
int werrno = 0; |
| 666 |
|
| 667 |
#if ! HAVE_WORKING_FORK |
| 668 |
wstatus = pclose (diffout); |
| 669 |
if (wstatus == -1) |
| 670 |
werrno = errno; |
| 671 |
#else |
| 672 |
ck_fclose (diffout); |
| 673 |
while (waitpid (diffpid, &wstatus, 0) < 0) |
| 674 |
if (errno == EINTR) |
| 675 |
checksigs (); |
| 676 |
else |
| 677 |
perror_fatal ("waitpid"); |
| 678 |
diffpid = 0; |
| 679 |
#endif |
| 680 |
|
| 681 |
if (tmpname) |
| 682 |
{ |
| 683 |
unlink (tmpname); |
| 684 |
tmpname = nullptr; |
| 685 |
} |
| 686 |
|
| 687 |
if (! interact_ok) |
| 688 |
exiterr (); |
| 689 |
|
| 690 |
check_child_status (werrno, wstatus, EXIT_FAILURE, diffargv[0]); |
| 691 |
untrapsig (0); |
| 692 |
checksigs (); |
| 693 |
exit (WEXITSTATUS (wstatus)); |
| 694 |
} |
| 695 |
} |
| 696 |
return EXIT_SUCCESS; /* Fool '-Wall'. */ |
| 697 |
} |
| 698 |
|
| 699 |
static void |
| 700 |
diffarg (char const *a) |
| 701 |
{ |
| 702 |
static idx_t diffargs, diffarglim; |
| 703 |
|
| 704 |
if (diffargs == diffarglim) |
| 705 |
diffargv = xpalloc (diffargv, &diffarglim, 1, -1, sizeof *diffargv); |
| 706 |
diffargv[diffargs++] = a; |
| 707 |
} |
| 708 |
|
| 709 |
/* Signal handling */ |
| 710 |
|
| 711 |
static bool volatile ignore_SIGINT; |
| 712 |
static int volatile signal_received; |
| 713 |
static bool sigs_trapped; |
| 714 |
|
| 715 |
static void |
| 716 |
catchsig (int s) |
| 717 |
{ |
| 718 |
#if ! HAVE_SIGACTION |
| 719 |
signal (s, SIG_IGN); |
| 720 |
#endif |
| 721 |
if (! (s == SIGINT && ignore_SIGINT)) |
| 722 |
signal_received = s; |
| 723 |
} |
| 724 |
|
| 725 |
#if HAVE_SIGACTION |
| 726 |
static struct sigaction catchaction; |
| 727 |
#endif |
| 728 |
|
| 729 |
static void |
| 730 |
signal_handler (int sig, sighandler handler) |
| 731 |
{ |
| 732 |
#if HAVE_SIGACTION |
| 733 |
catchaction.sa_handler = handler; |
| 734 |
sigaction (sig, &catchaction, 0); |
| 735 |
#else |
| 736 |
signal (sig, handler); |
| 737 |
#endif |
| 738 |
} |
| 739 |
|
| 740 |
static void |
| 741 |
trapsigs (void) |
| 742 |
{ |
| 743 |
#if HAVE_SIGACTION |
| 744 |
catchaction.sa_flags = SA_RESTART; |
| 745 |
sigemptyset (&catchaction.sa_mask); |
| 746 |
for (int i = 0; i < NUM_SIGS; i++) |
| 747 |
sigaddset (&catchaction.sa_mask, sigs[i]); |
| 748 |
#endif |
| 749 |
|
| 750 |
for (int i = 0; i < NUM_SIGS; i++) |
| 751 |
{ |
| 752 |
#if HAVE_SIGACTION |
| 753 |
sigaction (sigs[i], nullptr, &initial_action[i]); |
| 754 |
#else |
| 755 |
initial_action[i] = signal (sigs[i], SIG_IGN); |
| 756 |
#endif |
| 757 |
if (initial_handler (i) != SIG_IGN) |
| 758 |
signal_handler (sigs[i], catchsig); |
| 759 |
} |
| 760 |
|
| 761 |
#ifdef SIGCHLD |
| 762 |
/* System V fork+wait does not work if SIGCHLD is ignored. */ |
| 763 |
signal (SIGCHLD, SIG_DFL); |
| 764 |
#endif |
| 765 |
|
| 766 |
sigs_trapped = true; |
| 767 |
} |
| 768 |
|
| 769 |
/* Untrap signal S, or all trapped signals if S is zero. */ |
| 770 |
static void |
| 771 |
untrapsig (int s) |
| 772 |
{ |
| 773 |
if (sigs_trapped) |
| 774 |
for (int i = 0; i < NUM_SIGS; i++) |
| 775 |
if ((! s || sigs[i] == s) && initial_handler (i) != SIG_IGN) |
| 776 |
{ |
| 777 |
#if HAVE_SIGACTION |
| 778 |
sigaction (sigs[i], &initial_action[i], nullptr); |
| 779 |
#else |
| 780 |
signal (sigs[i], initial_action[i]); |
| 781 |
#endif |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
/* Exit if a signal has been received. */ |
| 786 |
static void |
| 787 |
checksigs (void) |
| 788 |
{ |
| 789 |
int s = signal_received; |
| 790 |
if (s) |
| 791 |
{ |
| 792 |
cleanup (0); |
| 793 |
|
| 794 |
/* Yield an exit status indicating that a signal was received. */ |
| 795 |
untrapsig (s); |
| 796 |
raise (s); |
| 797 |
|
| 798 |
/* That didn't work, so exit with error status. */ |
| 799 |
exit (EXIT_TROUBLE); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
static void |
| 804 |
give_help (void) |
| 805 |
{ |
| 806 |
fprintf (stderr, "%s", _("\ |
| 807 |
ed:\tEdit then use both versions, each decorated with a header.\n\ |
| 808 |
eb:\tEdit then use both versions.\n\ |
| 809 |
el or e1:\tEdit then use the left version.\n\ |
| 810 |
er or e2:\tEdit then use the right version.\n\ |
| 811 |
e:\tDiscard both versions then edit a new one.\n\ |
| 812 |
l or 1:\tUse the left version.\n\ |
| 813 |
r or 2:\tUse the right version.\n\ |
| 814 |
s:\tSilently include common lines.\n\ |
| 815 |
v:\tVerbosely include common lines.\n\ |
| 816 |
q:\tQuit.\n\ |
| 817 |
")); |
| 818 |
} |
| 819 |
|
| 820 |
static int |
| 821 |
skip_white (void) |
| 822 |
{ |
| 823 |
int c; |
| 824 |
for (;;) |
| 825 |
{ |
| 826 |
c = getchar (); |
| 827 |
if (! c_isspace (c) || c == '\n') |
| 828 |
break; |
| 829 |
checksigs (); |
| 830 |
} |
| 831 |
if (ferror (stdin)) |
| 832 |
perror_fatal (_("read failed")); |
| 833 |
return c; |
| 834 |
} |
| 835 |
|
| 836 |
static void |
| 837 |
flush_line (void) |
| 838 |
{ |
| 839 |
for (int c; (c = getchar ()) != '\n' && c != EOF; ) |
| 840 |
continue; |
| 841 |
if (ferror (stdin)) |
| 842 |
perror_fatal (_("read failed")); |
| 843 |
} |
| 844 |
|
| 845 |
/* Suppress gcc's "...may be used before initialized" warnings, |
| 846 |
generated by GCC versions up to at least GCC 14.0.0 20231227. */ |
| 847 |
#if __GNUC__ + (__GNUC_MINOR__ >= 7) > 4 |
| 848 |
# pragma GCC diagnostic push |
| 849 |
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 850 |
#endif |
| 851 |
|
| 852 |
/* interpret an edit command */ |
| 853 |
static bool |
| 854 |
edit (struct line_filter *left, char const *lname, lin lline, lin llen, |
| 855 |
struct line_filter *right, char const *rname, lin rline, lin rlen, |
| 856 |
FILE *outfile) |
| 857 |
{ |
| 858 |
for (;;) |
| 859 |
{ |
| 860 |
int cmd0; |
| 861 |
int cmd1; |
| 862 |
bool gotcmd = false; |
| 863 |
|
| 864 |
while (! gotcmd) |
| 865 |
{ |
| 866 |
if (putchar ('%') != '%') |
| 867 |
perror_fatal (_("write failed")); |
| 868 |
ck_fflush (stdout); |
| 869 |
|
| 870 |
cmd0 = skip_white (); |
| 871 |
switch (cmd0) |
| 872 |
{ |
| 873 |
case '1': case '2': case 'l': case 'r': |
| 874 |
case 's': case 'v': case 'q': |
| 875 |
if (skip_white () != '\n') |
| 876 |
{ |
| 877 |
give_help (); |
| 878 |
flush_line (); |
| 879 |
continue; |
| 880 |
} |
| 881 |
gotcmd = true; |
| 882 |
break; |
| 883 |
|
| 884 |
case 'e': |
| 885 |
cmd1 = skip_white (); |
| 886 |
switch (cmd1) |
| 887 |
{ |
| 888 |
case '1': case '2': case 'b': case 'd': case 'l': case 'r': |
| 889 |
if (skip_white () != '\n') |
| 890 |
{ |
| 891 |
give_help (); |
| 892 |
flush_line (); |
| 893 |
continue; |
| 894 |
} |
| 895 |
gotcmd = true; |
| 896 |
break; |
| 897 |
case '\n': |
| 898 |
gotcmd = true; |
| 899 |
break; |
| 900 |
default: |
| 901 |
give_help (); |
| 902 |
flush_line (); |
| 903 |
continue; |
| 904 |
} |
| 905 |
break; |
| 906 |
|
| 907 |
case EOF: |
| 908 |
if (feof (stdin)) |
| 909 |
{ |
| 910 |
gotcmd = true; |
| 911 |
cmd0 = 'q'; |
| 912 |
break; |
| 913 |
} |
| 914 |
FALLTHROUGH; |
| 915 |
default: |
| 916 |
flush_line (); |
| 917 |
FALLTHROUGH; |
| 918 |
case '\n': |
| 919 |
give_help (); |
| 920 |
continue; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
switch (cmd0) |
| 925 |
{ |
| 926 |
case '1': case 'l': |
| 927 |
lf_copy (left, llen, outfile); |
| 928 |
lf_skip (right, rlen); |
| 929 |
return true; |
| 930 |
case '2': case 'r': |
| 931 |
lf_copy (right, rlen, outfile); |
| 932 |
lf_skip (left, llen); |
| 933 |
return true; |
| 934 |
case 's': |
| 935 |
suppress_common_lines = true; |
| 936 |
break; |
| 937 |
case 'v': |
| 938 |
suppress_common_lines = false; |
| 939 |
break; |
| 940 |
case 'q': |
| 941 |
return false; |
| 942 |
|
| 943 |
case 'e': |
| 944 |
if (tmpname) |
| 945 |
tmp = fopen (tmpname, "we"); |
| 946 |
else |
| 947 |
{ |
| 948 |
int fd = temporary_file (); |
| 949 |
if (fd < 0) |
| 950 |
perror_fatal ("mkstemp"); |
| 951 |
tmp = fdopen (fd, "w"); |
| 952 |
} |
| 953 |
|
| 954 |
if (! tmp) |
| 955 |
perror_fatal (squote (0, tmpname)); |
| 956 |
|
| 957 |
switch (cmd1) |
| 958 |
{ |
| 959 |
case 'd': |
| 960 |
if (llen) |
| 961 |
{ |
| 962 |
if (llen == 1) |
| 963 |
fprintf (tmp, "--- %s %"pI"d\n", lname, lline); |
| 964 |
else |
| 965 |
fprintf (tmp, "--- %s %"pI"d,%"pI"d\n", lname, lline, |
| 966 |
lline + llen - 1); |
| 967 |
} |
| 968 |
FALLTHROUGH; |
| 969 |
case '1': case 'b': case 'l': |
| 970 |
lf_copy (left, llen, tmp); |
| 971 |
break; |
| 972 |
|
| 973 |
default: |
| 974 |
lf_skip (left, llen); |
| 975 |
break; |
| 976 |
} |
| 977 |
|
| 978 |
switch (cmd1) |
| 979 |
{ |
| 980 |
case 'd': |
| 981 |
if (rlen) |
| 982 |
{ |
| 983 |
if (rlen == 1) |
| 984 |
fprintf (tmp, "+++ %s %"pI"d\n", rname, rline); |
| 985 |
else |
| 986 |
fprintf (tmp, "+++ %s %"pI"d,%"pI"d\n", rname, rline, |
| 987 |
rline + rlen - 1); |
| 988 |
} |
| 989 |
FALLTHROUGH; |
| 990 |
case '2': case 'b': case 'r': |
| 991 |
lf_copy (right, rlen, tmp); |
| 992 |
break; |
| 993 |
|
| 994 |
default: |
| 995 |
lf_skip (right, rlen); |
| 996 |
break; |
| 997 |
} |
| 998 |
|
| 999 |
ck_fclose (tmp); |
| 1000 |
|
| 1001 |
ignore_SIGINT = true; |
| 1002 |
checksigs (); |
| 1003 |
char *argv[] = { (char *) editor_program, tmpname, nullptr }; |
| 1004 |
int wstatus; |
| 1005 |
int werrno = 0; |
| 1006 |
#if ! HAVE_WORKING_FORK |
| 1007 |
char *command = system_quote_argv (SCI_SYSTEM, argv); |
| 1008 |
wstatus = system (command); |
| 1009 |
if (wstatus == -1) |
| 1010 |
werrno = errno; |
| 1011 |
free (command); |
| 1012 |
#else |
| 1013 |
pid_t pid = fork (); |
| 1014 |
if (pid == 0) |
| 1015 |
{ |
| 1016 |
execvp (editor_program, argv); |
| 1017 |
_exit (errno == ENOENT ? 127 : 126); |
| 1018 |
} |
| 1019 |
|
| 1020 |
if (pid < 0) |
| 1021 |
perror_fatal ("fork"); |
| 1022 |
|
| 1023 |
while (waitpid (pid, &wstatus, 0) < 0) |
| 1024 |
if (errno == EINTR) |
| 1025 |
checksigs (); |
| 1026 |
else |
| 1027 |
perror_fatal ("waitpid"); |
| 1028 |
#endif |
| 1029 |
ignore_SIGINT = false; |
| 1030 |
check_child_status (werrno, wstatus, EXIT_SUCCESS, |
| 1031 |
editor_program); |
| 1032 |
|
| 1033 |
char buf[SDIFF_BUFSIZE]; |
| 1034 |
tmp = ck_fopen (tmpname, "re"); |
| 1035 |
for (idx_t size; |
| 1036 |
(size = ck_fread (buf, SDIFF_BUFSIZE, tmp)) != 0; ) |
| 1037 |
{ |
| 1038 |
checksigs (); |
| 1039 |
ck_fwrite (buf, size, outfile); |
| 1040 |
} |
| 1041 |
ck_fclose (tmp); |
| 1042 |
return true; |
| 1043 |
|
| 1044 |
default: |
| 1045 |
give_help (); |
| 1046 |
break; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
#if __GNUC__ + (__GNUC_MINOR__ >= 7) > 4 |
| 1052 |
# pragma GCC diagnostic pop |
| 1053 |
#endif |
| 1054 |
|
| 1055 |
/* Alternately reveal bursts of diff output and handle user commands. */ |
| 1056 |
static bool |
| 1057 |
interact (struct line_filter *diff, |
| 1058 |
struct line_filter *left, char const *lname, |
| 1059 |
struct line_filter *right, char const *rname, |
| 1060 |
FILE *outfile) |
| 1061 |
{ |
| 1062 |
lin lline = 1, rline = 1; |
| 1063 |
|
| 1064 |
for (;;) |
| 1065 |
{ |
| 1066 |
char diff_help[256]; |
| 1067 |
int snarfed = lf_snarf (diff, diff_help, sizeof diff_help); |
| 1068 |
|
| 1069 |
if (snarfed <= 0) |
| 1070 |
return snarfed != 0; |
| 1071 |
|
| 1072 |
checksigs (); |
| 1073 |
|
| 1074 |
if (diff_help[0] == ' ') |
| 1075 |
puts (diff_help + 1); |
| 1076 |
else |
| 1077 |
{ |
| 1078 |
errno = 0; |
| 1079 |
char *numend; |
| 1080 |
intmax_t val = strtoimax (diff_help + 1, &numend, 10); |
| 1081 |
if (! (0 <= val && val <= LIN_MAX) || errno || *numend != ',') |
| 1082 |
fatal (diff_help); |
| 1083 |
lin llen = val; |
| 1084 |
val = strtoimax (numend + 1, &numend, 10); |
| 1085 |
if (! (0 <= val && val <= LIN_MAX) || errno || *numend) |
| 1086 |
fatal (diff_help); |
| 1087 |
lin rlen = val; |
| 1088 |
|
| 1089 |
lin lenmax = MAX (llen, rlen); |
| 1090 |
|
| 1091 |
switch (diff_help[0]) |
| 1092 |
{ |
| 1093 |
case 'i': |
| 1094 |
if (suppress_common_lines) |
| 1095 |
lf_skip (diff, lenmax); |
| 1096 |
else |
| 1097 |
lf_copy (diff, lenmax, stdout); |
| 1098 |
|
| 1099 |
lf_copy (left, llen, outfile); |
| 1100 |
lf_skip (right, rlen); |
| 1101 |
break; |
| 1102 |
|
| 1103 |
case 'c': |
| 1104 |
lf_copy (diff, lenmax, stdout); |
| 1105 |
if (! edit (left, lname, lline, llen, |
| 1106 |
right, rname, rline, rlen, |
| 1107 |
outfile)) |
| 1108 |
return false; |
| 1109 |
break; |
| 1110 |
|
| 1111 |
default: |
| 1112 |
fatal (diff_help); |
| 1113 |
} |
| 1114 |
|
| 1115 |
lline += llen; |
| 1116 |
rline += rlen; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
/* Return true if DIR is an existing directory. */ |
| 1122 |
static bool |
| 1123 |
diraccess (char const *dir) |
| 1124 |
{ |
| 1125 |
struct stat buf; |
| 1126 |
return stat (dir, &buf) == 0 && S_ISDIR (buf.st_mode); |
| 1127 |
} |
| 1128 |
|
| 1129 |
#ifndef P_tmpdir |
| 1130 |
# define P_tmpdir "/tmp" |
| 1131 |
#endif |
| 1132 |
#ifndef TMPDIR_ENV |
| 1133 |
# define TMPDIR_ENV "TMPDIR" |
| 1134 |
#endif |
| 1135 |
|
| 1136 |
/* Open a temporary file and return its file descriptor. Put into |
| 1137 |
tmpname the address of a newly allocated buffer that holds the |
| 1138 |
file's name. Use the prefix "sdiff". */ |
| 1139 |
static int |
| 1140 |
temporary_file (void) |
| 1141 |
{ |
| 1142 |
char const *tmpdir = getenv (TMPDIR_ENV); |
| 1143 |
char const *dir = tmpdir ? tmpdir : P_tmpdir; |
| 1144 |
char *buf = xmalloc (strlen (dir) + 1 + 5 + 6 + 1); |
| 1145 |
strcpy (stpcpy (buf, dir), "/sdiffXXXXXX"); |
| 1146 |
int fd = mkstemp (buf); |
| 1147 |
if (fd < 0) |
| 1148 |
free (buf); |
| 1149 |
else |
| 1150 |
tmpname = buf; |
| 1151 |
return fd; |
| 1152 |
} |