1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* No copyright is claimed. This code is in the public domain; do with
* it what you wish.
*
* Copyright (C) 2012-2015 Karel Zak <kzak@redhat.com>
*/
#include "c.h"
#include "color-names.h"
#include <ctype.h>
struct ul_color_name {
const char *name;
const char *seq;
};
/*
* qsort/bsearch buddy
*/
static int cmp_color_name(const void *a0, const void *b0)
{
const struct ul_color_name
*a = (const struct ul_color_name *) a0,
*b = (const struct ul_color_name *) b0;
return strcmp(a->name, b->name);
}
/*
* Maintains human readable color names
*/
const char *color_sequence_from_colorname(const char *str)
{
static const struct ul_color_name basic_schemes[] = {
{ "black", UL_COLOR_BLACK },
{ "blink", UL_COLOR_BLINK },
{ "blue", UL_COLOR_BLUE },
{ "bold", UL_COLOR_BOLD },
{ "brown", UL_COLOR_BROWN },
{ "cyan", UL_COLOR_CYAN },
{ "darkgray", UL_COLOR_DARK_GRAY },
{ "gray", UL_COLOR_GRAY },
{ "green", UL_COLOR_GREEN },
{ "halfbright", UL_COLOR_HALFBRIGHT },
{ "lightblue", UL_COLOR_BOLD_BLUE },
{ "lightcyan", UL_COLOR_BOLD_CYAN },
{ "lightgray,", UL_COLOR_GRAY },
{ "lightgreen", UL_COLOR_BOLD_GREEN },
{ "lightmagenta", UL_COLOR_BOLD_MAGENTA },
{ "lightred", UL_COLOR_BOLD_RED },
{ "magenta", UL_COLOR_MAGENTA },
{ "red", UL_COLOR_RED },
{ "reset", UL_COLOR_RESET, },
{ "reverse", UL_COLOR_REVERSE },
{ "yellow", UL_COLOR_BOLD_YELLOW },
{ "white", UL_COLOR_WHITE }
};
struct ul_color_name key = { .name = str }, *res;
if (!str)
return NULL;
res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes),
sizeof(struct ul_color_name),
cmp_color_name);
return res ? res->seq : NULL;
}
int color_is_sequence(const char *color)
{
if (color && *color == 0x1B) {
size_t len = strlen(color);
if (len >= 4 &&
*(color + 1) == '[' &&
isdigit(*(color + 2)) &&
*(color + len - 1) == 'm')
return 1;
}
return 0;
}
/* canonicalize sequence */
static int __color_canonicalize(const char *str, char **seq)
{
char *in, *out;
int len;
if (!str)
return -EINVAL;
*seq = NULL;
/* convert color names like "red" to the real sequence */
if (*str != '\\' && isalpha(*str)) {
const char *s = color_sequence_from_colorname(str);
*seq = strdup(s ? s : str);
return *seq ? 0 : -ENOMEM;
}
/* convert xx;yy sequences to "\033[xx;yy" */
if ((len = asprintf(seq, "\033[%sm", str)) < 1)
return -ENOMEM;
for (in = *seq, out = *seq; in && *in; in++) {
if (*in != '\\') {
*out++ = *in;
continue;
}
switch(*(in + 1)) {
case 'a':
*out++ = '\a'; /* Bell */
break;
case 'b':
*out++ = '\b'; /* Backspace */
break;
case 'e':
*out++ = '\033'; /* Escape */
break;
case 'f':
*out++ = '\f'; /* Form Feed */
break;
case 'n':
*out++ = '\n'; /* Newline */
break;
case 'r':
*out++ = '\r'; /* Carriage Return */
break;
case 't':
*out++ = '\t'; /* Tab */
break;
case 'v':
*out++ = '\v'; /* Vertical Tab */
break;
case '\\':
*out++ = '\\'; /* Backslash */
break;
case '_':
*out++ = ' '; /* Space */
break;
case '#':
*out++ = '#'; /* Hash mark */
break;
case '?':
*out++ = '?'; /* Question mark */
break;
default:
*out++ = *in;
*out++ = *(in + 1);
break;
}
in++;
}
if (out) {
assert ((out - *seq) <= len);
*out = '\0';
}
return 0;
}
char *color_get_sequence(const char *color)
{
char *seq = NULL;
int rc = __color_canonicalize(color, &seq);
return rc ? NULL : seq;
}
|