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
|
/*
* Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
* Portions Copyright (C) 2001 Internet Software Consortium.
* Portions Copyright (C) 2001 Nominum, Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $ISC: alist.c,v 1.2.206.1 2004/03/06 08:15:18 marka Exp $ */
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <isccc/alist.h>
#include <isc/assertions.h>
#include <isccc/result.h>
#include <isccc/sexpr.h>
#include <isccc/util.h>
#define CAR(s) (s)->value.as_dottedpair.car
#define CDR(s) (s)->value.as_dottedpair.cdr
#define ALIST_TAG "*alist*"
#define MAX_INDENT 64
static char spaces[MAX_INDENT + 1] =
" ";
isccc_sexpr_t *
isccc_alist_create(void)
{
isccc_sexpr_t *alist, *tag;
tag = isccc_sexpr_fromstring(ALIST_TAG);
if (tag == NULL)
return (NULL);
alist = isccc_sexpr_cons(tag, NULL);
if (alist == NULL) {
isccc_sexpr_free(&tag);
return (NULL);
}
return (alist);
}
isc_boolean_t
isccc_alist_alistp(isccc_sexpr_t *alist)
{
isccc_sexpr_t *car;
if (alist == NULL || alist->type != ISCCC_SEXPRTYPE_DOTTEDPAIR)
return (ISC_FALSE);
car = CAR(alist);
if (car == NULL || car->type != ISCCC_SEXPRTYPE_STRING)
return (ISC_FALSE);
if (strcmp(car->value.as_string, ALIST_TAG) != 0)
return (ISC_FALSE);
return (ISC_TRUE);
}
isc_boolean_t
isccc_alist_emptyp(isccc_sexpr_t *alist)
{
REQUIRE(isccc_alist_alistp(alist));
if (CDR(alist) == NULL)
return (ISC_TRUE);
return (ISC_FALSE);
}
isccc_sexpr_t *
isccc_alist_first(isccc_sexpr_t *alist)
{
REQUIRE(isccc_alist_alistp(alist));
return (CDR(alist));
}
isccc_sexpr_t *
isccc_alist_assq(isccc_sexpr_t *alist, const char *key)
{
isccc_sexpr_t *car, *caar;
REQUIRE(isccc_alist_alistp(alist));
/*
* Skip alist type tag.
*/
alist = CDR(alist);
while (alist != NULL) {
INSIST(alist->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
car = CAR(alist);
INSIST(car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
caar = CAR(car);
if (caar->type == ISCCC_SEXPRTYPE_STRING &&
strcmp(caar->value.as_string, key) == 0)
return (car);
alist = CDR(alist);
}
return (NULL);
}
void
isccc_alist_delete(isccc_sexpr_t *alist, const char *key)
{
isccc_sexpr_t *car, *caar, *rest, *prev;
REQUIRE(isccc_alist_alistp(alist));
prev = alist;
rest = CDR(alist);
while (rest != NULL) {
INSIST(rest->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
car = CAR(rest);
INSIST(car != NULL && car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
caar = CAR(car);
if (caar->type == ISCCC_SEXPRTYPE_STRING &&
strcmp(caar->value.as_string, key) == 0) {
CDR(prev) = CDR(rest);
CDR(rest) = NULL;
isccc_sexpr_free(&rest);
break;
}
prev = rest;
rest = CDR(rest);
}
}
isccc_sexpr_t *
isccc_alist_define(isccc_sexpr_t *alist, const char *key, isccc_sexpr_t *value)
{
isccc_sexpr_t *kv, *k, *elt;
kv = isccc_alist_assq(alist, key);
if (kv == NULL) {
/*
* New association.
*/
k = isccc_sexpr_fromstring(key);
if (k == NULL)
return (NULL);
kv = isccc_sexpr_cons(k, value);
if (kv == NULL) {
isccc_sexpr_free(&kv);
return (NULL);
}
elt = isccc_sexpr_addtolist(&alist, kv);
if (elt == NULL) {
isccc_sexpr_free(&kv);
return (NULL);
}
} else {
/*
* We've already got an entry for this key. Replace it.
*/
isccc_sexpr_free(&CDR(kv));
CDR(kv) = value;
}
return (kv);
}
isccc_sexpr_t *
isccc_alist_definestring(isccc_sexpr_t *alist, const char *key, const char *str)
{
isccc_sexpr_t *v, *kv;
v = isccc_sexpr_fromstring(str);
if (v == NULL)
return (NULL);
kv = isccc_alist_define(alist, key, v);
if (kv == NULL)
isccc_sexpr_free(&v);
return (kv);
}
isccc_sexpr_t *
isccc_alist_definebinary(isccc_sexpr_t *alist, const char *key, isccc_region_t *r)
{
isccc_sexpr_t *v, *kv;
v = isccc_sexpr_frombinary(r);
if (v == NULL)
return (NULL);
kv = isccc_alist_define(alist, key, v);
if (kv == NULL)
isccc_sexpr_free(&v);
return (kv);
}
isccc_sexpr_t *
isccc_alist_lookup(isccc_sexpr_t *alist, const char *key)
{
isccc_sexpr_t *kv;
kv = isccc_alist_assq(alist, key);
if (kv != NULL)
return (CDR(kv));
return (NULL);
}
isc_result_t
isccc_alist_lookupstring(isccc_sexpr_t *alist, const char *key, char **strp)
{
isccc_sexpr_t *kv, *v;
kv = isccc_alist_assq(alist, key);
if (kv != NULL) {
v = CDR(kv);
if (isccc_sexpr_stringp(v)) {
if (strp != NULL)
*strp = isccc_sexpr_tostring(v);
return (ISC_R_SUCCESS);
} else
return (ISC_R_EXISTS);
}
return (ISC_R_NOTFOUND);
}
isc_result_t
isccc_alist_lookupbinary(isccc_sexpr_t *alist, const char *key, isccc_region_t **r)
{
isccc_sexpr_t *kv, *v;
kv = isccc_alist_assq(alist, key);
if (kv != NULL) {
v = CDR(kv);
if (isccc_sexpr_binaryp(v)) {
if (r != NULL)
*r = isccc_sexpr_tobinary(v);
return (ISC_R_SUCCESS);
} else
return (ISC_R_EXISTS);
}
return (ISC_R_NOTFOUND);
}
void
isccc_alist_prettyprint(isccc_sexpr_t *sexpr, unsigned int indent, FILE *stream)
{
isccc_sexpr_t *elt, *kv, *k, *v;
if (isccc_alist_alistp(sexpr)) {
fprintf(stream, "{\n");
indent += 4;
for (elt = isccc_alist_first(sexpr);
elt != NULL;
elt = CDR(elt)) {
kv = CAR(elt);
INSIST(isccc_sexpr_listp(kv));
k = CAR(kv);
v = CDR(kv);
INSIST(isccc_sexpr_stringp(k));
fprintf(stream, "%.*s%s => ", (int)indent, spaces,
isccc_sexpr_tostring(k));
isccc_alist_prettyprint(v, indent, stream);
if (CDR(elt) != NULL)
fprintf(stream, ",");
fprintf(stream, "\n");
}
indent -= 4;
fprintf(stream, "%.*s}", (int)indent, spaces);
} else if (isccc_sexpr_listp(sexpr)) {
fprintf(stream, "(\n");
indent += 4;
for (elt = sexpr;
elt != NULL;
elt = CDR(elt)) {
fprintf(stream, "%.*s", (int)indent, spaces);
isccc_alist_prettyprint(CAR(elt), indent, stream);
if (CDR(elt) != NULL)
fprintf(stream, ",");
fprintf(stream, "\n");
}
indent -= 4;
fprintf(stream, "%.*s)", (int)indent, spaces);
} else
isccc_sexpr_print(sexpr, stream);
}
|