argp-fmtstream.c 11 KB

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