2 * Copyright (c) 2007-2010 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 */
35 #endif /* HAVE_STRING_H */
38 #endif /* HAVE_STRINGS_H */
41 #endif /* HAVE_UNISTD_H */
54 #if !defined(TIOCGSIZE) && defined(TIOCGWINSZ)
55 # define TIOCGSIZE TIOCGWINSZ
56 # define ttysize winsize
57 # define ts_cols ws_col
68 if (ioctl(STDERR_FILENO, TIOCGSIZE, &win) == 0 && win.ts_cols != 0)
69 return((int)win.ts_cols);
72 /* Fall back on $COLUMNS. */
73 if ((p = getenv("COLUMNS")) == NULL || (cols = atoi(p)) <= 0)
79 lbuf_init(lbuf, output, indent, continuation)
81 int (*output)__P((const char *));
83 const char *continuation;
85 lbuf->output = output;
86 lbuf->continuation = continuation;
87 lbuf->indent = indent;
88 lbuf->cols = get_ttycols();
103 * Append strings to the buffer, expanding it as needed.
107 lbuf_append_quoted(struct lbuf *lbuf, const char *set, ...)
109 lbuf_append_quoted(lbuf, set, va_alist)
124 while ((s = va_arg(ap, char *)) != NULL) {
126 for (cp = s; (cp = strpbrk(cp, set)) != NULL; cp++)
131 /* Expand buffer as needed. */
132 if (lbuf->len + len >= lbuf->size) {
135 } while (lbuf->len + len >= lbuf->size);
136 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
144 /* Append each string. */
145 while ((s = va_arg(ap, char *)) != NULL) {
146 while ((cp = strpbrk(s, set)) != NULL) {
148 memcpy(lbuf->buf + lbuf->len, s, len);
150 lbuf->buf[lbuf->len++] = '\\';
151 lbuf->buf[lbuf->len++] = *cp;
156 memcpy(lbuf->buf + lbuf->len, s, len);
160 lbuf->buf[lbuf->len] = '\0';
165 * Append strings to the buffer, expanding it as needed.
169 lbuf_append(struct lbuf *lbuf, ...)
171 lbuf_append(lbuf, va_alist)
185 while ((s = va_arg(ap, char *)) != NULL)
189 /* Expand buffer as needed. */
190 if (lbuf->len + len >= lbuf->size) {
193 } while (lbuf->len + len >= lbuf->size);
194 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
202 /* Append each string. */
203 while ((s = va_arg(ap, char *)) != NULL) {
205 memcpy(lbuf->buf + lbuf->len, s, len);
208 lbuf->buf[lbuf->len] = '\0';
213 lbuf_println(lbuf, line, len)
219 int i, have, contlen;
221 contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
224 * Print the buffer, splitting the line as needed on a word
229 while (cp != NULL && *cp != '\0') {
231 int need = len - (int)(cp - line);
234 have -= contlen; /* subtract for continuation char */
235 if ((ep = memrchr(cp, ' ', have)) == NULL)
236 ep = memchr(cp + have, ' ', need - have);
238 need = (int)(ep - cp);
241 /* indent continued lines */
242 /* XXX - build up string instead? */
243 for (i = 0; i < lbuf->indent; i++)
246 /* NUL-terminate cp for the output function and restore afterwards */
254 * If there is more to print, reset have, incremement cp past
255 * the whitespace, and print a line continuaton char if needed.
258 have = lbuf->cols - lbuf->indent;
260 while (cp < ep && isblank((unsigned char)*cp)) {
264 lbuf->output(lbuf->continuation);
271 * Print the buffer with word wrap based on the tty width.
272 * The lbuf is reset on return.
281 contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
283 /* For very small widths just give up... */
284 if (lbuf->cols <= lbuf->indent + contlen + 20) {
285 lbuf->output(lbuf->buf);
290 /* Print each line in the buffer */
291 for (cp = lbuf->buf; cp != NULL && *cp != '\0'; ) {
296 ep = memchr(cp, '\n', lbuf->len - (cp - lbuf->buf));
297 len = ep ? (int)(ep - cp) : lbuf->len;
298 lbuf_println(lbuf, cp, len);
299 cp = ep ? ep + 1 : NULL;
304 lbuf->len = 0; /* reset the buffer for re-use. */