]> git.refcnt.org Git - colorize.git/blob - colorize.c
colorize 0.60
[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-2017 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 <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <time.h>
38 #include <unistd.h>
39
40 #ifndef DEBUG
41 # define DEBUG 0
42 #endif
43
44 #define str(arg) #arg
45 #define to_str(arg) str(arg)
46
47 #define streq(s1, s2) (strcmp (s1, s2) == 0)
48
49 #if !DEBUG
50 # define xmalloc(size) malloc_wrap(size)
51 # define xcalloc(nmemb, size) calloc_wrap(nmemb, size)
52 # define xrealloc(ptr, size) realloc_wrap(ptr, size)
53 # define xstrdup(str) strdup_wrap(str, NULL, 0)
54 # define str_concat(str1, str2) str_concat_wrap(str1, str2, NULL, 0)
55 #else
56 # define xmalloc(size) malloc_wrap_debug(size, __FILE__, __LINE__)
57 # define xcalloc(nmemb, size) calloc_wrap_debug(nmemb, size, __FILE__, __LINE__)
58 # define xrealloc(ptr, size) realloc_wrap_debug(ptr, size, __FILE__, __LINE__)
59 # define xstrdup(str) strdup_wrap(str, __FILE__, __LINE__)
60 # define str_concat(str1, str2) str_concat_wrap(str1, str2, __FILE__, __LINE__)
61 #endif
62
63 #define free_null(ptr) free_wrap((void **)&ptr)
64
65 #if defined(BUF_SIZE) && (BUF_SIZE <= 0 || BUF_SIZE > 65536)
66 # undef BUF_SIZE
67 #endif
68 #ifndef BUF_SIZE
69 # define BUF_SIZE 4096
70 #endif
71
72 #define LF 0x01
73 #define CR 0x02
74
75 #define SKIP_LINE_ENDINGS(flags) (((flags) & CR) && ((flags) & LF) ? 2 : 1)
76
77 #define VALID_FILE_TYPE(mode) (S_ISREG (mode) || S_ISLNK (mode) || S_ISFIFO (mode))
78
79 #define STACK_VAR(ptr) do { \
80 stack_var (&vars_list, &stacked_vars, stacked_vars, ptr); \
81 } while (false)
82
83 #define RELEASE_VAR(ptr) do { \
84 release_var (vars_list, stacked_vars, (void **)&ptr); \
85 } while (false)
86
87 #if !DEBUG
88 # define MEM_ALLOC_FAIL() do { \
89 fprintf (stderr, "%s: memory allocation failure\n", program_name); \
90 exit (EXIT_FAILURE); \
91 } while (false)
92 #else
93 # define MEM_ALLOC_FAIL_DEBUG(file, line) do { \
94 fprintf (stderr, "Memory allocation failure in source file %s, line %u\n", file, line); \
95 exit (EXIT_FAILURE); \
96 } while (false)
97 #endif
98
99 #define ABORT_TRACE() \
100 fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \
101 abort (); \
102
103 #define CHECK_COLORS_RANDOM(color1, color2) \
104 streq (color_names[color1]->name, "random") \
105 && (streq (color_names[color2]->name, "none") \
106 || streq (color_names[color2]->name, "default")) \
107
108 #define ALLOC_COMPLETE_PART_LINE 8
109
110 #if defined(COLOR_SEP_CHAR_COLON)
111 # define COLOR_SEP_CHAR ':'
112 #elif defined(COLOR_SEP_CHAR_SLASH)
113 # define COLOR_SEP_CHAR '/'
114 #else
115 # define COLOR_SEP_CHAR '/'
116 #endif
117
118 #define DEBUG_FILE "debug.txt"
119
120 #define VERSION "0.60"
121
122 typedef enum { false, true } bool;
123
124 struct color_name {
125 char *name;
126 char *orig;
127 };
128
129 struct color {
130 const char *name;
131 const char *code;
132 };
133
134 static const struct color fg_colors[] = {
135 { "none", NULL },
136 { "black", "30m" },
137 { "red", "31m" },
138 { "green", "32m" },
139 { "yellow", "33m" },
140 { "blue", "34m" },
141 { "magenta", "35m" },
142 { "cyan", "36m" },
143 { "white", "37m" },
144 { "default", "39m" },
145 };
146 static const struct color bg_colors[] = {
147 { "none", NULL },
148 { "black", "40m" },
149 { "red", "41m" },
150 { "green", "42m" },
151 { "yellow", "43m" },
152 { "blue", "44m" },
153 { "magenta", "45m" },
154 { "cyan", "46m" },
155 { "white", "47m" },
156 { "default", "49m" },
157 };
158
159 struct bytes_size {
160 unsigned int size;
161 char unit;
162 };
163
164 enum fmts {
165 FMT_GENERIC,
166 FMT_STRING,
167 FMT_QUOTE,
168 FMT_COLOR,
169 FMT_RANDOM,
170 FMT_ERROR,
171 FMT_FILE,
172 FMT_TYPE
173 };
174 static const char *formats[] = {
175 "%s", /* generic */
176 "%s '%s'", /* string */
177 "%s `%s' %s", /* quote */
178 "%s color '%s' %s", /* color */
179 "%s color '%s' %s '%s'", /* random */
180 "less than %lu bytes %s", /* error */
181 "%s: %s", /* file */
182 "%s: %s: %s", /* type */
183 };
184
185 enum { GENERIC, FOREGROUND = 0, BACKGROUND };
186
187 static const struct {
188 struct color const *entries;
189 unsigned int count;
190 const char *desc;
191 } tables[] = {
192 { fg_colors, sizeof (fg_colors) / sizeof (struct color), "foreground" },
193 { bg_colors, sizeof (bg_colors) / sizeof (struct color), "background" },
194 };
195
196 enum {
197 OPT_CLEAN = 1,
198 OPT_CLEAN_ALL,
199 OPT_EXCLUDE_RANDOM,
200 OPT_HELP,
201 OPT_VERSION
202 };
203 static int opt_type;
204 static const struct option long_opts[] = {
205 { "clean", no_argument, &opt_type, OPT_CLEAN },
206 { "clean-all", no_argument, &opt_type, OPT_CLEAN_ALL },
207 { "exclude-random", required_argument, &opt_type, OPT_EXCLUDE_RANDOM },
208 { "help", no_argument, &opt_type, OPT_HELP },
209 { "version", no_argument, &opt_type, OPT_VERSION },
210 { NULL, 0, NULL, 0 },
211 };
212
213 static FILE *stream;
214 #if DEBUG
215 static FILE *log;
216 #endif
217
218 static unsigned int stacked_vars;
219 static void **vars_list;
220
221 static bool clean;
222 static bool clean_all;
223
224 static char *exclude;
225
226 static const char *program_name;
227
228 static void process_opts (int, char **);
229 static void print_hint (void);
230 static void print_help (void);
231 static void print_version (void);
232 static void cleanup (void);
233 static void free_color_names (struct color_name **);
234 static void process_args (unsigned int, char **, bool *, const struct color **, const char **, FILE **);
235 static void process_file_arg (const char *, const char **, FILE **);
236 static void skip_path_colors (const char *, const char *, const struct stat *);
237 static void gather_color_names (const char *, bool *, struct color_name **);
238 static void read_print_stream (bool, const struct color **, const char *, FILE *);
239 static void merge_print_line (const char *, const char *, FILE *);
240 static void complete_part_line (const char *, char **, FILE *);
241 static bool get_next_char (char *, const char **, FILE *, bool *);
242 static void save_char (char, char **, size_t *, size_t *);
243 static void find_color_entries (struct color_name **, const struct color **);
244 static void find_color_entry (const struct color_name *, unsigned int, const struct color **);
245 static void print_line (bool, const struct color **, const char * const, unsigned int);
246 static void print_clean (const char *);
247 static bool is_esc (const char *);
248 static const char *get_end_of_esc (const char *);
249 static const char *get_end_of_text (const char *);
250 static void print_text (const char *, size_t);
251 static bool gather_esc_offsets (const char *, const char **, const char **);
252 static bool validate_esc_clean_all (const char **);
253 static bool validate_esc_clean (int, unsigned int, const char **, bool *);
254 static bool is_reset (int, unsigned int, const char **);
255 static bool is_bold (int, unsigned int, const char **);
256 static bool is_fg_color (int, const char **);
257 static bool is_bg_color (int, unsigned int, const char **);
258 #if !DEBUG
259 static void *malloc_wrap (size_t);
260 static void *calloc_wrap (size_t, size_t);
261 static void *realloc_wrap (void *, size_t);
262 #else
263 static void *malloc_wrap_debug (size_t, const char *, unsigned int);
264 static void *calloc_wrap_debug (size_t, size_t, const char *, unsigned int);
265 static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
266 #endif
267 static void free_wrap (void **);
268 static char *strdup_wrap (const char *, const char *, unsigned int);
269 static char *str_concat_wrap (const char *, const char *, const char *, unsigned int);
270 static bool get_bytes_size (unsigned long, struct bytes_size *);
271 static char *get_file_type (mode_t);
272 static bool has_color_name (const char *, const char *);
273 static FILE *open_file (const char *, const char *);
274 static void vfprintf_diag (const char *, ...);
275 static void vfprintf_fail (const char *, ...);
276 static void stack_var (void ***, unsigned int *, unsigned int, void *);
277 static void release_var (void **, unsigned int, void **);
278
279 extern int optind;
280
281 int
282 main (int argc, char **argv)
283 {
284 unsigned int arg_cnt;
285
286 bool bold = false;
287
288 const struct color *colors[2] = {
289 NULL, /* foreground */
290 NULL, /* background */
291 };
292
293 const char *file = NULL;
294
295 program_name = argv[0];
296 atexit (cleanup);
297
298 setvbuf (stdout, NULL, _IOLBF, 0);
299
300 #if DEBUG
301 log = open_file (DEBUG_FILE, "w");
302 #endif
303
304 process_opts (argc, argv);
305
306 arg_cnt = argc - optind;
307
308 if (clean || clean_all)
309 {
310 if (clean && clean_all)
311 vfprintf_fail (formats[FMT_GENERIC], "--clean and --clean-all switch are mutually exclusive");
312 if (arg_cnt > 1)
313 {
314 const char *format = "%s %s";
315 const char *message = "switch cannot be used with more than one file";
316 if (clean)
317 vfprintf_fail (format, "--clean", message);
318 else if (clean_all)
319 vfprintf_fail (format, "--clean-all", message);
320 }
321 }
322 else
323 {
324 if (arg_cnt == 0 || arg_cnt > 2)
325 {
326 vfprintf_diag ("%u arguments provided, expected 1-2 arguments or clean option", arg_cnt);
327 print_hint ();
328 exit (EXIT_FAILURE);
329 }
330 }
331
332 if (clean || clean_all)
333 process_file_arg (argv[optind], &file, &stream);
334 else
335 process_args (arg_cnt, &argv[optind], &bold, colors, &file, &stream);
336 read_print_stream (bold, colors, file, stream);
337
338 RELEASE_VAR (exclude);
339
340 exit (EXIT_SUCCESS);
341 }
342
343 #define PRINT_HELP_EXIT() \
344 print_help (); \
345 exit (EXIT_SUCCESS); \
346
347 #define PRINT_VERSION_EXIT() \
348 print_version (); \
349 exit (EXIT_SUCCESS); \
350
351 extern char *optarg;
352
353 static void
354 process_opts (int argc, char **argv)
355 {
356 int opt;
357 while ((opt = getopt_long (argc, argv, "hV", long_opts, NULL)) != -1)
358 {
359 switch (opt)
360 {
361 case 0: /* long opts */
362 switch (opt_type)
363 {
364 case OPT_CLEAN:
365 clean = true;
366 break;
367 case OPT_CLEAN_ALL:
368 clean_all = true;
369 break;
370 case OPT_EXCLUDE_RANDOM: {
371 bool valid = false;
372 unsigned int i;
373 exclude = xstrdup (optarg);
374 STACK_VAR (exclude);
375 for (i = 1; i < tables[GENERIC].count - 1; i++) /* skip color none and default */
376 {
377 const struct color *entry = &tables[GENERIC].entries[i];
378 if (streq (exclude, entry->name))
379 {
380 valid = true;
381 break;
382 }
383 }
384 if (!valid)
385 vfprintf_fail (formats[FMT_GENERIC], "--exclude-random switch must be provided a plain color");
386 break;
387 }
388 case OPT_HELP:
389 PRINT_HELP_EXIT ();
390 case OPT_VERSION:
391 PRINT_VERSION_EXIT ();
392 default: /* never reached */
393 ABORT_TRACE ();
394 }
395 break;
396 case 'h':
397 PRINT_HELP_EXIT ();
398 case 'V':
399 PRINT_VERSION_EXIT ();
400 case '?':
401 print_hint ();
402 exit (EXIT_FAILURE);
403 default: /* never reached */
404 ABORT_TRACE ();
405 }
406 }
407 }
408
409 static void
410 print_hint (void)
411 {
412 fprintf (stderr, "Type `%s --help' for help screen.\n", program_name);
413 }
414
415 static void
416 print_help (void)
417 {
418 struct short_opt {
419 const char *name;
420 const char *short_opt;
421 };
422 const struct short_opt short_opts[] = {
423 { "help", "h" },
424 { "version", "V" },
425 };
426 const struct option *opt = long_opts;
427 unsigned int i;
428
429 printf ("Usage: %s (foreground) OR (foreground)%c(background) OR --clean[-all] [-|file]\n\n", program_name, COLOR_SEP_CHAR);
430 printf ("\tColors (foreground) (background)\n");
431 for (i = 0; i < tables[FOREGROUND].count; i++)
432 {
433 const struct color *entry = &tables[FOREGROUND].entries[i];
434 const char *name = entry->name;
435 const char *code = entry->code;
436 if (code)
437 printf ("\t\t{\033[%s#\033[0m} [%c%c]%s%*s%s\n",
438 code, toupper (*name), *name, name + 1, 10 - (int)strlen (name), " ", name);
439 else
440 printf ("\t\t{-} %s%*s%s\n", name, 13 - (int)strlen (name), " ", name);
441 }
442 printf ("\t\t{*} [Rr]%s%*s%s [--exclude-random=<foreground color>]\n", "andom", 10 - (int)strlen ("random"), " ", "random");
443
444 printf ("\n\tFirst character of color name in upper case denotes increased intensity,\n");
445 printf ("\twhereas for lower case colors will be of normal intensity.\n");
446
447 printf ("\n\tOptions\n");
448 for (; opt->name; opt++)
449 {
450 const char *short_opt = NULL;
451 unsigned int i;
452 for (i = 0; i < sizeof (short_opts) / sizeof (struct short_opt); i++)
453 {
454 if (streq (opt->name, short_opts[i].name))
455 {
456 short_opt = short_opts[i].short_opt;
457 break;
458 }
459 }
460 if (short_opt)
461 printf ("\t\t-%s, --%s\n", short_opt, opt->name);
462 else
463 printf ("\t\t --%s\n", opt->name);
464 }
465 printf ("\n");
466 }
467
468 static void
469 print_version (void)
470 {
471 #ifdef HAVE_VERSION
472 # include "version.h"
473 #else
474 const char *version = NULL;
475 #endif
476 const char *version_prefix, *version_string;
477 const char *c_flags, *ld_flags, *cpp_flags;
478 struct bytes_size bytes_size;
479 bool debug;
480 #ifdef CFLAGS
481 c_flags = to_str (CFLAGS);
482 #else
483 c_flags = "unknown";
484 #endif
485 #ifdef LDFLAGS
486 ld_flags = to_str (LDFLAGS);
487 #else
488 ld_flags = "unknown";
489 #endif
490 #ifdef CPPFLAGS
491 cpp_flags = to_str (CPPFLAGS);
492 #else
493 cpp_flags = "unknown";
494 #endif
495 #if DEBUG
496 debug = true;
497 #else
498 debug = false;
499 #endif
500 version_prefix = version ? "" : "v";
501 version_string = version ? version : VERSION;
502 printf ("colorize %s%s (compiled at %s, %s)\n", version_prefix, version_string, __DATE__, __TIME__);
503
504 printf ("Compiler flags: %s\n", c_flags);
505 printf ("Linker flags: %s\n", ld_flags);
506 printf ("Preprocessor flags: %s\n", cpp_flags);
507 if (get_bytes_size (BUF_SIZE, &bytes_size))
508 {
509 if (BUF_SIZE % 1024 == 0)
510 printf ("Buffer size: %u%c\n", bytes_size.size, bytes_size.unit);
511 else
512 printf ("Buffer size: %u%c, %u byte%s\n", bytes_size.size, bytes_size.unit,
513 BUF_SIZE % 1024, BUF_SIZE % 1024 > 1 ? "s" : "");
514 }
515 else
516 printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE, BUF_SIZE > 1 ? "s" : "");
517 printf ("Color separator: '%c'\n", COLOR_SEP_CHAR);
518 printf ("Debugging: %s\n", debug ? "yes" : "no");
519 }
520
521 static void
522 cleanup (void)
523 {
524 if (stream && fileno (stream) != STDIN_FILENO)
525 fclose (stream);
526 #if DEBUG
527 if (log)
528 fclose (log);
529 #endif
530
531 if (vars_list)
532 {
533 unsigned int i;
534 for (i = 0; i < stacked_vars; i++)
535 free (vars_list[i]);
536 free_null (vars_list);
537 }
538 }
539
540 static void
541 free_color_names (struct color_name **color_names)
542 {
543 unsigned int i;
544 for (i = 0; color_names[i]; i++)
545 {
546 RELEASE_VAR (color_names[i]->name);
547 RELEASE_VAR (color_names[i]->orig);
548 RELEASE_VAR (color_names[i]);
549 }
550 }
551
552 static void
553 process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct color **colors, const char **file, FILE **stream)
554 {
555 int ret;
556 char *p;
557 struct stat sb;
558 struct color_name *color_names[3] = { NULL, NULL, NULL };
559
560 const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL;
561 const char *file_string = arg_cnt == 2 ? arg_strings[1] : NULL;
562
563 assert (color_string);
564
565 if (streq (color_string, "-"))
566 {
567 if (file_string)
568 vfprintf_fail (formats[FMT_GENERIC], "hyphen cannot be used as color string");
569 else
570 vfprintf_fail (formats[FMT_GENERIC], "hyphen must be preceded by color string");
571 }
572
573 ret = lstat (color_string, &sb);
574
575 /* Ensure that we don't fail if there's a file with one or more
576 color names in its path. */
577 if (ret == 0) /* success */
578 skip_path_colors (color_string, file_string, &sb);
579
580 if ((p = strchr (color_string, COLOR_SEP_CHAR)))
581 {
582 if (p == color_string)
583 vfprintf_fail (formats[FMT_STRING], "foreground color missing in string", color_string);
584 else if (p == color_string + strlen (color_string) - 1)
585 vfprintf_fail (formats[FMT_STRING], "background color missing in string", color_string);
586 else if (strchr (++p, COLOR_SEP_CHAR))
587 vfprintf_fail (formats[FMT_STRING], "one color pair allowed only for string", color_string);
588 }
589
590 gather_color_names (color_string, bold, color_names);
591
592 assert (color_names[FOREGROUND]);
593
594 if (color_names[BACKGROUND])
595 {
596 unsigned int i;
597 const unsigned int color_sets[2][2] = { { FOREGROUND, BACKGROUND }, { BACKGROUND, FOREGROUND } };
598 for (i = 0; i < 2; i++)
599 {
600 const unsigned int color1 = color_sets[i][0];
601 const unsigned int color2 = color_sets[i][1];
602 if (CHECK_COLORS_RANDOM (color1, color2))
603 vfprintf_fail (formats[FMT_RANDOM], tables[color1].desc, color_names[color1]->orig, "cannot be combined with", color_names[color2]->orig);
604 }
605 }
606
607 find_color_entries (color_names, colors);
608 free_color_names (color_names);
609
610 if (!colors[FOREGROUND]->code && colors[BACKGROUND] && colors[BACKGROUND]->code)
611 {
612 struct color_name color_name;
613 color_name.name = color_name.orig = "default";
614
615 find_color_entry (&color_name, FOREGROUND, colors);
616 }
617
618 process_file_arg (file_string, file, stream);
619 }
620
621 static void
622 process_file_arg (const char *file_string, const char **file, FILE **stream)
623 {
624 if (file_string)
625 {
626 if (streq (file_string, "-"))
627 *stream = stdin;
628 else
629 {
630 const char *file = file_string;
631 struct stat sb;
632 int ret;
633
634 errno = 0;
635 ret = stat (file, &sb);
636
637 if (ret == -1)
638 vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
639
640 if (!VALID_FILE_TYPE (sb.st_mode))
641 vfprintf_fail (formats[FMT_TYPE], file, "unrecognized type", get_file_type (sb.st_mode));
642
643 *stream = open_file (file, "r");
644 }
645 *file = file_string;
646 }
647 else
648 {
649 *stream = stdin;
650 *file = "stdin";
651 }
652
653 assert (*stream);
654 assert (*file);
655 }
656
657 static void
658 skip_path_colors (const char *color_string, const char *file_string, const struct stat *sb)
659 {
660 bool have_file;
661 unsigned int c;
662 const char *color = color_string;
663 const mode_t mode = sb->st_mode;
664
665 for (c = 1; c <= 2 && *color; c++)
666 {
667 bool matched = false;
668 unsigned int i;
669 for (i = 0; i < tables[GENERIC].count; i++)
670 {
671 const struct color *entry = &tables[GENERIC].entries[i];
672 if (has_color_name (color, entry->name))
673 {
674 color += strlen (entry->name);
675 matched = true;
676 break;
677 }
678 }
679 if (!matched && has_color_name (color, "random"))
680 {
681 color += strlen ("random");
682 matched = true;
683 }
684 if (matched && *color == COLOR_SEP_CHAR && *(color + 1))
685 color++;
686 else
687 break;
688 }
689
690 have_file = (*color != '\0');
691
692 if (have_file)
693 {
694 const char *file_exists = color_string;
695 if (file_string)
696 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "cannot be used as color string");
697 else
698 {
699 if (VALID_FILE_TYPE (mode))
700 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "must be preceded by color string");
701 else
702 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "is not a valid file type");
703 }
704 }
705 }
706
707 static void
708 gather_color_names (const char *color_string, bool *bold, struct color_name **color_names)
709 {
710 unsigned int index;
711 char *color, *p, *str;
712
713 str = xstrdup (color_string);
714 STACK_VAR (str);
715
716 for (index = 0, color = str; *color; index++, color = p)
717 {
718 char *ch, *sep;
719
720 p = NULL;
721 if ((sep = strchr (color, COLOR_SEP_CHAR)))
722 {
723 *sep = '\0';
724 p = sep + 1;
725 }
726 else
727 p = color + strlen (color);
728 assert (p);
729
730 for (ch = color; *ch; ch++)
731 if (!isalpha (*ch))
732 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be made of non-alphabetic characters");
733
734 for (ch = color + 1; *ch; ch++)
735 if (!islower (*ch))
736 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be in mixed lower/upper case");
737
738 if (streq (color, "None"))
739 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be bold");
740
741 if (isupper (*color))
742 {
743 switch (index)
744 {
745 case FOREGROUND:
746 *bold = true;
747 break;
748 case BACKGROUND:
749 vfprintf_fail (formats[FMT_COLOR], tables[BACKGROUND].desc, color, "cannot be bold");
750 default: /* never reached */
751 ABORT_TRACE ();
752 }
753 }
754
755 color_names[index] = xcalloc (1, sizeof (struct color_name));
756 STACK_VAR (color_names[index]);
757
758 color_names[index]->orig = xstrdup (color);
759 STACK_VAR (color_names[index]->orig);
760
761 for (ch = color; *ch; ch++)
762 *ch = tolower (*ch);
763
764 color_names[index]->name = xstrdup (color);
765 STACK_VAR (color_names[index]->name);
766 }
767
768 RELEASE_VAR (str);
769 }
770
771 static void
772 read_print_stream (bool bold, const struct color **colors, const char *file, FILE *stream)
773 {
774 char buf[BUF_SIZE + 1];
775 unsigned int flags = 0;
776
777 while (!feof (stream))
778 {
779 size_t bytes_read;
780 char *eol;
781 const char *line;
782 bytes_read = fread (buf, 1, BUF_SIZE, stream);
783 if (bytes_read != BUF_SIZE && ferror (stream))
784 vfprintf_fail (formats[FMT_ERROR], BUF_SIZE, "read");
785 buf[bytes_read] = '\0';
786 line = buf;
787 while ((eol = strpbrk (line, "\n\r")))
788 {
789 const char *p;
790 flags &= ~(CR|LF);
791 if (*eol == '\r')
792 {
793 flags |= CR;
794 if (*(eol + 1) == '\n')
795 flags |= LF;
796 }
797 else if (*eol == '\n')
798 flags |= LF;
799 else
800 vfprintf_fail (formats[FMT_FILE], file, "unrecognized line ending");
801 p = eol + SKIP_LINE_ENDINGS (flags);
802 *eol = '\0';
803 print_line (bold, colors, line, flags);
804 line = p;
805 }
806 if (feof (stream))
807 {
808 if (*line != '\0')
809 print_line (bold, colors, line, 0);
810 }
811 else if (*line != '\0')
812 {
813 char *p;
814 if ((clean || clean_all) && (p = strrchr (line, '\033')))
815 merge_print_line (line, p, stream);
816 else
817 print_line (bold, colors, line, 0);
818 }
819 }
820 }
821
822 static void
823 merge_print_line (const char *line, const char *p, FILE *stream)
824 {
825 char *buf = NULL;
826 char *merged_esc = NULL;
827 const char *esc = "";
828 const char char_restore = *p;
829
830 complete_part_line (p + 1, &buf, stream);
831
832 if (buf)
833 {
834 /* form escape sequence */
835 esc = merged_esc = str_concat (p, buf);
836 /* shorten partial line accordingly */
837 *(char *)p = '\0';
838 free (buf);
839 }
840
841 #ifdef TEST_MERGE_PART_LINE
842 printf ("%s%s", line, esc);
843 fflush (stdout);
844 _exit (EXIT_SUCCESS);
845 #else
846 print_clean (line);
847 *(char *)p = char_restore;
848 print_clean (esc);
849 free (merged_esc);
850 #endif
851 }
852
853 static void
854 complete_part_line (const char *p, char **buf, FILE *stream)
855 {
856 bool got_next_char = false, read_from_stream;
857 char ch;
858 size_t i = 0, size;
859
860 if (get_next_char (&ch, &p, stream, &read_from_stream))
861 {
862 if (ch == '[')
863 {
864 if (read_from_stream)
865 save_char (ch, buf, &i, &size);
866 }
867 else
868 {
869 if (read_from_stream)
870 ungetc ((int)ch, stream);
871 return; /* cancel */
872 }
873 }
874 else
875 return; /* cancel */
876
877 while (get_next_char (&ch, &p, stream, &read_from_stream))
878 {
879 if (isdigit (ch) || ch == ';')
880 {
881 if (read_from_stream)
882 save_char (ch, buf, &i, &size);
883 }
884 else /* read next character */
885 {
886 got_next_char = true;
887 break;
888 }
889 }
890
891 if (got_next_char)
892 {
893 if (ch == 'm')
894 {
895 if (read_from_stream)
896 save_char (ch, buf, &i, &size);
897 }
898 else
899 {
900 if (read_from_stream)
901 ungetc ((int)ch, stream);
902 return; /* cancel */
903 }
904 }
905 else
906 return; /* cancel */
907 }
908
909 static bool
910 get_next_char (char *ch, const char **p, FILE *stream, bool *read_from_stream)
911 {
912 if (**p == '\0')
913 {
914 int c;
915 if ((c = fgetc (stream)) != EOF)
916 {
917 *ch = (char)c;
918 *read_from_stream = true;
919 return true;
920 }
921 else
922 {
923 *read_from_stream = false;
924 return false;
925 }
926 }
927 else
928 {
929 *ch = **p;
930 (*p)++;
931 *read_from_stream = false;
932 return true;
933 }
934 }
935
936 static void
937 save_char (char ch, char **buf, size_t *i, size_t *size)
938 {
939 if (!*buf)
940 {
941 *size = ALLOC_COMPLETE_PART_LINE;
942 *buf = xmalloc (*size);
943 }
944 /* +1: effective occupied size of buffer */
945 else if ((*i + 1) == *size)
946 {
947 *size *= 2;
948 *buf = xrealloc (*buf, *size);
949 }
950 (*buf)[*i] = ch;
951 (*buf)[*i + 1] = '\0';
952 (*i)++;
953 }
954
955 static void
956 find_color_entries (struct color_name **color_names, const struct color **colors)
957 {
958 struct timeval tv;
959 unsigned int index;
960
961 /* randomness */
962 gettimeofday (&tv, NULL);
963 srand (tv.tv_usec * tv.tv_sec);
964
965 for (index = 0; color_names[index]; index++)
966 {
967 const char *color_name = color_names[index]->name;
968
969 const unsigned int count = tables[index].count;
970 const struct color *const color_entries = tables[index].entries;
971
972 if (streq (color_name, "random"))
973 {
974 bool excludable;
975 unsigned int i;
976 do {
977 excludable = false;
978 i = rand() % (count - 2) + 1; /* omit color none and default */
979 switch (index)
980 {
981 case FOREGROUND:
982 /* --exclude-random */
983 if (exclude && streq (exclude, color_entries[i].name))
984 excludable = true;
985 else if (color_names[BACKGROUND] && streq (color_names[BACKGROUND]->name, color_entries[i].name))
986 excludable = true;
987 break;
988 case BACKGROUND:
989 if (streq (colors[FOREGROUND]->name, color_entries[i].name))
990 excludable = true;
991 break;
992 default: /* never reached */
993 ABORT_TRACE ();
994 }
995 } while (excludable);
996 colors[index] = (struct color *)&color_entries[i];
997 }
998 else
999 find_color_entry (color_names[index], index, colors);
1000 }
1001 }
1002
1003 static void
1004 find_color_entry (const struct color_name *color_name, unsigned int index, const struct color **colors)
1005 {
1006 bool found = false;
1007 unsigned int i;
1008
1009 const unsigned int count = tables[index].count;
1010 const struct color *const color_entries = tables[index].entries;
1011
1012 for (i = 0; i < count; i++)
1013 if (streq (color_name->name, color_entries[i].name))
1014 {
1015 colors[index] = (struct color *)&color_entries[i];
1016 found = true;
1017 break;
1018 }
1019 if (!found)
1020 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color_name->orig, "not recognized");
1021 }
1022
1023 static void
1024 print_line (bool bold, const struct color **colors, const char *const line, unsigned int flags)
1025 {
1026 /* --clean[-all] */
1027 if (clean || clean_all)
1028 print_clean (line);
1029 else
1030 {
1031 /* Foreground color code is guaranteed to be set when background color code is present. */
1032 if (colors[BACKGROUND] && colors[BACKGROUND]->code)
1033 printf ("\033[%s", colors[BACKGROUND]->code);
1034 if (colors[FOREGROUND]->code)
1035 printf ("\033[%s%s%s\033[0m", bold ? "1;" : "", colors[FOREGROUND]->code, line);
1036 else
1037 printf (formats[FMT_GENERIC], line);
1038 }
1039 if (flags & CR)
1040 putchar ('\r');
1041 if (flags & LF)
1042 putchar ('\n');
1043 }
1044
1045 static void
1046 print_clean (const char *line)
1047 {
1048 const char *p = line;
1049
1050 if (is_esc (p))
1051 p = get_end_of_esc (p);
1052
1053 while (*p != '\0')
1054 {
1055 const char *text_start = p;
1056 const char *text_end = get_end_of_text (p);
1057 print_text (text_start, text_end - text_start);
1058 p = get_end_of_esc (text_end);
1059 }
1060 }
1061
1062 static bool
1063 is_esc (const char *p)
1064 {
1065 return gather_esc_offsets (p, NULL, NULL);
1066 }
1067
1068 static const char *
1069 get_end_of_esc (const char *p)
1070 {
1071 const char *esc;
1072 const char *end = NULL;
1073 while ((esc = strchr (p, '\033')))
1074 {
1075 if (gather_esc_offsets (esc, NULL, &end))
1076 break;
1077 p = esc + 1;
1078 }
1079 return end ? end + 1 : p + strlen (p);
1080 }
1081
1082 static const char *
1083 get_end_of_text (const char *p)
1084 {
1085 const char *esc;
1086 const char *start = NULL;
1087 while ((esc = strchr (p, '\033')))
1088 {
1089 if (gather_esc_offsets (esc, &start, NULL))
1090 break;
1091 p = esc + 1;
1092 }
1093 return start ? start : p + strlen (p);
1094 }
1095
1096 static void
1097 print_text (const char *p, size_t len)
1098 {
1099 size_t bytes_written;
1100 bytes_written = fwrite (p, 1, len, stdout);
1101 if (bytes_written != len)
1102 vfprintf_fail (formats[FMT_ERROR], (unsigned long)len, "written");
1103 }
1104
1105 static bool
1106 gather_esc_offsets (const char *p, const char **start, const char **end)
1107 {
1108 /* ESC[ */
1109 if (*p == 27 && *(p + 1) == '[')
1110 {
1111 bool valid = false;
1112 const char *const begin = p;
1113 p += 2;
1114 if (clean_all)
1115 valid = validate_esc_clean_all (&p);
1116 else if (clean)
1117 {
1118 bool check_values;
1119 unsigned int iter = 0;
1120 const char *digit;
1121 do {
1122 check_values = false;
1123 iter++;
1124 if (!isdigit (*p))
1125 break;
1126 digit = p;
1127 while (isdigit (*p))
1128 p++;
1129 if (p - digit > 2)
1130 break;
1131 else /* check range */
1132 {
1133 char val[3];
1134 int value;
1135 unsigned int i;
1136 const unsigned int digits = p - digit;
1137 for (i = 0; i < digits; i++)
1138 val[i] = *digit++;
1139 val[i] = '\0';
1140 value = atoi (val);
1141 valid = validate_esc_clean (value, iter, &p, &check_values);
1142 }
1143 } while (check_values);
1144 }
1145 if (valid)
1146 {
1147 if (start)
1148 *start = begin;
1149 if (end)
1150 *end = p;
1151 return true;
1152 }
1153 }
1154 return false;
1155 }
1156
1157 static bool
1158 validate_esc_clean_all (const char **p)
1159 {
1160 while (isdigit (**p) || **p == ';')
1161 (*p)++;
1162 return (**p == 'm');
1163 }
1164
1165 static bool
1166 validate_esc_clean (int value, unsigned int iter, const char **p, bool *check_values)
1167 {
1168 if (is_reset (value, iter, p))
1169 return true;
1170 else if (is_bold (value, iter, p))
1171 {
1172 (*p)++;
1173 *check_values = true;
1174 return false; /* partial escape sequence, need another valid value */
1175 }
1176 else if (is_fg_color (value, p))
1177 return true;
1178 else if (is_bg_color (value, iter, p))
1179 return true;
1180 else
1181 return false;
1182 }
1183
1184 static bool
1185 is_reset (int value, unsigned int iter, const char **p)
1186 {
1187 return (value == 0 && iter == 1 && **p == 'm');
1188 }
1189
1190 static bool
1191 is_bold (int value, unsigned int iter, const char **p)
1192 {
1193 return (value == 1 && iter == 1 && **p == ';');
1194 }
1195
1196 static bool
1197 is_fg_color (int value, const char **p)
1198 {
1199 return (((value >= 30 && value <= 37) || value == 39) && **p == 'm');
1200 }
1201
1202 static bool
1203 is_bg_color (int value, unsigned int iter, const char **p)
1204 {
1205 return (((value >= 40 && value <= 47) || value == 49) && iter == 1 && **p == 'm');
1206 }
1207
1208 #if !DEBUG
1209 static void *
1210 malloc_wrap (size_t size)
1211 {
1212 void *p = malloc (size);
1213 if (!p)
1214 MEM_ALLOC_FAIL ();
1215 return p;
1216 }
1217
1218 static void *
1219 calloc_wrap (size_t nmemb, size_t size)
1220 {
1221 void *p = calloc (nmemb, size);
1222 if (!p)
1223 MEM_ALLOC_FAIL ();
1224 return p;
1225 }
1226
1227 static void *
1228 realloc_wrap (void *ptr, size_t size)
1229 {
1230 void *p = realloc (ptr, size);
1231 if (!p)
1232 MEM_ALLOC_FAIL ();
1233 return p;
1234 }
1235 #else
1236 static void *
1237 malloc_wrap_debug (size_t size, const char *file, unsigned int line)
1238 {
1239 void *p = malloc (size);
1240 if (!p)
1241 MEM_ALLOC_FAIL_DEBUG (file, line);
1242 fprintf (log, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
1243 return p;
1244 }
1245
1246 static void *
1247 calloc_wrap_debug (size_t nmemb, size_t size, const char *file, unsigned int line)
1248 {
1249 void *p = calloc (nmemb, size);
1250 if (!p)
1251 MEM_ALLOC_FAIL_DEBUG (file, line);
1252 fprintf (log, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)(nmemb * size), file, line);
1253 return p;
1254 }
1255
1256 static void *
1257 realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
1258 {
1259 void *p = realloc (ptr, size);
1260 if (!p)
1261 MEM_ALLOC_FAIL_DEBUG (file, line);
1262 fprintf (log, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
1263 return p;
1264 }
1265 #endif /* !DEBUG */
1266
1267 static void
1268 free_wrap (void **ptr)
1269 {
1270 free (*ptr);
1271 *ptr = NULL;
1272 }
1273
1274 #if !DEBUG
1275 # define do_malloc(len, file, line) malloc_wrap(len)
1276 #else
1277 # define do_malloc(len, file, line) malloc_wrap_debug(len, file, line)
1278 #endif
1279
1280 static char *
1281 strdup_wrap (const char *str, const char *file, unsigned int line)
1282 {
1283 const size_t len = strlen (str) + 1;
1284 char *p = do_malloc (len, file, line);
1285 strncpy (p, str, len);
1286 return p;
1287 }
1288
1289 static char *
1290 str_concat_wrap (const char *str1, const char *str2, const char *file, unsigned int line)
1291 {
1292 const size_t len = strlen (str1) + strlen (str2) + 1;
1293 char *p, *str;
1294
1295 p = str = do_malloc (len, file, line);
1296 strncpy (p, str1, strlen (str1));
1297 p += strlen (str1);
1298 strncpy (p, str2, strlen (str2));
1299 p += strlen (str2);
1300 *p = '\0';
1301
1302 return str;
1303 }
1304
1305 static bool
1306 get_bytes_size (unsigned long bytes, struct bytes_size *bytes_size)
1307 {
1308 const char *unit, units[] = { '0', 'K', 'M', 'G', '\0' };
1309 unsigned long size = bytes;
1310 if (bytes < 1024)
1311 return false;
1312 unit = units;
1313 while (size >= 1024 && *(unit + 1))
1314 {
1315 size /= 1024;
1316 unit++;
1317 }
1318 bytes_size->size = (unsigned int)size;
1319 bytes_size->unit = *unit;
1320 return true;
1321 }
1322
1323 static char *
1324 get_file_type (mode_t mode)
1325 {
1326 if (S_ISREG (mode))
1327 return "file";
1328 else if (S_ISDIR (mode))
1329 return "directory";
1330 else if (S_ISCHR (mode))
1331 return "character device";
1332 else if (S_ISBLK (mode))
1333 return "block device";
1334 else if (S_ISFIFO (mode))
1335 return "named pipe";
1336 else if (S_ISLNK (mode))
1337 return "symbolic link";
1338 else if (S_ISSOCK (mode))
1339 return "socket";
1340 else
1341 return "file";
1342 }
1343
1344 static bool
1345 has_color_name (const char *str, const char *name)
1346 {
1347 char *p;
1348
1349 assert (strlen (str));
1350 assert (strlen (name));
1351
1352 if (!(*str == *name || *str == toupper (*name)))
1353 return false;
1354 else if (*(name + 1) != '\0'
1355 && !((p = strstr (str + 1, name + 1)) && p == str + 1))
1356 return false;
1357
1358 return true;
1359 }
1360
1361 static FILE *
1362 open_file (const char *file, const char *mode)
1363 {
1364 FILE *stream;
1365
1366 errno = 0;
1367 stream = fopen (file, mode);
1368 if (!stream)
1369 vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
1370
1371 return stream;
1372 }
1373
1374 #define DO_VFPRINTF(fmt) \
1375 va_list ap; \
1376 fprintf (stderr, "%s: ", program_name); \
1377 va_start (ap, fmt); \
1378 vfprintf (stderr, fmt, ap); \
1379 va_end (ap); \
1380 fprintf (stderr, "\n"); \
1381
1382 static void
1383 vfprintf_diag (const char *fmt, ...)
1384 {
1385 DO_VFPRINTF (fmt);
1386 }
1387
1388 static void
1389 vfprintf_fail (const char *fmt, ...)
1390 {
1391 DO_VFPRINTF (fmt);
1392 exit (EXIT_FAILURE);
1393 }
1394
1395 static void
1396 stack_var (void ***list, unsigned int *stacked, unsigned int index, void *ptr)
1397 {
1398 /* nothing to stack */
1399 if (ptr == NULL)
1400 return;
1401 if (!*list)
1402 *list = xmalloc (sizeof (void *));
1403 else
1404 {
1405 unsigned int i;
1406 for (i = 0; i < *stacked; i++)
1407 if (!(*list)[i])
1408 {
1409 (*list)[i] = ptr;
1410 return; /* reused */
1411 }
1412 *list = xrealloc (*list, (*stacked + 1) * sizeof (void *));
1413 }
1414 (*list)[index] = ptr;
1415 (*stacked)++;
1416 }
1417
1418 static void
1419 release_var (void **list, unsigned int stacked, void **ptr)
1420 {
1421 unsigned int i;
1422 /* nothing to release */
1423 if (*ptr == NULL)
1424 return;
1425 for (i = 0; i < stacked; i++)
1426 if (list[i] == *ptr)
1427 {
1428 free (*ptr);
1429 *ptr = NULL;
1430 list[i] = NULL;
1431 return;
1432 }
1433 }