OSDN Git Service

196af3cc6dad9bcce5d25e7eb3f19dc268264c0d
[uclinux-h8/uClibc.git] / include / printf.h
1 /* Copyright (C) 1991-1993,1995-1999,2000,2001 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 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.
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    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 /* March 11, 2001         Manuel Novoa III
19  *
20  * Modified as appropriate for my new stdio lib.
21  */
22
23 #ifndef _PRINTF_H
24
25 #define _PRINTF_H       1
26 #include <features.h>
27
28 __BEGIN_DECLS
29
30 #define __need_FILE
31 #include <stdio.h>
32 #define __need_size_t
33 #define __need_wchar_t
34 #include <stddef.h>
35
36 /* WARNING -- This is definitely nonportable... but it seems to work
37  * with gcc, which is currently the only "supported" compiler.
38  * The library code uses bitmasks for space-efficiency (you can't
39  * set/test multiple bitfields in one operation).  Unfortunatly, we
40  * need to support bitfields since that's what glibc made visible to users.
41  * So, we take
42  * advantage of how gcc lays out bitfields to create an appropriate
43  * mapping.  Inside uclibc (i.e. if _LIBC is defined) we access the
44  * bitfields using bitmasks in a single flag variable.
45  *
46  * WARNING -- This may very well fail if built with -fpack-struct!!!
47  *
48  * TODO -- Add a validation test.
49  * TODO -- Add an option to build in a shim translation function if
50  *         the bitfield<->bitmask mapping fails.
51  */
52 #include <endian.h>
53
54 struct printf_info {
55   int prec;                     /* Precision.  */
56   int width;                    /* Width.  */
57 #ifdef __UCLIBC_HAS_WCHAR__
58   wchar_t spec;                 /* Format letter.  */
59 #else
60   int spec;
61 #endif
62
63 #ifndef _LIBC
64
65 #if __BYTE_ORDER == __LITTLE_ENDIAN
66   unsigned int space:1;         /* Space flag.  */
67   unsigned int showsign:1;      /* + flag.  */
68   unsigned int extra:1;         /* For special use.  */
69   unsigned int left:1;          /* - flag.  */
70   unsigned int alt:1;           /* # flag.  */
71   unsigned int group:1;         /* ' flag.  */
72   unsigned int i18n:1;          /* I flag.  */
73   unsigned int wide:1;          /* Nonzero for wide character streams.  */
74   unsigned int is_char:1;       /* hh flag.  */
75   unsigned int is_short:1;      /* h flag.  */
76   unsigned int is_long:1;       /* l flag.  */
77   unsigned int is_long_double:1;/* L flag.  */
78   unsigned int __padding:20;    /* non-gnu: match _flags width of 32 bits */
79 #elif __BYTE_ORDER == __BIG_ENDIAN
80   unsigned int __padding:20;    /* non-gnu: match _flags width of 32 bits */
81   unsigned int is_long_double:1;/* L flag.  */
82   unsigned int is_long:1;       /* l flag.  */
83   unsigned int is_short:1;      /* h flag.  */
84   unsigned int is_char:1;       /* hh flag.  */
85   unsigned int wide:1;          /* Nonzero for wide character streams.  */
86   unsigned int i18n:1;          /* I flag.  */
87   unsigned int group:1;         /* ' flag.  */
88   unsigned int alt:1;           /* # flag.  */
89   unsigned int left:1;          /* - flag.  */
90   unsigned int extra:1;         /* For special use.  */
91   unsigned int showsign:1;      /* + flag.  */
92   unsigned int space:1;         /* Space flag.  */
93 #else
94 #error unsupported byte order!
95 #endif
96
97 #else  /* _LIBC */
98
99   uint32_t _flags;      /* non-gnu */
100 #define __PRINT_INFO_FLAG_space                 (1<<0)
101 #define __PRINT_INFO_FLAG_showsign              (1<<1)
102 #define __PRINT_INFO_FLAG_extra                 (1<<2)
103 #define __PRINT_INFO_FLAG_left                  (1<<3)
104 #define __PRINT_INFO_FLAG_alt                   (1<<4)
105 #define __PRINT_INFO_FLAG_group                 (1<<5)
106 #define __PRINT_INFO_FLAG_i18n                  (1<<6)
107 #define __PRINT_INFO_FLAG_wide                  (1<<7)
108
109 #define __PRINT_INFO_FLAG_is_char               (1<<8)
110 #define __PRINT_INFO_FLAG_is_short              (1<<9)
111 #define __PRINT_INFO_FLAG_is_long               (1<<10)
112 #define __PRINT_INFO_FLAG_is_long_double        (1<<11)
113
114 #define PRINT_INFO_FLAG_VAL(INFO_PTR,BITFIELD) \
115         ((INFO_PTR)->_flags & __PRINT_INFO_FLAG_##BITFIELD)
116 #define PRINT_INFO_SET_FLAG(INFO_PTR,BITFIELD) \
117         ((INFO_PTR)->_flags |= __PRINT_INFO_FLAG_##BITFIELD)
118 #define PRINT_INFO_CLR_FLAG(INFO_PTR,BITFIELD) \
119         ((INFO_PTR)->_flags &= ~__PRINT_INFO_FLAG_##BITFIELD)
120 #define PRINT_INFO_SET_extra(INFO_PTR,VAL) \
121         ((INFO_PTR)->_flags |= (((INFO_PTR)->_flags & ~1) | ((VAL) & 1)))
122
123 #endif /* _LIBC */
124
125 #ifdef __UCLIBC_HAS_WCHAR__
126   wchar_t pad;                  /* Padding character.  */
127 #else
128   int pad;
129 #endif
130 };
131
132
133 /* Type of a printf specifier-handler function.
134    STREAM is the FILE on which to write output.
135    INFO gives information about the format specification.
136    ARGS is a vector of pointers to the argument data;
137    the number of pointers will be the number returned
138    by the associated arginfo function for the same INFO.
139
140    The function should return the number of characters written,
141    or -1 for errors.  */
142
143 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
144 typedef int (*printf_function) (FILE *__stream,
145                              __const struct printf_info *__info,
146                              __const void *__const *__args);
147
148 /* Type of a printf specifier-arginfo function.
149    INFO gives information about the format specification.
150    N, ARGTYPES, and return value are as for parse_printf_format.  */
151
152 typedef int printf_arginfo_function (__const struct printf_info *__info,
153                                      size_t __n, int *__argtypes);
154
155
156 /* Register FUNC to be called to format SPEC specifiers; ARGINFO must be
157    specified to determine how many arguments a SPEC conversion requires and
158    what their types are.  */
159
160 extern int register_printf_function (int __spec, printf_function __func,
161                                      printf_arginfo_function __arginfo);
162 #endif
163
164
165 /* Parse FMT, and fill in N elements of ARGTYPES with the
166    types needed for the conversions FMT specifies.  Returns
167    the number of arguments required by FMT.
168
169    The ARGINFO function registered with a user-defined format is passed a
170    `struct printf_info' describing the format spec being parsed.  A width
171    or precision of INT_MIN means a `*' was used to indicate that the
172    width/precision will come from an arg.  The function should fill in the
173    array it is passed with the types of the arguments it wants, and return
174    the number of arguments it wants.  */
175
176 extern size_t parse_printf_format (__const char *__restrict __fmt, size_t __n,
177                                    int *__restrict __argtypes) __THROW;
178
179
180 /* Codes returned by `parse_printf_format' for basic types.
181
182    These values cover all the standard format specifications.
183    Users can add new values after PA_LAST for their own types.  */
184
185 /* WARNING -- The above is not entirely true, even for glibc.
186  * As far as the library code is concerned, such args are treated
187  * as 'your type' pointers if qualified by PA_FLAG_PTR.  If they
188  * aren't qualified as pointers, I _think_ glibc just ignores them
189  * and carries on.  I think it should be treated as an error. */
190
191 enum {                          /* C type: */
192   PA_INT,                       /* int */
193   PA_CHAR,                      /* int, cast to char */
194   PA_WCHAR,                     /* wide char */
195   PA_STRING,                    /* const char *, a '\0'-terminated string */
196   PA_WSTRING,                   /* const wchar_t *, wide character string */
197   PA_POINTER,                   /* void * */
198   PA_FLOAT,                     /* float */
199   PA_DOUBLE,                    /* double */
200   __PA_NOARG,                   /* non-glibc -- signals non-arg width or prec */
201   PA_LAST
202 };
203
204 /* Flag bits that can be set in a type returned by `parse_printf_format'.  */
205 /* WARNING -- These differ in value from what glibc uses. */
206 #define PA_FLAG_MASK            (0xff00)
207 #define __PA_FLAG_CHAR          (0x0100) /* non-gnu -- to deal with hh */
208 #define PA_FLAG_SHORT           (0x0200)
209 #define PA_FLAG_LONG            (0x0400)
210 #define PA_FLAG_LONG_LONG       (0x0800)
211 #define PA_FLAG_LONG_DOUBLE     PA_FLAG_LONG_LONG
212 #define PA_FLAG_PTR             (0x1000) /* TODO -- make dynamic??? */
213
214 #define __PA_INTMASK            (0x0f00) /* non-gnu -- all int flags */
215
216 #if 0
217 /* Function which can be registered as `printf'-handlers.  */
218
219 /* Print floating point value using using abbreviations for the orders
220    of magnitude used for numbers ('k' for kilo, 'm' for mega etc).  If
221    the format specifier is a uppercase character powers of 1000 are
222    used.  Otherwise powers of 1024.  */
223 extern int printf_size (FILE *__restrict __fp,
224                         __const struct printf_info *__info,
225                         __const void *__const *__restrict __args) __THROW;
226
227 /* This is the appropriate argument information function for `printf_size'.  */
228 extern int printf_size_info (__const struct printf_info *__restrict
229                              __info, size_t __n, int *__restrict __argtypes)
230      __THROW;
231
232 #endif
233
234 __END_DECLS
235
236 #endif /* printf.h  */