vfwscanf.c revision 1.6 1 1.6 christos /* $NetBSD: vfwscanf.c,v 1.6 2009/02/21 17:20:01 christos 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.6 christos __RCSID("$NetBSD: vfwscanf.c,v 1.6 2009/02/21 17:20:01 christos 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.1 christos #include <stdio.h>
53 1.1 christos #include <stdlib.h>
54 1.1 christos #include <stddef.h>
55 1.1 christos #include <stdarg.h>
56 1.1 christos #include <string.h>
57 1.1 christos #include <limits.h>
58 1.1 christos #include <wchar.h>
59 1.1 christos #include <wctype.h>
60 1.1 christos
61 1.1 christos #include "reentrant.h"
62 1.1 christos #include "local.h"
63 1.1 christos
64 1.1 christos #ifndef NO_FLOATING_POINT
65 1.1 christos #include <locale.h>
66 1.1 christos #endif
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.1 christos static int parsefloat(FILE *, wchar_t *, wchar_t *);
106 1.1 christos
107 1.1 christos #define INCCL(_c) \
108 1.1 christos (cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
109 1.1 christos (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
110 1.1 christos
111 1.1 christos /*
112 1.1 christos * MT-safe version.
113 1.1 christos */
114 1.1 christos int
115 1.1 christos vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
116 1.1 christos {
117 1.1 christos int ret;
118 1.1 christos
119 1.1 christos FLOCKFILE(fp);
120 1.1 christos _SET_ORIENTATION(fp, 1);
121 1.1 christos ret = __vfwscanf_unlocked(fp, fmt, ap);
122 1.1 christos FUNLOCKFILE(fp);
123 1.1 christos return (ret);
124 1.1 christos }
125 1.1 christos
126 1.3 christos #define SCANF_SKIP_SPACE() \
127 1.3 christos do { \
128 1.3 christos wint_t tc; \
129 1.3 christos \
130 1.3 christos while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace(tc)) \
131 1.3 christos continue; \
132 1.3 christos if (tc != WEOF) \
133 1.3 christos ungetwc(tc, fp); \
134 1.3 christos } while (/*CONSTCOND*/ 0)
135 1.3 christos
136 1.1 christos /*
137 1.1 christos * Non-MT-safe version.
138 1.1 christos */
139 1.1 christos int
140 1.1 christos __vfwscanf_unlocked(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
141 1.1 christos {
142 1.1 christos wint_t c; /* character from format, or conversion */
143 1.1 christos size_t width; /* field width, or 0 */
144 1.1 christos wchar_t *p; /* points into all kinds of strings */
145 1.1 christos int n; /* handy integer */
146 1.1 christos int flags; /* flags as defined above */
147 1.1 christos wchar_t *p0; /* saves original value of p when necessary */
148 1.1 christos int nassigned; /* number of fields assigned */
149 1.1 christos int nconversions; /* number of conversions */
150 1.1 christos int nread; /* number of characters consumed from fp */
151 1.1 christos int base; /* base argument to conversion function */
152 1.1 christos wchar_t buf[BUF]; /* buffer for numeric conversions */
153 1.1 christos const wchar_t *ccls; /* character class start */
154 1.1 christos const wchar_t *ccle; /* character class end */
155 1.1 christos int cclcompl; /* ccl is complemented? */
156 1.1 christos wint_t wi; /* handy wint_t */
157 1.1 christos char *mbp; /* multibyte string pointer for %c %s %[ */
158 1.1 christos size_t nconv; /* number of bytes in mb. conversion */
159 1.1 christos char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
160 1.1 christos static const mbstate_t initial;
161 1.1 christos mbstate_t mbs;
162 1.1 christos
163 1.1 christos /* `basefix' is used to avoid `if' tests in the integer scanner */
164 1.1 christos static short basefix[17] =
165 1.1 christos { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
166 1.1 christos
167 1.1 christos nassigned = 0;
168 1.1 christos nconversions = 0;
169 1.1 christos nread = 0;
170 1.1 christos ccls = ccle = NULL;
171 1.2 lukem base = 0;
172 1.2 lukem cclcompl = 0;
173 1.2 lukem mbp = NULL;
174 1.1 christos for (;;) {
175 1.1 christos c = *fmt++;
176 1.1 christos if (c == 0)
177 1.1 christos return (nassigned);
178 1.1 christos if (iswspace(c)) {
179 1.1 christos while ((c = __fgetwc_unlock(fp)) != WEOF &&
180 1.1 christos iswspace(c))
181 1.1 christos ;
182 1.1 christos if (c != WEOF)
183 1.1 christos ungetwc(c, fp);
184 1.1 christos continue;
185 1.1 christos }
186 1.1 christos if (c != '%')
187 1.1 christos goto literal;
188 1.1 christos width = 0;
189 1.1 christos flags = 0;
190 1.1 christos /*
191 1.1 christos * switch on the format. continue if done;
192 1.1 christos * break once format type is derived.
193 1.1 christos */
194 1.1 christos again: c = *fmt++;
195 1.1 christos switch (c) {
196 1.1 christos case '%':
197 1.3 christos SCANF_SKIP_SPACE();
198 1.1 christos literal:
199 1.1 christos if ((wi = __fgetwc_unlock(fp)) == WEOF)
200 1.1 christos goto input_failure;
201 1.1 christos if (wi != c) {
202 1.1 christos ungetwc(wi, fp);
203 1.1 christos goto input_failure;
204 1.1 christos }
205 1.1 christos nread++;
206 1.1 christos continue;
207 1.1 christos
208 1.1 christos case '*':
209 1.1 christos flags |= SUPPRESS;
210 1.1 christos goto again;
211 1.1 christos case 'j':
212 1.1 christos flags |= INTMAXT;
213 1.1 christos goto again;
214 1.1 christos case 'l':
215 1.1 christos if (flags & LONG) {
216 1.1 christos flags &= ~LONG;
217 1.1 christos flags |= LONGLONG;
218 1.1 christos } else
219 1.1 christos flags |= LONG;
220 1.1 christos goto again;
221 1.1 christos case 'q':
222 1.1 christos flags |= LONGLONG; /* not quite */
223 1.1 christos goto again;
224 1.1 christos case 't':
225 1.1 christos flags |= PTRDIFFT;
226 1.1 christos goto again;
227 1.1 christos case 'z':
228 1.1 christos flags |= SIZET;
229 1.1 christos goto again;
230 1.1 christos case 'L':
231 1.1 christos flags |= LONGDBL;
232 1.1 christos goto again;
233 1.1 christos case 'h':
234 1.1 christos if (flags & SHORT) {
235 1.1 christos flags &= ~SHORT;
236 1.1 christos flags |= SHORTSHORT;
237 1.1 christos } else
238 1.1 christos flags |= SHORT;
239 1.1 christos goto again;
240 1.1 christos
241 1.1 christos case '0': case '1': case '2': case '3': case '4':
242 1.1 christos case '5': case '6': case '7': case '8': case '9':
243 1.1 christos width = width * 10 + c - '0';
244 1.1 christos goto again;
245 1.1 christos
246 1.1 christos /*
247 1.1 christos * Conversions.
248 1.1 christos */
249 1.1 christos case 'd':
250 1.1 christos c = CT_INT;
251 1.1 christos base = 10;
252 1.1 christos break;
253 1.1 christos
254 1.1 christos case 'i':
255 1.1 christos c = CT_INT;
256 1.1 christos base = 0;
257 1.1 christos break;
258 1.1 christos
259 1.1 christos case 'o':
260 1.1 christos c = CT_INT;
261 1.1 christos flags |= UNSIGNED;
262 1.1 christos base = 8;
263 1.1 christos break;
264 1.1 christos
265 1.1 christos case 'u':
266 1.1 christos c = CT_INT;
267 1.1 christos flags |= UNSIGNED;
268 1.1 christos base = 10;
269 1.1 christos break;
270 1.1 christos
271 1.1 christos case 'X':
272 1.1 christos case 'x':
273 1.1 christos flags |= PFXOK; /* enable 0x prefixing */
274 1.1 christos c = CT_INT;
275 1.1 christos flags |= UNSIGNED;
276 1.1 christos base = 16;
277 1.1 christos break;
278 1.1 christos
279 1.1 christos #ifndef NO_FLOATING_POINT
280 1.1 christos case 'A': case 'E': case 'F': case 'G':
281 1.1 christos case 'a': case 'e': case 'f': case 'g':
282 1.1 christos c = CT_FLOAT;
283 1.1 christos break;
284 1.1 christos #endif
285 1.1 christos
286 1.1 christos case 'S':
287 1.1 christos flags |= LONG;
288 1.1 christos /* FALLTHROUGH */
289 1.1 christos case 's':
290 1.1 christos c = CT_STRING;
291 1.1 christos break;
292 1.1 christos
293 1.1 christos case '[':
294 1.1 christos ccls = fmt;
295 1.1 christos if (*fmt == '^') {
296 1.1 christos cclcompl = 1;
297 1.1 christos fmt++;
298 1.1 christos } else
299 1.1 christos cclcompl = 0;
300 1.1 christos if (*fmt == ']')
301 1.1 christos fmt++;
302 1.1 christos while (*fmt != '\0' && *fmt != ']')
303 1.1 christos fmt++;
304 1.1 christos ccle = fmt;
305 1.1 christos fmt++;
306 1.1 christos flags |= NOSKIP;
307 1.1 christos c = CT_CCL;
308 1.1 christos break;
309 1.1 christos
310 1.1 christos case 'C':
311 1.1 christos flags |= LONG;
312 1.1 christos /* FALLTHROUGH */
313 1.1 christos case 'c':
314 1.1 christos flags |= NOSKIP;
315 1.1 christos c = CT_CHAR;
316 1.1 christos break;
317 1.1 christos
318 1.1 christos case 'p': /* pointer format is like hex */
319 1.1 christos flags |= POINTER | PFXOK;
320 1.1 christos c = CT_INT; /* assumes sizeof(uintmax_t) */
321 1.1 christos flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
322 1.1 christos base = 16;
323 1.1 christos break;
324 1.1 christos
325 1.1 christos case 'n':
326 1.1 christos nconversions++;
327 1.1 christos if (flags & SUPPRESS) /* ??? */
328 1.1 christos continue;
329 1.1 christos if (flags & SHORTSHORT)
330 1.1 christos *va_arg(ap, char *) = nread;
331 1.1 christos else if (flags & SHORT)
332 1.1 christos *va_arg(ap, short *) = nread;
333 1.1 christos else if (flags & LONG)
334 1.1 christos *va_arg(ap, long *) = nread;
335 1.1 christos else if (flags & LONGLONG)
336 1.1 christos *va_arg(ap, quad_t *) = nread;
337 1.1 christos else if (flags & INTMAXT)
338 1.1 christos *va_arg(ap, intmax_t *) = nread;
339 1.1 christos else if (flags & SIZET)
340 1.1 christos *va_arg(ap, size_t *) = nread;
341 1.1 christos else if (flags & PTRDIFFT)
342 1.1 christos *va_arg(ap, ptrdiff_t *) = nread;
343 1.1 christos else
344 1.1 christos *va_arg(ap, int *) = nread;
345 1.1 christos continue;
346 1.1 christos
347 1.1 christos default:
348 1.1 christos goto match_failure;
349 1.1 christos
350 1.1 christos /*
351 1.1 christos * Disgusting backwards compatibility hack. XXX
352 1.1 christos */
353 1.1 christos case '\0': /* compat */
354 1.1 christos return (EOF);
355 1.1 christos }
356 1.1 christos
357 1.1 christos /*
358 1.1 christos * Consume leading white space, except for formats
359 1.1 christos * that suppress this.
360 1.1 christos */
361 1.1 christos if ((flags & NOSKIP) == 0) {
362 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF && iswspace(wi))
363 1.1 christos nread++;
364 1.1 christos if (wi == WEOF)
365 1.1 christos goto input_failure;
366 1.1 christos ungetwc(wi, fp);
367 1.1 christos }
368 1.1 christos
369 1.1 christos /*
370 1.1 christos * Do the conversion.
371 1.1 christos */
372 1.1 christos switch (c) {
373 1.1 christos
374 1.1 christos case CT_CHAR:
375 1.1 christos /* scan arbitrary characters (sets NOSKIP) */
376 1.1 christos if (width == 0)
377 1.1 christos width = 1;
378 1.1 christos if (flags & LONG) {
379 1.1 christos if (!(flags & SUPPRESS))
380 1.1 christos p = va_arg(ap, wchar_t *);
381 1.1 christos n = 0;
382 1.1 christos while (width-- != 0 &&
383 1.1 christos (wi = __fgetwc_unlock(fp)) != WEOF) {
384 1.1 christos if (!(flags & SUPPRESS))
385 1.1 christos *p++ = (wchar_t)wi;
386 1.1 christos n++;
387 1.1 christos }
388 1.1 christos if (n == 0)
389 1.1 christos goto input_failure;
390 1.1 christos nread += n;
391 1.1 christos if (!(flags & SUPPRESS))
392 1.1 christos nassigned++;
393 1.1 christos } else {
394 1.1 christos if (!(flags & SUPPRESS))
395 1.1 christos mbp = va_arg(ap, char *);
396 1.1 christos n = 0;
397 1.1 christos mbs = initial;
398 1.1 christos while (width != 0 &&
399 1.1 christos (wi = __fgetwc_unlock(fp)) != WEOF) {
400 1.1 christos if (width >= MB_CUR_MAX &&
401 1.1 christos !(flags & SUPPRESS)) {
402 1.1 christos nconv = wcrtomb(mbp, wi, &mbs);
403 1.1 christos if (nconv == (size_t)-1)
404 1.1 christos goto input_failure;
405 1.1 christos } else {
406 1.1 christos nconv = wcrtomb(mbbuf, wi,
407 1.1 christos &mbs);
408 1.1 christos if (nconv == (size_t)-1)
409 1.1 christos goto input_failure;
410 1.1 christos if (nconv > width) {
411 1.1 christos ungetwc(wi, fp);
412 1.1 christos break;
413 1.1 christos }
414 1.1 christos if (!(flags & SUPPRESS))
415 1.1 christos memcpy(mbp, mbbuf,
416 1.1 christos nconv);
417 1.1 christos }
418 1.1 christos if (!(flags & SUPPRESS))
419 1.1 christos mbp += nconv;
420 1.1 christos width -= nconv;
421 1.1 christos n++;
422 1.1 christos }
423 1.1 christos if (n == 0)
424 1.1 christos goto input_failure;
425 1.1 christos nread += n;
426 1.1 christos if (!(flags & SUPPRESS))
427 1.1 christos nassigned++;
428 1.1 christos }
429 1.1 christos nconversions++;
430 1.1 christos break;
431 1.1 christos
432 1.1 christos case CT_CCL:
433 1.1 christos /* scan a (nonempty) character class (sets NOSKIP) */
434 1.1 christos if (width == 0)
435 1.1 christos width = (size_t)~0; /* `infinity' */
436 1.1 christos /* take only those things in the class */
437 1.1 christos if ((flags & SUPPRESS) && (flags & LONG)) {
438 1.1 christos n = 0;
439 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
440 1.1 christos width-- != 0 && INCCL(wi))
441 1.1 christos n++;
442 1.1 christos if (wi != WEOF)
443 1.1 christos ungetwc(wi, fp);
444 1.1 christos if (n == 0)
445 1.1 christos goto match_failure;
446 1.1 christos } else if (flags & LONG) {
447 1.1 christos p0 = p = va_arg(ap, wchar_t *);
448 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
449 1.1 christos width-- != 0 && INCCL(wi))
450 1.1 christos *p++ = (wchar_t)wi;
451 1.1 christos if (wi != WEOF)
452 1.1 christos ungetwc(wi, fp);
453 1.1 christos n = p - p0;
454 1.1 christos if (n == 0)
455 1.1 christos goto match_failure;
456 1.1 christos *p = 0;
457 1.1 christos nassigned++;
458 1.1 christos } else {
459 1.1 christos if (!(flags & SUPPRESS))
460 1.1 christos mbp = va_arg(ap, char *);
461 1.1 christos n = 0;
462 1.1 christos mbs = initial;
463 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
464 1.1 christos width != 0 && INCCL(wi)) {
465 1.1 christos if (width >= MB_CUR_MAX &&
466 1.1 christos !(flags & SUPPRESS)) {
467 1.1 christos nconv = wcrtomb(mbp, wi, &mbs);
468 1.1 christos if (nconv == (size_t)-1)
469 1.1 christos goto input_failure;
470 1.1 christos } else {
471 1.1 christos nconv = wcrtomb(mbbuf, wi,
472 1.1 christos &mbs);
473 1.1 christos if (nconv == (size_t)-1)
474 1.1 christos goto input_failure;
475 1.1 christos if (nconv > width)
476 1.1 christos break;
477 1.1 christos if (!(flags & SUPPRESS))
478 1.1 christos memcpy(mbp, mbbuf,
479 1.1 christos nconv);
480 1.1 christos }
481 1.1 christos if (!(flags & SUPPRESS))
482 1.1 christos mbp += nconv;
483 1.1 christos width -= nconv;
484 1.1 christos n++;
485 1.1 christos }
486 1.1 christos if (wi != WEOF)
487 1.1 christos ungetwc(wi, fp);
488 1.1 christos if (!(flags & SUPPRESS)) {
489 1.1 christos *mbp = 0;
490 1.1 christos nassigned++;
491 1.1 christos }
492 1.1 christos }
493 1.1 christos nread += n;
494 1.1 christos nconversions++;
495 1.1 christos break;
496 1.1 christos
497 1.1 christos case CT_STRING:
498 1.1 christos /* like CCL, but zero-length string OK, & no NOSKIP */
499 1.1 christos if (width == 0)
500 1.1 christos width = (size_t)~0;
501 1.1 christos if ((flags & SUPPRESS) && (flags & LONG)) {
502 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
503 1.1 christos width-- != 0 &&
504 1.1 christos !iswspace(wi))
505 1.1 christos nread++;
506 1.1 christos if (wi != WEOF)
507 1.1 christos ungetwc(wi, fp);
508 1.1 christos } else if (flags & LONG) {
509 1.1 christos p0 = p = va_arg(ap, wchar_t *);
510 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
511 1.1 christos width-- != 0 &&
512 1.1 christos !iswspace(wi)) {
513 1.1 christos *p++ = (wchar_t)wi;
514 1.1 christos nread++;
515 1.1 christos }
516 1.1 christos if (wi != WEOF)
517 1.1 christos ungetwc(wi, fp);
518 1.1 christos *p = '\0';
519 1.1 christos nassigned++;
520 1.1 christos } else {
521 1.1 christos if (!(flags & SUPPRESS))
522 1.1 christos mbp = va_arg(ap, char *);
523 1.1 christos mbs = initial;
524 1.1 christos while ((wi = __fgetwc_unlock(fp)) != WEOF &&
525 1.1 christos width != 0 &&
526 1.1 christos !iswspace(wi)) {
527 1.1 christos if (width >= MB_CUR_MAX &&
528 1.1 christos !(flags & SUPPRESS)) {
529 1.1 christos nconv = wcrtomb(mbp, wi, &mbs);
530 1.1 christos if (nconv == (size_t)-1)
531 1.1 christos goto input_failure;
532 1.1 christos } else {
533 1.1 christos nconv = wcrtomb(mbbuf, wi,
534 1.1 christos &mbs);
535 1.1 christos if (nconv == (size_t)-1)
536 1.1 christos goto input_failure;
537 1.1 christos if (nconv > width)
538 1.1 christos break;
539 1.1 christos if (!(flags & SUPPRESS))
540 1.1 christos memcpy(mbp, mbbuf,
541 1.1 christos nconv);
542 1.1 christos }
543 1.1 christos if (!(flags & SUPPRESS))
544 1.1 christos mbp += nconv;
545 1.1 christos width -= nconv;
546 1.1 christos nread++;
547 1.1 christos }
548 1.1 christos if (wi != WEOF)
549 1.1 christos ungetwc(wi, fp);
550 1.1 christos if (!(flags & SUPPRESS)) {
551 1.1 christos *mbp = 0;
552 1.1 christos nassigned++;
553 1.1 christos }
554 1.1 christos }
555 1.1 christos nconversions++;
556 1.1 christos continue;
557 1.1 christos
558 1.1 christos case CT_INT:
559 1.1 christos /* scan an integer as if by the conversion function */
560 1.1 christos if (width == 0 || width > sizeof(buf) /
561 1.1 christos sizeof(*buf) - 1)
562 1.1 christos width = sizeof(buf) / sizeof(*buf) - 1;
563 1.1 christos flags |= SIGNOK | NDIGITS | NZDIGITS;
564 1.1 christos for (p = buf; width; width--) {
565 1.1 christos c = __fgetwc_unlock(fp);
566 1.1 christos /*
567 1.1 christos * Switch on the character; `goto ok'
568 1.1 christos * if we accept it as a part of number.
569 1.1 christos */
570 1.1 christos switch (c) {
571 1.1 christos
572 1.1 christos /*
573 1.1 christos * The digit 0 is always legal, but is
574 1.1 christos * special. For %i conversions, if no
575 1.1 christos * digits (zero or nonzero) have been
576 1.1 christos * scanned (only signs), we will have
577 1.1 christos * base==0. In that case, we should set
578 1.1 christos * it to 8 and enable 0x prefixing.
579 1.1 christos * Also, if we have not scanned zero digits
580 1.1 christos * before this, do not turn off prefixing
581 1.1 christos * (someone else will turn it off if we
582 1.1 christos * have scanned any nonzero digits).
583 1.1 christos */
584 1.1 christos case '0':
585 1.1 christos if (base == 0) {
586 1.1 christos base = 8;
587 1.1 christos flags |= PFXOK;
588 1.1 christos }
589 1.1 christos if (flags & NZDIGITS)
590 1.1 christos flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
591 1.1 christos else
592 1.1 christos flags &= ~(SIGNOK|PFXOK|NDIGITS);
593 1.1 christos goto ok;
594 1.1 christos
595 1.1 christos /* 1 through 7 always legal */
596 1.1 christos case '1': case '2': case '3':
597 1.1 christos case '4': case '5': case '6': case '7':
598 1.1 christos base = basefix[base];
599 1.1 christos flags &= ~(SIGNOK | PFXOK | NDIGITS);
600 1.1 christos goto ok;
601 1.1 christos
602 1.1 christos /* digits 8 and 9 ok iff decimal or hex */
603 1.1 christos case '8': case '9':
604 1.1 christos base = basefix[base];
605 1.1 christos if (base <= 8)
606 1.1 christos break; /* not legal here */
607 1.1 christos flags &= ~(SIGNOK | PFXOK | NDIGITS);
608 1.1 christos goto ok;
609 1.1 christos
610 1.1 christos /* letters ok iff hex */
611 1.1 christos case 'A': case 'B': case 'C':
612 1.1 christos case 'D': case 'E': case 'F':
613 1.1 christos case 'a': case 'b': case 'c':
614 1.1 christos case 'd': case 'e': case 'f':
615 1.1 christos /* no need to fix base here */
616 1.1 christos if (base <= 10)
617 1.1 christos break; /* not legal here */
618 1.1 christos flags &= ~(SIGNOK | PFXOK | NDIGITS);
619 1.1 christos goto ok;
620 1.1 christos
621 1.1 christos /* sign ok only as first character */
622 1.1 christos case '+': case '-':
623 1.1 christos if (flags & SIGNOK) {
624 1.1 christos flags &= ~SIGNOK;
625 1.1 christos flags |= HAVESIGN;
626 1.1 christos goto ok;
627 1.1 christos }
628 1.1 christos break;
629 1.1 christos
630 1.1 christos /*
631 1.1 christos * x ok iff flag still set & 2nd char (or
632 1.1 christos * 3rd char if we have a sign).
633 1.1 christos */
634 1.1 christos case 'x': case 'X':
635 1.1 christos if (flags & PFXOK && p ==
636 1.1 christos buf + 1 + !!(flags & HAVESIGN)) {
637 1.1 christos base = 16; /* if %i */
638 1.1 christos flags &= ~PFXOK;
639 1.1 christos goto ok;
640 1.1 christos }
641 1.1 christos break;
642 1.1 christos }
643 1.1 christos
644 1.1 christos /*
645 1.1 christos * If we got here, c is not a legal character
646 1.1 christos * for a number. Stop accumulating digits.
647 1.1 christos */
648 1.1 christos if (c != WEOF)
649 1.1 christos ungetwc(c, fp);
650 1.1 christos break;
651 1.1 christos ok:
652 1.1 christos /*
653 1.1 christos * c is legal: store it and look at the next.
654 1.1 christos */
655 1.1 christos *p++ = (wchar_t)c;
656 1.1 christos }
657 1.1 christos /*
658 1.1 christos * If we had only a sign, it is no good; push
659 1.1 christos * back the sign. If the number ends in `x',
660 1.1 christos * it was [sign] '0' 'x', so push back the x
661 1.1 christos * and treat it as [sign] '0'.
662 1.1 christos */
663 1.1 christos if (flags & NDIGITS) {
664 1.1 christos if (p > buf)
665 1.1 christos ungetwc(*--p, fp);
666 1.1 christos goto match_failure;
667 1.1 christos }
668 1.1 christos c = p[-1];
669 1.1 christos if (c == 'x' || c == 'X') {
670 1.1 christos --p;
671 1.1 christos ungetwc(c, fp);
672 1.1 christos }
673 1.1 christos if ((flags & SUPPRESS) == 0) {
674 1.1 christos uintmax_t res;
675 1.1 christos
676 1.1 christos *p = 0;
677 1.1 christos if ((flags & UNSIGNED) == 0)
678 1.1 christos res = wcstoimax(buf, NULL, base);
679 1.1 christos else
680 1.1 christos res = wcstoumax(buf, NULL, base);
681 1.1 christos if (flags & POINTER)
682 1.1 christos *va_arg(ap, void **) =
683 1.1 christos (void *)(uintptr_t)res;
684 1.1 christos else if (flags & SHORTSHORT)
685 1.1 christos *va_arg(ap, char *) = (char)res;
686 1.1 christos else if (flags & SHORT)
687 1.1 christos *va_arg(ap, short *) = (short)res;
688 1.1 christos else if (flags & LONG)
689 1.1 christos *va_arg(ap, long *) = (long)res;
690 1.1 christos else if (flags & LONGLONG)
691 1.1 christos *va_arg(ap, quad_t *) = res;
692 1.1 christos else if (flags & INTMAXT)
693 1.1 christos *va_arg(ap, intmax_t *) = res;
694 1.1 christos else if (flags & PTRDIFFT)
695 1.1 christos *va_arg(ap, ptrdiff_t *) = (ptrdiff_t)res;
696 1.1 christos else if (flags & SIZET)
697 1.1 christos *va_arg(ap, size_t *) = (size_t)res;
698 1.1 christos else
699 1.1 christos *va_arg(ap, int *) = (int)res;
700 1.1 christos nassigned++;
701 1.1 christos }
702 1.1 christos nread += p - buf;
703 1.1 christos nconversions++;
704 1.1 christos break;
705 1.1 christos
706 1.1 christos #ifndef NO_FLOATING_POINT
707 1.1 christos case CT_FLOAT:
708 1.1 christos /* scan a floating point number as if by strtod */
709 1.1 christos if (width == 0 || width > sizeof(buf) /
710 1.1 christos sizeof(*buf) - 1)
711 1.1 christos width = sizeof(buf) / sizeof(*buf) - 1;
712 1.1 christos if ((width = parsefloat(fp, buf, buf + width)) == 0)
713 1.1 christos goto match_failure;
714 1.1 christos if ((flags & SUPPRESS) == 0) {
715 1.1 christos if (flags & LONGDBL) {
716 1.1 christos long double res = wcstold(buf, &p);
717 1.1 christos *va_arg(ap, long double *) = res;
718 1.1 christos } else
719 1.1 christos if (flags & LONG) {
720 1.1 christos double res = wcstod(buf, &p);
721 1.1 christos *va_arg(ap, double *) = res;
722 1.1 christos } else {
723 1.1 christos float res = wcstof(buf, &p);
724 1.1 christos *va_arg(ap, float *) = res;
725 1.1 christos }
726 1.1 christos #ifdef DEBUG
727 1.6 christos if (p - buf != (ptrdiff_t)width)
728 1.1 christos abort();
729 1.1 christos #endif
730 1.1 christos nassigned++;
731 1.1 christos }
732 1.1 christos nread += width;
733 1.1 christos nconversions++;
734 1.1 christos break;
735 1.1 christos #endif /* !NO_FLOATING_POINT */
736 1.1 christos }
737 1.1 christos }
738 1.1 christos input_failure:
739 1.1 christos return (nconversions != 0 ? nassigned : EOF);
740 1.1 christos match_failure:
741 1.1 christos return (nassigned);
742 1.1 christos }
743 1.1 christos
744 1.1 christos #ifndef NO_FLOATING_POINT
745 1.1 christos static int
746 1.1 christos parsefloat(FILE *fp, wchar_t *buf, wchar_t *end)
747 1.1 christos {
748 1.1 christos wchar_t *commit, *p;
749 1.1 christos int infnanpos = 0;
750 1.1 christos enum {
751 1.1 christos S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
752 1.1 christos S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
753 1.1 christos } state = S_START;
754 1.1 christos wchar_t c;
755 1.1 christos wchar_t decpt = (wchar_t)(unsigned char)*localeconv()->decimal_point;
756 1.1 christos int gotmantdig = 0, ishex = 0;
757 1.1 christos
758 1.1 christos /*
759 1.1 christos * We set commit = p whenever the string we have read so far
760 1.1 christos * constitutes a valid representation of a floating point
761 1.1 christos * number by itself. At some point, the parse will complete
762 1.1 christos * or fail, and we will ungetc() back to the last commit point.
763 1.1 christos * To ensure that the file offset gets updated properly, it is
764 1.1 christos * always necessary to read at least one character that doesn't
765 1.1 christos * match; thus, we can't short-circuit "infinity" or "nan(...)".
766 1.1 christos */
767 1.1 christos commit = buf - 1;
768 1.1 christos c = WEOF;
769 1.1 christos for (p = buf; p < end; ) {
770 1.1 christos if ((c = __fgetwc_unlock(fp)) == WEOF)
771 1.1 christos break;
772 1.1 christos reswitch:
773 1.1 christos switch (state) {
774 1.1 christos case S_START:
775 1.1 christos state = S_GOTSIGN;
776 1.1 christos if (c == '-' || c == '+')
777 1.1 christos break;
778 1.1 christos else
779 1.1 christos goto reswitch;
780 1.1 christos case S_GOTSIGN:
781 1.1 christos switch (c) {
782 1.1 christos case '0':
783 1.1 christos state = S_MAYBEHEX;
784 1.1 christos commit = p;
785 1.1 christos break;
786 1.1 christos case 'I':
787 1.1 christos case 'i':
788 1.1 christos state = S_INF;
789 1.1 christos break;
790 1.1 christos case 'N':
791 1.1 christos case 'n':
792 1.1 christos state = S_NAN;
793 1.1 christos break;
794 1.1 christos default:
795 1.1 christos state = S_DIGITS;
796 1.1 christos goto reswitch;
797 1.1 christos }
798 1.1 christos break;
799 1.1 christos case S_INF:
800 1.1 christos if (infnanpos > 6 ||
801 1.1 christos (c != "nfinity"[infnanpos] &&
802 1.1 christos c != "NFINITY"[infnanpos]))
803 1.1 christos goto parsedone;
804 1.1 christos if (infnanpos == 1 || infnanpos == 6)
805 1.1 christos commit = p; /* inf or infinity */
806 1.1 christos infnanpos++;
807 1.1 christos break;
808 1.1 christos case S_NAN:
809 1.1 christos switch (infnanpos) {
810 1.1 christos case -1: /* XXX kludge to deal with nan(...) */
811 1.1 christos goto parsedone;
812 1.1 christos case 0:
813 1.1 christos if (c != 'A' && c != 'a')
814 1.1 christos goto parsedone;
815 1.1 christos break;
816 1.1 christos case 1:
817 1.1 christos if (c != 'N' && c != 'n')
818 1.1 christos goto parsedone;
819 1.1 christos else
820 1.1 christos commit = p;
821 1.1 christos break;
822 1.1 christos case 2:
823 1.1 christos if (c != '(')
824 1.1 christos goto parsedone;
825 1.1 christos break;
826 1.1 christos default:
827 1.1 christos if (c == ')') {
828 1.1 christos commit = p;
829 1.1 christos infnanpos = -2;
830 1.1 christos } else if (!iswalnum(c) && c != '_')
831 1.1 christos goto parsedone;
832 1.1 christos break;
833 1.1 christos }
834 1.1 christos infnanpos++;
835 1.1 christos break;
836 1.1 christos case S_MAYBEHEX:
837 1.1 christos state = S_DIGITS;
838 1.1 christos if (c == 'X' || c == 'x') {
839 1.1 christos ishex = 1;
840 1.1 christos break;
841 1.1 christos } else { /* we saw a '0', but no 'x' */
842 1.1 christos gotmantdig = 1;
843 1.1 christos goto reswitch;
844 1.1 christos }
845 1.1 christos case S_DIGITS:
846 1.1 christos if ((ishex && iswxdigit(c)) || iswdigit(c))
847 1.1 christos gotmantdig = 1;
848 1.1 christos else {
849 1.1 christos state = S_FRAC;
850 1.1 christos if (c != decpt)
851 1.1 christos goto reswitch;
852 1.1 christos }
853 1.1 christos if (gotmantdig)
854 1.1 christos commit = p;
855 1.1 christos break;
856 1.1 christos case S_FRAC:
857 1.1 christos if (((c == 'E' || c == 'e') && !ishex) ||
858 1.1 christos ((c == 'P' || c == 'p') && ishex)) {
859 1.1 christos if (!gotmantdig)
860 1.1 christos goto parsedone;
861 1.1 christos else
862 1.1 christos state = S_EXP;
863 1.1 christos } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
864 1.1 christos commit = p;
865 1.1 christos gotmantdig = 1;
866 1.1 christos } else
867 1.1 christos goto parsedone;
868 1.1 christos break;
869 1.1 christos case S_EXP:
870 1.1 christos state = S_EXPDIGITS;
871 1.1 christos if (c == '-' || c == '+')
872 1.1 christos break;
873 1.1 christos else
874 1.1 christos goto reswitch;
875 1.1 christos case S_EXPDIGITS:
876 1.1 christos if (iswdigit(c))
877 1.1 christos commit = p;
878 1.1 christos else
879 1.1 christos goto parsedone;
880 1.1 christos break;
881 1.1 christos default:
882 1.1 christos abort();
883 1.1 christos }
884 1.1 christos *p++ = c;
885 1.1 christos c = WEOF;
886 1.1 christos }
887 1.1 christos
888 1.1 christos parsedone:
889 1.1 christos if (c != WEOF)
890 1.1 christos ungetwc(c, fp);
891 1.1 christos while (commit < --p)
892 1.1 christos ungetwc(*p, fp);
893 1.1 christos *++commit = '\0';
894 1.1 christos return (commit - buf);
895 1.1 christos }
896 1.1 christos #endif
897