2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #ifndef included_format_h
39 #define included_format_h
43 #include <vppinfra/clib.h> /* for CLIB_UNIX, etc. */
44 #include <vppinfra/vec.h>
45 #include <vppinfra/error.h> /* for ASSERT */
46 #include <vppinfra/string.h>
48 typedef u8 * (format_function_t) (u8 * s, va_list * args);
50 u8 * va_format (u8 * s, char * format, va_list * args);
51 u8 * format (u8 * s, char * format, ...);
57 #else /* ! CLIB_UNIX */
59 /* We're not Unix and have not stdio.h */
61 #define stdin ((FILE *) 0)
62 #define stdout ((FILE *) 1)
63 #define stderr ((FILE *) 2)
67 word va_fformat (FILE * f, char * fmt, va_list * va);
68 word fformat (FILE * f, char * fmt, ...);
69 word fdformat (int fd, char * fmt, ...);
72 format_get_indent (u8 * s)
90 #define _(f) u8 * f (u8 * s, va_list * va)
92 /* Standard user-defined formats. */
95 _ (format_ascii_bytes);
97 _ (format_white_space);
99 _ (format_time_interval);
102 /* Unix specific formats. */
103 _ (format_address_family);
104 _ (format_unix_arphrd);
105 _ (format_unix_interface_flags);
106 _ (format_network_address);
107 _ (format_network_protocol);
108 _ (format_network_port);
110 _ (format_ip4_tos_byte);
111 _ (format_ip4_packet);
112 _ (format_icmp4_type_and_code);
113 _ (format_ethernet_packet);
116 _ (format_time_float);
118 _ (format_ucontext_pc);
125 typedef struct _unformat_input_t {
126 /* Input buffer (vector). */
129 /* Current index in input buffer. */
132 /* Vector of buffer marks. Used to delineate pieces of the buffer
133 for error reporting and for parse recovery. */
134 uword * buffer_marks;
136 /* User's function to fill the buffer when its empty
138 uword (* fill_buffer) (struct _unformat_input_t * i);
140 /* Return values for fill buffer function which indicate whether not
141 input has been exhausted. */
142 #define UNFORMAT_END_OF_INPUT (~0)
143 #define UNFORMAT_MORE_INPUT 0
145 /* User controlled argument to fill buffer function. */
146 void * fill_buffer_arg;
150 unformat_init (unformat_input_t * i,
151 uword (* fill_buffer) (unformat_input_t *),
152 void * fill_buffer_arg)
154 memset (i, 0, sizeof (i[0]));
155 i->fill_buffer = fill_buffer;
156 i->fill_buffer_arg = fill_buffer_arg;
160 unformat_free (unformat_input_t * i)
162 vec_free (i->buffer);
163 vec_free (i->buffer_marks);
164 memset (i, 0, sizeof (i[0]));
168 unformat_check_input (unformat_input_t * i)
170 /* Low level fill input function. */
171 extern uword _unformat_fill_input (unformat_input_t * i);
173 if (i->index >= vec_len (i->buffer)
174 && i->index != UNFORMAT_END_OF_INPUT)
175 _unformat_fill_input (i);
180 /* Return true if input is exhausted */
182 unformat_is_eof (unformat_input_t * input)
184 return unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
187 /* Return next element in input vector,
188 possibly calling fill input to get more. */
190 unformat_get_input (unformat_input_t * input)
192 uword i = unformat_check_input (input);
193 if (i < vec_len (input->buffer))
195 input->index = i + 1;
196 i = input->buffer[i];
201 /* Back up input pointer by one. */
203 unformat_put_input (unformat_input_t * input)
204 { input->index -= 1; }
206 /* Peek current input character without advancing. */
208 unformat_peek_input (unformat_input_t * input)
210 uword c = unformat_get_input (input);
211 if (c != UNFORMAT_END_OF_INPUT)
212 unformat_put_input (input);
216 /* Skip current input line. */
217 always_inline void unformat_skip_line (unformat_input_t * i)
221 while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT
226 uword unformat_skip_white_space (unformat_input_t * input);
228 /* Unformat function. */
229 typedef uword (unformat_function_t) (unformat_input_t * input, va_list * args);
231 /* External functions. */
233 /* General unformatting function with programmable input stream. */
234 uword unformat (unformat_input_t * i, char * fmt, ...);
236 /* Call user defined parse function.
237 unformat_user (i, f, ...) is equivalent to unformat (i, "%U", f, ...) */
238 uword unformat_user (unformat_input_t * input, unformat_function_t * func, ...);
240 /* Alternate version which allows for extensions. */
241 uword va_unformat (unformat_input_t * i, char * fmt, va_list * args);
243 /* Setup for unformat of Unix style command line. */
244 void unformat_init_command_line (unformat_input_t * input,
247 /* Setup for unformat of given string. */
248 void unformat_init_string (unformat_input_t * input,
253 unformat_init_cstring (unformat_input_t * input,
255 { unformat_init_string (input, string, strlen (string)); }
257 /* Setup for unformat of given vector string; vector will be freed by unformat_string. */
258 void unformat_init_vector (unformat_input_t * input,
261 /* Format function for unformat input usable when an unformat error
263 u8 * format_unformat_error (u8 * s, va_list * va);
265 #define unformat_parse_error(input) \
266 clib_error_return (0, "parse error `%U'", format_unformat_error, input)
268 /* Print all input: not just error context. */
269 u8 * format_unformat_input (u8 * s, va_list * va);
271 /* Unformat (parse) function which reads a %s string and converts it
272 to and unformat_input_t. */
273 unformat_function_t unformat_input;
275 /* Parse a line ending with \n and return it. */
276 unformat_function_t unformat_line;
278 /* Parse a line ending with \n and return it as an unformat_input_t. */
279 unformat_function_t unformat_line_input;
281 /* Parse a token containing given set of characters. */
282 unformat_function_t unformat_token;
284 /* Parses a hexstring into a vector of bytes. */
285 unformat_function_t unformat_hex_string;
287 /* Returns non-zero match if input is exhausted.
288 Useful to ensure that the entire input matches with no trailing junk. */
289 unformat_function_t unformat_eof;
291 /* Parse memory size e.g. 100, 100k, 100m, 100g. */
292 unformat_function_t unformat_memory_size;
294 /* Unparse memory size e.g. 100, 100k, 100m, 100g. */
295 u8 * format_memory_size (u8 * s, va_list * va);
297 /* Format c identifier: e.g. a_name -> "a name". */
298 u8 * format_c_identifier (u8 * s, va_list * va);
300 /* Unix specific formats. */
302 /* Setup input from Unix file. */
303 void unformat_init_unix_file (unformat_input_t * input,
304 int file_descriptor);
306 /* Take input from Unix environment variable; returns
307 1 if variable exists zero otherwise. */
308 uword unformat_init_unix_env (unformat_input_t * input, char * var);
309 #endif /* CLIB_UNIX */
312 int test_format_main (unformat_input_t * input);
313 int test_unformat_main (unformat_input_t * input);
315 /* This is not the right place for this, but putting it in vec.h
316 created circular dependency problems. */
317 int test_vec_main (unformat_input_t * input);
319 #endif /* included_format_h */