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