[08053f7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Petr Koupy
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libposix
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Implementation of the scanf backend.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #define LIBPOSIX_INTERNAL
|
---|
| 36 |
|
---|
[a3da2b2] | 37 | #include "posix/assert.h"
|
---|
| 38 | #include "posix/errno.h"
|
---|
[08053f7] | 39 |
|
---|
[a3da2b2] | 40 | #include "posix/stdio.h"
|
---|
| 41 | #include "posix/stdlib.h"
|
---|
| 42 | #include "posix/stddef.h"
|
---|
| 43 | #include "posix/string.h"
|
---|
| 44 | #include "posix/ctype.h"
|
---|
| 45 | #include "posix/sys/types.h"
|
---|
[08053f7] | 46 |
|
---|
| 47 | #include "../internal/common.h"
|
---|
[a3da2b2] | 48 | #include "libc/malloc.h"
|
---|
[3e6a98c5] | 49 | #include "libc/stdbool.h"
|
---|
[08053f7] | 50 |
|
---|
| 51 | /** Unified data type for possible data sources for scanf. */
|
---|
| 52 | typedef union __data_source {
|
---|
| 53 | FILE *stream; /**< Input file stream. */
|
---|
| 54 | const char *string; /**< Input string. */
|
---|
| 55 | } _data_source;
|
---|
| 56 |
|
---|
| 57 | /** Internal state of the input provider. */
|
---|
| 58 | enum {
|
---|
| 59 | /** Partly constructed but not yet functional. */
|
---|
| 60 | _PROV_CONSTRUCTED,
|
---|
| 61 | /** Ready to serve any request. */
|
---|
| 62 | _PROV_READY,
|
---|
| 63 | /** Cursor is temporarily lent to the external entity. No action is
|
---|
| 64 | * possible until the cursor is returned. */
|
---|
| 65 | _PROV_CURSOR_LENT,
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | /** Universal abstraction over data input for scanf. */
|
---|
| 69 | typedef struct __input_provider {
|
---|
| 70 | /** Source of data elements. */
|
---|
| 71 | _data_source source;
|
---|
| 72 | /** How many elements was already processed. */
|
---|
| 73 | int consumed;
|
---|
| 74 | /** How many elements was already fetched from the source. */
|
---|
| 75 | int fetched;
|
---|
| 76 | /** Elements are fetched from the source in batches (e.g. by getline())
|
---|
| 77 | * to allow using strtol/strtod family even on streams. */
|
---|
| 78 | char *window;
|
---|
| 79 | /** Size of the current window. */
|
---|
| 80 | size_t window_size;
|
---|
| 81 | /** Points to the next element to be processed inside the current window. */
|
---|
| 82 | const char *cursor;
|
---|
| 83 | /** Internal state of the provider. */
|
---|
| 84 | int state;
|
---|
| 85 |
|
---|
| 86 | /** Take control over data source. Finish initialization of the internal
|
---|
| 87 | * structures (e.g. allocation of window). */
|
---|
| 88 | void (*capture)(struct __input_provider *);
|
---|
| 89 | /** Get a single element from the source and update the internal structures
|
---|
| 90 | * accordingly (e.g. greedy update of the window). Return -1 if the
|
---|
| 91 | * element cannot be obtained. */
|
---|
| 92 | int (*pop)(struct __input_provider *);
|
---|
| 93 | /** Undo the most recent not-undone pop operation. Might be necesarry to
|
---|
| 94 | * flush current window and seek data source backwards. Return 0 if the
|
---|
| 95 | * pop history is exhausted, non-zero on success. */
|
---|
| 96 | int (*undo)(struct __input_provider *);
|
---|
| 97 | /** Lend the cursor to the caller. */
|
---|
| 98 | const char * (*borrow_cursor)(struct __input_provider *);
|
---|
| 99 | /** Take control over possibly incremented cursor and update the internal
|
---|
| 100 | * structures if necessary. */
|
---|
| 101 | void (*return_cursor)(struct __input_provider *, const char *);
|
---|
| 102 | /** Release the control over the source. That is, synchronize any
|
---|
| 103 | * fetched but non-consumed elements (e.g. by seeking) and destruct
|
---|
| 104 | * internal structures (e.g. window deallocation). */
|
---|
| 105 | void (*release)(struct __input_provider *);
|
---|
| 106 | } _input_provider;
|
---|
| 107 |
|
---|
| 108 | /** @see __input_provider */
|
---|
| 109 | static void _capture_stream(_input_provider *self)
|
---|
| 110 | {
|
---|
| 111 | assert(self->source.stream);
|
---|
| 112 | assert(self->state == _PROV_CONSTRUCTED);
|
---|
| 113 | /* Caller could already pre-allocated the window. */
|
---|
| 114 | assert((self->window == NULL && self->window_size == 0) ||
|
---|
[12fb8498] | 115 | (self->window && self->window_size > 0));
|
---|
[08053f7] | 116 |
|
---|
| 117 | /* Initialize internal structures. */
|
---|
| 118 | self->consumed = 0;
|
---|
| 119 | ssize_t fetched = posix_getline(
|
---|
[12fb8498] | 120 | &self->window, &self->window_size, self->source.stream);
|
---|
[08053f7] | 121 | if (fetched != -1) {
|
---|
| 122 | self->fetched = fetched;
|
---|
| 123 | self->cursor = self->window;
|
---|
| 124 | } else {
|
---|
| 125 | /* EOF encountered. */
|
---|
| 126 | self->fetched = 0;
|
---|
| 127 | self->cursor = NULL;
|
---|
| 128 | }
|
---|
| 129 | self->state = _PROV_READY;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /** @see __input_provider */
|
---|
| 133 | static void _capture_string(_input_provider *self)
|
---|
| 134 | {
|
---|
| 135 | assert(self->source.string);
|
---|
| 136 | assert(self->state == _PROV_CONSTRUCTED);
|
---|
| 137 |
|
---|
| 138 | /* Initialize internal structures. */
|
---|
| 139 | self->consumed = 0;
|
---|
| 140 | self->fetched = posix_strlen(self->source.string);
|
---|
| 141 | self->window = (char *) self->source.string;
|
---|
| 142 | self->window_size = self->fetched + 1;
|
---|
| 143 | self->cursor = self->window;
|
---|
| 144 | self->state = _PROV_READY;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /** @see __input_provider */
|
---|
| 148 | static int _pop_stream(_input_provider *self)
|
---|
| 149 | {
|
---|
| 150 | assert(self->state == _PROV_READY);
|
---|
| 151 |
|
---|
| 152 | if (self->cursor) {
|
---|
| 153 | int c = *self->cursor;
|
---|
| 154 | ++self->consumed;
|
---|
| 155 | ++self->cursor;
|
---|
| 156 | /* Do we need to fetch a new line from the source? */
|
---|
| 157 | if (*self->cursor == '\0') {
|
---|
| 158 | ssize_t fetched = posix_getline(&self->window,
|
---|
[12fb8498] | 159 | &self->window_size, self->source.stream);
|
---|
[08053f7] | 160 | if (fetched != -1) {
|
---|
| 161 | self->fetched += fetched;
|
---|
| 162 | self->cursor = self->window;
|
---|
| 163 | } else {
|
---|
| 164 | /* EOF encountered. */
|
---|
| 165 | self->cursor = NULL;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | return c;
|
---|
| 169 | } else {
|
---|
| 170 | /* Already at EOF. */
|
---|
| 171 | return -1;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /** @see __input_provider */
|
---|
| 176 | static int _pop_string(_input_provider *self)
|
---|
| 177 | {
|
---|
| 178 | assert(self->state == _PROV_READY);
|
---|
| 179 |
|
---|
| 180 | if (*self->cursor != '\0') {
|
---|
| 181 | int c = *self->cursor;
|
---|
| 182 | ++self->consumed;
|
---|
| 183 | ++self->cursor;
|
---|
| 184 | return c;
|
---|
| 185 | } else {
|
---|
| 186 | /* String depleted. */
|
---|
| 187 | return -1;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /** @see __input_provider */
|
---|
| 192 | static int _undo_stream(_input_provider *self)
|
---|
| 193 | {
|
---|
| 194 | assert(self->state == _PROV_READY);
|
---|
| 195 |
|
---|
| 196 | if (self->consumed == 0) {
|
---|
| 197 | /* Undo history exhausted. */
|
---|
| 198 | return 0;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | if (!self->cursor || self->window == self->cursor) {
|
---|
| 202 | /* Complex case. Either at EOF (cursor == NULL) or there is no more
|
---|
| 203 | * place to retreat to inside the window. Seek the source backwards
|
---|
| 204 | * and flush the window. Regarding the scanf, this could happend only
|
---|
| 205 | * when matching unbounded string (%s) or unbounded scanset (%[) not
|
---|
[9c1984f] | 206 | * containing newline, while at the same time newline is the character
|
---|
[08053f7] | 207 | * that breaks the matching process. */
|
---|
| 208 | int rc = posix_fseek(
|
---|
| 209 | self->source.stream, -1, SEEK_CUR);
|
---|
| 210 | if (rc == -1) {
|
---|
| 211 | /* Seek failed. */
|
---|
| 212 | return 0;
|
---|
| 213 | }
|
---|
| 214 | ssize_t fetched = posix_getline(&self->window,
|
---|
[12fb8498] | 215 | &self->window_size, self->source.stream);
|
---|
[08053f7] | 216 | if (fetched != -1) {
|
---|
| 217 | assert(fetched == 1);
|
---|
| 218 | self->fetched = self->consumed + 1;
|
---|
| 219 | self->cursor = self->window;
|
---|
| 220 | } else {
|
---|
| 221 | /* Stream is broken. */
|
---|
| 222 | return 0;
|
---|
| 223 | }
|
---|
| 224 | } else {
|
---|
| 225 | /* Simple case. Still inside window. */
|
---|
| 226 | --self->cursor;
|
---|
| 227 | }
|
---|
| 228 | --self->consumed;
|
---|
| 229 | return 1; /* Success. */
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /** @see __input_provider */
|
---|
| 233 | static int _undo_string(_input_provider *self)
|
---|
| 234 | {
|
---|
| 235 | assert(self->state == _PROV_READY);
|
---|
| 236 |
|
---|
| 237 | if (self->consumed > 0) {
|
---|
| 238 | --self->consumed;
|
---|
| 239 | --self->cursor;
|
---|
| 240 | } else {
|
---|
| 241 | /* Undo history exhausted. */
|
---|
| 242 | return 0;
|
---|
| 243 | }
|
---|
| 244 | return 1; /* Success. */
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /** @see __input_provider */
|
---|
| 248 | static const char *_borrow_cursor_universal(_input_provider *self)
|
---|
| 249 | {
|
---|
| 250 | assert(self->state == _PROV_READY);
|
---|
| 251 |
|
---|
| 252 | self->state = _PROV_CURSOR_LENT;
|
---|
| 253 | return self->cursor;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /** @see __input_provider */
|
---|
| 257 | static void _return_cursor_stream(_input_provider *self, const char *cursor)
|
---|
| 258 | {
|
---|
| 259 | assert(self->state == _PROV_CURSOR_LENT);
|
---|
| 260 |
|
---|
| 261 | /* Check how much of the window did external entity consumed. */
|
---|
| 262 | self->consumed += cursor - self->cursor;
|
---|
| 263 | self->cursor = cursor;
|
---|
| 264 | if (*self->cursor == '\0') {
|
---|
| 265 | /* Window was completely consumed, fetch new data. */
|
---|
| 266 | ssize_t fetched = posix_getline(&self->window,
|
---|
[12fb8498] | 267 | &self->window_size, self->source.stream);
|
---|
[08053f7] | 268 | if (fetched != -1) {
|
---|
| 269 | self->fetched += fetched;
|
---|
| 270 | self->cursor = self->window;
|
---|
| 271 | } else {
|
---|
| 272 | /* EOF encountered. */
|
---|
| 273 | self->cursor = NULL;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | self->state = _PROV_READY;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /** @see __input_provider */
|
---|
| 280 | static void _return_cursor_string(_input_provider *self, const char *cursor)
|
---|
| 281 | {
|
---|
| 282 | assert(self->state == _PROV_CURSOR_LENT);
|
---|
| 283 |
|
---|
| 284 | /* Check how much of the window did external entity consumed. */
|
---|
| 285 | self->consumed += cursor - self->cursor;
|
---|
| 286 | self->cursor = cursor;
|
---|
| 287 | self->state = _PROV_READY;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /** @see __input_provider */
|
---|
| 291 | static void _release_stream(_input_provider *self)
|
---|
| 292 | {
|
---|
| 293 | assert(self->state == _PROV_READY);
|
---|
| 294 | assert(self->consumed >= self->fetched);
|
---|
| 295 |
|
---|
| 296 | /* Try to correct the difference between the stream position and what was
|
---|
| 297 | * actually consumed. If it is not possible, continue anyway. */
|
---|
| 298 | posix_fseek(self->source.stream, self->consumed - self->fetched, SEEK_CUR);
|
---|
| 299 |
|
---|
| 300 | /* Destruct internal structures. */
|
---|
| 301 | self->fetched = 0;
|
---|
| 302 | self->cursor = NULL;
|
---|
[a12f7f1] | 303 | if (self->window) {
|
---|
| 304 | free(self->window);
|
---|
| 305 | self->window = NULL;
|
---|
| 306 | }
|
---|
[08053f7] | 307 | self->window_size = 0;
|
---|
| 308 | self->state = _PROV_CONSTRUCTED;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | /** @see __input_provider */
|
---|
| 312 | static void _release_string(_input_provider *self)
|
---|
| 313 | {
|
---|
| 314 | assert(self->state == _PROV_READY);
|
---|
| 315 |
|
---|
| 316 | /* Destruct internal structures. */
|
---|
| 317 | self->fetched = 0;
|
---|
| 318 | self->cursor = NULL;
|
---|
| 319 | self->window = NULL;
|
---|
| 320 | self->window_size = 0;
|
---|
| 321 | self->state = _PROV_CONSTRUCTED;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | /** Length modifier values. */
|
---|
| 325 | enum {
|
---|
| 326 | LMOD_NONE,
|
---|
| 327 | LMOD_hh,
|
---|
| 328 | LMOD_h,
|
---|
| 329 | LMOD_l,
|
---|
| 330 | LMOD_ll,
|
---|
| 331 | LMOD_j,
|
---|
| 332 | LMOD_z,
|
---|
| 333 | LMOD_t,
|
---|
| 334 | LMOD_L,
|
---|
| 335 | LMOD_p, /* Reserved for %p conversion. */
|
---|
| 336 | };
|
---|
| 337 |
|
---|
| 338 | /**
|
---|
| 339 | * Decides whether provided characters specify length modifier. If so, the
|
---|
| 340 | * recognized modifier is stored through provider pointer.
|
---|
| 341 | *
|
---|
| 342 | * @param c Candidate on the length modifier.
|
---|
| 343 | * @param _c Next character (might be NUL).
|
---|
| 344 | * @param modifier Pointer to the modifier value.
|
---|
| 345 | * @return Whether the modifier was recognized or not.
|
---|
| 346 | */
|
---|
| 347 | static inline int is_length_mod(int c, int _c, int *modifier)
|
---|
| 348 | {
|
---|
| 349 | assert(modifier);
|
---|
| 350 |
|
---|
| 351 | switch (c) {
|
---|
| 352 | case 'h':
|
---|
[9c1984f] | 353 | /* Check whether the modifier was not already recognized. */
|
---|
| 354 | if (*modifier == LMOD_NONE) {
|
---|
| 355 | *modifier = _c == 'h' ? LMOD_hh : LMOD_h;
|
---|
| 356 | } else {
|
---|
| 357 | /* Format string is invalid. Notify the caller. */
|
---|
| 358 | *modifier = LMOD_NONE;
|
---|
| 359 | }
|
---|
[08053f7] | 360 | return 1;
|
---|
| 361 | case 'l':
|
---|
[9c1984f] | 362 | if (*modifier == LMOD_NONE) {
|
---|
| 363 | *modifier = _c == 'l' ? LMOD_ll : LMOD_l;
|
---|
| 364 | } else {
|
---|
| 365 | *modifier = LMOD_NONE;
|
---|
| 366 | }
|
---|
[08053f7] | 367 | return 1;
|
---|
| 368 | case 'j':
|
---|
[9c1984f] | 369 | *modifier = *modifier == LMOD_NONE ? LMOD_j : LMOD_NONE;
|
---|
[08053f7] | 370 | return 1;
|
---|
| 371 | case 'z':
|
---|
[9c1984f] | 372 | *modifier = *modifier == LMOD_NONE ? LMOD_z : LMOD_NONE;
|
---|
[08053f7] | 373 | return 1;
|
---|
| 374 | case 't':
|
---|
[9c1984f] | 375 | *modifier = *modifier == LMOD_NONE ? LMOD_t : LMOD_NONE;
|
---|
[08053f7] | 376 | return 1;
|
---|
| 377 | case 'L':
|
---|
[9c1984f] | 378 | *modifier = *modifier == LMOD_NONE ? LMOD_L : LMOD_NONE;
|
---|
[08053f7] | 379 | return 1;
|
---|
| 380 | default:
|
---|
| 381 | return 0;
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | /**
|
---|
| 386 | * Decides whether provided character specifies integer conversion. If so, the
|
---|
| 387 | * semantics of the conversion is stored through provided pointers..
|
---|
| 388 | *
|
---|
| 389 | * @param c Candidate on the integer conversion.
|
---|
| 390 | * @param is_unsigned Pointer to store whether the conversion is signed or not.
|
---|
| 391 | * @param base Pointer to store the base of the integer conversion.
|
---|
| 392 | * @return Whether the conversion was recognized or not.
|
---|
| 393 | */
|
---|
| 394 | static inline int is_int_conv(int c, bool *is_unsigned, int *base)
|
---|
| 395 | {
|
---|
| 396 | assert(is_unsigned && base);
|
---|
| 397 |
|
---|
| 398 | switch (c) {
|
---|
| 399 | case 'd':
|
---|
| 400 | *is_unsigned = false;
|
---|
| 401 | *base = 10;
|
---|
| 402 | return 1;
|
---|
| 403 | case 'i':
|
---|
| 404 | *is_unsigned = false;
|
---|
| 405 | *base = 0;
|
---|
| 406 | return 1;
|
---|
| 407 | case 'o':
|
---|
| 408 | *is_unsigned = true;
|
---|
| 409 | *base = 8;
|
---|
| 410 | return 1;
|
---|
| 411 | case 'u':
|
---|
| 412 | *is_unsigned = true;
|
---|
| 413 | *base = 10;
|
---|
| 414 | return 1;
|
---|
| 415 | case 'p': /* According to POSIX, %p modifier is implementation defined but
|
---|
| 416 | * must correspond to its printf counterpart. */
|
---|
| 417 | case 'x':
|
---|
| 418 | case 'X':
|
---|
| 419 | *is_unsigned = true;
|
---|
| 420 | *base = 16;
|
---|
| 421 | return 1;
|
---|
| 422 | return 1;
|
---|
| 423 | default:
|
---|
| 424 | return 0;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
| 429 | * Decides whether provided character specifies conversion of the floating
|
---|
| 430 | * point number.
|
---|
| 431 | *
|
---|
| 432 | * @param c Candidate on the floating point conversion.
|
---|
| 433 | * @return Whether the conversion was recognized or not.
|
---|
| 434 | */
|
---|
| 435 | static inline int is_float_conv(int c)
|
---|
| 436 | {
|
---|
| 437 | switch (c) {
|
---|
| 438 | case 'a':
|
---|
| 439 | case 'A':
|
---|
| 440 | case 'e':
|
---|
| 441 | case 'E':
|
---|
| 442 | case 'f':
|
---|
| 443 | case 'F':
|
---|
| 444 | case 'g':
|
---|
| 445 | case 'G':
|
---|
| 446 | return 1;
|
---|
| 447 | default:
|
---|
| 448 | return 0;
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | /**
|
---|
| 453 | * Decides whether provided character specifies conversion of the character
|
---|
| 454 | * sequence.
|
---|
| 455 | *
|
---|
| 456 | * @param c Candidate on the character sequence conversion.
|
---|
| 457 | * @param modifier Pointer to store length modifier for wide chars.
|
---|
| 458 | * @return Whether the conversion was recognized or not.
|
---|
| 459 | */
|
---|
| 460 | static inline int is_seq_conv(int c, int *modifier)
|
---|
| 461 | {
|
---|
| 462 | assert(modifier);
|
---|
| 463 |
|
---|
| 464 | switch (c) {
|
---|
| 465 | case 'S':
|
---|
| 466 | *modifier = LMOD_l;
|
---|
| 467 | /* fallthrough */
|
---|
| 468 | case 's':
|
---|
| 469 | return 1;
|
---|
| 470 | case 'C':
|
---|
| 471 | *modifier = LMOD_l;
|
---|
| 472 | /* fallthrough */
|
---|
| 473 | case 'c':
|
---|
| 474 | return 1;
|
---|
| 475 | case '[':
|
---|
| 476 | return 1;
|
---|
| 477 | default:
|
---|
| 478 | return 0;
|
---|
| 479 | }
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | /**
|
---|
| 483 | * Backend for the whole family of scanf functions. Uses input provider
|
---|
| 484 | * to abstract over differences between strings and streams. Should be
|
---|
| 485 | * POSIX compliant (apart from the not supported stuff).
|
---|
| 486 | *
|
---|
| 487 | * NOT SUPPORTED: locale (see strtold), wide chars, numbered output arguments
|
---|
| 488 | *
|
---|
| 489 | * @param in Input provider.
|
---|
| 490 | * @param fmt Format description.
|
---|
| 491 | * @param arg Output arguments.
|
---|
| 492 | * @return The number of converted output items or EOF on failure.
|
---|
| 493 | */
|
---|
| 494 | static inline int _internal_scanf(
|
---|
| 495 | _input_provider *in, const char *restrict fmt, va_list arg)
|
---|
| 496 | {
|
---|
| 497 | int c = -1;
|
---|
| 498 | int converted_cnt = 0;
|
---|
| 499 | bool converting = false;
|
---|
| 500 | bool matching_failure = false;
|
---|
| 501 |
|
---|
| 502 | bool assign_supress = false;
|
---|
| 503 | bool assign_alloc = false;
|
---|
| 504 | long width = -1;
|
---|
| 505 | int length_mod = LMOD_NONE;
|
---|
| 506 | bool int_conv_unsigned = false;
|
---|
| 507 | int int_conv_base = 0;
|
---|
| 508 |
|
---|
| 509 | /* Buffers allocated by scanf for optional 'm' specifier must be remembered
|
---|
| 510 | * to deallocaate them in case of an error. Because each of those buffers
|
---|
| 511 | * corresponds to one of the argument from va_list, there is an upper bound
|
---|
| 512 | * on the number of those arguments. In case of C99, this uppper bound is
|
---|
| 513 | * 127 arguments. */
|
---|
| 514 | char *buffers[127];
|
---|
| 515 | for (int i = 0; i < 127; ++i) {
|
---|
| 516 | buffers[i] = NULL;
|
---|
| 517 | }
|
---|
| 518 | int next_unused_buffer_idx = 0;
|
---|
| 519 |
|
---|
| 520 | in->capture(in);
|
---|
| 521 |
|
---|
| 522 | /* Interpret format string. Control shall prematurely jump from the cycle
|
---|
| 523 | * on input failure, matching failure or illegal format string. In order
|
---|
| 524 | * to keep error reporting simple enough and to keep input consistent,
|
---|
| 525 | * error condition shall be always manifested as jump from the cycle,
|
---|
| 526 | * not function return. Format string pointer shall be updated specifically
|
---|
| 527 | * for each sub-case (i.e. there shall be no loop-wide increment).*/
|
---|
| 528 | while (*fmt) {
|
---|
| 529 |
|
---|
| 530 | if (converting) {
|
---|
| 531 |
|
---|
| 532 | /* Processing inside conversion specifier. Either collect optional
|
---|
| 533 | * parameters or execute the conversion. When the conversion
|
---|
| 534 | * is successfully completed, increment conversion count and switch
|
---|
| 535 | * back to normal mode. */
|
---|
| 536 | if (*fmt == '*') {
|
---|
| 537 | /* Assignment-supression (optional). */
|
---|
| 538 | if (assign_supress) {
|
---|
| 539 | /* Already set. Illegal format string. */
|
---|
| 540 | break;
|
---|
| 541 | }
|
---|
| 542 | assign_supress = true;
|
---|
| 543 | ++fmt;
|
---|
| 544 | } else if (*fmt == 'm') {
|
---|
| 545 | /* Assignment-allocation (optional). */
|
---|
| 546 | if (assign_alloc) {
|
---|
| 547 | /* Already set. Illegal format string. */
|
---|
| 548 | break;
|
---|
| 549 | }
|
---|
| 550 | assign_alloc = true;
|
---|
| 551 | ++fmt;
|
---|
| 552 | } else if (*fmt == '$') {
|
---|
| 553 | /* Reference to numbered output argument. */
|
---|
| 554 | // TODO
|
---|
| 555 | not_implemented();
|
---|
| 556 | } else if (isdigit(*fmt)) {
|
---|
| 557 | /* Maximum field length (optional). */
|
---|
| 558 | if (width != -1) {
|
---|
| 559 | /* Already set. Illegal format string. */
|
---|
| 560 | break;
|
---|
| 561 | }
|
---|
| 562 | char *fmt_new = NULL;
|
---|
[9c1984f] | 563 | width = posix_strtol(fmt, &fmt_new, 10);
|
---|
[08053f7] | 564 | if (width != 0) {
|
---|
| 565 | fmt = fmt_new;
|
---|
| 566 | } else {
|
---|
| 567 | /* Since POSIX requires width to be non-zero, it is
|
---|
| 568 | * sufficient to interpret zero width as error without
|
---|
| 569 | * referring to errno. */
|
---|
| 570 | break;
|
---|
| 571 | }
|
---|
| 572 | } else if (is_length_mod(*fmt, *(fmt + 1), &length_mod)) {
|
---|
| 573 | /* Length modifier (optional). */
|
---|
| 574 | if (length_mod == LMOD_NONE) {
|
---|
| 575 | /* Already set. Illegal format string. The actual detection
|
---|
| 576 | * is carried out in the is_length_mod(). */
|
---|
| 577 | break;
|
---|
| 578 | }
|
---|
| 579 | if (length_mod == LMOD_hh || length_mod == LMOD_ll) {
|
---|
| 580 | /* Modifier was two characters long. */
|
---|
| 581 | ++fmt;
|
---|
| 582 | }
|
---|
| 583 | ++fmt;
|
---|
| 584 | } else if (is_int_conv(*fmt, &int_conv_unsigned, &int_conv_base)) {
|
---|
| 585 | /* Integer conversion. */
|
---|
| 586 |
|
---|
| 587 | /* Check sanity of optional parts of conversion specifier. */
|
---|
| 588 | if (assign_alloc || length_mod == LMOD_L) {
|
---|
| 589 | /* Illegal format string. */
|
---|
| 590 | break;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | /* Conversion of the integer with %p specifier needs special
|
---|
| 594 | * handling, because it is not allowed to have arbitrary
|
---|
| 595 | * length modifier. */
|
---|
| 596 | if (*fmt == 'p') {
|
---|
| 597 | if (length_mod == LMOD_NONE) {
|
---|
| 598 | length_mod = LMOD_p;
|
---|
| 599 | } else {
|
---|
| 600 | /* Already set. Illegal format string. */
|
---|
| 601 | break;
|
---|
| 602 | }
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | /* First consume any white spaces, so we can borrow cursor
|
---|
| 606 | * from the input provider. This way, the cursor will either
|
---|
| 607 | * point to the non-white space while the input will be
|
---|
| 608 | * prefetched up to the newline (which is suitable for strtol),
|
---|
| 609 | * or the input will be at EOF. */
|
---|
| 610 | do {
|
---|
| 611 | c = in->pop(in);
|
---|
| 612 | } while (isspace(c));
|
---|
| 613 |
|
---|
| 614 | /* After skipping the white spaces, can we actually continue? */
|
---|
| 615 | if (c == -1) {
|
---|
| 616 | /* Input failure. */
|
---|
| 617 | break;
|
---|
| 618 | } else {
|
---|
| 619 | /* Everything is OK, just undo the last pop, so the cursor
|
---|
| 620 | * can be borrowed. */
|
---|
| 621 | in->undo(in);
|
---|
| 622 | }
|
---|
[9c1984f] | 623 |
|
---|
[08053f7] | 624 | const char *cur_borrowed = NULL;
|
---|
| 625 | const char *cur_limited = NULL;
|
---|
| 626 | char *cur_updated = NULL;
|
---|
| 627 |
|
---|
| 628 | /* Borrow the cursor. Until it is returned to the provider
|
---|
| 629 | * we cannot jump from the cycle, because it would leave
|
---|
| 630 | * the input inconsistent. */
|
---|
| 631 | cur_borrowed = in->borrow_cursor(in);
|
---|
| 632 |
|
---|
| 633 | /* If the width is limited, the cursor horizont must be
|
---|
| 634 | * decreased accordingly. Otherwise the strtol could read more
|
---|
| 635 | * than allowed by width. */
|
---|
| 636 | if (width != -1) {
|
---|
| 637 | cur_limited = posix_strndup(cur_borrowed, width);
|
---|
| 638 | } else {
|
---|
| 639 | cur_limited = cur_borrowed;
|
---|
| 640 | }
|
---|
| 641 | cur_updated = (char *) cur_limited;
|
---|
| 642 |
|
---|
| 643 | long long sres = 0;
|
---|
| 644 | unsigned long long ures = 0;
|
---|
| 645 | errno = 0; /* Reset errno to recognize error later. */
|
---|
| 646 | /* Try to convert the integer. */
|
---|
| 647 | if (int_conv_unsigned) {
|
---|
| 648 | ures = posix_strtoull(cur_limited, &cur_updated, int_conv_base);
|
---|
| 649 | } else {
|
---|
| 650 | sres = posix_strtoll(cur_limited, &cur_updated, int_conv_base);
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | /* Update the cursor so it can be returned to the provider. */
|
---|
| 654 | cur_borrowed += cur_updated - cur_limited;
|
---|
[a12f7f1] | 655 | if (width != -1 && cur_limited != NULL) {
|
---|
[08053f7] | 656 | /* Deallocate duplicated part of the cursor view. */
|
---|
| 657 | free(cur_limited);
|
---|
| 658 | }
|
---|
| 659 | cur_limited = NULL;
|
---|
| 660 | cur_updated = NULL;
|
---|
| 661 | /* Return the cursor to the provider. Input consistency is again
|
---|
| 662 | * the job of the provider, so we can report errors from
|
---|
| 663 | * now on. */
|
---|
| 664 | in->return_cursor(in, cur_borrowed);
|
---|
| 665 | cur_borrowed = NULL;
|
---|
| 666 |
|
---|
| 667 | /* Check whether the conversion was successful. */
|
---|
| 668 | if (errno != EOK) {
|
---|
| 669 | matching_failure = true;
|
---|
| 670 | break;
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[9c1984f] | 673 | /* If not supressed, assign the converted integer into
|
---|
[08053f7] | 674 | * the next output argument. */
|
---|
| 675 | if (!assign_supress) {
|
---|
| 676 | if (int_conv_unsigned) {
|
---|
| 677 | switch (length_mod) {
|
---|
| 678 | case LMOD_hh: ; /* Label cannot be part of declaration. */
|
---|
| 679 | unsigned char *phh = va_arg(arg, unsigned char *);
|
---|
| 680 | *phh = (unsigned char) ures;
|
---|
| 681 | break;
|
---|
| 682 | case LMOD_h: ;
|
---|
| 683 | unsigned short *ph = va_arg(arg, unsigned short *);
|
---|
| 684 | *ph = (unsigned short) ures;
|
---|
| 685 | break;
|
---|
| 686 | case LMOD_NONE: ;
|
---|
| 687 | unsigned *pdef = va_arg(arg, unsigned *);
|
---|
| 688 | *pdef = (unsigned) ures;
|
---|
| 689 | break;
|
---|
| 690 | case LMOD_l: ;
|
---|
| 691 | unsigned long *pl = va_arg(arg, unsigned long *);
|
---|
| 692 | *pl = (unsigned long) ures;
|
---|
| 693 | break;
|
---|
| 694 | case LMOD_ll: ;
|
---|
| 695 | unsigned long long *pll = va_arg(arg, unsigned long long *);
|
---|
| 696 | *pll = (unsigned long long) ures;
|
---|
| 697 | break;
|
---|
| 698 | case LMOD_j: ;
|
---|
| 699 | posix_uintmax_t *pj = va_arg(arg, posix_uintmax_t *);
|
---|
| 700 | *pj = (posix_uintmax_t) ures;
|
---|
| 701 | break;
|
---|
| 702 | case LMOD_z: ;
|
---|
| 703 | size_t *pz = va_arg(arg, size_t *);
|
---|
| 704 | *pz = (size_t) ures;
|
---|
| 705 | break;
|
---|
| 706 | case LMOD_t: ;
|
---|
[6921178] | 707 | // XXX: What is unsigned counterpart of the ptrdiff_t?
|
---|
[08053f7] | 708 | size_t *pt = va_arg(arg, size_t *);
|
---|
| 709 | *pt = (size_t) ures;
|
---|
| 710 | break;
|
---|
| 711 | case LMOD_p: ;
|
---|
| 712 | void **pp = va_arg(arg, void **);
|
---|
| 713 | *pp = (void *) (uintptr_t) ures;
|
---|
| 714 | break;
|
---|
| 715 | default:
|
---|
| 716 | assert(false);
|
---|
| 717 | }
|
---|
| 718 | } else {
|
---|
| 719 | switch (length_mod) {
|
---|
| 720 | case LMOD_hh: ; /* Label cannot be part of declaration. */
|
---|
| 721 | signed char *phh = va_arg(arg, signed char *);
|
---|
| 722 | *phh = (signed char) sres;
|
---|
| 723 | break;
|
---|
| 724 | case LMOD_h: ;
|
---|
| 725 | short *ph = va_arg(arg, short *);
|
---|
| 726 | *ph = (short) sres;
|
---|
| 727 | break;
|
---|
| 728 | case LMOD_NONE: ;
|
---|
| 729 | int *pdef = va_arg(arg, int *);
|
---|
| 730 | *pdef = (int) sres;
|
---|
| 731 | break;
|
---|
| 732 | case LMOD_l: ;
|
---|
| 733 | long *pl = va_arg(arg, long *);
|
---|
| 734 | *pl = (long) sres;
|
---|
| 735 | break;
|
---|
| 736 | case LMOD_ll: ;
|
---|
| 737 | long long *pll = va_arg(arg, long long *);
|
---|
| 738 | *pll = (long long) sres;
|
---|
| 739 | break;
|
---|
| 740 | case LMOD_j: ;
|
---|
| 741 | posix_intmax_t *pj = va_arg(arg, posix_intmax_t *);
|
---|
| 742 | *pj = (posix_intmax_t) sres;
|
---|
| 743 | break;
|
---|
| 744 | case LMOD_z: ;
|
---|
| 745 | ssize_t *pz = va_arg(arg, ssize_t *);
|
---|
| 746 | *pz = (ssize_t) sres;
|
---|
| 747 | break;
|
---|
| 748 | case LMOD_t: ;
|
---|
| 749 | posix_ptrdiff_t *pt = va_arg(arg, posix_ptrdiff_t *);
|
---|
| 750 | *pt = (posix_ptrdiff_t) sres;
|
---|
| 751 | break;
|
---|
| 752 | default:
|
---|
| 753 | assert(false);
|
---|
| 754 | }
|
---|
| 755 | }
|
---|
| 756 | ++converted_cnt;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | converting = false;
|
---|
| 760 | ++fmt;
|
---|
| 761 | } else if (is_float_conv(*fmt)) {
|
---|
| 762 | /* Floating point number conversion. */
|
---|
| 763 |
|
---|
| 764 | /* Check sanity of optional parts of conversion specifier. */
|
---|
| 765 | if (assign_alloc) {
|
---|
| 766 | /* Illegal format string. */
|
---|
| 767 | break;
|
---|
| 768 | }
|
---|
| 769 | if (length_mod != LMOD_NONE &&
|
---|
| 770 | length_mod != LMOD_l &&
|
---|
| 771 | length_mod != LMOD_L) {
|
---|
| 772 | /* Illegal format string. */
|
---|
| 773 | break;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | /* First consume any white spaces, so we can borrow cursor
|
---|
| 777 | * from the input provider. This way, the cursor will either
|
---|
| 778 | * point to the non-white space while the input will be
|
---|
| 779 | * prefetched up to the newline (which is suitable for strtof),
|
---|
| 780 | * or the input will be at EOF. */
|
---|
| 781 | do {
|
---|
| 782 | c = in->pop(in);
|
---|
| 783 | } while (isspace(c));
|
---|
| 784 |
|
---|
| 785 | /* After skipping the white spaces, can we actually continue? */
|
---|
| 786 | if (c == -1) {
|
---|
| 787 | /* Input failure. */
|
---|
| 788 | break;
|
---|
| 789 | } else {
|
---|
| 790 | /* Everything is OK, just undo the last pop, so the cursor
|
---|
| 791 | * can be borrowed. */
|
---|
| 792 | in->undo(in);
|
---|
| 793 | }
|
---|
| 794 |
|
---|
| 795 | const char *cur_borrowed = NULL;
|
---|
| 796 | const char *cur_limited = NULL;
|
---|
| 797 | char *cur_updated = NULL;
|
---|
| 798 |
|
---|
| 799 | /* Borrow the cursor. Until it is returned to the provider
|
---|
| 800 | * we cannot jump from the cycle, because it would leave
|
---|
| 801 | * the input inconsistent. */
|
---|
| 802 | cur_borrowed = in->borrow_cursor(in);
|
---|
| 803 |
|
---|
| 804 | /* If the width is limited, the cursor horizont must be
|
---|
| 805 | * decreased accordingly. Otherwise the strtof could read more
|
---|
| 806 | * than allowed by width. */
|
---|
| 807 | if (width != -1) {
|
---|
| 808 | cur_limited = posix_strndup(cur_borrowed, width);
|
---|
| 809 | } else {
|
---|
| 810 | cur_limited = cur_borrowed;
|
---|
| 811 | }
|
---|
| 812 | cur_updated = (char *) cur_limited;
|
---|
| 813 |
|
---|
| 814 | float fres = 0.0;
|
---|
| 815 | double dres = 0.0;
|
---|
| 816 | long double ldres = 0.0;
|
---|
| 817 | errno = 0; /* Reset errno to recognize error later. */
|
---|
| 818 | /* Try to convert the floating point nubmer. */
|
---|
| 819 | switch (length_mod) {
|
---|
| 820 | case LMOD_NONE:
|
---|
| 821 | fres = posix_strtof(cur_limited, &cur_updated);
|
---|
| 822 | break;
|
---|
| 823 | case LMOD_l:
|
---|
| 824 | dres = posix_strtod(cur_limited, &cur_updated);
|
---|
| 825 | break;
|
---|
| 826 | case LMOD_L:
|
---|
| 827 | ldres = posix_strtold(cur_limited, &cur_updated);
|
---|
| 828 | break;
|
---|
| 829 | default:
|
---|
| 830 | assert(false);
|
---|
| 831 | }
|
---|
| 832 |
|
---|
| 833 | /* Update the cursor so it can be returned to the provider. */
|
---|
| 834 | cur_borrowed += cur_updated - cur_limited;
|
---|
[a12f7f1] | 835 | if (width != -1 && cur_limited != NULL) {
|
---|
[08053f7] | 836 | /* Deallocate duplicated part of the cursor view. */
|
---|
| 837 | free(cur_limited);
|
---|
| 838 | }
|
---|
| 839 | cur_limited = NULL;
|
---|
| 840 | cur_updated = NULL;
|
---|
| 841 | /* Return the cursor to the provider. Input consistency is again
|
---|
| 842 | * the job of the provider, so we can report errors from
|
---|
| 843 | * now on. */
|
---|
| 844 | in->return_cursor(in, cur_borrowed);
|
---|
| 845 | cur_borrowed = NULL;
|
---|
| 846 |
|
---|
| 847 | /* Check whether the conversion was successful. */
|
---|
| 848 | if (errno != EOK) {
|
---|
| 849 | matching_failure = true;
|
---|
| 850 | break;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 | /* If nto supressed, assign the converted floating point number
|
---|
| 854 | * into the next output argument. */
|
---|
| 855 | if (!assign_supress) {
|
---|
| 856 | switch (length_mod) {
|
---|
| 857 | case LMOD_NONE: ; /* Label cannot be part of declaration. */
|
---|
| 858 | float *pf = va_arg(arg, float *);
|
---|
| 859 | *pf = fres;
|
---|
| 860 | break;
|
---|
| 861 | case LMOD_l: ;
|
---|
| 862 | double *pd = va_arg(arg, double *);
|
---|
| 863 | *pd = dres;
|
---|
| 864 | break;
|
---|
| 865 | case LMOD_L: ;
|
---|
| 866 | long double *pld = va_arg(arg, long double *);
|
---|
| 867 | *pld = ldres;
|
---|
| 868 | break;
|
---|
| 869 | default:
|
---|
| 870 | assert(false);
|
---|
| 871 | }
|
---|
| 872 | ++converted_cnt;
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | converting = false;
|
---|
| 876 | ++fmt;
|
---|
| 877 | } else if (is_seq_conv(*fmt, &length_mod)) {
|
---|
| 878 | /* Character sequence conversion. */
|
---|
| 879 |
|
---|
| 880 | /* Check sanity of optional parts of conversion specifier. */
|
---|
| 881 | if (length_mod != LMOD_NONE &&
|
---|
| 882 | length_mod != LMOD_l) {
|
---|
| 883 | /* Illegal format string. */
|
---|
| 884 | break;
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | if (length_mod == LMOD_l) {
|
---|
| 888 | /* Wide chars not supported. */
|
---|
| 889 | // TODO
|
---|
| 890 | not_implemented();
|
---|
| 891 | }
|
---|
| 892 |
|
---|
| 893 | int term_size = 1; /* Size of the terminator (0 or 1)). */
|
---|
| 894 | if (*fmt == 'c') {
|
---|
| 895 | term_size = 0;
|
---|
| 896 | width = width == -1 ? 1 : width;
|
---|
| 897 | }
|
---|
| 898 |
|
---|
| 899 | if (*fmt == 's') {
|
---|
| 900 | /* Skip white spaces. */
|
---|
| 901 | do {
|
---|
| 902 | c = in->pop(in);
|
---|
| 903 | } while (isspace(c));
|
---|
| 904 | } else {
|
---|
| 905 | /* Fetch a single character. */
|
---|
| 906 | c = in->pop(in);
|
---|
| 907 | }
|
---|
| 908 |
|
---|
| 909 | /* Check whether there is still input to read. */
|
---|
| 910 | if (c == -1) {
|
---|
| 911 | /* Input failure. */
|
---|
| 912 | break;
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 | /* Prepare scanset. */
|
---|
| 916 | char terminate_on[256];
|
---|
| 917 | for (int i = 0; i < 256; ++i) {
|
---|
| 918 | terminate_on[i] = 0;
|
---|
| 919 | }
|
---|
| 920 | if (*fmt == 'c') {
|
---|
| 921 | ++fmt;
|
---|
| 922 | } else if (*fmt == 's') {
|
---|
| 923 | terminate_on[' '] = 1;
|
---|
| 924 | terminate_on['\n'] = 1;
|
---|
| 925 | terminate_on['\t'] = 1;
|
---|
| 926 | terminate_on['\f'] = 1;
|
---|
| 927 | terminate_on['\r'] = 1;
|
---|
| 928 | terminate_on['\v'] = 1;
|
---|
| 929 | ++fmt;
|
---|
| 930 | } else {
|
---|
| 931 | assert(*fmt == '[');
|
---|
| 932 | bool not = false;
|
---|
| 933 | bool dash = false;
|
---|
| 934 | ++fmt;
|
---|
| 935 | /* Check for negation. */
|
---|
| 936 | if (*fmt == '^') {
|
---|
| 937 | not = true;
|
---|
| 938 | ++fmt;
|
---|
| 939 | }
|
---|
| 940 | /* Check for escape sequences. */
|
---|
| 941 | if (*fmt == '-' || *fmt == ']') {
|
---|
| 942 | terminate_on[(int) *fmt] = 1;
|
---|
| 943 | ++fmt;
|
---|
| 944 | }
|
---|
| 945 | /* Check for ordinary characters and ranges. */
|
---|
| 946 | while (*fmt != '\0' && *fmt != ']') {
|
---|
| 947 | if (dash) {
|
---|
| 948 | for (char chr = *(fmt - 2); chr <= *fmt; ++chr) {
|
---|
| 949 | terminate_on[(int) chr] = 1;
|
---|
| 950 | }
|
---|
| 951 | dash = false;
|
---|
| 952 | } else if (*fmt == '-') {
|
---|
| 953 | dash = true;
|
---|
| 954 | } else {
|
---|
| 955 | terminate_on[(int) *fmt] = 1;
|
---|
| 956 | }
|
---|
| 957 | ++fmt;
|
---|
| 958 | }
|
---|
| 959 | /* Check for escape sequence. */
|
---|
| 960 | if (dash == true) {
|
---|
| 961 | terminate_on['-'] = 1;
|
---|
| 962 | }
|
---|
| 963 | /* Check whether the specifier was correctly terminated.*/
|
---|
| 964 | if (*fmt == '\0') {
|
---|
| 965 | /* Illegal format string. */
|
---|
| 966 | break;
|
---|
| 967 | } else {
|
---|
| 968 | ++fmt;
|
---|
| 969 | }
|
---|
| 970 | /* Inverse the scanset if necessary. */
|
---|
| 971 | if (not == false) {
|
---|
| 972 | for (int i = 0; i < 256; ++i) {
|
---|
| 973 | terminate_on[i] = terminate_on[i] ? 0 : 1;
|
---|
| 974 | }
|
---|
| 975 | }
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | char * buf = NULL;
|
---|
| 979 | size_t buf_size = 0;
|
---|
| 980 | char * cur = NULL;
|
---|
| 981 | size_t alloc_step = 80; /* Buffer size gain during reallocation. */
|
---|
| 982 | int my_buffer_idx = 0;
|
---|
| 983 |
|
---|
| 984 | /* Retrieve the buffer into which popped characters
|
---|
| 985 | * will be stored. */
|
---|
| 986 | if (!assign_supress) {
|
---|
| 987 | if (assign_alloc) {
|
---|
| 988 | /* We must allocate our own buffer. */
|
---|
| 989 | buf_size =
|
---|
| 990 | width == -1 ? alloc_step : (size_t) width + term_size;
|
---|
| 991 | buf = malloc(buf_size);
|
---|
| 992 | if (!buf) {
|
---|
| 993 | /* No memory. */
|
---|
| 994 | break;
|
---|
| 995 | }
|
---|
| 996 | my_buffer_idx = next_unused_buffer_idx;
|
---|
| 997 | ++next_unused_buffer_idx;
|
---|
| 998 | buffers[my_buffer_idx] = buf;
|
---|
| 999 | cur = buf;
|
---|
| 1000 | } else {
|
---|
| 1001 | /* Caller provided its buffer. */
|
---|
| 1002 | buf = va_arg(arg, char *);
|
---|
| 1003 | cur = buf;
|
---|
| 1004 | buf_size =
|
---|
| 1005 | width == -1 ? SIZE_MAX : (size_t) width + term_size;
|
---|
| 1006 | }
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
| 1009 | /* Match the string. The next character is already popped. */
|
---|
| 1010 | while ((width == -1 || width > 0) && c != -1 && !terminate_on[c]) {
|
---|
| 1011 |
|
---|
| 1012 | /* Check whether the buffer is still sufficiently large. */
|
---|
| 1013 | if (!assign_supress) {
|
---|
| 1014 | /* Always reserve space for the null terminator. */
|
---|
| 1015 | if (cur == buf + buf_size - term_size) {
|
---|
| 1016 | /* Buffer size must be increased. */
|
---|
| 1017 | buf = realloc(buf, buf_size + alloc_step);
|
---|
| 1018 | if (buf) {
|
---|
| 1019 | buffers[my_buffer_idx] = buf;
|
---|
| 1020 | cur = buf + buf_size - term_size;
|
---|
| 1021 | buf_size += alloc_step;
|
---|
| 1022 | } else {
|
---|
| 1023 | /* Break just from this tight loop. Errno will
|
---|
| 1024 | * be checked after it. */
|
---|
| 1025 | break;
|
---|
| 1026 | }
|
---|
| 1027 | }
|
---|
| 1028 | /* Store the input character. */
|
---|
| 1029 | *cur = c;
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | width = width == -1 ? -1 : width - 1;
|
---|
| 1033 | ++cur;
|
---|
| 1034 | c = in->pop(in);
|
---|
| 1035 | }
|
---|
| 1036 | if (errno == ENOMEM) {
|
---|
| 1037 | /* No memory. */
|
---|
| 1038 | break;
|
---|
| 1039 | }
|
---|
| 1040 | if (c != -1) {
|
---|
| 1041 | /* There is still more input, so undo the last pop. */
|
---|
| 1042 | in->undo(in);
|
---|
| 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | /* Check for failures. */
|
---|
| 1046 | if (cur == buf) {
|
---|
| 1047 | /* Matching failure. Input failure was already checked
|
---|
| 1048 | * earlier. */
|
---|
| 1049 | matching_failure = true;
|
---|
| 1050 | if (!assign_supress && assign_alloc) {
|
---|
| 1051 | /* Roll back. */
|
---|
| 1052 | free(buf);
|
---|
| 1053 | buffers[my_buffer_idx] = NULL;
|
---|
| 1054 | --next_unused_buffer_idx;
|
---|
| 1055 | }
|
---|
| 1056 | break;
|
---|
| 1057 | }
|
---|
| 1058 |
|
---|
| 1059 | /* Store the terminator. */
|
---|
| 1060 | if (!assign_supress && term_size > 0) {
|
---|
| 1061 | /* Space for the terminator was reserved. */
|
---|
| 1062 | *cur = '\0';
|
---|
| 1063 | }
|
---|
| 1064 |
|
---|
| 1065 | /* Store the result if not already stored. */
|
---|
| 1066 | if (!assign_supress) {
|
---|
| 1067 | if (assign_alloc) {
|
---|
| 1068 | char **pbuf = va_arg(arg, char **);
|
---|
| 1069 | *pbuf = buf;
|
---|
| 1070 | }
|
---|
| 1071 | ++converted_cnt;
|
---|
| 1072 | }
|
---|
| 1073 |
|
---|
| 1074 | converting = false;
|
---|
| 1075 | /* Format string pointer already incremented. */
|
---|
| 1076 | } else if (*fmt == 'n') {
|
---|
| 1077 | /* Report the number of consumed bytes so far. */
|
---|
| 1078 |
|
---|
| 1079 | /* Sanity check. */
|
---|
| 1080 | bool sane =
|
---|
| 1081 | width == -1 &&
|
---|
[12fb8498] | 1082 | length_mod == LMOD_NONE &&
|
---|
| 1083 | assign_alloc == false &&
|
---|
| 1084 | assign_supress == false;
|
---|
[08053f7] | 1085 |
|
---|
| 1086 | if (sane) {
|
---|
| 1087 | int *pi = va_arg(arg, int *);
|
---|
| 1088 | *pi = in->consumed;
|
---|
| 1089 | } else {
|
---|
| 1090 | /* Illegal format string. */
|
---|
| 1091 | break;
|
---|
| 1092 | }
|
---|
| 1093 |
|
---|
| 1094 | /* This shall not be counted as conversion. */
|
---|
| 1095 | converting = false;
|
---|
| 1096 | ++fmt;
|
---|
| 1097 | } else {
|
---|
| 1098 | /* Illegal format string. */
|
---|
| 1099 | break;
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
| 1102 | } else {
|
---|
| 1103 |
|
---|
| 1104 | /* Processing outside conversion specifier. Either skip white
|
---|
| 1105 | * spaces or match characters one by one. If conversion specifier
|
---|
| 1106 | * is detected, switch to coversion mode. */
|
---|
| 1107 | if (isspace(*fmt)) {
|
---|
| 1108 | /* Skip white spaces in the format string. */
|
---|
| 1109 | while (isspace(*fmt)) {
|
---|
| 1110 | ++fmt;
|
---|
| 1111 | }
|
---|
| 1112 | /* Skip white spaces in the input. */
|
---|
| 1113 | do {
|
---|
| 1114 | c = in->pop(in);
|
---|
| 1115 | } while (isspace(c));
|
---|
| 1116 | if (c != -1) {
|
---|
| 1117 | /* Input is not at EOF, so undo the last pop operation. */
|
---|
| 1118 | in->undo(in);
|
---|
| 1119 | }
|
---|
| 1120 | } else if (*fmt == '%' && *(fmt + 1) != '%') {
|
---|
| 1121 | /* Conversion specifier detected. Switch modes. */
|
---|
| 1122 | converting = true;
|
---|
| 1123 | /* Reset the conversion context. */
|
---|
| 1124 | assign_supress = false;
|
---|
| 1125 | assign_alloc = false;
|
---|
| 1126 | width = -1;
|
---|
| 1127 | length_mod = LMOD_NONE;
|
---|
| 1128 | int_conv_unsigned = false;
|
---|
| 1129 | int_conv_base = 0;
|
---|
| 1130 | ++fmt;
|
---|
| 1131 | } else {
|
---|
| 1132 | /* One by one matching. */
|
---|
| 1133 | if (*fmt == '%') {
|
---|
| 1134 | /* Escape sequence detected. */
|
---|
| 1135 | ++fmt;
|
---|
| 1136 | assert(*fmt == '%');
|
---|
| 1137 | }
|
---|
| 1138 | c = in->pop(in);
|
---|
| 1139 | if (c == -1) {
|
---|
| 1140 | /* Input failure. */
|
---|
| 1141 | break;
|
---|
| 1142 | } else if (c != *fmt) {
|
---|
| 1143 | /* Matching failure. */
|
---|
| 1144 | in->undo(in);
|
---|
| 1145 | matching_failure = true;
|
---|
| 1146 | break;
|
---|
| 1147 | } else {
|
---|
| 1148 | ++fmt;
|
---|
| 1149 | }
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
| 1152 | }
|
---|
| 1153 |
|
---|
| 1154 | }
|
---|
| 1155 |
|
---|
| 1156 | in->release(in);
|
---|
| 1157 |
|
---|
| 1158 | /* This somewhat complicated return value decision is required by POSIX. */
|
---|
| 1159 | int rc;
|
---|
| 1160 | if (matching_failure) {
|
---|
| 1161 | rc = converted_cnt;
|
---|
| 1162 | } else {
|
---|
| 1163 | if (errno == EOK) {
|
---|
| 1164 | rc = converted_cnt > 0 ? converted_cnt : EOF;
|
---|
| 1165 | } else {
|
---|
| 1166 | rc = EOF;
|
---|
| 1167 | }
|
---|
| 1168 | }
|
---|
| 1169 | if (rc == EOF) {
|
---|
| 1170 | /* Caller will not know how many arguments were successfully converted,
|
---|
| 1171 | * so the deallocation of buffers is our responsibility. */
|
---|
| 1172 | for (int i = 0; i < next_unused_buffer_idx; ++i) {
|
---|
| 1173 | free(buffers[i]);
|
---|
| 1174 | buffers[i] = NULL;
|
---|
| 1175 | }
|
---|
| 1176 | next_unused_buffer_idx = 0;
|
---|
| 1177 | }
|
---|
| 1178 | return rc;
|
---|
| 1179 | }
|
---|
| 1180 |
|
---|
| 1181 | /**
|
---|
| 1182 | * Convert formatted input from the stream.
|
---|
| 1183 | *
|
---|
| 1184 | * @param stream Input stream.
|
---|
| 1185 | * @param format Format description.
|
---|
| 1186 | * @param arg Output items.
|
---|
| 1187 | * @return The number of converted output items or EOF on failure.
|
---|
| 1188 | */
|
---|
| 1189 | int posix_vfscanf(
|
---|
| 1190 | FILE *restrict stream, const char *restrict format, va_list arg)
|
---|
| 1191 | {
|
---|
| 1192 | _input_provider provider = {
|
---|
| 1193 | { 0 }, 0, 0, NULL, 0, NULL, _PROV_CONSTRUCTED,
|
---|
| 1194 | _capture_stream, _pop_stream, _undo_stream,
|
---|
| 1195 | _borrow_cursor_universal, _return_cursor_stream, _release_stream
|
---|
| 1196 | };
|
---|
| 1197 | provider.source.stream = stream;
|
---|
| 1198 | return _internal_scanf(&provider, format, arg);
|
---|
| 1199 | }
|
---|
| 1200 |
|
---|
| 1201 | /**
|
---|
| 1202 | * Convert formatted input from the string.
|
---|
| 1203 | *
|
---|
| 1204 | * @param s Input string.
|
---|
| 1205 | * @param format Format description.
|
---|
| 1206 | * @param arg Output items.
|
---|
| 1207 | * @return The number of converted output items or EOF on failure.
|
---|
| 1208 | */
|
---|
| 1209 | int posix_vsscanf(
|
---|
| 1210 | const char *restrict s, const char *restrict format, va_list arg)
|
---|
| 1211 | {
|
---|
| 1212 | _input_provider provider = {
|
---|
| 1213 | { 0 }, 0, 0, NULL, 0, NULL, _PROV_CONSTRUCTED,
|
---|
| 1214 | _capture_string, _pop_string, _undo_string,
|
---|
| 1215 | _borrow_cursor_universal, _return_cursor_string, _release_string
|
---|
| 1216 | };
|
---|
| 1217 | provider.source.string = s;
|
---|
| 1218 | return _internal_scanf(&provider, format, arg);
|
---|
| 1219 | }
|
---|
| 1220 |
|
---|
[6921178] | 1221 | // FIXME: put the testcases to the app/tester after scanf is included into libc
|
---|
[9c1984f] | 1222 |
|
---|
| 1223 | #if 0
|
---|
| 1224 |
|
---|
| 1225 | //#include <stdio.h>
|
---|
| 1226 | //#include <malloc.h>
|
---|
| 1227 | //#include <string.h>
|
---|
| 1228 |
|
---|
| 1229 | #define test_val(fmt, exp_val, act_val) \
|
---|
| 1230 | if (exp_val == act_val) { \
|
---|
| 1231 | printf("succ, expected "fmt", actual "fmt"\n", exp_val, act_val); \
|
---|
| 1232 | } else { \
|
---|
| 1233 | printf("fail, expected "fmt", actual "fmt"\n", exp_val, act_val); \
|
---|
| 1234 | ++fail; \
|
---|
| 1235 | }
|
---|
| 1236 |
|
---|
| 1237 | #define test_str(fmt, exp_str, act_str) \
|
---|
| 1238 | if (posix_strcmp(exp_str, act_str) == 0) { \
|
---|
| 1239 | printf("succ, expected "fmt", actual "fmt"\n", exp_str, act_str); \
|
---|
| 1240 | } else { \
|
---|
| 1241 | printf("fail, expected "fmt", actual "fmt"\n", exp_str, act_str); \
|
---|
| 1242 | ++fail; \
|
---|
| 1243 | }
|
---|
| 1244 |
|
---|
| 1245 | void __posix_scanf_test(void);
|
---|
| 1246 | void __posix_scanf_test(void)
|
---|
| 1247 | {
|
---|
| 1248 | int fail = 0;
|
---|
| 1249 |
|
---|
| 1250 | int ret;
|
---|
| 1251 |
|
---|
| 1252 | unsigned char uhh;
|
---|
| 1253 | signed char shh;
|
---|
| 1254 | unsigned short uh;
|
---|
| 1255 | short sh;
|
---|
| 1256 | unsigned udef;
|
---|
| 1257 | int sdef;
|
---|
| 1258 | unsigned long ul;
|
---|
| 1259 | long sl;
|
---|
| 1260 | unsigned long long ull;
|
---|
| 1261 | long long sll;
|
---|
| 1262 | void *p;
|
---|
| 1263 |
|
---|
| 1264 | float f;
|
---|
| 1265 | double d;
|
---|
| 1266 | long double ld;
|
---|
| 1267 |
|
---|
| 1268 | char str[20];
|
---|
| 1269 | char seq[20];
|
---|
| 1270 | char scanset[20];
|
---|
| 1271 |
|
---|
| 1272 | char *pstr;
|
---|
| 1273 | char *pseq;
|
---|
| 1274 | char *pscanset;
|
---|
| 1275 |
|
---|
| 1276 | ret = posix_sscanf(
|
---|
| 1277 | "\n j tt % \t -121314 98765 aqw 0765 0x77 0xABCDEF88 -99 884",
|
---|
| 1278 | " j tt %%%3hhd%1hhu%3hd %3hu%u aqw%n %lo%llx %p %li %lld",
|
---|
| 1279 | &shh, &uhh, &sh, &uh, &udef, &sdef, &ul, &ull, &p, &sl, &sll);
|
---|
| 1280 | test_val("%d", -12, shh);
|
---|
| 1281 | test_val("%u", 1, uhh);
|
---|
| 1282 | test_val("%d", 314, sh);
|
---|
| 1283 | test_val("%u", 987, uh);
|
---|
| 1284 | test_val("%u", 65, udef);
|
---|
| 1285 | test_val("%d", 28, sdef);
|
---|
| 1286 | test_val("%lo", (unsigned long) 0765, ul);
|
---|
| 1287 | test_val("%llx", (unsigned long long) 0x77, ull);
|
---|
| 1288 | test_val("%p", (void *) 0xABCDEF88, p);
|
---|
| 1289 | test_val("%ld", (long) -99, sl);
|
---|
| 1290 | test_val("%lld", (long long) 884, sll);
|
---|
| 1291 | test_val("%d", 10, ret);
|
---|
| 1292 |
|
---|
| 1293 | ret = posix_sscanf(
|
---|
| 1294 | "\n \t\t1.0 -0x555.AP10 1234.5678e12",
|
---|
| 1295 | "%f %lf %Lf",
|
---|
| 1296 | &f, &d, &ld);
|
---|
| 1297 | test_val("%f", 1.0, f);
|
---|
| 1298 | test_val("%lf", (double) -0x555.AP10, d);
|
---|
| 1299 | test_val("%Lf", (long double) 1234.5678e12, ld);
|
---|
| 1300 | test_val("%d", 3, ret);
|
---|
| 1301 |
|
---|
| 1302 | ret = posix_sscanf(
|
---|
| 1303 | "\n\n\thello world \n",
|
---|
| 1304 | "%5s %ms",
|
---|
| 1305 | str, &pstr);
|
---|
| 1306 | test_str("%s", "hello", str);
|
---|
| 1307 | test_str("%s", "world", pstr);
|
---|
| 1308 | test_val("%d", 2, ret);
|
---|
| 1309 | free(pstr);
|
---|
| 1310 |
|
---|
| 1311 | ret = posix_sscanf(
|
---|
| 1312 | "\n\n\thello world \n",
|
---|
| 1313 | " %5c %mc",
|
---|
| 1314 | seq, &pseq);
|
---|
| 1315 | seq[5] = '\0';
|
---|
| 1316 | pseq[1] = '\0';
|
---|
| 1317 | test_str("%s", "hello", seq);
|
---|
| 1318 | test_str("%s", "w", pseq);
|
---|
| 1319 | test_val("%d", 2, ret);
|
---|
| 1320 | free(pseq);
|
---|
| 1321 |
|
---|
| 1322 | ret = posix_sscanf(
|
---|
| 1323 | "\n\n\th-e-l-l-o world-] \n",
|
---|
| 1324 | " %9[-eh-o] %m[^]-]",
|
---|
| 1325 | scanset, &pscanset);
|
---|
| 1326 | test_str("%s", "h-e-l-l-o", scanset);
|
---|
| 1327 | test_str("%s", "world", pscanset);
|
---|
| 1328 | test_val("%d", 2, ret);
|
---|
| 1329 | free(pscanset);
|
---|
| 1330 |
|
---|
| 1331 | printf("Failed: %d\n", fail);
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
| 1334 | #endif
|
---|
| 1335 |
|
---|
[08053f7] | 1336 | /** @}
|
---|
| 1337 | */
|
---|