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