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 "ldns/parse.h"
12 #include "ldns/parseutil.h"
13 #include "ldns/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 parenthese seen, >0 nr of ( seen */
43 /* standard delimeters */
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;
128 /* check if we hit the delim */
129 for (d = del; *d; d++) {
130 if (c == *d && i > 0 && prev_c != '\\' && p == 0) {
131 if (c == '\n' && line_nr) {
132 *line_nr = *line_nr + 1;
137 if (c != '\0' && c != '\n') {
140 if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
144 if (c != '\0' && c != '\n') {
147 if (c == '\\' && prev_c == '\\')
167 /* do not skip over quotes after the string, they are part
168 * of the next string. But skip over whitespace (if needed)*/
169 sldns_fskipcs_l(f, del+1, line_nr);
170 else sldns_fskipcs_l(f, del, line_nr);
180 sldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data,
181 const char *d_del, size_t data_limit)
183 return sldns_fget_keyword_data_l(f, keyword, k_del, data, d_del,
188 sldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data,
189 const char *d_del, size_t data_limit, int *line_nr)
191 /* we assume: keyword|sep|data */
195 if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
197 fkeyword = (char*)malloc(LDNS_MAX_KEYWORDLEN);
201 i = sldns_fget_token(f, fkeyword, k_del, LDNS_MAX_KEYWORDLEN);
207 /* case??? i instead of strlen? */
208 if (strncmp(fkeyword, keyword, LDNS_MAX_KEYWORDLEN - 1) == 0) {
210 /* printf("%s\n%s\n", "Matching keyword", fkeyword); */
211 i = sldns_fget_token_l(f, data, d_del, data_limit, line_nr);
215 /*printf("no match for %s (read: %s)\n", keyword, fkeyword);*/
222 sldns_bgetc(sldns_buffer *buffer)
224 if (!sldns_buffer_available_at(buffer, buffer->_position, sizeof(uint8_t))) {
225 sldns_buffer_set_position(buffer, sldns_buffer_limit(buffer));
226 /* sldns_buffer_rewind(buffer);*/
229 return (int)sldns_buffer_read_u8(buffer);
233 sldns_bget_token(sldns_buffer *b, char *token, const char *delim, size_t limit)
235 return sldns_bget_token_par(b, token, delim, limit, NULL, NULL);
239 sldns_bget_token_par(sldns_buffer *b, char *token, const char *delim,
240 size_t limit, int* par, const char* skipw)
243 int p; /* 0 -> no parenthese seen, >0 nr of ( seen */
250 /* standard delimiters */
252 /* from isspace(3) */
253 del = LDNS_PARSE_NORMAL;
268 while ((c = sldns_bgetc(b)) != EOF) {
269 if (c == '\r') /* carriage return */
271 if (c == '(' && lc != '\\' && !quoted) {
272 /* this only counts for non-comments */
281 if (c == ')' && lc != '\\' && !quoted) {
282 /* this only counts for non-comments */
297 /* do something with comments ; */
298 if (c == ';' && quoted == 0) {
303 if (c == '"' && com == 0 && lc != '\\') {
307 if (c == '\n' && com != 0) {
321 if (c == '\n' && p != 0) {
323 /* do not write ' ' if we want to skip spaces */
324 if(!(skipw && (strchr(skipw, c)||strchr(skipw, ' '))))
330 /* check to skip whitespace at start, but also after ( */
331 if(skipw && i==0 && !com && !quoted && lc != '\\') {
332 if(strchr(skipw, c)) {
338 /* check if we hit the delim */
339 for (d = del; *d; d++) {
340 /* we can only exit if no parens or user tracks them */
341 if (c == *d && lc != '\\' && (p == 0 || par)) {
347 if (limit > 0 && (i >= limit || (size_t)(t-token) >= limit)) {
353 if (c == '\\' && lc == '\\') {
364 if (!par && p != 0) {
371 /* do not skip over quotes after the string, they are part
372 * of the next string. But skip over whitespace (if needed)*/
373 sldns_bskipcs(b, del+1);
374 else sldns_bskipcs(b, del);
377 if (!par && p != 0) {
385 sldns_bskipcs(sldns_buffer *buffer, const char *s)
391 while(sldns_buffer_available_at(buffer, buffer->_position, sizeof(char))) {
392 c = (char) sldns_buffer_read_u8_at(buffer, buffer->_position);
394 for (d = s; *d; d++) {
399 if (found && buffer->_limit > buffer->_position) {
400 buffer->_position += sizeof(char);
408 sldns_fskipcs(FILE *fp, const char *s)
410 sldns_fskipcs_l(fp, s, NULL);
414 sldns_fskipcs_l(FILE *fp, const char *s, int *line_nr)
420 while ((c = fgetc(fp)) != EOF) {
421 if (line_nr && c == '\n') {
422 *line_nr = *line_nr + 1;
425 for (d = s; *d; d++) {
431 /* with getc, we've read too far */
439 sldns_bget_keyword_data(sldns_buffer *b, const char *keyword, const char *k_del, char
440 *data, const char *d_del, size_t data_limit)
442 /* we assume: keyword|sep|data */
446 if(strlen(keyword) >= LDNS_MAX_KEYWORDLEN)
448 fkeyword = (char*)malloc(LDNS_MAX_KEYWORDLEN);
450 return -1; /* out of memory */
452 i = sldns_bget_token(b, fkeyword, k_del, data_limit);
455 return -1; /* nothing read */
459 if (strncmp(fkeyword, keyword, strlen(keyword)) == 0) {
461 /* whee, the match! */
462 /* retrieve it's data */
463 i = sldns_bget_token(b, data, d_del, 0);