Home | History | Annotate | Line # | Download | only in stdio
vfscanf.c revision 1.23
      1 /*	$NetBSD: vfscanf.c,v 1.23 1999/09/16 11:45:31 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Chris Torek.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)vfscanf.c	8.1 (Berkeley) 6/4/93";
     43 #else
     44 __RCSID("$NetBSD: vfscanf.c,v 1.23 1999/09/16 11:45:31 lukem Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 #include "namespace.h"
     49 
     50 #include <assert.h>
     51 #include <errno.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <ctype.h>
     55 #if __STDC__
     56 #include <stdarg.h>
     57 #else
     58 #include <varargs.h>
     59 #endif
     60 
     61 #include "local.h"
     62 
     63 #ifdef FLOATING_POINT
     64 #include "floatio.h"
     65 #endif
     66 
     67 #define	BUF		513	/* Maximum length of numeric string. */
     68 
     69 /*
     70  * Flags used during conversion.
     71  */
     72 #define	LONG		0x01	/* l: long or double */
     73 #define	LONGDBL		0x02	/* L: long double; unimplemented */
     74 #define	SHORT		0x04	/* h: short */
     75 #define QUAD		0x08	/* q: quad */
     76 #define	SUPPRESS	0x10	/* suppress assignment */
     77 #define	POINTER		0x20	/* weird %p pointer (`fake hex') */
     78 #define	NOSKIP		0x40	/* do not skip blanks */
     79 
     80 /*
     81  * The following are used in numeric conversions only:
     82  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
     83  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
     84  */
     85 #define	SIGNOK		0x080	/* +/- is (still) legal */
     86 #define	NDIGITS		0x100	/* no digits detected */
     87 
     88 #define	DPTOK		0x200	/* (float) decimal point is still legal */
     89 #define	EXPOK		0x400	/* (float) exponent (e+3, etc) still legal */
     90 
     91 #define	PFXOK		0x200	/* 0x prefix is (still) legal */
     92 #define	NZDIGITS	0x400	/* no zero digits detected */
     93 
     94 /*
     95  * Conversion types.
     96  */
     97 #define	CT_CHAR		0	/* %c conversion */
     98 #define	CT_CCL		1	/* %[...] conversion */
     99 #define	CT_STRING	2	/* %s conversion */
    100 #define	CT_INT		3	/* integer, i.e., strtoq or strtouq */
    101 #define	CT_FLOAT	4	/* floating, i.e., strtod */
    102 
    103 #define u_char unsigned char
    104 #define u_long unsigned long
    105 
    106 static const u_char *__sccl __P((char *, const u_char *));
    107 
    108 /*
    109  * vfscanf
    110  */
    111 int
    112 __svfscanf(fp, fmt0, ap)
    113 	FILE *fp;
    114 	const char *fmt0;
    115 	_BSD_VA_LIST_ ap;
    116 {
    117 	const u_char *fmt = (const u_char *)fmt0;
    118 	int c;		/* character from format, or conversion */
    119 	size_t width;	/* field width, or 0 */
    120 	char *p;	/* points into all kinds of strings */
    121 	int n;		/* handy integer */
    122 	int flags;	/* flags as defined above */
    123 	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 strtoq/strtouq */
    127 	u_quad_t (*ccfn) __P((const char *, char **, int));
    128 				/* conversion function (strtoq/strtouq) */
    129 	char ccltab[256];	/* character class table for %[...] */
    130 	char buf[BUF];		/* buffer for numeric conversions */
    131 
    132 	/* `basefix' is used to avoid `if' tests in the integer scanner */
    133 	static const short basefix[17] =
    134 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    135 
    136 	_DIAGASSERT(fp != NULL);
    137 	_DIAGASSERT(fmt0 != NULL);
    138 #ifdef _DIAGNOSTIC
    139 	if (fp == NULL) {
    140 		errno = EBADF;
    141 		return (EOF);
    142 	}
    143 	if (fmt0 == NULL) {
    144 		errno = EFAULT;
    145 		return (EOF);
    146 	}
    147 #endif
    148 
    149 	nassigned = 0;
    150 	nread = 0;
    151 	base = 0;		/* XXX just to keep gcc happy */
    152 	ccfn = NULL;		/* XXX just to keep gcc happy */
    153 	for (;;) {
    154 		c = *fmt++;
    155 		if (c == 0)
    156 			return (nassigned);
    157 		if (isspace(c)) {
    158 			while ((fp->_r > 0 || __srefill(fp) == 0) &&
    159 			    isspace(*fp->_p))
    160 				nread++, fp->_r--, fp->_p++;
    161 			continue;
    162 		}
    163 		if (c != '%')
    164 			goto literal;
    165 		width = 0;
    166 		flags = 0;
    167 		/*
    168 		 * switch on the format.  continue if done;
    169 		 * break once format type is derived.
    170 		 */
    171 again:		c = *fmt++;
    172 		switch (c) {
    173 		case '%':
    174 literal:
    175 			if (fp->_r <= 0 && __srefill(fp))
    176 				goto input_failure;
    177 			if (*fp->_p != c)
    178 				goto match_failure;
    179 			fp->_r--, fp->_p++;
    180 			nread++;
    181 			continue;
    182 
    183 		case '*':
    184 			flags |= SUPPRESS;
    185 			goto again;
    186 		case 'L':
    187 			flags |= LONGDBL;
    188 			goto again;
    189 		case 'h':
    190 			flags |= SHORT;
    191 			goto again;
    192 		case 'l':
    193 			if (*fmt == 'l') {
    194 				fmt++;
    195 				flags |= QUAD;
    196 			} else {
    197 				flags |= LONG;
    198 			}
    199 			goto again;
    200 		case 'q':
    201 			flags |= QUAD;
    202 			goto again;
    203 
    204 		case '0': case '1': case '2': case '3': case '4':
    205 		case '5': case '6': case '7': case '8': case '9':
    206 			width = width * 10 + c - '0';
    207 			goto again;
    208 
    209 		/*
    210 		 * Conversions.
    211 		 * Those marked `compat' are for 4.[123]BSD compatibility.
    212 		 *
    213 		 * (According to ANSI, E and X formats are supposed
    214 		 * to the same as e and x.  Sorry about that.)
    215 		 */
    216 		case 'D':	/* compat */
    217 			flags |= LONG;
    218 			/* FALLTHROUGH */
    219 		case 'd':
    220 			c = CT_INT;
    221 			ccfn = (u_quad_t (*) __P((const char *, char **, int)))strtoq;
    222 			base = 10;
    223 			break;
    224 
    225 		case 'i':
    226 			c = CT_INT;
    227 			ccfn = (u_quad_t (*) __P((const char *, char **, int)))strtoq;
    228 			base = 0;
    229 			break;
    230 
    231 		case 'O':	/* compat */
    232 			flags |= LONG;
    233 			/* FALLTHROUGH */
    234 		case 'o':
    235 			c = CT_INT;
    236 			ccfn = strtouq;
    237 			base = 8;
    238 			break;
    239 
    240 		case 'u':
    241 			c = CT_INT;
    242 			ccfn = strtouq;
    243 			base = 10;
    244 			break;
    245 
    246 		case 'X':
    247 		case 'x':
    248 			flags |= PFXOK;	/* enable 0x prefixing */
    249 			c = CT_INT;
    250 			ccfn = strtouq;
    251 			base = 16;
    252 			break;
    253 
    254 #ifdef FLOATING_POINT
    255 		case 'E':
    256 		case 'G':
    257 		case 'e':
    258 		case 'f':
    259 		case 'g':
    260 			c = CT_FLOAT;
    261 			break;
    262 #endif
    263 
    264 		case 's':
    265 			c = CT_STRING;
    266 			break;
    267 
    268 		case '[':
    269 			fmt = __sccl(ccltab, fmt);
    270 			flags |= NOSKIP;
    271 			c = CT_CCL;
    272 			break;
    273 
    274 		case 'c':
    275 			flags |= NOSKIP;
    276 			c = CT_CHAR;
    277 			break;
    278 
    279 		case 'p':	/* pointer format is like hex */
    280 			flags |= POINTER | PFXOK;
    281 			c = CT_INT;
    282 			ccfn = strtouq;
    283 			base = 16;
    284 			break;
    285 
    286 		case 'n':
    287 			if (flags & SUPPRESS)	/* ??? */
    288 				continue;
    289 			if (flags & SHORT)
    290 				*va_arg(ap, short *) = nread;
    291 			else if (flags & LONG)
    292 				*va_arg(ap, long *) = nread;
    293 			else
    294 				*va_arg(ap, int *) = nread;
    295 			continue;
    296 
    297 		/*
    298 		 * Disgusting backwards compatibility hacks.	XXX
    299 		 */
    300 		case '\0':	/* compat */
    301 			return (EOF);
    302 
    303 		default:	/* compat */
    304 			if (isupper(c))
    305 				flags |= LONG;
    306 			c = CT_INT;
    307 			ccfn = (u_quad_t (*) __P((const char *, char **, int)))strtoq;
    308 			base = 10;
    309 			break;
    310 		}
    311 
    312 		/*
    313 		 * We have a conversion that requires input.
    314 		 */
    315 		if (fp->_r <= 0 && __srefill(fp))
    316 			goto input_failure;
    317 
    318 		/*
    319 		 * Consume leading white space, except for formats
    320 		 * that suppress this.
    321 		 */
    322 		if ((flags & NOSKIP) == 0) {
    323 			while (isspace(*fp->_p)) {
    324 				nread++;
    325 				if (--fp->_r > 0)
    326 					fp->_p++;
    327 				else if (__srefill(fp))
    328 					goto input_failure;
    329 			}
    330 			/*
    331 			 * Note that there is at least one character in
    332 			 * the buffer, so conversions that do not set NOSKIP
    333 			 * ca no longer result in an input failure.
    334 			 */
    335 		}
    336 
    337 		/*
    338 		 * Do the conversion.
    339 		 */
    340 		switch (c) {
    341 
    342 		case CT_CHAR:
    343 			/* scan arbitrary characters (sets NOSKIP) */
    344 			if (width == 0)
    345 				width = 1;
    346 			if (flags & SUPPRESS) {
    347 				size_t sum = 0;
    348 				for (;;) {
    349 					if ((n = fp->_r) < width) {
    350 						sum += n;
    351 						width -= n;
    352 						fp->_p += n;
    353 						if (__srefill(fp)) {
    354 							if (sum == 0)
    355 							    goto input_failure;
    356 							break;
    357 						}
    358 					} else {
    359 						sum += width;
    360 						fp->_r -= width;
    361 						fp->_p += width;
    362 						break;
    363 					}
    364 				}
    365 				nread += sum;
    366 			} else {
    367 				size_t r = fread((void *)va_arg(ap, char *), 1,
    368 				    width, fp);
    369 
    370 				if (r == 0)
    371 					goto input_failure;
    372 				nread += r;
    373 				nassigned++;
    374 			}
    375 			break;
    376 
    377 		case CT_CCL:
    378 			/* scan a (nonempty) character class (sets NOSKIP) */
    379 			if (width == 0)
    380 				width = ~0U;	/* `infinity' */
    381 			/* take only those things in the class */
    382 			if (flags & SUPPRESS) {
    383 				n = 0;
    384 				while (ccltab[*fp->_p]) {
    385 					n++, fp->_r--, fp->_p++;
    386 					if (--width == 0)
    387 						break;
    388 					if (fp->_r <= 0 && __srefill(fp)) {
    389 						if (n == 0)
    390 							goto input_failure;
    391 						break;
    392 					}
    393 				}
    394 				if (n == 0)
    395 					goto match_failure;
    396 			} else {
    397 				p0 = p = va_arg(ap, char *);
    398 				while (ccltab[*fp->_p]) {
    399 					fp->_r--;
    400 					*p++ = *fp->_p++;
    401 					if (--width == 0)
    402 						break;
    403 					if (fp->_r <= 0 && __srefill(fp)) {
    404 						if (p == p0)
    405 							goto input_failure;
    406 						break;
    407 					}
    408 				}
    409 				n = p - p0;
    410 				if (n == 0)
    411 					goto match_failure;
    412 				*p = 0;
    413 				nassigned++;
    414 			}
    415 			nread += n;
    416 			break;
    417 
    418 		case CT_STRING:
    419 			/* like CCL, but zero-length string OK, & no NOSKIP */
    420 			if (width == 0)
    421 				width = ~0U;
    422 			if (flags & SUPPRESS) {
    423 				n = 0;
    424 				while (!isspace(*fp->_p)) {
    425 					n++, fp->_r--, fp->_p++;
    426 					if (--width == 0)
    427 						break;
    428 					if (fp->_r <= 0 && __srefill(fp))
    429 						break;
    430 				}
    431 				nread += n;
    432 			} else {
    433 				p0 = p = va_arg(ap, char *);
    434 				while (!isspace(*fp->_p)) {
    435 					fp->_r--;
    436 					*p++ = *fp->_p++;
    437 					if (--width == 0)
    438 						break;
    439 					if (fp->_r <= 0 && __srefill(fp))
    440 						break;
    441 				}
    442 				*p = 0;
    443 				nread += p - p0;
    444 				nassigned++;
    445 			}
    446 			continue;
    447 
    448 		case CT_INT:
    449 			/* scan an integer as if by strtoq/strtouq */
    450 #ifdef hardway
    451 			if (width == 0 || width > sizeof(buf) - 1)
    452 				width = sizeof(buf) - 1;
    453 #else
    454 			/* size_t is unsigned, hence this optimisation */
    455 			if (--width > sizeof(buf) - 2)
    456 				width = sizeof(buf) - 2;
    457 			width++;
    458 #endif
    459 			flags |= SIGNOK | NDIGITS | NZDIGITS;
    460 			for (p = buf; width; width--) {
    461 				c = *fp->_p;
    462 				/*
    463 				 * Switch on the character; `goto ok'
    464 				 * if we accept it as a part of number.
    465 				 */
    466 				switch (c) {
    467 
    468 				/*
    469 				 * The digit 0 is always legal, but is
    470 				 * special.  For %i conversions, if no
    471 				 * digits (zero or nonzero) have been
    472 				 * scanned (only signs), we will have
    473 				 * base==0.  In that case, we should set
    474 				 * it to 8 and enable 0x prefixing.
    475 				 * Also, if we have not scanned zero digits
    476 				 * before this, do not turn off prefixing
    477 				 * (someone else will turn it off if we
    478 				 * have scanned any nonzero digits).
    479 				 */
    480 				case '0':
    481 					if (base == 0) {
    482 						base = 8;
    483 						flags |= PFXOK;
    484 					}
    485 					if (flags & NZDIGITS)
    486 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
    487 					else
    488 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
    489 					goto ok;
    490 
    491 				/* 1 through 7 always legal */
    492 				case '1': case '2': case '3':
    493 				case '4': case '5': case '6': case '7':
    494 					base = basefix[base];
    495 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    496 					goto ok;
    497 
    498 				/* digits 8 and 9 ok iff decimal or hex */
    499 				case '8': case '9':
    500 					base = basefix[base];
    501 					if (base <= 8)
    502 						break;	/* not legal here */
    503 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    504 					goto ok;
    505 
    506 				/* letters ok iff hex */
    507 				case 'A': case 'B': case 'C':
    508 				case 'D': case 'E': case 'F':
    509 				case 'a': case 'b': case 'c':
    510 				case 'd': case 'e': case 'f':
    511 					/* no need to fix base here */
    512 					if (base <= 10)
    513 						break;	/* not legal here */
    514 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    515 					goto ok;
    516 
    517 				/* sign ok only as first character */
    518 				case '+': case '-':
    519 					if (flags & SIGNOK) {
    520 						flags &= ~SIGNOK;
    521 						goto ok;
    522 					}
    523 					break;
    524 
    525 				/* x ok iff flag still set & 2nd char */
    526 				case 'x': case 'X':
    527 					if (flags & PFXOK && p == buf + 1) {
    528 						base = 16;	/* if %i */
    529 						flags &= ~PFXOK;
    530 						goto ok;
    531 					}
    532 					break;
    533 				}
    534 
    535 				/*
    536 				 * If we got here, c is not a legal character
    537 				 * for a number.  Stop accumulating digits.
    538 				 */
    539 				break;
    540 		ok:
    541 				/*
    542 				 * c is legal: store it and look at the next.
    543 				 */
    544 				*p++ = c;
    545 				if (--fp->_r > 0)
    546 					fp->_p++;
    547 				else if (__srefill(fp))
    548 					break;		/* EOF */
    549 			}
    550 			/*
    551 			 * If we had only a sign, it is no good; push
    552 			 * back the sign.  If the number ends in `x',
    553 			 * it was [sign] '0' 'x', so push back the x
    554 			 * and treat it as [sign] '0'.
    555 			 */
    556 			if (flags & NDIGITS) {
    557 				if (p > buf)
    558 					(void) ungetc(*(u_char *)--p, fp);
    559 				goto match_failure;
    560 			}
    561 			c = ((u_char *)p)[-1];
    562 			if (c == 'x' || c == 'X') {
    563 				--p;
    564 				(void) ungetc(c, fp);
    565 			}
    566 			if ((flags & SUPPRESS) == 0) {
    567 				u_quad_t res;
    568 
    569 				*p = 0;
    570 				res = (*ccfn)(buf, (char **)NULL, base);
    571 				if (flags & POINTER)
    572 					*va_arg(ap, void **) =
    573 					    (void *)(long)res;
    574 				else if (flags & QUAD)
    575 					*va_arg(ap, quad_t *) = res;
    576 				else if (flags & LONG)
    577 					*va_arg(ap, long *) = (long)res;
    578 				else if (flags & SHORT)
    579 					*va_arg(ap, short *) = (short)res;
    580 				else
    581 					*va_arg(ap, int *) = (int)res;
    582 				nassigned++;
    583 			}
    584 			nread += p - buf;
    585 			break;
    586 
    587 #ifdef FLOATING_POINT
    588 		case CT_FLOAT:
    589 			/* scan a floating point number as if by strtod */
    590 #ifdef hardway
    591 			if (width == 0 || width > sizeof(buf) - 1)
    592 				width = sizeof(buf) - 1;
    593 #else
    594 			/* size_t is unsigned, hence this optimisation */
    595 			if (--width > sizeof(buf) - 2)
    596 				width = sizeof(buf) - 2;
    597 			width++;
    598 #endif
    599 			flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
    600 			for (p = buf; width; width--) {
    601 				c = *fp->_p;
    602 				/*
    603 				 * This code mimicks the integer conversion
    604 				 * code, but is much simpler.
    605 				 */
    606 				switch (c) {
    607 
    608 				case '0': case '1': case '2': case '3':
    609 				case '4': case '5': case '6': case '7':
    610 				case '8': case '9':
    611 					flags &= ~(SIGNOK | NDIGITS);
    612 					goto fok;
    613 
    614 				case '+': case '-':
    615 					if (flags & SIGNOK) {
    616 						flags &= ~SIGNOK;
    617 						goto fok;
    618 					}
    619 					break;
    620 				case '.':
    621 					if (flags & DPTOK) {
    622 						flags &= ~(SIGNOK | DPTOK);
    623 						goto fok;
    624 					}
    625 					break;
    626 				case 'e': case 'E':
    627 					/* no exponent without some digits */
    628 					if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
    629 						flags =
    630 						    (flags & ~(EXPOK|DPTOK)) |
    631 						    SIGNOK | NDIGITS;
    632 						goto fok;
    633 					}
    634 					break;
    635 				}
    636 				break;
    637 		fok:
    638 				*p++ = c;
    639 				if (--fp->_r > 0)
    640 					fp->_p++;
    641 				else if (__srefill(fp))
    642 					break;	/* EOF */
    643 			}
    644 			/*
    645 			 * If no digits, might be missing exponent digits
    646 			 * (just give back the exponent) or might be missing
    647 			 * regular digits, but had sign and/or decimal point.
    648 			 */
    649 			if (flags & NDIGITS) {
    650 				if (flags & EXPOK) {
    651 					/* no digits at all */
    652 					while (p > buf)
    653 						ungetc(*(u_char *)--p, fp);
    654 					goto match_failure;
    655 				}
    656 				/* just a bad exponent (e and maybe sign) */
    657 				c = *(u_char *)--p;
    658 				if (c != 'e' && c != 'E') {
    659 					(void) ungetc(c, fp);/* sign */
    660 					c = *(u_char *)--p;
    661 				}
    662 				(void) ungetc(c, fp);
    663 			}
    664 			if ((flags & SUPPRESS) == 0) {
    665 				double res;
    666 
    667 				*p = 0;
    668 				res = strtod(buf, (char **) NULL);
    669 				if (flags & LONGDBL)
    670 					*va_arg(ap, long double *) = res;
    671 				else if (flags & LONG)
    672 					*va_arg(ap, double *) = res;
    673 				else
    674 					*va_arg(ap, float *) = res;
    675 				nassigned++;
    676 			}
    677 			nread += p - buf;
    678 			break;
    679 #endif /* FLOATING_POINT */
    680 		}
    681 	}
    682 input_failure:
    683 	return (nassigned ? nassigned : EOF);
    684 match_failure:
    685 	return (nassigned);
    686 }
    687 
    688 /*
    689  * Fill in the given table from the scanset at the given format
    690  * (just after `[').  Return a pointer to the character past the
    691  * closing `]'.  The table has a 1 wherever characters should be
    692  * considered part of the scanset.
    693  */
    694 static const u_char *
    695 __sccl(tab, fmt)
    696 	char *tab;
    697 	const u_char *fmt;
    698 {
    699 	int c, n, v;
    700 
    701 	_DIAGASSERT(tab != NULL);
    702 	_DIAGASSERT(fmt != NULL);
    703 
    704 	/* first `clear' the whole table */
    705 	c = *fmt++;		/* first char hat => negated scanset */
    706 	if (c == '^') {
    707 		v = 1;		/* default => accept */
    708 		c = *fmt++;	/* get new first char */
    709 	} else
    710 		v = 0;		/* default => reject */
    711 	/* should probably use memset here */
    712 	for (n = 0; n < 256; n++)
    713 		tab[n] = v;
    714 	if (c == 0)
    715 		return (fmt - 1);/* format ended before closing ] */
    716 
    717 	/*
    718 	 * Now set the entries corresponding to the actual scanset
    719 	 * to the opposite of the above.
    720 	 *
    721 	 * The first character may be ']' (or '-') without being special;
    722 	 * the last character may be '-'.
    723 	 */
    724 	v = 1 - v;
    725 	for (;;) {
    726 		tab[c] = v;		/* take character c */
    727 doswitch:
    728 		n = *fmt++;		/* and examine the next */
    729 		switch (n) {
    730 
    731 		case 0:			/* format ended too soon */
    732 			return (fmt - 1);
    733 
    734 		case '-':
    735 			/*
    736 			 * A scanset of the form
    737 			 *	[01+-]
    738 			 * is defined as `the digit 0, the digit 1,
    739 			 * the character +, the character -', but
    740 			 * the effect of a scanset such as
    741 			 *	[a-zA-Z0-9]
    742 			 * is implementation defined.  The V7 Unix
    743 			 * scanf treats `a-z' as `the letters a through
    744 			 * z', but treats `a-a' as `the letter a, the
    745 			 * character -, and the letter a'.
    746 			 *
    747 			 * For compatibility, the `-' is not considerd
    748 			 * to define a range if the character following
    749 			 * it is either a close bracket (required by ANSI)
    750 			 * or is not numerically greater than the character
    751 			 * we just stored in the table (c).
    752 			 */
    753 			n = *fmt;
    754 			if (n == ']' || n < c) {
    755 				c = '-';
    756 				break;	/* resume the for(;;) */
    757 			}
    758 			fmt++;
    759 			do {		/* fill in the range */
    760 				tab[++c] = v;
    761 			} while (c < n);
    762 #if 1	/* XXX another disgusting compatibility hack */
    763 			/*
    764 			 * Alas, the V7 Unix scanf also treats formats
    765 			 * such as [a-c-e] as `the letters a through e'.
    766 			 * This too is permitted by the standard....
    767 			 */
    768 			goto doswitch;
    769 #else
    770 			c = *fmt++;
    771 			if (c == 0)
    772 				return (fmt - 1);
    773 			if (c == ']')
    774 				return (fmt);
    775 			break;
    776 #endif
    777 
    778 		case ']':		/* end of scanset */
    779 			return (fmt);
    780 
    781 		default:		/* just another character */
    782 			c = n;
    783 			break;
    784 		}
    785 	}
    786 	/* NOTREACHED */
    787 }
    788