Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #include <stdio.h> |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 2 | #include <stdlib.h> |
| 3 | #include <stdarg.h> |
| 4 | #include <ctype.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 5 | #include <wchar.h> |
| 6 | #include <wctype.h> |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | #include <string.h> |
| 9 | #include <errno.h> |
| 10 | #include <math.h> |
| 11 | #include <float.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 12 | |
| 13 | #include "stdio_impl.h" |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 14 | #include "shgetc.h" |
| 15 | #include "intscan.h" |
| 16 | #include "floatscan.h" |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 17 | |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 18 | #define SIZE_hh -2 |
| 19 | #define SIZE_h -1 |
| 20 | #define SIZE_def 0 |
| 21 | #define SIZE_l 1 |
| 22 | #define SIZE_L 2 |
| 23 | #define SIZE_ll 3 |
| 24 | |
| 25 | static void store_int(void *dest, int size, unsigned long long i) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 26 | { |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 27 | if (!dest) return; |
| 28 | switch (size) { |
| 29 | case SIZE_hh: |
| 30 | *(char *)dest = i; |
| 31 | break; |
| 32 | case SIZE_h: |
| 33 | *(short *)dest = i; |
| 34 | break; |
| 35 | case SIZE_def: |
| 36 | *(int *)dest = i; |
| 37 | break; |
| 38 | case SIZE_l: |
| 39 | *(long *)dest = i; |
| 40 | break; |
| 41 | case SIZE_ll: |
| 42 | *(long long *)dest = i; |
| 43 | break; |
| 44 | } |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 47 | static void *arg_n(va_list ap, unsigned int n) |
| 48 | { |
| 49 | void *p; |
| 50 | unsigned int i; |
| 51 | va_list ap2; |
| 52 | va_copy(ap2, ap); |
| 53 | for (i=n; i>1; i--) va_arg(ap2, void *); |
| 54 | p = va_arg(ap2, void *); |
| 55 | va_end(ap2); |
| 56 | return p; |
| 57 | } |
| 58 | |
| 59 | static int in_set(const wchar_t *set, int c) |
| 60 | { |
| 61 | int j; |
| 62 | const wchar_t *p = set; |
| 63 | if (*p == '-') { |
| 64 | if (c=='-') return 1; |
| 65 | p++; |
| 66 | } else if (*p == ']') { |
| 67 | if (c==']') return 1; |
| 68 | p++; |
| 69 | } |
| 70 | for (; *p && *p != ']'; p++) { |
| 71 | if (*p=='-' && p[1] && p[1] != ']') |
| 72 | for (j=p++[-1]; j<*p; j++) |
| 73 | if (c==j) return 1; |
| 74 | if (c==*p) return 1; |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | #if 1 |
| 80 | #undef getwc |
| 81 | #define getwc(f) \ |
| 82 | ((f)->rpos < (f)->rend && *(f)->rpos < 128 ? *(f)->rpos++ : (getwc)(f)) |
| 83 | |
| 84 | #undef ungetwc |
| 85 | #define ungetwc(c,f) \ |
Rich Felker | 0072251 | 2012-04-17 19:37:31 -0400 | [diff] [blame] | 86 | ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f))) |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 87 | #endif |
| 88 | |
Rich Felker | 400c5e5 | 2012-09-06 22:44:55 -0400 | [diff] [blame^] | 89 | int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 90 | { |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 91 | int width; |
| 92 | int size; |
| 93 | int alloc; |
| 94 | const wchar_t *p; |
| 95 | int c, t; |
| 96 | char *s; |
| 97 | wchar_t *wcs; |
| 98 | void *dest=NULL; |
| 99 | int invert; |
| 100 | int matches=0; |
| 101 | off_t pos = 0, cnt; |
| 102 | static const char size_pfx[][3] = { "hh", "h", "", "l", "L", "ll" }; |
| 103 | char tmp[3*sizeof(int)+10]; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 104 | |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 105 | FLOCK(f); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 106 | |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 107 | for (p=fmt; *p; p++) { |
| 108 | |
| 109 | if (iswspace(*p)) { |
| 110 | while (iswspace(p[1])) p++; |
| 111 | while (iswspace((c=getwc(f)))) pos++; |
| 112 | ungetwc(c, f); |
| 113 | continue; |
| 114 | } |
| 115 | if (*p != '%' || p[1] == '%') { |
| 116 | p += *p=='%'; |
| 117 | c = getwc(f); |
| 118 | if (c!=*p) { |
| 119 | ungetwc(c, f); |
| 120 | if (c<0) goto input_fail; |
| 121 | goto match_fail; |
| 122 | } |
| 123 | pos++; |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | p++; |
| 128 | if (*p=='*') { |
| 129 | dest = 0; p++; |
| 130 | } else if (iswdigit(*p) && p[1]=='$') { |
| 131 | dest = arg_n(ap, *p-'0'); p+=2; |
| 132 | } else { |
| 133 | dest = va_arg(ap, void *); |
| 134 | } |
| 135 | |
| 136 | for (width=0; iswdigit(*p); p++) { |
| 137 | width = 10*width + *p - '0'; |
| 138 | } |
| 139 | |
| 140 | if (*p=='m') { |
| 141 | alloc = 1; |
| 142 | p++; |
| 143 | } else { |
| 144 | alloc = 0; |
| 145 | } |
| 146 | |
| 147 | size = SIZE_def; |
| 148 | switch (*p++) { |
| 149 | case 'h': |
| 150 | if (*p == 'h') p++, size = SIZE_hh; |
| 151 | else size = SIZE_h; |
| 152 | break; |
| 153 | case 'l': |
| 154 | if (*p == 'l') p++, size = SIZE_ll; |
| 155 | else size = SIZE_l; |
| 156 | break; |
| 157 | case 'j': |
| 158 | size = SIZE_ll; |
| 159 | break; |
| 160 | case 'z': |
| 161 | case 't': |
| 162 | size = SIZE_l; |
| 163 | break; |
| 164 | case 'L': |
| 165 | size = SIZE_L; |
| 166 | break; |
| 167 | case 'd': case 'i': case 'o': case 'u': case 'x': |
| 168 | case 'a': case 'e': case 'f': case 'g': |
| 169 | case 'A': case 'E': case 'F': case 'G': case 'X': |
| 170 | case 's': case 'c': case '[': |
| 171 | case 'S': case 'C': |
| 172 | case 'p': case 'n': |
| 173 | p--; |
| 174 | break; |
| 175 | default: |
| 176 | goto fmt_fail; |
| 177 | } |
| 178 | |
| 179 | t = *p; |
| 180 | |
| 181 | /* Transform ls,lc -> S,C */ |
| 182 | if (size==SIZE_l && (t&15)==3) t&=~32; |
| 183 | |
Rich Felker | bdeb184 | 2012-04-17 23:35:49 -0400 | [diff] [blame] | 184 | if (t != 'n') { |
| 185 | if (t != '[' && (t|32) != 'c') |
| 186 | while (iswspace((c=getwc(f)))) pos++; |
| 187 | else |
| 188 | c=getwc(f); |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 189 | if (c < 0) goto input_fail; |
| 190 | ungetwc(c, f); |
| 191 | } |
| 192 | |
| 193 | switch (t) { |
| 194 | case 'n': |
| 195 | store_int(dest, size, pos); |
| 196 | /* do not increment match count, etc! */ |
| 197 | continue; |
| 198 | |
| 199 | case 'c': |
| 200 | if (width < 1) width = 1; |
| 201 | s = dest; |
| 202 | for (; width && (c=getwc(f)) >= 0; width--) { |
| 203 | int l = wctomb(s?s:tmp, c); |
| 204 | if (l<0) goto input_fail; |
| 205 | if (s) s+=l; |
| 206 | pos++; |
| 207 | } |
| 208 | if (width) goto match_fail; |
| 209 | break; |
| 210 | |
| 211 | case 'C': |
| 212 | if (width < 1) width = 1; |
| 213 | wcs = dest; |
| 214 | for (; width && (c=getwc(f)) >= 0; width--) |
| 215 | pos++, wcs && (*wcs++ = c); |
| 216 | if (width) goto match_fail; |
| 217 | break; |
| 218 | |
| 219 | case 's': |
Rich Felker | 9ab180f | 2012-04-17 22:15:33 -0400 | [diff] [blame] | 220 | if (width < 1) width = -1; |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 221 | s = dest; |
Rich Felker | 0072251 | 2012-04-17 19:37:31 -0400 | [diff] [blame] | 222 | while (width && !iswspace(c=getwc(f)) && c!=EOF) { |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 223 | int l = wctomb(s?s:tmp, c); |
| 224 | if (l<0) goto input_fail; |
| 225 | if (s) s+=l; |
| 226 | pos++; |
Rich Felker | 9ab180f | 2012-04-17 22:15:33 -0400 | [diff] [blame] | 227 | width-=(width>0); |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 228 | } |
Rich Felker | 0072251 | 2012-04-17 19:37:31 -0400 | [diff] [blame] | 229 | if (width) ungetwc(c, f); |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 230 | if (s) *s = 0; |
| 231 | break; |
| 232 | |
| 233 | case 'S': |
| 234 | wcs = dest; |
Rich Felker | 9ab180f | 2012-04-17 22:15:33 -0400 | [diff] [blame] | 235 | if (width < 1) width = -1; |
Rich Felker | 0072251 | 2012-04-17 19:37:31 -0400 | [diff] [blame] | 236 | while (width && !iswspace(c=getwc(f)) && c!=EOF) |
Rich Felker | 9ab180f | 2012-04-17 22:15:33 -0400 | [diff] [blame] | 237 | width-=(width>0), pos++, *wcs++ = c; |
Rich Felker | 0072251 | 2012-04-17 19:37:31 -0400 | [diff] [blame] | 238 | if (width) ungetwc(c, f); |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 239 | if (wcs) *wcs = 0; |
| 240 | break; |
| 241 | |
| 242 | case '[': |
| 243 | s = (size == SIZE_def) ? dest : 0; |
| 244 | wcs = (size == SIZE_l) ? dest : 0; |
| 245 | |
| 246 | if (*++p == '^') p++, invert = 1; |
| 247 | else invert = 0; |
| 248 | |
| 249 | int gotmatch = 0; |
| 250 | |
Rich Felker | 9ab180f | 2012-04-17 22:15:33 -0400 | [diff] [blame] | 251 | if (width < 1) width = -1; |
| 252 | |
Rich Felker | 99fbf4c | 2012-04-17 21:17:09 -0400 | [diff] [blame] | 253 | while (width) { |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 254 | if ((c=getwc(f))<0) break; |
| 255 | if (in_set(p, c) == invert) |
| 256 | break; |
| 257 | if (wcs) { |
| 258 | *wcs++ = c; |
| 259 | } else if (size != SIZE_l) { |
| 260 | int l = wctomb(s?s:tmp, c); |
| 261 | if (l<0) goto input_fail; |
| 262 | if (s) s+=l; |
| 263 | } |
| 264 | pos++; |
Rich Felker | 9ab180f | 2012-04-17 22:15:33 -0400 | [diff] [blame] | 265 | width-=(width>0); |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 266 | gotmatch=1; |
| 267 | } |
Rich Felker | 99fbf4c | 2012-04-17 21:17:09 -0400 | [diff] [blame] | 268 | if (width) ungetwc(c, f); |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 269 | |
Rich Felker | bdeb184 | 2012-04-17 23:35:49 -0400 | [diff] [blame] | 270 | if (!gotmatch) goto match_fail; |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 271 | |
| 272 | if (*p==']') p++; |
| 273 | while (*p!=']') { |
| 274 | if (!*p) goto fmt_fail; |
| 275 | p++; |
| 276 | } |
| 277 | |
| 278 | if (wcs) *wcs++ = 0; |
| 279 | if (s) *s++ = 0; |
| 280 | break; |
| 281 | |
| 282 | case 'd': case 'i': case 'o': case 'u': case 'x': |
| 283 | case 'a': case 'e': case 'f': case 'g': |
| 284 | case 'A': case 'E': case 'F': case 'G': case 'X': |
| 285 | case 'p': |
| 286 | if (width < 1) width = 0; |
| 287 | snprintf(tmp, sizeof tmp, "%.*s%.0d%s%c%%lln", |
| 288 | 1+!dest, "%*", width, size_pfx[size+2], t); |
| 289 | cnt = 0; |
| 290 | if (fscanf(f, tmp, dest?dest:&cnt, &cnt) == -1) |
| 291 | goto input_fail; |
| 292 | else if (!cnt) |
| 293 | goto match_fail; |
| 294 | pos += cnt; |
| 295 | break; |
| 296 | default: |
| 297 | goto fmt_fail; |
| 298 | } |
| 299 | |
| 300 | if (dest) matches++; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 301 | } |
Rich Felker | 73ec1d0 | 2012-04-17 14:19:46 -0400 | [diff] [blame] | 302 | if (0) { |
| 303 | fmt_fail: |
| 304 | input_fail: |
| 305 | if (!matches) matches--; |
| 306 | } |
| 307 | match_fail: |
| 308 | FUNLOCK(f); |
| 309 | return matches; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 310 | } |