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