]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/picsmap.c
ncurses 6.5 - patch 20240504
[ncurses.git] / test / picsmap.c
index 2a95a75122c5d9790607fa044ef0695b0dfaec02..b582f3aada5d0828c0f110e3ba434363bebb7e27 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2017,2018 Free Software Foundation, Inc.                   *
+ * Copyright 2018-2022,2023 Thomas E. Dickey                                *
+ * Copyright 2017,2018 Free Software Foundation, Inc.                       *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -26,7 +27,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: picsmap.c,v 1.122 2018/05/20 19:43:40 tom Exp $
+ * $Id: picsmap.c,v 1.149 2023/04/23 23:20:37 tom Exp $
  *
  * Author: Thomas E. Dickey
  *
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#if HAVE_STDINT_H
-#include <stdint.h>
-#define my_intptr_t    intptr_t
-#else
-#define my_intptr_t    long
-#endif
-
 #if HAVE_TSEARCH
 #include <search.h>
 #endif
@@ -103,16 +97,16 @@ typedef struct {
 #define P2I(n) (((int)(my_intptr_t)(n)) - 1)
 #define I2P(n) (void *)(my_intptr_t)((n) + 1)
 
-#define stop_curses() if (in_curses) endwin()
+#define pause_curses() if (in_curses) stop_curses()
 
 #define debugmsg if (debugging) logmsg
 #define debugmsg2 if (debugging) logmsg2
 
-static void cleanup(int) GCC_NORETURN;
-static void giveup(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
-static void logmsg(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
-static void logmsg2(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
-static void warning(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
+static GCC_NORETURN void cleanup(int);
+static void giveup(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static void logmsg(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static void logmsg2(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static void warning(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
 static int gather_c_values(int);
 
 static FILE *logfp = 0;
@@ -133,7 +127,7 @@ static FG_NODE *reading_ncols;
 static void *reading_ntree;
 #endif
 
-#if HAVE_ALLOC_PAIR && HAVE_INIT_EXTENDED_COLOR
+#if HAVE_ALLOC_PAIR && USE_EXTENDED_COLOR
 #define USE_EXTENDED_COLORS 1
 static bool use_extended_pairs = FALSE;
 static bool use_extended_colors = FALSE;
@@ -142,7 +136,7 @@ static bool use_extended_colors = FALSE;
 #endif
 
 static void
-logmsg(const char *fmt,...)
+logmsg(const char *fmt, ...)
 {
     if (logfp != 0) {
        va_list ap;
@@ -155,7 +149,7 @@ logmsg(const char *fmt,...)
 }
 
 static void
-logmsg2(const char *fmt,...)
+logmsg2(const char *fmt, ...)
 {
     if (logfp != 0) {
        va_list ap;
@@ -187,7 +181,7 @@ close_log(void)
 static void
 cleanup(int code)
 {
-    stop_curses();
+    pause_curses();
     close_log();
     ExitProgram(code);
     /* NOTREACHED */
@@ -203,7 +197,7 @@ failed(const char *msg)
 }
 
 static void
-warning(const char *fmt,...)
+warning(const char *fmt, ...)
 {
     if (logfp != 0) {
        va_list ap;
@@ -379,26 +373,15 @@ finish_c_values(PICS_HEAD * head)
     reading_ncols = 0;
 }
 
-#if HAVE_TSEARCH && HAVE_TDESTROY
-static void
-never_free(void *node GCC_UNUSED)
-{
-}
-#endif
-
 static void
 dispose_c_values(void)
 {
 #if HAVE_TSEARCH
     if (reading_ntree != 0) {
-#if HAVE_TDESTROY
-       tdestroy(reading_ntree, never_free);
-#else
        int n;
        for (n = 0; n < reading_last; ++n) {
            tdelete(I2P(n), &reading_ntree, compare_c_values);
        }
-#endif
        reading_ntree = 0;
     }
 #endif
@@ -434,16 +417,14 @@ read_file(const char *filename)
     struct stat sb;
 
     if (!quiet) {
-       stop_curses();
+       pause_curses();
        printf("** %s\n", filename);
     }
 
     if (is_file(filename, &sb)) {
        size_t size = (size_t) sb.st_size;
        char *blob = typeCalloc(char, size + 1);
-       bool had_line = TRUE;
        bool binary = FALSE;
-       unsigned j;
        unsigned k = 0;
 
        result = typeCalloc(char *, size + 1);
@@ -453,7 +434,11 @@ read_file(const char *filename)
            FILE *fp = fopen(filename, "r");
            if (fp != 0) {
                logmsg("opened %s", filename);
+
                if (fread(blob, sizeof(char), size, fp) == size) {
+                   bool had_line = TRUE;
+                   unsigned j;
+
                    for (j = 0; (size_t) j < size; ++j) {
                        if (blob[j] == '\0' ||
                            (UChar(blob[j]) < 32 &&
@@ -495,45 +480,46 @@ read_file(const char *filename)
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
        "Usage: picsmap [options] [imagefile [...]]"
        ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
        ,""
+       ,USAGE_COMMON
        ,"Options:"
-       ,"  -a ratio     aspect-ratio correction for ImageMagick"
+       ," -a ratio aspect-ratio correction for ImageMagick"
 #if HAVE_USE_DEFAULT_COLORS
-       ,"  -d           invoke use_default_colors"
+       ," -d       invoke use_default_colors"
 #endif
-       ,"  -L           add debugging information to logfile"
-       ,"  -l logfile   write informational messages to logfile"
-       ,"  -p palette   color-palette file (default \"$TERM.dat\")"
-       ,"  -q           less verbose"
-       ,"  -r rgb-path  xpm uses X rgb color-names (default \"" RGB_PATH "\")"
-       ,"  -s SECS      pause for SECS seconds after display vs getch"
+       ," -L       add debugging information to logfile"
+       ," -l FILE  write informational messages to FILE"
+       ," -p FILE  color-palette file (default \"$TERM.dat\")"
+       ," -q       less verbose"
+       ," -r FILE  xpm uses X rgb color-names in FILE (default \"" RGB_PATH "\")"
+       ," -s SECS  pause for SECS seconds after display vs getch"
 #if USE_EXTENDED_COLORS
-       ,"  -x [pc]      use extension (p=extended-pairs, c=extended-colors)"
-       ,"               Either/both extension may be given"
+       ," -x [pc]  use extension (p=extended-pairs, c=extended-colors)"
+       ,"          Either/both extension may be given"
 #endif
     };
     size_t n;
 
-    stop_curses();
+    pause_curses();
 
     fflush(stdout);
     for (n = 0; n < SIZEOF(msg); n++)
        fprintf(stderr, "%s\n", msg[n]);
-    cleanup(EXIT_FAILURE);
+    cleanup(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 static void
-giveup(const char *fmt,...)
+giveup(const char *fmt, ...)
 {
     va_list ap;
 
-    stop_curses();
+    pause_curses();
     fflush(stdout);
 
     va_start(ap, fmt);
@@ -549,7 +535,7 @@ giveup(const char *fmt,...)
        fflush(logfp);
     }
 
-    usage();
+    usage(FALSE);
 }
 
 /*
@@ -563,7 +549,9 @@ read_palette(const char *filename)
 {
     static const char *data_dir = DATA_DIR;
     char **result = 0;
-    char *full_name = malloc(strlen(data_dir) + 20 + strlen(filename));
+    size_t last = strlen(filename);
+    size_t need = (strlen(data_dir) + 20 + last);
+    char *full_name = malloc(need);
     char *s;
     struct stat sb;
 
@@ -574,14 +562,16 @@ read_palette(const char *filename)
            *(s = full_name) = '\0';
            if (tries & 1) {
                if (strchr(filename, '/') == 0) {
-                   sprintf(full_name, "%s/", data_dir);
+                   _nc_SPRINTF(full_name, _nc_SLIMIT(need) "%s/", data_dir);
                } else {
                    continue;
                }
            }
            s += strlen(s);
+           if (((size_t) (s - full_name) + last + 1) >= need)
+               continue;
 
-           strcpy(s, filename);
+           _nc_STRCAT(full_name, filename, need);
            if (tries & 4) {
                char *t = s;
                char *tc;
@@ -602,17 +592,17 @@ read_palette(const char *filename)
                }
                if (found && (t != s)
                    && (strncmp) (s, "xterm", (size_t) (t - s))) {
-                   sprintf(s, "xterm%s", filename + (t - s));
+                   _nc_SPRINTF(s, _nc_SLIMIT(need - (size_t) (s - full_name))
+                               "xterm%s", filename + (t - s));
                } else {
                    continue;
                }
            }
-           s += strlen(s);
 
            if (tries & 2) {
                int len = (int) strlen(filename);
                if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
-                   strcpy(s, ".dat");
+                   _nc_STRCAT(full_name, ".dat", need);
                } else {
                    continue;
                }
@@ -689,9 +679,9 @@ init_palette(const char *palette_file)
        if ((power2 != COLORS) || ((shift % 3) != 0)) {
            if (all_colors == 0) {
                init_palette(getenv("TERM"));
-           }
-           if (all_colors == 0) {
-               giveup("With %d colors, you need a palette-file", COLORS);
+               if (all_colors == 0) {
+                   giveup("With %d colors, you need a palette-file", COLORS);
+               }
            }
        }
     }
@@ -765,7 +755,7 @@ bytes_of(int value)
     return value;
 }
 
-static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
+static int match_c(const char *, const char *, ...) GCC_SCANFLIKE(2,3);
 
 static char *
 skip_s(char *s)
@@ -793,7 +783,7 @@ skip_word(char *s)
 }
 
 static int
-match_c(const char *source, const char *pattern,...)
+match_c(const char *source, const char *pattern, ...)
 {
     int limit = (int) strlen(source);
     const char *last_s = source + limit;
@@ -801,6 +791,7 @@ match_c(const char *source, const char *pattern,...)
     int ch;
     int *ip;
     char *cp;
+    float *fp;
     long lv;
 
     va_start(ap, pattern);
@@ -814,10 +805,13 @@ match_c(const char *source, const char *pattern,...)
            continue;
        }
        /* %c, %d, %s are like sscanf except for special treatment of blanks */
-       if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
+       if (ch == '%' && *pattern != '\0' && strchr("%cdnfsx", *pattern)) {
            bool found = FALSE;
            ch = *pattern++;
            switch (ch) {
+           case '%':
+               source++;
+               break;
            case 'c':
                cp = va_arg(ap, char *);
                do {
@@ -836,6 +830,29 @@ match_c(const char *source, const char *pattern,...)
                    goto finish;
                }
                break;
+           case 'f':
+               /* floating point for pixels... */
+               fp = va_arg(ap, float *);
+               lv = strtol(source, &cp, 10);
+               if (cp == 0 || cp == source)
+                   goto finish;
+               *fp = (float) lv;
+               source = cp;
+               if (*source == '.') {
+                   lv = strtol(++source, &cp, 10);
+                   if (cp == 0 || cp == source)
+                       goto finish;
+                   {
+                       float scale = 1.0f;
+                       int digits = (int) (cp - source);
+                       while (digits-- > 0) {
+                           scale *= 10.0f;
+                       }
+                       *fp += (float) lv / scale;
+                   }
+                   source = cp;
+               }
+               break;
            case 'n':
                /* not really sscanf... */
                limit = *va_arg(ap, int *);
@@ -882,7 +899,6 @@ match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
        arg1[cpp] = '\0';
        result = 1;
     } else {
-       char *t;
        const char *s = skip_cs(source);
        size_t have = strlen(source);
 
@@ -890,6 +906,7 @@ match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
            memcpy(arg1, s, (size_t) cpp);
            s += cpp;
            while (*s++ == '\t') {
+               char *t;
                for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
                    if (*s == ' ') {
                        s = skip_cs(s);
@@ -958,6 +975,20 @@ parse_rgb(char **data)
     return result;
 }
 
+#define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
+
+static int
+CaselessCmp(const char *a, const char *b)
+{                              /* strcasecmp isn't portable */
+    while (*a && *b) {
+       int cmp = LOWERCASE(*a) - LOWERCASE(*b);
+       if (cmp != 0)
+           break;
+       a++, b++;
+    }
+    return LOWERCASE(*a) - LOWERCASE(*b);
+}
+
 static RGB_NAME *
 lookup_rgb(const char *name)
 {
@@ -965,7 +996,7 @@ lookup_rgb(const char *name)
     if (rgb_table != 0) {
        int n;
        for (n = 0; rgb_table[n].name != 0; ++n) {
-           if (!strcasecmp(name, rgb_table[n].name)) {
+           if (!CaselessCmp(name, rgb_table[n].name)) {
                result = &rgb_table[n];
                break;
            }
@@ -979,7 +1010,7 @@ parse_xbm(char **data)
 {
     int n;
     int state = 0;
-    char buf[BUFSIZ];
+    char buf[2048];
     int num;
     char ch;
     char *s;
@@ -1004,7 +1035,7 @@ parse_xbm(char **data)
        case 0:
        case 1:
        case 2:
-           if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
+           if (sscanf(s, "#define %1024s %d%c", buf, &num, &ch) >= 2) {
                if ((t = strstr(buf, "_width")) != 0) {
                    state |= 1;
                    result->wide = (short) bytes_of(num);
@@ -1025,7 +1056,7 @@ parse_xbm(char **data)
            }
            break;
        case 3:
-           if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
+           if (sscanf(s, "static char %1024[^_ ]_bits[]%c", buf, &ch) >= 1) {
                if (strcmp(result->name, buf)) {
                    goto finish;
                }
@@ -1261,7 +1292,8 @@ parse_xpm(char **data)
 static PICS_HEAD *
 parse_img(const char *filename)
 {
-    char *cmd = malloc(strlen(filename) + 256);
+    size_t need = strlen(filename) + 256;
+    char *cmd = malloc(need);
     FILE *pp;
     char buffer[BUFSIZ];
     char dummy[BUFSIZ];
@@ -1271,9 +1303,9 @@ parse_img(const char *filename)
     int pic_y = 0;
     int width = in_curses ? COLS : 80;
 
-    sprintf(cmd, "identify \"%s\"", filename);
+    _nc_SPRINTF(cmd, _nc_SLIMIT(need) "identify \"%s\"", filename);
     if (quiet)
-       strcat(cmd, " 2>/dev/null");
+       _nc_STRCAT(cmd, " 2>/dev/null", need);
 
     logmsg("...opening pipe to %s", cmd);
 
@@ -1299,11 +1331,12 @@ parse_img(const char *filename)
     if (pic_x <= 0 || pic_y <= 0)
        goto finish;
 
-    sprintf(cmd, "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
-           "-define txt:compliance=SVG txt:-",
-           pic_x, pic_y, width, filename);
+    _nc_SPRINTF(cmd, _nc_SLIMIT(need)
+               "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
+               "-define txt:compliance=SVG txt:-",
+               pic_x, pic_y, width, filename);
     if (quiet)
-       strcat(cmd, " 2>/dev/null");
+       _nc_STRCAT(cmd, " 2>/dev/null", need);
 
     logmsg("...opening pipe to %s", cmd);
     if ((pp = popen(cmd, "r")) != 0) {
@@ -1334,29 +1367,61 @@ parse_img(const char *filename)
                    break;
                }
            } else {
-               /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
+               /*
+                * subsequent lines begin "col,row: (r,g,b,a) #RGB".
+                * Those r/g/b could be integers (0..255) or float-percentages.
+                */
                int r, g, b, nocolor;
+               float rf, gf, bf;
                unsigned check;
-               int which, c;
                char *t;
                char *s = t = strchr(buffer, '#');
+               bool matched = FALSE;
+
                if (s != 0) {
                    /* after the "#RGB", there are differences - just ignore */
                    while (*s != '\0' && !isspace(UChar(*s)))
                        ++s;
                    *++s = '\0';
                }
+
                if (match_c(buffer,
                            "%d,%d: (%d,%d,%d,%d) #%x ",
                            &col, &row,
                            &r, &g, &b, &nocolor,
                            &check)) {
-                   if ((s - t) > 8)    /* 6 hex digits vs 8 */
-                       check /= 256;
-                   if (r > MaxRGB ||
-                       g > MaxRGB ||
-                       b > MaxRGB ||
-                       check != (unsigned) ((r << 16) | (g << 8) | b)) {
+                   matched = TRUE;
+               } else if (match_c(buffer,
+                                  "%d,%d: (%f%%,%f%%,%f%%,%d) #%x ",
+                                  &col, &row,
+                                  &rf, &gf, &bf, &nocolor,
+                                  &check) ||
+                          match_c(buffer,
+                                  "%d,%d: (%f%%,%f%%,%f%%) #%x ",
+                                  &col, &row,
+                                  &rf, &gf, &bf,
+                                  &check)) {
+                   matched = TRUE;
+
+#define fp_fix(n) (int) (MaxRGB * (((n) > 100.0 ? 100.0 : (n)) / 100.0))
+
+                   r = fp_fix(rf);
+                   g = fp_fix(gf);
+                   b = fp_fix(bf);
+               }
+               if ((s - t) > 8)        /* 6 hex digits vs 8 */
+                   check /= 256;
+               if (matched) {
+                   int which, c;
+                   int want_r = (check >> 16) & 0xff;
+                   int want_g = (check >> 8) & 0xff;
+                   int want_b = (check >> 0) & 0xff;
+
+#define fp_err(tst,ref) ((tst > MaxRGB) || ((tst - ref)*(tst - ref)) > 4)
+
+                   if (fp_err(r, want_r) ||
+                       fp_err(g, want_g) ||
+                       fp_err(b, want_b)) {
                        okay = FALSE;
                        break;
                    }
@@ -1453,8 +1518,10 @@ dump_picture(PICS_HEAD * pics)
 static void
 init_display(const char *palette_path, int opt_d)
 {
+    (void) opt_d;
     if (isatty(fileno(stdout))) {
        in_curses = TRUE;
+       setlocale(LC_ALL, "");
        initscr();
        cbreak();
        noecho();
@@ -1468,7 +1535,7 @@ init_display(const char *palette_path, int opt_d)
            init_palette(palette_path);
        }
        scrollok(stdscr, FALSE);
-       exit_curses();
+       stop_curses();
     }
 }
 
@@ -1477,7 +1544,6 @@ show_picture(PICS_HEAD * pics)
 {
     int y, x;
     int n;
-    int my_pair, my_color;
 
     debugmsg("called show_picture");
     logmsg("...using %dx%d screen", LINES, COLS);
@@ -1490,8 +1556,8 @@ show_picture(PICS_HEAD * pics)
     if (has_colors()) {
        logmsg("...using %d colors", pics->colors);
        for (n = 0; n < pics->colors; ++n) {
-           my_pair = (n + 1);
-           my_color = map_color(fg_color(pics, n));
+           int my_pair = (n + 1);
+           int my_color = map_color(fg_color(pics, n));
 #if USE_EXTENDED_COLORS
            if (use_extended_pairs) {
                init_extended_pair(my_pair, my_color, my_color);
@@ -1510,7 +1576,10 @@ show_picture(PICS_HEAD * pics)
        if (y >= LINES)
            break;
        move(y, 0);
+
        for (x = 0; x < pics->wide; ++x) {
+           int my_pair;
+
            if (x >= COLS)
                break;
            n = (y * pics->wide + x);
@@ -1560,11 +1629,9 @@ compare_fg_counts(const void *a, const void *b)
 static void
 report_colors(PICS_HEAD * pics)
 {
-    int j, k;
-    int high;
-    int wide = 4;
     int accum;
     double level;
+    int j;
     int shift;
     int total;
     char buffer[256];
@@ -1578,6 +1645,8 @@ report_colors(PICS_HEAD * pics)
      */
     if (debugging && (pics->colors < 1000)) {
        int digits = 0;
+       int high;
+       int wide = 4;
        for (j = pics->colors; j != 0; j /= 10) {
            ++digits;
            if (j < 10)
@@ -1588,26 +1657,31 @@ report_colors(PICS_HEAD * pics)
        logmsg("These colors were used:");
        high = (pics->colors + wide - 1) / wide;
        for (j = 0; j < high && j < pics->colors; ++j) {
+           int k;
            char *s = buffer;
            *s = '\0';
            for (k = 0; k < wide; ++k) {
                int n = j + (k * high);
+               size_t want = (sizeof(buffer) - (size_t) (s - buffer));
+               if (want < 100 || want >= sizeof(buffer))
+                   break;
                if (n >= pics->colors)
                    break;
                if (k) {
                    *s++ = ' ';
                    if (digits < 8) {
-                       sprintf(s, "%*s", 8 - digits, " ");
+                       _nc_SPRINTF(s, _nc_SLIMIT(want) "%*s", 8 - digits,
+                                   " ");
                        s += strlen(s);
                    }
                }
                if (pics->fgcol[n].fgcol >= 0) {
-                   sprintf(s, "%3d #%06X %*d", n,
-                           pics->fgcol[n].fgcol,
-                           digits, pics->fgcol[n].count);
+                   _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d #%06X %*d", n,
+                               pics->fgcol[n].fgcol,
+                               digits, pics->fgcol[n].count);
                } else {
-                   sprintf(s, "%3d (empty) %*d", n,
-                           digits, pics->fgcol[n].count);
+                   _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d (empty) %*d", n,
+                               digits, pics->fgcol[n].count);
                }
                s += strlen(s);
                if ((s - buffer) > 100)
@@ -1643,24 +1717,27 @@ report_colors(PICS_HEAD * pics)
        }
     }
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
 main(int argc, char *argv[])
 {
-    int n;
+    int ch;
     int opt_d = FALSE;
     char ignore_ch;
     const char *palette_path = 0;
     const char *rgb_path = RGB_PATH;
 
-    while ((n = getopt(argc, argv, "a:dLl:p:qr:s:x:")) != -1) {
-       switch (n) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "a:dLl:p:qr:s:x:")) != -1) {
+       switch (ch) {
        case 'a':
            if (sscanf(optarg, "%lf%c", &aspect_ratio, &ignore_ch) != 1
                || aspect_ratio < 0.1
                || aspect_ratio > 10.) {
                fprintf(stderr, "Expected a number in [0.1 to 10.]: %s\n", optarg);
-               usage();
+               usage(FALSE);
            }
            break;
 #if HAVE_USE_DEFAULT_COLORS
@@ -1700,21 +1777,25 @@ main(int argc, char *argv[])
                        use_extended_colors = TRUE;
                        break;
                    default:
-                       usage();
+                       usage(FALSE);
                        break;
                    }
                }
            }
            break;
 #endif
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
-           break;
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
 
     if (optind < argc) {
        char **rgb_data = read_file(rgb_path);
+       int n;
 
        if (rgb_data)
            rgb_table = parse_rgb(rgb_data);
@@ -1747,7 +1828,7 @@ main(int argc, char *argv[])
        free(rgb_table);
        free(all_colors);
     } else {
-       usage();
+       usage(FALSE);
     }
 
     cleanup(EXIT_SUCCESS);