diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2003-07-22 01:16:02 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2003-07-22 01:16:02 +0000 |
commit | ef7f5be681ef9814382af82c117b9483f9c717ea (patch) | |
tree | 8ec0578a7e66ca0937ddf5d7eba1199ebfe58723 | |
parent | bcfb04daae23e7367c282ce19186bd8666214a1d (diff) |
Implement the -L and -T options from GNU diff.
-rw-r--r-- | usr.bin/diff/diff.1 | 42 | ||||
-rw-r--r-- | usr.bin/diff/diff.c | 31 | ||||
-rw-r--r-- | usr.bin/diff/diff.h | 6 | ||||
-rw-r--r-- | usr.bin/diff/diffreg.c | 68 |
4 files changed, 90 insertions, 57 deletions
diff --git a/usr.bin/diff/diff.1 b/usr.bin/diff/diff.1 index 63eaf893175..4103d271649 100644 --- a/usr.bin/diff/diff.1 +++ b/usr.bin/diff/diff.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: diff.1,v 1.15 2003/07/21 23:18:21 millert Exp $ +.\" $OpenBSD: diff.1,v 1.16 2003/07/22 01:16:01 millert Exp $ .\" .\" Copyright (c) 1980, 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\" .\" @(#)diff.1 8.1 (Berkeley) 6/30/93 .\" -.Dd June 25, 2003 +.Dd July 21, 2003 .Dt DIFF 1 .Os .Sh NAME @@ -37,31 +37,35 @@ .Nd differential file and directory comparator .Sh SYNOPSIS .Nm diff -.Op Fl abilqtw +.Op Fl abilqtTw .Oo .Fl c | Fl e | Fl f | .Fl n | Fl u .Oc +.Op Fl L Ar label .Ar file1 file2 .Nm diff -.Op Fl abilqtw -.Op Fl C Ar number +.Op Fl abilqtTw +.Op Fl L Ar label +.Fl C Ar number .Ar file1 file2 .Nm diff .Op Fl abilqtw -.Op Fl D Ar string +.Fl D Ar string .Ar file1 file2 .Nm diff -.Op Fl abilqtw -.Op Fl U Ar number +.Op Fl abilqtTw +.Op Fl L Ar label +.Fl U Ar number .Ar file1 file2 .Nm diff -.Op Fl abilNqtw +.Op Fl abilNqtTw .Oo .Fl c | Fl e | Fl f | .Fl n | Fl u .Oc .Bk -words +.Op Fl L Ar label .Op Fl r .Op Fl s .Op Fl S Ar name @@ -178,6 +182,12 @@ Treat all files as ASCII. .It Fl b Causes trailing blanks (spaces and tabs) to be ignored, and other strings of blanks to compare equal. +.It Fl i +Ignores the case of letters. +E.g., +.Dq A +will compare equal to +.Dq a . .It Fl l Long output format; each text file .Nm diff Ns \'d @@ -186,12 +196,10 @@ is piped through to paginate it; other differences are remembered and summarized after all text file differences are reported. -.It Fl i -Ignores the case of letters. -E.g., -.Dq A -will compare equal to -.Dq a . +.It Fl L Ar label +Print +.Ar label +instead of the first file name and time in the context or unified diff header. .It Fl t Will expand tabs in output lines. Normal or @@ -200,6 +208,10 @@ output adds character(s) to the front of each line which may screw up the indentation of the original source lines and make the output listing difficult to interpret. This option will preserve the original source's indentation. +.It Fl T +Print a tab rather than a space before the rest of the line for the +normal, context or unified output formats. +This makes the alignment of tabs in the line consistent. .It Fl w Is similar to .Fl b diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index e0be80890f1..5e94598900e 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.c,v 1.32 2003/07/21 23:02:35 millert Exp $ */ +/* $OpenBSD: diff.c,v 1.33 2003/07/22 01:16:01 millert Exp $ */ /* * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> @@ -21,7 +21,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diff.c,v 1.32 2003/07/21 23:02:35 millert Exp $"; +static const char rcsid[] = "$OpenBSD: diff.c,v 1.33 2003/07/22 01:16:01 millert Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -39,13 +39,14 @@ static const char rcsid[] = "$OpenBSD: diff.c,v 1.32 2003/07/21 23:02:35 millert #include "diff.h" -int aflag, bflag, iflag, lflag, Nflag, Pflag, rflag, sflag, tflag, wflag; +int aflag, bflag, iflag, lflag, Nflag, Pflag, rflag, sflag, tflag, Tflag, + wflag; int format, context, status; -char *start, *ifdefname, *diffargs; +char *start, *ifdefname, *diffargs, *label; struct stat stb1, stb2; struct excludes *excludes_list; -#define OPTIONS "abC:cD:efhilnNPqrS:stU:uwX:x:" +#define OPTIONS "abC:cD:efhiL:lnNPqrS:sTtU:uwX:x:" static struct option longopts[] = { { "text", no_argument, 0, 'a' }, { "ignore-space-change", no_argument, 0, 'b' }, @@ -55,6 +56,7 @@ static struct option longopts[] = { { "forward-ed", no_argument, 0, 'f' }, { "ignore-case", no_argument, 0, 'i' }, { "paginate", no_argument, 0, 'l' }, + { "label", required_argument, 0, 'L' }, { "new-file", no_argument, 0, 'N' }, { "rcs", no_argument, 0, 'n' }, { "unidirectional-new-file", no_argument, 0, 'P' }, @@ -63,6 +65,7 @@ static struct option longopts[] = { { "report-identical-files", no_argument, 0, 's' }, { "starting-file", required_argument, 0, 'S' }, { "expand-tabs", no_argument, 0, 't' }, + { "intial-tab", no_argument, 0, 'T' }, { "unified", optional_argument, 0, 'U' }, { "ignore-all-space", no_argument, 0, 'w' }, { "exclude", required_argument, 0, 'x' }, @@ -119,6 +122,9 @@ main(int argc, char **argv) case 'i': iflag = 1; break; + case 'L': + label = optarg; + break; case 'l': lflag = 1; signal(SIGPIPE, SIG_IGN); @@ -144,6 +150,9 @@ main(int argc, char **argv) case 's': sflag = 1; break; + case 'T': + Tflag = 1; + break; case 't': tflag = 1; break; @@ -351,12 +360,12 @@ __dead void usage(void) { (void)fprintf(stderr, - "usage: diff [-biqtw] [-c | -e | -f | -n | -u ] file1 file2\n" - " diff [-biqtw] -C number file1 file2\n" - " diff [-biqtw] -D string file1 file2\n" - " diff [-biqtw] -U number file1 file2\n" - " diff [-biNPqwt] [-c | -e | -f | -n | -u ] [-r] [-s] [-S name]" - " [-X file]\n [-x pattern] dir1 dir2\n"); + "usage: diff [-bilqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n" + " diff [-bilqtTw] [-L label] -C number file1 file2\n" + " diff [-bilqtw] -D string file1 file2\n" + " diff [-bilqtTw] [-L label] -U number file1 file2\n" + " diff [-bilNPqwtT] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s] [-S name]\n" + " [-X file] [-x pattern] dir1 dir2\n"); exit(2); } diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h index e7fc2c919fb..4ac9d45c62e 100644 --- a/usr.bin/diff/diff.h +++ b/usr.bin/diff/diff.h @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.h,v 1.20 2003/07/21 21:57:22 millert Exp $ */ +/* $OpenBSD: diff.h,v 1.21 2003/07/22 01:16:01 millert Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -69,9 +69,9 @@ struct excludes { }; extern int aflag, bflag, iflag, lflag, Nflag, Pflag, rflag, sflag, - tflag, wflag; + tflag, Tflag, wflag; extern int format, context, status, anychange; -extern char *start, *ifdefname, *diffargs; +extern char *start, *ifdefname, *diffargs, *label; extern struct stat stb1, stb2; extern struct excludes *excludes_list; diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index 7f2eda06521..e9ae0767967 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffreg.c,v 1.40 2003/07/22 00:20:40 millert Exp $ */ +/* $OpenBSD: diffreg.c,v 1.41 2003/07/22 01:16:01 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -65,7 +65,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.40 2003/07/22 00:20:40 millert Exp $"; +static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.41 2003/07/22 01:16:01 millert Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -209,7 +209,7 @@ static void unsort(struct line *, int, int *); static void change(char *, FILE *, char *, FILE *, int, int, int, int); static void sort(struct line *, int); static int asciifile(FILE *); -static int fetch(long *, int, int, FILE *, char *, int); +static int fetch(long *, int, int, FILE *, int, int); static int newcand(int, int, int); static int search(int *, int, int); static int skipline(FILE *); @@ -947,10 +947,16 @@ restart: /* * Print the context/unidiff header first time through. */ - printf("%s %s %s", format == D_CONTEXT ? "***" : "---", - file1, ctime(&stb1.st_mtime)); - printf("%s %s %s", format == D_CONTEXT ? "---" : "+++", - file2, ctime(&stb2.st_mtime)); + if (label != NULL) + printf("%s %s\n", + format == D_CONTEXT ? "***" : "---", label); + else + printf("%s %s %s", + format == D_CONTEXT ? "***" : "---", file1, + ctime(&stb1.st_mtime)); + printf("%s %s %s", + format == D_CONTEXT ? "---" : "+++", file2, + ctime(&stb2.st_mtime)); anychange = 1; } else if (a > context_vec_ptr->b + (2 * context) && c > context_vec_ptr->d + (2 * context)) { @@ -999,11 +1005,11 @@ restart: break; } if (format == D_NORMAL || format == D_IFDEF) { - fetch(ixold, a, b, f1, "< ", 1); + fetch(ixold, a, b, f1, '<', 1); if (a <= b && c <= d && format == D_NORMAL) puts("---"); } - i = fetch(ixnew, c, d, f2, format == D_NORMAL ? "> " : "", 0); + i = fetch(ixnew, c, d, f2, format == D_NORMAL ? '>' : '\0', 0); if (i != 0 && format == D_EDIT) { /* * A non-zero return value for D_EDIT indicates that the @@ -1027,7 +1033,7 @@ restart: } static int -fetch(long *f, int a, int b, FILE *lb, char *s, int oldfile) +fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile) { int i, j, c, lastc, col, nc; @@ -1059,8 +1065,14 @@ fetch(long *f, int a, int b, FILE *lb, char *s, int oldfile) for (i = a; i <= b; i++) { fseek(lb, f[i - 1], SEEK_SET); nc = f[i] - f[i - 1]; - if (format != D_IFDEF) - fputs(s, stdout); + if (format != D_IFDEF && ch != '\0') { + putchar(ch); + if (Tflag && (format == D_NORMAL || format == D_CONTEXT + || format == D_UNIFIED)) + putchar('\t'); + else if (format != D_UNIFIED) + putchar(' '); + } col = 0; for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) { if ((c = getc(lb)) == EOF) { @@ -1230,16 +1242,16 @@ dump_context_vec(FILE *f1, FILE *f2) ch = (a <= b) ? 'd' : 'a'; if (ch == 'a') - fetch(ixold, lowa, b, f1, " ", 0); + fetch(ixold, lowa, b, f1, ' ', 0); else { - fetch(ixold, lowa, a - 1, f1, " ", 0); + fetch(ixold, lowa, a - 1, f1, ' ', 0); fetch(ixold, a, b, f1, - ch == 'c' ? "! " : "- ", 0); + ch == 'c' ? '!' : '-', 0); } lowa = b + 1; cvp++; } - fetch(ixold, b + 1, upb, f1, " ", 0); + fetch(ixold, b + 1, upb, f1, ' ', 0); } /* output changes to the "new" file */ printf("--- "); @@ -1266,16 +1278,16 @@ dump_context_vec(FILE *f1, FILE *f2) ch = (a <= b) ? 'd' : 'a'; if (ch == 'd') - fetch(ixnew, lowc, d, f2, " ", 0); + fetch(ixnew, lowc, d, f2, ' ', 0); else { - fetch(ixnew, lowc, c - 1, f2, " ", 0); + fetch(ixnew, lowc, c - 1, f2, ' ', 0); fetch(ixnew, c, d, f2, - ch == 'c' ? "! " : "+ ", 0); + ch == 'c' ? '!' : '+', 0); } lowc = d + 1; cvp++; } - fetch(ixnew, d + 1, upd, f2, " ", 0); + fetch(ixnew, d + 1, upd, f2, ' ', 0); } context_vec_ptr = context_vec_start - 1; } @@ -1326,23 +1338,23 @@ dump_unified_vec(FILE *f1, FILE *f2) switch (ch) { case 'c': - fetch(ixold, lowa, a - 1, f1, " ", 0); - fetch(ixold, a, b, f1, "-", 0); - fetch(ixnew, c, d, f2, "+", 0); + fetch(ixold, lowa, a - 1, f1, ' ', 0); + fetch(ixold, a, b, f1, '-', 0); + fetch(ixnew, c, d, f2, '+', 0); break; case 'd': - fetch(ixold, lowa, a - 1, f1, " ", 0); - fetch(ixold, a, b, f1, "-", 0); + fetch(ixold, lowa, a - 1, f1, ' ', 0); + fetch(ixold, a, b, f1, '-', 0); break; case 'a': - fetch(ixnew, lowc, c - 1, f2, " ", 0); - fetch(ixnew, c, d, f2, "+", 0); + fetch(ixnew, lowc, c - 1, f2, ' ', 0); + fetch(ixnew, c, d, f2, '+', 0); break; } lowa = b + 1; lowc = d + 1; } - fetch(ixnew, d + 1, upd, f2, " ", 0); + fetch(ixnew, d + 1, upd, f2, ' ', 0); context_vec_ptr = context_vec_start - 1; } |