d0756de7d41fe3e8b85186962032a1c7eec1e60e
[platform/upstream/glibc.git] / stdio-common / printf-prs.c
1 /* Copyright (C) 1991, 1992, 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
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 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB.  If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA.  */
18
19 #include <stdio.h>
20 #include <printf.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24
25 #ifndef COMPILE_WPRINTF
26 # define CHAR_T         char
27 # define UCHAR_T        unsigned char
28 # define INT_T          int
29 # define L_(Str)        Str
30 # define ISDIGIT(Ch)    isdigit (Ch)
31
32 # ifdef USE_IN_LIBIO
33 #  define PUT(F, S, N)  _IO_sputn (F, S, N)
34 #  define PAD(Padchar)                                                        \
35   if (width > 0)                                                              \
36     done += _IO_padn (s, Padchar, width)
37 # else
38 #  define PUTC(C, F)    putc (C, F)
39 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
40 # define PAD(Padchar)                                                         \
41   if (width > 0)                                                              \
42     { if (__printf_pad (s, Padchar, width) == -1)                             \
43         return -1; else done += width; }
44 # endif
45 #else
46 # define vfprintf       vfwprintf
47 # define CHAR_T         wchar_t
48 # define UCHAR_T        uwchar_t
49 # define INT_T          wint_t
50 # define L_(Str)        L##Str
51 # define ISDIGIT(Ch)    iswdigit (Ch)
52
53 # ifdef USE_IN_LIBIO
54 # define PUT(F, S, N)   _IO_sputn (F, S, N)
55 # define PAD(Padchar)                                                         \
56   if (width > 0)                                                              \
57     done += _IO_wpadn (s, Padchar, width)
58 # else
59 #  define PUTC(C, F)    wputc (C, F)
60 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
61 # define PAD(Padchar)                                                         \
62   if (width > 0)                                                              \
63     { if (__wprintf_pad (s, Padchar, width) == -1)                            \
64         return -1; else done += width; }
65 # endif
66 #endif
67
68 #include "printf-parse.h"
69
70
71 size_t
72 parse_printf_format (fmt, n, argtypes)
73       const char *fmt;
74       size_t n;
75       int *argtypes;
76 {
77   size_t nargs;                 /* Number of arguments.  */
78   size_t max_ref_arg;           /* Highest index used in a positional arg.  */
79   struct printf_spec spec;
80   mbstate_t mbstate;
81
82   nargs = 0;
83   max_ref_arg = 0;
84   mbstate = 0;
85
86   /* Search for format specifications.  */
87   for (fmt = find_spec (fmt, &mbstate); *fmt != '\0'; fmt = spec.next_fmt)
88     {
89       /* Parse this spec.  */
90       nargs += parse_one_spec (fmt, nargs, &spec, &max_ref_arg, &mbstate);
91
92       /* If the width is determined by an argument this is an int.  */
93       if (spec.width_arg != -1 && spec.width_arg < n)
94         argtypes[spec.width_arg] = PA_INT;
95
96       /* If the precision is determined by an argument this is an int.  */
97       if (spec.prec_arg != -1 && spec.prec_arg < n)
98         argtypes[spec.prec_arg] = PA_INT;
99
100       if (spec.data_arg < n)
101         switch (spec.ndata_args)
102           {
103           case 0:               /* No arguments.  */
104             break;
105           case 1:               /* One argument; we already have the type.  */
106             argtypes[spec.data_arg] = spec.data_arg_type;
107             break;
108           default:
109             /* We have more than one argument for this format spec.  We must
110                call the arginfo function again to determine all the types.  */
111             (void) (*__printf_arginfo_table[spec.info.spec])
112               (&spec.info, n - spec.data_arg, &argtypes[spec.data_arg]);
113             break;
114           }
115     }
116
117   return MAX (nargs, max_ref_arg);
118 }