vfscanf.c revision 1.22 1 /* $NetBSD: vfscanf.c,v 1.22 1998/11/15 17:19:53 christos 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.22 1998/11/15 17:19:53 christos Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include "namespace.h"
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <ctype.h>
52 #if __STDC__
53 #include <stdarg.h>
54 #else
55 #include <varargs.h>
56 #endif
57 #include "local.h"
58
59 #ifdef FLOATING_POINT
60 #include "floatio.h"
61 #endif
62
63 #define BUF 513 /* Maximum length of numeric string. */
64
65 /*
66 * Flags used during conversion.
67 */
68 #define LONG 0x01 /* l: long or double */
69 #define LONGDBL 0x02 /* L: long double; unimplemented */
70 #define SHORT 0x04 /* h: short */
71 #define QUAD 0x08 /* q: quad */
72 #define SUPPRESS 0x10 /* suppress assignment */
73 #define POINTER 0x20 /* weird %p pointer (`fake hex') */
74 #define NOSKIP 0x40 /* do not skip blanks */
75
76 /*
77 * The following are used in numeric conversions only:
78 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
79 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
80 */
81 #define SIGNOK 0x080 /* +/- is (still) legal */
82 #define NDIGITS 0x100 /* no digits detected */
83
84 #define DPTOK 0x200 /* (float) decimal point is still legal */
85 #define EXPOK 0x400 /* (float) exponent (e+3, etc) still legal */
86
87 #define PFXOK 0x200 /* 0x prefix is (still) legal */
88 #define NZDIGITS 0x400 /* no zero digits detected */
89
90 /*
91 * Conversion types.
92 */
93 #define CT_CHAR 0 /* %c conversion */
94 #define CT_CCL 1 /* %[...] conversion */
95 #define CT_STRING 2 /* %s conversion */
96 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
97 #define CT_FLOAT 4 /* floating, i.e., strtod */
98
99 #define u_char unsigned char
100 #define u_long unsigned long
101
102 static const u_char *__sccl __P((char *, const u_char *));
103
104 /*
105 * vfscanf
106 */
107 int
108 __svfscanf(fp, fmt0, ap)
109 FILE *fp;
110 const char *fmt0;
111 _BSD_VA_LIST_ ap;
112 {
113 const u_char *fmt = (const u_char *)fmt0;
114 int c; /* character from format, or conversion */
115 size_t width; /* field width, or 0 */
116 char *p; /* points into all kinds of strings */
117 int n; /* handy integer */
118 int flags; /* flags as defined above */
119 char *p0; /* saves original value of p when necessary */
120 int nassigned; /* number of fields assigned */
121 int nread; /* number of characters consumed from fp */
122 int base; /* base argument to strtoq/strtouq */
123 u_quad_t (*ccfn) __P((const char *, char **, int));
124 /* conversion function (strtoq/strtouq) */
125 char ccltab[256]; /* character class table for %[...] */
126 char buf[BUF]; /* buffer for numeric conversions */
127
128 /* `basefix' is used to avoid `if' tests in the integer scanner */
129 static const short basefix[17] =
130 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
131
132 nassigned = 0;
133 nread = 0;
134 base = 0; /* XXX just to keep gcc happy */
135 ccfn = NULL; /* XXX just to keep gcc happy */
136 for (;;) {
137 c = *fmt++;
138 if (c == 0)
139 return (nassigned);
140 if (isspace(c)) {
141 while ((fp->_r > 0 || __srefill(fp) == 0) &&
142 isspace(*fp->_p))
143 nread++, fp->_r--, fp->_p++;
144 continue;
145 }
146 if (c != '%')
147 goto literal;
148 width = 0;
149 flags = 0;
150 /*
151 * switch on the format. continue if done;
152 * break once format type is derived.
153 */
154 again: c = *fmt++;
155 switch (c) {
156 case '%':
157 literal:
158 if (fp->_r <= 0 && __srefill(fp))
159 goto input_failure;
160 if (*fp->_p != c)
161 goto match_failure;
162 fp->_r--, fp->_p++;
163 nread++;
164 continue;
165
166 case '*':
167 flags |= SUPPRESS;
168 goto again;
169 case 'L':
170 flags |= LONGDBL;
171 goto again;
172 case 'h':
173 flags |= SHORT;
174 goto again;
175 case 'l':
176 if (*fmt == 'l') {
177 fmt++;
178 flags |= QUAD;
179 } else {
180 flags |= LONG;
181 }
182 goto again;
183 case 'q':
184 flags |= QUAD;
185 goto again;
186
187 case '0': case '1': case '2': case '3': case '4':
188 case '5': case '6': case '7': case '8': case '9':
189 width = width * 10 + c - '0';
190 goto again;
191
192 /*
193 * Conversions.
194 * Those marked `compat' are for 4.[123]BSD compatibility.
195 *
196 * (According to ANSI, E and X formats are supposed
197 * to the same as e and x. Sorry about that.)
198 */
199 case 'D': /* compat */
200 flags |= LONG;
201 /* FALLTHROUGH */
202 case 'd':
203 c = CT_INT;
204 ccfn = (u_quad_t (*) __P((const char *, char **, int)))strtoq;
205 base = 10;
206 break;
207
208 case 'i':
209 c = CT_INT;
210 ccfn = (u_quad_t (*) __P((const char *, char **, int)))strtoq;
211 base = 0;
212 break;
213
214 case 'O': /* compat */
215 flags |= LONG;
216 /* FALLTHROUGH */
217 case 'o':
218 c = CT_INT;
219 ccfn = strtouq;
220 base = 8;
221 break;
222
223 case 'u':
224 c = CT_INT;
225 ccfn = strtouq;
226 base = 10;
227 break;
228
229 case 'X':
230 case 'x':
231 flags |= PFXOK; /* enable 0x prefixing */
232 c = CT_INT;
233 ccfn = strtouq;
234 base = 16;
235 break;
236
237 #ifdef FLOATING_POINT
238 case 'E':
239 case 'G':
240 case 'e':
241 case 'f':
242 case 'g':
243 c = CT_FLOAT;
244 break;
245 #endif
246
247 case 's':
248 c = CT_STRING;
249 break;
250
251 case '[':
252 fmt = __sccl(ccltab, fmt);
253 flags |= NOSKIP;
254 c = CT_CCL;
255 break;
256
257 case 'c':
258 flags |= NOSKIP;
259 c = CT_CHAR;
260 break;
261
262 case 'p': /* pointer format is like hex */
263 flags |= POINTER | PFXOK;
264 c = CT_INT;
265 ccfn = strtouq;
266 base = 16;
267 break;
268
269 case 'n':
270 if (flags & SUPPRESS) /* ??? */
271 continue;
272 if (flags & SHORT)
273 *va_arg(ap, short *) = nread;
274 else if (flags & LONG)
275 *va_arg(ap, long *) = nread;
276 else
277 *va_arg(ap, int *) = nread;
278 continue;
279
280 /*
281 * Disgusting backwards compatibility hacks. XXX
282 */
283 case '\0': /* compat */
284 return (EOF);
285
286 default: /* compat */
287 if (isupper(c))
288 flags |= LONG;
289 c = CT_INT;
290 ccfn = (u_quad_t (*) __P((const char *, char **, int)))strtoq;
291 base = 10;
292 break;
293 }
294
295 /*
296 * We have a conversion that requires input.
297 */
298 if (fp->_r <= 0 && __srefill(fp))
299 goto input_failure;
300
301 /*
302 * Consume leading white space, except for formats
303 * that suppress this.
304 */
305 if ((flags & NOSKIP) == 0) {
306 while (isspace(*fp->_p)) {
307 nread++;
308 if (--fp->_r > 0)
309 fp->_p++;
310 else if (__srefill(fp))
311 goto input_failure;
312 }
313 /*
314 * Note that there is at least one character in
315 * the buffer, so conversions that do not set NOSKIP
316 * ca no longer result in an input failure.
317 */
318 }
319
320 /*
321 * Do the conversion.
322 */
323 switch (c) {
324
325 case CT_CHAR:
326 /* scan arbitrary characters (sets NOSKIP) */
327 if (width == 0)
328 width = 1;
329 if (flags & SUPPRESS) {
330 size_t sum = 0;
331 for (;;) {
332 if ((n = fp->_r) < width) {
333 sum += n;
334 width -= n;
335 fp->_p += n;
336 if (__srefill(fp)) {
337 if (sum == 0)
338 goto input_failure;
339 break;
340 }
341 } else {
342 sum += width;
343 fp->_r -= width;
344 fp->_p += width;
345 break;
346 }
347 }
348 nread += sum;
349 } else {
350 size_t r = fread((void *)va_arg(ap, char *), 1,
351 width, fp);
352
353 if (r == 0)
354 goto input_failure;
355 nread += r;
356 nassigned++;
357 }
358 break;
359
360 case CT_CCL:
361 /* scan a (nonempty) character class (sets NOSKIP) */
362 if (width == 0)
363 width = ~0U; /* `infinity' */
364 /* take only those things in the class */
365 if (flags & SUPPRESS) {
366 n = 0;
367 while (ccltab[*fp->_p]) {
368 n++, fp->_r--, fp->_p++;
369 if (--width == 0)
370 break;
371 if (fp->_r <= 0 && __srefill(fp)) {
372 if (n == 0)
373 goto input_failure;
374 break;
375 }
376 }
377 if (n == 0)
378 goto match_failure;
379 } else {
380 p0 = p = va_arg(ap, char *);
381 while (ccltab[*fp->_p]) {
382 fp->_r--;
383 *p++ = *fp->_p++;
384 if (--width == 0)
385 break;
386 if (fp->_r <= 0 && __srefill(fp)) {
387 if (p == p0)
388 goto input_failure;
389 break;
390 }
391 }
392 n = p - p0;
393 if (n == 0)
394 goto match_failure;
395 *p = 0;
396 nassigned++;
397 }
398 nread += n;
399 break;
400
401 case CT_STRING:
402 /* like CCL, but zero-length string OK, & no NOSKIP */
403 if (width == 0)
404 width = ~0U;
405 if (flags & SUPPRESS) {
406 n = 0;
407 while (!isspace(*fp->_p)) {
408 n++, fp->_r--, fp->_p++;
409 if (--width == 0)
410 break;
411 if (fp->_r <= 0 && __srefill(fp))
412 break;
413 }
414 nread += n;
415 } else {
416 p0 = p = va_arg(ap, char *);
417 while (!isspace(*fp->_p)) {
418 fp->_r--;
419 *p++ = *fp->_p++;
420 if (--width == 0)
421 break;
422 if (fp->_r <= 0 && __srefill(fp))
423 break;
424 }
425 *p = 0;
426 nread += p - p0;
427 nassigned++;
428 }
429 continue;
430
431 case CT_INT:
432 /* scan an integer as if by strtoq/strtouq */
433 #ifdef hardway
434 if (width == 0 || width > sizeof(buf) - 1)
435 width = sizeof(buf) - 1;
436 #else
437 /* size_t is unsigned, hence this optimisation */
438 if (--width > sizeof(buf) - 2)
439 width = sizeof(buf) - 2;
440 width++;
441 #endif
442 flags |= SIGNOK | NDIGITS | NZDIGITS;
443 for (p = buf; width; width--) {
444 c = *fp->_p;
445 /*
446 * Switch on the character; `goto ok'
447 * if we accept it as a part of number.
448 */
449 switch (c) {
450
451 /*
452 * The digit 0 is always legal, but is
453 * special. For %i conversions, if no
454 * digits (zero or nonzero) have been
455 * scanned (only signs), we will have
456 * base==0. In that case, we should set
457 * it to 8 and enable 0x prefixing.
458 * Also, if we have not scanned zero digits
459 * before this, do not turn off prefixing
460 * (someone else will turn it off if we
461 * have scanned any nonzero digits).
462 */
463 case '0':
464 if (base == 0) {
465 base = 8;
466 flags |= PFXOK;
467 }
468 if (flags & NZDIGITS)
469 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
470 else
471 flags &= ~(SIGNOK|PFXOK|NDIGITS);
472 goto ok;
473
474 /* 1 through 7 always legal */
475 case '1': case '2': case '3':
476 case '4': case '5': case '6': case '7':
477 base = basefix[base];
478 flags &= ~(SIGNOK | PFXOK | NDIGITS);
479 goto ok;
480
481 /* digits 8 and 9 ok iff decimal or hex */
482 case '8': case '9':
483 base = basefix[base];
484 if (base <= 8)
485 break; /* not legal here */
486 flags &= ~(SIGNOK | PFXOK | NDIGITS);
487 goto ok;
488
489 /* letters ok iff hex */
490 case 'A': case 'B': case 'C':
491 case 'D': case 'E': case 'F':
492 case 'a': case 'b': case 'c':
493 case 'd': case 'e': case 'f':
494 /* no need to fix base here */
495 if (base <= 10)
496 break; /* not legal here */
497 flags &= ~(SIGNOK | PFXOK | NDIGITS);
498 goto ok;
499
500 /* sign ok only as first character */
501 case '+': case '-':
502 if (flags & SIGNOK) {
503 flags &= ~SIGNOK;
504 goto ok;
505 }
506 break;
507
508 /* x ok iff flag still set & 2nd char */
509 case 'x': case 'X':
510 if (flags & PFXOK && p == buf + 1) {
511 base = 16; /* if %i */
512 flags &= ~PFXOK;
513 goto ok;
514 }
515 break;
516 }
517
518 /*
519 * If we got here, c is not a legal character
520 * for a number. Stop accumulating digits.
521 */
522 break;
523 ok:
524 /*
525 * c is legal: store it and look at the next.
526 */
527 *p++ = c;
528 if (--fp->_r > 0)
529 fp->_p++;
530 else if (__srefill(fp))
531 break; /* EOF */
532 }
533 /*
534 * If we had only a sign, it is no good; push
535 * back the sign. If the number ends in `x',
536 * it was [sign] '0' 'x', so push back the x
537 * and treat it as [sign] '0'.
538 */
539 if (flags & NDIGITS) {
540 if (p > buf)
541 (void) ungetc(*(u_char *)--p, fp);
542 goto match_failure;
543 }
544 c = ((u_char *)p)[-1];
545 if (c == 'x' || c == 'X') {
546 --p;
547 (void) ungetc(c, fp);
548 }
549 if ((flags & SUPPRESS) == 0) {
550 u_quad_t res;
551
552 *p = 0;
553 res = (*ccfn)(buf, (char **)NULL, base);
554 if (flags & POINTER)
555 *va_arg(ap, void **) =
556 (void *)(long)res;
557 else if (flags & QUAD)
558 *va_arg(ap, quad_t *) = res;
559 else if (flags & LONG)
560 *va_arg(ap, long *) = (long)res;
561 else if (flags & SHORT)
562 *va_arg(ap, short *) = (short)res;
563 else
564 *va_arg(ap, int *) = (int)res;
565 nassigned++;
566 }
567 nread += p - buf;
568 break;
569
570 #ifdef FLOATING_POINT
571 case CT_FLOAT:
572 /* scan a floating point number as if by strtod */
573 #ifdef hardway
574 if (width == 0 || width > sizeof(buf) - 1)
575 width = sizeof(buf) - 1;
576 #else
577 /* size_t is unsigned, hence this optimisation */
578 if (--width > sizeof(buf) - 2)
579 width = sizeof(buf) - 2;
580 width++;
581 #endif
582 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
583 for (p = buf; width; width--) {
584 c = *fp->_p;
585 /*
586 * This code mimicks the integer conversion
587 * code, but is much simpler.
588 */
589 switch (c) {
590
591 case '0': case '1': case '2': case '3':
592 case '4': case '5': case '6': case '7':
593 case '8': case '9':
594 flags &= ~(SIGNOK | NDIGITS);
595 goto fok;
596
597 case '+': case '-':
598 if (flags & SIGNOK) {
599 flags &= ~SIGNOK;
600 goto fok;
601 }
602 break;
603 case '.':
604 if (flags & DPTOK) {
605 flags &= ~(SIGNOK | DPTOK);
606 goto fok;
607 }
608 break;
609 case 'e': case 'E':
610 /* no exponent without some digits */
611 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
612 flags =
613 (flags & ~(EXPOK|DPTOK)) |
614 SIGNOK | NDIGITS;
615 goto fok;
616 }
617 break;
618 }
619 break;
620 fok:
621 *p++ = c;
622 if (--fp->_r > 0)
623 fp->_p++;
624 else if (__srefill(fp))
625 break; /* EOF */
626 }
627 /*
628 * If no digits, might be missing exponent digits
629 * (just give back the exponent) or might be missing
630 * regular digits, but had sign and/or decimal point.
631 */
632 if (flags & NDIGITS) {
633 if (flags & EXPOK) {
634 /* no digits at all */
635 while (p > buf)
636 ungetc(*(u_char *)--p, fp);
637 goto match_failure;
638 }
639 /* just a bad exponent (e and maybe sign) */
640 c = *(u_char *)--p;
641 if (c != 'e' && c != 'E') {
642 (void) ungetc(c, fp);/* sign */
643 c = *(u_char *)--p;
644 }
645 (void) ungetc(c, fp);
646 }
647 if ((flags & SUPPRESS) == 0) {
648 double res;
649
650 *p = 0;
651 res = strtod(buf, (char **) NULL);
652 if (flags & LONGDBL)
653 *va_arg(ap, long double *) = res;
654 else 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 }
665 input_failure:
666 return (nassigned ? nassigned : EOF);
667 match_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 */
677 static const u_char *
678 __sccl(tab, fmt)
679 char *tab;
680 const u_char *fmt;
681 {
682 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 */
707 doswitch:
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 break;
756 #endif
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 }
768