forked from muspellsson/omega-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenclr.c
365 lines (340 loc) · 9.88 KB
/
genclr.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Generate C code to initialize and support Omega colors on UNIX.
*
* Usage:
* cpp -DOMEGA_CLRGEN *.[ch] | genclr <c-file> <h-file>
* where <c-file> should be compiled and linked with the Omega binary and
* <h-file> should be #included in all Omega sources that reference colors.
*
* Curses color micro-tutorial:
* The curses library requires color attributes to be specified as
* COLOR_PAIR(<n>) macro calls, where init_pair(<n>, <fore-color>,
* <back-color>) has previously been called. <n> must fall in the range
* 1..COLOR_PAIRS, where COLOR_PAIRS is a run-time read-only curses variable
* typically set to something like 64.
*
* <c-file> defines function clrgen_init(), which contains the minimal number
* of curses init_pair() calls to support the color usages detected by this
* program on standard input.
*
* <h-file> defines preprocessor variables that cause each detected color
* usage to expand to an appropriate curses COLOR_PAIR() call.
*
* This approach to UNIX color support is perhaps overkill, but does have
* advantages over these alternative approaches:
*
* - hard-coded init_pair() calls and color definitions, which would
* require manual checking and possibly editing for color reference added
* or removed anywhere in the sources;
*
* - replacement of color references with function calls that lazily call
* init_pair() as necessary, which would consume more run-time resources
* and behave unpredictably if the number of pairs exceeded COLOR_PAIRS;
*
* - run-time analysis of color pairs required by e.g. monster list, which
* would be a bit more complex to code, consume more run-time resources,
* and require significant code changes to move all color references into
* static storage.
*/
#ifndef OMEGA_CLRGEN /* this file confuses its own scanner */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Special tag cpp prepends to color symbols
*/
#define PREFIX "OMEGA_CLRGEN"
/*
* Return whether a character could be part of a C identifier
*/
#define ISCID(c) \
(((c) >= 'A' && (c) <= 'Z') || \
((c) >= 'a' && (c) <= 'z') || \
((c) >= '0' && (c) <= '9') || \
(c) == '_')
/*
* Colors specified in cpp output on standard input
*/
typedef struct {
char *ofg, *obg; /* Omega fore/background color */
char *cfg, *cbg; /* curses fore/background color */
unsigned int boldfg, boldbg; /* fore/background bold flag */
unsigned int idx; /* COLOR_PAIR() argument */
} ClrPair;
/*
* Omega versus curses color names
*/
typedef struct {
char *omega;
char *curses;
unsigned int bold;
} ClrEquiv;
#ifdef USE_OPCURSES
static ClrEquiv clr_equiv[17] = {
{ "BLACK", "BLACK", 0 },
{ "BLUE", "BLUE", 0 },
{ "GREEN", "GREEN", 0 },
{ "CYAN", "CYAN", 0 },
{ "RED", "RED", 0 },
{ "PURPLE", "MAGENTA", 0 },
{ "BROWN", "YELLOW", 0 },
{ "WHITE", "WHITE", 0 },
{ "GREY", "WHITE", 1 },
{ "LIGHT_BLUE", "BLUE", 1 },
{ "LIGHT_GREEN", "GREEN", 1 },
{ "LIGHT_CYAN", "CYAN", 1 },
{ "LIGHT_RED", "RED", 1 },
{ "LIGHT_PURPLE", "MAGENTA", 1 },
{ "YELLOW", "YELLOW", 1 },
{ "BRIGHT_WHITE", "WHITE", 1 },
{ NULL, NULL, 0 } };
#else
static ClrEquiv clr_equiv[17] = {
{ "BLACK", "BLACK", 0 },
{ "BLUE", "BLUE", 0 },
{ "GREEN", "GREEN", 0 },
{ "CYAN", "CYAN", 0 },
{ "RED", "RED", 0 },
{ "PURPLE", "MAGENTA", 0 },
{ "BROWN", "YELLOW", 0 },
{ "WHITE", "WHITE", 0 },
{ "GREY", "BLACK", 1 },
{ "LIGHT_BLUE", "BLUE", 1 },
{ "LIGHT_GREEN", "GREEN", 1 },
{ "LIGHT_CYAN", "CYAN", 1 },
{ "LIGHT_RED", "RED", 1 },
{ "LIGHT_PURPLE", "MAGENTA", 1 },
{ "YELLOW", "YELLOW", 1 },
{ "BRIGHT_WHITE", "WHITE", 1 },
{ NULL, NULL, 0 } };
#endif
static char *clr_lookup (char *omega, char **curses, unsigned int *bold)
{
/*
* Point CURSES to the curses color corresponding to Omega color OMEGA,
* set *BOLD to whether the bold attribute should accompany that curses
* color, and return a copy of OMEGA. If OMEGA is unrecognized, return
* null.
*/
ClrEquiv *e = clr_equiv;
for (; e->omega; e++)
if (!strcmp (e->omega, omega)) {
*curses = e->curses;
*bold = e->bold;
return e->omega;
}
return NULL;
}
static char *clr_scan (char *p, char **curses, unsigned int *bold, char **end)
{
/*
* Return a copy of the Omega color nearest the start of writable buffer
* P, point CURSES to the corresponding curses color, and point END just
* past the color's location in P.
*
* If the Omega color is unrecognized, issue an error and exit.
*/
char c, *start, *omega;
for (; (c = *p); p++) {
if (!ISCID (c))
continue;
for (start = p++; (c = *p); p++) {
if (ISCID (c))
continue;
*p = '\0';
if (!(omega = clr_lookup (start, curses, bold))) {
fprintf (stderr, "unrecognized Omega color \"%s\"\n", start);
exit (1);
}
*p = c;
*end = p;
return omega;
}
}
return NULL;
}
static int opaircmp (const void *pair1, const void *pair2)
{
/*
* qsort comparison function: return less than, equal to, or greater than
* 0 according to whether PAIR1 precedes, coincides with, or follows PAIR2
* in a sorted list of Omega color pairs.
*/
ClrPair *p1 = (ClrPair *)pair1, *p2 = (ClrPair *)pair2;
int diff = strcmp (p1->ofg, p2->ofg);
if (diff)
return diff;
return strcmp (p1->obg, p2->obg);
}
static int cpaircmp (const void *pair1, const void *pair2)
{
/*
* qsort comparison function: return less than, equal to, or greater than
* 0 according to whether PAIR1 precedes, coincides with, or follows PAIR2
* in a sorted list of curses color pairs.
*/
ClrPair *p1 = *(ClrPair **)pair1, *p2 = *(ClrPair **)pair2;
int diff = strcmp (p1->cfg, p2->cfg);
if (diff)
return diff;
return strcmp (p1->cbg, p2->cbg);
}
static FILE *emitopen (char *file, char **argv)
{
/*
* Write to the top of FILE a suitable header based on ARGV, and return a
* writable file pointer on FILE. Exit on error.
*/
FILE *fp = fopen (file, "w");
if (!fp) {
fprintf (stderr, "error opening %s", file);
perror ("");
exit (1);
}
fprintf (fp, "\
/*\n\
* Do not edit this file. It was automatically generated by running:\n\
* %s %s %s\n\
*/\n\
\n\
",
argv[0], argv[1], argv[2]);
return fp;
}
static void emitclose (FILE *fp, char *file)
{
/*
* Close FP attached to FILE, exiting on error.
*/
if (fclose (fp) == 0)
return;
fprintf (stderr, "error closing %s", file);
perror ("");
exit (1);
}
int main (int argc, char **argv)
{
char line[1024], *p;
unsigned int i, j, nopairs = 0, ncpairs, opairslen = 80, one;
ClrPair *pair;
ClrPair *opairs; /* Omega color pairs */
ClrPair **cpairs; /* curses color pairs */
char *cfile, *hfile;
FILE *fp;
if (argc != 3) {
fprintf (stderr, "usage: %s <c-file> <h-file>\n", argv[0]);
exit (1);
}
cfile = argv[1];
hfile = argv[2];
/*
* Accumulate Omega color pairs from standard input into pairs.
*/
opairs = (ClrPair *)malloc (opairslen * sizeof (ClrPair));
while (fgets (line, 1024, stdin)) {
for (p = line; (p = strstr (p, PREFIX));) {
p += sizeof (PREFIX) - 1;
if (nopairs == opairslen) {
opairslen *= 2;
opairs = (ClrPair *)realloc (opairs, opairslen *
sizeof (ClrPair));
}
pair = opairs + nopairs++;
one = *p++ == '1';
pair->ofg = clr_scan (p, &pair->cfg, &pair->boldfg, &p);
pair->obg = one ?
clr_lookup ("BLACK", &pair->cbg, &pair->boldbg) :
clr_scan (p, &pair->cbg, &pair->boldbg, &p);
if (pair->boldbg)
fprintf (stderr, "warning: \"%s\": bg bold unimplemented\n",
pair->obg);
}
}
if (!nopairs) {
fputs ("no colors detected in standard input\n", stderr);
exit (1);
}
/*
* Remove duplicate Omega color pairs.
*/
qsort (opairs, nopairs, sizeof (ClrPair), opaircmp);
for (i = 0, j = 1; j < nopairs; j++) {
if (opaircmp (opairs + i, opairs + j))
opairs[++i] = opairs[j];
}
nopairs = i + 1;
/*
* Construct a list of unique curses color pairs, and instantiate all
* ClrPair.idx fields.
*/
cpairs = (ClrPair **)malloc (nopairs * sizeof (ClrPair *));
for (i = 0; i < nopairs; i++)
cpairs[i] = opairs + i;
qsort (cpairs, nopairs, sizeof (ClrPair *), cpaircmp);
cpairs[0]->idx = 1;
for (i = 0, j = 1; j < nopairs; j++) {
if (cpaircmp (cpairs + i, cpairs + j))
cpairs[++i] = cpairs[j];
cpairs[j]->idx = i + 1;
}
ncpairs = i + 1;
/*
* Emit .c file.
*/
fp = emitopen (cfile, argv);
#ifdef USE_OPCURSES
fprintf (fp, "\
#include \"../opcurses/curses.h\"\n");
#else
fprintf (fp, "\
#include <curses.h>\n");
#endif
fprintf (fp, "\
#include <stdio.h>\n\
#include <stdlib.h>\n\
\
#include \"%s\"\n\
\
void clrgen_init (void)\n\
""{\n\
if (!has_colors()) {\n\
fprintf(stderr,\"Omega currently requires ncurses or op-curses color support.\\n\");\n\
return;\n\
} else\n\
if (%d > COLOR_PAIRS - 1) {\n\
endwin();\n\
fprintf (stderr,\"Too few available color pairs (found %%d, need %d)!\\n\", COLOR_PAIRS);\n\
exit (1);\n\
}\n\
",
hfile, ncpairs, ncpairs);
for (i = 0; i < ncpairs; i++)
fprintf (fp, "\
init_pair (%d, COLOR_%s, COLOR_%s);\n\
",
cpairs[i]->idx, cpairs[i]->cfg, cpairs[i]->cbg);
fputs ("\
""}\n\
",
fp);
emitclose (fp, cfile);
/*
* Emit .h file.
*/
fp = emitopen (hfile, argv);
for (i = 0; i < nopairs; i++) {
pair = opairs + i;
fprintf (fp, "#define CLR_%s_%s\t%sCOLOR_PAIR(%d)%s\n",
pair->ofg, pair->obg,
strlen (pair->ofg) + strlen (pair->obg) > 10 ? "" : "\t",
pair->idx, pair->boldfg ? "|A_BOLD" : "");
}
fputs ("\
\n\
extern void clrgen_init (void);\n\
",
fp);
emitclose (fp, hfile);
return 0;
}
#endif /* !OMEGA_CLRGEN */