Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libstdio/printf-pos.c

1/*-
2 * Copyright (c) 1990, 1993
3 *The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
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.
19 *
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.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)vfprintf.c8.1 (Berkeley) 6/4/93";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37//__FBSDID("$FreeBSD: src/lib/libc/stdio/printf-pos.c,v 1.7.2.2 2012/11/17 11:36:20 svnexp Exp $");
38
39/*
40 * This is the code responsible for handling positional arguments
41 * (%m$ and %m$.n$) for vfprintf() and vfwprintf().
42 */
43
44#include "namespace.h"
45#include <sys/types.h>
46
47#include <stdarg.h>
48#include <stddef.h>
49#include <stdint.h>
50#include "stdio.h"
51#include "libsaio.h"
52//#include <wchar.h>
53
54#include "un-namespace.h"
55#include "printflocal.h"
56
57/*
58 * Type ids for argument type table.
59 */
60enum typeid {
61T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
62T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
63T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SSIZET,
64T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
65T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
66};
67
68/* An expandable array of types. */
69struct typetable {
70enum typeid *table; /* table of types */
71enum typeid stattable[STATIC_ARG_TBL_SIZE];
72int tablesize;/* current size of type table */
73int tablemax;/* largest used index in table */
74int nextarg;/* 1-based argument index */
75};
76
77static int__grow_type_table(struct typetable *);
78static voidbuild_arg_table (struct typetable *, va_list, union arg **);
79
80/*
81 * Initialize a struct typetable.
82 */
83static inline void
84inittypes(struct typetable *types)
85{
86int n;
87
88types->table = types->stattable;
89types->tablesize = STATIC_ARG_TBL_SIZE;
90types->tablemax = 0;
91types->nextarg = 1;
92for (n = 0; n < STATIC_ARG_TBL_SIZE; n++)
93types->table[n] = T_UNUSED;
94}
95
96/*
97 * struct typetable destructor.
98 */
99static inline void
100freetypes(struct typetable *types)
101{
102
103if (types->table != types->stattable)
104free (types->table);
105}
106
107/*
108 * Ensure that there is space to add a new argument type to the type table.
109 * Expand the table if necessary. Returns 0 on success.
110 */
111static inline int
112_ensurespace(struct typetable *types)
113{
114
115if (types->nextarg >= types->tablesize) {
116if (__grow_type_table(types))
117return (-1);
118}
119if (types->nextarg > types->tablemax)
120types->tablemax = types->nextarg;
121return (0);
122}
123
124/*
125 * Add an argument type to the table, expanding if necessary.
126 * Returns 0 on success.
127 */
128static inline int
129addtype(struct typetable *types, enum typeid type)
130{
131
132if (_ensurespace(types))
133return (-1);
134types->table[types->nextarg++] = type;
135return (0);
136}
137
138static inline int
139addsarg(struct typetable *types, int flags)
140{
141
142if (_ensurespace(types))
143return (-1);
144if (flags & INTMAXT)
145types->table[types->nextarg++] = T_INTMAXT;
146else if (flags & SIZET)
147types->table[types->nextarg++] = T_SSIZET;
148else if (flags & PTRDIFFT)
149types->table[types->nextarg++] = T_PTRDIFFT;
150else if (flags & LLONGINT)
151types->table[types->nextarg++] = T_LLONG;
152else if (flags & LONGINT)
153types->table[types->nextarg++] = T_LONG;
154else
155types->table[types->nextarg++] = T_INT;
156return (0);
157}
158
159static inline int
160adduarg(struct typetable *types, int flags)
161{
162
163if (_ensurespace(types))
164return (-1);
165if (flags & INTMAXT)
166types->table[types->nextarg++] = T_UINTMAXT;
167else if (flags & SIZET)
168types->table[types->nextarg++] = T_SIZET;
169else if (flags & PTRDIFFT)
170types->table[types->nextarg++] = T_SIZET;
171else if (flags & LLONGINT)
172types->table[types->nextarg++] = T_U_LLONG;
173else if (flags & LONGINT)
174types->table[types->nextarg++] = T_U_LONG;
175else
176types->table[types->nextarg++] = T_U_INT;
177return (0);
178}
179
180/*
181 * Add * arguments to the type array.
182 */
183static inline int
184addaster(struct typetable *types, char **fmtp)
185{
186char *cp;
187int n2;
188
189n2 = 0;
190cp = *fmtp;
191while (is_digit(*cp)) {
192n2 = 10 * n2 + to_digit(*cp);
193cp++;
194}
195if (*cp == '$') {
196int hold = types->nextarg;
197types->nextarg = n2;
198if (addtype(types, T_INT))
199return (-1);
200types->nextarg = hold;
201*fmtp = ++cp;
202} else {
203if (addtype(types, T_INT))
204return (-1);
205}
206return (0);
207}
208
209static inline int
210addwaster(struct typetable *types, wchar_t **fmtp)
211{
212wchar_t *cp;
213int n2;
214
215n2 = 0;
216cp = *fmtp;
217while (is_digit(*cp)) {
218n2 = 10 * n2 + to_digit(*cp);
219cp++;
220}
221if (*cp == '$') {
222int hold = types->nextarg;
223types->nextarg = n2;
224if (addtype(types, T_INT))
225return (-1);
226types->nextarg = hold;
227*fmtp = ++cp;
228} else {
229if (addtype(types, T_INT))
230return (-1);
231}
232return (0);
233}
234
235/*
236 * Find all arguments when a positional parameter is encountered. Returns a
237 * table, indexed by argument number, of pointers to each arguments. The
238 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
239 * It will be replaces with a malloc-ed one if it overflows.
240 * Returns 0 on success. On failure, returns nonzero and sets errno.
241 */
242int
243__find_arguments (const char *fmt0, va_list ap, union arg **argtable)
244{
245char *fmt;/* format string */
246int ch;/* character from fmt */
247int n;/* handy integer (short term usage) */
248int error;
249int flags;/* flags as above */
250struct typetable types;/* table of types */
251
252fmt = (char *)fmt0;
253inittypes(&types);
254error = 0;
255
256/*
257 * Scan the format for conversions (`%' character).
258 */
259for (;;) {
260while ((ch = *fmt) != '\0' && ch != '%')
261fmt++;
262if (ch == '\0')
263goto done;
264fmt++;/* skip over '%' */
265
266flags = 0;
267
268rflag:ch = *fmt++;
269reswitch:switch (ch) {
270case ' ':
271case '#':
272goto rflag;
273case '*':
274if ((error = addaster(&types, &fmt)))
275goto error;
276goto rflag;
277case '-':
278case '+':
279case '\'':
280goto rflag;
281case '.':
282if ((ch = *fmt++) == '*') {
283if ((error = addaster(&types, &fmt)))
284goto error;
285goto rflag;
286}
287while (is_digit(ch)) {
288ch = *fmt++;
289}
290goto reswitch;
291case '0':
292goto rflag;
293case '1': case '2': case '3': case '4':
294case '5': case '6': case '7': case '8': case '9':
295n = 0;
296do {
297n = 10 * n + to_digit(ch);
298ch = *fmt++;
299} while (is_digit(ch));
300if (ch == '$') {
301types.nextarg = n;
302goto rflag;
303}
304goto reswitch;
305#ifndef NO_FLOATING_POINT
306case 'L':
307flags |= LONGDBL;
308goto rflag;
309#endif
310case 'h':
311if (flags & SHORTINT) {
312flags &= ~SHORTINT;
313flags |= CHARINT;
314} else
315flags |= SHORTINT;
316goto rflag;
317case 'j':
318flags |= INTMAXT;
319goto rflag;
320case 'l':
321if (flags & LONGINT) {
322flags &= ~LONGINT;
323flags |= LLONGINT;
324} else
325flags |= LONGINT;
326goto rflag;
327case 'q':
328flags |= LLONGINT;/* not necessarily */
329goto rflag;
330case 't':
331flags |= PTRDIFFT;
332goto rflag;
333case 'z':
334flags |= SIZET;
335goto rflag;
336case 'C':
337flags |= LONGINT;
338/*FALLTHROUGH*/
339case 'c':
340error = addtype(&types,
341(flags & LONGINT) ? T_WINT : T_INT);
342if (error)
343goto error;
344break;
345case 'D':
346flags |= LONGINT;
347/*FALLTHROUGH*/
348case 'd':
349case 'i':
350if ((error = addsarg(&types, flags)))
351goto error;
352break;
353#ifndef NO_FLOATING_POINT
354case 'a':
355case 'A':
356case 'e':
357case 'E':
358case 'f':
359case 'g':
360case 'G':
361error = addtype(&types,
362 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
363if (error)
364goto error;
365break;
366#endif /* !NO_FLOATING_POINT */
367case 'n':
368if (flags & INTMAXT)
369error = addtype(&types, TP_INTMAXT);
370else if (flags & PTRDIFFT)
371error = addtype(&types, TP_PTRDIFFT);
372else if (flags & SIZET)
373error = addtype(&types, TP_SSIZET);
374else if (flags & LLONGINT)
375error = addtype(&types, TP_LLONG);
376else if (flags & LONGINT)
377error = addtype(&types, TP_LONG);
378else if (flags & SHORTINT)
379error = addtype(&types, TP_SHORT);
380else if (flags & CHARINT)
381error = addtype(&types, TP_SCHAR);
382else
383error = addtype(&types, TP_INT);
384if (error)
385goto error;
386continue;/* no output */
387case 'O':
388flags |= LONGINT;
389/*FALLTHROUGH*/
390case 'o':
391if ((error = adduarg(&types, flags)))
392goto error;
393break;
394case 'p':
395if ((error = addtype(&types, TP_VOID)))
396goto error;
397break;
398case 'S':
399flags |= LONGINT;
400/*FALLTHROUGH*/
401case 's':
402error = addtype(&types,
403(flags & LONGINT) ? TP_WCHAR : TP_CHAR);
404if (error)
405goto error;
406break;
407case 'U':
408flags |= LONGINT;
409/*FALLTHROUGH*/
410case 'u':
411case 'X':
412case 'x':
413if ((error = adduarg(&types, flags)))
414goto error;
415break;
416default:/* "%?" prints ?, unless ? is NUL */
417if (ch == '\0')
418goto done;
419break;
420}
421}
422done:
423build_arg_table(&types, ap, argtable);
424error:
425freetypes(&types);
426return (error || *argtable == NULL);
427}
428
429/* wchar version of __find_arguments. */
430int
431__find_warguments (const wchar_t *fmt0, va_list ap, union arg **argtable)
432{
433wchar_t *fmt;/* format string */
434wchar_t ch;/* character from fmt */
435int n;/* handy integer (short term usage) */
436int error;
437int flags;/* flags as above */
438struct typetable types;/* table of types */
439
440fmt = (wchar_t *)fmt0;
441inittypes(&types);
442error = 0;
443
444/*
445 * Scan the format for conversions (`%' character).
446 */
447for (;;) {
448while ((ch = *fmt) != '\0' && ch != '%')
449fmt++;
450if (ch == '\0')
451goto done;
452fmt++;/* skip over '%' */
453
454flags = 0;
455
456rflag:ch = *fmt++;
457reswitch:switch (ch) {
458case ' ':
459case '#':
460goto rflag;
461case '*':
462if ((error = addwaster(&types, &fmt)))
463goto error;
464goto rflag;
465case '-':
466case '+':
467case '\'':
468goto rflag;
469case '.':
470if ((ch = *fmt++) == '*') {
471if ((error = addwaster(&types, &fmt)))
472goto error;
473goto rflag;
474}
475while (is_digit(ch)) {
476ch = *fmt++;
477}
478goto reswitch;
479case '0':
480goto rflag;
481case '1': case '2': case '3': case '4':
482case '5': case '6': case '7': case '8': case '9':
483n = 0;
484do {
485n = 10 * n + to_digit(ch);
486ch = *fmt++;
487} while (is_digit(ch));
488if (ch == '$') {
489types.nextarg = n;
490goto rflag;
491}
492goto reswitch;
493#ifndef NO_FLOATING_POINT
494case 'L':
495flags |= LONGDBL;
496goto rflag;
497#endif
498case 'h':
499if (flags & SHORTINT) {
500flags &= ~SHORTINT;
501flags |= CHARINT;
502} else
503flags |= SHORTINT;
504goto rflag;
505case 'j':
506flags |= INTMAXT;
507goto rflag;
508case 'l':
509if (flags & LONGINT) {
510flags &= ~LONGINT;
511flags |= LLONGINT;
512} else
513flags |= LONGINT;
514goto rflag;
515case 'q':
516flags |= LLONGINT;/* not necessarily */
517goto rflag;
518case 't':
519flags |= PTRDIFFT;
520goto rflag;
521case 'z':
522flags |= SIZET;
523goto rflag;
524case 'C':
525flags |= LONGINT;
526/*FALLTHROUGH*/
527case 'c':
528error = addtype(&types,
529(flags & LONGINT) ? T_WINT : T_INT);
530if (error)
531goto error;
532break;
533case 'D':
534flags |= LONGINT;
535/*FALLTHROUGH*/
536case 'd':
537case 'i':
538if ((error = addsarg(&types, flags)))
539goto error;
540break;
541#ifndef NO_FLOATING_POINT
542case 'a':
543case 'A':
544case 'e':
545case 'E':
546case 'f':
547case 'g':
548case 'G':
549error = addtype(&types,
550 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
551if (error)
552goto error;
553break;
554#endif /* !NO_FLOATING_POINT */
555case 'n':
556if (flags & INTMAXT)
557error = addtype(&types, TP_INTMAXT);
558else if (flags & PTRDIFFT)
559error = addtype(&types, TP_PTRDIFFT);
560else if (flags & SIZET)
561error = addtype(&types, TP_SSIZET);
562else if (flags & LLONGINT)
563error = addtype(&types, TP_LLONG);
564else if (flags & LONGINT)
565error = addtype(&types, TP_LONG);
566else if (flags & SHORTINT)
567error = addtype(&types, TP_SHORT);
568else if (flags & CHARINT)
569error = addtype(&types, TP_SCHAR);
570else
571error = addtype(&types, TP_INT);
572if (error)
573goto error;
574continue;/* no output */
575case 'O':
576flags |= LONGINT;
577/*FALLTHROUGH*/
578case 'o':
579if ((error = adduarg(&types, flags)))
580goto error;
581break;
582case 'p':
583if ((error = addtype(&types, TP_VOID)))
584goto error;
585break;
586case 'S':
587flags |= LONGINT;
588/*FALLTHROUGH*/
589case 's':
590error = addtype(&types,
591 (flags & LONGINT) ? TP_WCHAR : TP_CHAR);
592if (error)
593goto error;
594break;
595case 'U':
596flags |= LONGINT;
597/*FALLTHROUGH*/
598case 'u':
599case 'X':
600case 'x':
601if ((error = adduarg(&types, flags)))
602goto error;
603break;
604default:/* "%?" prints ?, unless ? is NUL */
605if (ch == '\0')
606goto done;
607break;
608}
609}
610done:
611build_arg_table(&types, ap, argtable);
612error:
613freetypes(&types);
614return (error || *argtable == NULL);
615}
616
617/*
618 * Increase the size of the type table. Returns 0 on success.
619 */
620static int
621__grow_type_table(struct typetable *types)
622{
623enum typeid *const oldtable = types->table;
624const int oldsize = types->tablesize;
625enum typeid *newtable;
626int n, newsize = oldsize * 2;
627
628if (newsize < types->nextarg + 1)
629newsize = types->nextarg + 1;
630if (oldsize == STATIC_ARG_TBL_SIZE) {
631if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
632return (-1);
633bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
634} else {
635newtable = realloc(oldtable, newsize * sizeof(enum typeid));
636if (newtable == NULL)
637return (-1);
638}
639for (n = oldsize; n < newsize; n++)
640newtable[n] = T_UNUSED;
641
642types->table = newtable;
643types->tablesize = newsize;
644
645return (0);
646}
647
648/*
649 * Build the argument table from the completed type table.
650 * On malloc failure, *argtable is set to NULL.
651 */
652static void
653build_arg_table(struct typetable *types, va_list ap, union arg **argtable)
654{
655int n;
656
657if (types->tablemax >= STATIC_ARG_TBL_SIZE) {
658*argtable = (union arg *)
659 malloc (sizeof (union arg) * (types->tablemax + 1));
660if (*argtable == NULL)
661return;
662}
663
664(*argtable) [0].intarg = 0;
665for (n = 1; n <= types->tablemax; n++) {
666switch (types->table[n]) {
667 case T_UNUSED: /* whoops! */
668(*argtable) [n].intarg = va_arg (ap, int);
669break;
670 case TP_SCHAR:
671(*argtable) [n].pschararg = va_arg (ap, signed char *);
672break;
673 case TP_SHORT:
674(*argtable) [n].pshortarg = va_arg (ap, short *);
675break;
676 case T_INT:
677(*argtable) [n].intarg = va_arg (ap, int);
678break;
679 case T_U_INT:
680(*argtable) [n].uintarg = va_arg (ap, unsigned int);
681break;
682 case TP_INT:
683(*argtable) [n].pintarg = va_arg (ap, int *);
684break;
685 case T_LONG:
686(*argtable) [n].longarg = va_arg (ap, long);
687break;
688 case T_U_LONG:
689(*argtable) [n].ulongarg = va_arg (ap, unsigned long);
690break;
691 case TP_LONG:
692(*argtable) [n].plongarg = va_arg (ap, long *);
693break;
694 case T_LLONG:
695(*argtable) [n].longlongarg = va_arg (ap, long long);
696break;
697 case T_U_LLONG:
698(*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
699break;
700 case TP_LLONG:
701(*argtable) [n].plonglongarg = va_arg (ap, long long *);
702break;
703 case T_PTRDIFFT:
704(*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
705break;
706 case TP_PTRDIFFT:
707(*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
708break;
709 case T_SIZET:
710(*argtable) [n].sizearg = va_arg (ap, size_t);
711break;
712 case T_SSIZET:
713(*argtable) [n].sizearg = va_arg (ap, ssize_t);
714break;
715 case TP_SSIZET:
716(*argtable) [n].pssizearg = va_arg (ap, ssize_t *);
717break;
718 case T_INTMAXT:
719(*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
720break;
721 case T_UINTMAXT:
722(*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
723break;
724 case TP_INTMAXT:
725(*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
726break;
727 case T_DOUBLE:
728#ifndef NO_FLOATING_POINT
729(*argtable) [n].doublearg = va_arg (ap, double);
730#endif
731break;
732 case T_LONG_DOUBLE:
733#ifndef NO_FLOATING_POINT
734(*argtable) [n].longdoublearg = va_arg (ap, long double);
735#endif
736break;
737 case TP_CHAR:
738(*argtable) [n].pchararg = va_arg (ap, char *);
739break;
740 case TP_VOID:
741(*argtable) [n].pvoidarg = va_arg (ap, void *);
742break;
743 case T_WINT:
744(*argtable) [n].wintarg = va_arg (ap, wint_t);
745break;
746 case TP_WCHAR:
747(*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
748break;
749}
750}
751}
752

Archive Download this file

Revision: 2154