1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <ctype.h>
#include <math.h>
/* some macros to cut this short
* NEXT(c); read next character
* PREV(c); ungetc a character
* VAL(a) leads to 1 if a is true and valid
*/
#define NEXT(c) ((c)=xgetc(opaque),size++,incount++)
#define PREV(c) do{if((c)!=EOF)xungetc(opaque,(c));size--;incount--;}while(0)
#define VAL(a) ((a)&&size<=width)
#ifdef NOFLOATINGPOINT
#undef FULL_SPECIFIERS
#else
#define FULL_SPECIFIERS
#endif
extern unsigned char *__decimalpoint;
#ifdef FULL_SPECIFIERS
static const unsigned char undef[3][sizeof(double)]= /* Undefined numeric values, IEEE */
{ { 0x7f,0xf0,0x00,0x00,0x00,0x00,0x00,0x00 }, /* +inf */
{ 0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00 }, /* -inf */
{ 0x7f,0xf1,0x00,0x00,0x00,0x00,0x00,0x00 } /* NaN */
};
#endif
int vxscanf(int (*xgetc)(void *),void (*xungetc)(void*,int),void *opaque,const char *format,va_list args)
{
size_t blocks=0,incount=0;
int c=0;
while(*format)
{
size_t size=0;
if(*format=='%')
{
size_t width=ULONG_MAX;
char type,subtype='i',ignore=0;
const unsigned char *ptr=(const unsigned char *)format+1;
size_t i;
if(isdigit(*ptr))
{ width=0;
while(isdigit(*ptr))
width=width*10+(*ptr++-'0'); }
while(*ptr=='h'||*ptr=='l'||*ptr=='L'||*ptr=='*')
{ if(*ptr=='*')
ignore=1;
else
subtype=*ptr;
ptr++;
}
type=*ptr++;
if(type&&type!='%'&&type!='c'&&type!='n'&&type!='[')
{ do /* ignore leading whitespace characters */
NEXT(c);
while(isspace(c));
size=1; } /* The first non-whitespace character is already read */
switch(type)
{ case 'c':
{ unsigned char *bp;
if(width==ULONG_MAX) /* Default */
width=1;
if(!ignore)
bp=va_arg(args,unsigned char *);
else
bp=NULL; /* Just to get the compiler happy */
NEXT(c); /* 'c' did not skip whitespace */
while(VAL(c!=EOF))
{ if(!ignore)
*bp++=c;
NEXT(c);
}
PREV(c);
if(!ignore&&size)
blocks++;
break;
}
case '[':
{ unsigned char *bp;
unsigned char tab[32],a,b;
char circflag=0;
if(*ptr=='^')
{ circflag=1;
ptr++; }
for(i=0;i<sizeof(tab);i++)
tab[i]=circflag?255:0;
for(;;)
{ if(!*ptr)
break;
a=b=*ptr++;
if(*ptr=='-'&&ptr[1]&&ptr[1]!=']')
{ ptr++;
b=*ptr++; }
for(i=a;i<=b;i++)
if(circflag)
tab[i/8]&=~(1<<(i&7));
else
tab[i/8]|=1<<(i&7);
if(*ptr==']')
{ ptr++;
break; }
}
if(!ignore)
bp=va_arg(args,unsigned char *);
else
bp=NULL; /* Just to get the compiler happy */
NEXT(c);
while(VAL(c!=EOF&&tab[c/8]&(1<<(c&7))))
{ if(!ignore)
*bp++=c;
NEXT(c);
}
PREV(c);
if(!ignore&&size)
{ *bp++='\0';
blocks++; }
break;
}
case 's':
{ unsigned char *bp;
if(!ignore)
bp=va_arg(args,unsigned char *);
else
bp=NULL; /* Just to get the compiler happy */
while(VAL(c!=EOF&&!isspace(c)))
{ if(!ignore)
*bp++=c;
NEXT(c);
}
PREV(c);
if(!ignore&&size)
{ *bp++='\0';
blocks++; }
break;
}
#ifdef FULL_SPECIFIERS
case 'e':
case 'f':
case 'g':
{ double v;
int ex=0;
int min=0,mine=0; /* This is a workaround for gcc 2.3.3: should be char */
do /* This is there just to be able to break out */
{
if(VAL(c=='-'||c=='+'))
{ min=c;
NEXT(c); }
if(VAL(tolower(c)=='i')) /* +- inf */
{ int d;
NEXT(d);
if(VAL(tolower(d)=='n'))
{ int e;
NEXT(e);
if(VAL(tolower(e)=='f'))
{ v=*(double *)&undef[min=='-'];
break; } /* break out */
PREV(e);
}
PREV(d);
}
else if(VAL(toupper(c)=='N')) /* NaN */
{ int d;
NEXT(d);
if(VAL(tolower(d)=='a'))
{ int e;
NEXT(e);
if(VAL(toupper(e)=='N'))
{ v=*(double *)&undef[2];
break; }
PREV(e);
}
PREV(d);
}
v=0.0;
while(VAL(isdigit(c)))
{ v=v*10.0+(c-'0');
NEXT(c);
}
if(VAL(c==__decimalpoint[0]))
{ double dp=0.1;
NEXT(c);
while(VAL(isdigit(c)))
{ v=v+dp*(c-'0');
dp=dp/10.0;
NEXT(c); }
if(size==2+(min!=0)) /* No number read till now -> malformatted */
{ PREV(c);
c=__decimalpoint[0]; }
}
if(min&&size==2) /* No number read till now -> malformatted */
{ PREV(c);
c=min; }
if(size==1)
break;
if(VAL(tolower(c)=='e'))
{ int d;
NEXT(d);
if(VAL(d=='-'||d=='+'))
{ mine=d;
NEXT(d); }
if(VAL(isdigit(d)))
{ do
{ ex=ex*10+(d-'0');
NEXT(d);
}while(VAL(isdigit(d)&&ex<100));
c=d;
}else
{ PREV(d);
if(mine)
PREV(mine);
}
}
PREV(c);
if(mine=='-')
v=v/pow(10.0,ex);
else
v=v*pow(10.0,ex);
if(min=='-')
v=-v;
}while(0);
if(!ignore&&size)
{ switch(subtype)
{ case 'l':
case 'L':
*va_arg(args,double *)=v;
break;
case 'i':
*va_arg(args,float *)=v;
break;
}
blocks++;
}
break;
}
#endif
case '%':
NEXT(c);
if(c!='%')
PREV(c); /* unget non-'%' character */
break;
case 'n':
if(!ignore)
*va_arg(args,int *)=incount;
size=1; /* fake a valid argument */
blocks++;
break;
default:
{ unsigned long v=0;
int base;
int min=0;
if(!type)
ptr--; /* unparse NUL character */
if(type=='p')
{ subtype='l'; /* This is the same as %lx */
type='x'; }
if(VAL((c=='-'&&type!='u')||c=='+'))
{ min=c;
NEXT(c); }
if(type=='i') /* which one to use ? */
{ if(VAL(c=='0')) /* Could be octal or sedecimal */
{ int d;
NEXT(d); /* Get a look at next character */
if(VAL(tolower(d)=='x'))
{ int e;
NEXT(e); /* And the next */
if(VAL(isxdigit(c)))
type='x'; /* Is a valid x number with '0x?' */
PREV(e);
}else
type='o';
PREV(d);
}else if(VAL(!isdigit(c)&&isxdigit(c)))
type='x'; /* Is a valid x number without '0x' */
}
while(type=='x'&&VAL(c=='0')) /* sedecimal */
{ int d;
NEXT(d);
if(VAL(tolower(d)=='x'))
{ int e;
NEXT(e);
if(VAL(isxdigit(e)))
{ c=e;
break; } /* Used while just to do this ;-) */
PREV(e);
}
PREV(d);
break; /* Need no loop */
}
base=type=='x'||type=='X'?16:(type=='o'?8:10);
while(VAL(isxdigit(c)&&(base!=10||isdigit(c))&&(base!=8||c<='7')))
{ v=v*base+(isdigit(c)?c-'0':0)+(isupper(c)?c-'A'+10:0)+(islower(c)?c-'a'+10:0);
NEXT(c);
}
if(min&&size==2) /* If there is no valid character after sign, unget last */
{ PREV(c);
c=min; }
PREV(c);
if(ignore||!size)
break;
if(type=='u')
switch(subtype)
{ case 'l':
case 'L':
*va_arg(args,unsigned long *)=v;
break;
case 'i':
*va_arg(args,unsigned int *)=v;
break;
case 'h':
*va_arg(args,unsigned short *)=v;
break;
}
else
{ signed long v2;
if(min=='-')
v2=-v;
else
v2=v;
switch(subtype)
{ case 'l':
case 'L':
*va_arg(args,signed long *)=v2;
break;
case 'i':
*va_arg(args,signed int *)=v2;
break;
case 'h':
*va_arg(args,signed short *)=v2;
break;
}
}
blocks++;
break;
}
}
format=(const char *)ptr;
}else
{ if(isspace(*format))
{ do
NEXT(c);
while(isspace(c));
PREV(c);
size=1; }
else
{ NEXT(c);
if(c!=*format)
PREV(c); }
format++;
}
if(!size)
break;
}
if(c==EOF&&!blocks)
return c;
else
return blocks;
}
|