xref: /haiku/src/system/libroot/posix/glibc/stdio-common/printf-parse.c (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /* Copyright (C) 1991,92,95,96,99,2000,2002 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.
18 */
19 
20 
21 #include <stdio.h>
22 #include <printf.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <wchar.h>
26 #include <sys/param.h>
27 
28 #include "../locale/localeinfo.h"
29 
30 #ifndef COMPILE_WPRINTF
31 # define CHAR_T		char
32 # define UCHAR_T	unsigned char
33 # define INT_T		int
34 # define L_(Str)	Str
35 # define ISDIGIT(Ch)	isdigit (Ch)
36 # define ISASCII(Ch)	isascii (Ch)
37 # define MBRLEN(Cp, L, St) __mbrlen (Cp, L, St)
38 
39 # define PUTC(C, F)	putc (C, F)
40 ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
41 # define PAD(Padchar)							      \
42   if (width > 0)							      \
43     { if (__printf_pad (s, Padchar, width) == -1)			      \
44 	return -1; else done += width; }
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 # define PUTC(C, F)	wputc (C, F)
54 ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
55 # define PAD(Padchar)							      \
56   if (width > 0)							      \
57     { if (__wprintf_pad (s, Padchar, width) == -1)			      \
58 	return -1; else done += width; }
59 #endif
60 
61 #include "printf-parse.h"
62 
63 
64 size_t
65 parse_printf_format(const char *fmt, size_t n, int *argtypes)
66 {
67 	size_t nargs = 0;			/* Number of arguments. */
68 	size_t max_ref_arg = 0;		/* Highest index used in a positional arg. */
69 	struct printf_spec spec;
70 	mbstate_t mbstate;
71 
72 	/* Search for format specifications. */
73 	for (fmt = find_spec(fmt /*, &mbstate*/); *fmt != '\0'; fmt = spec.next_fmt) {
74 		/* Parse this spec. */
75 		nargs += parse_one_spec(fmt, nargs, &spec, &max_ref_arg /*, &mbstate*/);
76 
77 		/* If the width is determined by an argument this is an int. */
78 		if (spec.width_arg != -1 && (size_t)spec.width_arg < n)
79 			argtypes[spec.width_arg] = PA_INT;
80 
81 		/* If the precision is determined by an argument this is an int. */
82 		if (spec.prec_arg != -1 && (size_t) spec.prec_arg < n)
83 			argtypes[spec.prec_arg] = PA_INT;
84 
85 		if ((size_t) spec.data_arg < n) {
86 			switch (spec.ndata_args) {
87 				case 0:		/* No arguments. */
88 					break;
89 				case 1:		/* One argument; we already have the type. */
90 					argtypes[spec.data_arg] = spec.data_arg_type;
91 					break;
92 				default:
93 					/* We have more than one argument for this format spec.  We must
94 					 * call the arginfo function again to determine all the types.
95 					 */
96 					(*__printf_arginfo_table[spec.info.spec])
97 						(&spec.info, n - spec.data_arg, &argtypes[spec.data_arg]);
98 					break;
99 			}
100 		}
101 	}
102 
103 	return MAX(nargs, max_ref_arg);
104 }
105