2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
16 SM_IDSTR(id, "@(#)$Id: vfscanf.c,v 1.51 2001/09/11 04:04:49 gshapiro Exp $")
23 #include <sm/varargs.h>
24 #include <sm/config.h>
26 #include <sm/signal.h>
28 #include <sm/string.h>
31 #define BUF 513 /* Maximum length of numeric string. */
33 /* Flags used during conversion. */
34 #define LONG 0x01 /* l: long or double */
35 #define SHORT 0x04 /* h: short */
36 #define QUAD 0x08 /* q: quad (same as ll) */
37 #define SUPPRESS 0x10 /* suppress assignment */
38 #define POINTER 0x20 /* weird %p pointer (`fake hex') */
39 #define NOSKIP 0x40 /* do not skip blanks */
42 ** The following are used in numeric conversions only:
43 ** SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
44 ** SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
47 #define SIGNOK 0x080 /* +/- is (still) legal */
48 #define NDIGITS 0x100 /* no digits detected */
50 #define DPTOK 0x200 /* (float) decimal point is still legal */
51 #define EXPOK 0x400 /* (float) exponent (e+3, etc) still legal */
53 #define PFXOK 0x200 /* 0x prefix is (still) legal */
54 #define NZDIGITS 0x400 /* no zero digits detected */
56 /* Conversion types. */
57 #define CT_CHAR 0 /* %c conversion */
58 #define CT_CCL 1 /* %[...] conversion */
59 #define CT_STRING 2 /* %s conversion */
60 #define CT_INT 3 /* integer, i.e., strtoll or strtoull */
61 #define CT_FLOAT 4 /* floating, i.e., strtod */
63 static unsigned char *sm_sccl __P((char *, unsigned char *));
64 static jmp_buf ScanTimeOut;
67 ** SCANALRM -- handler when timeout activated for sm_io_vfscanf()
69 ** Returns flow of control to where setjmp(ScanTimeOut) was set.
78 ** returns flow of control to setjmp(ScanTimeOut).
80 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
81 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
90 longjmp(ScanTimeOut, 1);
94 ** SM_VFSCANF -- convert input into data units
97 ** fp -- file pointer for input data
98 ** timeout -- time intvl allowed to complete (milliseconds)
99 ** fmt0 -- format for finding data units
100 ** ap -- vectors for memory location for storing data units
103 ** Success: number of data units assigned
104 ** Failure: SM_IO_EOF
108 sm_vfscanf(fp, timeout, fmt0, ap)
109 register SM_FILE_T *fp;
110 int SM_NONVOLATILE timeout;
112 va_list SM_NONVOLATILE ap;
114 register unsigned char *SM_NONVOLATILE fmt = (unsigned char *) fmt0;
115 register int c; /* character from format, or conversion */
116 register size_t width; /* field width, or 0 */
117 register char *p; /* points into all kinds of strings */
118 register int n; /* handy integer */
119 register int flags; /* flags as defined above */
120 register char *p0; /* saves original value of p when necessary */
121 int nassigned; /* number of fields assigned */
122 int nread; /* number of characters consumed from fp */
123 int base; /* base argument to strtoll/strtoull */
124 ULONGLONG_T (*ccfn)(); /* conversion function (strtoll/strtoull) */
125 char ccltab[256]; /* character class table for %[...] */
126 char buf[BUF]; /* buffer for numeric conversions */
127 SM_EVENT *evt = NULL;
129 /* `basefix' is used to avoid `if' tests in the integer scanner */
130 static short basefix[17] =
131 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
133 if (timeout == SM_TIME_DEFAULT)
134 timeout = fp->f_timeout;
135 if (timeout == SM_TIME_IMMEDIATE)
138 ** Filling the buffer will take time and we are wanted to
139 ** return immediately. So...
146 if (timeout != SM_TIME_FOREVER)
148 if (setjmp(ScanTimeOut) != 0)
154 evt = sm_seteventm(timeout, scanalrm, 0);
159 base = 0; /* XXX just to keep gcc happy */
160 ccfn = NULL; /* XXX just to keep gcc happy */
167 sm_clrevent(evt); /* undo our timeout */
172 while ((fp->f_r > 0 || sm_refill(fp, SM_TIME_FOREVER)
175 nread++, fp->f_r--, fp->f_p++;
184 ** switch on the format. continue if done;
185 ** break once format type is derived.
193 if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
197 fp->f_r--, fp->f_p++;
222 case '0': case '1': case '2': case '3': case '4':
223 case '5': case '6': case '7': case '8': case '9':
224 width = width * 10 + c - '0';
229 ** Those marked `compat' are for 4.[123]BSD compatibility.
231 ** (According to ANSI, E and X formats are supposed
232 ** to the same as e and x. Sorry about that.)
235 case 'D': /* compat */
240 ccfn = (ULONGLONG_T (*)())sm_strtoll;
246 ccfn = (ULONGLONG_T (*)())sm_strtoll;
250 case 'O': /* compat */
267 flags |= PFXOK; /* enable 0x prefixing */
286 fmt = sm_sccl(ccltab, fmt);
296 case 'p': /* pointer format is like hex */
297 flags |= POINTER | PFXOK;
304 if (flags & SUPPRESS) /* ??? */
307 *SM_VA_ARG(ap, short *) = nread;
308 else if (flags & LONG)
309 *SM_VA_ARG(ap, long *) = nread;
311 *SM_VA_ARG(ap, int *) = nread;
314 /* Disgusting backwards compatibility hacks. XXX */
315 case '\0': /* compat */
317 sm_clrevent(evt); /* undo our timeout */
320 default: /* compat */
324 ccfn = (ULONGLONG_T (*)()) sm_strtoll;
329 /* We have a conversion that requires input. */
330 if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
334 ** Consume leading white space, except for formats
335 ** that suppress this.
338 if ((flags & NOSKIP) == 0)
340 while (isspace(*fp->f_p))
345 else if (sm_refill(fp, SM_TIME_FOREVER))
349 ** Note that there is at least one character in
350 ** the buffer, so conversions that do not set NOSKIP
351 ** can no longer result in an input failure.
355 /* Do the conversion. */
359 /* scan arbitrary characters (sets NOSKIP) */
362 if (flags & SUPPRESS)
367 if ((size_t) (n = fp->f_r) < width)
394 r = sm_io_read(fp, SM_TIME_FOREVER,
395 (void *) SM_VA_ARG(ap, char *),
405 /* scan a (nonempty) character class (sets NOSKIP) */
407 width = (size_t)~0; /* `infinity' */
409 /* take only those things in the class */
410 if (flags & SUPPRESS)
413 while (ccltab[*fp->f_p] != '\0')
415 n++, fp->f_r--, fp->f_p++;
419 sm_refill(fp, SM_TIME_FOREVER))
421 if (n == 0) /* XXX how? */
431 p0 = p = SM_VA_ARG(ap, char *);
432 while (ccltab[*fp->f_p] != '\0')
439 sm_refill(fp, SM_TIME_FOREVER))
456 /* like CCL, but zero-length string OK, & no NOSKIP */
459 if (flags & SUPPRESS)
462 while (!isspace(*fp->f_p))
464 n++, fp->f_r--, fp->f_p++;
468 sm_refill(fp, SM_TIME_FOREVER))
475 p0 = p = SM_VA_ARG(ap, char *);
476 while (!isspace(*fp->f_p))
483 sm_refill(fp, SM_TIME_FOREVER))
493 /* scan an integer as if by strtoll/strtoull */
494 #if SM_CONF_BROKEN_SIZE_T
495 if (width == 0 || width > sizeof(buf) - 1)
496 width = sizeof(buf) - 1;
497 #else /* SM_CONF_BROKEN_SIZE_T */
498 /* size_t is unsigned, hence this optimisation */
499 if (--width > sizeof(buf) - 2)
500 width = sizeof(buf) - 2;
502 #endif /* SM_CONF_BROKEN_SIZE_T */
503 flags |= SIGNOK | NDIGITS | NZDIGITS;
504 for (p = buf; width > 0; width--)
509 ** Switch on the character; `goto ok'
510 ** if we accept it as a part of number.
517 ** The digit 0 is always legal, but is
518 ** special. For %i conversions, if no
519 ** digits (zero or nonzero) have been
520 ** scanned (only signs), we will have
521 ** base==0. In that case, we should set
522 ** it to 8 and enable 0x prefixing.
523 ** Also, if we have not scanned zero digits
524 ** before this, do not turn off prefixing
525 ** (someone else will turn it off if we
526 ** have scanned any nonzero digits).
535 if (flags & NZDIGITS)
536 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
538 flags &= ~(SIGNOK|PFXOK|NDIGITS);
541 /* 1 through 7 always legal */
542 case '1': case '2': case '3':
543 case '4': case '5': case '6': case '7':
544 base = basefix[base];
545 flags &= ~(SIGNOK | PFXOK | NDIGITS);
548 /* digits 8 and 9 ok iff decimal or hex */
550 base = basefix[base];
552 break; /* not legal here */
553 flags &= ~(SIGNOK | PFXOK | NDIGITS);
556 /* letters ok iff hex */
557 case 'A': case 'B': case 'C':
558 case 'D': case 'E': case 'F':
559 case 'a': case 'b': case 'c':
560 case 'd': case 'e': case 'f':
562 /* no need to fix base here */
564 break; /* not legal here */
565 flags &= ~(SIGNOK | PFXOK | NDIGITS);
568 /* sign ok only as first character */
577 /* x ok iff flag still set & 2nd char */
579 if (flags & PFXOK && p == buf + 1)
581 base = 16; /* if %i */
589 ** If we got here, c is not a legal character
590 ** for a number. Stop accumulating digits.
595 /* c is legal: store it and look at the next. */
599 else if (sm_refill(fp, SM_TIME_FOREVER))
600 break; /* SM_IO_EOF */
604 ** If we had only a sign, it is no good; push
605 ** back the sign. If the number ends in `x',
606 ** it was [sign] '0' 'x', so push back the x
607 ** and treat it as [sign] '0'.
613 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
614 *(unsigned char *)--p);
617 c = ((unsigned char *)p)[-1];
618 if (c == 'x' || c == 'X')
621 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
623 if ((flags & SUPPRESS) == 0)
628 res = (*ccfn)(buf, (char **)NULL, base);
630 *SM_VA_ARG(ap, void **) =
632 else if (flags & QUAD)
633 *SM_VA_ARG(ap, LONGLONG_T *) = res;
634 else if (flags & LONG)
635 *SM_VA_ARG(ap, long *) = res;
636 else if (flags & SHORT)
637 *SM_VA_ARG(ap, short *) = res;
639 *SM_VA_ARG(ap, int *) = res;
646 /* scan a floating point number as if by strtod */
647 if (width == 0 || width > sizeof(buf) - 1)
648 width = sizeof(buf) - 1;
649 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
650 for (p = buf; width; width--)
655 ** This code mimicks the integer conversion
656 ** code, but is much simpler.
662 case '0': case '1': case '2': case '3':
663 case '4': case '5': case '6': case '7':
665 flags &= ~(SIGNOK | NDIGITS);
678 flags &= ~(SIGNOK | DPTOK);
684 /* no exponent without some digits */
685 if ((flags&(NDIGITS|EXPOK)) == EXPOK)
688 (flags & ~(EXPOK|DPTOK)) |
699 else if (sm_refill(fp, SM_TIME_FOREVER))
700 break; /* SM_IO_EOF */
704 ** If no digits, might be missing exponent digits
705 ** (just give back the exponent) or might be missing
706 ** regular digits, but had sign and/or decimal point.
713 /* no digits at all */
715 (void) sm_io_ungetc(fp,
717 *(unsigned char *)--p);
721 /* just a bad exponent (e and maybe sign) */
722 c = *(unsigned char *) --p;
723 if (c != 'e' && c != 'E')
725 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
727 c = *(unsigned char *)--p;
729 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
731 if ((flags & SUPPRESS) == 0)
736 res = strtod(buf, (char **) NULL);
738 *SM_VA_ARG(ap, double *) = res;
740 *SM_VA_ARG(ap, float *) = res;
749 sm_clrevent(evt); /* undo our timeout */
750 return nassigned ? nassigned : -1;
753 sm_clrevent(evt); /* undo our timeout */
758 ** SM_SCCL -- sequenced character comparison list
760 ** Fill in the given table from the scanset at the given format
761 ** (just after `['). Return a pointer to the character past the
762 ** closing `]'. The table has a 1 wherever characters should be
763 ** considered part of the scanset.
766 ** tab -- array flagging "active" char's to match (returned)
767 ** fmt -- character list (within "[]")
772 static unsigned char *
775 register unsigned char *fmt;
777 register int c, n, v;
779 /* first `clear' the whole table */
780 c = *fmt++; /* first char hat => negated scanset */
783 v = 1; /* default => accept */
784 c = *fmt++; /* get new first char */
787 v = 0; /* default => reject */
789 /* should probably use memset here */
790 for (n = 0; n < 256; n++)
793 return fmt - 1; /* format ended before closing ] */
796 ** Now set the entries corresponding to the actual scanset
797 ** to the opposite of the above.
799 ** The first character may be ']' (or '-') without being special;
800 ** the last character may be '-'.
806 tab[c] = v; /* take character c */
808 n = *fmt++; /* and examine the next */
812 case 0: /* format ended too soon */
817 ** A scanset of the form
819 ** is defined as `the digit 0, the digit 1,
820 ** the character +, the character -', but
821 ** the effect of a scanset such as
823 ** is implementation defined. The V7 Unix
824 ** scanf treats `a-z' as `the letters a through
825 ** z', but treats `a-a' as `the letter a, the
826 ** character -, and the letter a'.
828 ** For compatibility, the `-' is not considerd
829 ** to define a range if the character following
830 ** it is either a close bracket (required by ANSI)
831 ** or is not numerically greater than the character
832 ** we just stored in the table (c).
836 if (n == ']' || n < c)
839 break; /* resume the for(;;) */
844 /* fill in the range */
847 #if 1 /* XXX another disgusting compatibility hack */
850 ** Alas, the V7 Unix scanf also treats formats
851 ** such as [a-c-e] as `the letters a through e'.
852 ** This too is permitted by the standard....
865 case ']': /* end of scanset */
868 default: /* just another character */