diff options
Diffstat (limited to 'stdio/printf-prs.c')
-rw-r--r-- | stdio/printf-prs.c | 205 |
1 files changed, 33 insertions, 172 deletions
diff --git a/stdio/printf-prs.c b/stdio/printf-prs.c index 2f55dd3157..811a9cb7eb 100644 --- a/stdio/printf-prs.c +++ b/stdio/printf-prs.c @@ -16,196 +16,57 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <ansidecl.h> #include <stdio.h> #include <printf.h> -#include <limits.h> +#include <stdlib.h> #include <string.h> -#include <ctype.h> -#ifdef __GNUC__ -#define HAVE_LONGLONG -#endif +#include "printf-parse.h" -extern printf_arginfo_function *__printf_arginfo_table[]; size_t -DEFUN(parse_printf_format, (fmt, n, argtypes), - CONST char *fmt AND size_t n AND int *argtypes) +parse_printf_format (fmt, n, argtypes) + const char *fmt; + size_t n; + int *argtypes; { - register CONST char *f; - size_t need = 0; + size_t nargs; /* Number of arguments. */ + size_t max_ref_arg; /* Highest index used in a positional arg. */ + struct printf_spec spec; - for (f = strchr (fmt, '%'); f != NULL; f = strchr (f, '%')) - { - struct printf_info info; - printf_arginfo_function *arginfo; - - ++f; + nargs = 0; + max_ref_arg = 0; - info.space = info.showsign = info.left = info.alt = info.group = 0; - info.pad = ' '; - while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0' || - *f == '\'') - switch (*f++) - { - case ' ': - info.space = 1; - break; - case '+': - info.showsign = 1; - break; - case '-': - info.left = 1; - break; - case '#': - info.alt = 1; - break; - case '\'': - info.group = 1; - break; - case '0': - info.pad = '0'; - break; - } - if (info.left) - info.pad = ' '; + /* Search for format specifications. */ + for (fmt = find_spec (fmt); *fmt != '\0'; fmt = spec.next_fmt) + { + /* Parse this spec. */ + nargs += parse_one_spec (fmt, nargs, &spec, &max_ref_arg); - /* Get the field width. */ - if (*f == '*') - { - if (++need < n) - *argtypes++ = PA_INT; - info.width = INT_MIN; - ++f; - } - else - { - info.width = 0; - while (isdigit(*f)) - { - info.width *= 10; - info.width += *f++ - '0'; - } - } + /* If the width is determined by an argument this is an int. */ + if (spec.width_arg != -1 && spec.width_arg < n) + argtypes[spec.width_arg] = PA_INT; - /* Get the precision. */ - /* -1 means none given; 0 means explicit 0. */ - info.prec = -1; - if (*f == '.') - { - ++f; - if (*f == '*') - { - /* The precision is given in an argument. */ - if (++need < n) - *argtypes++ = PA_INT; - info.prec = INT_MIN; - ++f; - } - else if (isdigit(*f)) - { - info.prec = 0; - while (*f != '\0' && isdigit(*f)) - { - info.prec *= 10; - info.prec += *f++ - '0'; - } - } - } + /* If the precision is determined by an argument this is an int. */ + if (spec.prec_arg != -1 && spec.prec_arg < n) + argtypes[spec.prec_arg] = PA_INT; - /* Check for type modifiers. */ - info.is_short = info.is_long = info.is_long_double = 0; - while (*f == 'h' || *f == 'l' || *f == 'L') - switch (*f++) + if (spec.data_arg < n) + switch (spec.ndata_args) { - case 'h': - /* int's are short int's. */ - info.is_short = 1; + case 0: /* No arguments. */ break; - case 'l': -#ifdef HAVE_LONGLONG - if (info.is_long) - /* A double `l' is equivalent to an `L'. */ - info.is_long_double = 1; - else -#endif - /* int's are long int's. */ - info.is_long = 1; + case 1: /* One argument; we already have the type. */ + argtypes[spec.data_arg] = spec.data_arg_type; break; - case 'L': - /* double's are long double's, and int's are long long int's. */ - info.is_long_double = 1; + default: + /* We have more than one argument for this format spec. We must + call the arginfo function again to determine all the types. */ + (void) (*__printf_arginfo_table[spec.info.spec]) + (&spec.info, n - spec.data_arg, &argtypes[spec.data_arg]); break; } - - if (*f == '\0') - return need; - - info.spec = *f++; - - arginfo = __printf_arginfo_table[info.spec]; - if (arginfo != NULL) - { - size_t nargs - = (*arginfo) (&info, need > n ? 0 : n - need, argtypes); - need += nargs; - argtypes += nargs; - } - else - { - int type; - switch (info.spec) - { - case 'i': - case 'd': - case 'u': - case 'o': - case 'X': - case 'x': - type = PA_INT; - break; - - case 'e': - case 'E': - case 'f': - case 'g': - case 'G': - type = PA_DOUBLE; - break; - - case 'c': - type = PA_CHAR; - break; - - case 's': - type = PA_STRING; - break; - - case 'p': - type = PA_POINTER; - break; - - case 'n': - type = PA_INT | PA_FLAG_PTR; - break; - - default: - /* No arg for an unknown spec. */ - continue; - } - - if (info.is_long_double) - type |= PA_FLAG_LONG_DOUBLE; - if (info.is_long) - type |= PA_FLAG_LONG; - if (info.is_short) - type |= PA_FLAG_SHORT; - - if (++need < n) - *argtypes++ = type; - } } - return need; + return MAX (nargs, max_ref_arg); } |