argp-fmtstream.c 13 KB

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