vfscanf.c revision 1.37.4.1 1 1.37.4.1 pavel /* $NetBSD: vfscanf.c,v 1.37.4.1 2007/05/07 19:49:08 pavel Exp $ */
2 1.12 jtc
3 1.1 cgd /*-
4 1.12 jtc * Copyright (c) 1990, 1993
5 1.12 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Chris Torek.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.35 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.16 christos #include <sys/cdefs.h>
36 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
37 1.12 jtc #if 0
38 1.12 jtc static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
39 1.37.4.1 pavel __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.41 2007/01/09 00:28:07 imp Exp $");
40 1.16 christos #else
41 1.37.4.1 pavel __RCSID("$NetBSD: vfscanf.c,v 1.37.4.1 2007/05/07 19:49:08 pavel Exp $");
42 1.12 jtc #endif
43 1.1 cgd #endif /* LIBC_SCCS and not lint */
44 1.1 cgd
45 1.16 christos #include "namespace.h"
46 1.23 lukem #include <assert.h>
47 1.37.4.1 pavel #include <ctype.h>
48 1.28 kleink #include <inttypes.h>
49 1.1 cgd #include <stdio.h>
50 1.1 cgd #include <stdlib.h>
51 1.37.4.1 pavel #include <stddef.h>
52 1.37.4.1 pavel #include <stdarg.h>
53 1.37.4.1 pavel #include <string.h>
54 1.37.4.1 pavel #include <wchar.h>
55 1.37.4.1 pavel #include <wctype.h>
56 1.23 lukem
57 1.33 thorpej #include "reentrant.h"
58 1.1 cgd #include "local.h"
59 1.1 cgd
60 1.37 christos #ifndef NO_FLOATING_POINT
61 1.37.4.1 pavel #include <locale.h>
62 1.31 thorpej #endif
63 1.31 thorpej
64 1.31 thorpej /*
65 1.31 thorpej * Provide an external name for vfscanf. Note, we don't use the normal
66 1.31 thorpej * namespace.h method; stdio routines explicitly use the internal name
67 1.31 thorpej * __svfscanf.
68 1.31 thorpej */
69 1.31 thorpej #ifdef __weak_alias
70 1.31 thorpej __weak_alias(vfscanf,__svfscanf)
71 1.8 mycroft #endif
72 1.1 cgd
73 1.3 cgd #define BUF 513 /* Maximum length of numeric string. */
74 1.1 cgd
75 1.1 cgd /*
76 1.1 cgd * Flags used during conversion.
77 1.1 cgd */
78 1.28 kleink #define LONG 0x0001 /* l: long or double */
79 1.37.4.1 pavel #define LONGDBL 0x0002 /* L: long double */
80 1.28 kleink #define SHORT 0x0004 /* h: short */
81 1.37.4.1 pavel #define SUPPRESS 0x0008 /* *: suppress assignment */
82 1.37.4.1 pavel #define POINTER 0x0010 /* p: void * (as hex) */
83 1.37.4.1 pavel #define NOSKIP 0x0020 /* [ or c: do not skip blanks */
84 1.37.4.1 pavel #define LONGLONG 0x0400 /* ll: long long (+ deprecated q: quad) */
85 1.37.4.1 pavel #define INTMAXT 0x0800 /* j: intmax_t */
86 1.37.4.1 pavel #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
87 1.37.4.1 pavel #define SIZET 0x2000 /* z: size_t */
88 1.37.4.1 pavel #define SHORTSHORT 0x4000 /* hh: char */
89 1.37.4.1 pavel #define UNSIGNED 0x8000 /* %[oupxX] conversions */
90 1.37.4.1 pavel
91 1.1 cgd /*
92 1.37.4.1 pavel * The following are used in integral conversions only:
93 1.37.4.1 pavel * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
94 1.1 cgd */
95 1.37.4.1 pavel #define SIGNOK 0x00040 /* +/- is (still) legal */
96 1.37.4.1 pavel #define NDIGITS 0x00080 /* no digits detected */
97 1.37.4.1 pavel #define PFXOK 0x00100 /* 0x prefix is (still) legal */
98 1.37.4.1 pavel #define NZDIGITS 0x00200 /* no zero digits detected */
99 1.37.4.1 pavel #define HAVESIGN 0x10000 /* sign detected */
100 1.1 cgd
101 1.1 cgd /*
102 1.1 cgd * Conversion types.
103 1.1 cgd */
104 1.1 cgd #define CT_CHAR 0 /* %c conversion */
105 1.1 cgd #define CT_CCL 1 /* %[...] conversion */
106 1.1 cgd #define CT_STRING 2 /* %s conversion */
107 1.37.4.1 pavel #define CT_INT 3 /* %[dioupxX] conversion */
108 1.37.4.1 pavel #define CT_FLOAT 4 /* %[efgEFG] conversion */
109 1.1 cgd
110 1.37.4.1 pavel static const u_char *__sccl(char *, const u_char *);
111 1.37.4.1 pavel #ifndef NO_FLOATING_POINT
112 1.37.4.1 pavel static int parsefloat(FILE *, char *, char *);
113 1.37.4.1 pavel #endif
114 1.37.4.1 pavel
115 1.37.4.1 pavel int __scanfdebug = 0;
116 1.1 cgd
117 1.37.4.1 pavel #define __collate_load_error /*CONSTCOND*/0
118 1.37.4.1 pavel static int
119 1.37.4.1 pavel __collate_range_cmp(int c1, int c2)
120 1.37.4.1 pavel {
121 1.37.4.1 pavel static char s1[2], s2[2];
122 1.1 cgd
123 1.37.4.1 pavel s1[0] = c1;
124 1.37.4.1 pavel s2[0] = c2;
125 1.37.4.1 pavel return strcoll(s1, s2);
126 1.37.4.1 pavel }
127 1.37.4.1 pavel
128 1.37.4.1 pavel
129 1.37.4.1 pavel /*
130 1.37.4.1 pavel * __svfscanf - MT-safe version
131 1.37.4.1 pavel */
132 1.33 thorpej int
133 1.37.4.1 pavel __svfscanf(FILE *fp, char const *fmt0, va_list ap)
134 1.33 thorpej {
135 1.33 thorpej int ret;
136 1.33 thorpej
137 1.33 thorpej FLOCKFILE(fp);
138 1.33 thorpej ret = __svfscanf_unlocked(fp, fmt0, ap);
139 1.33 thorpej FUNLOCKFILE(fp);
140 1.37.4.1 pavel return (ret);
141 1.33 thorpej }
142 1.33 thorpej
143 1.1 cgd /*
144 1.37.4.1 pavel * __svfscanf_unlocked - non-MT-safe version of __svfscanf
145 1.1 cgd */
146 1.15 jtc int
147 1.37.4.1 pavel __svfscanf_unlocked(FILE *fp, const char *fmt0, va_list ap)
148 1.1 cgd {
149 1.22 christos const u_char *fmt = (const u_char *)fmt0;
150 1.37.4.1 pavel int c; /* character from format, or conversion */
151 1.37.4.1 pavel size_t width; /* field width, or 0 */
152 1.37.4.1 pavel char *p; /* points into all kinds of strings */
153 1.37.4.1 pavel size_t n; /* handy size_t */
154 1.37.4.1 pavel int flags; /* flags as defined above */
155 1.37.4.1 pavel char *p0; /* saves original value of p when necessary */
156 1.1 cgd int nassigned; /* number of fields assigned */
157 1.37.4.1 pavel int nconversions; /* number of conversions */
158 1.1 cgd int nread; /* number of characters consumed from fp */
159 1.37.4.1 pavel int base; /* base argument to conversion function */
160 1.1 cgd char ccltab[256]; /* character class table for %[...] */
161 1.37.4.1 pavel char buf[BUF]; /* buffer for numeric and mb conversions */
162 1.37.4.1 pavel wchar_t *wcp; /* handy wide character pointer */
163 1.37.4.1 pavel size_t nconv; /* length of multibyte sequence converted */
164 1.37.4.1 pavel static const mbstate_t initial;
165 1.37.4.1 pavel mbstate_t mbs;
166 1.1 cgd
167 1.1 cgd /* `basefix' is used to avoid `if' tests in the integer scanner */
168 1.19 mycroft static const short basefix[17] =
169 1.1 cgd { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
170 1.1 cgd
171 1.23 lukem _DIAGASSERT(fp != NULL);
172 1.23 lukem _DIAGASSERT(fmt0 != NULL);
173 1.23 lukem
174 1.30 yamt _SET_ORIENTATION(fp, -1);
175 1.25 mycroft
176 1.1 cgd nassigned = 0;
177 1.37.4.1 pavel nconversions = 0;
178 1.1 cgd nread = 0;
179 1.37.4.1 pavel base = 0;
180 1.1 cgd for (;;) {
181 1.37.4.1 pavel c = (unsigned char)*fmt++;
182 1.37.4.1 pavel if (c == 0)
183 1.1 cgd return (nassigned);
184 1.1 cgd if (isspace(c)) {
185 1.21 kleink while ((fp->_r > 0 || __srefill(fp) == 0) &&
186 1.21 kleink isspace(*fp->_p))
187 1.1 cgd nread++, fp->_r--, fp->_p++;
188 1.1 cgd continue;
189 1.1 cgd }
190 1.1 cgd if (c != '%')
191 1.1 cgd goto literal;
192 1.1 cgd width = 0;
193 1.1 cgd flags = 0;
194 1.1 cgd /*
195 1.1 cgd * switch on the format. continue if done;
196 1.1 cgd * break once format type is derived.
197 1.1 cgd */
198 1.1 cgd again: c = *fmt++;
199 1.1 cgd switch (c) {
200 1.1 cgd case '%':
201 1.1 cgd literal:
202 1.1 cgd if (fp->_r <= 0 && __srefill(fp))
203 1.1 cgd goto input_failure;
204 1.1 cgd if (*fp->_p != c)
205 1.1 cgd goto match_failure;
206 1.1 cgd fp->_r--, fp->_p++;
207 1.1 cgd nread++;
208 1.1 cgd continue;
209 1.1 cgd
210 1.1 cgd case '*':
211 1.1 cgd flags |= SUPPRESS;
212 1.1 cgd goto again;
213 1.28 kleink case 'j':
214 1.37.4.1 pavel flags |= INTMAXT;
215 1.28 kleink goto again;
216 1.10 jtc case 'l':
217 1.37.4.1 pavel if (flags & LONG) {
218 1.37.4.1 pavel flags &= ~LONG;
219 1.27 kleink flags |= LONGLONG;
220 1.37.4.1 pavel } else
221 1.14 jtc flags |= LONG;
222 1.10 jtc goto again;
223 1.10 jtc case 'q':
224 1.37.4.1 pavel flags |= LONGLONG; /* not quite */
225 1.10 jtc goto again;
226 1.28 kleink case 't':
227 1.37.4.1 pavel flags |= PTRDIFFT;
228 1.28 kleink goto again;
229 1.28 kleink case 'z':
230 1.37.4.1 pavel flags |= SIZET;
231 1.37.4.1 pavel goto again;
232 1.37.4.1 pavel case 'L':
233 1.37.4.1 pavel flags |= LONGDBL;
234 1.37.4.1 pavel goto again;
235 1.37.4.1 pavel case 'h':
236 1.37.4.1 pavel if (flags & SHORT) {
237 1.37.4.1 pavel flags &= ~SHORT;
238 1.37.4.1 pavel flags |= SHORTSHORT;
239 1.37.4.1 pavel } else
240 1.37.4.1 pavel flags |= SHORT;
241 1.28 kleink goto again;
242 1.1 cgd
243 1.1 cgd case '0': case '1': case '2': case '3': case '4':
244 1.1 cgd case '5': case '6': case '7': case '8': case '9':
245 1.1 cgd width = width * 10 + c - '0';
246 1.1 cgd goto again;
247 1.1 cgd
248 1.1 cgd /*
249 1.1 cgd * Conversions.
250 1.1 cgd */
251 1.1 cgd case 'd':
252 1.1 cgd c = CT_INT;
253 1.1 cgd base = 10;
254 1.1 cgd break;
255 1.1 cgd
256 1.1 cgd case 'i':
257 1.1 cgd c = CT_INT;
258 1.1 cgd base = 0;
259 1.1 cgd break;
260 1.1 cgd
261 1.1 cgd case 'o':
262 1.1 cgd c = CT_INT;
263 1.37.4.1 pavel flags |= UNSIGNED;
264 1.1 cgd base = 8;
265 1.1 cgd break;
266 1.1 cgd
267 1.1 cgd case 'u':
268 1.1 cgd c = CT_INT;
269 1.37.4.1 pavel flags |= UNSIGNED;
270 1.1 cgd base = 10;
271 1.1 cgd break;
272 1.1 cgd
273 1.9 jtc case 'X':
274 1.1 cgd case 'x':
275 1.1 cgd flags |= PFXOK; /* enable 0x prefixing */
276 1.1 cgd c = CT_INT;
277 1.37.4.1 pavel flags |= UNSIGNED;
278 1.1 cgd base = 16;
279 1.1 cgd break;
280 1.1 cgd
281 1.37 christos #ifndef NO_FLOATING_POINT
282 1.37.4.1 pavel case 'A': case 'E': case 'F': case 'G':
283 1.37.4.1 pavel case 'a': case 'e': case 'f': case 'g':
284 1.1 cgd c = CT_FLOAT;
285 1.1 cgd break;
286 1.1 cgd #endif
287 1.1 cgd
288 1.37.4.1 pavel case 'S':
289 1.37.4.1 pavel flags |= LONG;
290 1.37.4.1 pavel /* FALLTHROUGH */
291 1.1 cgd case 's':
292 1.1 cgd c = CT_STRING;
293 1.1 cgd break;
294 1.1 cgd
295 1.1 cgd case '[':
296 1.1 cgd fmt = __sccl(ccltab, fmt);
297 1.1 cgd flags |= NOSKIP;
298 1.1 cgd c = CT_CCL;
299 1.1 cgd break;
300 1.1 cgd
301 1.37.4.1 pavel case 'C':
302 1.37.4.1 pavel flags |= LONG;
303 1.37.4.1 pavel /* FALLTHROUGH */
304 1.1 cgd case 'c':
305 1.1 cgd flags |= NOSKIP;
306 1.1 cgd c = CT_CHAR;
307 1.1 cgd break;
308 1.1 cgd
309 1.1 cgd case 'p': /* pointer format is like hex */
310 1.1 cgd flags |= POINTER | PFXOK;
311 1.37.4.1 pavel c = CT_INT; /* assumes sizeof(uintmax_t) */
312 1.37.4.1 pavel flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
313 1.1 cgd base = 16;
314 1.1 cgd break;
315 1.1 cgd
316 1.1 cgd case 'n':
317 1.37.4.1 pavel nconversions++;
318 1.1 cgd if (flags & SUPPRESS) /* ??? */
319 1.1 cgd continue;
320 1.37.4.1 pavel if (flags & SHORTSHORT)
321 1.36 martin *va_arg(ap, char *) = nread;
322 1.37.4.1 pavel else if (flags & SHORT)
323 1.37.4.1 pavel *va_arg(ap, short *) = nread;
324 1.1 cgd else if (flags & LONG)
325 1.1 cgd *va_arg(ap, long *) = nread;
326 1.28 kleink else if (flags & LONGLONG)
327 1.37.4.1 pavel *va_arg(ap, long long *) = nread;
328 1.37.4.1 pavel else if (flags & INTMAXT)
329 1.28 kleink *va_arg(ap, intmax_t *) = nread;
330 1.37.4.1 pavel else if (flags & SIZET)
331 1.37.4.1 pavel *va_arg(ap, size_t *) = nread;
332 1.37.4.1 pavel else if (flags & PTRDIFFT)
333 1.37.4.1 pavel *va_arg(ap, ptrdiff_t *) = nread;
334 1.1 cgd else
335 1.1 cgd *va_arg(ap, int *) = nread;
336 1.1 cgd continue;
337 1.1 cgd
338 1.37.4.1 pavel default:
339 1.37.4.1 pavel goto match_failure;
340 1.37.4.1 pavel
341 1.1 cgd /*
342 1.37.4.1 pavel * Disgusting backwards compatibility hack. XXX
343 1.1 cgd */
344 1.1 cgd case '\0': /* compat */
345 1.1 cgd return (EOF);
346 1.1 cgd }
347 1.1 cgd
348 1.1 cgd /*
349 1.1 cgd * We have a conversion that requires input.
350 1.1 cgd */
351 1.1 cgd if (fp->_r <= 0 && __srefill(fp))
352 1.1 cgd goto input_failure;
353 1.1 cgd
354 1.1 cgd /*
355 1.1 cgd * Consume leading white space, except for formats
356 1.1 cgd * that suppress this.
357 1.1 cgd */
358 1.1 cgd if ((flags & NOSKIP) == 0) {
359 1.1 cgd while (isspace(*fp->_p)) {
360 1.1 cgd nread++;
361 1.1 cgd if (--fp->_r > 0)
362 1.1 cgd fp->_p++;
363 1.1 cgd else if (__srefill(fp))
364 1.1 cgd goto input_failure;
365 1.1 cgd }
366 1.1 cgd /*
367 1.1 cgd * Note that there is at least one character in
368 1.1 cgd * the buffer, so conversions that do not set NOSKIP
369 1.1 cgd * ca no longer result in an input failure.
370 1.1 cgd */
371 1.1 cgd }
372 1.1 cgd
373 1.1 cgd /*
374 1.1 cgd * Do the conversion.
375 1.1 cgd */
376 1.1 cgd switch (c) {
377 1.1 cgd
378 1.1 cgd case CT_CHAR:
379 1.1 cgd /* scan arbitrary characters (sets NOSKIP) */
380 1.1 cgd if (width == 0)
381 1.1 cgd width = 1;
382 1.37.4.1 pavel if (flags & LONG) {
383 1.37.4.1 pavel if ((flags & SUPPRESS) == 0)
384 1.37.4.1 pavel wcp = va_arg(ap, wchar_t *);
385 1.37.4.1 pavel else
386 1.37.4.1 pavel wcp = NULL;
387 1.37.4.1 pavel n = 0;
388 1.37.4.1 pavel while (width != 0) {
389 1.37.4.1 pavel if (n == MB_CUR_MAX) {
390 1.37.4.1 pavel fp->_flags |= __SERR;
391 1.37.4.1 pavel goto input_failure;
392 1.37.4.1 pavel }
393 1.37.4.1 pavel buf[n++] = *fp->_p;
394 1.37.4.1 pavel fp->_p++;
395 1.37.4.1 pavel fp->_r--;
396 1.37.4.1 pavel mbs = initial;
397 1.37.4.1 pavel nconv = mbrtowc(wcp, buf, n, &mbs);
398 1.37.4.1 pavel if (nconv == (size_t)-1) {
399 1.37.4.1 pavel fp->_flags |= __SERR;
400 1.37.4.1 pavel goto input_failure;
401 1.37.4.1 pavel }
402 1.37.4.1 pavel if (nconv == 0 && !(flags & SUPPRESS))
403 1.37.4.1 pavel *wcp = L'\0';
404 1.37.4.1 pavel if (nconv != (size_t)-2) {
405 1.37.4.1 pavel nread += n;
406 1.37.4.1 pavel width--;
407 1.37.4.1 pavel if (!(flags & SUPPRESS))
408 1.37.4.1 pavel wcp++;
409 1.37.4.1 pavel n = 0;
410 1.37.4.1 pavel }
411 1.37.4.1 pavel if (fp->_r <= 0 && __srefill(fp)) {
412 1.37.4.1 pavel if (n != 0) {
413 1.37.4.1 pavel fp->_flags |= __SERR;
414 1.37.4.1 pavel goto input_failure;
415 1.37.4.1 pavel }
416 1.37.4.1 pavel break;
417 1.37.4.1 pavel }
418 1.37.4.1 pavel }
419 1.37.4.1 pavel if (!(flags & SUPPRESS))
420 1.37.4.1 pavel nassigned++;
421 1.37.4.1 pavel } else if (flags & SUPPRESS) {
422 1.1 cgd size_t sum = 0;
423 1.1 cgd for (;;) {
424 1.1 cgd if ((n = fp->_r) < width) {
425 1.1 cgd sum += n;
426 1.1 cgd width -= n;
427 1.1 cgd fp->_p += n;
428 1.1 cgd if (__srefill(fp)) {
429 1.1 cgd if (sum == 0)
430 1.1 cgd goto input_failure;
431 1.1 cgd break;
432 1.1 cgd }
433 1.1 cgd } else {
434 1.1 cgd sum += width;
435 1.1 cgd fp->_r -= width;
436 1.1 cgd fp->_p += width;
437 1.1 cgd break;
438 1.1 cgd }
439 1.1 cgd }
440 1.1 cgd nread += sum;
441 1.1 cgd } else {
442 1.37.4.1 pavel size_t r = fread(va_arg(ap, char *), 1,
443 1.1 cgd width, fp);
444 1.1 cgd
445 1.1 cgd if (r == 0)
446 1.1 cgd goto input_failure;
447 1.1 cgd nread += r;
448 1.1 cgd nassigned++;
449 1.1 cgd }
450 1.37.4.1 pavel nconversions++;
451 1.1 cgd break;
452 1.1 cgd
453 1.1 cgd case CT_CCL:
454 1.1 cgd /* scan a (nonempty) character class (sets NOSKIP) */
455 1.1 cgd if (width == 0)
456 1.37.4.1 pavel width = (size_t)~0; /* `infinity' */
457 1.1 cgd /* take only those things in the class */
458 1.37.4.1 pavel if (flags & LONG) {
459 1.37.4.1 pavel wchar_t twc;
460 1.37.4.1 pavel int nchars;
461 1.37.4.1 pavel
462 1.37.4.1 pavel if ((flags & SUPPRESS) == 0)
463 1.37.4.1 pavel wcp = va_arg(ap, wchar_t *);
464 1.37.4.1 pavel else
465 1.37.4.1 pavel wcp = &twc;
466 1.37.4.1 pavel n = 0;
467 1.37.4.1 pavel nchars = 0;
468 1.37.4.1 pavel while (width != 0) {
469 1.37.4.1 pavel if (n == MB_CUR_MAX) {
470 1.37.4.1 pavel fp->_flags |= __SERR;
471 1.37.4.1 pavel goto input_failure;
472 1.37.4.1 pavel }
473 1.37.4.1 pavel buf[n++] = *fp->_p;
474 1.37.4.1 pavel fp->_p++;
475 1.37.4.1 pavel fp->_r--;
476 1.37.4.1 pavel mbs = initial;
477 1.37.4.1 pavel nconv = mbrtowc(wcp, buf, n, &mbs);
478 1.37.4.1 pavel if (nconv == (size_t)-1) {
479 1.37.4.1 pavel fp->_flags |= __SERR;
480 1.37.4.1 pavel goto input_failure;
481 1.37.4.1 pavel }
482 1.37.4.1 pavel if (nconv == 0)
483 1.37.4.1 pavel *wcp = L'\0';
484 1.37.4.1 pavel if (nconv != (size_t)-2) {
485 1.37.4.1 pavel if (wctob(*wcp) != EOF &&
486 1.37.4.1 pavel !ccltab[wctob(*wcp)]) {
487 1.37.4.1 pavel while (n != 0) {
488 1.37.4.1 pavel n--;
489 1.37.4.1 pavel (void)ungetc(buf[n],
490 1.37.4.1 pavel fp);
491 1.37.4.1 pavel }
492 1.37.4.1 pavel break;
493 1.37.4.1 pavel }
494 1.37.4.1 pavel nread += n;
495 1.37.4.1 pavel width--;
496 1.37.4.1 pavel if (!(flags & SUPPRESS))
497 1.37.4.1 pavel wcp++;
498 1.37.4.1 pavel nchars++;
499 1.37.4.1 pavel n = 0;
500 1.37.4.1 pavel }
501 1.37.4.1 pavel if (fp->_r <= 0 && __srefill(fp)) {
502 1.37.4.1 pavel if (n != 0) {
503 1.37.4.1 pavel fp->_flags |= __SERR;
504 1.37.4.1 pavel goto input_failure;
505 1.37.4.1 pavel }
506 1.37.4.1 pavel break;
507 1.37.4.1 pavel }
508 1.37.4.1 pavel }
509 1.37.4.1 pavel if (n != 0) {
510 1.37.4.1 pavel fp->_flags |= __SERR;
511 1.37.4.1 pavel goto input_failure;
512 1.37.4.1 pavel }
513 1.37.4.1 pavel n = nchars;
514 1.37.4.1 pavel if (n == 0)
515 1.37.4.1 pavel goto match_failure;
516 1.37.4.1 pavel if (!(flags & SUPPRESS)) {
517 1.37.4.1 pavel *wcp = L'\0';
518 1.37.4.1 pavel nassigned++;
519 1.37.4.1 pavel }
520 1.37.4.1 pavel } else if (flags & SUPPRESS) {
521 1.1 cgd n = 0;
522 1.1 cgd while (ccltab[*fp->_p]) {
523 1.1 cgd n++, fp->_r--, fp->_p++;
524 1.1 cgd if (--width == 0)
525 1.1 cgd break;
526 1.1 cgd if (fp->_r <= 0 && __srefill(fp)) {
527 1.1 cgd if (n == 0)
528 1.1 cgd goto input_failure;
529 1.1 cgd break;
530 1.1 cgd }
531 1.1 cgd }
532 1.1 cgd if (n == 0)
533 1.1 cgd goto match_failure;
534 1.1 cgd } else {
535 1.1 cgd p0 = p = va_arg(ap, char *);
536 1.1 cgd while (ccltab[*fp->_p]) {
537 1.1 cgd fp->_r--;
538 1.1 cgd *p++ = *fp->_p++;
539 1.1 cgd if (--width == 0)
540 1.1 cgd break;
541 1.1 cgd if (fp->_r <= 0 && __srefill(fp)) {
542 1.1 cgd if (p == p0)
543 1.1 cgd goto input_failure;
544 1.1 cgd break;
545 1.1 cgd }
546 1.1 cgd }
547 1.1 cgd n = p - p0;
548 1.1 cgd if (n == 0)
549 1.1 cgd goto match_failure;
550 1.1 cgd *p = 0;
551 1.1 cgd nassigned++;
552 1.1 cgd }
553 1.1 cgd nread += n;
554 1.37.4.1 pavel nconversions++;
555 1.1 cgd break;
556 1.1 cgd
557 1.1 cgd case CT_STRING:
558 1.1 cgd /* like CCL, but zero-length string OK, & no NOSKIP */
559 1.1 cgd if (width == 0)
560 1.37.4.1 pavel width = (size_t)~0;
561 1.37.4.1 pavel if (flags & LONG) {
562 1.37.4.1 pavel wchar_t twc;
563 1.37.4.1 pavel
564 1.37.4.1 pavel if ((flags & SUPPRESS) == 0)
565 1.37.4.1 pavel wcp = va_arg(ap, wchar_t *);
566 1.37.4.1 pavel else
567 1.37.4.1 pavel wcp = &twc;
568 1.37.4.1 pavel n = 0;
569 1.37.4.1 pavel while (!isspace(*fp->_p) && width != 0) {
570 1.37.4.1 pavel if (n == MB_CUR_MAX) {
571 1.37.4.1 pavel fp->_flags |= __SERR;
572 1.37.4.1 pavel goto input_failure;
573 1.37.4.1 pavel }
574 1.37.4.1 pavel buf[n++] = *fp->_p;
575 1.37.4.1 pavel fp->_p++;
576 1.37.4.1 pavel fp->_r--;
577 1.37.4.1 pavel mbs = initial;
578 1.37.4.1 pavel nconv = mbrtowc(wcp, buf, n, &mbs);
579 1.37.4.1 pavel if (nconv == (size_t)-1) {
580 1.37.4.1 pavel fp->_flags |= __SERR;
581 1.37.4.1 pavel goto input_failure;
582 1.37.4.1 pavel }
583 1.37.4.1 pavel if (nconv == 0)
584 1.37.4.1 pavel *wcp = L'\0';
585 1.37.4.1 pavel if (nconv != (size_t)-2) {
586 1.37.4.1 pavel if (iswspace(*wcp)) {
587 1.37.4.1 pavel while (n != 0) {
588 1.37.4.1 pavel n--;
589 1.37.4.1 pavel (void)ungetc(buf[n],
590 1.37.4.1 pavel fp);
591 1.37.4.1 pavel }
592 1.37.4.1 pavel break;
593 1.37.4.1 pavel }
594 1.37.4.1 pavel nread += n;
595 1.37.4.1 pavel width--;
596 1.37.4.1 pavel if (!(flags & SUPPRESS))
597 1.37.4.1 pavel wcp++;
598 1.37.4.1 pavel n = 0;
599 1.37.4.1 pavel }
600 1.37.4.1 pavel if (fp->_r <= 0 && __srefill(fp)) {
601 1.37.4.1 pavel if (n != 0) {
602 1.37.4.1 pavel fp->_flags |= __SERR;
603 1.37.4.1 pavel goto input_failure;
604 1.37.4.1 pavel }
605 1.37.4.1 pavel break;
606 1.37.4.1 pavel }
607 1.37.4.1 pavel }
608 1.37.4.1 pavel if (!(flags & SUPPRESS)) {
609 1.37.4.1 pavel *wcp = L'\0';
610 1.37.4.1 pavel nassigned++;
611 1.37.4.1 pavel }
612 1.37.4.1 pavel } else if (flags & SUPPRESS) {
613 1.1 cgd n = 0;
614 1.1 cgd while (!isspace(*fp->_p)) {
615 1.1 cgd n++, fp->_r--, fp->_p++;
616 1.1 cgd if (--width == 0)
617 1.1 cgd break;
618 1.1 cgd if (fp->_r <= 0 && __srefill(fp))
619 1.1 cgd break;
620 1.1 cgd }
621 1.1 cgd nread += n;
622 1.1 cgd } else {
623 1.1 cgd p0 = p = va_arg(ap, char *);
624 1.1 cgd while (!isspace(*fp->_p)) {
625 1.1 cgd fp->_r--;
626 1.1 cgd *p++ = *fp->_p++;
627 1.1 cgd if (--width == 0)
628 1.1 cgd break;
629 1.1 cgd if (fp->_r <= 0 && __srefill(fp))
630 1.1 cgd break;
631 1.1 cgd }
632 1.1 cgd *p = 0;
633 1.1 cgd nread += p - p0;
634 1.1 cgd nassigned++;
635 1.1 cgd }
636 1.37.4.1 pavel nconversions++;
637 1.1 cgd continue;
638 1.1 cgd
639 1.1 cgd case CT_INT:
640 1.37.4.1 pavel /* scan an integer as if by the conversion function */
641 1.1 cgd #ifdef hardway
642 1.1 cgd if (width == 0 || width > sizeof(buf) - 1)
643 1.1 cgd width = sizeof(buf) - 1;
644 1.1 cgd #else
645 1.1 cgd /* size_t is unsigned, hence this optimisation */
646 1.1 cgd if (--width > sizeof(buf) - 2)
647 1.1 cgd width = sizeof(buf) - 2;
648 1.1 cgd width++;
649 1.1 cgd #endif
650 1.1 cgd flags |= SIGNOK | NDIGITS | NZDIGITS;
651 1.1 cgd for (p = buf; width; width--) {
652 1.1 cgd c = *fp->_p;
653 1.1 cgd /*
654 1.1 cgd * Switch on the character; `goto ok'
655 1.1 cgd * if we accept it as a part of number.
656 1.1 cgd */
657 1.1 cgd switch (c) {
658 1.1 cgd
659 1.1 cgd /*
660 1.1 cgd * The digit 0 is always legal, but is
661 1.1 cgd * special. For %i conversions, if no
662 1.1 cgd * digits (zero or nonzero) have been
663 1.1 cgd * scanned (only signs), we will have
664 1.1 cgd * base==0. In that case, we should set
665 1.1 cgd * it to 8 and enable 0x prefixing.
666 1.1 cgd * Also, if we have not scanned zero digits
667 1.1 cgd * before this, do not turn off prefixing
668 1.1 cgd * (someone else will turn it off if we
669 1.1 cgd * have scanned any nonzero digits).
670 1.1 cgd */
671 1.1 cgd case '0':
672 1.1 cgd if (base == 0) {
673 1.1 cgd base = 8;
674 1.1 cgd flags |= PFXOK;
675 1.1 cgd }
676 1.1 cgd if (flags & NZDIGITS)
677 1.1 cgd flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
678 1.1 cgd else
679 1.1 cgd flags &= ~(SIGNOK|PFXOK|NDIGITS);
680 1.1 cgd goto ok;
681 1.1 cgd
682 1.1 cgd /* 1 through 7 always legal */
683 1.1 cgd case '1': case '2': case '3':
684 1.1 cgd case '4': case '5': case '6': case '7':
685 1.1 cgd base = basefix[base];
686 1.1 cgd flags &= ~(SIGNOK | PFXOK | NDIGITS);
687 1.1 cgd goto ok;
688 1.1 cgd
689 1.1 cgd /* digits 8 and 9 ok iff decimal or hex */
690 1.1 cgd case '8': case '9':
691 1.1 cgd base = basefix[base];
692 1.1 cgd if (base <= 8)
693 1.1 cgd break; /* not legal here */
694 1.1 cgd flags &= ~(SIGNOK | PFXOK | NDIGITS);
695 1.1 cgd goto ok;
696 1.1 cgd
697 1.1 cgd /* letters ok iff hex */
698 1.1 cgd case 'A': case 'B': case 'C':
699 1.1 cgd case 'D': case 'E': case 'F':
700 1.1 cgd case 'a': case 'b': case 'c':
701 1.1 cgd case 'd': case 'e': case 'f':
702 1.1 cgd /* no need to fix base here */
703 1.1 cgd if (base <= 10)
704 1.1 cgd break; /* not legal here */
705 1.1 cgd flags &= ~(SIGNOK | PFXOK | NDIGITS);
706 1.1 cgd goto ok;
707 1.1 cgd
708 1.1 cgd /* sign ok only as first character */
709 1.1 cgd case '+': case '-':
710 1.1 cgd if (flags & SIGNOK) {
711 1.1 cgd flags &= ~SIGNOK;
712 1.34 thorpej flags |= HAVESIGN;
713 1.1 cgd goto ok;
714 1.1 cgd }
715 1.1 cgd break;
716 1.37.4.1 pavel
717 1.34 thorpej /*
718 1.37.4.1 pavel * x ok iff flag still set & 2nd char (or
719 1.34 thorpej * 3rd char if we have a sign).
720 1.34 thorpej */
721 1.1 cgd case 'x': case 'X':
722 1.34 thorpej if (flags & PFXOK && p ==
723 1.34 thorpej buf + 1 + !!(flags & HAVESIGN)) {
724 1.1 cgd base = 16; /* if %i */
725 1.1 cgd flags &= ~PFXOK;
726 1.1 cgd goto ok;
727 1.1 cgd }
728 1.1 cgd break;
729 1.1 cgd }
730 1.1 cgd
731 1.1 cgd /*
732 1.1 cgd * If we got here, c is not a legal character
733 1.1 cgd * for a number. Stop accumulating digits.
734 1.1 cgd */
735 1.1 cgd break;
736 1.1 cgd ok:
737 1.1 cgd /*
738 1.1 cgd * c is legal: store it and look at the next.
739 1.1 cgd */
740 1.1 cgd *p++ = c;
741 1.1 cgd if (--fp->_r > 0)
742 1.1 cgd fp->_p++;
743 1.1 cgd else if (__srefill(fp))
744 1.1 cgd break; /* EOF */
745 1.1 cgd }
746 1.1 cgd /*
747 1.1 cgd * If we had only a sign, it is no good; push
748 1.1 cgd * back the sign. If the number ends in `x',
749 1.1 cgd * it was [sign] '0' 'x', so push back the x
750 1.1 cgd * and treat it as [sign] '0'.
751 1.1 cgd */
752 1.1 cgd if (flags & NDIGITS) {
753 1.1 cgd if (p > buf)
754 1.37.4.1 pavel (void)ungetc(*(u_char *)--p, fp);
755 1.1 cgd goto match_failure;
756 1.1 cgd }
757 1.1 cgd c = ((u_char *)p)[-1];
758 1.1 cgd if (c == 'x' || c == 'X') {
759 1.1 cgd --p;
760 1.37.4.1 pavel (void)ungetc(c, fp);
761 1.1 cgd }
762 1.1 cgd if ((flags & SUPPRESS) == 0) {
763 1.28 kleink uintmax_t res;
764 1.1 cgd
765 1.1 cgd *p = 0;
766 1.37.4.1 pavel if ((flags & UNSIGNED) == 0)
767 1.37.4.1 pavel res = strtoimax(buf, (char **)NULL, base);
768 1.37.4.1 pavel else
769 1.37.4.1 pavel res = strtoumax(buf, (char **)NULL, base);
770 1.1 cgd if (flags & POINTER)
771 1.13 cgd *va_arg(ap, void **) =
772 1.37.4.1 pavel (void *)(uintptr_t)res;
773 1.37.4.1 pavel else if (flags & SHORTSHORT)
774 1.37.4.1 pavel *va_arg(ap, char *) = (char)res;
775 1.37.4.1 pavel else if (flags & SHORT)
776 1.37.4.1 pavel *va_arg(ap, short *) = (short)res;
777 1.37.4.1 pavel else if (flags & LONG)
778 1.37.4.1 pavel *va_arg(ap, long *) = (long)res;
779 1.37.4.1 pavel else if (flags & LONGLONG)
780 1.37.4.1 pavel *va_arg(ap, long long *) = res;
781 1.37.4.1 pavel else if (flags & INTMAXT)
782 1.28 kleink *va_arg(ap, intmax_t *) = res;
783 1.37.4.1 pavel else if (flags & PTRDIFFT)
784 1.28 kleink *va_arg(ap, ptrdiff_t *) =
785 1.28 kleink (ptrdiff_t)res;
786 1.37.4.1 pavel else if (flags & SIZET)
787 1.37.4.1 pavel *va_arg(ap, size_t *) = (size_t)res;
788 1.1 cgd else
789 1.22 christos *va_arg(ap, int *) = (int)res;
790 1.1 cgd nassigned++;
791 1.1 cgd }
792 1.1 cgd nread += p - buf;
793 1.37.4.1 pavel nconversions++;
794 1.1 cgd break;
795 1.1 cgd
796 1.37 christos #ifndef NO_FLOATING_POINT
797 1.1 cgd case CT_FLOAT:
798 1.1 cgd /* scan a floating point number as if by strtod */
799 1.1 cgd if (width == 0 || width > sizeof(buf) - 1)
800 1.1 cgd width = sizeof(buf) - 1;
801 1.37.4.1 pavel if ((width = parsefloat(fp, buf, buf + width)) == 0)
802 1.37.4.1 pavel goto match_failure;
803 1.1 cgd if ((flags & SUPPRESS) == 0) {
804 1.37.4.1 pavel if (flags & LONGDBL) {
805 1.37.4.1 pavel long double res = strtold(buf, &p);
806 1.11 jtc *va_arg(ap, long double *) = res;
807 1.37.4.1 pavel } else if (flags & LONG) {
808 1.37.4.1 pavel double res = strtod(buf, &p);
809 1.1 cgd *va_arg(ap, double *) = res;
810 1.37.4.1 pavel } else {
811 1.37.4.1 pavel float res = strtof(buf, &p);
812 1.1 cgd *va_arg(ap, float *) = res;
813 1.37.4.1 pavel }
814 1.37.4.1 pavel if (__scanfdebug && p - buf != width)
815 1.37.4.1 pavel abort();
816 1.1 cgd nassigned++;
817 1.1 cgd }
818 1.37.4.1 pavel nread += width;
819 1.37.4.1 pavel nconversions++;
820 1.1 cgd break;
821 1.37.4.1 pavel #endif /* !NO_FLOATING_POINT */
822 1.1 cgd }
823 1.1 cgd }
824 1.1 cgd input_failure:
825 1.37.4.1 pavel return (nconversions != 0 ? nassigned : EOF);
826 1.1 cgd match_failure:
827 1.1 cgd return (nassigned);
828 1.1 cgd }
829 1.1 cgd
830 1.1 cgd /*
831 1.1 cgd * Fill in the given table from the scanset at the given format
832 1.1 cgd * (just after `['). Return a pointer to the character past the
833 1.1 cgd * closing `]'. The table has a 1 wherever characters should be
834 1.1 cgd * considered part of the scanset.
835 1.1 cgd */
836 1.20 mycroft static const u_char *
837 1.1 cgd __sccl(tab, fmt)
838 1.18 perry char *tab;
839 1.20 mycroft const u_char *fmt;
840 1.1 cgd {
841 1.37.4.1 pavel int c, n, v, i;
842 1.23 lukem
843 1.23 lukem _DIAGASSERT(tab != NULL);
844 1.23 lukem _DIAGASSERT(fmt != NULL);
845 1.1 cgd /* first `clear' the whole table */
846 1.1 cgd c = *fmt++; /* first char hat => negated scanset */
847 1.1 cgd if (c == '^') {
848 1.1 cgd v = 1; /* default => accept */
849 1.1 cgd c = *fmt++; /* get new first char */
850 1.1 cgd } else
851 1.1 cgd v = 0; /* default => reject */
852 1.37.4.1 pavel
853 1.37.4.1 pavel /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
854 1.37.4.1 pavel (void)memset(tab, v, 256);
855 1.37.4.1 pavel
856 1.1 cgd if (c == 0)
857 1.1 cgd return (fmt - 1);/* format ended before closing ] */
858 1.1 cgd
859 1.1 cgd /*
860 1.1 cgd * Now set the entries corresponding to the actual scanset
861 1.1 cgd * to the opposite of the above.
862 1.1 cgd *
863 1.1 cgd * The first character may be ']' (or '-') without being special;
864 1.1 cgd * the last character may be '-'.
865 1.1 cgd */
866 1.1 cgd v = 1 - v;
867 1.1 cgd for (;;) {
868 1.1 cgd tab[c] = v; /* take character c */
869 1.1 cgd doswitch:
870 1.1 cgd n = *fmt++; /* and examine the next */
871 1.1 cgd switch (n) {
872 1.1 cgd
873 1.1 cgd case 0: /* format ended too soon */
874 1.1 cgd return (fmt - 1);
875 1.1 cgd
876 1.1 cgd case '-':
877 1.1 cgd /*
878 1.1 cgd * A scanset of the form
879 1.1 cgd * [01+-]
880 1.1 cgd * is defined as `the digit 0, the digit 1,
881 1.1 cgd * the character +, the character -', but
882 1.1 cgd * the effect of a scanset such as
883 1.1 cgd * [a-zA-Z0-9]
884 1.1 cgd * is implementation defined. The V7 Unix
885 1.1 cgd * scanf treats `a-z' as `the letters a through
886 1.1 cgd * z', but treats `a-a' as `the letter a, the
887 1.1 cgd * character -, and the letter a'.
888 1.1 cgd *
889 1.1 cgd * For compatibility, the `-' is not considerd
890 1.1 cgd * to define a range if the character following
891 1.1 cgd * it is either a close bracket (required by ANSI)
892 1.1 cgd * or is not numerically greater than the character
893 1.1 cgd * we just stored in the table (c).
894 1.1 cgd */
895 1.1 cgd n = *fmt;
896 1.37.4.1 pavel if (n == ']' || (__collate_load_error ? n < c :
897 1.37.4.1 pavel __collate_range_cmp(n, c) < 0)) {
898 1.1 cgd c = '-';
899 1.1 cgd break; /* resume the for(;;) */
900 1.1 cgd }
901 1.1 cgd fmt++;
902 1.37.4.1 pavel /* fill in the range */
903 1.37.4.1 pavel if (__collate_load_error) {
904 1.37.4.1 pavel do
905 1.37.4.1 pavel tab[++c] = v;
906 1.37.4.1 pavel while (c < n);
907 1.37.4.1 pavel } else {
908 1.37.4.1 pavel for (i = 0; i < 256; i ++)
909 1.37.4.1 pavel if (__collate_range_cmp(c, i) < 0 &&
910 1.37.4.1 pavel __collate_range_cmp(i, n) <= 0)
911 1.37.4.1 pavel tab[i] = v;
912 1.37.4.1 pavel }
913 1.1 cgd #if 1 /* XXX another disgusting compatibility hack */
914 1.37.4.1 pavel c = n;
915 1.1 cgd /*
916 1.1 cgd * Alas, the V7 Unix scanf also treats formats
917 1.1 cgd * such as [a-c-e] as `the letters a through e'.
918 1.1 cgd * This too is permitted by the standard....
919 1.1 cgd */
920 1.1 cgd goto doswitch;
921 1.1 cgd #else
922 1.1 cgd c = *fmt++;
923 1.1 cgd if (c == 0)
924 1.1 cgd return (fmt - 1);
925 1.1 cgd if (c == ']')
926 1.1 cgd return (fmt);
927 1.1 cgd #endif
928 1.1 cgd
929 1.1 cgd case ']': /* end of scanset */
930 1.1 cgd return (fmt);
931 1.1 cgd
932 1.1 cgd default: /* just another character */
933 1.1 cgd c = n;
934 1.1 cgd break;
935 1.1 cgd }
936 1.1 cgd }
937 1.1 cgd /* NOTREACHED */
938 1.1 cgd }
939 1.37.4.1 pavel
940 1.37.4.1 pavel #ifndef NO_FLOATING_POINT
941 1.37.4.1 pavel static int
942 1.37.4.1 pavel parsefloat(FILE *fp, char *buf, char *end)
943 1.37.4.1 pavel {
944 1.37.4.1 pavel char *commit, *p;
945 1.37.4.1 pavel int infnanpos = 0;
946 1.37.4.1 pavel enum {
947 1.37.4.1 pavel S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
948 1.37.4.1 pavel S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
949 1.37.4.1 pavel } state = S_START;
950 1.37.4.1 pavel unsigned char c;
951 1.37.4.1 pavel char decpt = *localeconv()->decimal_point;
952 1.37.4.1 pavel _Bool gotmantdig = 0, ishex = 0;
953 1.37.4.1 pavel
954 1.37.4.1 pavel /*
955 1.37.4.1 pavel * We set commit = p whenever the string we have read so far
956 1.37.4.1 pavel * constitutes a valid representation of a floating point
957 1.37.4.1 pavel * number by itself. At some point, the parse will complete
958 1.37.4.1 pavel * or fail, and we will ungetc() back to the last commit point.
959 1.37.4.1 pavel * To ensure that the file offset gets updated properly, it is
960 1.37.4.1 pavel * always necessary to read at least one character that doesn't
961 1.37.4.1 pavel * match; thus, we can't short-circuit "infinity" or "nan(...)".
962 1.37.4.1 pavel */
963 1.37.4.1 pavel commit = buf - 1;
964 1.37.4.1 pavel for (p = buf; p < end; ) {
965 1.37.4.1 pavel c = *fp->_p;
966 1.37.4.1 pavel reswitch:
967 1.37.4.1 pavel switch (state) {
968 1.37.4.1 pavel case S_START:
969 1.37.4.1 pavel state = S_GOTSIGN;
970 1.37.4.1 pavel if (c == '-' || c == '+')
971 1.37.4.1 pavel break;
972 1.37.4.1 pavel else
973 1.37.4.1 pavel goto reswitch;
974 1.37.4.1 pavel case S_GOTSIGN:
975 1.37.4.1 pavel switch (c) {
976 1.37.4.1 pavel case '0':
977 1.37.4.1 pavel state = S_MAYBEHEX;
978 1.37.4.1 pavel commit = p;
979 1.37.4.1 pavel break;
980 1.37.4.1 pavel case 'I':
981 1.37.4.1 pavel case 'i':
982 1.37.4.1 pavel state = S_INF;
983 1.37.4.1 pavel break;
984 1.37.4.1 pavel case 'N':
985 1.37.4.1 pavel case 'n':
986 1.37.4.1 pavel state = S_NAN;
987 1.37.4.1 pavel break;
988 1.37.4.1 pavel default:
989 1.37.4.1 pavel state = S_DIGITS;
990 1.37.4.1 pavel goto reswitch;
991 1.37.4.1 pavel }
992 1.37.4.1 pavel break;
993 1.37.4.1 pavel case S_INF:
994 1.37.4.1 pavel if (infnanpos > 6 ||
995 1.37.4.1 pavel (c != "nfinity"[infnanpos] &&
996 1.37.4.1 pavel c != "NFINITY"[infnanpos]))
997 1.37.4.1 pavel goto parsedone;
998 1.37.4.1 pavel if (infnanpos == 1 || infnanpos == 6)
999 1.37.4.1 pavel commit = p; /* inf or infinity */
1000 1.37.4.1 pavel infnanpos++;
1001 1.37.4.1 pavel break;
1002 1.37.4.1 pavel case S_NAN:
1003 1.37.4.1 pavel switch (infnanpos) {
1004 1.37.4.1 pavel case -1: /* XXX kludge to deal with nan(...) */
1005 1.37.4.1 pavel goto parsedone;
1006 1.37.4.1 pavel case 0:
1007 1.37.4.1 pavel if (c != 'A' && c != 'a')
1008 1.37.4.1 pavel goto parsedone;
1009 1.37.4.1 pavel break;
1010 1.37.4.1 pavel case 1:
1011 1.37.4.1 pavel if (c != 'N' && c != 'n')
1012 1.37.4.1 pavel goto parsedone;
1013 1.37.4.1 pavel else
1014 1.37.4.1 pavel commit = p;
1015 1.37.4.1 pavel break;
1016 1.37.4.1 pavel case 2:
1017 1.37.4.1 pavel if (c != '(')
1018 1.37.4.1 pavel goto parsedone;
1019 1.37.4.1 pavel break;
1020 1.37.4.1 pavel default:
1021 1.37.4.1 pavel if (c == ')') {
1022 1.37.4.1 pavel commit = p;
1023 1.37.4.1 pavel infnanpos = -2;
1024 1.37.4.1 pavel } else if (!isalnum(c) && c != '_')
1025 1.37.4.1 pavel goto parsedone;
1026 1.37.4.1 pavel break;
1027 1.37.4.1 pavel }
1028 1.37.4.1 pavel infnanpos++;
1029 1.37.4.1 pavel break;
1030 1.37.4.1 pavel case S_MAYBEHEX:
1031 1.37.4.1 pavel state = S_DIGITS;
1032 1.37.4.1 pavel if (c == 'X' || c == 'x') {
1033 1.37.4.1 pavel ishex = 1;
1034 1.37.4.1 pavel break;
1035 1.37.4.1 pavel } else { /* we saw a '0', but no 'x' */
1036 1.37.4.1 pavel gotmantdig = 1;
1037 1.37.4.1 pavel goto reswitch;
1038 1.37.4.1 pavel }
1039 1.37.4.1 pavel case S_DIGITS:
1040 1.37.4.1 pavel if ((ishex && isxdigit(c)) || isdigit(c))
1041 1.37.4.1 pavel gotmantdig = 1;
1042 1.37.4.1 pavel else {
1043 1.37.4.1 pavel state = S_FRAC;
1044 1.37.4.1 pavel if (c != decpt)
1045 1.37.4.1 pavel goto reswitch;
1046 1.37.4.1 pavel }
1047 1.37.4.1 pavel if (gotmantdig)
1048 1.37.4.1 pavel commit = p;
1049 1.37.4.1 pavel break;
1050 1.37.4.1 pavel case S_FRAC:
1051 1.37.4.1 pavel if (((c == 'E' || c == 'e') && !ishex) ||
1052 1.37.4.1 pavel ((c == 'P' || c == 'p') && ishex)) {
1053 1.37.4.1 pavel if (!gotmantdig)
1054 1.37.4.1 pavel goto parsedone;
1055 1.37.4.1 pavel else
1056 1.37.4.1 pavel state = S_EXP;
1057 1.37.4.1 pavel } else if ((ishex && isxdigit(c)) || isdigit(c)) {
1058 1.37.4.1 pavel commit = p;
1059 1.37.4.1 pavel gotmantdig = 1;
1060 1.37.4.1 pavel } else
1061 1.37.4.1 pavel goto parsedone;
1062 1.37.4.1 pavel break;
1063 1.37.4.1 pavel case S_EXP:
1064 1.37.4.1 pavel state = S_EXPDIGITS;
1065 1.37.4.1 pavel if (c == '-' || c == '+')
1066 1.37.4.1 pavel break;
1067 1.37.4.1 pavel else
1068 1.37.4.1 pavel goto reswitch;
1069 1.37.4.1 pavel case S_EXPDIGITS:
1070 1.37.4.1 pavel if (isdigit(c))
1071 1.37.4.1 pavel commit = p;
1072 1.37.4.1 pavel else
1073 1.37.4.1 pavel goto parsedone;
1074 1.37.4.1 pavel break;
1075 1.37.4.1 pavel default:
1076 1.37.4.1 pavel abort();
1077 1.37.4.1 pavel }
1078 1.37.4.1 pavel *p++ = c;
1079 1.37.4.1 pavel if (--fp->_r > 0)
1080 1.37.4.1 pavel fp->_p++;
1081 1.37.4.1 pavel else if (__srefill(fp))
1082 1.37.4.1 pavel break; /* EOF */
1083 1.37.4.1 pavel }
1084 1.37.4.1 pavel
1085 1.37.4.1 pavel parsedone:
1086 1.37.4.1 pavel while (commit < --p)
1087 1.37.4.1 pavel (void)ungetc(*(u_char *)p, fp);
1088 1.37.4.1 pavel *++commit = '\0';
1089 1.37.4.1 pavel return (commit - buf);
1090 1.37.4.1 pavel }
1091 1.37.4.1 pavel #endif
1092