From: Steven Schubiger Date: Tue, 25 Apr 2017 19:53:24 +0000 (+0200) Subject: Merge branch 'opt_attr' X-Git-Tag: v0.61~1 X-Git-Url: http://git.refcnt.org/?p=colorize.git;a=commitdiff_plain;h=f1c44195b0950ef15dc9108025dbc2251b274225;hp=78eed47b63832cf88d2c1a9700cec16298901a44 Merge branch 'opt_attr' --- diff --git a/colorize.1 b/colorize.1 index 1862930..de53070 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-21" "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 +Attributes: 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 diff --git a/colorize.c b/colorize.c index 3c96a29..7683e1c 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,9 @@ process_opts (int argc, char **argv) case 0: /* long opts */ switch (opt_type) { + case OPT_ATTR: + process_opt_attr (optarg); + break; case OPT_CLEAN: clean = true; break; @@ -406,6 +417,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 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)) + 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 +601,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 +638,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 +756,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 +794,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 +820,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 +851,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 +865,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 +1072,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 +1083,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 +1167,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 +1190,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 +1215,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 +1241,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 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: ""
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'                          ],
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;
 };