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