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