X-Git-Url: http://git.refcnt.org/?a=blobdiff_plain;f=colorize.c;h=70de8488614cd2c2cbb24a8b8740b5c245f2584c;hb=72deb049525df85b69a7714665fa3d41448af6de;hp=eb2be2f5080eb9acfa3a58f52fd977d29efe671f;hpb=189af18c6bd1f1f01d410fff4bc9d3996136b373;p=colorize.git diff --git a/colorize.c b/colorize.c index eb2be2f..70de848 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; @@ -125,8 +125,6 @@ struct color_name { char *orig; }; -static struct color_name *color_names[3] = { NULL, NULL, NULL }; - struct color { const char *name; const char *code; @@ -183,7 +181,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 +192,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 +345,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 +369,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 +414,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 +444,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 @@ -489,8 +508,6 @@ print_version (void) static void cleanup (void) { - free_color_names (color_names); - if (stream && fileno (stream) != STDIN_FILENO) fclose (stream); #if DEBUG @@ -513,9 +530,9 @@ free_color_names (struct color_name **color_names) unsigned int i; for (i = 0; color_names[i]; i++) { - free (color_names[i]->name); - free (color_names[i]->orig); - free_null (color_names[i]); + RELEASE_VAR (color_names[i]->name); + RELEASE_VAR (color_names[i]->orig); + RELEASE_VAR (color_names[i]); } } @@ -525,6 +542,7 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct int ret; char *p; struct stat sb; + struct color_name *color_names[3] = { NULL, NULL, NULL }; const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL; const char *file_string = arg_cnt == 2 ? arg_strings[1] : NULL; @@ -536,7 +554,7 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct if (file_string) vfprintf_fail (formats[FMT_GENERIC], "hyphen cannot be used as color string"); else - vfprintf_fail (formats[FMT_GENERIC], "hyphen must be preceeded by color string"); + vfprintf_fail (formats[FMT_GENERIC], "hyphen must be preceded by color string"); } ret = lstat (color_string, &sb); @@ -635,9 +653,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); @@ -666,7 +684,7 @@ skip_path_colors (const char *color_string, const char *file_string, const struc else { if (VALID_FILE_TYPE (mode)) - vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "must be preceeded by color string"); + vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "must be preceded by color string"); else vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "is not a valid file type"); } @@ -722,13 +740,16 @@ gather_color_names (const char *color_string, bool *bold, struct color_name **co } color_names[index] = xcalloc (1, sizeof (struct color_name)); + STACK_VAR (color_names[index]); color_names[index]->orig = xstrdup (color); + STACK_VAR (color_names[index]->orig); for (ch = color; *ch; ch++) *ch = tolower (*ch); color_names[index]->name = xstrdup (color); + STACK_VAR (color_names[index]->name); } RELEASE_VAR (str); @@ -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);