*** empty log message ***
[glibc.git] / stdio-common / printf-prs.c
blob7aef42d819e3d4fb738cf71c27229ff595652d89
1 /* Copyright (C) 1991,92,95,96,99,2000,2002,2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <stdio.h>
20 #include <printf.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24 #include <sys/param.h>
26 #include "../locale/localeinfo.h"
28 #ifndef COMPILE_WPRINTF
29 # define CHAR_T char
30 # define UCHAR_T unsigned char
31 # define INT_T int
32 # define L_(Str) Str
33 # define ISDIGIT(Ch) isdigit (Ch)
34 # define ISASCII(Ch) isascii (Ch)
35 # define MBRLEN(Cp, L, St) __mbrlen (Cp, L, St)
37 # ifdef USE_IN_LIBIO
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 PUTC(C, F) putc (C, F)
44 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
45 # define PAD(Padchar) \
46 if (width > 0) \
47 { if (__printf_pad (s, Padchar, width) == -1) \
48 return -1; else done += width; }
49 # endif
50 #else
51 # define vfprintf vfwprintf
52 # define CHAR_T wchar_t
53 # define UCHAR_T uwchar_t
54 # define INT_T wint_t
55 # define L_(Str) L##Str
56 # define ISDIGIT(Ch) iswdigit (Ch)
58 # ifdef USE_IN_LIBIO
59 # define PUT(F, S, N) _IO_sputn (F, S, N)
60 # define PAD(Padchar) \
61 if (width > 0) \
62 done += _IO_wpadn (s, Padchar, width)
63 # else
64 # define PUTC(C, F) wputc (C, F)
65 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
66 # define PAD(Padchar) \
67 if (width > 0) \
68 { if (__wprintf_pad (s, Padchar, width) == -1) \
69 return -1; else done += width; }
70 # endif
71 #endif
73 #define DONT_NEED_READ_INT
74 #include "printf-parse.h"
77 size_t
78 parse_printf_format (fmt, n, argtypes)
79 const char *fmt;
80 size_t n;
81 int *argtypes;
83 size_t nargs; /* Number of arguments. */
84 size_t max_ref_arg; /* Highest index used in a positional arg. */
85 struct printf_spec spec;
86 mbstate_t mbstate;
88 nargs = 0;
89 max_ref_arg = 0;
91 /* Search for format specifications. */
92 for (fmt = __find_specmb (fmt, &mbstate); *fmt != '\0'; fmt = spec.next_fmt)
94 /* Parse this spec. */
95 nargs += __parse_one_specmb (fmt, nargs, &spec, &max_ref_arg, &mbstate);
97 /* If the width is determined by an argument this is an int. */
98 if (spec.width_arg != -1 && (size_t) spec.width_arg < n)
99 argtypes[spec.width_arg] = PA_INT;
101 /* If the precision is determined by an argument this is an int. */
102 if (spec.prec_arg != -1 && (size_t) spec.prec_arg < n)
103 argtypes[spec.prec_arg] = PA_INT;
105 if ((size_t) spec.data_arg < n)
106 switch (spec.ndata_args)
108 case 0: /* No arguments. */
109 break;
110 case 1: /* One argument; we already have the type. */
111 argtypes[spec.data_arg] = spec.data_arg_type;
112 break;
113 default:
114 /* We have more than one argument for this format spec. We must
115 call the arginfo function again to determine all the types. */
116 (void) (*__printf_arginfo_table[spec.info.spec])
117 (&spec.info, n - spec.data_arg, &argtypes[spec.data_arg]);
118 break;
122 return MAX (nargs, max_ref_arg);