Sync libc/stdio with FreeBSD:
[dragonfly.git] / lib / libc / stdio / printf-pos.c
blob9344661bab4861d0b12c7abc4e4016b3e3cef88b
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)vfprintf.c 8.1 (Berkeley) 6/4/93
33 * $FreeBSD: src/lib/libc/stdio/printf-pos.c,v 1.6 2009/03/02 04:07:58 das Exp $
37 * This is the code responsible for handling positional arguments
38 * (%m$ and %m$.n$) for vfprintf() and vfwprintf().
41 #include "namespace.h"
42 #include <sys/types.h>
44 #include <stdarg.h>
45 #include <stddef.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <wchar.h>
52 #include "un-namespace.h"
53 #include "printflocal.h"
56 * Type ids for argument type table.
58 enum typeid {
59 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
60 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
61 T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SSIZET,
62 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
63 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
66 /* An expandable array of types. */
67 struct typetable {
68 enum typeid *table; /* table of types */
69 enum typeid stattable[STATIC_ARG_TBL_SIZE];
70 int tablesize; /* current size of type table */
71 int tablemax; /* largest used index in table */
72 int nextarg; /* 1-based argument index */
75 static int __grow_type_table(struct typetable *);
76 static void build_arg_table (struct typetable *, va_list, union arg **);
79 * Initialize a struct typetable.
81 static inline void
82 inittypes(struct typetable *types)
84 int n;
86 types->table = types->stattable;
87 types->tablesize = STATIC_ARG_TBL_SIZE;
88 types->tablemax = 0;
89 types->nextarg = 1;
90 for (n = 0; n < STATIC_ARG_TBL_SIZE; n++)
91 types->table[n] = T_UNUSED;
95 * struct typetable destructor.
97 static inline void
98 freetypes(struct typetable *types)
101 if (types->table != types->stattable)
102 free(types->table);
106 * Ensure that there is space to add a new argument type to the type table.
107 * Expand the table if necessary. Returns 0 on success.
109 static inline int
110 _ensurespace(struct typetable *types)
113 if (types->nextarg >= types->tablesize) {
114 if (__grow_type_table(types))
115 return (-1);
117 if (types->nextarg > types->tablemax)
118 types->tablemax = types->nextarg;
119 return (0);
123 * Add an argument type to the table, expanding if necessary.
124 * Returns 0 on success.
126 static inline int
127 addtype(struct typetable *types, enum typeid type)
130 if (_ensurespace(types))
131 return (-1);
132 types->table[types->nextarg++] = type;
133 return (0);
136 static inline int
137 addsarg(struct typetable *types, int flags)
140 if (_ensurespace(types))
141 return (-1);
142 if (flags & INTMAXT)
143 types->table[types->nextarg++] = T_INTMAXT;
144 else if (flags & SIZET)
145 types->table[types->nextarg++] = T_SSIZET;
146 else if (flags & PTRDIFFT)
147 types->table[types->nextarg++] = T_PTRDIFFT;
148 else if (flags & LLONGINT)
149 types->table[types->nextarg++] = T_LLONG;
150 else if (flags & LONGINT)
151 types->table[types->nextarg++] = T_LONG;
152 else
153 types->table[types->nextarg++] = T_INT;
154 return (0);
157 static inline int
158 adduarg(struct typetable *types, int flags)
161 if (_ensurespace(types))
162 return (-1);
163 if (flags & INTMAXT)
164 types->table[types->nextarg++] = T_UINTMAXT;
165 else if (flags & SIZET)
166 types->table[types->nextarg++] = T_SIZET;
167 else if (flags & PTRDIFFT)
168 types->table[types->nextarg++] = T_SIZET;
169 else if (flags & LLONGINT)
170 types->table[types->nextarg++] = T_U_LLONG;
171 else if (flags & LONGINT)
172 types->table[types->nextarg++] = T_U_LONG;
173 else
174 types->table[types->nextarg++] = T_U_INT;
175 return (0);
179 * Add * arguments to the type array.
181 static inline int
182 addaster(struct typetable *types, char **fmtp)
184 char *cp;
185 int n2;
187 n2 = 0;
188 cp = *fmtp;
189 while (is_digit(*cp)) {
190 n2 = 10 * n2 + to_digit(*cp);
191 cp++;
193 if (*cp == '$') {
194 int hold = types->nextarg;
195 types->nextarg = n2;
196 if (addtype(types, T_INT))
197 return (-1);
198 types->nextarg = hold;
199 *fmtp = ++cp;
200 } else {
201 if (addtype(types, T_INT))
202 return (-1);
204 return (0);
207 static inline int
208 addwaster(struct typetable *types, wchar_t **fmtp)
210 wchar_t *cp;
211 int n2;
213 n2 = 0;
214 cp = *fmtp;
215 while (is_digit(*cp)) {
216 n2 = 10 * n2 + to_digit(*cp);
217 cp++;
219 if (*cp == '$') {
220 int hold = types->nextarg;
221 types->nextarg = n2;
222 if (addtype(types, T_INT))
223 return (-1);
224 types->nextarg = hold;
225 *fmtp = ++cp;
226 } else {
227 if (addtype(types, T_INT))
228 return (-1);
230 return (0);
234 * Find all arguments when a positional parameter is encountered. Returns a
235 * table, indexed by argument number, of pointers to each arguments. The
236 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
237 * It will be replaces with a malloc-ed one if it overflows.
238 * Returns 0 on success. On failure, returns nonzero and sets errno.
241 __find_arguments(const char *fmt0, va_list ap, union arg **argtable)
243 char *fmt; /* format string */
244 int ch; /* character from fmt */
245 int n; /* handy integer (short term usage) */
246 int error;
247 int flags; /* flags as above */
248 int width; /* width from format (%8d), or 0 */
249 struct typetable types; /* table of types */
251 fmt = __DECONST(char *, fmt0);
252 inittypes(&types);
253 error = 0;
256 * Scan the format for conversions (`%' character).
258 for (;;) {
259 while ((ch = *fmt) != '\0' && ch != '%')
260 fmt++;
261 if (ch == '\0')
262 goto done;
263 fmt++; /* skip over '%' */
265 flags = 0;
266 width = 0;
268 rflag: ch = *fmt++;
269 reswitch: switch (ch) {
270 case ' ':
271 case '#':
272 goto rflag;
273 case '*':
274 if ((error = addaster(&types, &fmt)))
275 goto error;
276 goto rflag;
277 case '-':
278 case '+':
279 case '\'':
280 goto rflag;
281 case '.':
282 if ((ch = *fmt++) == '*') {
283 if ((error = addaster(&types, &fmt)))
284 goto error;
285 goto rflag;
287 while (is_digit(ch)) {
288 ch = *fmt++;
290 goto reswitch;
291 case '0':
292 goto rflag;
293 case '1': case '2': case '3': case '4':
294 case '5': case '6': case '7': case '8': case '9':
295 n = 0;
296 do {
297 n = 10 * n + to_digit(ch);
298 ch = *fmt++;
299 } while (is_digit(ch));
300 if (ch == '$') {
301 types.nextarg = n;
302 goto rflag;
304 width = n;
305 goto reswitch;
306 #ifndef NO_FLOATING_POINT
307 case 'L':
308 flags |= LONGDBL;
309 goto rflag;
310 #endif
311 case 'h':
312 if (flags & SHORTINT) {
313 flags &= ~SHORTINT;
314 flags |= CHARINT;
315 } else
316 flags |= SHORTINT;
317 goto rflag;
318 case 'j':
319 flags |= INTMAXT;
320 goto rflag;
321 case 'l':
322 if (flags & LONGINT) {
323 flags &= ~LONGINT;
324 flags |= LLONGINT;
325 } else
326 flags |= LONGINT;
327 goto rflag;
328 case 'q':
329 flags |= LLONGINT; /* not necessarily */
330 goto rflag;
331 case 't':
332 flags |= PTRDIFFT;
333 goto rflag;
334 case 'z':
335 flags |= SIZET;
336 goto rflag;
337 case 'C':
338 flags |= LONGINT;
339 /*FALLTHROUGH*/
340 case 'c':
341 error = addtype(&types,
342 (flags & LONGINT) ? T_WINT : T_INT);
343 if (error)
344 goto error;
345 break;
346 case 'D':
347 flags |= LONGINT;
348 /*FALLTHROUGH*/
349 case 'd':
350 case 'i':
351 if ((error = addsarg(&types, flags)))
352 goto error;
353 break;
354 #ifndef NO_FLOATING_POINT
355 case 'a':
356 case 'A':
357 case 'e':
358 case 'E':
359 case 'f':
360 case 'g':
361 case 'G':
362 error = addtype(&types,
363 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
364 if (error)
365 goto error;
366 break;
367 #endif /* !NO_FLOATING_POINT */
368 case 'n':
369 if (flags & INTMAXT)
370 error = addtype(&types, TP_INTMAXT);
371 else if (flags & PTRDIFFT)
372 error = addtype(&types, TP_PTRDIFFT);
373 else if (flags & SIZET)
374 error = addtype(&types, TP_SSIZET);
375 else if (flags & LLONGINT)
376 error = addtype(&types, TP_LLONG);
377 else if (flags & LONGINT)
378 error = addtype(&types, TP_LONG);
379 else if (flags & SHORTINT)
380 error = addtype(&types, TP_SHORT);
381 else if (flags & CHARINT)
382 error = addtype(&types, TP_SCHAR);
383 else
384 error = addtype(&types, TP_INT);
385 if (error)
386 goto error;
387 continue; /* no output */
388 case 'O':
389 flags |= LONGINT;
390 /*FALLTHROUGH*/
391 case 'o':
392 if ((error = adduarg(&types, flags)))
393 goto error;
394 break;
395 case 'p':
396 if ((error = addtype(&types, TP_VOID)))
397 goto error;
398 break;
399 case 'S':
400 flags |= LONGINT;
401 /*FALLTHROUGH*/
402 case 's':
403 error = addtype(&types,
404 (flags & LONGINT) ? TP_WCHAR : TP_CHAR);
405 if (error)
406 goto error;
407 break;
408 case 'U':
409 flags |= LONGINT;
410 /*FALLTHROUGH*/
411 case 'u':
412 case 'X':
413 case 'x':
414 if ((error = adduarg(&types, flags)))
415 goto error;
416 break;
417 default: /* "%?" prints ?, unless ? is NUL */
418 if (ch == '\0')
419 goto done;
420 break;
423 done:
424 build_arg_table(&types, ap, argtable);
425 error:
426 freetypes(&types);
427 return (error || *argtable == NULL);
430 /* wchar version of __find_arguments. */
432 __find_warguments(const wchar_t *fmt0, va_list ap, union arg **argtable)
434 wchar_t *fmt; /* format string */
435 wchar_t ch; /* character from fmt */
436 int n; /* handy integer (short term usage) */
437 int error;
438 int flags; /* flags as above */
439 int width; /* width from format (%8d), or 0 */
440 struct typetable types; /* table of types */
442 fmt = __DECONST(wchar_t *, fmt0);
443 inittypes(&types);
444 error = 0;
447 * Scan the format for conversions (`%' character).
449 for (;;) {
450 while ((ch = *fmt) != '\0' && ch != '%')
451 fmt++;
452 if (ch == '\0')
453 goto done;
454 fmt++; /* skip over '%' */
456 flags = 0;
457 width = 0;
459 rflag: ch = *fmt++;
460 reswitch: switch (ch) {
461 case ' ':
462 case '#':
463 goto rflag;
464 case '*':
465 if ((error = addwaster(&types, &fmt)))
466 goto error;
467 goto rflag;
468 case '-':
469 case '+':
470 case '\'':
471 goto rflag;
472 case '.':
473 if ((ch = *fmt++) == '*') {
474 if ((error = addwaster(&types, &fmt)))
475 goto error;
476 goto rflag;
478 while (is_digit(ch)) {
479 ch = *fmt++;
481 goto reswitch;
482 case '0':
483 goto rflag;
484 case '1': case '2': case '3': case '4':
485 case '5': case '6': case '7': case '8': case '9':
486 n = 0;
487 do {
488 n = 10 * n + to_digit(ch);
489 ch = *fmt++;
490 } while (is_digit(ch));
491 if (ch == '$') {
492 types.nextarg = n;
493 goto rflag;
495 width = n;
496 goto reswitch;
497 #ifndef NO_FLOATING_POINT
498 case 'L':
499 flags |= LONGDBL;
500 goto rflag;
501 #endif
502 case 'h':
503 if (flags & SHORTINT) {
504 flags &= ~SHORTINT;
505 flags |= CHARINT;
506 } else
507 flags |= SHORTINT;
508 goto rflag;
509 case 'j':
510 flags |= INTMAXT;
511 goto rflag;
512 case 'l':
513 if (flags & LONGINT) {
514 flags &= ~LONGINT;
515 flags |= LLONGINT;
516 } else
517 flags |= LONGINT;
518 goto rflag;
519 case 'q':
520 flags |= LLONGINT; /* not necessarily */
521 goto rflag;
522 case 't':
523 flags |= PTRDIFFT;
524 goto rflag;
525 case 'z':
526 flags |= SIZET;
527 goto rflag;
528 case 'C':
529 flags |= LONGINT;
530 /*FALLTHROUGH*/
531 case 'c':
532 error = addtype(&types,
533 (flags & LONGINT) ? T_WINT : T_INT);
534 if (error)
535 goto error;
536 break;
537 case 'D':
538 flags |= LONGINT;
539 /*FALLTHROUGH*/
540 case 'd':
541 case 'i':
542 if ((error = addsarg(&types, flags)))
543 goto error;
544 break;
545 #ifndef NO_FLOATING_POINT
546 case 'a':
547 case 'A':
548 case 'e':
549 case 'E':
550 case 'f':
551 case 'g':
552 case 'G':
553 error = addtype(&types,
554 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
555 if (error)
556 goto error;
557 break;
558 #endif /* !NO_FLOATING_POINT */
559 case 'n':
560 if (flags & INTMAXT)
561 error = addtype(&types, TP_INTMAXT);
562 else if (flags & PTRDIFFT)
563 error = addtype(&types, TP_PTRDIFFT);
564 else if (flags & SIZET)
565 error = addtype(&types, TP_SSIZET);
566 else if (flags & LLONGINT)
567 error = addtype(&types, TP_LLONG);
568 else if (flags & LONGINT)
569 error = addtype(&types, TP_LONG);
570 else if (flags & SHORTINT)
571 error = addtype(&types, TP_SHORT);
572 else if (flags & CHARINT)
573 error = addtype(&types, TP_SCHAR);
574 else
575 error = addtype(&types, TP_INT);
576 if (error)
577 goto error;
578 continue; /* no output */
579 case 'O':
580 flags |= LONGINT;
581 /*FALLTHROUGH*/
582 case 'o':
583 if ((error = adduarg(&types, flags)))
584 goto error;
585 break;
586 case 'p':
587 if ((error = addtype(&types, TP_VOID)))
588 goto error;
589 break;
590 case 'S':
591 flags |= LONGINT;
592 /*FALLTHROUGH*/
593 case 's':
594 error = addtype(&types,
595 (flags & LONGINT) ? TP_WCHAR : TP_CHAR);
596 if (error)
597 goto error;
598 break;
599 case 'U':
600 flags |= LONGINT;
601 /*FALLTHROUGH*/
602 case 'u':
603 case 'X':
604 case 'x':
605 if ((error = adduarg(&types, flags)))
606 goto error;
607 break;
608 default: /* "%?" prints ?, unless ? is NUL */
609 if (ch == '\0')
610 goto done;
611 break;
614 done:
615 build_arg_table(&types, ap, argtable);
616 error:
617 freetypes(&types);
618 return (error || *argtable == NULL);
622 * Increase the size of the type table. Returns 0 on success.
624 static int
625 __grow_type_table(struct typetable *types)
627 enum typeid *const oldtable = types->table;
628 const int oldsize = types->tablesize;
629 enum typeid *newtable;
630 int n, newsize = oldsize * 2;
632 if (newsize < types->nextarg + 1)
633 newsize = types->nextarg + 1;
634 if (oldsize == STATIC_ARG_TBL_SIZE) {
635 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
636 return (-1);
637 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
638 } else {
639 newtable = realloc(oldtable, newsize * sizeof(enum typeid));
640 if (newtable == NULL)
641 return (-1);
643 for (n = oldsize; n < newsize; n++)
644 newtable[n] = T_UNUSED;
646 types->table = newtable;
647 types->tablesize = newsize;
649 return (0);
653 * Build the argument table from the completed type table.
654 * On malloc failure, *argtable is set to NULL.
656 static void
657 build_arg_table(struct typetable *types, va_list ap, union arg **argtable)
659 int n;
661 if (types->tablemax >= STATIC_ARG_TBL_SIZE) {
662 *argtable = (union arg *)
663 malloc (sizeof (union arg) * (types->tablemax + 1));
664 if (*argtable == NULL)
665 return;
668 (*argtable) [0].intarg = 0;
669 for (n = 1; n <= types->tablemax; n++) {
670 switch (types->table[n]) {
671 case T_UNUSED: /* whoops! */
672 (*argtable) [n].intarg = va_arg(ap, int);
673 break;
674 case TP_SCHAR:
675 (*argtable) [n].pschararg = va_arg(ap, signed char *);
676 break;
677 case TP_SHORT:
678 (*argtable) [n].pshortarg = va_arg(ap, short *);
679 break;
680 case T_INT:
681 (*argtable) [n].intarg = va_arg(ap, int);
682 break;
683 case T_U_INT:
684 (*argtable) [n].uintarg = va_arg(ap, unsigned int);
685 break;
686 case TP_INT:
687 (*argtable) [n].pintarg = va_arg(ap, int *);
688 break;
689 case T_LONG:
690 (*argtable) [n].longarg = va_arg(ap, long);
691 break;
692 case T_U_LONG:
693 (*argtable) [n].ulongarg = va_arg(ap, unsigned long);
694 break;
695 case TP_LONG:
696 (*argtable) [n].plongarg = va_arg(ap, long *);
697 break;
698 case T_LLONG:
699 (*argtable) [n].longlongarg = va_arg(ap, long long);
700 break;
701 case T_U_LLONG:
702 (*argtable) [n].ulonglongarg = va_arg(ap, unsigned long long);
703 break;
704 case TP_LLONG:
705 (*argtable) [n].plonglongarg = va_arg(ap, long long *);
706 break;
707 case T_PTRDIFFT:
708 (*argtable) [n].ptrdiffarg = va_arg(ap, ptrdiff_t);
709 break;
710 case TP_PTRDIFFT:
711 (*argtable) [n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
712 break;
713 case T_SIZET:
714 (*argtable) [n].sizearg = va_arg(ap, size_t);
715 break;
716 case T_SSIZET:
717 (*argtable) [n].sizearg = va_arg(ap, ssize_t);
718 break;
719 case TP_SSIZET:
720 (*argtable) [n].pssizearg = va_arg(ap, ssize_t *);
721 break;
722 case T_INTMAXT:
723 (*argtable) [n].intmaxarg = va_arg(ap, intmax_t);
724 break;
725 case T_UINTMAXT:
726 (*argtable) [n].uintmaxarg = va_arg(ap, uintmax_t);
727 break;
728 case TP_INTMAXT:
729 (*argtable) [n].pintmaxarg = va_arg(ap, intmax_t *);
730 break;
731 case T_DOUBLE:
732 #ifndef NO_FLOATING_POINT
733 (*argtable) [n].doublearg = va_arg(ap, double);
734 #endif
735 break;
736 case T_LONG_DOUBLE:
737 #ifndef NO_FLOATING_POINT
738 (*argtable) [n].longdoublearg = va_arg(ap, long double);
739 #endif
740 break;
741 case TP_CHAR:
742 (*argtable) [n].pchararg = va_arg(ap, char *);
743 break;
744 case TP_VOID:
745 (*argtable) [n].pvoidarg = va_arg(ap, void *);
746 break;
747 case T_WINT:
748 (*argtable) [n].wintarg = va_arg(ap, wint_t);
749 break;
750 case TP_WCHAR:
751 (*argtable) [n].pwchararg = va_arg(ap, wchar_t *);
752 break;