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