-/*
- * Find all arguments when a positional parameter is encountered. Returns a
- * table, indexed by argument number, of pointers to each arguments. The
- * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
- * It will be replaces with a malloc-ed one if it overflows.
- */
-static void
-__find_arguments (const char *fmt0, va_list ap, union arg **argtable)
-{
- char *fmt; /* format string */
- int ch; /* character from fmt */
- int n, n2; /* handy integer (short term usage) */
- char *cp; /* handy char pointer (short term usage) */
- int flags; /* flags as above */
- int width; /* width from format (%8d), or 0 */
- enum typeid *typetable; /* table of types */
- enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
- int tablesize; /* current size of type table */
- int tablemax; /* largest used index in table */
- int nextarg; /* 1-based argument index */
-
- /*
- * Add an argument type to the table, expanding if necessary.
- */
-#define ADDTYPE(type) \
- ((nextarg >= tablesize) ? \
- __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
- (nextarg > tablemax) ? tablemax = nextarg : 0, \
- typetable[nextarg++] = type)
-
-#define ADDSARG() \
- ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
- ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
- ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
- ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
- ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
-
-#define ADDUARG() \
- ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
- ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
- ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
- ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
- ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
-
- /*
- * Add * arguments to the type array.
- */
-#define ADDASTER() \
- n2 = 0; \
- cp = fmt; \
- while (is_digit(*cp)) { \
- n2 = 10 * n2 + to_digit(*cp); \
- cp++; \
- } \
- if (*cp == '$') { \
- int hold = nextarg; \
- nextarg = n2; \
- ADDTYPE (T_INT); \
- nextarg = hold; \
- fmt = ++cp; \
- } else { \
- ADDTYPE (T_INT); \
- }
- fmt = (char *)fmt0;
- typetable = stattypetable;
- tablesize = STATIC_ARG_TBL_SIZE;
- tablemax = 0;
- nextarg = 1;
- memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
-
- /*
- * Scan the format for conversions (`%' character).
- */
- for (;;) {
- for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
- /* void */;
- if (ch == '\0')
- goto done;
- fmt++; /* skip over '%' */
-
- flags = 0;
- width = 0;
-
-rflag: ch = *fmt++;
-reswitch: switch (ch) {
- case ' ':
- case '#':
- goto rflag;
- case '*':
- ADDASTER ();
- goto rflag;
- case '-':
- case '+':
- case '\'':
- goto rflag;
- case '.':
- if ((ch = *fmt++) == '*') {
- ADDASTER ();
- goto rflag;
- }
- while (is_digit(ch)) {
- ch = *fmt++;
- }
- goto reswitch;
- case '0':
- goto rflag;
- case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- n = 0;
- do {
- n = 10 * n + to_digit(ch);
- ch = *fmt++;
- } while (is_digit(ch));
- if (ch == '$') {
- nextarg = n;
- goto rflag;
- }
- width = n;
- goto reswitch;
-#ifdef FLOATING_POINT
- case 'L':
- flags |= LONGDBL;
- goto rflag;
-#endif
- case 'h':
- if (flags & SHORTINT) {
- flags &= ~SHORTINT;
- flags |= CHARINT;
- } else
- flags |= SHORTINT;
- goto rflag;
- case 'j':
- flags |= INTMAXT;
- goto rflag;
- case 'l':
- if (flags & LONGINT) {
- flags &= ~LONGINT;
- flags |= LLONGINT;
- } else
- flags |= LONGINT;
- goto rflag;
- case 'q':
- flags |= LLONGINT; /* not necessarily */
- goto rflag;
- case 't':
- flags |= PTRDIFFT;
- goto rflag;
- case 'z':
- flags |= SIZET;
- goto rflag;
- case 'C':
- flags |= LONGINT;
- /*FALLTHROUGH*/
- case 'c':
- if (flags & LONGINT)
- ADDTYPE(T_WINT);
- else
- ADDTYPE(T_INT);
- break;
- case 'D':
- flags |= LONGINT;
- /*FALLTHROUGH*/
- case 'd':
- case 'i':
- ADDSARG();
- break;
-#ifdef FLOATING_POINT
-#ifdef HEXFLOAT
- case 'a':
- case 'A':
-#endif
- case 'e':
- case 'E':
- case 'f':
- case 'g':
- case 'G':
- if (flags & LONGDBL)
- ADDTYPE(T_LONG_DOUBLE);
- else
- ADDTYPE(T_DOUBLE);
- break;
-#endif /* FLOATING_POINT */
- case 'n':
- if (flags & INTMAXT)
- ADDTYPE(TP_INTMAXT);
- else if (flags & PTRDIFFT)
- ADDTYPE(TP_PTRDIFFT);
- else if (flags & SIZET)
- ADDTYPE(TP_SIZET);
- else if (flags & LLONGINT)
- ADDTYPE(TP_LLONG);
- else if (flags & LONGINT)
- ADDTYPE(TP_LONG);
- else if (flags & SHORTINT)
- ADDTYPE(TP_SHORT);
- else if (flags & CHARINT)
- ADDTYPE(TP_SCHAR);
- else
- ADDTYPE(TP_INT);
- continue; /* no output */
- case 'O':
- flags |= LONGINT;
- /*FALLTHROUGH*/
- case 'o':
- ADDUARG();
- break;
- case 'p':
- ADDTYPE(TP_VOID);
- break;
- case 'S':
- flags |= LONGINT;
- /*FALLTHROUGH*/
- case 's':
- if (flags & LONGINT)
- ADDTYPE(TP_WCHAR);
- else
- ADDTYPE(TP_CHAR);
- break;
- case 'U':
- flags |= LONGINT;
- /*FALLTHROUGH*/
- case 'u':
- case 'X':
- case 'x':
- ADDUARG();
- break;
- default: /* "%?" prints ?, unless ? is NUL */
- if (ch == '\0')
- goto done;
- break;
- }
- }
-done:
- /*
- * Build the argument table.
- */
- if (tablemax >= STATIC_ARG_TBL_SIZE) {
- *argtable = (union arg *)
- malloc (sizeof (union arg) * (tablemax + 1));
- }
-
- (*argtable) [0].intarg = 0;
- for (n = 1; n <= tablemax; n++) {
- switch (typetable [n]) {
- case T_UNUSED: /* whoops! */
- (*argtable) [n].intarg = va_arg (ap, int);
- break;
- case TP_SCHAR:
- (*argtable) [n].pschararg = va_arg (ap, signed char *);
- break;
- case TP_SHORT:
- (*argtable) [n].pshortarg = va_arg (ap, short *);
- break;
- case T_INT:
- (*argtable) [n].intarg = va_arg (ap, int);
- break;
- case T_U_INT:
- (*argtable) [n].uintarg = va_arg (ap, unsigned int);
- break;
- case TP_INT:
- (*argtable) [n].pintarg = va_arg (ap, int *);
- break;
- case T_LONG:
- (*argtable) [n].longarg = va_arg (ap, long);
- break;
- case T_U_LONG:
- (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
- break;
- case TP_LONG:
- (*argtable) [n].plongarg = va_arg (ap, long *);
- break;
- case T_LLONG:
- (*argtable) [n].longlongarg = va_arg (ap, long long);
- break;
- case T_U_LLONG:
- (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
- break;
- case TP_LLONG:
- (*argtable) [n].plonglongarg = va_arg (ap, long long *);
- break;
- case T_PTRDIFFT:
- (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
- break;
- case TP_PTRDIFFT:
- (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
- break;
- case T_SIZET:
- (*argtable) [n].sizearg = va_arg (ap, size_t);
- break;
- case TP_SIZET:
- (*argtable) [n].psizearg = va_arg (ap, ssize_t *);
- break;
- case T_INTMAXT:
- (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
- break;
- case T_UINTMAXT:
- (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
- break;
- case TP_INTMAXT:
- (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
- break;
-#ifdef FLOATING_POINT
- case T_DOUBLE:
- (*argtable) [n].doublearg = va_arg (ap, double);
- break;
- case T_LONG_DOUBLE:
- (*argtable) [n].longdoublearg = va_arg (ap, long double);
- break;
-#endif
- case TP_CHAR:
- (*argtable) [n].pchararg = va_arg (ap, char *);
- break;
- case TP_VOID:
- (*argtable) [n].pvoidarg = va_arg (ap, void *);
- break;
- case T_WINT:
- (*argtable) [n].wintarg = va_arg (ap, wint_t);
- break;
- case TP_WCHAR:
- (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
- break;
- }
- }
-
- if ((typetable != NULL) && (typetable != stattypetable))
- free (typetable);
-}
-
-/*
- * Increase the size of the type table.
- */
-static void
-__grow_type_table (int nextarg, enum typeid **typetable, int *tablesize)
-{
- enum typeid *const oldtable = *typetable;
- const int oldsize = *tablesize;
- enum typeid *newtable;
- int newsize = oldsize * 2;
-
- if (newsize < nextarg + 1)
- newsize = nextarg + 1;
- if (oldsize == STATIC_ARG_TBL_SIZE) {
- if ((newtable = malloc(newsize)) == NULL)
- abort(); /* XXX handle better */
- bcopy(oldtable, newtable, oldsize);
- } else {
- if ((newtable = reallocf(oldtable, newsize)) == NULL)
- abort(); /* XXX handle better */
- }
- memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
-
- *typetable = newtable;
- *tablesize = newsize;
-}
-
-
-#ifdef FLOATING_POINT
-
-static int
-exponent(char *p0, int exp, int fmtch)
-{
- char *p, *t;
- char expbuf[MAXEXPDIG];
-
- p = p0;
- *p++ = fmtch;
- if (exp < 0) {
- exp = -exp;
- *p++ = '-';
- }
- else
- *p++ = '+';
- t = expbuf + MAXEXPDIG;
- if (exp > 9) {
- do {
- *--t = to_char(exp % 10);
- } while ((exp /= 10) > 9);
- *--t = to_char(exp);
- for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
- }
- else {
- /*
- * Exponents for decimal floating point conversions
- * (%[eEgG]) must be at least two characters long,
- * whereas exponents for hexadecimal conversions can
- * be only one character long.
- */
- if (fmtch == 'e' || fmtch == 'E')
- *p++ = '0';
- *p++ = to_char(exp);
- }
- return (p - p0);
-}
-#endif /* FLOATING_POINT */