2 * Copyright (c) 2007-2009 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 */
36 # ifdef HAVE_STRINGS_H
39 #endif /* HAVE_STRING_H */
42 #endif /* HAVE_UNISTD_H */
55 #if !defined(TIOCGSIZE) && defined(TIOCGWINSZ)
56 # define TIOCGSIZE TIOCGWINSZ
57 # define ttysize winsize
58 # define ts_cols ws_col
69 if (ioctl(STDERR_FILENO, TIOCGSIZE, &win) == 0 && win.ts_cols != 0)
70 return((int)win.ts_cols);
73 /* Fall back on $COLUMNS. */
74 if ((p = getenv("COLUMNS")) == NULL || (cols = atoi(p)) <= 0)
80 * TODO: add support for embedded newlines in lbufs
84 lbuf_init(lbuf, buf, indent, continuation)
90 lbuf->continuation = continuation;
91 lbuf->indent = indent;
106 * Append strings to the buffer, expanding it as needed.
110 lbuf_append_quoted(struct lbuf *lbuf, const char *set, ...)
112 lbuf_append_quoted(lbuf, set, va_alist)
127 while ((s = va_arg(ap, char *)) != NULL) {
129 for (cp = s; (cp = strpbrk(cp, set)) != NULL; cp++)
134 /* Expand buffer as needed. */
135 if (lbuf->len + len >= lbuf->size) {
138 } while (lbuf->len + len >= lbuf->size);
139 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
147 /* Append each string. */
148 while ((s = va_arg(ap, char *)) != NULL) {
149 while ((cp = strpbrk(s, set)) != NULL) {
151 memcpy(lbuf->buf + lbuf->len, s, len);
153 lbuf->buf[lbuf->len++] = '\\';
154 lbuf->buf[lbuf->len++] = *cp;
159 memcpy(lbuf->buf + lbuf->len, s, len);
163 lbuf->buf[lbuf->len] = '\0';
168 * Append strings to the buffer, expanding it as needed.
172 lbuf_append(struct lbuf *lbuf, ...)
174 lbuf_append(lbuf, va_alist)
188 while ((s = va_arg(ap, char *)) != NULL)
192 /* Expand buffer as needed. */
193 if (lbuf->len + len >= lbuf->size) {
196 } while (lbuf->len + len >= lbuf->size);
197 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
205 /* Append each string. */
206 while ((s = va_arg(ap, char *)) != NULL) {
208 memcpy(lbuf->buf + lbuf->len, s, len);
211 lbuf->buf[lbuf->len] = '\0';
216 * Print the buffer with word wrap based on the tty width.
217 * The lbuf is reset on return.
224 int i, have, contlen;
225 static int cols = -1;
228 cols = get_ttycols();
229 contlen = lbuf->continuation ? 2 : 0;
231 /* For very small widths just give up... */
232 if (cols <= lbuf->indent + contlen + 20) {
238 * Print the buffer, splitting the line as needed on a word
243 while (cp != NULL && *cp != '\0') {
245 int need = lbuf->len - (int)(cp - lbuf->buf);
248 have -= contlen; /* subtract for continuation char */
249 if ((ep = memrchr(cp, ' ', have)) == NULL)
250 ep = memchr(cp + have, ' ', need - have);
252 need = (int)(ep - cp);
254 if (cp != lbuf->buf) {
255 /* indent continued lines */
256 for (i = 0; i < lbuf->indent; i++)
259 fwrite(cp, need, 1, stdout);
263 * If there is more to print, reset have, incremement cp past
264 * the whitespace, and print a line continuaton char if needed.
267 have = cols - lbuf->indent;
270 } while (isspace((unsigned char)*cp));
271 if (lbuf->continuation) {
273 putchar(lbuf->continuation);
280 lbuf->len = 0; /* reset the buffer for re-use. */