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, const char *format, va_list * args);
51 u8 *format (u8 * s, const 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) __clib_export u8 * f (u8 * s, va_list * va)
92 /* Standard user-defined formats. */
95 _(format_ascii_bytes);
97 _(format_hex_bytes_no_wrap);
98 _(format_white_space);
100 _(format_time_interval);
103 /* Unix specific formats. */
104 _(format_address_family);
105 _(format_network_address);
106 _(format_network_protocol);
107 _(format_network_port);
110 _(format_time_float);
112 _(format_ucontext_pc);
119 typedef struct _unformat_input_t
121 /* Input buffer (vector). */
124 /* Current index in input buffer. */
127 /* Vector of buffer marks. Used to delineate pieces of the buffer
128 for error reporting and for parse recovery. */
131 /* User's function to fill the buffer when its empty
133 uword (*fill_buffer) (struct _unformat_input_t * i);
135 /* Return values for fill buffer function which indicate whether not
136 input has been exhausted. */
137 #define UNFORMAT_END_OF_INPUT (~0)
138 #define UNFORMAT_MORE_INPUT 0
140 /* User controlled argument to fill buffer function. */
141 void *fill_buffer_arg;
145 unformat_init (unformat_input_t * i,
146 uword (*fill_buffer) (unformat_input_t *),
147 void *fill_buffer_arg)
149 clib_memset (i, 0, sizeof (i[0]));
150 i->fill_buffer = fill_buffer;
151 i->fill_buffer_arg = fill_buffer_arg;
155 unformat_free (unformat_input_t * i)
157 vec_free (i->buffer);
158 vec_free (i->buffer_marks);
159 clib_memset (i, 0, sizeof (i[0]));
163 unformat_check_input (unformat_input_t * i)
165 /* Low level fill input function. */
166 extern uword _unformat_fill_input (unformat_input_t * i);
168 if (i->index >= vec_len (i->buffer) && i->index != UNFORMAT_END_OF_INPUT)
169 _unformat_fill_input (i);
174 /* Return true if input is exhausted */
176 unformat_is_eof (unformat_input_t * input)
178 return unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
181 /* Return next element in input vector,
182 possibly calling fill input to get more. */
184 unformat_get_input (unformat_input_t * input)
186 uword i = unformat_check_input (input);
187 if (i < vec_len (input->buffer))
189 input->index = i + 1;
190 i = input->buffer[i];
195 /* Back up input pointer by one. */
197 unformat_put_input (unformat_input_t * input)
202 /* Peek current input character without advancing. */
204 unformat_peek_input (unformat_input_t * input)
206 uword c = unformat_get_input (input);
207 if (c != UNFORMAT_END_OF_INPUT)
208 unformat_put_input (input);
212 /* Skip current input line. */
214 unformat_skip_line (unformat_input_t * i)
218 while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT && c != '\n')
222 uword unformat_skip_white_space (unformat_input_t * input);
224 /* Unformat function. */
225 typedef uword (unformat_function_t) (unformat_input_t * input,
228 /* External functions. */
230 /* General unformatting function with programmable input stream. */
231 uword unformat (unformat_input_t * i, const char *fmt, ...);
233 /* Call user defined parse function.
234 unformat_user (i, f, ...) is equivalent to unformat (i, "%U", f, ...) */
235 uword unformat_user (unformat_input_t * input, unformat_function_t * func,
238 /* Alternate version which allows for extensions. */
239 uword va_unformat (unformat_input_t * i, const char *fmt, va_list * args);
241 /* Setup for unformat of Unix style command line. */
242 void unformat_init_command_line (unformat_input_t * input, char *argv[]);
244 /* Setup for unformat of given string. */
245 void unformat_init_string (unformat_input_t * input,
246 char *string, int string_len);
249 unformat_init_cstring (unformat_input_t * input, char *string)
251 unformat_init_string (input, string, strlen (string));
254 /* Setup for unformat of given vector string; vector will be freed by unformat_string. */
255 void unformat_init_vector (unformat_input_t * input, u8 * vector_string);
257 /* Format function for unformat input usable when an unformat error
259 u8 *format_unformat_error (u8 * s, va_list * va);
261 #define unformat_parse_error(input) \
262 clib_error_return (0, "parse error `%U'", format_unformat_error, input)
264 /* Print all input: not just error context. */
265 u8 *format_unformat_input (u8 * s, va_list * va);
267 /* Unformat (parse) function which reads a %s string and converts it
268 to and unformat_input_t. */
269 unformat_function_t unformat_input;
271 /* Parse a line ending with \n and return it. */
272 unformat_function_t unformat_line;
274 /* Parse a line ending with \n and return it as an unformat_input_t. */
275 unformat_function_t unformat_line_input;
277 /* Parse a token containing given set of characters. */
278 unformat_function_t unformat_token;
280 /* Parses a hexstring into a vector of bytes. */
281 unformat_function_t unformat_hex_string;
283 /* Returns non-zero match if input is exhausted.
284 Useful to ensure that the entire input matches with no trailing junk. */
285 unformat_function_t unformat_eof;
287 /* Parse memory size e.g. 100, 100k, 100m, 100g. */
288 unformat_function_t unformat_memory_size;
290 /* Unparse memory size e.g. 100, 100k, 100m, 100g. */
291 u8 *format_memory_size (u8 * s, va_list * va);
293 /* Parse memory page size e.g. 4K, 2M */
294 unformat_function_t unformat_log2_page_size;
296 /* Unparse memory page size e.g. 4K, 2M */
297 u8 *format_log2_page_size (u8 * s, va_list * va);
299 /* Format c identifier: e.g. a_name -> "a name". */
300 u8 *format_c_identifier (u8 * s, va_list * va);
302 /* Format hexdump with both hex and printable chars - compatible with text2pcap */
303 u8 *format_hexdump (u8 * s, va_list * va);
305 /* Unix specific formats. */
307 /* Setup input from Unix file. */
308 void unformat_init_clib_file (unformat_input_t * input, int file_descriptor);
310 /* Take input from Unix environment variable; returns
311 1 if variable exists zero otherwise. */
312 uword unformat_init_unix_env (unformat_input_t * input, char *var);
314 /* Unformat unix group id (gid) specified as integer or string */
315 unformat_function_t unformat_unix_gid;
316 #endif /* CLIB_UNIX */
318 uword unformat_data_size (unformat_input_t * input, va_list * args);
321 int test_format_main (unformat_input_t * input);
322 int test_unformat_main (unformat_input_t * input);
324 /* This is not the right place for this, but putting it in vec.h
325 created circular dependency problems. */
326 int test_vec_main (unformat_input_t * input);
328 #endif /* included_format_h */
331 * fd.io coding-style-patch-verification: ON
334 * eval: (c-set-style "gnu")