~ubuntu-branches/ubuntu/quantal/hexer/quantal

« back to all changes in this revision

Viewing changes to set.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Mathiasson
  • Date: 2003-06-19 09:48:34 UTC
  • Revision ID: james.westby@ubuntu.com-20030619094834-t127jo9kd93shx02
Tags: upstream-0.1.4c
ImportĀ upstreamĀ versionĀ 0.1.4c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* set.c        8/19/1995
 
2
 * Database of runtime-cunfigurable options
 
3
 */
 
4
 
 
5
/* Copyright (c) 1995,1996 Sascha Demetrio
 
6
 * All rights reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions
 
10
 * are met:
 
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
 
22
 *    changes you made.
 
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
 
25
 *    permission.
 
26
 *
 
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.
 
31
 *
 
32
 * DISCLAIMER:
 
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
 
43
 * SUCH DAMAGE.
 
44
 */
 
45
 
 
46
#include "config.h"
 
47
 
 
48
#include <stdio.h>
 
49
#include <stdlib.h>
 
50
#include <string.h>
 
51
#include <assert.h>
 
52
#include <ctype.h>
 
53
 
 
54
#if USE_STDARG
 
55
#include <stdarg.h>
 
56
#else
 
57
#include <varargs.h>
 
58
#endif
 
59
 
 
60
#include "defs.h"
 
61
#include "set.h"
 
62
 
 
63
struct option_s {
 
64
  char option[256];
 
65
  union {
 
66
    long ival; /* integer value */
 
67
    char sval[1024]; /* string value */
 
68
    int bval; /* boolean value */
 
69
  } u;
 
70
  enum s_option_e type;
 
71
  struct option_s *next;
 
72
  set_fn action;
 
73
};
 
74
 
 
75
static const char *true_strings[] = { "true", "t", "yes", "y", "on", 0 };
 
76
static const char *false_string = "false";
 
77
 
 
78
static struct option_s *option_first = 0;
 
79
 
 
80
  long
 
81
s_get_option_integer(option)
 
82
  const char *option;
 
83
{
 
84
  struct option_s *i;
 
85
 
 
86
  for (i = option_first; i; i = i->next)
 
87
    if (!strcmp(option, i->option)) {
 
88
      assert(i->type == S_INTEGER);
 
89
      return i->u.ival;
 
90
    }
 
91
  return -1;
 
92
}
 
93
/* s_get_option_integer */
 
94
 
 
95
  char *
 
96
s_get_option_string(option)
 
97
  const char *option;
 
98
{
 
99
  struct option_s *i;
 
100
 
 
101
  for (i = option_first; i; i = i->next)
 
102
    if (!strcmp(option, i->option)) {
 
103
      assert(i->type == S_STRING);
 
104
      return i->u.sval;
 
105
    }
 
106
  return "";
 
107
}
 
108
/* s_get_option_string */
 
109
 
 
110
  int
 
111
s_get_option_bool(option)
 
112
  const char *option;
 
113
{
 
114
  struct option_s *i;
 
115
 
 
116
  for (i = option_first; i; i = i->next)
 
117
    if (!strcmp(option, i->option)) {
 
118
      assert(i->type == S_BOOL);
 
119
      return i->u.bval;
 
120
    }
 
121
  return 0;
 
122
}
 
123
/* s_get_option_bool */
 
124
 
 
125
  int
 
126
s_delete_option(option)
 
127
  const char *option;
 
128
{
 
129
  struct option_s *i, *j;
 
130
 
 
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;
 
134
      free((char *)i);
 
135
      return 0;
 
136
    }
 
137
  return -1;
 
138
}
 
139
/* s_delete_option */
 
140
 
 
141
#if USE_STDARG
 
142
  static void
 
143
s_append(struct option_s *last,
 
144
         const char *option,
 
145
         const enum s_option_e type, ...)
 
146
#else
 
147
  static void
 
