include cleanups: remove unused headers and add feature test macros
[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
10 #include "stdio_impl.h"
11 #include "shgetc.h"
12 #include "intscan.h"
13 #include "floatscan.h"
14 #include "libc.h"
15
16 #define SIZE_hh -2
17 #define SIZE_h  -1
18 #define SIZE_def 0
19 #define SIZE_l   1
20 #define SIZE_L   2
21 #define SIZE_ll  3
22
23 static void store_int(void *dest, int size, unsigned long long i)
24 {
25         if (!dest) return;
26         switch (size) {
27         case SIZE_hh:
28                 *(char *)dest = i;
29                 break;
30         case SIZE_h:
31                 *(short *)dest = i;
32                 break;
33         case SIZE_def:
34                 *(int *)dest = i;
35                 break;
36         case SIZE_l:
37                 *(long *)dest = i;
38                 break;
39         case SIZE_ll:
40                 *(long long *)dest = i;
41                 break;
42         }
43 }
44
45 static void *arg_n(va_list ap, unsigned int n)
46 {
47         void *p;
48         unsigned int i;
49         va_list ap2;
50         va_copy(ap2, ap);
51         for (i=n; i>1; i--) va_arg(ap2, void *);
52         p = va_arg(ap2, void *);
53         va_end(ap2);
54         return p;
55 }
56
57 static int in_set(const wchar_t *set, int c)
58 {
59         int j;
60         const wchar_t *p = set;
61         if (*p == '-') {
62                 if (c=='-') return 1;
63                 p++;
64         } else if (*p == ']') {
65                 if (c==']') return 1;
66                 p++;
67         }
68         for (; *p && *p != ']'; p++) {
69                 if (*p=='-' && p[1] && p[1] != ']')
70                         for (j=p++[-1]; j<*p; j++)
71                                 if (c==j) return 1;
72                 if (c==*p) return 1;
73         }
74         return 0;
75 }
76
77 #if 1
78 #undef getwc
79 #define getwc(f) \
80         ((f)->rpos < (f)->rend && *(f)->rpos < 128 ? *(f)->rpos++ : (getwc)(f))
81
82 #undef ungetwc
83 #define ungetwc(c,f) \
84         ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f)))
85 #endif
86
87 int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
88 {
89         int width;
90         int size;
91         int alloc;
92         const wchar_t *p;
93         int c, t;
94         char *s;
95         wchar_t *wcs;
96         void *dest=NULL;
97         int invert;
98         int matches=0;
99         off_t pos = 0, cnt;
100         static const char size_pfx[][3] = { "hh", "h", "", "l", "L", "ll" };
101         char tmp[3*sizeof(int)+10];
102         const wchar_t *set;
103         size_t i, k;
104
105         FLOCK(f);
106
107         for (p=fmt; *p; p++) {
108
109                 alloc = 0;
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                         wcs = 0;
144                         s = 0;
145                         alloc = !!dest;
146                         p++;
147                 } else {
148                         alloc = 0;
149                 }
150
151                 size = SIZE_def;
152                 switch (*p++) {
153                 case 'h':
154                         if (*p == 'h') p++, size = SIZE_hh;
155                         else size = SIZE_h;
156                         break;
157                 case 'l':
158                         if (*p == 'l') p++, size = SIZE_ll;
159                         else size = SIZE_l;
160                         break;
161                 case 'j':
162                         size = SIZE_ll;
163                         break;
164                 case 'z':
165                 case 't':
166                         size = SIZE_l;
167                         break;
168                 case 'L':
169                         size = SIZE_L;
170                         break;
171                 case 'd': case 'i': case 'o': case 'u': case 'x':
172                 case 'a': case 'e': case 'f': case 'g':
173                 case 'A': case 'E': case 'F': case 'G': case 'X':
174                 case 's': case 'c': case '[':
175                 case 'S': case 'C':
176                 case 'p': case 'n':
177                         p--;
178                         break;
179                 default:
180                         goto fmt_fail;
181                 }
182
183                 t = *p;
184
185                 /* Transform S,C -> ls,lc */
186                 if ((t&0x2f)==3) {
187                         size = SIZE_l;
188                         t |= 32;
189                 }
190
191                 if (t != 'n') {
192                         if (t != '[' && (t|32) != 'c')
193                                 while (iswspace((c=getwc(f)))) pos++;
194                         else
195                                 c=getwc(f);
196                         if (c < 0) goto input_fail;
197                         ungetwc(c, f);
198                 }
199
200                 switch (t) {
201                 case 'n':
202                         store_int(dest, size, pos);
203                         /* do not increment match count, etc! */
204                         continue;
205
206                 case 's':
207                 case 'c':
208                 case '[':
209                         if (t == 'c') {
210                                 if (width<1) width = 1;
211                                 invert = 1;
212                                 set = L"";
213                         } else if (t == 's') {
214                                 invert = 1;
215                                 set = (const wchar_t[]){
216                                         ' ', '\t', '\n', '\r', 11, 12,  0x0085,
217                                         0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
218                                         0x2006, 0x2008, 0x2009, 0x200a,
219                                         0x2028, 0x2029, 0x205f, 0x3000, 0 };
220                         } else {
221                                 if (*++p == '^') p++, invert = 1;
222                                 else invert = 0;
223                                 set = p;
224                                 if (*p==']') p++;
225                                 while (*p!=']') {
226                                         if (!*p) goto fmt_fail;
227                                         p++;
228                                 }
229                         }
230
231                         s = (size == SIZE_def) ? dest : 0;
232                         wcs = (size == SIZE_l) ? dest : 0;
233
234                         int gotmatch = 0;
235
236                         if (width < 1) width = -1;
237
238                         i = 0;
239                         if (alloc) {
240                                 k = t=='c' ? width+1U : 31;
241                                 if (size == SIZE_l) {
242                                         wcs = malloc(k*sizeof(wchar_t));
243                                         if (!wcs) goto alloc_fail;
244                                 } else {
245                                         s = malloc(k);
246                                         if (!s) goto alloc_fail;
247                                 }
248                         }
249                         while (width) {
250                                 if ((c=getwc(f))<0) break;
251                                 if (in_set(set, c) == invert)
252                                         break;
253                                 if (wcs) {
254                                         wcs[i++] = c;
255                                         if (alloc && i==k) {
256                                                 k += k+1;
257                                                 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
258                                                 if (!tmp) goto alloc_fail;
259                                                 wcs = tmp;
260                                         }
261                                 } else if (size != SIZE_l) {
262                                         int l = wctomb(s?s+i:tmp, c);
263                                         if (l<0) goto input_fail;
264                                         i += l;
265                                         if (alloc && i > k-4) {
266                                                 k += k+1;
267                                                 char *tmp = realloc(s, k);
268                                                 if (!tmp) goto alloc_fail;
269                                                 s = tmp;
270                                         }
271                                 }
272                                 pos++;
273                                 width-=(width>0);
274                                 gotmatch=1;
275                         }
276                         if (width) {
277                                 ungetwc(c, f);
278                                 if (t == 'c' || !gotmatch) goto match_fail;
279                         }
280
281                         if (alloc) {
282                                 if (size == SIZE_l) *(wchar_t **)dest = wcs;
283                                 else *(char **)dest = s;
284                         }
285                         if (t != 'c') {
286                                 if (wcs) wcs[i] = 0;
287                                 if (s) s[i] = 0;
288                         }
289                         break;
290
291                 case 'd': case 'i': case 'o': case 'u': case 'x':
292                 case 'a': case 'e': case 'f': case 'g':
293                 case 'A': case 'E': case 'F': case 'G': case 'X':
294                 case 'p':
295                         if (width < 1) width = 0;
296                         snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
297                                 1+!dest, "%*", width, size_pfx[size+2], t);
298                         cnt = 0;
299                         if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
300                                 goto input_fail;
301                         else if (!cnt)
302                                 goto match_fail;
303                         pos += cnt;
304                         break;
305                 default:
306                         goto fmt_fail;
307                 }
308
309                 if (dest) matches++;
310         }
311         if (0) {
312 fmt_fail:
313 alloc_fail:
314 input_fail:
315                 if (!matches) matches--;
316 match_fail:
317                 if (alloc) {
318                         free(s);
319                         free(wcs);
320                 }
321         }
322         FUNLOCK(f);
323         return matches;
324 }
325
326 weak_alias(vfwscanf,__isoc99_vfwscanf);