vfwprintf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "stdio_impl.h"
  2. /* Convenient bit representation for modifier flags, which all fall
  3. * within 31 codepoints of the space character. */
  4. #define ALT_FORM (1U<<'#'-' ')
  5. #define ZERO_PAD (1U<<'0'-' ')
  6. #define LEFT_ADJ (1U<<'-'-' ')
  7. #define PAD_POS (1U<<' '-' ')
  8. #define MARK_POS (1U<<'+'-' ')
  9. #define GROUPED (1U<<'\''-' ')
  10. #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
  11. #if UINT_MAX == ULONG_MAX
  12. #define LONG_IS_INT
  13. #endif
  14. #if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
  15. #define ODD_TYPES
  16. #endif
  17. /* State machine to accept length modifiers + conversion specifiers.
  18. * Result is 0 on failure, or an argument type to pop on success. */
  19. enum {
  20. BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
  21. ZTPRE, JPRE,
  22. STOP,
  23. PTR, INT, UINT, ULLONG,
  24. #ifndef LONG_IS_INT
  25. LONG, ULONG,
  26. #else
  27. #define LONG INT
  28. #define ULONG UINT
  29. #endif
  30. SHORT, USHORT, CHAR, UCHAR,
  31. #ifdef ODD_TYPES
  32. LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
  33. #else
  34. #define LLONG ULLONG
  35. #define SIZET ULONG
  36. #define IMAX LLONG
  37. #define UMAX ULLONG
  38. #define PDIFF LONG
  39. #define UIPTR ULONG
  40. #endif
  41. DBL, LDBL,
  42. NOARG,
  43. MAXSTATE
  44. };
  45. #define S(x) [(x)-'A']
  46. static const unsigned char states[]['z'-'A'+1] = {
  47. { /* 0: bare types */
  48. S('d') = INT, S('i') = INT,
  49. S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
  50. S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
  51. S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
  52. S('c') = CHAR, S('C') = INT,
  53. S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
  54. S('m') = NOARG,
  55. S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
  56. S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
  57. }, { /* 1: l-prefixed */
  58. S('d') = LONG, S('i') = LONG,
  59. S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
  60. S('c') = INT, S('s') = PTR, S('n') = PTR,
  61. S('l') = LLPRE,
  62. }, { /* 2: ll-prefixed */
  63. S('d') = LLONG, S('i') = LLONG,
  64. S('o') = ULLONG, S('u') = ULLONG,
  65. S('x') = ULLONG, S('X') = ULLONG,
  66. S('n') = PTR,
  67. }, { /* 3: h-prefixed */
  68. S('d') = SHORT, S('i') = SHORT,
  69. S('o') = USHORT, S('u') = USHORT,
  70. S('x') = USHORT, S('X') = USHORT,
  71. S('n') = PTR,
  72. S('h') = HHPRE,
  73. }, { /* 4: hh-prefixed */
  74. S('d') = CHAR, S('i') = CHAR,
  75. S('o') = UCHAR, S('u') = UCHAR,
  76. S('x') = UCHAR, S('X') = UCHAR,
  77. S('n') = PTR,
  78. }, { /* 5: L-prefixed */
  79. S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
  80. S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
  81. S('n') = PTR,
  82. }, { /* 6: z- or t-prefixed (assumed to be same size) */
  83. S('d') = PDIFF, S('i') = PDIFF,
  84. S('o') = SIZET, S('u') = SIZET,
  85. S('x') = SIZET, S('X') = SIZET,
  86. S('n') = PTR,
  87. }, { /* 7: j-prefixed */
  88. S('d') = IMAX, S('i') = IMAX,
  89. S('o') = UMAX, S('u') = UMAX,
  90. S('x') = UMAX, S('X') = UMAX,
  91. S('n') = PTR,
  92. }
  93. };
  94. #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
  95. union arg
  96. {
  97. uintmax_t i;
  98. long double f;
  99. void *p;
  100. };
  101. static void pop_arg(union arg *arg, int type, va_list *ap)
  102. {
  103. /* Give the compiler a hint for optimizing the switch. */
  104. if ((unsigned)type > MAXSTATE) return;
  105. switch (type) {
  106. case PTR: arg->p = va_arg(*ap, void *);
  107. break; case INT: arg->i = va_arg(*ap, int);
  108. break; case UINT: arg->i = va_arg(*ap, unsigned int);
  109. #ifndef LONG_IS_INT
  110. break; case LONG: arg->i = va_arg(*ap, long);
  111. break; case ULONG: arg->i = va_arg(*ap, unsigned long);
  112. #endif
  113. break; case ULLONG: arg->i = va_arg(*ap, unsigned long long);
  114. break; case SHORT: arg->i = (short)va_arg(*ap, int);
  115. break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int);
  116. break; case CHAR: arg->i = (signed char)va_arg(*ap, int);
  117. break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int);
  118. #ifdef ODD_TYPES
  119. break; case LLONG: arg->i = va_arg(*ap, long long);
  120. break; case SIZET: arg->i = va_arg(*ap, size_t);
  121. break; case IMAX: arg->i = va_arg(*ap, intmax_t);
  122. break; case UMAX: arg->i = va_arg(*ap, uintmax_t);
  123. break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t);
  124. break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *);
  125. #endif
  126. break; case DBL: arg->f = va_arg(*ap, double);
  127. break; case LDBL: arg->f = va_arg(*ap, long double);
  128. }
  129. }
  130. static void out(FILE *f, const wchar_t *s, size_t l)
  131. {
  132. while (l--) fputwc(*s++, f);
  133. }
  134. static int getint(wchar_t **s) {
  135. int i;
  136. for (i=0; iswdigit(**s); (*s)++)
  137. i = 10*i + (**s-'0');
  138. return i;
  139. }
  140. static const char sizeprefix['y'-'a'] = {
  141. ['a'-'a']='L', ['e'-'a']='L', ['f'-'a']='L', ['g'-'a']='L',
  142. ['d'-'a']='j', ['i'-'a']='j', ['o'-'a']='j', ['u'-'a']='j', ['x'-'a']='j',
  143. ['p'-'a']='j'
  144. };
  145. static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
  146. {
  147. wchar_t *a, *z, *s=(wchar_t *)fmt, *s0;
  148. unsigned l10n=0, litpct, fl;
  149. int w, p;
  150. union arg arg;
  151. int argpos;
  152. unsigned st, ps;
  153. int cnt=0, l=0;
  154. int i;
  155. int t;
  156. char *bs;
  157. char charfmt[16];
  158. wchar_t wc;
  159. for (;;) {
  160. /* Update output count, end loop when fmt is exhausted */
  161. if (cnt >= 0) {
  162. if (l > INT_MAX - cnt) {
  163. if (!ferror(f)) errno = EOVERFLOW;
  164. cnt = -1;
  165. } else cnt += l;
  166. }
  167. if (!*s) break;
  168. /* Handle literal text and %% format specifiers */
  169. for (a=s; *s && *s!='%'; s++);
  170. litpct = wcsspn(s, L"%")/2; /* Optimize %%%% runs */
  171. z = s+litpct;
  172. s += 2*litpct;
  173. l = z-a;
  174. if (f) out(f, a, l);
  175. if (l) continue;
  176. if (iswdigit(s[1]) && s[2]=='$') {
  177. l10n=1;
  178. argpos = s[1]-'0';
  179. s+=3;
  180. } else {
  181. argpos = -1;
  182. s++;
  183. }
  184. /* Read modifier flags */
  185. for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
  186. fl |= 1U<<*s-' ';
  187. /* Read field width */
  188. if (*s=='*') {
  189. if (iswdigit(s[1]) && s[2]=='$') {
  190. l10n=1;
  191. nl_type[s[1]-'0'] = INT;
  192. w = nl_arg[s[1]-'0'].i;
  193. s+=3;
  194. } else if (!l10n) {
  195. w = f ? va_arg(*ap, int) : 0;
  196. s++;
  197. } else return -1;
  198. if (w<0) fl|=LEFT_ADJ, w=-w;
  199. } else if ((w=getint(&s))<0) return -1;
  200. /* Read precision */
  201. if (*s=='.' && s[1]=='*') {
  202. if (isdigit(s[2]) && s[3]=='$') {
  203. nl_type[s[2]-'0'] = INT;
  204. p = nl_arg[s[2]-'0'].i;
  205. s+=4;
  206. } else if (!l10n) {
  207. p = f ? va_arg(*ap, int) : 0;
  208. s+=2;
  209. } else return -1;
  210. } else if (*s=='.') {
  211. s++;
  212. p = getint(&s);
  213. } else p = -1;
  214. /* Format specifier state machine */
  215. s0=s;
  216. st=0;
  217. do {
  218. if (OOB(*s)) return -1;
  219. ps=st;
  220. st=states[st]S(*s++);
  221. } while (st-1<STOP);
  222. if (!st) return -1;
  223. /* Check validity of argument type (nl/normal) */
  224. if (st==NOARG) {
  225. if (argpos>=0) return -1;
  226. else if (!f) continue;
  227. } else {
  228. if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
  229. else if (f) pop_arg(&arg, st, ap);
  230. else return 0;
  231. }
  232. if (!f) continue;
  233. t = s[-1];
  234. if (ps && (t&15)==3) t&=~32;
  235. switch (t) {
  236. case 'n':
  237. switch(ps) {
  238. case BARE: *(int *)arg.p = cnt; break;
  239. case LPRE: *(long *)arg.p = cnt; break;
  240. case LLPRE: *(long long *)arg.p = cnt; break;
  241. case HPRE: *(unsigned short *)arg.p = cnt; break;
  242. case HHPRE: *(unsigned char *)arg.p = cnt; break;
  243. case ZTPRE: *(size_t *)arg.p = cnt; break;
  244. case JPRE: *(uintmax_t *)arg.p = cnt; break;
  245. }
  246. continue;
  247. case 'c':
  248. fputwc(btowc(arg.i), f);
  249. l = 1;
  250. continue;
  251. case 'C':
  252. fputwc(arg.i, f);
  253. l = 1;
  254. continue;
  255. case 'S':
  256. a = arg.p;
  257. z = wmemchr(a, 0, p);
  258. if (!z) z=a+p;
  259. else p=z-a;
  260. if (w<p) w=p;
  261. if (!(fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
  262. out(f, a, p);
  263. if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
  264. l=w;
  265. continue;
  266. case 's':
  267. bs = arg.p;
  268. if (p<0) p = INT_MAX;
  269. for (l=0; l<p && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
  270. if (i<0) return -1;
  271. p=l;
  272. if (w<p) w=p;
  273. if (!(fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
  274. bs = arg.p;
  275. while (l--) {
  276. i=mbtowc(&wc, bs, MB_LEN_MAX);
  277. bs+=i;
  278. fputwc(wc, f);
  279. }
  280. if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
  281. l=w;
  282. continue;
  283. }
  284. snprintf(charfmt, sizeof charfmt, "%%%s%s%s%s%s*.*%c%c",
  285. "#"+!(fl & ALT_FORM),
  286. "+"+!(fl & MARK_POS),
  287. "-"+!(fl & LEFT_ADJ),
  288. " "+!(fl & PAD_POS),
  289. "0"+!(fl & ZERO_PAD),
  290. sizeprefix[(t|32)-'a'], t);
  291. switch (t|32) {
  292. case 'a': case 'e': case 'f': case 'g':
  293. l = fprintf(f, charfmt, w, p, arg.f);
  294. break;
  295. case 'd': case 'i': case 'o': case 'u': case 'x': case 'p':
  296. l = fprintf(f, charfmt, w, p, arg.i);
  297. break;
  298. }
  299. }
  300. if (f) return cnt;
  301. if (!l10n) return 0;
  302. for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
  303. pop_arg(nl_arg+i, nl_type[i], ap);
  304. for (; i<=NL_ARGMAX && !nl_type[i]; i++);
  305. if (i<=NL_ARGMAX) return -1;
  306. return 1;
  307. }
  308. int vfwprintf(FILE *f, const wchar_t *fmt, va_list ap)
  309. {
  310. va_list ap2;
  311. int nl_type[NL_ARGMAX] = {0};
  312. union arg nl_arg[NL_ARGMAX];
  313. int ret;
  314. va_copy(ap2, ap);
  315. if (wprintf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
  316. FLOCK(f);
  317. ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
  318. FUNLOCK(f);
  319. va_end(ap2);
  320. return ret;
  321. }