148
s_append(last, option, type, va_alist)
 
149
  struct option_s *last;
 
150
  const char *option;
 
151
  const enum s_option_e type; 
 
152
  va_dcl
 
153
#endif
 
154
{
 
155
  va_list ap;
 
156
  struct option_s *i;
 
157
 
 
158
#if USE_STDARG
 
159
  va_start(ap, type);
 
160
#else
 
161
  va_start(ap);
 
162
#endif
 
163
  i = (struct option_s *)malloc(sizeof(struct option_s));
 
164
  i->type = type;
 
165
  i->next = 0;
 
166
  strcpy(i->option, option);
 
167
  if (last) last->next = i; else option_first = i;
 
168
  i->action = 0;
 
169
  switch (type) {
 
170
    case S_STRING:
 
171
      strcpy(i->u.sval, va_arg(ap, char *));
 
172
      break;
 
173
    case S_INTEGER:
 
174
      i->u.ival = va_arg(ap, long);
 
175
      break;
 
176
    case S_BOOL:
 
177
      i->u.bval = !!va_arg(ap, int);
 
178
      break;
 
179
    default:
 
180
      abort();
 
181
  }
 
182
  va_end(ap);
 
183
}
 
184
/* s_append */
 
185
 
 
186
  void
 
187
s_set_option_string(option, value)
 
188
  const char *option;
 
189
  const char *value;
 
190
{
 
191
  struct option_s *i, *j;
 
192
 
 
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);
 
196
      i->action = 0;
 
197
      i->type = S_STRING;
 
198
      return;
 
199
    }
 
200
  s_append(j, option, S_STRING, value);
 
201
}
 
202
/* s_set_option_string */
 
203
 
 
204
  void
 
205
s_set_option_integer(option, value)
 
206
  const char *option;
 
207
  const long value;
 
208
{
 
209
  struct option_s *i, *j;
 
210
 
 
211
  for (i = option_first, j = 0; i; j = i, i = i->next)
 
212
    if (!strcmp(option, i->option)) {
 
213
      i->u.ival = value;
 
214
      i->type = S_INTEGER;
 
215
      i->action = 0;
 
216
      return;
 
217
    }
 
218
  s_append(j, option, S_INTEGER, value);
 
219
}
 
220
/* s_set_option_integer */
 
221
 
 
222
  void
 
223
s_set_option_bool(option, value)
 
224
  const char *option;
 
225
  const int value;
 
226
{
 
227
  struct option_s *i, *j;
 
228
 
 
229
  for (i = option_first, j = 0; i; j = i, i = i->next)
 
230
    if (!strcmp(option, i->option)) {
 
231
      i->u.bval = value;
 
232
      i->type = S_BOOL;
 
233
      i->action = 0;
 
234
      return;
 
235
    }
 
236
  s_append(j, option, S_BOOL, value);
 
237
}
 
238
/* s_set_option_bool */
 
239
 
 
240
  enum s_option_e
 
241
s_get_type(option)
 
242
  const char *option;
 
243
{
 
244
  struct option_s *i;
 
245
 
 
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;
 
249
}
 
250
/* s_get_type */
 
251
 
 
252
  char *
 
253
s_get_option(option)
 
254
  const char *option;
 
255
{
 
256
  struct option_s *i;
 
257
  static char rval[1024];
 
258
 
 
259
  for (i = option_first; i; i = i->next)
 
260
    if (!strcmp(option, i->option)) break;
 
261
  if (!i) return "";
 
262
  switch (i->type) {
 
263
    case S_STRING:
 
264
      strcpy(rval, i->u.sval);
 
265
      break;
 
266
    case S_INTEGER:
 
267
      sprintf(rval, "%li", i->u.ival);
 
268
      break;
 
269
    case S_BOOL:
 
270
      if (i->u.bval)
 
271
        strcpy(rval, true_strings[0]);
 
272
      else
 
273
        strcpy(rval, false_string);
 
274
      break;
 
275
    default:
 
276
      abort();
 
277
  }
 
278
  return rval;
 
279
}
 
