printf-parse.c revision 1.1 1 1.1 christos /* Formatted output to strings.
2 1.1 christos Copyright (C) 1999-2000, 2002-2003 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This program is free software; you can redistribute it and/or modify
5 1.1 christos it under the terms of the GNU General Public License as published by
6 1.1 christos the Free Software Foundation; either version 2, or (at your option)
7 1.1 christos any later version.
8 1.1 christos
9 1.1 christos This program is distributed in the hope that it will be useful,
10 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
11 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 1.1 christos GNU General Public License for more details.
13 1.1 christos
14 1.1 christos You should have received a copy of the GNU General Public License along
15 1.1 christos with this program; if not, write to the Free Software Foundation,
16 1.1 christos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 1.1 christos
18 1.1 christos #ifdef HAVE_CONFIG_H
19 1.1 christos # include <config.h>
20 1.1 christos #endif
21 1.1 christos
22 1.1 christos /* Specification. */
23 1.1 christos #if WIDE_CHAR_VERSION
24 1.1 christos # include "wprintf-parse.h"
25 1.1 christos #else
26 1.1 christos # include "printf-parse.h"
27 1.1 christos #endif
28 1.1 christos
29 1.1 christos /* Get size_t, NULL. */
30 1.1 christos #include <stddef.h>
31 1.1 christos
32 1.1 christos /* Get intmax_t. */
33 1.1 christos #if HAVE_STDINT_H_WITH_UINTMAX
34 1.1 christos # include <stdint.h>
35 1.1 christos #endif
36 1.1 christos #if HAVE_INTTYPES_H_WITH_UINTMAX
37 1.1 christos # include <inttypes.h>
38 1.1 christos #endif
39 1.1 christos
40 1.1 christos /* malloc(), realloc(), free(). */
41 1.1 christos #include <stdlib.h>
42 1.1 christos
43 1.1 christos /* Checked size_t computations. */
44 1.1 christos #include "xsize.h"
45 1.1 christos
46 1.1 christos #if WIDE_CHAR_VERSION
47 1.1 christos # define PRINTF_PARSE wprintf_parse
48 1.1 christos # define CHAR_T wchar_t
49 1.1 christos # define DIRECTIVE wchar_t_directive
50 1.1 christos # define DIRECTIVES wchar_t_directives
51 1.1 christos #else
52 1.1 christos # define PRINTF_PARSE printf_parse
53 1.1 christos # define CHAR_T char
54 1.1 christos # define DIRECTIVE char_directive
55 1.1 christos # define DIRECTIVES char_directives
56 1.1 christos #endif
57 1.1 christos
58 1.1 christos #ifdef STATIC
59 1.1 christos STATIC
60 1.1 christos #endif
61 1.1 christos int
62 1.1 christos PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
63 1.1 christos {
64 1.1 christos const CHAR_T *cp = format; /* pointer into format */
65 1.1 christos size_t arg_posn = 0; /* number of regular arguments consumed */
66 1.1 christos size_t d_allocated; /* allocated elements of d->dir */
67 1.1 christos size_t a_allocated; /* allocated elements of a->arg */
68 1.1 christos size_t max_width_length = 0;
69 1.1 christos size_t max_precision_length = 0;
70 1.1 christos
71 1.1 christos d->count = 0;
72 1.1 christos d_allocated = 1;
73 1.1 christos d->dir = malloc (d_allocated * sizeof (DIRECTIVE));
74 1.1 christos if (d->dir == NULL)
75 1.1 christos /* Out of memory. */
76 1.1 christos return -1;
77 1.1 christos
78 1.1 christos a->count = 0;
79 1.1 christos a_allocated = 0;
80 1.1 christos a->arg = NULL;
81 1.1 christos
82 1.1 christos #define REGISTER_ARG(_index_,_type_) \
83 1.1 christos { \
84 1.1 christos size_t n = (_index_); \
85 1.1 christos if (n >= a_allocated) \
86 1.1 christos { \
87 1.1 christos size_t memory_size; \
88 1.1 christos argument *memory; \
89 1.1 christos \
90 1.1 christos a_allocated = xtimes (a_allocated, 2); \
91 1.1 christos if (a_allocated <= n) \
92 1.1 christos a_allocated = xsum (n, 1); \
93 1.1 christos memory_size = xtimes (a_allocated, sizeof (argument)); \
94 1.1 christos if (size_overflow_p (memory_size)) \
95 1.1 christos /* Overflow, would lead to out of memory. */ \
96 1.1 christos goto error; \
97 1.1 christos memory = (a->arg \
98 1.1 christos ? realloc (a->arg, memory_size) \
99 1.1 christos : malloc (memory_size)); \
100 1.1 christos if (memory == NULL) \
101 1.1 christos /* Out of memory. */ \
102 1.1 christos goto error; \
103 1.1 christos a->arg = memory; \
104 1.1 christos } \
105 1.1 christos while (a->count <= n) \
106 1.1 christos a->arg[a->count++].type = TYPE_NONE; \
107 1.1 christos if (a->arg[n].type == TYPE_NONE) \
108 1.1 christos a->arg[n].type = (_type_); \
109 1.1 christos else if (a->arg[n].type != (_type_)) \
110 1.1 christos /* Ambiguous type for positional argument. */ \
111 1.1 christos goto error; \
112 1.1 christos }
113 1.1 christos
114 1.1 christos while (*cp != '\0')
115 1.1 christos {
116 1.1 christos CHAR_T c = *cp++;
117 1.1 christos if (c == '%')
118 1.1 christos {
119 1.1 christos size_t arg_index = ARG_NONE;
120 1.1 christos DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
121 1.1 christos
122 1.1 christos /* Initialize the next directive. */
123 1.1 christos dp->dir_start = cp - 1;
124 1.1 christos dp->flags = 0;
125 1.1 christos dp->width_start = NULL;
126 1.1 christos dp->width_end = NULL;
127 1.1 christos dp->width_arg_index = ARG_NONE;
128 1.1 christos dp->precision_start = NULL;
129 1.1 christos dp->precision_end = NULL;
130 1.1 christos dp->precision_arg_index = ARG_NONE;
131 1.1 christos dp->arg_index = ARG_NONE;
132 1.1 christos
133 1.1 christos /* Test for positional argument. */
134 1.1 christos if (*cp >= '0' && *cp <= '9')
135 1.1 christos {
136 1.1 christos const CHAR_T *np;
137 1.1 christos
138 1.1 christos for (np = cp; *np >= '0' && *np <= '9'; np++)
139 1.1 christos ;
140 1.1 christos if (*np == '$')
141 1.1 christos {
142 1.1 christos size_t n = 0;
143 1.1 christos
144 1.1 christos for (np = cp; *np >= '0' && *np <= '9'; np++)
145 1.1 christos n = xsum (xtimes (n, 10), *np - '0');
146 1.1 christos if (n == 0)
147 1.1 christos /* Positional argument 0. */
148 1.1 christos goto error;
149 1.1 christos if (size_overflow_p (n))
150 1.1 christos /* n too large, would lead to out of memory later. */
151 1.1 christos goto error;
152 1.1 christos arg_index = n - 1;
153 1.1 christos cp = np + 1;
154 1.1 christos }
155 1.1 christos }
156 1.1 christos
157 1.1 christos /* Read the flags. */
158 1.1 christos for (;;)
159 1.1 christos {
160 1.1 christos if (*cp == '\'')
161 1.1 christos {
162 1.1 christos dp->flags |= FLAG_GROUP;
163 1.1 christos cp++;
164 1.1 christos }
165 1.1 christos else if (*cp == '-')
166 1.1 christos {
167 1.1 christos dp->flags |= FLAG_LEFT;
168 1.1 christos cp++;
169 1.1 christos }
170 1.1 christos else if (*cp == '+')
171 1.1 christos {
172 1.1 christos dp->flags |= FLAG_SHOWSIGN;
173 1.1 christos cp++;
174 1.1 christos }
175 1.1 christos else if (*cp == ' ')
176 1.1 christos {
177 1.1 christos dp->flags |= FLAG_SPACE;
178 1.1 christos cp++;
179 1.1 christos }
180 1.1 christos else if (*cp == '#')
181 1.1 christos {
182 1.1 christos dp->flags |= FLAG_ALT;
183 1.1 christos cp++;
184 1.1 christos }
185 1.1 christos else if (*cp == '0')
186 1.1 christos {
187 1.1 christos dp->flags |= FLAG_ZERO;
188 1.1 christos cp++;
189 1.1 christos }
190 1.1 christos else
191 1.1 christos break;
192 1.1 christos }
193 1.1 christos
194 1.1 christos /* Parse the field width. */
195 1.1 christos if (*cp == '*')
196 1.1 christos {
197 1.1 christos dp->width_start = cp;
198 1.1 christos cp++;
199 1.1 christos dp->width_end = cp;
200 1.1 christos if (max_width_length < 1)
201 1.1 christos max_width_length = 1;
202 1.1 christos
203 1.1 christos /* Test for positional argument. */
204 1.1 christos if (*cp >= '0' && *cp <= '9')
205 1.1 christos {
206 1.1 christos const CHAR_T *np;
207 1.1 christos
208 1.1 christos for (np = cp; *np >= '0' && *np <= '9'; np++)
209 1.1 christos ;
210 1.1 christos if (*np == '$')
211 1.1 christos {
212 1.1 christos size_t n = 0;
213 1.1 christos
214 1.1 christos for (np = cp; *np >= '0' && *np <= '9'; np++)
215 1.1 christos n = xsum (xtimes (n, 10), *np - '0');
216 1.1 christos if (n == 0)
217 1.1 christos /* Positional argument 0. */
218 1.1 christos goto error;
219 1.1 christos if (size_overflow_p (n))
220 1.1 christos /* n too large, would lead to out of memory later. */
221 1.1 christos goto error;
222 1.1 christos dp->width_arg_index = n - 1;
223 1.1 christos cp = np + 1;
224 1.1 christos }
225 1.1 christos }
226 1.1 christos if (dp->width_arg_index == ARG_NONE)
227 1.1 christos {
228 1.1 christos dp->width_arg_index = arg_posn++;
229 1.1 christos if (dp->width_arg_index == ARG_NONE)
230 1.1 christos /* arg_posn wrapped around. */
231 1.1 christos goto error;
232 1.1 christos }
233 1.1 christos REGISTER_ARG (dp->width_arg_index, TYPE_INT);
234 1.1 christos }
235 1.1 christos else if (*cp >= '0' && *cp <= '9')
236 1.1 christos {
237 1.1 christos size_t width_length;
238 1.1 christos
239 1.1 christos dp->width_start = cp;
240 1.1 christos for (; *cp >= '0' && *cp <= '9'; cp++)
241 1.1 christos ;
242 1.1 christos dp->width_end = cp;
243 1.1 christos width_length = dp->width_end - dp->width_start;
244 1.1 christos if (max_width_length < width_length)
245 1.1 christos max_width_length = width_length;
246 1.1 christos }
247 1.1 christos
248 1.1 christos /* Parse the precision. */
249 1.1 christos if (*cp == '.')
250 1.1 christos {
251 1.1 christos cp++;
252 1.1 christos if (*cp == '*')
253 1.1 christos {
254 1.1 christos dp->precision_start = cp - 1;
255 1.1 christos cp++;
256 1.1 christos dp->precision_end = cp;
257 1.1 christos if (max_precision_length < 2)
258 1.1 christos max_precision_length = 2;
259 1.1 christos
260 1.1 christos /* Test for positional argument. */
261 1.1 christos if (*cp >= '0' && *cp <= '9')
262 1.1 christos {
263 1.1 christos const CHAR_T *np;
264 1.1 christos
265 1.1 christos for (np = cp; *np >= '0' && *np <= '9'; np++)
266 1.1 christos ;
267 1.1 christos if (*np == '$')
268 1.1 christos {
269 1.1 christos size_t n = 0;
270 1.1 christos
271 1.1 christos for (np = cp; *np >= '0' && *np <= '9'; np++)
272 1.1 christos n = xsum (xtimes (n, 10), *np - '0');
273 1.1 christos if (n == 0)
274 1.1 christos /* Positional argument 0. */
275 1.1 christos goto error;
276 1.1 christos if (size_overflow_p (n))
277 1.1 christos /* n too large, would lead to out of memory
278 1.1 christos later. */
279 1.1 christos goto error;
280 1.1 christos dp->precision_arg_index = n - 1;
281 1.1 christos cp = np + 1;
282 1.1 christos }
283 1.1 christos }
284 1.1 christos if (dp->precision_arg_index == ARG_NONE)
285 1.1 christos {
286 1.1 christos dp->precision_arg_index = arg_posn++;
287 1.1 christos if (dp->precision_arg_index == ARG_NONE)
288 1.1 christos /* arg_posn wrapped around. */
289 1.1 christos goto error;
290 1.1 christos }
291 1.1 christos REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
292 1.1 christos }
293 1.1 christos else
294 1.1 christos {
295 1.1 christos size_t precision_length;
296 1.1 christos
297 1.1 christos dp->precision_start = cp - 1;
298 1.1 christos for (; *cp >= '0' && *cp <= '9'; cp++)
299 1.1 christos ;
300 1.1 christos dp->precision_end = cp;
301 1.1 christos precision_length = dp->precision_end - dp->precision_start;
302 1.1 christos if (max_precision_length < precision_length)
303 1.1 christos max_precision_length = precision_length;
304 1.1 christos }
305 1.1 christos }
306 1.1 christos
307 1.1 christos {
308 1.1 christos arg_type type;
309 1.1 christos
310 1.1 christos /* Parse argument type/size specifiers. */
311 1.1 christos {
312 1.1 christos int flags = 0;
313 1.1 christos
314 1.1 christos for (;;)
315 1.1 christos {
316 1.1 christos if (*cp == 'h')
317 1.1 christos {
318 1.1 christos flags |= (1 << (flags & 1));
319 1.1 christos cp++;
320 1.1 christos }
321 1.1 christos else if (*cp == 'L')
322 1.1 christos {
323 1.1 christos flags |= 4;
324 1.1 christos cp++;
325 1.1 christos }
326 1.1 christos else if (*cp == 'l')
327 1.1 christos {
328 1.1 christos flags += 8;
329 1.1 christos cp++;
330 1.1 christos }
331 1.1 christos #ifdef HAVE_INTMAX_T
332 1.1 christos else if (*cp == 'j')
333 1.1 christos {
334 1.1 christos if (sizeof (intmax_t) > sizeof (long))
335 1.1 christos {
336 1.1 christos /* intmax_t = long long */
337 1.1 christos flags += 16;
338 1.1 christos }
339 1.1 christos else if (sizeof (intmax_t) > sizeof (int))
340 1.1 christos {
341 1.1 christos /* intmax_t = long */
342 1.1 christos flags += 8;
343 1.1 christos }
344 1.1 christos cp++;
345 1.1 christos }
346 1.1 christos #endif
347 1.1 christos else if (*cp == 'z' || *cp == 'Z')
348 1.1 christos {
349 1.1 christos /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
350 1.1 christos because the warning facility in gcc-2.95.2 understands
351 1.1 christos only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
352 1.1 christos if (sizeof (size_t) > sizeof (long))
353 1.1 christos {
354 1.1 christos /* size_t = long long */
355 1.1 christos flags += 16;
356 1.1 christos }
357 1.1 christos else if (sizeof (size_t) > sizeof (int))
358 1.1 christos {
359 1.1 christos /* size_t = long */
360 1.1 christos flags += 8;
361 1.1 christos }
362 1.1 christos cp++;
363 1.1 christos }
364 1.1 christos else if (*cp == 't')
365 1.1 christos {
366 1.1 christos if (sizeof (ptrdiff_t) > sizeof (long))
367 1.1 christos {
368 1.1 christos /* ptrdiff_t = long long */
369 1.1 christos flags += 16;
370 1.1 christos }
371 1.1 christos else if (sizeof (ptrdiff_t) > sizeof (int))
372 1.1 christos {
373 1.1 christos /* ptrdiff_t = long */
374 1.1 christos flags += 8;
375 1.1 christos }
376 1.1 christos cp++;
377 1.1 christos }
378 1.1 christos else
379 1.1 christos break;
380 1.1 christos }
381 1.1 christos
382 1.1 christos /* Read the conversion character. */
383 1.1 christos c = *cp++;
384 1.1 christos switch (c)
385 1.1 christos {
386 1.1 christos case 'd': case 'i':
387 1.1 christos #ifdef HAVE_LONG_LONG
388 1.1 christos if (flags >= 16 || (flags & 4))
389 1.1 christos type = TYPE_LONGLONGINT;
390 1.1 christos else
391 1.1 christos #endif
392 1.1 christos if (flags >= 8)
393 1.1 christos type = TYPE_LONGINT;
394 1.1 christos else if (flags & 2)
395 1.1 christos type = TYPE_SCHAR;
396 1.1 christos else if (flags & 1)
397 1.1 christos type = TYPE_SHORT;
398 1.1 christos else
399 1.1 christos type = TYPE_INT;
400 1.1 christos break;
401 1.1 christos case 'o': case 'u': case 'x': case 'X':
402 1.1 christos #ifdef HAVE_LONG_LONG
403 1.1 christos if (flags >= 16 || (flags & 4))
404 1.1 christos type = TYPE_ULONGLONGINT;
405 1.1 christos else
406 1.1 christos #endif
407 1.1 christos if (flags >= 8)
408 1.1 christos type = TYPE_ULONGINT;
409 1.1 christos else if (flags & 2)
410 1.1 christos type = TYPE_UCHAR;
411 1.1 christos else if (flags & 1)
412 1.1 christos type = TYPE_USHORT;
413 1.1 christos else
414 1.1 christos type = TYPE_UINT;
415 1.1 christos break;
416 1.1 christos case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
417 1.1 christos case 'a': case 'A':
418 1.1 christos #ifdef HAVE_LONG_DOUBLE
419 1.1 christos if (flags >= 16 || (flags & 4))
420 1.1 christos type = TYPE_LONGDOUBLE;
421 1.1 christos else
422 1.1 christos #endif
423 1.1 christos type = TYPE_DOUBLE;
424 1.1 christos break;
425 1.1 christos case 'c':
426 1.1 christos if (flags >= 8)
427 1.1 christos #ifdef HAVE_WINT_T
428 1.1 christos type = TYPE_WIDE_CHAR;
429 1.1 christos #else
430 1.1 christos goto error;
431 1.1 christos #endif
432 1.1 christos else
433 1.1 christos type = TYPE_CHAR;
434 1.1 christos break;
435 1.1 christos #ifdef HAVE_WINT_T
436 1.1 christos case 'C':
437 1.1 christos type = TYPE_WIDE_CHAR;
438 1.1 christos c = 'c';
439 1.1 christos break;
440 1.1 christos #endif
441 1.1 christos case 's':
442 1.1 christos if (flags >= 8)
443 1.1 christos #ifdef HAVE_WCHAR_T
444 1.1 christos type = TYPE_WIDE_STRING;
445 1.1 christos #else
446 1.1 christos goto error;
447 1.1 christos #endif
448 1.1 christos else
449 1.1 christos type = TYPE_STRING;
450 1.1 christos break;
451 1.1 christos #ifdef HAVE_WCHAR_T
452 1.1 christos case 'S':
453 1.1 christos type = TYPE_WIDE_STRING;
454 1.1 christos c = 's';
455 1.1 christos break;
456 1.1 christos #endif
457 1.1 christos case 'p':
458 1.1 christos type = TYPE_POINTER;
459 1.1 christos break;
460 1.1 christos case 'n':
461 1.1 christos #ifdef HAVE_LONG_LONG
462 1.1 christos if (flags >= 16 || (flags & 4))
463 1.1 christos type = TYPE_COUNT_LONGLONGINT_POINTER;
464 1.1 christos else
465 1.1 christos #endif
466 1.1 christos if (flags >= 8)
467 1.1 christos type = TYPE_COUNT_LONGINT_POINTER;
468 1.1 christos else if (flags & 2)
469 1.1 christos type = TYPE_COUNT_SCHAR_POINTER;
470 1.1 christos else if (flags & 1)
471 1.1 christos type = TYPE_COUNT_SHORT_POINTER;
472 1.1 christos else
473 1.1 christos type = TYPE_COUNT_INT_POINTER;
474 1.1 christos break;
475 1.1 christos case '%':
476 1.1 christos type = TYPE_NONE;
477 1.1 christos break;
478 1.1 christos default:
479 1.1 christos /* Unknown conversion character. */
480 1.1 christos goto error;
481 1.1 christos }
482 1.1 christos }
483 1.1 christos
484 1.1 christos if (type != TYPE_NONE)
485 1.1 christos {
486 1.1 christos dp->arg_index = arg_index;
487 1.1 christos if (dp->arg_index == ARG_NONE)
488 1.1 christos {
489 1.1 christos dp->arg_index = arg_posn++;
490 1.1 christos if (dp->arg_index == ARG_NONE)
491 1.1 christos /* arg_posn wrapped around. */
492 1.1 christos goto error;
493 1.1 christos }
494 1.1 christos REGISTER_ARG (dp->arg_index, type);
495 1.1 christos }
496 1.1 christos dp->conversion = c;
497 1.1 christos dp->dir_end = cp;
498 1.1 christos }
499 1.1 christos
500 1.1 christos d->count++;
501 1.1 christos if (d->count >= d_allocated)
502 1.1 christos {
503 1.1 christos size_t memory_size;
504 1.1 christos DIRECTIVE *memory;
505 1.1 christos
506 1.1 christos d_allocated = xtimes (d_allocated, 2);
507 1.1 christos memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
508 1.1 christos if (size_overflow_p (memory_size))
509 1.1 christos /* Overflow, would lead to out of memory. */
510 1.1 christos goto error;
511 1.1 christos memory = realloc (d->dir, memory_size);
512 1.1 christos if (memory == NULL)
513 1.1 christos /* Out of memory. */
514 1.1 christos goto error;
515 1.1 christos d->dir = memory;
516 1.1 christos }
517 1.1 christos }
518 1.1 christos }
519 1.1 christos d->dir[d->count].dir_start = cp;
520 1.1 christos
521 1.1 christos d->max_width_length = max_width_length;
522 1.1 christos d->max_precision_length = max_precision_length;
523 1.1 christos return 0;
524 1.1 christos
525 1.1 christos error:
526 1.1 christos if (a->arg)
527 1.1 christos free (a->arg);
528 1.1 christos if (d->dir)
529 1.1 christos free (d->dir);
530 1.1 christos return -1;
531 1.1 christos }
532 1.1 christos
533 1.1 christos #undef DIRECTIVES
534 1.1 christos #undef DIRECTIVE
535 1.1 christos #undef CHAR_T
536 1.1 christos #undef PRINTF_PARSE
537