printf-parse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /* Formatted output to strings.
  2. Copyright (C) 1999-2000, 2002-2004, 2006 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. /* Specification. */
  16. #if WIDE_CHAR_VERSION
  17. # include "wprintf-parse.h"
  18. #else
  19. # include "printf-parse.h"
  20. #endif
  21. /* Get size_t, NULL. */
  22. #include <stddef.h>
  23. /* Get intmax_t. */
  24. #if HAVE_STDINT_H_WITH_UINTMAX
  25. # include <stdint.h>
  26. #endif
  27. #if HAVE_INTTYPES_H_WITH_UINTMAX
  28. # include <inttypes.h>
  29. #endif
  30. /* malloc(), realloc(), free(). */
  31. #include <stdlib.h>
  32. #ifndef SIZE_MAX
  33. # define SIZE_MAX ((size_t) -1)
  34. #endif
  35. #if WIDE_CHAR_VERSION
  36. # define PRINTF_PARSE wprintf_parse
  37. # define CHAR_T wchar_t
  38. # define DIRECTIVE wchar_t_directive
  39. # define DIRECTIVES wchar_t_directives
  40. #else
  41. # define PRINTF_PARSE printf_parse
  42. # define CHAR_T char
  43. # define DIRECTIVE char_directive
  44. # define DIRECTIVES char_directives
  45. #endif
  46. #ifdef STATIC
  47. STATIC
  48. #endif
  49. int
  50. PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
  51. {
  52. const CHAR_T *cp = format; /* pointer into format */
  53. size_t arg_posn = 0; /* number of regular arguments consumed */
  54. size_t d_allocated; /* allocated elements of d->dir */
  55. size_t a_allocated; /* allocated elements of a->arg */
  56. size_t max_width_length = 0;
  57. size_t max_precision_length = 0;
  58. d->count = 0;
  59. d_allocated = 1;
  60. d->dir = malloc (d_allocated * sizeof (DIRECTIVE));
  61. if (d->dir == NULL)
  62. /* Out of memory. */
  63. return -1;
  64. a->count = 0;
  65. a_allocated = 0;
  66. a->arg = NULL;
  67. #define REGISTER_ARG(_index_,_type_) \
  68. { \
  69. size_t n = (_index_); \
  70. if (n >= a_allocated) \
  71. { \
  72. size_t memory_size; \
  73. argument *memory; \
  74. \
  75. a_allocated *= 2; \
  76. if (a_allocated <= n) \
  77. a_allocated = n + 1; \
  78. if (SIZE_MAX / sizeof (argument) < a_allocated) \
  79. /* Overflow, would lead to out of memory. */ \
  80. goto error; \
  81. memory_size = a_allocated * sizeof (argument); \
  82. memory = (a->arg \
  83. ? realloc (a->arg, memory_size) \
  84. : malloc (memory_size)); \
  85. if (memory == NULL) \
  86. /* Out of memory. */ \
  87. goto error; \
  88. a->arg = memory; \
  89. } \
  90. while (a->count <= n) \
  91. a->arg[a->count++].type = TYPE_NONE; \
  92. if (a->arg[n].type == TYPE_NONE) \
  93. a->arg[n].type = (_type_); \
  94. else if (a->arg[n].type != (_type_)) \
  95. /* Ambiguous type for positional argument. */ \
  96. goto error; \
  97. }
  98. while (*cp != '\0')
  99. {
  100. CHAR_T c = *cp++;
  101. if (c == '%')
  102. {
  103. size_t arg_index = ARG_NONE;
  104. DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
  105. /* Initialize the next directive. */
  106. dp->dir_start = cp - 1;
  107. dp->flags = 0;
  108. dp->width_start = NULL;
  109. dp->width_end = NULL;
  110. dp->width_arg_index = ARG_NONE;
  111. dp->precision_start = NULL;
  112. dp->precision_end = NULL;
  113. dp->precision_arg_index = ARG_NONE;
  114. dp->arg_index = ARG_NONE;
  115. /* Test for positional argument. */
  116. if (*cp >= '0' && *cp <= '9')
  117. {
  118. const CHAR_T *np;
  119. for (np = cp; *np >= '0' && *np <= '9'; np++)
  120. ;
  121. if (*np == '$')
  122. {
  123. size_t n = 0;
  124. for (np = cp; *np >= '0' && *np <= '9'; np++)
  125. if (n < SIZE_MAX / 10)
  126. n = 10 * n + (*np - '0');
  127. else
  128. /* n too large for memory. */
  129. goto error;
  130. if (n == 0)
  131. /* Positional argument 0. */
  132. goto error;
  133. arg_index = n - 1;
  134. cp = np + 1;
  135. }
  136. }
  137. /* Read the flags. */
  138. for (;;)
  139. {
  140. if (*cp == '\'')
  141. {
  142. dp->flags |= FLAG_GROUP;
  143. cp++;
  144. }
  145. else if (*cp == '-')
  146. {
  147. dp->flags |= FLAG_LEFT;
  148. cp++;
  149. }
  150. else if (*cp == '+')
  151. {
  152. dp->flags |= FLAG_SHOWSIGN;
  153. cp++;
  154. }
  155. else if (*cp == ' ')
  156. {
  157. dp->flags |= FLAG_SPACE;
  158. cp++;
  159. }
  160. else if (*cp == '#')
  161. {
  162. dp->flags |= FLAG_ALT;
  163. cp++;
  164. }
  165. else if (*cp == '0')
  166. {
  167. dp->flags |= FLAG_ZERO;
  168. cp++;
  169. }
  170. else
  171. break;
  172. }
  173. /* Parse the field width. */
  174. if (*cp == '*')
  175. {
  176. dp->width_start = cp;
  177. cp++;
  178. dp->width_end = cp;
  179. if (max_width_length < 1)
  180. max_width_length = 1;
  181. /* Test for positional argument. */
  182. if (*cp >= '0' && *cp <= '9')
  183. {
  184. const CHAR_T *np;
  185. for (np = cp; *np >= '0' && *np <= '9'; np++)
  186. ;
  187. if (*np == '$')
  188. {
  189. size_t n = 0;
  190. for (np = cp; *np >= '0' && *np <= '9'; np++)
  191. if (n < SIZE_MAX / 10)
  192. n = 10 * n + (*np - '0');
  193. else
  194. /* n too large for memory. */
  195. goto error;
  196. if (n == 0)
  197. /* Positional argument 0. */
  198. goto error;
  199. dp->width_arg_index = n - 1;
  200. cp = np + 1;
  201. }
  202. }
  203. if (dp->width_arg_index == ARG_NONE)
  204. {
  205. dp->width_arg_index = arg_posn++;
  206. if (dp->width_arg_index == ARG_NONE)
  207. /* arg_posn wrapped around. */
  208. goto error;
  209. }
  210. REGISTER_ARG (dp->width_arg_index, TYPE_INT);
  211. }
  212. else if (*cp >= '0' && *cp <= '9')
  213. {
  214. size_t width_length;
  215. dp->width_start = cp;
  216. for (; *cp >= '0' && *cp <= '9'; cp++)
  217. ;
  218. dp->width_end = cp;
  219. width_length = dp->width_end - dp->width_start;
  220. if (max_width_length < width_length)
  221. max_width_length = width_length;
  222. }
  223. /* Parse the precision. */
  224. if (*cp == '.')
  225. {
  226. cp++;
  227. if (*cp == '*')
  228. {
  229. dp->precision_start = cp - 1;
  230. cp++;
  231. dp->precision_end = cp;
  232. if (max_precision_length < 2)
  233. max_precision_length = 2;
  234. /* Test for positional argument. */
  235. if (*cp >= '0' && *cp <= '9')
  236. {
  237. const CHAR_T *np;
  238. for (np = cp; *np >= '0' && *np <= '9'; np++)
  239. ;
  240. if (*np == '$')
  241. {
  242. size_t n = 0;
  243. for (np = cp; *np >= '0' && *np <= '9'; np++)
  244. if (n < SIZE_MAX / 10)
  245. n = 10 * n + (*np - '0');
  246. else
  247. /* n too large for memory. */
  248. goto error;
  249. if (n == 0)
  250. /* Positional argument 0. */
  251. goto error;
  252. dp->precision_arg_index = n - 1;
  253. cp = np + 1;
  254. }
  255. }
  256. if (dp->precision_arg_index == ARG_NONE)
  257. {
  258. dp->precision_arg_index = arg_posn++;
  259. if (dp->precision_arg_index == ARG_NONE)
  260. /* arg_posn wrapped around. */
  261. goto error;
  262. }
  263. REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
  264. }
  265. else
  266. {
  267. size_t precision_length;
  268. dp->precision_start = cp - 1;
  269. for (; *cp >= '0' && *cp <= '9'; cp++)
  270. ;
  271. dp->precision_end = cp;
  272. precision_length = dp->precision_end - dp->precision_start;
  273. if (max_precision_length < precision_length)
  274. max_precision_length = precision_length;
  275. }
  276. }
  277. {
  278. arg_type type;
  279. /* Parse argument type/size specifiers. */
  280. {
  281. int flags = 0;
  282. for (;;)
  283. {
  284. if (*cp == 'h')
  285. {
  286. flags |= (1 << (flags & 1));
  287. cp++;
  288. }
  289. else if (*cp == 'L')
  290. {
  291. flags |= 4;
  292. cp++;
  293. }
  294. else if (*cp == 'l')
  295. {
  296. flags += 8;
  297. cp++;
  298. }
  299. #ifdef HAVE_INTMAX_T
  300. else if (*cp == 'j')
  301. {
  302. if (sizeof (intmax_t) > sizeof (long))
  303. {
  304. /* intmax_t = long long */
  305. flags += 16;
  306. }
  307. else if (sizeof (intmax_t) > sizeof (int))
  308. {
  309. /* intmax_t = long */
  310. flags += 8;
  311. }
  312. cp++;
  313. }
  314. #endif
  315. else if (*cp == 'z' || *cp == 'Z')
  316. {
  317. /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
  318. because the warning facility in gcc-2.95.2 understands
  319. only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
  320. if (sizeof (size_t) > sizeof (long))
  321. {
  322. /* size_t = long long */
  323. flags += 16;
  324. }
  325. else if (sizeof (size_t) > sizeof (int))
  326. {
  327. /* size_t = long */
  328. flags += 8;
  329. }
  330. cp++;
  331. }
  332. else if (*cp == 't')
  333. {
  334. if (sizeof (ptrdiff_t) > sizeof (long))
  335. {
  336. /* ptrdiff_t = long long */
  337. flags += 16;
  338. }
  339. else if (sizeof (ptrdiff_t) > sizeof (int))
  340. {
  341. /* ptrdiff_t = long */
  342. flags += 8;
  343. }
  344. cp++;
  345. }
  346. else
  347. break;
  348. }
  349. /* Read the conversion character. */
  350. c = *cp++;
  351. switch (c)
  352. {
  353. case 'd': case 'i':
  354. #ifdef HAVE_LONG_LONG
  355. if (flags >= 16 || (flags & 4))
  356. type = TYPE_LONGLONGINT;
  357. else
  358. #endif
  359. if (flags >= 8)
  360. type = TYPE_LONGINT;
  361. else if (flags & 2)
  362. type = TYPE_SCHAR;
  363. else if (flags & 1)
  364. type = TYPE_SHORT;
  365. else
  366. type = TYPE_INT;
  367. break;
  368. case 'o': case 'u': case 'x': case 'X':
  369. #ifdef HAVE_LONG_LONG
  370. if (flags >= 16 || (flags & 4))
  371. type = TYPE_ULONGLONGINT;
  372. else
  373. #endif
  374. if (flags >= 8)
  375. type = TYPE_ULONGINT;
  376. else if (flags & 2)
  377. type = TYPE_UCHAR;
  378. else if (flags & 1)
  379. type = TYPE_USHORT;
  380. else
  381. type = TYPE_UINT;
  382. break;
  383. case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
  384. case 'a': case 'A':
  385. #ifdef HAVE_LONG_DOUBLE
  386. if (flags >= 16 || (flags & 4))
  387. type = TYPE_LONGDOUBLE;
  388. else
  389. #endif
  390. type = TYPE_DOUBLE;
  391. break;
  392. case 'c':
  393. if (flags >= 8)
  394. #ifdef HAVE_WINT_T
  395. type = TYPE_WIDE_CHAR;
  396. #else
  397. goto error;
  398. #endif
  399. else
  400. type = TYPE_CHAR;
  401. break;
  402. #ifdef HAVE_WINT_T
  403. case 'C':
  404. type = TYPE_WIDE_CHAR;
  405. c = 'c';
  406. break;
  407. #endif
  408. case 's':
  409. if (flags >= 8)
  410. #ifdef HAVE_WCHAR_T
  411. type = TYPE_WIDE_STRING;
  412. #else
  413. goto error;
  414. #endif
  415. else
  416. type = TYPE_STRING;
  417. break;
  418. #ifdef HAVE_WCHAR_T
  419. case 'S':
  420. type = TYPE_WIDE_STRING;
  421. c = 's';
  422. break;
  423. #endif
  424. case 'p':
  425. type = TYPE_POINTER;
  426. break;
  427. case 'n':
  428. #ifdef HAVE_LONG_LONG
  429. if (flags >= 16 || (flags & 4))
  430. type = TYPE_COUNT_LONGLONGINT_POINTER;
  431. else
  432. #endif
  433. if (flags >= 8)
  434. type = TYPE_COUNT_LONGINT_POINTER;
  435. else if (flags & 2)
  436. type = TYPE_COUNT_SCHAR_POINTER;
  437. else if (flags & 1)
  438. type = TYPE_COUNT_SHORT_POINTER;
  439. else
  440. type = TYPE_COUNT_INT_POINTER;
  441. break;
  442. case '%':
  443. type = TYPE_NONE;
  444. break;
  445. default:
  446. /* Unknown conversion character. */
  447. goto error;
  448. }
  449. }
  450. if (type != TYPE_NONE)
  451. {
  452. dp->arg_index = arg_index;
  453. if (dp->arg_index == ARG_NONE)
  454. {
  455. dp->arg_index = arg_posn++;
  456. if (dp->arg_index == ARG_NONE)
  457. /* arg_posn wrapped around. */
  458. goto error;
  459. }
  460. REGISTER_ARG (dp->arg_index, type);
  461. }
  462. dp->conversion = c;
  463. dp->dir_end = cp;
  464. }
  465. d->count++;
  466. if (d->count >= d_allocated)
  467. {
  468. DIRECTIVE *memory;
  469. if (SIZE_MAX / (2 * sizeof (DIRECTIVE)) < d_allocated)
  470. /* Overflow, would lead to out of memory. */
  471. goto error;
  472. d_allocated *= 2;
  473. memory = realloc (d->dir, d_allocated * sizeof (DIRECTIVE));
  474. if (memory == NULL)
  475. /* Out of memory. */
  476. goto error;
  477. d->dir = memory;
  478. }
  479. }
  480. }
  481. d->dir[d->count].dir_start = cp;
  482. d->max_width_length = max_width_length;
  483. d->max_precision_length = max_precision_length;
  484. return 0;
  485. error:
  486. if (a->arg)
  487. free (a->arg);
  488. if (d->dir)
  489. free (d->dir);
  490. return -1;
  491. }
  492. #undef DIRECTIVES
  493. #undef DIRECTIVE
  494. #undef CHAR_T
  495. #undef PRINTF_PARSE