Fixed varargs handling in the scanf functions (spotted by Eric
[wine] / dlls / msvcrt / scanf.h
1 /*
2  * general implementation of scanf used by scanf, sscanf, fscanf,
3  * _cscanf, wscanf, swscanf and fwscanf
4  *
5  * Copyright 1996,1998 Marcus Meissner
6  * Copyright 1996 Jukka Iivonen
7  * Copyright 1997,2000, 2003 Uwe Bonnes
8  * Copyright 2000 Jon Griffiths
9  * Copyright 2002 Daniel Gudbjartsson
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #ifdef WIDE_SCANF
27 #define _CHAR_ MSVCRT_wchar_t
28 #define _EOF_ MSVCRT_WEOF
29 #define _EOF_RET MSVCRT_WEOF
30 #define _ISSPACE_(c) MSVCRT_iswspace(c)
31 #define _ISDIGIT_(c) MSVCRT_iswdigit(c)
32 #define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
33 #define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
34 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
35 #define _BITMAPSIZE_ 256*256
36 #else /* WIDE_SCANF */
37 #define _CHAR_ char
38 #define _EOF_ MSVCRT_EOF
39 #define _EOF_RET MSVCRT_EOF
40 #define _ISSPACE_(c) isspace(c)
41 #define _ISDIGIT_(c) isdigit(c)
42 #define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
43 #define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
44 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
45 #define _BITMAPSIZE_ 256
46 #endif /* WIDE_SCANF */
47
48 #ifdef CONSOLE
49 #define _GETC_(file) (consumed++, _getch())
50 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
51 #define _FUNCTION_ static int MSVCRT_vcscanf(const char *format, va_list ap)
52 #else
53 #ifdef STRING
54 #undef _EOF_
55 #define _EOF_ 0
56 #define _GETC_(file) (consumed++, *file++)
57 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
58 #ifdef WIDE_SCANF
59 #define _FUNCTION_ static int MSVCRT_vswscanf(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, va_list ap)
60 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int MSVCRT_vsscanf(const char *file, const char *format, va_list ap)
62 #endif /* WIDE_SCANF */
63 #else /* STRING */
64 #ifdef WIDE_SCANF
65 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
66 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
67 #define _FUNCTION_ static int MSVCRT_vfwscanf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list ap)
68 #else /* WIDE_SCANF */
69 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
70 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
71 #define _FUNCTION_ static int MSVCRT_vfscanf(MSVCRT_FILE* file, const char *format, va_list ap)
72 #endif /* WIDE_SCANF */
73 #endif /* STRING */
74 #endif /* CONSOLE */
75
76 /*********************************************************************
77  * Implemented based on
78  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_format_specification_fields_.2d_.scanf_and_wscanf_functions.asp
79  * Extended by C. Scott Ananian <cananian@alumni.princeton.edu> to handle
80  * more types of format spec.
81  */
82 _FUNCTION_ {
83     int rd = 0, consumed = 0;
84     int nch;
85     if (!*format) return 0;
86 #ifndef WIDE_SCANF
87 #ifdef CONSOLE
88     WARN("(%s): semi-stub\n", debugstr_a(format));
89 #else /* CONSOLE */
90 #ifdef STRING
91     WARN("%s (%s): semi-stub\n", file, debugstr_a(format));
92 #else /* STRING */
93     WARN("%p (%s): semi-stub\n", file, debugstr_a(format));
94 #endif /* STRING */
95 #endif /* CONSOLE */
96 #endif /* WIDE_SCANF */
97     nch = _GETC_(file);
98     if (nch == _EOF_) return _EOF_RET;
99
100     while (*format) {
101         /* a whitespace character in the format string causes scanf to read,
102          * but not store, all consecutive white-space characters in the input
103          * up to the next non-white-space character.  One white space character
104          * in the input matches any number (including zero) and combination of
105          * white-space characters in the input. */
106         if (_ISSPACE_(*format)) {
107             /* skip whitespace */
108             while ((nch!=_EOF_) && _ISSPACE_(nch))
109                 nch = _GETC_(file);
110         }
111         /* a format specification causes scanf to read and convert characters
112          * in the input into values of a specified type.  The value is assigned
113          * to an argument in the argument list.  Format specifications have
114          * the form %[*][width][{h | l | I64 | L}]type */
115         else if (*format == '%') {
116             int st = 0; int suppress = 0; int width = 0;
117             int base, number_signed;
118             int h_prefix = 0;
119             int l_prefix = 0;
120             int L_prefix = 0;
121             int w_prefix = 0;
122             int prefix_finished = 0;
123             /* int I64_prefix = 0; */
124             format++;
125             /* look for leading asterisk, which means 'suppress assignment of
126              * this field'. */
127             if (*format=='*') {
128                 format++;
129                 suppress=1;
130             }
131             /* look for width specification */
132             while (_ISDIGIT_(*format)) {
133                 width*=10;
134                 width+=*format++ - '0';
135             }
136             if (width==0) width=-1; /* no width spec seen */
137             /* read prefix (if any) */
138             while (!prefix_finished) {
139                 switch(*format) {
140                 case 'h': h_prefix = 1; break;
141                 case 'l': l_prefix = 1; break;
142                 case 'w': w_prefix = 1; break;
143                 case 'L': L_prefix = 1; break;
144                 case 'I':
145                     if (*(format + 1) == '6' &&
146                         *(format + 2) == '4') {
147                         /* I64_prefix = 1; */
148                         format += 2;
149                         FIXME("I64 prefix currently not implemented in fscanf/fwscanf");
150                     }
151                     break;
152                 default:
153                     prefix_finished = 1;
154                 }
155                 if (!prefix_finished) format++;
156             }
157             /* read type */
158             switch(*format) {
159             case 'x':
160             case 'X': /* hexadecimal integer. */
161                 base = 16; number_signed = 0;
162                 goto number;
163             case 'o': /* octal integer */
164                 base = 8; number_signed = 0;
165                 goto number;
166             case 'u': /* unsigned decimal integer */
167                 base = 10; number_signed = 0;
168                 goto number;
169             case 'd': /* signed decimal integer */
170                 base = 10; number_signed = 1;
171                 goto number;
172             case 'i': /* generic integer */
173                 base = 10; number_signed = 1;
174             number: {
175                     /* read an integer */
176                     long unsigned int cur = 0;
177                     int negative = 0;
178                     int seendigit=0;
179                     /* skip initial whitespace */
180                     while ((nch!=_EOF_) && _ISSPACE_(nch))
181                         nch = _GETC_(file);
182                     /* get sign */
183                     if (number_signed && (nch == '-' ||
184                                           nch == '+')) {
185                         negative = (nch=='-');
186                         nch = _GETC_(file);
187                         if (width>0) width--;
188                     }
189                     /* look for leading indication of base */
190                     if (width!=0 && nch == '0') {
191                         nch = _GETC_(file);
192                         if (width>0) width--;
193                         seendigit=1;
194                         if (width!=0 && (nch=='x' || nch=='X')) {
195                             if (base==0)
196                                 base=16;
197                             if (base==16) {
198                                 nch = _GETC_(file);
199                                 if (width>0) width--;
200                                 seendigit=0;
201                             }
202                         } else if (base==0)
203                             base = 8;
204                     }
205                     /* throw away leading zeros */
206                     while (width!=0 && nch=='0') {
207                         nch = _GETC_(file);
208                         if (width>0) width--;
209                         seendigit=1;
210                     }
211                     if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
212                         cur = _CHAR2DIGIT_(nch, base);
213                         nch = _GETC_(file);
214                         if (width>0) width--;
215                         seendigit=1;
216                     }
217                     /* read until no more digits */
218                     while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
219                         cur = cur*base + _CHAR2DIGIT_(nch, base);
220                         nch = _GETC_(file);
221                         if (width>0) width--;
222                         seendigit=1;
223                     }
224                     /* okay, done! */
225                     if (!seendigit) break; /* not a valid number */
226                     st = 1;
227                     if (!suppress) {
228 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
229                         if (number_signed) {
230                             if (l_prefix) _SET_NUMBER_(long int);
231                             else if (h_prefix) _SET_NUMBER_(short int);
232                             else _SET_NUMBER_(int);
233                         } else {
234                             if (negative) {
235                                 WARN("Dropping sign in reading a negative number into an unsigned value");
236                                 negative = 0;
237                             }
238                             if (l_prefix) _SET_NUMBER_(unsigned long int);
239                             else if (h_prefix)
240                                 _SET_NUMBER_(unsigned short int);
241                             else _SET_NUMBER_(unsigned int);
242                         }
243                     }
244                 }
245                 break;
246             case 'e':
247             case 'E':
248             case 'f':
249             case 'g':
250             case 'G': { /* read a float */
251                     long double cur = 0;
252                     int negative = 0;
253                     /* skip initial whitespace */
254                     while ((nch!=_EOF_) && _ISSPACE_(nch))
255                         nch = _GETC_(file);
256                     /* get sign. */
257                     if (nch == '-' || nch == '+') {
258                         negative = (nch=='-');
259                         if (width>0) width--;
260                         if (width==0) break;
261                         nch = _GETC_(file);
262                     }
263                     /* get first digit. */
264                     if (!_ISDIGIT_(nch)) break;
265                     cur = (nch - '0');
266                     nch = _GETC_(file);
267                     if (width>0) width--;
268                     /* read until no more digits */
269                     while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
270                         cur = cur*10 + (nch - '0');
271                         nch = _GETC_(file);
272                         if (width>0) width--;
273                     }
274                     /* handle decimals */
275                     if (width!=0 && nch == '.') {
276                         float dec = 1;
277                         nch = _GETC_(file);
278                         if (width>0) width--;
279                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
280                             dec /= 10;
281                             cur += dec * (nch - '0');
282                             nch = _GETC_(file);
283                             if (width>0) width--;
284                         }
285                     }
286                     /* handle exponent */
287                     if (width!=0 && (nch == 'e' || nch == 'E')) {
288                         int exponent = 0, negexp = 0;
289                         float expcnt;
290                         nch = _GETC_(file);
291                         if (width>0) width--;
292                         /* possible sign on the exponent */
293                         if (width!=0 && (nch=='+' || nch=='-')) {
294                             negexp = (nch=='-');
295                             nch = _GETC_(file);
296                             if (width>0) width--;
297                         }
298                         /* exponent digits */
299                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
300                             exponent *= 10;
301                             exponent += (nch - '0');
302                             nch = _GETC_(file);
303                             if (width>0) width--;
304                         }
305                         /* update 'cur' with this exponent. */
306                         expcnt =  negexp ? .1 : 10;
307                         while (exponent!=0) {
308                             if (exponent&1)
309                                 cur*=expcnt;
310                             exponent/=2;
311                             expcnt=expcnt*expcnt;
312                         }
313                     }
314                     st = 1;
315                     if (!suppress) {
316                         if (L_prefix) _SET_NUMBER_(long double);
317                         else if (l_prefix) _SET_NUMBER_(double);
318                         else _SET_NUMBER_(float);
319                     }
320                 }
321                 break;
322                 /* According to
323                  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_scanf_type_field_characters.asp
324                  * 's' reads a character string in a call to fscanf
325                  * and 'S' a wide character string and vice versa in a
326                  * call to fwscanf. The 'h', 'w' and 'l' prefixes override
327                  * this behaviour. 'h' forces reading char * but 'l' and 'w'
328                  * force reading WCHAR. */
329             case 's':
330                     if (w_prefix || l_prefix) goto widecharstring;
331                     else if (h_prefix) goto charstring;
332 #ifdef WIDE_SCANF
333                     else goto widecharstring;
334 #else /* WIDE_SCANF */
335                     else goto charstring;
336 #endif /* WIDE_SCANF */
337             case 'S':
338                     if (w_prefix || l_prefix) goto widecharstring;
339                     else if (h_prefix) goto charstring;
340 #ifdef WIDE_SCANF
341                     else goto charstring;
342 #else /* WIDE_SCANF */
343                     else goto widecharstring;
344 #endif /* WIDE_SCANF */
345             charstring: { /* read a word into a char */
346                     char*str = suppress ? NULL : va_arg(ap, char*);
347                     char*sptr = str;
348                     /* skip initial whitespace */
349                     while ((nch!=_EOF_) && _ISSPACE_(nch))
350                         nch = _GETC_(file);
351                     /* read until whitespace */
352                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
353                         if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
354                         st++;
355                         nch = _GETC_(file);
356                         if (width>0) width--;
357                     }
358                     /* terminate */
359                     if (!suppress) *sptr = 0;
360                 }
361                 break;
362             widecharstring: { /* read a word into a wchar_t* */
363                     MSVCRT_wchar_t*str =
364                         suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
365                     MSVCRT_wchar_t*sptr = str;
366                     /* skip initial whitespace */
367                     while ((nch!=_EOF_) && _ISSPACE_(nch))
368                         nch = _GETC_(file);
369                     /* read until whitespace */
370                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
371                         if (!suppress) *sptr++ = _WIDE2SUPPORTED_(nch);
372                         st++;
373                         nch = _GETC_(file);
374                         if (width>0) width--;
375                     }
376                     /* terminate */
377                     if (!suppress) *sptr = 0;
378                 }
379                 break;
380             /* 'c' and 'C work analogously to 's' and 'S' as described
381              * above */
382             case 'c':
383                     if (w_prefix || l_prefix) goto widecharacter;
384                     else if (h_prefix) goto character;
385 #ifdef WIDE_SCANF
386                     else goto widecharacter;
387 #else /* WIDE_SCANF */
388                     else goto character;
389 #endif /* WIDE_SCANF */
390             case 'C':
391                     if (w_prefix || l_prefix) goto widecharacter;
392                     else if (h_prefix) goto character;
393 #ifdef WIDE_SCANF
394                     else goto character;
395 #else /* WIDE_SCANF */
396                     else goto widecharacter;
397 #endif /* WIDE_SCANF */
398           character: { /* read single character into char */
399                     if (!suppress) {
400                         char*c = va_arg(ap, char*);
401                         *c = _CHAR2SUPPORTED_(nch);
402                     }
403                     st = 1;
404                     nch = _GETC_(file);
405                 }
406                 break;
407           widecharacter: {
408                     if (!suppress) { /* read single character into a wchar_t */
409                         MSVCRT_wchar_t*c = va_arg(ap, MSVCRT_wchar_t*);
410                         *c = _WIDE2SUPPORTED_(nch);
411                     }
412                     nch = _GETC_(file);
413                     st = 1;
414                 }
415                 break;
416             case 'n': {
417                     if (!suppress) {
418                         int*n = va_arg(ap, int*);
419                         *n = consumed - (nch!=_EOF_);
420                     }
421                 }
422                 break;
423             case '[': {
424                     _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
425                     _CHAR_ *sptr = str;
426                     RTL_BITMAP bitMask;
427                     LPBYTE Mask;
428                     int invert = 0; /* Set if we are NOT to find the chars */
429
430                     /* Init our bitmap */
431                     Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
432                     RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
433
434                     /* Read the format */
435                     format++;
436                     if(*format == '^') {
437                         invert = 1;
438                         format++;
439                     }
440                     if(*format == ']') {
441                         RtlSetBits(&bitMask, ']', 1);
442                         format++;
443                     }
444                     while(*format && (*format != ']')) {
445                         /* According to:
446                          * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_scanf_width_specification.asp
447                          * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
448                         if((*format == '-') && (*(format + 1) != ']')) {
449                             if ((*(format - 1)) < *(format + 1))
450                                 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
451                             else
452                                 RtlSetBits(&bitMask, *(format + 1)    , *(format - 1) - *(format + 1));                       
453                             format++;
454                         } else
455                             RtlSetBits(&bitMask, *format, 1);
456                         format++;
457                     }
458                     /* read until char is not suitable */
459                     while ((width != 0) && (nch != _EOF_)) {
460                         if(!invert) {
461                             if(RtlAreBitsSet(&bitMask, nch, 1)) {
462                                 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
463                             } else
464                                 break;
465                         } else {
466                             if(RtlAreBitsClear(&bitMask, nch, 1)) {
467                                 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
468                             } else
469                                 break;
470                         }
471                         st++;
472                         nch = _GETC_(file);
473                         if (width>0) width--;
474                     }
475                     /* terminate */
476                     if (!suppress) *sptr = 0;
477                     HeapFree(GetProcessHeap(), 0, Mask);
478                 }
479                 break;
480             default:
481                 /* From spec: "if a percent sign is followed by a character
482                  * that has no meaning as a format-control character, that
483                  * character and the following characters are treated as
484                  * an ordinary sequence of characters, that is, a sequence
485                  * of characters that must match the input.  For example,
486                  * to specify that a percent-sign character is to be input,
487                  * use %%." */
488                 while ((nch!=_EOF_) && _ISSPACE_(nch))
489                     nch = _GETC_(file);
490                 if (nch==*format) {
491                     suppress = 1; /* whoops no field to be read */
492                     st = 1; /* but we got what we expected */
493                     nch = _GETC_(file);
494                 }
495                 break;
496             }
497             if (st && !suppress) rd++;
498             else if (!st) break;
499         }
500         /* a non-white-space character causes scanf to read, but not store,
501          * a matching non-white-space character. */
502         else {
503             /* check for character match */
504             if (nch == *format) {
505                 nch = _GETC_(file);
506             } else break;
507         }
508         format++;
509     }
510     if (nch!=_EOF_) {
511         _UNGETC_(nch, file);
512     }
513     TRACE("returning %d\n", rd);
514     return rd;
515 }
516
517 #undef _CHAR_
518 #undef _EOF_
519 #undef _EOF_RET
520 #undef _ISSPACE_
521 #undef _ISDIGIT_
522 #undef _CHAR2SUPPORTED_
523 #undef _WIDE2SUPPORTED_
524 #undef _CHAR2DIGIT_
525 #undef _GETC_
526 #undef _UNGETC_
527 #undef _FUNCTION_
528 #undef _BITMAPSIZE_