forked from Elsa002/queercat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
772 lines (690 loc) · 25.9 KB
/
main.c
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
#define _XOPEN_SOURCE
/* *** Includes ******************************************************/
#include <stdbool.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <wchar.h>
#include <time.h>
#include "math.h"
/* *** Common ********************************************************/
/* Constants */
#define NEWLINE '\n'
#define ESCAPE_CHAR '\033'
/* Types */
typedef enum escape_state_e
{
ESCAPE_STATE_OUT = 0,
ESCAPE_STATE_IN,
ESCAPE_STATE_LAST
} escape_state_t;
/* Macros */
#define UNUSED(var) ((void)(var))
#define NEXT_CYCLIC_ELEMENT(array, index, array_size) \
(((index) + 1 == (array_size)) ? (array)[0] : (array)[((index) + 1)])
#define IS_LETTER(c) (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
// const FLAG_IDS
// TODO create array of IDs to flag names
/* *** Constants *****************************************************/
static char help_str[] = "\n"
"Usage: queerstyle [-f flag_number][-h horizontal_speed] [-v vertical_speed] [--] [FILES...]\n"
"\n"
"Concatenate FILE(s), or standard input, to standard output.\n"
"With no FILE, or when FILE is -, read standard input.\n"
"\n"
"--flag <d> , -f <d>: Choose colors to use:\n"
" [rainbow: 0, trans: 1, NB: 2, lesbian: 3,\n"
" gay: 4, pan: 5, bi: 6, genderfluid: 7, asexual: 8,\n"
" unlabeled: 9, aromantic: 10, aroace: 11]\n"
" default is rainbow (0)\n"
" TODO allow digit or phrase"
"--color-array=rgb,rgb,... TODO provide custom color array \n"
"--seed-value <d>, -s <d> TODO provide seed value for starting color point, primarily for\n"
" rainbow\n"
"--horizontal-frequency <d>, -h <d>: Horizontal rainbow frequency (default: 0.23)\n"
"--vertical-frequency <d>, -v <d>: Vertical rainbow frequency (default: 0.1)\n"
" --force-color, -F: Force color even when stdout is not a tty\n"
" --no-force-locale, -l: Use encoding from system locale instead of\n"
" assuming UTF-8\n"
" --random, -r: Random colors\n"
" --24bit, -b: Output in 24-bit \"true\" RGB mode (slower and\n"
" not supported by all terminals)\n"
" --version: Print version and exit\n"
" --help: Show this message\n"
"\n"
"Examples:\n"
" queerstyle f - g Output f's contents, then stdin, then g's contents.\n"
" queerstyle Copy standard input to standard output.\n"
" fortune | queerstyle Display a rainbow cookie.\n"
"\n"
"Report queerstyle bugs to <https://github.com/elsa002/queerstyle/issues>\n"
"queerstyle home page: <https://github.com/elsa002/queerstyle/>\n"
"base for code: <https://github.com/jaseg/lolcat/>\n"
"Original idea: <https://github.com/busyloop/lolcat/>\n";
#define MAX_FLAG_STRIPES (6)
#define MAX_ANSI_CODES_PER_STRIPE (5)
#define MAX_ANSI_CODES_COUNT (MAX_FLAG_STRIPES * MAX_ANSI_CODES_PER_STRIPE)
#define MAX_FLAG_NAME_LENGTH (64)
/* *** Types *********************************************************/
/* Colors. */
typedef uint32_t hex_color_t;
typedef unsigned char ansi_code_t;
typedef struct color_s
{
uint8_t red;
uint8_t green;
uint8_t blue;
} color_t;
/* Color type patterns. */
typedef enum color_type_e
{
COLOR_TYPE_INVALID = -1,
COLOR_TYPE_ANSI = 0,
COLOR_TYPE_24_BIT,
COLOR_TYPE_COUNT
} color_type_t;
typedef struct ansi_pattern_s
{
const unsigned int codes_count;
const unsigned char ansi_codes[MAX_ANSI_CODES_COUNT];
} ansi_pattern_t;
typedef struct color_pattern_s
{
const uint8_t stripes_count;
const uint32_t stripes_colors[MAX_FLAG_STRIPES];
const float factor;
} color_pattern_t;
/* Get color function. */
typedef void(get_color_f)(const color_pattern_t *color_pattern, float theta, color_t *color);
/* Pattern. */
typedef struct pattern_s
{
const char name[MAX_FLAG_NAME_LENGTH];
const ansi_pattern_t ansi_pattern;
const color_pattern_t color_pattern;
get_color_f *get_color;
} pattern_t;
/* Patterns enum. */
typedef enum flag_type_e
{
FLAG_TYPE_INVALID = -1,
FLAG_TYPE_RAINBOW = 0,
FLAG_TYPE_TRANS,
FLAG_TYPE_NB,
FLAG_TYPE_LESBIAN,
FLAG_TYPE_GAY,
FLAG_TYPE_PAN,
FLAG_TYPE_BI,
FLAG_TYPE_GENDERFLUID,
FLAG_TYPE_ASEXUAL,
FLAG_TYPE_UNLABELED,
FLAG_TYPE_AROMANTIC,
FLAG_TYPE_AROACE,
FLAG_TYPE_END
} flag_type_t;
/* *** Pattern Functions *********************************************/
get_color_f get_color_rainbow;
get_color_f get_color_stripes;
/* *** Flags *********************************************************/
const pattern_t rainbow = {
.name = "rainbow",
.ansi_pattern = {
.codes_count = 30,
.ansi_codes = {39, 38, 44, 43, 49, 48, 84, 83, 119, 118, 154, 148, 184, 178,
214, 208, 209, 203, 204, 198, 199, 163, 164, 128, 129, 93, 99, 63, 69, 33}},
.color_pattern = {0},
.get_color = get_color_rainbow};
const pattern_t transgender = {
.name = "transgender",
.ansi_pattern = {
.codes_count = 10,
.ansi_codes = {117, 117, 225, 225, 255, 255, 225, 225, 117, 117}},
.color_pattern = {.stripes_count = 5, .stripes_colors = {
0x55cdfc, /* #55cdfd - Blue */
0xf7a8b8, /* #f7a8b8 - Pink */
0xffffff, /* #ffffff - White */
0xf7a8b8, /* #f7a8b8 - Pink */
0x55cdfc /* #55cdfc - Blue */
},
.factor = 4.0f},
.get_color = get_color_stripes};
const pattern_t nonbinary = {
.name = "nonbinary",
.ansi_pattern = {
.codes_count = 8,
.ansi_codes = {226, 226, 255, 255, 93, 93, 234, 234}},
.color_pattern = {.stripes_count = 4, .stripes_colors = {
0xffff00, /* #ffff00 - Yellow */
0xb000ff, /* #b000ff - Purple */
0xffffff, /* #ffffff - White */
0x000000 /* #000000 - Black */
},
.factor = 4.0f},
.get_color = get_color_stripes};
const pattern_t lesbian = {
.name = "lesbian",
.ansi_pattern = {
.codes_count = 5,
.ansi_codes = {196, 208, 255, 170, 128}},
.color_pattern = {.stripes_count = 5, .stripes_colors = {
0xff0000, /* #ff0000 - Red */
0xff993f, /* #ff993f - Orange */
0xffffff, /* #ffffff - White */
0xff8cbd, /* #ff8cbd - Pink */
0xff4284 /* #ff4284 - Purple */
},
.factor = 2.0f},
.get_color = get_color_stripes};
const pattern_t gay = {
.name = "gay",
.ansi_pattern = {
.codes_count = 7,
.ansi_codes = {36, 49, 121, 255, 117, 105, 92}},
.color_pattern = {.stripes_count = 5, .stripes_colors = {
0x00b685, /* #00b685 - Teal */
0x6bffb6, /* #6bffb6 - Green */
0xffffff, /* #ffffff - White */
0x8be1ff, /* #8be1ff - Blue */
0x8e1ae1 /* #8e1ae1 - Purple */
},
.factor = 6.0f},
.get_color = get_color_stripes};
const pattern_t pansexual = {
.name = "pansexual",
.ansi_pattern = {
.codes_count = 9,
.ansi_codes = {200, 200, 200, 227, 227, 227, 45, 45, 45}},
.color_pattern = {.stripes_count = 3, .stripes_colors = {
0xff3388, /* #ff3388 - Pink */
0xffea00, /* #ffea00 - Yellow */
0x00dbff /* #00dbff - Cyan */
},
.factor = 8.0f},
.get_color = get_color_stripes};
const pattern_t bisexual = {
.name = "bisexual",
.ansi_pattern = {
.codes_count = 8,
.ansi_codes = {162, 162, 162, 129, 129, 27, 27, 27}},
.color_pattern = {.stripes_count = 5, .stripes_colors = {
0xff3b7b, /* #ff3b7b - Pink */
0xff3b7b, /* #ff3b7b - Pink */
0xd06bcc, /* #d06bcc - Purple */
0x3b72ff, /* #3b72ff - Blue */
0x3b72ff /* #3b72ff - Blue */
},
.factor = 4.0f},
.get_color = get_color_stripes};
const pattern_t gender_fluid = {
.name = "gender_fluid",
.ansi_pattern = {
.codes_count = 10,
.ansi_codes = {219, 219, 255, 255, 128, 128, 234, 234, 20, 20}},
.color_pattern = {.stripes_count = 5, .stripes_colors = {
0xffa0bc, /* #ffa0bc - Pink */
0xffffff, /* #ffffff - White */
0xc600e4, /* #c600e4 - Purple */
0x000000, /* #000000 - Black */
0x4e3cbb /* #4e3cbb - Blue */
},
.factor = 2.0f},
.get_color = get_color_stripes};
const pattern_t asexual = {
.name = "asexual",
.ansi_pattern = {
.codes_count = 8,
.ansi_codes = {233, 233, 247, 247, 255, 255, 5, 5}},
.color_pattern = {.stripes_count = 4, .stripes_colors = {
0x000000, /* #000000 - Black */
0xa3a3a3, /* #a3a3a3 - Gray */
0xffffff, /* #ffffff - White */
0x800080 /* #800080 - Purple */
},
.factor = 4.0f},
.get_color = get_color_stripes};
const pattern_t unlabeled = {
.name = "unlabeled",
.ansi_pattern = {
.codes_count = 8,
.ansi_codes = {194, 194, 255, 255, 195, 195, 223, 223}},
.color_pattern = {.stripes_count = 4, .stripes_colors = {
0xe6f9e3, /* #e6f9e3 - Green */
0xfdfdfb, /* #fdfdfb - White */
0xdeeff9, /* #deeff9 - Blue */
0xfae1c2 /* #fae1c2 - Orange */
},
.factor = 4.0f},
.get_color = get_color_stripes};
const pattern_t aromantic = {
.name = "aromantic",
.ansi_pattern = {
.codes_count = 10,
.ansi_codes = {
34, 34,
120, 120,
255, 255,
247, 247,
233, 233}},
.color_pattern = {.stripes_count = 5, .stripes_colors = {
0x3da542, /* #3da542 - Green */
0xa8d379, /* #a8d379 - Light green */
0xffffff, /* #ffffff - White */
0xa9a9a9, /* #a9a9a9 - Grey */
0x000000 /* #000000 - Black */
},
.factor = 1.0f},
.get_color = get_color_stripes};
const pattern_t aroace = {
.name = "aroace",
.ansi_pattern = {
.codes_count = 10,
.ansi_codes = {
208, 208,
220, 220,
255, 255,
75, 75,
62, 62},
},
.color_pattern = {.stripes_count = 5, .stripes_colors = {
0xe28d00, /* #e28d00 - Orange */
0xeccd00, /* #eccd00 - Yellow */
0xffffff, /* #ffffff - White */
0x62afdd, /* #62afdd - Light blue */
0x203756 /* #203756 - Blue */
},
.factor = 1.0f},
.get_color = get_color_stripes};
/* *** Functions Declarations ****************************************/
/* Info */
static void usage(void);
static void version(void);
/* Helpers */
static void find_escape_sequences(wint_t current_char, escape_state_t *state);
static wint_t help_str_hack(FILE *_ignored);
/* Colors handling */
static const pattern_t *get_pattern(flag_type_t flag_type);
static void mix_colors(uint32_t color1, uint32_t color2, float balance, float factor, color_t *output_color);
static void print_color(const pattern_t *pattern, color_type_t color_type, int char_index, int line_index, double freq_h, double freq_v, double off_x, int rand_offset, int cc);
/* *** Functions *****************************************************/
static void usage(void)
{
wprintf(L"Usage: queerstyle");
wprintf(L" [-h horizontal_speed]");
wprintf(L" [-v vertical_speed]");
wprintf(L" [-f (# or flag name)]");
wprintf(L" [-s (# for seed start point]");
wprintf(L" [--] [FILES...]\n");
exit(1);
}
static void version(void)
{
wprintf(L"queerstyle version 0.69, (c) 2022 Tallicia\n");
exit(0);
}
static void find_escape_sequences(wint_t current_char, escape_state_t *state)
{
if (current_char == '\033')
{
*state = ESCAPE_STATE_IN;
}
else if (*state == ESCAPE_STATE_IN)
{
*state = IS_LETTER(current_char) ? ESCAPE_STATE_LAST : ESCAPE_STATE_IN;
}
else
{
*state = ESCAPE_STATE_OUT;
}
}
static wint_t help_str_hack(FILE *_ignored)
{
(void)_ignored;
static size_t idx = 0;
char c = help_str[idx++];
if (c)
return c;
idx = 0;
return WEOF;
}
static const flag_type_t get_string_flag_enum(char *flag_name)
{
flag_type_t ret = FLAG_TYPE_RAINBOW;
if (strcmp("rainbow", flag_name))
{
ret = FLAG_TYPE_RAINBOW;
}
else if (strcmp("trans", flag_name) || strcmp("transgender", flag_name))
{
ret = FLAG_TYPE_TRANS;
}
// Todo Convert the strings to the
return ret;
}
static const pattern_t *get_pattern(flag_type_t flag_type)
{
switch (flag_type)
{
case FLAG_TYPE_RAINBOW:
return &rainbow;
case FLAG_TYPE_TRANS:
return &transgender;
case FLAG_TYPE_NB:
return &nonbinary;
case FLAG_TYPE_LESBIAN:
return &lesbian;
case FLAG_TYPE_GAY:
return &gay;
case FLAG_TYPE_PAN:
return &pansexual;
case FLAG_TYPE_BI:
return &bisexual;
case FLAG_TYPE_GENDERFLUID:
return &gender_fluid;
case FLAG_TYPE_ASEXUAL:
return &asexual;
case FLAG_TYPE_UNLABELED:
return &unlabeled;
case FLAG_TYPE_AROMANTIC:
return &aromantic;
case FLAG_TYPE_AROACE:
return &aroace;
default:
return NULL;
}
}
static void mix_colors(uint32_t color1, uint32_t color2, float balance, float factor, color_t *output_color)
{
uint8_t red_1 = (color1 & 0xff0000) >> 16;
uint8_t green_1 = (color1 & 0x00ff00) >> 8;
uint8_t blue_1 = (color1 & 0x0000ff) >> 0;
uint8_t red_2 = (color2 & 0xff0000) >> 16;
uint8_t green_2 = (color2 & 0x00ff00) >> 8;
uint8_t blue_2 = (color2 & 0x0000ff) >> 0;
balance = pow(balance, factor);
output_color->red = lrintf(red_1 * balance + red_2 * (1.0f - balance));
output_color->green = lrintf(green_1 * balance + green_2 * (1.0f - balance));
output_color->blue = lrintf(blue_1 * balance + blue_2 * (1.0f - balance));
}
void get_color_rainbow(const color_pattern_t *color_pattern, float theta, color_t *color)
{
/* Unused variables. */
UNUSED(color_pattern);
/* Get theta in range. */
while (theta < 0)
theta += 2.0f * (float)M_PI;
while (theta >= 2.0f * (float)M_PI)
theta -= 2.0f * (float)M_PI;
/* Generate the color. */
color->red = lrintf((1.0f * (0.5f + 0.5f * sin(theta + 0))) * 255.0f);
color->green = lrintf((1.0f * (0.5f + 0.5f * sin(theta + 2 * M_PI / 3))) * 255.0f);
color->blue = lrintf((1.0f * (0.5f + 0.5f * sin(theta + 4 * M_PI / 3))) * 255.0f);
}
void get_color_stripes(const color_pattern_t *color_pattern, float theta, color_t *color)
{
/* Get theta in range. */
while (theta < 0)
theta += 2.0f * (float)M_PI;
while (theta >= 2.0f * (float)M_PI)
theta -= 2.0f * (float)M_PI;
/* Find the stripe based on theta and generate the color. */
for (int i = 0; i < color_pattern->stripes_count; i++)
{
float stripe_size = (2.0f * M_PI) / color_pattern->stripes_count;
float min_theta = i * stripe_size;
float max_theta = (i + 1) * stripe_size;
if (min_theta <= theta && max_theta > theta)
{
float balance = 1 - ((theta - min_theta) / stripe_size);
mix_colors(
color_pattern->stripes_colors[i],
NEXT_CYCLIC_ELEMENT(color_pattern->stripes_colors, i, color_pattern->stripes_count),
balance,
color_pattern->factor,
color);
return;
}
}
}
static void print_color(const pattern_t *pattern, color_type_t color_type, int char_index, int line_index, double freq_h, double freq_v, double off_x, int rand_offset, int cc)
{
float theta;
color_t color = {0};
int ncc;
switch (color_type)
{
case COLOR_TYPE_24_BIT:
theta = char_index * freq_h / 5.0f + line_index * freq_v + (off_x + 2.0f * rand_offset / (float)RAND_MAX) * M_PI;
pattern->get_color(&pattern->color_pattern, theta, &color);
wprintf(L"\033[38;2;%d;%d;%dm", color.red, color.green, color.blue);
break;
case COLOR_TYPE_ANSI:
ncc = off_x * pattern->ansi_pattern.codes_count + (int)(char_index * freq_h + line_index * freq_v);
if (cc != ncc)
wprintf(L"\033[38;5;%hhum", pattern->ansi_pattern.ansi_codes[(rand_offset + (cc = ncc)) % pattern->ansi_pattern.codes_count]);
break;
default:
exit(1);
}
}
int main(int argc, char **argv)
{
char *default_argv[] = {"-"};
int cc = -1;
int i = 0;
int char_index = 0;
int line_index = 0;
wint_t current_char = '\0';
bool print_colors = isatty(STDOUT_FILENO);
bool force_locale = true;
bool random = false;
color_type_t color_type = COLOR_TYPE_ANSI;
double freq_h = 0.23;
double freq_v = 0.1;
flag_type_t flag_type = FLAG_TYPE_RAINBOW;
const pattern_t *pattern;
struct timeval tv;
gettimeofday(&tv, NULL);
double off_x = (tv.tv_sec % 300) / 300.0;
/* Handle flags. */
for (i = 1; i < argc; i++)
{
char *endptr;
if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--flag"))
{
if ((++i) < argc)
{
if (strspn(argv[i], "0123456789") == strlen(argv[i]))
{
size_t big_digit = 0;
sscanf(argv[i], "%zu%*c", &big_digit);
flag_type = (flag_type_t)strtod(argv[i], &endptr);
}
else
{
flag_type = get_string_flag_enum(argv[i]);
}
if (*endptr)
usage();
}
else
{
usage();
}
}
else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--color-array"))
{
}
else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--seed"))
{
}
else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--horizontal-frequency"))
{
if ((++i) < argc)
{
freq_h = strtod(argv[i], &endptr);
if (*endptr)
usage();
}
else
{
usage();
}
}
else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--vertical-frequency"))
{
if ((++i) < argc)
{
freq_v = strtod(argv[i], &endptr);
if (*endptr)
usage();
}
else
{
usage();
}
}
else if (!strcmp(argv[i], "-F") || !strcmp(argv[i], "--force-color"))
{
print_colors = true;
}
else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--no-force-locale"))
{
force_locale = false;
}
else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--random"))
{
random = true;
}
else if (!strcmp(argv[i], "-b") || !strcmp(argv[i], "--24bit"))
{
color_type = COLOR_TYPE_24_BIT;
}
else if (!strcmp(argv[i], "--version"))
{
version();
}
else
{
if (!strcmp(argv[i], "--"))
i++;
break;
}
}
if (flag_type <= FLAG_TYPE_INVALID || flag_type >= FLAG_TYPE_END)
{
fprintf(stderr, "Invalid flag: %d\n", flag_type);
exit(1);
}
/* Handle randomness. */
int rand_offset = 0;
if (random)
{
srand(time(NULL));
rand_offset = rand();
}
/* Get inputs. */
char **inputs = argv + i;
char **inputs_end = argv + argc;
if (inputs == inputs_end)
{
inputs = default_argv;
inputs_end = inputs + 1;
}
/* Handle locale. */
char *env_lang = getenv("LANG");
if (force_locale && env_lang && !strstr(env_lang, "UTF-8"))
{
if (!setlocale(LC_ALL, "C.UTF-8"))
{ /* C.UTF-8 may not be available on all platforms */
setlocale(LC_ALL, ""); /* Let's hope for the best */
}
}
else
{
setlocale(LC_ALL, "");
}
/* Get pattern. */
pattern = get_pattern(flag_type);
/* For file in inputs. */
for (char **filename = inputs; filename < inputs_end; filename++)
{
wint_t (*this_file_read_wchar)(FILE *); /* Used for --help because fmemopen is universally broken when used with fgetwc */
FILE *f;
escape_state_t escape_state = ESCAPE_STATE_OUT;
/* Handle "--help", "-" (STDIN) and file names. */
if (!strcmp(*filename, "--help"))
{
this_file_read_wchar = &help_str_hack;
f = 0;
}
else if (!strcmp(*filename, "-"))
{
this_file_read_wchar = &fgetwc;
f = stdin;
}
else
{
this_file_read_wchar = &fgetwc;
f = fopen(*filename, "r");
if (!f)
{
fwprintf(stderr, L"Cannot open input file \"%s\": %s\n", *filename, strerror(errno));
return 2;
}
}
/* While there are chars to read. */
while ((current_char = this_file_read_wchar(f)) != WEOF)
{
/* If set to print colors, handle the colors. */
if (print_colors)
{
/* Skip escape sequences. */
find_escape_sequences(current_char, &escape_state);
if (escape_state == ESCAPE_STATE_OUT)
{
/* Handle newlines. */
if (current_char == '\n')
{
line_index++;
char_index = 0;
}
else
{
char_index += wcwidth(current_char);
print_color(pattern, color_type, char_index, line_index, freq_h, freq_v, off_x, rand_offset, cc);
}
}
}
/* Print the char. */
putwchar(current_char);
if (escape_state == ESCAPE_STATE_LAST)
{ /* implies "print_colors" */
print_color(pattern, color_type, char_index, line_index, freq_h, freq_v, off_x, rand_offset, cc);
}
}
if (print_colors)
wprintf(L"\033[0m");
cc = -1;
if (f)
{
if (ferror(f))
{
fwprintf(stderr, L"Error reading input file \"%s\": %s\n", *filename, strerror(errno));
fclose(f);
return 2;
}
if (fclose(f))
{
fwprintf(stderr, L"Error closing input file \"%s\": %s\n", *filename, strerror(errno));
return 2;
}
}
}
}