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