From 38114a984055e8491772cfb7824be517ff2ec83f Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 16 Apr 2017 16:52:04 +0200 Subject: First draft of attr option --- colorize.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 84 insertions(+), 27 deletions(-) diff --git a/colorize.c b/colorize.c index 3c96a29..d128228 100644 --- a/colorize.c +++ b/colorize.c @@ -45,6 +45,7 @@ #define to_str(arg) str(arg) #define streq(s1, s2) (strcmp (s1, s2) == 0) +#define strneq(s1, s2, n) (strncmp (s1, s2, n) == 0) #if !DEBUG # define xmalloc(size) malloc_wrap(size) @@ -117,6 +118,8 @@ #define DEBUG_FILE "debug.txt" +#define MAX_ATTRIBUTE_CHARS (5 * 2) + #define VERSION "0.60" typedef enum { false, true } bool; @@ -194,7 +197,8 @@ static const struct { }; enum { - OPT_CLEAN = 1, + OPT_ATTR = 1, + OPT_CLEAN, OPT_CLEAN_ALL, OPT_EXCLUDE_RANDOM, OPT_HELP, @@ -202,6 +206,7 @@ enum { }; static int opt_type; static const struct option long_opts[] = { + { "attr", required_argument, &opt_type, OPT_ATTR }, { "clean", no_argument, &opt_type, OPT_CLEAN }, { "clean-all", no_argument, &opt_type, OPT_CLEAN_ALL }, { "exclude-random", required_argument, &opt_type, OPT_EXCLUDE_RANDOM }, @@ -221,28 +226,31 @@ static void **vars_list; static bool clean; static bool clean_all; +static char attr[MAX_ATTRIBUTE_CHARS + 1]; static char *exclude; static const char *program_name; static void process_opts (int, char **); +static void process_opt_attr (const char *); +static void write_attr (unsigned int); static void print_hint (void); static void print_help (void); static void print_version (void); static void cleanup (void); static void free_color_names (struct color_name **); -static void process_args (unsigned int, char **, bool *, const struct color **, const char **, FILE **); +static void process_args (unsigned int, char **, char *, const struct color **, const char **, FILE **); static void process_file_arg (const char *, const char **, FILE **); static void skip_path_colors (const char *, const char *, const struct stat *); -static void gather_color_names (const char *, bool *, struct color_name **); -static void read_print_stream (bool, const struct color **, const char *, FILE *); +static void gather_color_names (const char *, char *, struct color_name **); +static void read_print_stream (const char *, const struct color **, const char *, FILE *); static void merge_print_line (const char *, const char *, FILE *); static void complete_part_line (const char *, char **, FILE *); static bool get_next_char (char *, const char **, FILE *, bool *); static void save_char (char, char **, size_t *, size_t *); static void find_color_entries (struct color_name **, const struct color **); static void find_color_entry (const struct color_name *, unsigned int, const struct color **); -static void print_line (bool, const struct color **, const char * const, unsigned int); +static void print_line (const char *, const struct color **, const char * const, unsigned int); static void print_clean (const char *); static bool is_esc (const char *); static const char *get_end_of_esc (const char *); @@ -250,9 +258,9 @@ static const char *get_end_of_text (const char *); static void print_text (const char *, size_t); static bool gather_esc_offsets (const char *, const char **, const char **); static bool validate_esc_clean_all (const char **); -static bool validate_esc_clean (int, unsigned int, const char **, bool *); +static bool validate_esc_clean (int, unsigned int, unsigned int *, const char **, bool *); static bool is_reset (int, unsigned int, const char **); -static bool is_bold (int, unsigned int, const char **); +static bool is_attr (int, unsigned int, unsigned int, const char **); static bool is_fg_color (int, const char **); static bool is_bg_color (int, unsigned int, const char **); #if !DEBUG @@ -283,8 +291,6 @@ main (int argc, char **argv) { unsigned int arg_cnt; - bool bold = false; - const struct color *colors[2] = { NULL, /* foreground */ NULL, /* background */ @@ -301,6 +307,8 @@ main (int argc, char **argv) log = open_file (DEBUG_FILE, "w"); #endif + attr[0] = '\0'; + process_opts (argc, argv); arg_cnt = argc - optind; @@ -332,8 +340,8 @@ main (int argc, char **argv) if (clean || clean_all) process_file_arg (argv[optind], &file, &stream); else - process_args (arg_cnt, &argv[optind], &bold, colors, &file, &stream); - read_print_stream (bold, colors, file, stream); + process_args (arg_cnt, &argv[optind], &attr[0], colors, &file, &stream); + read_print_stream (&attr[0], colors, file, stream); RELEASE_VAR (exclude); @@ -361,6 +369,13 @@ process_opts (int argc, char **argv) case 0: /* long opts */ switch (opt_type) { + case OPT_ATTR: { + char *opt; + opt = xstrdup (optarg); + process_opt_attr (opt); + free (opt); + break; + } case OPT_CLEAN: clean = true; break; @@ -406,6 +421,46 @@ process_opts (int argc, char **argv) } } +static void +process_opt_attr (const char *p) +{ + while (*p) + { + const char *s; + if (!isalnum (*p)) + vfprintf_fail (formats[FMT_GENERIC], "--attr switch must be provided a string"); + s = p; + while (isalnum (*p)) + p++; + if (*p != '\0' && *p != ',') + vfprintf_fail (formats[FMT_GENERIC], "--attr switch must have strings separated by ,"); + else + { + /* If atttributes are added to this "list", also increase MAX_ATTRIBUTE_CHARS! */ + if (p - s == 4 && strneq (s, "bold", 4)) + write_attr (1); + else if (p - s == 10 && strneq (s, "underscore", 10)) + write_attr (4); + else if (p - s == 5 && strneq (s, "blink", 5)) + write_attr (5); + else if (p - s == 7 && strneq (s, "reverse", 7)) + write_attr (7); + else if (p - s == 9 && strneq (s, "concealed", 9)) + write_attr (8); + else + vfprintf_fail (formats[FMT_GENERIC], "--attr switch must be provided valid attribute names"); + } + if (*p) + p++; + } +} + +static void +write_attr (unsigned int val) +{ + snprintf (attr + strlen (attr), 3, "%u;", val); +} + static void print_hint (void) { @@ -550,7 +605,7 @@ free_color_names (struct color_name **color_names) } static void -process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct color **colors, const char **file, FILE **stream) +process_args (unsigned int arg_cnt, char **arg_strings, char *attr, const struct color **colors, const char **file, FILE **stream) { int ret; char *p; @@ -587,7 +642,7 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct vfprintf_fail (formats[FMT_STRING], "one color pair allowed only for string", color_string); } - gather_color_names (color_string, bold, color_names); + gather_color_names (color_string, attr, color_names); assert (color_names[FOREGROUND]); @@ -705,7 +760,7 @@ skip_path_colors (const char *color_string, const char *file_string, const struc } static void -gather_color_names (const char *color_string, bool *bold, struct color_name **color_names) +gather_color_names (const char *color_string, char *attr, struct color_name **color_names) { unsigned int index; char *color, *p, *str; @@ -743,7 +798,7 @@ gather_color_names (const char *color_string, bool *bold, struct color_name **co switch (index) { case FOREGROUND: - *bold = true; + snprintf (attr + strlen (attr), 3, "1;"); break; case BACKGROUND: vfprintf_fail (formats[FMT_COLOR], tables[BACKGROUND].desc, color, "cannot be bold"); @@ -769,7 +824,7 @@ gather_color_names (const char *color_string, bool *bold, struct color_name **co } static void -read_print_stream (bool bold, const struct color **colors, const char *file, FILE *stream) +read_print_stream (const char *attr, const struct color **colors, const char *file, FILE *stream) { char buf[BUF_SIZE + 1]; unsigned int flags = 0; @@ -800,13 +855,13 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL vfprintf_fail (formats[FMT_FILE], file, "unrecognized line ending"); p = eol + SKIP_LINE_ENDINGS (flags); *eol = '\0'; - print_line (bold, colors, line, flags); + print_line (attr, colors, line, flags); line = p; } if (feof (stream)) { if (*line != '\0') - print_line (bold, colors, line, 0); + print_line (attr, colors, line, 0); } else if (*line != '\0') { @@ -814,7 +869,7 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL if ((clean || clean_all) && (p = strrchr (line, '\033'))) merge_print_line (line, p, stream); else - print_line (bold, colors, line, 0); + print_line (attr, colors, line, 0); } } } @@ -1021,7 +1076,7 @@ find_color_entry (const struct color_name *color_name, unsigned int index, const } static void -print_line (bool bold, const struct color **colors, const char *const line, unsigned int flags) +print_line (const char *attr, const struct color **colors, const char *const line, unsigned int flags) { /* --clean[-all] */ if (clean || clean_all) @@ -1032,7 +1087,7 @@ print_line (bool bold, const struct color **colors, const char *const line, unsi if (colors[BACKGROUND] && colors[BACKGROUND]->code) printf ("\033[%s", colors[BACKGROUND]->code); if (colors[FOREGROUND]->code) - printf ("\033[%s%s%s\033[0m", bold ? "1;" : "", colors[FOREGROUND]->code, line); + printf ("\033[%s%s%s\033[0m", attr, colors[FOREGROUND]->code, line); else printf (formats[FMT_GENERIC], line); } @@ -1116,8 +1171,9 @@ gather_esc_offsets (const char *p, const char **start, const char **end) else if (clean) { bool check_values; - unsigned int iter = 0; + unsigned int prev_iter, iter; const char *digit; + prev_iter = iter = 0; do { check_values = false; iter++; @@ -1138,7 +1194,7 @@ gather_esc_offsets (const char *p, const char **start, const char **end) val[i] = *digit++; val[i] = '\0'; value = atoi (val); - valid = validate_esc_clean (value, iter, &p, &check_values); + valid = validate_esc_clean (value, iter, &prev_iter, &p, &check_values); } } while (check_values); } @@ -1163,14 +1219,15 @@ validate_esc_clean_all (const char **p) } static bool -validate_esc_clean (int value, unsigned int iter, const char **p, bool *check_values) +validate_esc_clean (int value, unsigned int iter, unsigned int *prev_iter, const char **p, bool *check_values) { if (is_reset (value, iter, p)) return true; - else if (is_bold (value, iter, p)) + else if (is_attr (value, iter, *prev_iter, p)) { (*p)++; *check_values = true; + *prev_iter = iter; return false; /* partial escape sequence, need another valid value */ } else if (is_fg_color (value, p)) @@ -1188,9 +1245,9 @@ is_reset (int value, unsigned int iter, const char **p) } static bool -is_bold (int value, unsigned int iter, const char **p) +is_attr (int value, unsigned int iter, unsigned int prev_iter, const char **p) { - return (value == 1 && iter == 1 && **p == ';'); + return ((value > 0 && value < 10) && (iter - prev_iter == 1) && **p == ';'); } static bool -- cgit v1.2.3 From eaa41b9e9f0bc54518efb56afb3bac671dbe2aba Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 16 Apr 2017 17:06:04 +0200 Subject: Tweak comment --- colorize.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colorize.c b/colorize.c index d128228..0cfbaad 100644 --- a/colorize.c +++ b/colorize.c @@ -436,7 +436,7 @@ process_opt_attr (const char *p) vfprintf_fail (formats[FMT_GENERIC], "--attr switch must have strings separated by ,"); else { - /* If atttributes are added to this "list", also increase MAX_ATTRIBUTE_CHARS! */ + /* If attributes are added to this "list", also increase MAX_ATTRIBUTE_CHARS! */ if (p - s == 4 && strneq (s, "bold", 4)) write_attr (1); else if (p - s == 10 && strneq (s, "underscore", 10)) -- cgit v1.2.3 From 4848afa27f5788eba267c7e8508efdcb1fec41b3 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 16 Apr 2017 22:29:22 +0200 Subject: Don't xstrdup() optarg --- colorize.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/colorize.c b/colorize.c index 0cfbaad..7683e1c 100644 --- a/colorize.c +++ b/colorize.c @@ -369,13 +369,9 @@ process_opts (int argc, char **argv) case 0: /* long opts */ switch (opt_type) { - case OPT_ATTR: { - char *opt; - opt = xstrdup (optarg); - process_opt_attr (opt); - free (opt); + case OPT_ATTR: + process_opt_attr (optarg); break; - } case OPT_CLEAN: clean = true; break; -- cgit v1.2.3 From 061db3b04401177fa9348785f71c7c5cc4946142 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Mon, 17 Apr 2017 17:45:48 +0200 Subject: Test --attr failures --- t/fail.t | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/t/fail.t b/t/fail.t index b152094..0348201 100755 --- a/t/fail.t +++ b/t/fail.t @@ -12,7 +12,7 @@ use IPC::Open3 qw(open3); use Symbol qw(gensym); use Test::More; -my $tests = 20; +my $tests = 23; my $run_program_fail = sub { @@ -40,6 +40,9 @@ SKIP: { my $dir = tempdir(CLEANUP => true); my @set = ( + [ '--attr=:', 'must be provided a string' ], + [ '--attr=bold:underscore', 'must have strings separated by ,' ], + [ '--attr=b0ld', 'must be provided valid attribute names' ], [ '--exclude-random=random', 'must be provided a plain color' ], [ '--clean --clean-all', 'mutually exclusive' ], [ '--clean file1 file2', 'more than one file' ], -- cgit v1.2.3 From 7c04baad866cced3a3171af6bbe9f3ce5696db6f Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Wed, 19 Apr 2017 14:45:35 +0200 Subject: Document --attr --- colorize.1 | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/colorize.1 b/colorize.1 index 1862930..d6c8f05 100644 --- a/colorize.1 +++ b/colorize.1 @@ -1,10 +1,10 @@ -.TH COLORIZE 1 "2015-03-02" "colorize v0.60" "User Commands" +.TH COLORIZE 1 "2017-04-19" "colorize v0.60" "User Commands" .SH NAME colorize \- colorize text with escape sequences .SH SYNOPSIS -\fBcolorize\fR [\fIoption\fR] (\fIforeground\fR) [\fIfile\fR] +\fBcolorize\fR [\fIoption\fR]... (\fIforeground\fR) [\fIfile\fR] .PP -\fBcolorize\fR [\fIoption\fR] (\fIforeground\fR)/(\fIbackground\fR) [\fIfile\fR] +\fBcolorize\fR [\fIoption\fR]... (\fIforeground\fR)/(\fIbackground\fR) [\fIfile\fR] .PP \fBcolorize\fR \-\-clean[\-all] [\fIfile\fR] .PP @@ -28,6 +28,12 @@ were emitted by colorize (see NOTES for list), whereas \-\-clean\-all omits all valid ones. If in doubt, consider using \-\-clean\-all. .SH OPTIONS .TP +.BR \-\-attr=\fIATTR1,ATTR2,...\fR +set attributes by name +.RS +Currently: bold, underscore, blink, reverse and concealed. +.RE +.TP .BR \-\-clean clean text from color escape sequences emitted by colorize .TP @@ -48,7 +54,7 @@ as follows: .IP * 4 30-37,39 (foreground colors) .IP * 4 -1;30-37,39 (bold foreground colors) +1-9;30-37,39 (foreground colors with attributes) .IP * 4 40-47,49 (background colors) .IP * 4 -- cgit v1.2.3 From 7642c25f95efc7dc7705592d302c5210a3e83394 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Wed, 19 Apr 2017 18:00:36 +0200 Subject: Add tests for --attr --- test.pl | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test.pl b/test.pl index a44395f..ddd6288 100755 --- a/test.pl +++ b/test.pl @@ -12,7 +12,7 @@ use Getopt::Long qw(:config no_auto_abbrev no_ignore_case); use Test::Harness qw(runtests); use Test::More; -my $tests = 24; +my $tests = 28; my $valgrind_cmd = ''; { @@ -82,6 +82,19 @@ SKIP: { is(qx(printf %s "hello world" | $program Magenta | $valgrind_cmd$program $switch), 'hello world', "$type colored line"); is_deeply([split /\n/, qx($program cyan $infile1 | $valgrind_cmd$program $switch)], [split /\n/, $text], "$type colored text"); + { + my @attrs = qw(bold underscore blink reverse concealed); + + my $ok = true; + foreach my $attr (@attrs) { + $ok &= qx(printf %s "$attr" | $program green --attr=$attr | $valgrind_cmd$program $switch) eq $attr; + } + ok($ok, "$type attribute"); + + my $attrs = join ',', @attrs; + is(qx(printf %s "$attrs" | $program green --attr=$attrs | $valgrind_cmd$program $switch), $attrs, "$type attributes"); + } + ok(qx(printf %s "\e[\e[33m" | $valgrind_cmd$program $switch) eq "\e[", "$type with invalid sequence"); }; @@ -145,6 +158,14 @@ EOT system(qq(printf '%s\n' "$bold_color" | $program $bold_color)); } + print <<'EOT'; +Attributes +========== +EOT + foreach my $attr (qw(bold underscore blink reverse concealed)) { + system(qq(printf '%s\n' "$attr" | $program green --attr=$attr)); + } + unlink $program; }; -- cgit v1.2.3 From e04549722f3d1bdf44e84bb0db437b3e82abcef0 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Fri, 21 Apr 2017 19:34:48 +0200 Subject: Improve man page wording --- colorize.1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colorize.1 b/colorize.1 index d6c8f05..a4cbba6 100644 --- a/colorize.1 +++ b/colorize.1 @@ -31,7 +31,7 @@ omits all valid ones. If in doubt, consider using \-\-clean\-all. .BR \-\-attr=\fIATTR1,ATTR2,...\fR set attributes by name .RS -Currently: bold, underscore, blink, reverse and concealed. +Attributes: bold, underscore, blink, reverse and concealed. .RE .TP .BR \-\-clean -- cgit v1.2.3 From 847a1b10725ce024bba24725355f89c3d005a55e Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Fri, 21 Apr 2017 20:00:22 +0200 Subject: Amend man page date --- colorize.1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colorize.1 b/colorize.1 index a4cbba6..de53070 100644 --- a/colorize.1 +++ b/colorize.1 @@ -1,4 +1,4 @@ -.TH COLORIZE 1 "2017-04-19" "colorize v0.60" "User Commands" +.TH COLORIZE 1 "2017-04-21" "colorize v0.60" "User Commands" .SH NAME colorize \- colorize text with escape sequences .SH SYNOPSIS -- cgit v1.2.3 From b87c52d154d2953b932a54866a74fc9e10e6ae80 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sat, 22 Apr 2017 23:16:51 +0200 Subject: Update plain documentation --- doc/colorize.html | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/colorize.html b/doc/colorize.html index 8084cc7..1f489af 100644 --- a/doc/colorize.html +++ b/doc/colorize.html @@ -28,6 +28,7 @@ Usage: ./colorize (foreground) OR (foreground)/(background) OR --clean[-all] [-| whereas for lower case colors will be of normal intensity. Options + --attr --clean --clean-all --exclude-random @@ -85,12 +86,22 @@ permitted by applicable law. permitted by applicable law.
+[sts@apollo ~/colorize]$ ./colorize --attr=bold magenta /etc/motd
+
+The programs included with the Debian GNU/Linux system are free software;
+the exact distribution terms for each program are described in the
+individual files in /usr/share/doc/*/copyright.
+
+Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
+permitted by applicable law.
+
+
 [sts@apollo ~/colorize]$ ./colorize green /etc/motd | head -n2 | tail -n1
 The programs included with the Debian GNU/Linux system are free software;
 
 [sts@apollo ~/colorize]$ ./colorize --version
-colorize v0.59-16-gf340b58 (compiled at Mar 18 2017, 00:08:31)
+colorize v0.60-9-g847a1b1 (compiled at Apr 22 2017, 14:32:01)
 Compiler flags: "-ansi -pedantic "
 Linker flags: ""
 Preprocessor flags: ""
-- 
cgit v1.2.3