From 1e6e5a372e0b9f8aadc1aa1a24a380f4a1d9332e Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Wed, 28 Jul 2021 13:51:58 -0400 Subject: Update diff(1) --color patch --- diff-1-Add-color-support.patch | 105 ++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 33 deletions(-) diff --git a/diff-1-Add-color-support.patch b/diff-1-Add-color-support.patch index 31b1fde..da8838c 100644 --- a/diff-1-Add-color-support.patch +++ b/diff-1-Add-color-support.patch @@ -1,14 +1,14 @@ -From 392ea394e6ae7fe34aed7b0b98ab4d47b3652df2 Mon Sep 17 00:00:00 2001 +From 30124d753b8e95bc4180506b7cd9dfb3280ad364 Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Fri, 28 May 2021 15:21:45 -0400 Subject: [PATCH] diff(1): Add --color support --- - usr.bin/diff/diff.1 | 17 +++++++++++++++++ - usr.bin/diff/diff.c | 39 ++++++++++++++++++++++++++++++++++++++- - usr.bin/diff/diff.h | 3 ++- - usr.bin/diff/diffreg.c | 16 ++++++++++++++++ - 4 files changed, 73 insertions(+), 2 deletions(-) + usr.bin/diff/diff.1 | 17 ++++++++++ + usr.bin/diff/diff.c | 71 +++++++++++++++++++++++++++++++++++++++++- + usr.bin/diff/diff.h | 3 +- + usr.bin/diff/diffreg.c | 16 ++++++++++ + 4 files changed, 105 insertions(+), 2 deletions(-) diff --git a/usr.bin/diff/diff.1 b/usr.bin/diff/diff.1 index e0a790f6efb..50fd43902d5 100644 @@ -81,24 +81,30 @@ index e0a790f6efb..50fd43902d5 100644 Try very hard to produce a diff as small as possible. This may consume a lot of processing power and memory when processing diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c -index 1bad6226f49..5204f85ed1c 100644 +index 1bad6226f49..b344e9c17cf 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c -@@ -38,11 +38,12 @@ __FBSDID("$FreeBSD$"); +@@ -38,11 +38,18 @@ __FBSDID("$FreeBSD$"); #include "diff.h" #include "xmalloc.h" -int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag; -+int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag, colorflag = 0; ++#define COLORFLAG_UNSET -1 ++#define COLORFLAG_NEVER 0 ++#define COLORFLAG_AUTO 0 ++#define COLORFLAG_ALWAYS 0 ++ ++int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag, color = 0; int diff_format, diff_context, status, ignore_file_case, suppress_common; int tabsize = 8, width = 130; ++static int colorflag = COLORFLAG_UNSET; char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; char *group_format = NULL; +const char *add_code, *del_code; struct stat stb1, stb2; struct excludes *excludes_list; regex_t ignore_re; -@@ -57,6 +58,7 @@ enum { +@@ -57,6 +64,7 @@ enum { OPT_HORIZON_LINES, OPT_CHANGED_GROUP_FORMAT, OPT_SUPPRESS_COMMON, @@ -106,7 +112,7 @@ index 1bad6226f49..5204f85ed1c 100644 }; static struct option longopts[] = { -@@ -97,6 +99,7 @@ static struct option longopts[] = { +@@ -97,6 +105,7 @@ static struct option longopts[] = { { "tabsize", required_argument, NULL, OPT_TSIZE }, { "changed-group-format", required_argument, NULL, OPT_CHANGED_GROUP_FORMAT}, { "suppress-common-lines", no_argument, NULL, OPT_SUPPRESS_COMMON }, @@ -114,41 +120,74 @@ index 1bad6226f49..5204f85ed1c 100644 { NULL, 0, 0, '\0'} }; -@@ -106,6 +109,7 @@ void push_excludes(char *); +@@ -106,6 +115,8 @@ void push_excludes(char *); void push_ignore_pats(char *); void read_excludes_file(char *file); void set_argstr(char **, char **); +static const char *init_code(int, const char *); ++static int do_color(void); int main(int argc, char **argv) -@@ -301,6 +305,21 @@ main(int argc, char **argv) +@@ -301,6 +312,17 @@ main(int argc, char **argv) case OPT_SUPPRESS_COMMON: suppress_common = 1; break; + case OPT_COLOR: + if (optarg == NULL || strncmp(optarg, "auto", 4) == 0) -+ colorflag = isatty(STDOUT_FILENO) ? 1 : 0; ++ colorflag = COLORFLAG_AUTO; + else if (strncmp(optarg, "always", 6) == 0) -+ colorflag = 1; ++ colorflag = COLORFLAG_ALWAYS; + else if (strncmp(optarg, "never", 5) == 0) -+ colorflag = 0; ++ colorflag = COLORFLAG_NEVER; + else + errx(2, "unsupported --color value '%s' (must be always, auto, or never)", + optarg); -+ if (colorflag) { -+ add_code = init_code(1, "32"); -+ del_code = init_code(2, "31"); -+ } + break; default: usage(); break; -@@ -550,3 +569,21 @@ conflicting_format(void) +@@ -316,6 +338,12 @@ main(int argc, char **argv) + argc -= optind; + argv += optind; + ++ if (do_color()) { ++ color = 1; ++ add_code = init_code(1, "32"); ++ del_code = init_code(2, "31"); ++ } ++ + #ifdef __OpenBSD__ + if (pledge("stdio rpath tmppath", NULL) == -1) + err(2, "pledge"); +@@ -550,3 +578,44 @@ conflicting_format(void) fprintf(stderr, "error: conflicting output format options.\n"); usage(); } + ++static int ++do_color(void) ++{ ++ const char *p, *p2; ++ int ret = 0; ++ ++ p = getenv("CLICOLOR"); ++ p2 = getenv("COLORTERM"); ++ ++ if ((p != NULL && *p != '\0') || (p2 != NULL && *p2 != '\0')) ++ ret = isatty(STDOUT_FILENO) ? 1 : 0; ++ ++ if (colorflag == COLORFLAG_AUTO) ++ ret = isatty(STDOUT_FILENO) ? 1 : 0; ++ else if (colorflag == COLORFLAG_ALWAYS) ++ ret = 1; ++ else if (colorflag == COLORFLAG_NEVER) ++ ret = 0; ++ ++ printf("do_color(): %i\n", ret); ++ return ret; ++} ++ +static const char * +init_code(int i, const char *rv) +{ @@ -167,7 +206,7 @@ index 1bad6226f49..5204f85ed1c 100644 + return rv; +} diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h -index b5536bd7bf7..0c325d004b8 100644 +index b5536bd7bf7..5eccce51210 100644 --- a/usr.bin/diff/diff.h +++ b/usr.bin/diff/diff.h @@ -90,12 +90,13 @@ struct excludes { @@ -175,7 +214,7 @@ index b5536bd7bf7..0c325d004b8 100644 }; -extern int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag; -+extern int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag, colorflag; ++extern int lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag, color; extern int diff_format, diff_context, status, ignore_file_case; extern int suppress_common; extern int tabsize, width; @@ -186,16 +225,16 @@ index b5536bd7bf7..0c325d004b8 100644 extern struct excludes *excludes_list; extern regex_t ignore_re; diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c -index 1b28281024c..6b01af67a82 100644 +index 651ec88df90..5f1b0d5ad94 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1183,13 +1183,23 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d, } } if (diff_format == D_SIDEBYSIDE) { -+ if (colorflag && (a>b)) ++ if (color && (a>b)) + printf("\033[%sm", add_code); -+ else if (colorflag && (c>d)) ++ else if (color && (c>d)) + printf("\033[%sm", del_code); if (a > b) { print_space(0, hw + padding , *pflags); @@ -203,12 +242,12 @@ index 1b28281024c..6b01af67a82 100644 nc = fetch(ixold, a, b, f1, '\0', 1, *pflags); print_space(nc, hw - nc + padding, *pflags); } -+ if (colorflag && (a>b)) ++ if (color && (a>b)) + printf("\033[%sm", add_code); -+ else if (colorflag && (c>d)) ++ else if (color && (c>d)) + printf("\033[%sm", del_code); printf("%c", (a>b)? '>' : ((c>d)? '<' : '|')); -+ if (colorflag && (c>d)) ++ if (color && (c>d)) + printf("\33[m"); print_space(hw + padding + 1 , padding, *pflags); fetch(ixnew, c, d, f2, '\0', 0, *pflags); @@ -217,9 +256,9 @@ index 1b28281024c..6b01af67a82 100644 nc = hw; if ((diff_format != D_IFDEF && diff_format != D_GFORMAT) && ch != '\0') { -+ if (colorflag && (ch == '>' || ch == '+')) ++ if (color && (ch == '>' || ch == '+')) + printf("\033[%sm", add_code); -+ else if (colorflag && (ch == '<' || ch == '-')) ++ else if (color && (ch == '<' || ch == '-')) + printf("\033[%sm", del_code); printf("%c", ch); if (Tflag && (diff_format == D_NORMAL || @@ -228,11 +267,11 @@ index 1b28281024c..6b01af67a82 100644 } } } -+ if (colorflag) ++ if (color) + printf("\33[m"); return col; } -- -2.31.1 +2.32.0 -- cgit v1.2.3-56-ge451