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 */
56 __unused static const char rcsid[] = "$Sudo: lbuf.c,v 1.9 2009/05/25 12:02:41 millert Exp $";
59 #if !defined(TIOCGSIZE) && defined(TIOCGWINSZ)
60 # define TIOCGSIZE TIOCGWINSZ
61 # define ttysize winsize
62 # define ts_cols ws_col
73 if (ioctl(STDERR_FILENO, TIOCGSIZE, &win) == 0 && win.ts_cols != 0)
74 return((int)win.ts_cols);
77 /* Fall back on $COLUMNS. */
78 if ((p = getenv("COLUMNS")) == NULL || (cols = atoi(p)) <= 0)
84 * TODO: add support for embedded newlines in lbufs
88 lbuf_init(lbuf, buf, indent, continuation)
94 lbuf->continuation = continuation;
95 lbuf->indent = indent;
110 * Append strings to the buffer, expanding it as needed.
114 lbuf_append_quoted(struct lbuf *lbuf, const char *set, ...)
116 lbuf_append_quoted(lbuf, set, va_alist)
131 while ((s = va_arg(ap, char *)) != NULL) {
133 for (cp = s; (cp = strpbrk(cp, set)) != NULL; cp++)
138 /* Expand buffer as needed. */
139 if (lbuf->len + len >= lbuf->size) {
142 } while (lbuf->len + len >= lbuf->size);
143 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
151 /* Append each string. */
152 while ((s = va_arg(ap, char *)) != NULL) {
153 while ((cp = strpbrk(s, set)) != NULL) {
155 memcpy(lbuf->buf + lbuf->len, s, len);
157 lbuf->buf[lbuf->len++] = '\\';
158 lbuf->buf[lbuf->len++] = *cp;
163 memcpy(lbuf->buf + lbuf->len, s, len);
167 lbuf->buf[lbuf->len] = '\0';
172 * Append strings to the buffer, expanding it as needed.
176 lbuf_append(struct lbuf *lbuf, ...)
178 lbuf_append(lbuf, va_alist)
192 while ((s = va_arg(ap, char *)) != NULL)
196 /* Expand buffer as needed. */
197 if (lbuf->len + len >= lbuf->size) {
200 } while (lbuf->len + len >= lbuf->size);
201 lbuf->buf = erealloc(lbuf->buf, lbuf->size);
209 /* Append each string. */
210 while ((s = va_arg(ap, char *)) != NULL) {
212 memcpy(lbuf->buf + lbuf->len, s, len);
215 lbuf->buf[lbuf->len] = '\0';
220 * Print the buffer with word wrap based on the tty width.
221 * The lbuf is reset on return.
228 int i, have, contlen;
229 static int cols = -1;
232 cols = get_ttycols();
233 contlen = lbuf->continuation ? 2 : 0;
235 /* For very small widths just give up... */
236 if (cols <= lbuf->indent + contlen + 20) {
242 * Print the buffer, splitting the line as needed on a word
247 while (cp != NULL && *cp != '\0') {
249 int need = lbuf->len - (int)(cp - lbuf->buf);
252 have -= contlen; /* subtract for continuation char */
253 if ((ep = memrchr(cp, ' ', have)) == NULL)
254 ep = memchr(cp + have, ' ', need - have);
256 need = (int)(ep - cp);
258 if (cp != lbuf->buf) {
259 /* indent continued lines */
260 for (i = 0; i < lbuf->indent; i++)
263 fwrite(cp, need, 1, stdout);
267 * If there is more to print, reset have, incremement cp past
268 * the whitespace, and print a line continuaton char if needed.
271 have = cols - lbuf->indent;
274 } while (isspace((unsigned char)*cp));
275 if (lbuf->continuation) {
277 putchar(lbuf->continuation);
284 lbuf->len = 0; /* reset the buffer for re-use. */