~ubuntu-branches/ubuntu/breezy/remind/breezy

« back to all changes in this revision

Viewing changes to src/var.c

  • Committer: Bazaar Package Importer
  • Author(s): Javier Fernandez-Sanguino Pen~a
  • Date: 1999-02-19 13:36:15 UTC
  • Revision ID: james.westby@ubuntu.com-19990219133615-ovob95sord67b0ks
Tags: 03.00.22-1
* NMU upload, maintainer seems to be missing.
* Changed to main (now GPL) (Closes: #42402)
* New upstream version (Closes: #59447)
* Moved to use debconf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************/
 
2
/*                                                             */
 
3
/*  VAR.C                                                      */
 
4
/*                                                             */
 
5
/*  This file contains routines, structures, etc for           */
 
6
/*  user- and system-defined variables.                        */
 
7
/*                                                             */
 
8
/*  This file is part of REMIND.                               */
 
9
/*  Copyright (C) 1992-1998 by David F. Skoll                  */
 
10
/*  Copyright (C) 1999-2000 by Roaring Penguin Software Inc.   */
 
11
/*                                                             */
 
12
/***************************************************************/
 
13
 
 
14
#include "config.h"
 
15
static char const RCSID[] = "$Id: var.c,v 1.7 2000/02/18 03:46:15 dfs Exp $";
 
16
 
 
17
#include <stdio.h>
 
18
#include <string.h>
 
19
#include <ctype.h>
 
20
 
 
21
#ifdef HAVE_STDLIB_H
 
22
#include <stdlib.h>
 
23
#endif
 
24
 
 
25
#ifdef HAVE_MALLOC_H
 
26
#include <malloc.h>
 
27
#endif
 
28
 
 
29
#include "types.h"
 
30
#include "expr.h"
 
31
#include "globals.h"
 
32
#include "protos.h"
 
33
#include "err.h"
 
34
 
 
35
#define UPPER(c) (islower(c) ? toupper(c) : c)
 
36
 
 
37
/* The variable hash table */
 
38
#define VAR_HASH_SIZE 64
 
39
#define VARIABLE ErrMsg[E_VAR]
 
40
#define VALUE    ErrMsg[E_VAL]
 
41
#define UNDEF    ErrMsg[E_UNDEF]
 
42
 
 
43
static Var *VHashTbl[VAR_HASH_SIZE];
 
44
 
 
45
/***************************************************************/
 
46
/*                                                             */
 
47
/*  HashVal                                                    */
 
48
/*  Given a string, compute the hash value.                    */
 
49
/*                                                             */
 
50
/***************************************************************/
 
51
#ifdef HAVE_PROTOS
 
52
PUBLIC unsigned int HashVal(const char *str)
 
53
#else
 
54
unsigned int HashVal(str)
 
55
char *str;
 
56
#endif
 
57
{
 
58
    register unsigned int i=0;
 
59
    register unsigned int j=1;
 
60
    register unsigned int len=0;
 
61
 
 
62
    while(*str && len < VAR_NAME_LEN) {
 
63
        i += j * (unsigned int) UPPER(*str);
 
64
        str++;
 
65
        len++;
 
66
        j = 3-j;
 
67
    }
 
68
    return i;
 
69
}
 
70
 
 
71
/***************************************************************/
 
72
/*                                                             */
 
73
/*  FindVar                                                    */
 
74
/*  Given a string, find the variable whose name is that       */
 
75
/*  string.  If create is 1, create the variable.              */
 
76
/*                                                             */
 
77
/***************************************************************/
 
78
#ifdef HAVE_PROTOS
 
79
PUBLIC Var *FindVar(const char *str, int create)
 
80
#else
 
81
Var *FindVar(str, create)
 
82
char *str;
 
83
int create;
 
84
#endif
 
85
{
 
86
    register int h;
 
87
    register Var *v;
 
88
    register Var *prev;
 
89
 
 
90
    h = HashVal(str) % VAR_HASH_SIZE;
 
91
    v = VHashTbl[h];
 
92
    prev = NULL;
 
93
 
 
94
    while(v) {
 
95
        if (! StrinCmp(str, v->name, VAR_NAME_LEN)) return v;
 
96
        prev = v;
 
97
        v = v-> next;
 
98
    }
 
99
    if (!create) return v;
 
100
 
 
101
/* Create the variable */
 
102
    v = NEW(Var);
 
103
    if (!v) return v;
 
104
    v->next = NULL;
 
105
    v->v.type = INT_TYPE;
 
106
    v->v.v.val = 0;
 
107
    v->preserve = 0;
 
108
    StrnCpy(v->name, str, VAR_NAME_LEN);
 
109
 
 
110
    if (prev) prev->next = v; else VHashTbl[h] = v;
 
111
    return v;
 
112
}
 
113
 
 
114
/***************************************************************/
 
115
/*                                                             */
 
116
/*  DeleteVar                                                  */
 
117
/*  Given a string, find the variable whose name is that       */
 
118
/*  string and delete it.                                      */
 
119
/*                                                             */
 
120
/***************************************************************/
 
121
#ifdef HAVE_PROTOS
 
122
PUBLIC int DeleteVar(const char *str)
 
123
#else
 
124
int DeleteVar(str)
 
125
char *str;
 
126
#endif
 
127
{
 
128
    register int h;
 
129
    register Var *v;
 
130
    register Var *prev;
 
131
 
 
132
    h = HashVal(str) % VAR_HASH_SIZE;
 
133
    v = VHashTbl[h];
 
134
    prev = NULL;
 
135
 
 
136
    while(v) {
 
137
        if (! StrinCmp(str, v->name, VAR_NAME_LEN)) break;
 
138
        prev = v;
 
139
        v = v-> next;
 
140
    }
 
141
    if (!v) return E_NOSUCH_VAR;
 
142
    DestroyValue(v->v);
 
143
    if (prev) prev->next = v->next; else VHashTbl[h] = v->next;
 
144
    free(v);
 
145
    return OK;
 
146
}
 
147
 
 
148
/***************************************************************/
 
149
/*                                                             */
 
150
/*  SetVar                                                     */
 
151
/*                                                             */
 
152
/*  Set the indicate variable to the specified value.          */
 
153
/*                                                             */
 
154
/***************************************************************/
 
155
#ifdef HAVE_PROTOS
 
156
PUBLIC int SetVar(const char *str, Value *val)
 
157
#else
 
158
int SetVar(str, val)
 
159
char *str;
 
160
Value *val;
 
161
#endif
 
162
{
 
163
    Var *v = FindVar(str, 1);
 
164
 
 
165
    if (!v) return E_NO_MEM;  /* Only way FindVar can fail */
 
166
 
 
167
    DestroyValue(v->v);
 
168
    v->v = *val;
 
169
    return OK;
 
170
}
 
171
 
 
172
/***************************************************************/
 
173
/*                                                             */
 
174
/*  GetVarValue                                                */
 
175
/*                                                             */
 
176
/*  Get a copy of the value of the variable.                   */
 
177
/*                                                             */
 
178
/***************************************************************/
 
179
#ifdef HAVE_PROTOS
 
180
PUBLIC int GetVarValue(const char *str, Value *val, Var *locals)
 
181
#else
 
182
int GetVarValue(str, val, locals)
 
183
char *str;
 
184
Value *val;
 
185
Var *locals;
 
186
#endif
 
187
{
 
188
    Var *v;
 
189
 
 
190
    /* Try searching local variables first */
 
191
    v = locals;
 
192
    while (v) {
 
193
        if (! StrinCmp(str, v->name, VAR_NAME_LEN))
 
194
            return CopyValue(val, &v->v);
 
195
        v = v->next;
 
196
    }
 
197
 
 
198
    v=FindVar(str, 0);
 
199
 
 
200
    if (!v) {
 
201
        Eprint("%s: %s", ErrMsg[E_NOSUCH_VAR], str);
 
202
        return E_NOSUCH_VAR;
 
203
    }
 
204
    return CopyValue(val, &v->v);
 
205
}
 
206
 
 
207
/***************************************************************/
 
208
/*                                                             */
 
209
/*  DoSet - set a variable.                                    */
 
210
/*                                                             */
 
211
/***************************************************************/
 
212
#ifdef HAVE_PROTOS
 
213
PUBLIC int DoSet (Parser *p)
 
214
#else
 
215
int DoSet (p)
 
216
Parser *p;
 
217
#endif
 
218
{
 
219
    Value v;
 
220
    int r;
 
221
 
 
222
    DynamicBuffer buf;
 
223
    DBufInit(&buf);
 
224
 
 
225
    r = ParseIdentifier(p, &buf);
 
226
    if (r) return r;
 
227
 
 
228
    r = EvaluateExpr(p, &v);
 
229
    if (r) {
 
230
        DBufFree(&buf);
 
231
        return r;
 
232
    }
 
233
 
 
234
    if (*DBufValue(&buf) == '$') r = SetSysVar(DBufValue(&buf)+1, &v);
 
235
    else r = SetVar(DBufValue(&buf), &v);
 
236
    DBufFree(&buf);
 
237
    return r;
 
238
}
 
239
 
 
240
/***************************************************************/
 
241
/*                                                             */
 
242
/*  DoUnset - delete a bunch of variables.                     */
 
243
/*                                                             */
 
244
/***************************************************************/
 
245
#ifdef HAVE_PROTOS
 
246
PUBLIC int DoUnset (Parser *p)
 
247
#else
 
248
int DoUnset (p)
 
249
Parser *p;
 
250
#endif
 
251
{
 
252
    int r;
 
253
 
 
254
    DynamicBuffer buf;
 
255
    DBufInit(&buf);
 
256
 
 
257
    r = ParseToken(p, &buf);
 
258
    if (r) return r;
 
259
    if (!DBufLen(&buf)) {
 
260
        DBufFree(&buf);
 
261
        return E_EOLN;
 
262
    }
 
263
 
 
264
    (void) DeleteVar(DBufValue(&buf));  /* Ignore error - nosuchvar */
 
265
 
 
266
/* Keep going... */
 
267
    while(1) {
 
268
        r = ParseToken(p, &buf);
 
269
        if (r) return r;
 
270
        if (!DBufLen(&buf)) {
 
271
            DBufFree(&buf);
 
272
            return OK;
 
273
        }
 
274
        (void) DeleteVar(DBufValue(&buf));
 
275
    }
 
276
}
 
277
 
 
278
/***************************************************************/
 
279
/*                                                             */
 
280
/*  DoDump                                                     */
 
281
/*                                                             */
 
282
/*  Command file command to dump variable table.               */
 
283
/*                                                             */
 
284
/***************************************************************/
 
285
#ifdef HAVE_PROTOS
 
286
PUBLIC int DoDump(ParsePtr p)
 
287
#else
 
288
int DoDump(p)
 
289
ParsePtr p;
 
290
#endif
 
291
{
 
292
    int r;
 
293
    Var *v;
 
294
    DynamicBuffer buf;
 
295
 
 
296
    DBufInit(&buf);
 
297
    r = ParseToken(p, &buf);
 
298
    if (r) return r;
 
299
    if (!*DBufValue(&buf) ||
 
300
        *DBufValue(&buf) == '#' ||
 
301
        *DBufValue(&buf) == ';') {
 
302
        DBufFree(&buf);
 
303
        DumpVarTable();
 
304
        return OK;
 
305
    }
 
306
    fprintf(ErrFp, "%*s  %s\n\n", VAR_NAME_LEN, VARIABLE, VALUE);
 
307
    while(1) {
 
308
        if (*DBufValue(&buf) == '$') {
 
309
            DumpSysVarByName(DBufValue(&buf)+1);
 
310
        } else {
 
311
            v = FindVar(DBufValue(&buf), 0);
 
312
            DBufValue(&buf)[VAR_NAME_LEN] = 0;
 
313
            if (!v) fprintf(ErrFp, "%*s  %s\n", VAR_NAME_LEN,
 
314
                            DBufValue(&buf), UNDEF);
 
315
            else {
 
316
                fprintf(ErrFp, "%*s  ", VAR_NAME_LEN, v->name);
 
317
                PrintValue(&(v->v), ErrFp);
 
318
                fprintf(ErrFp, "\n");
 
319
            }
 
320
        }
 
321
        r = ParseToken(p, &buf);
 
322
        if (r) return r;
 
323
        if (!*DBufValue(&buf) ||
 
324
            *DBufValue(&buf) == '#' ||
 
325
            *DBufValue(&buf) == ';') {
 
326
            DBufFree(&buf);
 
327
            return OK;
 
328
        }
 
329
    }
 
330
}
 
331
 
 
332
/***************************************************************/
 
333
/*                                                             */
 
334
/*  DumpVarTable                                               */
 
335
/*                                                             */
 
336
/*  Dump the variable table to stderr.                         */
 
337
/*                                                             */
 
338
/***************************************************************/
 
339
#ifdef HAVE_PROTOS
 
340
PUBLIC void DumpVarTable(void)
 
341
#else
 
342
void DumpVarTable()
 
343
#endif
 
344
{
 
345
    register Var *v;
 
346
    register int i;
 
347
 
 
348
    fprintf(ErrFp, "%*s  %s\n\n", VAR_NAME_LEN, VARIABLE, VALUE);
 
349
 
 
350
    for (i=0; i<VAR_HASH_SIZE; i++) {
 
351
        v = VHashTbl[i];
 
352
        while(v) {
 
353
            fprintf(ErrFp, "%*s  ", VAR_NAME_LEN, v->name);
 
354
            PrintValue(&(v->v), ErrFp);
 
355
            fprintf(ErrFp, "\n");
 
356
            v = v->next;
 
357
        }
 
358
    }
 
359
}
 
360
 
 
361
/***************************************************************/
 
362
/*                                                             */
 
363
/*  DestroyVars                                                */
 
364
/*                                                             */
 
365
/*  Free all the memory used by variables, but don't delete    */
 
366
/*  preserved variables unless ALL is non-zero.                */
 
367
/*                                                             */
 
368
/***************************************************************/
 
369
#ifdef HAVE_PROTOS
 
370
PUBLIC void DestroyVars(int all)
 
371
#else
 
372
void DestroyVars(all)
 
373
int all;
 
374
#endif
 
375
{
 
376
    int i;
 
377
    Var *v, *next, *prev;
 
378
 
 
379
    for (i=0; i<VAR_HASH_SIZE; i++) {
 
380
        v = VHashTbl[i];
 
381
        VHashTbl[i] = NULL;
 
382
        prev = NULL;
 
383
        while(v) {
 
384
            if (all || !v->preserve) {
 
385
                DestroyValue(v->v);
 
386
                next = v->next;
 
387
                free(v);
 
388
            } else {
 
389
                if (prev) prev->next = v;
 
390
                else VHashTbl[i] = v;
 
391
                prev = v;
 
392
                next = v->next;
 
393
                v->next = NULL;
 
394
            }
 
395
            v = next;
 
396
        }
 
397
    }
 
398
}
 
399
 
 
400
/***************************************************************/
 
401
/*                                                             */
 
402
/*  PreserveVar                                                */
 
403
/*                                                             */
 
404
/*  Given the name of a variable, "preserve" it.               */
 
405
/*                                                             */
 
406
/***************************************************************/
 
407
#ifdef HAVE_PROTOS
 
408
PUBLIC int PreserveVar(char *name)
 
409
#else
 
410
int PreserveVar(name)
 
411
char *name;
 
412
#endif
 
413
{
 
414
    Var *v;
 
415
 
 
416
    v = FindVar(name, 1);
 
417
    if (!v) return E_NO_MEM;
 
418
    v->preserve = 1;
 
419
    return OK;
 
420
}
 
421
 
 
422
/***************************************************************/
 
423
/*                                                             */
 
424
/*  DoPreserve - preserve a bunch of variables.                */
 
425
/*                                                             */
 
426
/***************************************************************/
 
427
#ifdef HAVE_PROTOS
 
428
PUBLIC int DoPreserve (Parser *p)
 
429
#else
 
430
int DoPreserve (p)
 
431
Parser *p;
 
432
#endif
 
433
{
 
434
    int r;
 
435
 
 
436
    DynamicBuffer buf;
 
437
    DBufInit(&buf);
 
438
 
 
439
    r = ParseToken(p, &buf);
 
440
    if (r) return r;
 
441
    if (!DBufLen(&buf)) {
 
442
        DBufFree(&buf);
 
443
        return E_EOLN;
 
444
    }
 
445
 
 
446
    r = PreserveVar(DBufValue(&buf));
 
447
    DBufFree(&buf);
 
448
    if (r) return r;
 
449
 
 
450
/* Keep going... */
 
451
    while(1) {
 
452
        r = ParseToken(p, &buf);
 
453
        if (r) return r;
 
454
        if (!DBufLen(&buf)) {
 
455
            DBufFree(&buf);
 
456
            return OK;
 
457
        }
 
458
        r = PreserveVar(DBufValue(&buf));
 
459
        DBufFree(&buf);
 
460
        if (r) return r;
 
461
    }
 
462
}
 
463
 
 
464
/***************************************************************/
 
465
/*                                                             */
 
466
/*  SYSTEM VARIABLES                                           */
 
467
/*                                                             */
 
468
/*  Interface for modifying and reading system variables.      */
 
469
/*                                                             */
 
470
/***************************************************************/
 
471
 
 
472
/* The structure of a system variable */
 
473
typedef struct {
 
474
    char *name;
 
475
    char modifiable;
 
476
    int type;
 
477
    void *value;
 
478
    int min;
 
479
    int max;
 
480
} SysVar;
 
481
 
 
482
/* If the type of a sys variable is STR_TYPE, then min is redefined
 
483
   to be a flag indicating whether or not the value has been malloc'd. */
 
484
#define been_malloced min
 
485
 
 
486
/* Flag for no min/max constraint */
 
487
#define ANY 4532
 
488
/* All of the system variables sorted alphabetically */
 
489
static SysVar SysVarArr[] = {
 
490
    /* name               mod   type            value           min/mal max */
 
491
    {   "CalcUTC",        1,    INT_TYPE,       &CalculateUTC,  0,      1   },
 
492
    {   "CalMode",        0,    INT_TYPE,       &DoCalendar,    0,      0   },
 
493
    {   "Daemon",         0,    INT_TYPE,       &Daemon,        0,      0   },
 
494
    {   "DefaultPrio",    1,    INT_TYPE,       &DefaultPrio,   0,      9999 },
 
495
    {   "DontFork",       0,    INT_TYPE,       &DontFork,      0,      0   },
 
496
    {   "DontQueue",      0,    INT_TYPE,       &DontQueue,     0,      0   },
 
497
    {   "DontTrigAts",    0,    INT_TYPE,       &DontIssueAts,  0,      0   },
 
498
    {   "EndSent",        1,    STR_TYPE,       &EndSent,       0,      0   },
 
499
    {   "EndSentIg",      1,    STR_TYPE,       &EndSentIg,     0,      0   },
 
500
    {   "FirstIndent",    1,    INT_TYPE,       &FirstIndent,   0,      132 },
 
501
    {   "FoldYear",       1,    INT_TYPE,       &FoldYear,      0,      1   },
 
502
    {   "FormWidth",      1,    INT_TYPE,       &FormWidth,     20,     132 },
 
503
    {   "HushMode",       0,    INT_TYPE,       &Hush,          0,      0   },
 
504
    {   "IgnoreOnce",     0,    INT_TYPE,       &IgnoreOnce,    0,      0   },
 
505
    {   "InfDelta",       0,    INT_TYPE,       &InfiniteDelta, 0,      0   },
 
506
    {   "LatDeg",         1,    INT_TYPE,       &LatDeg,        -90,    90  },
 
507
    {   "LatMin",         1,    INT_TYPE,       &LatMin,        -59,    59  },
 
508
    {   "LatSec",         1,    INT_TYPE,       &LatSec,        -59,    59  },
 
509
    {   "Location",       1,    STR_TYPE,       &Location,      0,      0   },
 
510
    {   "LongDeg",        1,    INT_TYPE,       &LongDeg,       -180,   180  },
 
511
    {   "LongMin",        1,    INT_TYPE,       &LongMin,       -59,    59  },
 
512
    {   "LongSec",        1,    INT_TYPE,       &LongSec,       -59,    59  },
 
513
    {   "MaxSatIter",     1,    INT_TYPE,       &MaxSatIter,    10,     ANY },
 
514
    {   "MinsFromUTC",    1,    INT_TYPE,       &MinsFromUTC,   -13*60, 13*60 },
 
515
    {   "NextMode",       0,    INT_TYPE,       &NextMode,      0,      0   },
 
516
    {   "NumQueued",      0,    INT_TYPE,       &NumQueued,     0,      0   },
 
517
    {   "NumTrig",        0,    INT_TYPE,       &NumTriggered,  0,      0   },
 
518
    {   "PSCal",                  0,    INT_TYPE,       &PsCal,         0,      0   },
 
519
    {   "RunOff",         0,    INT_TYPE,       &RunDisabled,   0,      0   },
 
520
    {   "SimpleCal",      0,    INT_TYPE,       &DoSimpleCalendar,      0,  0 },
 
521
    {   "SortByDate",     0,    INT_TYPE,       &SortByDate,    0,      0},
 
522
    {   "SortByPrio",     0,    INT_TYPE,       &SortByPrio,    0,      0},
 
523
    {   "SortByTime",     0,    INT_TYPE,       &SortByTime,    0,      0},
 
524
    {   "SubsIndent",     1,    INT_TYPE,       &SubsIndent,    0,      132}
 
525
};
 
526
 
 
527
#define NUMSYSVARS ( sizeof(SysVarArr) / sizeof(SysVar) )
 
528
PRIVATE SysVar *FindSysVar ARGS((const char *name));
 
529
PRIVATE void DumpSysVar ARGS((const char *name, const SysVar *v));
 
530
/***************************************************************/
 
531
/*                                                             */
 
532
/*  SetSysVar                                                  */
 
533
/*                                                             */
 
534
/*  Set a system variable to the indicated value.              */
 
535
/*                                                             */
 
536
/***************************************************************/
 
537
#ifdef HAVE_PROTOS
 
538
PUBLIC int SetSysVar(const char *name, Value *value)
 
539
#else
 
540
int SetSysVar(name, value)
 
541
char *name;
 
542
Value *value;
 
543
#endif
 
544
{
 
545
    SysVar *v = FindSysVar(name);
 
546
    if (!v) return E_NOSUCH_VAR;
 
547
    if (v->type != value->type) return E_BAD_TYPE;
 
548
    if (!v->modifiable) {
 
549
        Eprint("%s: `$%s'", ErrMsg[E_CANT_MODIFY], name);
 
550
        return E_CANT_MODIFY;
 
551
    }
 
552
 
 
553
/* If it's a string variable, special measures must be taken */
 
554
    if (v->type == STR_TYPE) {
 
555
        if (v->been_malloced) free(*((char **)(v->value)));
 
556
        v->been_malloced = 1;
 
557
        *((char **) v->value) = value->v.str;
 
558
        value->type = ERR_TYPE;  /* So that it's not accidentally freed */
 
559
    } else {
 
560
        if (v->max != ANY && value->v.val > v->max) return E_2HIGH;
 
561
        if (v->min != ANY && value->v.val < v->min) return E_2LOW;
 
562
        *((int *)v->value) = value->v.val;
 
563
    }
 
564
    return OK;
 
565
}
 
566
 
 
567
/***************************************************************/
 
568
/*                                                             */
 
569
/*  GetSysVar                                                  */
 
570
/*                                                             */
 
571
/*  Get the value of a system variable                         */
 
572
/*                                                             */
 
573
/***************************************************************/
 
574
#ifdef HAVE_PROTOS
 
575
PUBLIC int GetSysVar(const char *name, Value *val)
 
576
#else
 
577
int GetSysVar(name, val)
 
578
char *name;
 
579
Value *val;
 
580
#endif
 
581
{
 
582
    SysVar *v = FindSysVar(name);
 
583
 
 
584
    val->type = ERR_TYPE;
 
585
    if (!v) return E_NOSUCH_VAR;
 
586
    if (v->type == STR_TYPE) {
 
587
        val->v.str = StrDup(*((char **) v->value));
 
588
        if (!val->v.str) return E_NO_MEM;
 
589
    } else {
 
590
        val->v.val = *((int *) v->value);
 
591
    }
 
592
    val->type = v->type;
 
593
 
 
594
    /* In "verbose" mode, print attempts to test $RunOff */
 
595
    if (DebugFlag & DB_PRTLINE) {
 
596
        if (v->value == (void *) &RunDisabled) {
 
597
            Eprint("(Security note: $RunOff variable tested.)\n");
 
598
            /* Allow further messages from this line */
 
599
            FreshLine = 1;
 
600
        }
 
601
    }
 
602
    return OK;
 
603
}
 
604
 
 
605
/***************************************************************/
 
606
/*                                                             */
 
607
/* FindSysVar                                                  */
 
608
/*                                                             */
 
609
/* Find a system var with specified name.                      */
 
610
/*                                                             */
 
611
/***************************************************************/
 
612
#ifdef HAVE_PROTOS
 
613
PRIVATE SysVar *FindSysVar(const char *name)
 
614
#else
 
615
static SysVar *FindSysVar(name)
 
616
char *name;
 
617
#endif
 
618
{
 
619
    int top=NUMSYSVARS-1, bottom=0;
 
620
    int mid=(top + bottom) / 2;
 
621
    int r;
 
622
 
 
623
    while (top >= bottom) {
 
624
        r = StrCmpi(name, SysVarArr[mid].name);
 
625
        if (!r) return &SysVarArr[mid];
 
626
        else if (r>0) bottom = mid+1;
 
627
        else        top = mid-1;
 
628
        mid = (top+bottom) / 2;
 
629
    }
 
630
    return NULL;
 
631
}
 
632
   
 
633
/***************************************************************/
 
634
/*                                                             */
 
635
/*  DumpSysVarByName                                           */
 
636
/*                                                             */
 
637
/*  Given the name of a system variable, display it.           */
 
638
/*  If name is "", dump all system variables.                  */
 
639
/*                                                             */
 
640
/***************************************************************/
 
641
#ifdef HAVE_PROTOS
 
642
PUBLIC void DumpSysVarByName(const char *name)
 
643
#else
 
644
void DumpSysVarByName(name)
 
645
char *name;
 
646
#endif
 
647
{
 
648
    int i;
 
649
    SysVar *v;
 
650
 
 
651
    if (!name || !*name) {
 
652
        for (i=0; i<NUMSYSVARS; i++) DumpSysVar(name, SysVarArr + i);
 
653
        return;
 
654
    }
 
655
   
 
656
    v = FindSysVar(name);
 
657
    DumpSysVar(name, v);
 
658
    return;
 
659
}
 
660
 
 
661
/***************************************************************/
 
662
/*                                                             */
 
663
/*  DumpSysVar                                                 */
 
664
/*                                                             */
 
665
/*  Dump the system variable.                                  */
 
666
/*                                                             */
 
667
/***************************************************************/
 
668
#ifdef HAVE_PROTOS
 
669
PRIVATE void DumpSysVar(const char *name, const SysVar *v)
 
670
#else
 
671
static void DumpSysVar(name, v)
 
672
char *name;
 
673
SysVar *v;
 
674
#endif
 
675
{
 
676
    char buffer[VAR_NAME_LEN+10];
 
677
 
 
678
    if (name && !*name) name=NULL;
 
679
    if (!v && !name) return;  /* Shouldn't happen... */
 
680
   
 
681
    buffer[0]='$'; buffer[1] = 0;
 
682
    if (name) strcat(buffer, name); else strcat(buffer, v->name);
 
683
    fprintf(ErrFp, "%*s  ", VAR_NAME_LEN, buffer);
 
684
    if (v) {
 
685
        if (v->type == STR_TYPE) {
 
686
            char *s = *((char **)v->value);
 
687
            int y;
 
688
            Putc('"', ErrFp);
 
689
            for (y=0; y<MAX_PRT_LEN && *s; y++) Putc(*s++, ErrFp);
 
690
            Putc('"', ErrFp);
 
691
            if (*s) fprintf(ErrFp, "...");
 
692
            Putc('\n', ErrFp);
 
693
        } else {
 
694
            if (!v->modifiable) fprintf(ErrFp, "%d\n", *((int *)v->value));
 
695
            else {
 
696
                fprintf(ErrFp, "%-10d  ", *((int *)v->value));
 
697
                if (v->min == ANY) fprintf(ErrFp, "(-Inf, ");
 
698
                else                         fprintf(ErrFp, "[%d, ", v->min);
 
699
                if (v->max == ANY) fprintf(ErrFp, "Inf)\n");
 
700
                else                         fprintf(ErrFp, "%d]\n", v->max);
 
701
            }
 
702
        }
 
703
    } else   fprintf(ErrFp, "%s\n", UNDEF);
 
704
 
 
705
    return;
 
706
}
 
707