]>
git.refcnt.org Git - colorize.git/blob - colorize.c
06ef8fba68707d2a477c0b3042756282b42c664c
2 * colorize - Read text from standard input stream or file and print
3 * it colorized through use of ANSI escape sequences
5 * Copyright (c) 2011-2015 Steven Schubiger
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.
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.
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/>.
23 #define _XOPEN_SOURCE 700
24 #define _FILE_OFFSET_BITS 64
34 #include <sys/types.h>
44 #define to_str(arg) str(arg)
46 #define streq(s1, s2) (strcmp (s1, s2) == 0)
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)
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__)
62 #define free_null(ptr) free_wrap((void **)&ptr)
64 #if defined(BUF_SIZE) && (BUF_SIZE <= 0 || BUF_SIZE > 65536)
68 # define BUF_SIZE 4096
74 #define SKIP_LINE_ENDINGS(flags) (((flags) & CR) && ((flags) & LF) ? 2 : 1)
76 #define VALID_FILE_TYPE(mode) (S_ISREG (mode) || S_ISLNK (mode) || S_ISFIFO (mode))
78 #define STACK_VAR(ptr) do { \
79 stack_var (&vars_list, &stacked_vars, stacked_vars, ptr); \
82 #define RELEASE_VAR(ptr) do { \
83 release_var (vars_list, stacked_vars, (void **)&ptr); \
87 # define MEM_ALLOC_FAIL() do { \
88 fprintf (stderr, "%s: memory allocation failure\n", program_name); \
89 exit (EXIT_FAILURE); \
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); \
98 #define ABORT_TRACE() \
99 fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \
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")) \
107 #define ALLOC_COMPLETE_PART_LINE 8
109 #define COLOR_SEP_CHAR '/'
111 #define DEBUG_FILE "debug.txt"
113 #define VERSION "0.56"
115 typedef enum { false, true } bool;
122 static struct color_name
*color_names
[3] = { NULL
, NULL
, NULL
};
129 static const struct color fg_colors
[] = {
136 { "magenta", "35m" },
139 { "default", "39m" },
141 static const struct color bg_colors
[] = {
148 { "magenta", "45m" },
151 { "default", "49m" },
169 static const char *formats
[] = {
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 */
177 "%s: %s: %s", /* type */
180 enum { FOREGROUND
, BACKGROUND
};
182 static const struct {
183 struct color
const *entries
;
187 { fg_colors
, sizeof (fg_colors
) / sizeof (struct color
), "foreground" },
188 { bg_colors
, sizeof (bg_colors
) / sizeof (struct color
), "background" },
196 static unsigned int stacked_vars
;
197 static void **vars_list
;
200 static bool clean_all
;
202 static char *exclude
;
204 static const char *program_name
;
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 read_print_stream (bool, const struct color
**, const char *, FILE *);
215 static void merge_print_line (bool, const struct color
**, const char *, const char *, FILE *);
216 static void complete_part_line (const char *, char **, FILE *);
217 static bool get_next_char (char *, const char **, FILE *, bool *);
218 static void save_char (char, char **, unsigned long *, size_t *);
219 static void find_color_entries (struct color_name
**, const struct color
**);
220 static void find_color_entry (const struct color_name
*, unsigned int, const struct color
**);
221 static void print_line (bool, const struct color
**, const char * const, unsigned int);
222 static void print_clean (const char *);
223 static bool is_esc (const char *);
224 static const char *get_end_of_esc (const char *);
225 static const char *get_end_of_text (const char *);
226 static void print_text (const char *, size_t);
227 static bool gather_esc_offsets (const char *, const char **, const char **);
228 static bool validate_esc_clean_all (const char **);
229 static bool validate_esc_clean (int, unsigned int, const char **, bool *);
230 static bool is_reset (int, unsigned int, const char **);
231 static bool is_bold (int, unsigned int, const char **);
232 static bool is_fg_color (int, const char **);
233 static bool is_bg_color (int, unsigned int, const char **);
235 static void *malloc_wrap (size_t);
236 static void *calloc_wrap (size_t, size_t);
237 static void *realloc_wrap (void *, size_t);
239 static void *malloc_wrap_debug (size_t, const char *, unsigned int);
240 static void *calloc_wrap_debug (size_t, size_t, const char *, unsigned int);
241 static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
243 static void free_wrap (void **);
244 static char *strdup_wrap (const char *, const char *, unsigned int);
245 static char *str_concat_wrap (const char *, const char *, const char *, unsigned int);
246 static bool get_bytes_size (unsigned long, struct bytes_size
*);
247 static char *get_file_type (mode_t
);
248 static bool has_color_name (const char *, const char *);
249 static FILE *open_file (const char *, const char *);
250 static void vfprintf_diag (const char *, ...);
251 static void vfprintf_fail (const char *, ...);
252 static void stack_var (void ***, unsigned int *, unsigned int, void *);
253 static void release_var (void **, unsigned int, void **);
258 main (int argc
, char **argv
)
260 unsigned int arg_cnt
= 0;
264 const struct color
*colors
[2] = {
265 NULL
, /* foreground */
266 NULL
, /* background */
269 const char *file
= NULL
;
271 program_name
= argv
[0];
274 setvbuf (stdout
, NULL
, _IOLBF
, 0);
277 log
= open_file (DEBUG_FILE
, "w");
280 process_opts (argc
, argv
);
282 arg_cnt
= argc
- optind
;
284 if (clean
|| clean_all
)
286 if (clean
&& clean_all
)
287 vfprintf_fail (formats
[FMT_GENERIC
], "--clean and --clean-all switch are mutually exclusive");
290 const char *format
= "%s %s";
291 const char *message
= "switch cannot be used with more than one file";
293 vfprintf_fail (format
, "--clean", message
);
295 vfprintf_fail (format
, "--clean-all", message
);
300 if (arg_cnt
== 0 || arg_cnt
> 2)
302 vfprintf_diag ("%u arguments provided, expected 1-2 arguments or clean option", arg_cnt
);
308 if (clean
|| clean_all
)
309 process_file_arg (argv
[optind
], &file
, &stream
);
311 process_args (arg_cnt
, &argv
[optind
], &bold
, colors
, &file
, &stream
);
312 read_print_stream (bold
, colors
, file
, stream
);
314 RELEASE_VAR (exclude
);
319 #define SET_OPT_TYPE(type) \
328 process_opts (int argc
, char **argv
)
339 struct option long_opts
[] = {
340 { "clean", no_argument
, &opt_type
, OPT_CLEAN
},
341 { "clean-all", no_argument
, &opt_type
, OPT_CLEAN_ALL
},
342 { "exclude-random", required_argument
, &opt_type
, OPT_EXCLUDE_RANDOM
},
343 { "help", no_argument
, &opt_type
, OPT_HELP
},
344 { "version", no_argument
, &opt_type
, OPT_VERSION
},
345 { NULL
, 0, NULL
, 0 },
348 while ((opt
= getopt_long (argc
, argv
, "hV", long_opts
, NULL
)) != -1)
353 case 0: /* long opts */
362 case OPT_EXCLUDE_RANDOM
: {
365 exclude
= xstrdup (optarg
);
367 for (i
= 1; i
< tables
[FOREGROUND
].count
- 1; i
++) /* skip color none and default */
369 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
370 if (streq (exclude
, entry
->name
))
377 vfprintf_fail (formats
[FMT_GENERIC
], "--exclude-random switch must be provided a plain color");
386 default: /* never reached */
391 SET_OPT_TYPE (OPT_HELP
);
393 SET_OPT_TYPE (OPT_VERSION
);
397 default: /* never reached */
406 fprintf (stderr
, "Type `%s --help' for help screen.\n", program_name
);
414 printf ("Usage: %s (foreground) OR (foreground)%c(background) OR --clean[-all] [-|file]\n\n", program_name
, COLOR_SEP_CHAR
);
415 printf ("\tColors (foreground) (background)\n");
416 for (i
= 0; i
< tables
[FOREGROUND
].count
; i
++)
418 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
419 const char *name
= entry
->name
;
420 const char *code
= entry
->code
;
422 printf ("\t\t{\033[%s#\033[0m} [%c%c]%s%*s%s\n",
423 code
, toupper (*name
), *name
, name
+ 1, 10 - (int)strlen (name
), " ", name
);
425 printf ("\t\t{-} %s%*s%s\n", name
, 13 - (int)strlen (name
), " ", name
);
427 printf ("\t\t{*} [Rr]%s%*s%s [--exclude-random=<foreground color>]\n", "andom", 10 - (int)strlen ("random"), " ", "random");
429 printf ("\n\tFirst character of color name in upper case denotes increased intensity,\n");
430 printf ("\twhereas for lower case colors will be of normal intensity.\n");
432 printf ("\n\tOptions\n");
433 printf ("\t\t --clean\n");
434 printf ("\t\t --clean-all\n");
435 printf ("\t\t --exclude-random\n");
436 printf ("\t\t-h, --help\n");
437 printf ("\t\t-V, --version\n\n");
444 # include "version.h"
446 const char *version
= NULL
;
448 const char *version_prefix
, *version_string
;
450 struct bytes_size bytes_size
;
453 c_flags
= to_str (CFLAGS
);
462 version_prefix
= version
? "" : "v";
463 version_string
= version
? version
: VERSION
;
464 printf ("colorize %s%s (compiled at %s, %s)\n", version_prefix
, version_string
, __DATE__
, __TIME__
);
466 printf ("Compiler flags: %s\n", c_flags
);
467 if (get_bytes_size (BUF_SIZE
, &bytes_size
))
469 if (BUF_SIZE
% 1024 == 0)
470 printf ("Buffer size: %u%c\n", bytes_size
.size
, bytes_size
.unit
);
472 printf ("Buffer size: %u%c, %u byte%s\n", bytes_size
.size
, bytes_size
.unit
,
473 BUF_SIZE
% 1024, BUF_SIZE
% 1024 > 1 ? "s" : "");
476 printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE
, BUF_SIZE
> 1 ? "s" : "");
477 printf ("Debugging: %s\n", debug
? "yes" : "no");
483 free_color_names (color_names
);
485 if (stream
&& fileno (stream
) != STDIN_FILENO
)
495 for (i
= 0; i
< stacked_vars
; i
++)
499 free_null (vars_list
);
504 free_color_names (struct color_name
**color_names
)
507 for (i
= 0; color_names
[i
]; i
++)
509 free (color_names
[i
]->name
);
510 free (color_names
[i
]->orig
);
511 free_null (color_names
[i
]);
516 process_args (unsigned int arg_cnt
, char **arg_strings
, bool *bold
, const struct color
**colors
, const char **file
, FILE **stream
)
520 char *color
, *p
, *str
;
523 const char *color_string
= arg_cnt
>= 1 ? arg_strings
[0] : NULL
;
524 const char *file_string
= arg_cnt
== 2 ? arg_strings
[1] : NULL
;
526 assert (color_string
);
528 if (streq (color_string
, "-"))
531 vfprintf_fail (formats
[FMT_GENERIC
], "hyphen cannot be used as color string");
533 vfprintf_fail (formats
[FMT_GENERIC
], "hyphen must be preceeded by color string");
536 ret
= lstat (color_string
, &sb
);
538 /* Ensure that we don't fail if there's a file with one or more
539 color names in its path. */
540 if (ret
== 0) /* success */
544 const char *color
= color_string
;
545 const mode_t mode
= sb
.st_mode
;
547 for (c
= 1; c
<= 2 && *color
; c
++)
549 bool matched
= false;
551 for (i
= 0; i
< tables
[FOREGROUND
].count
; i
++)
553 const struct color
*entry
= &tables
[FOREGROUND
].entries
[i
];
554 if (has_color_name (color
, entry
->name
))
556 color
+= strlen (entry
->name
);
561 if (!matched
&& has_color_name (color
, "random"))
563 color
+= strlen ("random");
566 if (matched
&& *color
== COLOR_SEP_CHAR
&& *(color
+ 1))
572 have_file
= (*color
!= '\0');
576 const char *file_exists
= color_string
;
578 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "cannot be used as color string");
581 if (VALID_FILE_TYPE (mode
))
582 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "must be preceeded by color string");
584 vfprintf_fail (formats
[FMT_QUOTE
], get_file_type (mode
), file_exists
, "is not a valid file type");
589 if ((p
= strchr (color_string
, COLOR_SEP_CHAR
)))
591 if (p
== color_string
)
592 vfprintf_fail (formats
[FMT_STRING
], "foreground color missing in string", color_string
);
593 else if (p
== color_string
+ strlen (color_string
) - 1)
594 vfprintf_fail (formats
[FMT_STRING
], "background color missing in string", color_string
);
595 else if (strchr (++p
, COLOR_SEP_CHAR
))
596 vfprintf_fail (formats
[FMT_STRING
], "one color pair allowed only for string", color_string
);
599 str
= xstrdup (color_string
);
602 for (index
= 0, color
= str
; *color
; index
++, color
= p
)
607 if ((sep
= strchr (color
, COLOR_SEP_CHAR
)))
613 p
= color
+ strlen (color
);
616 for (ch
= color
; *ch
; ch
++)
618 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be made of non-alphabetic characters");
620 for (ch
= color
+ 1; *ch
; ch
++)
622 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be in mixed lower/upper case");
624 if (streq (color
, "None"))
625 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color
, "cannot be bold");
627 if (isupper (*color
))
635 vfprintf_fail (formats
[FMT_COLOR
], tables
[BACKGROUND
].desc
, color
, "cannot be bold");
636 default: /* never reached */
641 color_names
[index
] = xcalloc (1, sizeof (struct color_name
));
643 color_names
[index
]->orig
= xstrdup (color
);
645 for (ch
= color
; *ch
; ch
++)
648 color_names
[index
]->name
= xstrdup (color
);
653 assert (color_names
[FOREGROUND
]);
655 if (color_names
[BACKGROUND
])
658 const unsigned int color_sets
[2][2] = { { FOREGROUND
, BACKGROUND
}, { BACKGROUND
, FOREGROUND
} };
659 for (i
= 0; i
< 2; i
++)
661 const unsigned int color1
= color_sets
[i
][0];
662 const unsigned int color2
= color_sets
[i
][1];
663 if (CHECK_COLORS_RANDOM (color1
, color2
))
664 vfprintf_fail (formats
[FMT_RANDOM
], tables
[color1
].desc
, color_names
[color1
]->orig
, "cannot be combined with", color_names
[color2
]->orig
);
668 find_color_entries (color_names
, colors
);
669 free_color_names (color_names
);
671 if (!colors
[FOREGROUND
]->code
&& colors
[BACKGROUND
] && colors
[BACKGROUND
]->code
)
673 struct color_name color_name
;
674 color_name
.name
= color_name
.orig
= "default";
676 find_color_entry (&color_name
, FOREGROUND
, colors
);
679 process_file_arg (file_string
, file
, stream
);
683 process_file_arg (const char *file_string
, const char **file
, FILE **stream
)
687 if (streq (file_string
, "-"))
691 const char *file
= file_string
;
696 ret
= stat (file
, &sb
);
699 vfprintf_fail (formats
[FMT_FILE
], file
, strerror (errno
));
701 if (!VALID_FILE_TYPE (sb
.st_mode
))
702 vfprintf_fail (formats
[FMT_TYPE
], file
, "unrecognized type", get_file_type (sb
.st_mode
));
704 *stream
= open_file (file
, "r");
719 read_print_stream (bool bold
, const struct color
**colors
, const char *file
, FILE *stream
)
721 char buf
[BUF_SIZE
+ 1];
722 unsigned int flags
= 0;
724 while (!feof (stream
))
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");
734 while ((eol
= strpbrk (line
, "\n\r")))
741 if (*(eol
+ 1) == '\n')
744 else if (*eol
== '\n')
747 vfprintf_fail (formats
[FMT_FILE
], file
, "unrecognized line ending");
748 p
= eol
+ SKIP_LINE_ENDINGS (flags
);
750 print_line (bold
, colors
, line
, flags
);
756 print_line (bold
, colors
, line
, 0);
758 else if (*line
!= '\0')
761 if ((clean
|| clean_all
) && (p
= strrchr (line
, '\033')))
762 merge_print_line (bold
, colors
, line
, p
, stream
);
764 print_line (bold
, colors
, line
, 0);
770 merge_print_line (bool bold
, const struct color
**colors
, const char *line
, const char *p
, FILE *stream
)
773 char *merged_part_line
= NULL
;
774 const char *part_line
;
776 complete_part_line (p
+ 1, &buf
, stream
);
779 part_line
= merged_part_line
= str_concat (line
, buf
);
784 #ifdef TEST_MERGE_PART_LINE
785 printf ("%s", part_line
);
786 free (merged_part_line
);
789 print_line (bold
, colors
, part_line
, 0);
790 free (merged_part_line
);
795 complete_part_line (const char *p
, char **buf
, FILE *stream
)
797 bool got_next_char
= false, read_from_stream
;
802 if (get_next_char (&ch
, &p
, stream
, &read_from_stream
))
806 if (read_from_stream
)
807 save_char (ch
, buf
, &i
, &size
);
811 if (read_from_stream
)
812 ungetc ((int)ch
, stream
);
819 while (get_next_char (&ch
, &p
, stream
, &read_from_stream
))
821 if (isdigit (ch
) || ch
== ';')
823 if (read_from_stream
)
824 save_char (ch
, buf
, &i
, &size
);
826 else /* read next character */
828 got_next_char
= true;
837 if (read_from_stream
)
838 save_char (ch
, buf
, &i
, &size
);
842 if (read_from_stream
)
843 ungetc ((int)ch
, stream
);
852 get_next_char (char *ch
, const char **p
, FILE *stream
, bool *read_from_stream
)
857 if ((c
= fgetc (stream
)) != EOF
)
860 *read_from_stream
= true;
865 *read_from_stream
= false;
873 *read_from_stream
= false;
879 save_char (char ch
, char **buf
, unsigned long *i
, size_t *size
)
883 *size
= ALLOC_COMPLETE_PART_LINE
;
884 *buf
= xmalloc (*size
);
886 /* +1: effective occupied size of buffer */
887 else if ((*i
+ 1) == *size
)
890 *buf
= xrealloc (*buf
, *size
);
893 (*buf
)[*i
+ 1] = '\0';
898 find_color_entries (struct color_name
**color_names
, const struct color
**colors
)
904 gettimeofday (&tv
, NULL
);
905 srand (tv
.tv_usec
* tv
.tv_sec
);
907 for (index
= 0; color_names
[index
]; index
++)
909 const char *color_name
= color_names
[index
]->name
;
911 const unsigned int count
= tables
[index
].count
;
912 const struct color
*const color_entries
= tables
[index
].entries
;
914 if (streq (color_name
, "random"))
920 i
= rand() % (count
- 2) + 1; /* omit color none and default */
924 /* --exclude-random */
925 if (exclude
&& streq (exclude
, color_entries
[i
].name
))
927 else if (color_names
[BACKGROUND
] && streq (color_names
[BACKGROUND
]->name
, color_entries
[i
].name
))
931 if (streq (colors
[FOREGROUND
]->name
, color_entries
[i
].name
))
934 default: /* never reached */
937 } while (excludable
);
938 colors
[index
] = (struct color
*)&color_entries
[i
];
941 find_color_entry (color_names
[index
], index
, colors
);
946 find_color_entry (const struct color_name
*color_name
, unsigned int index
, const struct color
**colors
)
951 const unsigned int count
= tables
[index
].count
;
952 const struct color
*const color_entries
= tables
[index
].entries
;
954 for (i
= 0; i
< count
; i
++)
955 if (streq (color_name
->name
, color_entries
[i
].name
))
957 colors
[index
] = (struct color
*)&color_entries
[i
];
962 vfprintf_fail (formats
[FMT_COLOR
], tables
[index
].desc
, color_name
->orig
, "not recognized");
966 print_line (bool bold
, const struct color
**colors
, const char *const line
, unsigned int flags
)
969 if (clean
|| clean_all
)
973 /* Foreground color code is guaranteed to be set when background color code is present. */
974 if (colors
[BACKGROUND
] && colors
[BACKGROUND
]->code
)
975 printf ("\033[%s", colors
[BACKGROUND
]->code
);
976 if (colors
[FOREGROUND
]->code
)
977 printf ("\033[%s%s%s\033[0m", bold
? "1;" : "", colors
[FOREGROUND
]->code
, line
);
979 printf (formats
[FMT_GENERIC
], line
);
988 print_clean (const char *line
)
990 const char *p
= line
;
993 p
= get_end_of_esc (p
);
997 const char *text_start
= p
;
998 const char *text_end
= get_end_of_text (p
);
999 print_text (text_start
, text_end
- text_start
);
1000 p
= get_end_of_esc (text_end
);
1005 is_esc (const char *p
)
1007 return gather_esc_offsets (p
, NULL
, NULL
);
1011 get_end_of_esc (const char *p
)
1014 const char *end
= NULL
;
1015 while ((esc
= strchr (p
, '\033')))
1017 if (gather_esc_offsets (esc
, NULL
, &end
))
1021 return end
? end
+ 1 : p
+ strlen (p
);
1025 get_end_of_text (const char *p
)
1028 const char *start
= NULL
;
1029 while ((esc
= strchr (p
, '\033')))
1031 if (gather_esc_offsets (esc
, &start
, NULL
))
1035 return start
? start
: p
+ strlen (p
);
1039 print_text (const char *p
, size_t len
)
1041 size_t bytes_written
;
1042 bytes_written
= fwrite (p
, 1, len
, stdout
);
1043 if (bytes_written
!= len
)
1044 vfprintf_fail (formats
[FMT_ERROR
], (unsigned long)len
, "written");
1048 gather_esc_offsets (const char *p
, const char **start
, const char **end
)
1051 if (*p
== 27 && *(p
+ 1) == '[')
1054 const char *begin
= p
;
1057 valid
= validate_esc_clean_all (&p
);
1061 unsigned int iter
= 0;
1064 check_values
= false;
1069 while (isdigit (*p
))
1073 else /* check range */
1078 const unsigned int digits
= p
- digit
;
1079 for (i
= 0; i
< digits
; i
++)
1083 valid
= validate_esc_clean (value
, iter
, &p
, &check_values
);
1085 } while (check_values
);
1100 validate_esc_clean_all (const char **p
)
1102 while (isdigit (**p
) || **p
== ';')
1104 return (**p
== 'm');
1108 validate_esc_clean (int value
, unsigned int iter
, const char **p
, bool *check_values
)
1110 if (is_reset (value
, iter
, p
))
1112 else if (is_bold (value
, iter
, p
))
1115 *check_values
= true;
1116 return false; /* partial escape sequence, need another valid value */
1118 else if (is_fg_color (value
, p
))
1120 else if (is_bg_color (value
, iter
, p
))
1127 is_reset (int value
, unsigned int iter
, const char **p
)
1129 return (value
== 0 && iter
== 1 && **p
== 'm');
1133 is_bold (int value
, unsigned int iter
, const char **p
)
1135 return (value
== 1 && iter
== 1 && **p
== ';');
1139 is_fg_color (int value
, const char **p
)
1141 return (((value
>= 30 && value
<= 37) || value
== 39) && **p
== 'm');
1145 is_bg_color (int value
, unsigned int iter
, const char **p
)
1147 return (((value
>= 40 && value
<= 47) || value
== 49) && iter
== 1 && **p
== 'm');
1152 malloc_wrap (size_t size
)
1154 void *p
= malloc (size
);
1161 calloc_wrap (size_t nmemb
, size_t size
)
1163 void *p
= calloc (nmemb
, size
);
1170 realloc_wrap (void *ptr
, size_t size
)
1172 void *p
= realloc (ptr
, size
);
1179 malloc_wrap_debug (size_t size
, const char *file
, unsigned int line
)
1181 void *p
= malloc (size
);
1183 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1184 fprintf (log
, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name
, (unsigned long)size
, file
, line
);
1189 calloc_wrap_debug (size_t nmemb
, size_t size
, const char *file
, unsigned int line
)
1191 void *p
= calloc (nmemb
, size
);
1193 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1194 fprintf (log
, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name
, (unsigned long)(nmemb
* size
), file
, line
);
1199 realloc_wrap_debug (void *ptr
, size_t size
, const char *file
, unsigned int line
)
1201 void *p
= realloc (ptr
, size
);
1203 MEM_ALLOC_FAIL_DEBUG (file
, line
);
1204 fprintf (log
, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name
, (unsigned long)size
, file
, line
);
1210 free_wrap (void **ptr
)
1217 # define do_malloc(len, file, line) malloc_wrap(len)
1219 # define do_malloc(len, file, line) malloc_wrap_debug(len, file, line)
1223 strdup_wrap (const char *str
, const char *file
, unsigned int line
)
1225 const size_t len
= strlen (str
) + 1;
1226 char *p
= do_malloc (len
, file
, line
);
1227 strncpy (p
, str
, len
);
1232 str_concat_wrap (const char *str1
, const char *str2
, const char *file
, unsigned int line
)
1234 const size_t len
= strlen (str1
) + strlen (str2
) + 1;
1237 p
= str
= do_malloc (len
, file
, line
);
1238 strncpy (p
, str1
, strlen (str1
));
1240 strncpy (p
, str2
, strlen (str2
));
1248 get_bytes_size (unsigned long bytes
, struct bytes_size
*bytes_size
)
1250 const char *unit
, units
[] = { '0', 'K', 'M', 'G', '\0' };
1251 unsigned long size
= bytes
;
1255 while (size
>= 1024 && *(unit
+ 1))
1260 bytes_size
->size
= (unsigned int)size
;
1261 bytes_size
->unit
= *unit
;
1266 get_file_type (mode_t mode
)
1270 else if (S_ISDIR (mode
))
1272 else if (S_ISCHR (mode
))
1273 return "character device";
1274 else if (S_ISBLK (mode
))
1275 return "block device";
1276 else if (S_ISFIFO (mode
))
1277 return "named pipe";
1278 else if (S_ISLNK (mode
))
1279 return "symbolic link";
1280 else if (S_ISSOCK (mode
))
1287 has_color_name (const char *str
, const char *name
)
1291 assert (strlen (str
));
1292 assert (strlen (name
));
1294 if (!(*str
== *name
|| *str
== toupper (*name
)))
1296 else if (*(name
+ 1) != '\0'
1297 && !((p
= strstr (str
+ 1, name
+ 1)) && p
== str
+ 1))
1304 open_file (const char *file
, const char *mode
)
1309 stream
= fopen (file
, mode
);
1311 vfprintf_fail (formats
[FMT_FILE
], file
, strerror (errno
));
1316 #define DO_VFPRINTF(fmt) \
1318 fprintf (stderr, "%s: ", program_name); \
1319 va_start (ap, fmt); \
1320 vfprintf (stderr, fmt, ap); \
1322 fprintf (stderr, "\n"); \
1325 vfprintf_diag (const char *fmt
, ...)
1331 vfprintf_fail (const char *fmt
, ...)
1334 exit (EXIT_FAILURE
);
1338 stack_var (void ***list
, unsigned int *stacked
, unsigned int index
, void *ptr
)
1340 /* nothing to stack */
1344 *list
= xmalloc (sizeof (void *));
1348 for (i
= 0; i
< *stacked
; i
++)
1352 return; /* reused */
1354 *list
= xrealloc (*list
, (*stacked
+ 1) * sizeof (void *));
1356 (*list
)[index
] = ptr
;
1361 release_var (void **list
, unsigned int stacked
, void **ptr
)
1364 /* nothing to release */
1367 for (i
= 0; i
< stacked
; i
++)
1368 if (list
[i
] == *ptr
)