blob: a7cd09231a311f2a4abc8dba4cd5a3921ff65bc1 [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>
Rich Felker0b44a032011-02-12 00:22:29 -05009
10#include "stdio_impl.h"
Rich Felker73ec1d02012-04-17 14:19:46 -040011#include "shgetc.h"
12#include "intscan.h"
13#include "floatscan.h"
Isaac Dunham14f02722013-04-05 23:20:28 -070014#include "libc.h"
Rich Felker0b44a032011-02-12 00:22:29 -050015
Rich Felker73ec1d02012-04-17 14:19:46 -040016#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
23static void store_int(void *dest, int size, unsigned long long i)
Rich Felker0b44a032011-02-12 00:22:29 -050024{
Rich Felker73ec1d02012-04-17 14:19:46 -040025 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 }
Rich Felker0b44a032011-02-12 00:22:29 -050043}
44
Rich Felker73ec1d02012-04-17 14:19:46 -040045static 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
57static 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) \
Rich Felker00722512012-04-17 19:37:31 -040084 ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f)))
Rich Felker73ec1d02012-04-17 14:19:46 -040085#endif
86
Rich Felker400c5e52012-09-06 22:44:55 -040087int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
Rich Felker0b44a032011-02-12 00:22:29 -050088{
Rich Felker73ec1d02012-04-17 14:19:46 -040089 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];
Rich Felkerde80ea92013-06-05 16:53:26 -0400102 const wchar_t *set;
Rich Felkere039db22013-06-06 00:26:17 -0400103 size_t i, k;
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 Felker536c6d52015-06-13 05:17:16 +0000107 fwide(f, 1);
Rich Felker984c25b2014-07-02 12:09:48 -0400108
Rich Felker73ec1d02012-04-17 14:19:46 -0400109 for (p=fmt; *p; p++) {
110
Rich Felker1d92cdd2013-07-20 00:21:11 -0400111 alloc = 0;
112
Rich Felker73ec1d02012-04-17 14:19:46 -0400113 if (iswspace(*p)) {
114 while (iswspace(p[1])) p++;
115 while (iswspace((c=getwc(f)))) pos++;
116 ungetwc(c, f);
117 continue;
118 }
119 if (*p != '%' || p[1] == '%') {
Bartosz Brachaczek9255dad2017-07-09 23:00:18 +0200120 if (*p == '%') {
121 p++;
122 while (iswspace((c=getwc(f)))) pos++;
123 } else {
124 c = getwc(f);
125 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400126 if (c!=*p) {
127 ungetwc(c, f);
128 if (c<0) goto input_fail;
129 goto match_fail;
130 }
131 pos++;
132 continue;
133 }
134
135 p++;
136 if (*p=='*') {
137 dest = 0; p++;
138 } else if (iswdigit(*p) && p[1]=='$') {
139 dest = arg_n(ap, *p-'0'); p+=2;
140 } else {
141 dest = va_arg(ap, void *);
142 }
143
144 for (width=0; iswdigit(*p); p++) {
145 width = 10*width + *p - '0';
146 }
147
148 if (*p=='m') {
Rich Felkerf0328a52013-08-31 22:52:41 -0400149 wcs = 0;
150 s = 0;
Rich Felkere039db22013-06-06 00:26:17 -0400151 alloc = !!dest;
Rich Felker73ec1d02012-04-17 14:19:46 -0400152 p++;
153 } else {
154 alloc = 0;
155 }
156
157 size = SIZE_def;
158 switch (*p++) {
159 case 'h':
160 if (*p == 'h') p++, size = SIZE_hh;
161 else size = SIZE_h;
162 break;
163 case 'l':
164 if (*p == 'l') p++, size = SIZE_ll;
165 else size = SIZE_l;
166 break;
167 case 'j':
168 size = SIZE_ll;
169 break;
170 case 'z':
171 case 't':
172 size = SIZE_l;
173 break;
174 case 'L':
175 size = SIZE_L;
176 break;
177 case 'd': case 'i': case 'o': case 'u': case 'x':
178 case 'a': case 'e': case 'f': case 'g':
179 case 'A': case 'E': case 'F': case 'G': case 'X':
180 case 's': case 'c': case '[':
181 case 'S': case 'C':
182 case 'p': case 'n':
183 p--;
184 break;
185 default:
186 goto fmt_fail;
187 }
188
189 t = *p;
190
Rich Felkerde80ea92013-06-05 16:53:26 -0400191 /* Transform S,C -> ls,lc */
192 if ((t&0x2f)==3) {
193 size = SIZE_l;
194 t |= 32;
195 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400196
Rich Felkerbdeb1842012-04-17 23:35:49 -0400197 if (t != 'n') {
198 if (t != '[' && (t|32) != 'c')
199 while (iswspace((c=getwc(f)))) pos++;
200 else
201 c=getwc(f);
Rich Felker73ec1d02012-04-17 14:19:46 -0400202 if (c < 0) goto input_fail;
203 ungetwc(c, f);
204 }
205
206 switch (t) {
207 case 'n':
208 store_int(dest, size, pos);
209 /* do not increment match count, etc! */
210 continue;
211
Rich Felker73ec1d02012-04-17 14:19:46 -0400212 case 's':
Rich Felkerde80ea92013-06-05 16:53:26 -0400213 case 'c':
Rich Felker73ec1d02012-04-17 14:19:46 -0400214 case '[':
Rich Felkerde80ea92013-06-05 16:53:26 -0400215 if (t == 'c') {
216 if (width<1) width = 1;
217 invert = 1;
218 set = L"";
219 } else if (t == 's') {
220 invert = 1;
Rich Felker733d1ea2017-03-14 15:06:58 -0400221 static const wchar_t spaces[] = {
Rich Felkerde80ea92013-06-05 16:53:26 -0400222 ' ', '\t', '\n', '\r', 11, 12, 0x0085,
223 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
224 0x2006, 0x2008, 0x2009, 0x200a,
225 0x2028, 0x2029, 0x205f, 0x3000, 0 };
Rich Felker733d1ea2017-03-14 15:06:58 -0400226 set = spaces;
Rich Felkerde80ea92013-06-05 16:53:26 -0400227 } else {
228 if (*++p == '^') p++, invert = 1;
229 else invert = 0;
230 set = p;
231 if (*p==']') p++;
232 while (*p!=']') {
233 if (!*p) goto fmt_fail;
234 p++;
235 }
236 }
237
Rich Felker73ec1d02012-04-17 14:19:46 -0400238 s = (size == SIZE_def) ? dest : 0;
239 wcs = (size == SIZE_l) ? dest : 0;
240
Rich Felker73ec1d02012-04-17 14:19:46 -0400241 int gotmatch = 0;
242
Rich Felker9ab180f2012-04-17 22:15:33 -0400243 if (width < 1) width = -1;
244
Rich Felkere039db22013-06-06 00:26:17 -0400245 i = 0;
246 if (alloc) {
247 k = t=='c' ? width+1U : 31;
248 if (size == SIZE_l) {
249 wcs = malloc(k*sizeof(wchar_t));
250 if (!wcs) goto alloc_fail;
251 } else {
252 s = malloc(k);
253 if (!s) goto alloc_fail;
254 }
255 }
Rich Felker99fbf4c2012-04-17 21:17:09 -0400256 while (width) {
Rich Felker73ec1d02012-04-17 14:19:46 -0400257 if ((c=getwc(f))<0) break;
Rich Felkerde80ea92013-06-05 16:53:26 -0400258 if (in_set(set, c) == invert)
Rich Felker73ec1d02012-04-17 14:19:46 -0400259 break;
260 if (wcs) {
Rich Felkere039db22013-06-06 00:26:17 -0400261 wcs[i++] = c;
262 if (alloc && i==k) {
263 k += k+1;
264 wchar_t *tmp = realloc(wcs, k*sizeof(wchar_t));
265 if (!tmp) goto alloc_fail;
266 wcs = tmp;
267 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400268 } else if (size != SIZE_l) {
Rich Felkere039db22013-06-06 00:26:17 -0400269 int l = wctomb(s?s+i:tmp, c);
Rich Felker73ec1d02012-04-17 14:19:46 -0400270 if (l<0) goto input_fail;
Rich Felkere039db22013-06-06 00:26:17 -0400271 i += l;
272 if (alloc && i > k-4) {
273 k += k+1;
274 char *tmp = realloc(s, k);
275 if (!tmp) goto alloc_fail;
276 s = tmp;
277 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400278 }
279 pos++;
Rich Felker9ab180f2012-04-17 22:15:33 -0400280 width-=(width>0);
Rich Felker73ec1d02012-04-17 14:19:46 -0400281 gotmatch=1;
282 }
Rich Felkerde80ea92013-06-05 16:53:26 -0400283 if (width) {
284 ungetwc(c, f);
285 if (t == 'c' || !gotmatch) goto match_fail;
Rich Felker73ec1d02012-04-17 14:19:46 -0400286 }
287
Rich Felkere039db22013-06-06 00:26:17 -0400288 if (alloc) {
289 if (size == SIZE_l) *(wchar_t **)dest = wcs;
290 else *(char **)dest = s;
291 }
Rich Felkeref550782013-06-22 17:23:45 -0400292 if (t != 'c') {
293 if (wcs) wcs[i] = 0;
294 if (s) s[i] = 0;
295 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400296 break;
297
298 case 'd': case 'i': case 'o': case 'u': case 'x':
299 case 'a': case 'e': case 'f': case 'g':
300 case 'A': case 'E': case 'F': case 'G': case 'X':
301 case 'p':
302 if (width < 1) width = 0;
303 snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln",
304 1+!dest, "%*", width, size_pfx[size+2], t);
305 cnt = 0;
306 if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1)
307 goto input_fail;
308 else if (!cnt)
309 goto match_fail;
310 pos += cnt;
311 break;
312 default:
313 goto fmt_fail;
314 }
315
316 if (dest) matches++;
Rich Felker0b44a032011-02-12 00:22:29 -0500317 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400318 if (0) {
319fmt_fail:
Rich Felkere039db22013-06-06 00:26:17 -0400320alloc_fail:
Rich Felker73ec1d02012-04-17 14:19:46 -0400321input_fail:
322 if (!matches) matches--;
Rich Felker73ec1d02012-04-17 14:19:46 -0400323match_fail:
Rich Felkere039db22013-06-06 00:26:17 -0400324 if (alloc) {
325 free(s);
326 free(wcs);
327 }
328 }
Rich Felker73ec1d02012-04-17 14:19:46 -0400329 FUNLOCK(f);
330 return matches;
Rich Felker0b44a032011-02-12 00:22:29 -0500331}
Isaac Dunham14f02722013-04-05 23:20:28 -0700332
333weak_alias(vfwscanf,__isoc99_vfwscanf);