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 #include <vppinfra/format.h>
40 /* Call user's function to fill input buffer. */
42 _unformat_fill_input (unformat_input_t * i)
46 if (i->index == UNFORMAT_END_OF_INPUT)
49 first_mark = l = vec_len (i->buffer);
50 if (vec_len (i->buffer_marks) > 0)
51 first_mark = i->buffer_marks[0];
53 /* Re-use buffer when no marks. */
55 vec_delete (i->buffer, first_mark, 0);
57 i->index = vec_len (i->buffer);
58 for (l = 0; l < vec_len (i->buffer_marks); l++)
59 i->buffer_marks[l] -= first_mark;
61 /* Call user's function to fill the buffer. */
63 i->index = i->fill_buffer (i);
65 /* If input pointer is still beyond end of buffer even after
66 fill then we've run out of input. */
67 if (i->index >= vec_len (i->buffer))
68 i->index = UNFORMAT_END_OF_INPUT;
73 /* Format function for dumping input stream. */
75 format_unformat_error (u8 * s, va_list * va)
77 unformat_input_t *i = va_arg (*va, unformat_input_t *);
78 uword l = vec_len (i->buffer);
80 /* Only show so much of the input buffer (it could be really large). */
85 uword n = l - i->index;
88 p = i->buffer + i->index;
89 p_end = p + (n > n_max ? n_max : n);
91 /* Skip white space at end. */
94 while (p_end > p && is_white_space (p_end[-1]))
103 vec_add (s, "\\r", 2);
106 vec_add (s, "\\n", 2);
109 vec_add (s, "\\t", 2);
119 vec_add (s, "...", 3);
125 /* Print everything: not just error context. */
127 format_unformat_input (u8 * s, va_list * va)
129 unformat_input_t *i = va_arg (*va, unformat_input_t *);
132 if (i->index == UNFORMAT_END_OF_INPUT)
133 s = format (s, "{END_OF_INPUT}");
136 l = vec_len (i->buffer);
139 vec_add (s, i->buffer + i->index, n);
147 di (unformat_input_t * i)
149 fformat (stderr, "%U\n", format_unformat_input, i);
153 /* Parse delimited vector string. If string starts with { then string
154 is delimited by balanced parenthesis. Other string is delimited by
155 white space. {} were chosen since they are special to the shell. */
157 unformat_string (unformat_input_t * input,
158 uword delimiter_character,
159 uword format_character, va_list * va)
161 u8 **string_return = va_arg (*va, u8 **);
164 word is_paren_delimited = 0;
168 switch (delimiter_character)
173 delimiter_character = 0;
177 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
181 /* Null return string means to skip over delimited input. */
182 add_to_vector = string_return != 0;
195 if (paren == 0 && vec_len (s) == 0)
197 is_paren_delimited = 1;
205 if (is_paren_delimited && paren == 0)
213 if (!is_paren_delimited)
215 unformat_put_input (input);
221 if (!is_paren_delimited && c == delimiter_character)
223 unformat_put_input (input);
235 /* Match the string { END-OF-INPUT as a single brace. */
236 if (c == UNFORMAT_END_OF_INPUT && vec_len (s) == 0 && paren == 1)
239 /* Don't match null string. */
240 if (c == UNFORMAT_END_OF_INPUT && vec_len (s) == 0)
243 /* Null terminate C string. */
244 if (format_character == 's')
250 vec_free (s); /* just to make sure */
256 unformat_hex_string (unformat_input_t * input, va_list * va)
258 u8 **hexstring_return = va_arg (*va, u8 **);
265 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
267 if (c >= '0' && c <= '9')
268 d = 16 * d + c - '0';
269 else if (c >= 'a' && c <= 'f')
270 d = 16 * d + 10 + c - 'a';
271 else if (c >= 'A' && c <= 'F')
272 d = 16 * d + 10 + c - 'A';
275 unformat_put_input (input);
287 /* Hex string must have even number of digits. */
293 /* Make sure something was processed. */
299 *hexstring_return = s;
303 /* unformat (input "foo%U", unformat_eof) matches terminal foo only */
305 unformat_eof (unformat_input_t * input, va_list * va)
307 return unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
310 /* Parse a token containing given set of characters. */
312 unformat_token (unformat_input_t * input, va_list * va)
314 u8 *token_chars = va_arg (*va, u8 *);
315 u8 **string_return = va_arg (*va, u8 **);
320 token_chars = (u8 *) "a-zA-Z0-9_";
322 clib_memset (map, 0, sizeof (map));
323 for (s = token_chars; *s;)
327 * The test order is important: s[1] is valid because s[0] != '\0' but
328 * s[2] might not if s[1] == '\0'
329 * Also, if s[1] == '-' but s[2] == '\0' the test s[0] < s[2] will
332 if (s[1] == '-' && s[0] < s[2])
334 for (i = s[0]; i <= s[2]; i++)
346 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
350 unformat_put_input (input);
357 if (vec_len (s) == 0)
364 /* Unformat (parse) function which reads a %s string and converts it
365 to and unformat_input_t. */
367 unformat_input (unformat_input_t * i, va_list * args)
369 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
372 if (unformat (i, "%v", &s))
374 unformat_init_vector (sub_input, s);
381 /* Parse a line ending with \n and return it. */
383 unformat_line (unformat_input_t * i, va_list * va)
385 u8 *line = 0, **result = va_arg (*va, u8 **);
388 while ((c = unformat_get_input (i)) != '\n' && c != UNFORMAT_END_OF_INPUT)
394 return vec_len (line);
397 /* Parse a line ending with \n and return it as an unformat_input_t. */
399 unformat_line_input (unformat_input_t * i, va_list * va)
401 unformat_input_t *result = va_arg (*va, unformat_input_t *);
403 if (!unformat_user (i, unformat_line, &line))
405 unformat_init_vector (result, line);
409 /* Values for is_signed. */
410 #define UNFORMAT_INTEGER_SIGNED 1
411 #define UNFORMAT_INTEGER_UNSIGNED 0
414 unformat_integer (unformat_input_t * input,
415 va_list * va, uword base, uword is_signed, uword data_bytes)
423 /* We only support bases <= 64. */
424 if (base < 2 || base > 64)
427 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
440 /* Leading sign for unsigned number. */
443 /* Sign after input (e.g. 100-200). */
457 digit = 10 + (c - 'a');
461 digit = 10 + (base >= 36 ? 26 : 0) + (c - 'A');
479 unformat_put_input (input);
484 uword new_value = base * value + digit;
486 /* Check for overflow. */
487 if (new_value < value)
503 void *v = va_arg (*va, void *);
505 if (data_bytes == ~0)
506 data_bytes = sizeof (int);
535 times_power_of_ten (f64 x, int n)
539 static f64 t[8] = { 1e+0, 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, };
549 static f64 t[8] = { 1e-0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, };
561 unformat_float (unformat_input_t * input, va_list * va)
565 uword n_digits[3], value_index = 0;
566 uword signs[2], sign_index = 0;
569 clib_memset (values, 0, sizeof (values));
570 clib_memset (n_digits, 0, sizeof (n_digits));
571 clib_memset (signs, 0, sizeof (signs));
573 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
578 if (value_index == 2 && n_digits[2] == 0)
579 /* sign of exponent: it's ok. */ ;
581 else if (value_index < 2 && n_digits[0] > 0)
584 unformat_put_input (input);
588 else if (n_input > 0)
591 signs[sign_index++] = 1;
595 if (value_index == 2 && n_digits[2] == 0)
596 /* sign of exponent: it's ok. */ ;
598 else if (value_index < 2 && n_digits[0] > 0)
601 unformat_put_input (input);
605 else if (n_input > 0)
607 signs[sign_index++] = 0;
628 tmp = values[value_index] * 10 + c - '0';
630 /* Check for overflow. */
631 if (tmp < values[value_index])
633 values[value_index] = tmp;
634 n_digits[value_index] += 1;
639 unformat_put_input (input);
649 f64 f_values[2], *value_return;
652 /* Must have either whole or fraction digits. */
653 if (n_digits[0] + n_digits[1] <= 0)
656 f_values[0] = values[0];
658 f_values[0] = -f_values[0];
660 f_values[1] = values[1];
661 f_values[1] = times_power_of_ten (f_values[1], -n_digits[1]);
663 f_values[0] += f_values[1];
669 f_values[0] = times_power_of_ten (f_values[0], expon);
671 value_return = va_arg (*va, f64 *);
672 *value_return = f_values[0];
681 match_input_with_format (unformat_input_t * input, const char *f)
690 if (cf == 0 || cf == '%' || cf == ' ')
694 ci = unformat_get_input (input);
703 do_percent (unformat_input_t * input, va_list * va, const char *f)
705 uword cf, n, data_bytes = ~0;
717 data_bytes = sizeof (uword);
725 data_bytes = sizeof (long long);
729 data_bytes = sizeof (long);
735 data_bytes = sizeof (long long);
743 data_bytes = va_arg (*va, int);
745 n = unformat_integer (input, va, 10,
746 UNFORMAT_INTEGER_SIGNED, data_bytes);
750 n = unformat_integer (input, va, 10,
751 UNFORMAT_INTEGER_UNSIGNED, data_bytes);
755 n = unformat_integer (input, va, 2,
756 UNFORMAT_INTEGER_UNSIGNED, data_bytes);
760 n = unformat_integer (input, va, 8,
761 UNFORMAT_INTEGER_UNSIGNED, data_bytes);
765 data_bytes = va_arg (*va, int);
767 n = unformat_integer (input, va, 16,
768 UNFORMAT_INTEGER_UNSIGNED, data_bytes);
772 n = unformat_float (input, va);
777 n = unformat_string (input, f[0], cf, va);
782 unformat_function_t *f = va_arg (*va, unformat_function_t *);
790 int *var = va_arg (*va, int *);
791 uword val = va_arg (*va, int);
805 unformat_skip_white_space (unformat_input_t * input)
810 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
812 if (!is_white_space (c))
814 unformat_put_input (input);
823 va_unformat (unformat_input_t * input, const char *fmt, va_list * va)
826 uword input_matches_format;
827 uword default_skip_input_white_space;
828 uword n_input_white_space_skipped;
829 uword last_non_white_space_match_percent;
830 uword last_non_white_space_match_format;
832 vec_add1_aligned (input->buffer_marks, input->index,
833 sizeof (input->buffer_marks[0]));
836 default_skip_input_white_space = 1;
837 input_matches_format = 0;
838 last_non_white_space_match_percent = 0;
839 last_non_white_space_match_format = 0;
844 uword is_percent, skip_input_white_space;
849 /* Always skip input white space at start of format string.
850 Otherwise use default skip value which can be changed by %_
852 skip_input_white_space = f == fmt || default_skip_input_white_space;
854 /* Spaces in format request skipping input white space. */
855 if (is_white_space (cf))
857 skip_input_white_space = 1;
859 /* Multiple format spaces are equivalent to a single white
861 while (is_white_space (*++f))
866 /* %_ toggles whether or not to skip input white space. */
870 default_skip_input_white_space =
871 !default_skip_input_white_space;
873 /* For transition from skip to no-skip in middle of format
874 string, skip input white space. For example, the following matches:
875 fmt = "%_%d.%d%_->%_%d.%d%_"
877 Without this the space after -> does not get skipped. */
878 if (!default_skip_input_white_space
879 && !(f == fmt + 2 || *f == 0))
880 unformat_skip_white_space (input);
883 /* %% means match % */
887 /* % at end of format string. */
897 n_input_white_space_skipped = 0;
898 if (skip_input_white_space)
899 n_input_white_space_skipped = unformat_skip_white_space (input);
901 /* End of format string. */
904 /* Force parse error when format string ends and input is
905 not white or at end. As an example, this is to prevent
906 format "foo" from matching input "food".
907 The last_non_white_space_match_percent is to make
908 "foo %d" match input "foo 10,bletch" with %d matching 10. */
909 if (skip_input_white_space
910 && !last_non_white_space_match_percent
911 && !last_non_white_space_match_format
912 && n_input_white_space_skipped == 0
913 && input->index != UNFORMAT_END_OF_INPUT)
918 last_non_white_space_match_percent = is_percent;
919 last_non_white_space_match_format = 0;
921 /* Explicit spaces in format must match input white space. */
922 if (cf == ' ' && !default_skip_input_white_space)
924 if (n_input_white_space_skipped == 0)
930 if (!(f = do_percent (input, va, f)))
936 const char *g = match_input_with_format (input, f);
939 last_non_white_space_match_format = g > f;
944 input_matches_format = 1;
947 /* Rewind buffer marks. */
949 uword l = vec_len (input->buffer_marks);
951 /* If we did not match back up buffer to last mark. */
952 if (!input_matches_format)
953 input->index = input->buffer_marks[l - 1];
955 _vec_len (input->buffer_marks) = l - 1;
958 return input_matches_format;
962 unformat (unformat_input_t * input, const char *fmt, ...)
967 result = va_unformat (input, fmt, &va);
973 unformat_user (unformat_input_t * input, unformat_function_t * func, ...)
978 /* Save place in input buffer in case parse fails. */
979 l = vec_len (input->buffer_marks);
980 vec_add1_aligned (input->buffer_marks, input->index,
981 sizeof (input->buffer_marks[0]));
984 result = func (input, &va);
987 if (!result && input->index != UNFORMAT_END_OF_INPUT)
988 input->index = input->buffer_marks[l];
990 _vec_len (input->buffer_marks) = l;
995 /* Setup for unformat of Unix style command line. */
997 unformat_init_command_line (unformat_input_t * input, char *argv[])
1001 unformat_init (input, 0, 0);
1003 /* Concatenate argument strings with space in between. */
1004 for (i = 1; argv[i]; i++)
1006 vec_add (input->buffer, argv[i], strlen (argv[i]));
1008 vec_add1 (input->buffer, ' ');
1013 unformat_init_string (unformat_input_t *input, const char *string,
1016 unformat_init (input, 0, 0);
1018 vec_add (input->buffer, string, string_len);
1022 unformat_init_vector (unformat_input_t * input, u8 * vector_string)
1024 unformat_init (input, 0, 0);
1025 input->buffer = vector_string;
1031 clib_file_fill_buffer (unformat_input_t * input)
1033 int fd = pointer_to_uword (input->fill_buffer_arg);
1036 l = vec_len (input->buffer);
1037 vec_resize (input->buffer, 4096);
1038 n = read (fd, input->buffer + l, 4096);
1040 _vec_len (input->buffer) = l + n;
1043 return UNFORMAT_END_OF_INPUT;
1045 return input->index;
1049 unformat_init_clib_file (unformat_input_t * input, int file_descriptor)
1051 unformat_init (input, clib_file_fill_buffer,
1052 uword_to_pointer (file_descriptor, void *));
1055 /* Take input from Unix environment variable. */
1057 unformat_init_unix_env (unformat_input_t * input, char *var)
1059 char *val = getenv (var);
1061 unformat_init_string (input, val, strlen (val));
1066 unformat_data_size (unformat_input_t * input, va_list * args)
1069 u64 *a = va_arg (*args, u64 *);
1070 if (unformat (input, "%lluGb", &_a))
1072 else if (unformat (input, "%lluG", &_a))
1074 else if (unformat (input, "%lluMb", &_a))
1076 else if (unformat (input, "%lluM", &_a))
1078 else if (unformat (input, "%lluKb", &_a))
1080 else if (unformat (input, "%lluK", &_a))
1082 else if (unformat (input, "%llu", a))
1089 #endif /* CLIB_UNIX */
1093 * fd.io coding-style-patch-verification: ON
1096 * eval: (c-set-style "gnu")