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