vfwscanf.c revision 1.11 1 1.11 pooka /* $NetBSD: vfwscanf.c,v 1.11 2013/09/23 12:41:37 pooka Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1990, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to Berkeley by
8 1.1 christos * Chris Torek.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the University of
21 1.1 christos * California, Berkeley and its contributors.
22 1.1 christos * 4. Neither the name of the University nor the names of its contributors
23 1.1 christos * may be used to endorse or promote products derived from this software
24 1.1 christos * without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 christos * SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos #include <sys/cdefs.h>
40 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
41 1.1 christos #if 0
42 1.1 christos static char sccsid[] = "@(#)ftell.c 8.2 (Berkeley) 5/4/95";
43 1.1 christos __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
44 1.1 christos #else
45 1.11 pooka __RCSID("$NetBSD: vfwscanf.c,v 1.11 2013/09/23 12:41:37 pooka Exp $");
46 1.1 christos #endif
47 1.1 christos #endif /* LIBC_SCCS and not lint */
48 1.1 christos
49 1.1 christos #include "namespace.h"
50 1.1 christos #include <ctype.h>
51 1.1 christos #include <inttypes.h>
52 1.7 christos #include <assert.h>
53 1.1 christos #include <stdio.h>
54 1.1 christos #include <stdlib.h>
55 1.1 christos #include <stddef.h>
56 1.1 christos #include <stdarg.h>
57 1.1 christos #include <string.h>
58 1.1 christos #include <limits.h>
59 1.1 christos #include <wchar.h>
60 1.1 christos #include <wctype.h>
61 1.1 christos
62 1.1 christos #include "reentrant.h"
63 1.1 christos #include "local.h"
64 1.1 christos
65 1.1 christos #include <locale.h>
66 1.9 joerg #include "setlocale_local.h"
67 1.1 christos
68 1.1 christos #define BUF 513 /* Maximum length of numeric string. */
69 1.1 christos
70 1.1 christos /*
71 1.1 christos * Flags used during conversion.
72 1.1 christos */
73 1.1 christos #define LONG 0x01 /* l: long or double */
74 1.1 christos #define LONGDBL 0x02 /* L: long double */
75 1.1 christos #define SHORT 0x04 /* h: short */
76 1.1 christos #define SUPPRESS 0x08 /* *: suppress assignment */
77 1.1 christos #define POINTER 0x10 /* p: void * (as hex) */
78 1.1 christos #define NOSKIP 0x20 /* [ or c: do not skip blanks */
79 1.1 christos #define LONGLONG 0x400 /* ll: quad_t (+ deprecated q: quad) */
80 1.1 christos #define INTMAXT 0x800 /* j: intmax_t */
81 1.1 christos #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
82 1.1 christos #define SIZET 0x2000 /* z: size_t */
83 1.1 christos #define SHORTSHORT 0x4000 /* hh: char */
84 1.1 christos #define UNSIGNED 0x8000 /* %[oupxX] conversions */
85 1.1 christos
86 1.1 christos /*
87 1.1 christos * The following are used in integral conversions only:
88 1.1 christos * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
89 1.1 christos */
90 1.1 christos #define SIGNOK 0x40 /* +/- is (still) legal */
91 1.1 christos #define NDIGITS 0x80 /* no digits detected */
92 1.1 christos #define PFXOK 0x100 /* 0x prefix is (still) legal */
93 1.1 christos #define NZDIGITS 0x200 /* no zero digits detected */
94 1.1 christos #define HAVESIGN 0x10000 /* sign detected */
95 1.1 christos
96 1.1 christos /*
97 1.1 christos * Conversion types.
98 1.1 christos */
99 1.1 christos #define CT_CHAR 0 /* %c conversion */
100 1.1 christos #define CT_CCL 1 /* %[...] conversion */
101 1.1 christos #define CT_STRING 2 /* %s conversion */
102 1.1 christos #define CT_INT 3 /* %[dioupxX] conversion */
103 1.1 christos #define CT_FLOAT 4 /* %[efgEFG] conversion */
104 1.1 christos
105 1.11 pooka #ifndef NO_FLOATING_POINT
106 1.9 joerg static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
107 1.11 pooka #endif
108 1.1 christos
109 1.1 christos #define INCCL(_c) \
110 1.1 christos (cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
111 1.1 christos (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
112 1.1 christos
113 1.1 christos /*
114 1.1 christos * MT-safe version.
115 1.1 christos */
116 1.1 christos int
117 1.1 christos vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
118 1.1 christos {
119 1.10 joerg return vfwscanf_l(fp, _current_locale(), fmt, ap);
120 1.9 joerg }
121 1.9 joerg
122 1.9 joerg int
123 1.9 joerg vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
124 1.9 joerg va_list ap)
125 1.9 joerg {
126 1.1 christos int ret;
127 1.1 christos
128 1.1 christos FLOCKFILE(fp);
129 1.1 christos _SET_ORIENTATION(fp, 1);
130 1.9 joerg ret = __vfwscanf_unlocked_l(fp, loc, fmt, ap);
131 1.1 christos FUNLOCKFILE(fp);
132 1.8 christos return ret;
133 1.1 christos }
134 1.1 christos
135 1.3 christos #define SCANF_SKIP_SPACE() \
136 1.3 christos do { \
137 1.3 christos wint_t tc; \
138 1.3 christos \
139 1.9 joerg while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace_l(tc, loc)) \
140 1.3 christos continue; \
141 1.3 christos if (tc != WEOF) \
142 1.3 christos ungetwc(tc, fp); \
143 1.3 christos } while (/*CONSTCOND*/ 0)
144 1.3 christos
145 1.1 christos /*
146 1.1 christos * Non-MT-safe version.
147 1.1 christos */
148 1.1 christos int
149 1.9 joerg __vfwscanf_unlocked_l(FILE * __restrict fp, locale_t loc,
150 1.9 joerg const wchar_t * __restrict fmt, va_list ap)
151 1.1 christos {
152 1.1 christos wint_t c; /* character from format, or conversion */
153 1.1 christos size_t width; /* field width, or 0 */
154 1.1 christos wchar_t *p; /* points into all kinds of strings */
155 1.1 christos int n; /* handy integer */
156 1.1 christos int flags; /* flags as defined above */
157 1.1 christos wchar_t *p0; /* saves original value of p when necessary */
158 1.1 christos int nassigned; /* number of fields assigned */
159 1.1 christos int nconversions; /* number of conversions */
160 1.7 christos size_t nread; /* number of characters consumed from fp */
161 1.1 christos int base; /* base argument to conversion function */
162 1.1 christos wchar_t buf[BUF]; /* buffer for numeric conversions */
163 1.1 christos const wchar_t *ccls; /* character class start */
164 1.1 christos const wchar_t *ccle; /* character class end */
165 1.1 christos int cclcompl; /* ccl is complemented? */
166 1.1 christos wint_t wi; /* handy wint_t */
167 1.1 christos char *mbp; /* multibyte string pointer for %c %s %[ */
168 1.1 christos size_t nconv; /* number of bytes in mb. conversion */
169 1.1 christos static const mbstate_t initial;
170 1.1 christos mbstate_t mbs;
171 1.9 joerg char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
172 1.1 christos /* `basefix' is used to avoid `if' tests in the integer scanner */
173 1.1 christos static short basefix[17] =
174 1.1 christos { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
175 1.1 christos
176 1.1 christos nassigned = 0;
177 1.1 christos nconversions = 0;
178 1.1 christos nread = 0;
179 1.1 christos ccls = ccle = NULL;
180 1.2 lukem base = 0;
181 1.2 lukem cclcompl = 0;
182 1.2 lukem mbp = NULL;
183 1.1 christos for (;;) {
184 1.1 christos c = *fmt++;
185 1.1 christos if (c == 0)
186 1.8 christos return nassigned;
187 1.9 joerg if (iswspace_l(c, loc)) {
188 1.1 christos while ((c = __fgetwc_unlock(fp)) != WEOF &&
189 1.9 joerg iswspace_l(c, loc))
190 1.1 christos ;
191 1.1 christos if (c != WEOF)
192 1.1 christos ungetwc(c, fp);
193 1.1 christos continue;
194 1.1 christos }
195 1.1 christos if (c != '%')
196 1.1 christos goto literal;
197 1.1 christos width = 0;
198 1.1 christos flags = 0;
199 1.1 christos /*
200 1.1 christos * switch on the format. continue if done;
201 1.1 christos * break once format type is derived.
202 1.1 christos */
203 1.1 christos again: c = *fmt++;
204 1.1 christos switch (c) {
205 1.1 christos case '%':
206 1.3 christos SCANF_SKIP_SPACE();
207 1.1 christos literal:
208 1.1 christos if ((wi = __fgetwc_unlock(fp)) == WEOF)
209 1.1 christos goto input_failure;
210 1.1 christos if (wi != c) {
211 1.1 christos ungetwc(wi, fp);
212 1.1 christos goto input_failure;
213 1.1 christos }
214 1.1 christos nread++;
215 1.1 christos continue;
216 1.1 christos
217 1.1 christos case '*':
218 1.1 christos flags |= SUPPRESS;
219 1.1 christos goto again;
220 1.1 christos case 'j':
221 1.1 christos flags |= INTMAXT;
222 1.1 christos goto again;
223 1.1 christos case 'l':
224 1.1 christos if (flags & LONG) {
225 1.1 christos flags &= ~LONG;
226 1.1 christos flags |= LONGLONG;
227 1.1 christos } else
228 1.1 christos flags |= LONG;
229 1.1 christos goto again;
230 1.1 christos case 'q':
231 1.1 christos flags |= LONGLONG; /* not quite */
232 1.1 christos goto again;
233 1.1 christos case 't':
234 1.1 christos flags |= PTRDIFFT;
235 1.1 christos goto again;
236 1.1 christos case 'z':
237 1.1 christos flags |= SIZET;
238 1.1 christos goto again;
239 1.1 christos case 'L':
240 1.1 christos flags |= LONGDBL;
241 1.1 christos goto again;
242 1.1 christos case 'h':
243 1.1 christos if (flags & SHORT) {
244 1.1 christos flags &= ~SHORT;
245 1.1 christos flags |= SHORTSHORT;
246 1.1 christos } else
247 1.1 christos flags |= SHORT;
248 1.1 christos goto again;
249 1.1 christos
250 1.1 christos case '0': case '1': case '2': case '3': case '4':
251 1.1 christos case '5': case '6': case '7': case '8': case '9':
252 1.1 christos width = width * 10 + c - '0';
253 1.1 christos goto again;
254 1.1 christos
255 1.1 christos /*
256 1.1 christos * Conversions.
257 1.1 christos */
258 1.1 christos case 'd':
259 1.1 christos c = CT_INT;
260 1.1 christos base = 10;
261 1.1 christos break;
262 1.1 christos
263 1.1 christos case 'i':
264 1.1 christos c = CT_INT;
265 1.1 christos base = 0;
266 1.1 christos break;
267 1.1 christos
268 1.1 christos case 'o':
269 1.1 christos c = CT_INT;
270 1.1 christos flags |= UNSIGNED;
271 1.1 christos base = 8;
272 1.1 christos break;
273 1.1 christos
274 1.1 christos case 'u':
275 1.1 christos c = CT_INT;
276 1.1 christos flags |= UNSIGNED;
277 1.1 christos base = 10;
278 1.1 christos break;
279 1.1 christos
280 1.1 christos case 'X':
281 1.1 christos case 'x':
282 1.1 christos flags |= PFXOK; /* enable 0x prefixing */
283 1.1 christos c = CT_INT;
284 1.1 christos flags |= UNSIGNED;
285 1.1 christos base = 16;
286 1.1 christos break;
287 1.1 christos
288 1.1 christos #ifndef NO_FLOATING_POINT
289 1.1 christos case 'A': case 'E': case 'F': case 'G':
290 1.1 christos case 'a': case 'e': case 'f': case 'g':
291 1.1 christos c = CT_FLOAT;
292 1.1 christos break;
293 1.1 christos #endif
294 1.1 christos
295 1.1 christos case 'S':
296 1.1 christos flags |= LONG;
297 1.1 christos /* FALLTHROUGH */
298 1.1 christos case 's':
299 1.1 christos c = CT_STRING;
300 1.1 christos break;
301 1.1 christos
302 1.1 christos case '[':
303 1.1 christos ccls = fmt;
304 1.1 christos if (*fmt == '^') {
305 1.1 christos cclcompl = 1;
306 1.1 christos fmt++;
307 1.1 christos } else
308 1.1 christos cclcompl = 0;
309 1.1 christos if (*fmt == ']')
310 1.1 christos fmt++;
311 1.1 christos while (*fmt != '\0' && *fmt != ']')
312 1.1 christos fmt++;
313 1.1 christos ccle = fmt;
314 1.1 christos fmt++;
315 1.1 christos flags |= NOSKIP;
316 1.1 christos c = CT_CCL;
317 1.1 christos break;
318 1.1 christos
319 1.1 christos case 'C':
320 1.1 christos flags |= LONG;
321 1.1 christos /* FALLTHROUGH */
322 1.1 christos case 'c':
323 1.1 christos flags |= NOSKIP;
324 1.1 christos c = CT_CHAR;
325 1.1 christos break;
326 1.1 christos
327 1.1 christos case 'p': /* pointer format is like hex */
328 1.1 christos flags |= POINTER | PFXOK;
329 1.1 christos c = CT_INT; /* assumes sizeof(uintmax_t) */
330 1.1 christos flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
331 1.1 christos base = 16;
332 1.1 christos break;
333 1.1 christos
334 1.1 christos case 'n':
335 1.1 christos nconversions++;
336 1.1 christos if (flags & SUPPRESS) /* ??? */
337 1.1 christos continue;
338 1.1 christos if (flags & SHORTSHORT)
339 1.7 christos *va_arg(ap, char *) = (char)nread;
340 1.1 christos else if (flags & SHORT)
341 1.7 christos *va_arg(ap, short *) = (short)nread;
342 1.1 christos else if (flags & LONG)
343 1.1 christos *va_arg(ap, long *) = nread;
344 1.1 christos else if (flags & LONGLONG)
345 1.1 christos *va_arg(ap, quad_t *) = nread;
346 1.1 christos else if (flags & INTMAXT)
347 1.1 christos *va_arg(ap, intmax_t *) = nread;
348 1.1 christos else if (flags & SIZET)
349 1.1 christos *va_arg(ap, size_t *) = nread;
350 1.1 christos else if (flags & PTRDIFFT)
351 1.1 christos *va_arg(ap, ptrdiff_t *) = nread;
352 1.1 christos else
353 1.7 christos *va_arg(ap, int *) = (int)nread;
354 1.1 christos continue;
355 1.1 christos
356 1.1 christos default:
357 1.1 christos goto match_failure;
358 1.1 christos
359 1.1 christos /*
360 1.1 christos * Disgusting backwards compatibility hack. XXX
361 1.1 christos */
362 1.1 christos case '\0': /* compat */
363 1.8 christos return EOF;
364 1.1 christos }
365 1.1 christos
366 1.1 christos /*
367 1.1 christos * Consume leading white space, except for formats
368 1.1 christos * that suppress this.
369 1.1 christos */
370 1.1 christos if ((flags & NOSKIP) == 0) {
371 1.9 joerg while ((wi = __fgetwc_unlock(fp)) != WEOF &&
372 1.9 joerg iswspace_l(wi, loc))
373 1.1 christos nread++;
374 1.1 christos if (wi == WEOF)
375 1.1 christos goto input_failure;
376 1.1 christos ungetwc(wi, fp);
377 1.1 christos }
378 1.1 christos
379 1.1 christos /*
380 1.1 christos * Do the conversion.
381 1.1 christos */
382 1.1 christos switch (c) {
383 1.1 christos
384 1.1 christos case CT_CHAR:
385 1.1 christos /* scan arbitrary characters (sets NOSKIP) */
386 1.1 christos if (width == 0)
387 1.1 christos width = 1;
388 1.1 christos if (flags & LONG) {
389 1.1 christos if (!(flags & SUPPRESS))
390 1.1 christos p = va_arg(ap, wchar_t *);
391 1.1 christos n = 0;
392 1.1 christos while (width-- != 0 &&
393 1.1 christos (wi = __fgetwc_unlock(fp)) != WEOF) {
394 1.1 christos if (!(flags & SUPPRESS))
395 1.1 christos *p++ = (wchar_t)wi;
396 1.1 christos n++;
397 1.1 christos }
398 1.1 christos if (n == 0)
399 1.1 christos goto input_failure;
400 1.1 christos nread += n;
401 1.1 christos if (!(flags & SUPPRESS))
402 1.1 christos nassigned++;
403 1.1 christos } else {
404 1.1 christos if (!(flags & SUPPRESS))
405 1.1 christos mbp = va_arg(ap, char *);
406 1.1 christos n = 0;
407 1.1 christos mbs = initial;
408 1.1 christos while (width != 0 &&
409 1.1 christos (wi = __fgetwc_unlock(fp)) != WEOF) {
410 1.9 joerg if (width >= MB_CUR_MAX_L(loc) &&
411 1.1 christos !(flags & SUPPRESS)) {
412 1.9 joerg nconv = wcrtomb_l(mbp, wi,
413 1.9 joerg &mbs, loc);
414 1.1 christos if (nconv == (size_t)-1)
415 1.1 christos goto input_failure;
416 1.1 christos } else {
417 1.9 joerg nconv = wcrtomb_l(mbbuf, wi,
418 1.9 joerg &mbs, loc);
419 1.1 christos if (nconv == (size_t)-1)
420 1.1 christos goto input_failure;
421 1.1 christos if (nconv > width) {
422 1.1 christos ungetwc(wi, fp);
423 1.1 christos break;
424 1.1 christos }
425 1.1 christos if (!(flags & SUPPRESS))
426 1.1 christos memcpy(mbp, mbbuf,
427 1.1 christos nconv);
428 1.1 christos }
429 1.1 christos if (!(flags & SUPPRESS))
430 1.1 christos mbp += nconv;
431 1.1 christos width -= nconv;
432 1.1 christos n++;
433 1.1 christos }
434 1.1 christos if (n == 0)
435 1.1 christos goto input_failure;
436 1.1 christos nread += n;
437 1.1 christos if (!(flags & SUPPRESS))
438 1.1 christos nassigned++;
439 1.1 christos }
440 1.1 christos nconversions++;
441 1.1 christos break;
442 1.1 christos
443 1.1 christos case CT_CCL:
444 1.1 christos /* scan a (nonempty) character class (sets NOSKIP) */
445 1.1 christos if (width == 0)
446 1.1 christos width = (size_t)~0; /* `infinity' */
447 1.1 christos /* take only those things in the class */
448 1.1 christos if ((flags & SUPPRESS) && (flags & LONG)) {
449 1.1 christos n = 0;
450 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
451 1.1 christos width-- != 0 && INCCL(wi))
452 1.1 christos n++;
453 1.1 christos if (wi != WEOF)
454 1.1 christos ungetwc(wi, fp);
455 1.1 christos if (n == 0)
456 1.1 christos goto match_failure;
457 1.1 christos } else if (flags & LONG) {
458 1.1 christos p0 = p = va_arg(ap, wchar_t *);
459 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
460 1.1 christos width-- != 0 && INCCL(wi))
461 1.1 christos *p++ = (wchar_t)wi;
462 1.1 christos if (wi != WEOF)
463 1.1 christos ungetwc(wi, fp);
464 1.7 christos _DIAGASSERT(__type_fit(int, p - p0));
465 1.7 christos n = (int)(p - p0);
466 1.1 christos if (n == 0)
467 1.1 christos goto match_failure;
468 1.1 christos *p = 0;
469 1.1 christos nassigned++;
470 1.1 christos } else {
471 1.1 christos if (!(flags & SUPPRESS))
472 1.1 christos mbp = va_arg(ap, char *);
473 1.1 christos n = 0;
474 1.1 christos mbs = initial;
475 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
476 1.1 christos width != 0 && INCCL(wi)) {
477 1.9 joerg if (width >= MB_CUR_MAX_L(loc) &&
478 1.1 christos !(flags & SUPPRESS)) {
479 1.9 joerg nconv = wcrtomb_l(mbp, wi,
480 1.9 joerg &mbs, loc);
481 1.1 christos if (nconv == (size_t)-1)
482 1.1 christos goto input_failure;
483 1.1 christos } else {
484 1.9 joerg nconv = wcrtomb_l(mbbuf, wi,
485 1.9 joerg &mbs, loc);
486 1.1 christos if (nconv == (size_t)-1)
487 1.1 christos goto input_failure;
488 1.1 christos if (nconv > width)
489 1.1 christos break;
490 1.1 christos if (!(flags & SUPPRESS))
491 1.1 christos memcpy(mbp, mbbuf,
492 1.1 christos nconv);
493 1.1 christos }
494 1.1 christos if (!(flags & SUPPRESS))
495 1.1 christos mbp += nconv;
496 1.1 christos width -= nconv;
497 1.1 christos n++;
498 1.1 christos }
499 1.1 christos if (wi != WEOF)
500 1.1 christos ungetwc(wi, fp);
501 1.1 christos if (!(flags & SUPPRESS)) {
502 1.1 christos *mbp = 0;
503 1.1 christos nassigned++;
504 1.1 christos }
505 1.1 christos }
506 1.1 christos nread += n;
507 1.1 christos nconversions++;
508 1.1 christos break;
509 1.1 christos
510 1.1 christos case CT_STRING:
511 1.1 christos /* like CCL, but zero-length string OK, & no NOSKIP */
512 1.1 christos if (width == 0)
513 1.1 christos width = (size_t)~0;
514 1.1 christos if ((flags & SUPPRESS) && (flags & LONG)) {
515 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
516 1.1 christos width-- != 0 &&
517 1.9 joerg !iswspace_l(wi, loc))
518 1.1 christos nread++;
519 1.1 christos if (wi != WEOF)
520 1.1 christos ungetwc(wi, fp);
521 1.1 christos } else if (flags & LONG) {
522 1.1 christos p0 = p = va_arg(ap, wchar_t *);
523 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
524 1.1 christos width-- != 0 &&
525 1.9 joerg !iswspace_l(wi, loc)) {
526 1.1 christos *p++ = (wchar_t)wi;
527 1.1 christos nread++;
528 1.1 christos }
529 1.1 christos if (wi != WEOF)
530 1.1 christos ungetwc(wi, fp);
531 1.1 christos *p = '\0';
532 1.1 christos nassigned++;
533 1.1 christos } else {
534 1.1 christos if (!(flags & SUPPRESS))
535 1.1 christos mbp = va_arg(ap, char *);
536 1.1 christos mbs = initial;
537 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
538 1.1 christos width != 0 &&
539 1.9 joerg !iswspace_l(wi, loc)) {
540 1.9 joerg if (width >= MB_CUR_MAX_L(loc) &&
541 1.1 christos !(flags & SUPPRESS)) {
542 1.9 joerg nconv = wcrtomb_l(mbp, wi,
543 1.9 joerg &mbs, loc);
544 1.1 christos if (nconv == (size_t)-1)
545 1.1 christos goto input_failure;
546 1.1 christos } else {
547 1.9 joerg nconv = wcrtomb_l(mbbuf, wi,
548 1.9 joerg &mbs, loc);
549 1.1 christos if (nconv == (size_t)-1)
550 1.1 christos goto input_failure;
551 1.1 christos if (nconv > width)
552 1.1 christos break;
553 1.1 christos if (!(flags & SUPPRESS))
554 1.1 christos memcpy(mbp, mbbuf,
555 1.1 christos nconv);
556 1.1 christos }
557 1.1 christos if (!(flags & SUPPRESS))
558 1.1 christos mbp += nconv;
559 1.1 christos width -= nconv;
560 1.1 christos nread++;
561 1.1 christos }
562 1.1 christos if (wi != WEOF)
563 1.1 christos ungetwc(wi, fp);
564 1.1 christos if (!(flags & SUPPRESS)) {
565 1.1 christos *mbp = 0;
566 1.1 christos nassigned++;
567 1.1 christos }
568 1.1 christos }
569 1.1 christos nconversions++;
570 1.1 christos continue;
571 1.1 christos
572 1.1 christos case CT_INT:
573 1.1 christos /* scan an integer as if by the conversion function */
574 1.1 christos if (width == 0 || width > sizeof(buf) /
575 1.1 christos sizeof(*buf) - 1)
576 1.1 christos width = sizeof(buf) / sizeof(*buf) - 1;
577 1.1 christos flags |= SIGNOK | NDIGITS | NZDIGITS;
578 1.1 christos for (p = buf; width; width--) {
579 1.1 christos c = __fgetwc_unlock(fp);
580 1.1 christos /*
581 1.1 christos * Switch on the character; `goto ok'
582 1.1 christos * if we accept it as a part of number.
583 1.1 christos */
584 1.1 christos switch (c) {
585 1.1 christos
586 1.1 christos /*
587 1.1 christos * The digit 0 is always legal, but is
588 1.1 christos * special. For %i conversions, if no
589 1.1 christos * digits (zero or nonzero) have been
590 1.1 christos * scanned (only signs), we will have
591 1.1 christos * base==0. In that case, we should set
592 1.1 christos * it to 8 and enable 0x prefixing.
593 1.1 christos * Also, if we have not scanned zero digits
594 1.1 christos * before this, do not turn off prefixing
595 1.1 christos * (someone else will turn it off if we
596 1.1 christos * have scanned any nonzero digits).
597 1.1 christos */
598 1.1 christos case '0':
599 1.1 christos if (base == 0) {
600 1.1 christos base = 8;
601 1.1 christos flags |= PFXOK;
602 1.1 christos }
603 1.1 christos if (flags & NZDIGITS)
604 1.1 christos flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
605 1.1 christos else
606 1.1 christos flags &= ~(SIGNOK|PFXOK|NDIGITS);
607 1.1 christos goto ok;
608 1.1 christos
609 1.1 christos /* 1 through 7 always legal */
610 1.1 christos case '1': case '2': case '3':
611 1.1 christos case '4': case '5': case '6': case '7':
612 1.1 christos base = basefix[base];
613 1.1 christos flags &= ~(SIGNOK | PFXOK | NDIGITS);
614 1.1 christos goto ok;
615 1.1 christos
616 1.1 christos /* digits 8 and 9 ok iff decimal or hex */
617 1.1 christos case '8': case '9':
618 1.1 christos base = basefix[base];
619 1.1 christos if (base <= 8)
620 1.1 christos break; /* not legal here */
621 1.1 christos flags &= ~(SIGNOK | PFXOK | NDIGITS);
622 1.1 christos goto ok;
623 1.1 christos
624 1.1 christos /* letters ok iff hex */
625 1.1 christos case 'A': case 'B': case 'C':
626 1.1 christos case 'D': case 'E': case 'F':
627 1.1 christos case 'a': case 'b': case 'c':
628 1.1 christos case 'd': case 'e': case 'f':
629 1.1 christos /* no need to fix base here */
630 1.1 christos if (base <= 10)
631 1.1 christos break; /* not legal here */
632 1.1 christos flags &= ~(SIGNOK | PFXOK | NDIGITS);
633 1.1 christos goto ok;
634 1.1 christos
635 1.1 christos /* sign ok only as first character */
636 1.1 christos case '+': case '-':
637 1.1 christos if (flags & SIGNOK) {
638 1.1 christos flags &= ~SIGNOK;
639 1.1 christos flags |= HAVESIGN;
640 1.1 christos goto ok;
641 1.1 christos }
642 1.1 christos break;
643 1.1 christos
644 1.1 christos /*
645 1.1 christos * x ok iff flag still set & 2nd char (or
646 1.1 christos * 3rd char if we have a sign).
647 1.1 christos */
648 1.1 christos case 'x': case 'X':
649 1.1 christos if (flags & PFXOK && p ==
650 1.1 christos buf + 1 + !!(flags & HAVESIGN)) {
651 1.1 christos base = 16; /* if %i */
652 1.1 christos flags &= ~PFXOK;
653 1.1 christos goto ok;
654 1.1 christos }
655 1.1 christos break;
656 1.1 christos }
657 1.1 christos
658 1.1 christos /*
659 1.1 christos * If we got here, c is not a legal character
660 1.1 christos * for a number. Stop accumulating digits.
661 1.1 christos */
662 1.1 christos if (c != WEOF)
663 1.1 christos ungetwc(c, fp);
664 1.1 christos break;
665 1.1 christos ok:
666 1.1 christos /*
667 1.1 christos * c is legal: store it and look at the next.
668 1.1 christos */
669 1.1 christos *p++ = (wchar_t)c;
670 1.1 christos }
671 1.1 christos /*
672 1.1 christos * If we had only a sign, it is no good; push
673 1.1 christos * back the sign. If the number ends in `x',
674 1.1 christos * it was [sign] '0' 'x', so push back the x
675 1.1 christos * and treat it as [sign] '0'.
676 1.1 christos */
677 1.1 christos if (flags & NDIGITS) {
678 1.1 christos if (p > buf)
679 1.1 christos ungetwc(*--p, fp);
680 1.1 christos goto match_failure;
681 1.1 christos }
682 1.1 christos c = p[-1];
683 1.1 christos if (c == 'x' || c == 'X') {
684 1.1 christos --p;
685 1.1 christos ungetwc(c, fp);
686 1.1 christos }
687 1.1 christos if ((flags & SUPPRESS) == 0) {
688 1.1 christos uintmax_t res;
689 1.1 christos
690 1.1 christos *p = 0;
691 1.1 christos if ((flags & UNSIGNED) == 0)
692 1.9 joerg res = wcstoimax_l(buf, NULL, base, loc);
693 1.1 christos else
694 1.9 joerg res = wcstoumax_l(buf, NULL, base, loc);
695 1.1 christos if (flags & POINTER)
696 1.1 christos *va_arg(ap, void **) =
697 1.1 christos (void *)(uintptr_t)res;
698 1.1 christos else if (flags & SHORTSHORT)
699 1.1 christos *va_arg(ap, char *) = (char)res;
700 1.1 christos else if (flags & SHORT)
701 1.1 christos *va_arg(ap, short *) = (short)res;
702 1.1 christos else if (flags & LONG)
703 1.1 christos *va_arg(ap, long *) = (long)res;
704 1.1 christos else if (flags & LONGLONG)
705 1.1 christos *va_arg(ap, quad_t *) = res;
706 1.1 christos else if (flags & INTMAXT)
707 1.1 christos *va_arg(ap, intmax_t *) = res;
708 1.1 christos else if (flags & PTRDIFFT)
709 1.1 christos *va_arg(ap, ptrdiff_t *) = (ptrdiff_t)res;
710 1.1 christos else if (flags & SIZET)
711 1.1 christos *va_arg(ap, size_t *) = (size_t)res;
712 1.1 christos else
713 1.1 christos *va_arg(ap, int *) = (int)res;
714 1.1 christos nassigned++;
715 1.1 christos }
716 1.7 christos _DIAGASSERT(__type_fit(int, p - buf));
717 1.7 christos nread += (int)(p - buf);
718 1.1 christos nconversions++;
719 1.1 christos break;
720 1.1 christos
721 1.1 christos #ifndef NO_FLOATING_POINT
722 1.1 christos case CT_FLOAT:
723 1.1 christos /* scan a floating point number as if by strtod */
724 1.1 christos if (width == 0 || width > sizeof(buf) /
725 1.1 christos sizeof(*buf) - 1)
726 1.1 christos width = sizeof(buf) / sizeof(*buf) - 1;
727 1.9 joerg if ((width = parsefloat(fp, buf, buf + width, loc)) == 0)
728 1.1 christos goto match_failure;
729 1.1 christos if ((flags & SUPPRESS) == 0) {
730 1.1 christos if (flags & LONGDBL) {
731 1.9 joerg long double res = wcstold_l(buf, &p,
732 1.9 joerg loc);
733 1.1 christos *va_arg(ap, long double *) = res;
734 1.1 christos } else
735 1.1 christos if (flags & LONG) {
736 1.9 joerg double res = wcstod_l(buf, &p, loc);
737 1.1 christos *va_arg(ap, double *) = res;
738 1.1 christos } else {
739 1.9 joerg float res = wcstof_l(buf, &p, loc);
740 1.1 christos *va_arg(ap, float *) = res;
741 1.1 christos }
742 1.1 christos #ifdef DEBUG
743 1.6 christos if (p - buf != (ptrdiff_t)width)
744 1.1 christos abort();
745 1.1 christos #endif
746 1.1 christos nassigned++;
747 1.1 christos }
748 1.1 christos nread += width;
749 1.1 christos nconversions++;
750 1.1 christos break;
751 1.1 christos #endif /* !NO_FLOATING_POINT */
752 1.1 christos }
753 1.1 christos }
754 1.1 christos input_failure:
755 1.8 christos return nconversions != 0 ? nassigned : EOF;
756 1.1 christos match_failure:
757 1.8 christos return nassigned;
758 1.1 christos }
759 1.1 christos
760 1.1 christos #ifndef NO_FLOATING_POINT
761 1.1 christos static int
762 1.9 joerg parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t loc)
763 1.1 christos {
764 1.1 christos wchar_t *commit, *p;
765 1.1 christos int infnanpos = 0;
766 1.1 christos enum {
767 1.1 christos S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
768 1.1 christos S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
769 1.1 christos } state = S_START;
770 1.1 christos wchar_t c;
771 1.9 joerg wchar_t decpt = (wchar_t)(unsigned char)*localeconv_l(loc)->decimal_point;
772 1.1 christos int gotmantdig = 0, ishex = 0;
773 1.1 christos
774 1.1 christos /*
775 1.1 christos * We set commit = p whenever the string we have read so far
776 1.1 christos * constitutes a valid representation of a floating point
777 1.1 christos * number by itself. At some point, the parse will complete
778 1.1 christos * or fail, and we will ungetc() back to the last commit point.
779 1.1 christos * To ensure that the file offset gets updated properly, it is
780 1.1 christos * always necessary to read at least one character that doesn't
781 1.1 christos * match; thus, we can't short-circuit "infinity" or "nan(...)".
782 1.1 christos */
783 1.1 christos commit = buf - 1;
784 1.1 christos c = WEOF;
785 1.1 christos for (p = buf; p < end; ) {
786 1.1 christos if ((c = __fgetwc_unlock(fp)) == WEOF)
787 1.1 christos break;
788 1.1 christos reswitch:
789 1.1 christos switch (state) {
790 1.1 christos case S_START:
791 1.1 christos state = S_GOTSIGN;
792 1.1 christos if (c == '-' || c == '+')
793 1.1 christos break;
794 1.1 christos else
795 1.1 christos goto reswitch;
796 1.1 christos case S_GOTSIGN:
797 1.1 christos switch (c) {
798 1.1 christos case '0':
799 1.1 christos state = S_MAYBEHEX;
800 1.1 christos commit = p;
801 1.1 christos break;
802 1.1 christos case 'I':
803 1.1 christos case 'i':
804 1.1 christos state = S_INF;
805 1.1 christos break;
806 1.1 christos case 'N':
807 1.1 christos case 'n':
808 1.1 christos state = S_NAN;
809 1.1 christos break;
810 1.1 christos default:
811 1.1 christos state = S_DIGITS;
812 1.1 christos goto reswitch;
813 1.1 christos }
814 1.1 christos break;
815 1.1 christos case S_INF:
816 1.1 christos if (infnanpos > 6 ||
817 1.1 christos (c != "nfinity"[infnanpos] &&
818 1.1 christos c != "NFINITY"[infnanpos]))
819 1.1 christos goto parsedone;
820 1.1 christos if (infnanpos == 1 || infnanpos == 6)
821 1.1 christos commit = p; /* inf or infinity */
822 1.1 christos infnanpos++;
823 1.1 christos break;
824 1.1 christos case S_NAN:
825 1.1 christos switch (infnanpos) {
826 1.1 christos case -1: /* XXX kludge to deal with nan(...) */
827 1.1 christos goto parsedone;
828 1.1 christos case 0:
829 1.1 christos if (c != 'A' && c != 'a')
830 1.1 christos goto parsedone;
831 1.1 christos break;
832 1.1 christos case 1:
833 1.1 christos if (c != 'N' && c != 'n')
834 1.1 christos goto parsedone;
835 1.1 christos else
836 1.1 christos commit = p;
837 1.1 christos break;
838 1.1 christos case 2:
839 1.1 christos if (c != '(')
840 1.1 christos goto parsedone;
841 1.1 christos break;
842 1.1 christos default:
843 1.1 christos if (c == ')') {
844 1.1 christos commit = p;
845 1.1 christos infnanpos = -2;
846 1.9 joerg } else if (!iswalnum_l(c, loc) && c != '_')
847 1.1 christos goto parsedone;
848 1.1 christos break;
849 1.1 christos }
850 1.1 christos infnanpos++;
851 1.1 christos break;
852 1.1 christos case S_MAYBEHEX:
853 1.1 christos state = S_DIGITS;
854 1.1 christos if (c == 'X' || c == 'x') {
855 1.1 christos ishex = 1;
856 1.1 christos break;
857 1.1 christos } else { /* we saw a '0', but no 'x' */
858 1.1 christos gotmantdig = 1;
859 1.1 christos goto reswitch;
860 1.1 christos }
861 1.1 christos case S_DIGITS:
862 1.9 joerg if ((ishex && iswxdigit_l(c, loc)) ||
863 1.9 joerg iswdigit_l(c, loc))
864 1.1 christos gotmantdig = 1;
865 1.1 christos else {
866 1.1 christos state = S_FRAC;
867 1.1 christos if (c != decpt)
868 1.1 christos goto reswitch;
869 1.1 christos }
870 1.1 christos if (gotmantdig)
871 1.1 christos commit = p;
872 1.1 christos break;
873 1.1 christos case S_FRAC:
874 1.1 christos if (((c == 'E' || c == 'e') && !ishex) ||
875 1.1 christos ((c == 'P' || c == 'p') && ishex)) {
876 1.1 christos if (!gotmantdig)
877 1.1 christos goto parsedone;
878 1.1 christos else
879 1.1 christos state = S_EXP;
880 1.9 joerg } else if ((ishex && iswxdigit_l(c, loc)) ||
881 1.9 joerg iswdigit_l(c, loc)) {
882 1.1 christos commit = p;
883 1.1 christos gotmantdig = 1;
884 1.1 christos } else
885 1.1 christos goto parsedone;
886 1.1 christos break;
887 1.1 christos case S_EXP:
888 1.1 christos state = S_EXPDIGITS;
889 1.1 christos if (c == '-' || c == '+')
890 1.1 christos break;
891 1.1 christos else
892 1.1 christos goto reswitch;
893 1.1 christos case S_EXPDIGITS:
894 1.9 joerg if (iswdigit_l(c, loc))
895 1.1 christos commit = p;
896 1.1 christos else
897 1.1 christos goto parsedone;
898 1.1 christos break;
899 1.1 christos default:
900 1.1 christos abort();
901 1.1 christos }
902 1.1 christos *p++ = c;
903 1.1 christos c = WEOF;
904 1.1 christos }
905 1.1 christos
906 1.1 christos parsedone:
907 1.1 christos if (c != WEOF)
908 1.1 christos ungetwc(c, fp);
909 1.1 christos while (commit < --p)
910 1.1 christos ungetwc(*p, fp);
911 1.1 christos *++commit = '\0';
912 1.7 christos _DIAGASSERT(__type_fit(int, commit - buf));
913 1.7 christos return (int)(commit - buf);
914 1.1 christos }
915 1.1 christos #endif
916