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