From: Miek Gieben Date: Wed, 13 Apr 2005 10:37:14 +0000 (+0000) Subject: added ldns_fskipcs for FILE* X-Git-Tag: release-0.50~135 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c1650d8a113fe960df67a606eb0b3dc8272bbe9;p=thirdparty%2Fldns.git added ldns_fskipcs for FILE* used that in resolv.conf reader, it now parses open's /etc/resolv.conf some tweaks default dprintf to print (for debugging) --- diff --git a/buffer.c b/buffer.c index 2467d072..2f8e8dc2 100644 --- a/buffer.c +++ b/buffer.c @@ -189,49 +189,3 @@ ldns_bgetc(ldns_buffer *buffer) } return (int)ldns_buffer_read_u8(buffer); } - -/* fast forwards the buffer, skipping the given char, setting the - * buffer position to the location of the first different char - * (or to the end of the buffer) - */ -void -ldns_bskipc(ldns_buffer *buffer, char c) -{ - while (c == (char) ldns_buffer_read_u8_at(buffer, ldns_buffer_position(buffer))) { - if (ldns_buffer_available_at(buffer, buffer->_position + sizeof(char), sizeof(char))) { - buffer->_position += sizeof(char); - } else { - return; - } - } -} - -/* fast forwards the buffer, skipping all chars in the given string, - * setting the buffer position to the first char that is not contained - * in the string (or to the end of the buffer) - */ -void -ldns_bskipcs(ldns_buffer *buffer, const char *s) -{ - bool found; - char c; - char *d; - - while(ldns_buffer_available_at(buffer, buffer->_position, sizeof(char))) { - c = (char) ldns_buffer_read_u8_at(buffer, - buffer->_position); - found = false; - for (d = (char *) s; *d; d++) { - if (*d == c) { - found = true; - } - } - if (found && buffer->_limit > buffer->_position) { - buffer->_position += sizeof(char); - } else { - return; - } - } -} - - diff --git a/ldns/buffer.h b/ldns/buffer.h index d1937e1a..38f34726 100644 --- a/ldns/buffer.h +++ b/ldns/buffer.h @@ -420,17 +420,4 @@ void ldns_buffer_free(ldns_buffer *buffer); */ void *ldns_buffer_export(ldns_buffer *buffer); -/* - * Get the next character from a buffer. Advance the position - * pointer with 1. - * When end of buffer is reached return EOF - */ -int ldns_bgetc(ldns_buffer *buffer); - -/** - * Skip all of the characters in the given string in the buffer, moving - * the position to the first character that is not in *s - */ -void ldns_bskipcs(ldns_buffer *buffer, const char *s); - #endif /* _BUFFER_H_ */ diff --git a/ldns/parse.h b/ldns/parse.h index 86f628ef..b4c43912 100644 --- a/ldns/parse.h +++ b/ldns/parse.h @@ -14,7 +14,7 @@ #include -#define LDNS_PARSE_SKIP_SPACE "\f\n\r\t\v" +#define LDNS_PARSE_SKIP_SPACE "\f\n\r\v" #define LDNS_PARSE_NORMAL " \f\n\r\t\v" #define MAXLINE_LEN 512 #define MAXKEYWORD_LEN 32 @@ -61,4 +61,28 @@ ldns_bget_keyword_data(ldns_buffer *b, const char *keyword, const char *k_del, c */ char * ldns_str_remove_comment(char *str); -#endif /* _PARSE_H_ */ +/* + * Get the next character from a buffer. Advance the position + * pointer with 1. + * When end of buffer is reached return EOF + */ +int ldns_bgetc(ldns_buffer *buffer); + +/** + * Skip all of the characters in the given string in the buffer, moving + * the position to the first character that is not in *s + */ +void ldns_bskipcs(ldns_buffer *buffer, const char *s); + +/** + * Skip all of the characters in the given string in the fp, moving + * the position to the first character that is not in *s + */ +void ldns_fskipcs(FILE *fp, const char *s); + + + + + + +#endif /* _PARSE_H */ diff --git a/parse.c b/parse.c index 12ac85ea..dda77f9a 100644 --- a/parse.c +++ b/parse.c @@ -48,6 +48,7 @@ ldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *da } /* walk along the file until you get a hit */ +/* number of occurences.... !! */ ssize_t ldns_fget_all_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del) @@ -101,7 +102,7 @@ ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit) if (p < 0) { /* more ) then ( - close off the string */ *t = '\0'; - return -1; + return 0; } if (c == '\n' && p != 0) { @@ -124,13 +125,17 @@ ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit) } } *t = '\0'; + if (i == 0) { + /* nothing read */ + return -1; + } if (p != 0) { return -1; } - return 0; + return (ssize_t)i; tokenread: - /* skip something here too; ldns_fskipc(f, del) */ + ldns_fskipcs(f, del); *t = '\0'; if (p != 0) { return -1; @@ -240,7 +245,6 @@ ldns_bget_token(ldns_buffer *b, char *token, const char *delim, size_t limit) return -1; } } - *t = '\0'; if (i == 0) { /* nothing read */ @@ -285,3 +289,77 @@ ldns_str_remove_comment(char *str) } return str2; } + +/* fast forwards the buffer, skipping the given char, setting the + * buffer position to the location of the first different char + * (or to the end of the buffer) + */ +void +ldns_bskipc(ldns_buffer *buffer, char c) +{ + while (c == (char) ldns_buffer_read_u8_at(buffer, ldns_buffer_position(buffer))) { + if (ldns_buffer_available_at(buffer, buffer->_position + sizeof(char), sizeof(char))) { + buffer->_position += sizeof(char); + } else { + return; + } + } +} + +/* fast forwards the buffer, skipping all chars in the given string, + * setting the buffer position to the first char that is not contained + * in the string (or to the end of the buffer) + */ +void +ldns_bskipcs(ldns_buffer *buffer, const char *s) +{ + bool found; + char c; + char *d; + + while(ldns_buffer_available_at(buffer, buffer->_position, sizeof(char))) { + c = (char) ldns_buffer_read_u8_at(buffer, + buffer->_position); + found = false; + for (d = (char *) s; *d; d++) { + if (*d == c) { + found = true; + } + } + if (found && buffer->_limit > buffer->_position) { + buffer->_position += sizeof(char); + } else { + return; + } + } +} + +void +ldns_fskipc(FILE *fp, char c) +{ + + +} + + +void +ldns_fskipcs(FILE *fp, const char *s) +{ + bool found; + char c; + char *d; + + while ((c = fgetc(fp)) != EOF) { + found = false; + for (d = (char *) s; *d; d++) { + if (*d == c) { + found = true; + } + } + if (!found) { + /* with getc, we've read too far */ + ungetc(c, fp); + return; + } + } +} diff --git a/resolver.c b/resolver.c index 1fb4b8cd..e3d1f80d 100644 --- a/resolver.c +++ b/resolver.c @@ -488,13 +488,12 @@ ldns_resolver_new_frm_fp(FILE *fp) keyword[0] = "domain"; keyword[1] = "nameserver"; word = XMALLOC(char, MAXLINE_LEN); - expect = 0; + expect = RESOLV_KEYWORD; r = ldns_resolver_new(); if (!r) { return NULL; } - gtr = ldns_fget_token(fp, word, LDNS_PARSE_NORMAL, 0); while (gtr > 0) { /* do something */ @@ -512,7 +511,7 @@ ldns_resolver_new_frm_fp(FILE *fp) } /* no keyword recognized */ if (expect == 0) { - /* dprintf("[%s] unreg keyword\n", word); */ + dprintf("[%s] unreg keyword\n", word); } break; case RESOLV_DEFDOMAIN: diff --git a/run-test18.c b/run-test18.c index 2a8ce693..3fe05c43 100644 --- a/run-test18.c +++ b/run-test18.c @@ -51,12 +51,13 @@ main() 604800 \n\ 3600 \n\ )"; - soa_string2 = "miek.nl. 3600 IN SOA elektron.atoom.net. miekg.atoom.net. \ + soa_string2 = "miek.nl. 3600 IN SOA elektron.atoom.net. miekg.atoom.net. (\ 2002120700 ; serial \n\ 21600 ; more shit\n\ 7200 ; ahh retry \n\ 604800 ; meaningless number to annoy me\n\ -3600 ; negative caching"; +3600 ; negative caching\n\ +)"; rr = ldns_rr_new_frm_str(soa_string1); ldns_rr_print(stdout, rr); diff --git a/run-test19.c b/run-test19.c index b90019b3..50367003 100644 --- a/run-test19.c +++ b/run-test19.c @@ -17,32 +17,13 @@ usage(FILE *fp, char *prog) { int main() { - FILE *f; - char *tok; - size_t b; + ldns_resolver *r; - if (!(f = fopen("blaat", "r"))) { - exit(1); + r = ldns_resolver_new_frm_file(NULL); + if (!r) { + printf("something wrong?\n"); } - tok = XMALLOC(char, 1024); - - while ((b = ldns_get_token(f, tok, LDNS_PARSE_SKIP_SPACE)) != 0) { - fprintf(stdout, "%d: %s\n", (int)b, tok); - } - fclose(f); - - if (!(f = fopen("Kdnssec.nl.+005+32820.private", "r"))) { - exit(1); - } - - /* ldns_get_keyword_data(f, "Algorithm", ": \t", tok, LDNS_STR);*/ - if (ldns_get_keyword_data(f, "Private-key-format", - ":", tok, LDNS_PARSE_SKIP_SPACE) != -1) { - printf("found it, found it\n"); - printf("%s\n", tok); - } - - fclose(f); + ldns_resolver_print(stdout, r); return 0; } diff --git a/util.h b/util.h index 703d5eb0..32132b72 100644 --- a/util.h +++ b/util.h @@ -15,8 +15,8 @@ #include -/* #define dprintf(X,Y) printf((X), (Y)) */ -#define dprintf(X, Y) +#define dprintf(X,Y) printf((X), (Y)) +/* #define dprintf(X, Y) */ /** * splint static inline workaround