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 /** 99 * returns a token/char from the buffer b. 100 * This function deals with ( and ) in the buffer, 101 * and ignores when it finds them. 102 * \param[in] *b the buffer to read from 103 * \param[out] *token the token is put here 104 * \param[in] *delim chars at which the parsing should stop 105 * \param[in] *limit how much to read. If 0 the builtin maximum is used 106 * \returns 0 on error of EOF of b. Otherwise return the length of what is read 107 */ 108 ssize_t ldns_bget_token(ldns_buffer *b, char *token, const char *delim, size_t limit); 109 110 /* 111 * searches for keyword and delim in a file. Gives everything back 112 * after the keyword + k_del until we hit d_del 113 * \param[in] f file pointer to read from 114 * \param[in] keyword keyword to look for 115 * \param[in] k_del keyword delimiter 116 * \param[out] data the data found 117 * \param[in] d_del the data delimiter 118 * \param[in] data_limit maximum size the the data buffer 119 * \return the number of character read 120 */ 121 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); 122 123 /* 124 * searches for keyword and delim. Gives everything back 125 * after the keyword + k_del until we hit d_del 126 * \param[in] f file pointer to read from 127 * \param[in] keyword keyword to look for 128 * \param[in] k_del keyword delimiter 129 * \param[out] data the data found 130 * \param[in] d_del the data delimiter 131 * \param[in] data_limit maximum size the the data buffer 132 * \param[in] line_nr pointer to an integer containing the current line number (for 133 debugging purposes) 134 * \return the number of character read 135 */ 136 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); 137 138 /* 139 * searches for keyword and delim in a buffer. Gives everything back 140 * after the keyword + k_del until we hit d_del 141 * \param[in] b buffer pointer to read from 142 * \param[in] keyword keyword to look for 143 * \param[in] k_del keyword delimiter 144 * \param[out] data the data found 145 * \param[in] d_del the data delimiter 146 * \param[in] data_limit maximum size the the data buffer 147 * \return the number of character read 148 */ 149 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); 150 151 /** 152 * returns the next character from a buffer. Advances the position pointer with 1. 153 * When end of buffer is reached returns EOF. This is the buffer's equivalent 154 * for getc(). 155 * \param[in] *buffer buffer to read from 156 * \return EOF on failure otherwise return the character 157 */ 158 int ldns_bgetc(ldns_buffer *buffer); 159 160 /** 161 * skips all of the characters in the given string in the buffer, moving 162 * the position to the first character that is not in *s. 163 * \param[in] *buffer buffer to use 164 * \param[in] *s characters to skip 165 * \return void 166 */ 167 void ldns_bskipcs(ldns_buffer *buffer, const char *s); 168 169 /** 170 * skips all of the characters in the given string in the fp, moving 171 * the position to the first character that is not in *s. 172 * \param[in] *fp file to use 173 * \param[in] *s characters to skip 174 * \return void 175 */ 176 void ldns_fskipcs(FILE *fp, const char *s); 177 178 179 /** 180 * skips all of the characters in the given string in the fp, moving 181 * the position to the first character that is not in *s. 182 * \param[in] *fp file to use 183 * \param[in] *s characters to skip 184 * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes) 185 * \return void 186 */ 187 void ldns_fskipcs_l(FILE *fp, const char *s, int *line_nr); 188 189 #ifdef __cplusplus 190 } 191 #endif 192 193 #endif /* LDNS_PARSE_H */ 194