+ printf("\"%.*s\n", y+1<h, ",");
+ }
+ puts("};");
+}
+
+static int decode_colourseq(char *out, const char *arg)
+{
+ int i;
+
+ for (i = 0; i < 9; i++) {
+ switch (arg[i]) {
+ case 0: return 0;
+ case 'R': case 'r': out[i] = '1'; break;
+ case 'O': case 'o': out[i] = '2'; break;
+ case 'Y': case 'y': out[i] = '3'; break;
+ case 'G': case 'g': out[i] = '4'; break;
+ case 'B': case 'b': out[i] = '5'; break;
+ case 'W': case 'w': out[i] = '6'; break;
+ case '1': case '2': case '3': case '4': case '5': case '6':
+ case '0': out[i] = arg[i]; break;
+ default: goto err;
+ }
+ }
+
+ if (arg[i]) {
+err:
+ fprintf(stderr, "%s: error: invalid colour sequence '%s'\n",
+ progname, arg);
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Convert the user sequence into a list of colour index specifiers */
+static unsigned long *expand_seq(unsigned long *out, const char *seq)
+{
+ int i;
+
+ for (i = 0; i < 9; i++) {
+ out[i] = 0x30303 * (seq[i]-'0') + 0x20100;
+ }
+
+ return out;
+}
+
+static unsigned long decode_size(char *size_spec)
+{
+ unsigned long w, h;
+ char c = 0;
+ size_t n;
+
+ n = strspn(size_spec, "0123456789");
+ switch (size_spec[n]) {
+ default:
+ goto err_invalid;
+ case 0: case 'x': case 'X':
+ c = size_spec[n];
+ size_spec[n] = 0;
+ }
+
+ w = strtoul(size_spec, NULL, 10);
+ size_spec[n] = c;
+ if (w > 0xffff)
+ goto err_range;
+
+ if (!c) {
+ h = w;
+ } else {
+ n = strspn(size_spec += n+1, "0123456789");
+ if (size_spec[n])
+ goto err_invalid;
+
+ h = strtoul(size_spec, NULL, 10);
+ if (h > 0xffff)
+ goto err_range;
+ }
+
+ return (h << 16) | w;
+err_range:
+ fprintf(stderr, "%s: %s: %s\n", progname, size_spec, strerror(ERANGE));
+ return 0;
+err_invalid:
+ fprintf(stderr, "%s: %s: %s\n", progname, size_spec,
+ "invalid size specification");
+ return 0;
+}
+
+static unsigned long *find_icon(unsigned long size, unsigned long *ewmhicon)
+{
+ unsigned long w = size & 0xffff, h = (size >> 16) & 0xffff;
+ unsigned long i;
+
+ if (!size)
+ return NULL;
+
+ for (i = 0; i < EWMH_ICON_NELEM-2;) {
+ unsigned long icon_w = ewmhicon[i];
+ unsigned long icon_h = ewmhicon[i+1];
+
+ if (w == icon_w && h == icon_h) {
+ i += 2;
+ break;
+ }
+
+ assert(icon_w < ULONG_MAX / icon_h);
+ assert(i < ULONG_MAX - icon_w*icon_h);
+ i += 2 + icon_w*icon_h;
+ }
+
+ if (i < EWMH_ICON_NELEM)
+ return &ewmhicon[i];
+
+ fprintf(stderr, "%s: error: no %lux%lu icon found\n", progname, w, h);
+ return NULL;
+}
+
+int main(int argc, char **argv)
+{
+ unsigned long *ewmhicon, *icon, size, buf[9];
+ char colourseq[10] = "000000000";
+ int w32vga = 0;
+ int opt;
+
+ if (argc > 0)
+ progname = argv[0];
+
+ while ((opt = getopt_long(argc, argv, sopts, lopts, 0)) != -1) {
+ switch (opt) {
+ case 'c':
+ if (decode_colourseq(colourseq, optarg) != 0)
+ return EXIT_FAILURE;
+ break;
+ case LOPT_W32VGA:
+ w32vga = 1;
+ break;
+ case 'H':
+ print_help();
+ return EXIT_SUCCESS;
+ case 'V':
+ print_version();
+ return EXIT_SUCCESS;
+ default:
+ print_usage(stderr);
+ return EXIT_FAILURE;
+ }
+ }
+
+ if (argc != optind+1) {
+ printf("%s: error: size not specified\n", progname);
+ print_usage(stderr);
+ return EXIT_FAILURE;
+ }
+
+ size = decode_size(argv[optind]);
+ if (!size)
+ return EXIT_FAILURE;
+
+ ewmhicon = ewmh_icon_generate(expand_seq(buf, colourseq), colours[0]);
+ if (!ewmhicon) {
+ fprintf(stderr, "%s: failed to allocate memory\n", progname);
+ return EXIT_FAILURE;
+ }
+
+ icon = find_icon(size, ewmhicon);
+ if (!icon)
+ return EXIT_FAILURE;
+
+ if (w32vga) {
+ print_xpm_w32vga(size, icon);
+ } else {
+ print_xpm(colourseq, size, icon);