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