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