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
|
/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 Free Software Foundation,
Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
#include "gdbmtool.h"
#define VARF_DFL 0x00
#define VARF_SET 0x01
struct variable
{
char *name;
int type;
int flags;
union
{
char *string;
int bool;
} v;
};
static struct variable vartab[] = {
/* Top-level prompt */
{ "ps1", VART_STRING, VARF_DFL, { "%p>%_" } },
/* Second-level prompt (used within "def" block) */
{ "ps2", VART_STRING, VARF_DFL, { "%_>%_" } },
/* This delimits array members */
{ "delim1", VART_STRING, VARF_DFL, { "," } },
/* This delimits structure members */
{ "delim2", VART_STRING, VARF_DFL, { "," } },
{ NULL }
};
static struct variable *
varfind (const char *name)
{
struct variable *vp;
for (vp = vartab; vp->name; vp++)
if (strcmp (vp->name, name) == 0)
return vp;
return NULL;
}
int
variable_set (const char *name, int type, void *val)
{
struct variable *vp = varfind (name);
if (!vp)
return VAR_ERR_NOTDEF;
if (type != vp->type)
return VAR_ERR_BADTYPE;
switch (vp->type)
{
case VART_STRING:
if (vp->flags && VARF_SET)
free (vp->v.string);
vp->v.string = estrdup (val);
break;
case VART_BOOL:
if (val == NULL)
vp->v.bool = 0;
else
vp->v.bool = *(int*)val;
break;
}
vp->flags |= VARF_SET;
return VAR_OK;
}
int
variable_get (const char *name, int type, void **val)
{
struct variable *vp = varfind (name);
if (!vp)
return VAR_ERR_NOTDEF;
if (type != vp->type)
return VAR_ERR_BADTYPE;
switch (vp->type)
{
case VART_STRING:
*val = vp->v.string;
break;
case VART_BOOL:
*(int*)val = vp->v.bool;
break;
}
return VAR_OK;
}
void
variable_print_all (FILE *fp)
{
struct variable *vp;
char *s;
for (vp = vartab; vp->name; vp++)
{
switch (vp->type)
{
case VART_BOOL:
fprintf (fp, "%s%s", vp->v.bool ? "no" : "", vp->name);
break;
case VART_STRING:
fprintf (fp, "%s=\"", vp->name);
for (s = vp->v.string; *s; s++)
{
int c;
if (isprint (*s))
fputc (*s, fp);
else if ((c = escape (*s)))
fprintf (fp, "\\%c", c);
else
fprintf (fp, "\\%03o", *s);
}
fprintf (fp, "\"");
}
fputc ('\n', fp);
}
}
|