rework langinfo code for ABI compat and for use by time code
[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         size_t i, k;
107
108         FLOCK(f);
109
110         for (p=fmt; *p; p++) {
111
112                 alloc = 0;
113
114                 if (iswspace(*p)) {
115                         while (iswspace(p[1])) p++;
116                         while (iswspace((c=getwc(f)))) pos++;
117                         ungetwc(c, f);
118                         continue;
119                 }
120                 if (*p != '%' || p[1] == '%') {
121                         p += *p=='%';
122                         c = getwc(f);
123                         if (c!=*p) {
124                                 ungetwc(c, f);
125                                 if (c<0) goto input_fail;
126                                 goto match_fail;
127                         }
128                         pos++;
129                         continue;
130                 }
131
132                 p++;
133                 if (*p=='*') {
134                         dest = 0; p++;
135                 } else if (iswdigit(*p) && p[1]=='$') {
136                         dest = arg_n(ap, *p-'0'); p+=2;
137                 } else {
138                         dest = va_arg(ap, void *);
139                 }
140
141                 for (width=0; iswdigit(*p); p++) {
142                         width = 10*width + *p - '0';
143                 }
144
145                 if (*p=='m') {
146                         alloc = !!dest;
147                         p++;
148                 } else {
149                         alloc = 0;
150                 }
151
152                 size = SIZE_def;
153                 switch (*p++) {
154                 case 'h':
155                         if (*p == 'h') p++, size = SIZE_hh;
156                         else size = SIZE_h;
157                         break;
158                 case 'l':
159                         if (*p == 'l') p++, size = SIZE_ll;
160                         else size = SIZE_l;
161                         break;
162                 case 'j':
163                         size = SIZE_ll;
164                         break;
165                 case 'z':
166                 case 't':
167                         size = SIZE_l;
168                         break;
169                 case 'L':
170                         size = SIZE_L;
171                         break;
172                 case 'd': case 'i': case 'o': case 'u': case 'x':
173                 case 'a': case 'e': case 'f': case 'g':
174                 case 'A': case 'E': case 'F': case 'G': case 'X':
175                 case 's': case 'c': case '[':
176                 case 'S': case 'C':
177                 case 'p': case 'n':
178                         p--;
179                         break;
180                 default:
181                         goto fmt_fail;
182                 }
183
184                 t = *p;
185
186                 /* Transform S,C -> ls,lc */
187                 if ((t&0x2f)==3) {
188                         size = SIZE_l;
189                         t |= 32;
190                 }
191
192                 if (t != 'n') {
193                         if (t != '[' && (t|32) != 'c')
194                                 while (iswspace((c=getwc(f)))) pos++;
195                         else
196                                 c=getwc(f);
197                         if (c < 0) goto input_fail;
198                         ungetwc(c, f);
199                 }
200
201                 switch (t) {
202                 case 'n':
203                         store_int(dest, size, pos);
204                         /* do not increment match count, etc! */
205                         continue;
206
207                 case 's':
208                 case 'c':
209                 case '[':
210                         if (t == 'c') {
211                                 if (width<1) width = 1;
212                                 invert = 1;
213                                 set = L"";
214                         } else if (t == 's') {
215                                 invert = 1;
216                                 set = (const wchar_t[]){
217                                         ' ', '\t', '\n', '\r', 11, 12,  0x0085,
218                                         0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
219                                         0x2006, 0x2008, 0x2009, 0x200a,
220                                         0x2028, 0x2029, 0x205f, 0x3000, 0 };
221                         } else {
222                                 if (*++p == '^') p++, invert = 1;
223                                 else invert = 0;
224                                 set = p;
225                                 if (*p==']') p++;
226                                 while (*p!=']') {
227                                         if (!*p) goto fmt_fail;
228                                         p++;
229                                 }
230                         }
231
232                         s = (size == SIZE_def) ? dest : 0;
233                         wcs = (size == SIZE_l) ? dest : 0;
234
235                         int gotmatch = 0;
236
237                         if (width < 1) width = -1;
238
239                         i = 0;
240                         if (alloc) {
241                                 k = t=='c' ? width+1U : 31;
242                                 if (size == SIZE_l) {
243                                         wcs = malloc(k*sizeof(wchar_t));
244                                         if (!wcs) goto alloc_fail;
245                                 } else {
246                                         s = malloc(k);
247                                         if (!s) goto alloc_fail;
248                                 }
249                         }
250                         while (width) {
251                                 if ((c=getwc(f))<0) break;
252                                 if (in_set(set, c) == invert)
253                                         break;
254                                 if (wcs) {
255                                         wcs[i++] = c;
256                                         if (alloc && i==k) {
257                                                 k += k+1;
258                                                 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
259                                                 if (!tmp) goto alloc_fail;
260                                                 wcs = tmp;
261                                         }
262                                 } else if (size != SIZE_l) {
263                                         int l = wctomb(s?s+i:tmp, c);
264                                         if (l<0) goto input_fail;
265                                         i += l;
266                                         if (alloc && i > k-4) {
267                                                 k += k+1;
268                                                 char *tmp = realloc(s, k);
269                                                 if (!tmp) goto alloc_fail;
270                                                 s = tmp;
271                                         }
272                                 }
273                                 pos++;
274                                 width-=(width>0);
275                                 gotmatch=1;
276                         }
277                         if (width) {
278                                 ungetwc(c, f);
279                                 if (t == 'c' || !gotmatch) goto match_fail;
280                         }
281
282                         if (alloc) {
283                                 if (size == SIZE_l) *(wchar_t **)dest = wcs;
284                                 else *(char **)dest = s;
285                         }
286                         if (t != 'c') {
287                                 if (wcs) wcs[i] = 0;
288                                 if (s) s[i] = 0;
289                         }
290                         break;
291
292                 case 'd': case 'i': case 'o': case 'u': case 'x':
293                 case 'a': case 'e': case 'f': case 'g':
294                 case 'A': case 'E': case 'F': case 'G': case 'X':
295                 case 'p':
296                         if (width < 1) width = 0;
297                         snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
298                                 1+!dest, "%*", width, size_pfx[size+2], t);
299                         cnt = 0;
300                         if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
301                                 goto input_fail;
302                         else if (!cnt)
303                                 goto match_fail;
304                         pos += cnt;
305                         break;
306                 default:
307                         goto fmt_fail;
308                 }
309
310                 if (dest) matches++;
311         }
312         if (0) {
313 fmt_fail:
314 alloc_fail:
315 input_fail:
316                 if (!matches) matches--;
317 match_fail:
318                 if (alloc) {
319                         free(s);
320                         free(wcs);
321                 }
322         }
323         FUNLOCK(f);
324         return matches;
325 }
326
327 weak_alias(vfwscanf,__isoc99_vfwscanf);