comparison src/xxd/xxd.c @ 33099:49f317597430 v9.0.1834

patch 9.0.1834: Some problems with xxd coloring Commit: https://github.com/vim/vim/commit/f6fc255e8d9c46a0e51e03f16a508833d309dee6 Author: K.Takata <kentkt@csc.jp> Date: Fri Sep 1 18:41:04 2023 +0200 patch 9.0.1834: Some problems with xxd coloring Problem: Some problems with xxd coloring Solution: Fix the following problems: * Support colored output on Windows. SetConsoleMode() is required to enable ANSI color sequences. * Support "NO_COLOR" environment variable. If "NO_COLOR" is defined and not empty, colored output should be disabled. See https://no-color.org/ * "-R" should only accept "always", "never" or "auto" as the parameter. * Adjust help and documentation. "-R" cannot omit the parameter. Remove surrounding brackets. Related #12131 closes: #12997 closes: #12991 closes: #12986 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: K.Takata <kentkt@csc.jp>
author Christian Brabandt <cb@256bit.org>
date Fri, 01 Sep 2023 18:45:06 +0200
parents e1a219f47e3a
children e8805650fec1
comparison
equal deleted inserted replaced
33098:d3d82d3f6006 33099:49f317597430
85 #else 85 #else
86 # include <fcntl.h> 86 # include <fcntl.h>
87 #endif 87 #endif
88 #if defined(WIN32) || defined(CYGWIN) 88 #if defined(WIN32) || defined(CYGWIN)
89 # include <io.h> /* for setmode() */ 89 # include <io.h> /* for setmode() */
90 #else 90 # include <windows.h>
91 # ifdef UNIX 91 #endif
92 # include <unistd.h> 92 #ifdef UNIX
93 # endif 93 # include <unistd.h>
94 #endif 94 #endif
95 #include <stdlib.h> 95 #include <stdlib.h>
96 #include <string.h> /* for strncmp() */ 96 #include <string.h> /* for strncmp() */
97 #include <ctype.h> /* for isalnum() */ 97 #include <ctype.h> /* for isalnum() */
98 #include <limits.h> 98 #include <limits.h>
133 133
134 extern void perror __P((char *)); 134 extern void perror __P((char *));
135 # endif 135 # endif
136 #endif 136 #endif
137 137
138 char version[] = "xxd 2023-08-31 by Juergen Weigert et al."; 138 char version[] = "xxd 2023-09-01 by Juergen Weigert et al.";
139 #ifdef WIN32 139 #ifdef WIN32
140 char osver[] = " (Win32)"; 140 char osver[] = " (Win32)";
141 #else 141 #else
142 char osver[] = ""; 142 char osver[] = "";
143 #endif 143 #endif
254 "[+][-]", "(or +: rel.) "); 254 "[+][-]", "(or +: rel.) ");
255 #else 255 #else
256 "", ""); 256 "", "");
257 #endif 257 #endif
258 fprintf(stderr, " -u use upper case hex letters.\n"); 258 fprintf(stderr, " -u use upper case hex letters.\n");
259 fprintf(stderr, " -R [WHEN] colorize the output; WHEN can be 'always', 'auto' or 'never'. Default: 'auto'.\n"), 259 fprintf(stderr, " -R when colorize the output; <when> can be 'always', 'auto' or 'never'. Default: 'auto'.\n"),
260 fprintf(stderr, " -v show version: \"%s%s\".\n", version, osver); 260 fprintf(stderr, " -v show version: \"%s%s\".\n", version, osver);
261 exit(1); 261 exit(1);
262 } 262 }
263 263
264 static void 264 static void
500 0131,0132,0364,0365,0366,0367,0370,0371, 500 0131,0132,0364,0365,0366,0367,0370,0371,
501 0060,0061,0062,0063,0064,0065,0066,0067, 501 0060,0061,0062,0063,0064,0065,0066,0067,
502 0070,0071,0372,0373,0374,0375,0376,0377 502 0070,0071,0372,0373,0374,0375,0376,0377
503 }; 503 };
504 504
505 static void 505 static void
506 begin_coloring_char (char *l, int *c, int e, int ebcdic) 506 begin_coloring_char (char *l, int *c, int e, int ebcdic)
507 { 507 {
508 if (ebcdic) 508 if (ebcdic)
509 { 509 {
510 if ((e >= 75 && e <= 80) || (e >= 90 && e <= 97) || 510 if ((e >= 75 && e <= 80) || (e >= 90 && e <= 97) ||
543 l[(*c)++] = COLOR_BLUE; 543 l[(*c)++] = COLOR_BLUE;
544 else 544 else
545 l[(*c)++] = COLOR_RED; 545 l[(*c)++] = COLOR_RED;
546 } 546 }
547 l[(*c)++] = 'm'; 547 l[(*c)++] = 'm';
548 }
549
550 static int
551 enable_color(void)
552 {
553 #ifdef WIN32
554 DWORD mode;
555 HANDLE out;
556
557 if (!isatty(1))
558 return 0;
559
560 out = GetStdHandle(STD_OUTPUT_HANDLE);
561 GetConsoleMode(out, &mode);
562 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
563 return (int)SetConsoleMode(out, mode);
564 #elif defined(UNIX)
565 return isatty(STDOUT_FILENO);
566 #else
567 return 0;
568 #endif
548 } 569 }
549 570
550 int 571 int
551 main(int argc, char *argv[]) 572 main(int argc, char *argv[])
552 { 573 {
562 static char l[LLEN+1]; /* static because it may be too big for stack */ 583 static char l[LLEN+1]; /* static because it may be too big for stack */
563 char *pp; 584 char *pp;
564 char *varname = NULL; 585 char *varname = NULL;
565 int addrlen = 9; 586 int addrlen = 9;
566 int color = 0; 587 int color = 0;
567 588 char *no_color;
568 #ifdef UNIX 589
569 color = isatty(STDOUT_FILENO); 590 no_color = getenv("NO_COLOR");
570 #endif 591 if (no_color == NULL || no_color[0] == '\0')
592 color = enable_color();
571 593
572 #ifdef AMIGA 594 #ifdef AMIGA
573 /* This program doesn't work when started from the Workbench */ 595 /* This program doesn't work when started from the Workbench */
574 if (argc == 0) 596 if (argc == 0)
575 exit(1); 597 exit(1);
718 argc--; 740 argc--;
719 } 741 }
720 } 742 }
721 else if (!STRNCMP(pp, "-R", 2)) 743 else if (!STRNCMP(pp, "-R", 2))
722 { 744 {
723 if (!argv[2]) 745 char *pw = pp + 2;
746 if (!pw[0])
747 {
748 pw = argv[2];
749 argv++;
750 argc--;
751 }
752 if (!pw)
724 exit_with_usage(); 753 exit_with_usage();
725 if (!STRNCMP(argv[2], "always", 2)) 754 if (!STRNCMP(pw, "always", 6))
726 color = 1; 755 {
727 else if (!STRNCMP(argv[2], "never", 1)) 756 (void)enable_color();
757 color = 1;
758 }
759 else if (!STRNCMP(pw, "never", 5))
728 color = 0; 760 color = 0;
729 argv++; 761 else if (!STRNCMP(pw, "auto", 4))
730 argc--; 762 ; /* Do nothing. */
763 else
764 exit_with_usage();
731 } 765 }
732 else if (!strcmp(pp, "--")) /* end of options */ 766 else if (!strcmp(pp, "--")) /* end of options */
733 { 767 {
734 argv++; 768 argv++;
735 argc--; 769 argc--;