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