2 * a generic (simple) parser. Use to parse rr's, private key
3 * information and /etc/resolv.conf files
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
11 #include "sldns/parse.h"
12 #include "sldns/parseutil.h"
13 #include "sldns/sbuffer.h"
18 sldns_lookup_table sldns_directive_types[] = {
19 { LDNS_DIR_TTL, "$TTL" },
20 { LDNS_DIR_ORIGIN, "$ORIGIN" },
21 { LDNS_DIR_INCLUDE, "$INCLUDE" },
25 /* add max_limit here? */
27 sldns_fget_token(FILE *f, char *token, const char *delim, size_t limit)
29 return sldns_fget_token_l(f, token, delim, limit, NULL);
33 sldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr)
36 int p; /* 0 -> no parentheses seen, >0 nr of ( seen */
43 /* standard delimiters */
46 del = LDNS_PARSE_NORMAL;
60 while ((c = getc(f)) != EOF) {
61 if (c == '\r') /* carriage return */
63 if (c == '(' && prev_c != '\\' && !quoted) {
64 /* this only counts for non-comments */
72 if (c == ')' && prev_c != '\\' && !quoted) {
73 /* this only counts for non-comments */
82 /* more ) then ( - close off the string */
87 /* do something with comments ; */
88 if (c == ';' && quoted == 0) {
93 if (c == '\"' && com == 0 && prev_c != '\\') {
97 if (c == '\n' && com != 0) {
102 *line_nr = *line_nr + 1;
104 if (p == 0 && i > 0) {
118 if (c == '\n' && p != 0 && t > token) {
121 *line_nr = *line_nr + 1;
123 if (limit > 0 && (i+1 >= limit || (size_t)(t-token)+1 >= limit)) {
132 /* check if we hit the delim */
133 for (d = del; *d; d++) {
134 if (c == *d && i > 0 && prev_c != '\\' && p == 0) {
135 if (c == '\n' && line_nr) {
136 *line_nr = *line_nr + 1;
141 if (c != '\0' && c != '\n') {
144 /* is there space for the character and the zero after it */
145 if (limit > 0 && (i+1 >= limit || (size_t)(t-token)+1 >= limit)) {
149 if (c != '\0' && c != '\n') {
152 if (c == '\\' && prev_c == '\\')
172 /* do not skip over quotes after the string, they are part
173 * of the next string. But skip over whitespace (if needed)*/
174 sldns_fskipcs_l(f, del+1, line_nr);
175 else sldns_fskipcs_l(f, del, line_nr);
185 sldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data,
186 const char *d_del, size_t data_limit)
188 return sldns_fget_keyword_data_l(f, keyword, k_del, data, d_del,
193 sldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data,
194 const char *d_del, size_t data_limit, int *line_nr)
196 /* we assume: keyword|sep|data */
200 if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
202 fkeyword = (char*)malloc(LDNS_MAX_KEYWORDLEN);
206 i = sldns_fget_token(f, fkeyword, k_del, LDNS_MAX_KEYWORDLEN);
212 /* case??? i instead of strlen? */
213 if (strncmp(fkeyword, keyword, LDNS_MAX_KEYWORDLEN - 1) == 0) {
215 /* printf("%s\n%s\n", "Matching keyword", fkeyword); */
216 i = sldns_fget_token_l(f, data, d_del, data_limit, line_nr);
220 /*printf("no match for %s (read: %s)\n", keyword, fkeyword);*/
227 sldns_bgetc(sldns_buffer *buffer)
229 if (!sldns_buffer_available_at(buffer, buffer->_position, sizeof(uint8_t))) {
230 sldns_buffer_set_position(buffer, sldns_buffer_limit(buffer));
231 /* sldns_buffer_rewind(buffer);*/
234 return (int)sldns_buffer_read_u8(buffer);
238 sldns_bget_token(sldns_buffer *b, char *token, const char *delim, size_t limit)
240 return sldns_bget_token_par(b, token, delim, limit, NULL, NULL);
244 sldns_bget_token_par(sldns_buffer *b, char *token, const char *delim,
245 size_t limit, int* par, const char* skipw)
248 int p; /* 0 -> no parentheses seen, >0 nr of ( seen */
255 /* standard delimiters */
257 /* from isspace(3) */
258 del = LDNS_PARSE_NORMAL;
273 while ((c = sldns_bgetc(b)) != EOF) {
274 if (c == '\r') /* carriage return */
276 if (c == '(' && lc != '\\' && !quoted) {
277 /* this only counts for non-comments */
286 if (c == ')' && lc != '\\' && !quoted) {
287 /* this only counts for non-comments */
302 /* do something with comments ; */
303 if (c == ';' && quoted == 0) {
308 if (c == '"' && com == 0 && lc != '\\') {
312 if (c == '\n' && com != 0) {
326 if (c == '\n' && p != 0) {
328 /* do not write ' ' if we want to skip spaces */
329 if(!(skipw && (strchr(skipw, c)||strchr(skipw, ' ')))) {
330 /* check for space for the space character and a zero delimiter after that. */
331 if (limit > 0 && (i+1 >= limit || (size_t)(t-token)+1 >= limit)) {
341 /* check to skip whitespace at start, but also after ( */
342 if(skipw && i==0 && !com && !quoted && lc != '\\') {
343 if(strchr(skipw, c)) {
349 /* check if we hit the delim */
350 for (d = del; *d; d++) {
351 /* we can only exit if no parens or user tracks them */
352 if (c == *d && lc != '\\' && (p == 0 || par)) {
358 if (limit > 0 && (i+1 >= limit || (size_t)(t-token)+1 >= limit)) {
364 if (c == '\\' && lc == '\\') {
375 if (!par && p != 0) {
382 /* do not skip over quotes after the string, they are part
383 * of the next string. But skip over whitespace (if needed)*/
384 sldns_bskipcs(b, del+1);
385 else sldns_bskipcs(b, del);
388 if (!par && p != 0) {
396 sldns_bskipcs(sldns_buffer *buffer, const char *s)
402 while(sldns_buffer_available_at(buffer, buffer->_position, sizeof(char))) {
403 c = (char) sldns_buffer_read_u8_at(buffer, buffer->_position);
405 for (d = s; *d; d++) {
410 if (found && buffer->_limit > buffer->_position) {
411 buffer->_position += sizeof(char);
419 sldns_fskipcs(FILE *fp, const char *s)
421 sldns_fskipcs_l(fp, s, NULL);
425 sldns_fskipcs_l(FILE *fp, const char *s, int *line_nr)
431 while ((c = fgetc(fp)) != EOF) {
432 if (line_nr && c == '\n') {
433 *line_nr = *line_nr + 1;
436 for (d = s; *d; d++) {
442 /* with getc, we've read too far */
450 sldns_bget_keyword_data(sldns_buffer *b, const char *keyword, const char *k_del, char
451 *data, const char *d_del, size_t data_limit)
453 /* we assume: keyword|sep|data */
457 if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
459 fkeyword = (char*)malloc(LDNS_MAX_KEYWORDLEN);
461 return -1; /* out of memory */
463 i = sldns_bget_token(b, fkeyword, k_del, data_limit);
466 return -1; /* nothing read */
470 if (strncmp(fkeyword, keyword, strlen(keyword)) == 0) {
472 /* whee, the match! */
473 /* retrieve it's data */
474 i = sldns_bget_token(b, data, d_del, 0);