1 /* Handling of color output.
2 Copyright (C) 2011 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2011.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
43 /* Prototype for option handler. */
44 static error_t parse_opt (int key, char *arg, struct argp_state *state);
47 #define OPT_COLOR 0x100100
49 /* Definitions of arguments for argp functions. */
50 static const struct argp_option options[] =
52 { "color", OPT_COLOR, "WHEN", OPTION_ARG_OPTIONAL,
53 N_("colorize the output. WHEN defaults to 'always' or can be 'auto' or 'never'"), 0 },
55 { NULL, 0, NULL, 0, NULL, 0 }
58 /* Parser data structure. */
59 const struct argp color_argp =
61 options, parse_opt, NULL, NULL, NULL, NULL, NULL
65 enum color_enum color_mode;
67 /* Colors to use for the various components. */
68 char *color_address = "";
69 char *color_bytes = "";
70 char *color_mnemonic = "";
71 char *color_operand = NULL;
72 char *color_operand1 = "";
73 char *color_operand2 = "";
74 char *color_operand3 = "";
75 char *color_label = "";
76 char *color_undef = "";
77 char *color_undef_tls = "";
78 char *color_undef_weak = "";
79 char *color_symbol = "";
81 char *color_weak = "";
83 const char color_off[] = "\e[0m";
86 /* Handle program arguments. */
88 parse_opt (int key, char *arg,
89 struct argp_state *state __attribute__ ((unused)))
95 color_mode = color_always;
101 enum color_enum mode;
104 { "always", color_always },
105 { "yes", color_always },
106 { "force", color_always },
107 { "never", color_never },
108 { "no", color_never },
109 { "none", color_never },
110 { "auto", color_auto },
111 { "tty", color_auto },
112 { "if-tty", color_auto }
114 const int nvalues = sizeof (values) / sizeof (values[0]);
116 for (i = 0; i < nvalues; ++i)
117 if (strcmp (arg, values[i].str) == 0)
119 color_mode = values[i].mode;
120 if (color_mode == color_auto)
122 = isatty (STDOUT_FILENO) ? color_always : color_never;
127 error (0, 0, dgettext ("elfutils", "\
128 %s: invalid argument '%s' for '--color'\n\
129 valid arguments are:\n\
130 - 'always', 'yes', 'force'\n\
131 - 'never', 'no', 'none'\n\
132 - 'auto', 'tty', 'if-tty'\n"),
133 program_invocation_short_name, arg);
134 argp_help (&color_argp, stderr, ARGP_HELP_SEE,
135 program_invocation_short_name);
140 if (color_mode == color_always)
142 const char *env = getenv ("ELFUTILS_COLORS");
147 const char *start = env;
148 while (*env != '=' && *env != '\0')
150 if (*env == '=' && env != start)
152 size_t name_len = env - start;
153 const char *val = ++env;
154 env = strchrnul (env, ':');
160 const char name[sizeof (char *) - 1];
164 #define E(name, var) { sizeof (#name) - 1, #name, &color_##var }
180 const size_t nknown = (sizeof (known)
181 / sizeof (known[0]));
183 for (size_t i = 0; i < nknown; ++i)
184 if (name_len == known[i].len
185 && memcmp (start, known[i].name, name_len) == 0)
187 if (asprintf (known[i].varp, "\e[%.*sm",
188 (int) (env - val), val) < 0)
189 error (EXIT_FAILURE, errno,
190 gettext ("cannot allocate memory"));
198 while (*env != '\0');
200 if (color_operand != NULL)
202 if (color_operand1[0] == '\0')
203 color_operand1 = color_operand;
204 if (color_operand2[0] == '\0')
205 color_operand2 = color_operand;
206 if (color_operand3[0] == '\0')
207 color_operand3 = color_operand;
213 // XXX Just for testing.
214 color_address = xstrdup ("\e[38;5;166;1m");
215 color_bytes = xstrdup ("\e[38;5;141m");
216 color_mnemonic = xstrdup ("\e[38;5;202;1m");
217 color_operand1 = xstrdup ("\e[38;5;220m");
218 color_operand2 = xstrdup ("\e[38;5;48m");
219 color_operand3 = xstrdup ("\e[38;5;112m");
220 color_label = xstrdup ("\e[38;5;21m");
227 return ARGP_ERR_UNKNOWN;