1
/* __gmp_doscan -- formatted input internals.
3
THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
4
CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5
FUTURE GNU MP RELEASES.
7
Copyright 2001, 2002 Free Software Foundation, Inc.
9
This file is part of the GNU MP Library.
11
The GNU MP Library is free software; you can redistribute it and/or modify
12
it under the terms of the GNU Lesser General Public License as published by
13
the Free Software Foundation; either version 2.1 of the License, or (at your
14
option) any later version.
16
The GNU MP Library is distributed in the hope that it will be useful, but
17
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19
License for more details.
21
You should have received a copy of the GNU Lesser General Public License
22
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
23
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24
MA 02111-1307, USA. */
35
#include <stddef.h> /* for ptrdiff_t */
40
#include <locale.h> /* for localeconv */
44
# include <inttypes.h> /* for intmax_t */
52
#include <sys/types.h> /* for quad_t */
59
/* Change this to "#define TRACE(x) x" for some traces. */
63
/* It's necessary to parse up the string to recognise the GMP extra types F,
64
Q and Z. Other types and conversions are passed across to the standard
65
sscanf or fscanf via funs->scan, for ease of implemenation. This is
66
essential in the case of something like glibc %p where the pointer format
67
isn't actually documented.
69
Because funs->scan doesn't get the whole input it can't put the right
70
values in for %n, so that's handled in __gmp_doscan. Neither sscanf nor
71
fscanf directly indicate how many characters were read, so an extra %n is
72
appended to each run for that. For fscanf this merely supports our %n
73
output, but for sscanf it lets funs->step move us along the input string.
75
Whitespace and literal matches in the format string, including %%, are
76
handled directly within __gmp_doscan. This is reasonably efficient, and
77
avoids some suspicious behaviour observed in various system libc's.
78
GLIBC 2.2.4 for instance returns 0 on sscanf(" "," x") or on sscanf(" ",
79
" x%d",&n), whereas we think they should return EOF, since end-of-string
80
is reached when a match of "x" is required.
82
For standard % conversions, funs->scan is called once for each
83
conversion. If we had vfscanf and vsscanf and could rely on their fixed
84
text matching behaviour then we could call them with multiple consecutive
85
standard conversions. But plain fscanf and sscanf work fine, and parsing
86
one field at a time shouldn't be too much of a slowdown.
88
gmpscan reads a gmp type. It's only used from one place, but is a
89
separate subroutine to avoid a big chunk of complicated code in the
90
middle of __gmp_doscan. Within gmpscan a couple of loopbacks make it
91
possible to share code for parsing integers, rationals and floats.
93
In gmpscan normally one char of lookahead is maintained, but when width
94
is reached that stops, on the principle that an fgetc/ungetc of a char
95
past where we're told to stop would be undesirable. "chars" is how many
96
characters have been read so far, including the current c. When
97
chars==width and another character is desired then a jump is done to the
98
"convert" stage. c is invalid and mustn't be unget'ed in this case;
99
chars is set to width+1 to indicate that.
101
gmpscan normally returns the number of characters read. -1 means an
102
invalid field, like a "-" or "+" alone. -2 means EOF reached before any
103
matching characters were read.
105
Consideration was given to using a separate code for gmp_fscanf and
106
gmp_sscanf. The sscanf case could zip across a string making literal
107
matches or recognising digits in gmpscan, rather than making a function
108
call fun->get per character. The fscanf could use getc rather than fgetc
109
too, which might help those systems where getc is a macro or otherwise
110
inlined. But none of this scanning and converting will be particularly
111
fast, so the two are done together to keep it a bit simpler for now.
115
A way to read the GLIBC printf %a format that we support in gmp_printf
116
would be good. That would probably be good for plain GLIBC scanf too, so
117
perhaps we can simply follow its lead if it gets such a feature in the
121
struct gmp_doscan_params_t {
131
ASSERT (chars <= width); \
135
(c) = (*funs->get) (data); \
138
/* store into "s", extending if necessary */
141
ASSERT (s_upto <= s_alloc); \
142
if (s_upto >= s_alloc) \
144
size_t s_alloc_new = s_alloc + S_ALLOC_STEP; \
145
s = __GMP_REALLOCATE_FUNC_TYPE (s, s_alloc, s_alloc_new, char); \
146
s_alloc = s_alloc_new; \
151
#define S_ALLOC_STEP 512
154
gmpscan (const struct gmp_doscan_funs_t *funs, void *data,
155
const struct gmp_doscan_params_t *p, void *dst)
157
int chars, c, base, first, width, seen_point, seen_digit;
158
size_t s_upto, s_alloc;
162
TRACE (printf ("gmpscan\n"));
164
ASSERT (p->type == 'F' || p->type == 'Q' || p->type == 'Z');
166
c = (*funs->get) (data);
174
width = (p->width == 0 ? INT_MAX-1 : p->width);
176
s_alloc = S_ALLOC_STEP;
177
s = __GMP_ALLOCATE_FUNC_TYPE (s_alloc, char);
188
/* don't store '+', it's not accepted by mpz_set_str etc */
202
if (c == 'x' || c == 'X')
217
if (! (isascii (c) && isxdigit (c)))
222
if (! (isascii (c) && isdigit (c)))
224
if (base == 8 && (c == '8' || c == '9'))
236
if (p->type == 'F' && ! seen_point)
239
/* For a multi-character decimal point, if the first character is
240
present then all of it must be, otherwise the input is
241
considered invalid. */
244
point = localeconv()->decimal_point;
265
goto store_get_digits;
271
if (p->type == 'F' && (c == 'e' || c == 'E'))
273
/* must have at least one digit in the mantissa, just an exponent
274
is not good enough */
286
if (p->type == 'Q' && c == '/')
288
/* must have at least one digit in the numerator */
292
/* now look for at least one digit in the denominator */
295
/* allow the base to be redetermined for "%i" */
312
TRACE (printf (" convert \"%s\"\n", s));
314
/* We ought to have parsed out a valid string above, so just test
315
mpz_set_str etc with an ASSERT. */
318
ASSERT (p->base == 10);
319
ASSERT_NOCARRY (mpf_set_str ((mpf_ptr) dst, s, 10));
322
ASSERT_NOCARRY (mpq_set_str ((mpq_ptr) dst, s, p->base));
325
ASSERT_NOCARRY (mpz_set_str ((mpz_ptr) dst, s, p->base));
335
ASSERT (chars <= width+1);
336
if (chars != width+1)
338
(*funs->unget) (c, data);
339
TRACE (printf (" ungetc %d, to give %d chars\n", c, chars-1));
343
(*__gmp_free_func) (s, s_alloc);
347
TRACE (printf (" invalid\n"));
351
TRACE (printf (" return %d chars (cf width %d)\n", chars, width));
356
/* Read and discard whitespace, if any. Return number of chars skipped.
357
Whitespace skipping never provokes the EOF return from __gmp_doscan, so
358
it's not necessary to watch for EOF from funs->get, */
360
skip_white (const struct gmp_doscan_funs_t *funs, void *data)
367
c = (funs->get) (data);
370
while (isascii (c) && isspace (c));
372
(funs->unget) (c, data);
375
TRACE (printf (" skip white %d\n", ret));
381
__gmp_doscan (const struct gmp_doscan_funs_t *funs, void *data,
382
const char *orig_fmt, va_list orig_ap)
384
struct gmp_doscan_params_t param;
387
const char *fmt, *this_fmt, *end_fmt;
388
size_t orig_fmt_len, alloc_fmt_size, len;
389
int new_fields, new_chars;
394
TRACE (printf ("__gmp_doscan \"%s\"\n", orig_fmt);
395
if (funs->scan == (gmp_doscan_scan_t) sscanf)
396
printf (" s=\"%s\"\n", (const char *) data));
398
/* Don't modify orig_ap, if va_list is actually an array and hence call by
399
reference. It could be argued that it'd be more efficient to leave
400
callers to make a copy if they care, but doing so here is going to be a
401
very small part of the total work, and we may as well keep applications
403
va_copy (ap, orig_ap);
405
/* Parts of the format string are going to be copied so that a " %n" can
406
be appended. alloc_fmt is some space for that. orig_fmt_len+4 will be
407
needed if fmt consists of a single "%" specifier, but otherwise is an
408
overestimate. We're not going to be very fast here, so use
409
__gmp_allocate_func rather than TMP_ALLOC. */
410
orig_fmt_len = strlen (orig_fmt);
411
alloc_fmt_size = orig_fmt_len + 4;
412
alloc_fmt = __GMP_ALLOCATE_FUNC_TYPE (alloc_fmt_size, char);
415
end_fmt = orig_fmt + orig_fmt_len;
425
if (isascii (fchar) && isspace (fchar))
427
chars += skip_white (funs, data);
435
c = (funs->get) (data);
438
(funs->unget) (c, data);
457
TRACE (printf (" this_fmt \"%s\"\n", this_fmt));
461
ASSERT (fmt <= end_fmt);
466
case '\0': /* unterminated % sequence */
470
case '%': /* literal % */
473
case '[': /* character range */
477
/* ']' allowed as the first char (possibly after '^') */
482
ASSERT (fmt <= end_fmt);
485
/* unterminated % sequence */
494
case 'c': /* characters */
495
case 's': /* string of non-whitespace */
496
case 'p': /* pointer */
498
len = fmt - this_fmt;
499
memcpy (alloc_fmt, this_fmt, len);
500
alloc_fmt[len++] = '%';
501
alloc_fmt[len++] = 'n';
502
alloc_fmt[len] = '\0';
504
TRACE (printf (" scan \"%s\"\n", alloc_fmt);
505
if (funs->scan == (gmp_doscan_scan_t) sscanf)
506
printf (" s=\"%s\"\n", (const char *) data));
511
new_fields = (*funs->scan) (data, alloc_fmt, &new_chars);
512
ASSERT (new_fields == 0 || new_fields == EOF);
516
new_fields = (*funs->scan) (data, alloc_fmt,
517
va_arg (ap, void *), &new_chars);
518
ASSERT (new_fields==0 || new_fields==1 || new_fields==EOF);
521
goto done; /* invalid input */
524
ASSERT (new_chars != -1);
526
TRACE (printf (" new_fields %d new_chars %d\n",
527
new_fields, new_chars));
529
if (new_fields == -1)
530
goto eof_no_match; /* EOF before anything matched */
532
/* Wnder param.ignore, when new_fields==0 we don't know if
533
it's a successful match or an invalid field. new_chars
534
won't have been assigned if it was an invalid field. */
536
goto done; /* invalid input */
539
(*funs->step) (data, new_chars);
546
case 'd': /* decimal */
547
case 'e': /* float */
548
case 'E': /* float */
549
case 'f': /* float */
550
case 'g': /* float */
551
case 'G': /* float */
552
case 'u': /* decimal */
554
if (param.type != 'F' && param.type != 'Q' && param.type != 'Z')
557
chars += skip_white (funs, data);
559
new_chars = gmpscan (funs, data, ¶m,
560
param.ignore ? NULL : va_arg (ap, void*));
566
ASSERT (new_chars >= 0);
568
goto increment_fields;
570
case 'a': /* glibc allocate string */
571
case '\'': /* glibc digit groupings */
574
case 'F': /* mpf_t */
575
case 'j': /* intmax_t */
576
case 'L': /* long long */
577
case 'q': /* quad_t */
578
case 'Q': /* mpq_t */
579
case 't': /* ptrdiff_t */
580
case 'z': /* size_t */
581
case 'Z': /* mpz_t */
586
case 'h': /* short or char */
587
if (param.type != 'h')
589
param.type = 'H'; /* internal code for "hh" */
596
case 'l': /* long, long long, double or long double */
597
if (param.type != 'l')
599
param.type = 'L'; /* "ll" means "L" */
606
p = va_arg (ap, void *);
607
TRACE (printf (" store %%n to %p\n", p));
608
switch (param.type) {
609
case '\0': * (int *) p = chars; break;
610
case 'F': mpf_set_si ((mpf_ptr) p, (long) chars); break;
611
case 'H': * (char *) p = chars; break;
612
case 'h': * (short *) p = chars; break;
614
case 'j': * (intmax_t *) p = chars; break;
616
case 'j': ASSERT_FAIL (intmax_t not available); break;
618
case 'l': * (long *) p = chars; break;
619
#if HAVE_QUAD_T && HAVE_LONG_LONG
621
ASSERT_ALWAYS (sizeof (quad_t) == sizeof (long long));
624
case 'q': ASSERT_FAIL (quad_t not available); break;
627
case 'L': * (long long *) p = chars; break;
629
case 'L': ASSERT_FAIL (long long not available); break;
631
case 'Q': mpq_set_si ((mpq_ptr) p, (long) chars, 1L); break;
633
case 't': * (ptrdiff_t *) p = chars; break;
635
case 't': ASSERT_FAIL (ptrdiff_t not available); break;
637
case 'z': * (size_t *) p = chars; break;
638
case 'Z': mpz_set_si ((mpz_ptr) p, (long) chars); break;
639
default: ASSERT (0); break;
653
case '0': case '1': case '2': case '3': case '4':
654
case '5': case '6': case '7': case '8': case '9':
657
param.width = param.width * 10 + (fchar-'0');
659
} while (isascii (fchar) && isdigit (fchar));
660
fmt--; /* unget the non-digit */
668
/* something invalid in a % sequence */
676
(*__gmp_free_func) (alloc_fmt, alloc_fmt_size);