]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/color-names.c
readprofile: check input file is not empty [asan]
[thirdparty/util-linux.git] / lib / color-names.c
CommitLineData
0bef6f75
KZ
1
2#include "c.h"
3#include "color-names.h"
4
5struct ul_color_name {
6 const char *name;
7 const char *seq;
8};
9
10/*
11 * qsort/bsearch buddy
12 */
13static int cmp_color_name(const void *a0, const void *b0)
14{
c8b237a0
KZ
15 const struct ul_color_name
16 *a = (const struct ul_color_name *) a0,
17 *b = (const struct ul_color_name *) b0;
0bef6f75
KZ
18 return strcmp(a->name, b->name);
19}
20
21/*
22 * Maintains human readable color names
23 */
24const char *color_sequence_from_colorname(const char *str)
25{
26 static const struct ul_color_name basic_schemes[] = {
27 { "black", UL_COLOR_BLACK },
4683cf36 28 { "blink", UL_COLOR_BLINK },
0bef6f75 29 { "blue", UL_COLOR_BLUE },
4683cf36 30 { "bold", UL_COLOR_BOLD },
0bef6f75
KZ
31 { "brown", UL_COLOR_BROWN },
32 { "cyan", UL_COLOR_CYAN },
33 { "darkgray", UL_COLOR_DARK_GRAY },
34 { "gray", UL_COLOR_GRAY },
35 { "green", UL_COLOR_GREEN },
4683cf36 36 { "halfbright", UL_COLOR_HALFBRIGHT },
0bef6f75
KZ
37 { "lightblue", UL_COLOR_BOLD_BLUE },
38 { "lightcyan", UL_COLOR_BOLD_CYAN },
39 { "lightgray,", UL_COLOR_GRAY },
40 { "lightgreen", UL_COLOR_BOLD_GREEN },
41 { "lightmagenta", UL_COLOR_BOLD_MAGENTA },
42 { "lightred", UL_COLOR_BOLD_RED },
43 { "magenta", UL_COLOR_MAGENTA },
44 { "red", UL_COLOR_RED },
4683cf36
KZ
45 { "reset", UL_COLOR_RESET, },
46 { "reverse", UL_COLOR_REVERSE },
0bef6f75
KZ
47 { "yellow", UL_COLOR_BOLD_YELLOW },
48 };
c8b237a0 49 struct ul_color_name key = { .name = str }, *res;
0bef6f75
KZ
50
51 if (!str)
52 return NULL;
53
54 res = bsearch(&key, basic_schemes, ARRAY_SIZE(basic_schemes),
55 sizeof(struct ul_color_name),
56 cmp_color_name);
57 return res ? res->seq : NULL;
58}