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