aboutsummaryrefslogtreecommitdiffstats
path: root/color.c
blob: c681082505b8d37f0acd7cfd01162d05184dafb6 (plain) (blame)
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
366
367
368
369
370
371
372
373
374
375
376
/*
 * Copyright (c) 1984 through 2008, William LeFebvre
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 * 
 *     * Neither the name of William LeFebvre nor the names of other
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  Top users/processes display for Unix
 *  Version 3
 */

/*
 * This file handles color definitions and access for augmenting
 * the output with ansi color sequences.
 *
 * The definition of a color setting is as follows, separated by
 * colons:
 *
 * tag=minimum,maximum#code
 *
 * "tag" is the name of the value to display with color.
 *
 * "minimum" and "maximum" are positive integer values defining a range:
 * when the value is within this range it will be shown with the
 * specified color.  A missing value indicates that no check should be
 * made (i.e.: ",25" is n <= 25; "25,50" is 25 <= n <= 50; and "50,"
 * is 50 <= n).
 * 
 * "code" is the ansi sequence that defines the color to use with the
 * escape sequence "[m".  Semi-colons are allowed in this string to
 * combine attributes.
 */

#include "os.h"
#include "display.h"
#include "message.h"
#include "color.h"
#include "utils.h"

typedef struct color_entry {
    char *tag;
    int min;
    int max;
    char color;
    struct color_entry *next;
    struct color_entry *tagnext;
} color_entry;

static color_entry *entries = NULL;

static color_entry **bytag = NULL;
static char **bytag_names = NULL;
static int totaltags = 0;
static int tagcnt = 0;
static int color_off = 0;

static char **color_ansi = NULL;
static int num_color_ansi = 0;
static int max_color_ansi = 0;

static int
color_slot(char *str)

{
    int i;

    for (i = 0; i < num_color_ansi; i++)
    {
	if (strcmp(color_ansi[i], str) == 0)
	{
	    return i;
	}
    }

    /* need a new slot */
    if (num_color_ansi >= max_color_ansi)
    {
	max_color_ansi += COLOR_ANSI_SLOTS;
	color_ansi = (char **)realloc(color_ansi, max_color_ansi * sizeof(char *));
    }
    color_ansi[num_color_ansi] = strdup(str);
    return num_color_ansi++;
}

/*
 * int color_env_parse(char *env)
 *
 * Parse a color specification "env" (such as one found in the environment) and
 * add them to the list of entries.  Always returns 0.  Should only be called
 * once.
 */

int
color_env_parse(char *env)

{
    char *p;
    char *min;
    char *max;
    char *str;
    int len;
    color_entry *ce;

    /* initialization */
    color_ansi = (char **)malloc(COLOR_ANSI_SLOTS * sizeof(char *));
    max_color_ansi = COLOR_ANSI_SLOTS;

    /* color slot 0 is always "0" */
    color_slot("0");

    if (env != NULL)
    {
	p = strtok(env, ":");
	while (p != NULL)
	{
	    if ((min = strchr(p, '=')) != NULL &&
		(max = strchr(min, ',')) != NULL &&
		(str = strchr(max, '#')) != NULL)
	    {
		ce = (color_entry *)malloc(sizeof(color_entry));
		len = min - p;
		ce->tag = (char *)malloc(len + 1);
		strncpy(ce->tag, p, len);
		ce->tag[len] = '\0';
		ce->min = atoi(++min);
		ce->max = atoi(++max);
		ce->color = color_slot(++str);
		ce->next = entries;
		entries = ce;
	    }
	    else
	    {
		if (min != NULL)
		{
		    len = min - p;
		}
		else
		{
		    len = strlen(p);
		}
		message_error(" %.*s: bad color entry", len, p);
	    }
	    p = strtok(NULL, ":");
	}
    }
    return 0;
}

/*
 * int color_tag(char *tag)
 *
 * Declare "tag" as a color tag.  Return a tag index to use when testing
 * a value against the tests for this tag.  Should not be called before
 * color_env_parse.
 */

int
color_tag(char *tag)

