]> git.saurik.com Git - apple/libc.git/blame - stdio.subproj/vfscanf.c
Libc-166.tar.gz
[apple/libc.git] / stdio.subproj / vfscanf.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * This code is derived from software contributed to Berkeley by
27 * Chris Torek.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <ctype.h>
62#if __STDC__
63#include <stdarg.h>
64#else
65#include <varargs.h>
66#endif
67#include "local.h"
68
69#define FLOATING_POINT
70
71#include "floatio.h"
72#define BUF 513 /* Maximum length of numeric string. */
73
74/*
75 * Flags used during conversion.
76 */
77#define LONG 0x01 /* l: long or double */
78#define LONGDBL 0x02 /* L: long double; unimplemented */
79#define SHORT 0x04 /* h: short */
80#define SUPPRESS 0x08 /* suppress assignment */
81#define POINTER 0x10 /* weird %p pointer (`fake hex') */
82#define NOSKIP 0x20 /* do not skip blanks */
83
84/*
85 * The following are used in numeric conversions only:
86 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
87 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
88 */
89#define SIGNOK 0x40 /* +/- is (still) legal */
90#define NDIGITS 0x80 /* no digits detected */
91
92#define DPTOK 0x100 /* (float) decimal point is still legal */
93#define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
94
95#define PFXOK 0x100 /* 0x prefix is (still) legal */
96#define NZDIGITS 0x200 /* no zero digits detected */
97
98/*
99 * Conversion types.
100 */
101#define CT_CHAR 0 /* %c conversion */
102#define CT_CCL 1 /* %[...] conversion */
103#define CT_STRING 2 /* %s conversion */
104#define CT_INT 3 /* integer, i.e., strtol or strtoul */
105#define CT_FLOAT 4 /* floating, i.e., strtod */
106
107#define u_char unsigned char
108#define u_long unsigned long
109
110static u_char *__sccl();
111
112/*
113 * vfscanf
114 */
115int
116__svfscanf(fp, fmt0, ap)
117 register FILE *fp;
118 char const *fmt0;
119 va_list ap;
120{
121 register u_char *fmt = (u_char *)fmt0;
122 register int c; /* character from format, or conversion */
123 register size_t width; /* field width, or 0 */
124 register char *p; /* points into all kinds of strings */
125 register int n; /* handy integer */
126 register int flags; /* flags as defined above */
127 register char *p0; /* saves original value of p when necessary */
128 int nassigned; /* number of fields assigned */
129 int nread; /* number of characters consumed from fp */
130 int base; /* base argument to strtol/strtoul */
131 u_long (*ccfn)(); /* conversion function (strtol/strtoul) */
132 char ccltab[256]; /* character class table for %[...] */
133 char buf[BUF]; /* buffer for numeric conversions */
134
135 /* `basefix' is used to avoid `if' tests in the integer scanner */
136 static short basefix[17] =
137 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
138
139 nassigned = 0;
140 nread = 0;
141 base = 0; /* XXX just to keep gcc happy */
142 ccfn = NULL; /* XXX just to keep gcc happy */
143 for (;;) {
144 c = *fmt++;
145 if (c == 0)
146 return (nassigned);
147 if (isspace(c)) {
148 for (;;) {
149 if (fp->_r <= 0 && __srefill(fp))
150 return (nassigned);
151 if (!isspace(*fp->_p))
152 break;
153 nread++, fp->_r--, fp->_p++;
154 }
155 continue;
156 }
157 if (c != '%')
158 goto literal;
159 width = 0;
160 flags = 0;
161 /*
162 * switch on the format. continue if done;
163 * break once format type is derived.
164 */
165again: c = *fmt++;
166 switch (c) {
167 case '%':
168literal:
169 if (fp->_r <= 0 && __srefill(fp))
170 goto input_failure;
171 if (*fp->_p != c)
172 goto match_failure;
173 fp->_r--, fp->_p++;
174 nread++;
175 continue;
176
177 case '*':
178 flags |= SUPPRESS;
179 goto again;
180 case 'l':
181 flags |= LONG;
182 goto again;
183 case 'L':
184 flags |= LONGDBL;
185 goto again;
186 case 'h':
187 flags |= SHORT;
188 goto again;
189
190 case '0': case '1': case '2': case '3': case '4':
191 case '5': case '6': case '7': case '8': case '9':
192 width = width * 10 + c - '0';
193 goto again;
194
195 /*
196 * Conversions.
197 * Those marked `compat' are for 4.[123]BSD compatibility.
198 *
199 * (According to ANSI, E and X formats are supposed
200 * to the same as e and x. Sorry about that.)
201 */
202 case 'D': /* compat */
203 flags |= LONG;
204 /* FALLTHROUGH */
205 case 'd':
206 c = CT_INT;
207 ccfn = (u_long (*)())strtol;
208 base = 10;
209 break;
210
211 case 'i':
212 c = CT_INT;
213 ccfn = (u_long (*)())strtol;
214 base = 0;
215 break;
216
217 case 'O': /* compat */
218 flags |= LONG;
219 /* FALLTHROUGH */
220 case 'o':
221 c = CT_INT;
222 ccfn = strtoul;
223 base = 8;
224 break;
225
226 case 'u':
227 c = CT_INT;
228 ccfn = strtoul;
229 base = 10;
230 break;
231
232 case 'X': /* compat XXX */
233 flags |= LONG;
234 /* FALLTHROUGH */
235 case 'x':
236 flags |= PFXOK; /* enable 0x prefixing */
237 c = CT_INT;
238 ccfn = strtoul;
239 base = 16;
240 break;
241
242#ifdef FLOATING_POINT
243 case 'E': /* compat XXX */
244 case 'F': /* compat */
245 flags |= LONG;
246 /* FALLTHROUGH */
247 case 'e': case 'f': case 'g':
248 c = CT_FLOAT;
249 break;
250#endif
251
252 case 's':
253 c = CT_STRING;
254 break;
255
256 case '[':
257 fmt = __sccl(ccltab, fmt);
258 flags |= NOSKIP;
259 c = CT_CCL;
260 break;
261
262 case 'c':
263 flags |= NOSKIP;
264 c = CT_CHAR;
265 break;
266
267 case 'p': /* pointer format is like hex */
268 flags |= POINTER | PFXOK;
269 c = CT_INT;
270 ccfn = strtoul;
271 base = 16;
272 break;
273
274 case 'n':
275 if (flags & SUPPRESS) /* ??? */
276 continue;
277 if (flags & SHORT)
278 *va_arg(ap, short *) = nread;
279 else if (flags & LONG)
280 *va_arg(ap, long *) = nread;
281 else
282 *va_arg(ap, int *) = nread;
283 continue;
284
285 /*
286 * Disgusting backwards compatibility hacks. XXX
287 */
288 case '\0': /* compat */
289 return (EOF);
290
291 default: /* compat */
292 if (isupper(c))
293 flags |= LONG;
294 c = CT_INT;
295 ccfn = (u_long (*)())strtol;
296 base = 10;
297 break;
298 }
299
300 /*
301 * We have a conversion that requires input.
302 */
303 if (fp->_r <= 0 && __srefill(fp))
304 goto input_failure;
305
306 /*
307 * Consume leading white space, except for formats
308 * that suppress this.
309 */
310 if ((flags & NOSKIP) == 0) {
311 while (isspace(*fp->_p)) {
312 nread++;
313 if (--fp->_r > 0)
314 fp->_p++;
315 else if (__srefill(fp))
316 goto input_failure;
317 }
318 /*
319 * Note that there is at least one character in
320 * the buffer, so conversions that do not set NOSKIP
321 * ca no longer result in an input failure.
322 */
323 }
324
325 /*
326 * Do the conversion.
327 */
328 switch (c) {
329
330 case CT_CHAR:
331 /* scan arbitrary characters (sets NOSKIP) */
332 if (width == 0)
333 width = 1;
334 if (flags & SUPPRESS) {
335 size_t sum = 0;
336 for (;;) {
337 if ((n = fp->_r) < width) {
338 sum += n;
339 width -= n;
340 fp->_p += n;
341 if (__srefill(fp)) {
342 if (sum == 0)
343 goto input_failure;
344 break;
345 }
346 } else {
347 sum += width;
348 fp->_r -= width;
349 fp->_p += width;
350 break;
351 }
352 }
353 nread += sum;
354 } else {
355 size_t r = fread((void *)va_arg(ap, char *), 1,
356 width, fp);
357
358 if (r == 0)
359 goto input_failure;
360 nread += r;
361 nassigned++;
362 }
363 break;
364
365 case CT_CCL:
366 /* scan a (nonempty) character class (sets NOSKIP) */
367 if (width == 0)
368 width = ~0; /* `infinity' */
369 /* take only those things in the class */
370 if (flags & SUPPRESS) {
371 n = 0;
372 while (ccltab[*fp->_p]) {
373 n++, fp->_r--, fp->_p++;
374 if (--width == 0)
375 break;
376 if (fp->_r <= 0 && __srefill(fp)) {
377 if (n == 0)
378 goto input_failure;
379 break;
380 }
381 }
382 if (n == 0)
383 goto match_failure;
384 } else {
385 p0 = p = va_arg(ap, char *);
386 while (ccltab[*fp->_p]) {
387 fp->_r--;
388 *p++ = *fp->_p++;
389 if (--width == 0)
390 break;
391 if (fp->_r <= 0 && __srefill(fp)) {
392 if (p == p0)
393 goto input_failure;
394 break;
395 }
396 }
397 n = p - p0;
398 if (n == 0)
399 goto match_failure;
400 *p = 0;
401 nassigned++;
402 }
403 nread += n;
404 break;
405
406 case CT_STRING:
407 /* like CCL, but zero-length string OK, & no NOSKIP */
408 if (width == 0)
409 width = ~0;
410 if (flags & SUPPRESS) {
411 n = 0;
412 while (!isspace(*fp->_p)) {
413 n++, fp->_r--, fp->_p++;
414 if (--width == 0)
415 break;
416 if (fp->_r <= 0 && __srefill(fp))
417 break;
418 }
419 nread += n;
420 } else {
421 p0 = p = va_arg(ap, char *);
422 while (!isspace(*fp->_p)) {
423 fp->_r--;
424 *p++ = *fp->_p++;
425 if (--width == 0)
426 break;
427 if (fp->_r <= 0 && __srefill(fp))
428 break;
429 }
430 *p = 0;
431 nread += p - p0;
432 nassigned++;
433 }
434 continue;
435
436 case CT_INT:
437 /* scan an integer as if by strtol/strtoul */
438#ifdef hardway
439 if (width == 0 || width > sizeof(buf) - 1)
440 width = sizeof(buf) - 1;
441#else
442 /* size_t is unsigned, hence this optimisation */
443 if (--width > sizeof(buf) - 2)
444 width = sizeof(buf) - 2;
445 width++;
446#endif
447 flags |= SIGNOK | NDIGITS | NZDIGITS;
448 for (p = buf; width; width--) {
449 c = *fp->_p;
450 /*
451 * Switch on the character; `goto ok'
452 * if we accept it as a part of number.
453 */
454 switch (c) {
455
456 /*
457 * The digit 0 is always legal, but is
458 * special. For %i conversions, if no
459 * digits (zero or nonzero) have been
460 * scanned (only signs), we will have
461 * base==0. In that case, we should set
462 * it to 8 and enable 0x prefixing.
463 * Also, if we have not scanned zero digits
464 * before this, do not turn off prefixing
465 * (someone else will turn it off if we
466 * have scanned any nonzero digits).
467 */
468 case '0':
469 if (base == 0) {
470 base = 8;
471 flags |= PFXOK;
472 }
473 if (flags & NZDIGITS)
474 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
475 else
476 flags &= ~(SIGNOK|PFXOK|NDIGITS);
477 goto ok;
478
479 /* 1 through 7 always legal */
480 case '1': case '2': case '3':
481 case '4': case '5': case '6': case '7':
482 base = basefix[base];
483 flags &= ~(SIGNOK | PFXOK | NDIGITS);
484 goto ok;
485
486 /* digits 8 and 9 ok iff decimal or hex */
487 case '8': case '9':
488 base = basefix[base];
489 if (base <= 8)
490 break; /* not legal here */
491 flags &= ~(SIGNOK | PFXOK | NDIGITS);
492 goto ok;
493
494 /* letters ok iff hex */
495 case 'A': case 'B': case 'C':
496 case 'D': case 'E': case 'F':
497 case 'a': case 'b': case 'c':
498 case 'd': case 'e': case 'f':
499 /* no need to fix base here */
500 if (base <= 10)
501 break; /* not legal here */
502 flags &= ~(SIGNOK | PFXOK | NDIGITS);
503 goto ok;
504
505 /* sign ok only as first character */
506 case '+': case '-':
507 if (flags & SIGNOK) {
508 flags &= ~SIGNOK;
509 goto ok;
510 }
511 break;
512
513 /* x ok iff flag still set & 2nd char */
514 case 'x': case 'X':
515 if (flags & PFXOK && p == buf + 1) {
516 base = 16; /* if %i */
517 flags &= ~PFXOK;
518 goto ok;
519 }
520 break;
521 }
522
523 /*
524 * If we got here, c is not a legal character
525 * for a number. Stop accumulating digits.
526 */
527 break;
528 ok:
529 /*
530 * c is legal: store it and look at the next.
531 */
532 *p++ = c;
533 if (--fp->_r > 0)
534 fp->_p++;
535 else if (__srefill(fp))
536 break; /* EOF */
537 }
538 /*
539 * If we had only a sign, it is no good; push
540 * back the sign. If the number ends in `x',
541 * it was [sign] '0' 'x', so push back the x
542 * and treat it as [sign] '0'.
543 */
544 if (flags & NDIGITS) {
545 if (p > buf)
546 (void) ungetc(*(u_char *)--p, fp);
547 goto match_failure;
548 }
549 c = ((u_char *)p)[-1];
550 if (c == 'x' || c == 'X') {
551 --p;
552 (void) ungetc(c, fp);
553 }
554 if ((flags & SUPPRESS) == 0) {
555 u_long res;
556
557 *p = 0;
558 res = (*ccfn)(buf, (char **)NULL, base);
559 if (flags & POINTER)
560 *va_arg(ap, void **) = (void *)res;
561 else if (flags & SHORT)
562 *va_arg(ap, short *) = res;
563 else if (flags & LONG)
564 *va_arg(ap, long *) = res;
565 else
566 *va_arg(ap, int *) = res;
567 nassigned++;
568 }
569 nread += p - buf;
570 break;
571
572#ifdef FLOATING_POINT
573 case CT_FLOAT:
574 /* scan a floating point number as if by strtod */
575#ifdef hardway
576 if (width == 0 || width > sizeof(buf) - 1)
577 width = sizeof(buf) - 1;
578#else
579 /* size_t is unsigned, hence this optimisation */
580 if (--width > sizeof(buf) - 2)
581 width = sizeof(buf) - 2;
582 width++;
583#endif
584 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
585 for (p = buf; width; width--) {
586 c = *fp->_p;
587 /*
588 * This code mimicks the integer conversion
589 * code, but is much simpler.
590 */
591 switch (c) {
592
593 case '0': case '1': case '2': case '3':
594 case '4': case '5': case '6': case '7':
595 case '8': case '9':
596 flags &= ~(SIGNOK | NDIGITS);
597 goto fok;
598
599 case '+': case '-':
600 if (flags & SIGNOK) {
601 flags &= ~SIGNOK;
602 goto fok;
603 }
604 break;
605 case '.':
606 if (flags & DPTOK) {
607 flags &= ~(SIGNOK | DPTOK);
608 goto fok;
609 }
610 break;
611 case 'e': case 'E':
612 /* no exponent without some digits */
613 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
614 flags =
615 (flags & ~(EXPOK|DPTOK)) |
616 SIGNOK | NDIGITS;
617 goto fok;
618 }
619 break;
620 }
621 break;
622 fok:
623 *p++ = c;
624 if (--fp->_r > 0)
625 fp->_p++;
626 else if (__srefill(fp))
627 break; /* EOF */
628 }
629 /*
630 * If no digits, might be missing exponent digits
631 * (just give back the exponent) or might be missing
632 * regular digits, but had sign and/or decimal point.
633 */
634 if (flags & NDIGITS) {
635 if (flags & EXPOK) {
636 /* no digits at all */
637 while (p > buf)
638 ungetc(*(u_char *)--p, fp);
639 goto match_failure;
640 }
641 /* just a bad exponent (e and maybe sign) */
642 c = *(u_char *)--p;
643 if (c != 'e' && c != 'E') {
644 (void) ungetc(c, fp);/* sign */
645 c = *(u_char *)--p;
646 }
647 (void) ungetc(c, fp);
648 }
649 if ((flags & SUPPRESS) == 0) {
650 double res;
651
652 *p = 0;
653 res = strtod(buf,(char **) NULL);
654 if (flags & LONG)
655 *va_arg(ap, double *) = res;
656 else
657 *va_arg(ap, float *) = res;
658 nassigned++;
659 }
660 nread += p - buf;
661 break;
662#endif /* FLOATING_POINT */
663 }
664 }
665input_failure:
666 return (nassigned ? nassigned : -1);
667match_failure:
668 return (nassigned);
669}
670
671/*
672 * Fill in the given table from the scanset at the given format
673 * (just after `['). Return a pointer to the character past the
674 * closing `]'. The table has a 1 wherever characters should be
675 * considered part of the scanset.
676 */
677static u_char *
678__sccl(tab, fmt)
679 register char *tab;
680 register u_char *fmt;
681{
682 register int c, n, v;
683
684 /* first `clear' the whole table */
685 c = *fmt++; /* first char hat => negated scanset */
686 if (c == '^') {
687 v = 1; /* default => accept */
688 c = *fmt++; /* get new first char */
689 } else
690 v = 0; /* default => reject */
691 /* should probably use memset here */
692 for (n = 0; n < 256; n++)
693 tab[n] = v;
694 if (c == 0)
695 return (fmt - 1);/* format ended before closing ] */
696
697 /*
698 * Now set the entries corresponding to the actual scanset
699 * to the opposite of the above.
700 *
701 * The first character may be ']' (or '-') without being special;
702 * the last character may be '-'.
703 */
704 v = 1 - v;
705 for (;;) {
706 tab[c] = v; /* take character c */
707doswitch:
708 n = *fmt++; /* and examine the next */
709 switch (n) {
710
711 case 0: /* format ended too soon */
712 return (fmt - 1);
713
714 case '-':
715 /*
716 * A scanset of the form
717 * [01+-]
718 * is defined as `the digit 0, the digit 1,
719 * the character +, the character -', but
720 * the effect of a scanset such as
721 * [a-zA-Z0-9]
722 * is implementation defined. The V7 Unix
723 * scanf treats `a-z' as `the letters a through
724 * z', but treats `a-a' as `the letter a, the
725 * character -, and the letter a'.
726 *
727 * For compatibility, the `-' is not considerd
728 * to define a range if the character following
729 * it is either a close bracket (required by ANSI)
730 * or is not numerically greater than the character
731 * we just stored in the table (c).
732 */
733 n = *fmt;
734 if (n == ']' || n < c) {
735 c = '-';
736 break; /* resume the for(;;) */
737 }
738 fmt++;
739 do { /* fill in the range */
740 tab[++c] = v;
741 } while (c < n);
742#if 1 /* XXX another disgusting compatibility hack */
743 /*
744 * Alas, the V7 Unix scanf also treats formats
745 * such as [a-c-e] as `the letters a through e'.
746 * This too is permitted by the standard....
747 */
748 goto doswitch;
749#else
750 c = *fmt++;
751 if (c == 0)
752 return (fmt - 1);
753 if (c == ']')
754 return (fmt);
755#endif
756 break;
757
758 case ']': /* end of scanset */
759 return (fmt);
760
761 default: /* just another character */
762 c = n;
763 break;
764 }
765 }
766 /* NOTREACHED */
767}