source: trunk/essentials/sys-apps/diffutils/src/sdiff.c@ 3506

Last change on this file since 3506 was 2556, checked in by bird, 20 years ago

diffutils 2.8.1

File size: 26.0 KB
Line 
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
35static char const authorship_msgid[] = N_("Written by Thomas Lord.");
36
37static char const copyright_string[] =
38 "Copyright (C) 2002 Free Software Foundation, Inc.";
39
40extern 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
45char *program_name;
46
47static char const *editor_program = DEFAULT_EDITOR_PROGRAM;
48static char const **diffargv;
49
50static char * volatile tmpname;
51static FILE *tmp;
52
53#if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
54static pid_t volatile diffpid;
55#endif
56
57struct line_filter;
58
59static RETSIGTYPE catchsig (int);
60static bool edit (struct line_filter *, char const *, lin, lin, struct line_filter *, char const *, lin, lin, FILE *);
61static bool interact (struct line_filter *, struct line_filter *, char const *, struct line_filter *, char const *, FILE *);
62static void checksigs (void);
63static void diffarg (char const *);
64static void fatal (char const *) __attribute__((noreturn));
65static void perror_fatal (char const *) __attribute__((noreturn));
66static void trapsigs (void);
67static void untrapsig (int);
68
69#define NUM_SIGS (sizeof sigs / sizeof *sigs)
70static 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
120static bool diraccess (char const *);
121static int temporary_file (void);
122
123/* Options: */
124
125/* Name of output file if -o specified. */
126static char const *output;
127
128/* Do not print common lines. */
129static bool suppress_common_lines;
130
131/* Value for the long option that does not have single-letter equivalents. */
132enum
133{
134 DIFF_PROGRAM_OPTION = CHAR_MAX + 1,
135 HELP_OPTION,
136 STRIP_TRAILING_CR_OPTION
137};
138
139static 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
162static void try_help (char const *, char const *) __attribute__((noreturn));
163static void
164try_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
173static void
174check_stdout (void)
175{
176 if (ferror (stdout))
177 fatal ("write failed");
178 else if (fclose (stdout) != 0)
179 perror_fatal (_("standard output"));
180}
181
182static 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
209static void
210usage (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 <[email protected]>."));
224}
225
226static void
227cleanup (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
237static void exiterr (void) __attribute__((noreturn));
238static void
239exiterr (void)
240{
241 cleanup ();
242 untrapsig (0);
243 checksigs ();
244 exit (EXIT_TROUBLE);
245}
246
247static void
248fatal (char const *msgid)
249{
250 error (0, 0, "%s", _(msgid));
251 exiterr ();
252}
253
254static void
255perror_fatal (char const *msg)
256{
257 int e = errno;
258 checksigs ();
259 error (0, e, "%s", msg);
260 exiterr ();
261}
262
263static void
264ck_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
284static FILE *
285ck_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
293static void
294ck_fclose (FILE *f)
295{
296 if (fclose (f))
297 perror_fatal ("fclose");
298}
299
300static size_t
301ck_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
309static void
310ck_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
316static void
317ck_fflush (FILE *f)
318{
319 if (fflush (f) != 0)
320 perror_fatal (_("write failed"));
321}
322
323static char const *
324expand_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
347struct line_filter {
348 FILE *infile;
349 char *bufpos;
350 char *buffer;
351 char *buflim;
352};
353
354static void
355lf_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 */
363static size_t
364lf_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 */
375static void
376lf_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 */
401static void
402lf_skip (struct line_filter *lf, lin lines)