1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
/*
* cwal - generate application themes from a single theme file
* Copyright (C) 2026 deltegui
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define TEXT_SIZE 128
typedef struct {
char name[TEXT_SIZE];
char terminal_font[TEXT_SIZE];
char terminal_font_fallback[TEXT_SIZE];
int terminal_font_size;
char ui_font[TEXT_SIZE];
char icon_theme[TEXT_SIZE];
int ui_font_size;
char waybar_font[TEXT_SIZE];
char waybar_background[7], waybar_text[7];
int waybar_radius;
char crust[7], base[7], surface0[7], surface1[7], surface2[7];
char overlay0[7], subtext0[7], subtext1[7], text[7];
char accent[7], red[7], green[7], yellow[7], blue[7];
char pink[7], teal[7], peach[7];
int border_width;
int border_radius;
int gap_inner;
int gap_outer;
double opacity;
double terminal_opacity;
} Theme;
/*
* The palette colors. Each row ties a template variable name to the Theme
* field that holds it. For every row ${name}, ${name_rgb} and ${name_256}
* are available to templates; to add a palette color, add a field to Theme
* and one row here.
*/
typedef struct {
const char *name;
size_t offset;
} ColorField;
#define COLOR_FIELD(field) {#field, offsetof(Theme, field)}
static const ColorField color_fields[] = {
COLOR_FIELD(crust), COLOR_FIELD(base), COLOR_FIELD(surface0),
COLOR_FIELD(surface1), COLOR_FIELD(surface2), COLOR_FIELD(overlay0),
COLOR_FIELD(subtext0), COLOR_FIELD(subtext1), COLOR_FIELD(text),
COLOR_FIELD(accent), COLOR_FIELD(red), COLOR_FIELD(green),
COLOR_FIELD(yellow), COLOR_FIELD(blue), COLOR_FIELD(pink),
COLOR_FIELD(teal), COLOR_FIELD(peach),
};
#undef COLOR_FIELD
#define COLOR_FIELD_COUNT (sizeof(color_fields) / sizeof(color_fields[0]))
typedef struct {
const char *name;
const char *value;
} TemplateVariable;
static int format_value(char *buffer, size_t size, const char *format, ...)
{
va_list arguments;
int result;
va_start(arguments, format);
result = vsnprintf(buffer, size, format, arguments);
va_end(arguments);
return result < 0 || (size_t)result >= size ? -1 : 0;
}
static unsigned int hex_digit(char character)
{
return isdigit((unsigned char)character)
? (unsigned int)(character - '0')
: (unsigned int)(tolower((unsigned char)character) - 'a' + 10);
}
static void parse_hex_color(const char *color, unsigned int *red,
unsigned int *green, unsigned int *blue)
{
*red = hex_digit(color[0]) * 16U + hex_digit(color[1]);
*green = hex_digit(color[2]) * 16U + hex_digit(color[3]);
*blue = hex_digit(color[4]) * 16U + hex_digit(color[5]);
}
static int format_rgb(char *buffer, size_t size, const char *color)
{
unsigned int red;
unsigned int green;
unsigned int blue;
parse_hex_color(color, &red, &green, &blue);
return format_value(buffer, size, "%u,%u,%u", red, green, blue);
}
static int format_xterm_color(char *buffer, size_t size, const char *color)
{
static const unsigned int ansi[16][3] = {
{0, 0, 0}, {128, 0, 0}, {0, 128, 0}, {128, 128, 0},
{0, 0, 128}, {128, 0, 128}, {0, 128, 128}, {192, 192, 192},
{128, 128, 128}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0},
{0, 0, 255}, {255, 0, 255}, {0, 255, 255}, {255, 255, 255}};
unsigned int red;
unsigned int green;
unsigned int blue;
unsigned int best_index = 0;
unsigned long best_distance = (unsigned long)-1;
unsigned int index;
parse_hex_color(color, &red, &green, &blue);
for (index = 0; index < 256; index++) {
unsigned int candidate_red;
unsigned int candidate_green;
unsigned int candidate_blue;
long red_difference;
long green_difference;
long blue_difference;
unsigned long distance;
if (index < 16) {
candidate_red = ansi[index][0];
candidate_green = ansi[index][1];
candidate_blue = ansi[index][2];
} else if (index < 232) {
static const unsigned int cube[] = {0, 95, 135, 175, 215, 255};
unsigned int cube_index = index - 16;
candidate_red = cube[cube_index / 36];
candidate_green = cube[(cube_index / 6) % 6];
candidate_blue = cube[cube_index % 6];
} else {
unsigned int gray = 8 + (index - 232) * 10;
candidate_red = gray;
candidate_green = gray;
candidate_blue = gray;
}
red_difference = (long)red - (long)candidate_red;
green_difference = (long)green - (long)candidate_green;
blue_difference = (long)blue - (long)candidate_blue;
distance = (unsigned long)(red_difference * red_difference +
green_difference * green_difference +
blue_difference * blue_difference);
if (distance < best_distance) {
best_distance = distance;
best_index = index;
}
}
return format_value(buffer, size, "%02x", best_index);
}
typedef struct {
const char *suffix;
int (*format)(char *buffer, size_t size, const char *color);
} ColorForm;
static int format_hex(char *buffer, size_t size, const char *color)
{
return format_value(buffer, size, "%s", color);
}
static const ColorForm color_forms[] = {
{"", format_hex},
{"_rgb", format_rgb},
{"_256", format_xterm_color},
};
/*
* Resolves ${color}, ${color_rgb} and ${color_256}. Returns 1 and sets *value
* (to buffer) when name is a palette variable, 0 when it is not, -1 when
* formatting fails.
*/
static int find_color_variable(const Theme *theme, const char *name,
size_t name_length, char *buffer, size_t size,
const char **value)
{
size_t field_index;
size_t form_index;
for (field_index = 0; field_index < COLOR_FIELD_COUNT; field_index++) {
const ColorField *field = &color_fields[field_index];
const char *color = (const char *)theme + field->offset;
for (form_index = 0;
form_index < sizeof(color_forms) / sizeof(color_forms[0]);
form_index++) {
const ColorForm *form = &color_forms[form_index];
size_t field_length = strlen(field->name);
size_t suffix_length = strlen(form->suffix);
if (name_length != field_length + suffix_length ||
strncmp(name, field->name, field_length) != 0 ||
strncmp(name + field_length, form->suffix, suffix_length) != 0)
continue;
if (form->format(buffer, size, color) != 0)
return -1;
*value = buffer;
return 1;
}
}
return 0;
}
static const char *find_template_variable(
const TemplateVariable *variables, size_t variable_count,
const char *name, size_t name_length)
{
size_t index;
for (index = 0; index < variable_count; index++) {
if (strlen(variables[index].name) == name_length &&
strncmp(variables[index].name, name, name_length) == 0)
return variables[index].value;
}
return NULL;
}
static int write_template(FILE *file, const char *template_text,
const Theme *theme)
{
char terminal_font_size[16];
char ui_font_size[16];
char border_width[16];
char border_radius[16];
char inner_radius[16];
char waybar_radius[16];
char gap_inner[16];
char gap_outer[16];
char opacity[32];
char terminal_opacity[32];
char alpha_hex[3];
int alpha;
int inner;
const char *cursor;
TemplateVariable variables[] = {
{"name", theme->name},
{"terminal_font", theme->terminal_font},
{"terminal_font_fallback", theme->terminal_font_fallback},
{"ui_font", theme->ui_font},
{"icon_theme", theme->icon_theme},
{"waybar_font", theme->waybar_font},
{"waybar_background", theme->waybar_background},
{"waybar_text", theme->waybar_text},
{"terminal_font_size", terminal_font_size},
{"ui_font_size", ui_font_size},
{"border_width", border_width},
{"border_radius", border_radius},
{"inner_radius", inner_radius},
{"waybar_radius", waybar_radius},
{"gap_inner", gap_inner},
{"gap_outer", gap_outer},
{"opacity", opacity},
{"terminal_opacity", terminal_opacity},
{"alpha_hex", alpha_hex},
};
size_t variable_count = sizeof(variables) / sizeof(variables[0]);
alpha = (int)(theme->opacity * 255.0 + 0.5);
inner = theme->border_radius > theme->border_width
? theme->border_radius - theme->border_width
: theme->border_radius;
if (format_value(terminal_font_size, sizeof(terminal_font_size), "%d",
theme->terminal_font_size) != 0 ||
format_value(ui_font_size, sizeof(ui_font_size), "%d",
theme->ui_font_size) != 0 ||
format_value(border_width, sizeof(border_width), "%d",
theme->border_width) != 0 ||
format_value(border_radius, sizeof(border_radius), "%d",
theme->border_radius) != 0 ||
format_value(inner_radius, sizeof(inner_radius), "%d", inner) != 0 ||
format_value(waybar_radius, sizeof(waybar_radius), "%d",
theme->waybar_radius) != 0 ||
format_value(gap_inner, sizeof(gap_inner), "%d", theme->gap_inner) !=
0 ||
format_value(gap_outer, sizeof(gap_outer), "%d", theme->gap_outer) !=
0 ||
format_value(opacity, sizeof(opacity), "%.3f", theme->opacity) != 0 ||
format_value(terminal_opacity, sizeof(terminal_opacity), "%.3f",
theme->terminal_opacity) != 0 ||
format_value(alpha_hex, sizeof(alpha_hex), "%02x", alpha) != 0) {
fprintf(stderr, "cwal: could not format template values\n");
return -1;
}
cursor = template_text;
while (*cursor != '\0') {
const char *dollar = strchr(cursor, '$');
const char *closing;
const char *value;
char color_buffer[16];
size_t length;
int found;
if (dollar == NULL) {
if (fputs(cursor, file) == EOF)
return -1;
break;
}
length = (size_t)(dollar - cursor);
if (length > 0 && fwrite(cursor, 1, length, file) != length)
return -1;
if (dollar[1] == '$') {
if (fputc('$', file) == EOF)
return -1;
cursor = dollar + 2;
continue;
}
if (dollar[1] != '{') {
if (fputc('$', file) == EOF)
return -1;
cursor = dollar + 1;
continue;
}
closing = strchr(dollar + 2, '}');
if (closing == NULL) {
fprintf(stderr,
"cwal: unterminated variable in template\n");
return -1;
}
length = (size_t)(closing - (dollar + 2));
found = find_color_variable(theme, dollar + 2, length, color_buffer,
sizeof(color_buffer), &value);
if (found < 0) {
fprintf(stderr, "cwal: could not format terminal color\n");
return -1;
}
if (found == 0)
value = find_template_variable(variables, variable_count,
dollar + 2, length);
if (value == NULL) {
fprintf(stderr, "cwal: unknown template variable: ");
fwrite(dollar + 2, 1, length, stderr);
fputc('\n', stderr);
return -1;
}
if (fputs(value, file) == EOF)
return -1;
cursor = closing + 1;
}
return 0;
}
static char *trim(char *text)
{
char *end;
while (isspace((unsigned char)*text))
text++;
if (*text == '\0')
return text;
end = text + strlen(text) - 1;
while (end > text && isspace((unsigned char)*end))
end--;
end[1] = '\0';
return text;
}
static int copy_text(char *destination, size_t size, const char *value,
const char *key, unsigned long line)
{
int result;
if (*value == '\0') {
fprintf(stderr, "cwal: line %lu: %s cannot be empty\n",
line, key);
return -1;
}
result = snprintf(destination, size, "%s", value);
if (result < 0 || (size_t)result >= size) {
fprintf(stderr, "cwal: line %lu: %s is too long\n",
line, key);
return -1;
}
return 0;
}
static int copy_color(char destination[7], const char *value, const char *key,
unsigned long line)
{
size_t index;
if (*value == '#')
value++;
if (strlen(value) != 6) {
fprintf(stderr,
"cwal: line %lu: %s must be an RRGGBB color\n",
line, key);
return -1;
}
for (index = 0; index < 6; index++) {
if (!isxdigit((unsigned char)value[index])) {
fprintf(stderr,
"cwal: line %lu: invalid character in %s\n",
line, key);
return -1;
}
destination[index] = (char)tolower((unsigned char)value[index]);
}
destination[6] = '\0';
return 0;
}
static int parse_integer(const char *value, int minimum, int maximum, int *out,
const char *key, unsigned long line)
{
char *end;
long parsed;
errno = 0;
parsed = strtol(value, &end, 10);
if (errno != 0 || *value == '\0' || *end != '\0' || parsed < minimum ||
parsed > maximum) {
fprintf(stderr,
"cwal: line %lu: invalid value for %s (%d-%d)\n",
line, key, minimum, maximum);
return -1;
}
*out = (int)parsed;
return 0;
}
static int parse_opacity(const char *value, double *out, const char *key,
unsigned long line)
{
char *end;
double parsed;
errno = 0;
parsed = strtod(value, &end);
if (errno != 0 || *value == '\0' || *end != '\0' || !isfinite(parsed) ||
parsed < 0.0 || parsed > 1.0) {
fprintf(stderr,
"cwal: line %lu: %s must be between 0.0 and 1.0\n",
line, key);
return -1;
}
*out = parsed;
return 0;
}
static int set_value(Theme *theme, const char *key, const char *value,
unsigned long line, int colors_only)
{
#define TEXT_KEY(field) \
if (strcmp(key, #field) == 0) \
return copy_text(theme->field, sizeof(theme->field), value, key, line)
#define COLOR_KEY(field) \
if (strcmp(key, #field) == 0) \
return copy_color(theme->field, value, key, line)
if (!colors_only) {
TEXT_KEY(name);
TEXT_KEY(terminal_font);
TEXT_KEY(terminal_font_fallback);
TEXT_KEY(ui_font);
TEXT_KEY(icon_theme);
TEXT_KEY(waybar_font);
}
COLOR_KEY(waybar_background);
COLOR_KEY(waybar_text);
COLOR_KEY(crust);
COLOR_KEY(base);
COLOR_KEY(surface0);
COLOR_KEY(surface1);
COLOR_KEY(surface2);
COLOR_KEY(overlay0);
COLOR_KEY(subtext0);
COLOR_KEY(subtext1);
COLOR_KEY(text);
COLOR_KEY(accent);
COLOR_KEY(red);
COLOR_KEY(green);
COLOR_KEY(yellow);
COLOR_KEY(blue);
COLOR_KEY(pink);
COLOR_KEY(teal);
COLOR_KEY(peach);
#undef TEXT_KEY
#undef COLOR_KEY
if (colors_only) {
fprintf(stderr,
"cwal: line %lu: color overrides cannot set %s\n",
line, key);
return -1;
}
if (strcmp(key, "terminal_font_size") == 0)
return parse_integer(value, 6, 72, &theme->terminal_font_size, key,
line);
if (strcmp(key, "ui_font_size") == 0)
return parse_integer(value, 6, 72, &theme->ui_font_size, key, line);
if (strcmp(key, "border_width") == 0)
return parse_integer(value, 0, 20, &theme->border_width, key, line);
if (strcmp(key, "border_radius") == 0)
return parse_integer(value, 0, 100, &theme->border_radius, key, line);
if (strcmp(key, "waybar_radius") == 0)
return parse_integer(value, 0, 100, &theme->waybar_radius, key, line);
if (strcmp(key, "gap_inner") == 0)
return parse_integer(value, 0, 100, &theme->gap_inner, key, line);
if (strcmp(key, "gap_outer") == 0)
return parse_integer(value, 0, 100, &theme->gap_outer, key, line);
if (strcmp(key, "opacity") == 0)
return parse_opacity(value, &theme->opacity, key, line);
if (strcmp(key, "terminal_opacity") == 0)
return parse_opacity(value, &theme->terminal_opacity, key, line);
fprintf(stderr, "cwal: line %lu: unknown key: %s\n", line,
key);
return -1;
}
static void initialize_theme(Theme *theme)
{
memset(theme, 0, sizeof(*theme));
theme->terminal_font_size = -1;
theme->ui_font_size = -1;
theme->border_width = -1;
theme->border_radius = -1;
theme->waybar_radius = -1;
theme->gap_inner = -1;
theme->gap_outer = -1;
theme->opacity = -1.0;
theme->terminal_opacity = -1.0;
}
static int load_theme(const char *path, Theme *theme, int colors_only)
{
FILE *file;
int close_file = 1;
char line_buffer[512];
unsigned long line = 0;
if (strcmp(path, "-") == 0) {
file = stdin;
close_file = 0;
} else {
file = fopen(path, "r");
if (file == NULL) {
fprintf(stderr, "cwal: could not open %s: %s\n", path,
strerror(errno));
return -1;
}
}
while (fgets(line_buffer, sizeof(line_buffer), file) != NULL) {
char *key;
char *value;
char *separator;
line++;
if (strchr(line_buffer, '\n') == NULL && !feof(file)) {
fprintf(stderr, "cwal: line %lu is too long\n", line);
if (close_file)
fclose(file);
return -1;
}
key = trim(line_buffer);
if (*key == '\0' || *key == '#' || *key == ';')
continue;
separator = strchr(key, '=');
if (separator == NULL) {
fprintf(stderr, "cwal: line %lu: expected key=value\n",
line);
if (close_file)
fclose(file);
return -1;
}
*separator = '\0';
value = trim(separator + 1);
key = trim(key);
if (set_value(theme, key, value, line, colors_only) != 0) {
if (close_file)
fclose(file);
return -1;
}
}
if (ferror(file)) {
fprintf(stderr, "cwal: error reading %s\n", path);
if (close_file)
fclose(file);
return -1;
}
if (close_file)
fclose(file);
return 0;
}
static int validate_theme(const Theme *theme)
{
#define REQUIRE(field) \
if (theme->field[0] == '\0') { \
fprintf(stderr, "cwal: missing key %s\n", #field); \
return -1; \
}
REQUIRE(name);
REQUIRE(terminal_font);
REQUIRE(terminal_font_fallback);
REQUIRE(ui_font);
REQUIRE(waybar_font);
REQUIRE(waybar_background);
REQUIRE(waybar_text);
REQUIRE(crust);
REQUIRE(base);
REQUIRE(surface0);
REQUIRE(surface1);
REQUIRE(surface2);
REQUIRE(overlay0);
REQUIRE(subtext0);
REQUIRE(subtext1);
REQUIRE(text);
REQUIRE(accent);
REQUIRE(red);
REQUIRE(green);
REQUIRE(yellow);
REQUIRE(blue);
REQUIRE(pink);
REQUIRE(teal);
REQUIRE(peach);
#undef REQUIRE
if (theme->terminal_font_size < 0 || theme->ui_font_size < 0 ||
theme->border_width < 0 || theme->border_radius < 0 ||
theme->waybar_radius < 0 || theme->gap_inner < 0 ||
theme->gap_outer < 0 || theme->opacity < 0.0 ||
theme->terminal_opacity < 0.0) {
fprintf(stderr, "cwal: missing numeric visual values\n");
return -1;
}
if (strpbrk(theme->terminal_font, "\"\\,") != NULL ||
strpbrk(theme->terminal_font_fallback, "\"\\,") != NULL ||
strpbrk(theme->ui_font, "\"\\,") != NULL ||
strpbrk(theme->waybar_font, "\"\\,") != NULL ||
strpbrk(theme->icon_theme, "/\\[]=") != NULL) {
fprintf(stderr, "cwal: invalid font or icon theme name\n");
return -1;
}
return 0;
}
static int load_template(const char *path, char **template_text)
{
FILE *file = NULL;
char *buffer = NULL;
size_t capacity = 4096;
size_t length = 0;
*template_text = NULL;
file = fopen(path, "r");
if (file == NULL) {
fprintf(stderr, "cwal: could not open template %s: %s\n", path,
strerror(errno));
return -1;
}
buffer = malloc(capacity);
if (buffer == NULL) {
fprintf(stderr, "cwal: could not allocate template buffer\n");
goto error;
}
for (;;) {
size_t available;
size_t read_count;
if (length == capacity - 1) {
char *resized;
size_t new_capacity;
if (capacity > (size_t)-1 / 2) {
fprintf(stderr, "cwal: template is too large\n");
goto error;
}
new_capacity = capacity * 2;
resized = realloc(buffer, new_capacity);
if (resized == NULL) {
fprintf(stderr,
"cwal: could not allocate template buffer\n");
goto error;
}
buffer = resized;
capacity = new_capacity;
}
available = capacity - length - 1;
read_count = fread(buffer + length, 1, available, file);
length += read_count;
if (read_count < available) {
if (ferror(file)) {
fprintf(stderr, "cwal: error reading template %s\n",
path);
goto error;
}
break;
}
}
buffer[length] = '\0';
if (fclose(file) != 0) {
file = NULL;
fprintf(stderr, "cwal: error closing template %s\n", path);
goto error;
}
*template_text = buffer;
return 0;
error:
if (file != NULL)
fclose(file);
free(buffer);
return -1;
}
static void print_usage(FILE *stream, const char *program)
{
fprintf(stream,
"Usage: %s <theme-file> <template-file>\n"
" %s --base <base-theme-file> <color-overrides-file> "
"<template-file>\n"
" %s --help\n"
"\n"
"Use - as the color-overrides file to read it from standard input.\n",
program, program, program);
}
int main(int argc, char **argv)
{
const char *theme_path;
const char *overrides_path = NULL;
const char *template_path;
char *template_text;
Theme theme;
int result;
if (argc == 2 && (strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-h") == 0)) {
print_usage(stdout, argv[0]);
return EXIT_SUCCESS;
}
if (argc == 3) {
theme_path = argv[1];
template_path = argv[2];
} else if (argc == 5 && strcmp(argv[1], "--base") == 0) {
theme_path = argv[2];
overrides_path = argv[3];
template_path = argv[4];
} else {
print_usage(stderr, argv[0]);
return EXIT_FAILURE;
}
initialize_theme(&theme);
if (load_theme(theme_path, &theme, 0) != 0 ||
(overrides_path != NULL &&
load_theme(overrides_path, &theme, 1) != 0) ||
validate_theme(&theme) != 0 ||
load_template(template_path, &template_text) != 0)
return EXIT_FAILURE;
result = write_template(stdout, template_text, &theme);
free(template_text);
if (result != 0 || fflush(stdout) != 0) {
fprintf(stderr, "cwal: error writing standard output\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|