blob: b3bc6f3a285ee0a2339aae41d200a15ad4b27b01 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include <stdio.h>
Rich Felker73ec1d02012-04-17 14:19:46 -04002#include <stdlib.h>
3#include <stdarg.h>
4#include <ctype.h>
Rich Felker0b44a032011-02-12 00:22:29 -05005#include <wchar.h>
6#include <wctype.h>
Rich Felker73ec1d02012-04-17 14:19:46 -04007#include <limits.h>
8#include <string.h>
9#include <errno.h>
10#include <math.h>
11#include <float.h>
Rich Felker0b44a032011-02-12 00:22:29 -050012
13#include "stdio_impl.h"
Rich Felker73ec1d02012-04-17 14:19:46 -040014#include "shgetc.h"
15#include "intscan.h"
16#include "floatscan.h"
Rich Felker0b44a032011-02-12 00:22:29 -050017
Rich Felker73ec1d02012-04-17 14:19:46 -040018#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
25static void store_int(void *dest, int size, unsigned long long i)
Rich Felker0b44a032011-02-12 00:22:29 -050026{
Rich Felker73ec1d02012-04-17 14:19:46 -040027 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 }
Rich Felker0b44a032011-02-12 00:22:29 -050045}
46
Rich Felker73ec1d02012-04-17 14:19:46 -040047static 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
59static int in_set(const wchar_t *set, int c)
60{
61 int j;
62 const wchar_t *p = set;
63 if (*p == '-') {
64 if (c=='-') return 1;
65 p++;
66 } else if (*p == ']') {
67 if (c==']') return 1;
68 p++;
69 }
70 for (; *p && *p != ']'; p++) {
71 if (*p=='-' && p[1] && p[1] != ']')
72 for (j=p++[-1]; j<*p; j++)
73 if (c==j) return 1;
74 if (c==*p) return 1;
75 }
76 return 0;
77}
78
79#if 1
80#undef getwc
81#define getwc(f) \
82 ((f)->rpos < (f)->rend && *(f)->rpos < 128 ? *(f)->rpos++ : (getwc)(f))
83
84#undef ungetwc
85#define ungetwc(c,f) \
Rich Felker00722512012-04-17 19:37:31 -040086 ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f)))
Rich Felker73ec1d02012-04-17 14:19:46 -040087#endif
88
Rich Felker400c5e52012-09-06 22:44:55 -040089int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050090{
Rich Felker73ec1d02012-04-17 14:19:46 -040091 int width;
92 int size;
93 int alloc;
94 const wchar_t *p;
95 int c, t;
96 char *s;
97 wchar_t *wcs;
98 void *dest=NULL;
99 int invert;
100 int matches=0;
101 off_t pos = 0, cnt;
102 static const char size_pfx[][3] = { "hh", "h", "", "l", "L", "ll" };
103 char tmp[3*sizeof(int)+10];
Rich Felker0b44a032011-02-12 00:22:29 -0500104
Rich Felker73ec1d02012-04-17 14:19:46 -0400105 FLOCK(f);
Rich Felker0b44a032011-02-12 00:22:29 -0500106
Rich Felker73ec1d02012-04-17 14:19:46 -0400107 for (p=fmt; *p; p++) {
108
109 if (iswspace(*p)) {
110 while (iswspace(p[1])) p++;
111 while (iswspace((c=getwc(f)))) pos++;
112 ungetwc(c, f);
113 continue;
114 }
115 if (*p != '%' || p[1] == '%') {
116 p += *p=='%';
117 c = getwc(f);
118 if (c!=*p) {
119 ungetwc(c, f);
120 if (c<0) goto input_fail;
121 goto match_fail;
122 }
123 pos++;
124 continue;
125 }
126
127 p++;
128 if (*p=='*') {
129 dest = 0; p++;
130 } else if (iswdigit(*p) && p[1]=='$') {
131 dest = arg_n(ap, *p-'0'); p+=2;
132 } else {
133 dest = va_arg(ap, void *);
134 }
135
136 for (width=0; iswdigit(*p); p++) {
137 width = 10*width + *p - '0';
138 }
139
140 if (*p=='m') {
141 alloc = 1;
142 p++;
143 } else {
144 alloc = 0;
145 }
146
147 size = SIZE_def;
148 switch (*p++) {
149 case 'h':
150 if (*p == 'h') p++, size = SIZE_hh;
151 else size = SIZE_h;
152 break;
153 case 'l':
154 if (*p == 'l') p++, size = SIZE_ll;
155 else size = SIZE_l;
156 break;
157 case 'j':
158 size = SIZE_ll;
159 break;
160 case 'z':
161 case 't':
162 size = SIZE_l;
163 break;
164 case 'L':
165 size = SIZE_L;
166 break;
167 case 'd': case 'i': case 'o': case 'u': case 'x':
168 case 'a': case 'e': case 'f': case 'g':
169 case 'A': case 'E': case 'F': case 'G': case 'X':
170 case 's': case 'c': case '[':
171 case 'S': case 'C':
172 case 'p': case 'n':
173 p--;
174 break;
175 default:
176 goto fmt_fail;
177 }
178
179 t = *p;
180
181 /* Transform ls,lc -> S,C */
182 if (size==SIZE_l && (t&15)==3) t&=~32;
183
Rich Felkerbdeb1842012-04-17 23:35:49 -0400184 if (t != 'n') {
185 if (t != '[' && (t|32) != 'c')
186 while (iswspace((c=getwc(f)))) pos++;
187 else
188 c=getwc(f);
Rich Felker73ec1d02012-04-17 14:19:46 -0400189 if (c < 0) goto input_fail;
190 ungetwc(c, f);
191 }
192
193 switch (t) {
194 case 'n':
195 store_int(dest, size, pos);
196 /* do not increment match count, etc! */
197 continue;
198
199 case 'c':
200 if (width < 1) width = 1;
201 s = dest;
202 for (; width && (c=getwc(f)) >= 0; width--) {
203 int l = wctomb(s?s:tmp, c);
204 if (l<0) goto input_fail;
205 if (s) s+=l;
206 pos++;
207 }
208 if (width) goto match_fail;
209 break;
210
211 case 'C':
212 if (width < 1) width = 1;
213 wcs = dest;
214 for (; width && (c=getwc(f)) >= 0; width--)
215 pos++, wcs && (*wcs++ = c);
216 if (width) goto match_fail;
217 break;
218
219 case 's':
Rich Felker9ab180f2012-04-17 22:15:33 -0400220 if (width < 1) width = -1;
Rich Felker73ec1d02012-04-17 14:19:46 -0400221 s = dest;
Rich Felker00722512012-04-17 19:37:31 -0400222 while (width && !iswspace(c=getwc(f)) && c!=EOF) {
Rich Felker73ec1d02012-04-17 14:19:46 -0400223 int l = wctomb(s?s:tmp, c);
224 if (l<0) goto input_fail;
225 if (s) s+=l;
226 pos++;
Rich Felker9ab180f2012-04-17 22:15:33 -0400227 width-=(width>0);
Rich Felker73ec1d02012-04-17 14:19:46 -0400228 }
Rich Felker00722512012-04-17 19:37:31 -0400229 if (width) ungetwc(c, f);
Rich Felker73ec1d02012-04-17 14:19:46 -0400230 if (s) *s = 0;
231 break;
232
233 case 'S':
234 wcs = dest;
Rich Felker9ab180f2012-04-17 22:15:33 -0400235 if (width < 1) width = -1;
Rich Felker00722512012-04-17 19:37:31 -0400236 while (width && !iswspace(c=getwc(f)) && c!=EOF)
Rich Felker9ab180f2012-04-17 22:15:33 -0400237 width-=(width>0), pos++, *wcs++ = c;
Rich Felker00722512012-04-17 19:37:31 -0400238 if (width) ungetwc(c, f);
Rich Felker73ec1d02012-04-17 14:19:46 -0400239 if (wcs) *wcs = 0;
240 break;
241
242 case '[':
243 s = (size == SIZE_def) ? dest : 0;
244 wcs = (size == SIZE_l) ? dest : 0;
245
246 if (*++p == '^') p++, invert = 1;
247 else invert = 0;
248
249 int gotmatch = 0;
250
Rich Felker9ab180f2012-04-17 22:15:33 -0400251 if (width < 1) width = -1;
252
Rich Felker99fbf4c2012-04-17 21:17:09 -0400253 while (width) {
Rich Felker73ec1d02012-04-17 14:19:46 -0400254 if ((c=getwc(f))<0) break;
255 if (in_set(p, c) == invert)
256 break;
257 if (wcs) {
258 *wcs++ = c;
259 } else if (size != SIZE_l) {
260 int l = wctomb(s?s:tmp, c);
261 if (l<0) goto input_fail;
262 if (s) s+=l;
263 }
264 pos++;
Rich Felker9ab180f2012-04-17 22:15:33 -0400265 width-=(width>0);
Rich Felker73ec1d02012-04-17 14:19:46 -0400266 gotmatch=1;
267 }
Rich Felker99fbf4c2012-04-17 21:17:09 -0400268 if (width) ungetwc(c, f);
Rich Felker73ec1d02012-04-17 14:19:46 -0400269
Rich Felkerbdeb1842012-04-17 23:35:49 -0400270 if (!gotmatch) goto match_fail;
Rich Felker73ec1d02012-04-17 14:19:46 -0400271
272 if (*p==']') p++;
273 while (*p!=']') {
274 if (!*p) goto fmt_fail;
275 p++;
276 }
277
278 if (wcs) *wcs++ = 0;
279 if (s) *s++ = 0;
280 break;
281
282 case 'd': case 'i': case 'o': case 'u': case 'x':
283 case 'a': case 'e': case 'f': case 'g':
284 case 'A': case 'E': case 'F': case 'G': case 'X':
285 case 'p':
286 if (width < 1) width = 0;
287 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
288 1+!dest, "%*", width, size_pfx[size+2], t);
289 cnt = 0;
290 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
291 goto input_fail;
292 else if (!cnt)
293 goto match_fail;
294 pos += cnt;
295 break;
296 default:
297 goto fmt_fail;
298 }
299
300 if (dest) matches++;
Rich Felker0b44a032011-02-12 00:22:29 -0500301 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400302 if (0) {
303fmt_fail:
304input_fail:
305 if (!matches) matches--;
306 }
307match_fail:
308 FUNLOCK(f);
309 return matches;
Rich Felker0b44a032011-02-12 00:22:29 -0500310}