refactor scanf core to use common code path for all string formats
[musl] / src / stdio / vfscanf.c
1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <ctype.h>
4 #include <wchar.h>
5 #include <wctype.h>
6 #include <limits.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <math.h>
10 #include <float.h>
11 #include <inttypes.h>
12
13 #include "stdio_impl.h"
14 #include "shgetc.h"
15 #include "intscan.h"
16 #include "floatscan.h"
17
18 #define SIZE_hh -2
19 #define SIZE_h  -1
20 #define SIZE_def 0
21 #define SIZE_l   1
22 #define SIZE_L   2
23 #define SIZE_ll  3
24
25 static void store_int(void *dest, int size, unsigned long long i)
26 {
27         if (!dest) return;
28         switch (size) {
29         case SIZE_hh:
30                 *(char *)dest = i;
31                 break;
32         case SIZE_h:
33                 *(short *)dest = i;
34                 break;
35         case SIZE_def:
36                 *(int *)dest = i;
37                 break;
38         case SIZE_l:
39                 *(long *)dest = i;
40                 break;
41         case SIZE_ll:
42                 *(long long *)dest = i;
43                 break;
44         }
45 }
46
47 static void *arg_n(va_list ap, unsigned int n)
48 {
49         void *p;
50         unsigned int i;
51         va_list ap2;
52         va_copy(ap2, ap);
53         for (i=n; i>1; i--) va_arg(ap2, void *);
54         p = va_arg(ap2, void *);
55         va_end(ap2);
56         return p;
57 }
58
59 static int readwc(int c, wchar_t **wcs, mbstate_t *st)
60 {
61         char ch = c;
62         wchar_t wc;
63         switch (mbrtowc(&wc, &ch, 1, st)) {
64         case -1:
65                 return -1;
66         case -2:
67                 break;
68         default:
69                 if (*wcs) *(*wcs)++ = wc;
70         }
71         return 0;
72 }
73
74 int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
75 {
76         int width;
77         int size;
78         int alloc;
79         int base;
80         const unsigned char *p;
81         int c, t;
82         char *s;
83         wchar_t *wcs;
84         mbstate_t st;
85         void *dest=NULL;
86         int invert;
87         int matches=0;
88         unsigned long long x;
89         long double y;
90         off_t pos = 0;
91         unsigned char scanset[257];
92
93         FLOCK(f);
94
95         for (p=(const unsigned char *)fmt; *p; p++) {
96
97                 if (isspace(*p)) {
98                         while (isspace(p[1])) p++;
99                         shlim(f, 0);
100                         while (isspace(shgetc(f)));
101                         shunget(f);
102                         pos += shcnt(f);
103                         continue;
104                 }
105                 if (*p != '%' || p[1] == '%') {
106                         p += *p=='%';
107                         shlim(f, 0);
108                         c = shgetc(f);
109                         if (c!=*p) {
110                                 shunget(f);
111                                 if (c<0) goto input_fail;
112                                 goto match_fail;
113                         }
114                         pos++;
115                         continue;
116                 }
117
118                 p++;
119                 if (*p=='*') {
120                         dest = 0; p++;
121                 } else if (isdigit(*p) && p[1]=='$') {
122                         dest = arg_n(ap, *p-'0'); p+=2;
123                 } else {
124                         dest = va_arg(ap, void *);
125                 }
126
127                 for (width=0; isdigit(*p); p++) {
128                         width = 10*width + *p - '0';
129                 }
130
131                 if (*p=='m') {
132                         alloc = 1;
133                         p++;
134                 } else {
135                         alloc = 0;
136                 }
137
138                 size = SIZE_def;
139                 switch (*p++) {
140                 case 'h':
141                         if (*p == 'h') p++, size = SIZE_hh;
142                         else size = SIZE_h;
143                         break;
144                 case 'l':
145                         if (*p == 'l') p++, size = SIZE_ll;
146                         else size = SIZE_l;
147                         break;
148                 case 'j':
149                         size = SIZE_ll;
150                         break;
151                 case 'z':
152                 case 't':
153                         size = SIZE_l;
154                         break;
155                 case 'L':
156                         size = SIZE_L;
157                         break;
158                 case 'd': case 'i': case 'o': case 'u': case 'x':
159                 case 'a': case 'e': case 'f': case 'g':
160                 case 'A': case 'E': case 'F': case 'G': case 'X':
161                 case 's': case 'c': case '[':
162                 case 'S': case 'C':
163                 case 'p': case 'n':
164                         p--;
165                         break;
166                 default:
167                         goto fmt_fail;
168                 }
169
170                 t = *p;
171
172                 switch (t) {
173                 case 'C':
174                         if (width < 1) width = 1;
175                 case 'S':
176                         t |= 32;
177                         size = SIZE_l;
178                         break;
179                 case 'c':
180                         if (width < 1) width = 1;
181                 case 'd': case 'i': case 'o': case 'u': case 'x':
182                 case 'a': case 'e': case 'f': case 'g':
183                 case 'A': case 'E': case 'F': case 'G': case 'X':
184                 case '[': case 's':
185                 case 'p': case 'n':
186                         break;
187                 default:
188                         goto fmt_fail;
189                 }
190
191                 if (t == 'n') {
192                         store_int(dest, size, pos);
193                         /* do not increment match count, etc! */
194                         continue;
195                 }
196
197                 if (t != '[' && (t|32) != 'c') {
198                         shlim(f, 0);
199                         while (isspace(shgetc(f)));
200                         shunget(f);
201                         pos += shcnt(f);
202                 }
203
204                 shlim(f, width);
205                 if (shgetc(f) < 0) goto input_fail;
206                 shunget(f);
207
208                 switch (t) {
209                 case 's':
210                 case 'c':
211                 case '[':
212                         if (t == 'c' || t == 's') {
213                                 memset(scanset, -1, sizeof scanset);
214                                 scanset[0] = 0;
215                                 if (t == 's') {
216                                         scanset[1+'\t'] = 0;
217                                         scanset[1+'\n'] = 0;
218                                         scanset[1+'\v'] = 0;
219                                         scanset[1+'\f'] = 0;
220                                         scanset[1+'\r'] = 0;
221                                         scanset[1+' '] = 0;
222                                 }
223                         } else {
224                                 if (*++p == '^') p++, invert = 1;
225                                 else invert = 0;
226                                 memset(scanset, invert, sizeof scanset);
227                                 scanset[0] = 0;
228                                 if (*p == '-') p++, scanset[1+'-'] = 1-invert;
229                                 else if (*p == ']') p++, scanset[1+']'] = 1-invert;
230                                 for (; *p != ']'; p++) {
231                                         if (!*p) goto fmt_fail;
232                                         if (*p=='-' && p[1] && p[1] != ']')
233                                                 for (c=p++[-1]; c<*p; c++)
234                                                         scanset[1+c] = 1-invert;
235                                         scanset[1+*p] = 1-invert;
236                                 }
237                         }
238                         wcs = 0;
239                         s = 0;
240                         if (size == SIZE_l) {
241                                 wcs = dest;
242                                 st = (mbstate_t){0};
243                                 while (scanset[(c=shgetc(f))+1]) {
244                                         if (readwc(c, &wcs, &st) < 0)
245                                                 goto input_fail;
246                                 }
247                                 if (!mbsinit(&st)) goto input_fail;
248                         } else if ((s = dest)) {
249                                 while (scanset[(c=shgetc(f))+1])
250                                         *s++ = c;
251                         } else {
252                                 while (scanset[(c=shgetc(f))+1]);
253                         }
254                         shunget(f);
255                         if (!shcnt(f)) goto match_fail;
256                         if (t == 'c' && shcnt(f) != width) goto match_fail;
257                         if (wcs) *wcs = 0;
258                         if (s) *s = 0;
259                         break;
260                 case 'p':
261                 case 'X':
262                 case 'x':
263                         base = 16;
264                         goto int_common;
265                 case 'o':
266                         base = 8;
267                         goto int_common;
268                 case 'd':
269                 case 'u':
270                         base = 10;
271                         goto int_common;
272                 case 'i':
273                         base = 0;
274                 int_common:
275                         x = __intscan(f, base, 0, ULLONG_MAX);
276                         if (!shcnt(f)) goto match_fail;
277                         if (t=='p' && dest) *(void **)dest = (void *)(uintptr_t)x;
278                         else store_int(dest, size, x);
279                         break;
280                 case 'a': case 'A':
281                 case 'e': case 'E':
282                 case 'f': case 'F':
283                 case 'g': case 'G':
284                         y = __floatscan(f, size, 0);
285                         if (!shcnt(f)) goto match_fail;
286                         if (dest) switch (size) {
287                         case SIZE_def:
288                                 *(float *)dest = y;
289                                 break;
290                         case SIZE_l:
291                                 *(double *)dest = y;
292                                 break;
293                         case SIZE_L:
294                                 *(long double *)dest = y;
295                                 break;
296                         }
297                         break;
298                 }
299
300                 pos += shcnt(f);
301                 if (dest) matches++;
302         }
303         if (0) {
304 fmt_fail:
305 input_fail:
306                 if (!matches) matches--;
307         }
308 match_fail:
309         FUNLOCK(f);
310         return matches;
311 }