/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*------------------------------------------------------------------ * format.c -- see notice below * * October 2003, Eliot Dresselhaus * * Modifications to this file Copyright (c) 2003 by cisco Systems, Inc. * All rights reserved. *------------------------------------------------------------------ */ /* Copyright (c) 2001, 2002, 2003, 2006 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include /* va_start, etc */ #ifdef CLIB_UNIX #include #include #endif #ifdef CLIB_STANDALONE #include #endif #include #include #include #include #include #include /* os_puts */ #include typedef struct { /* Output number in this base. */ u8 base; /* Number of show of 64 bit number. */ u8 n_bits; /* Signed or unsigned. */ u8 is_signed; /* Output digits uppercase (not lowercase) %X versus %x. */ u8 uppercase_digits; } format_integer_options_t; static u8 *format_integer (u8 * s, u64 number, format_integer_options_t * options); static u8 *format_float (u8 * s, f64 x, uword n_digits_to_print, uword output_style); typedef struct { /* String justification: + => right, - => left, = => center. */ uword justify; /* Width of string (before and after decimal point for numbers). 0 => natural width. */ uword width[2]; /* Long => 'l', long long 'L', int 0. */ uword how_long; /* Pad character. Defaults to space. */ uword pad_char; } format_info_t; static u8 * justify (u8 * s, format_info_t * fi, uword s_len_orig) { uword i0, l0, l1; i0 = s_len_orig; l0 = i0 + fi->width[0]; l1 = vec_len (s); /* If width is zero user returned width. */ if (l0 == i0) l0 = l1; if (l1 > l0) _vec_len (s) = l0; else if (l0 > l1) { uword n = l0 - l1; uword n_left = 0, n_right = 0; switch (fi->justify) { case '-': n_right = n; break; case '+': n_left = n; break; case '=': n_right = n_left = n / 2; if (n % 2) n_left++; break; } if (n_left > 0) { vec_insert (s, n_left, i0); memset (s + i0, fi->pad_char, n_left); l1 = vec_len (s); } if (n_right > 0) { vec_resize (s, n_right); memset (s + l1, fi->pad_char, n_right); } } return s; } static const u8 * do_percent (u8 ** _s, const u8 * fmt, va_list * va) { u8 *s = *_s; uword c; const u8 *f = fmt; format_info_t fi = { .justify = '+', .width = {0}, .pad_char = ' ', .how_long = 0, }; uword i; ASSERT (f[0] == '%'); switch (c = *++f) { case '%': /* %% => % */ vec_add1 (s, c); f++; goto done; case '-': case '+': case '=': fi.justify = c; c = *++f; break; } /* Parse width0 . width1. */ { uword is_first_digit = 1; fi.width[0] = fi.width[1] = 0; for (i = 0; i < 2; i++) { if (c == '0' && i == 0 && is_first_digit) fi.pad_char = '0'; is_first_digit = 0; if (c == '*') { fi.width[i] = va_arg (*va, int); c = *++f; } else { while (c >= '0' && c <= '9') { fi.width[i] = 10 * fi.width[i] + (c - '0'); c = *++f; } } if (c != '.') break; c = *++f; } } /* Parse %l* and %L* */ switch (c) { case 'w': /* word format. */ fi.how_long = 'w'; c = *++f; break; case 'L': case 'l': fi.how_long = c; c = *++f; if (c == 'l' && *f == 'l') { fi.how_long = 'L'; c = *++f; } break; } /* Finally we are ready for format letter. */ if (c != 0) { uword s_initial_len = vec_len (s); format_integer_options_t o = { .is_signed = 0, .base = 10, .n_bits = BITS (uword), .uppercase_digits = 0, }; f++; switch (c) { default: { /* Try to give a helpful error message. */ vec_free (s); s = format (s, "**** CLIB unknown format `%%%c' ****", c); goto done; } case 'c': vec_add1 (s, va_arg (*va, int)); break; case 'p': vec_add1 (s, '0'); vec_add1 (s, 'x'); o.is_signed = 0; o.n_bits = BITS (uword *); o.base = 16; o.uppercase_digits = 0; s = format_integer (s, pointer_to_uword (va_arg (*va, void *)), &o); break; case 'x': case 'X': case 'u': case 'd': { u64 number; o.base = 10; if (c == 'x' || c == 'X') o.base = 16; o.is_signed = c == 'd'; o.uppercase_digits = c == 'X'; switch (fi.how_long) { case 'L': number = va_arg (*va, unsigned long long); o.n_bits = BITS (unsigned long long); break; case 'l': number = va_arg (*va, long); o.n_bits = BITS (long); break; case 'w': number = va_arg (*va, word); o.n_bits = BITS (uword); break; default: number = va_arg (*va, int); o.n_bits = BITS (int); break; } s = format_integer (s, number, &o); } break; case 's': case 'S': { char *cstring = va_arg (*va, char *); uword len; if (!cstring) { cstring = "(nil)"; len = 5; } else if (fi.width[1] != 0) len = clib_min (strlen (cstring), fi.width[1]); else len = strlen (cstring); /* %S => format string as C identifier (replace _ with space). */ if (c == 'S') { for (i = 0; i < len; i++) vec_add1 (s, cstring[i] == '_' ? ' ' : cstring[i]); } else vec_add (s, cstring, len); } break; case 'v': { u8 *v = va_arg (*va, u8 *); uword len; if (fi.width[1] != 0) len = clib_min (vec_len (v), fi.width[1]); else len = vec_len (v); vec_add (s, v, len); } break; case 'f': case 'g': case 'e': /* Floating point. */ ASSERT (fi.how_long == 0 || fi.how_long == 'l'); s = format_float (s, va_arg (*va, double), fi.width[1], c); break; case 'U': /* User defined function. */ { typedef u8 *(user_func_t) (u8 * s, va_list * args); user_func_t *u = va_arg (*va, user_func_t *); s = (*u) (s, va); } break; } s = justify (s, &fi, s_initial_len); } done: *_s = s; return f; } u8 * va_format (u8 * s, const char *fmt, va_list * va) { const u8 *f = (u8 *) fmt, *g; u8 c; g = f; while (1) { c = *f; if (!c) break; if (c == '%') { if (f > g) vec_add (s, g, f - g); f = g = do_percent (&s, f, va); } else { f++; } } if (f > g) vec_add (s, g, f - g); return s; } u8 * format (u8 * s, const char *fmt, ...) { va_list va; va_start (va, fmt); s = va_format (s, fmt, &va); va_end (va); return s; }