1 #include <linux/kernel.h>
7 static int parse_color(
const char *
name,
int len)
9 static const char *
const color_names[] = {
10 "normal",
"black",
"red",
"green",
"yellow",
11 "blue",
"magenta",
"cyan",
"white"
17 const char *
str = color_names[
i];
21 i = strtol(name, &end, 10);
22 if (end - name == len && i >= -1 && i <= 255)
27 static int parse_attr(
const char *
name,
int len)
29 static const int attr_values[] = { 1, 2, 4, 5, 7 };
30 static const char *
const attr_names[] = {
31 "bold",
"dim",
"ul",
"blink",
"reverse"
36 const char *
str = attr_names[
i];
38 return attr_values[i];
67 while (len > 0 && !
isspace(word[wordlen])) {
73 while (len > 0 &&
isspace(*ptr)) {
78 val = parse_color(word, wordlen);
90 val = parse_attr(word, wordlen);
91 if (val < 0 || attr != -1)
96 if (attr >= 0 || fg >= 0 || bg >= 0) {
112 dst +=
sprintf(dst,
"38;5;%d", fg);
122 dst +=
sprintf(dst,
"48;5;%d", bg);
130 die(
"bad color value '%.*s' for variable '%s'", value_len, value, var);
150 if (stdout_is_tty < 0)
151 stdout_is_tty = isatty(1);
153 char *term = getenv(
"TERM");
154 if (term &&
strcmp(term,
"dumb"))
162 if (!
strcmp(var,
"color.ui")) {
170 static int __color_vsnprintf(
char *
bf,
size_t size,
const char *
color,
171 const char *
fmt,
va_list args,
const char *trail)
191 r +=
scnprintf(bf + r, size - r,
"%s", trail);
195 static int __color_vfprintf(FILE *
fp,
const char *color,
const char *fmt,
196 va_list args,
const char *trail)
223 return __color_vsnprintf(bf, size, color, fmt, args,
NULL);
228 return __color_vfprintf(fp, color, fmt, args,
NULL);
232 const char *fmt, ...)
259 r = __color_vfprintf(fp, color, fmt, args,
"\n");
273 return fwrite(buf, count, 1, fp) != 1;
276 char *
p =
memchr(buf,
'\n', count);
278 if (p != buf && (fputs(color, fp) < 0 ||
279 fwrite(buf, p ? (
size_t)(p - buf) : count, 1, fp) != 1 ||
284 if (fputc(
'\n', fp) < 0)
286 count -= p + 1 -
buf;