X-Git-Url: http://git.refcnt.org/?a=blobdiff_plain;f=colorize.c;h=05fd84639763a84768f1a5b8bc999c623fd889a7;hb=2fd4dd491d33792fe07cbd277d92331c8936eab2;hp=eb2be2f5080eb9acfa3a58f52fd977d29efe671f;hpb=189af18c6bd1f1f01d410fff4bc9d3996136b373;p=colorize.git diff --git a/colorize.c b/colorize.c index eb2be2f..05fd846 100644 --- a/colorize.c +++ b/colorize.c @@ -116,7 +116,7 @@ #define DEBUG_FILE "debug.txt" -#define VERSION "0.58" +#define VERSION "0.59" typedef enum { false, true } bool; @@ -183,7 +183,7 @@ static const char *formats[] = { "%s: %s: %s", /* type */ }; -enum { FOREGROUND, BACKGROUND }; +enum { GENERIC, FOREGROUND = 0, BACKGROUND }; static const struct { struct color const *entries; @@ -194,6 +194,23 @@ static const struct { { bg_colors, sizeof (bg_colors) / sizeof (struct color), "background" }, }; +enum { + OPT_CLEAN = 1, + OPT_CLEAN_ALL, + OPT_EXCLUDE_RANDOM, + OPT_HELP, + OPT_VERSION +}; +static int opt_type; +static const struct option long_opts[] = { + { "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 }, +}; + static FILE *stream; #if DEBUG static FILE *log; @@ -330,29 +347,11 @@ main (int argc, char **argv) goto PARSE_OPT; \ extern char *optarg; -static int opt_type; static void process_opts (int argc, char **argv) { - enum { - OPT_CLEAN = 1, - OPT_CLEAN_ALL, - OPT_EXCLUDE_RANDOM, - OPT_HELP, - OPT_VERSION - }; - int opt; - struct option long_opts[] = { - { "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 }, - }; - while ((opt = getopt_long (argc, argv, "hV", long_opts, NULL)) != -1) { PARSE_OPT: @@ -372,9 +371,9 @@ process_opts (int argc, char **argv) unsigned int i; exclude = xstrdup (optarg); STACK_VAR (exclude); - for (i = 1; i < tables[FOREGROUND].count - 1; i++) /* skip color none and default */ + for (i = 1; i < tables[GENERIC].count - 1; i++) /* skip color none and default */ { - const struct color *entry = &tables[FOREGROUND].entries[i]; + const struct color *entry = &tables[GENERIC].entries[i]; if (streq (exclude, entry->name)) { valid = true; @@ -417,6 +416,15 @@ print_hint (void) static void print_help (void) { + struct short_opt { + const char *name; + const char *short_opt; + }; + const struct short_opt short_opts[] = { + { "help", "h" }, + { "version", "V" }, + }; + const struct option *opt = long_opts; unsigned int i; printf ("Usage: %s (foreground) OR (foreground)%c(background) OR --clean[-all] [-|file]\n\n", program_name, COLOR_SEP_CHAR); @@ -438,11 +446,24 @@ print_help (void) printf ("\twhereas for lower case colors will be of normal intensity.\n"); printf ("\n\tOptions\n"); - printf ("\t\t --clean\n"); - printf ("\t\t --clean-all\n"); - printf ("\t\t --exclude-random\n"); - printf ("\t\t-h, --help\n"); - printf ("\t\t-V, --version\n\n"); + for (; opt->name; opt++) + { + const char *short_opt = NULL; + unsigned int i; + for (i = 0; i < sizeof (short_opts) / sizeof (struct short_opt); i++) + { + if (streq (opt->name, short_opts[i].name)) + { + short_opt = short_opts[i].short_opt; + break; + } + } + if (short_opt) + printf ("\t\t-%s, --%s\n", short_opt, opt->name); + else + printf ("\t\t --%s\n", opt->name); + } + printf ("\n"); } static void @@ -635,9 +656,9 @@ skip_path_colors (const char *color_string, const char *file_string, const struc { bool matched = false; unsigned int i; - for (i = 0; i < tables[FOREGROUND].count; i++) + for (i = 0; i < tables[GENERIC].count; i++) { - const struct color *entry = &tables[FOREGROUND].entries[i]; + const struct color *entry = &tables[GENERIC].entries[i]; if (has_color_name (color, entry->name)) { color += strlen (entry->name); @@ -745,10 +766,10 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL size_t bytes_read; char *eol; const char *line; - memset (buf, '\0', BUF_SIZE + 1); bytes_read = fread (buf, 1, BUF_SIZE, stream); if (bytes_read != BUF_SIZE && ferror (stream)) vfprintf_fail (formats[FMT_ERROR], BUF_SIZE, "read"); + buf[bytes_read] = '\0'; line = buf; while ((eol = strpbrk (line, "\n\r"))) { @@ -1075,7 +1096,7 @@ gather_esc_offsets (const char *p, const char **start, const char **end) if (*p == 27 && *(p + 1) == '[') { bool valid = false; - const char *begin = p; + const char *const begin = p; p += 2; if (clean_all) valid = validate_esc_clean_all (&p);