1 /* 2 * parse.h 3 * 4 * a Net::DNS like library for C 5 * LibDNS Team @ NLnet Labs 6 * (c) NLnet Labs, 2005-2006 7 * See the file LICENSE for the license 8 */ 9 10 #ifndef LDNS_PARSE_H 11 #define LDNS_PARSE_H 12 13 #include <ldns/common.h> 14 #include <ldns/buffer.h> 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 #define LDNS_PARSE_SKIP_SPACE "\f\n\r\v" 21 #define LDNS_PARSE_NORMAL " \f\n\r\t\v" 22 #define LDNS_PARSE_NO_NL " \t" 23 #define LDNS_MAX_LINELEN 10230 24 #define LDNS_MAX_KEYWORDLEN 32 25 26 27 /** 28 * \file 29 * 30 * Contains some low-level parsing functions, mostly used in the _frm_str 31 * family of functions. 32 */ 33 34 /** 35 * different type of directives in zone files 36 * We now deal with $TTL, $ORIGIN and $INCLUDE. 37 * The latter is not implemented in ldns (yet) 38 */ 39 enum ldns_enum_directive 40 { 41 LDNS_DIR_TTL, 42 LDNS_DIR_ORIGIN, 43 LDNS_DIR_INCLUDE 44 }; 45 typedef enum ldns_enum_directive ldns_directive; 46 47 /** 48 * returns a token/char from the stream F. 49 * This function deals with ( and ) in the stream, 50 * and ignores them when encountered 51 * \param[in] *f the file to read from 52 * \param[out] *token the read token is put here 53 * \param[in] *delim chars at which the parsing should stop 54 * \param[in] *limit how much to read. If 0 the builtin maximum is used 55 * \return 0 on error of EOF of the stream F. Otherwise return the length of what is read 56 */ 57 ssize_t ldns_fget_token(FILE *f, char *token, const char *delim, size_t limit); 58 59 /** 60 * returns a token/char from the stream F. 61 * This function deals with ( and ) in the stream, 62 * and ignores when it finds them. 63 * \param[in] *f the file to read from 64 * \param[out] *token the token is put here 65 * \param[in] *delim chars at which the parsing should stop 66 * \param[in] *limit how much to read. If 0 use builtin maximum 67 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes) 68 * \return 0 on error of EOF of F otherwise return the length of what is read 69 */ 70 ssize_t ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr); 71 72 /** 73 * returns a token/char from the stream f. 74 * This function deals with ( and ) in the stream, 75 * and ignores when it finds them. 76 * \param[in] *f the file to read from 77 * \param[out] **token this should be a reference to a string buffer in which 78 * the token is put. A new buffer will be allocated when 79 * *token is NULL and fixed is false. If the buffer is too 80 * small to hold the token, the buffer is reallocated with 81 * double the size (of limit). 82 * If fixed is true, the string buffer may not be NULL 83 * and limit must be set to the buffer size. In that case 84 * no reallocations will be done. 85 * \param[in,out] *limit reference to the size of the token buffer. Will be 86 * reset to the new limit of the token buffer if the 87 * buffer is reallocated. 88 * \param [in] fixed If fixed is false, the token buffer is allowed to grow 89 * when needed (by way of reallocation). If true, the token 90 * buffer will not be resized. 91 * \param[in] *delim chars at which the parsing should stop 92 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes) 93 * \return LDNS_STATUS_OK on success, LDNS_STATUS_SYNTAX_EMPTY when no token 94 * was read and an error otherwise. 95 */ 96 ldns_status ldns_fget_token_l_st(FILE *f, char **token, size_t *limit, bool fixed, const char *delim, int *line_nr); 97 98 ssize_t ldns_fget_token_l_resolv_conf(FILE *f, char *token, const char *delim, size_t limit, int *line_nr); 99 100 /** 101 * returns a token/char from the buffer b. 102 * This function deals with ( and ) in the buffer, 103 * and ignores when it finds them. 104 * \param[in] *b the buffer to read from 105 * \param[out] *token the token is put here 106 * \param[in] *delim chars at which the parsing should stop 107 * \param[in] *limit how much to read. If 0 the builtin maximum is used 108 * \returns 0 on error of EOF of b. Otherwise return the length of what is read 109 */ 110 ssize_t ldns_bget_token(ldns_buffer *b, char *token, const char *delim, size_t limit); 111 112 /* 113 * searches for keyword and delim in a file. Gives everything back 114 * after the keyword + k_del until we hit d_del 115 * \param[in] f file pointer to read from 116 * \param[in] keyword keyword to look for 117 * \param[in] k_del keyword delimiter 118 * \param[out] data the data found 119 * \param[in] d_del the data delimiter 120 * \param[in] data_limit maximum size the the data buffer 121 * \return the number of character read 122 */ 123 ssize_t ldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit); 124 125 /* 126 * searches for keyword and delim. Gives everything back 127 * after the keyword + k_del until we hit d_del 128 * \param[in] f file pointer to read from 129 * \param[in] keyword keyword to look for 130 * \param[in] k_del keyword delimiter 131 * \param[out] data the data found 132 * \param[in] d_del the data delimiter 133 * \param[in] data_limit maximum size the the data buffer 134 * \param[in] line_nr pointer to an integer containing the current line number (for 135 debugging purposes) 136 * \return the number of character read 137 */ 138 ssize_t ldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit, int *line_nr); 139 140 /* 141 * searches for keyword and delim in a buffer. Gives everything back 142 * after the keyword + k_del until we hit d_del 143 * \param[in] b buffer pointer to read from 144 * \param[in] keyword keyword to look for 145 * \param[in] k_del keyword delimiter 146 * \param[out] data the data found 147 * \param[in] d_del the data delimiter 148 * \param[in] data_limit maximum size the the data buffer 149 * \return the number of character read 150 */ 151 ssize_t ldns_bget_keyword_data(ldns_buffer *b, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit); 152 153 /** 154 * returns the next character from a buffer. Advances the position pointer with 1. 155 * When end of buffer is reached returns EOF. This is the buffer's equivalent 156 * for getc(). 157 * \param[in] *buffer buffer to read from 158 * \return EOF on failure otherwise return the character 159 */ 160 int ldns_bgetc(ldns_buffer *buffer); 161 162 /** 163 * skips all of the characters in the given string in the buffer, moving 164 * the position to the first character that is not in *s. 165 * \param[in] *buffer buffer to use 166 * \param[in] *s characters to skip 167 * \return void 168 */ 169 void ldns_bskipcs(ldns_buffer *buffer, const char *s); 170 171 /** 172 * skips all of the characters in the given string in the fp, moving 173 * the position to the first character that is not in *s. 174 * \param[in] *fp file to use 175 * \param[in] *s characters to skip 176 * \return void 177 */ 178 void ldns_fskipcs(FILE *fp, const char *s); 179 180 181 /** 182 * skips all of the characters in the given string in the fp, moving 183 * the position to the first character that is not in *s. 184 * \param[in] *fp file to use 185 * \param[in] *s characters to skip 186 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes) 187 * \return void 188 */ 189 void ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr); 190 191 #ifdef __cplusplus 192 } 193 #endif 194 195 #endif /* LDNS_PARSE_H */ 196