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