123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <stdarg.h>
- #include <ctype.h>
- #include "argp-fmtstream.h"
- #ifndef ARGP_FMTSTREAM_USE_LINEWRAP
- #ifndef isblank
- #define isblank(ch) ((ch)==' ' || (ch)=='\t')
- #endif
- #if defined _LIBC && defined USE_IN_LIBIO
- # include <wchar.h>
- # include <libio/libioP.h>
- # define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
- #else
- # define __vsnprintf(s, l, f, a) vsnprintf (s, l, f, a)
- #endif
- #define INIT_BUF_SIZE 200
- #define PRINTF_SIZE_GUESS 150
- argp_fmtstream_t
- __argp_make_fmtstream (FILE *stream,
- size_t lmargin, size_t rmargin, ssize_t wmargin)
- {
- argp_fmtstream_t fs;
- fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
- if (fs != NULL)
- {
- fs->stream = stream;
- fs->lmargin = lmargin;
- fs->rmargin = rmargin;
- fs->wmargin = wmargin;
- fs->point_col = 0;
- fs->point_offs = 0;
- fs->buf = (char *) malloc (INIT_BUF_SIZE);
- if (! fs->buf)
- {
- free (fs);
- fs = 0;
- }
- else
- {
- fs->p = fs->buf;
- fs->end = fs->buf + INIT_BUF_SIZE;
- }
- }
- return fs;
- }
- #if 0
- #ifdef weak_alias
- weak_alias (__argp_make_fmtstream, argp_make_fmtstream)
- #endif
- #endif
- void
- __argp_fmtstream_free (argp_fmtstream_t fs)
- {
- __argp_fmtstream_update (fs);
- if (fs->p > fs->buf)
- {
- #ifdef USE_IN_LIBIO
- __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
- #else
- fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
- #endif
- }
- free (fs->buf);
- free (fs);
- }
- #if 0
- #ifdef weak_alias
- weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
- #endif
- #endif
- void
- __argp_fmtstream_update (argp_fmtstream_t fs)
- {
- char *buf, *nl;
- size_t len;
-
- buf = fs->buf + fs->point_offs;
- while (buf < fs->p)
- {
- size_t r;
- if (fs->point_col == 0 && fs->lmargin != 0)
- {
-
- const size_t pad = fs->lmargin;
- if (fs->p + pad < fs->end)
- {
-
- memmove (buf + pad, buf, fs->p - buf);
- fs->p += pad;
- memset (buf, ' ', pad);
- buf += pad;
- }
- else
- {
-
- size_t i;
- for (i = 0; i < pad; i++)
- {
- #ifdef USE_IN_LIBIO
- if (_IO_fwide (fs->stream, 0) > 0)
- putwc_unlocked (L' ', fs->stream);
- else
- #endif
- putc_unlocked (' ', fs->stream);
- }
- }
- fs->point_col = pad;
- }
- len = fs->p - buf;
- nl = memchr (buf, '\n', len);
- if (fs->point_col < 0)
- fs->point_col = 0;
- if (!nl)
- {
-
- if (fs->point_col + len < fs->rmargin)
- {
-
- fs->point_col += len;
- break;
- }
- else
-
- nl = fs->p;
- }
- else if (fs->point_col + (nl - buf) < (ssize_t) fs->rmargin)
- {
-
- fs->point_col = 0;
- buf = nl + 1;
- continue;
- }
-
- r = fs->rmargin - 1;
- if (fs->wmargin < 0)
- {
-
- if (nl < fs->p)
- {
- memmove (buf + (r - fs->point_col), nl, fs->p - nl);
- fs->p -= buf + (r - fs->point_col) - nl;
-
- fs->point_col = 0;
- buf += r + 1;
- }
- else
- {
-
- fs->point_col += len;
- fs->p -= fs->point_col - r;
- break;
- }
- }
- else
- {
-
- char *p, *nextline;
- int i;
- p = buf + (r + 1 - fs->point_col);
- while (p >= buf && !isblank (*p))
- --p;
- nextline = p + 1;
- if (nextline > buf)
- {
-
- if (p >= buf)
- do
- --p;
- while (p >= buf && isblank (*p));
- nl = p + 1;
- }
- else
- {
-
- p = buf + (r + 1 - fs->point_col);
-
- do
- ++p;
- while (p < nl && !isblank (*p));
- if (p == nl)
- {
-
- fs->point_col = 0;
- buf = nl + 1;
- continue;
- }
-
- nl = p;
-
- do
- ++p;
- while (isblank (*p));
-
- nextline = p;
- }
-
- if ((nextline == buf + len + 1
- ? fs->end - nl < fs->wmargin + 1
- : nextline - (nl + 1) < fs->wmargin)
- && fs->p > nextline)
- {
-
- if (fs->end - fs->p > fs->wmargin + 1)
-
- {
- size_t mv = fs->p - nextline;
- memmove (nl + 1 + fs->wmargin, nextline, mv);
- nextline = nl + 1 + fs->wmargin;
- len = nextline + mv - buf;
- *nl++ = '\n';
- }
- else
-
- {
- #if defined _LIBC && defined USE_IN_LIBIO
- __fxprintf (fs->stream, "%.*s\n",
- (int) (nl - fs->buf), fs->buf);
- #else
- if (nl > fs->buf)
- fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
- putc_unlocked ('\n', fs->stream);
- #endif
- len += buf - fs->buf;
- nl = buf = fs->buf;
- }
- }
- else
-
- *nl++ = '\n';
- if (nextline - nl >= fs->wmargin
- || (nextline == buf + len + 1 && fs->end - nextline >= fs->wmargin))
-
- for (i = 0; i < fs->wmargin; ++i)
- *nl++ = ' ';
- else
- for (i = 0; i < fs->wmargin; ++i)
- #ifdef USE_IN_LIBIO
- if (_IO_fwide (fs->stream, 0) > 0)
- putwc_unlocked (L' ', fs->stream);
- else
- #endif
- putc_unlocked (' ', fs->stream);
-
- if (nl < nextline)
- memmove (nl, nextline, buf + len - nextline);
- len -= nextline - buf;
-
- buf = nl;
-
- fs->p = nl + len;
-
- fs->point_col = fs->wmargin ? fs->wmargin : -1;
- }
- }
-
- fs->point_offs = fs->p - fs->buf;
- }
- int
- __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
- {
- if ((size_t) (fs->end - fs->p) < amount)
- {
- ssize_t wrote;
-
- __argp_fmtstream_update (fs);
- #if defined _LIBC && defined USE_IN_LIBIO
- __fxprintf (fs->stream, "%.*s", (int) (fs->p - fs->buf), fs->buf);
- wrote = fs->p - fs->buf;
- #else
- wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
- #endif
- if (wrote == fs->p - fs->buf)
- {
- fs->p = fs->buf;
- fs->point_offs = 0;
- }
- else
- {
- fs->p -= wrote;
- fs->point_offs -= wrote;
- memmove (fs->buf, fs->buf + wrote, fs->p - fs->buf);
- return 0;
- }
- if ((size_t) (fs->end - fs->buf) < amount)
-
- {
- size_t old_size = fs->end - fs->buf;
- size_t new_size = old_size + amount;
- char *new_buf;
- if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
- {
- __set_errno (ENOMEM);
- return 0;
- }
- fs->buf = new_buf;
- fs->end = new_buf + new_size;
- fs->p = fs->buf;
- }
- }
- return 1;
- }
- ssize_t
- __argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
- {
- int out;
- size_t avail;
- size_t size_guess = PRINTF_SIZE_GUESS;
- do
- {
- va_list args;
- if (! __argp_fmtstream_ensure (fs, size_guess))
- return -1;
- va_start (args, fmt);
- avail = fs->end - fs->p;
- out = __vsnprintf (fs->p, avail, fmt, args);
- va_end (args);
- if ((size_t) out >= avail)
- size_guess = out + 1;
- }
- while ((size_t) out >= avail);
- fs->p += out;
- return out;
- }
- #if 0
- #ifdef weak_alias
- weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
- #endif
- #endif
- #endif
|