X-Git-Url: http://git.refcnt.org/?a=blobdiff_plain;f=colorize.c;h=58625fb56db157019419ed04a50357f2b1c7e10e;hb=4655e8c06df10fefccf97e48663d906b3af044d6;hp=0cf44cb310efebb3d17e5c8756b272622f427ade;hpb=5b88a2f7a9adb6ea181f897fb64485b56be08e80;p=colorize.git diff --git a/colorize.c b/colorize.c index 0cf44cb..58625fb 100644 --- a/colorize.c +++ b/colorize.c @@ -99,12 +99,12 @@ #define ABORT_TRACE() \ fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \ - abort (); \ + abort (); #define CHECK_COLORS_RANDOM(color1, color2) \ streq (color_names[color1]->name, "random") \ && (streq (color_names[color2]->name, "none") \ - || streq (color_names[color2]->name, "default")) \ + || streq (color_names[color2]->name, "default")) #define ALLOC_COMPLETE_PART_LINE 8 @@ -122,7 +122,9 @@ #define MAX_ATTRIBUTE_CHARS (6 * 2) -#define VERSION "0.62" +#define PROGRAM_NAME "colorize" + +#define VERSION "0.63" typedef enum { false, true } bool; @@ -203,18 +205,20 @@ enum { OPT_CLEAN, OPT_CLEAN_ALL, OPT_EXCLUDE_RANDOM, + OPT_OMIT_COLOR_EMPTY, OPT_HELP, OPT_VERSION }; 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 }, - { "help", no_argument, &opt_type, OPT_HELP }, - { "version", no_argument, &opt_type, OPT_VERSION }, - { NULL, 0, NULL, 0 }, + { "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 }, + { "omit-color-empty", no_argument, &opt_type, OPT_OMIT_COLOR_EMPTY }, + { "help", no_argument, &opt_type, OPT_HELP }, + { "version", no_argument, &opt_type, OPT_VERSION }, + { NULL, 0, NULL, 0 }, }; enum attr_type { @@ -240,6 +244,7 @@ static void **vars_list; static bool clean; static bool clean_all; +static bool omit_color_empty; static char attr[MAX_ATTRIBUTE_CHARS + 1]; static char *exclude; @@ -265,7 +270,7 @@ 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 (const char *, const struct color **, const char * const, unsigned int); +static void print_line (const char *, const struct color **, const char * const, unsigned int, bool); static void print_clean (const char *); static bool is_esc (const char *); static const char *get_end_of_esc (const char *); @@ -334,8 +339,8 @@ main (int argc, char **argv) vfprintf_fail (formats[FMT_GENERIC], "--clean and --clean-all switch are mutually exclusive"); if (arg_cnt > 1) { - const char *format = "%s %s"; - const char *message = "switch cannot be used with more than one file"; + const char *const format = "%s %s"; + const char *const message = "switch cannot be used with more than one file"; if (clean) vfprintf_fail (format, "--clean", message); else if (clean_all) @@ -365,11 +370,11 @@ main (int argc, char **argv) #define PRINT_HELP_EXIT() \ print_help (); \ - exit (EXIT_SUCCESS); \ + exit (EXIT_SUCCESS); #define PRINT_VERSION_EXIT() \ print_version (); \ - exit (EXIT_SUCCESS); \ + exit (EXIT_SUCCESS); extern char *optarg; @@ -411,6 +416,9 @@ process_opts (int argc, char **argv) vfprintf_fail (formats[FMT_GENERIC], "--exclude-random switch must be provided a plain color"); break; } + case OPT_OMIT_COLOR_EMPTY: + omit_color_empty = true; + break; case OPT_HELP: PRINT_HELP_EXIT (); case OPT_VERSION: @@ -568,7 +576,7 @@ print_version (void) #ifdef HAVE_VERSION # include "version.h" #else - const char *version = NULL; + const char *const version = NULL; #endif const char *version_prefix, *version_string; const char *c_flags, *ld_flags, *cpp_flags; @@ -597,7 +605,7 @@ print_version (void) #endif version_prefix = version ? "" : "v"; version_string = version ? version : VERSION; - printf ("colorize %s%s (compiled at %s, %s)\n", version_prefix, version_string, __DATE__, __TIME__); + printf ("%s %s%s (compiled at %s, %s)\n", PROGRAM_NAME, version_prefix, version_string, __DATE__, __TIME__); printf ("Compiler flags: %s\n", c_flags); printf ("Linker flags: %s\n", ld_flags); @@ -653,7 +661,11 @@ process_args (unsigned int arg_cnt, char **arg_strings, char *attr, const struct int ret; char *p; struct stat sb; - struct color_name *color_names[3] = { NULL, NULL, NULL }; + struct color_name *color_names[3] = { + NULL, /* foreground */ + NULL, /* background */ + NULL, /* sentinel value */ + }; const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL; const char *file_string = arg_cnt == 2 ? arg_strings[1] : NULL; @@ -886,6 +898,7 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi line = buf; while ((eol = strpbrk (line, "\n\r"))) { + const bool has_text = (eol > line); const char *p; flags &= ~(CR|LF); if (*eol == '\r') @@ -896,17 +909,18 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi } else if (*eol == '\n') flags |= LF; - else + else /* never reached */ vfprintf_fail (formats[FMT_FILE], file, "unrecognized line ending"); p = eol + SKIP_LINE_ENDINGS (flags); *eol = '\0'; - print_line (attr, colors, line, flags); + print_line (attr, colors, line, flags, + omit_color_empty ? has_text : true); line = p; } if (feof (stream)) { if (*line != '\0') - print_line (attr, colors, line, 0); + print_line (attr, colors, line, 0, true); } else if (*line != '\0') { @@ -914,7 +928,7 @@ read_print_stream (const char *attr, const struct color **colors, const char *fi if ((clean || clean_all) && (p = strrchr (line, '\033'))) merge_print_line (line, p, stream); else - print_line (attr, colors, line, 0); + print_line (attr, colors, line, 0, true); } } } @@ -1121,12 +1135,13 @@ find_color_entry (const struct color_name *color_name, unsigned int index, const } static void -print_line (const char *attr, 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, bool emit_colors) { /* --clean[-all] */ if (clean || clean_all) print_clean (line); - else + /* skip for --omit-color-empty? */ + else if (emit_colors) { /* Foreground color code is guaranteed to be set when background color code is present. */ if (colors[BACKGROUND] && colors[BACKGROUND]->code) @@ -1456,8 +1471,8 @@ has_color_name (const char *str, const char *name) else if (*(name + 1) != '\0' && !((p = strstr (str + 1, name + 1)) && p == str + 1)) return false; - - return true; + else + return true; } static FILE * @@ -1479,7 +1494,7 @@ open_file (const char *file, const char *mode) va_start (ap, fmt); \ vfprintf (stderr, fmt, ap); \ va_end (ap); \ - fprintf (stderr, "\n"); \ + fprintf (stderr, "\n"); static void vfprintf_diag (const char *fmt, ...)