diff: improve code style

Reflow comments, strip trailing space, improve wrapping of lines.
This commit is contained in:
Piotr Pawel Stefaniak 2021-08-29 11:15:45 +02:00
parent 22b20b45c9
commit 0358202111
3 changed files with 123 additions and 150 deletions

View File

@ -362,9 +362,9 @@ main(int argc, char **argv)
}
if (dflags & D_EMPTY1 && dflags & D_EMPTY2){
warn("%s", argv[0]);
warn("%s", argv[0]);
warn("%s", argv[1]);
exit(2);
exit(2);
}
if (stb1.st_mode == 0)
@ -477,8 +477,10 @@ print_only(const char *path, size_t dirlen, const char *entry)
void
print_status(int val, char *path1, char *path2, const char *entry)
{
if (label[0] != NULL) path1 = label[0];
if (label[1] != NULL) path2 = label[1];
if (label[0] != NULL)
path1 = label[0];
if (label[1] != NULL)
path2 = label[1];
switch (val) {
case D_BINARY:
@ -535,10 +537,10 @@ usage(void)
" [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
" [-S name] [-X file] [-x pattern] dir1 dir2\n"
" diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
" [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
" [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
" [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
" -y | --side-by-side file1 file2\n");
" [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
" [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
" [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
" -y | --side-by-side file1 file2\n");
exit(2);
}

View File

