| 1 | /* sdiff - side-by-side merge of file differences
|
|---|
| 2 |
|
|---|
| 3 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 2001, 2002 Free
|
|---|
| 4 | Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 | This file is part of GNU DIFF.
|
|---|
| 7 |
|
|---|
| 8 | GNU DIFF 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 2, or (at your option)
|
|---|
| 11 | any later version.
|
|---|
| 12 |
|
|---|
| 13 | GNU DIFF 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.
|
|---|
| 16 | See the 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; see the file COPYING.
|
|---|
| 20 | If not, write to the Free Software Foundation,
|
|---|
| 21 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|---|
| 22 |
|
|---|
| 23 | #include "system.h"
|
|---|
| 24 |
|
|---|
| 25 | #include <c-stack.h>
|
|---|
| 26 | #include <dirname.h>
|
|---|
| 27 | #include <error.h>
|
|---|
| 28 | #include <exitfail.h>
|
|---|
| 29 | #include <freesoft.h>
|
|---|
| 30 | #include <getopt.h>
|
|---|
| 31 | #include <quotesys.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <xalloc.h>
|
|---|
| 34 |
|
|---|
| 35 | static char const authorship_msgid[] = N_("Written by Thomas Lord.");
|
|---|
| 36 |
|
|---|
| 37 | static char const copyright_string[] =
|
|---|
| 38 | "Copyright (C) 2002 Free Software Foundation, Inc.";
|
|---|
| 39 |
|
|---|
| 40 | extern char const version_string[];
|
|---|
| 41 |
|
|---|
| 42 | /* Size of chunks read from files which must be parsed into lines. */
|
|---|
| 43 | #define SDIFF_BUFSIZE ((size_t) 65536)
|
|---|
| 44 |
|
|---|
| 45 | char *program_name;
|
|---|
| 46 |
|
|---|
| 47 | static char const *editor_program = DEFAULT_EDITOR_PROGRAM;
|
|---|
| 48 | static char const **diffargv;
|
|---|
| 49 |
|
|---|
| 50 | static char * volatile tmpname;
|
|---|
| 51 | static FILE *tmp;
|
|---|
| 52 |
|
|---|
| 53 | #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
|
|---|
| 54 | static pid_t volatile diffpid;
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 | struct line_filter;
|
|---|
| 58 |
|
|---|
| 59 | static RETSIGTYPE catchsig (int);
|
|---|
| 60 | static bool edit (struct line_filter *, char const *, lin, lin, struct line_filter *, char const *, lin, lin, FILE *);
|
|---|
| 61 | static bool interact (struct line_filter *, struct line_filter *, char const *, struct line_filter *, char const *, FILE *);
|
|---|
| 62 | static void checksigs (void);
|
|---|
| 63 | static void diffarg (char const *);
|
|---|
| 64 | static void fatal (char const *) __attribute__((noreturn));
|
|---|
| 65 | static void perror_fatal (char const *) __attribute__((noreturn));
|
|---|
| 66 | static void trapsigs (void);
|
|---|
| 67 | static void untrapsig (int);
|
|---|
| 68 |
|
|---|
| 69 | #define NUM_SIGS (sizeof sigs / sizeof *sigs)
|
|---|
| 70 | static int const sigs[] = {
|
|---|
| 71 | #ifdef SIGHUP
|
|---|
| 72 | SIGHUP,
|
|---|
| 73 | #endif
|
|---|
| 74 | #ifdef SIGQUIT
|
|---|
| 75 | SIGQUIT,
|
|---|
| 76 | #endif
|
|---|
| 77 | #ifdef SIGTERM
|
|---|
| 78 | SIGTERM,
|
|---|
| 79 | #endif
|
|---|
| 80 | #ifdef SIGXCPU
|
|---|
| 81 | SIGXCPU,
|
|---|
| 82 | #endif
|
|---|
| 83 | #ifdef SIGXFSZ
|
|---|
| 84 | SIGXFSZ,
|
|---|
| 85 | #endif
|
|---|
| 86 | SIGINT,
|
|---|
| 87 | SIGPIPE
|
|---|
| 88 | };
|
|---|
| 89 | #define handler_index_of_SIGINT (NUM_SIGS - 2)
|
|---|
| 90 | #define handler_index_of_SIGPIPE (NUM_SIGS - 1)
|
|---|
| 91 |
|
|---|
| 92 | #if HAVE_SIGACTION
|
|---|
| 93 | /* Prefer `sigaction' if available, since `signal' can lose signals. */
|
|---|
| 94 | static struct sigaction initial_action[NUM_SIGS];
|
|---|
| 95 | # define initial_handler(i) (initial_action[i].sa_handler)
|
|---|
| 96 | static void signal_handler (int, RETSIGTYPE (*) (int));
|
|---|
| 97 | #else
|
|---|
| 98 | static RETSIGTYPE (*initial_action[NUM_SIGS]) ();
|
|---|
| 99 | # define initial_handler(i) (initial_action[i])
|
|---|
| 100 | # define signal_handler(sig, handler) signal (sig, handler)
|
|---|
| 101 | #endif
|
|---|
| 102 |
|
|---|
| 103 | #if ! HAVE_SIGPROCMASK
|
|---|
| 104 | # define sigset_t int
|
|---|
| 105 | # define sigemptyset(s) (*(s) = 0)
|
|---|
| 106 | # ifndef sigmask
|
|---|
| 107 | # define sigmask(sig) (1 << ((sig) - 1))
|
|---|
| 108 | # endif
|
|---|
| 109 | # define sigaddset(s, sig) (*(s) |= sigmask (sig))
|
|---|
| 110 | # ifndef SIG_BLOCK
|
|---|
| 111 | # define SIG_BLOCK 0
|
|---|
| 112 | # endif
|
|---|
| 113 | # ifndef SIG_SETMASK
|
|---|
| 114 | # define SIG_SETMASK (! SIG_BLOCK)
|
|---|
| 115 | # endif
|
|---|
| 116 | # define sigprocmask(how, n, o) \
|
|---|
| 117 | ((how) == SIG_BLOCK ? *(o) = sigblock (*(n)) : sigsetmask (*(n)))
|
|---|
| 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 | };
|
|---|
| 138 |
|
|---|
| 139 | static struct option const longopts[] =
|
|---|
| 140 | {
|
|---|
| 141 | {"diff-program", 1, 0, DIFF_PROGRAM_OPTION},
|
|---|
| 142 | {"expand-tabs", 0, 0, 't'},
|
|---|
| 143 | {"help", 0, 0, HELP_OPTION},
|
|---|
| 144 | {"ignore-all-space", 0, 0, 'W'}, /* swap W and w for historical reasons */
|
|---|
| 145 | {"ignore-blank-lines", 0, 0, 'B'},
|
|---|
| 146 | {"ignore-case", 0, 0, 'i'},
|
|---|
| 147 | {"ignore-matching-lines", 1, 0, 'I'},
|
|---|
| 148 | {"ignore-space-change", 0, 0, 'b'},
|
|---|
| 149 | {"ignore-tab-expansion", 0, 0, 'E'},
|
|---|
| 150 | {"left-column", 0, 0, 'l'},
|
|---|
| 151 | {"minimal", 0, 0, 'd'},
|
|---|
| 152 | {"output", 1, 0, 'o'},
|
|---|
| 153 | {"speed-large-files", 0, 0, 'H'},
|
|---|
| 154 | {"strip-trailing-cr", 0, 0, STRIP_TRAILING_CR_OPTION},
|
|---|
| 155 | {"suppress-common-lines", 0, 0, 's'},
|
|---|
| 156 | {"text", 0, 0, 'a'},
|
|---|
| 157 | {"version", 0, 0, 'v'},
|
|---|
| 158 | {"width", 1, 0, 'w'},
|
|---|
| 159 | {0, 0, 0, 0}
|
|---|
| 160 | };
|
|---|
| 161 |
|
|---|
| 162 | static void try_help (char const *, char const *) __attribute__((noreturn));
|
|---|
| 163 | static void
|
|---|
| 164 | try_help (char const *reason_msgid, char const *operand)
|
|---|
| 165 | {
|
|---|
| 166 | if (reason_msgid)
|
|---|
| 167 | error (0, 0, _(reason_msgid), operand);
|
|---|
| 168 | error (EXIT_TROUBLE, 0, _("Try `%s --help' for more information."),
|
|---|
| 169 | program_name);
|
|---|
| 170 | abort ();
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | static void
|
|---|
| 174 | check_stdout (void)
|
|---|
| 175 | {
|
|---|
| 176 | if (ferror (stdout))
|
|---|
| 177 | fatal ("write failed");
|
|---|
| 178 | else if (fclose (stdout) != 0)
|
|---|
| 179 | perror_fatal (_("standard output"));
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | static char const * const option_help_msgid[] = {
|
|---|
| 183 | N_("-o FILE --output=FILE Operate interactively, sending output to FILE."),
|
|---|
| 184 | "",
|
|---|
| 185 | N_("-i --ignore-case Consider upper- and lower-case to be the same."),
|
|---|
| 186 | N_("-E --ignore-tab-expansion Ignore changes due to tab expansion."),
|
|---|
| 187 | N_("-b --ignore-space-change Ignore changes in the amount of white space."),
|
|---|
| 188 | N_("-W --ignore-all-space Ignore all white space."),
|
|---|
| 189 | N_("-B --ignore-blank-lines Ignore changes whose lines are all blank."),
|
|---|
| 190 | N_("-I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE."),
|
|---|
| 191 | N_("--strip-trailing-cr Strip trailing carriage return on input."),
|
|---|
| 192 | N_("-a --text Treat all files as text."),
|
|---|
| 193 | "",
|
|---|
| 194 | N_("-w NUM --width=NUM Output at most NUM (default 130) columns per line."),
|
|---|
| 195 | N_("-l --left-column Output only the left column of common lines."),
|
|---|
| 196 | N_("-s --suppress-common-lines Do not output common lines."),
|
|---|
| 197 | "",
|
|---|
| 198 | N_("-t --expand-tabs Expand tabs to spaces in output."),
|
|---|
| 199 | "",
|
|---|
| 200 | N_("-d --minimal Try hard to find a smaller set of changes."),
|
|---|
| 201 | N_("-H --speed-large-files Assume large files and many scattered small changes."),
|
|---|
| 202 | N_("--diff-program=PROGRAM Use PROGRAM to compare files."),
|
|---|
| 203 | "",
|
|---|
| 204 | N_("-v --version Output version info."),
|
|---|
| 205 | N_("--help Output this help."),
|
|---|
| 206 | 0
|
|---|
| 207 | };
|
|---|
| 208 |
|
|---|
| 209 | static void
|
|---|
| 210 | usage (void)
|
|---|
| 211 | {
|
|---|
| 212 | char const * const *p;
|
|---|
| 213 |
|
|---|
| 214 | printf (_("Usage: %s [OPTION]... FILE1 FILE2\n"), program_name);
|
|---|
| 215 | printf ("%s\n\n", _("Side-by-side merge of file differences."));
|
|---|
| 216 | for (p = option_help_msgid; *p; p++)
|
|---|
| 217 | if (**p)
|
|---|
| 218 | printf (" %s\n", _(*p));
|
|---|
| 219 | else
|
|---|
| 220 | putchar ('\n');
|
|---|
| 221 | printf ("\n%s\n\n%s\n",
|
|---|
| 222 | _("If a FILE is `-', read standard input."),
|
|---|
| 223 | _("Report bugs to <bug-gnu-utils@gnu.org>."));
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | static void
|
|---|
| 227 | cleanup (void)
|
|---|
| 228 | {
|
|---|
| 229 | #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
|
|---|
| 230 | if (0 < diffpid)
|
|---|
| 231 | kill (diffpid, SIGPIPE);
|
|---|
| 232 | #endif
|
|---|
| 233 | if (tmpname)
|
|---|
| 234 | unlink (tmpname);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | static void exiterr (void) __attribute__((noreturn));
|
|---|
| 238 | static void
|
|---|
| 239 | exiterr (void)
|
|---|
| 240 | {
|
|---|
| 241 | cleanup ();
|
|---|
| 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 | ck_editor_status (int errnum, int status)
|
|---|
| 265 | {
|
|---|
| 266 | if (errnum | status)
|
|---|
| 267 | {
|
|---|
| 268 | char const *failure_msgid = N_("subsidiary program `%s' failed");
|
|---|
| 269 | if (! errnum && WIFEXITED (status))
|
|---|
| 270 | switch (WEXITSTATUS (status))
|
|---|
| 271 | {
|
|---|
| 272 | case 126:
|
|---|
| 273 | failure_msgid = N_("subsidiary program `%s' not executable");
|
|---|
| 274 | break;
|
|---|
| 275 | case 127:
|
|---|
| 276 | failure_msgid = N_("subsidiary program `%s' not found");
|
|---|
| 277 | break;
|
|---|
| 278 | }
|
|---|
| 279 | error (0, errnum, _(failure_msgid), editor_program);
|
|---|
| 280 | exiterr ();
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | static FILE *
|
|---|
| 285 | ck_fopen (char const *fname, char const *type)
|
|---|
| 286 | {
|
|---|
| 287 | FILE *r = fopen (fname, type);
|
|---|
| 288 | if (! r)
|
|---|
| 289 | perror_fatal (fname);
|
|---|
| 290 | return r;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | static void
|
|---|
| 294 | ck_fclose (FILE *f)
|
|---|
| 295 | {
|
|---|
| 296 | if (fclose (f))
|
|---|
| 297 | perror_fatal ("fclose");
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | static size_t
|
|---|
| 301 | ck_fread (char *buf, size_t size, FILE *f)
|
|---|
| 302 | {
|
|---|
| 303 | size_t r = fread (buf, sizeof (char), size, f);
|
|---|
| 304 | if (r == 0 && ferror (f))
|
|---|
| 305 | perror_fatal (_("read failed"));
|
|---|
| 306 | return r;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | static void
|
|---|
| 310 | ck_fwrite (char const *buf, size_t size, FILE *f)
|
|---|
| 311 | {
|
|---|
| 312 | if (fwrite (buf, sizeof (char), size, f) != size)
|
|---|
| 313 | perror_fatal (_("write failed"));
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | static void
|
|---|
| 317 | ck_fflush (FILE *f)
|
|---|
| 318 | {
|
|---|
| 319 | if (fflush (f) != 0)
|
|---|
| 320 | perror_fatal (_("write failed"));
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | static char const *
|
|---|
| 324 | expand_name (char *name, bool is_dir, char const *other_name)
|
|---|
| 325 | {
|
|---|
| 326 | if (strcmp (name, "-") == 0)
|
|---|
| 327 | fatal ("cannot interactively merge standard input");
|
|---|
| 328 | if (! is_dir)
|
|---|
| 329 | return name;
|
|---|
| 330 | else
|
|---|
| 331 | {
|
|---|
| 332 | /* Yield NAME/BASE, where BASE is OTHER_NAME's basename. */
|
|---|
| 333 | char const *base = base_name (other_name);
|
|---|
| 334 | size_t namelen = strlen (name), baselen = strlen (base);
|
|---|
| 335 | bool insert_slash = *base_name (name) && name[namelen - 1] != '/';
|
|---|
| 336 | char *r = xmalloc (namelen + insert_slash + baselen + 1);
|
|---|
| 337 | memcpy (r, name, namelen);
|
|---|
| 338 | r[namelen] = '/';
|
|---|
| 339 | memcpy (r + namelen + insert_slash, base, baselen + 1);
|
|---|
| 340 | return r;
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 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 = xmalloc (SDIFF_BUFSIZE + 1);
|
|---|
| 359 | lf->buflim[0] = '\n';
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /* Fill an exhausted line_filter buffer from its INFILE */
|
|---|
| 363 | static size_t
|
|---|
| 364 | lf_refill (struct line_filter *lf)
|
|---|
| 365 | {
|
|---|
| 366 | size_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 = (char *) memchr (lf->bufpos, '\n', lf->buflim - lf->bufpos);
|
|---|
| 383 | if (! lf->bufpos)
|
|---|
| 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 = (char *) memchr (lf->bufpos, '\n', lf->buflim - lf->bufpos);
|
|---|
| 407 | if (! lf->bufpos)
|
|---|
| 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, size_t bufsize)
|
|---|
| 423 | {
|
|---|
| 424 | for (;;)
|
|---|
| 425 | {
|
|---|
| 426 | char *start = lf->bufpos;
|
|---|
| 427 | char *next = (char *) memchr (start, '\n', lf->buflim + 1 - start);
|
|---|
| 428 | size_t s = next - start;
|
|---|
| 429 | if (bufsize <= s)
|
|---|
| 430 | return 0;
|
|---|
| 431 | memcpy (buffer, start, s);
|
|---|
| 432 | if (next < lf->buflim)
|
|---|
| 433 | {
|
|---|
| 434 | buffer[s] = 0;
|
|---|
| 435 | lf->bufpos = next + 1;
|
|---|
| 436 | return 1;
|
|---|
| 437 | }
|
|---|
| 438 | if (! lf_refill (lf))
|
|---|
| 439 | return s ? 0 : EOF;
|
|---|
| 440 | buffer += s;
|
|---|
| 441 | bufsize -= s;
|
|---|
| 442 | }
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | |
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 | int
|
|---|
| 449 | main (int argc, char *argv[])
|
|---|
| 450 | {
|
|---|
| 451 | int opt;
|
|---|
| 452 | char const *prog;
|
|---|
| 453 |
|
|---|
| 454 | exit_failure = EXIT_TROUBLE;
|
|---|
| 455 | initialize_main (&argc, &argv);
|
|---|
| 456 | program_name = argv[0];
|
|---|
| 457 | setlocale (LC_ALL, "");
|
|---|
| 458 | bindtextdomain (PACKAGE, LOCALEDIR);
|
|---|
| 459 | textdomain (PACKAGE);
|
|---|
| 460 | c_stack_action (c_stack_die);
|
|---|
| 461 |
|
|---|
| 462 | prog = getenv ("EDITOR");
|
|---|
| 463 | if (prog)
|
|---|
| 464 | editor_program = prog;
|
|---|
| 465 |
|
|---|
| 466 | diffarg (DEFAULT_DIFF_PROGRAM);
|
|---|
| 467 |
|
|---|
| 468 | /* parse command line args */
|
|---|
| 469 | while ((opt = getopt_long (argc, argv, "abBdHiI:lo:stvw:W", longopts, 0))
|
|---|
| 470 | != -1)
|
|---|
| 471 | {
|
|---|
| 472 | switch (opt)
|
|---|
| 473 | {
|
|---|
| 474 | case 'a':
|
|---|
| 475 | diffarg ("-a");
|
|---|
| 476 | break;
|
|---|
| 477 |
|
|---|
| 478 | case 'b':
|
|---|
| 479 | diffarg ("-b");
|
|---|
| 480 | break;
|
|---|
| 481 |
|
|---|
| 482 | case 'B':
|
|---|
| 483 | diffarg ("-B");
|
|---|
| 484 | break;
|
|---|
| 485 |
|
|---|
| 486 | case 'd':
|
|---|
| 487 | diffarg ("-d");
|
|---|
| 488 | break;
|
|---|
| 489 |
|
|---|
| 490 | case 'E':
|
|---|
| 491 | diffarg ("-E");
|
|---|
| 492 | break;
|
|---|
| 493 |
|
|---|
| 494 | case 'H':
|
|---|
| 495 | diffarg ("-H");
|
|---|
| 496 | break;
|
|---|
| 497 |
|
|---|
| 498 | case 'i':
|
|---|
| 499 | diffarg ("-i");
|
|---|
| 500 | break;
|
|---|
| 501 |
|
|---|
| 502 | case 'I':
|
|---|
| 503 | diffarg ("-I");
|
|---|
| 504 | diffarg (optarg);
|
|---|
| 505 | break;
|
|---|
| 506 |
|
|---|
| 507 | case 'l':
|
|---|
| 508 | diffarg ("--left-column");
|
|---|
| 509 | break;
|
|---|
| 510 |
|
|---|
| 511 | case 'o':
|
|---|
| 512 | output = optarg;
|
|---|
| 513 | break;
|
|---|
| 514 |
|
|---|
| 515 | case 's':
|
|---|
| 516 | suppress_common_lines = 1;
|
|---|
| 517 | break;
|
|---|
| 518 |
|
|---|
| 519 | case 't':
|
|---|
| 520 | diffarg ("-t");
|
|---|
| 521 | break;
|
|---|
| 522 |
|
|---|
| 523 | case 'v':
|
|---|
| 524 | printf ("sdiff %s\n%s\n\n%s\n\n%s\n",
|
|---|
| 525 | version_string, copyright_string,
|
|---|
| 526 | _(free_software_msgid), _(authorship_msgid));
|
|---|
| 527 | check_stdout ();
|
|---|
| 528 | return EXIT_SUCCESS;
|
|---|
| 529 |
|
|---|
| 530 | case 'w':
|
|---|
| 531 | diffarg ("-W");
|
|---|
| 532 | diffarg (optarg);
|
|---|
| 533 | break;
|
|---|
| 534 |
|
|---|
| 535 | case 'W':
|
|---|
| 536 | diffarg ("-w");
|
|---|
| 537 | break;
|
|---|
| 538 |
|
|---|
| 539 | case DIFF_PROGRAM_OPTION:
|
|---|
| 540 | diffargv[0] = optarg;
|
|---|
| 541 | break;
|
|---|
| 542 |
|
|---|
| 543 | case HELP_OPTION:
|
|---|
| 544 | usage ();
|
|---|
| 545 | check_stdout ();
|
|---|
| 546 | return EXIT_SUCCESS;
|
|---|
| 547 |
|
|---|
| 548 | case STRIP_TRAILING_CR_OPTION:
|
|---|
| 549 | diffarg ("--strip-trailing-cr");
|
|---|
| 550 | break;
|
|---|
| 551 |
|
|---|
| 552 | default:
|
|---|
| 553 | try_help (0, 0);
|
|---|
| 554 | }
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | if (argc - optind != 2)
|
|---|
| 558 | {
|
|---|
| 559 | if (argc - optind < 2)
|
|---|
| 560 | try_help ("missing operand after `%s'", argv[argc - 1]);
|
|---|
| 561 | else
|
|---|
| 562 | try_help ("extra operand `%s'", argv[optind + 2]);
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | if (! output)
|
|---|
| 566 | {
|
|---|
| 567 | /* easy case: diff does everything for us */
|
|---|
| 568 | if (suppress_common_lines)
|
|---|
| 569 | diffarg ("--suppress-common-lines");
|
|---|
| 570 | diffarg ("-y");
|
|---|
| 571 | diffarg ("--");
|
|---|
| 572 | diffarg (argv[optind]);
|
|---|
| 573 | diffarg (argv[optind + 1]);
|
|---|
| 574 | diffarg (0);
|
|---|
| 575 | execvp (diffargv[0], (char **) diffargv);
|
|---|
| 576 | perror_fatal (diffargv[0]);
|
|---|
| 577 | }
|
|---|
| 578 | else
|
|---|
| 579 | {
|
|---|
| 580 | char const *lname, *rname;
|
|---|
| 581 | FILE *left, *right, *out, *diffout;
|
|---|
| 582 | bool interact_ok;
|
|---|
| 583 | struct line_filter lfilt;
|
|---|
| 584 | struct line_filter rfilt;
|
|---|
| 585 | struct line_filter diff_filt;
|
|---|
| 586 | bool leftdir = diraccess (argv[optind]);
|
|---|
| 587 | bool rightdir = diraccess (argv[optind + 1]);
|
|---|
| 588 |
|
|---|
| 589 | if (leftdir & rightdir)
|
|---|
| 590 | fatal ("both files to be compared are directories");
|
|---|
| 591 |
|
|---|
| 592 | lname = expand_name (argv[optind], leftdir, argv[optind + 1]);
|
|---|
| 593 | left = ck_fopen (lname, "r");
|
|---|
| 594 | rname = expand_name (argv[optind + 1], rightdir, argv[optind]);
|
|---|
| 595 | right = ck_fopen (rname, "r");
|
|---|
| 596 | out = ck_fopen (output, "w");
|
|---|
| 597 |
|
|---|
| 598 | diffarg ("--sdiff-merge-assist");
|
|---|
| 599 | diffarg ("--");
|
|---|
| 600 | diffarg (argv[optind]);
|
|---|
| 601 | diffarg (argv[optind + 1]);
|
|---|
| 602 | diffarg (0);
|
|---|
| 603 |
|
|---|
| 604 | trapsigs ();
|
|---|
| 605 |
|
|---|
| 606 | #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)
|
|---|
| 607 | {
|
|---|
| 608 | size_t cmdsize = 1;
|
|---|
| 609 | char *p, *command;
|
|---|
| 610 | int i;
|
|---|
| 611 |
|
|---|
| 612 | for (i = 0; diffargv[i]; i++)
|
|---|
| 613 | cmdsize += quote_system_arg (0, diffargv[i]) + 1;
|
|---|
| 614 | command = p = xmalloc (cmdsize);
|
|---|
| 615 | for (i = 0; diffargv[i]; i++)
|
|---|
| 616 | {
|
|---|
| 617 | p += quote_system_arg (p, diffargv[i]);
|
|---|
| 618 | *p++ = ' ';
|
|---|
| 619 | }
|
|---|
| 620 | p[-1] = 0;
|
|---|
| 621 | errno = 0;
|
|---|
| 622 | diffout = popen (command, "r");
|
|---|
| 623 | if (! diffout)
|
|---|
| 624 | perror_fatal (command);
|
|---|
| 625 | free (command);
|
|---|
| 626 | }
|
|---|
| 627 | #else
|
|---|
| 628 | {
|
|---|
| 629 | int diff_fds[2];
|
|---|
| 630 | # if HAVE_WORKING_VFORK
|
|---|
| 631 | sigset_t procmask;
|
|---|
| 632 | sigset_t blocked;
|
|---|
| 633 | # endif
|
|---|
| 634 |
|
|---|
| 635 | if (pipe (diff_fds) != 0)
|
|---|
| 636 | perror_fatal ("pipe");
|
|---|
| 637 |
|
|---|
| 638 | # if HAVE_WORKING_VFORK
|
|---|
| 639 | /* Block SIGINT and SIGPIPE. */
|
|---|
| 640 | sigemptyset (&blocked);
|
|---|
| 641 | sigaddset (&blocked, SIGINT);
|
|---|
| 642 | sigaddset (&blocked, SIGPIPE);
|
|---|
| 643 | sigprocmask (SIG_BLOCK, &blocked, &procmask);
|
|---|
| 644 | # endif
|
|---|
| 645 | diffpid = vfork ();
|
|---|
| 646 | if (diffpid < 0)
|
|---|
| 647 | perror_fatal ("fork");
|
|---|
| 648 | if (! diffpid)
|
|---|
| 649 | {
|
|---|
| 650 | /* Alter the child's SIGINT and SIGPIPE handlers;
|
|---|
| 651 | this may munge the parent.
|
|---|
| 652 | The child ignores SIGINT in case the user interrupts the editor.
|
|---|
| 653 | The child does not ignore SIGPIPE, even if the parent does. */
|
|---|
| 654 | if (initial_handler (handler_index_of_SIGINT) != SIG_IGN)
|
|---|
| 655 | signal_handler (SIGINT, SIG_IGN);
|
|---|
| 656 | signal_handler (SIGPIPE, SIG_DFL);
|
|---|
| 657 | # if HAVE_WORKING_VFORK
|
|---|
| 658 | /* Stop blocking SIGINT and SIGPIPE in the child. */
|
|---|
| 659 | sigprocmask (SIG_SETMASK, &procmask, 0);
|
|---|
| 660 | # endif
|
|---|
| 661 | close (diff_fds[0]);
|
|---|
| 662 | if (diff_fds[1] != STDOUT_FILENO)
|
|---|
| 663 | {
|
|---|
| 664 | dup2 (diff_fds[1], STDOUT_FILENO);
|
|---|
| 665 | close (diff_fds[1]);
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | execvp (diffargv[0], (char **) diffargv);
|
|---|
| 669 | _exit (errno == ENOEXEC ? 126 : 127);
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | # if HAVE_WORKING_VFORK
|
|---|
| 673 | /* Restore the parent's SIGINT and SIGPIPE behavior. */
|
|---|
| 674 | if (initial_handler (handler_index_of_SIGINT) != SIG_IGN)
|
|---|
| 675 | signal_handler (SIGINT, catchsig);
|
|---|
| 676 | if (initial_handler (handler_index_of_SIGPIPE) != SIG_IGN)
|
|---|
| 677 | signal_handler (SIGPIPE, catchsig);
|
|---|
| 678 | else
|
|---|
| 679 | signal_handler (SIGPIPE, SIG_IGN);
|
|---|
| 680 |
|
|---|
| 681 | /* Stop blocking SIGINT and SIGPIPE in the parent. */
|
|---|
| 682 | sigprocmask (SIG_SETMASK, &procmask, 0);
|
|---|
| 683 | # endif
|
|---|
| 684 |
|
|---|
| 685 | close (diff_fds[1]);
|
|---|
| 686 | diffout = fdopen (diff_fds[0], "r");
|
|---|
| 687 | if (! diffout)
|
|---|
| 688 | perror_fatal ("fdopen");
|
|---|
| 689 | }
|
|---|
| 690 | #endif
|
|---|
| 691 |
|
|---|
| 692 | lf_init (&diff_filt, diffout);
|
|---|
| 693 | lf_init (&lfilt, left);
|
|---|
| 694 | lf_init (&rfilt, right);
|
|---|
| 695 |
|
|---|
| 696 | interact_ok = interact (&diff_filt, &lfilt, lname, &rfilt, rname, out);
|
|---|
| 697 |
|
|---|
| 698 | ck_fclose (left);
|
|---|
| 699 | ck_fclose (right);
|
|---|
| 700 | ck_fclose (out);
|
|---|
| 701 |
|
|---|
| 702 | {
|
|---|
| 703 | int wstatus;
|
|---|
| 704 | int werrno = 0;
|
|---|
| 705 |
|
|---|
| 706 | #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)
|
|---|
| 707 | wstatus = pclose (diffout);
|
|---|
| 708 | if (wstatus == -1)
|
|---|
| 709 | werrno = errno;
|
|---|
| 710 | #else
|
|---|
| 711 | ck_fclose (diffout);
|
|---|
| 712 | while (waitpid (diffpid, &wstatus, 0) < 0)
|
|---|
| 713 | if (errno == EINTR)
|
|---|
| 714 | checksigs ();
|
|---|
| 715 | else
|
|---|
| 716 | perror_fatal ("waitpid");
|
|---|
| 717 | diffpid = 0;
|
|---|
| 718 | #endif
|
|---|
| 719 |
|
|---|
| 720 | if (tmpname)
|
|---|
| 721 | {
|
|---|
| 722 | unlink (tmpname);
|
|---|
| 723 | tmpname = 0;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | if (! interact_ok)
|
|---|
| 727 | exiterr ();
|
|---|
| 728 |
|
|---|
| 729 | ck_editor_status (werrno, wstatus);
|
|---|
| 730 | untrapsig (0);
|
|---|
| 731 | checksigs ();
|
|---|
| 732 | exit (WEXITSTATUS (wstatus));
|
|---|
| 733 | }
|
|---|
| 734 | }
|
|---|
| 735 | return EXIT_SUCCESS; /* Fool `-Wall'. */
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | static void
|
|---|
| 739 | diffarg (char const *a)
|
|---|
| 740 | {
|
|---|
| 741 | static size_t diffargs, diffarglim;
|
|---|
| 742 |
|
|---|
| 743 | if (diffargs == diffarglim)
|
|---|
| 744 | {
|
|---|
| 745 | if (! diffarglim)
|
|---|
| 746 | diffarglim = 16;
|
|---|
| 747 | else if (PTRDIFF_MAX / (2 * sizeof *diffargv) <= diffarglim)
|
|---|
| 748 | xalloc_die ();
|
|---|
| 749 | else
|
|---|
| 750 | diffarglim *= 2;
|
|---|
| 751 | diffargv = xrealloc (diffargv, diffarglim * sizeof *diffargv);
|
|---|
| 752 | }
|
|---|
| 753 | diffargv[diffargs++] = a;
|
|---|
| 754 | }
|
|---|
| 755 |
|
|---|
| 756 | |
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 | /* Signal handling */
|
|---|
| 761 |
|
|---|
| 762 | static bool volatile ignore_SIGINT;
|
|---|
| 763 | static int volatile signal_received;
|
|---|
| 764 | static bool sigs_trapped;
|
|---|
| 765 |
|
|---|
| 766 | static RETSIGTYPE
|
|---|
| 767 | catchsig (int s)
|
|---|
| 768 | {
|
|---|
| 769 | #if ! HAVE_SIGACTION
|
|---|
| 770 | signal (s, SIG_IGN);
|
|---|
| 771 | #endif
|
|---|
| 772 | if (! (s == SIGINT && ignore_SIGINT))
|
|---|
| 773 | signal_received = s;
|
|---|
| 774 | }
|
|---|
| 775 |
|
|---|
| 776 | #if HAVE_SIGACTION
|
|---|
| 777 | static struct sigaction catchaction;
|
|---|
| 778 |
|
|---|
| 779 | static void
|
|---|
| 780 | signal_handler (int sig, RETSIGTYPE (*handler) (int))
|
|---|
| 781 | {
|
|---|
| 782 | catchaction.sa_handler = handler;
|
|---|
| 783 | sigaction (sig, &catchaction, 0);
|
|---|
| 784 | }
|
|---|
| 785 | #endif
|
|---|
| 786 |
|
|---|
| 787 | static void
|
|---|
| 788 | trapsigs (void)
|
|---|
| 789 | {
|
|---|
| 790 | int i;
|
|---|
| 791 |
|
|---|
| 792 | #if HAVE_SIGACTION
|
|---|
| 793 | catchaction.sa_flags = SA_RESTART;
|
|---|
| 794 | sigemptyset (&catchaction.sa_mask);
|
|---|
| 795 | for (i = 0; i < NUM_SIGS; i++)
|
|---|
| 796 | sigaddset (&catchaction.sa_mask, sigs[i]);
|
|---|
| 797 | #endif
|
|---|
| 798 |
|
|---|
| 799 | for (i = 0; i < NUM_SIGS; i++)
|
|---|
| 800 | {
|
|---|
| 801 | #if HAVE_SIGACTION
|
|---|
| 802 | sigaction (sigs[i], 0, &initial_action[i]);
|
|---|
| 803 | #else
|
|---|
| 804 | initial_action[i] = signal (sigs[i], SIG_IGN);
|
|---|
| 805 | #endif
|
|---|
| 806 | if (initial_handler (i) != SIG_IGN)
|
|---|
| 807 | signal_handler (sigs[i], catchsig);
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | #ifdef SIGCHLD
|
|---|
| 811 | /* System V fork+wait does not work if SIGCHLD is ignored. */
|
|---|
| 812 | signal (SIGCHLD, SIG_DFL);
|
|---|
| 813 | #endif
|
|---|
| 814 |
|
|---|
| 815 | sigs_trapped = 1;
|
|---|
| 816 | }
|
|---|
| 817 |
|
|---|
| 818 | /* Untrap signal S, or all trapped signals if S is zero. */
|
|---|
| 819 | static void
|
|---|
| 820 | untrapsig (int s)
|
|---|
| 821 | {
|
|---|
| 822 | int i;
|
|---|
| 823 |
|
|---|
| 824 | if (sigs_trapped)
|
|---|
| 825 | for (i = 0; i < NUM_SIGS; i++)
|
|---|
| 826 | if ((! s || sigs[i] == s) && initial_handler (i) != SIG_IGN)
|
|---|
| 827 | #if HAVE_SIGACTION
|
|---|
| 828 | sigaction (sigs[i], &initial_action[i], 0);
|
|---|
| 829 | #else
|
|---|
| 830 | signal (sigs[i], initial_action[i]);
|
|---|
| 831 | #endif
|
|---|
| 832 | }
|
|---|
| 833 |
|
|---|
| 834 | /* Exit if a signal has been received. */
|
|---|
| 835 | static void
|
|---|
| 836 | checksigs (void)
|
|---|
| 837 | {
|
|---|
| 838 | int s = signal_received;
|
|---|
| 839 | if (s)
|
|---|
| 840 | {
|
|---|
| 841 | cleanup ();
|
|---|
| 842 |
|
|---|
| 843 | /* Yield an exit status indicating that a signal was received. */
|
|---|
| 844 | untrapsig (s);
|
|---|
| 845 | kill (getpid (), s);
|
|---|
| 846 |
|
|---|
| 847 | /* That didn't work, so exit with error status. */
|
|---|
| 848 | exit (EXIT_TROUBLE);
|
|---|
| 849 | }
|
|---|
| 850 | }
|
|---|
| 851 |
|
|---|
| 852 | |
|---|
| 853 |
|
|---|
| 854 | static void
|
|---|
| 855 | give_help (void)
|
|---|
| 856 | {
|
|---|
| 857 | fprintf (stderr, "%s", _("\
|
|---|
| 858 | ed:\tEdit then use both versions, each decorated with a header.\n\
|
|---|
| 859 | eb:\tEdit then use both versions.\n\
|
|---|
| 860 | el:\tEdit then use the left version.\n\
|
|---|
| 861 | er:\tEdit then use the right version.\n\
|
|---|
| 862 | e:\tEdit a new version.\n\
|
|---|
| 863 | l:\tUse the left version.\n\
|
|---|
| 864 | r:\tUse the right version.\n\
|
|---|
| 865 | s:\tSilently include common lines.\n\
|
|---|
| 866 | v:\tVerbosely include common lines.\n\
|
|---|
| 867 | q:\tQuit.\n\
|
|---|
| 868 | "));
|
|---|
| 869 | }
|
|---|
| 870 |
|
|---|
| 871 | static int
|
|---|
| 872 | skip_white (void)
|
|---|
| 873 | {
|
|---|
| 874 | int c;
|
|---|
| 875 | for (;;)
|
|---|
| 876 | {
|
|---|
| 877 | c = getchar ();
|
|---|
| 878 | if (! ISSPACE (c) || c == '\n')
|
|---|
| 879 | break;
|
|---|
| 880 | checksigs ();
|
|---|
| 881 | }
|
|---|
| 882 | if (ferror (stdin))
|
|---|
| 883 | perror_fatal (_("read failed"));
|
|---|
| 884 | return c;
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | static void
|
|---|
| 888 | flush_line (void)
|
|---|
| 889 | {
|
|---|
| 890 | int c;
|
|---|
| 891 | while ((c = getchar ()) != '\n' && c != EOF)
|
|---|
| 892 | continue;
|
|---|
| 893 | if (ferror (stdin))
|
|---|
| 894 | perror_fatal (_("read failed"));
|
|---|
| 895 | }
|
|---|
| 896 |
|
|---|
| 897 |
|
|---|
| 898 | /* interpret an edit command */
|
|---|
| 899 | static bool
|
|---|
| 900 | edit (struct line_filter *left, char const *lname, lin lline, lin llen,
|
|---|
| 901 | struct line_filter *right, char const *rname, lin rline, lin rlen,
|
|---|
| 902 | FILE *outfile)
|
|---|
| 903 | {
|
|---|
| 904 | for (;;)
|
|---|
| 905 | {
|
|---|
| 906 | int cmd0, cmd1;
|
|---|
| 907 | bool gotcmd = 0;
|
|---|
| 908 |
|
|---|
| 909 | cmd1 = 0; /* Pacify `gcc -W'. */
|
|---|
| 910 |
|
|---|
| 911 | while (! gotcmd)
|
|---|
| 912 | {
|
|---|
| 913 | if (putchar ('%') != '%')
|
|---|
| 914 | perror_fatal (_("write failed"));
|
|---|
| 915 | ck_fflush (stdout);
|
|---|
| 916 |
|
|---|
| 917 | cmd0 = skip_white ();
|
|---|
| 918 | switch (cmd0)
|
|---|
| 919 | {
|
|---|
| 920 | case 'l': case 'r': case 's': case 'v': case 'q':
|
|---|
| 921 | if (skip_white () != '\n')
|
|---|
| 922 | {
|
|---|
| 923 | give_help ();
|
|---|
| 924 | flush_line ();
|
|---|
| 925 | continue;
|
|---|
| 926 | }
|
|---|
| 927 | gotcmd = 1;
|
|---|
| 928 | break;
|
|---|
| 929 |
|
|---|
| 930 | case 'e':
|
|---|
| 931 | cmd1 = skip_white ();
|
|---|
| 932 | switch (cmd1)
|
|---|
| 933 | {
|
|---|
| 934 | case 'b': case 'd': case 'l': case 'r':
|
|---|
| 935 | if (skip_white () != '\n')
|
|---|
| 936 | {
|
|---|
| 937 | give_help ();
|
|---|
| 938 | flush_line ();
|
|---|
| 939 | continue;
|
|---|
| 940 | }
|
|---|
| 941 | gotcmd = 1;
|
|---|
| 942 | break;
|
|---|
| 943 | case '\n':
|
|---|
| 944 | gotcmd = 1;
|
|---|
| 945 | break;
|
|---|
| 946 | default:
|
|---|
| 947 | give_help ();
|
|---|
| 948 | flush_line ();
|
|---|
| 949 | continue;
|
|---|
| 950 | }
|
|---|
| 951 | break;
|
|---|
| 952 |
|
|---|
| 953 | case EOF:
|
|---|
| 954 | if (feof (stdin))
|
|---|
| 955 | {
|
|---|
| 956 | gotcmd = 1;
|
|---|
| 957 | cmd0 = 'q';
|
|---|
| 958 | break;
|
|---|
| 959 | }
|
|---|
| 960 | /* Fall through. */
|
|---|
| 961 | default:
|
|---|
| 962 | flush_line ();
|
|---|
| 963 | /* Fall through. */
|
|---|
| 964 | case '\n':
|
|---|
| 965 | give_help ();
|
|---|
| 966 | continue;
|
|---|
| 967 | }
|
|---|
| 968 | }
|
|---|
| 969 |
|
|---|
| 970 | switch (cmd0)
|
|---|
| 971 | {
|
|---|
| 972 | case 'l':
|
|---|
| 973 | lf_copy (left, llen, outfile);
|
|---|
| 974 | lf_skip (right, rlen);
|
|---|
| 975 | return 1;
|
|---|
| 976 | case 'r':
|
|---|
| 977 | lf_copy (right, rlen, outfile);
|
|---|
| 978 | lf_skip (left, llen);
|
|---|
| 979 | return 1;
|
|---|
| 980 | case 's':
|
|---|
| 981 | suppress_common_lines = 1;
|
|---|
| 982 | break;
|
|---|
| 983 | case 'v':
|
|---|
| 984 | suppress_common_lines = 0;
|
|---|
| 985 | break;
|
|---|
| 986 | case 'q':
|
|---|
| 987 | return 0;
|
|---|
| 988 | case 'e':
|
|---|
| 989 | {
|
|---|
| 990 | int fd;
|
|---|
| 991 |
|
|---|
| 992 | if (tmpname)
|
|---|
| 993 | tmp = fopen (tmpname, "w");
|
|---|
| 994 | else
|
|---|
| 995 | {
|
|---|
| 996 | if ((fd = temporary_file ()) < 0)
|
|---|
| 997 | perror_fatal ("mkstemp");
|
|---|
| 998 | tmp = fdopen (fd, "w");
|
|---|
| 999 | }
|
|---|
| 1000 |
|
|---|
| 1001 | if (! tmp)
|
|---|
| 1002 | perror_fatal (tmpname);
|
|---|
| 1003 |
|
|---|
| 1004 | switch (cmd1)
|
|---|
| 1005 | {
|
|---|
| 1006 | case 'd':
|
|---|
| 1007 | if (llen)
|
|---|
| 1008 | {
|
|---|
| 1009 | if (llen == 1)
|
|---|
| 1010 | fprintf (tmp, "--- %s %ld\n", lname, (long) lline);
|
|---|
| 1011 | else
|
|---|
| 1012 | fprintf (tmp, "--- %s %ld,%ld\n", lname,
|
|---|
| 1013 | (long) lline, (long) (lline + llen - 1));
|
|---|
| 1014 | }
|
|---|
| 1015 | /* Fall through. */
|
|---|
| 1016 | case 'b': case 'l':
|
|---|
| 1017 | lf_copy (left, llen, tmp);
|
|---|
| 1018 | break;
|
|---|
| 1019 |
|
|---|
| 1020 | default:
|
|---|
| 1021 | lf_skip (left, llen);
|
|---|
| 1022 | break;
|
|---|
| 1023 | }
|
|---|
| 1024 |
|
|---|
| 1025 | switch (cmd1)
|
|---|
| 1026 | {
|
|---|
| 1027 | case 'd':
|
|---|
| 1028 | if (rlen)
|
|---|
| 1029 | {
|
|---|
| 1030 | if (rlen == 1)
|
|---|
| 1031 | fprintf (tmp, "+++ %s %ld\n", rname, (long) rline);
|
|---|
| 1032 | else
|
|---|
| 1033 | fprintf (tmp, "+++ %s %ld,%ld\n", rname,
|
|---|
| 1034 | (long) rline, (long) (rline + rlen - 1));
|
|---|
| 1035 | }
|
|---|
| 1036 | /* Fall through. */
|
|---|
| 1037 | case 'b': case 'r':
|
|---|
| 1038 | lf_copy (right, rlen, tmp);
|
|---|
| 1039 | break;
|
|---|
| 1040 |
|
|---|
| 1041 | default:
|
|---|
| 1042 | lf_skip (right, rlen);
|
|---|
| 1043 | break;
|
|---|
| 1044 | }
|
|---|
| 1045 |
|
|---|
| 1046 | ck_fclose (tmp);
|
|---|
| 1047 |
|
|---|
| 1048 | {
|
|---|
| 1049 | int wstatus;
|
|---|
| 1050 | int werrno = 0;
|
|---|
| 1051 | ignore_SIGINT = 1;
|
|---|
| 1052 | checksigs ();
|
|---|
| 1053 |
|
|---|
| 1054 | {
|
|---|
| 1055 | #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)
|
|---|
| 1056 | char *command =
|
|---|
| 1057 | xmalloc (quote_system_arg (0, editor_program)
|
|---|
| 1058 | + 1 + strlen (tmpname) + 1);
|
|---|
| 1059 | sprintf (command + quote_system_arg (command, editor_program),
|
|---|
| 1060 | " %s", tmpname);
|
|---|
| 1061 | wstatus = system (command);
|
|---|
| 1062 | if (wstatus == -1)
|
|---|
| 1063 | werrno = errno;
|
|---|
| 1064 | free (command);
|
|---|
| 1065 | #else
|
|---|
| 1066 | pid_t pid;
|
|---|
| 1067 |
|
|---|
| 1068 | pid = vfork ();
|
|---|
| 1069 | if (pid == 0)
|
|---|
| 1070 | {
|
|---|
| 1071 | char const *argv[3];
|
|---|
| 1072 | int i = 0;
|
|---|
| 1073 |
|
|---|
| 1074 | argv[i++] = editor_program;
|
|---|
| 1075 | argv[i++] = tmpname;
|
|---|
| 1076 | argv[i] = 0;
|
|---|
| 1077 |
|
|---|
| 1078 | execvp (editor_program, (char **) argv);
|
|---|
| 1079 | _exit (errno == ENOEXEC ? 126 : 127);
|
|---|
| 1080 | }
|
|---|
| 1081 |
|
|---|
| 1082 | if (pid < 0)
|
|---|
| 1083 | perror_fatal ("fork");
|
|---|
| 1084 |
|
|---|
| 1085 | while (waitpid (pid, &wstatus, 0) < 0)
|
|---|
| 1086 | if (errno == EINTR)
|
|---|
| 1087 | checksigs ();
|
|---|
| 1088 | else
|
|---|
| 1089 | perror_fatal ("waitpid");
|
|---|
| 1090 | #endif
|
|---|
| 1091 | }
|
|---|
| 1092 |
|
|---|
| 1093 | ignore_SIGINT = 0;
|
|---|
| 1094 | ck_editor_status (werrno, wstatus);
|
|---|
| 1095 | }
|
|---|
| 1096 |
|
|---|
| 1097 | {
|
|---|
| 1098 | char buf[SDIFF_BUFSIZE];
|
|---|
| 1099 | size_t size;
|
|---|
| 1100 | tmp = ck_fopen (tmpname, "r");
|
|---|
| 1101 | while ((size = ck_fread (buf, SDIFF_BUFSIZE, tmp)) != 0)
|
|---|
| 1102 | {
|
|---|
| 1103 | checksigs ();
|
|---|
| 1104 | ck_fwrite (buf, size, outfile);
|
|---|
| 1105 | }
|
|---|
| 1106 | ck_fclose (tmp);
|
|---|
| 1107 | }
|
|---|
| 1108 | return 1;
|
|---|
| 1109 | }
|
|---|
| 1110 | default:
|
|---|
| 1111 | give_help ();
|
|---|
| 1112 | break;
|
|---|
| 1113 | }
|
|---|
| 1114 | }
|
|---|
| 1115 | }
|
|---|
| 1116 |
|
|---|
| 1117 | |
|---|
| 1118 |
|
|---|
| 1119 |
|
|---|
| 1120 | /* Alternately reveal bursts of diff output and handle user commands. */
|
|---|
| 1121 | static bool
|
|---|
| 1122 | interact (struct line_filter *diff,
|
|---|
| 1123 | struct line_filter *left, char const *lname,
|
|---|
| 1124 | struct line_filter *right, char const *rname,
|
|---|
| 1125 | FILE *outfile)
|
|---|
| 1126 | {
|
|---|
| 1127 | lin lline = 1, rline = 1;
|
|---|
| 1128 |
|
|---|
| 1129 | for (;;)
|
|---|
| 1130 | {
|
|---|
| 1131 | char diff_help[256];
|
|---|
| 1132 | int snarfed = lf_snarf (diff, diff_help, sizeof diff_help);
|
|---|
| 1133 |
|
|---|
| 1134 | if (snarfed <= 0)
|
|---|
| 1135 | return snarfed != 0;
|
|---|
| 1136 |
|
|---|
| 1137 | checksigs ();
|
|---|
| 1138 |
|
|---|
| 1139 | if (diff_help[0] == ' ')
|
|---|
| 1140 | puts (diff_help + 1);
|
|---|
| 1141 | else
|
|---|
| 1142 | {
|
|---|
| 1143 | char *numend;
|
|---|
| 1144 | uintmax_t val;
|
|---|
| 1145 | lin llen, rlen, lenmax;
|
|---|
| 1146 | errno = 0;
|
|---|
| 1147 | llen = val = strtoumax (diff_help + 1, &numend, 10);
|
|---|
| 1148 | if (llen < 0 || llen != val || errno || *numend != ',')
|
|---|
| 1149 | fatal (diff_help);
|
|---|
| 1150 | rlen = val = strtoumax (numend + 1, &numend, 10);
|
|---|
| 1151 | if (rlen < 0 || rlen != val || errno || *numend)
|
|---|
| 1152 | fatal (diff_help);
|
|---|
| 1153 |
|
|---|
| 1154 | lenmax = MAX (llen, rlen);
|
|---|
| 1155 |
|
|---|
| 1156 | switch (diff_help[0])
|
|---|
| 1157 | {
|
|---|
| 1158 | case 'i':
|
|---|
| 1159 | if (suppress_common_lines)
|
|---|
| 1160 | lf_skip (diff, lenmax);
|
|---|
| 1161 | else
|
|---|
| 1162 | lf_copy (diff, lenmax, stdout);
|
|---|
| 1163 |
|
|---|
| 1164 | lf_copy (left, llen, outfile);
|
|---|
| 1165 | lf_skip (right, rlen);
|
|---|
| 1166 | break;
|
|---|
| 1167 |
|
|---|
| 1168 | case 'c':
|
|---|
| 1169 | lf_copy (diff, lenmax, stdout);
|
|---|
| 1170 | if (! edit (left, lname, lline, llen,
|
|---|
| 1171 | right, rname, rline, rlen,
|
|---|
| 1172 | outfile))
|
|---|
| 1173 | return 0;
|
|---|
| 1174 | break;
|
|---|
| 1175 |
|
|---|
| 1176 | default:
|
|---|
| 1177 | fatal (diff_help);
|
|---|
| 1178 | }
|
|---|
| 1179 |
|
|---|
| 1180 | lline += llen;
|
|---|
| 1181 | rline += rlen;
|
|---|
| 1182 | }
|
|---|
| 1183 | }
|
|---|
| 1184 | }
|
|---|
| 1185 |
|
|---|
| 1186 | /* Return nonzero if DIR is an existing directory. */
|
|---|
| 1187 | static bool
|
|---|
| 1188 | diraccess (char const *dir)
|
|---|
| 1189 | {
|
|---|
| 1190 | struct stat buf;
|
|---|
| 1191 | return stat (dir, &buf) == 0 && S_ISDIR (buf.st_mode);
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 1194 | #ifndef P_tmpdir
|
|---|
| 1195 | # define P_tmpdir "/tmp"
|
|---|
| 1196 | #endif
|
|---|
| 1197 | #ifndef TMPDIR_ENV
|
|---|
| 1198 | # define TMPDIR_ENV "TMPDIR"
|
|---|
| 1199 | #endif
|
|---|
| 1200 |
|
|---|
| 1201 | /* Open a temporary file and return its file descriptor. Put into
|
|---|
| 1202 | tmpname the address of a newly allocated buffer that holds the
|
|---|
| 1203 | file's name. Use the prefix "sdiff". */
|
|---|
| 1204 | static int
|
|---|
| 1205 | temporary_file (void)
|
|---|
| 1206 | {
|
|---|
| 1207 | char const *tmpdir = getenv (TMPDIR_ENV);
|
|---|
| 1208 | char const *dir = tmpdir ? tmpdir : P_tmpdir;
|
|---|
| 1209 | char *buf = xmalloc (strlen (dir) + 1 + 5 + 6 + 1);
|
|---|
| 1210 | int fd;
|
|---|
| 1211 | int e;
|
|---|
| 1212 | sigset_t procmask;
|
|---|
| 1213 | sigset_t blocked;
|
|---|
| 1214 | sprintf (buf, "%s/sdiffXXXXXX", dir);
|
|---|
| 1215 | sigemptyset (&blocked);
|
|---|
| 1216 | sigaddset (&blocked, SIGINT);
|
|---|
| 1217 | sigprocmask (SIG_BLOCK, &blocked, &procmask);
|
|---|
| 1218 | fd = mkstemp (buf);
|
|---|
| 1219 | e = errno;
|
|---|
| 1220 | if (0 <= fd)
|
|---|
| 1221 | tmpname = buf;
|
|---|
| 1222 | sigprocmask (SIG_SETMASK, &procmask, 0);
|
|---|
| 1223 | errno = e;
|
|---|
| 1224 | return fd;
|
|---|
| 1225 | }
|
|---|