vxlan: vxlan/vxlan.api API cleanup
[vpp.git] / src / vppinfra / unformat.c
index ac8b7dd..3c67113 100644 (file)
@@ -167,7 +167,7 @@ di (unformat_input_t * i)
 #endif
 
 /* Parse delimited vector string.  If string starts with { then string
-   is delimited by balenced parenthesis.  Other string is delimited by
+   is delimited by balanced parenthesis.  Other string is delimited by
    white space.  {} were chosen since they are special to the shell. */
 static uword
 unformat_string (unformat_input_t * input,
@@ -306,6 +306,11 @@ unformat_hex_string (unformat_input_t * input, va_list * va)
       vec_free (s);
       return 0;
     }
+  /* Make sure something was processed. */
+  else if (s == 0)
+    {
+      return 0;
+    }
 
   *hexstring_return = s;
   return 1;
@@ -330,11 +335,17 @@ unformat_token (unformat_input_t * input, va_list * va)
   if (!token_chars)
     token_chars = (u8 *) "a-zA-Z0-9_";
 
-  memset (map, 0, sizeof (map));
+  clib_memset (map, 0, sizeof (map));
   for (s = token_chars; *s;)
     {
-      /* Parse range. */
-      if (s[0] < s[2] && s[1] == '-')
+      /*
+       * Parse range.
+       * The test order is important: s[1] is valid because s[0] != '\0' but
+       * s[2] might not if s[1] == '\0'
+       * Also, if s[1] == '-' but s[2] == '\0' the test s[0] < s[2] will
+       * (correctly) fail
+       */
+      if (s[1] == '-' && s[0] < s[2])
        {
          for (i = s[0]; i <= s[2]; i++)
            map[i] = 1;
@@ -396,7 +407,7 @@ unformat_line (unformat_input_t * i, va_list * va)
     }
 
   *result = line;
-  return 1;
+  return vec_len (line);
 }
 
 /* Parse a line ending with \n and return it as an unformat_input_t. */
@@ -405,7 +416,8 @@ unformat_line_input (unformat_input_t * i, va_list * va)
 {
   unformat_input_t *result = va_arg (*va, unformat_input_t *);
   u8 *line;
-  unformat_user (i, unformat_line, &line);
+  if (!unformat_user (i, unformat_line, &line))
+    return 0;
   unformat_init_vector (result, line);
   return 1;
 }
@@ -570,9 +582,9 @@ unformat_float (unformat_input_t * input, va_list * va)
   uword signs[2], sign_index = 0;
   uword n_input = 0;
 
-  memset (values, 0, sizeof (values));
-  memset (n_digits, 0, sizeof (n_digits));
-  memset (signs, 0, sizeof (signs));
+  clib_memset (values, 0, sizeof (values));
+  clib_memset (n_digits, 0, sizeof (n_digits));
+  clib_memset (signs, 0, sizeof (signs));
 
   while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
     {
@@ -681,8 +693,8 @@ error:
   return 0;
 }
 
-static char *
-match_input_with_format (unformat_input_t * input, char *f)
+static const char *
+match_input_with_format (unformat_input_t * input, const char *f)
 {
   uword cf, ci;
 
@@ -703,8 +715,8 @@ match_input_with_format (unformat_input_t * input, char *f)
   return f;
 }
 
-static char *
-do_percent (unformat_input_t * input, va_list * va, char *f)
+static const char *
+do_percent (unformat_input_t * input, va_list * va, const char *f)
 {
   uword cf, n, data_bytes = ~0;
 
@@ -824,9 +836,9 @@ unformat_skip_white_space (unformat_input_t * input)
 }
 
 uword
-va_unformat (unformat_input_t * input, char *fmt, va_list * va)
+va_unformat (unformat_input_t * input, const char *fmt, va_list * va)
 {
-  char *f;
+  const char *f;
   uword input_matches_format;
   uword default_skip_input_white_space;
   uword n_input_white_space_skipped;
@@ -937,7 +949,7 @@ va_unformat (unformat_input_t * input, char *fmt, va_list * va)
 
       else
        {
-         char *g = match_input_with_format (input, f);
+         const char *g = match_input_with_format (input, f);
          if (!g)
            goto parse_fail;
          last_non_white_space_match_format = g > f;
@@ -963,7 +975,7 @@ parse_fail:
 }
 
 uword
-unformat (unformat_input_t * input, char *fmt, ...)
+unformat (unformat_input_t * input, const char *fmt, ...)
 {
   va_list va;
   uword result;
@@ -1031,7 +1043,7 @@ unformat_init_vector (unformat_input_t * input, u8 * vector_string)
 #ifdef CLIB_UNIX
 
 static uword
-unix_file_fill_buffer (unformat_input_t * input)
+clib_file_fill_buffer (unformat_input_t * input)
 {
   int fd = pointer_to_uword (input->fill_buffer_arg);
   uword l, n;
@@ -1049,9 +1061,9 @@ unix_file_fill_buffer (unformat_input_t * input)
 }
 
 void
-unformat_init_unix_file (unformat_input_t * input, int file_descriptor)
+unformat_init_clib_file (unformat_input_t * input, int file_descriptor)
 {
-  unformat_init (input, unix_file_fill_buffer,
+  unformat_init (input, clib_file_fill_buffer,
                 uword_to_pointer (file_descriptor, void *));
 }
 
@@ -1065,6 +1077,30 @@ unformat_init_unix_env (unformat_input_t * input, char *var)
   return val != 0;
 }
 
+uword
+unformat_data_size (unformat_input_t * input, va_list * args)
+{
+  u64 _a;
+  u64 *a = va_arg (*args, u64 *);
+  if (unformat (input, "%lluGb", &_a))
+    *a = _a << 30;
+  else if (unformat (input, "%lluG", &_a))
+    *a = _a << 30;
+  else if (unformat (input, "%lluMb", &_a))
+    *a = _a << 20;
+  else if (unformat (input, "%lluM", &_a))
+    *a = _a << 20;
+  else if (unformat (input, "%lluKb", &_a))
+    *a = _a << 10;
+  else if (unformat (input, "%lluK", &_a))
+    *a = _a << 10;
+  else if (unformat (input, "%llu", a))
+    ;
+  else
+    return 0;
+  return 1;
+}
+
 #endif /* CLIB_UNIX */