{
    color_entry *entryp;
    color_entry *tp;

    /* check for absurd arguments */
    if (tag == NULL || *tag == '\0')
    {
	return -1;
    }

    dprintf("color_tag(%s)\n", tag);

    /* initial allocation */
    if (bytag == NULL)
    {
	totaltags = 10;
	bytag = (color_entry **)malloc(totaltags * sizeof(color_entry *));
	bytag_names = (char **)malloc(totaltags * sizeof(char *));
    }

    /* if we dont have enough room then reallocate */
    if (tagcnt >= totaltags)
    {
	totaltags *= 2;
	bytag = (color_entry **)realloc(bytag, totaltags * sizeof(color_entry *));
	bytag_names = (char **)realloc(bytag_names, totaltags * sizeof(char *));
    }

    /* initialize scan */
    entryp = entries;
    tp = NULL;

    /* look for tag in the list of entries */
    while (entryp != NULL)
    {
	if (strcmp(entryp->tag, tag) == 0)
	{
	    entryp->tagnext = tp;
	    tp = entryp;
	}
	entryp = entryp->next;
    }

    /* we track names in the array bytag */
    bytag[tagcnt] = tp;
    bytag_names[tagcnt] = strdup(tag);

    /* return this index number as a reference */
    dprintf("color_tag: returns %d\n", tagcnt);
    return (tagcnt++);
}

/*
 * int color_test(int tagidx, int value)
 *
 * Test "value" against tests for tag "tagidx", a number previously returned
 * by color_tag.  Return the correct color number to use when highlighting.
 * If there is no match, return 0 (color 0).
 */

int
color_test(int tagidx, int value)

{
    color_entry *ce;

    /* sanity check */
    if (tagidx < 0 || tagidx >= tagcnt || color_off)
    {
	return 0;
    }

    ce = bytag[tagidx];

    while (ce != NULL)
    {
	if ((!ce->min || ce->min <= value) &&
	    (!ce->max || ce->max >= value))
	{
	    return ce->color;
	}
	ce = ce->tagnext;
    }

    return 0;
}

/*
 * char *color_setstr(int color)
 *
 * Return ANSI string to set the terminal for color number "color".
 */

char *
color_setstr(int color)

{
    static char v[32];

    v[0] = '\0';
    if (color >= 0 && color < num_color_ansi)
    {
	snprintf(v, sizeof(v), "\033[%sm", color_ansi[color]);
    }
    return v;
}

void
color_dump(FILE *f)

{
    color_entry *ep;
    int i;
    int col;
    int len;

    fputs("These color tags are available:", f);
    col = 81;
    for (i = 0; i < tagcnt; i++)
    {
	len = strlen(bytag_names[i]) + 1;
	if (len + col > 79)
	{
	    fputs("\n  ", f);
	    col = 2;
	}
	fprintf(f, " %s", bytag_names[i]);
	col += len;
    }

    fputs("\n\nTop color settings:\n", f);

    for (i = 0; i < tagcnt; i++)
    {
	ep = bytag[i];
	while (ep != NULL)
	{
	    fprintf(f, "   %s (%d-", ep->tag, ep->min);
	    if (ep->max != 0)
	    {
		fprintf(f, "%d", ep->max);
	    }
	    fprintf(f, "): ansi color %s, %sSample Text",
		    color_ansi[(int)ep->color],
		    color_setstr(ep->color));
	    fprintf(f, "%s\n", color_setstr(0));
	    ep = ep -> tagnext;
	}
    }
}

void
color_debug(FILE *f)

{
    color_entry *ep;
    int i;

    fprintf(f, "color debug dump\n");
    ep = entries;
    while (ep != NULL)
    {
	fprintf(f, "%s(%d,%d): slot %d, ansi %s, %sSample Text",
		ep->tag, ep->min, ep->max, ep->color, color_ansi[(int)ep->color],
	       color_setstr(ep->color));
	fprintf(f, "%s\n", color_setstr(0));
	ep = ep -> next;
    }

    fprintf(f, "\ntags:");
    for (i = 0; i < tagcnt; i++)
    {
	fprintf(f, " %s", bytag_names[i]);
    }
    fprintf(f, "\n");
}

int
color_activate(int i)

{
    if (i == -1)
    {
	color_off = !color_off;
    }
    else
    {
	color_off = !i;
    }
    return color_off;
}