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