sdiff.c
来自「WinMerge可以显示两个文件的不同之处」· C语言 代码 · 共 1,196 行 · 第 1/2 页
C
1,196 行
/* SDIFF -- interactive merge front end to diff
Copyright (C) 1992, 1993 Free Software Foundation, Inc.
This file is part of GNU DIFF.
GNU DIFF is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU DIFF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU DIFF; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* GNU SDIFF was written by Thomas Lord. */
#include "system.h"
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include "getopt.h"
/* Size of chunks read from files which must be parsed into lines. */
#define SDIFF_BUFSIZE ((size_t) 65536)
/* Default name of the diff program */
#ifndef DIFF_PROGRAM
#define DIFF_PROGRAM "/usr/bin/diff"
#endif
/* Users' editor of nonchoice */
#ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "ed"
#endif
extern char version_string[];
static char const *prog;
static char const *diffbin = DIFF_PROGRAM;
static char const *edbin = DEFAULT_EDITOR;
static char *tmpname;
static int volatile tmpmade;
static pid_t volatile diffpid;
struct line_filter;
static FILE *ck_fdopen PARAMS((int, char const *));
static FILE *ck_fopen PARAMS((char const *, char const *));
static RETSIGTYPE catchsig PARAMS((int));
static VOID *xmalloc PARAMS((size_t));
static char const *expand_name PARAMS((char *, int, char const *));
static int edit PARAMS((struct line_filter *, int, struct line_filter *, int, FILE*));
static int interact PARAMS((struct line_filter *, struct line_filter *, struct line_filter *, FILE*));
static int lf_snarf PARAMS((struct line_filter *, char *, size_t));
static int skip_white PARAMS((void));
static size_t ck_fread PARAMS((char *, size_t, FILE *));
static size_t lf_refill PARAMS((struct line_filter *));
static void checksigs PARAMS((void));
static void ck_fclose PARAMS((FILE *));
static void ck_fflush PARAMS((FILE *));
static void ck_fwrite PARAMS((char const *, size_t, FILE *));
static void cleanup PARAMS((void));
static void diffarg PARAMS((char const *));
static void execdiff PARAMS((int, char const *, char const *, char const *));
static void exiterr PARAMS((void));
static void fatal PARAMS((char const *));
static void flush_line PARAMS((void));
static void give_help PARAMS((void));
static void lf_copy PARAMS((struct line_filter *, int, FILE *));
static void lf_init PARAMS((struct line_filter *, FILE *));
static void lf_skip PARAMS((struct line_filter *, int));
static void perror_fatal PARAMS((char const *));
static void trapsigs PARAMS((void));
static void untrapsig PARAMS((int));
static void usage PARAMS((void));
/* this lossage until the gnu libc conquers the universe */
#define PVT_tmpdir "/tmp"
static char *private_tempnam PARAMS((char const *, char const *, int, size_t *));
static int diraccess PARAMS((char const *));
static int exists PARAMS((char const *));
/* Options: */
/* name of output file if -o spec'd */
static char *out_file;
/* do not print common lines if true, set by -s option */
static int suppress_common_flag;
static struct option const longopts[] =
{
{"ignore-blank-lines", 0, 0, 'B'},
{"speed-large-files", 0, 0, 'H'},
{"ignore-matching-lines", 1, 0, 'I'},
{"ignore-all-space", 0, 0, 'W'}, /* swap W and w for historical reasons */
{"text", 0, 0, 'a'},
{"ignore-space-change", 0, 0, 'b'},
{"minimal", 0, 0, 'd'},
{"ignore-case", 0, 0, 'i'},
{"left-column", 0, 0, 'l'},
{"output", 1, 0, 'o'},
{"suppress-common-lines", 0, 0, 's'},
{"expand-tabs", 0, 0, 't'},
{"width", 1, 0, 'w'},
{"version", 0, 0, 'v'},
{0, 0, 0, 0}
};
/* prints usage message and quits */
static void
usage ()
{
fprintf (stderr, "Usage: %s [options] from-file to-file\n", prog);
fprintf (stderr, "Options:\n\
[-abBdHilstv] [-I regexp] [-o outfile] [-w columns]\n\
[--text] [--minimal] [--speed-large-files] [--expand-tabs]\n\
[--ignore-case] [--ignore-matching-lines=regexp]\n\
[--ignore-space-change] [--ignore-blank-lines] [--ignore-all-space]\n\
[--suppress-common-lines] [--left-column] [--output=outfile]\n\
[--version] [--width=columns]\n");
exit (2);
}
static void
cleanup ()
{
if (0 < diffpid)
kill (diffpid, SIGPIPE);
if (tmpmade)
unlink (tmpname);
}
static void
exiterr ()
{
cleanup ();
untrapsig (0);
checksigs ();
exit (2);
}
static void
fatal (msg)
char const *msg;
{
fprintf (stderr, "%s: %s\n", prog, msg);
exiterr ();
}
static void
perror_fatal (msg)
char const *msg;
{
int e = errno;
checksigs ();
fprintf (stderr, "%s: ", prog);
errno = e;
perror (msg);
exiterr ();
}
/* malloc freely or DIE! */
static VOID *
xmalloc (size)
size_t size;
{
VOID *r = (VOID *) malloc (size);
if (!r)
fatal ("virtual memory exhausted");
return r;
}
static FILE *
ck_fopen (fname, type)
char const *fname, *type;
{
FILE *r = fopen (fname, type);
if (!r)
perror_fatal (fname);
return r;
}
static FILE *
ck_fdopen (fd, type)
int fd;
char const *type;
{
FILE *r = fdopen (fd, type);
if (!r)
perror_fatal ("fdopen");
return r;
}
static void
ck_fclose (f)
FILE *f;
{
if (fclose (f))
perror_fatal ("input/output error");
}
static size_t
ck_fread (buf, size, f)
char *buf;
size_t size;
FILE *f;
{
size_t r = fread (buf, sizeof (char), size, f);
if (r == 0 && ferror (f))
perror_fatal ("input error");
return r;
}
static void
ck_fwrite (buf, size, f)
char const *buf;
size_t size;
FILE *f;
{
if (fwrite (buf, sizeof (char), size, f) != size)
perror_fatal ("output error");
}
static void
ck_fflush (f)
FILE *f;
{
if (fflush (f) != 0)
perror_fatal ("output error");
}
#if !HAVE_MEMCHR
char *
memchr (s, c, n)
char const *s;
int c;
size_t n;
{
unsigned char const *p = (unsigned char const *) s, *lim = p + n;
for (; p < lim; p++)
if (*p == c)
return (char *) p;
return 0;
}
#endif
#ifndef HAVE_WAITPID
/* Emulate waitpid well enough for sdiff, which has at most two children. */
static pid_t
waitpid (pid, stat_loc, options)
pid_t pid;
int *stat_loc;
int options;
{
static int ostatus;
static pid_t opid;
int npid, status;
if (pid == opid)
{
opid = 0;
status = ostatus;
}
else
while ((npid = wait (&status)) != pid)
{
if (npid < 0)
return npid;
opid = npid;
ostatus = status;
}
*stat_loc = status;
return pid;
}
#endif
static char const *
expand_name (name, isdir, other_name)
char *name;
int isdir;
char const *other_name;
{
if (strcmp (name, "-") == 0)
fatal ("cannot interactively merge standard input");
if (!isdir)
return name;
else
{
/* Yield NAME/BASE, where BASE is OTHER_NAME's basename. */
char const
*p = strrchr (other_name, '/'),
*base = p ? p+1 : other_name;
size_t namelen = strlen (name), baselen = strlen (base);
char *r = xmalloc (namelen + baselen + 2);
memcpy (r, name, namelen);
r[namelen] = '/';
memcpy (r + namelen + 1, base, baselen + 1);
return r;
}
}
struct line_filter {
FILE *infile;
char *bufpos;
char *buffer;
char *buflim;
};
static void
lf_init (lf, infile)
struct line_filter *lf;
FILE *infile;
{
lf->infile = infile;
lf->bufpos = lf->buffer = lf->buflim = xmalloc (SDIFF_BUFSIZE + 1);
lf->buflim[0] = '\n';
}
/* Fill an exhausted line_filter buffer from its INFILE */
static size_t
lf_refill (lf)
struct line_filter *lf;
{
size_t s = ck_fread (lf->buffer, SDIFF_BUFSIZE, lf->infile);
lf->bufpos = lf->buffer;
lf->buflim = lf->buffer + s;
lf->buflim[0] = '\n';
checksigs ();
return s;
}
/* Advance LINES on LF's infile, copying lines to OUTFILE */
static void
lf_copy (lf, lines, outfile)
struct line_filter *lf;
int lines;
FILE *outfile;
{
char *start = lf->bufpos;
while (lines)
{
lf->bufpos = (char *) memchr (lf->bufpos, '\n', lf->buflim - lf->bufpos);
if (! lf->bufpos)
{
ck_fwrite (start, lf->buflim - start, outfile);
if (! lf_refill (lf))
return;
start = lf->bufpos;
}
else
{
--lines;
++lf->bufpos;
}
}
ck_fwrite (start, lf->bufpos - start, outfile);
}
/* Advance LINES on LF's infile without doing output */
static void
lf_skip (lf, lines)
struct line_filter *lf;
int lines;
{
while (lines)
{
lf->bufpos = (char *) memchr (lf->bufpos, '\n', lf->buflim - lf->bufpos);
if (! lf->bufpos)
{
if (! lf_refill (lf))
break;
}
else
{
--lines;
++lf->bufpos;
}
}
}
/* Snarf a line into a buffer. Return EOF if EOF, 0 if error, 1 if OK. */
static int
lf_snarf (lf, buffer, bufsize)
struct line_filter *lf;
char *buffer;
size_t bufsize;
{
char *start = lf->bufpos;
for (;;)
{
char *next = (char *) memchr (start, '\n', lf->buflim + 1 - start);
size_t s = next - start;
if (bufsize <= s)
return 0;
memcpy (buffer, start, s);
if (next < lf->buflim)
{
buffer[s] = 0;
lf->bufpos = next + 1;
return 1;
}
if (! lf_refill (lf))
return s ? 0 : EOF;
buffer += s;
bufsize -= s;
start = next;
}
}
int
main (argc, argv)
int argc;
char *argv[];
{
int opt;
int version_requested = 0;
char *editor = getenv ("EDITOR");
char *differ = getenv ("DIFF");
prog = argv[0];
if (editor)
edbin = editor;
if (differ)
diffbin = differ;
diffarg ("diff");
/* parse command line args */
while ((opt = getopt_long (argc, argv, "abBdHiI:lo:stvw:W", longopts, 0))
!= EOF)
{
switch (opt)
{
case 'a':
diffarg ("-a");
break;
case 'b':
diffarg ("-b");
break;
case 'B':
diffarg ("-B");
break;
case 'd':
diffarg ("-d");
break;
case 'H':
diffarg ("-H");
break;
case 'i':
diffarg ("-i");
break;
case 'I':
diffarg ("-I");
diffarg (optarg);
break;
case 'l':
diffarg ("--left-column");
break;
case 'o':
out_file = optarg;
break;
case 's':
suppress_common_flag = 1;
break;
case 't':
diffarg ("-t");
break;
case 'v':
version_requested = 1;
fprintf (stderr, "GNU sdiff version %s\n", version_string);
ck_fflush (stderr);
break;
case 'w':
diffarg ("-W");
diffarg (optarg);
break;
case 'W':
diffarg ("-w");
break;
default:
usage ();
}
}
/* check: did user just want version message? if so exit. */
if (version_requested && argc - optind == 0)
exit (0);
if (argc - optind != 2)
usage ();
if (! out_file)
/* easy case: diff does everything for us */
execdiff (suppress_common_flag, "-y", argv[optind], argv[optind + 1]);
else
{
FILE *left, *right, *out, *diffout;
int diff_fds[2];
int interact_ok;
pid_t pid;
struct line_filter lfilt;
struct line_filter rfilt;
struct line_filter diff_filt;
int leftdir = diraccess (argv[optind]);
int rightdir = diraccess (argv[optind + 1]);
if (leftdir && rightdir)
fatal ("both files to be compared are directories");
left = ck_fopen (expand_name (argv[optind], leftdir, argv[optind + 1]), "r");
;
right = ck_fopen (expand_name (argv[optind + 1], rightdir, argv[optind]), "r");
out = ck_fopen (out_file, "w");
if (pipe (diff_fds))
perror_fatal ("pipe");
trapsigs ();
diffpid = pid = vfork ();
if (pid == 0)
{
signal (SIGINT, SIG_IGN); /* in case user interrupts editor */
signal (SIGPIPE, SIG_DFL);
close (diff_fds[0]);
if (diff_fds[1] != STDOUT_FILENO)
{
dup2 (diff_fds[1], STDOUT_FILENO);
close (diff_fds[1]);
}
execdiff (0, "--sdiff-merge-assist", argv[optind], argv[optind + 1]);
}
if (pid < 0)
perror_fatal ("fork failed");
close (diff_fds[1]);
diffout = ck_fdopen (diff_fds[0], "r");
lf_init (&diff_filt, diffout);
lf_init (&lfilt, left);
lf_init (&rfilt, right);
interact_ok = interact (&diff_filt, &lfilt, &rfilt, out);
ck_fclose (diffout);
ck_fclose (left);
ck_fclose (right);
ck_fclose (out);
{
int wstatus;
while (waitpid (pid, &wstatus, 0) < 0)
if (errno == EINTR)
checksigs ();
else
perror_fatal ("wait failed");
diffpid = 0;
if (tmpmade)
{
unlink (tmpname);
tmpmade = 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?