2
* Database of runtime-cunfigurable options
5
/* Copyright (c) 1995,1996 Sascha Demetrio
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* If you modify any part of HEXER and resitribute it, you must add
14
* a notice to the `README' file and the modified source files containing
15
* information about the changes you made. I do not want to take
16
* credit or be blamed for your modifications.
17
* 2. Redistributions in binary form must reproduce the above copyright
18
* notice, this list of conditions and the following disclaimer in the
19
* documentation and/or other materials provided with the distribution.
20
* If you modify any part of HEXER and resitribute it in binary form,
21
* you must supply a `README' file containing information about the
23
* 3. The name of the developer may not be used to endorse or promote
24
* products derived from this software without specific prior written
27
* HEXER WAS DEVELOPED BY SASCHA DEMETRIO.
28
* THIS SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
29
* THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
30
* NOT MAKE USE OF THIS WORK.
33
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
34
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36
* ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
37
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66
long ival; /* integer value */
67
char sval[1024]; /* string value */
68
int bval; /* boolean value */
71
struct option_s *next;
75
static const char *true_strings[] = { "true", "t", "yes", "y", "on", 0 };
76
static const char *false_string = "false";
78
static struct option_s *option_first = 0;
81
s_get_option_integer(option)
86
for (i = option_first; i; i = i->next)
87
if (!strcmp(option, i->option)) {
88
assert(i->type == S_INTEGER);
93
/* s_get_option_integer */
96
s_get_option_string(option)
101
for (i = option_first; i; i = i->next)
102
if (!strcmp(option, i->option)) {
103
assert(i->type == S_STRING);
108
/* s_get_option_string */
111
s_get_option_bool(option)
116
for (i = option_first; i; i = i->next)
117
if (!strcmp(option, i->option)) {
118
assert(i->type == S_BOOL);
123
/* s_get_option_bool */
126
s_delete_option(option)
129
struct option_s *i, *j;
131
for (i = option_first, j = 0; i; j = i, i = i->next)
132
if (!strcmp(option, i->option)) {
133
if (j) j->next = i->next; else option_first = i->next;
139
/* s_delete_option */
143
s_append(struct option_s *last,
145
const enum s_option_e type, ...)
148
s_append(last, option, type, va_alist)
149
struct option_s *last;
151
const enum s_option_e type;
163
i = (struct option_s *)malloc(sizeof(struct option_s));
166
strcpy(i->option, option);
167
if (last) last->next = i; else option_first = i;
171
strcpy(i->u.sval, va_arg(ap, char *));
174
i->u.ival = va_arg(ap, long);
177
i->u.bval = !!va_arg(ap, int);
187
s_set_option_string(option, value)
191
struct option_s *i, *j;
193
for (i = option_first, j = 0; i; j = i, i = i->next)
194
if (!strcmp(option, i->option)) {
195
strcpy(i->u.sval, value);
200
s_append(j, option, S_STRING, value);
202
/* s_set_option_string */
205
s_set_option_integer(option, value)
209
struct option_s *i, *j;
211
for (i = option_first, j = 0; i; j = i, i = i->next)
212
if (!strcmp(option, i->option)) {
218
s_append(j, option, S_INTEGER, value);
220
/* s_set_option_integer */
223
s_set_option_bool(option, value)
227
struct option_s *i, *j;
229
for (i = option_first, j = 0; i; j = i, i = i->next)
230
if (!strcmp(option, i->option)) {
236
s_append(j, option, S_BOOL, value);
238
/* s_set_option_bool */
246
for (i = option_first; i; i = i->next)
247
if (!strcmp(option, i->option)) return i->type;
248
return (enum s_option_e)0;
257
static char rval[1024];
259
for (i = option_first; i; i = i->next)
260
if (!strcmp(option, i->option)) break;
264
strcpy(rval, i->u.sval);
267
sprintf(rval, "%li", i->u.ival);
271
strcpy(rval, true_strings[0]);
273
strcpy(rval, false_string);
283
s_set_type(option, type)
285
const enum s_option_e type;
289
s_set_option_string(option, "");
292
s_set_option_integer(option, 0);
295
s_set_option_bool(option, 0);
304
s_set_option(option, value_string, no_action)
306
const char *value_string;
312
for (i = option_first; i; i = i->next)
313
if (!strcmp(option, i->option)) break;
317
strcpy(i->u.sval, value_string);
318
if (i->action && !no_action) i->action(value_string);
321
i->u.ival = atol(value_string);
322
if (i->action && !no_action) i->action(i->u.ival);
326
for (k = 0; true_strings[k]; ++k)
327
if (!strcasecmp(true_strings[k], value_string)) {
331
if (i->action && !no_action) i->action(i->u.bval);
340
s_set_action(option, action)
346
for (i = option_first; i; i = i->next)
347
if (!strcmp(i->option, option)) {
356
s_option_list(prefix, bool_only)
364
for (i = option_first, n = 0; i; i = i->next)
365
if (!strncmp(i->option, prefix, strlen(prefix))) ++n;
366
list = (char **)malloc((n + 1) * sizeof(char *));
367
for (i = option_first, n = 0; i; i = i->next)
368
if (!strncmp(i->option, prefix, strlen(prefix)))
369
if (!bool_only || i->type == S_BOOL) {
370
list[n] = (char *)malloc(strlen(i->option) + 1);
371
strcpy(list[n], i->option);
380
s_option_value_list()
384
int n, option_maxlen = 0;
386
for (i = option_first, n = 0; i; i = i->next, ++n)
387
if (strlen(i->option) > option_maxlen) option_maxlen = strlen(i->option);
388
list = (char **)malloc((n + 1) * sizeof(char *));
389
for (i = option_first, n = 0; i; i = i->next, ++n) {
390
list[n] = (char *)malloc(option_maxlen + 4
391
+ strlen(s_get_option(i->option)));
392
memset(list[n], ' ', option_maxlen + 2);
393
strcpy(list[n], i->option);
394
list[n][strlen(list[n])] = ' ';
395
strcpy(list[n] + option_maxlen + 2, s_get_option(i->option));
396
strcpy(list[n] + strlen(list[n]), "\n");
401
/* s_option_value_list */
406
struct option_s *i, *j;
409
for (j = option_first, i = j->next; i; j = i, i = i->next) free((char *)j);
419
/* VIM configuration: (do not delete this line)
421
* vim:aw:bk:bdir=./bak:ch=2:nodg:ef=make.log:efm=%f\:%l\:%m:et:hid:icon:
422
* vim:sw=2:sc:sm:si:textwidth=79:to:ul=1024:wh=12:wrap:wb: