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