2.3.90-16
[glibc/history.git] / stdio-common / printf-prs.c
blob015b01f1edc872b85a47807ad816bf6cfcb5133a
1 /* Copyright (C) 1991, 1992, 1995, 1996, 1999, 2000, 2002, 2003, 2004, 2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdio.h>
21 #include <printf.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <wchar.h>
25 #include <sys/param.h>
27 #include "../locale/localeinfo.h"
29 #ifndef COMPILE_WPRINTF
30 # define CHAR_T char
31 # define UCHAR_T unsigned char
32 # define INT_T int
33 # define L_(Str) Str
34 # define ISDIGIT(Ch) isdigit (Ch)
35 # define ISASCII(Ch) isascii (Ch)
36 # define MBRLEN(Cp, L, St) __mbrlen (Cp, L, St)
38 # define PUT(F, S, N) _IO_sputn (F, S, N)
39 # define PAD(Padchar) \
40 if (width > 0) \
41 done += INTUSE(_IO_padn) (s, Padchar, width)
42 #else
43 # define vfprintf vfwprintf
44 # define CHAR_T wchar_t
45 # define UCHAR_T uwchar_t
46 # define INT_T wint_t
47 # define L_(Str) L##Str
48 # define ISDIGIT(Ch) iswdigit (Ch)
50 # define PUT(F, S, N) _IO_sputn (F, S, N)
51 # define PAD(Padchar) \
52 if (width > 0) \
53 done += _IO_wpadn (s, Padchar, width)
54 #endif
56 #define DONT_NEED_READ_INT
57 #include "printf-parse.h"
60 size_t
61 parse_printf_format (fmt, n, argtypes)
62 const char *fmt;
63 size_t n;
64 int *argtypes;
66 size_t nargs; /* Number of arguments. */
67 size_t max_ref_arg; /* Highest index used in a positional arg. */
68 struct printf_spec spec;
69 mbstate_t mbstate;
70 const unsigned char *f = (const unsigned char *) fmt;
72 nargs = 0;
73 max_ref_arg = 0;
75 /* Search for format specifications. */
76 for (f = __find_specmb (f, &mbstate); *f != '\0'; f = spec.next_fmt)
78 /* Parse this spec. */
79 nargs += __parse_one_specmb (f, nargs, &spec, &max_ref_arg, &mbstate);
81 /* If the width is determined by an argument this is an int. */
82 if (spec.width_arg != -1 && (size_t) spec.width_arg < n)
83 argtypes[spec.width_arg] = PA_INT;
85 /* If the precision is determined by an argument this is an int. */
86 if (spec.prec_arg != -1 && (size_t) spec.prec_arg < n)
87 argtypes[spec.prec_arg] = PA_INT;
89 if ((size_t) spec.data_arg < n)
90 switch (spec.ndata_args)
92 case 0: /* No arguments. */
93 break;
94 case 1: /* One argument; we already have the type. */
95 argtypes[spec.data_arg] = spec.data_arg_type;
96 break;
97 default:
98 /* We have more than one argument for this format spec. We must
99 call the arginfo function again to determine all the types. */
100 (void) (*__printf_arginfo_table[spec.info.spec])
101 (&spec.info, n - spec.data_arg, &argtypes[spec.data_arg]);
102 break;
106 return MAX (nargs, max_ref_arg);