You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printf-parse.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* Formatted output to strings.
  2. Copyright (C) 1999-2000, 2002-2003 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by 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 GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  14. USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. /* Specification. */
  19. #if WIDE_CHAR_VERSION
  20. # include "wprintf-parse.h"
  21. #else
  22. # include "printf-parse.h"
  23. #endif
  24. /* Get size_t, NULL. */
  25. #include <stddef.h>
  26. /* Get intmax_t. */
  27. #if HAVE_STDINT_H_WITH_UINTMAX
  28. # include <stdint.h>
  29. #endif
  30. #if HAVE_INTTYPES_H_WITH_UINTMAX
  31. # include <inttypes.h>
  32. #endif
  33. /* malloc(), realloc(), free(). */
  34. #include <stdlib.h>
  35. /* Checked size_t computations. */
  36. #include "xsize.h"
  37. #if WIDE_CHAR_VERSION
  38. # define PRINTF_PARSE wprintf_parse
  39. # define CHAR_T wchar_t
  40. # define DIRECTIVE wchar_t_directive
  41. # define DIRECTIVES wchar_t_directives
  42. #else
  43. # define PRINTF_PARSE printf_parse
  44. # define CHAR_T char
  45. # define DIRECTIVE char_directive
  46. # define DIRECTIVES char_directives
  47. #endif
  48. #ifdef STATIC
  49. STATIC
  50. #endif
  51. int
  52. PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
  53. {
  54. const CHAR_T *cp = format; /* pointer into format */
  55. size_t arg_posn = 0; /* number of regular arguments consumed */
  56. size_t d_allocated; /* allocated elements of d->dir */
  57. size_t a_allocated; /* allocated elements of a->arg */
  58. size_t max_width_length = 0;
  59. size_t max_precision_length = 0;
  60. d->count = 0;
  61. d_allocated = 1;
  62. d->dir = malloc (d_allocated * sizeof (DIRECTIVE));
  63. if (d->dir == NULL)
  64. /* Out of memory. */
  65. return -1;
  66. a->count = 0;
  67. a_allocated = 0;
  68. a->arg = NULL;
  69. #define REGISTER_ARG(_index_,_type_) \
  70. { \
  71. size_t n = (_index_); \
  72. if (n >= a_allocated) \
  73. { \
  74. size_t memory_size; \
  75. argument *memory; \
  76. \
  77. a_allocated = xtimes (a_allocated, 2); \
  78. if (a_allocated <= n) \
  79. a_allocated = xsum (n, 1); \
  80. memory_size = xtimes (a_allocated, sizeof (argument)); \
  81. if (size_overflow_p (memory_size)) \
  82. /* Overflow, would lead to out of memory. */ \
  83. goto error; \
  84. memory = (a->arg \
  85. ? realloc (a->arg, memory_size) \
  86. : malloc (memory_size)); \
  87. if (memory == NULL) \
  88. /* Out of memory. */ \
  89. goto error; \
  90. a->arg = memory; \
  91. } \
  92. while (a->count <= n) \
  93. a->arg[a->count++].type = TYPE_NONE; \
  94. if (a->arg[n].type == TYPE_NONE) \
  95. a->arg[n].type = (_type_); \
  96. else if (a->arg[n].type != (_type_)) \
  97. /* Ambiguous type for positional argument. */ \
  98. goto error; \
  99. }
  100. while (*cp != '\0')
  101. {
  102. CHAR_T c = *cp++;
  103. if (c == '%')
  104. {
  105. size_t arg_index = ARG_NONE;
  106. DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
  107. /* Initialize the next directive. */
  108. dp->dir_start = cp - 1;
  109. dp->flags = 0;
  110. dp->width_start = NULL;
  111. dp->width_end = NULL;
  112. dp->width_arg_index = ARG_NONE;
  113. dp->precision_start = NULL;
  114. dp->precision_end = NULL;
  115. dp->precision_arg_index = ARG_NONE;
  116. dp->arg_index = ARG_NONE;
  117. /* Test for positional argument. */
  118. if (*cp >= '0' && *cp <= '9')
  119. {
  120. const CHAR_T *np;
  121. for (np = cp; *np >= '0' && *np <= '9'; np++)
  122. ;
  123. if (*np == '$')
  124. {
  125. size_t n = 0;
  126. for (np = cp; *np >= '0' && *np <= '9'; np++)
  127. n = xsum (xtimes (n, 10), *np - '0');
  128. if (n == 0)
  129. /* Positional argument 0. */
  130. goto error;
  131. if (size_overflow_p (n))
  132. /* n too large, would lead to out of memory later. */
  133. goto error;
  134. arg_index = n - 1;
  135. cp = np + 1;
  136. }
  137. }
  138. /* Read the flags. */
  139. for (;;)
  140. {
  141. if (*cp == '\'')
  142. {
  143. dp->flags |= FLAG_GROUP;
  144. cp++;
  145. }
  146. else if (*cp == '-')
  147. {
  148. dp->flags |= FLAG_LEFT;
  149. cp++;
  150. }
  151. else if (*cp == '+')
  152. {
  153. dp->flags |= FLAG_SHOWSIGN;
  154. cp++;
  155. }
  156. else if (*cp == ' ')
  157. {
  158. dp->flags |= FLAG_SPACE;
  159. cp++;
  160. }
  161. else if (*cp == '#')
  162. {
  163. dp->flags |= FLAG_ALT;
  164. cp++;
  165. }
  166. else if (*cp == '0')
  167. {
  168. dp->flags |= FLAG_ZERO;
  169. cp++;
  170. }
  171. else
  172. break;
  173. }
  174. /* Parse the field width. */
  175. if (*cp == '*')
  176. {
  177. dp->width_start = cp;
  178. cp++;
  179. dp->width_end = cp;
  180. if (max_width_length < 1)
  181. max_width_length = 1;
  182. /* Test for positional argument. */
  183. if (*cp >= '0' && *cp <= '9')
  184. {
  185. const CHAR_T *np;
  186. for (np = cp; *np >= '0' && *np <= '9'; np++)
  187. ;
  188. if (*np == '$')
  189. {
  190. size_t n = 0;
  191. for (np = cp; *np >= '0' && *np <= '9'; np++)
  192. n = xsum (xtimes (n, 10), *np - '0');
  193. if (n == 0)
  194. /* Positional argument 0. */
  195. goto error;
  196. if (size_overflow_p (n))
  197. /* n too large, would lead to out of memory later. */
  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. n = xsum (xtimes (n, 10), *np - '0');
  245. if (n == 0)
  246. /* Positional argument 0. */
  247. goto error;
  248. if (size_overflow_p (n))
  249. /* n too large, would lead to out of memory
  250. later. */
  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. size_t memory_size;
  469. DIRECTIVE *memory;
  470. d_allocated = xtimes (d_allocated, 2);
  471. memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
  472. if (size_overflow_p (memory_size))
  473. /* Overflow, would lead to out of memory. */
  474. goto error;
  475. memory = realloc (d->dir, memory_size);
  476. if (memory == NULL)
  477. /* Out of memory. */
  478. goto error;
  479. d->dir = memory;
  480. }
  481. }
  482. }
  483. d->dir[d->count].dir_start = cp;
  484. d->max_width_length = max_width_length;
  485. d->max_precision_length = max_precision_length;
  486. return 0;
  487. error:
  488. if (a->arg)
  489. free (a->arg);
  490. if (d->dir)
  491. free (d->dir);
  492. return -1;
  493. }
  494. #undef DIRECTIVES
  495. #undef DIRECTIVE
  496. #undef CHAR_T
  497. #undef PRINTF_PARSE