]> git.refcnt.org Git - colorize.git/blob - colorize.c
Include all flags in version output
[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 PRINT_HELP_EXIT() \
343 print_help (); \
344 exit (EXIT_SUCCESS); \
345
346 #define PRINT_VERSION_EXIT() \
347 print_version (); \
348 exit (EXIT_SUCCESS); \
349
350 extern char *optarg;
351
352 static void
353 process_opts (int argc, char **argv)
354 {
355 int opt;
356 while ((opt = getopt_long (argc, argv, "hV", long_opts, NULL)) != -1)
357 {
358 switch (opt)
359 {
360 case 0: /* long opts */
361 switch (opt_type)
362 {
363 case OPT_CLEAN:
364 clean = true;
365 break;
366 case OPT_CLEAN_ALL:
367 clean_all = true;
368 break;
369 case OPT_EXCLUDE_RANDOM: {
370 bool valid = false;
371 unsigned int i;
372 exclude = xstrdup (optarg);
373 STACK_VAR (exclude);
374 for (i = 1; i < tables[GENERIC].count - 1; i++) /* skip color none and default */
375 {
376 const struct color *entry = &tables[GENERIC].entries[i];
377 if (streq (exclude, entry->name))
378 {
379 valid = true;
380 break;
381 }
382 }
383 if (!valid)
384 vfprintf_fail (formats[FMT_GENERIC], "--exclude-random switch must be provided a plain color");
385 break;
386 }
387 case OPT_HELP:
388 PRINT_HELP_EXIT ();
389 case OPT_VERSION:
390 PRINT_VERSION_EXIT ();
391 default: /* never reached */
392 ABORT_TRACE ();
393 }
394 break;
395 case 'h':
396 PRINT_HELP_EXIT ();
397 case 'V':
398 PRINT_VERSION_EXIT ();
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, *ld_flags, *cpp_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 #ifdef LDFLAGS
485 ld_flags = to_str (LDFLAGS);
486 #else
487 ld_flags = "unknown";
488 #endif
489 #ifdef CPPFLAGS
490 cpp_flags = to_str (CPPFLAGS);
491 #else
492 cpp_flags = "unknown";
493 #endif
494 #if DEBUG
495 debug = true;
496 #else
497 debug = false;
498 #endif
499 version_prefix = version ? "" : "v";
500 version_string = version ? version : VERSION;
501 printf ("colorize %s%s (compiled at %s, %s)\n", version_prefix, version_string, __DATE__, __TIME__);
502
503 printf ("Compiler flags: %s\n", c_flags);
504 printf ("Linker flags: %s\n", ld_flags);
505 printf ("Preprocessor flags: %s\n", cpp_flags);
506 if (get_bytes_size (BUF_SIZE, &bytes_size))
507 {
508 if (BUF_SIZE % 1024 == 0)
509 printf ("Buffer size: %u%c\n", bytes_size.size, bytes_size.unit);
510 else
511 printf ("Buffer size: %u%c, %u byte%s\n", bytes_size.size, bytes_size.unit,
512 BUF_SIZE % 1024, BUF_SIZE % 1024 > 1 ? "s" : "");
513 }
514 else
515 printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE, BUF_SIZE > 1 ? "s" : "");
516 printf ("Color separator: '%c'\n", COLOR_SEP_CHAR);
517 printf ("Debugging: %s\n", debug ? "yes" : "no");
518 }
519
520 static void
521 cleanup (void)
522 {
523 if (stream && fileno (stream) != STDIN_FILENO)
524 fclose (stream);
525 #if DEBUG
526 if (log)
527 fclose (log);
528 #endif
529
530 if (vars_list)
531 {
532 unsigned int i;
533 for (i = 0; i < stacked_vars; i++)
534 free (vars_list[i]);
535 free_null (vars_list);
536 }
537 }
538
539 static void
540 free_color_names (struct color_name **color_names)
541 {
542 unsigned int i;
543 for (i = 0; color_names[i]; i++)
544 {
545 RELEASE_VAR (color_names[i]->name);
546 RELEASE_VAR (color_names[i]->orig);
547 RELEASE_VAR (color_names[i]);
548 }
549 }
550
551 static void
552 process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct color **colors, const char **file, FILE **stream)
553 {
554 int ret;
555 char *p;
556 struct stat sb;
557 struct color_name *color_names[3] = { NULL, NULL, NULL };
558
559 const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL;
560 const char *file_string = arg_cnt == 2 ? arg_strings[1] : NULL;
561
562 assert (color_string);
563
564 if (streq (color_string, "-"))
565 {
566 if (file_string)
567 vfprintf_fail (formats[FMT_GENERIC], "hyphen cannot be used as color string");
568 else
569 vfprintf_fail (formats[FMT_GENERIC], "hyphen must be preceded by color string");
570 }
571
572 ret = lstat (color_string, &sb);
573
574 /* Ensure that we don't fail if there's a file with one or more
575 color names in its path. */
576 if (ret == 0) /* success */
577 skip_path_colors (color_string, file_string, &sb);
578
579 if ((p = strchr (color_string, COLOR_SEP_CHAR)))
580 {
581 if (p == color_string)
582 vfprintf_fail (formats[FMT_STRING], "foreground color missing in string", color_string);
583 else if (p == color_string + strlen (color_string) - 1)
584 vfprintf_fail (formats[FMT_STRING], "background color missing in string", color_string);
585 else if (strchr (++p, COLOR_SEP_CHAR))
586 vfprintf_fail (formats[FMT_STRING], "one color pair allowed only for string", color_string);
587 }
588
589 gather_color_names (color_string, bold, color_names);
590
591 assert (color_names[FOREGROUND]);
592
593 if (color_names[BACKGROUND])
594 {
595 unsigned int i;
596 const unsigned int color_sets[2][2] = { { FOREGROUND, BACKGROUND }, { BACKGROUND, FOREGROUND } };
597 for (i = 0; i < 2; i++)
598 {
599 const unsigned int color1 = color_sets[i][0];
600 const unsigned int color2 = color_sets[i][1];
601 if (CHECK_COLORS_RANDOM (color1, color2))
602 vfprintf_fail (formats[FMT_RANDOM], tables[color1].desc, color_names[color1]->orig, "cannot be combined with", color_names[color2]->orig);
603 }
604 }
605
606 find_color_entries (color_names, colors);
607 free_color_names (color_names);
608
609 if (!colors[FOREGROUND]->code && colors[BACKGROUND] && colors[BACKGROUND]->code)
610 {
611 struct color_name color_name;
612 color_name.name = color_name.orig = "default";
613
614 find_color_entry (&color_name, FOREGROUND, colors);
615 }
616
617 process_file_arg (file_string, file, stream);
618 }
619
620 static void
621 process_file_arg (const char *file_string, const char **file, FILE **stream)
622 {
623 if (file_string)
624 {
625 if (streq (file_string, "-"))
626 *stream = stdin;
627 else
628 {
629 const char *file = file_string;
630 struct stat sb;
631 int ret;
632
633 errno = 0;
634 ret = stat (file, &sb);
635
636 if (ret == -1)
637 vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
638
639 if (!VALID_FILE_TYPE (sb.st_mode))
640 vfprintf_fail (formats[FMT_TYPE], file, "unrecognized type", get_file_type (sb.st_mode));
641
642 *stream = open_file (file, "r");
643 }
644 *file = file_string;
645 }
646 else
647 {
648 *stream = stdin;
649 *file = "stdin";
650 }
651
652 assert (*stream);
653 assert (*file);
654 }
655
656 static void
657 skip_path_colors (const char *color_string, const char *file_string, const struct stat *sb)
658 {
659 bool have_file;
660 unsigned int c;
661 const char *color = color_string;
662 const mode_t mode = sb->st_mode;
663
664 for (c = 1; c <= 2 && *color; c++)
665 {
666 bool matched = false;
667 unsigned int i;
668 for (i = 0; i < tables[GENERIC].count; i++)
669 {
670 const struct color *entry = &tables[GENERIC].entries[i];
671 if (has_color_name (color, entry->name))
672 {
673 color += strlen (entry->name);
674 matched = true;
675 break;
676 }
677 }
678 if (!matched && has_color_name (color, "random"))
679 {
680 color += strlen ("random");
681 matched = true;
682 }
683 if (matched && *color == COLOR_SEP_CHAR && *(color + 1))
684 color++;
685 else
686 break;
687 }
688
689 have_file = (*color != '\0');
690
691 if (have_file)
692 {
693 const char *file_exists = color_string;
694 if (file_string)
695 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "cannot be used as color string");
696 else
697 {
698 if (VALID_FILE_TYPE (mode))
699 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "must be preceded by color string");
700 else
701 vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "is not a valid file type");
702 }
703 }
704 }
705
706 static void
707 gather_color_names (const char *color_string, bool *bold, struct color_name **color_names)
708 {
709 unsigned int index;
710 char *color, *p, *str;
711
712 str = xstrdup (color_string);
713 STACK_VAR (str);
714
715 for (index = 0, color = str; *color; index++, color = p)
716 {
717 char *ch, *sep;
718
719 p = NULL;
720 if ((sep = strchr (color, COLOR_SEP_CHAR)))
721 {
722 *sep = '\0';
723 p = sep + 1;
724 }
725 else
726 p = color + strlen (color);
727 assert (p);
728
729 for (ch = color; *ch; ch++)
730 if (!isalpha (*ch))
731 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be made of non-alphabetic characters");
732
733 for (ch = color + 1; *ch; ch++)
734 if (!islower (*ch))
735 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be in mixed lower/upper case");
736
737 if (streq (color, "None"))
738 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be bold");
739
740 if (isupper (*color))
741 {
742 switch (index)
743 {
744 case FOREGROUND:
745 *bold = true;
746 break;
747 case BACKGROUND:
748 vfprintf_fail (formats[FMT_COLOR], tables[BACKGROUND].desc, color, "cannot be bold");
749 default: /* never reached */
750 ABORT_TRACE ();
751 }
752 }
753
754 color_names[index] = xcalloc (1, sizeof (struct color_name));
755 STACK_VAR (color_names[index]);
756
757 color_names[index]->orig = xstrdup (color);
758 STACK_VAR (color_names[index]->orig);
759
760 for (ch = color; *ch; ch++)
761 *ch = tolower (*ch);
762
763 color_names[index]->name = xstrdup (color);
764 STACK_VAR (color_names[index]->name);
765 }
766
767 RELEASE_VAR (str);
768 }
769
770 static void
771 read_print_stream (bool bold, const struct color **colors, const char *file, FILE *stream)
772 {
773 char buf[BUF_SIZE + 1];
774 unsigned int flags = 0;
775
776 while (!feof (stream))
777 {
778 size_t bytes_read;
779 char *eol;
780 const char *line;
781 bytes_read = fread (buf, 1, BUF_SIZE, stream);
782 if (bytes_read != BUF_SIZE && ferror (stream))
783 vfprintf_fail (formats[FMT_ERROR], BUF_SIZE, "read");
784 buf[bytes_read] = '\0';
785 line = buf;
786 while ((eol = strpbrk (line, "\n\r")))
787 {
788 char *p;
789 flags &= ~(CR|LF);
790 if (*eol == '\r')
791 {
792 flags |= CR;
793 if (*(eol + 1) == '\n')
794 flags |= LF;
795 }
796 else if (*eol == '\n')
797 flags |= LF;
798 else
799 vfprintf_fail (formats[FMT_FILE], file, "unrecognized line ending");
800 p = eol + SKIP_LINE_ENDINGS (flags);
801 *eol = '\0';
802 print_line (bold, colors, line, flags);
803 line = p;
804 }
805 if (feof (stream))
806 {
807 if (*line != '\0')
808 print_line (bold, colors, line, 0);
809 }
810 else if (*line != '\0')
811 {
812 char *p;
813 if ((clean || clean_all) && (p = strrchr (line, '\033')))
814 merge_print_line (line, p, stream);
815 else
816 print_line (bold, colors, line, 0);
817 }
818 }
819 }
820
821 static void
822 merge_print_line (const char *line, const char *p, FILE *stream)
823 {
824 char *buf = NULL;
825 char *merged_esc = NULL;
826 const char *esc = "";
827 const char char_restore = *p;
828
829 complete_part_line (p + 1, &buf, stream);
830
831 if (buf)
832 {
833 /* form escape sequence */
834 esc = merged_esc = str_concat (p, buf);
835 /* shorten partial line accordingly */
836 *(char *)p = '\0';
837 free (buf);
838 }
839
840 #ifdef TEST_MERGE_PART_LINE
841 printf ("%s%s", line, esc);
842 fflush (stdout);
843 _exit (EXIT_SUCCESS);
844 #else
845 print_clean (line);
846 *(char *)p = char_restore;
847 print_clean (esc);
848 free (merged_esc);
849 #endif
850 }
851
852 static void
853 complete_part_line (const char *p, char **buf, FILE *stream)
854 {
855 bool got_next_char = false, read_from_stream;
856 char ch;
857 size_t i = 0, size;
858
859 if (get_next_char (&ch, &p, stream, &read_from_stream))
860 {
861 if (ch == '[')
862 {
863 if (read_from_stream)
864 save_char (ch, buf, &i, &size);
865 }
866 else
867 {
868 if (read_from_stream)
869 ungetc ((int)ch, stream);
870 return; /* cancel */
871 }
872 }
873 else
874 return; /* cancel */
875
876 while (get_next_char (&ch, &p, stream, &read_from_stream))
877 {
878 if (isdigit (ch) || ch == ';')
879 {
880 if (read_from_stream)
881 save_char (ch, buf, &i, &size);
882 }
883 else /* read next character */
884 {
885 got_next_char = true;
886 break;
887 }
888 }
889
890 if (got_next_char)
891 {
892 if (ch == 'm')
893 {
894 if (read_from_stream)
895 save_char (ch, buf, &i, &size);
896 }
897 else
898 {
899 if (read_from_stream)
900 ungetc ((int)ch, stream);
901 return; /* cancel */
902 }
903 }
904 else
905 return; /* cancel */
906 }
907
908 static bool
909 get_next_char (char *ch, const char **p, FILE *stream, bool *read_from_stream)
910 {
911 if (**p == '\0')
912 {
913 int c;
914 if ((c = fgetc (stream)) != EOF)
915 {
916 *ch = (char)c;
917 *read_from_stream = true;
918 return true;
919 }
920 else
921 {
922 *read_from_stream = false;
923 return false;
924 }
925 }
926 else
927 {
928 *ch = **p;
929 (*p)++;
930 *read_from_stream = false;
931 return true;
932 }
933 }
934
935 static void
936 save_char (char ch, char **buf, size_t *i, size_t *size)
937 {
938 if (!*buf)
939 {
940 *size = ALLOC_COMPLETE_PART_LINE;
941 *buf = xmalloc (*size);
942 }
943 /* +1: effective occupied size of buffer */
944 else if ((*i + 1) == *size)
945 {
946 *size *= 2;
947 *buf = xrealloc (*buf, *size);
948 }
949 (*buf)[*i] = ch;
950 (*buf)[*i + 1] = '\0';
951 (*i)++;
952 }
953
954 static void
955 find_color_entries (struct color_name **color_names, const struct color **colors)
956 {
957 struct timeval tv;
958 unsigned int index;
959
960 /* randomness */
961 gettimeofday (&tv, NULL);
962 srand (tv.tv_usec * tv.tv_sec);
963
964 for (index = 0; color_names[index]; index++)
965 {
966 const char *color_name = color_names[index]->name;
967
968 const unsigned int count = tables[index].count;
969 const struct color *const color_entries = tables[index].entries;
970
971 if (streq (color_name, "random"))
972 {
973 bool excludable;
974 unsigned int i;
975 do {
976 excludable = false;
977 i = rand() % (count - 2) + 1; /* omit color none and default */
978 switch (index)
979 {
980 case FOREGROUND:
981 /* --exclude-random */
982 if (exclude && streq (exclude, color_entries[i].name))
983 excludable = true;
984 else if (color_names[BACKGROUND] && streq (color_names[BACKGROUND]->name, color_entries[i].name))
985 excludable = true;
986 break;
987 case BACKGROUND:
988 if (streq (colors[FOREGROUND]->name, color_entries[i].name))
989 excludable = true;
990 break;
991 default: /* never reached */
992 ABORT_TRACE ();
993 }
994 } while (excludable);
995 colors[index] = (struct color *)&color_entries[i];
996 }
997 else
998 find_color_entry (color_names[index], index, colors);
999 }
1000 }
1001
1002 static void
1003 find_color_entry (const struct color_name *color_name, unsigned int index, const struct color **colors)
1004 {
1005 bool found = false;
1006 unsigned int i;
1007
1008 const unsigned int count = tables[index].count;
1009 const struct color *const color_entries = tables[index].entries;
1010
1011 for (i = 0; i < count; i++)
1012 if (streq (color_name->name, color_entries[i].name))
1013 {
1014 colors[index] = (struct color *)&color_entries[i];
1015 found = true;
1016 break;
1017 }
1018 if (!found)
1019 vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color_name->orig, "not recognized");
1020 }
1021
1022 static void
1023 print_line (bool bold, const struct color **colors, const char *const line, unsigned int flags)
1024 {
1025 /* --clean[-all] */
1026 if (clean || clean_all)
1027 print_clean (line);
1028 else
1029 {
1030 /* Foreground color code is guaranteed to be set when background color code is present. */
1031 if (colors[BACKGROUND] && colors[BACKGROUND]->code)
1032 printf ("\033[%s", colors[BACKGROUND]->code);
1033 if (colors[FOREGROUND]->code)
1034 printf ("\033[%s%s%s\033[0m", bold ? "1;" : "", colors[FOREGROUND]->code, line);
1035 else
1036 printf (formats[FMT_GENERIC], line);
1037 }
1038 if (flags & CR)
1039 putchar ('\r');
1040 if (flags & LF)
1041 putchar ('\n');
1042 }
1043
1044 static void
1045 print_clean (const char *line)
1046 {
1047 const char *p = line;
1048
1049 if (is_esc (p))
1050 p = get_end_of_esc (p);
1051
1052 while (*p != '\0')
1053 {
1054 const char *text_start = p;
1055 const char *text_end = get_end_of_text (p);
1056 print_text (text_start, text_end - text_start);
1057 p = get_end_of_esc (text_end);
1058 }
1059 }
1060
1061 static bool
1062 is_esc (const char *p)
1063 {
1064 return gather_esc_offsets (p, NULL, NULL);
1065 }
1066
1067 static const char *
1068 get_end_of_esc (const char *p)
1069 {
1070 const char *esc;
1071 const char *end = NULL;
1072 while ((esc = strchr (p, '\033')))
1073 {
1074 if (gather_esc_offsets (esc, NULL, &end))
1075 break;
1076 p = esc + 1;
1077 }
1078 return end ? end + 1 : p + strlen (p);
1079 }
1080
1081 static const char *
1082 get_end_of_text (const char *p)
1083 {
1084 const char *esc;
1085 const char *start = NULL;
1086 while ((esc = strchr (p, '\033')))
1087 {
1088 if (gather_esc_offsets (esc, &start, NULL))
1089 break;
1090 p = esc + 1;
1091 }
1092 return start ? start : p + strlen (p);
1093 }
1094
1095 static void
1096 print_text (const char *p, size_t len)
1097 {
1098 size_t bytes_written;
1099 bytes_written = fwrite (p, 1, len, stdout);
1100 if (bytes_written != len)
1101 vfprintf_fail (formats[FMT_ERROR], (unsigned long)len, "written");
1102 }
1103
1104 static bool
1105 gather_esc_offsets (const char *p, const char **start, const char **end)
1106 {
1107 /* ESC[ */
1108 if (*p == 27 && *(p + 1) == '[')
1109 {
1110 bool valid = false;
1111 const char *const begin = p;
1112 p += 2;
1113 if (clean_all)
1114 valid = validate_esc_clean_all (&p);
1115 else if (clean)
1116 {
1117 bool check_values;
1118 unsigned int iter = 0;
1119 const char *digit;
1120 do {
1121 check_values = false;
1122 iter++;
1123 if (!isdigit (*p))
1124 break;
1125 digit = p;
1126 while (isdigit (*p))
1127 p++;
1128 if (p - digit > 2)
1129 break;
1130 else /* check range */
1131 {
1132 char val[3];
1133 int value;
1134 unsigned int i;
1135 const unsigned int digits = p - digit;
1136 for (i = 0; i < digits; i++)
1137 val[i] = *digit++;
1138 val[i] = '\0';
1139 value = atoi (val);
1140 valid = validate_esc_clean (value, iter, &p, &check_values);
1141 }
1142 } while (check_values);
1143 }
1144 if (valid)
1145 {
1146 if (start)
1147 *start = begin;
1148 if (end)
1149 *end = p;
1150 return true;
1151 }
1152 }
1153 return false;
1154 }
1155
1156 static bool
1157 validate_esc_clean_all (const char **p)
1158 {
1159 while (isdigit (**p) || **p == ';')
1160 (*p)++;
1161 return (**p == 'm');
1162 }
1163
1164 static bool
1165 validate_esc_clean (int value, unsigned int iter, const char **p, bool *check_values)
1166 {
1167 if (is_reset (value, iter, p))
1168 return true;
1169 else if (is_bold (value, iter, p))
1170 {
1171 (*p)++;
1172 *check_values = true;
1173 return false; /* partial escape sequence, need another valid value */
1174 }
1175 else if (is_fg_color (value, p))
1176 return true;
1177 else if (is_bg_color (value, iter, p))
1178 return true;
1179 else
1180 return false;
1181 }
1182
1183 static bool
1184 is_reset (int value, unsigned int iter, const char **p)
1185 {
1186 return (value == 0 && iter == 1 && **p == 'm');
1187 }
1188
1189 static bool
1190 is_bold (int value, unsigned int iter, const char **p)
1191 {
1192 return (value == 1 && iter == 1 && **p == ';');
1193 }
1194
1195 static bool
1196 is_fg_color (int value, const char **p)
1197 {
1198 return (((value >= 30 && value <= 37) || value == 39) && **p == 'm');
1199 }
1200
1201 static bool
1202 is_bg_color (int value, unsigned int iter, const char **p)
1203 {
1204 return (((value >= 40 && value <= 47) || value == 49) && iter == 1 && **p == 'm');
1205 }
1206
1207 #if !DEBUG
1208 static void *
1209 malloc_wrap (size_t size)
1210 {
1211 void *p = malloc (size);
1212 if (!p)
1213 MEM_ALLOC_FAIL ();
1214 return p;
1215 }
1216
1217 static void *
1218 calloc_wrap (size_t nmemb, size_t size)
1219 {
1220 void *p = calloc (nmemb, size);
1221 if (!p)
1222 MEM_ALLOC_FAIL ();
1223 return p;
1224 }
1225
1226 static void *
1227 realloc_wrap (void *ptr, size_t size)
1228 {
1229 void *p = realloc (ptr, size);
1230 if (!p)
1231 MEM_ALLOC_FAIL ();
1232 return p;
1233 }
1234 #else
1235 static void *
1236 malloc_wrap_debug (size_t size, const char *file, unsigned int line)
1237 {
1238 void *p = malloc (size);
1239 if (!p)
1240 MEM_ALLOC_FAIL_DEBUG (file, line);
1241 fprintf (log, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
1242 return p;
1243 }
1244
1245 static void *
1246 calloc_wrap_debug (size_t nmemb, size_t size, const char *file, unsigned int line)
1247 {
1248 void *p = calloc (nmemb, size);
1249 if (!p)
1250 MEM_ALLOC_FAIL_DEBUG (file, line);
1251 fprintf (log, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)(nmemb * size), file, line);
1252 return p;
1253 }
1254
1255 static void *
1256 realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
1257 {
1258 void *p = realloc (ptr, size);
1259 if (!p)
1260 MEM_ALLOC_FAIL_DEBUG (file, line);
1261 fprintf (log, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
1262 return p;
1263 }
1264 #endif /* !DEBUG */
1265
1266 static void
1267 free_wrap (void **ptr)
1268 {
1269 free (*ptr);
1270 *ptr = NULL;
1271 }
1272
1273 #if !DEBUG
1274 # define do_malloc(len, file, line) malloc_wrap(len)
1275 #else
1276 # define do_malloc(len, file, line) malloc_wrap_debug(len, file, line)
1277 #endif
1278
1279 static char *
1280 strdup_wrap (const char *str, const char *file, unsigned int line)
1281 {
1282 const size_t len = strlen (str) + 1;
1283 char *p = do_malloc (len, file, line);
1284 strncpy (p, str, len);
1285 return p;
1286 }
1287
1288 static char *
1289 str_concat_wrap (const char *str1, const char *str2, const char *file, unsigned int line)
1290 {
1291 const size_t len = strlen (str1) + strlen (str2) + 1;
1292 char *p, *str;
1293
1294 p = str = do_malloc (len, file, line);
1295 strncpy (p, str1, strlen (str1));
1296 p += strlen (str1);
1297 strncpy (p, str2, strlen (str2));
1298 p += strlen (str2);
1299 *p = '\0';
1300
1301 return str;
1302 }
1303
1304 static bool
1305 get_bytes_size (unsigned long bytes, struct bytes_size *bytes_size)
1306 {
1307 const char *unit, units[] = { '0', 'K', 'M', 'G', '\0' };
1308 unsigned long size = bytes;
1309 if (bytes < 1024)
1310 return false;
1311 unit = units;
1312 while (size >= 1024 && *(unit + 1))
1313 {
1314 size /= 1024;
1315 unit++;
1316 }
1317 bytes_size->size = (unsigned int)size;
1318 bytes_size->unit = *unit;
1319 return true;
1320 }
1321
1322 static char *
1323 get_file_type (mode_t mode)
1324 {
1325 if (S_ISREG (mode))
1326 return "file";
1327 else if (S_ISDIR (mode))
1328 return "directory";
1329 else if (S_ISCHR (mode))
1330 return "character device";
1331 else if (S_ISBLK (mode))
1332 return "block device";
1333 else if (S_ISFIFO (mode))
1334 return "named pipe";
1335 else if (S_ISLNK (mode))
1336 return "symbolic link";
1337 else if (S_ISSOCK (mode))
1338 return "socket";
1339 else
1340 return "file";
1341 }
1342
1343 static bool
1344 has_color_name (const char *str, const char *name)
1345 {
1346 char *p;
1347
1348 assert (strlen (str));
1349 assert (strlen (name));
1350
1351 if (!(*str == *name || *str == toupper (*name)))
1352 return false;
1353 else if (*(name + 1) != '\0'
1354 && !((p = strstr (str + 1, name + 1)) && p == str + 1))
1355 return false;
1356
1357 return true;
1358 }
1359
1360 static FILE *
1361 open_file (const char *file, const char *mode)
1362 {
1363 FILE *stream;
1364
1365 errno = 0;
1366 stream = fopen (file, mode);
1367 if (!stream)
1368 vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
1369
1370 return stream;
1371 }
1372
1373 #define DO_VFPRINTF(fmt) \
1374 va_list ap; \
1375 fprintf (stderr, "%s: ", program_name); \
1376 va_start (ap, fmt); \
1377 vfprintf (stderr, fmt, ap); \
1378 va_end (ap); \
1379 fprintf (stderr, "\n"); \
1380
1381 static void
1382 vfprintf_diag (const char *fmt, ...)
1383 {
1384 DO_VFPRINTF (fmt);
1385 }
1386
1387 static void
1388 vfprintf_fail (const char *fmt, ...)
1389 {
1390 DO_VFPRINTF (fmt);
1391 exit (EXIT_FAILURE);
1392 }
1393
1394 static void
1395 stack_var (void ***list, unsigned int *stacked, unsigned int index, void *ptr)
1396 {
1397 /* nothing to stack */
1398 if (ptr == NULL)
1399 return;
1400 if (!*list)
1401 *list = xmalloc (sizeof (void *));
1402 else
1403 {
1404 unsigned int i;
1405 for (i = 0; i < *stacked; i++)
1406 if (!(*list)[i])
1407 {
1408 (*list)[i] = ptr;
1409 return; /* reused */
1410 }
1411 *list = xrealloc (*list, (*stacked + 1) * sizeof (void *));
1412 }
1413 (*list)[index] = ptr;
1414 (*stacked)++;
1415 }
1416
1417 static void
1418 release_var (void **list, unsigned int stacked, void **ptr)
1419 {
1420 unsigned int i;
1421 /* nothing to release */
1422 if (*ptr == NULL)
1423 return;
1424 for (i = 0; i < stacked; i++)
1425 if (list[i] == *ptr)
1426 {
1427 free (*ptr);
1428 *ptr = NULL;
1429 list[i] = NULL;
1430 return;
1431 }
1432 }