1
0

vfscanf.c 6.2 KB

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