blob: c4becff3a67f2e6de095a73bce7b7d66dae84d19 [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
90#define _(f) u8 * f (u8 * s, va_list * va)
91
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
102#ifdef CLIB_UNIX
103/* Unix specific formats. */
Dave Barachc3799992016-08-15 11:12:27 -0400104_(format_address_family);
105_(format_unix_arphrd);
106_(format_unix_interface_flags);
107_(format_network_address);
108_(format_network_protocol);
109_(format_network_port);
110_(format_sockaddr);
111_(format_ip4_tos_byte);
112_(format_ip4_packet);
113_(format_icmp4_type_and_code);
114_(format_ethernet_packet);
115_(format_hostname);
116_(format_timeval);
117_(format_time_float);
118_(format_signal);
119_(format_ucontext_pc);
Damjan Marion926b5642018-07-02 21:33:31 +0200120_(format_page_map);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121#endif
122
123#undef _
124
125/* Unformat. */
126
Dave Barachc3799992016-08-15 11:12:27 -0400127typedef struct _unformat_input_t
128{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 /* Input buffer (vector). */
Dave Barachc3799992016-08-15 11:12:27 -0400130 u8 *buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
132 /* Current index in input buffer. */
133 uword index;
134
135 /* Vector of buffer marks. Used to delineate pieces of the buffer
136 for error reporting and for parse recovery. */
Dave Barachc3799992016-08-15 11:12:27 -0400137 uword *buffer_marks;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139 /* User's function to fill the buffer when its empty
140 (and argument). */
Dave Barachc3799992016-08-15 11:12:27 -0400141 uword (*fill_buffer) (struct _unformat_input_t * i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
143 /* Return values for fill buffer function which indicate whether not
144 input has been exhausted. */
145#define UNFORMAT_END_OF_INPUT (~0)
146#define UNFORMAT_MORE_INPUT 0
147
148 /* User controlled argument to fill buffer function. */
Dave Barachc3799992016-08-15 11:12:27 -0400149 void *fill_buffer_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150} unformat_input_t;
151
152always_inline void
153unformat_init (unformat_input_t * i,
Dave Barachc3799992016-08-15 11:12:27 -0400154 uword (*fill_buffer) (unformat_input_t *),
155 void *fill_buffer_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156{
Dave Barachb7b92992018-10-17 10:38:51 -0400157 clib_memset (i, 0, sizeof (i[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 i->fill_buffer = fill_buffer;
159 i->fill_buffer_arg = fill_buffer_arg;
160}
161
162always_inline void
163unformat_free (unformat_input_t * i)
164{
165 vec_free (i->buffer);
166 vec_free (i->buffer_marks);
Dave Barachb7b92992018-10-17 10:38:51 -0400167 clib_memset (i, 0, sizeof (i[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168}
169
170always_inline uword
171unformat_check_input (unformat_input_t * i)
172{
173 /* Low level fill input function. */
174 extern uword _unformat_fill_input (unformat_input_t * i);
175
Dave Barachc3799992016-08-15 11:12:27 -0400176 if (i->index >= vec_len (i->buffer) && i->index != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 _unformat_fill_input (i);
178
179 return i->index;
180}
181
182/* Return true if input is exhausted */
183always_inline uword
184unformat_is_eof (unformat_input_t * input)
185{
186 return unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
187}
188
189/* Return next element in input vector,
190 possibly calling fill input to get more. */
191always_inline uword
192unformat_get_input (unformat_input_t * input)
193{
194 uword i = unformat_check_input (input);
195 if (i < vec_len (input->buffer))
196 {
197 input->index = i + 1;
198 i = input->buffer[i];
199 }
200 return i;
201}
202
203/* Back up input pointer by one. */
204always_inline void
205unformat_put_input (unformat_input_t * input)
Dave Barachc3799992016-08-15 11:12:27 -0400206{
207 input->index -= 1;
208}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
210/* Peek current input character without advancing. */
211always_inline uword
212unformat_peek_input (unformat_input_t * input)
213{
214 uword c = unformat_get_input (input);
215 if (c != UNFORMAT_END_OF_INPUT)
216 unformat_put_input (input);
217 return c;
218}
219
220/* Skip current input line. */
Dave Barachc3799992016-08-15 11:12:27 -0400221always_inline void
222unformat_skip_line (unformat_input_t * i)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223{
224 uword c;
225
Dave Barachc3799992016-08-15 11:12:27 -0400226 while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT && c != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 ;
228}
229
230uword unformat_skip_white_space (unformat_input_t * input);
231
232/* Unformat function. */
Dave Barachc3799992016-08-15 11:12:27 -0400233typedef uword (unformat_function_t) (unformat_input_t * input,
234 va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
236/* External functions. */
237
238/* General unformatting function with programmable input stream. */
Neale Ranns32e1c012016-11-22 17:07:28 +0000239uword unformat (unformat_input_t * i, const char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
241/* Call user defined parse function.
242 unformat_user (i, f, ...) is equivalent to unformat (i, "%U", f, ...) */
Dave Barachc3799992016-08-15 11:12:27 -0400243uword unformat_user (unformat_input_t * input, unformat_function_t * func,
244 ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
246/* Alternate version which allows for extensions. */
Neale Ranns32e1c012016-11-22 17:07:28 +0000247uword va_unformat (unformat_input_t * i, const char *fmt, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
249/* Setup for unformat of Unix style command line. */
Dave Barachc3799992016-08-15 11:12:27 -0400250void unformat_init_command_line (unformat_input_t * input, char *argv[]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
252/* Setup for unformat of given string. */
253void unformat_init_string (unformat_input_t * input,
Dave Barachc3799992016-08-15 11:12:27 -0400254 char *string, int string_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
256always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400257unformat_init_cstring (unformat_input_t * input, char *string)
258{
259 unformat_init_string (input, string, strlen (string));
260}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
262/* Setup for unformat of given vector string; vector will be freed by unformat_string. */
Dave Barachc3799992016-08-15 11:12:27 -0400263void unformat_init_vector (unformat_input_t * input, u8 * vector_string);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
265/* Format function for unformat input usable when an unformat error
266 has occurred. */
Dave Barachc3799992016-08-15 11:12:27 -0400267u8 *format_unformat_error (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
269#define unformat_parse_error(input) \
270 clib_error_return (0, "parse error `%U'", format_unformat_error, input)
271
272/* Print all input: not just error context. */
Dave Barachc3799992016-08-15 11:12:27 -0400273u8 *format_unformat_input (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
275/* Unformat (parse) function which reads a %s string and converts it
276 to and unformat_input_t. */
277unformat_function_t unformat_input;
278
279/* Parse a line ending with \n and return it. */
280unformat_function_t unformat_line;
281
282/* Parse a line ending with \n and return it as an unformat_input_t. */
283unformat_function_t unformat_line_input;
284
285/* Parse a token containing given set of characters. */
286unformat_function_t unformat_token;
287
288/* Parses a hexstring into a vector of bytes. */
289unformat_function_t unformat_hex_string;
290
291/* Returns non-zero match if input is exhausted.
292 Useful to ensure that the entire input matches with no trailing junk. */
293unformat_function_t unformat_eof;
294
295/* Parse memory size e.g. 100, 100k, 100m, 100g. */
296unformat_function_t unformat_memory_size;
297
298/* Unparse memory size e.g. 100, 100k, 100m, 100g. */
Dave Barachc3799992016-08-15 11:12:27 -0400299u8 *format_memory_size (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
301/* Format c identifier: e.g. a_name -> "a name". */
Dave Barachc3799992016-08-15 11:12:27 -0400302u8 *format_c_identifier (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
Damjan Mariona7e83ce2016-06-09 12:38:22 +0200304/* Format hexdump with both hex and printable chars - compatible with text2pcap */
Dave Barachc3799992016-08-15 11:12:27 -0400305u8 *format_hexdump (u8 * s, va_list * va);
Damjan Mariona7e83ce2016-06-09 12:38:22 +0200306
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307/* Unix specific formats. */
308#ifdef CLIB_UNIX
309/* Setup input from Unix file. */
Dave Barach59b25652017-09-10 15:04:27 -0400310void unformat_init_clib_file (unformat_input_t * input, int file_descriptor);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312/* Take input from Unix environment variable; returns
313 1 if variable exists zero otherwise. */
Dave Barachc3799992016-08-15 11:12:27 -0400314uword unformat_init_unix_env (unformat_input_t * input, char *var);
Damjan Mariona54230d2017-06-21 11:57:07 +0200315
316/* Unformat unix group id (gid) specified as integer or string */
317unformat_function_t unformat_unix_gid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318#endif /* CLIB_UNIX */
319
320/* Test code. */
321int test_format_main (unformat_input_t * input);
322int test_unformat_main (unformat_input_t * input);
323
Dave Barachc3799992016-08-15 11:12:27 -0400324/* This is not the right place for this, but putting it in vec.h
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325created circular dependency problems. */
326int test_vec_main (unformat_input_t * input);
327
328#endif /* included_format_h */
Dave Barachc3799992016-08-15 11:12:27 -0400329
330/*
331 * fd.io coding-style-patch-verification: ON
332 *
333 * Local Variables:
334 * eval: (c-set-style "gnu")
335 * End:
336 */