]> git.refcnt.org Git - colorize.git/blob - colorize.c
Guard against fallthrough in switch statement
[colorize.git] / colorize.c
1 /*
2 * colorize - Read text from standard input stream or file and print
3 * it colorized through use of ANSI escape sequences
4 *
5 * Copyright (c) 2011-2019 Steven Schubiger
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #define _DEFAULT_SOURCE
23 #define _BSD_SOURCE
24 #define _XOPEN_SOURCE 700
25 #define _FILE_OFFSET_BITS 64
26 #include <assert.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <pwd.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <time.h>
39 #include <unistd.h>
40 #include <wordexp.h>
41
42 #ifndef DEBUG
43 # define DEBUG 0
44 #endif
45
46 #define str(arg) #arg
47 #define to_str(arg) str(arg)
48
49 #define streq(s1, s2) (strcmp (s1, s2) == 0)
50 #define strneq(s1, s2, n) (strncmp (s1, s2, n) == 0)
51
52 #if !DEBUG
53 # define xmalloc(size) malloc_wrap(size)
54 # define xcalloc(nmemb, size) calloc_wrap(nmemb, size)
55 # define xrealloc(ptr, size) realloc_wrap(ptr, size)
56 # define xstrdup(str) strdup_wrap(str, NULL, 0)
57 # define str_concat(str1, str2) str_concat_wrap(str1, str2, NULL, 0)
58 #else
59 # define xmalloc(size) malloc_wrap_debug(size, __FILE__, __LINE__)
60 # define xcalloc(nmemb, size) calloc_wrap_debug(nmemb, size, __FILE__, __LINE__)
61 # define xrealloc(ptr, size) realloc_wrap_debug(ptr, size, __FILE__, __LINE__)
62 # define xstrdup(str) strdup_wrap(str, __FILE__, __LINE__)
63 # define str_concat(str1, str2) str_concat_wrap(str1, str2, __FILE__, __LINE__)
64 #endif
65
66 #define free_null(ptr) free_wrap((void **)&ptr)
67
68 #if defined(BUF_SIZE) && (BUF_SIZE <= 0 || BUF_SIZE > 65536)
69 # undef BUF_SIZE
70 #endif
71 #ifndef BUF_SIZE
72 # define BUF_SIZE 4096
73 #endif
74
75 #define LF 0x01
76 #define CR 0x02
77
78 #define COUNT_OF(obj, type) (sizeof (obj) / sizeof (type))
79
80 #define SKIP_LINE_ENDINGS(flags) ((flags) == (CR|LF) ? 2 : 1)
81
82 #define VALID_FILE_TYPE(mode) (S_ISREG (mode) || S_ISLNK (mode) || S_ISFIFO (mode))
83
84 #define STACK_VAR(ptr) do { \
85 stack (&vars_list, &stacked_vars, stacked_vars, ptr, IS_GENERIC); \
86 } while (false)
87 #define STACK_FILE(ptr) do { \
88 stack (&vars_list, &stacked_vars, stacked_vars, ptr, IS_FILE); \
89 } while (false)
90 #define RELEASE(ptr) do { \
91 release (vars_list, stacked_vars, (void **)&ptr); \
92 } while (false)
93
94 #if !DEBUG
95 # define MEM_ALLOC_FAIL() do { \
96 fprintf (stderr, "%s: memory allocation failure\n", program_name); \
97 exit (EXIT_FAILURE); \
98 } while (false)
99 #else
100 # define MEM_ALLOC_FAIL_DEBUG(file, line) do { \
101 fprintf (stderr, "Memory allocation failure in source file %s, line %u\n", file, line); \
102 exit (EXIT_FAILURE); \
103 } while (false)
104 #endif
105
106 #define ABORT_TRACE() \
107 fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \
108 abort ();
109
110 #define CHECK_COLORS_RANDOM(color1, color2) \
111 streq (color_names[color1]->name, "random") \
112 && (streq (color_names[color2]->name, "none") \
113 || streq (color_names[color2]->name, "default"))
114
115 #define ALLOC_COMPLETE_PART_LINE 8
116
117 #if defined(COLOR_SEP_CHAR_COLON)
118 # define COLOR_SEP_CHAR ':'
119 #elif defined(COLOR_SEP_CHAR_SLASH)
120 # define COLOR_SEP_CHAR '/'
121 #else
122 # define COLOR_SEP_CHAR '/'
123 #endif
124
125 #define CONF_FILE ".colorize.conf"
126
127 #if DEBUG
128 # define DEBUG_FILE "debug.txt"
129 #endif
130
131 #define MAX_ATTRIBUTE_CHARS (6 * 2)
132
133 #define PROGRAM_NAME "colorize"
134
135 #define VERSION "0.65"
136
137 typedef enum { false, true } bool;
138
139 struct conf {
140 char *attr;
141 char *color;
142 char *exclude_random;
143 char *omit_color_empty;
144 };
145
146 enum { DESC_OPTION, DESC_CONF };
147
148 struct color_name {
149 char *name;
150 char *orig;
151 };
152
153 struct color {
154 const char *name;
155 const char *code;
156 };
157
158 static const struct color fg_colors[] = {
159 { "none", NULL },
160 { "black", "30m" },
161 { "red", "31m" },
162 { "green", "32m" },
163 { "yellow", "33m" },
164 { "blue", "34m" },
165 { "magenta", "35m" },
166 { "cyan", "36m" },
167 { "white", "37m" },
168 { "default", "39m" },
169 };
170 static const struct color bg_colors[] = {
171 { "none", NULL },
172 { "black", "40m" },
173 { "red", "41m" },
174 { "green", "42m" },
175 { "yellow", "43m" },
176 { "blue", "44m" },
177 { "magenta", "45m" },
178 { "cyan", "46m" },
179 { "white", "47m" },
180 { "default", "49m" },
181 };
182
183 struct bytes_size {
184 unsigned int size;
185 char unit;
186 };
187
188 enum {
189 FMT_GENERIC,
190 FMT_STRING,
191 FMT_QUOTE,
192 FMT_COLOR,
193 FMT_RANDOM,
194 FMT_ERROR,
195 FMT_FILE,
196 FMT_TYPE,
197 FMT_CONF,
198 FMT_CONF_FILE
199 };
200 static const char *formats[] = {
201 "%s", /* generic */
202 "%s '%s'", /* string */
203 "%s `%s' %s", /* quote */
204 "%s color '%s' %s", /* color */
205 "%s color '%s' %s '%s'", /* random */
206 "less than %lu bytes %s", /* error */
207 "%s: %s", /* file */
208 "%s: %s: %s", /* type */
209 "%s: option '%s' %s", /* conf */
210 "config file %s: %s" /* conf file */
211 };
212
213 enum { GENERIC, FOREGROUND = 0, BACKGROUND };
214
215 static const struct {
216 const struct color *entries;
217 unsigned int count;
218 const char *desc;
219 } tables[] = {
220 { fg_colors, COUNT_OF (fg_colors, struct color), "foreground" },
221 { bg_colors, COUNT_OF (bg_colors, struct color), "background" },
222 };
223
224 static unsigned int opts_set;
225 enum opt_set {
226 OPT_ATTR_SET = 0x01,
227 OPT_EXCLUDE_RANDOM_SET = 0x02,
228 OPT_OMIT_COLOR_EMPTY_SET = 0x04
229 };
230 static struct {
231 char *attr;
232 char *exclude_random;
233 } opts_arg = { NULL, NULL };
234
235 enum {
236 OPT_ATTR = 1,
237 OPT_CLEAN,
238 OPT_CLEAN_ALL,
239 OPT_CONFIG,
240 OPT_EXCLUDE_RANDOM,
241 OPT_OMIT_COLOR_EMPTY,
242 OPT_HELP,
243 OPT_VERSION
244 };
245 static int opt_type;
246 static const struct option long_opts[] = {
247 { "attr", required_argument, &opt_type, OPT_ATTR },
248 { "clean", no_argument, &opt_type, OPT_CLEAN },
249 { "clean-all", no_argument, &opt_type, OPT_CLEAN_ALL },
250 { "config", required_argument, &opt_type, OPT_CONFIG },
251 { "exclude-random", required_argument, &opt_type, OPT_EXCLUDE_RANDOM },
252 { "omit-color-empty", no_argument, &opt_type, OPT_OMIT_COLOR_EMPTY },
253 { "help", no_argument, &opt_type, OPT_HELP },
254 { "version", no_argument, &opt_type, OPT_VERSION },
255 { NULL, 0, NULL, 0 },
256 };
257
258 enum attr_type {
259 ATTR_BOLD = 0x01,
260 ATTR_UNDERSCORE = 0x02,
261 ATTR_BLINK = 0x04,
262 ATTR_REVERSE = 0x08,
263 ATTR_CONCEALED = 0x10
264 };
265 struct attr {
266 const char *name;
267 unsigned int val;
268 enum attr_type type;
269 };
270
271 enum var_type {
272 IS_GENERIC,
273 IS_FILE,
274 IS_UNUSED
275 };
276 struct var_list {
277 void *ptr;
278 enum var_type type;
279 };
280
281 static FILE *stream;
282 #if DEBUG
283 static FILE *log;
284 #endif
285
286 static unsigned int stacked_vars;
287 static struct var_list *vars_list;
288
289 static bool clean;
290 static bool clean_all;
291 static bool omit_color_empty;
292
293 static char attr[MAX_ATTRIBUTE_CHARS + 1];
294 static char *exclude;
295
296 static const char *program_name;
297
298 #if DEBUG
299 static void print_tstamp (FILE *);
300 #endif
301 static void process_opts (int, char **, char **);
302 static void conf_file_path (char **);
303 static void process_opt_attr (const char *, const bool);
304 static void write_attr (const struct attr *, unsigned int *, const bool);
305 static void process_opt_exclude_random (const char *, const bool);
306 static void parse_conf (const char *, struct conf *);
307 static void assign_conf (const char *, struct conf *, const char *, char *);
308 static void init_conf_vars (const struct conf *);
309 static void init_opts_vars (void);
310 static void print_hint (void);
311 static void print_help (void);
312 static void print_version (void);
313 static void cleanup (void);
314 static void free_color_names (struct color_name **);
315 static void free_conf (struct conf *);
316 static void process_args (unsigned int, char **, char *, const struct color **, const char **, FILE **, struct conf *);
317 static void process_file_arg (const char *, const char **, FILE **);
318 static bool skip_path_colors (const char *, const char *, const struct stat *, const bool);
319 static void gather_color_names (const char *, char *, struct color_name **);
320 static void read_print_stream (const char *, const struct color **, const char *, FILE *);
321 static void merge_print_line (const char *, const char *, FILE *);
322 static void complete_part_line (const char *, char **, FILE *);
323 static bool get_next_char (char *, const char **, FILE *, bool *);
324 static void save_char (char, char **, size_t *, size_t *);
325 static void find_color_entries (struct color_name **, const struct color **);
326 static void find_color_entry (const struct color_name *, unsigned int, const struct color **);
327 static void print_line (const char *, const struct color **, const char * const, unsigned int, bool);
328 static void print_clean (const char *);
329 static bool is_esc (const char *);
330 static const char *get_end_of_esc (const char *);
331 static const char *get_end_of_text (const char *);
332 static void print_text (const char *, size_t);
333 static bool gather_esc_offsets (const char *, const char **, const char **);
334 static bool validate_esc_clean_all (const char **);
335 static bool validate_esc_clean (int, unsigned int, unsigned int *, const char **, bool *);
336 static bool is_reset (int, unsigned int, const char **);
337 static bool is_attr (int, unsigned int, unsigned int, const char **);
338 static bool is_fg_color (int, const char **);
339 static bool is_bg_color (int, unsigned int, const char **);
340 #if !DEBUG
341 static void *malloc_wrap (size_t);
342 static void *calloc_wrap (size_t, size_t);
343 static void *realloc_wrap (void *, size_t);
344 #else
345 static void *malloc_wrap_debug (size_t, const char *, unsigned int);
346 static void *calloc_wrap_debug (size_t, size_t, const char *, unsigned int);
347 static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
348 #endif
349 static void free_wrap (void **);
350 static char *strdup_wrap (const char *, const char *, unsigned int);
351 static char *str_concat_wrap (const char *, const char *, const char *, unsigned int);
352 static char *expand_string (const char *);
353 static bool get_bytes_size (unsigned long, struct bytes_size *);
354 static char *get_file_type (mode_t);
355 static bool has_color_name (const char *, const char *);
356 static FILE *open_file (const char *, const char *);
357 static void vfprintf_diag (const char *, ...);
358 static void vfprintf_fail (const char *, ...);
359 static void stack (struct var_list **, unsigned int *, unsigned int, void *, enum var_type);
360 static void release (struct var_list *, unsigned int, void **);
361
362 extern int optind;
363
364 int
365 main (int argc, char **argv)
366 {
367 unsigned int arg_cnt;
368
369 const struct color *colors[2] = {
370 NULL, /* foreground */
371 NULL, /* background */
372 };
373
374 const char *file = NULL;
375
376 char *conf_file = NULL;
377 struct conf config = { NULL, NULL, NULL, NULL };
378
379 program_name = argv[0];
380 atexit (cleanup);
381
382 setvbuf (stdout, NULL, _IOLBF, 0);
383
384 #if DEBUG
385 log = open_file (DEBUG_FILE, "w");
386 STACK_FILE (log);
387 print_tstamp (log);
388 #endif
389
390 attr[0] = '\0';
391
392 process_opts (argc, argv, &conf_file);
393
394 #ifdef CONF_FILE_TEST
395 conf_file = to_str (CONF_FILE_TEST);
396 #elif !defined(TEST)
397 if (conf_file == NULL)
398 {
399 conf_file_path (&conf_file);
400 STACK_VAR (conf_file);
401 }
402 else
403 {
404 char *s;
405 if ((s = expand_string (conf_file)))
406 {
407 free (conf_file);
408 conf_file = s;
409 }
410 STACK_VAR (conf_file);
411 errno = 0;
412 if (access (conf_file, F_OK) == -1)
413 vfprintf_fail (formats[FMT_CONF_FILE], conf_file, strerror (errno));
414 }
415 #endif
416 #if defined(CONF_FILE_TEST) || !defined(TEST)
417 if (access (conf_file, F_OK) != -1)
418 parse_conf (conf_file, &config);
419 #endif
420 #if !defined(CONF_FILE_TEST) && !defined(TEST)
421 RELEASE (conf_file);
422 #endif
423 init_conf_vars (&config);
424
425 init_opts_vars ();
426
427 arg_cnt = argc - optind;
428
429 if (clean || clean_all)
430 {
431 if (clean && clean_all)
432 vfprintf_fail (formats[FMT_GENERIC], "--clean and --clean-all switch are mutually exclusive");
433 if (arg_cnt > 1)
434 vfprintf_fail ("--clean%s switch cannot be used with more than one file", clean_all ? "-all" : "");
435 {
436 unsigned int i;
437 const struct option_set {
438 const char *option;
439 enum opt_set set;
440 } options[] = {
441 { "attr", OPT_ATTR_SET },
442 { "exclude-random", OPT_EXCLUDE_RANDOM_SET },
443 { "omit-color-empty", OPT_OMIT_COLOR_EMPTY_SET },
444 };
445 for (i = 0; i < COUNT_OF (options, struct option_set); i++)
446 if (opts_set & options[i].set)
447 vfprintf_diag ("--%s switch has no meaning with --clean%s", options[i].option, clean_all ? "-all" : "");
448 }
449 }
450 else
451 {
452 if (arg_cnt == 0 || arg_cnt > 2)
453 {
454 vfprintf_diag ("%u arguments provided, expected 1-2 arguments or clean option", arg_cnt);
455 print_hint ();
456 exit (EXIT_FAILURE);
457 }
458 }
459
460 if (clean || clean_all)
461 process_file_arg (argv[optind], &file, &stream);
462 else
463 process_args (arg_cnt, &argv[optind], &attr[0], colors, &file, &stream, &config);
464 read_print_stream (&attr[0], colors, file, stream);
465
466 free_conf (&config);
467
468 RELEASE (exclude);
469
470 exit (EXIT_SUCCESS);
471 }
472
473 #if DEBUG
474 static void
475 print_tstamp (FILE *log)
476 {
477 time_t t;
478 struct tm *tm;
479 char str[128];
480 size_t written;
481
482 t = time (NULL);
483 tm = localtime (&t);
484 if (tm == NULL)
485 {
486 perror ("localtime");
487 exit (EXIT_FAILURE);
488 }
489 written = strftime (str, sizeof (str), "%Y-%m-%d %H:%M:%S %Z", tm);
490 if (written == 0)
491 vfprintf_fail (formats[FMT_GENERIC], "strftime: 0 returned");
492
493 fprintf (log, "%s\n", str);
494 while (written--)
495 fprintf (log, "=");
496 fprintf (log, "\n");
497 }
498 #endif
499
500 #define DUP_CONFIG() \
501 *conf_file = xstrdup (optarg); \
502 break;
503
504 #define PRINT_HELP_EXIT() \
505 print_help (); \
506 exit (EXIT_SUCCESS);
507
508 #define PRINT_VERSION_EXIT() \
509 print_version (); \
510 exit (EXIT_SUCCESS);
511
512 extern char *optarg;
513
514 static void
515 process_opts (int argc, char **argv, char **conf_file)
516 {
517 int opt;
518 while ((opt = getopt_long (argc, argv, "c:hV", long_opts, NULL)) != -1)
519 {
520 switch (opt)
521 {
522 case 0: /* long opts */
523 switch (opt_type)
524 {
525 case OPT_ATTR:
526 opts_set |= OPT_ATTR_SET;
527 opts_arg.attr = xstrdup (optarg);
528 STACK_VAR (opts_arg.attr);
529 break;
530 case OPT_CLEAN:
531 clean = true;
532 break;
533 case OPT_CLEAN_ALL:
534 clean_all = true;
535 break;
536 case OPT_CONFIG:
537 DUP_CONFIG ();
538 case OPT_EXCLUDE_RANDOM:
539 opts_set |= OPT_EXCLUDE_RANDOM_SET;
540 opts_arg.exclude_random = xstrdup (optarg);
541 STACK_VAR (opts_arg.exclude_random);
542 break;
543 case OPT_OMIT_COLOR_EMPTY:
544 opts_set |= OPT_OMIT_COLOR_EMPTY_SET;
545 break;
546 case OPT_HELP:
547 PRINT_HELP_EXIT ();
548 case OPT_VERSION:
549 PRINT_VERSION_EXIT ();
550 default: /* never reached */
551 ABORT_TRACE ();
552 }
553 break;
554 case 'c':
555 DUP_CONFIG ();
556 case 'h':
557 PRINT_HELP_EXIT ();
558 case 'V':
559 PRINT_VERSION_EXIT ();
560 case '?':
561 print_hint ();
562 exit (EXIT_FAILURE);
563 default: /* never reached */
564 ABORT_TRACE ();
565 }
566 }
567 }
568
569 static void
570 conf_file_path (char **conf_file)
571 {
572 char *path;
573 uid_t uid;
574 struct passwd *passwd;
575 size_t size;
576
577 uid = getuid ();
578 errno = 0;
579 if ((passwd = getpwuid (uid)) == NULL)
580 {
581 if (errno == 0)
582 vfprintf_diag ("password file entry for uid %lu not found", (unsigned long)uid);
583 else
584 perror ("getpwuid");
585 exit (EXIT_FAILURE);
586 }
587 size = strlen (passwd->pw_dir) + 1 + strlen (CONF_FILE) + 1;
588 path = xmalloc (size);
589 snprintf (path, size, "%s/%s", passwd->pw_dir, CONF_FILE);
590
591 *conf_file = path;
592 }
593
594 static void
595 process_opt_attr (const char *p, const bool is_opt)
596 {
597 /* If attributes are added to this "list", also increase MAX_ATTRIBUTE_CHARS! */
598 const struct attr attrs[] = {
599 { "bold", 1, ATTR_BOLD },
600 { "underscore", 4, ATTR_UNDERSCORE },
601 { "blink", 5, ATTR_BLINK },
602 { "reverse", 7, ATTR_REVERSE },
603 { "concealed", 8, ATTR_CONCEALED },
604 };
605 unsigned int attr_types = 0;
606 const char *desc_type[2] = { "--attr switch", "attr conf option" };
607 const unsigned int DESC_TYPE = is_opt ? DESC_OPTION : DESC_CONF;
608
609 while (*p)
610 {
611 const char *s;
612 if (!isalnum (*p))
613 vfprintf_fail ("%s must be provided a string", desc_type[DESC_TYPE]);
614 s = p;
615 while (isalnum (*p))
616 p++;
617 if (*p != '\0' && *p != ',')
618 vfprintf_fail ("%s must have strings separated by ,", desc_type[DESC_TYPE]);
619 else
620 {
621 bool valid_attr = false;
622 unsigned int i;
623 for (i = 0; i < COUNT_OF (attrs, struct attr); i++)
624 {
625 const size_t name_len = strlen (attrs[i].name);
626 if ((size_t)(p - s) == name_len && strneq (s, attrs[i].name, name_len))
627 {
628 write_attr (&attrs[i], &attr_types, is_opt);
629 valid_attr = true;
630 break;
631 }
632 }
633 if (!valid_attr)
634 {
635 char *attr_invalid = xmalloc ((p - s) + 1);
636 STACK_VAR (attr_invalid);
637 strncpy (attr_invalid, s, p - s);
638 attr_invalid[p - s] = '\0';
639 vfprintf_fail ("%s attribute '%s' is not valid", desc_type[DESC_TYPE], attr_invalid);
640 RELEASE (attr_invalid); /* never reached */
641 }
642 }
643 if (*p)
644 p++;
645 }
646 }
647
648 static void
649 write_attr (const struct attr *attr_i, unsigned int *attr_types, const bool is_opt)
650 {
651 const unsigned int val = attr_i->val;
652 const enum attr_type attr_type = attr_i->type;
653 const char *attr_name = attr_i->name;
654
655 if (*attr_types & attr_type)
656 vfprintf_fail ("%s has attribute '%s' twice or more",
657 is_opt ? "--attr switch" : "attr conf option", attr_name);
658 snprintf (attr + strlen (attr), 3, "%u;", val);
659 *attr_types |= attr_type;
660 }
661
662 static void
663 process_opt_exclude_random (const char *s, const bool is_opt)
664 {
665 bool valid = false;
666 unsigned int i;
667 RELEASE (exclude);
668 exclude = xstrdup (s);
669 STACK_VAR (exclude);
670 for (i = 1; i < tables[GENERIC].count - 1; i++) /* skip color none and default */
671 {
672 const struct color *entry = &tables[GENERIC].entries[i];
673 if (streq (exclude, entry->name))
674 {
675 valid = true;
676 break;
677 }
678 }
679 if (!valid)
680 vfprintf_fail ("%s must be provided a plain color",
681 is_opt ? "--exclude-random switch" : "exclude-random conf option");
682 }
683
684 static void
685 init_opts_vars (void)
686 {
687 if (opts_set & OPT_ATTR_SET)
688 {
689 attr[0] = '\0'; /* Clear attr string to discard values from the config file. */
690 process_opt_attr (opts_arg.attr, true);
691 }
692 if (opts_set & OPT_EXCLUDE_RANDOM_SET)
693 process_opt_exclude_random (opts_arg.exclude_random, true);
694 if (opts_set & OPT_OMIT_COLOR_EMPTY_SET)
695 omit_color_empty = true;
696
697 RELEASE (opts_arg.attr);
698 RELEASE (opts_arg.exclude_random);
699 }
700
701 #define IS_SPACE(c) ((c) == ' ' || (c) == '\t')
702
703 static void
704 parse_conf (const char *conf_file, struct conf *config)
705 {
706 unsigned int cnt = 0;
707 char line[256 + 1];
708 FILE *conf;
709
710 conf = open_file (conf_file, "r");
711 STACK_FILE (conf);
712
713 while (fgets (line, sizeof (line), conf))
714 {
715 char *cfg, *val;
716 char *assign, *comment, *opt, *value;
717 char *p;
718
719 cnt++;
720 if ((p = strchr (line, '\r')) && *(p + 1) != '\n')
721 vfprintf_fail ("%s: CR ending of line %u is not supported, switch to CRLF/LF instead", conf_file, cnt);
722 if (strlen (line) > (sizeof (line) - 2))
723 vfprintf_fail ("%s: line %u exceeds maximum of %u characters", conf_file, cnt, (unsigned int)(sizeof (line) - 2));
724 if ((p = strpbrk (line, "\n\r")))
725 *p = '\0';
726 /* NAME PARSING (start) */
727 p = line;
728 /* skip leading spaces and tabs for name */
729 while (IS_SPACE (*p))
730 p++;
731 /* skip line if a) string end, b) comment, [cd]) newline */
732 if (*p == '\0' || *p == '#' || *p == '\n' || *p == '\r')
733 continue;
734 opt = p;
735 if (!(assign = strchr (opt, '='))) /* check for = */
736 {
737 char *s;
738 if ((s = strpbrk (opt, "# ")))
739 *s = '\0';
740 vfprintf_fail (formats[FMT_CONF], conf_file, opt, "not followed by =");
741 }
742 p = assign;
743 /* skip trailing spaces and tabs for name */
744 while (IS_SPACE (*(p - 1)))
745 p--;
746 *p = '\0';
747 /* NAME PARSING (end) */
748 /* NAME VALIDATION (start) */
749 for (p = opt; *p; p++)
750 if (!isalnum (*p) && *p != '-')
751 vfprintf_fail (formats[FMT_CONF], conf_file, opt, "cannot be made of non-option characters");
752 /* NAME VALIDATION (end) */
753 /* VALUE PARSING (start) */
754 p = assign + 1;
755 /* skip leading spaces and tabs for value */
756 while (IS_SPACE (*p))
757 p++;
758 /* skip line if comment */
759 if (*p == '#')
760 continue;
761 value = p;
762 if ((comment = strchr (p, '#')))
763 p = comment;
764 else
765 p += strlen (p);
766 /* skip trailing spaces and tabs for value */
767 while (IS_SPACE (*(p - 1)))
768 p--;
769 *p = '\0';
770 /* VALUE PARSING (end) */
771
772 /* save option name */
773 cfg = xstrdup (opt);
774 STACK_VAR (cfg);
775 /* save option value (allow empty ones) */
776 val = strlen (value) ? xstrdup (value) : NULL;
777 STACK_VAR (val);
778
779 assign_conf (conf_file, config, cfg, val);
780 RELEASE (cfg);
781 }
782
783 RELEASE (conf);
784 }
785
786 #define ASSIGN_CONF(str,val) do { \
787 RELEASE (str); \
788 str = val; \
789 } while (false)
790
791 static void
792 assign_conf (const char *conf_file, struct conf *config, const char *cfg, char *val)
793 {
794 if (streq (cfg, "attr"))
795 ASSIGN_CONF (config->attr, val);
796 else if (streq (cfg, "color"))
797 ASSIGN_CONF (config->color, val);
798 else if (streq (cfg, "exclude-random"))
799 ASSIGN_CONF (config->exclude_random, val);
800 else if (streq (cfg, "omit-color-empty"))
801 ASSIGN_CONF (config->omit_color_empty, val);
802 else
803 vfprintf_fail (formats[FMT_CONF], conf_file, cfg, "not recognized");
804 }
805
806 static void
807 init_conf_vars (const struct conf *config)
808 {
809 if (config->attr)
810 process_opt_attr (config->attr, false);
811 if (config->exclude_random)
812 process_opt_exclude_random (config->exclude_random, false);
813 if (config->omit_color_empty)
814 {
815 if (streq (config->omit_color_empty, "yes"))
816 omit_color_empty = true;
817 else if (streq (config->omit_color_empty, "no"))
818 omit_color_empty = false;
819 else
820 vfprintf_fail (formats[FMT_GENERIC], "omit-color-empty conf option is not valid");
821 }
822 }
823
824 static void
825 print_hint (void)
826 {
827 fprintf (stderr, "Type `%s --help' for help screen.\n", program_name);
828 }
829
830 static void
831 print_help (void)
832 {
833 struct opt_data {
834 const char *name;
835 const char *short_opt;
836 const char *arg;
837 };
838 const struct opt_data opts_data[] = {
839 { "attr", NULL, "=ATTR1,ATTR2,..." },
840 { "config", "c", "=PATH" },
841 { "exclude-random", NULL, "=COLOR" },
842 { "help", "h", NULL },
843 { "version", "V", NULL },
844 };
845 const struct option *opt = long_opts;
846 unsigned int i;
847
848 printf ("Usage: %s (foreground) OR (foreground)%c(background) OR --clean[-all] [-|file]\n\n", program_name, COLOR_SEP_CHAR);
849 printf ("\tColors (foreground) (background)\n");
850 for (i = 0; i < tables[FOREGROUND].count; i++)
851 {
852 const struct color *entry = &tables[FOREGROUND].entries[i];
853 const char *name = entry->name;
854 const char *code = entry->code;
855 if (code)
856 printf ("\t\t{\033[%s#\033[0m} [%c%c]%s%*s%s\n",
857 code, toupper (*name), *name, name + 1, 10 - (int)strlen (name), " ", name);
858 else
859 printf ("\t\t{-} %s%*s%s\n", name, 13 - (int)strlen (name), " ", name);
860 }
861 printf ("\t\t{*} [Rr]%s%*s%s [--exclude-random=<foreground color>]\n", "andom", 10 - (int)strlen ("random"), " ", "random");
862
863 printf ("\n\tFirst character of color name in upper case denotes increased intensity,\n");
864 printf ("\twhereas for lower case colors will be of normal intensity.\n");
865
866 printf ("\n\tOptions\n");
867 for (; opt->name; opt++)
868 {
869 const struct opt_data *opt_data = NULL;
870 unsigned int i;
871 for (i = 0; i < COUNT_OF (opts_data, struct opt_data); i++)
872 if (streq (opt->name, opts_data[i].name))
873 {
874 opt_data = &opts_data[i];
875 break;
876 }
877 if (opt_data)
878 {
879 if (opt_data->short_opt)
880 printf ("\t\t-%s, --%s", opt_data->short_opt, opt->name);
881 else
882 printf ("\t\t --%s", opt->name);
883 if (opt_data->arg)
884 printf ("%s", opt_data->arg);
885 printf ("\n");
886 }
887 else
888 printf ("\t\t --%s\n", opt->name);
889 }
890 printf ("\n");
891 }
892
893 static void
894 print_version (void)
895 {
896 #ifdef HAVE_VERSION
897 # include "version.h"
898 #else
899 const char *const version = NULL;
900 #endif
901 const char *version_prefix, *version_string;
902 const char *c_flags, *ld_flags, *cpp_flags;
903 const char *const desc_flags_unknown = "unknown";
904 struct bytes_size bytes_size;
905 bool debug;
906 #ifdef CFLAGS
907 c_flags = to_str (CFLAGS);
908 #else
909 c_flags = desc_flags_unknown;
910 #endif
911 #ifdef LDFLAGS
912 ld_flags = to_str (LDFLAGS);
913 #else
914 ld_flags = desc_flags_unknown;
915 #endif
916 #ifdef CPPFLAGS
917 cpp_flags = to_str (CPPFLAGS);
918 #else
919 cpp_flags = desc_flags_unknown;
920 #endif
921 #if DEBUG
922 debug = true;
923 #else
924 debug = false;
925 #endif
926 version_prefix = version ? "" : "v";
927 version_string = version ? version : VERSION;
928 printf ("%s %s%s (compiled at %s, %s)\n", PROGRAM_NAME, version_prefix, version_string, __DATE__, __TIME__);
929
930 printf ("Compiler flags: %s\n", c_flags);
931 printf ("Linker flags: %s\n", ld_flags);
932 printf ("Preprocessor flags: %s\n", cpp_flags);
933 if (get_bytes_size (BUF_SIZE, &bytes_size))
934 {
935 if (BUF_SIZE % 1024 == 0)
936 printf ("Buffer size: %u%c\n", bytes_size.size, bytes_size.unit);
937 else
938 printf ("Buffer size: %u%c, %u byte%s\n", bytes_size.size, bytes_size.unit,
939 BUF_SIZE % 1024, BUF_SIZE % 1024 > 1 ? "s" : "");
940 }
941 else
942 printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE, BUF_SIZE > 1 ? "s" : "");
943 printf ("Color separator: '%c'\n", COLOR_SEP_CHAR);
944 printf ("Debugging: %s\n", debug ? "yes" : "no");
945 }
946
947 static void
948 cleanup (void)
949 {
950 if (stream && fileno (stream) != STDIN_FILENO)
951 RELEASE (stream);
952 #if DEBUG
953 if (log)
954 RELEASE (log);
955 #endif
956
957 if (vars_list)
958 {
959 unsigned int i;
960 for (i = 0; i < stacked_vars; i++)
961 {
962 struct var_list *var = &vars_list[i];
963 switch (var->type)
964 {
965 case IS_GENERIC:
966 free (var->ptr);
967 break;
968 case IS_FILE:
969 fclose (var->ptr);
970 break;
971 case IS_UNUSED:
972 break;
973 default: /* never reached */
974 ABORT_TRACE ();
975 }
976 }
977 free_null (vars_list);
978 }
979 }
980
981 static void
982 free_color_names (struct color_name **color_names)
983 {
984 unsigned int i;
985 for (i = 0; color_names[i]; i++)
986 {
987 RELEASE (color_names[i]->name);
988 RELEASE (color_names[i]->orig);
989 RELEASE (color_names[i]);
990 }
991 }
992
993 static void
994 free_conf (struct conf *config)
995 {
996 RELEASE (config->attr);
997 RELEASE (config->color);
998 RELEASE (config->exclude_random);
999 RELEASE (config->omit_color_empty);
1000 }
1001
1002 static void
1003 process_args (unsigned int arg_cnt, char **arg_strings, char *attr, const struct color **colors, const char **file, FILE **stream, struct conf *config)
1004 {
1005 bool has_hyphen, use_conf_color;
1006 int ret;
1007 char *p;
1008 struct stat sb;
1009 struct color_name *color_names[3] = {
1010 NULL, /* foreground */
1011 NULL, /* background */
1012 NULL, /* sentinel value */
1013 };
1014
1015 const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL;
1016 const char *file_string = arg_cnt == 2 ? arg_strings[1] : NULL;
1017
1018 assert (color_string != NULL);
1019
1020 has_hyphen = streq (color_string, "-");
1021
1022 if (has_hyphen)
1023 {
1024 if (file_string)
1025 vfprintf_fail (formats[FMT_GENERIC], "hyphen cannot be used as color string");
1026 else if (!config->color)
1027 vfprintf_fail (formats[FMT_GENERIC], "hyphen must be preceded by color string");
1028 }
1029
1030 if (!has_hyphen && (ret = lstat (color_string, &sb)) == 0) /* exists */
1031 /* Ensure that we don't fail if there's a file with one or more
1032 color names in its path. */
1033 use_conf_color = skip_path_colors (color_string, file_string, &sb, !!config->color);
1034 else if (has_hyphen)
1035 use_conf_color = true;
1036 else
1037 use_conf_color = false;
1038
1039 /* Use color from config file. */
1040 if (arg_cnt == 1 && use_conf_color)
1041 {
1042 file_string = color_string;
1043 color_string = config->color;
1044 }
1045
1046 if ((p = strchr (color_string, COLOR_SEP_CHAR)))
1047 {
1048 if (p == color_string)
1049 vfprintf_fail (formats[FMT_STRING], "foreground color missing in string", color_string);
1050 else if (p == color_string + strlen (color_string) - 1)
1051 vfprintf_fail (formats[FMT_STRING], "background color missing in string", color_string);
1052 else if (strchr (++p, COLOR_SEP_CHAR))
1053 vfprintf_fail (formats[FMT_STRING], "one color pair allowed only for string", color_string);
1054 }
1055
1056 gather_color_names (color_string, attr, color_names);
1057
1058 assert (color_names[FOREGROUND] != NULL);
1059
1060 if (color_names[BACKGROUND])
1061 {
1062 unsigned int i;
1063 const unsigned int color_sets[2][2] = { { FOREGROUND, BACKGROUND }, { BACKGROUND, FOREGROUND } };
1064 for (i = 0; i < 2; i++)
1065 {
1066 const unsigned int color1 = color_sets[i][0];
1067 const unsigned int color2 = color_sets[i][1];
1068 if (CHECK_COLORS_RANDOM (color1, color2))
1069 vfprintf_fail (formats[FMT_RANDOM], tables[color1].desc, color_names[color1]->orig, "cannot be combined with", color_names[color2]->orig);
1070 }
1071 }
1072
1073 find_color_entries (color_names, colors);
1074 assert (colors[FOREGROUND] != NULL);
1075 free_color_names (color_names);
1076
1077 if (!colors[FOREGROUND]->code && colors[BACKGROUND] && colors[BACKGROUND]->code)
1078 {
1079 struct color_name color_name;
1080 color_name.name = color_name.orig = "default";
1081
1082 find_color_entry (&color_name, FOREGROUND, colors);
1083 assert (colors[FOREGROUND]->code != NULL);
1084 }
1085
1086 process_file_arg (file_string, file, stream);
1087 }
1088
1089 static void
1090 process_file_arg (const char *file_string, const char **file, FILE **stream)
1091 {
1092 if (file_string)
1093 {
1094 if (streq (file_string, "-"))
1095 *stream = stdin;
1096 else
1097 {
1098 const char *file = file_string;
1099 struct stat sb;
1100 int ret;
1101
1102 errno = 0;
1103 ret = stat (file, &sb);
1104
1105 if (ret == -1)
1106 vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
1107
1108 if (!VALID_FILE_TYPE (sb.st_mode))
1109 vfprintf_fail (formats[FMT_TYPE], file, "unrecognized type", get_file_type (sb.st_mode));
1110
1111 *stream = open_file (file, "r");
1112 STACK_FILE (*stream);
1113 }
1114 *file = file_string;
1115 }
1116 else
1117 {
1118 *stream = stdin;
1119 *file = "stdin";
1120 }
1121
1122 assert (*stream != NULL);
1123 assert (*file != NULL);
1124 }
1125
1126 static bool
1127 skip_path_colors (const char *color_string, const char *file_string, const struct stat *sb, const bool has_conf)
1128 {
1129 bool have_file;
1130 unsigned int c;
1131 const char *color = color_string;
1132 const mode_t mode = sb->st_mode;
1133
1134 for (c = 1; c <= 2 && *color; c++)
1135 {
1136 bool matched = false;
1137 unsigned int i;
1138 for (i = 0; i < tables[GENERIC].count; i++)
1139 {
1140 const struct color *entry = &tables[GENERIC].entries[i];
1141 if (has_color_name (color, entry->name))
1142 {
1143 color += strlen (entry->name);
1144 matched = true;
1145 break;
1146 }
1147 }
1148 if (!matched && has_color_name (color, "random"))
1149 {
1150 color += strlen ("random");
1151 matched = true;
1152 }
1153 if (matched && *color == COLOR_SEP_CHAR && *(color + 1))
1154 color++;
1155 else
1156 break;
1157 }
1158
1159 have_file = (*color != '\0');
1160
1161 if (have_file)
1162 {
1163 const char *file_existing = color_string;
1164 if (file_string)
1165 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_existing, "cannot be used as color string");
1166 else
1167 {
1168 if (VALID_FILE_TYPE (mode))
1169 {
1170 if (has_conf)
1171 return true;
1172 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_existing, "must be preceded by color string");
1173 }
1174 else
1175 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_existing, "is not a valid file type");
1176 }
1177 }
1178 return false;
1179 }
1180
1181 static void
1182 gather_color_names (const char *color_string, char *attr, struct color_name **color_names)
1183 {
1184 unsigned int index;
1185 char *color, *p, *str;
1186
1187 str = xstrdup (color_string);
1188 STACK_VAR (str);
1189
1190 for (index = 0, color = str; *color; index++, color = p)
1191 {
1192 char *ch, *sep;
1193
1194 p = NULL;
1195 if ((sep = strchr (color, COLOR_SEP_CHAR)))
1196 {
1197 *sep = '\0';
1198 p = sep + 1;
1199 }
1200 else
1201 p = color + strlen (color);
1202 assert (p != NULL);
1203
1204 for (ch = color; *ch; ch++)
1205 if (!isalpha (*ch))
1206 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be made of non-alphabetic characters");
1207
1208 for (ch = color + 1; *ch; ch++)
1209 if (!islower (*ch))
1210 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be in mixed lower/upper case");
1211
1212 if (streq (color, "None"))
1213 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be bold");
1214
1215 if (isupper (*color))
1216 {
1217 switch (index)
1218 {
1219 case FOREGROUND:
1220 snprintf (attr + strlen (attr), 3, "1;");
1221 break;
1222 case BACKGROUND:
1223 vfprintf_fail (formats[FMT_COLOR], tables[BACKGROUND].desc, color, "cannot be bold");
1224 break;
1225 default: /* never reached */
1226 ABORT_TRACE ();
1227 }
1228 }
1229
1230 color_names[index] = xcalloc (1, sizeof (struct color_name));
1231 STACK_VAR (color_names[index]);
1232
1233 color_names[index]->orig = xstrdup (color);
1234 STACK_VAR (color_names[index]->orig);
1235
1236 for (ch = color; *ch; ch++)
1237 *ch = tolower (*ch);
1238
1239 color_names[index]->name = xstrdup (color);
1240 STACK_VAR (color_names[index]->name);
1241 }
1242
1243 RELEASE (str);
1244 }
1245
1246 static void
1247 read_print_stream (const char *attr, const struct color **colors, const char *file, FILE *stream)
1248 {
1249 char buf[BUF_SIZE + 1];
1250 unsigned int flags = 0;
1251
1252 while (!feof (stream))
1253 {
1254 size_t bytes_read;
1255 char *eol;
1256 const char *line;
1257 bytes_read = fread (buf, 1, BUF_SIZE, stream);
1258 if (bytes_read != BUF_SIZE && ferror (stream))
1259 vfprintf_fail (formats[FMT_ERROR], BUF_SIZE, "read");
1260 buf[bytes_read] = '\0';
1261 line = buf;
1262 while ((eol = strpbrk (line, "\n\r")))
1263 {
1264 const bool has_text = (eol > line);
1265 const char *p;
1266 flags &= ~(CR|LF);
1267 if (*eol == '\r')
1268 {
1269 flags |= CR;
1270 if (*(eol + 1) == '\n')
1271 flags |= LF;
1272 }
1273 else if (*eol == '\n')
1274 flags |= LF;
1275 else /* never reached */
1276 vfprintf_fail (formats[FMT_FILE], file, "unrecognized line ending");
1277 p = eol + SKIP_LINE_ENDINGS (flags);
1278 *eol = '\0';
1279 print_line (attr, colors, line, flags,
1280 omit_color_empty ? has_text : true);
1281 line = p;
1282 }
1283 if (feof (stream))
1284 {
1285 if (*line != '\0')
1286 print_line (attr, colors, line, 0, true);
1287 }
1288 else if (*line != '\0')
1289 {
1290 char *p;
1291 if ((clean || clean_all) && (p = strrchr (line, '\033')))
1292 merge_print_line (line, p, stream);
1293 else
1294 print_line (attr, colors, line, 0, true);
1295 }
1296 }
1297 }
1298
1299 static void
1300 merge_print_line (const char *line, const char *p, FILE *stream)
1301 {
1302 char *buf = NULL;
1303 char *merged_esc = NULL;
1304 const char *esc = "";
1305 const char char_restore = *p;
1306
1307 complete_part_line (p + 1, &buf, stream);
1308
1309 if (buf)
1310 {
1311 /* form escape sequence */
1312 esc = merged_esc = str_concat (p, buf);
1313 /* shorten partial line accordingly */
1314 *(char *)p = '\0';
1315 free (buf);
1316 }
1317
1318 #ifdef TEST_MERGE_PART_LINE
1319 printf ("%s%s", line, esc);
1320 fflush (stdout);
1321 _exit (EXIT_SUCCESS);
1322 #else
1323 print_clean (line);
1324 *(char *)p = char_restore;
1325 print_clean (esc);
1326 free (merged_esc);
1327 #endif
1328 }
1329
1330 static void
1331 complete_part_line (const char *p, char **buf, FILE *stream)
1332 {
1333 bool got_next_char = false, read_from_stream;
1334 char ch;
1335 size_t i = 0, size;
1336
1337 if (get_next_char (&ch, &p, stream, &read_from_stream))
1338 {
1339 if (ch == '[')
1340 {
1341 if (read_from_stream)
1342 save_char (ch, buf, &i, &size);
1343 }
1344 else
1345 {
1346 if (read_from_stream)
1347 ungetc ((int)ch, stream);
1348 return; /* cancel */
1349 }
1350 }
1351 else
1352 return; /* cancel */
1353
1354 while (get_next_char (&ch, &p, stream, &read_from_stream))
1355 {
1356 if (isdigit (ch) || ch == ';')
1357 {
1358 if (read_from_stream)
1359 save_char (ch, buf, &i, &size);
1360 }
1361 else /* got next character */
1362 {
1363 got_next_char = true;
1364 break;
1365 }
1366 }
1367
1368 if (got_next_char)
1369 {
1370 if (ch == 'm')
1371 {
1372 if (read_from_stream)
1373 save_char (ch, buf, &i, &size);
1374 }
1375 else
1376 {
1377 if (read_from_stream)
1378 ungetc ((int)ch, stream);
1379 return; /* cancel */
1380 }
1381 }
1382 else
1383 return; /* cancel */
1384 }
1385
1386 static bool
1387 get_next_char (char *ch, const char **p, FILE *stream, bool *read_from_stream)
1388 {
1389 if (**p == '\0')
1390 {
1391 int c;
1392 if ((c = fgetc (stream)) != EOF)
1393 {
1394 *ch = (char)c;
1395 *read_from_stream = true;
1396 return true;
1397 }
1398 else
1399 {
1400 *read_from_stream = false;
1401 return false;
1402 }
1403 }
1404 else
1405 {
1406 *ch = **p;
1407 (*p)++;
1408 *read_from_stream = false;
1409 return true;
1410 }
1411 }
1412
1413 static void
1414 save_char (char ch, char **buf, size_t *i, size_t *size)
1415 {
1416 if (!*buf)
1417 {
1418 *size = ALLOC_COMPLETE_PART_LINE;
1419 *buf = xmalloc (*size);
1420 }
1421 /* +1: effective occupied size of buffer */
1422 else if ((*i + 1) == *size)
1423 {
1424 *size *= 2;
1425 *buf = xrealloc (*buf, *size);
1426 }
1427 (*buf)[*i] = ch;
1428 (*buf)[*i + 1] = '\0';
1429 (*i)++;
1430 }
1431
1432 static void
1433 find_color_entries (struct color_name **color_names, const struct color **colors)
1434 {
1435 struct timeval tv;
1436 unsigned int index;
1437
1438 /* randomness */
1439 gettimeofday (&tv, NULL);
1440 srand (tv.tv_usec * tv.tv_sec);
1441
1442 for (index = 0; color_names[index]; index++)
1443 {
1444 const char *color_name = color_names[index]->name;
1445
1446 const unsigned int count = tables[index].count;
1447 const struct color *const color_entries = tables[index].entries;
1448
1449 if (streq (color_name, "random"))
1450 {
1451 bool excludable;
1452 unsigned int i;
1453 do {
1454 excludable = false;
1455 i = rand() % (count - 2) + 1; /* omit color none and default */
1456 switch (index)
1457 {
1458 case FOREGROUND:
1459 /* --exclude-random */
1460 if (exclude && streq (exclude, color_entries[i].name))
1461 excludable = true;
1462 else if (color_names[BACKGROUND] && streq (color_names[BACKGROUND]->name, color_entries[i].name))
1463 excludable = true;
1464 break;
1465 case BACKGROUND:
1466 if (streq (colors[FOREGROUND]->name, color_entries[i].name))
1467 excludable = true;
1468 break;
1469 default: /* never reached */
1470 ABORT_TRACE ();
1471 }
1472 } while (excludable);
1473 colors[index] = (struct color *)&color_entries[i];
1474 }
1475 else
1476 find_color_entry (color_names[index], index, colors);
1477 }
1478 }
1479
1480 static void
1481 find_color_entry (const struct color_name *color_name, unsigned int index, const struct color **colors)
1482 {
1483 bool found = false;
1484 unsigned int i;
1485
1486 const unsigned int count = tables[index].count;
1487 const struct color *const color_entries = tables[index].entries;
1488
1489 for (i = 0; i < count; i++)
1490 if (streq (color_name->name, color_entries[i].name))
1491 {
1492 colors[index] = (struct color *)&color_entries[i];
1493 found = true;
1494 break;
1495 }
1496 if (!found)
1497 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color_name->orig, "not recognized");
1498 }
1499
1500 static void
1501 print_line (const char *attr, const struct color **colors, const char *const line, unsigned int flags, bool emit_colors)
1502 {
1503 /* --clean[-all] */
1504 if (clean || clean_all)
1505 print_clean (line);
1506 /* skip for --omit-color-empty? */
1507 else if (emit_colors)
1508 {
1509 /* Foreground color code is guaranteed to be set when background color code is present. */
1510 if (colors[BACKGROUND] && colors[BACKGROUND]->code)
1511 printf ("\033[%s", colors[BACKGROUND]->code);
1512 if (colors[FOREGROUND]->code)
1513 printf ("\033[%s%s%s\033[0m", attr, colors[FOREGROUND]->code, line);
1514 else
1515 printf (formats[FMT_GENERIC], line);
1516 }
1517 if (flags & CR)
1518 putchar ('\r');
1519 if (flags & LF)
1520 putchar ('\n');
1521 }
1522
1523 static void
1524 print_clean (const char *line)
1525 {
1526 const char *p = line;
1527
1528 if (is_esc (p))
1529 p = get_end_of_esc (p);
1530
1531 while (*p != '\0')
1532 {
1533 const char *text_start = p;
1534 const char *text_end = get_end_of_text (p);
1535 print_text (text_start, text_end - text_start);
1536 p = get_end_of_esc (text_end);
1537 }
1538 }
1539
1540 static bool
1541 is_esc (const char *p)
1542 {
1543 return gather_esc_offsets (p, NULL, NULL);
1544 }
1545
1546 static const char *
1547 get_end_of_esc (const char *p)
1548 {
1549 const char *esc;
1550 const char *end = NULL;
1551 while ((esc = strchr (p, '\033')))
1552 {
1553 if (gather_esc_offsets (esc, NULL, &end))
1554 break;
1555 p = esc + 1;
1556 }
1557 return end ? end + 1 : p + strlen (p);
1558 }
1559
1560 static const char *
1561 get_end_of_text (const char *p)
1562 {
1563 const char *esc;
1564 const char *start = NULL;
1565 while ((esc = strchr (p, '\033')))
1566 {
1567 if (gather_esc_offsets (esc, &start, NULL))
1568 break;
1569 p = esc + 1;
1570 }
1571 return start ? start : p + strlen (p);
1572 }
1573
1574 static void
1575 print_text (const char *p, size_t len)
1576 {
1577 size_t bytes_written;
1578 bytes_written = fwrite (p, 1, len, stdout);
1579 if (bytes_written != len)
1580 vfprintf_fail (formats[FMT_ERROR], (unsigned long)len, "written");
1581 }
1582
1583 static bool
1584 gather_esc_offsets (const char *p, const char **start, const char **end)
1585 {
1586 /* ESC[ */
1587 if (*p == 27 && *(p + 1) == '[')
1588 {
1589 bool valid = false;
1590 const char *const begin = p;
1591 p += 2;
1592 if (clean_all)
1593 valid = validate_esc_clean_all (&p);
1594 else if (clean)
1595 {
1596 bool check_values;
1597 unsigned int prev_iter, iter;
1598 const char *digit;
1599 prev_iter = iter = 0;
1600 do {
1601 check_values = false;
1602 iter++;
1603 if (!isdigit (*p))
1604 break;
1605 digit = p;
1606 while (isdigit (*p))
1607 p++;
1608 if (p - digit > 2)
1609 break;
1610 else /* check range */
1611 {
1612 char val[3];
1613 int value;
1614 unsigned int i;
1615 const unsigned int digits = p - digit;
1616 for (i = 0; i < digits; i++)
1617 val[i] = *digit++;
1618 val[i] = '\0';
1619 value = atoi (val);
1620 valid = validate_esc_clean (value, iter, &prev_iter, &p, &check_values);
1621 }
1622 } while (check_values);
1623 }
1624 if (valid)
1625 {
1626 if (start)
1627 *start = begin;
1628 if (end)
1629 *end = p;
1630 return true;
1631 }
1632 }
1633 return false;
1634 }
1635
1636 static bool
1637 validate_esc_clean_all (const char **p)
1638 {
1639 while (isdigit (**p) || **p == ';')
1640 (*p)++;
1641 return (**p == 'm');
1642 }
1643
1644 static bool
1645 validate_esc_clean (int value, unsigned int iter, unsigned int *prev_iter, const char **p, bool *check_values)
1646 {
1647 if (is_reset (value, iter, p))
1648 return true;
1649 else if (is_attr (value, iter, *prev_iter, p))
1650 {
1651 (*p)++;
1652 *check_values = true;
1653 *prev_iter = iter;
1654 return false; /* partial escape sequence, need another valid value */
1655 }
1656 else if (is_fg_color (value, p))
1657 return true;
1658 else if (is_bg_color (value, iter, p))
1659 return true;
1660 else
1661 return false;
1662 }
1663
1664 static bool
1665 is_reset (int value, unsigned int iter, const char **p)
1666 {
1667 return (value == 0 && iter == 1 && **p == 'm');
1668 }
1669
1670 static bool
1671 is_attr (int value, unsigned int iter, unsigned int prev_iter, const char **p)
1672 {
1673 return ((value > 0 && value < 10) && (iter - prev_iter == 1) && **p == ';');
1674 }
1675
1676 static bool
1677 is_fg_color (int value, const char **p)
1678 {
1679 return (((value >= 30 && value <= 37) || value == 39) && **p == 'm');
1680 }
1681
1682 static bool
1683 is_bg_color (int value, unsigned int iter, const char **p)
1684 {
1685 return (((value >= 40 && value <= 47) || value == 49) && iter == 1 && **p == 'm');
1686 }
1687
1688 #if !DEBUG
1689 static void *
1690 malloc_wrap (size_t size)
1691 {
1692 void *p = malloc (size);
1693 if (!p)
1694 MEM_ALLOC_FAIL ();
1695 return p;
1696 }
1697
1698 static void *
1699 calloc_wrap (size_t nmemb, size_t size)
1700 {
1701 void *p = calloc (nmemb, size);
1702 if (!p)
1703 MEM_ALLOC_FAIL ();
1704 return p;
1705 }
1706
1707 static void *
1708 realloc_wrap (void *ptr, size_t size)
1709 {
1710 void *p = realloc (ptr, size);
1711 if (!p)
1712 MEM_ALLOC_FAIL ();
1713 return p;
1714 }
1715 #else
1716 static const char *const format_debug = "%s: %10s %7lu bytes [source file %s, line %5u]\n";
1717 static void *
1718 malloc_wrap_debug (size_t size, const char *file, unsigned int line)
1719 {
1720 void *p = malloc (size);
1721 if (!p)
1722 MEM_ALLOC_FAIL_DEBUG (file, line);
1723 fprintf (log, format_debug, program_name, "malloc'ed", (unsigned long)size, file, line);
1724 return p;
1725 }
1726
1727 static void *
1728 calloc_wrap_debug (size_t nmemb, size_t size, const char *file, unsigned int line)
1729 {
1730 void *p = calloc (nmemb, size);
1731 if (!p)
1732 MEM_ALLOC_FAIL_DEBUG (file, line);
1733 fprintf (log, format_debug, program_name, "calloc'ed", (unsigned long)(nmemb * size), file, line);
1734 return p;
1735 }
1736
1737 static void *
1738 realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
1739 {
1740 void *p = realloc (ptr, size);
1741 if (!p)
1742 MEM_ALLOC_FAIL_DEBUG (file, line);
1743 fprintf (log, format_debug, program_name, "realloc'ed", (unsigned long)size, file, line);
1744 return p;
1745 }
1746 #endif /* !DEBUG */
1747
1748 static void
1749 free_wrap (void **ptr)
1750 {
1751 free (*ptr);
1752 *ptr = NULL;
1753 }
1754
1755 #if !DEBUG
1756 # define do_malloc(len, file, line) malloc_wrap(len)
1757 #else
1758 # define do_malloc(len, file, line) malloc_wrap_debug(len, file, line)
1759 #endif
1760
1761 static char *
1762 strdup_wrap (const char *str, const char *file, unsigned int line)
1763 {
1764 const size_t len = strlen (str) + 1;
1765 char *p = do_malloc (len, file, line);
1766 strncpy (p, str, len);
1767 return p;
1768 }
1769
1770 static char *
1771 str_concat_wrap (const char *str1, const char *str2, const char *file, unsigned int line)
1772 {
1773 const size_t len = strlen (str1) + strlen (str2) + 1;
1774 char *p, *str;
1775
1776 p = str = do_malloc (len, file, line);
1777 strncpy (p, str1, strlen (str1));
1778 p += strlen (str1);
1779 strncpy (p, str2, strlen (str2));
1780 p += strlen (str2);
1781 *p = '\0';
1782
1783 return str;
1784 }
1785
1786 static char *
1787 expand_string (const char *str)
1788 {
1789 char *s = NULL;
1790 wordexp_t p;
1791
1792 wordexp (str, &p, 0);
1793 if (p.we_wordc >= 1)
1794 s = xstrdup (p.we_wordv[0]);
1795 wordfree (&p);
1796
1797 return s;
1798 }
1799
1800 static bool
1801 get_bytes_size (unsigned long bytes, struct bytes_size *bytes_size)
1802 {
1803 const char *unit, units[] = { '0', 'K', 'M', 'G', '\0' };
1804 unsigned long size = bytes;
1805 if (bytes < 1024)
1806 return false;
1807 unit = units;
1808 while (size >= 1024 && *(unit + 1))
1809 {
1810 size /= 1024;
1811 unit++;
1812 }
1813 bytes_size->size = (unsigned int)size;
1814 bytes_size->unit = *unit;
1815 return true;
1816 }
1817
1818 static char *
1819 get_file_type (mode_t mode)
1820 {
1821 if (S_ISREG (mode))
1822 return "file";
1823 else if (S_ISDIR (mode))
1824 return "directory";
1825 else if (S_ISCHR (mode))
1826 return "character device";
1827 else if (S_ISBLK (mode))
1828 return "block device";
1829 else if (S_ISFIFO (mode))
1830 return "named pipe";
1831 else if (S_ISLNK (mode))
1832 return "symbolic link";
1833 else if (S_ISSOCK (mode))
1834 return "socket";
1835 else
1836 return "file";
1837 }
1838
1839 static bool
1840 has_color_name (const char *str, const char *name)
1841 {
1842 char *p;
1843
1844 assert (strlen (str) > 0);
1845 assert (strlen (name) > 0);
1846
1847 if (!(*str == *name || *str == toupper (*name)))
1848 return false;
1849 else if (*(name + 1) != '\0'
1850 && !((p = strstr (str + 1, name + 1)) && p == str + 1))
1851 return false;
1852 else
1853 return true;
1854 }
1855
1856 static FILE *
1857 open_file (const char *file, const char *mode)
1858 {
1859 FILE *stream;
1860
1861 errno = 0;
1862 stream = fopen (file, mode);
1863 if (!stream)
1864 vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
1865
1866 return stream;
1867 }
1868
1869 #define DO_VFPRINTF(fmt) \
1870 va_list ap; \
1871 fprintf (stderr, "%s: ", program_name); \
1872 va_start (ap, fmt); \
1873 vfprintf (stderr, fmt, ap); \
1874 va_end (ap); \
1875 fprintf (stderr, "\n");
1876
1877 static void
1878 vfprintf_diag (const char *fmt, ...)
1879 {
1880 DO_VFPRINTF (fmt);
1881 }
1882
1883 static void
1884 vfprintf_fail (const char *fmt, ...)
1885 {
1886 DO_VFPRINTF (fmt);
1887 exit (EXIT_FAILURE);
1888 }
1889
1890 static void
1891 stack (struct var_list **list, unsigned int *stacked, unsigned int index, void *ptr, enum var_type type)
1892 {
1893 struct var_list *var;
1894 /* nothing to stack */
1895 if (ptr == NULL)
1896 return;
1897 if (!*list)
1898 *list = xmalloc (sizeof (struct var_list));
1899 else
1900 {
1901 unsigned int i;
1902 for (i = 0; i < *stacked; i++)
1903 {
1904 var = &(*list)[i];
1905 if (var->type == IS_UNUSED)
1906 {
1907 var->ptr = ptr;
1908 var->type = type;
1909 return; /* reused */
1910 }
1911 }
1912 *list = xrealloc (*list, (*stacked + 1) * sizeof (struct var_list));
1913 }
1914 var = &(*list)[index];
1915 var->ptr = ptr;
1916 var->type = type;
1917 (*stacked)++;
1918 }
1919
1920 static void
1921 release (struct var_list *list, unsigned int stacked, void **ptr)
1922 {
1923 unsigned int i;
1924 /* nothing to release */
1925 if (*ptr == NULL)
1926 return;
1927 for (i = 0; i < stacked; i++)
1928 {
1929 struct var_list *var = &list[i];
1930 if (var->ptr == *ptr)
1931 {
1932 switch (var->type)
1933 {
1934 case IS_GENERIC:
1935 free (*ptr);
1936 break;
1937 case IS_FILE:
1938 fclose (*ptr);
1939 break;
1940 default: /* never reached */
1941 ABORT_TRACE ();
1942 }
1943 *ptr = NULL;
1944 var->ptr = NULL;
1945 var->type = IS_UNUSED;
1946 return;
1947 }
1948 }
1949 }