1/* Parse printf format string.
2 Copyright (C) 1999, 2002-2003, 2005, 2007, 2010-2019 Free Software
3 Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, see <https://www.gnu.org/licenses/>. */
17
18#ifndef _PRINTF_PARSE_H
19#define _PRINTF_PARSE_H
20
21/* This file can be parametrized with the following macros:
22 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
23 STATIC Set to 'static' to declare the function static. */
24
25#if HAVE_FEATURES_H
26# include <features.h> /* for __GLIBC__, __UCLIBC__ */
27#endif
28
29#include "printf-args.h"
30
31
32/* Flags */
33#define FLAG_GROUP 1 /* ' flag */
34#define FLAG_LEFT 2 /* - flag */
35#define FLAG_SHOWSIGN 4 /* + flag */
36#define FLAG_SPACE 8 /* space flag */
37#define FLAG_ALT 16 /* # flag */
38#define FLAG_ZERO 32
39#if __GLIBC__ >= 2 && !defined __UCLIBC__
40# define FLAG_LOCALIZED 64 /* I flag, uses localized digits */
41#endif
42
43/* arg_index value indicating that no argument is consumed. */
44#define ARG_NONE (~(size_t)0)
45
46/* xxx_directive: A parsed directive.
47 xxx_directives: A parsed format string. */
48
49/* Number of directly allocated directives (no malloc() needed). */
50#define N_DIRECT_ALLOC_DIRECTIVES 7
51
52/* A parsed directive. */
53typedef struct
54{
55 const char* dir_start;
56 const char* dir_end;
57 int flags;
58 const char* width_start;
59 const char* width_end;
60 size_t width_arg_index;
61 const char* precision_start;
62 const char* precision_end;
63 size_t precision_arg_index;
64 char conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
65 size_t arg_index;
66}
67char_directive;
68
69/* A parsed format string. */
70typedef struct
71{
72 size_t count;
73 char_directive *dir;
74 size_t max_width_length;
75 size_t max_precision_length;
76 char_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
77}
78char_directives;
79
80#if ENABLE_UNISTDIO
81
82/* A parsed directive. */
83typedef struct
84{
85 const uint8_t* dir_start;
86 const uint8_t* dir_end;
87 int flags;
88 const uint8_t* width_start;
89 const uint8_t* width_end;
90 size_t width_arg_index;
91 const uint8_t* precision_start;
92 const uint8_t* precision_end;
93 size_t precision_arg_index;
94 uint8_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
95 size_t arg_index;
96}
97u8_directive;
98
99/* A parsed format string. */
100typedef struct
101{
102 size_t count;
103 u8_directive *dir;
104 size_t max_width_length;
105 size_t max_precision_length;
106 u8_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
107}
108u8_directives;
109
110/* A parsed directive. */
111typedef struct
112{
113 const uint16_t* dir_start;
114 const uint16_t* dir_end;
115 int flags;
116 const uint16_t* width_start;
117 const uint16_t* width_end;
118 size_t width_arg_index;
119 const uint16_t* precision_start;
120 const uint16_t* precision_end;
121 size_t precision_arg_index;
122 uint16_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
123 size_t arg_index;
124}
125u16_directive;
126
127/* A parsed format string. */
128typedef struct
129{
130 size_t count;
131 u16_directive *dir;
132 size_t max_width_length;
133 size_t max_precision_length;
134 u16_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
135}
136u16_directives;
137
138/* A parsed directive. */
139typedef struct
140{
141 const uint32_t* dir_start;
142 const uint32_t* dir_end;
143 int flags;
144 const uint32_t* width_start;
145 const uint32_t* width_end;
146 size_t width_arg_index;
147 const uint32_t* precision_start;
148 const uint32_t* precision_end;
149 size_t precision_arg_index;
150 uint32_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
151 size_t arg_index;
152}
153u32_directive;
154
155/* A parsed format string. */
156typedef struct
157{
158 size_t count;
159 u32_directive *dir;
160 size_t max_width_length;
161 size_t max_precision_length;
162 u32_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
163}
164u32_directives;
165
166#endif
167
168
169/* Parses the format string. Fills in the number N of directives, and fills
170 in directives[0], ..., directives[N-1], and sets directives[N].dir_start
171 to the end of the format string. Also fills in the arg_type fields of the
172 arguments and the needed count of arguments. */
173#if ENABLE_UNISTDIO
174extern int
175 ulc_printf_parse (const char *format, char_directives *d, arguments *a);
176extern int
177 u8_printf_parse (const uint8_t *format, u8_directives *d, arguments *a);
178extern int
179 u16_printf_parse (const uint16_t *format, u16_directives *d,
180 arguments *a);
181extern int
182 u32_printf_parse (const uint32_t *format, u32_directives *d,
183 arguments *a);
184#else
185# ifdef STATIC
186STATIC
187# else
188extern
189# endif
190int printf_parse (const char *format, char_directives *d, arguments *a);
191#endif
192
193#endif /* _PRINTF_PARSE_H */
194