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