argp-fmtstream.c 14 KB

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