xref: /illumos-gate/usr/src/cmd/sendmail/libsm/vfscanf.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001, 2004 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
57c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
87c478bd9Sstevel@tonic-gate  * Chris Torek.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
117c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
127c478bd9Sstevel@tonic-gate  * the sendmail distribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include <sm/gen.h>
16*058561cbSjbeck SM_IDSTR(id, "@(#)$Id: vfscanf.c,v 1.54 2006/10/12 22:03:52 ca Exp $")
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate #include <stdlib.h>
207c478bd9Sstevel@tonic-gate #include <errno.h>
217c478bd9Sstevel@tonic-gate #include <setjmp.h>
2249218d4fSjbeck #include <sm/time.h>
237c478bd9Sstevel@tonic-gate #include <sm/varargs.h>
247c478bd9Sstevel@tonic-gate #include <sm/config.h>
257c478bd9Sstevel@tonic-gate #include <sm/io.h>
267c478bd9Sstevel@tonic-gate #include <sm/signal.h>
277c478bd9Sstevel@tonic-gate #include <sm/clock.h>
287c478bd9Sstevel@tonic-gate #include <sm/string.h>
297c478bd9Sstevel@tonic-gate #include "local.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #define BUF		513	/* Maximum length of numeric string. */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* Flags used during conversion. */
347c478bd9Sstevel@tonic-gate #define LONG		0x01	/* l: long or double */
357c478bd9Sstevel@tonic-gate #define SHORT		0x04	/* h: short */
367c478bd9Sstevel@tonic-gate #define QUAD		0x08	/* q: quad (same as ll) */
377c478bd9Sstevel@tonic-gate #define SUPPRESS	0x10	/* suppress assignment */
387c478bd9Sstevel@tonic-gate #define POINTER		0x20	/* weird %p pointer (`fake hex') */
397c478bd9Sstevel@tonic-gate #define NOSKIP		0x40	/* do not skip blanks */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate **  The following are used in numeric conversions only:
437c478bd9Sstevel@tonic-gate **  SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
447c478bd9Sstevel@tonic-gate **  SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
457c478bd9Sstevel@tonic-gate */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define SIGNOK		0x080	/* +/- is (still) legal */
487c478bd9Sstevel@tonic-gate #define NDIGITS		0x100	/* no digits detected */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define DPTOK		0x200	/* (float) decimal point is still legal */
517c478bd9Sstevel@tonic-gate #define EXPOK		0x400	/* (float) exponent (e+3, etc) still legal */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define PFXOK		0x200	/* 0x prefix is (still) legal */
547c478bd9Sstevel@tonic-gate #define NZDIGITS	0x400	/* no zero digits detected */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* Conversion types. */
577c478bd9Sstevel@tonic-gate #define CT_CHAR		0	/* %c conversion */
587c478bd9Sstevel@tonic-gate #define CT_CCL		1	/* %[...] conversion */
597c478bd9Sstevel@tonic-gate #define CT_STRING	2	/* %s conversion */
607c478bd9Sstevel@tonic-gate #define CT_INT		3	/* integer, i.e., strtoll or strtoull */
617c478bd9Sstevel@tonic-gate #define CT_FLOAT	4	/* floating, i.e., strtod */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static void		scanalrm __P((int));
647c478bd9Sstevel@tonic-gate static unsigned char	*sm_sccl __P((char *, unsigned char *));
657c478bd9Sstevel@tonic-gate static jmp_buf		ScanTimeOut;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate **  SCANALRM -- handler when timeout activated for sm_io_vfscanf()
697c478bd9Sstevel@tonic-gate **
707c478bd9Sstevel@tonic-gate **  Returns flow of control to where setjmp(ScanTimeOut) was set.
717c478bd9Sstevel@tonic-gate **
727c478bd9Sstevel@tonic-gate **	Parameters:
737c478bd9Sstevel@tonic-gate **		sig -- unused
747c478bd9Sstevel@tonic-gate **
757c478bd9Sstevel@tonic-gate **	Returns:
767c478bd9Sstevel@tonic-gate **		does not return
777c478bd9Sstevel@tonic-gate **
787c478bd9Sstevel@tonic-gate **	Side Effects:
797c478bd9Sstevel@tonic-gate **		returns flow of control to setjmp(ScanTimeOut).
807c478bd9Sstevel@tonic-gate **
817c478bd9Sstevel@tonic-gate **	NOTE:	THIS CAN BE CALLED FROM A SIGNAL HANDLER.  DO NOT ADD
827c478bd9Sstevel@tonic-gate **		ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
837c478bd9Sstevel@tonic-gate **		DOING.
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
877c478bd9Sstevel@tonic-gate static void
scanalrm(sig)887c478bd9Sstevel@tonic-gate scanalrm(sig)
897c478bd9Sstevel@tonic-gate 	int sig;
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	longjmp(ScanTimeOut, 1);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate **  SM_VFSCANF -- convert input into data units
967c478bd9Sstevel@tonic-gate **
977c478bd9Sstevel@tonic-gate **	Parameters:
987c478bd9Sstevel@tonic-gate **		fp -- file pointer for input data
997c478bd9Sstevel@tonic-gate **		timeout -- time intvl allowed to complete (milliseconds)
1007c478bd9Sstevel@tonic-gate **		fmt0 -- format for finding data units
1017c478bd9Sstevel@tonic-gate **		ap -- vectors for memory location for storing data units
1027c478bd9Sstevel@tonic-gate **
1037c478bd9Sstevel@tonic-gate **	Results:
1047c478bd9Sstevel@tonic-gate **		Success: number of data units assigned
1057c478bd9Sstevel@tonic-gate **		Failure: SM_IO_EOF
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate int
sm_vfscanf(fp,timeout,fmt0,ap)1097c478bd9Sstevel@tonic-gate sm_vfscanf(fp, timeout, fmt0, ap)
1107c478bd9Sstevel@tonic-gate 	register SM_FILE_T *fp;
1117c478bd9Sstevel@tonic-gate 	int SM_NONVOLATILE timeout;
1127c478bd9Sstevel@tonic-gate 	char const *fmt0;
1137c478bd9Sstevel@tonic-gate 	va_list SM_NONVOLATILE ap;
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	register unsigned char *SM_NONVOLATILE fmt = (unsigned char *) fmt0;
1167c478bd9Sstevel@tonic-gate 	register int c;		/* character from format, or conversion */
1177c478bd9Sstevel@tonic-gate 	register size_t width;	/* field width, or 0 */
1187c478bd9Sstevel@tonic-gate 	register char *p;	/* points into all kinds of strings */
1197c478bd9Sstevel@tonic-gate 	register int n;		/* handy integer */
1207c478bd9Sstevel@tonic-gate 	register int flags;	/* flags as defined above */
1217c478bd9Sstevel@tonic-gate 	register char *p0;	/* saves original value of p when necessary */
1227c478bd9Sstevel@tonic-gate 	int nassigned;		/* number of fields assigned */
1237c478bd9Sstevel@tonic-gate 	int nread;		/* number of characters consumed from fp */
1247c478bd9Sstevel@tonic-gate 	int base;		/* base argument to strtoll/strtoull */
125*058561cbSjbeck 
126*058561cbSjbeck 	/* conversion function (strtoll/strtoull) */
127*058561cbSjbeck 	ULONGLONG_T (*ccfn) __P((const char *, char **, int));
1287c478bd9Sstevel@tonic-gate 	char ccltab[256];	/* character class table for %[...] */
1297c478bd9Sstevel@tonic-gate 	char buf[BUF];		/* buffer for numeric conversions */
1307c478bd9Sstevel@tonic-gate 	SM_EVENT *evt = NULL;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/* `basefix' is used to avoid `if' tests in the integer scanner */
1337c478bd9Sstevel@tonic-gate 	static short basefix[17] =
1347c478bd9Sstevel@tonic-gate 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (timeout == SM_TIME_DEFAULT)
1377c478bd9Sstevel@tonic-gate 		timeout = fp->f_timeout;
1387c478bd9Sstevel@tonic-gate 	if (timeout == SM_TIME_IMMEDIATE)
1397c478bd9Sstevel@tonic-gate 	{
1407c478bd9Sstevel@tonic-gate 		/*
1417c478bd9Sstevel@tonic-gate 		**  Filling the buffer will take time and we are wanted to
1427c478bd9Sstevel@tonic-gate 		**  return immediately. So...
1437c478bd9Sstevel@tonic-gate 		*/
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		errno = EAGAIN;
1467c478bd9Sstevel@tonic-gate 		return SM_IO_EOF;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (timeout != SM_TIME_FOREVER)
1507c478bd9Sstevel@tonic-gate 	{
1517c478bd9Sstevel@tonic-gate 		if (setjmp(ScanTimeOut) != 0)
1527c478bd9Sstevel@tonic-gate 		{
1537c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
1547c478bd9Sstevel@tonic-gate 			return SM_IO_EOF;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		evt = sm_seteventm(timeout, scanalrm, 0);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	nassigned = 0;
1617c478bd9Sstevel@tonic-gate 	nread = 0;
1627c478bd9Sstevel@tonic-gate 	base = 0;		/* XXX just to keep gcc happy */
1637c478bd9Sstevel@tonic-gate 	ccfn = NULL;		/* XXX just to keep gcc happy */
1647c478bd9Sstevel@tonic-gate 	for (;;)
1657c478bd9Sstevel@tonic-gate 	{
1667c478bd9Sstevel@tonic-gate 		c = *fmt++;
1677c478bd9Sstevel@tonic-gate 		if (c == 0)
1687c478bd9Sstevel@tonic-gate 		{
1697c478bd9Sstevel@tonic-gate 			if (evt != NULL)
1707c478bd9Sstevel@tonic-gate 				sm_clrevent(evt); /*  undo our timeout */
1717c478bd9Sstevel@tonic-gate 			return nassigned;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 		if (isspace(c))
1747c478bd9Sstevel@tonic-gate 		{
1757c478bd9Sstevel@tonic-gate 			while ((fp->f_r > 0 || sm_refill(fp, SM_TIME_FOREVER)
1767c478bd9Sstevel@tonic-gate 						== 0) &&
1777c478bd9Sstevel@tonic-gate 			    isspace(*fp->f_p))
1787c478bd9Sstevel@tonic-gate 				nread++, fp->f_r--, fp->f_p++;
1797c478bd9Sstevel@tonic-gate 			continue;
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 		if (c != '%')
1827c478bd9Sstevel@tonic-gate 			goto literal;
1837c478bd9Sstevel@tonic-gate 		width = 0;
1847c478bd9Sstevel@tonic-gate 		flags = 0;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		/*
1877c478bd9Sstevel@tonic-gate 		**  switch on the format.  continue if done;
1887c478bd9Sstevel@tonic-gate 		**  break once format type is derived.
1897c478bd9Sstevel@tonic-gate 		*/
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate again:		c = *fmt++;
1927c478bd9Sstevel@tonic-gate 		switch (c)
1937c478bd9Sstevel@tonic-gate 		{
1947c478bd9Sstevel@tonic-gate 		  case '%':
1957c478bd9Sstevel@tonic-gate literal:
1967c478bd9Sstevel@tonic-gate 			if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
1977c478bd9Sstevel@tonic-gate 				goto input_failure;
1987c478bd9Sstevel@tonic-gate 			if (*fp->f_p != c)
1997c478bd9Sstevel@tonic-gate 				goto match_failure;
2007c478bd9Sstevel@tonic-gate 			fp->f_r--, fp->f_p++;
2017c478bd9Sstevel@tonic-gate 			nread++;
2027c478bd9Sstevel@tonic-gate 			continue;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		  case '*':
2057c478bd9Sstevel@tonic-gate 			flags |= SUPPRESS;
2067c478bd9Sstevel@tonic-gate 			goto again;
2077c478bd9Sstevel@tonic-gate 		  case 'h':
2087c478bd9Sstevel@tonic-gate 			flags |= SHORT;
2097c478bd9Sstevel@tonic-gate 			goto again;
2107c478bd9Sstevel@tonic-gate 		  case 'l':
2117c478bd9Sstevel@tonic-gate 			if (*fmt == 'l')
2127c478bd9Sstevel@tonic-gate 			{
2137c478bd9Sstevel@tonic-gate 				fmt++;
2147c478bd9Sstevel@tonic-gate 				flags |= QUAD;
2157c478bd9Sstevel@tonic-gate 			}
2167c478bd9Sstevel@tonic-gate 			else
2177c478bd9Sstevel@tonic-gate 			{
2187c478bd9Sstevel@tonic-gate 				flags |= LONG;
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 			goto again;
2217c478bd9Sstevel@tonic-gate 		  case 'q':
2227c478bd9Sstevel@tonic-gate 			flags |= QUAD;
2237c478bd9Sstevel@tonic-gate 			goto again;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		  case '0': case '1': case '2': case '3': case '4':
2267c478bd9Sstevel@tonic-gate 		  case '5': case '6': case '7': case '8': case '9':
2277c478bd9Sstevel@tonic-gate 			width = width * 10 + c - '0';
2287c478bd9Sstevel@tonic-gate 			goto again;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		/*
2317c478bd9Sstevel@tonic-gate 		**  Conversions.
2327c478bd9Sstevel@tonic-gate 		**  Those marked `compat' are for 4.[123]BSD compatibility.
2337c478bd9Sstevel@tonic-gate 		**
2347c478bd9Sstevel@tonic-gate 		**  (According to ANSI, E and X formats are supposed
2357c478bd9Sstevel@tonic-gate 		**  to the same as e and x.  Sorry about that.)
2367c478bd9Sstevel@tonic-gate 		*/
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		  case 'D':	/* compat */
2397c478bd9Sstevel@tonic-gate 			flags |= LONG;
2407c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
2417c478bd9Sstevel@tonic-gate 		  case 'd':
2427c478bd9Sstevel@tonic-gate 			c = CT_INT;
2437c478bd9Sstevel@tonic-gate 			ccfn = (ULONGLONG_T (*)())sm_strtoll;
2447c478bd9Sstevel@tonic-gate 			base = 10;
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		  case 'i':
2487c478bd9Sstevel@tonic-gate 			c = CT_INT;
2497c478bd9Sstevel@tonic-gate 			ccfn = (ULONGLONG_T (*)())sm_strtoll;
2507c478bd9Sstevel@tonic-gate 			base = 0;
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		  case 'O':	/* compat */
2547c478bd9Sstevel@tonic-gate 			flags |= LONG;
2557c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
2567c478bd9Sstevel@tonic-gate 		  case 'o':
2577c478bd9Sstevel@tonic-gate 			c = CT_INT;
2587c478bd9Sstevel@tonic-gate 			ccfn = sm_strtoull;
2597c478bd9Sstevel@tonic-gate 			base = 8;
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		  case 'u':
2637c478bd9Sstevel@tonic-gate 			c = CT_INT;
2647c478bd9Sstevel@tonic-gate 			ccfn = sm_strtoull;
2657c478bd9Sstevel@tonic-gate 			base = 10;
2667c478bd9Sstevel@tonic-gate 			break;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		  case 'X':
2697c478bd9Sstevel@tonic-gate 		  case 'x':
2707c478bd9Sstevel@tonic-gate 			flags |= PFXOK;	/* enable 0x prefixing */
2717c478bd9Sstevel@tonic-gate 			c = CT_INT;
2727c478bd9Sstevel@tonic-gate 			ccfn = sm_strtoull;
2737c478bd9Sstevel@tonic-gate 			base = 16;
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		  case 'E':
2777c478bd9Sstevel@tonic-gate 		  case 'G':
2787c478bd9Sstevel@tonic-gate 		  case 'e':
2797c478bd9Sstevel@tonic-gate 		  case 'f':
2807c478bd9Sstevel@tonic-gate 		  case 'g':
2817c478bd9Sstevel@tonic-gate 			c = CT_FLOAT;
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		  case 's':
2857c478bd9Sstevel@tonic-gate 			c = CT_STRING;
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		  case '[':
2897c478bd9Sstevel@tonic-gate 			fmt = sm_sccl(ccltab, fmt);
2907c478bd9Sstevel@tonic-gate 			flags |= NOSKIP;
2917c478bd9Sstevel@tonic-gate 			c = CT_CCL;
2927c478bd9Sstevel@tonic-gate 			break;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		  case 'c':
2957c478bd9Sstevel@tonic-gate 			flags |= NOSKIP;
2967c478bd9Sstevel@tonic-gate 			c = CT_CHAR;
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		  case 'p':	/* pointer format is like hex */
3007c478bd9Sstevel@tonic-gate 			flags |= POINTER | PFXOK;
3017c478bd9Sstevel@tonic-gate 			c = CT_INT;
3027c478bd9Sstevel@tonic-gate 			ccfn = sm_strtoull;
3037c478bd9Sstevel@tonic-gate 			base = 16;
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		  case 'n':
3077c478bd9Sstevel@tonic-gate 			if (flags & SUPPRESS)	/* ??? */
3087c478bd9Sstevel@tonic-gate 				continue;
3097c478bd9Sstevel@tonic-gate 			if (flags & SHORT)
3107c478bd9Sstevel@tonic-gate 				*SM_VA_ARG(ap, short *) = nread;
3117c478bd9Sstevel@tonic-gate 			else if (flags & LONG)
3127c478bd9Sstevel@tonic-gate 				*SM_VA_ARG(ap, long *) = nread;
3137c478bd9Sstevel@tonic-gate 			else
3147c478bd9Sstevel@tonic-gate 				*SM_VA_ARG(ap, int *) = nread;
3157c478bd9Sstevel@tonic-gate 			continue;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		/* Disgusting backwards compatibility hacks.	XXX */
3187c478bd9Sstevel@tonic-gate 		  case '\0':	/* compat */
3197c478bd9Sstevel@tonic-gate 			if (evt != NULL)
3207c478bd9Sstevel@tonic-gate 				sm_clrevent(evt); /*  undo our timeout */
3217c478bd9Sstevel@tonic-gate 			return SM_IO_EOF;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		  default:	/* compat */
3247c478bd9Sstevel@tonic-gate 			if (isupper(c))
3257c478bd9Sstevel@tonic-gate 				flags |= LONG;
3267c478bd9Sstevel@tonic-gate 			c = CT_INT;
3277c478bd9Sstevel@tonic-gate 			ccfn = (ULONGLONG_T (*)()) sm_strtoll;
3287c478bd9Sstevel@tonic-gate 			base = 10;
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		/* We have a conversion that requires input. */
3337c478bd9Sstevel@tonic-gate 		if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
3347c478bd9Sstevel@tonic-gate 			goto input_failure;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		/*
3377c478bd9Sstevel@tonic-gate 		**  Consume leading white space, except for formats
3387c478bd9Sstevel@tonic-gate 		**  that suppress this.
3397c478bd9Sstevel@tonic-gate 		*/
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		if ((flags & NOSKIP) == 0)
3427c478bd9Sstevel@tonic-gate 		{
3437c478bd9Sstevel@tonic-gate 			while (isspace(*fp->f_p))
3447c478bd9Sstevel@tonic-gate 			{
3457c478bd9Sstevel@tonic-gate 				nread++;
3467c478bd9Sstevel@tonic-gate 				if (--fp->f_r > 0)
3477c478bd9Sstevel@tonic-gate 					fp->f_p++;
3487c478bd9Sstevel@tonic-gate 				else if (sm_refill(fp, SM_TIME_FOREVER))
3497c478bd9Sstevel@tonic-gate 					goto input_failure;
3507c478bd9Sstevel@tonic-gate 			}
3517c478bd9Sstevel@tonic-gate 			/*
3527c478bd9Sstevel@tonic-gate 			**  Note that there is at least one character in
3537c478bd9Sstevel@tonic-gate 			**  the buffer, so conversions that do not set NOSKIP
3547c478bd9Sstevel@tonic-gate 			**  can no longer result in an input failure.
3557c478bd9Sstevel@tonic-gate 			*/
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		/* Do the conversion. */
3597c478bd9Sstevel@tonic-gate 		switch (c)
3607c478bd9Sstevel@tonic-gate 		{
3617c478bd9Sstevel@tonic-gate 		  case CT_CHAR:
3627c478bd9Sstevel@tonic-gate 			/* scan arbitrary characters (sets NOSKIP) */
3637c478bd9Sstevel@tonic-gate 			if (width == 0)
3647c478bd9Sstevel@tonic-gate 				width = 1;
3657c478bd9Sstevel@tonic-gate 			if (flags & SUPPRESS)
3667c478bd9Sstevel@tonic-gate 			{
3677c478bd9Sstevel@tonic-gate 				size_t sum = 0;
3687c478bd9Sstevel@tonic-gate 				for (;;)
3697c478bd9Sstevel@tonic-gate 				{
3707c478bd9Sstevel@tonic-gate 					if ((size_t) (n = fp->f_r) < width)
3717c478bd9Sstevel@tonic-gate 					{
3727c478bd9Sstevel@tonic-gate 						sum += n;
3737c478bd9Sstevel@tonic-gate 						width -= n;
3747c478bd9Sstevel@tonic-gate 						fp->f_p += n;
3757c478bd9Sstevel@tonic-gate 						if (sm_refill(fp,
3767c478bd9Sstevel@tonic-gate 							      SM_TIME_FOREVER))
3777c478bd9Sstevel@tonic-gate 						{
3787c478bd9Sstevel@tonic-gate 							if (sum == 0)
3797c478bd9Sstevel@tonic-gate 								goto input_failure;
3807c478bd9Sstevel@tonic-gate 							break;
3817c478bd9Sstevel@tonic-gate 						}
3827c478bd9Sstevel@tonic-gate 					}
3837c478bd9Sstevel@tonic-gate 					else
3847c478bd9Sstevel@tonic-gate 					{
3857c478bd9Sstevel@tonic-gate 						sum += width;
3867c478bd9Sstevel@tonic-gate 						fp->f_r -= width;
3877c478bd9Sstevel@tonic-gate 						fp->f_p += width;
3887c478bd9Sstevel@tonic-gate 						break;
3897c478bd9Sstevel@tonic-gate 					}
3907c478bd9Sstevel@tonic-gate 				}
3917c478bd9Sstevel@tonic-gate 				nread += sum;
3927c478bd9Sstevel@tonic-gate 			}
3937c478bd9Sstevel@tonic-gate 			else
3947c478bd9Sstevel@tonic-gate 			{
3957c478bd9Sstevel@tonic-gate 				size_t r;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 				r = sm_io_read(fp, SM_TIME_FOREVER,
3987c478bd9Sstevel@tonic-gate 						(void *) SM_VA_ARG(ap, char *),
3997c478bd9Sstevel@tonic-gate 						width);
4007c478bd9Sstevel@tonic-gate 				if (r == 0)
4017c478bd9Sstevel@tonic-gate 					goto input_failure;
4027c478bd9Sstevel@tonic-gate 				nread += r;
4037c478bd9Sstevel@tonic-gate 				nassigned++;
4047c478bd9Sstevel@tonic-gate 			}
4057c478bd9Sstevel@tonic-gate 			break;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		  case CT_CCL:
4087c478bd9Sstevel@tonic-gate 			/* scan a (nonempty) character class (sets NOSKIP) */
4097c478bd9Sstevel@tonic-gate 			if (width == 0)
4107c478bd9Sstevel@tonic-gate 				width = (size_t)~0;	/* `infinity' */
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			/* take only those things in the class */
4137c478bd9Sstevel@tonic-gate 			if (flags & SUPPRESS)
4147c478bd9Sstevel@tonic-gate 			{
4157c478bd9Sstevel@tonic-gate 				n = 0;
4167c478bd9Sstevel@tonic-gate 				while (ccltab[*fp->f_p] != '\0')
4177c478bd9Sstevel@tonic-gate 				{
4187c478bd9Sstevel@tonic-gate 					n++, fp->f_r--, fp->f_p++;
4197c478bd9Sstevel@tonic-gate 					if (--width == 0)
4207c478bd9Sstevel@tonic-gate 						break;
4217c478bd9Sstevel@tonic-gate 					if (fp->f_r <= 0 &&
4227c478bd9Sstevel@tonic-gate 					    sm_refill(fp, SM_TIME_FOREVER))
4237c478bd9Sstevel@tonic-gate 					{
4247c478bd9Sstevel@tonic-gate 						if (n == 0) /* XXX how? */
4257c478bd9Sstevel@tonic-gate 							goto input_failure;
4267c478bd9Sstevel@tonic-gate 						break;
4277c478bd9Sstevel@tonic-gate 					}
4287c478bd9Sstevel@tonic-gate 				}
4297c478bd9Sstevel@tonic-gate 				if (n == 0)
4307c478bd9Sstevel@tonic-gate 					goto match_failure;
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			else
4337c478bd9Sstevel@tonic-gate 			{
4347c478bd9Sstevel@tonic-gate 				p0 = p = SM_VA_ARG(ap, char *);
4357c478bd9Sstevel@tonic-gate 				while (ccltab[*fp->f_p] != '\0')
4367c478bd9Sstevel@tonic-gate 				{
4377c478bd9Sstevel@tonic-gate 					fp->f_r--;
4387c478bd9Sstevel@tonic-gate 					*p++ = *fp->f_p++;
4397c478bd9Sstevel@tonic-gate 					if (--width == 0)
4407c478bd9Sstevel@tonic-gate 						break;
4417c478bd9Sstevel@tonic-gate 					if (fp->f_r <= 0 &&
4427c478bd9Sstevel@tonic-gate 					    sm_refill(fp, SM_TIME_FOREVER))
4437c478bd9Sstevel@tonic-gate 					{
4447c478bd9Sstevel@tonic-gate 						if (p == p0)
4457c478bd9Sstevel@tonic-gate 							goto input_failure;
4467c478bd9Sstevel@tonic-gate 						break;
4477c478bd9Sstevel@tonic-gate 					}
4487c478bd9Sstevel@tonic-gate 				}
4497c478bd9Sstevel@tonic-gate 				n = p - p0;
4507c478bd9Sstevel@tonic-gate 				if (n == 0)
4517c478bd9Sstevel@tonic-gate 					goto match_failure;
4527c478bd9Sstevel@tonic-gate 				*p = 0;
4537c478bd9Sstevel@tonic-gate 				nassigned++;
4547c478bd9Sstevel@tonic-gate 			}
4557c478bd9Sstevel@tonic-gate 			nread += n;
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		  case CT_STRING:
4597c478bd9Sstevel@tonic-gate 			/* like CCL, but zero-length string OK, & no NOSKIP */
4607c478bd9Sstevel@tonic-gate 			if (width == 0)
4617c478bd9Sstevel@tonic-gate 				width = (size_t)~0;
4627c478bd9Sstevel@tonic-gate 			if (flags & SUPPRESS)
4637c478bd9Sstevel@tonic-gate 			{
4647c478bd9Sstevel@tonic-gate 				n = 0;
4657c478bd9Sstevel@tonic-gate 				while (!isspace(*fp->f_p))
4667c478bd9Sstevel@tonic-gate 				{
4677c478bd9Sstevel@tonic-gate 					n++, fp->f_r--, fp->f_p++;
4687c478bd9Sstevel@tonic-gate 					if (--width == 0)
4697c478bd9Sstevel@tonic-gate 						break;
4707c478bd9Sstevel@tonic-gate 					if (fp->f_r <= 0 &&
4717c478bd9Sstevel@tonic-gate 					    sm_refill(fp, SM_TIME_FOREVER))
4727c478bd9Sstevel@tonic-gate 						break;
4737c478bd9Sstevel@tonic-gate 				}
4747c478bd9Sstevel@tonic-gate 				nread += n;
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 			else
4777c478bd9Sstevel@tonic-gate 			{
4787c478bd9Sstevel@tonic-gate 				p0 = p = SM_VA_ARG(ap, char *);
4797c478bd9Sstevel@tonic-gate 				while (!isspace(*fp->f_p))
4807c478bd9Sstevel@tonic-gate 				{
4817c478bd9Sstevel@tonic-gate 					fp->f_r--;
4827c478bd9Sstevel@tonic-gate 					*p++ = *fp->f_p++;
4837c478bd9Sstevel@tonic-gate 					if (--width == 0)
4847c478bd9Sstevel@tonic-gate 						break;
4857c478bd9Sstevel@tonic-gate 					if (fp->f_r <= 0 &&
4867c478bd9Sstevel@tonic-gate 					    sm_refill(fp, SM_TIME_FOREVER))
4877c478bd9Sstevel@tonic-gate 						break;
4887c478bd9Sstevel@tonic-gate 				}
4897c478bd9Sstevel@tonic-gate 				*p = 0;
4907c478bd9Sstevel@tonic-gate 				nread += p - p0;
4917c478bd9Sstevel@tonic-gate 				nassigned++;
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 			continue;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		  case CT_INT:
4967c478bd9Sstevel@tonic-gate 			/* scan an integer as if by strtoll/strtoull */
4977c478bd9Sstevel@tonic-gate #if SM_CONF_BROKEN_SIZE_T
4987c478bd9Sstevel@tonic-gate 			if (width == 0 || width > sizeof(buf) - 1)
4997c478bd9Sstevel@tonic-gate 				width = sizeof(buf) - 1;
5007c478bd9Sstevel@tonic-gate #else /* SM_CONF_BROKEN_SIZE_T */
5017c478bd9Sstevel@tonic-gate 			/* size_t is unsigned, hence this optimisation */
5027c478bd9Sstevel@tonic-gate 			if (--width > sizeof(buf) - 2)
5037c478bd9Sstevel@tonic-gate 				width = sizeof(buf) - 2;
5047c478bd9Sstevel@tonic-gate 			width++;
5057c478bd9Sstevel@tonic-gate #endif /* SM_CONF_BROKEN_SIZE_T */
5067c478bd9Sstevel@tonic-gate 			flags |= SIGNOK | NDIGITS | NZDIGITS;
5077c478bd9Sstevel@tonic-gate 			for (p = buf; width > 0; width--)
5087c478bd9Sstevel@tonic-gate 			{
5097c478bd9Sstevel@tonic-gate 				c = *fp->f_p;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 				/*
5127c478bd9Sstevel@tonic-gate 				**  Switch on the character; `goto ok'
5137c478bd9Sstevel@tonic-gate 				**  if we accept it as a part of number.
5147c478bd9Sstevel@tonic-gate 				*/
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 				switch (c)
5177c478bd9Sstevel@tonic-gate 				{
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 				/*
5207c478bd9Sstevel@tonic-gate 				**  The digit 0 is always legal, but is
5217c478bd9Sstevel@tonic-gate 				**  special.  For %i conversions, if no
5227c478bd9Sstevel@tonic-gate 				**  digits (zero or nonzero) have been
5237c478bd9Sstevel@tonic-gate 				**  scanned (only signs), we will have
5247c478bd9Sstevel@tonic-gate 				**  base==0.  In that case, we should set
5257c478bd9Sstevel@tonic-gate 				**  it to 8 and enable 0x prefixing.
5267c478bd9Sstevel@tonic-gate 				**  Also, if we have not scanned zero digits
5277c478bd9Sstevel@tonic-gate 				**  before this, do not turn off prefixing
5287c478bd9Sstevel@tonic-gate 				**  (someone else will turn it off if we
5297c478bd9Sstevel@tonic-gate 				**  have scanned any nonzero digits).
5307c478bd9Sstevel@tonic-gate 				*/
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 				  case '0':
5337c478bd9Sstevel@tonic-gate 					if (base == 0)
5347c478bd9Sstevel@tonic-gate 					{
5357c478bd9Sstevel@tonic-gate 						base = 8;
5367c478bd9Sstevel@tonic-gate 						flags |= PFXOK;
5377c478bd9Sstevel@tonic-gate 					}
5387c478bd9Sstevel@tonic-gate 					if (flags & NZDIGITS)
5397c478bd9Sstevel@tonic-gate 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
5407c478bd9Sstevel@tonic-gate 					else
5417c478bd9Sstevel@tonic-gate 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
5427c478bd9Sstevel@tonic-gate 					goto ok;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 				/* 1 through 7 always legal */
5457c478bd9Sstevel@tonic-gate 				  case '1': case '2': case '3':
5467c478bd9Sstevel@tonic-gate 				  case '4': case '5': case '6': case '7':
5477c478bd9Sstevel@tonic-gate 					base = basefix[base];
5487c478bd9Sstevel@tonic-gate 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
5497c478bd9Sstevel@tonic-gate 					goto ok;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 				/* digits 8 and 9 ok iff decimal or hex */
5527c478bd9Sstevel@tonic-gate 				  case '8': case '9':
5537c478bd9Sstevel@tonic-gate 					base = basefix[base];
5547c478bd9Sstevel@tonic-gate 					if (base <= 8)
5557c478bd9Sstevel@tonic-gate 						break;	/* not legal here */
5567c478bd9Sstevel@tonic-gate 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
5577c478bd9Sstevel@tonic-gate 					goto ok;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 				/* letters ok iff hex */
5607c478bd9Sstevel@tonic-gate 				  case 'A': case 'B': case 'C':
5617c478bd9Sstevel@tonic-gate 				  case 'D': case 'E': case 'F':
5627c478bd9Sstevel@tonic-gate 				  case 'a': case 'b': case 'c':
5637c478bd9Sstevel@tonic-gate 				  case 'd': case 'e': case 'f':
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 					/* no need to fix base here */
5667c478bd9Sstevel@tonic-gate 					if (base <= 10)
5677c478bd9Sstevel@tonic-gate 						break;	/* not legal here */
5687c478bd9Sstevel@tonic-gate 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
5697c478bd9Sstevel@tonic-gate 					goto ok;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 				/* sign ok only as first character */
5727c478bd9Sstevel@tonic-gate 				  case '+': case '-':
5737c478bd9Sstevel@tonic-gate 					if (flags & SIGNOK)
5747c478bd9Sstevel@tonic-gate 					{
5757c478bd9Sstevel@tonic-gate 						flags &= ~SIGNOK;
5767c478bd9Sstevel@tonic-gate 						goto ok;
5777c478bd9Sstevel@tonic-gate 					}
5787c478bd9Sstevel@tonic-gate 					break;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 				/* x ok iff flag still set & 2nd char */
5817c478bd9Sstevel@tonic-gate 				  case 'x': case 'X':
5827c478bd9Sstevel@tonic-gate 					if (flags & PFXOK && p == buf + 1)
5837c478bd9Sstevel@tonic-gate 					{
5847c478bd9Sstevel@tonic-gate 						base = 16;	/* if %i */
5857c478bd9Sstevel@tonic-gate 						flags &= ~PFXOK;
5867c478bd9Sstevel@tonic-gate 						goto ok;
5877c478bd9Sstevel@tonic-gate 					}
5887c478bd9Sstevel@tonic-gate 					break;
5897c478bd9Sstevel@tonic-gate 				}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 				/*
5927c478bd9Sstevel@tonic-gate 				**  If we got here, c is not a legal character
5937c478bd9Sstevel@tonic-gate 				**  for a number.  Stop accumulating digits.
5947c478bd9Sstevel@tonic-gate 				*/
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 				break;
5977c478bd9Sstevel@tonic-gate 		ok:
5987c478bd9Sstevel@tonic-gate 				/* c is legal: store it and look at the next. */
5997c478bd9Sstevel@tonic-gate 				*p++ = c;
6007c478bd9Sstevel@tonic-gate 				if (--fp->f_r > 0)
6017c478bd9Sstevel@tonic-gate 					fp->f_p++;
6027c478bd9Sstevel@tonic-gate 				else if (sm_refill(fp, SM_TIME_FOREVER))
6037c478bd9Sstevel@tonic-gate 					break;		/* SM_IO_EOF */
6047c478bd9Sstevel@tonic-gate 			}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			/*
6077c478bd9Sstevel@tonic-gate 			**  If we had only a sign, it is no good; push
6087c478bd9Sstevel@tonic-gate 			**  back the sign.  If the number ends in `x',
6097c478bd9Sstevel@tonic-gate 			**  it was [sign] '0' 'x', so push back the x
6107c478bd9Sstevel@tonic-gate 			**  and treat it as [sign] '0'.
6117c478bd9Sstevel@tonic-gate 			*/
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 			if (flags & NDIGITS)
6147c478bd9Sstevel@tonic-gate 			{
6157c478bd9Sstevel@tonic-gate 				if (p > buf)
6167c478bd9Sstevel@tonic-gate 					(void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
6177c478bd9Sstevel@tonic-gate 							    *(unsigned char *)--p);
6187c478bd9Sstevel@tonic-gate 				goto match_failure;
6197c478bd9Sstevel@tonic-gate 			}
6207c478bd9Sstevel@tonic-gate 			c = ((unsigned char *)p)[-1];
6217c478bd9Sstevel@tonic-gate 			if (c == 'x' || c == 'X')
6227c478bd9Sstevel@tonic-gate 			{
6237c478bd9Sstevel@tonic-gate 				--p;
6247c478bd9Sstevel@tonic-gate 				(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 			if ((flags & SUPPRESS) == 0)
6277c478bd9Sstevel@tonic-gate 			{
6287c478bd9Sstevel@tonic-gate 				ULONGLONG_T res;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 				*p = 0;
6317c478bd9Sstevel@tonic-gate 				res = (*ccfn)(buf, (char **)NULL, base);
6327c478bd9Sstevel@tonic-gate 				if (flags & POINTER)
6337c478bd9Sstevel@tonic-gate 					*SM_VA_ARG(ap, void **) =
6347c478bd9Sstevel@tonic-gate 					    (void *)(long) res;
6357c478bd9Sstevel@tonic-gate 				else if (flags & QUAD)
6367c478bd9Sstevel@tonic-gate 					*SM_VA_ARG(ap, LONGLONG_T *) = res;
6377c478bd9Sstevel@tonic-gate 				else if (flags & LONG)
6387c478bd9Sstevel@tonic-gate 					*SM_VA_ARG(ap, long *) = res;
6397c478bd9Sstevel@tonic-gate 				else if (flags & SHORT)
6407c478bd9Sstevel@tonic-gate 					*SM_VA_ARG(ap, short *) = res;
6417c478bd9Sstevel@tonic-gate 				else
6427c478bd9Sstevel@tonic-gate 					*SM_VA_ARG(ap, int *) = res;
6437c478bd9Sstevel@tonic-gate 				nassigned++;
6447c478bd9Sstevel@tonic-gate 			}
6457c478bd9Sstevel@tonic-gate 			nread += p - buf;
6467c478bd9Sstevel@tonic-gate 			break;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		  case CT_FLOAT:
6497c478bd9Sstevel@tonic-gate 			/* scan a floating point number as if by strtod */
6507c478bd9Sstevel@tonic-gate 			if (width == 0 || width > sizeof(buf) - 1)
6517c478bd9Sstevel@tonic-gate 				width = sizeof(buf) - 1;
6527c478bd9Sstevel@tonic-gate 			flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
6537c478bd9Sstevel@tonic-gate 			for (p = buf; width; width--)
6547c478bd9Sstevel@tonic-gate 			{
6557c478bd9Sstevel@tonic-gate 				c = *fp->f_p;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 				/*
6587c478bd9Sstevel@tonic-gate 				**  This code mimicks the integer conversion
6597c478bd9Sstevel@tonic-gate 				**  code, but is much simpler.
6607c478bd9Sstevel@tonic-gate 				*/
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 				switch (c)
6637c478bd9Sstevel@tonic-gate 				{
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 				  case '0': case '1': case '2': case '3':
6667c478bd9Sstevel@tonic-gate 				  case '4': case '5': case '6': case '7':
6677c478bd9Sstevel@tonic-gate 				  case '8': case '9':
6687c478bd9Sstevel@tonic-gate 					flags &= ~(SIGNOK | NDIGITS);
6697c478bd9Sstevel@tonic-gate 					goto fok;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 				  case '+': case '-':
6727c478bd9Sstevel@tonic-gate 					if (flags & SIGNOK)
6737c478bd9Sstevel@tonic-gate 					{
6747c478bd9Sstevel@tonic-gate 						flags &= ~SIGNOK;
6757c478bd9Sstevel@tonic-gate 						goto fok;
6767c478bd9Sstevel@tonic-gate 					}
6777c478bd9Sstevel@tonic-gate 					break;
6787c478bd9Sstevel@tonic-gate 				  case '.':
6797c478bd9Sstevel@tonic-gate 					if (flags & DPTOK)
6807c478bd9Sstevel@tonic-gate 					{
6817c478bd9Sstevel@tonic-gate 						flags &= ~(SIGNOK | DPTOK);
6827c478bd9Sstevel@tonic-gate 						goto fok;
6837c478bd9Sstevel@tonic-gate 					}
6847c478bd9Sstevel@tonic-gate 					break;
6857c478bd9Sstevel@tonic-gate 				  case 'e': case 'E':
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 					/* no exponent without some digits */
6887c478bd9Sstevel@tonic-gate 					if ((flags&(NDIGITS|EXPOK)) == EXPOK)
6897c478bd9Sstevel@tonic-gate 					{
6907c478bd9Sstevel@tonic-gate 						flags =
6917c478bd9Sstevel@tonic-gate 						    (flags & ~(EXPOK|DPTOK)) |
6927c478bd9Sstevel@tonic-gate 						    SIGNOK | NDIGITS;
6937c478bd9Sstevel@tonic-gate 						goto fok;
6947c478bd9Sstevel@tonic-gate 					}
6957c478bd9Sstevel@tonic-gate 					break;
6967c478bd9Sstevel@tonic-gate 				}
6977c478bd9Sstevel@tonic-gate 				break;
6987c478bd9Sstevel@tonic-gate 		fok:
6997c478bd9Sstevel@tonic-gate 				*p++ = c;
7007c478bd9Sstevel@tonic-gate 				if (--fp->f_r > 0)
7017c478bd9Sstevel@tonic-gate 					fp->f_p++;
7027c478bd9Sstevel@tonic-gate 				else if (sm_refill(fp, SM_TIME_FOREVER))
7037c478bd9Sstevel@tonic-gate 					break;	/* SM_IO_EOF */
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 			/*
7077c478bd9Sstevel@tonic-gate 			**  If no digits, might be missing exponent digits
7087c478bd9Sstevel@tonic-gate 			**  (just give back the exponent) or might be missing
7097c478bd9Sstevel@tonic-gate 			**  regular digits, but had sign and/or decimal point.
7107c478bd9Sstevel@tonic-gate 			*/
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 			if (flags & NDIGITS)
7137c478bd9Sstevel@tonic-gate 			{
7147c478bd9Sstevel@tonic-gate 				if (flags & EXPOK)
7157c478bd9Sstevel@tonic-gate 				{
7167c478bd9Sstevel@tonic-gate 					/* no digits at all */
7177c478bd9Sstevel@tonic-gate 					while (p > buf)
7187c478bd9Sstevel@tonic-gate 						(void) sm_io_ungetc(fp,
7197c478bd9Sstevel@tonic-gate 							     SM_TIME_DEFAULT,
7207c478bd9Sstevel@tonic-gate 							     *(unsigned char *)--p);
7217c478bd9Sstevel@tonic-gate 					goto match_failure;
7227c478bd9Sstevel@tonic-gate 				}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 				/* just a bad exponent (e and maybe sign) */
7257c478bd9Sstevel@tonic-gate 				c = *(unsigned char *) --p;
7267c478bd9Sstevel@tonic-gate 				if (c != 'e' && c != 'E')
7277c478bd9Sstevel@tonic-gate 				{
7287c478bd9Sstevel@tonic-gate 					(void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
7297c478bd9Sstevel@tonic-gate 							    c); /* sign */
7307c478bd9Sstevel@tonic-gate 					c = *(unsigned char *)--p;
7317c478bd9Sstevel@tonic-gate 				}
7327c478bd9Sstevel@tonic-gate 				(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
7337c478bd9Sstevel@tonic-gate 			}
7347c478bd9Sstevel@tonic-gate 			if ((flags & SUPPRESS) == 0)
7357c478bd9Sstevel@tonic-gate 			{
7367c478bd9Sstevel@tonic-gate 				double res;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 				*p = 0;
7397c478bd9Sstevel@tonic-gate 				res = strtod(buf, (char **) NULL);
7407c478bd9Sstevel@tonic-gate 				if (flags & LONG)
7417c478bd9Sstevel@tonic-gate 					*SM_VA_ARG(ap, double *) = res;
7427c478bd9Sstevel@tonic-gate 				else
7437c478bd9Sstevel@tonic-gate 					*SM_VA_ARG(ap, float *) = res;
7447c478bd9Sstevel@tonic-gate 				nassigned++;
7457c478bd9Sstevel@tonic-gate 			}
7467c478bd9Sstevel@tonic-gate 			nread += p - buf;
7477c478bd9Sstevel@tonic-gate 			break;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate input_failure:
7517c478bd9Sstevel@tonic-gate 	if (evt != NULL)
7527c478bd9Sstevel@tonic-gate 		sm_clrevent(evt); /*  undo our timeout */
7537c478bd9Sstevel@tonic-gate 	return nassigned ? nassigned : -1;
7547c478bd9Sstevel@tonic-gate match_failure:
7557c478bd9Sstevel@tonic-gate 	if (evt != NULL)
7567c478bd9Sstevel@tonic-gate 		sm_clrevent(evt); /*  undo our timeout */
7577c478bd9Sstevel@tonic-gate 	return nassigned;
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate **  SM_SCCL -- sequenced character comparison list
7627c478bd9Sstevel@tonic-gate **
7637c478bd9Sstevel@tonic-gate **  Fill in the given table from the scanset at the given format
7647c478bd9Sstevel@tonic-gate **  (just after `[').  Return a pointer to the character past the
7657c478bd9Sstevel@tonic-gate **  closing `]'.  The table has a 1 wherever characters should be
7667c478bd9Sstevel@tonic-gate **  considered part of the scanset.
7677c478bd9Sstevel@tonic-gate **
7687c478bd9Sstevel@tonic-gate **	Parameters:
7697c478bd9Sstevel@tonic-gate **		tab -- array flagging "active" char's to match (returned)
7707c478bd9Sstevel@tonic-gate **		fmt -- character list (within "[]")
7717c478bd9Sstevel@tonic-gate **
7727c478bd9Sstevel@tonic-gate **	Results:
7737c478bd9Sstevel@tonic-gate */
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate static unsigned char *
sm_sccl(tab,fmt)7767c478bd9Sstevel@tonic-gate sm_sccl(tab, fmt)
7777c478bd9Sstevel@tonic-gate 	register char *tab;
7787c478bd9Sstevel@tonic-gate 	register unsigned char *fmt;
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	register int c, n, v;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/* first `clear' the whole table */
7837c478bd9Sstevel@tonic-gate 	c = *fmt++;		/* first char hat => negated scanset */
7847c478bd9Sstevel@tonic-gate 	if (c == '^')
7857c478bd9Sstevel@tonic-gate 	{
7867c478bd9Sstevel@tonic-gate 		v = 1;		/* default => accept */
7877c478bd9Sstevel@tonic-gate 		c = *fmt++;	/* get new first char */
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	else
7907c478bd9Sstevel@tonic-gate 		v = 0;		/* default => reject */
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/* should probably use memset here */
7937c478bd9Sstevel@tonic-gate 	for (n = 0; n < 256; n++)
7947c478bd9Sstevel@tonic-gate 		tab[n] = v;
7957c478bd9Sstevel@tonic-gate 	if (c == 0)
7967c478bd9Sstevel@tonic-gate 		return fmt - 1;	/* format ended before closing ] */
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	/*
7997c478bd9Sstevel@tonic-gate 	**  Now set the entries corresponding to the actual scanset
8007c478bd9Sstevel@tonic-gate 	**  to the opposite of the above.
8017c478bd9Sstevel@tonic-gate 	**
8027c478bd9Sstevel@tonic-gate 	**  The first character may be ']' (or '-') without being special;
8037c478bd9Sstevel@tonic-gate 	**  the last character may be '-'.
8047c478bd9Sstevel@tonic-gate 	*/
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	v = 1 - v;
8077c478bd9Sstevel@tonic-gate 	for (;;)
8087c478bd9Sstevel@tonic-gate 	{
8097c478bd9Sstevel@tonic-gate 		tab[c] = v;		/* take character c */
8107c478bd9Sstevel@tonic-gate doswitch:
8117c478bd9Sstevel@tonic-gate 		n = *fmt++;		/* and examine the next */
8127c478bd9Sstevel@tonic-gate 		switch (n)
8137c478bd9Sstevel@tonic-gate 		{
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		  case 0:			/* format ended too soon */
8167c478bd9Sstevel@tonic-gate 			return fmt - 1;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		  case '-':
8197c478bd9Sstevel@tonic-gate 			/*
8207c478bd9Sstevel@tonic-gate 			**  A scanset of the form
8217c478bd9Sstevel@tonic-gate 			**	[01+-]
8227c478bd9Sstevel@tonic-gate 			**  is defined as `the digit 0, the digit 1,
8237c478bd9Sstevel@tonic-gate 			**  the character +, the character -', but
8247c478bd9Sstevel@tonic-gate 			**  the effect of a scanset such as
8257c478bd9Sstevel@tonic-gate 			**	[a-zA-Z0-9]
8267c478bd9Sstevel@tonic-gate 			**  is implementation defined.  The V7 Unix
8277c478bd9Sstevel@tonic-gate 			**  scanf treats `a-z' as `the letters a through
8287c478bd9Sstevel@tonic-gate 			**  z', but treats `a-a' as `the letter a, the
8297c478bd9Sstevel@tonic-gate 			**  character -, and the letter a'.
8307c478bd9Sstevel@tonic-gate 			**
8317c478bd9Sstevel@tonic-gate 			**  For compatibility, the `-' is not considerd
8327c478bd9Sstevel@tonic-gate 			**  to define a range if the character following
8337c478bd9Sstevel@tonic-gate 			**  it is either a close bracket (required by ANSI)
8347c478bd9Sstevel@tonic-gate 			**  or is not numerically greater than the character
8357c478bd9Sstevel@tonic-gate 			**  we just stored in the table (c).
8367c478bd9Sstevel@tonic-gate 			*/
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 			n = *fmt;
8397c478bd9Sstevel@tonic-gate 			if (n == ']' || n < c)
8407c478bd9Sstevel@tonic-gate 			{
8417c478bd9Sstevel@tonic-gate 				c = '-';
8427c478bd9Sstevel@tonic-gate 				break;	/* resume the for(;;) */
8437c478bd9Sstevel@tonic-gate 			}
8447c478bd9Sstevel@tonic-gate 			fmt++;
8457c478bd9Sstevel@tonic-gate 			do
8467c478bd9Sstevel@tonic-gate 			{
8477c478bd9Sstevel@tonic-gate 				/* fill in the range */
8487c478bd9Sstevel@tonic-gate 				tab[++c] = v;
8497c478bd9Sstevel@tonic-gate 			} while (c < n);
8507c478bd9Sstevel@tonic-gate #if 1	/* XXX another disgusting compatibility hack */
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 			/*
8537c478bd9Sstevel@tonic-gate 			**  Alas, the V7 Unix scanf also treats formats
8547c478bd9Sstevel@tonic-gate 			**  such as [a-c-e] as `the letters a through e'.
8557c478bd9Sstevel@tonic-gate 			**  This too is permitted by the standard....
8567c478bd9Sstevel@tonic-gate 			*/
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 			goto doswitch;
8597c478bd9Sstevel@tonic-gate #else
8607c478bd9Sstevel@tonic-gate 			c = *fmt++;
8617c478bd9Sstevel@tonic-gate 			if (c == 0)
8627c478bd9Sstevel@tonic-gate 				return fmt - 1;
8637c478bd9Sstevel@tonic-gate 			if (c == ']')
8647c478bd9Sstevel@tonic-gate 				return fmt;
8657c478bd9Sstevel@tonic-gate 			break;
8667c478bd9Sstevel@tonic-gate #endif
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		  case ']':		/* end of scanset */
8697c478bd9Sstevel@tonic-gate 			return fmt;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		  default:		/* just another character */
8727c478bd9Sstevel@tonic-gate 			c = n;
8737c478bd9Sstevel@tonic-gate 			break;
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8777c478bd9Sstevel@tonic-gate }
878