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