@ -1,6 +1,6 @@
/* $OpenBSD: diff.h,v 1.34 2020/11/01 18:16:08 jcs Exp $ */
/*ROR
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*

View File

@ -95,66 +95,54 @@ __FBSDID("$FreeBSD$");
*/
/*
* Uses an algorithm due to Harold Stone, which finds
* a pair of longest identical subsequences in the two
* files.
* Uses an algorithm due to Harold Stone, which finds a pair of longest
* identical subsequences in the two files.
*
* The major goal is to generate the match vector J.
* J[i] is the index of the line in file1 corresponding
* to line i file0. J[i] = 0 if there is no
* The major goal is to generate the match vector J. J[i] is the index of
* the line in file1 corresponding to line i file0. J[i] = 0 if there is no
* such line in file1.
*
* Lines are hashed so as to work in core. All potential
* matches are located by sorting the lines of each file
* on the hash (called ``value''). In particular, this
* collects the equivalence classes in file1 together.
* Subroutine equiv replaces the value of each line in
* file0 by the index of the first element of its
* matching equivalence in (the reordered) file1.
* To save space equiv squeezes file1 into a single
* array member in which the equivalence classes
* are simply concatenated, except that their first
* members are flagged by changing sign.
* Lines are hashed so as to work in core. All potential matches are
* located by sorting the lines of each file on the hash (called
* ``value''). In particular, this collects the equivalence classes in
* file1 together. Subroutine equiv replaces the value of each line in
* file0 by the index of the first element of its matching equivalence in
* (the reordered) file1. To save space equiv squeezes file1 into a single
* array member in which the equivalence classes are simply concatenated,
* except that their first members are flagged by changing sign.
*
* Next the indices that point into member are unsorted into
* array class according to the original order of file0.
* Next the indices that point into member are unsorted into array class
* according to the original order of file0.
*
* The cleverness lies in routine stone. This marches
* through the lines of file0, developing a vector klist
* of "k-candidates". At step i a k-candidate is a matched
* pair of lines x,y (x in file0 y in file1) such that
* there is a common subsequence of length k
* between the first i lines of file0 and the first y
* lines of file1, but there is no such subsequence for
* any smaller y. x is the earliest possible mate to y
* that occurs in such a subsequence.
* The cleverness lies in routine stone. This marches through the lines of
* file0, developing a vector klist of "k-candidates". At step i
* a k-candidate is a matched pair of lines x,y (x in file0 y in file1)
* such that there is a common subsequence of length k between the first
* i lines of file0 and the first y lines of file1, but there is no such
* subsequence for any smaller y. x is the earliest possible mate to y that
* occurs in such a subsequence.
*
* Whenever any of the members of the equivalence class of
* lines in file1 matable to a line in file0 has serial number
* less than the y of some k-candidate, that k-candidate
* with the smallest such y is replaced. The new
* k-candidate is chained (via pred) to the current
* k-1 candidate so that the actual subsequence can
* be recovered. When a member has serial number greater
* that the y of all k-candidates, the klist is extended.
* At the end, the longest subsequence is pulled out
* and placed in the array J by unravel
* Whenever any of the members of the equivalence class of lines in file1
* matable to a line in file0 has serial number less than the y of some
* k-candidate, that k-candidate with the smallest such y is replaced. The
* new k-candidate is chained (via pred) to the current k-1 candidate so
* that the actual subsequence can be recovered. When a member has serial
* number greater that the y of all k-candidates, the klist is extended. At
* the end, the longest subsequence is pulled out and placed in the array J
* by unravel.
*
* With J in hand, the matches there recorded are
* check'ed against reality to assure that no spurious
* matches have crept in due to hashing. If they have,
* they are broken, and "jackpot" is recorded--a harmless
* matter except that a true match for a spuriously
* mated line may now be unnecessarily reported as a change.
* With J in hand, the matches there recorded are check'ed against reality
* to assure that no spurious matches have crept in due to hashing. If they
* have, they are broken, and "jackpot" is recorded -- a harmless matter
* except that a true match for a spuriously mated line may now be
* unnecessarily reported as a change.
*
* Much of the complexity of the program comes simply
* from trying to minimize core utilization and
* maximize the range of doable problems by dynamically
* allocating what is needed and reusing what is not.
* The core requirements for problems larger than somewhat
* are (in words) 2*length(file0) + length(file1) +
* 3*(number of k-candidates installed), typically about
* 6n words for files of length n.
* Much of the complexity of the program comes simply from trying to
* minimize core utilization and maximize the range of doable problems by
* dynamically allocating what is needed and reusing what is not. The core
* requirements for problems larger than somewhat are (in words)
* 2*length(file0) + length(file1) + 3*(number of k-candidates installed),
* typically about 6n words for files of length n.
*/
struct cand {
@ -213,24 +201,24 @@ static int files_differ(FILE *, FILE *, int);
static char *match_function(const long *, int, FILE *);
static char *preadline(int, size_t, off_t);
static int *J; /* will be overlaid on class */
static int *class; /* will be overlaid on file[0] */
static int *klist; /* will be overlaid on file[0] after class */
static int *member; /* will be overlaid on file[1] */
static int clen;
static int inifdef; /* whether or not we are in a #ifdef block */
static int len[2];
static int pref, suff; /* length of prefix and suffix */
static int slen[2];
static int anychange;
static int hw, padding; /* half width and padding */
static int edoffset;
static long *ixnew; /* will be overlaid on file[1] */
static long *ixold; /* will be overlaid on klist */
static int *J; /* will be overlaid on class */
static int *class; /* will be overlaid on file[0] */
static int *klist; /* will be overlaid on file[0] after class */
static int *member; /* will be overlaid on file[1] */
static int clen;
static int inifdef; /* whether or not we are in a #ifdef block */
static int len[2];
static int pref, suff; /* length of prefix and suffix */
static int slen[2];
static int anychange;
static int hw, padding; /* half width and padding */
static int edoffset;
static long *ixnew; /* will be overlaid on file[1] */
static long *ixold; /* will be overlaid on klist */
static struct cand *clist; /* merely a free storage pot for candidates */
static int clistlen; /* the length of clist */
static int clistlen; /* the length of clist */
static struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
static int (*chrtran)(int); /* translation table for case-folding */
static int (*chrtran)(int); /* translation table for case-folding */
static struct context_vec *context_vec_start;
static struct context_vec *context_vec_end;
static struct context_vec *context_vec_ptr;
@ -251,7 +239,7 @@ static int
cup2low(int c)
{
return tolower(c);
return (tolower(c));
}
int
@ -278,11 +266,11 @@ diffreg(char *file1, char *file2, int flags, int capsicum)
padding = tabsize - (hw % tabsize);
if ((flags & D_EXPANDTABS) != 0 || (padding % tabsize == 0))
padding = MIN_PAD;
hw = (width >> 1) -
((padding == MIN_PAD) ? (padding << 1) : padding) - 1;
}
if (flags & D_IGNORECASE)
chrtran = cup2low;
@ -622,7 +610,7 @@ stone(int *a, int n, int *b, int *c, int flags)
{
int i, k, y, j, l;
int oldc, tc, oldl, sq;
u_int numtries, bound;
unsigned numtries, bound;
if (flags & D_MINIMAL)
bound = UINT_MAX;
@ -720,9 +708,9 @@ unravel(int p)
/*
* Check does double duty:
* 1. ferret out any fortuitous correspondences due
* to confounding by hashing (which result in "jackpot")
* 2. collect random access indexes to the two files
* 1. ferret out any fortuitous correspondences due to confounding by
* hashing (which result in "jackpot")
* 2. collect random access indexes to the two files
*/
static void
check(FILE *f1, FILE *f2, int flags)
@ -745,7 +733,7 @@ check(FILE *f1, FILE *f2, int flags)
ixnew[j] = ctnew += skipline(f2);
j++;
}
if (flags & (D_FOLDBLANKS|D_IGNOREBLANKS|D_IGNORECASE|D_STRIPCR)) {
if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS | D_IGNORECASE | D_STRIPCR)) {
for (;;) {
c = getc(f1);
d = getc(f2);
@ -753,7 +741,7 @@ check(FILE *f1, FILE *f2, int flags)
* GNU diff ignores a missing newline
* in one file for -b or -w.
*/
if (flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) {
if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS)) {
if (c == EOF && d == '\n') {
ctnew++;
break;
@ -793,7 +781,7 @@ check(FILE *f1, FILE *f2, int flags)
break;
ctnew++;
} while (isspace(d = getc(f2)));
} else if ((flags & D_IGNOREBLANKS)) {
} else if (flags & D_IGNOREBLANKS) {
while (isspace(c) && c != '\n') {
c = getc(f1);
ctold++;
@ -913,16 +901,11 @@ output(char *file1, FILE *f1, char *file2, FILE *f2, int flags)
J[m + 1] = len[1] + 1;
if (diff_format != D_EDIT) {
for (i0 = 1; i0 <= m; i0 = i1 + 1) {
while (i0 <= m && J[i0] == J[i0 - 1] + 1){
if (diff_format == D_SIDEBYSIDE &&
suppress_common != 1) {
nc = fetch(ixold, i0, i0, f1, '\0',
1, flags);
print_space(nc,
(hw - nc) + (padding << 1) + 1,
flags);
fetch(ixnew, J[i0], J[i0], f2, '\0',
0, flags);
while (i0 <= m && J[i0] == J[i0 - 1] + 1) {
if (diff_format == D_SIDEBYSIDE && suppress_common != 1) {
nc = fetch(ixold, i0, i0, f1, '\0', 1, flags);
print_space(nc, (hw - nc) + (padding << 1) + 1, flags);
fetch(ixnew, J[i0], J[i0], f2, '\0', 0, flags);
printf("\n");
}
i0++;
@ -935,33 +918,28 @@ output(char *file1, FILE *f1, char *file2, FILE *f2, int flags)
J[i1] = j1;
/*
* When using side-by-side, lines from both of the
* files are printed. The algorithm used by diff(1)
* identifies the ranges in which two files differ.
* When using side-by-side, lines from both of the files are
* printed. The algorithm used by diff(1) identifies the ranges
* in which two files differ.
* See the change() function below.
* The for loop below consumes the shorter range,
* whereas one of the while loops deals with the
* longer one.
* The for loop below consumes the shorter range, whereas one of
* the while loops deals with the longer one.
*/
if (diff_format == D_SIDEBYSIDE) {
for (i=i0, j=j0; i<=i1 && j<=j1; i++, j++)
change(file1, f1, file2, f2, i, i,
j, j, &flags);
for (i = i0, j = j0; i <= i1 && j <= j1; i++, j++)
change(file1, f1, file2, f2, i, i, j, j, &flags);
while (i <= i1) {
change(file1, f1, file2, f2,
i, i, j+1, j, &flags);
change(file1, f1, file2, f2, i, i, j + 1, j, &flags);
i++;
}
while (j <= j1) {
change(file1, f1, file2, f2,
i+1, i, j, j, &flags);
change(file1, f1, file2, f2, i + 1, i, j, j, &flags);
j++;
}
} else
change(file1, f1, file2, f2, i0, i1, j0,
j1, &flags);
change(file1, f1, file2, f2, i0, i1, j0, j1, &flags);
}
} else {
for (i0 = m; i0 >= 1; i0 = i1 - 1) {
@ -1078,9 +1056,8 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
if (ignore_pats != NULL || skip_blanks) {
char *line;
/*
* All lines in the change, insert, or delete must
* match an ignore pattern for the change to be
* ignored.
* All lines in the change, insert, or delete must match an ignore
* pattern for the change to be ignored.
*/
if (a <= b) { /* Changes and deletes. */
for (i = a; i <= b; i++) {
@ -1202,7 +1179,7 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
nc = fetch(ixold, a, b, f1, '\0', 1, *pflags);
print_space(nc, hw - nc + padding, *pflags);
}
printf("%c", (a>b)? '>' : ((c>d)? '<' : '|'));
printf("%c", (a > b) ? '>' : ((c > d) ? '<' : '|'));
print_space(hw + padding + 1 , padding, *pflags);
fetch(ixnew, c, d, f2, '\0', 0, *pflags);
printf("\n");
@ -1216,11 +1193,11 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0, *pflags);
if (edoffset != 0 && diff_format == D_EDIT) {
/*
* A non-zero edoffset value for D_EDIT indicates that the
* last line printed was a bare dot (".") that has been
* escaped as ".." to prevent ed(1) from misinterpreting
* it. We have to add a substitute command to change this
* back and restart where we left off.
* A non-zero edoffset value for D_EDIT indicates that the last line
* printed was a bare dot (".") that has been escaped as ".." to
* prevent ed(1) from misinterpreting it. We have to add a
* substitute command to change this back and restart where we left
* off.
*/
printf(".\n");
printf("%ds/.//\n", a + edoffset - 1);
@ -1271,10 +1248,10 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
}
for (i = a; i <= b; i++) {
fseek(lb, f[i - 1], SEEK_SET);
nc = (f[i] - f[i - 1]);
nc = f[i] - f[i - 1];
if (diff_format == D_SIDEBYSIDE && hw < nc)
nc = hw;
if ((diff_format != D_IFDEF && diff_format != D_GFORMAT) &&
if (diff_format != D_IFDEF && diff_format != D_GFORMAT &&
ch != '\0') {
printf("%c", ch);
if (Tflag && (diff_format == D_NORMAL ||
@ -1301,9 +1278,8 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
diff_format == D_NREVERSE)
warnx("No newline at end of file");
else
printf("\n\\ No newline at end of "
"file\n");
return col;
printf("\n\\ No newline at end of file\n");
return (col);
}
/*
* when using --side-by-side, col needs to be increased
@ -1311,8 +1287,8 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
*/
if (c == '\t') {
if (flags & D_EXPANDTABS) {
newcol = ((col/tabsize)+1)*tabsize;
do {
newcol = ((col / tabsize) + 1) * tabsize;
do {
if (diff_format == D_SIDEBYSIDE)
j++;
printf(" ");
@ -1320,8 +1296,7 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
} else {
if (diff_format == D_SIDEBYSIDE) {
if ((j + tabsize) > nc) {
printf("%*s",
nc - j,"");
printf("%*s", nc - j, "");
j = col = nc;
} else {
printf("\t");
@ -1334,20 +1309,17 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
}
}
} else {
if (diff_format == D_EDIT && j == 1 && c == '\n'
&& lastc == '.') {
if (diff_format == D_EDIT && j == 1 && c == '\n' &&
lastc == '.') {
/*
* Don't print a bare "." line
* since that will confuse ed(1).
* Print ".." instead and set the,
* global variable edoffset to an
* offset from which to restart.
* The caller must check the value
* of edoffset
* Don't print a bare "." line since that will confuse
* ed(1). Print ".." instead and set the, global variable
* edoffset to an offset from which to restart. The
* caller must check the value of edoffset
*/
printf(".\n");
edoffset = i - a + 1;
return edoffset;
return (edoffset);
}
/* when side-by-side, do not print a newline */
if (diff_format != D_SIDEBYSIDE || c != '\n') {
@ -1357,7 +1329,7 @@ fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
}
}
}
return col;
return (col);
}
/*
@ -1428,7 +1400,7 @@ asciifile(FILE *f)
return (memchr(buf, '\0', cnt) == NULL);
}
#define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
#define begins_with(s, pre) (strncmp(s, pre, sizeof(pre) - 1) == 0)
static char *
match_function(const long *f, int pos, FILE *fp)
@ -1459,18 +1431,17 @@ match_function(const long *f, int pos, FILE *fp)
if (!state)
state = " (public)";
} else {
strlcpy(lastbuf, buf, sizeof lastbuf);
strlcpy(lastbuf, buf, sizeof(lastbuf));
if (state)
strlcat(lastbuf, state,
sizeof lastbuf);
strlcat(lastbuf, state, sizeof(lastbuf));
lastmatchline = pos;
return lastbuf;
return (lastbuf);
}
}
}
pos--;
}
return lastmatchline > 0 ? lastbuf : NULL;
return (lastmatchline > 0 ? lastbuf : NULL);
}
/* dump accumulated "context" diff changes */
@ -1493,7 +1464,7 @@ dump_context_vec(FILE *f1, FILE *f2, int flags)
printf("***************");
if ((flags & D_PROTOTYPE)) {
f = match_function(ixold, lowa-1, f1);
f = match_function(ixold, lowa - 1, f1);
if (f != NULL)
printf(" %s", f);
}
@ -1600,7 +1571,7 @@ dump_unified_vec(FILE *f1, FILE *f2, int flags)
uni_range(lowc, upd);
printf(" @@");
if ((flags & D_PROTOTYPE)) {
f = match_function(ixold, lowa-1, f1);
f = match_function(ixold, lowa - 1, f1);
if (f != NULL)
printf(" %s", f);
}