FD.io VPP  v16.06
Vector Packet Processing
format.h
Go to the documentation of this file.
1 /*
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 
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>
47 
48 typedef u8 * (format_function_t) (u8 * s, va_list * args);
49 
50 u8 * va_format (u8 * s, char * format, va_list * args);
51 u8 * format (u8 * s, char * format, ...);
52 
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 
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, ...);
70 
73 {
74  uword indent = 0;
75  u8 * nl;
76 
77  if (! s)
78  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. */
93 _ (format_vec32);
98 _ (format_f64);
100 
101 #ifdef CLIB_UNIX
102 /* Unix specific formats. */
109 _ (format_sockaddr);
114 _ (format_hostname);
115 _ (format_timeval);
117 _ (format_signal);
119 #endif
120 
121 #undef _
122 
123 /* Unformat. */
124 
125 typedef struct _unformat_input_t {
126  /* Input buffer (vector). */
127  u8 * buffer;
128 
129  /* Current index in input buffer. */
130  uword index;
131 
132  /* Vector of buffer marks. Used to delineate pieces of the buffer
133  for error reporting and for parse recovery. */
134  uword * buffer_marks;
135 
136  /* User's function to fill the buffer when its empty
137  (and argument). */
138  uword (* fill_buffer) (struct _unformat_input_t * i);
139 
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
144 
145  /* User controlled argument to fill buffer function. */
146  void * fill_buffer_arg;
148 
149 always_inline void
151  uword (* fill_buffer) (unformat_input_t *),
152  void * fill_buffer_arg)
153 {
154  memset (i, 0, sizeof (i[0]));
155  i->fill_buffer = fill_buffer;
156  i->fill_buffer_arg = fill_buffer_arg;
157 }
158 
159 always_inline void
161 {
162  vec_free (i->buffer);
163  vec_free (i->buffer_marks);
164  memset (i, 0, sizeof (i[0]));
165 }
166 
169 {
170  /* Low level fill input function. */
171  extern uword _unformat_fill_input (unformat_input_t * i);
172 
173  if (i->index >= vec_len (i->buffer)
174  && i->index != UNFORMAT_END_OF_INPUT)
175  _unformat_fill_input (i);
176 
177  return i->index;
178 }
179 
180 /* Return true if input is exhausted */
183 {
185 }
186 
187 /* Return next element in input vector,
188  possibly calling fill input to get more. */
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. */
202 always_inline void
204 { input->index -= 1; }
205 
206 /* Peek current input character without advancing. */
209 {
210  uword c = unformat_get_input (input);
211  if (c != UNFORMAT_END_OF_INPUT)
212  unformat_put_input (input);
213  return c;
214 }
215 
216 /* Skip current input line. */
218 {
219  uword c;
220 
221  while ((c = unformat_get_input (i)) != UNFORMAT_END_OF_INPUT
222  && c != '\n')
223  ;
224 }
225 
227 
228 /* Unformat function. */
229 typedef uword (unformat_function_t) (unformat_input_t * input, va_list * args);
230 
231 /* External functions. */
232 
233 /* General unformatting function with programmable input stream. */
234 uword unformat (unformat_input_t * i, char * fmt, ...);
235 
236 /* Call user defined parse function.
237  unformat_user (i, f, ...) is equivalent to unformat (i, "%U", f, ...) */
239 
240 /* Alternate version which allows for extensions. */
241 uword va_unformat (unformat_input_t * i, char * fmt, va_list * args);
242 
243 /* Setup for unformat of Unix style command line. */
245  char * argv[]);
246 
247 /* Setup for unformat of given string. */
249  char * string,
250  int string_len);
251 
252 always_inline void
254  char * string)
255 { unformat_init_string (input, string, strlen (string)); }
256 
257 /* Setup for unformat of given vector string; vector will be freed by unformat_string. */
259  u8 * vector_string);
260 
261 /* Format function for unformat input usable when an unformat error
262  has occurred. */
263 u8 * format_unformat_error (u8 * s, va_list * va);
264 
265 #define unformat_parse_error(input) \
266  clib_error_return (0, "parse error `%U'", format_unformat_error, input)
267 
268 /* Print all input: not just error context. */
269 u8 * format_unformat_input (u8 * s, va_list * va);
270 
271 /* Unformat (parse) function which reads a %s string and converts it
272  to and unformat_input_t. */
274 
275 /* Parse a line ending with \n and return it. */
277 
278 /* Parse a line ending with \n and return it as an unformat_input_t. */
280 
281 /* Parse a token containing given set of characters. */
283 
284 /* Parses a hexstring into a vector of bytes. */
286 
287 /* Returns non-zero match if input is exhausted.
288  Useful to ensure that the entire input matches with no trailing junk. */
290 
291 /* Parse memory size e.g. 100, 100k, 100m, 100g. */
293 
294 /* Unparse memory size e.g. 100, 100k, 100m, 100g. */
295 u8 * format_memory_size (u8 * s, va_list * va);
296 
297 /* Format c identifier: e.g. a_name -> "a name". */
298 u8 * format_c_identifier (u8 * s, va_list * va);
299 
300 /* Unix specific formats. */
301 #ifdef CLIB_UNIX
302 /* Setup input from Unix file. */
304  int file_descriptor);
305 
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 */
310 
311 /* Test code. */
312 int test_format_main (unformat_input_t * input);
314 
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);
318 
319 #endif /* included_format_h */
void unformat_init_vector(unformat_input_t *input, u8 *vector_string)
Definition: unformat.c:1000
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
u8 * format_time_float(u8 *s, va_list *args)
Definition: unix-formats.c:822
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:229
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
u8 * format_ascii_bytes(u8 *s, va_list *va)
Definition: std-formats.c:70
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
unformat_function_t unformat_token
Definition: format.h:282
u8 * format_c_identifier(u8 *s, va_list *va)
Definition: std-formats.c:239
u8 * va_format(u8 *s, char *format, va_list *args)
Definition: format.c:374
u8 * format_ip4_tos_byte(u8 *s, va_list *args)
Definition: unix-formats.c:407
u8 * format_icmp4_type_and_code(u8 *s, va_list *args)
Definition: unix-formats.c:306
u8 * format_ip4_packet(u8 *s, va_list *args)
Definition: unix-formats.c:437
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
u8 * format_timeval(u8 *s, va_list *args)
Definition: unix-formats.c:748
uword unformat_init_unix_env(unformat_input_t *input, char *var)
Definition: unformat.c:1034
u8 * format_address_family(u8 *s, va_list *va)
Definition: unix-formats.c:92
always_inline void unformat_init_cstring(unformat_input_t *input, char *string)
Definition: format.h:253
#define always_inline
Definition: clib.h:84
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:107
unformat_function_t unformat_hex_string
Definition: format.h:285
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:79
unformat_function_t unformat_line
Definition: format.h:276
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:991
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
#define vec_end(v)
End (last data address) of vector.
int test_unformat_main(unformat_input_t *input)
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:975
uword unformat_skip_white_space(unformat_input_t *input)
Definition: unformat.c:787
u8 * format_unix_interface_flags(u8 *s, va_list *args)
Definition: unix-formats.c:569
u8 * format_unix_arphrd(u8 *s, va_list *args)
Definition: unix-formats.c:519
u8 * format_network_port(u8 *s, va_list *args)
Definition: unix-formats.c:186
always_inline void unformat_skip_line(unformat_input_t *i)
Definition: format.h:217
word fformat(FILE *f, char *fmt,...)
Definition: format.c:437
u8 * format_sockaddr(u8 *s, va_list *args)
Definition: unix-formats.c:229
unformat_function_t unformat_eof
Definition: format.h:289
always_inline uword unformat_is_eof(unformat_input_t *input)
Definition: format.h:182
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
u8 * format_vec32(u8 *s, va_list *va)
Definition: std-formats.c:41
void unformat_init_unix_file(unformat_input_t *input, int file_descriptor)
Definition: unformat.c:1026
u8 * format_unformat_input(u8 *s, va_list *va)
Definition: unformat.c:130
u8 * format_memory_size(u8 *s, va_list *va)
Definition: std-formats.c:185
u8 * format(u8 *s, char *format,...)
Definition: format.c:405
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
always_inline uword unformat_get_input(unformat_input_t *input)
Definition: format.h:190
u8 * format_network_protocol(u8 *s, va_list *args)
Definition: unix-formats.c:168
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
always_inline uword format_get_indent(u8 *s)
Definition: format.h:72
unformat_function_t unformat_input
Definition: format.h:273
u64 uword
Definition: types.h:112
u8 * format_vec_uword(u8 *s, va_list *va)
Definition: std-formats.c:55
always_inline void unformat_init(unformat_input_t *i, uword(*fill_buffer)(unformat_input_t *), void *fill_buffer_arg)
Definition: format.h:150
word va_fformat(FILE *f, char *fmt, va_list *va)
Definition: format.c:414
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
uword va_unformat(unformat_input_t *i, char *fmt, va_list *args)
Definition: unformat.c:805
u8 * format_signal(u8 *s, va_list *args)
Definition: unix-formats.c:834
u8 * format_time_interval(u8 *s, va_list *args)
Definition: std-formats.c:115
int test_format_main(unformat_input_t *input)
u8 * format_network_address(u8 *s, va_list *args)
Definition: unix-formats.c:205
always_inline void unformat_put_input(unformat_input_t *input)
Definition: format.h:203
u8 * format_ethernet_packet(u8 *s, va_list *args)
Definition: unix-formats.c:697
u8 * format_hostname(u8 *s, va_list *args)
Definition: unix-formats.c:737
unformat_function_t unformat_memory_size
Definition: format.h:292
struct _unformat_input_t unformat_input_t
always_inline uword unformat_peek_input(unformat_input_t *input)
Definition: format.h:208
unformat_function_t unformat_line_input
Definition: format.h:279
u8 * format_ucontext_pc(u8 *s, va_list *args)
Definition: unix-formats.c:885
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
word fdformat(int fd, char *fmt,...)
Definition: format.c:450
int test_vec_main(unformat_input_t *input)