2 * Copyright (c) 2000-2001, 2004 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.52 2004/08/03 20:56:32 ca 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 void scanalrm __P((int));
64 static unsigned char *sm_sccl __P((char *, unsigned char *));
65 static jmp_buf ScanTimeOut;
68 ** SCANALRM -- handler when timeout activated for sm_io_vfscanf()
70 ** Returns flow of control to where setjmp(ScanTimeOut) was set.
79 ** returns flow of control to setjmp(ScanTimeOut).
81 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
82 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
91 longjmp(ScanTimeOut, 1);
95 ** SM_VFSCANF -- convert input into data units
98 ** fp -- file pointer for input data
99 ** timeout -- time intvl allowed to complete (milliseconds)
100 ** fmt0 -- format for finding data units
101 ** ap -- vectors for memory location for storing data units
104 ** Success: number of data units assigned
105 ** Failure: SM_IO_EOF
109 sm_vfscanf(fp, timeout, fmt0, ap)
110 register SM_FILE_T *fp;
111 int SM_NONVOLATILE timeout;
113 va_list SM_NONVOLATILE ap;
115 register unsigned char *SM_NONVOLATILE fmt = (unsigned char *) fmt0;
116 register int c; /* character from format, or conversion */
117 register size_t width; /* field width, or 0 */
118 register char *p; /* points into all kinds of strings */
119 register int n; /* handy integer */
120 register int flags; /* flags as defined above */
121 register char *p0; /* saves original value of p when necessary */
122 int nassigned; /* number of fields assigned */
123 int nread; /* number of characters consumed from fp */
124 int base; /* base argument to strtoll/strtoull */
125 ULONGLONG_T (*ccfn)(); /* conversion function (strtoll/strtoull) */
126 char ccltab[256]; /* character class table for %[...] */
127 char buf[BUF]; /* buffer for numeric conversions */
128 SM_EVENT *evt = NULL;
130 /* `basefix' is used to avoid `if' tests in the integer scanner */
131 static short basefix[17] =
132 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
134 if (timeout == SM_TIME_DEFAULT)
135 timeout = fp->f_timeout;
136 if (timeout == SM_TIME_IMMEDIATE)
139 ** Filling the buffer will take time and we are wanted to
140 ** return immediately. So...
147 if (timeout != SM_TIME_FOREVER)
149 if (setjmp(ScanTimeOut) != 0)
155 evt = sm_seteventm(timeout, scanalrm, 0);
160 base = 0; /* XXX just to keep gcc happy */
161 ccfn = NULL; /* XXX just to keep gcc happy */
168 sm_clrevent(evt); /* undo our timeout */
173 while ((fp->f_r > 0 || sm_refill(fp, SM_TIME_FOREVER)
176 nread++, fp->f_r--, fp->f_p++;
185 ** switch on the format. continue if done;
186 ** break once format type is derived.
194 if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
198 fp->f_r--, fp->f_p++;
223 case '0': case '1': case '2': case '3': case '4':
224 case '5': case '6': case '7': case '8': case '9':
225 width = width * 10 + c - '0';
230 ** Those marked `compat' are for 4.[123]BSD compatibility.
232 ** (According to ANSI, E and X formats are supposed
233 ** to the same as e and x. Sorry about that.)
236 case 'D': /* compat */
241 ccfn = (ULONGLONG_T (*)())sm_strtoll;
247 ccfn = (ULONGLONG_T (*)())sm_strtoll;
251 case 'O': /* compat */
268 flags |= PFXOK; /* enable 0x prefixing */
287 fmt = sm_sccl(ccltab, fmt);
297 case 'p': /* pointer format is like hex */
298 flags |= POINTER | PFXOK;
305 if (flags & SUPPRESS) /* ??? */
308 *SM_VA_ARG(ap, short *) = nread;
309 else if (flags & LONG)
310 *SM_VA_ARG(ap, long *) = nread;
312 *SM_VA_ARG(ap, int *) = nread;
315 /* Disgusting backwards compatibility hacks. XXX */
316 case '\0': /* compat */
318 sm_clrevent(evt); /* undo our timeout */
321 default: /* compat */
325 ccfn = (ULONGLONG_T (*)()) sm_strtoll;
330 /* We have a conversion that requires input. */
331 if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
335 ** Consume leading white space, except for formats
336 ** that suppress this.
339 if ((flags & NOSKIP) == 0)
341 while (isspace(*fp->f_p))
346 else if (sm_refill(fp, SM_TIME_FOREVER))
350 ** Note that there is at least one character in
351 ** the buffer, so conversions that do not set NOSKIP
352 ** can no longer result in an input failure.
356 /* Do the conversion. */
360 /* scan arbitrary characters (sets NOSKIP) */
363 if (flags & SUPPRESS)
368 if ((size_t) (n = fp->f_r) < width)
395 r = sm_io_read(fp, SM_TIME_FOREVER,
396 (void *) SM_VA_ARG(ap, char *),
406 /* scan a (nonempty) character class (sets NOSKIP) */
408 width = (size_t)~0; /* `infinity' */
410 /* take only those things in the class */
411 if (flags & SUPPRESS)
414 while (ccltab[*fp->f_p] != '\0')
416 n++, fp->f_r--, fp->f_p++;
420 sm_refill(fp, SM_TIME_FOREVER))
422 if (n == 0) /* XXX how? */
432 p0 = p = SM_VA_ARG(ap, char *);
433 while (ccltab[*fp->f_p] != '\0')
440 sm_refill(fp, SM_TIME_FOREVER))
457 /* like CCL, but zero-length string OK, & no NOSKIP */
460 if (flags & SUPPRESS)
463 while (!isspace(*fp->f_p))
465 n++, fp->f_r--, fp->f_p++;
469 sm_refill(fp, SM_TIME_FOREVER))
476 p0 = p = SM_VA_ARG(ap, char *);
477 while (!isspace(*fp->f_p))
484 sm_refill(fp, SM_TIME_FOREVER))
494 /* scan an integer as if by strtoll/strtoull */
495 #if SM_CONF_BROKEN_SIZE_T
496 if (width == 0 || width > sizeof(buf) - 1)
497 width = sizeof(buf) - 1;
498 #else /* SM_CONF_BROKEN_SIZE_T */
499 /* size_t is unsigned, hence this optimisation */
500 if (--width > sizeof(buf) - 2)
501 width = sizeof(buf) - 2;
503 #endif /* SM_CONF_BROKEN_SIZE_T */
504 flags |= SIGNOK | NDIGITS | NZDIGITS;
505 for (p = buf; width > 0; width--)
510 ** Switch on the character; `goto ok'
511 ** if we accept it as a part of number.
518 ** The digit 0 is always legal, but is
519 ** special. For %i conversions, if no
520 ** digits (zero or nonzero) have been
521 ** scanned (only signs), we will have
522 ** base==0. In that case, we should set
523 ** it to 8 and enable 0x prefixing.
524 ** Also, if we have not scanned zero digits
525 ** before this, do not turn off prefixing
526 ** (someone else will turn it off if we
527 ** have scanned any nonzero digits).
536 if (flags & NZDIGITS)
537 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
539 flags &= ~(SIGNOK|PFXOK|NDIGITS);
542 /* 1 through 7 always legal */
543 case '1': case '2': case '3':
544 case '4': case '5': case '6': case '7':
545 base = basefix[base];
546 flags &= ~(SIGNOK | PFXOK | NDIGITS);
549 /* digits 8 and 9 ok iff decimal or hex */
551 base = basefix[base];
553 break; /* not legal here */
554 flags &= ~(SIGNOK | PFXOK | NDIGITS);
557 /* letters ok iff hex */
558 case 'A': case 'B': case 'C':
559 case 'D': case 'E': case 'F':
560 case 'a': case 'b': case 'c':
561 case 'd': case 'e': case 'f':
563 /* no need to fix base here */
565 break; /* not legal here */
566 flags &= ~(SIGNOK | PFXOK | NDIGITS);
569 /* sign ok only as first character */
578 /* x ok iff flag still set & 2nd char */
580 if (flags & PFXOK && p == buf + 1)
582 base = 16; /* if %i */
590 ** If we got here, c is not a legal character
591 ** for a number. Stop accumulating digits.
596 /* c is legal: store it and look at the next. */
600 else if (sm_refill(fp, SM_TIME_FOREVER))
601 break; /* SM_IO_EOF */
605 ** If we had only a sign, it is no good; push
606 ** back the sign. If the number ends in `x',
607 ** it was [sign] '0' 'x', so push back the x
608 ** and treat it as [sign] '0'.
614 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
615 *(unsigned char *)--p);
618 c = ((unsigned char *)p)[-1];
619 if (c == 'x' || c == 'X')
622 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
624 if ((flags & SUPPRESS) == 0)
629 res = (*ccfn)(buf, (char **)NULL, base);
631 *SM_VA_ARG(ap, void **) =
633 else if (flags & QUAD)
634 *SM_VA_ARG(ap, LONGLONG_T *) = res;
635 else if (flags & LONG)
636 *SM_VA_ARG(ap, long *) = res;
637 else if (flags & SHORT)
638 *SM_VA_ARG(ap, short *) = res;
640 *SM_VA_ARG(ap, int *) = res;
647 /* scan a floating point number as if by strtod */
648 if (width == 0 || width > sizeof(buf) - 1)
649 width = sizeof(buf) - 1;
650 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
651 for (p = buf; width; width--)
656 ** This code mimicks the integer conversion
657 ** code, but is much simpler.
663 case '0': case '1': case '2': case '3':
664 case '4': case '5': case '6': case '7':
666 flags &= ~(SIGNOK | NDIGITS);
679 flags &= ~(SIGNOK | DPTOK);
685 /* no exponent without some digits */
686 if ((flags&(NDIGITS|EXPOK)) == EXPOK)
689 (flags & ~(EXPOK|DPTOK)) |
700 else if (sm_refill(fp, SM_TIME_FOREVER))
701 break; /* SM_IO_EOF */
705 ** If no digits, might be missing exponent digits
706 ** (just give back the exponent) or might be missing
707 ** regular digits, but had sign and/or decimal point.
714 /* no digits at all */
716 (void) sm_io_ungetc(fp,
718 *(unsigned char *)--p);
722 /* just a bad exponent (e and maybe sign) */
723 c = *(unsigned char *) --p;
724 if (c != 'e' && c != 'E')
726 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
728 c = *(unsigned char *)--p;
730 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
732 if ((flags & SUPPRESS) == 0)
737 res = strtod(buf, (char **) NULL);
739 *SM_VA_ARG(ap, double *) = res;
741 *SM_VA_ARG(ap, float *) = res;
750 sm_clrevent(evt); /* undo our timeout */
751 return nassigned ? nassigned : -1;
754 sm_clrevent(evt); /* undo our timeout */
759 ** SM_SCCL -- sequenced character comparison list
761 ** Fill in the given table from the scanset at the given format
762 ** (just after `['). Return a pointer to the character past the
763 ** closing `]'. The table has a 1 wherever characters should be
764 ** considered part of the scanset.
767 ** tab -- array flagging "active" char's to match (returned)
768 ** fmt -- character list (within "[]")
773 static unsigned char *
776 register unsigned char *fmt;
778 register int c, n, v;
780 /* first `clear' the whole table */
781 c = *fmt++; /* first char hat => negated scanset */
784 v = 1; /* default => accept */
785 c = *fmt++; /* get new first char */
788 v = 0; /* default => reject */
790 /* should probably use memset here */
791 for (n = 0; n < 256; n++)
794 return fmt - 1; /* format ended before closing ] */
797 ** Now set the entries corresponding to the actual scanset
798 ** to the opposite of the above.
800 ** The first character may be ']' (or '-') without being special;
801 ** the last character may be '-'.
807 tab[c] = v; /* take character c */
809 n = *fmt++; /* and examine the next */
813 case 0: /* format ended too soon */
818 ** A scanset of the form
820 ** is defined as `the digit 0, the digit 1,
821 ** the character +, the character -', but
822 ** the effect of a scanset such as
824 ** is implementation defined. The V7 Unix
825 ** scanf treats `a-z' as `the letters a through
826 ** z', but treats `a-a' as `the letter a, the
827 ** character -, and the letter a'.
829 ** For compatibility, the `-' is not considerd
830 ** to define a range if the character following
831 ** it is either a close bracket (required by ANSI)
832 ** or is not numerically greater than the character
833 ** we just stored in the table (c).
837 if (n == ']' || n < c)
840 break; /* resume the for(;;) */
845 /* fill in the range */
848 #if 1 /* XXX another disgusting compatibility hack */
851 ** Alas, the V7 Unix scanf also treats formats
852 ** such as [a-c-e] as `the letters a through e'.
853 ** This too is permitted by the standard....
866 case ']': /* end of scanset */
869 default: /* just another character */