argp-fmtstream.c 13 KB

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