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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
18 SM_IDSTR(id
, "@(#)$Id: vfscanf.c,v 1.54 2006/10/12 22:03:52 ca Exp $")
25 #include <sm/varargs.h>
26 #include <sm/config.h>
28 #include <sm/signal.h>
30 #include <sm/string.h>
33 #define BUF 513 /* Maximum length of numeric string. */
35 /* Flags used during conversion. */
36 #define LONG 0x01 /* l: long or double */
37 #define SHORT 0x04 /* h: short */
38 #define QUAD 0x08 /* q: quad (same as ll) */
39 #define SUPPRESS 0x10 /* suppress assignment */
40 #define POINTER 0x20 /* weird %p pointer (`fake hex') */
41 #define NOSKIP 0x40 /* do not skip blanks */
44 ** The following are used in numeric conversions only:
45 ** SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
46 ** SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
49 #define SIGNOK 0x080 /* +/- is (still) legal */
50 #define NDIGITS 0x100 /* no digits detected */
52 #define DPTOK 0x200 /* (float) decimal point is still legal */
53 #define EXPOK 0x400 /* (float) exponent (e+3, etc) still legal */
55 #define PFXOK 0x200 /* 0x prefix is (still) legal */
56 #define NZDIGITS 0x400 /* no zero digits detected */
58 /* Conversion types. */
59 #define CT_CHAR 0 /* %c conversion */
60 #define CT_CCL 1 /* %[...] conversion */
61 #define CT_STRING 2 /* %s conversion */
62 #define CT_INT 3 /* integer, i.e., strtoll or strtoull */
63 #define CT_FLOAT 4 /* floating, i.e., strtod */
65 static void scanalrm
__P((int));
66 static unsigned char *sm_sccl
__P((char *, unsigned char *));
67 static jmp_buf ScanTimeOut
;
70 ** SCANALRM -- handler when timeout activated for sm_io_vfscanf()
72 ** Returns flow of control to where setjmp(ScanTimeOut) was set.
81 ** returns flow of control to setjmp(ScanTimeOut).
83 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
84 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
93 longjmp(ScanTimeOut
, 1);
97 ** SM_VFSCANF -- convert input into data units
100 ** fp -- file pointer for input data
101 ** timeout -- time intvl allowed to complete (milliseconds)
102 ** fmt0 -- format for finding data units
103 ** ap -- vectors for memory location for storing data units
106 ** Success: number of data units assigned
107 ** Failure: SM_IO_EOF
111 sm_vfscanf(fp
, timeout
, fmt0
, ap
)
112 register SM_FILE_T
*fp
;
113 int SM_NONVOLATILE timeout
;
115 va_list SM_NONVOLATILE ap
;
117 register unsigned char *SM_NONVOLATILE fmt
= (unsigned char *) fmt0
;
118 register int c
; /* character from format, or conversion */
119 register size_t width
; /* field width, or 0 */
120 register char *p
; /* points into all kinds of strings */
121 register int n
; /* handy integer */
122 register int flags
; /* flags as defined above */
123 register char *p0
; /* saves original value of p when necessary */
124 int nassigned
; /* number of fields assigned */
125 int nread
; /* number of characters consumed from fp */
126 int base
; /* base argument to strtoll/strtoull */
128 /* conversion function (strtoll/strtoull) */
129 ULONGLONG_T (*ccfn
) __P((const char *, char **, int));
130 char ccltab
[256]; /* character class table for %[...] */
131 char buf
[BUF
]; /* buffer for numeric conversions */
132 SM_EVENT
*evt
= NULL
;
134 /* `basefix' is used to avoid `if' tests in the integer scanner */
135 static short basefix
[17] =
136 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
138 if (timeout
== SM_TIME_DEFAULT
)
139 timeout
= fp
->f_timeout
;
140 if (timeout
== SM_TIME_IMMEDIATE
)
143 ** Filling the buffer will take time and we are wanted to
144 ** return immediately. So...
151 if (timeout
!= SM_TIME_FOREVER
)
153 if (setjmp(ScanTimeOut
) != 0)
159 evt
= sm_seteventm(timeout
, scanalrm
, 0);
164 base
= 0; /* XXX just to keep gcc happy */
165 ccfn
= NULL
; /* XXX just to keep gcc happy */
172 sm_clrevent(evt
); /* undo our timeout */
177 while ((fp
->f_r
> 0 || sm_refill(fp
, SM_TIME_FOREVER
)
180 nread
++, fp
->f_r
--, fp
->f_p
++;
189 ** switch on the format. continue if done;
190 ** break once format type is derived.
198 if (fp
->f_r
<= 0 && sm_refill(fp
, SM_TIME_FOREVER
))
202 fp
->f_r
--, fp
->f_p
++;
227 case '0': case '1': case '2': case '3': case '4':
228 case '5': case '6': case '7': case '8': case '9':
229 width
= width
* 10 + c
- '0';
234 ** Those marked `compat' are for 4.[123]BSD compatibility.
236 ** (According to ANSI, E and X formats are supposed
237 ** to the same as e and x. Sorry about that.)
240 case 'D': /* compat */
245 ccfn
= (ULONGLONG_T (*)())sm_strtoll
;
251 ccfn
= (ULONGLONG_T (*)())sm_strtoll
;
255 case 'O': /* compat */
272 flags
|= PFXOK
; /* enable 0x prefixing */
291 fmt
= sm_sccl(ccltab
, fmt
);
301 case 'p': /* pointer format is like hex */
302 flags
|= POINTER
| PFXOK
;
309 if (flags
& SUPPRESS
) /* ??? */
312 *SM_VA_ARG(ap
, short *) = nread
;
313 else if (flags
& LONG
)
314 *SM_VA_ARG(ap
, long *) = nread
;
316 *SM_VA_ARG(ap
, int *) = nread
;
319 /* Disgusting backwards compatibility hacks. XXX */
320 case '\0': /* compat */
322 sm_clrevent(evt
); /* undo our timeout */
325 default: /* compat */
329 ccfn
= (ULONGLONG_T (*)()) sm_strtoll
;
334 /* We have a conversion that requires input. */
335 if (fp
->f_r
<= 0 && sm_refill(fp
, SM_TIME_FOREVER
))
339 ** Consume leading white space, except for formats
340 ** that suppress this.
343 if ((flags
& NOSKIP
) == 0)
345 while (isspace(*fp
->f_p
))
350 else if (sm_refill(fp
, SM_TIME_FOREVER
))
354 ** Note that there is at least one character in
355 ** the buffer, so conversions that do not set NOSKIP
356 ** can no longer result in an input failure.
360 /* Do the conversion. */
364 /* scan arbitrary characters (sets NOSKIP) */
367 if (flags
& SUPPRESS
)
372 if ((size_t) (n
= fp
->f_r
) < width
)
399 r
= sm_io_read(fp
, SM_TIME_FOREVER
,
400 (void *) SM_VA_ARG(ap
, char *),
410 /* scan a (nonempty) character class (sets NOSKIP) */
412 width
= (size_t)~0; /* `infinity' */
414 /* take only those things in the class */
415 if (flags
& SUPPRESS
)
418 while (ccltab
[*fp
->f_p
] != '\0')
420 n
++, fp
->f_r
--, fp
->f_p
++;
424 sm_refill(fp
, SM_TIME_FOREVER
))
426 if (n
== 0) /* XXX how? */
436 p0
= p
= SM_VA_ARG(ap
, char *);
437 while (ccltab
[*fp
->f_p
] != '\0')
444 sm_refill(fp
, SM_TIME_FOREVER
))
461 /* like CCL, but zero-length string OK, & no NOSKIP */
464 if (flags
& SUPPRESS
)
467 while (!isspace(*fp
->f_p
))
469 n
++, fp
->f_r
--, fp
->f_p
++;
473 sm_refill(fp
, SM_TIME_FOREVER
))
480 p0
= p
= SM_VA_ARG(ap
, char *);
481 while (!isspace(*fp
->f_p
))
488 sm_refill(fp
, SM_TIME_FOREVER
))
498 /* scan an integer as if by strtoll/strtoull */
499 #if SM_CONF_BROKEN_SIZE_T
500 if (width
== 0 || width
> sizeof(buf
) - 1)
501 width
= sizeof(buf
) - 1;
502 #else /* SM_CONF_BROKEN_SIZE_T */
503 /* size_t is unsigned, hence this optimisation */
504 if (--width
> sizeof(buf
) - 2)
505 width
= sizeof(buf
) - 2;
507 #endif /* SM_CONF_BROKEN_SIZE_T */
508 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
509 for (p
= buf
; width
> 0; width
--)
514 ** Switch on the character; `goto ok'
515 ** if we accept it as a part of number.
522 ** The digit 0 is always legal, but is
523 ** special. For %i conversions, if no
524 ** digits (zero or nonzero) have been
525 ** scanned (only signs), we will have
526 ** base==0. In that case, we should set
527 ** it to 8 and enable 0x prefixing.
528 ** Also, if we have not scanned zero digits
529 ** before this, do not turn off prefixing
530 ** (someone else will turn it off if we
531 ** have scanned any nonzero digits).
540 if (flags
& NZDIGITS
)
541 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
543 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
546 /* 1 through 7 always legal */
547 case '1': case '2': case '3':
548 case '4': case '5': case '6': case '7':
549 base
= basefix
[base
];
550 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
553 /* digits 8 and 9 ok iff decimal or hex */
555 base
= basefix
[base
];
557 break; /* not legal here */
558 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
561 /* letters ok iff hex */
562 case 'A': case 'B': case 'C':
563 case 'D': case 'E': case 'F':
564 case 'a': case 'b': case 'c':
565 case 'd': case 'e': case 'f':
567 /* no need to fix base here */
569 break; /* not legal here */
570 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
573 /* sign ok only as first character */
582 /* x ok iff flag still set & 2nd char */
584 if (flags
& PFXOK
&& p
== buf
+ 1)
586 base
= 16; /* if %i */
594 ** If we got here, c is not a legal character
595 ** for a number. Stop accumulating digits.
600 /* c is legal: store it and look at the next. */
604 else if (sm_refill(fp
, SM_TIME_FOREVER
))
605 break; /* SM_IO_EOF */
609 ** If we had only a sign, it is no good; push
610 ** back the sign. If the number ends in `x',
611 ** it was [sign] '0' 'x', so push back the x
612 ** and treat it as [sign] '0'.
618 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
,
619 *(unsigned char *)--p
);
622 c
= ((unsigned char *)p
)[-1];
623 if (c
== 'x' || c
== 'X')
626 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
, c
);
628 if ((flags
& SUPPRESS
) == 0)
633 res
= (*ccfn
)(buf
, (char **)NULL
, base
);
635 *SM_VA_ARG(ap
, void **) =
637 else if (flags
& QUAD
)
638 *SM_VA_ARG(ap
, LONGLONG_T
*) = res
;
639 else if (flags
& LONG
)
640 *SM_VA_ARG(ap
, long *) = res
;
641 else if (flags
& SHORT
)
642 *SM_VA_ARG(ap
, short *) = res
;
644 *SM_VA_ARG(ap
, int *) = res
;
651 /* scan a floating point number as if by strtod */
652 if (width
== 0 || width
> sizeof(buf
) - 1)
653 width
= sizeof(buf
) - 1;
654 flags
|= SIGNOK
| NDIGITS
| DPTOK
| EXPOK
;
655 for (p
= buf
; width
; width
--)
660 ** This code mimicks the integer conversion
661 ** code, but is much simpler.
667 case '0': case '1': case '2': case '3':
668 case '4': case '5': case '6': case '7':
670 flags
&= ~(SIGNOK
| NDIGITS
);
683 flags
&= ~(SIGNOK
| DPTOK
);
689 /* no exponent without some digits */
690 if ((flags
&(NDIGITS
|EXPOK
)) == EXPOK
)
693 (flags
& ~(EXPOK
|DPTOK
)) |
704 else if (sm_refill(fp
, SM_TIME_FOREVER
))
705 break; /* SM_IO_EOF */
709 ** If no digits, might be missing exponent digits
710 ** (just give back the exponent) or might be missing
711 ** regular digits, but had sign and/or decimal point.
718 /* no digits at all */
720 (void) sm_io_ungetc(fp
,
722 *(unsigned char *)--p
);
726 /* just a bad exponent (e and maybe sign) */
727 c
= *(unsigned char *) --p
;
728 if (c
!= 'e' && c
!= 'E')
730 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
,
732 c
= *(unsigned char *)--p
;
734 (void) sm_io_ungetc(fp
, SM_TIME_DEFAULT
, c
);
736 if ((flags
& SUPPRESS
) == 0)
741 res
= strtod(buf
, (char **) NULL
);
743 *SM_VA_ARG(ap
, double *) = res
;
745 *SM_VA_ARG(ap
, float *) = res
;
754 sm_clrevent(evt
); /* undo our timeout */
755 return nassigned
? nassigned
: -1;
758 sm_clrevent(evt
); /* undo our timeout */
763 ** SM_SCCL -- sequenced character comparison list
765 ** Fill in the given table from the scanset at the given format
766 ** (just after `['). Return a pointer to the character past the
767 ** closing `]'. The table has a 1 wherever characters should be
768 ** considered part of the scanset.
771 ** tab -- array flagging "active" char's to match (returned)
772 ** fmt -- character list (within "[]")
777 static unsigned char *
780 register unsigned char *fmt
;
782 register int c
, n
, v
;
784 /* first `clear' the whole table */
785 c
= *fmt
++; /* first char hat => negated scanset */
788 v
= 1; /* default => accept */
789 c
= *fmt
++; /* get new first char */
792 v
= 0; /* default => reject */
794 /* should probably use memset here */
795 for (n
= 0; n
< 256; n
++)
798 return fmt
- 1; /* format ended before closing ] */
801 ** Now set the entries corresponding to the actual scanset
802 ** to the opposite of the above.
804 ** The first character may be ']' (or '-') without being special;
805 ** the last character may be '-'.
811 tab
[c
] = v
; /* take character c */
813 n
= *fmt
++; /* and examine the next */
817 case 0: /* format ended too soon */
822 ** A scanset of the form
824 ** is defined as `the digit 0, the digit 1,
825 ** the character +, the character -', but
826 ** the effect of a scanset such as
828 ** is implementation defined. The V7 Unix
829 ** scanf treats `a-z' as `the letters a through
830 ** z', but treats `a-a' as `the letter a, the
831 ** character -, and the letter a'.
833 ** For compatibility, the `-' is not considerd
834 ** to define a range if the character following
835 ** it is either a close bracket (required by ANSI)
836 ** or is not numerically greater than the character
837 ** we just stored in the table (c).
841 if (n
== ']' || n
< c
)
844 break; /* resume the for(;;) */
849 /* fill in the range */
852 #if 1 /* XXX another disgusting compatibility hack */
855 ** Alas, the V7 Unix scanf also treats formats
856 ** such as [a-c-e] as `the letters a through e'.
857 ** This too is permitted by the standard....
870 case ']': /* end of scanset */
873 default: /* just another character */