8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / libsm / vfscanf.c
blob5bf0ffbac293dc87000313a967720767d53d1e32
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.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
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.
15 #pragma ident "%Z%%M% %I% %E% SMI"
17 #include <sm/gen.h>
18 SM_IDSTR(id, "@(#)$Id: vfscanf.c,v 1.54 2006/10/12 22:03:52 ca Exp $")
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <setjmp.h>
24 #include <sm/time.h>
25 #include <sm/varargs.h>
26 #include <sm/config.h>
27 #include <sm/io.h>
28 #include <sm/signal.h>
29 #include <sm/clock.h>
30 #include <sm/string.h>
31 #include "local.h"
33 #define BUF 513 /* Maximum length of numeric string. */
35 /* Flags used during conversion. */
36 #define LONG 0x01 /* l: long or double */
37 #define SHORT 0x04 /* h: short */
38 #define QUAD 0x08 /* q: quad (same as ll) */
39 #define SUPPRESS 0x10 /* suppress assignment */
40 #define POINTER 0x20 /* weird %p pointer (`fake hex') */
41 #define NOSKIP 0x40 /* do not skip blanks */
44 ** The following are used in numeric conversions only:
45 ** SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
46 ** SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
49 #define SIGNOK 0x080 /* +/- is (still) legal */
50 #define NDIGITS 0x100 /* no digits detected */
52 #define DPTOK 0x200 /* (float) decimal point is still legal */
53 #define EXPOK 0x400 /* (float) exponent (e+3, etc) still legal */
55 #define PFXOK 0x200 /* 0x prefix is (still) legal */
56 #define NZDIGITS 0x400 /* no zero digits detected */
58 /* Conversion types. */
59 #define CT_CHAR 0 /* %c conversion */
60 #define CT_CCL 1 /* %[...] conversion */
61 #define CT_STRING 2 /* %s conversion */
62 #define CT_INT 3 /* integer, i.e., strtoll or strtoull */
63 #define CT_FLOAT 4 /* floating, i.e., strtod */
65 static void scanalrm __P((int));
66 static unsigned char *sm_sccl __P((char *, unsigned char *));
67 static jmp_buf ScanTimeOut;
70 ** SCANALRM -- handler when timeout activated for sm_io_vfscanf()
72 ** Returns flow of control to where setjmp(ScanTimeOut) was set.
74 ** Parameters:
75 ** sig -- unused
77 ** Returns:
78 ** does not return
80 ** Side Effects:
81 ** returns flow of control to setjmp(ScanTimeOut).
83 ** NOTE: THIS CAN BE CALLED FROM A SIGNAL HANDLER. DO NOT ADD
84 ** ANYTHING TO THIS ROUTINE UNLESS YOU KNOW WHAT YOU ARE
85 ** DOING.
88 /* ARGSUSED0 */
89 static void
90 scanalrm(sig)
91 int sig;
93 longjmp(ScanTimeOut, 1);
97 ** SM_VFSCANF -- convert input into data units
99 ** Parameters:
100 ** fp -- file pointer for input data
101 ** timeout -- time intvl allowed to complete (milliseconds)
102 ** fmt0 -- format for finding data units
103 ** ap -- vectors for memory location for storing data units
105 ** Results:
106 ** Success: number of data units assigned
107 ** Failure: SM_IO_EOF
111 sm_vfscanf(fp, timeout, fmt0, ap)
112 register SM_FILE_T *fp;
113 int SM_NONVOLATILE timeout;
114 char const *fmt0;
115 va_list SM_NONVOLATILE ap;
117 register unsigned char *SM_NONVOLATILE fmt = (unsigned char *) fmt0;
118 register int c; /* character from format, or conversion */
119 register size_t width; /* field width, or 0 */
120 register char *p; /* points into all kinds of strings */
121 register int n; /* handy integer */
122 register int flags; /* flags as defined above */
123 register 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 strtoll/strtoull */
128 /* conversion function (strtoll/strtoull) */
129 ULONGLONG_T (*ccfn) __P((const char *, char **, int));
130 char ccltab[256]; /* character class table for %[...] */
131 char buf[BUF]; /* buffer for numeric conversions */
132 SM_EVENT *evt = NULL;
134 /* `basefix' is used to avoid `if' tests in the integer scanner */
135 static short basefix[17] =
136 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
138 if (timeout == SM_TIME_DEFAULT)
139 timeout = fp->f_timeout;
140 if (timeout == SM_TIME_IMMEDIATE)
143 ** Filling the buffer will take time and we are wanted to
144 ** return immediately. So...
147 errno = EAGAIN;
148 return SM_IO_EOF;
151 if (timeout != SM_TIME_FOREVER)
153 if (setjmp(ScanTimeOut) != 0)
155 errno = EAGAIN;
156 return SM_IO_EOF;
159 evt = sm_seteventm(timeout, scanalrm, 0);
162 nassigned = 0;
163 nread = 0;
164 base = 0; /* XXX just to keep gcc happy */
165 ccfn = NULL; /* XXX just to keep gcc happy */
166 for (;;)
168 c = *fmt++;
169 if (c == 0)
171 if (evt != NULL)
172 sm_clrevent(evt); /* undo our timeout */
173 return nassigned;
175 if (isspace(c))
177 while ((fp->f_r > 0 || sm_refill(fp, SM_TIME_FOREVER)
178 == 0) &&
179 isspace(*fp->f_p))
180 nread++, fp->f_r--, fp->f_p++;
181 continue;
183 if (c != '%')
184 goto literal;
185 width = 0;
186 flags = 0;
189 ** switch on the format. continue if done;
190 ** break once format type is derived.
193 again: c = *fmt++;
194 switch (c)
196 case '%':
197 literal:
198 if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
199 goto input_failure;
200 if (*fp->f_p != c)
201 goto match_failure;
202 fp->f_r--, fp->f_p++;
203 nread++;
204 continue;
206 case '*':
207 flags |= SUPPRESS;
208 goto again;
209 case 'h':
210 flags |= SHORT;
211 goto again;
212 case 'l':
213 if (*fmt == 'l')
215 fmt++;
216 flags |= QUAD;
218 else
220 flags |= LONG;
222 goto again;
223 case 'q':
224 flags |= QUAD;
225 goto again;
227 case '0': case '1': case '2': case '3': case '4':
228 case '5': case '6': case '7': case '8': case '9':
229 width = width * 10 + c - '0';
230 goto again;
233 ** Conversions.
234 ** Those marked `compat' are for 4.[123]BSD compatibility.
236 ** (According to ANSI, E and X formats are supposed
237 ** to the same as e and x. Sorry about that.)
240 case 'D': /* compat */
241 flags |= LONG;
242 /* FALLTHROUGH */
243 case 'd':
244 c = CT_INT;
245 ccfn = (ULONGLONG_T (*)())sm_strtoll;
246 base = 10;
247 break;
249 case 'i':
250 c = CT_INT;
251 ccfn = (ULONGLONG_T (*)())sm_strtoll;
252 base = 0;
253 break;
255 case 'O': /* compat */
256 flags |= LONG;
257 /* FALLTHROUGH */
258 case 'o':
259 c = CT_INT;
260 ccfn = sm_strtoull;
261 base = 8;
262 break;
264 case 'u':
265 c = CT_INT;
266 ccfn = sm_strtoull;
267 base = 10;
268 break;
270 case 'X':
271 case 'x':
272 flags |= PFXOK; /* enable 0x prefixing */
273 c = CT_INT;
274 ccfn = sm_strtoull;
275 base = 16;
276 break;
278 case 'E':
279 case 'G':
280 case 'e':
281 case 'f':
282 case 'g':
283 c = CT_FLOAT;
284 break;
286 case 's':
287 c = CT_STRING;
288 break;
290 case '[':
291 fmt = sm_sccl(ccltab, fmt);
292 flags |= NOSKIP;
293 c = CT_CCL;
294 break;
296 case 'c':
297 flags |= NOSKIP;
298 c = CT_CHAR;
299 break;
301 case 'p': /* pointer format is like hex */
302 flags |= POINTER | PFXOK;
303 c = CT_INT;
304 ccfn = sm_strtoull;
305 base = 16;
306 break;
308 case 'n':
309 if (flags & SUPPRESS) /* ??? */
310 continue;
311 if (flags & SHORT)
312 *SM_VA_ARG(ap, short *) = nread;
313 else if (flags & LONG)
314 *SM_VA_ARG(ap, long *) = nread;
315 else
316 *SM_VA_ARG(ap, int *) = nread;
317 continue;
319 /* Disgusting backwards compatibility hacks. XXX */
320 case '\0': /* compat */
321 if (evt != NULL)
322 sm_clrevent(evt); /* undo our timeout */
323 return SM_IO_EOF;
325 default: /* compat */
326 if (isupper(c))
327 flags |= LONG;
328 c = CT_INT;
329 ccfn = (ULONGLONG_T (*)()) sm_strtoll;
330 base = 10;
331 break;
334 /* We have a conversion that requires input. */
335 if (fp->f_r <= 0 && sm_refill(fp, SM_TIME_FOREVER))
336 goto input_failure;
339 ** Consume leading white space, except for formats
340 ** that suppress this.
343 if ((flags & NOSKIP) == 0)
345 while (isspace(*fp->f_p))
347 nread++;
348 if (--fp->f_r > 0)
349 fp->f_p++;
350 else if (sm_refill(fp, SM_TIME_FOREVER))
351 goto input_failure;
354 ** Note that there is at least one character in
355 ** the buffer, so conversions that do not set NOSKIP
356 ** can no longer result in an input failure.
360 /* Do the conversion. */
361 switch (c)
363 case CT_CHAR:
364 /* scan arbitrary characters (sets NOSKIP) */
365 if (width == 0)
366 width = 1;
367 if (flags & SUPPRESS)
369 size_t sum = 0;
370 for (;;)
372 if ((size_t) (n = fp->f_r) < width)
374 sum += n;
375 width -= n;
376 fp->f_p += n;
377 if (sm_refill(fp,
378 SM_TIME_FOREVER))
380 if (sum == 0)
381 goto input_failure;
382 break;
385 else
387 sum += width;
388 fp->f_r -= width;
389 fp->f_p += width;
390 break;
393 nread += sum;
395 else
397 size_t r;
399 r = sm_io_read(fp, SM_TIME_FOREVER,
400 (void *) SM_VA_ARG(ap, char *),
401 width);
402 if (r == 0)
403 goto input_failure;
404 nread += r;
405 nassigned++;
407 break;
409 case CT_CCL:
410 /* scan a (nonempty) character class (sets NOSKIP) */
411 if (width == 0)
412 width = (size_t)~0; /* `infinity' */
414 /* take only those things in the class */
415 if (flags & SUPPRESS)
417 n = 0;
418 while (ccltab[*fp->f_p] != '\0')
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))
426 if (n == 0) /* XXX how? */
427 goto input_failure;
428 break;
431 if (n == 0)
432 goto match_failure;
434 else
436 p0 = p = SM_VA_ARG(ap, char *);
437 while (ccltab[*fp->f_p] != '\0')
439 fp->f_r--;
440 *p++ = *fp->f_p++;
441 if (--width == 0)
442 break;
443 if (fp->f_r <= 0 &&
444 sm_refill(fp, SM_TIME_FOREVER))
446 if (p == p0)
447 goto input_failure;
448 break;
451 n = p - p0;
452 if (n == 0)
453 goto match_failure;
454 *p = 0;
455 nassigned++;
457 nread += n;
458 break;
460 case CT_STRING:
461 /* like CCL, but zero-length string OK, & no NOSKIP */
462 if (width == 0)
463 width = (size_t)~0;
464 if (flags & SUPPRESS)
466 n = 0;
467 while (!isspace(*fp->f_p))
469 n++, fp->f_r--, fp->f_p++;
470 if (--width == 0)
471 break;
472 if (fp->f_r <= 0 &&
473 sm_refill(fp, SM_TIME_FOREVER))
474 break;
476 nread += n;
478 else
480 p0 = p = SM_VA_ARG(ap, char *);
481 while (!isspace(*fp->f_p))
483 fp->f_r--;
484 *p++ = *fp->f_p++;
485 if (--width == 0)
486 break;
487 if (fp->f_r <= 0 &&
488 sm_refill(fp, SM_TIME_FOREVER))
489 break;
491 *p = 0;
492 nread += p - p0;
493 nassigned++;
495 continue;
497 case CT_INT:
498 /* scan an integer as if by strtoll/strtoull */
499 #if SM_CONF_BROKEN_SIZE_T
500 if (width == 0 || width > sizeof(buf) - 1)
501 width = sizeof(buf) - 1;
502 #else /* SM_CONF_BROKEN_SIZE_T */
503 /* size_t is unsigned, hence this optimisation */
504 if (--width > sizeof(buf) - 2)
505 width = sizeof(buf) - 2;
506 width++;
507 #endif /* SM_CONF_BROKEN_SIZE_T */
508 flags |= SIGNOK | NDIGITS | NZDIGITS;
509 for (p = buf; width > 0; width--)
511 c = *fp->f_p;
514 ** Switch on the character; `goto ok'
515 ** if we accept it as a part of number.
518 switch (c)
522 ** The digit 0 is always legal, but is
523 ** special. For %i conversions, if no
524 ** digits (zero or nonzero) have been
525 ** scanned (only signs), we will have
526 ** base==0. In that case, we should set
527 ** it to 8 and enable 0x prefixing.
528 ** Also, if we have not scanned zero digits
529 ** before this, do not turn off prefixing
530 ** (someone else will turn it off if we
531 ** have scanned any nonzero digits).
534 case '0':
535 if (base == 0)
537 base = 8;
538 flags |= PFXOK;
540 if (flags & NZDIGITS)
541 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
542 else
543 flags &= ~(SIGNOK|PFXOK|NDIGITS);
544 goto ok;
546 /* 1 through 7 always legal */
547 case '1': case '2': case '3':
548 case '4': case '5': case '6': case '7':
549 base = basefix[base];
550 flags &= ~(SIGNOK | PFXOK | NDIGITS);
551 goto ok;
553 /* digits 8 and 9 ok iff decimal or hex */
554 case '8': case '9':
555 base = basefix[base];
556 if (base <= 8)
557 break; /* not legal here */
558 flags &= ~(SIGNOK | PFXOK | NDIGITS);
559 goto ok;
561 /* letters ok iff hex */
562 case 'A': case 'B': case 'C':
563 case 'D': case 'E': case 'F':
564 case 'a': case 'b': case 'c':
565 case 'd': case 'e': case 'f':
567 /* no need to fix base here */
568 if (base <= 10)
569 break; /* not legal here */
570 flags &= ~(SIGNOK | PFXOK | NDIGITS);
571 goto ok;
573 /* sign ok only as first character */
574 case '+': case '-':
575 if (flags & SIGNOK)
577 flags &= ~SIGNOK;
578 goto ok;
580 break;
582 /* x ok iff flag still set & 2nd char */
583 case 'x': case 'X':
584 if (flags & PFXOK && p == buf + 1)
586 base = 16; /* if %i */
587 flags &= ~PFXOK;
588 goto ok;
590 break;
594 ** If we got here, c is not a legal character
595 ** for a number. Stop accumulating digits.
598 break;
600 /* c is legal: store it and look at the next. */
601 *p++ = c;
602 if (--fp->f_r > 0)
603 fp->f_p++;
604 else if (sm_refill(fp, SM_TIME_FOREVER))
605 break; /* SM_IO_EOF */
609 ** If we had only a sign, it is no good; push
610 ** back the sign. If the number ends in `x',
611 ** it was [sign] '0' 'x', so push back the x
612 ** and treat it as [sign] '0'.
615 if (flags & NDIGITS)
617 if (p > buf)
618 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
619 *(unsigned char *)--p);
620 goto match_failure;
622 c = ((unsigned char *)p)[-1];
623 if (c == 'x' || c == 'X')
625 --p;
626 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
628 if ((flags & SUPPRESS) == 0)
630 ULONGLONG_T res;
632 *p = 0;
633 res = (*ccfn)(buf, (char **)NULL, base);
634 if (flags & POINTER)
635 *SM_VA_ARG(ap, void **) =
636 (void *)(long) res;
637 else if (flags & QUAD)
638 *SM_VA_ARG(ap, LONGLONG_T *) = res;
639 else if (flags & LONG)
640 *SM_VA_ARG(ap, long *) = res;
641 else if (flags & SHORT)
642 *SM_VA_ARG(ap, short *) = res;
643 else
644 *SM_VA_ARG(ap, int *) = res;
645 nassigned++;
647 nread += p - buf;
648 break;
650 case CT_FLOAT:
651 /* scan a floating point number as if by strtod */
652 if (width == 0 || width > sizeof(buf) - 1)
653 width = sizeof(buf) - 1;
654 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
655 for (p = buf; width; width--)
657 c = *fp->f_p;
660 ** This code mimicks the integer conversion
661 ** code, but is much simpler.
664 switch (c)
667 case '0': case '1': case '2': case '3':
668 case '4': case '5': case '6': case '7':
669 case '8': case '9':
670 flags &= ~(SIGNOK | NDIGITS);
671 goto fok;
673 case '+': case '-':
674 if (flags & SIGNOK)
676 flags &= ~SIGNOK;
677 goto fok;
679 break;
680 case '.':
681 if (flags & DPTOK)
683 flags &= ~(SIGNOK | DPTOK);
684 goto fok;
686 break;
687 case 'e': case 'E':
689 /* no exponent without some digits */
690 if ((flags&(NDIGITS|EXPOK)) == EXPOK)
692 flags =
693 (flags & ~(EXPOK|DPTOK)) |
694 SIGNOK | NDIGITS;
695 goto fok;
697 break;
699 break;
700 fok:
701 *p++ = c;
702 if (--fp->f_r > 0)
703 fp->f_p++;
704 else if (sm_refill(fp, SM_TIME_FOREVER))
705 break; /* SM_IO_EOF */
709 ** If no digits, might be missing exponent digits
710 ** (just give back the exponent) or might be missing
711 ** regular digits, but had sign and/or decimal point.
714 if (flags & NDIGITS)
716 if (flags & EXPOK)
718 /* no digits at all */
719 while (p > buf)
720 (void) sm_io_ungetc(fp,
721 SM_TIME_DEFAULT,
722 *(unsigned char *)--p);
723 goto match_failure;
726 /* just a bad exponent (e and maybe sign) */
727 c = *(unsigned char *) --p;
728 if (c != 'e' && c != 'E')
730 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
731 c); /* sign */
732 c = *(unsigned char *)--p;
734 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
736 if ((flags & SUPPRESS) == 0)
738 double res;
740 *p = 0;
741 res = strtod(buf, (char **) NULL);
742 if (flags & LONG)
743 *SM_VA_ARG(ap, double *) = res;
744 else
745 *SM_VA_ARG(ap, float *) = res;
746 nassigned++;
748 nread += p - buf;
749 break;
752 input_failure:
753 if (evt != NULL)
754 sm_clrevent(evt); /* undo our timeout */
755 return nassigned ? nassigned : -1;
756 match_failure:
757 if (evt != NULL)
758 sm_clrevent(evt); /* undo our timeout */
759 return nassigned;
763 ** SM_SCCL -- sequenced character comparison list
765 ** Fill in the given table from the scanset at the given format
766 ** (just after `['). Return a pointer to the character past the
767 ** closing `]'. The table has a 1 wherever characters should be
768 ** considered part of the scanset.
770 ** Parameters:
771 ** tab -- array flagging "active" char's to match (returned)
772 ** fmt -- character list (within "[]")
774 ** Results:
777 static unsigned char *
778 sm_sccl(tab, fmt)
779 register char *tab;
780 register unsigned char *fmt;
782 register int c, n, v;
784 /* first `clear' the whole table */
785 c = *fmt++; /* first char hat => negated scanset */
786 if (c == '^')
788 v = 1; /* default => accept */
789 c = *fmt++; /* get new first char */
791 else
792 v = 0; /* default => reject */
794 /* should probably use memset here */
795 for (n = 0; n < 256; n++)
796 tab[n] = v;
797 if (c == 0)
798 return fmt - 1; /* format ended before closing ] */
801 ** Now set the entries corresponding to the actual scanset
802 ** to the opposite of the above.
804 ** The first character may be ']' (or '-') without being special;
805 ** the last character may be '-'.
808 v = 1 - v;
809 for (;;)
811 tab[c] = v; /* take character c */
812 doswitch:
813 n = *fmt++; /* and examine the next */
814 switch (n)
817 case 0: /* format ended too soon */
818 return fmt - 1;
820 case '-':
822 ** A scanset of the form
823 ** [01+-]
824 ** is defined as `the digit 0, the digit 1,
825 ** the character +, the character -', but
826 ** the effect of a scanset such as
827 ** [a-zA-Z0-9]
828 ** is implementation defined. The V7 Unix
829 ** scanf treats `a-z' as `the letters a through
830 ** z', but treats `a-a' as `the letter a, the
831 ** character -, and the letter a'.
833 ** For compatibility, the `-' is not considerd
834 ** to define a range if the character following
835 ** it is either a close bracket (required by ANSI)
836 ** or is not numerically greater than the character
837 ** we just stored in the table (c).
840 n = *fmt;
841 if (n == ']' || n < c)
843 c = '-';
844 break; /* resume the for(;;) */
846 fmt++;
849 /* fill in the range */
850 tab[++c] = v;
851 } while (c < n);
852 #if 1 /* XXX another disgusting compatibility hack */
855 ** Alas, the V7 Unix scanf also treats formats
856 ** such as [a-c-e] as `the letters a through e'.
857 ** This too is permitted by the standard....
860 goto doswitch;
861 #else
862 c = *fmt++;
863 if (c == 0)
864 return fmt - 1;
865 if (c == ']')
866 return fmt;
867 break;
868 #endif
870 case ']': /* end of scanset */
871 return fmt;
873 default: /* just another character */
874 c = n;
875 break;
878 /* NOTREACHED */