blob: cf5e1f6dccebd585b0c25d372f9f4cad8309f6b5 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/*
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
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:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
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.
36*/
37
38#ifndef included_format_h
39#define included_format_h
40
41#include <stdarg.h>
42
Dave Barachc3799992016-08-15 11:12:27 -040043#include <vppinfra/clib.h> /* for CLIB_UNIX, etc. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#include <vppinfra/vec.h>
Dave Barachc3799992016-08-15 11:12:27 -040045#include <vppinfra/error.h> /* for ASSERT */
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#include <vppinfra/string.h>
47
Dave Barachc3799992016-08-15 11:12:27 -040048typedef u8 *(format_function_t) (u8 * s, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
Neale Ranns0bfe5d82016-08-25 15:29:12 +010050u8 *va_format (u8 * s, const char *format, va_list * args);
51u8 *format (u8 * s, const char *format, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
53#ifdef CLIB_UNIX
54
55#include <stdio.h>
56
57#else /* ! CLIB_UNIX */
58
59/* We're not Unix and have not stdio.h */
60#define FILE void
61#define stdin ((FILE *) 0)
62#define stdout ((FILE *) 1)
63#define stderr ((FILE *) 2)
64
65#endif
66
Dave Barachc3799992016-08-15 11:12:27 -040067word va_fformat (FILE * f, char *fmt, va_list * va);
68word fformat (FILE * f, char *fmt, ...);
69word fdformat (int fd, char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
Christophe Fontained3c008d2017-10-02 18:10:54 +020071always_inline u32
Ed Warnickecb9cada2015-12-08 15:45:58 -070072format_get_indent (u8 * s)
73{
Christophe Fontained3c008d2017-10-02 18:10:54 +020074 u32 indent = 0;
Dave Barachc3799992016-08-15 11:12:27 -040075 u8 *nl;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Dave Barachc3799992016-08-15 11:12:27 -040077 if (!s)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 return indent;
79
80 nl = vec_end (s) - 1;
81 while (nl >= s)
82 {
83 if (*nl-- == '\n')
84 break;
85 indent++;
86 }
87 return indent;
88}
89
Damjan Mariondae1c7e2020-10-17 13:32:25 +020090#define _(f) __clib_export u8 * f (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
92/* Standard user-defined formats. */
Dave Barachc3799992016-08-15 11:12:27 -040093_(format_vec32);
94_(format_vec_uword);
95_(format_ascii_bytes);
96_(format_hex_bytes);
Damjan Marion3860a772019-06-17 23:20:27 +020097_(format_hex_bytes_no_wrap);
Dave Barachc3799992016-08-15 11:12:27 -040098_(format_white_space);
99_(format_f64);
100_(format_time_interval);
Nathan Skrzypczak6de58f52022-01-24 17:10:41 +0100101_ (format_duration);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
103#ifdef CLIB_UNIX
104/* Unix specific formats. */
Dave Barachc3799992016-08-15 11:12:27 -0400105_(format_address_family);
Dave Barachc3799992016-08-15 11:12:27 -0400106_(format_network_address);
107_(format_network_protocol);
108_(format_network_port);
109_(format_sockaddr);
Dave Barachc3799992016-08-15 11:12:27 -0400110_(format_timeval);
111_(format_time_float);
112_(format_signal);
113_(format_ucontext_pc);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114#endif
115
116#undef _
117
118/* Unformat. */
119
Dave Barachc3799992016-08-15 11:12:27 -0400120typedef struct _unformat_input_t
121{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 /* Input buffer (vector). */
Dave Barachc3799992016-08-15 11:12:27 -0400123 u8 *buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
125 /* Current index in input buffer. */
126 uword index;
127
128 /* Vector of buffer marks. Used to delineate pieces of the buffer
129 for error reporting and for parse recovery. */
Dave Barachc3799992016-08-15 11:12:27 -0400130 uword *buffer_marks;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
132 /* User's function to fill the buffer when its empty
133 (and argument). */
Dave Barachc3799992016-08-15 11:12:27 -0400134 uword (*fill_buffer) (struct _unformat_input_t * i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200136 /* User's function to be called on input_free */
137 void (*free) (struct _unformat_input_t *i);
138
139 /* Return values for fill buffer function which indicate whether not
140 input has been exhausted. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141#define UNFORMAT_END_OF_INPUT (~0)
142#define UNFORMAT_MORE_INPUT 0
143
144 /* User controlled argument to fill buffer function. */
Dave Barachc3799992016-08-15 11:12:27 -0400145 void *fill_buffer_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146} unformat_input_t;
147
148always_inline void
149unformat_init (unformat_input_t * i,
Dave Barachc3799992016-08-15 11:12:27 -0400150 uword (*fill_buffer) (unformat_input_t *),
151 void *fill_buffer_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152{
Dave Barachb7b92992018-10-17 10:38:51 -0400153 clib_memset (i, 0, sizeof (i[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 i->fill_buffer = fill_buffer;
155 i->fill_buffer_arg = fill_buffer_arg;
156}
157
158always_inline void
159unformat_free (unformat_input_t * i)
160{
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200161 if (i->free)
162 i->free (i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 vec_free (i->buffer);
164 vec_free (i->buffer_marks);
Dave Barachb7b92992018-10-17 10:38:51 -0400165 clib_memset (i, 0, sizeof (i[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166}
167
168always_inline uword
169unformat_check_input (unformat_input_t * i)
170{
171 /* Low level fill input function. */
172 extern uword _unformat_fill_input (unformat_input_t * i);
173
Dave Barachc3799992016-08-15 11:12:27 -0400174 if (i->index >= vec_len (i->buffer) && i->index != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 _unformat_fill_input (i);
176
177 return i->index;
178}
179
180/* Return true if input is exhausted */
181always_inline uword
182unformat_is_eof (unformat_input_t * input)
183{
184 return unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
185}
186
187/* Return next element in input vector,
188 possibly calling fill input to get more. */
189always_inline uword
190unformat_get_input (unformat_input_t * input)
191{
192 uword i = unformat_check_input (input);
193 if (i < vec_len (input->buffer))
194 {
195 input->index = i + 1;
196 i = input->buffer[i];
197 }
198 return i;
199}
200
201/* Back up input pointer by one. */
202always_inline void
203unformat_put_input (unformat_input_t * input)
Dave Barachc3799992016-08-15 11:12:27 -0400204{
205 input->index -= 1;
206}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207
Nathan Skrzypczake2d0c6e2022-03-23 18:08:53 +0100208always_inline uword
209is_white_space (uword c)
210{
211 switch (c)
212 {
213 case ' ':
214 case '\t':
215 case '\n':
216 case '\r':
217 return 1;
218
219 default:
220 return 0;
221 }
222}
223
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224/* Peek current input character without advancing. */
225always_inline uword
226unformat_peek_input (unformat_input_t * input)
227{
228 uword c = unformat_get_input (input);
229 if (c != UNFORMAT_END_OF_INPUT)
230 unformat_put_input (input);
231 return c;
232}
233
234/* Skip current input line. */
Dave Barachc3799992016-08-15 11:12:27 -0400235always_inline void
236unformat_skip_line (unformat_input_t * i)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237{
238 uword c;
239
Dave Barachc3799992016-08-15 11:12:27 -0400240 while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT && c != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 ;
242}
243
244uword unformat_skip_white_space (unformat_input_t * input);
245
246/* Unformat function. */
Dave Barachc3799992016-08-15 11:12:27 -0400247typedef uword (unformat_function_t) (unformat_input_t * input,
248 va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
250/* External functions. */
251
252/* General unformatting function with programmable input stream. */
Neale Ranns32e1c012016-11-22 17:07:28 +0000253uword unformat (unformat_input_t * i, const char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
255/* Call user defined parse function.
256 unformat_user (i, f, ...) is equivalent to unformat (i, "%U", f, ...) */
Dave Barachc3799992016-08-15 11:12:27 -0400257uword unformat_user (unformat_input_t * input, unformat_function_t * func,
258 ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
260/* Alternate version which allows for extensions. */
Neale Ranns32e1c012016-11-22 17:07:28 +0000261uword va_unformat (unformat_input_t * i, const char *fmt, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
263/* Setup for unformat of Unix style command line. */
Dave Barachc3799992016-08-15 11:12:27 -0400264void unformat_init_command_line (unformat_input_t * input, char *argv[]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
266/* Setup for unformat of given string. */
BenoƮt Ganne82f618c2021-10-13 11:35:15 +0200267void unformat_init_string (unformat_input_t *input, const char *string,
268 int string_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
270always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400271unformat_init_cstring (unformat_input_t * input, char *string)
272{
273 unformat_init_string (input, string, strlen (string));
274}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
276/* Setup for unformat of given vector string; vector will be freed by unformat_string. */
Dave Barachc3799992016-08-15 11:12:27 -0400277void unformat_init_vector (unformat_input_t * input, u8 * vector_string);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
279/* Format function for unformat input usable when an unformat error
280 has occurred. */
Dave Barachc3799992016-08-15 11:12:27 -0400281u8 *format_unformat_error (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
283#define unformat_parse_error(input) \
284 clib_error_return (0, "parse error `%U'", format_unformat_error, input)
285
286/* Print all input: not just error context. */
Dave Barachc3799992016-08-15 11:12:27 -0400287u8 *format_unformat_input (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
289/* Unformat (parse) function which reads a %s string and converts it
290 to and unformat_input_t. */
291unformat_function_t unformat_input;
292
293/* Parse a line ending with \n and return it. */
294unformat_function_t unformat_line;
295
296/* Parse a line ending with \n and return it as an unformat_input_t. */
297unformat_function_t unformat_line_input;
298
299/* Parse a token containing given set of characters. */
300unformat_function_t unformat_token;
301
302/* Parses a hexstring into a vector of bytes. */
303unformat_function_t unformat_hex_string;
304
305/* Returns non-zero match if input is exhausted.
306 Useful to ensure that the entire input matches with no trailing junk. */
307unformat_function_t unformat_eof;
308
309/* Parse memory size e.g. 100, 100k, 100m, 100g. */
310unformat_function_t unformat_memory_size;
311
Damjan Marionf5665242023-07-31 20:07:31 +0200312/* Unformat C string array, takes array length as 2nd argument */
313unformat_function_t unformat_c_string_array;
314
Nathan Skrzypczak162ff5e2021-11-09 18:18:21 +0100315/* Format base 10 e.g. 100, 100K, 100M, 100G */
316u8 *format_base10 (u8 *s, va_list *va);
317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318/* Unparse memory size e.g. 100, 100k, 100m, 100g. */
Dave Barachc3799992016-08-15 11:12:27 -0400319u8 *format_memory_size (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Damjan Marionb5095042020-09-11 22:13:46 +0200321/* Parse memory page size e.g. 4K, 2M */
322unformat_function_t unformat_log2_page_size;
323
324/* Unparse memory page size e.g. 4K, 2M */
325u8 *format_log2_page_size (u8 * s, va_list * va);
326
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327/* Format c identifier: e.g. a_name -> "a name". */
Dave Barachc3799992016-08-15 11:12:27 -0400328u8 *format_c_identifier (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Damjan Mariona7e83ce2016-06-09 12:38:22 +0200330/* Format hexdump with both hex and printable chars - compatible with text2pcap */
Dave Barachc3799992016-08-15 11:12:27 -0400331u8 *format_hexdump (u8 * s, va_list * va);
Damjan Marioncbc04102023-04-16 11:12:22 +0000332u8 *format_hexdump_u16 (u8 *s, va_list *va);
333u8 *format_hexdump_u32 (u8 *s, va_list *va);
334u8 *format_hexdump_u64 (u8 *s, va_list *va);
Damjan Mariona7e83ce2016-06-09 12:38:22 +0200335
Damjan Marion5294cdc2023-04-04 17:06:26 +0000336/* Format bitmap of array of uword numbers */
337u8 *format_uword_bitmap (u8 *s, va_list *va);
Damjan Marione6387b22021-12-22 21:58:54 +0100338
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339/* Unix specific formats. */
340#ifdef CLIB_UNIX
341/* Setup input from Unix file. */
Dave Barach59b25652017-09-10 15:04:27 -0400342void unformat_init_clib_file (unformat_input_t * input, int file_descriptor);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200344/* Setup input from flesystem path. */
345uword unformat_init_file (unformat_input_t *input, char *fmt, ...);
346
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347/* Take input from Unix environment variable; returns
348 1 if variable exists zero otherwise. */
Dave Barachc3799992016-08-15 11:12:27 -0400349uword unformat_init_unix_env (unformat_input_t * input, char *var);
Damjan Mariona54230d2017-06-21 11:57:07 +0200350
351/* Unformat unix group id (gid) specified as integer or string */
352unformat_function_t unformat_unix_gid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353#endif /* CLIB_UNIX */
354
MathiasRaoul579b1652019-10-04 09:53:45 +0000355uword unformat_data_size (unformat_input_t * input, va_list * args);
356
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357/* Test code. */
358int test_format_main (unformat_input_t * input);
359int test_unformat_main (unformat_input_t * input);
360
Dave Barachc3799992016-08-15 11:12:27 -0400361/* This is not the right place for this, but putting it in vec.h
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362created circular dependency problems. */
363int test_vec_main (unformat_input_t * input);
364
365#endif /* included_format_h */
Dave Barachc3799992016-08-15 11:12:27 -0400366
367/*
368 * fd.io coding-style-patch-verification: ON
369 *
370 * Local Variables:
371 * eval: (c-set-style "gnu")
372 * End:
373 */