argp-fmtstream.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* Word-wrapping and line-truncating streams
  2. Copyright (C) 1997-1999,2001,2002,2003,2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Miles Bader <miles at gnu.ai.mit.edu>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA.
  17. Modified for uClibc by: Salvatore Cro <salvatore.cro at st.com>
  18. */
  19. /* This package emulates glibc `line_wrap_stream' semantics for systems that
  20. don't have that. */
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <stdarg.h>
  28. #include <ctype.h>
  29. #include "argp-fmtstream.h"
  30. #ifndef ARGP_FMTSTREAM_USE_LINEWRAP
  31. #ifndef isblank
  32. #define isblank(ch) ((ch)==' ' || (ch)=='\t')
  33. #endif
  34. #if defined _LIBC && defined USE_IN_LIBIO
  35. # include <wchar.h>
  36. # include <libio/libioP.h>
  37. # define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
  38. #else
  39. # define __vsnprintf(s, l, f, a) vsnprintf (s, l, f, a)
  40. #endif
  41. #define INIT_BUF_SIZE 200
  42. #define PRINTF_SIZE_GUESS 150
  43. /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
  44. written on it with LMARGIN spaces and limits them to RMARGIN columns
  45. total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
  46. replacing the whitespace before them with a newline and WMARGIN spaces.
  47. Otherwise, chars beyond RMARGIN are simply dropped until a newline.
  48. Returns NULL if there was an error. */
  49. argp_fmtstream_t
  50. __argp_make_fmtstream (FILE *stream,
  51. size_t lmargin, size_t rmargin, ssize_t wmargin)
  52. {
  53. argp_fmtstream_t fs;
  54. fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
  55. if (fs != NULL)
  56. {
  57. fs->stream = stream;
  58. fs->lmargin = lmargin;
  59. fs->rmargin = rmargin;
  60. fs->wmargin = wmargin;
  61. fs->point_col = 0;
  62. fs->point_offs = 0;
  63. fs->buf = (char *) malloc (INIT_BUF_SIZE);
  64. if (! fs->buf)
  65. {
  66. free (fs);
  67. fs = 0;
  68. }
  69. else
  70. {
  71. fs->p = fs->buf;
  72. fs->end = fs->buf + INIT_BUF_SIZE;
  73. }
  74. }
  75. return fs;
  76. }
  77. #if 0
  78. /* Not exported. */
  79. #ifdef weak_alias
  80. weak_alias (__argp_make_fmtstream, argp_make_fmtstream)
  81. #endif
  82. #endif
  83. /* Flush FS to its stream, and free it (but don't close the stream). */
  84. void
  85. __argp_fmtstream_free (argp_fmtstream_t fs)
  86. {
  87. __argp_fmtstream_update (fs);
  88. if (fs->p > fs->buf)
  89. {
  90. #ifdef USE_IN_LIBIO
  91. __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
  92. #else
  93. fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
  94. #endif
  95. }
  96. free (fs->buf);
  97. free (fs);
  98. }
  99. #if 0
  100. /* Not exported. */
  101. #ifdef weak_alias
  102. weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
  103. #endif
  104. #endif
  105. /* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
  106. end of its buffer. This code is mostly from glibc stdio/linewrap.c. */
  107. void
  108. __argp_fmtstream_update (argp_fmtstream_t fs)
  109. {
  110. char *buf, *nl;
  111. size_t len;
  112. /* Scan the buffer for newlines. */
  113. buf = fs->buf + fs->point_offs;
  114. while (buf < fs->p)
  115. {
  116. size_t r;
  117. if (fs->point_col == 0 && fs->lmargin != 0)
  118. {
  119. /* We are starting a new line. Print spaces to the left margin. */
  120. const size_t pad = fs->lmargin;
  121. if (fs->p + pad < fs->end)
  122. {
  123. /* We can fit in them in the buffer by moving the
  124. buffer text up and filling in the beginning. */
  125. memmove (buf + pad, buf, fs->p - buf);
  126. fs->p += pad; /* Compensate for bigger buffer. */
  127. memset (buf, ' ', pad); /* Fill in the spaces. */
  128. buf += pad; /* Don't bother searching them. */
  129. }
  130. else
  131. {
  132. /* No buffer space for spaces. Must flush. */
  133. size_t i;
  134. for (i = 0; i < pad; i++)
  135. {
  136. #ifdef USE_IN_LIBIO
  137. if (_IO_fwide (fs->stream, 0) > 0)
  138. putwc_unlocked (L' ', fs->stream);
  139. else
  140. #endif
  141. putc_unlocked (' ', fs->stream);
  142. }
  143. }
  144. fs->point_col = pad;
  145. }
  146. len = fs->p - buf;
  147. nl = memchr (buf, '\n', len);
  148. if (fs->point_col < 0)
  149. fs->point_col = 0;
  150. if (!nl)
  151. {
  152. /* The buffer ends in a partial line. */
  153. if (fs->point_col + len < fs->rmargin)
  154. {
  155. /* The remaining buffer text is a partial line and fits
  156. within the maximum line width. Advance point for the
  157. characters to be written and stop scanning. */
  158. fs->point_col += len;
  159. break;
  160. }
  161. else
  162. /* Set the end-of-line pointer for the code below to
  163. the end of the buffer. */
  164. nl = fs->p;
  165. }
  166. else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin)
  167. {
  168. /* The buffer contains a full line that fits within the maximum
  169. line width. Reset point and scan the next line. */
  170. fs->point_col = 0;
  171. buf = nl + 1;
  172. continue;
  173. }
  174. /* This line is too long. */
  175. r = fs->rmargin - 1;
  176. if (fs->wmargin < 0)
  177. {
  178. /* Truncate the line by overwriting the excess with the
  179. newline and anything after it in the buffer. */
  180. if (nl < fs->p)
  181. {
  182. memmove (buf + (r - fs->point_col), nl, fs->p - nl);
  183. fs->p -= buf + (r - fs->point_col) - nl;
  184. /* Reset point for the next line and start scanning it. */
  185. fs->point_col = 0;
  186. buf += r + 1; /* Skip full line plus \n. */
  187. }
  188. else
  189. {
  190. /* The buffer ends with a partial line that is beyond the
  191. maximum line width. Advance point for the characters
  192. written, and discard those past the max from the buffer. */
  193. fs->point_col += len;
  194. fs->p -= fs->point_col - r;
  195. break;
  196. }
  197. }
  198. else
  199. {
  200. /* Do word wrap. Go to the column just past the maximum line
  201. width and scan back for the beginning of the word there.
  202. Then insert a line break. */
  203. char *p, *nextline;
  204. int i;
  205. p = buf + (r + 1 - fs->point_col);
  206. while (p >= buf && !isblank (*p))
  207. --p;
  208. nextline = p + 1; /* This will begin the next line. */
  209. if (nextline > buf)
  210. {
  211. /* Swallow separating blanks. */
  212. if (p >= buf)
  213. do
  214. --p;
  215. while (p >= buf && isblank (*p));
  216. nl = p + 1; /* The newline will replace the first blank. */
  217. }
  218. else
  219. {
  220. /* A single word that is greater than the maximum line width.
  221. Oh well. Put it on an overlong line by itself. */
  222. p = buf + (r + 1 - fs->point_col);
  223. /* Find the end of the long word. */
  224. do
  225. ++p;
  226. while (p < nl && !isblank (*p));
  227. if (p == nl)
  228. {
  229. /* It already ends a line. No fussing required. */
  230. fs->point_col = 0;
  231. buf = nl + 1;
  232. continue;
  233. }
  234. /* We will move the newline to replace the first blank. */
  235. nl = p;
  236. /* Swallow separating blanks. */
  237. do
  238. ++p;
  239. while (isblank (*p));
  240. /* The next line will start here. */
  241. nextline = p;
  242. }
  243. /* Note: There are a bunch of tests below for
  244. NEXTLINE == BUF + LEN + 1; this case is where NL happens to fall
  245. at the end of the buffer, and NEXTLINE is in fact empty (and so
  246. we need not be careful to maintain its contents). */
  247. if ((nextline == buf + len + 1
  248. ? fs->end - nl < fs->wmargin + 1
  249. : nextline - (nl + 1) < fs->wmargin)
  250. && fs->p > nextline)
  251. {
  252. /* The margin needs more blanks than we removed. */
  253. if (fs->end - fs->p > fs->wmargin + 1)
  254. /* Make some space for them. */
  255. {
  256. size_t mv = fs->p - nextline;
  257. memmove (nl + 1 + fs->wmargin, nextline, mv);
  258. nextline = nl + 1 + fs->wmargin;
  259. len = nextline + mv - buf;
  260. *nl++ = '\n';
  261. }
  262. else
  263. /* Output the first line so we can use the space. */
  264. {
  265. #if defined _LIBC && defined USE_IN_LIBIO
  266. __fxprintf (fs->stream, "%.*s\n",
  267. (int) (nl - fs->buf), fs->buf);
  268. #else
  269. if (nl > fs->buf)
  270. fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
  271. putc_unlocked ('\n', fs->stream);
  272. #endif
  273. len += buf - fs->buf;
  274. nl = buf = fs->buf;
  275. }
  276. }
  277. else
  278. /* We can fit the newline and blanks in before
  279. the next word. */
  280. *nl++ = '\n';
  281. if (nextline - nl >= fs->wmargin
  282. || (nextline == buf + len + 1 && fs->end - nextline >= fs->wmargin))
  283. /* Add blanks up to the wrap margin column. */
  284. for (i = 0; i < fs->wmargin; ++i)
  285. *nl++ = ' ';
  286. else
  287. for (i = 0; i < fs->wmargin; ++i)
  288. #ifdef USE_IN_LIBIO
  289. if (_IO_fwide (fs->stream, 0) > 0)
  290. putwc_unlocked (L' ', fs->stream);
  291. else
  292. #endif
  293. putc_unlocked (' ', fs->stream);
  294. /* Copy the tail of the original buffer into the current buffer
  295. position. */
  296. if (nl < nextline)
  297. memmove (nl, nextline, buf + len - nextline);
  298. len -= nextline - buf;
  299. /* Continue the scan on the remaining lines in the buffer. */
  300. buf = nl;
  301. /* Restore bufp to include all the remaining text. */
  302. fs->p = nl + len;
  303. /* Reset the counter of what has been output this line. If wmargin
  304. is 0, we want to avoid the lmargin getting added, so we set
  305. point_col to a magic value of -1 in that case. */
  306. fs->point_col = fs->wmargin ? fs->wmargin : -1;
  307. }
  308. }
  309. /* Remember that we've scanned as far as the end of the buffer. */
  310. fs->point_offs = fs->p - fs->buf;
  311. }
  312. /* Ensure that FS has space for AMOUNT more bytes in its buffer, either by
  313. growing the buffer, or by flushing it. True is returned iff we succeed. */
  314. int
  315. __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
  316. {
  317. if ((size_t) (fs->end - fs->p) < amount)
  318. {
  319. ssize_t wrote;
  320. /* Flush FS's buffer. */
  321. __argp_fmtstream_update (fs);
  322. #if defined _LIBC && defined USE_IN_LIBIO
  323. __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
  324. wrote = fs->p - fs->buf;
  325. #else
  326. wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
  327. #endif
  328. if (wrote == fs->p - fs->buf)
  329. {
  330. fs->p = fs->buf;
  331. fs->point_offs = 0;
  332. }
  333. else
  334. {
  335. fs->p -= wrote;
  336. fs->point_offs -= wrote;
  337. memmove (fs->buf, fs->buf + wrote, fs->p - fs->buf);
  338. return 0;
  339. }
  340. if ((size_t) (fs->end - fs->buf) < amount)
  341. /* Gotta grow the buffer. */
  342. {
  343. size_t old_size = fs->end - fs->buf;
  344. size_t new_size = old_size + amount;
  345. char *new_buf;
  346. if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
  347. {
  348. __set_errno (ENOMEM);
  349. return 0;
  350. }
  351. fs->buf = new_buf;
  352. fs->end = new_buf + new_size;
  353. fs->p = fs->buf;
  354. }
  355. }
  356. return 1;
  357. }
  358. ssize_t
  359. __argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
  360. {
  361. int out;
  362. size_t avail;
  363. size_t size_guess = PRINTF_SIZE_GUESS; /* How much space to reserve. */
  364. do
  365. {
  366. va_list args;
  367. if (! __argp_fmtstream_ensure (fs, size_guess))
  368. return -1;
  369. va_start (args, fmt);
  370. avail = fs->end - fs->p;
  371. out = __vsnprintf (fs->p, avail, fmt, args);
  372. va_end (args);
  373. if ((size_t) out >= avail)
  374. size_guess = out + 1;
  375. }
  376. while ((size_t) out >= avail);
  377. fs->p += out;
  378. return out;
  379. }
  380. #if 0
  381. /* Not exported. */
  382. #ifdef weak_alias
  383. weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
  384. #endif
  385. #endif
  386. #endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */