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