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 377 378 379 380 381 382 383 384 385 386 387
|
/* set.c 8/19/1995
* Database of runtime-cunfigurable options
*/
/* Copyright (c) 1995,1996 Sascha Demetrio
* Copyright (c) 2009, 2015 Peter Pentchev
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* If you modify any part of HEXER and redistribute it, you must add
* a notice to the `README' file and the modified source files containing
* information about the changes you made. I do not want to take
* credit or be blamed for your modifications.
* 2. 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.
* If you modify any part of HEXER and redistribute it in binary form,
* you must supply a `README' file containing information about the
* changes you made.
* 3. The name of the developer may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* HEXER WAS DEVELOPED BY SASCHA DEMETRIO.
* THIS SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
* THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
* NOT MAKE USE OF THIS WORK.
*
* DISCLAIMER:
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``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 DEVELOPER 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.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <ctype.h>
#include "buffer.h"
#include "hexer.h"
#include "set.h"
#include "util.h"
struct option_s {
char option[256];
union {
long ival; /* integer value */
char sval[1024]; /* string value */
int bval; /* boolean value */
} u;
enum s_option_e type;
struct option_s *next;
set_fn action;
};
static const char *true_strings[] = { "true", "t", "yes", "y", "on", 0 };
static const char *false_string = "false";
static struct option_s *option_first = 0;
long
s_get_option_integer(const char *option)
{
struct option_s *i;
for (i = option_first; i; i = i->next)
if (!strcmp(option, i->option)) {
assert(i->type == S_INTEGER);
return i->u.ival;
}
return -1;
}
/* s_get_option_integer */
const char *
s_get_option_string(const char * const option)
{
struct option_s *i;
for (i = option_first; i; i = i->next)
if (!strcmp(option, i->option)) {
assert(i->type == S_STRING);
return i->u.sval;
}
return "";
}
/* s_get_option_string */
int
s_get_option_bool(const char *option)
{
struct option_s *i;
for (i = option_first; i; i = i->next)
if (!strcmp(option, i->option)) {
assert(i->type == S_BOOL);
return i->u.bval;
}
return 0;
}
/* s_get_option_bool */
int
s_delete_option(const char *option)
{
struct option_s *i, *j;
for (i = option_first, j = 0; i; j = i, i = i->next)
if (!strcmp(option, i->option)) {
if (j) j->next = i->next; else option_first = i->next;
free((char *)i);
return 0;
}
return -1;
}
/* s_delete_option */
static void
s_append(struct option_s * const last,
const char * const option,
const enum s_option_e type, ...)
{
va_list ap;
struct option_s *i;
va_start(ap, type);
i = (struct option_s *)malloc_fatal(sizeof(struct option_s));
i->type = type;
i->next = 0;
strcpy(i->option, option);
if (last) last->next = i; else option_first = i;
i->action = 0;
switch (type) {
case S_STRING:
strcpy(i->u.sval, va_arg(ap, char *));
break;
case S_INTEGER:
i->u.ival = va_arg(ap, long);
break;
case S_BOOL:
i->u.bval = !!va_arg(ap, int);
break;
default:
abort();
}
va_end(ap);
}
/* s_append */
void
s_set_option_string(const char * const option, const char * const value)
{
struct option_s *i, *j;
for (i = option_first, j = 0; i; j = i, i = i->next)
if (!strcmp(option, i->option)) {
strcpy(i->u.sval, value);
i->action = 0;
i->type = S_STRING;
return;
}
s_append(j, option, S_STRING, value);
}
/* s_set_option_string */
void
s_set_option_integer(const char * const option, const long value)
{
struct option_s *i, *j;
for (i = option_first, j = 0; i; j = i, i = i->next)
if (!strcmp(option, i->option)) {
i->u.ival = value;
i->type = S_INTEGER;
i->action = 0;
return;
}
s_append(j, option, S_INTEGER, value);
}
/* s_set_option_integer */
void
s_set_option_bool(const char * const option, const int value)
{
struct option_s *i, *j;
for (i = option_first, j = 0; i; j = i, i = i->next)
if (!strcmp(option, i->option)) {
i->u.bval = value;
i->type = S_BOOL;
i->action = 0;
return;
}
s_append(j, option, S_BOOL, value);
}
/* s_set_option_bool */
enum s_option_e
s_get_type(const char *option)
{
struct option_s *i;
for (i = option_first; i; i = i->next)
if (!strcmp(option, i->option)) return i->type;
return (enum s_option_e)0;
}
/* s_get_type */
const char *
s_get_option(const char *option)
{
struct option_s *i;
static char rval[1024];
for (i = option_first; i; i = i->next)
if (!strcmp(option, i->option)) break;
if (!i) return "";
switch (i->type) {
case S_STRING:
strcpy(rval, i->u.sval);
break;
case S_INTEGER:
sprintf(rval, "%li", i->u.ival);
break;
case S_BOOL:
if (i->u.bval)
strcpy(rval, true_strings[0]);
else
strcpy(rval, false_string);
break;
default:
abort();
}
return rval;
}
/* s_get_option */
void
s_set_type(const char * const option, const enum s_option_e type)
{
switch (type) {
case S_STRING:
s_set_option_string(option, "");
break;
case S_INTEGER:
s_set_option_integer(option, 0);
break;
case S_BOOL:
s_set_option_bool(option, 0);
break;
default:
abort();
}
}
/* s_set_type */
void
s_set_option(const char * const option, const char * const value_string, const int no_action)
{
struct option_s *i;
int k;
for (i = option_first; i; i = i->next)
if (!strcmp(option, i->option)) break;
assert(i);
switch (i->type) {
case S_STRING:
strcpy(i->u.sval, value_string);
if (i->action && !no_action) i->action(value_string);
break;
case S_INTEGER:
i->u.ival = atol(value_string);
if (i->action && !no_action) i->action(i->u.ival);
break;
case S_BOOL:
i->u.bval = 0;
for (k = 0; true_strings[k]; ++k)
if (!strcasecmp(true_strings[k], value_string)) {
i->u.bval = 1;
break;
}
if (i->action && !no_action) i->action(i->u.bval);
break;
default:
abort();
}
}
/* s_set_option */
void
s_set_action(const char * const option, const set_fn action)
{
struct option_s *i;
for (i = option_first; i; i = i->next)
if (!strcmp(i->option, option)) {
i->action = action;
return;
}
abort();
}
/* s_set_action */
char **
s_option_list(const char *prefix, int bool_only)
{
struct option_s *i;
char **list;
int n;
for (i = option_first, n = 0; i; i = i->next)
if (!strncmp(i->option, prefix, strlen(prefix))) ++n;
list = (char **)malloc_fatal((n + 1) * sizeof(char *));
for (i = option_first, n = 0; i; i = i->next)
if (!strncmp(i->option, prefix, strlen(prefix)))
if (!bool_only || i->type == S_BOOL) {
list[n] = (char *)malloc_fatal(strlen(i->option) + 1);
strcpy(list[n], i->option);
++n;
}
list[n] = 0;
return list;
}
/* s_option_list */
char **
s_option_value_list(void)
{
struct option_s *i;
char **list;
size_t n, option_maxlen = 0;
for (i = option_first, n = 0; i; i = i->next, ++n)
if (strlen(i->option) > option_maxlen) option_maxlen = strlen(i->option);
list = (char **)malloc_fatal((n + 1) * sizeof(char *));
for (i = option_first, n = 0; i; i = i->next, ++n) {
list[n] = (char *)malloc_fatal(option_maxlen + 4
+ strlen(s_get_option(i->option)));
memset(list[n], ' ', option_maxlen + 2);
strcpy(list[n], i->option);
list[n][strlen(list[n])] = ' ';
strcpy(list[n] + option_maxlen + 2, s_get_option(i->option));
strcpy(list[n] + strlen(list[n]), "\n");
}
list[n] = 0;
return list;
}
/* s_option_value_list */
void
s_clear_all(void)
{
struct option_s *i, *j;
if (option_first) {
for (j = option_first, i = j->next; i; j = i, i = i->next) free((char *)j);
free((char *)j);
option_first = 0;
}
}
/* s_clear_all */
/* end of set.c */
/* VIM configuration: (do not delete this line)
*
* vim:bk:nodg:efm=%f\:%l\:%m:hid:icon:
* vim:sw=2:sm:textwidth=79:ul=1024:wrap:
*/
|