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