-#include <assert.h>
-#include <stdint.h>
-#include <errno.h>
-
-#define __PRINTF_INFO_NO_BITFIELD
-#include <printf.h>
-
-#ifdef __STDIO_THREADSAFE
-#include <stdio_ext.h>
-#include <pthread.h>
-#endif /* __STDIO_THREADSAFE */
-
-/**********************************************************************/
-
-/* These provide some control over printf's feature set */
-#define __STDIO_PRINTF_FLOAT
-#define __STDIO_PRINTF_M_SUPPORT
-
-
-/**********************************************************************/
-
-#if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_FLOATS__)
-#undef __STDIO_PRINTF_FLOAT
-#endif
-
-#ifdef __BCC__
-#undef __STDIO_PRINTF_FLOAT
-#endif
-
-#ifndef __STDIO_PRINTF_FLOAT
-#undef L__dtostr
-#endif
-
-/**********************************************************************/
-
-#define __STDIO_GLIBC_CUSTOM_PRINTF
-
-/* TODO -- move these to a configuration section? */
-#define MAX_FIELD_WIDTH 4095
-#define MAX_USER_SPEC 10
-#define MAX_POS_ARGS 10
-
-/* TODO - fix the defs below */
-#define MAX_ARGS_PER_SPEC (MAX_POS_ARGS-2)
-
-#if MAX_ARGS_PER_SPEC + 2 > MAX_POS_ARGS
-#define MAX_ARGS MAX_ARGS_PER_SPEC + 2
-#else
-#define MAX_ARGS MAX_POS_ARGS
-#endif
-
-/**********************************************************************/
-/* Deal with pre-C99 compilers. */
-
-#ifndef va_copy
-
-#ifdef __va_copy
-#define va_copy(A,B) __va_copy(A,B)
-#else
- /* TODO -- maybe create a bits/vacopy.h for arch specific versions
- * to ensure we get the right behavior? Either that or fall back
- * on the portable (but costly in size) method of using a va_list *.
- * That means a pointer derefs in the va_arg() invocations... */
-#warning neither va_copy or __va_copy is defined. using a simple copy instead...
- /* the glibc manual suggests that this will usually suffice when
- __va_copy doesn't exist. */
-#define va_copy(A,B) A = B
-#endif
-
-#endif /* va_copy */
-
-/**********************************************************************/
-
-#define __PA_FLAG_INTMASK \
- (__PA_FLAG_CHAR|PA_FLAG_SHORT|__PA_FLAG_INT|PA_FLAG_LONG|PA_FLAG_LONG_LONG)
-
-extern printf_function _custom_printf_handler[MAX_USER_SPEC];
-extern printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
-extern char *_custom_printf_spec;
-
-/**********************************************************************/
-
-#define SPEC_FLAGS " +0-#'I"
-enum {
- FLAG_SPACE = 0x01,
- FLAG_PLUS = 0x02, /* must be 2 * FLAG_SPACE */
- FLAG_ZERO = 0x04,
- FLAG_MINUS = 0x08, /* must be 2 * FLAG_ZERO */
- FLAG_HASH = 0x10,
- FLAG_THOUSANDS = 0x20,
- FLAG_I18N = 0x40 /* only works for d, i, u */
-};
-
-/**********************************************************************/
-
-/* float layout 01234567890123456789 TODO: B?*/
-#define SPEC_CHARS "npxXoudifFeEgGaACScs"
-enum {
- CONV_n = 0,
- CONV_p,
- CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
- CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
- CONV_C, CONV_S, CONV_c, CONV_s,
-#ifdef __STDIO_PRINTF_M_SUPPORT
- CONV_m,
-#endif
- CONV_custom0 /* must be last */
-};
-
-/* p x X o u d i */
-#define SPEC_BASE { 16, 16, 16, 8, 10, 10, 10 }
-
-#define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
- CONV_C, CONV_S, CONV_c, CONV_s, CONV_custom0 }
-
-#define SPEC_OR_MASK { \
- /* n */ (PA_FLAG_PTR|PA_INT), \
- /* p */ PA_POINTER, \
- /* oxXudi */ PA_INT, \
- /* fFeEgGaA */ PA_DOUBLE, \
- /* C */ PA_WCHAR, \
- /* S */ PA_WSTRING, \
- /* c */ PA_CHAR, \
- /* s */ PA_STRING, \
-}
-
-#define SPEC_AND_MASK { \
- /* n */ (PA_FLAG_PTR|__PA_INTMASK), \
- /* p */ PA_POINTER, \
- /* oxXudi */ (__PA_INTMASK), \
- /* fFeEgGaA */ (PA_FLAG_LONG_DOUBLE|PA_DOUBLE), \
- /* C */ (PA_WCHAR), \
- /* S */ (PA_WSTRING), \
- /* c */ (PA_CHAR), \
- /* s */ (PA_STRING), \
-}
-
-/**********************************************************************/
-/*
- * In order to ease translation to what arginfo and _print_info._flags expect,
- * we map: 0:int 1:char 2:longlong 4:long 8:short
- * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
- */
-
-/* TODO -- Fix the table below to take into account stdint.h. */
-/* #ifndef LLONG_MAX */
-/* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
-/* #else */
-/* #if LLONG_MAX != INTMAX_MAX */
-/* #error fix QUAL_CHARS intmax_t entry 'j'! */
-/* #endif */
-/* #endif */
-
-#ifdef PDS
-#error PDS already defined!
-#endif
-#ifdef SS
-#error SS already defined!
-#endif
-#ifdef IMS
-#error IMS already defined!
-#endif
-
-#if PTRDIFF_MAX == INT_MAX
-#define PDS 0
-#elif PTRDIFF_MAX == LONG_MAX
-#define PDS 4
-#elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
-#define PDS 8
-#else
-#error fix QUAL_CHARS ptrdiff_t entry 't'!
-#endif
-
-#if SIZE_MAX == UINT_MAX
-#define SS 0
-#elif SIZE_MAX == ULONG_MAX
-#define SS 4
-#elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
-#define SS 8
-#else
-#error fix QUAL_CHARS size_t entries 'z', 'Z'!
-#endif
-
-#if INTMAX_MAX == INT_MAX
-#define IMS 0
-#elif INTMAX_MAX == LONG_MAX
-#define IMS 4
-#elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
-#define IMS 8
-#else
-#error fix QUAL_CHARS ptrdiff_t entry 't'!
-#endif
-
-#define QUAL_CHARS { \
- /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \
- /* q:long_long Z:(s)size_t */ \
- 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \
- 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\
- 1, 8 \
-}
-
-/**********************************************************************/
-
-#ifdef __STDIO_VA_ARG_PTR
-#ifdef __BCC__
-#define __va_arg_ptr(ap,type) (((type *)(ap += sizeof(type))) - 1)
-#endif
-
-#if 1
-#ifdef __GNUC__
-/* TODO -- need other than for 386 as well! */
-
-#ifndef __va_rounded_size
-#define __va_rounded_size(TYPE) \
- (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
-#endif
-#define __va_arg_ptr(AP, TYPE) \
- (AP = (va_list) ((char *) (AP) + __va_rounded_size (TYPE)), \
- ((void *) ((char *) (AP) - __va_rounded_size (TYPE))))
-#endif
-#endif
-#endif /* __STDIO_VA_ARG_PTR */
-
-#ifdef __va_arg_ptr
-#define GET_VA_ARG(AP,F,TYPE,ARGS) (*(AP) = __va_arg_ptr(ARGS,TYPE))
-#define GET_ARG_VALUE(AP,F,TYPE) (*((TYPE *)(*(AP))))
-#else
-typedef union {
- wchar_t wc;
- unsigned int u;
- unsigned long ul;
-#ifdef ULLONG_MAX
- unsigned long long ull;
-#endif
-#ifdef __STDIO_PRINTF_FLOAT
- double d;
- long double ld;
-#endif /* __STDIO_PRINTF_FLOAT */
- void *p;
-} argvalue_t;
-
-#define GET_VA_ARG(AU,F,TYPE,ARGS) (AU->F = va_arg(ARGS,TYPE))
-#define GET_ARG_VALUE(AU,F,TYPE) ((TYPE)((AU)->F))
-#endif
-
-typedef struct {
- const char *fmtpos; /* TODO: move below struct?? */
- struct printf_info info;
- int maxposarg; /* > 0 if args are positional, 0 if not, -1 if unknown */
- int num_data_args; /* TODO: use sentinal??? */
- unsigned int conv_num;
- unsigned char argnumber[4]; /* width | prec | 1st data | unused */
- int argtype[MAX_ARGS];
- va_list arg;
-#ifdef __va_arg_ptr
- void *argptr[MAX_ARGS];
-#else
- /* While this is wasteful of space in the case where pos args aren't
- * enabled, it is also needed to support custom printf handlers. */
- argvalue_t argvalue[MAX_ARGS];
-#endif
-} ppfs_t; /* parse printf format state */
-
-/**********************************************************************/
-
-/* TODO: fix printf to return 0 and set errno if format error. Standard says
- only returns -1 if sets error indicator for the stream. */
-
-#ifdef __STDIO_PRINTF_FLOAT
-extern size_t _dtostr(FILE * fp, long double x, struct printf_info *info);
-#endif
-
-#define _outnstr(stream, string, len) _stdio_fwrite(s, len, stream) /* TODO */
-
-extern int _do_one_spec(FILE * __restrict stream, ppfs_t *ppfs, int *count);
-
-extern int _ppfs_init(ppfs_t *ppfs, const char *fmt0); /* validates */
-extern void _ppfs_prepargs(ppfs_t *ppfs, va_list arg); /* sets posargptrs */
-extern void _ppfs_setargs(ppfs_t *ppfs); /* sets argptrs for current spec */
-extern int _ppfs_parsespec(ppfs_t *ppfs); /* parses specifier */
-
-/**********************************************************************/
-#ifdef L_parse_printf_format
-
-/* NOTE: This function differs from the glibc version in that parsing stops
- * upon encountering an invalid conversion specifier. Since this is the way
- * my printf functions work, I think it makes sense to do it that way here.
- * Unfortunately, since glibc sets the return type as size_t, we have no way
- * of returning that the template is illegal, other than returning 0.
- */
-
-size_t parse_printf_format(register const char *template,
- size_t n, register int *argtypes)
-{
- ppfs_t ppfs;
- size_t i;
- size_t count = 0;
-
- if (_ppfs_init(&ppfs, template) >= 0) {
- if (ppfs.maxposarg > 0) { /* Using positional args. */
- count = ppfs.maxposarg;
- if (n > count) {
- n = count;
- }
- for (i = 0 ; i < n ; i++) {
- *argtypes++ = ppfs.argtype[i];
- }
- } else { /* Not using positional args. */
- while (*template) {
- if ((*template == '%') && (*++template != '%')) {
- ppfs.fmtpos = template;
- _ppfs_parsespec(&ppfs); /* Can't fail. */
- template = ppfs.fmtpos; /* Update to one past spec end. */
- if (ppfs.info.width == INT_MIN) {
- ++count;
- if (n > 0) {
- *argtypes++ = PA_INT;
- --n;
- }
- }
- if (ppfs.info.prec == INT_MIN) {
- ++count;
- if (n > 0) {
- *argtypes++ = PA_INT;
- --n;
- }
- }
- for (i = 0 ; i < ppfs.num_data_args ; i++) {
- if ((ppfs.argtype[i]) != __PA_NOARG) {
- ++count;
- if (n > 0) {
- *argtypes++ = ppfs.argtype[i];
- --n;
- }
- }
- }
- } else {
- ++template;
- }
- }
- }
- }
-
- return count;
-}
-
-#endif
-/**********************************************************************/
-#ifdef L_vfprintf
-
-/* We only support ascii digits (or their USC equivalent codes) in
- * precision and width settings in *printf (wide) format strings.
- * In other words, we don't currently support glibc's 'I' flag.
- * We do accept it, but it is currently ignored. */
-
-int vfprintf(FILE * __restrict stream, register const char * __restrict format,
- va_list arg)
-{
- ppfs_t ppfs;
- int count, r;
- register const char *s;
-
- __STDIO_THREADLOCK(stream);
-
- count = 0;
- s = format;
-
- if (_ppfs_init(&ppfs, format) < 0) { /* Bad format string. */
- _outnstr(stream, format, strlen(format));
- count = -1;
- } else {
- _ppfs_prepargs(&ppfs, arg); /* This did a va_copy!!! */
-
- do {
- while (*format && (*format != '%')) {
- ++format;
- }
-
- if (format-s) { /* output any literal text in format string */
- if ( (r = _outnstr(stream, s, format-s)) < 0) {
- count = -1;
- break;
- }
- count += r;
- }
-
- if (!*format) { /* we're done */
- break;
- }
-
- if (format[1] != '%') { /* if we get here, *format == '%' */
- /* TODO: _do_one_spec needs to know what the output funcs are!!! */
- ppfs.fmtpos = ++format;
- /* TODO: check -- should only fail on stream error */
- if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
- count = -1;
- break;
- }
- s = format = ppfs.fmtpos;
- } else { /* %% means literal %, so start new string */
- s = ++format;
- ++format;
- }
- } while (1);
-
- va_end(ppfs.arg); /* Need to clean up after va_copy! */
- }
-
- __STDIO_THREADUNLOCK(stream);
-
- return count;
-}
-#endif
-/**********************************************************************/
-#ifdef L__ppfs_init
-
-int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
-{
- int r;
-
- /* First, zero out everything... argnumber[], argtype[], argptr[] */
- memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
- --ppfs->maxposarg; /* set to -1 */
- /* now set all argtypes to no-arg */
- {
-#if 1
- /* TODO - use memset here since already "paid for"? */
- register int *p = ppfs->argtype;
-
- r = MAX_ARGS;
- do {
- *p++ = __PA_NOARG;
- } while (--r);
-#else
- /* TODO -- get rid of this?? */
- register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
-
- do {
- *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
- p -= sizeof(int);
- } while (p);
-#endif
- }
-
- /*
- * Run through the entire format string to validate it and initialize
- * the positional arg numbers (if any).
- */
- {
- register const char *fmt = fmt0;
-
- while (*fmt) {
- if ((*fmt == '%') && (*++fmt != '%')) {
- ppfs->fmtpos = fmt; /* back up to the '%' */
- if ((r = _ppfs_parsespec(ppfs)) < 0) {
- return -1;
- }
- fmt = ppfs->fmtpos; /* update to one past end of spec */
- } else {
- ++fmt;
- }
- }
- ppfs->fmtpos = fmt0; /* rewind */
- }
-
- /* If we have positional args, make sure we know all the types. */
- {
- register int *p = ppfs->argtype;
- r = ppfs->maxposarg;
- while (--r >= 0) {
- if ( *p == __PA_NOARG ) { /* missing arg type!!! */
- return -1;
- }
- ++p;
- }
- }
-
- return 0;
-}
-#endif
-/**********************************************************************/
-#ifdef L__ppfs_prepargs
-void _ppfs_prepargs(register ppfs_t *ppfs, va_list arg)
-{
- int i;
-
- va_copy(ppfs->arg, arg);
-
- if ((i = ppfs->maxposarg) > 0) { /* init for positional args */
- ppfs->num_data_args = i;
- ppfs->info.width = ppfs->info.prec = ppfs->maxposarg = 0;
- _ppfs_setargs(ppfs);
- ppfs->maxposarg = i;
- }
-}
-#endif
-/**********************************************************************/
-#ifdef L__ppfs_setargs
-
-void _ppfs_setargs(register ppfs_t *ppfs)
-{
-#ifdef __va_arg_ptr
- register void **p = ppfs->argptr;
-#else
- register argvalue_t *p = ppfs->argvalue;
-#endif
- int i;
-
- if (ppfs->maxposarg == 0) { /* initing for or no pos args */
- if (ppfs->info.width == INT_MIN) {
- ppfs->info.width =
-#ifdef __va_arg_ptr
- *(int *)
-#endif
- GET_VA_ARG(p,u,unsigned int,ppfs->arg);
- }
- if (ppfs->info.prec == INT_MIN) {
- ppfs->info.prec =
-#ifdef __va_arg_ptr
- *(int *)
-#endif
- GET_VA_ARG(p,u,unsigned int,ppfs->arg);
- }
- i = 0;
- while (i < ppfs->num_data_args) {
- switch(ppfs->argtype[i++]) {
- case (PA_INT|PA_FLAG_LONG_LONG):
-#ifdef ULLONG_MAX
- GET_VA_ARG(p,ull,unsigned long long,ppfs->arg);
- break;
-#endif
- case (PA_INT|PA_FLAG_LONG):
-#if ULONG_MAX != UINT_MAX
- GET_VA_ARG(p,ul,unsigned long,ppfs->arg);
- break;
-#endif
- case PA_CHAR: /* TODO - be careful */
- /* ... users could use above and really want below!! */
- case (PA_INT|__PA_FLAG_CHAR):/* TODO -- translate this!!! */
- case (PA_INT|PA_FLAG_SHORT):
- case PA_INT:
- GET_VA_ARG(p,u,unsigned int,ppfs->arg);
- break;
- case PA_WCHAR: /* TODO -- assume int? */
- /* we're assuming wchar_t is at least an int */
- GET_VA_ARG(p,wc,wchar_t,ppfs->arg);
- break;
-#ifdef __STDIO_PRINTF_FLOAT
- /* PA_FLOAT */
- case PA_DOUBLE:
- GET_VA_ARG(p,d,double,ppfs->arg);
- break;
- case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
- GET_VA_ARG(p,ld,long double,ppfs->arg);
- break;
-#else /* __STDIO_PRINTF_FLOAT */
- case PA_DOUBLE:
- case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
- assert(0);
- continue;
-#endif /* __STDIO_PRINTF_FLOAT */
- default:
- /* TODO -- really need to ensure this can't happen */
- assert(ppfs->argtype[i-1] & PA_FLAG_PTR);
- case PA_POINTER:
- case PA_STRING:
- case PA_WSTRING:
- GET_VA_ARG(p,p,void *,ppfs->arg);
- break;
- case __PA_NOARG:
- continue;
- }
- ++p;
- }
- } else {
- if (ppfs->info.width == INT_MIN) {
- ppfs->info.width
- = (int) GET_ARG_VALUE(p + ppfs->argnumber[0] - 1,u,unsigned int);
- }
- if (ppfs->info.prec == INT_MIN) {
- ppfs->info.prec
- = (int) GET_ARG_VALUE(p + ppfs->argnumber[1] - 1,u,unsigned int);
- }
- }
-
- /* Now we know the width and precision. */
- if (ppfs->info.width < 0) {
- ppfs->info.width = -ppfs->info.width;
- PRINT_INFO_SET_FLAG(&(ppfs->info),left);
- PRINT_INFO_CLR_FLAG(&(ppfs->info),space);
- ppfs->info.pad = ' ';
- }
-#if 0
- /* NOTE -- keep neg for now so float knows! */
- if (ppfs->info.prec < 0) { /* spec says treat as omitted. */
- /* so use default prec... 1 for everything but floats and strings. */
- ppfs->info.prec = 1;
- }
-#endif
-}
-#endif
-/**********************************************************************/
-#ifdef L__ppfs_parsespec
-
-/* Notes: argtype differs from glibc for the following:
- * mine glibc
- * lc PA_WCHAR PA_CHAR the standard says %lc means %C
- * ls PA_WSTRING PA_STRING the standard says %ls means %S
- * {*}n {*}|PA_FLAG_PTR PA_FLAG_PTR size of n can be qualified
- */
-
-/* TODO: store positions of positional args */
-
-/* TODO -- WARNING -- assumes aligned on integer boundaries!!! */
-
-/* TODO -- disable if not using positional args!!! */
-#define _OVERLAPPING_DIFFERENT_ARGS
-
-/* TODO -- rethink this -- perhaps we should set to largest type??? */
-
-#ifdef _OVERLAPPING_DIFFERENT_ARGS
-
-#define PROMOTED_SIZE_OF(X) ((sizeof(X) + sizeof(int) - 1) / sizeof(X))
-
-static const short int type_codes[] = {
- __PA_NOARG, /* must be first entry */
- PA_POINTER,
- PA_STRING,
- PA_WSTRING,
- PA_CHAR,
- PA_INT|PA_FLAG_SHORT,
- PA_INT,
- PA_INT|PA_FLAG_LONG,
- PA_INT|PA_FLAG_LONG_LONG,
- PA_WCHAR,
-#ifdef __STDIO_PRINTF_FLOAT
- /* PA_FLOAT, */
- PA_DOUBLE,
- PA_DOUBLE|PA_FLAG_LONG_DOUBLE,
-#endif /* __STDIO_PRINTF_FLOAT */
-};
-
-static const unsigned char type_sizes[] = {
- /* unknown type consumes no arg */
- 0, /* must be first entry */
- PROMOTED_SIZE_OF(void *),
- PROMOTED_SIZE_OF(char *),
- PROMOTED_SIZE_OF(wchar_t *),
- PROMOTED_SIZE_OF(char),
- PROMOTED_SIZE_OF(short),
- PROMOTED_SIZE_OF(int),
- PROMOTED_SIZE_OF(long),
-#ifdef ULLONG_MAX
- PROMOTED_SIZE_OF(long long),
-#else
- PROMOTED_SIZE_OF(long), /* TODO -- is this correct? (above too) */
-#endif
- PROMOTED_SIZE_OF(wchar_t),
-#ifdef __STDIO_PRINTF_FLOAT
- /* PROMOTED_SIZE_OF(float), */
- PROMOTED_SIZE_OF(double),
- PROMOTED_SIZE_OF(long double),
-#endif /* __STDIO_PRINTF_FLOAT */
-};
-
-static int _promoted_size(int argtype)
-{
- register const short int *p;
-
- /* note -- since any unrecognized type is treated as a pointer */
- p = type_codes + sizeof(type_codes)/sizeof(type_codes[0]);
- do {
- if (*--p == argtype) {
- break;
- }
- } while (p > type_codes);
-
- return type_sizes[(int)(p - type_codes)];
-}
-
-static int _is_equal_or_bigger_arg(int curtype, int newtype)
-{
- /* Quick test */
- if (newtype == __PA_NOARG) {
- return 0;
- }
- if ((curtype == __PA_NOARG) || (curtype == newtype)) {
- return 1;
- }
- /* Ok... slot is already filled and types are different in name. */
- /* So, compare promoted sizes of curtype and newtype args. */
- return _promoted_size(curtype) <= _promoted_size(newtype);
-}
-
-#else
-
-#define _is_equal_or_bigger_arg(C,N) (((C) == __PA_NOARG) || ((C) == (N)))
-
-#endif
-
-/* TODO - do this differently? */
-static char _bss_custom_printf_spec[MAX_USER_SPEC]; /* 0-init'd for us. */
-
-char *_custom_printf_spec = _bss_custom_printf_spec;
-printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
-
-extern int _ppfs_parsespec(ppfs_t *ppfs)
-{
- register const char *fmt;
- register const char *p;
- int preci;
- int width;
- int flags;
- int dataargtype;
- int i;
- int dpoint;
- int maxposarg;
- int p_m_spec_chars;
- int n;
- int argtype[MAX_ARGS_PER_SPEC+2];
- int argnumber[3]; /* width, precision, 1st data arg */
- unsigned int conv_num; /* This does not need to be initialized. */
- static const char spec_flags[] = SPEC_FLAGS;
- static const char spec_chars[] = SPEC_CHARS;/* TODO: b? */
- static const char spec_ranges[] = SPEC_RANGES;
- static const short spec_or_mask[] = SPEC_OR_MASK;
- static const short spec_and_mask[] = SPEC_AND_MASK;
- static const char qual_chars[] = QUAL_CHARS;
-
- /* WIDE note: we can test against '%' here since we don't allow */
- /* WIDE note: other mappings of '%' in the wide char set. */
- preci = -1;
- width = flags = dpoint = 0;
- argnumber[0] = 0;
- argnumber[1] = 0;
- argtype[0] = __PA_NOARG;
- argtype[1] = __PA_NOARG;
- maxposarg = ppfs->maxposarg;
- fmt = ppfs->fmtpos;
-
- assert(fmt[-1] == '%');
- assert(fmt[0] != '%');
-
- /* Process arg pos and/or flags and/or width and/or precision. */
- width_precision:
- p = fmt;
- if (*fmt == '*') {
- argtype[-dpoint] = PA_INT;
- ++fmt;
- }
- i = 0;
- while (__isdigit(*fmt)) {
- if (i < MAX_FIELD_WIDTH) { /* Avoid overflow. */
- i = (i * 10) + (*fmt - '0');
- }
- ++fmt;
- }
- if (p[-1] == '%') { /* Check for a position. */
-
- /* TODO: if val not in range, then error */
-
- if ((*fmt == '$') && (i > 0)) {/* Positional spec. */
- ++fmt;
- if (maxposarg == 0) {
- return -1;
- }
- if ((argnumber[2] = i) > maxposarg) {
- maxposarg = i;
- }
- /* Now fall through to check flags. */
- } else {
- if (maxposarg > 0) {
- return -1;
- }
- maxposarg = 0; /* Possible redundant store, but cuts size. */
-
- if ((fmt > p) && (*p != '0')) {
- goto PREC_WIDTH;
- }
-
- fmt = p; /* Back up for possible '0's flag. */
- /* Now fall through to check flags. */
- }
-
- restart_flags: /* Process flags. */
- i = 1;
- p = spec_flags;
-
- do {
- if (*fmt == *p++) {
- ++fmt;
- flags |= i;
- goto restart_flags;
- }
- i += i; /* Better than i <<= 1 for bcc */
- } while (*p);
- i = 0;
-
- /* If '+' then ignore ' ', and if '-' then ignore '0'. */
- /* Note: Need to ignore '0' when prec is an arg with val < 0, */
- /* but that test needs to wait until the arg is retrieved. */
- flags &= ~((flags & (FLAG_PLUS|FLAG_MINUS)) >> 1);
- /* Note: Ignore '0' when prec is specified < 0 too (in printf). */
-
- if (fmt[-1] != '%') { /* If we've done anything, loop for width. */
- goto width_precision;
- }
- }
- PREC_WIDTH:
- if (*p == '*') { /* Prec or width takes an arg. */
- if (maxposarg) {
- if ((*fmt++ != '$') || (i <= 0)) {
- /* Using pos args and no $ or invalid arg number. */
- return -1;
- }
- argnumber[-dpoint] = i;
- } else if (++p != fmt) {
- /* Not using pos args but digits followed *. */
- return -1;
- }
- i = INT_MIN;
- }
-
- if (!dpoint) {
- width = i;
- if (*fmt == '.') {
- ++fmt;
- dpoint = -1; /* To use as default precison. */
- goto width_precision;
- }
- } else {
- preci = i;
- }
-
- /* Process qualifier. */
- p = qual_chars;
- do {
- if (*fmt == *p) {
- ++fmt;
- break;
- }
- } while (*++p);
- if ((p - qual_chars < 2) && (*fmt == *p)) {
- p += ((sizeof(qual_chars)-2) / 2);
- ++fmt;
- }
- dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
-
- /* Process conversion specifier. */
- if (!*fmt) {
- return -1;
- }
-
- p = spec_chars;
-
- do {
- if (*fmt == *p) {
- p_m_spec_chars = p - spec_chars;
-
- if ((p_m_spec_chars >= CONV_c)
- && (dataargtype & PA_FLAG_LONG)) {
- p_m_spec_chars -= 2; /* lc -> C and ls -> S */
- }
-
- conv_num = p_m_spec_chars;
- p = spec_ranges-1;
- while (p_m_spec_chars > *++p) {}
-
- i = p - spec_ranges;
- argtype[2] = (dataargtype | spec_or_mask[i]) & spec_and_mask[i];
- p = spec_chars;
- break;
- }
- } while(*++p);
-
- ppfs->info.spec = *fmt;
- ppfs->info.prec = preci;
- ppfs->info.width = width;
- ppfs->info.pad = ((flags & FLAG_ZERO) ? '0' : ' ');
- ppfs->info._flags = (flags & ~FLAG_ZERO) | (dataargtype & __PA_INTMASK);
- ppfs->num_data_args = 1;
-
- if (!*p) {
-#ifdef __STDIO_GLIBC_CUSTOM_PRINTF
- /* TODO -- gnu %m support build option. */
-#ifdef __STDIO_PRINTF_M_SUPPORT
- if (*fmt == 'm') {
- conv_num = CONV_m;
- ppfs->num_data_args = 0;
- goto DONE;
- }
-#endif
-
- /* Handle custom arg -- WARNING -- overwrites p!!! */
- conv_num = CONV_custom0;
- p = _custom_printf_spec;
- do {
- if (*p == *fmt) {
- if ((ppfs->num_data_args
- = ((*_custom_printf_arginfo[(int)(p-_custom_printf_spec)])
- (&(ppfs->info), MAX_ARGS_PER_SPEC, argtype+2)))
- > MAX_ARGS_PER_SPEC) {
- break; /* Error -- too many args! */
- }
- goto DONE;
- }
- } while (++p < (_custom_printf_spec + MAX_USER_SPEC));
-#endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
- /* Otherwise error. */
- return -1;
- }
-
-#ifdef __STDIO_GLIBC_CUSTOM_PRINTF
- DONE:
-#endif
-
- if (maxposarg > 0) {
- i = 0;
- do {
- /* Update maxposarg and check that NL_ARGMAX is not exceeded. */
- n = ((i <= 2)
- ? (ppfs->argnumber[i] = argnumber[i])
- : argnumber[2] + (i-2));
- if (n > maxposarg) {
- if ((maxposarg = n) > NL_ARGMAX) {
- return -1;
- }
- }
- --n;
- /* Record argtype with largest size (current, new). */
- if (_is_equal_or_bigger_arg(ppfs->argtype[n], argtype[i])) {
- ppfs->argtype[n] = argtype[i];
- }
- } while (++i < ppfs->num_data_args + 2);
- } else {
- ppfs->argnumber[2] = 1;
- memcpy(ppfs->argtype, argtype + 2, ppfs->num_data_args * sizeof(int));
- }
-
- ppfs->maxposarg = maxposarg;
- ppfs->fmtpos = ++fmt;
- ppfs->conv_num = conv_num;
-
- return ppfs->num_data_args + 2;
-}
-
-#endif
-/**********************************************************************/
-#ifdef L_register_printf_function
-
-int register_printf_function(int spec, printf_function handler,
- printf_arginfo_function arginfo)
-{
- register char *r;
- register char *p;
-
- if (spec && (arginfo != NULL)) { /* TODO -- check if spec is valid char */
- r = NULL;
- p = _custom_printf_spec + MAX_USER_SPEC;
- do {
- --p;
- if (!*p) {
- r = p;
- }
-#ifdef __BCC__
- else /* bcc generates less code with fall-through */
-#endif
- if (*p == spec) {
- r = p;
- p = _custom_printf_spec;
- }
- } while (p > _custom_printf_spec);
-
- if (r) {
- if (handler) {
- *r = spec;
- _custom_printf_handler[(int)(r - p)] = handler;
- _custom_printf_arginfo[(int)(r - p)] = arginfo;
- } else {
- *r = 0;
- }
- return 0;
- }
- /* TODO -- if asked to unregister a non-existent spec, return what? */
- }
- return -1;
-}
-#endif
-/**********************************************************************/
-#ifdef L__do_one_spec
-
-printf_function _custom_printf_handler[MAX_USER_SPEC];
-
-extern void _store_inttype(void *dest, int desttype, uintmax_t val);
-extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
-extern void _charpad(FILE * __restrict stream, char padchar, size_t numpad);
-extern void output(FILE * __restrict stream, const char *s);
-
-#define output(F,S) fputs(S,F)
-
-#undef putc
-void _charpad(FILE * __restrict stream, char padchar, size_t numpad)
-{
- /* TODO -- Use a buffer to cut down on function calls... */
- char pad[1];
-
- *pad = padchar;
- while (numpad) {
- _stdio_fwrite(pad, 1, stream);
- --numpad;
- }
-}
-
-/* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
-
-int _do_one_spec(FILE * __restrict stream, register ppfs_t *ppfs, int *count)
-{
- static const char spec_base[] = SPEC_BASE;
- static const char prefix[] = "+\0-\0 \0000x\0000X";
- /* 0 2 4 6 9 11*/
- enum {
- PREFIX_PLUS = 0,
- PREFIX_MINUS = 2,
- PREFIX_SPACE = 4,
- PREFIX_LWR_X = 6,
- PREFIX_UPR_X = 9,
- PREFIX_NONE = 11
- };
-
-#ifdef __va_arg_ptr
- const void * const *argptr;
-#else
- const void * argptr[MAX_ARGS_PER_SPEC];
-#endif
- int *argtype;
- size_t slen;
- int base;
- int numpad;
- int alphacase;
- int numfill = 0; /* TODO: fix */
- int prefix_num = PREFIX_NONE;
- char padchar = ' ';
-#ifdef __UCLIBC_MJN3_ONLY__
-#warning REMINDER: buf size
-#endif
- /* TODO: buf needs to be big enough for any possible error return strings
- * and also for any locale-grouped long long integer strings generated.
- * This should be large enough for any of the current archs/locales, but
- * eventually this should be handled robustly. */
- char buf[128];
-
-#ifdef NDEBUG
- _ppfs_parsespec(ppfs);
-#else
- if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
- abort();
- }
-#endif
- _ppfs_setargs(ppfs);
-
- argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
- /* Deal with the argptr vs argvalue issue. */
-#ifdef __va_arg_ptr
- argptr = (const void * const *) ppfs->argptr;
- if (ppfs->maxposarg > 0) { /* Using positional args... */
- argptr += ppfs->argnumber[2] - 1;
- }
-#else
- /* Need to build a local copy... */
- {
- register argvalue_t *p = ppfs->argvalue;
- int i;
- if (ppfs->maxposarg > 0) { /* Using positional args... */
- p += ppfs->argnumber[2] - 1;
- }
- for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
- argptr[i] = (void *) p++;
- }
- }
-#endif
- {
- register char *s; /* TODO: Should s be unsigned char * ? */
-
- if (ppfs->conv_num == CONV_n) {
- _store_inttype(*(void **)*argptr,
- ppfs->info._flags & __PA_INTMASK,
- (intmax_t) (*count));
- return 0;
- }
- if (ppfs->conv_num <= CONV_i) { /* pointer or (un)signed int */
- alphacase = __UIM_LOWER;
- if (((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10)
- && (PRINT_INFO_FLAG_VAL(&(ppfs->info),group))
- ) {
- alphacase = __UIM_GROUP;
- }
- if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
- if (ppfs->conv_num == CONV_X) {
- alphacase = __UIM_UPPER;
- }
- if (ppfs->conv_num == CONV_p) { /* pointer */
- prefix_num = PREFIX_LWR_X;
- } else { /* unsigned int */
- }
- } else { /* signed int */
- base = -base;
- }
- if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
- padchar = ppfs->info.pad;
- }
- s = _uintmaxtostr(buf + sizeof(buf) - 1,
- (uintmax_t)
- _load_inttype(*argtype & __PA_INTMASK,
- *argptr, base), base, alphacase);
- if (ppfs->conv_num > CONV_u) { /* signed int */
- if (*s == '-') {
- PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
- ++s; /* handle '-' in the prefix string */
- prefix_num = PREFIX_MINUS;
- } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
- prefix_num = PREFIX_PLUS;
- } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
- prefix_num = PREFIX_SPACE;
- }
- }
- slen = (char *)(buf + sizeof(buf) - 1) - s;
- numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
- if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
- if (ppfs->conv_num <= CONV_x) { /* x or p */
- prefix_num = PREFIX_LWR_X;
- }
- if (ppfs->conv_num == CONV_X) {
- prefix_num = PREFIX_UPR_X;
- }
- if ((ppfs->conv_num == CONV_o) && (numfill <= slen)) {
- numfill = ((*s == '0') ? 1 : slen + 1);
- }
- }
- if (*s == '0') {
- if (prefix_num >= PREFIX_LWR_X) {
- prefix_num = PREFIX_NONE;
- }
- if (ppfs->conv_num == CONV_p) {/* null pointer */
- s = "(nil)";
- slen = 5;
- numfill = 0;
- } else if (numfill == 0) { /* if precision 0, no output */
- slen = 0;
- }
- }
- numfill = ((numfill > slen) ? numfill - slen : 0);
- } else if (ppfs->conv_num <= CONV_A) { /* floating point */
-#ifdef __STDIO_PRINTF_FLOAT
- *count += _dtostr(stream,
- (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
- ? *(long double *) *argptr
- : (long double) (* (double *) *argptr)),
- &ppfs->info);
- return 0;
-#else /* __STDIO_PRINTF_FLOAT */
- return -1; /* TODO -- try ton continue? */
-#endif /* __STDIO_PRINTF_FLOAT */
- } else if (ppfs->conv_num <= CONV_S) { /* wide char or string */
-#if 1
- return -1; /* TODO -- wide */
-#else
- if (ppfs->conv_num == CONV_S) { /* wide string */
-
- } else { /* wide char */
-
- }
-#endif
- } else if (ppfs->conv_num <= CONV_s) { /* char or string */
- if (ppfs->conv_num == CONV_s) { /* string */
- s = *((char **) (*argptr));
- if (s) {
- SET_STRING_LEN:
- slen = strnlen(s, ((ppfs->info.prec >= 0)
- ? ppfs->info.prec : SIZE_MAX));
- } else {
- s = "(null)";
- slen = 6;
- }
- } else { /* char */
- s = (char *) buf;
- *s = (unsigned char)(*((const int *) *argptr));
- s[1] = 0;
- slen = 1;
- }
-#ifdef __STDIO_PRINTF_M_SUPPORT
- } else if (ppfs->conv_num == CONV_m) {
- s = _glibc_strerror_r(errno, buf, sizeof(buf));
- goto SET_STRING_LEN;
-#endif
- } else {
- assert(ppfs->conv_num == CONV_custom0);
-
- s = _custom_printf_spec;
- do {
- if (*s == ppfs->info.spec) {
- int rv;
- /* TODO -- check return value for sanity? */
- rv = (*_custom_printf_handler
- [(int)(s-_custom_printf_spec)])
- (stream, &ppfs->info, argptr);
- if (rv < 0) {
- return -1;
- }
- *count += rv;
- return 0;
- }
- } while (++s < (_custom_printf_spec + MAX_USER_SPEC));
- assert(0);
- return -1;
- }
-
- {
- size_t t;
-
- t = slen + numfill;
- if (prefix_num != PREFIX_NONE) {
- t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
- }
- numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
- *count += t + numpad;
- }
- if (padchar == '0') { /* TODO: check this */
- numfill += numpad;
- numpad = 0;
- }
-
- /* Now handle the output itself. */
- if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
- _charpad(stream, ' ', numpad);
- numpad = 0;
- }
- output(stream, prefix + prefix_num);
- _charpad(stream, '0', numfill);
- _outnstr(stream, s, slen);
- _charpad(stream, ' ', numpad);
- }
-
- return 0;
-}
-#endif
-/**********************************************************************/
-#ifdef L_vsnprintf
-
-#ifdef __STDIO_BUFFERS
-int vsnprintf(char *__restrict buf, size_t size,
- const char * __restrict format, va_list arg)
-{
- FILE f;
- int rv;
-
-#ifdef __STDIO_GETC_MACRO
- f.bufgetc =
-#endif
- f.bufpos = f.bufread = f.bufstart = buf;
-
- if (size > SIZE_MAX - (size_t) buf) {
- size = SIZE_MAX - (size_t) buf;
- }
-#ifdef __STDIO_PUTC_MACRO
- f.bufputc =
-#endif
- f.bufend = buf + size;
-
-#if 0 /* shouldn't be necessary */
-/* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
- f.cookie = &(f.filedes);
- f.gcs.read = 0;
- f.gcs.write = 0;
- f.gcs.seek = 0;
- f.gcs.close = 0;
-#endif
- f.filedes = -2; /* for debugging */
- f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
-
-#ifdef __STDIO_MBSTATE
- __INIT_MBSTATE(&(f.state));
-#endif /* __STDIO_MBSTATE */
-
-#ifdef __STDIO_THREADSAFE
- f.user_locking = 0;
- __stdio_init_mutex(&f.lock);
-#endif
-
- rv = vfprintf(&f, format, arg);
- if (size) {
- if (f.bufpos == f.bufend) {
- --f.bufpos;
- }
- *f.bufpos = 0;
- }
- return rv;
-}
-#else /* __STDIO_BUFFERS */
-#ifdef __STDIO_GLIBC_CUSTOM_STREAMS
-
-typedef struct {
- size_t pos;
- size_t len;
- unsigned char *buf;
- FILE *fp;
-} __snpf_cookie;