280
/* s_get_option */
 
281
 
 
282
  void
 
283
s_set_type(option, type)
 
284
  const char *option;
 
285
  const enum s_option_e type;
 
286
{
 
287
  switch (type) {
 
288
    case S_STRING:
 
289
      s_set_option_string(option, "");
 
290
      break;
 
291
    case S_INTEGER:
 
292
      s_set_option_integer(option, 0);
 
293
      break;
 
294
    case S_BOOL:
 
295
      s_set_option_bool(option, 0);
 
296
      break;
 
297
    default:
 
298
      abort();
 
299
  }
 
300
}
 
301
/* s_set_type */
 
302
 
 
303
  void
 
304
s_set_option(option, value_string, no_action)
 
305
  const char *option;
 
306
  const char *value_string;
 
307
  int no_action;
 
308
{
 
309
  struct option_s *i;
 
310
  int k;
 
311
 
 
312
  for (i = option_first; i; i = i->next)
 
313
    if (!strcmp(option, i->option)) break;
 
314
  assert(i);
 
315
  switch (i->type) {
 
316
    case S_STRING:
 
317
      strcpy(i->u.sval, value_string);
 
318
      if (i->action && !no_action) i->action(value_string);
 
319
      break;
 
320
    case S_INTEGER:
 
321
      i->u.ival = atol(value_string);
 
322
      if (i->action && !no_action) i->action(i->u.ival);
 
323
      break;
 
324
    case S_BOOL:
 
325
      i->u.bval = 0;
 
326
      for (k = 0; true_strings[k]; ++k)
 
327
        if (!strcasecmp(true_strings[k], value_string)) {
 
328
          i->u.bval = 1;
 
329
          break;
 
330
        }
 
331
      if (i->action && !no_action) i->action(i->u.bval);
 
332
      break;
 
333
    default:
 
334
      abort();
 
335
  }
 
336
}
 
337
/* s_set_option */
 
338
 
 
339
  void
 
340
s_set_action(option, action)
 
341
  const char *option;
 
342
  set_fn action;
 
343
{
 
344
  struct option_s *i;
 
345
 
 
346
  for (i = option_first; i; i = i->next)
 
347
    if (!strcmp(i->option, option)) {
 
348
      i->action = action;
 
349
      return;
 
350
    }
 
351
  abort();
 
352
}
 
353
/* s_set_action */
 
354
 
 
355
  char **
 
356
s_option_list(prefix, bool_only)
 
357
  const char *prefix;
 
358
  int bool_only;
 
359
{
 
360
  struct option_s *i;
 
361
  char **list;
 
362
  int n;
 
363
 
 
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);
 
372
        ++n;
 
373
      }
 
374
  list[n] = 0;
 
375
  return list;
 
376
}
 
377
/* s_option_list */
 
378
 
 
379
  char **
 
380
s_option_value_list()
 
381
{
 
382
  struct option_s *i;
 
383
  char **list;
 
384
  int n, option_maxlen = 0;
 
385
 
 
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");
 
397
  }
 
398
  list[n] = 0;
 
399
  return list;
 
400
}
 
401
/* s_option_value_list */
 
402
    
 
403
  void
 
404
s_clear_all()
 
405
{
 
406
  struct option_s *i, *j;
 
407
 
 
408
  if (option_first) {
 
409
    for (j = option_first, i = j->next; i; j = i, i = i->next) free((char *)j);
 
410
    free((char *)j);
 
411
    option_first = 0;
 
412
  }
 
413
}
 
414
/* s_clear_all */
 
415
 
 
416
/* end of set.c */
 
417
 
 
418
 
 
419
/* VIM configuration: (do not delete this line)
 
420
 *
 
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:
 
423
 */