1266077Sdes/*
2266077Sdes * parse.h
3266077Sdes *
4266077Sdes * a Net::DNS like library for C
5266077Sdes * LibDNS Team @ NLnet Labs
6266077Sdes * (c) NLnet Labs, 2005-2006
7266077Sdes * See the file LICENSE for the license
8266077Sdes */
9266077Sdes
10266077Sdes#ifndef LDNS_PARSE_H
11266077Sdes#define LDNS_PARSE_H
12266077Sdes
13266077Sdesstruct sldns_buffer;
14266077Sdes
15266077Sdes#ifdef __cplusplus
16266077Sdesextern "C" {
17266077Sdes#endif
18266077Sdes
19266077Sdes#define LDNS_PARSE_SKIP_SPACE		"\f\n\r\v"
20266077Sdes#define LDNS_PARSE_NORMAL		" \f\n\r\t\v"
21266077Sdes#define LDNS_PARSE_NO_NL		" \t"
22266077Sdes#define LDNS_MAX_LINELEN		10230
23266077Sdes#define LDNS_MAX_KEYWORDLEN		32
24266077Sdes
25266077Sdes
26266077Sdes/**
27266077Sdes * \file
28266077Sdes *
29266077Sdes * Contains some low-level parsing functions, mostly used in the _frm_str
30266077Sdes * family of functions.
31266077Sdes */
32266077Sdes
33266077Sdes/**
34266077Sdes * different type of directives in zone files
35266077Sdes * We now deal with $TTL, $ORIGIN and $INCLUDE.
36266077Sdes * The latter is not implemented in ldns (yet)
37266077Sdes */
38266077Sdesenum sldns_enum_directive
39266077Sdes{
40266077Sdes	LDNS_DIR_TTL,
41266077Sdes	LDNS_DIR_ORIGIN,
42266077Sdes	LDNS_DIR_INCLUDE
43266077Sdes};
44266077Sdestypedef enum sldns_enum_directive sldns_directive;
45266077Sdes
46266077Sdes/**
47266077Sdes * returns a token/char from the stream F.
48266077Sdes * This function deals with ( and ) in the stream,
49266077Sdes * and ignores them when encountered
50266077Sdes * \param[in] *f the file to read from
51266077Sdes * \param[out] *token the read token is put here
52266077Sdes * \param[in] *delim chars at which the parsing should stop
53266077Sdes * \param[in] *limit how much to read. If 0 the builtin maximum is used
54266077Sdes * \return 0 on error of EOF of the stream F.  Otherwise return the length of what is read
55266077Sdes */
56266077Sdesssize_t sldns_fget_token(FILE *f, char *token, const char *delim, size_t limit);
57266077Sdes
58266077Sdes/**
59266077Sdes * returns a token/char from the stream F.
60266077Sdes * This function deals with ( and ) in the stream,
61266077Sdes * and ignores when it finds them.
62266077Sdes * \param[in] *f the file to read from
63266077Sdes * \param[out] *token the token is put here
64266077Sdes * \param[in] *delim chars at which the parsing should stop
65266077Sdes * \param[in] *limit how much to read. If 0 use builtin maximum
66266077Sdes * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
67266077Sdes * \return 0 on error of EOF of F otherwise return the length of what is read
68266077Sdes */
69266077Sdesssize_t sldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr);
70266077Sdes
71266077Sdes/**
72266077Sdes * returns a token/char from the buffer b.
73266077Sdes * This function deals with ( and ) in the buffer,
74266077Sdes * and ignores when it finds them.
75266077Sdes * \param[in] *b the buffer to read from
76266077Sdes * \param[out] *token the token is put here
77266077Sdes * \param[in] *delim chars at which the parsing should stop
78266077Sdes * \param[in] *limit how much to read. If 0 the builtin maximum is used
79266077Sdes * \param[in] *par if you pass nonNULL, set to 0 on first call, the parenthesis
80266077Sdes * state is stored in it, for use on next call.  User must check it is back
81266077Sdes * to zero after last bget in string (for parse error).  If you pass NULL,
82266077Sdes * the entire parenthesized string is read in.
83266077Sdes * \param[in] skipw string with whitespace to skip before the start of the
84266077Sdes * token, like " ", or " \t", or NULL for none.
85266077Sdes * \returns 0 on error of EOF of b. Otherwise return the length of what is read
86266077Sdes */
87266077Sdesssize_t sldns_bget_token_par(struct sldns_buffer *b, char *token, const char *delim, size_t limit, int* par, const char* skipw);
88266077Sdes
89266077Sdes/**
90266077Sdes * returns a token/char from the buffer b.
91266077Sdes * This function deals with ( and ) in the buffer,
92266077Sdes * and ignores when it finds them.
93266077Sdes * \param[in] *b the buffer to read from
94266077Sdes * \param[out] *token the token is put here
95266077Sdes * \param[in] *delim chars at which the parsing should stop
96266077Sdes * \param[in] *limit how much to read. If 0 the builtin maximum is used
97266077Sdes * \returns 0 on error of EOF of b. Otherwise return the length of what is read
98266077Sdes */
99266077Sdesssize_t sldns_bget_token(struct sldns_buffer *b, char *token, const char *delim, size_t limit);
100266077Sdes
101266077Sdes/*
102266077Sdes * searches for keyword and delim in a file. Gives everything back
103266077Sdes * after the keyword + k_del until we hit d_del
104266077Sdes * \param[in] f file pointer to read from
105266077Sdes * \param[in] keyword keyword to look for
106266077Sdes * \param[in] k_del keyword delimeter
107266077Sdes * \param[out] data the data found
108266077Sdes * \param[in] d_del the data delimeter
109266077Sdes * \param[in] data_limit maximum size the the data buffer
110266077Sdes * \return the number of character read
111266077Sdes */
112266077Sdesssize_t sldns_fget_keyword_data(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit);
113266077Sdes
114266077Sdes/*
115266077Sdes * searches for keyword and delim. Gives everything back
116266077Sdes * after the keyword + k_del until we hit d_del
117266077Sdes * \param[in] f file pointer to read from
118266077Sdes * \param[in] keyword keyword to look for
119266077Sdes * \param[in] k_del keyword delimeter
120266077Sdes * \param[out] data the data found
121266077Sdes * \param[in] d_del the data delimeter
122266077Sdes * \param[in] data_limit maximum size the the data buffer
123266077Sdes * \param[in] line_nr pointer to an integer containing the current line number (for
124266077Sdesdebugging purposes)
125266077Sdes * \return the number of character read
126266077Sdes */
127266077Sdesssize_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);
128266077Sdes
129266077Sdes/*
130266077Sdes * searches for keyword and delim in a buffer. Gives everything back
131266077Sdes * after the keyword + k_del until we hit d_del
132266077Sdes * \param[in] b buffer pointer to read from
133266077Sdes * \param[in] keyword keyword to look for
134266077Sdes * \param[in] k_del keyword delimeter
135266077Sdes * \param[out] data the data found
136266077Sdes * \param[in] d_del the data delimeter
137266077Sdes * \param[in] data_limit maximum size the the data buffer
138266077Sdes * \return the number of character read
139266077Sdes */
140266077Sdesssize_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);
141266077Sdes
142266077Sdes/**
143266077Sdes * returns the next character from a buffer. Advances the position pointer with 1.
144266077Sdes * When end of buffer is reached returns EOF. This is the buffer's equivalent
145266077Sdes * for getc().
146266077Sdes * \param[in] *buffer buffer to read from
147266077Sdes * \return EOF on failure otherwise return the character
148266077Sdes */
149266077Sdesint sldns_bgetc(struct sldns_buffer *buffer);
150266077Sdes
151266077Sdes/**
152266077Sdes * skips all of the characters in the given string in the buffer, moving
153266077Sdes * the position to the first character that is not in *s.
154266077Sdes * \param[in] *buffer buffer to use
155266077Sdes * \param[in] *s characters to skip
156266077Sdes * \return void
157266077Sdes */
158266077Sdesvoid sldns_bskipcs(struct sldns_buffer *buffer, const char *s);
159266077Sdes
160266077Sdes/**
161266077Sdes * skips all of the characters in the given string in the fp, moving
162266077Sdes * the position to the first character that is not in *s.
163266077Sdes * \param[in] *fp file to use
164266077Sdes * \param[in] *s characters to skip
165266077Sdes * \return void
166266077Sdes */
167266077Sdesvoid sldns_fskipcs(FILE *fp, const char *s);
168266077Sdes
169266077Sdes
170266077Sdes/**
171266077Sdes * skips all of the characters in the given string in the fp, moving
172266077Sdes * the position to the first character that is not in *s.
173266077Sdes * \param[in] *fp file to use
174266077Sdes * \param[in] *s characters to skip
175266077Sdes * \param[in] line_nr pointer to an integer containing the current line number (for debugging purposes)
176266077Sdes * \return void
177266077Sdes */
178266077Sdesvoid sldns_fskipcs_l(FILE *fp, const char *s, int *line_nr);
179266077Sdes
180266077Sdes#ifdef __cplusplus
181266077Sdes}
182266077Sdes#endif
183266077Sdes
184266077Sdes#endif /* LDNS_PARSE_H */
185