uclibc-0030-stdio-implement-assignment-allocation-m-character.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. From 050cd6971f92c2337bc506043dfcf1395dd5d622 Mon Sep 17 00:00:00 2001
  2. From: Mike Frysinger <vapier@gentoo.org>
  3. Date: Sun, 6 May 2012 03:50:44 -0400
  4. Subject: [PATCH] stdio: implement assignment-allocation "m" character
  5. The latest POSIX spec introduces a "m" character to allocate buffers for
  6. the user when using scanf type functions. This is like the old glibc "a"
  7. flag, but now standardized. With packages starting to use these, we need
  8. to implement it.
  9. for example:
  10. char *s;
  11. sscanf("foo", "%ms", &s);
  12. printf("%s\n", s);
  13. free(s);
  14. This will automatically allocate storage for "s", read in "foo" to it,
  15. and then display it.
  16. I'm not terribly familiar with the stdio layer, so this could be wrong.
  17. But it seems to work for me.
  18. Signed-off-by: Mike Frysinger <vapier@gentoo.org>
  19. ---
  20. extra/Configs/Config.in | 13 ---------
  21. libc/stdio/_scanf.c | 68 +++++++++++++++++++++++++++--------------------
  22. 2 files changed, 39 insertions(+), 42 deletions(-)
  23. diff --git a/extra/Configs/Config.in b/extra/Configs/Config.in
  24. index 1060729..c2f2fc7 100644
  25. --- a/extra/Configs/Config.in
  26. +++ b/extra/Configs/Config.in
  27. @@ -1590,19 +1590,6 @@ config UCLIBC_PRINTF_SCANF_POSITIONAL_ARGS
  28. Most people will answer 9.
  29. -
  30. -config UCLIBC_HAS_SCANF_GLIBC_A_FLAG
  31. - bool "Support glibc's 'a' flag for scanf string conversions (not implemented)"
  32. - help
  33. - NOTE!!! Currently Not Implemented!!! Just A Place Holder!! NOTE!!!
  34. - NOTE!!! Conflicts with an ANSI/ISO C99 scanf flag!! NOTE!!!
  35. -
  36. - Answer Y to enable support for glibc's 'a' flag for the scanf string
  37. - conversions '%s', '%[', '%ls', '%l[', and '%S'. This is used to
  38. - auto-allocate sufficient memory to hold the data retrieved.
  39. -
  40. - Most people will answer N.
  41. -
  42. choice
  43. prompt "Stdio buffer size"
  44. default UCLIBC_HAS_STDIO_BUFSIZ_4096
  45. diff --git a/libc/stdio/_scanf.c b/libc/stdio/_scanf.c
  46. index f38e72b..952853c 100644
  47. --- a/libc/stdio/_scanf.c
  48. +++ b/libc/stdio/_scanf.c
  49. @@ -77,14 +77,6 @@
  50. #include <bits/uClibc_fpmax.h>
  51. #endif /* __UCLIBC_HAS_FLOATS__ */
  52. -#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  53. -#ifdef L_vfscanf
  54. -/* only emit this once */
  55. -#warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
  56. -#endif
  57. -#undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  58. -#endif
  59. -
  60. #undef __STDIO_HAS_VSSCANF
  61. #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC_HAS_WCHAR__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  62. #define __STDIO_HAS_VSSCANF 1
  63. @@ -433,8 +425,9 @@ libc_hidden_def(vswscanf)
  64. /* float layout 0123456789012345678901 repeat n for "l[" */
  65. -#define SPEC_CHARS "npxXoudifFeEgGaACSncs["
  66. -/* npxXoudif eEgG CS cs[ */
  67. +#define SPEC_CHARS "npxXoudifFeEgGaACSnmcs["
  68. +/* npxXoudif eEgG CS cs[ */
  69. +/* NOTE: the 'm' flag must come before any convs that support it */
  70. /* NOTE: Ordering is important! In particular, CONV_LEFTBRACKET
  71. * must immediately precede CONV_c. */
  72. @@ -444,7 +437,7 @@ enum {
  73. CONV_p,
  74. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  75. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  76. - CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
  77. + CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_m, CONV_c, CONV_s, CONV_leftbracket,
  78. CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
  79. };
  80. @@ -474,7 +467,7 @@ enum {
  81. FLAG_SURPRESS = 0x10, /* MUST BE 1ST!! See DO_FLAGS. */
  82. FLAG_THOUSANDS = 0x20,
  83. FLAG_I18N = 0x40, /* only works for d, i, u */
  84. - FLAG_MALLOC = 0x80, /* only works for s, S, and [ (and l[)*/
  85. + FLAG_MALLOC = 0x80, /* only works for c, s, S, and [ (and l[)*/
  86. };
  87. @@ -491,7 +484,7 @@ enum {
  88. /* fFeEgGaA */ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
  89. /* C */ ( 0|FLAG_SURPRESS), \
  90. /* S and l[ */ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
  91. - /* c */ (0x04|FLAG_SURPRESS), \
  92. + /* c */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
  93. /* s and [ */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
  94. }
  95. @@ -904,17 +897,17 @@ int attribute_hidden __psfs_parse_spec(register psfs_t *psfs)
  96. if (*psfs->fmt == *p) {
  97. int p_m_spec_chars = p - spec_chars;
  98. -#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  99. -#error implement gnu a flag
  100. - if ((*p == 'a')
  101. - && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
  102. - ) { /* Assumes ascii for 's' and 'S' test. */
  103. - psfs->flags |= FLAG_MALLOC;
  104. + if (*p == 'm' &&
  105. + (psfs->fmt[1] == '[' || psfs->fmt[1] == 'c' ||
  106. + /* Assumes ascii for 's' and 'S' test. */
  107. + (psfs->fmt[1] | 0x20) == 's'))
  108. + {
  109. + if (psfs->store)
  110. + psfs->flags |= FLAG_MALLOC;
  111. ++psfs->fmt;
  112. ++p;
  113. - continue; /* The related conversions follow 'a'. */
  114. + continue; /* The related conversions follow 'm'. */
  115. }
  116. -#endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  117. for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
  118. if (((psfs->dataargtype >> 8) | psfs->flags)
  119. @@ -1265,12 +1258,6 @@ int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
  120. while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
  121. *b++ = *wf++;
  122. }
  123. -#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  124. -#error this is wrong... we need to ched in __psfs_parse_spec instead since this checks last char in buffer and conversion my have stopped before it.
  125. - if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
  126. - goto DONE; /* Spec was excessively long. */
  127. - }
  128. -#endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  129. *b = 0;
  130. if (b == buf) { /* Bad conversion specifier! */
  131. goto DONE;
  132. @@ -1390,13 +1377,36 @@ int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
  133. }
  134. if (psfs.conv_num == CONV_s) {
  135. + /* We might have to handle the allocation ourselves */
  136. + int len;
  137. + /* With 'm', we actually got a pointer to a pointer */
  138. + unsigned char **ptr = (void *)b;
  139. +
  140. + i = 0;
  141. + if (psfs.flags & FLAG_MALLOC) {
  142. + len = 0;
  143. + b = NULL;
  144. + } else
  145. + len = -1;
  146. +
  147. /* Yes, believe it or not, a %s conversion can store nuls. */
  148. while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
  149. zero_conversions = 0;
  150. - *b = sc.cc;
  151. - b += psfs.store;
  152. + if (i == len) {
  153. + /* Pick a size that won't trigger a lot of
  154. + * mallocs early on ... */
  155. + len += 256;
  156. + b = realloc(b, len + 1);
  157. + }
  158. + b[i] = sc.cc;
  159. + i += psfs.store;
  160. fail = 0;
  161. }
  162. +
  163. + if (psfs.flags & FLAG_MALLOC)
  164. + *ptr = b;
  165. + /* The code below takes care of terminating NUL */
  166. + b += i;
  167. } else {
  168. #ifdef __UCLIBC_HAS_WCHAR__
  169. assert((psfs.conv_num == CONV_LEFTBRACKET) || \
  170. --
  171. 1.7.10.4