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