xref: /freebsd/contrib/unbound/sldns/parse.h (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
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 struct sldns_buffer;
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #define LDNS_PARSE_SKIP_SPACE		"\f\n\r\v"
20 #define LDNS_PARSE_NORMAL		" \f\n\r\t\v"
21 #define LDNS_PARSE_NO_NL		" \t"
22 #define LDNS_MAX_LINELEN		10230
23 #define LDNS_MAX_KEYWORDLEN		32
24 
25 
26 /**
27  * \file
28  *
29  * Contains some low-level parsing functions, mostly used in the _frm_str
30  * family of functions.
31  */
32 
33 /**
34  * different type of directives in zone files
35  * We now deal with $TTL, $ORIGIN and $INCLUDE.
36  * The latter is not implemented in ldns (yet)
37  */
38 enum sldns_enum_directive
39 {
40 	LDNS_DIR_TTL,
41 	LDNS_DIR_ORIGIN,
42 	LDNS_DIR_INCLUDE
43 };
44 typedef enum sldns_enum_directive sldns_directive;
45 
46 /**
47  * returns a token/char from the stream F.
48  * This function deals with ( and ) in the stream,
49  * and ignores them when encountered
50  * \param[in] *f the file to read from
51  * \param[out] *token the read token is put here
52  * \param[in] *delim chars at which the parsing should stop
53  * \param[in] *limit how much to read. If 0 the builtin maximum is used
54  * \return 0 on error of EOF of the stream F.  Otherwise return the length of what is read
55  */
56 ssize_t sldns_fget_token(FILE *f, char *token, const char *delim, size_t limit);
57 
58 /**
59  * returns a token/char from the stream F.
60  * This function deals with ( and ) in the stream,
61  * and ignores when it finds them.
62  * \param[in] *f the file to read from
63  * \param[out] *token the token is put here
64  * \param[in] *delim chars at which the parsing should stop
65  * \param[in] *limit how much to read. If 0 use builtin maximum
66  * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
67  * \return 0 on error of EOF of F otherwise return the length of what is read
68  */
69 ssize_t sldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr);
70 
71 /**
72  * returns a token/char from the buffer b.
73  * This function deals with ( and ) in the buffer,
74  * and ignores when it finds them.
75  * \param[in] *b the buffer to read from
76  * \param[out] *token the token is put here
77  * \param[in] *delim chars at which the parsing should stop
78  * \param[in] *limit how much to read. If 0 the builtin maximum is used
79  * \param[in] *par if you pass nonNULL, set to 0 on first call, the parenthesis
80  * state is stored in it, for use on next call.  User must check it is back
81  * to zero after last bget in string (for parse error).  If you pass NULL,
82  * the entire parenthesized string is read in.
83  * \param[in] skipw string with whitespace to skip before the start of the
84  * token, like " ", or " \t", or NULL for none.
85  * \returns 0 on error of EOF of b. Otherwise return the length of what is read
86  */
87 ssize_t sldns_bget_token_par(struct sldns_buffer *b, char *token, const char *delim, size_t limit, int* par, const char* skipw);
88 
89 /**
90  * returns a token/char from the buffer b.
91  * This function deals with ( and ) in the buffer,
92  * and ignores when it finds them.
93  * \param[in] *b the buffer to read from
94  * \param[out] *token the token is put here
95  * \param[in] *delim chars at which the parsing should stop
96  * \param[in] *limit how much to read. If 0 the builtin maximum is used
97  * \returns 0 on error of EOF of b. Otherwise return the length of what is read
98  */
99 ssize_t sldns_bget_token(struct sldns_buffer *b, char *token, const char *delim, size_t limit);
100 
101 /*
102  * searches for keyword and delim in a file. Gives everything back
103  * after the keyword + k_del until we hit d_del
104  * \param[in] f file pointer to read from
105  * \param[in] keyword keyword to look for
106  * \param[in] k_del keyword delimiter
107  * \param[out] data the data found
108  * \param[in] d_del the data delimiter
109  * \param[in] data_limit maximum size the the data buffer
110  * \return the number of character read
111  */
112 ssize_t sldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit);
113 
114 /*
115  * searches for keyword and delim. Gives everything back
116  * after the keyword + k_del until we hit d_del
117  * \param[in] f file pointer to read from
118  * \param[in] keyword keyword to look for
119  * \param[in] k_del keyword delimiter
120  * \param[out] data the data found
121  * \param[in] d_del the data delimiter
122  * \param[in] data_limit maximum size the the data buffer
123  * \param[in] line_nr pointer to an integer containing the current line number (for
124 debugging purposes)
125  * \return the number of character read
126  */
127 ssize_t sldns_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);
128 
129 /*
130  * searches for keyword and delim in a buffer. Gives everything back
131  * after the keyword + k_del until we hit d_del
132  * \param[in] b buffer pointer to read from
133  * \param[in] keyword keyword to look for
134  * \param[in] k_del keyword delimiter
135  * \param[out] data the data found
136  * \param[in] d_del the data delimiter
137  * \param[in] data_limit maximum size the the data buffer
138  * \return the number of character read
139  */
140 ssize_t sldns_bget_keyword_data(struct sldns_buffer *b, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit);
141 
142 /**
143  * returns the next character from a buffer. Advances the position pointer with 1.
144  * When end of buffer is reached returns EOF. This is the buffer's equivalent
145  * for getc().
146  * \param[in] *buffer buffer to read from
147  * \return EOF on failure otherwise return the character
148  */
149 int sldns_bgetc(struct sldns_buffer *buffer);
150 
151 /**
152  * skips all of the characters in the given string in the buffer, moving
153  * the position to the first character that is not in *s.
154  * \param[in] *buffer buffer to use
155  * \param[in] *s characters to skip
156  * \return void
157  */
158 void sldns_bskipcs(struct sldns_buffer *buffer, const char *s);
159 
160 /**
161  * skips all of the characters in the given string in the fp, moving
162  * the position to the first character that is not in *s.
163  * \param[in] *fp file to use
164  * \param[in] *s characters to skip
165  * \return void
166  */
167 void sldns_fskipcs(FILE *fp, const char *s);
168 
169 
170 /**
171  * skips all of the characters in the given string in the fp, moving
172  * the position to the first character that is not in *s.
173  * \param[in] *fp file to use
174  * \param[in] *s characters to skip
175  * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
176  * \return void
177  */
178 void sldns_fskipcs_l(FILE *fp, const char *s, int *line_nr);
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 
184 #endif /* LDNS_PARSE_H */
185