ldns/drill: Update vendor branch to 1.6.9
[dragonfly.git] / contrib / ldns / parse.c
CommitLineData
825eb42b 1/*
ac996e71 2 * a generic (simple) parser. Use to parse rr's, private key
825eb42b
JL
3 * information and /etc/resolv.conf files
4 *
5 * a Net::DNS like library for C
6 * LibDNS Team @ NLnet Labs
7 * (c) NLnet Labs, 2005-2006
8 * See the file LICENSE for the license
9 */
10#include <ldns/config.h>
11#include <ldns/ldns.h>
12
13#include <limits.h>
14#include <strings.h>
15
16ldns_lookup_table ldns_directive_types[] = {
ac996e71
JL
17 { LDNS_DIR_TTL, "$TTL" },
18 { LDNS_DIR_ORIGIN, "$ORIGIN" },
19 { LDNS_DIR_INCLUDE, "$INCLUDE" },
825eb42b
JL
20 { 0, NULL }
21};
22
23/* add max_limit here? */
24ssize_t
25ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit)
ac996e71 26{
825eb42b
JL
27 return ldns_fget_token_l(f, token, delim, limit, NULL);
28}
29
30ssize_t
31ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr)
ac996e71 32{
825eb42b
JL
33 int c, prev_c;
34 int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
35 int com, quoted;
36 char *t;
37 size_t i;
38 const char *d;
39 const char *del;
40
41 /* standard delimeters */
42 if (!delim) {
43 /* from isspace(3) */
44 del = LDNS_PARSE_NORMAL;
45 } else {
46 del = delim;
47 }
48
49 p = 0;
50 i = 0;
51 com = 0;
52 quoted = 0;
53 prev_c = 0;
54 t = token;
55 if (del[0] == '"') {
56 quoted = 1;
57 }
58 while ((c = getc(f)) != EOF) {
59 if (c == '(' && prev_c != '\\' && !quoted) {
60 /* this only counts for non-comments */
61 if (com == 0) {
62 p++;
63 }
64 prev_c = c;
65 continue;
66 }
67
68 if (c == ')' && prev_c != '\\' && !quoted) {
69 /* this only counts for non-comments */
70 if (com == 0) {
71 p--;
72 }
73 prev_c = c;
74 continue;
75 }
76
77 if (p < 0) {
78 /* more ) then ( - close off the string */
79 *t = '\0';
80 return 0;
81 }
82
83 /* do something with comments ; */
84 if (c == ';' && quoted == 0) {
85 if (prev_c != '\\') {
86 com = 1;
87 }
88 }
89 if (c == '\"' && com == 0 && prev_c != '\\') {
90 quoted = 1 - quoted;
91 }
92
93 if (c == '\n' && com != 0) {
94 /* comments */
95 com = 0;
96 *t = ' ';
97 if (line_nr) {
98 *line_nr = *line_nr + 1;
99 }
100 if (p == 0 && i > 0) {
101 goto tokenread;
102 } else {
103 prev_c = c;
104 continue;
105 }
106 }
107
108 if (com == 1) {
109 *t = ' ';
110 prev_c = c;
111 continue;
112 }
113
ac996e71 114
825eb42b
JL
115 if (c == '\n' && p != 0 && t > token) {
116 /* in parentheses */
117 if (line_nr) {
118 *line_nr = *line_nr + 1;
119 }
ac996e71 120 *t++ = ' ';
825eb42b
JL
121 prev_c = c;
122 continue;
123 }
124
125 /* check if we hit the delim */
126 for (d = del; *d; d++) {
127 if (c == *d && i > 0 && prev_c != '\\') {
128 if (c == '\n' && line_nr) {
129 *line_nr = *line_nr + 1;
130 }
131 goto tokenread;
132 }
133 }
134 if (c != '\0' && c != '\n') {
135 *t++ = c;
136 i++;
137 }
138 if (limit > 0 && i >= limit) {
139 *t = '\0';
140 return -1;
141 }
142 if (c == '\\' && prev_c == '\\')
143 prev_c = 0;
144 else prev_c = c;
145 }
146 *t = '\0';
147 if (c == EOF) {
148 return (ssize_t)i;
149 }
150
151 if (i == 0) {
152 /* nothing read */
153 return -1;
154 }
155 if (p != 0) {
156 return -1;
157 }
158 return (ssize_t)i;
159
160tokenread:
161 ldns_fskipcs_l(f, delim, line_nr);
162 *t = '\0';
163 if (p != 0) {
164 return -1;
165 }
166
167 return (ssize_t)i;
168}
169
170ssize_t
171ldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data,
172 const char *d_del, size_t data_limit)
173{
ac996e71 174 return ldns_fget_keyword_data_l(f, keyword, k_del, data, d_del,
825eb42b
JL
175 data_limit, NULL);
176}
177
178ssize_t
179ldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data,
180 const char *d_del, size_t data_limit, int *line_nr)
181{
182 /* we assume: keyword|sep|data */
183 char *fkeyword;
184 ssize_t i;
185
fd185f4d
JL
186 if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
187 return -1;
825eb42b 188 fkeyword = LDNS_XMALLOC(char, LDNS_MAX_KEYWORDLEN);
ac996e71
JL
189 if(!fkeyword)
190 return -1;
825eb42b
JL
191
192 i = ldns_fget_token(f, fkeyword, k_del, LDNS_MAX_KEYWORDLEN);
ac996e71
JL
193 if(i==0 || i==-1) {
194 LDNS_FREE(fkeyword);
195 return -1;
196 }
825eb42b
JL
197
198 /* case??? i instead of strlen? */
199 if (strncmp(fkeyword, keyword, LDNS_MAX_KEYWORDLEN - 1) == 0) {
200 /* whee! */
201 /* printf("%s\n%s\n", "Matching keyword", fkeyword); */
202 i = ldns_fget_token_l(f, data, d_del, data_limit, line_nr);
203 LDNS_FREE(fkeyword);
204 return i;
205 } else {
206 /*printf("no match for %s (read: %s)\n", keyword, fkeyword);*/
207 LDNS_FREE(fkeyword);
208 return -1;
209 }
210}
211
212
213ssize_t
214ldns_bget_token(ldns_buffer *b, char *token, const char *delim, size_t limit)
ac996e71 215{
825eb42b
JL
216 int c, lc;
217 int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
218 int com, quoted;
219 char *t;
220 size_t i;
221 const char *d;
222 const char *del;
223
224 /* standard delimiters */
225 if (!delim) {
226 /* from isspace(3) */
227 del = LDNS_PARSE_NORMAL;
228 } else {
229 del = delim;
230 }
231
232 p = 0;
233 i = 0;
234 com = 0;
235 quoted = 0;
236 t = token;
237 lc = 0;
ac996e71 238 if (del[0] == '"') {
825eb42b
JL
239 quoted = 1;
240 }
241
242 while ((c = ldns_bgetc(b)) != EOF) {
243 if (c == '(' && lc != '\\' && !quoted) {
244 /* this only counts for non-comments */
245 if (com == 0) {
246 p++;
247 }
248 lc = c;
249 continue;
250 }
251
252 if (c == ')' && lc != '\\' && !quoted) {
253 /* this only counts for non-comments */
254 if (com == 0) {
255 p--;
256 }
257 lc = c;
258 continue;
259 }
260
261 if (p < 0) {
262 /* more ) then ( */
263 *t = '\0';
825eb42b
JL
264 return 0;
265 }
266
267 /* do something with comments ; */
268 if (c == ';' && quoted == 0) {
269 if (lc != '\\') {
270 com = 1;
271 }
272 }
273 if (c == '"' && com == 0 && lc != '\\') {
274 quoted = 1 - quoted;
275 }
276
277 if (c == '\n' && com != 0) {
278 /* comments */
279 com = 0;
280 *t = ' ';
281 lc = c;
282 continue;
283 }
284
285 if (com == 1) {
286 *t = ' ';
287 lc = c;
288 continue;
289 }
290
291 if (c == '\n' && p != 0) {
292 /* in parentheses */
ac996e71 293 *t++ = ' ';
825eb42b
JL
294 lc = c;
295 continue;
296 }
297
298 /* check if we hit the delim */
299 for (d = del; *d; d++) {
300 if (c == *d && lc != '\\') {
301 goto tokenread;
302 }
303 }
ac996e71 304
825eb42b
JL
305 *t++ = c;
306 i++;
ac996e71 307 if (limit > 0 && i >= limit) {
825eb42b
JL
308 *t = '\0';
309 return -1;
310 }
311
312 if (c == '\\' && lc == '\\') {
313 lc = 0;
314 } else {
315 lc = c;
316 }
317 }
318 *t = '\0';
319 if (i == 0) {
320 /* nothing read */
321 return -1;
322 }
323 if (p != 0) {
324 return -1;
325 }
326 return (ssize_t)i;
327
328tokenread:
329 ldns_bskipcs(b, delim);
330 *t = '\0';
ac996e71 331
825eb42b 332 if (p != 0) {
ac996e71 333 return -1;
825eb42b
JL
334 }
335 return (ssize_t)i;
336}
337
338void
339ldns_bskipc(ldns_buffer *buffer, char c)
340{
341 while (c == (char) ldns_buffer_read_u8_at(buffer, ldns_buffer_position(buffer))) {
ac996e71 342 if (ldns_buffer_available_at(buffer,
825eb42b
JL
343 buffer->_position + sizeof(char), sizeof(char))) {
344 buffer->_position += sizeof(char);
345 } else {
346 return;
347 }
348 }
349}
350
351void
352ldns_bskipcs(ldns_buffer *buffer, const char *s)
353{
354 bool found;
355 char c;
356 const char *d;
357
358 while(ldns_buffer_available_at(buffer, buffer->_position, sizeof(char))) {
359 c = (char) ldns_buffer_read_u8_at(buffer, buffer->_position);
360 found = false;
361 for (d = s; *d; d++) {
362 if (*d == c) {
363 found = true;
364 }
365 }
366 if (found && buffer->_limit > buffer->_position) {
367 buffer->_position += sizeof(char);
368 } else {
369 return;
370 }
371 }
372}
373
374void
375ldns_fskipc(FILE *fp, char c)
376{
377 fp = fp;
378 c = c;
379}
380
381
382void
383ldns_fskipcs(FILE *fp, const char *s)
384{
385 ldns_fskipcs_l(fp, s, NULL);
386}
387
388void
389ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr)
390{
391 bool found;
392 int c;
393 const char *d;
394
395 while ((c = fgetc(fp)) != EOF) {
396 if (line_nr && c == '\n') {
397 *line_nr = *line_nr + 1;
398 }
399 found = false;
400 for (d = s; *d; d++) {
401 if (*d == c) {
402 found = true;
403 }
404 }
405 if (!found) {
406 /* with getc, we've read too far */
407 ungetc(c, fp);
408 return;
409 }
410 }
411}
412
413ssize_t
414ldns_bget_keyword_data(ldns_buffer *b, const char *keyword, const char *k_del, char
415*data, const char *d_del, size_t data_limit)
416{
417 /* we assume: keyword|sep|data */
418 char *fkeyword;
419 ssize_t i;
420
fd185f4d
JL
421 if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
422 return -1;
825eb42b 423 fkeyword = LDNS_XMALLOC(char, LDNS_MAX_KEYWORDLEN);
ac996e71
JL
424 if(!fkeyword)
425 return -1; /* out of memory */
825eb42b
JL
426
427 i = ldns_bget_token(b, fkeyword, k_del, data_limit);
ac996e71
JL
428 if(i==0 || i==-1) {
429 LDNS_FREE(fkeyword);
430 return -1; /* nothing read */
431 }
825eb42b
JL
432
433 /* case??? */
434 if (strncmp(fkeyword, keyword, strlen(keyword)) == 0) {
ac996e71 435 LDNS_FREE(fkeyword);
825eb42b
JL
436 /* whee, the match! */
437 /* retrieve it's data */
438 i = ldns_bget_token(b, data, d_del, 0);
439 return i;
440 } else {
ac996e71 441 LDNS_FREE(fkeyword);
825eb42b
JL
442 return -1;
443 }
444}
445