2 * Copyright (c) 2007-2011 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
16 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 #include <sys/types.h>
22 #include <sys/param.h>
23 #include <sys/ioctl.h>
32 #endif /* STDC_HEADERS */
34 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
38 #endif /* HAVE_STRING_H */
41 #endif /* HAVE_STRINGS_H */
44 #endif /* HAVE_UNISTD_H */
57 /* Compatibility with older tty systems. */
58 #if !defined(TIOCGWINSZ) && defined(TIOCGSIZE)
59 # define TIOCGWINSZ TIOCGSIZE
60 # define winsize ttysize
61 # define ws_col ts_cols
72 if (ioctl(STDERR_FILENO, TIOCGWINSZ, &wsize) == 0 && wsize.ws_col != 0)
73 return (int)wsize.ws_col;
76 /* Fall back on $COLUMNS. */
77 if ((p = getenv("COLUMNS")) == NULL || (cols = atoi(p)) <= 0)
83 lbuf_init(lbuf, output, indent, continuation)
85 int (*output)__P((const char *));
87 const char *continuation;
89 lbuf->output = output;
90 lbuf->continuation = continuation;
91 lbuf->indent = indent;
92 lbuf->cols = get_ttycols();
107 * Append strings to the buffer, expanding it as needed.
111 lbuf_append_quoted(struct lbuf *lbuf, const char *set, ...)
113 lbuf_append_quoted(lbuf, set, va_alist)
128 while ((s = va_arg(ap, char *)) != NULL) {
130 for (cp = s; (cp = strpbrk(cp, set)) != NULL; cp++)
135 /* Expand buffer as needed. */
136 if (lbuf->len + len >= lbuf->size) {
139 } while (lbuf->len + len >= lbuf->size);
140 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
148 /* Append each string. */
149 while ((s = va_arg(ap, char *)) != NULL) {
150 while ((cp = strpbrk(s, set)) != NULL) {
152 memcpy(lbuf->buf + lbuf->len, s, len);
154 lbuf->buf[lbuf->len++] = '\\';
155 lbuf->buf[lbuf->len++] = *cp;
160 memcpy(lbuf->buf + lbuf->len, s, len);
164 lbuf->buf[lbuf->len] = '\0';
169 * Append strings to the buffer, expanding it as needed.
173 lbuf_append(struct lbuf *lbuf, ...)
175 lbuf_append(lbuf, va_alist)
189 while ((s = va_arg(ap, char *)) != NULL)
193 /* Expand buffer as needed. */
194 if (lbuf->len + len >= lbuf->size) {
197 } while (lbuf->len + len >= lbuf->size);
198 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
206 /* Append each string. */
207 while ((s = va_arg(ap, char *)) != NULL) {
209 memcpy(lbuf->buf + lbuf->len, s, len);
212 lbuf->buf[lbuf->len] = '\0';
217 lbuf_println(lbuf, line, len)
223 int i, have, contlen;
225 contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
228 * Print the buffer, splitting the line as needed on a word
233 while (cp != NULL && *cp != '\0') {
235 int need = len - (int)(cp - line);
238 have -= contlen; /* subtract for continuation char */
239 if ((ep = memrchr(cp, ' ', have)) == NULL)
240 ep = memchr(cp + have, ' ', need - have);
242 need = (int)(ep - cp);
245 /* indent continued lines */
246 /* XXX - build up string instead? */
247 for (i = 0; i < lbuf->indent; i++)
250 /* NUL-terminate cp for the output function and restore afterwards */
258 * If there is more to print, reset have, incremement cp past
259 * the whitespace, and print a line continuaton char if needed.
262 have = lbuf->cols - lbuf->indent;
264 while (cp < ep && isblank((unsigned char)*cp)) {
268 lbuf->output(lbuf->continuation);
275 * Print the buffer with word wrap based on the tty width.
276 * The lbuf is reset on return.
285 if (lbuf->buf == NULL || lbuf->len == 0)
288 /* For very small widths just give up... */
289 len = lbuf->continuation ? strlen(lbuf->continuation) : 0;
290 if (lbuf->cols <= lbuf->indent + len + 20) {
291 lbuf->buf[lbuf->len] = '\0';
292 lbuf->output(lbuf->buf);
296 /* Print each line in the buffer */
297 for (cp = lbuf->buf; cp != NULL && *cp != '\0'; ) {
302 len = lbuf->len - (cp - lbuf->buf);
303 if ((ep = memchr(cp, '\n', len)) != NULL)
304 len = (int)(ep - cp);
306 lbuf_println(lbuf, cp, len);
307 cp = ep ? ep + 1 : NULL;
312 lbuf->len = 0; /* reset the buffer for re-use. */