PageRenderTime 32ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/sendmail/libsm/vfscanf.c

https://github.com/okuoku/freebsd-head
C | 877 lines | 608 code | 86 blank | 183 comment | 158 complexity | 6fb9afd634af9639be4d422e7feccdda MD5 | raw file
  1. /*
  2. * Copyright (c) 2000-2001, 2004 Sendmail, Inc. and its suppliers.
  3. * All rights reserved.
  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. * By using this file, you agree to the terms and conditions set
  11. * forth in the LICENSE file which can be found at the top level of
  12. * the sendmail distribution.
  13. */
  14. #include <sm/gen.h>
  15. SM_IDSTR(id, "@(#)$Id: vfscanf.c,v 1.54 2006/10/12 22:03:52 ca Exp $")
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <setjmp.h>
  20. #include <sm/time.h>
  21. #include <sm/varargs.h>
  22. #include <sm/config.h>
  23. #include <sm/io.h>
  24. #include <sm/signal.h>
  25. #include <sm/clock.h>
  26. #include <sm/string.h>
  27. #include "local.h"
  28. #define BUF 513 /* Maximum length of numeric string. */
  29. /* Flags used during conversion. */
  30. #define LONG 0x01 /* l: long or double */
  31. #define SHORT 0x04 /* h: short */
  32. #define QUAD 0x08 /* q: quad (same as ll) */
  33. #define SUPPRESS 0x10 /* suppress assignment */
  34. #define POINTER 0x20 /* weird %p pointer (`fake hex') */
  35. #define NOSKIP 0x40 /* do not skip blanks */
  36. /*
  37. ** The following are used in numeric conversions only:
  38. ** SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
  39. ** SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
  40. */
  41. #define SIGNOK 0x080 /* +/- is (still) legal */
  42. #define NDIGITS 0x100 /* no digits detected */
  43. #define DPTOK 0x200 /* (float) decimal point is still legal */
  44. #define EXPOK 0x400 /* (float) exponent (e+3, etc) still legal */
  45. #define PFXOK 0x200 /* 0x prefix is (still) legal */
  46. #define NZDIGITS 0x400 /* no zero digits detected */
  47. /* Conversion types. */
  48. #define CT_CHAR 0 /* %c conversion */
  49. #define CT_CCL 1 /* %[...] conversion */
  50. #define CT_STRING 2 /* %s conversion */
  51. #define CT_INT 3 /* integer, i.e., strtoll or strtoull */
  52. #define CT_FLOAT 4 /* floating, i.e., strtod */
  53. static void scanalrm __P((int));
  54. static unsigned char *sm_sccl __P((char *, unsigned char *));
  55. static jmp_buf ScanTimeOut;
  56. /*
  57. ** SCANALRM -- handler when timeout activated for sm_io_vfscanf()
  58. **
  59. ** Returns flow of control to where setjmp(ScanTimeOut) was set.
  60. **
  61. ** Parameters:
  62. ** sig -- unused
  63. **
  64. ** Returns:
  65. ** does not return
  66. **
  67. ** Side Effects:
  68. ** returns flow of control to setjmp(ScanTimeOut).
  69. **
  70. ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
  71. ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
  72. ** DOING.
  73. */
  74. /* ARGSUSED0 */
  75. static void
  76. scanalrm(sig)
  77. int sig;
  78. {
  79. longjmp(ScanTimeOut, 1);
  80. }
  81. /*
  82. ** SM_VFSCANF -- convert input into data units
  83. **
  84. ** Parameters:
  85. ** fp -- file pointer for input data
  86. ** timeout -- time intvl allowed to complete (milliseconds)
  87. ** fmt0 -- format for finding data units
  88. ** ap -- vectors for memory location for storing data units
  89. **
  90. ** Results:
  91. ** Success: number of data units assigned
  92. ** Failure: SM_IO_EOF
  93. */
  94. int
  95. sm_vfscanf(fp, timeout, fmt0, ap)
  96. register SM_FILE_T *fp;
  97. int SM_NONVOLATILE timeout;
  98. char const *fmt0;
  99. va_list SM_NONVOLATILE ap;
  100. {
  101. register unsigned char *SM_NONVOLATILE fmt = (unsigned char *) fmt0;
  102. register int c; /* character from format, or conversion */
  103. register size_t width; /* field width, or 0 */
  104. register char *p; /* points into all kinds of strings */
  105. register int n; /* handy integer */
  106. register int flags; /* flags as defined above */
  107. register char *p0; /* saves original value of p when necessary */
  108. int nassigned; /* number of fields assigned */
  109. int nread; /* number of characters consumed from fp */
  110. int base; /* base argument to strtoll/strtoull */
  111. /* conversion function (strtoll/strtoull) */
  112. ULONGLONG_T (*ccfn) __P((const char *, char **, int));
  113. char ccltab[256]; /* character class table for %[...] */
  114. char buf[BUF]; /* buffer for numeric conversions */
  115. SM_EVENT *evt = NULL;
  116. /* `basefix' is used to avoid `if' tests in the integer scanner */
  117. static short basefix[17] =
  118. { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  119. if (timeout == SM_TIME_DEFAULT)
  120. timeout = fp->f_timeout;
  121. if (timeout == SM_TIME_IMMEDIATE)
  122. {
  123. /*
  124. ** Filling the buffer will take time and we are wanted to
  125. ** return immediately. So...
  126. */
  127. errno = EAGAIN;
  128. return SM_IO_EOF;
  129. }
  130. if (timeout != SM_TIME_FOREVER)
  131. {
  132. if (setjmp(ScanTimeOut) != 0)
  133. {
  134. errno = EAGAIN;
  135. return SM_IO_EOF;
  136. }
  137. evt = sm_seteventm(timeout, scanalrm, 0);
  138. }
  139. nassigned = 0;
  140. nread = 0;
  141. base = 0; /* XXX just to keep gcc happy */
  142. ccfn = NULL; /* XXX just to keep gcc happy */
  143. for (;;)
  144. {
  145. c = *fmt++;
  146. if (c == 0)
  147. {
  148. if (evt != NULL)
  149. sm_clrevent(evt); /* undo our timeout */
  150. return nassigned;
  151. }
  152. if (isspace(c))
  153. {
  154. while ((fp->f_r > 0 || sm_refill(fp, SM_TIME_FOREVER)
  155. == 0) &&
  156. isspace(*fp->f_p))
  157. nread++, fp->f_r--, fp->f_p++;
  158. continue;
  159. }
  160. if (c != '%')
  161. goto literal;
  162. width = 0;
  163. flags = 0;
  164. /*
  165. ** switch on the format. continue if done;
  166. ** break once format type is derived.
  167. */
  168. again: c = *fmt++;
  169. switch (c)
  170. {
  171. case '%':
  172. literal:
  173. if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
  174. goto input_failure;
  175. if (*fp->f_p != c)
  176. goto match_failure;
  177. fp->f_r--, fp->f_p++;
  178. nread++;
  179. continue;
  180. case '*':
  181. flags |= SUPPRESS;
  182. goto again;
  183. case 'h':
  184. flags |= SHORT;
  185. goto again;
  186. case 'l':
  187. if (*fmt == 'l')
  188. {
  189. fmt++;
  190. flags |= QUAD;
  191. }
  192. else
  193. {
  194. flags |= LONG;
  195. }
  196. goto again;
  197. case 'q':
  198. flags |= QUAD;
  199. goto again;
  200. case '0': case '1': case '2': case '3': case '4':
  201. case '5': case '6': case '7': case '8': case '9':
  202. width = width * 10 + c - '0';
  203. goto again;
  204. /*
  205. ** Conversions.
  206. ** Those marked `compat' are for 4.[123]BSD compatibility.
  207. **
  208. ** (According to ANSI, E and X formats are supposed
  209. ** to the same as e and x. Sorry about that.)
  210. */
  211. case 'D': /* compat */
  212. flags |= LONG;
  213. /* FALLTHROUGH */
  214. case 'd':
  215. c = CT_INT;
  216. ccfn = (ULONGLONG_T (*)())sm_strtoll;
  217. base = 10;
  218. break;
  219. case 'i':
  220. c = CT_INT;
  221. ccfn = (ULONGLONG_T (*)())sm_strtoll;
  222. base = 0;
  223. break;
  224. case 'O': /* compat */
  225. flags |= LONG;
  226. /* FALLTHROUGH */
  227. case 'o':
  228. c = CT_INT;
  229. ccfn = sm_strtoull;
  230. base = 8;
  231. break;
  232. case 'u':
  233. c = CT_INT;
  234. ccfn = sm_strtoull;
  235. base = 10;
  236. break;
  237. case 'X':
  238. case 'x':
  239. flags |= PFXOK; /* enable 0x prefixing */
  240. c = CT_INT;
  241. ccfn = sm_strtoull;
  242. base = 16;
  243. break;
  244. case 'E':
  245. case 'G':
  246. case 'e':
  247. case 'f':
  248. case 'g':
  249. c = CT_FLOAT;
  250. break;
  251. case 's':
  252. c = CT_STRING;
  253. break;
  254. case '[':
  255. fmt = sm_sccl(ccltab, fmt);
  256. flags |= NOSKIP;
  257. c = CT_CCL;
  258. break;
  259. case 'c':
  260. flags |= NOSKIP;
  261. c = CT_CHAR;
  262. break;
  263. case 'p': /* pointer format is like hex */
  264. flags |= POINTER | PFXOK;
  265. c = CT_INT;
  266. ccfn = sm_strtoull;
  267. base = 16;
  268. break;
  269. case 'n':
  270. if (flags & SUPPRESS) /* ??? */
  271. continue;
  272. if (flags & SHORT)
  273. *SM_VA_ARG(ap, short *) = nread;
  274. else if (flags & LONG)
  275. *SM_VA_ARG(ap, long *) = nread;
  276. else
  277. *SM_VA_ARG(ap, int *) = nread;
  278. continue;
  279. /* Disgusting backwards compatibility hacks. XXX */
  280. case '\0': /* compat */
  281. if (evt != NULL)
  282. sm_clrevent(evt); /* undo our timeout */
  283. return SM_IO_EOF;
  284. default: /* compat */
  285. if (isupper(c))
  286. flags |= LONG;
  287. c = CT_INT;
  288. ccfn = (ULONGLONG_T (*)()) sm_strtoll;
  289. base = 10;
  290. break;
  291. }
  292. /* We have a conversion that requires input. */
  293. if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
  294. goto input_failure;
  295. /*
  296. ** Consume leading white space, except for formats
  297. ** that suppress this.
  298. */
  299. if ((flags & NOSKIP) == 0)
  300. {
  301. while (isspace(*fp->f_p))
  302. {
  303. nread++;
  304. if (--fp->f_r > 0)
  305. fp->f_p++;
  306. else if (sm_refill(fp, SM_TIME_FOREVER))
  307. goto input_failure;
  308. }
  309. /*
  310. ** Note that there is at least one character in
  311. ** the buffer, so conversions that do not set NOSKIP
  312. ** can no longer result in an input failure.
  313. */
  314. }
  315. /* Do the conversion. */
  316. switch (c)
  317. {
  318. case CT_CHAR:
  319. /* scan arbitrary characters (sets NOSKIP) */
  320. if (width == 0)
  321. width = 1;
  322. if (flags & SUPPRESS)
  323. {
  324. size_t sum = 0;
  325. for (;;)
  326. {
  327. if ((size_t) (n = fp->f_r) < width)
  328. {
  329. sum += n;
  330. width -= n;
  331. fp->f_p += n;
  332. if (sm_refill(fp,
  333. SM_TIME_FOREVER))
  334. {
  335. if (sum == 0)
  336. goto input_failure;
  337. break;
  338. }
  339. }
  340. else
  341. {
  342. sum += width;
  343. fp->f_r -= width;
  344. fp->f_p += width;
  345. break;
  346. }
  347. }
  348. nread += sum;
  349. }
  350. else
  351. {
  352. size_t r;
  353. r = sm_io_read(fp, SM_TIME_FOREVER,
  354. (void *) SM_VA_ARG(ap, char *),
  355. width);
  356. if (r == 0)
  357. goto input_failure;
  358. nread += r;
  359. nassigned++;
  360. }
  361. break;
  362. case CT_CCL:
  363. /* scan a (nonempty) character class (sets NOSKIP) */
  364. if (width == 0)
  365. width = (size_t)~0; /* `infinity' */
  366. /* take only those things in the class */
  367. if (flags & SUPPRESS)
  368. {
  369. n = 0;
  370. while (ccltab[*fp->f_p] != '\0')
  371. {
  372. n++, fp->f_r--, fp->f_p++;
  373. if (--width == 0)
  374. break;
  375. if (fp->f_r <= 0 &&
  376. sm_refill(fp, SM_TIME_FOREVER))
  377. {
  378. if (n == 0) /* XXX how? */
  379. goto input_failure;
  380. break;
  381. }
  382. }
  383. if (n == 0)
  384. goto match_failure;
  385. }
  386. else
  387. {
  388. p0 = p = SM_VA_ARG(ap, char *);
  389. while (ccltab[*fp->f_p] != '\0')
  390. {
  391. fp->f_r--;
  392. *p++ = *fp->f_p++;
  393. if (--width == 0)
  394. break;
  395. if (fp->f_r <= 0 &&
  396. sm_refill(fp, SM_TIME_FOREVER))
  397. {
  398. if (p == p0)
  399. goto input_failure;
  400. break;
  401. }
  402. }
  403. n = p - p0;
  404. if (n == 0)
  405. goto match_failure;
  406. *p = 0;
  407. nassigned++;
  408. }
  409. nread += n;
  410. break;
  411. case CT_STRING:
  412. /* like CCL, but zero-length string OK, & no NOSKIP */
  413. if (width == 0)
  414. width = (size_t)~0;
  415. if (flags & SUPPRESS)
  416. {
  417. n = 0;
  418. while (!isspace(*fp->f_p))
  419. {
  420. n++, fp->f_r--, fp->f_p++;
  421. if (--width == 0)
  422. break;
  423. if (fp->f_r <= 0 &&
  424. sm_refill(fp, SM_TIME_FOREVER))
  425. break;
  426. }
  427. nread += n;
  428. }
  429. else
  430. {
  431. p0 = p = SM_VA_ARG(ap, char *);
  432. while (!isspace(*fp->f_p))
  433. {
  434. fp->f_r--;
  435. *p++ = *fp->f_p++;
  436. if (--width == 0)
  437. break;
  438. if (fp->f_r <= 0 &&
  439. sm_refill(fp, SM_TIME_FOREVER))
  440. break;
  441. }
  442. *p = 0;
  443. nread += p - p0;
  444. nassigned++;
  445. }
  446. continue;
  447. case CT_INT:
  448. /* scan an integer as if by strtoll/strtoull */
  449. #if SM_CONF_BROKEN_SIZE_T
  450. if (width == 0 || width > sizeof(buf) - 1)
  451. width = sizeof(buf) - 1;
  452. #else /* SM_CONF_BROKEN_SIZE_T */
  453. /* size_t is unsigned, hence this optimisation */
  454. if (--width > sizeof(buf) - 2)
  455. width = sizeof(buf) - 2;
  456. width++;
  457. #endif /* SM_CONF_BROKEN_SIZE_T */
  458. flags |= SIGNOK | NDIGITS | NZDIGITS;
  459. for (p = buf; width > 0; width--)
  460. {
  461. c = *fp->f_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. {
  483. base = 8;
  484. flags |= PFXOK;
  485. }
  486. if (flags & NZDIGITS)
  487. flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
  488. else
  489. flags &= ~(SIGNOK|PFXOK|NDIGITS);
  490. goto ok;
  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. /* 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. /* letters ok iff hex */
  505. case 'A': case 'B': case 'C':
  506. case 'D': case 'E': case 'F':
  507. case 'a': case 'b': case 'c':
  508. case 'd': case 'e': case 'f':
  509. /* no need to fix base here */
  510. if (base <= 10)
  511. break; /* not legal here */
  512. flags &= ~(SIGNOK | PFXOK | NDIGITS);
  513. goto ok;
  514. /* sign ok only as first character */
  515. case '+': case '-':
  516. if (flags & SIGNOK)
  517. {
  518. flags &= ~SIGNOK;
  519. goto ok;
  520. }
  521. break;
  522. /* x ok iff flag still set & 2nd char */
  523. case 'x': case 'X':
  524. if (flags & PFXOK && p == buf + 1)
  525. {
  526. base = 16; /* if %i */
  527. flags &= ~PFXOK;
  528. goto ok;
  529. }
  530. break;
  531. }
  532. /*
  533. ** If we got here, c is not a legal character
  534. ** for a number. Stop accumulating digits.
  535. */
  536. break;
  537. ok:
  538. /* c is legal: store it and look at the next. */
  539. *p++ = c;
  540. if (--fp->f_r > 0)
  541. fp->f_p++;
  542. else if (sm_refill(fp, SM_TIME_FOREVER))
  543. break; /* SM_IO_EOF */
  544. }
  545. /*
  546. ** If we had only a sign, it is no good; push
  547. ** back the sign. If the number ends in `x',
  548. ** it was [sign] '0' 'x', so push back the x
  549. ** and treat it as [sign] '0'.
  550. */
  551. if (flags & NDIGITS)
  552. {
  553. if (p > buf)
  554. (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
  555. *(unsigned char *)--p);
  556. goto match_failure;
  557. }
  558. c = ((unsigned char *)p)[-1];
  559. if (c == 'x' || c == 'X')
  560. {
  561. --p;
  562. (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
  563. }
  564. if ((flags & SUPPRESS) == 0)
  565. {
  566. ULONGLONG_T res;
  567. *p = 0;
  568. res = (*ccfn)(buf, (char **)NULL, base);
  569. if (flags & POINTER)
  570. *SM_VA_ARG(ap, void **) =
  571. (void *)(long) res;
  572. else if (flags & QUAD)
  573. *SM_VA_ARG(ap, LONGLONG_T *) = res;
  574. else if (flags & LONG)
  575. *SM_VA_ARG(ap, long *) = res;
  576. else if (flags & SHORT)
  577. *SM_VA_ARG(ap, short *) = res;
  578. else
  579. *SM_VA_ARG(ap, int *) = res;
  580. nassigned++;
  581. }
  582. nread += p - buf;
  583. break;
  584. case CT_FLOAT:
  585. /* scan a floating point number as if by strtod */
  586. if (width == 0 || width > sizeof(buf) - 1)
  587. width = sizeof(buf) - 1;
  588. flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
  589. for (p = buf; width; width--)
  590. {
  591. c = *fp->f_p;
  592. /*
  593. ** This code mimicks the integer conversion
  594. ** code, but is much simpler.
  595. */
  596. switch (c)
  597. {
  598. case '0': case '1': case '2': case '3':
  599. case '4': case '5': case '6': case '7':
  600. case '8': case '9':
  601. flags &= ~(SIGNOK | NDIGITS);
  602. goto fok;
  603. case '+': case '-':
  604. if (flags & SIGNOK)
  605. {
  606. flags &= ~SIGNOK;
  607. goto fok;
  608. }
  609. break;
  610. case '.':
  611. if (flags & DPTOK)
  612. {
  613. flags &= ~(SIGNOK | DPTOK);
  614. goto fok;
  615. }
  616. break;
  617. case 'e': case 'E':
  618. /* no exponent without some digits */
  619. if ((flags&(NDIGITS|EXPOK)) == EXPOK)
  620. {
  621. flags =
  622. (flags & ~(EXPOK|DPTOK)) |
  623. SIGNOK | NDIGITS;
  624. goto fok;
  625. }
  626. break;
  627. }
  628. break;
  629. fok:
  630. *p++ = c;
  631. if (--fp->f_r > 0)
  632. fp->f_p++;
  633. else if (sm_refill(fp, SM_TIME_FOREVER))
  634. break; /* SM_IO_EOF */
  635. }
  636. /*
  637. ** If no digits, might be missing exponent digits
  638. ** (just give back the exponent) or might be missing
  639. ** regular digits, but had sign and/or decimal point.
  640. */
  641. if (flags & NDIGITS)
  642. {
  643. if (flags & EXPOK)
  644. {
  645. /* no digits at all */
  646. while (p > buf)
  647. (void) sm_io_ungetc(fp,
  648. SM_TIME_DEFAULT,
  649. *(unsigned char *)--p);
  650. goto match_failure;
  651. }
  652. /* just a bad exponent (e and maybe sign) */
  653. c = *(unsigned char *) --p;
  654. if (c != 'e' && c != 'E')
  655. {
  656. (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
  657. c); /* sign */
  658. c = *(unsigned char *)--p;
  659. }
  660. (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
  661. }
  662. if ((flags & SUPPRESS) == 0)
  663. {
  664. double res;
  665. *p = 0;
  666. res = strtod(buf, (char **) NULL);
  667. if (flags & LONG)
  668. *SM_VA_ARG(ap, double *) = res;
  669. else
  670. *SM_VA_ARG(ap, float *) = res;
  671. nassigned++;
  672. }
  673. nread += p - buf;
  674. break;
  675. }
  676. }
  677. input_failure:
  678. if (evt != NULL)
  679. sm_clrevent(evt); /* undo our timeout */
  680. return nassigned ? nassigned : -1;
  681. match_failure:
  682. if (evt != NULL)
  683. sm_clrevent(evt); /* undo our timeout */
  684. return nassigned;
  685. }
  686. /*
  687. ** SM_SCCL -- sequenced character comparison list
  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. ** Parameters:
  695. ** tab -- array flagging "active" char's to match (returned)
  696. ** fmt -- character list (within "[]")
  697. **
  698. ** Results:
  699. */
  700. static unsigned char *
  701. sm_sccl(tab, fmt)
  702. register char *tab;
  703. register unsigned char *fmt;
  704. {
  705. register int c, n, v;
  706. /* first `clear' the whole table */
  707. c = *fmt++; /* first char hat => negated scanset */
  708. if (c == '^')
  709. {
  710. v = 1; /* default => accept */
  711. c = *fmt++; /* get new first char */
  712. }
  713. else
  714. v = 0; /* default => reject */
  715. /* should probably use memset here */
  716. for (n = 0; n < 256; n++)
  717. tab[n] = v;
  718. if (c == 0)
  719. return fmt - 1; /* format ended before closing ] */
  720. /*
  721. ** Now set the entries corresponding to the actual scanset
  722. ** to the opposite of the above.
  723. **
  724. ** The first character may be ']' (or '-') without being special;
  725. ** the last character may be '-'.
  726. */
  727. v = 1 - v;
  728. for (;;)
  729. {
  730. tab[c] = v; /* take character c */
  731. doswitch:
  732. n = *fmt++; /* and examine the next */
  733. switch (n)
  734. {
  735. case 0: /* format ended too soon */
  736. return fmt - 1;
  737. case '-':
  738. /*
  739. ** A scanset of the form
  740. ** [01+-]
  741. ** is defined as `the digit 0, the digit 1,
  742. ** the character +, the character -', but
  743. ** the effect of a scanset such as
  744. ** [a-zA-Z0-9]
  745. ** is implementation defined. The V7 Unix
  746. ** scanf treats `a-z' as `the letters a through
  747. ** z', but treats `a-a' as `the letter a, the
  748. ** character -, and the letter a'.
  749. **
  750. ** For compatibility, the `-' is not considerd
  751. ** to define a range if the character following
  752. ** it is either a close bracket (required by ANSI)
  753. ** or is not numerically greater than the character
  754. ** we just stored in the table (c).
  755. */
  756. n = *fmt;
  757. if (n == ']' || n < c)
  758. {
  759. c = '-';
  760. break; /* resume the for(;;) */
  761. }
  762. fmt++;
  763. do
  764. {
  765. /* fill in the range */
  766. tab[++c] = v;
  767. } while (c < n);
  768. #if 1 /* XXX another disgusting compatibility hack */
  769. /*
  770. ** Alas, the V7 Unix scanf also treats formats
  771. ** such as [a-c-e] as `the letters a through e'.
  772. ** This too is permitted by the standard....
  773. */
  774. goto doswitch;
  775. #else
  776. c = *fmt++;
  777. if (c == 0)
  778. return fmt - 1;
  779. if (c == ']')
  780. return fmt;
  781. break;
  782. #endif
  783. case ']': /* end of scanset */
  784. return fmt;
  785. default: /* just another character */
  786. c = n;
  787. break;
  788. }
  789. }
  790. /* NOTREACHED */
  791. }