5 static inline bool isspace(char c
)
7 return (c
== ' ') || (c
== '\t') || (c
== '\n');
10 static inline bool isdigit(char c
)
12 return (c
>= '0') && (c
<= '9');
15 static inline bool isxdigit(char c
)
17 return ((c
>= '0') && (c
<= '9'))
18 || ((c
>= 'a') && (c
<= 'f')) || ((c
>= 'A') && (c
<= 'F'));
21 static int parse_dec(int (*peek
)(void *userp
),
22 void (*pop
)(void *userp
),
31 if ((*peek
)(userp
) == '-')
44 v
= v
* 10 + ch
- '0';
48 } while (isdigit(ch
));
54 static int parse_chars(int (*peek
)(void *userp
),
55 void (*pop
)(void *userp
),
64 while (!isspace((*peek
)(userp
)))
67 *(pt
++) = (*peek
)(userp
);
79 static int parse_hex(int (*peek
)(void *userp
),
80 void (*pop
)(void *userp
),
104 } while (isxdigit(ch
));
110 static int skip_spaces(int (*peek
)(void *userp
),
111 void (*pop
)(void *userp
),
115 while (isspace((*peek
)(userp
))) {
122 static int scan(int (*peek
)(void *userp
),
123 void (*pop
)(void *userp
),
136 while ((ch
= *fmt
++) != '\0')
138 bool literal
= false;
144 if(ch
== '*') /* We should process this, but not store it in an arguement */
157 n_chars
+= skip_spaces(peek
, pop
, userp
);
158 if ((r
= parse_hex(peek
, pop
, userp
, &ulval
)) >= 0)
162 *(va_arg(ap
, unsigned int *)) = ulval
;
171 n_chars
+= skip_spaces(peek
, pop
, userp
);
172 if ((r
= parse_dec(peek
, pop
, userp
, &lval
)) >= 0)
176 *(va_arg(ap
, int *)) = lval
;
187 *(va_arg(ap
, int *)) = n_chars
;
192 n_chars
+= skip_spaces(peek
, pop
, userp
);
197 if ((r
= parse_hex(peek
, pop
, userp
, &ulval
)) >= 0)
201 *(va_arg(ap
, unsigned long *)) = ulval
;
210 if ((r
= parse_dec(peek
, pop
, userp
, &lval
)) >= 0)
214 *(va_arg(ap
, long *)) = lval
;
230 n_chars
+= skip_spaces(peek
, pop
, userp
);
231 n_chars
+= parse_chars(peek
,pop
, userp
,skip
?0:va_arg(ap
, char *), skip
);
248 n_chars
+= skip_spaces(peek
, pop
, userp
);
249 if ((*peek
)(userp
) != ch
)
261 static int sspeek(void *userp
)
263 return **((char **)userp
);
266 static void sspop(void *userp
)
268 (*((char **)userp
))++;
271 int sscanf(const char *s
, const char *fmt
, ...)
279 r
= scan(sspeek
, sspop
, &p
, fmt
, ap
);