-
Notifications
You must be signed in to change notification settings - Fork 42
/
theme.c
291 lines (262 loc) · 7.75 KB
/
theme.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/******************************************************************************
*
* theme.c - slurm theme engine
*
******************************************************************************
* $Id: theme.c,v 1.5 2004/09/28 14:23:02 hscholz Exp $
*****************************************************************************/
#ifndef _THEME_C
#define _THEME_C
/******************************************************************************
*
* theme_setdefault()
*
* set default theme
*
*****************************************************************************/
void theme_setdefault(theme * t)
{
if (t == NULL)
error(ERR_FATAL, "theme_setdefault() received NULL pointer");
t->background = COLOR_TRANSPARENT;
t->rx = COLOR_GREEN;
t->tx = COLOR_RED;
t->text = COLOR_WHITE;
t->var = COLOR_WHITE;
t->title = COLOR_WHITE;
t->rx_attrib = COL_BOLD;
t->tx_attrib = COL_BOLD;
t->text_attrib = COL_NORMAL;
t->var_attrib = COL_BOLD;
t->title_attrib = COL_BOLD;
t->complete = E_ALL;
}
/******************************************************************************
*
* theme_readfile()
*
* read color definitions from the given file and store in theme struct
*
*****************************************************************************/
int theme_readfile(theme * t, char *name)
{
FILE *f;
char buf[BUFSIZ + 1];
char filename[BUFSIZ + 1];
char *search_paths[3];
char *p, *val, *key;
int iter;
char pref[sizeof(INSTALL_PREFIX)+15];
strcpy(pref, INSTALL_PREFIX);
t->complete = E_NULL;
/* search for the theme:
* - in the local directory
* - in ~/.slurm
* - in /usr/local/share/slurm
* (make this /usr/share/slurm for Debian systems by specifing
* -D__Debian__)
*/
search_paths[0] = "."; /* local directory */
search_paths[1] = strcat(getenv("HOME"), "/.slurm"); /* $HOME/.slurm/ */
search_paths[2] = strcat(pref, "/share/slurm"); /* search in $PREFIX/share/slurm/ */
for (iter = 0; iter <= 2; iter++) {
bzero(&filename, BUFSIZ);
snprintf(filename, BUFSIZ, "%s/%s.theme", search_paths[iter],
name);
if ((f = fopen(filename, "r")) != NULL) {
/* found the theme; leave the loop */
break;
}
}
/* lets see if we found the theme */
if ((f = fopen(filename, "r")) == NULL) {
error(ERR_FATAL, "cannot find theme '%s'", name);
}
while (fgets(buf, BUFSIZ, f) != NULL) {
if (!strncmp(buf, "#", 1))
continue; /* comment */
p = buf;
while (isspace((int) *p) && (*p + 1 != '\0'))
p++;
key = p;
while (isalpha((int) *p) && (*p + 1 != '\0'))
p++;
if (isspace((int) *p)) {
*p++ = '\0';
while (isspace((int) *p) && (*p + 1 != '\0'))
p++;
if (*p != '=')
continue;
p++;
while (isspace((int) *p) && (*p + 1 != '\0'))
p++;
} else if (*p == '=') {
*p++ = '\0';
while (isspace((int) *p) && (*p + 1 != '\0'))
p++;
} else
continue;
val = p;
while (isalpha((int) *p) && (*p + 1 != '\0'))
p++;
*p++ = '\0';
switch (theme_mapkey(key)) {
case E_BACKGROUND:
t->background = theme_mapcolor(val);
t->complete |= E_BACKGROUND;
break;
case E_RX:
t->rx = theme_mapcolor(val);
t->complete |= E_RX;
break;
case E_TX:
t->tx = theme_mapcolor(val);
t->complete |= E_TX;
break;
case E_TEXT:
t->text = theme_mapcolor(val);
t->complete |= E_TEXT;
break;
case E_TITLE:
t->title = theme_mapcolor(val);
t->complete |= E_TITLE;
break;
case E_TEXTVAR:
t->var = theme_mapcolor(val);
t->complete |= E_TEXTVAR;
break;
case E_RXATTR:
t->rx_attrib = theme_mapattrib(val);
t->complete |= E_RXATTR;
break;
case E_TXATTR:
t->tx_attrib = theme_mapattrib(val);
t->complete |= E_TXATTR;
break;
case E_TEXTATTR:
t->text_attrib = theme_mapattrib(val);
t->complete |= E_TEXTATTR;
break;
case E_TEXTVARATTR:
t->var_attrib = theme_mapattrib(val);
t->complete |= E_TEXTVARATTR;
break;
case E_TITLEATTR:
t->title_attrib = theme_mapattrib(val);
t->complete |= E_TITLEATTR;
break;
default:
error(ERR_WARNING, "unknown key '%s' in theme '%s'",
key, filename);
}
}
fclose(f);
return (t->complete == E_ALL) ? 1 : 0;
}
/******************************************************************************
*
* theme_mapattrib()
*
* map entity attribute to coresponding curses attribute
*
*****************************************************************************/
int theme_mapattrib(char *str)
{
int val = COL_BOLD;
char buf[32];
int i;
if (strlen(str) > 31)
return val;
bzero(&buf, 31);
for (i = 0; i < strlen(str); i++)
buf[i] = toupper((int) str[i]);
buf[i + 1] = '\0';
if (!strcmp(buf, "BOLD"))
val = COL_BOLD;
else if (!strcmp(buf, "NORMAL"))
val = COL_NORMAL;
else if (!strcmp(buf, "DIM"))
val = COL_DIM;
return val;
}
/******************************************************************************
*
* theme_mapcolor()
*
* map color name to coresponding curses color name
*
*****************************************************************************/
int theme_mapcolor(char *str)
{
int val = COLOR_GREEN;
char buf[32];
int i;
if (strlen(str) > 31)
return val;
bzero(&buf, 31);
for (i = 0; i < strlen(str); i++)
buf[i] = toupper((int) str[i]);
buf[i + 1] = '\0';
if (!strcmp(buf, "BLACK"))
val = COLOR_BLACK;
else if (!strcmp(buf, "RED"))
val = COLOR_RED;
else if (!strcmp(buf, "GREEN"))
val = COLOR_GREEN;
else if (!strcmp(buf, "YELLOW"))
val = COLOR_YELLOW;
else if (!strcmp(buf, "BLUE"))
val = COLOR_BLUE;
else if (!strcmp(buf, "MAGENTA"))
val = COLOR_MAGENTA;
else if (!strcmp(buf, "CYAN"))
val = COLOR_CYAN;
else if (!strcmp(buf, "WHITE"))
val = COLOR_WHITE;
else if (!strcmp(buf, "TRANSPARENT"))
val = COLOR_TRANSPARENT;
return val;
}
/******************************************************************************
*
* theme_mapkey()
*
* map entity name to internal name
*
*****************************************************************************/
int theme_mapkey(char *str)
{
int e = 0;
char buf[32];
int i;
if (strlen(str) > 31)
return e;
bzero(&buf, 31);
for (i = 0; i < strlen(str); i++)
buf[i] = toupper((int) str[i]);
buf[i + 1] = '\0';
if (!strcmp(buf, "BACKGROUND"))
e = E_BACKGROUND;
else if (!strcmp(buf, "RX"))
e = E_RX;
else if (!strcmp(buf, "TX"))
e = E_TX;
else if (!strcmp(buf, "TEXT"))
e = E_TEXT;
else if (!strcmp(buf, "TITLE"))
e = E_TITLE;
else if (!strcmp(buf, "TEXTVAR"))
e = E_TEXTVAR;
else if (!strcmp(buf, "RXATTR"))
e = E_RXATTR;
else if (!strcmp(buf, "TXATTR"))
e = E_TXATTR;
else if (!strcmp(buf, "TEXTATTR"))
e = E_TEXTATTR;
else if (!strcmp(buf, "TEXTVARATTR"))
e = E_TEXTVARATTR;
else if (!strcmp(buf, "TITLEATTR"))
e = E_TITLEATTR;
return e;
}
#endif