]> git.saurik.com Git - apple/libc.git/blob - stdio/vfscanf.c
35bb094d4351ccc67edd8409cc024c58729864e1
[apple/libc.git] / stdio / vfscanf.c
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 #define QUAD 0x400
84
85 /*
86 * The following are used in numeric conversions only:
87 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
88 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
89 */
90 #define SIGNOK 0x40 /* +/- is (still) legal */
91 #define NDIGITS 0x80 /* no digits detected */
92
93 #define DPTOK 0x100 /* (float) decimal point is still legal */
94 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
95
96 #define PFXOK 0x100 /* 0x prefix is (still) legal */
97 #define NZDIGITS 0x200 /* no zero digits detected */
98
99 /*
100 * Conversion types.
101 */
102 #define CT_CHAR 0 /* %c conversion */
103 #define CT_CCL 1 /* %[...] conversion */
104 #define CT_STRING 2 /* %s conversion */
105 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
106 #define CT_FLOAT 4 /* floating, i.e., strtod */
107
108 #define u_char unsigned char
109 #define u_long unsigned long
110
111 static u_char *__sccl(char *, u_char *);
112
113 /*
114 * vfscanf
115 */
116 int
117 __svfscanf(fp, fmt0, ap)
118 register FILE *fp;
119 char const *fmt0;
120 va_list ap;
121 {
122 register u_char *fmt = (u_char *)fmt0;
123 register int c; /* character from format, or conversion */
124 register size_t width; /* field width, or 0 */
125 register char *p; /* points into all kinds of strings */
126 register int n; /* handy integer */
127 register int flags; /* flags as defined above */
128 register char *p0; /* saves original value of p when necessary */
129 int nassigned; /* number of fields assigned */
130 int nread; /* number of characters consumed from fp */
131 int base; /* base argument to strtoq/strtouq */
132 u_quad_t (*ccfn)(); /* conversion function (strtoq/strtouq) */
133 char ccltab[256]; /* character class table for %[...] */
134 char buf[BUF]; /* buffer for numeric conversions */
135
136 /* `basefix' is used to avoid `if' tests in the integer scanner */
137 static short basefix[17] =
138 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
139
140 nassigned = 0;
141 nread = 0;
142 base = 0; /* XXX just to keep gcc happy */
143 ccfn = NULL; /* XXX just to keep gcc happy */
144 for (;;) {
145 c = *fmt++;
146 if (c == 0)
147 return (nassigned);
148 if (isspace(c)) {
149 for (;;) {
150 if (fp->_r <= 0 && __srefill(fp))
151 return (nassigned);
152 if (!isspace(*fp->_p))
153 break;
154 nread++, fp->_r--, fp->_p++;
155 }
156 continue;
157 }
158 if (c != '%')
159 goto literal;
160 width = 0;
161 flags = 0;
162 /*
163 * switch on the format. continue if done;
164 * break once format type is derived.
165 */
166 again: c = *fmt++;
167 switch (c) {
168 case '%':
169 literal:
170 if (fp->_r <= 0 && __srefill(fp))
171 goto input_failure;
172 if (*fp->_p != c)
173 goto match_failure;
174 fp->_r--, fp->_p++;
175 nread++;
176 continue;
177
178 case '*':
179 flags |= SUPPRESS;
180 goto again;
181 case 'l':
182 if( flags & LONG )
183 flags |= QUAD;
184 else
185 flags |= LONG;
186 goto again;
187 case 'q':
188 flags |= QUAD;
189 goto again;
190 case 'z':
191 if( sizeof(size_t) == sizeof(long) )
192 flags |= LONG;
193 if( sizeof(size_t) == sizeof(quad_t) )
194 flags |= QUAD;
195 goto again;
196 case 'L':
197 flags |= LONGDBL;
198 goto again;
199 case 'h':
200 flags |= SHORT;
201 goto again;
202
203 case '0': case '1': case '2': case '3': case '4':
204 case '5': case '6': case '7': case '8': case '9':
205 width = width * 10 + c - '0';
206 goto again;
207
208 /*
209 * Conversions.
210 * Those marked `compat' are for 4.[123]BSD compatibility.
211 *
212 * (According to ANSI, E and X formats are supposed
213 * to the same as e and x. Sorry about that.)
214 */
215 case 'D': /* compat */
216 flags |= LONG;
217 /* FALLTHROUGH */
218 case 'd':
219 c = CT_INT;
220 ccfn = (u_quad_t (*)())strtoq;
221 base = 10;
222 break;
223
224 case 'i':
225 c = CT_INT;
226 ccfn = (u_quad_t (*)())strtoq;
227 base = 0;
228 break;
229
230 case 'O': /* compat */
231 flags |= LONG;
232 /* FALLTHROUGH */
233 case 'o':
234 c = CT_INT;
235 ccfn = strtouq;
236 base = 8;
237 break;
238
239 case 'u':
240 c = CT_INT;
241 ccfn = strtouq;
242 base = 10;
243 break;
244
245 case 'X': /* compat XXX */
246 flags |= LONG;
247 /* FALLTHROUGH */
248 case 'x':
249 flags |= PFXOK; /* enable 0x prefixing */
250 c = CT_INT;
251 ccfn = strtouq;
252 base = 16;
253 break;
254
255 #ifdef FLOATING_POINT
256 case 'E': /* compat XXX */
257 case 'F': /* compat */
258 flags |= LONG;
259 /* FALLTHROUGH */
260 case 'e': case 'f': case 'g':
261 c = CT_FLOAT;
262 break;
263 #endif
264
265 case 's':
266 c = CT_STRING;
267 break;
268
269 case '[':
270 fmt = __sccl(ccltab, fmt);
271 flags |= NOSKIP;
272 c = CT_CCL;
273 break;
274
275 case 'c':
276 flags |= NOSKIP;
277 c = CT_CHAR;
278 break;
279
280 case 'p': /* pointer format is like hex */
281 flags |= POINTER | PFXOK;
282 c = CT_INT;
283 ccfn = strtouq;
284 base = 16;
285 break;
286
287 case 'n':
288 if (flags & SUPPRESS) /* ??? */
289 continue;
290 if (flags & SHORT)
291 *va_arg(ap, short *) = nread;
292 else if (flags & LONG)
293 *va_arg(ap, long *) = nread;
294 else if (flags & QUAD)
295 *va_arg(ap, quad_t *) = nread;
296 else
297 *va_arg(ap, int *) = nread;
298 continue;
299
300 /*
301 * Disgusting backwards compatibility hacks. XXX
302 */
303 case '\0': /* compat */
304 return (EOF);
305
306 default: /* compat */
307 if (isupper(c))
308 flags |= LONG;
309 c = CT_INT;
310 ccfn = (u_quad_t (*)())strtoq;
311 base = 10;
312 break;
313 }
314
315 /*
316 * We have a conversion that requires input.
317 */
318 if (fp->_r <= 0 && __srefill(fp))
319 goto input_failure;
320
321 /*
322 * Consume leading white space, except for formats
323 * that suppress this.
324 */
325 if ((flags & NOSKIP) == 0) {
326 while (isspace(*fp->_p)) {
327 nread++;
328 if (--fp->_r > 0)
329 fp->_p++;
330 else if (__srefill(fp))
331 goto input_failure;
332 }
333 /*
334 * Note that there is at least one character in
335 * the buffer, so conversions that do not set NOSKIP
336 * ca no longer result in an input failure.
337 */
338 }
339
340 /*
341 * Do the conversion.
342 */
343 switch (c) {
344
345 case CT_CHAR:
346 /* scan arbitrary characters (sets NOSKIP) */
347 if (width == 0)
348 width = 1;
349 if (flags & SUPPRESS) {
350 size_t sum = 0;
351 for (;;) {
352 if ((n = fp->_r) < width) {
353 sum += n;
354 width -= n;
355 fp->_p += n;
356 if (__srefill(fp)) {
357 if (sum == 0)
358 goto input_failure;
359 break;
360 }
361 } else {
362 sum += width;
363 fp->_r -= width;
364 fp->_p += width;
365 break;
366 }
367 }
368 nread += sum;
369 } else {
370 size_t r = fread((void *)va_arg(ap, char *), 1,
371 width, fp);
372
373 if (r == 0)
374 goto input_failure;
375 nread += r;
376 nassigned++;
377 }
378 break;
379
380 case CT_CCL:
381 /* scan a (nonempty) character class (sets NOSKIP) */
382 if (width == 0)
383 width = ~0; /* `infinity' */
384 /* take only those things in the class */
385 if (flags & SUPPRESS) {
386 n = 0;
387 while (ccltab[*fp->_p]) {
388 n++, fp->_r--, fp->_p++;
389 if (--width == 0)
390 break;
391 if (fp->_r <= 0 && __srefill(fp)) {
392 if (n == 0)
393 goto input_failure;
394 break;
395 }
396 }
397 if (n == 0)
398 goto match_failure;
399 } else {
400 p0 = p = va_arg(ap, char *);
401 while (ccltab[*fp->_p]) {
402 fp->_r--;
403 *p++ = *fp->_p++;
404 if (--width == 0)
405 break;
406 if (fp->_r <= 0 && __srefill(fp)) {
407 if (p == p0)
408 goto input_failure;
409 break;
410 }
411 }
412 n = p - p0;
413 if (n == 0)
414 goto match_failure;
415 *p = 0;
416 nassigned++;
417 }
418 nread += n;
419 break;
420
421 case CT_STRING:
422 /* like CCL, but zero-length string OK, & no NOSKIP */
423 if (width == 0)
424 width = ~0;
425 if (flags & SUPPRESS) {
426 n = 0;
427 while (!isspace(*fp->_p)) {
428 n++, fp->_r--, fp->_p++;
429 if (--width == 0)
430 break;
431 if (fp->_r <= 0 && __srefill(fp))
432 break;
433 }
434 nread += n;
435 } else {
436 p0 = p = va_arg(ap, char *);
437 while (!isspace(*fp->_p)) {
438 fp->_r--;
439 *p++ = *fp->_p++;
440 if (--width == 0)
441 break;
442 if (fp->_r <= 0 && __srefill(fp))
443 break;
444 }
445 *p = 0;
446 nread += p - p0;
447 nassigned++;
448 }
449 continue;
450
451 case CT_INT:
452 /* scan an integer as if by strtoq/strtouq */
453 #ifdef hardway
454 if (width == 0 || width > sizeof(buf) - 1)
455 width = sizeof(buf) - 1;
456 #else
457 /* size_t is unsigned, hence this optimisation */
458 if (--width > sizeof(buf) - 2)
459 width = sizeof(buf) - 2;
460 width++;
461 #endif
462 flags |= SIGNOK | NDIGITS | NZDIGITS;
463 for (p = buf; width; width--) {
464 c = *fp->_p;
465 /*
466 * Switch on the character; `goto ok'
467 * if we accept it as a part of number.
468 */
469 switch (c) {
470
471 /*
472 * The digit 0 is always legal, but is
473 * special. For %i conversions, if no
474 * digits (zero or nonzero) have been
475 * scanned (only signs), we will have
476 * base==0. In that case, we should set
477 * it to 8 and enable 0x prefixing.
478 * Also, if we have not scanned zero digits
479 * before this, do not turn off prefixing
480 * (someone else will turn it off if we
481 * have scanned any nonzero digits).
482 */
483 case '0':
484 if (base == 0) {
485 base = 8;
486 flags |= PFXOK;
487 }
488 if (flags & NZDIGITS)
489 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
490 else
491 flags &= ~(SIGNOK|PFXOK|NDIGITS);
492 goto ok;
493
494 /* 1 through 7 always legal */
495 case '1': case '2': case '3':
496 case '4': case '5': case '6': case '7':
497 base = basefix[base];
498 flags &= ~(SIGNOK | PFXOK | NDIGITS);
499 goto ok;
500
501 /* digits 8 and 9 ok iff decimal or hex */
502 case '8': case '9':
503 base = basefix[base];
504 if (base <= 8)
505 break; /* not legal here */
506 flags &= ~(SIGNOK | PFXOK | NDIGITS);
507 goto ok;
508
509 /* letters ok iff hex */
510 case 'A': case 'B': case 'C':
511 case 'D': case 'E': case 'F':
512 case 'a': case 'b': case 'c':
513 case 'd': case 'e': case 'f':
514 /* no need to fix base here */
515 if (base <= 10)
516 break; /* not legal here */
517 flags &= ~(SIGNOK | PFXOK | NDIGITS);
518 goto ok;
519
520 /* sign ok only as first character */
521 case '+': case '-':
522 if (flags & SIGNOK) {
523 flags &= ~SIGNOK;
524 goto ok;
525 }
526 break;
527
528 /* x ok iff flag still set & 2nd char */
529 case 'x': case 'X':
530 if (flags & PFXOK && p == buf + 1) {
531 base = 16; /* if %i */
532 flags &= ~PFXOK;
533 goto ok;
534 }
535 break;
536 }
537
538 /*
539 * If we got here, c is not a legal character
540 * for a number. Stop accumulating digits.
541 */
542 break;
543 ok:
544 /*
545 * c is legal: store it and look at the next.
546 */
547 *p++ = c;
548 if (--fp->_r > 0)
549 fp->_p++;
550 else if (__srefill(fp))
551 break; /* EOF */
552 }
553 /*
554 * If we had only a sign, it is no good; push
555 * back the sign. If the number ends in `x',
556 * it was [sign] '0' 'x', so push back the x
557 * and treat it as [sign] '0'.
558 */
559 if (flags & NDIGITS) {
560 if (p > buf)
561 (void) ungetc(*(u_char *)--p, fp);
562 goto match_failure;
563 }
564 c = ((u_char *)p)[-1];
565 if (c == 'x' || c == 'X') {
566 --p;
567 (void) ungetc(c, fp);
568 }
569 if ((flags & SUPPRESS) == 0) {
570 u_quad_t res;
571
572 *p = 0;
573 res = (*ccfn)(buf, (char **)NULL, base);
574 if (flags & POINTER)
575 *va_arg(ap, void **) = (void *)res;
576 else if (flags & SHORT)
577 *va_arg(ap, short *) = res;
578 else if (flags & QUAD)
579 *va_arg(ap, quad_t *) = res;
580 else if (flags & LONG)
581 *va_arg(ap, long *) = res;
582 else
583 *va_arg(ap, int *) = res;
584 nassigned++;
585 }
586 nread += p - buf;
587 break;
588
589 #ifdef FLOATING_POINT
590 case CT_FLOAT:
591 /* scan a floating point number as if by strtod */
592 #ifdef hardway
593 if (width == 0 || width > sizeof(buf) - 1)
594 width = sizeof(buf) - 1;
595 #else
596 /* size_t is unsigned, hence this optimisation */
597 if (--width > sizeof(buf) - 2)
598 width = sizeof(buf) - 2;
599 width++;
600 #endif
601 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
602 for (p = buf; width; width--) {
603 c = *fp->_p;
604 /*
605 * This code mimicks the integer conversion
606 * code, but is much simpler.
607 */
608 switch (c) {
609
610 case '0': case '1': case '2': case '3':
611 case '4': case '5': case '6': case '7':
612 case '8': case '9':
613 flags &= ~(SIGNOK | NDIGITS);
614 goto fok;
615
616 case '+': case '-':
617 if (flags & SIGNOK) {
618 flags &= ~SIGNOK;
619 goto fok;
620 }
621 break;
622 case '.':
623 if (flags & DPTOK) {
624 flags &= ~(SIGNOK | DPTOK);
625 goto fok;
626 }
627 break;
628 case 'e': case 'E':
629 /* no exponent without some digits */
630 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
631 flags =
632 (flags & ~(EXPOK|DPTOK)) |
633 SIGNOK | NDIGITS;
634 goto fok;
635 }
636 break;
637 }
638 break;
639 fok:
640 *p++ = c;
641 if (--fp->_r > 0)
642 fp->_p++;
643 else if (__srefill(fp))
644 break; /* EOF */
645 }
646 /*
647 * If no digits, might be missing exponent digits
648 * (just give back the exponent) or might be missing
649 * regular digits, but had sign and/or decimal point.
650 */
651 if (flags & NDIGITS) {
652 if (flags & EXPOK) {
653 /* no digits at all */
654 while (p > buf)
655 ungetc(*(u_char *)--p, fp);
656 goto match_failure;
657 }
658 /* just a bad exponent (e and maybe sign) */
659 c = *(u_char *)--p;
660 if (c != 'e' && c != 'E') {
661 (void) ungetc(c, fp);/* sign */
662 c = *(u_char *)--p;
663 }
664 (void) ungetc(c, fp);
665 }
666 if ((flags & SUPPRESS) == 0) {
667 long double res;
668
669 *p = 0;
670 res = strtod(buf,(char **) NULL);
671 if (flags & LONGDBL)
672 *va_arg(ap, long double *) = res;
673 else if (flags & LONG)
674 *va_arg(ap, double *) = res;
675 else
676 *va_arg(ap, float *) = res;
677 nassigned++;
678 }
679 nread += p - buf;
680 break;
681 #endif /* FLOATING_POINT */
682 }
683 }
684 input_failure:
685 return (nassigned ? nassigned : -1);
686 match_failure:
687 return (nassigned);
688 }
689
690 /*
691 * Fill in the given table from the scanset at the given format
692 * (just after `['). Return a pointer to the character past the
693 * closing `]'. The table has a 1 wherever characters should be
694 * considered part of the scanset.
695 */
696 static u_char *
697 __sccl(tab, fmt)
698 register char *tab;
699 register u_char *fmt;
700 {
701 register int c, n, v;
702
703 /* first `clear' the whole table */
704 c = *fmt++; /* first char hat => negated scanset */
705 if (c == '^') {
706 v = 1; /* default => accept */
707 c = *fmt++; /* get new first char */
708 } else
709 v = 0; /* default => reject */
710 /* should probably use memset here */
711 for (n = 0; n < 256; n++)
712 tab[n] = v;
713 if (c == 0)
714 return (fmt - 1);/* format ended before closing ] */
715
716 /*
717 * Now set the entries corresponding to the actual scanset
718 * to the opposite of the above.
719 *
720 * The first character may be ']' (or '-') without being special;
721 * the last character may be '-'.
722 */
723 v = 1 - v;
724 for (;;) {
725 tab[c] = v; /* take character c */
726 doswitch:
727 n = *fmt++; /* and examine the next */
728 switch (n) {
729
730 case 0: /* format ended too soon */
731 return (fmt - 1);
732
733 case '-':
734 /*
735 * A scanset of the form
736 * [01+-]
737 * is defined as `the digit 0, the digit 1,
738 * the character +, the character -', but
739 * the effect of a scanset such as
740 * [a-zA-Z0-9]
741 * is implementation defined. The V7 Unix
742 * scanf treats `a-z' as `the letters a through
743 * z', but treats `a-a' as `the letter a, the
744 * character -, and the letter a'.
745 *
746 * For compatibility, the `-' is not considerd
747 * to define a range if the character following
748 * it is either a close bracket (required by ANSI)
749 * or is not numerically greater than the character
750 * we just stored in the table (c).
751 */
752 n = *fmt;
753 if (n == ']' || n < c) {
754 c = '-';
755 break; /* resume the for(;;) */
756 }
757 fmt++;
758 do { /* fill in the range */
759 tab[++c] = v;
760 } while (c < n);
761 #if 1 /* XXX another disgusting compatibility hack */
762 /*
763 * Alas, the V7 Unix scanf also treats formats
764 * such as [a-c-e] as `the letters a through e'.
765 * This too is permitted by the standard....
766 */
767 goto doswitch;
768 #else
769 c = *fmt++;
770 if (c == 0)
771 return (fmt - 1);
772 if (c == ']')
773 return (fmt);
774 #endif
775 break;
776
777 case ']': /* end of scanset */
778 return (fmt);
779
780 default: /* just another character */
781 c = n;
782 break;
783 }
784 }
785 /* NOTREACHED */
786 }