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.54 2006/10/12 22:03:52 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 */
126 /* conversion function (strtoll/strtoull) */
127 ULONGLONG_T (*ccfn
) __P((const char *, char **, int));
128 char ccltab
[256]; /* character class table for %[...] */
129 char buf
[BUF
]; /* buffer for numeric conversions */
130 SM_EVENT
*evt
= NULL
;
132 /* `basefix' is used to avoid `if' tests in the integer scanner */
133 static short basefix
[17] =
134 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
136 if (timeout
== SM_TIME_DEFAULT
)
137 timeout
= fp
->f_timeout
;
138 if (timeout
== SM_TIME_IMMEDIATE
)
141 ** Filling the buffer will take time and we are wanted to
142 ** return immediately. So...
149 if (timeout
!= SM_TIME_FOREVER
)
151 if (setjmp(ScanTimeOut
) != 0)
157 evt
= sm_seteventm(timeout
, scanalrm
, 0);
162 base
= 0; /* XXX just to keep gcc happy */
163 ccfn
= NULL
; /* XXX just to keep gcc happy */
170 sm_clrevent(evt
); /* undo our timeout */
175 while ((fp
->f_r
> 0 || sm_refill(fp
, SM_TIME_FOREVER
)
178 nread
++, fp
->f_r
--, fp
->f_p
++;
187 ** switch on the format. continue if done;
188 ** break once format type is derived.
196 if (fp
->f_r
<= 0 && sm_refill(fp
, SM_TIME_FOREVER
))
200 fp
->f_r
--, fp
->f_p
++;
225 case '0': case '1': case '2': case '3': case '4':
226 case '5': case '6': case '7': case '8': case '9':
227 width
= width
* 10 + c
- '0';
232 ** Those marked `compat' are for 4.[123]BSD compatibility.
234 ** (According to ANSI, E and X formats are supposed
235 ** to the same as e and x. Sorry about that.)
238 case 'D': /* compat */
243 ccfn
= (ULONGLONG_T (*)())sm_strtoll
;
249 ccfn
= (ULONGLONG_T (*)())sm_strtoll
;
253 case 'O': /* compat */
270 flags
|= PFXOK
; /* enable 0x prefixing */
289 fmt
= sm_sccl(ccltab
, fmt
);
299 case 'p': /* pointer format is like hex */
300 flags
|= POINTER
| PFXOK
;
307 if (flags
& SUPPRESS
) /* ??? */
310 *SM_VA_ARG(ap
, short *) = nread
;
311 else if (flags
& LONG
)
312 *SM_VA_ARG(ap
, long *) = nread
;
314 *SM_VA_ARG(ap
, int *) = nread
;
317 /* Disgusting backwards compatibility hacks. XXX */
318 case '\0': /* compat */
320 sm_clrevent(evt
); /* undo our timeout */
323 default: /* compat */
327 ccfn
= (ULONGLONG_T (*)()) sm_strtoll
;
332 /* We have a conversion that requires input. */
333 if (fp
->f_r
<= 0 && sm_refill(fp
, SM_TIME_FOREVER
))
337 ** Consume leading white space, except for formats
338 ** that suppress this.
341 if ((flags
& NOSKIP
) == 0)
343 while (isspace(*fp
->f_p
))
348 else if (sm_refill(fp
, SM_TIME_FOREVER
))
352 ** Note that there is at least one character in
353 ** the buffer, so conversions that do not set NOSKIP
354 ** can no longer result in an input failure.
358 /* Do the conversion. */
362 /* scan arbitrary characters (sets NOSKIP) */
365 if (flags
& SUPPRESS
)
370 if ((size_t) (n
= fp
->f_r
) < width
)
397 r
= sm_io_read(fp
, SM_TIME_FOREVER
,
398 (void *) SM_VA_ARG(ap
, char *),
408 /* scan a (nonempty) character class (sets NOSKIP) */
410 width
= (size_t)~0; /* `infinity' */
412 /* take only those things in the class */
413 if (flags
& SUPPRESS
)
416 while (ccltab
[*fp
->f_p
] != '\0')
418 n
++, fp
->f_r
--, fp
->f_p
++;
422 sm_refill(fp
, SM_TIME_FOREVER
))
424 if (n
== 0) /* XXX how? */
434 p0
= p
= SM_VA_ARG(ap
, char *);
435 while (ccltab
[*fp
->f_p
] != '\0')
442 sm_refill(fp
, SM_TIME_FOREVER
))
459 /* like CCL, but zero-length string OK, & no NOSKIP */
462 if (flags
& SUPPRESS
)
465 while (!isspace(*fp
->f_p
))
467 n
++, fp
->f_r
--, fp
->f_p
++;
471 sm_refill(fp
, SM_TIME_FOREVER
))
478 p0
= p
= SM_VA_ARG(ap
, char *);
479 while (!isspace(*fp
->f_p
))
486 sm_refill(fp
, SM_TIME_FOREVER
))
496 /* scan an integer as if by strtoll/strtoull */
497 #if SM_CONF_BROKEN_SIZE_T
498 if (width
== 0 || width
> sizeof(buf
) - 1)
499 width
= sizeof(buf
) - 1;
500 #else /* SM_CONF_BROKEN_SIZE_T */
501 /* size_t is unsigned, hence this optimisation */
502 if (--width
> sizeof(buf
) - 2)
503 width
= sizeof(buf
) - 2;
505 #endif /* SM_CONF_BROKEN_SIZE_T */
506 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
507 for (p
= buf
; width
> 0; width
--)
512 ** Switch on the character; `goto ok'
513 ** if we accept it as a part of number.
520 ** The digit 0 is always legal, but is
521 ** special. For %i conversions, if no
522 ** digits (zero or nonzero) have been
523 ** scanned (only signs), we will have
524 ** base==0. In that case, we should set
525 ** it to 8 and enable 0x prefixing.
526 ** Also, if we have not scanned zero digits
527 ** before this, do not turn off prefixing
528 ** (someone else will turn it off if we
529 ** have scanned any nonzero digits).
538 if (flags
& NZDIGITS
)
539 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
541 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
544 /* 1 through 7 always legal */
545 case '1': case '2': case '3':
546 case '4': case '5': case '6': case '7':
547 base
= basefix
[base
];
548 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
551 /* digits 8 and 9 ok iff decimal or hex */
553 base
= basefix
[base
];
555 break; /* not legal here */
556 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
559 /* letters ok iff hex */
560 case 'A': case 'B': case 'C':
561 case 'D': case 'E': case 'F':
562 case 'a': case 'b': case 'c':
563 case 'd': case 'e': case 'f':
565 /* no need to fix base here */
567 break; /* not legal here */
568 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
571 /* sign ok only as first character */
580 /* x ok iff flag still set & 2nd char */
582 if (flags
& PFXOK
&& p
== buf
+ 1)
584 base
= 16; /* if %i */
592 ** If we got here, c is not a legal character
593 ** for a number. Stop accumulating digits.
598 /* c is legal: store it and look at the next. */
602 else if (sm_refill(fp
, SM_TIME_FOREVER
))
603 break; /* SM_IO_EOF */
607 ** If we had only a sign, it is no good; push
608 ** back the sign. If the number ends in `x',
609 ** it was [sign] '0' 'x', so push back the x
610 ** and treat it as [sign] '0'.
616 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
,
617 *(unsigned char *)--p
);
620 c
= ((unsigned char *)p
)[-1];
621 if (c
== 'x' || c
== 'X')
624 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
, c
);
626 if ((flags
& SUPPRESS
) == 0)
631 res
= (*ccfn
)(buf
, (char **)NULL
, base
);
633 *SM_VA_ARG(ap
, void **) =
635 else if (flags
& QUAD
)
636 *SM_VA_ARG(ap
, LONGLONG_T
*) = res
;
637 else if (flags
& LONG
)
638 *SM_VA_ARG(ap
, long *) = res
;
639 else if (flags
& SHORT
)
640 *SM_VA_ARG(ap
, short *) = res
;
642 *SM_VA_ARG(ap
, int *) = res
;
649 /* scan a floating point number as if by strtod */
650 if (width
== 0 || width
> sizeof(buf
) - 1)
651 width
= sizeof(buf
) - 1;
652 flags
|= SIGNOK
| NDIGITS
| DPTOK
| EXPOK
;
653 for (p
= buf
; width
; width
--)
658 ** This code mimicks the integer conversion
659 ** code, but is much simpler.
665 case '0': case '1': case '2': case '3':
666 case '4': case '5': case '6': case '7':
668 flags
&= ~(SIGNOK
| NDIGITS
);
681 flags
&= ~(SIGNOK
| DPTOK
);
687 /* no exponent without some digits */
688 if ((flags
&(NDIGITS
|EXPOK
)) == EXPOK
)
691 (flags
& ~(EXPOK
|DPTOK
)) |
702 else if (sm_refill(fp
, SM_TIME_FOREVER
))
703 break; /* SM_IO_EOF */
707 ** If no digits, might be missing exponent digits
708 ** (just give back the exponent) or might be missing
709 ** regular digits, but had sign and/or decimal point.
716 /* no digits at all */
718 (void) sm_io_ungetc(fp
,
720 *(unsigned char *)--p
);
724 /* just a bad exponent (e and maybe sign) */
725 c
= *(unsigned char *) --p
;
726 if (c
!= 'e' && c
!= 'E')
728 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
,
730 c
= *(unsigned char *)--p
;
732 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
, c
);
734 if ((flags
& SUPPRESS
) == 0)
739 res
= strtod(buf
, (char **) NULL
);
741 *SM_VA_ARG(ap
, double *) = res
;
743 *SM_VA_ARG(ap
, float *) = res
;
752 sm_clrevent(evt
); /* undo our timeout */
753 return nassigned
? nassigned
: -1;
756 sm_clrevent(evt
); /* undo our timeout */
761 ** SM_SCCL -- sequenced character comparison list
763 ** Fill in the given table from the scanset at the given format
764 ** (just after `['). Return a pointer to the character past the
765 ** closing `]'. The table has a 1 wherever characters should be
766 ** considered part of the scanset.
769 ** tab -- array flagging "active" char's to match (returned)
770 ** fmt -- character list (within "[]")
775 static unsigned char *
778 register unsigned char *fmt
;
780 register int c
, n
, v
;
782 /* first `clear' the whole table */
783 c
= *fmt
++; /* first char hat => negated scanset */
786 v
= 1; /* default => accept */
787 c
= *fmt
++; /* get new first char */
790 v
= 0; /* default => reject */
792 /* should probably use memset here */
793 for (n
= 0; n
< 256; n
++)
796 return fmt
- 1; /* format ended before closing ] */
799 ** Now set the entries corresponding to the actual scanset
800 ** to the opposite of the above.
802 ** The first character may be ']' (or '-') without being special;
803 ** the last character may be '-'.
809 tab
[c
] = v
; /* take character c */
811 n
= *fmt
++; /* and examine the next */
815 case 0: /* format ended too soon */
820 ** A scanset of the form
822 ** is defined as `the digit 0, the digit 1,
823 ** the character +, the character -', but
824 ** the effect of a scanset such as
826 ** is implementation defined. The V7 Unix
827 ** scanf treats `a-z' as `the letters a through
828 ** z', but treats `a-a' as `the letter a, the
829 ** character -, and the letter a'.
831 ** For compatibility, the `-' is not considerd
832 ** to define a range if the character following
833 ** it is either a close bracket (required by ANSI)
834 ** or is not numerically greater than the character
835 ** we just stored in the table (c).
839 if (n
== ']' || n
< c
)
842 break; /* resume the for(;;) */
847 /* fill in the range */
850 #if 1 /* XXX another disgusting compatibility hack */
853 ** Alas, the V7 Unix scanf also treats formats
854 ** such as [a-c-e] as `the letters a through e'.
855 ** This too is permitted by the standard....
868 case ']': /* end of scanset */
871 default: /* just another character */